12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946 |
- /* $OpenBSD: sshkey.c,v 1.111 2020/08/27 01:06:19 djm Exp $ */
- /*
- * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
- * Copyright (c) 2008 Alexander von Gernler. All rights reserved.
- * Copyright (c) 2010,2011 Damien Miller. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "includes.h"
- #include <sys/types.h>
- #include <netinet/in.h>
- #ifdef WITH_OPENSSL
- #include <openssl/evp.h>
- #include <openssl/err.h>
- #include <openssl/pem.h>
- #endif
- #include "crypto_api.h"
- #include <errno.h>
- #include <limits.h>
- #include <stdio.h>
- #include <string.h>
- #include <resolv.h>
- #include <time.h>
- #ifdef HAVE_UTIL_H
- #include <util.h>
- #endif /* HAVE_UTIL_H */
- #include "ssh2.h"
- #include "ssherr.h"
- #include "misc.h"
- #include "sshbuf.h"
- #include "cipher.h"
- #include "digest.h"
- #define SSHKEY_INTERNAL
- #include "sshkey.h"
- #include "match.h"
- #include "ssh-sk.h"
- #ifdef WITH_XMSS
- #include "sshkey-xmss.h"
- #include "xmss_fast.h"
- #endif
- #include "openbsd-compat/openssl-compat.h"
- /* openssh private key file format */
- #define MARK_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----\n"
- #define MARK_END "-----END OPENSSH PRIVATE KEY-----\n"
- #define MARK_BEGIN_LEN (sizeof(MARK_BEGIN) - 1)
- #define MARK_END_LEN (sizeof(MARK_END) - 1)
- #define KDFNAME "bcrypt"
- #define AUTH_MAGIC "openssh-key-v1"
- #define SALT_LEN 16
- #define DEFAULT_CIPHERNAME "aes256-ctr"
- #define DEFAULT_ROUNDS 16
- /* Version identification string for SSH v1 identity files. */
- #define LEGACY_BEGIN "SSH PRIVATE KEY FILE FORMAT 1.1\n"
- /*
- * Constants relating to "shielding" support; protection of keys expected
- * to remain in memory for long durations
- */
- #define SSHKEY_SHIELD_PREKEY_LEN (16 * 1024)
- #define SSHKEY_SHIELD_CIPHER "aes256-ctr" /* XXX want AES-EME* */
- #define SSHKEY_SHIELD_PREKEY_HASH SSH_DIGEST_SHA512
- int sshkey_private_serialize_opt(struct sshkey *key,
- struct sshbuf *buf, enum sshkey_serialize_rep);
- static int sshkey_from_blob_internal(struct sshbuf *buf,
- struct sshkey **keyp, int allow_cert);
- /* Supported key types */
- struct keytype {
- const char *name;
- const char *shortname;
- const char *sigalg;
- int type;
- int nid;
- int cert;
- int sigonly;
- };
- static const struct keytype keytypes[] = {
- { "ssh-ed25519", "ED25519", NULL, KEY_ED25519, 0, 0, 0 },
- { "ssh-ed25519-cert-v01@openssh.com", "ED25519-CERT", NULL,
- KEY_ED25519_CERT, 0, 1, 0 },
- { "sk-ssh-ed25519@openssh.com", "ED25519-SK", NULL,
- KEY_ED25519_SK, 0, 0, 0 },
- { "sk-ssh-ed25519-cert-v01@openssh.com", "ED25519-SK-CERT", NULL,
- KEY_ED25519_SK_CERT, 0, 1, 0 },
- #ifdef WITH_XMSS
- { "ssh-xmss@openssh.com", "XMSS", NULL, KEY_XMSS, 0, 0, 0 },
- { "ssh-xmss-cert-v01@openssh.com", "XMSS-CERT", NULL,
- KEY_XMSS_CERT, 0, 1, 0 },
- #endif /* WITH_XMSS */
- #ifdef WITH_OPENSSL
- { "ssh-rsa", "RSA", NULL, KEY_RSA, 0, 0, 0 },
- { "rsa-sha2-256", "RSA", NULL, KEY_RSA, 0, 0, 1 },
- { "rsa-sha2-512", "RSA", NULL, KEY_RSA, 0, 0, 1 },
- { "ssh-dss", "DSA", NULL, KEY_DSA, 0, 0, 0 },
- # ifdef OPENSSL_HAS_ECC
- { "ecdsa-sha2-nistp256", "ECDSA", NULL,
- KEY_ECDSA, NID_X9_62_prime256v1, 0, 0 },
- { "ecdsa-sha2-nistp384", "ECDSA", NULL,
- KEY_ECDSA, NID_secp384r1, 0, 0 },
- # ifdef OPENSSL_HAS_NISTP521
- { "ecdsa-sha2-nistp521", "ECDSA", NULL,
- KEY_ECDSA, NID_secp521r1, 0, 0 },
- # endif /* OPENSSL_HAS_NISTP521 */
- { "sk-ecdsa-sha2-nistp256@openssh.com", "ECDSA-SK", NULL,
- KEY_ECDSA_SK, NID_X9_62_prime256v1, 0, 0 },
- { "webauthn-sk-ecdsa-sha2-nistp256@openssh.com", "ECDSA-SK", NULL,
- KEY_ECDSA_SK, NID_X9_62_prime256v1, 0, 1 },
- # endif /* OPENSSL_HAS_ECC */
- { "ssh-rsa-cert-v01@openssh.com", "RSA-CERT", NULL,
- KEY_RSA_CERT, 0, 1, 0 },
- { "rsa-sha2-256-cert-v01@openssh.com", "RSA-CERT",
- "rsa-sha2-256", KEY_RSA_CERT, 0, 1, 1 },
- { "rsa-sha2-512-cert-v01@openssh.com", "RSA-CERT",
- "rsa-sha2-512", KEY_RSA_CERT, 0, 1, 1 },
- { "ssh-dss-cert-v01@openssh.com", "DSA-CERT", NULL,
- KEY_DSA_CERT, 0, 1, 0 },
- # ifdef OPENSSL_HAS_ECC
- { "ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-CERT", NULL,
- KEY_ECDSA_CERT, NID_X9_62_prime256v1, 1, 0 },
- { "ecdsa-sha2-nistp384-cert-v01@openssh.com", "ECDSA-CERT", NULL,
- KEY_ECDSA_CERT, NID_secp384r1, 1, 0 },
- # ifdef OPENSSL_HAS_NISTP521
- { "ecdsa-sha2-nistp521-cert-v01@openssh.com", "ECDSA-CERT", NULL,
- KEY_ECDSA_CERT, NID_secp521r1, 1, 0 },
- # endif /* OPENSSL_HAS_NISTP521 */
- { "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com", "ECDSA-SK-CERT", NULL,
- KEY_ECDSA_SK_CERT, NID_X9_62_prime256v1, 1, 0 },
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- { NULL, NULL, NULL, -1, -1, 0, 0 }
- };
- const char *
- sshkey_type(const struct sshkey *k)
- {
- const struct keytype *kt;
- for (kt = keytypes; kt->type != -1; kt++) {
- if (kt->type == k->type)
- return kt->shortname;
- }
- return "unknown";
- }
- static const char *
- sshkey_ssh_name_from_type_nid(int type, int nid)
- {
- const struct keytype *kt;
- for (kt = keytypes; kt->type != -1; kt++) {
- if (kt->type == type && (kt->nid == 0 || kt->nid == nid))
- return kt->name;
- }
- return "ssh-unknown";
- }
- int
- sshkey_type_is_cert(int type)
- {
- const struct keytype *kt;
- for (kt = keytypes; kt->type != -1; kt++) {
- if (kt->type == type)
- return kt->cert;
- }
- return 0;
- }
- const char *
- sshkey_ssh_name(const struct sshkey *k)
- {
- return sshkey_ssh_name_from_type_nid(k->type, k->ecdsa_nid);
- }
- const char *
- sshkey_ssh_name_plain(const struct sshkey *k)
- {
- return sshkey_ssh_name_from_type_nid(sshkey_type_plain(k->type),
- k->ecdsa_nid);
- }
- int
- sshkey_type_from_name(const char *name)
- {
- const struct keytype *kt;
- for (kt = keytypes; kt->type != -1; kt++) {
- /* Only allow shortname matches for plain key types */
- if ((kt->name != NULL && strcmp(name, kt->name) == 0) ||
- (!kt->cert && strcasecmp(kt->shortname, name) == 0))
- return kt->type;
- }
- return KEY_UNSPEC;
- }
- static int
- key_type_is_ecdsa_variant(int type)
- {
- switch (type) {
- case KEY_ECDSA:
- case KEY_ECDSA_CERT:
- case KEY_ECDSA_SK:
- case KEY_ECDSA_SK_CERT:
- return 1;
- }
- return 0;
- }
- int
- sshkey_ecdsa_nid_from_name(const char *name)
- {
- const struct keytype *kt;
- for (kt = keytypes; kt->type != -1; kt++) {
- if (!key_type_is_ecdsa_variant(kt->type))
- continue;
- if (kt->name != NULL && strcmp(name, kt->name) == 0)
- return kt->nid;
- }
- return -1;
- }
- char *
- sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
- {
- char *tmp, *ret = NULL;
- size_t nlen, rlen = 0;
- const struct keytype *kt;
- for (kt = keytypes; kt->type != -1; kt++) {
- if (kt->name == NULL)
- continue;
- if (!include_sigonly && kt->sigonly)
- continue;
- if ((certs_only && !kt->cert) || (plain_only && kt->cert))
- continue;
- if (ret != NULL)
- ret[rlen++] = sep;
- nlen = strlen(kt->name);
- if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
- free(ret);
- return NULL;
- }
- ret = tmp;
- memcpy(ret + rlen, kt->name, nlen + 1);
- rlen += nlen;
- }
- return ret;
- }
- int
- sshkey_names_valid2(const char *names, int allow_wildcard)
- {
- char *s, *cp, *p;
- const struct keytype *kt;
- int type;
- if (names == NULL || strcmp(names, "") == 0)
- return 0;
- if ((s = cp = strdup(names)) == NULL)
- return 0;
- for ((p = strsep(&cp, ",")); p && *p != '\0';
- (p = strsep(&cp, ","))) {
- type = sshkey_type_from_name(p);
- if (type == KEY_UNSPEC) {
- if (allow_wildcard) {
- /*
- * Try matching key types against the string.
- * If any has a positive or negative match then
- * the component is accepted.
- */
- for (kt = keytypes; kt->type != -1; kt++) {
- if (match_pattern_list(kt->name,
- p, 0) != 0)
- break;
- }
- if (kt->type != -1)
- continue;
- }
- free(s);
- return 0;
- }
- }
- free(s);
- return 1;
- }
- u_int
- sshkey_size(const struct sshkey *k)
- {
- #ifdef WITH_OPENSSL
- const BIGNUM *rsa_n, *dsa_p;
- #endif /* WITH_OPENSSL */
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA:
- case KEY_RSA_CERT:
- if (k->rsa == NULL)
- return 0;
- RSA_get0_key(k->rsa, &rsa_n, NULL, NULL);
- return BN_num_bits(rsa_n);
- case KEY_DSA:
- case KEY_DSA_CERT:
- if (k->dsa == NULL)
- return 0;
- DSA_get0_pqg(k->dsa, &dsa_p, NULL, NULL);
- return BN_num_bits(dsa_p);
- case KEY_ECDSA:
- case KEY_ECDSA_CERT:
- case KEY_ECDSA_SK:
- case KEY_ECDSA_SK_CERT:
- return sshkey_curve_nid_to_bits(k->ecdsa_nid);
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- case KEY_ED25519_CERT:
- case KEY_ED25519_SK:
- case KEY_ED25519_SK_CERT:
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- return 256; /* XXX */
- }
- return 0;
- }
- static int
- sshkey_type_is_valid_ca(int type)
- {
- switch (type) {
- case KEY_RSA:
- case KEY_DSA:
- case KEY_ECDSA:
- case KEY_ECDSA_SK:
- case KEY_ED25519:
- case KEY_ED25519_SK:
- case KEY_XMSS:
- return 1;
- default:
- return 0;
- }
- }
- int
- sshkey_is_private(const struct sshkey *k)
- {
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA_CERT:
- case KEY_RSA: {
- const BIGNUM *d;
- RSA_get0_key(k->rsa, NULL, NULL, &d);
- return d != NULL;
- }
- case KEY_DSA_CERT:
- case KEY_DSA: {
- const BIGNUM *priv_key;
- DSA_get0_key(k->dsa, NULL, &priv_key);
- return priv_key != NULL;
- }
- #ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_CERT:
- case KEY_ECDSA:
- return EC_KEY_get0_private_key(k->ecdsa) != NULL;
- #endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- case KEY_ED25519_CERT:
- case KEY_ED25519:
- return (k->ed25519_pk != NULL);
- default:
- /* fatal("key_is_private: bad key type %d", k->type); */
- return 0;
- }
- }
- int
- sshkey_is_cert(const struct sshkey *k)
- {
- if (k == NULL)
- return 0;
- return sshkey_type_is_cert(k->type);
- }
- int
- sshkey_is_sk(const struct sshkey *k)
- {
- if (k == NULL)
- return 0;
- switch (sshkey_type_plain(k->type)) {
- case KEY_ECDSA_SK:
- case KEY_ED25519_SK:
- return 1;
- default:
- return 0;
- }
- }
- /* Return the cert-less equivalent to a certified key type */
- int
- sshkey_type_plain(int type)
- {
- switch (type) {
- case KEY_RSA_CERT:
- return KEY_RSA;
- case KEY_DSA_CERT:
- return KEY_DSA;
- case KEY_ECDSA_CERT:
- return KEY_ECDSA;
- case KEY_ECDSA_SK_CERT:
- return KEY_ECDSA_SK;
- case KEY_ED25519_CERT:
- return KEY_ED25519;
- case KEY_ED25519_SK_CERT:
- return KEY_ED25519_SK;
- case KEY_XMSS_CERT:
- return KEY_XMSS;
- default:
- return type;
- }
- }
- #ifdef WITH_OPENSSL
- int
- sshkey_calculate_signature(EVP_PKEY *pkey, int hash_alg, u_char **sigp,
- int *lenp, const u_char *data, size_t datalen)
- {
- EVP_MD_CTX *ctx = NULL;
- u_char *sig = NULL;
- int ret, slen, len;
- if (sigp == NULL || lenp == NULL) {
- return SSH_ERR_INVALID_ARGUMENT;
- }
- slen = EVP_PKEY_size(pkey);
- if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
- return SSH_ERR_INVALID_ARGUMENT;
- len = slen;
- if ((sig = malloc(slen)) == NULL) {
- return SSH_ERR_ALLOC_FAIL;
- }
- if ((ctx = EVP_MD_CTX_new()) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto error;
- }
- if (EVP_SignInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 ||
- EVP_SignUpdate(ctx, data, datalen) <= 0 ||
- EVP_SignFinal(ctx, sig, &len, pkey) <= 0) {
- debug("EVP_SignInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 || EVP_SignUpdate(ctx, data, datalen) <= 0 || EVP_SignFinal(ctx, sig, &len, pkey) <= 0)");
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto error;
- }
- *sigp = sig;
- *lenp = len;
- /* Now owned by the caller */
- sig = NULL;
- ret = 0;
- error:
- EVP_MD_CTX_free(ctx);
- free(sig);
- return ret;
- }
- int
- sshkey_verify_signature(EVP_PKEY *pkey, int hash_alg, const u_char *data,
- size_t datalen, u_char *sigbuf, int siglen)
- {
- EVP_MD_CTX *ctx = NULL;
- int ret;
- if ((ctx = EVP_MD_CTX_new()) == NULL) {
- return SSH_ERR_ALLOC_FAIL;
- }
- if (EVP_VerifyInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 ||
- EVP_VerifyUpdate(ctx, data, datalen) <= 0) {
- debug("(EVP_VerifyInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 || EVP_VerifyUpdate(ctx, data, datalen) <= 0)");
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto done;
- }
- ret = EVP_VerifyFinal(ctx, sigbuf, siglen, pkey);
- switch (ret) {
- case 1:
- ret = 0;
- break;
- case 0:
- ret = SSH_ERR_SIGNATURE_INVALID;
- break;
- default:
- debug("ret = EVP_VerifyFinal(ctx, sigbuf, siglen, pkey); switch (ret); ret != 0 or 1, default to error.");
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- break;
- }
- done:
- EVP_MD_CTX_free(ctx);
- return ret;
- }
- /* XXX: these are really begging for a table-driven approach */
- int
- sshkey_curve_name_to_nid(const char *name)
- {
- if (strcmp(name, "nistp256") == 0)
- return NID_X9_62_prime256v1;
- else if (strcmp(name, "nistp384") == 0)
- return NID_secp384r1;
- # ifdef OPENSSL_HAS_NISTP521
- else if (strcmp(name, "nistp521") == 0)
- return NID_secp521r1;
- # endif /* OPENSSL_HAS_NISTP521 */
- else
- return -1;
- }
- u_int
- sshkey_curve_nid_to_bits(int nid)
- {
- switch (nid) {
- case NID_X9_62_prime256v1:
- return 256;
- case NID_secp384r1:
- return 384;
- # ifdef OPENSSL_HAS_NISTP521
- case NID_secp521r1:
- return 521;
- # endif /* OPENSSL_HAS_NISTP521 */
- default:
- return 0;
- }
- }
- int
- sshkey_ecdsa_bits_to_nid(int bits)
- {
- switch (bits) {
- case 256:
- return NID_X9_62_prime256v1;
- case 384:
- return NID_secp384r1;
- # ifdef OPENSSL_HAS_NISTP521
- case 521:
- return NID_secp521r1;
- # endif /* OPENSSL_HAS_NISTP521 */
- default:
- return -1;
- }
- }
- const char *
- sshkey_curve_nid_to_name(int nid)
- {
- switch (nid) {
- case NID_X9_62_prime256v1:
- return "nistp256";
- case NID_secp384r1:
- return "nistp384";
- # ifdef OPENSSL_HAS_NISTP521
- case NID_secp521r1:
- return "nistp521";
- # endif /* OPENSSL_HAS_NISTP521 */
- default:
- return NULL;
- }
- }
- int
- sshkey_ec_nid_to_hash_alg(int nid)
- {
- int kbits = sshkey_curve_nid_to_bits(nid);
- if (kbits <= 0)
- return -1;
- /* RFC5656 section 6.2.1 */
- if (kbits <= 256)
- return SSH_DIGEST_SHA256;
- else if (kbits <= 384)
- return SSH_DIGEST_SHA384;
- else
- return SSH_DIGEST_SHA512;
- }
- #endif /* WITH_OPENSSL */
- static void
- cert_free(struct sshkey_cert *cert)
- {
- u_int i;
- if (cert == NULL)
- return;
- sshbuf_free(cert->certblob);
- sshbuf_free(cert->critical);
- sshbuf_free(cert->extensions);
- free(cert->key_id);
- for (i = 0; i < cert->nprincipals; i++)
- free(cert->principals[i]);
- free(cert->principals);
- sshkey_free(cert->signature_key);
- free(cert->signature_type);
- freezero(cert, sizeof(*cert));
- }
- static struct sshkey_cert *
- cert_new(void)
- {
- struct sshkey_cert *cert;
- if ((cert = calloc(1, sizeof(*cert))) == NULL)
- return NULL;
- if ((cert->certblob = sshbuf_new()) == NULL ||
- (cert->critical = sshbuf_new()) == NULL ||
- (cert->extensions = sshbuf_new()) == NULL) {
- cert_free(cert);
- return NULL;
- }
- cert->key_id = NULL;
- cert->principals = NULL;
- cert->signature_key = NULL;
- cert->signature_type = NULL;
- return cert;
- }
- struct sshkey *
- sshkey_new(int type)
- {
- struct sshkey *k;
- #ifdef WITH_OPENSSL
- RSA *rsa;
- DSA *dsa;
- #endif /* WITH_OPENSSL */
- if ((k = calloc(1, sizeof(*k))) == NULL)
- return NULL;
- k->type = type;
- k->ecdsa = NULL;
- k->ecdsa_nid = -1;
- k->dsa = NULL;
- k->rsa = NULL;
- k->cert = NULL;
- k->ed25519_sk = NULL;
- k->ed25519_pk = NULL;
- k->xmss_sk = NULL;
- k->xmss_pk = NULL;
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA:
- case KEY_RSA_CERT:
- if ((rsa = RSA_new()) == NULL) {
- free(k);
- return NULL;
- }
- k->rsa = rsa;
- break;
- case KEY_DSA:
- case KEY_DSA_CERT:
- if ((dsa = DSA_new()) == NULL) {
- free(k);
- return NULL;
- }
- k->dsa = dsa;
- break;
- case KEY_ECDSA:
- case KEY_ECDSA_CERT:
- case KEY_ECDSA_SK:
- case KEY_ECDSA_SK_CERT:
- /* Cannot do anything until we know the group */
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- case KEY_ED25519_CERT:
- case KEY_ED25519_SK:
- case KEY_ED25519_SK_CERT:
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- /* no need to prealloc */
- break;
- case KEY_UNSPEC:
- break;
- default:
- free(k);
- return NULL;
- }
- if (sshkey_is_cert(k)) {
- if ((k->cert = cert_new()) == NULL) {
- sshkey_free(k);
- return NULL;
- }
- }
- return k;
- }
- void
- sshkey_free(struct sshkey *k)
- {
- if (k == NULL)
- return;
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA:
- case KEY_RSA_CERT:
- RSA_free(k->rsa);
- k->rsa = NULL;
- break;
- case KEY_DSA:
- case KEY_DSA_CERT:
- DSA_free(k->dsa);
- k->dsa = NULL;
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_SK:
- case KEY_ECDSA_SK_CERT:
- free(k->sk_application);
- sshbuf_free(k->sk_key_handle);
- sshbuf_free(k->sk_reserved);
- /* FALLTHROUGH */
- case KEY_ECDSA:
- case KEY_ECDSA_CERT:
- EC_KEY_free(k->ecdsa);
- k->ecdsa = NULL;
- break;
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- case KEY_ED25519_SK:
- case KEY_ED25519_SK_CERT:
- free(k->sk_application);
- sshbuf_free(k->sk_key_handle);
- sshbuf_free(k->sk_reserved);
- /* FALLTHROUGH */
- case KEY_ED25519:
- case KEY_ED25519_CERT:
- freezero(k->ed25519_pk, ED25519_PK_SZ);
- k->ed25519_pk = NULL;
- freezero(k->ed25519_sk, ED25519_SK_SZ);
- k->ed25519_sk = NULL;
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- freezero(k->xmss_pk, sshkey_xmss_pklen(k));
- k->xmss_pk = NULL;
- freezero(k->xmss_sk, sshkey_xmss_sklen(k));
- k->xmss_sk = NULL;
- sshkey_xmss_free_state(k);
- free(k->xmss_name);
- k->xmss_name = NULL;
- free(k->xmss_filename);
- k->xmss_filename = NULL;
- break;
- #endif /* WITH_XMSS */
- case KEY_UNSPEC:
- break;
- default:
- break;
- }
- if (sshkey_is_cert(k))
- cert_free(k->cert);
- freezero(k->shielded_private, k->shielded_len);
- freezero(k->shield_prekey, k->shield_prekey_len);
- freezero(k, sizeof(*k));
- }
- static int
- cert_compare(struct sshkey_cert *a, struct sshkey_cert *b)
- {
- if (a == NULL && b == NULL)
- return 1;
- if (a == NULL || b == NULL)
- return 0;
- if (sshbuf_len(a->certblob) != sshbuf_len(b->certblob))
- return 0;
- if (timingsafe_bcmp(sshbuf_ptr(a->certblob), sshbuf_ptr(b->certblob),
- sshbuf_len(a->certblob)) != 0)
- return 0;
- return 1;
- }
- /*
- * Compare public portions of key only, allowing comparisons between
- * certificates and plain keys too.
- */
- int
- sshkey_equal_public(const struct sshkey *a, const struct sshkey *b)
- {
- #if defined(WITH_OPENSSL)
- const BIGNUM *rsa_e_a, *rsa_n_a;
- const BIGNUM *rsa_e_b, *rsa_n_b;
- const BIGNUM *dsa_p_a, *dsa_q_a, *dsa_g_a, *dsa_pub_key_a;
- const BIGNUM *dsa_p_b, *dsa_q_b, *dsa_g_b, *dsa_pub_key_b;
- #endif /* WITH_OPENSSL */
- if (a == NULL || b == NULL ||
- sshkey_type_plain(a->type) != sshkey_type_plain(b->type))
- return 0;
- switch (a->type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA_CERT:
- case KEY_RSA:
- if (a->rsa == NULL || b->rsa == NULL)
- return 0;
- RSA_get0_key(a->rsa, &rsa_n_a, &rsa_e_a, NULL);
- RSA_get0_key(b->rsa, &rsa_n_b, &rsa_e_b, NULL);
- return BN_cmp(rsa_e_a, rsa_e_b) == 0 &&
- BN_cmp(rsa_n_a, rsa_n_b) == 0;
- case KEY_DSA_CERT:
- case KEY_DSA:
- if (a->dsa == NULL || b->dsa == NULL)
- return 0;
- DSA_get0_pqg(a->dsa, &dsa_p_a, &dsa_q_a, &dsa_g_a);
- DSA_get0_pqg(b->dsa, &dsa_p_b, &dsa_q_b, &dsa_g_b);
- DSA_get0_key(a->dsa, &dsa_pub_key_a, NULL);
- DSA_get0_key(b->dsa, &dsa_pub_key_b, NULL);
- return BN_cmp(dsa_p_a, dsa_p_b) == 0 &&
- BN_cmp(dsa_q_a, dsa_q_b) == 0 &&
- BN_cmp(dsa_g_a, dsa_g_b) == 0 &&
- BN_cmp(dsa_pub_key_a, dsa_pub_key_b) == 0;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_SK:
- case KEY_ECDSA_SK_CERT:
- if (a->sk_application == NULL || b->sk_application == NULL)
- return 0;
- if (strcmp(a->sk_application, b->sk_application) != 0)
- return 0;
- /* FALLTHROUGH */
- case KEY_ECDSA_CERT:
- case KEY_ECDSA:
- if (a->ecdsa == NULL || b->ecdsa == NULL ||
- EC_KEY_get0_public_key(a->ecdsa) == NULL ||
- EC_KEY_get0_public_key(b->ecdsa) == NULL)
- return 0;
- if (EC_GROUP_cmp(EC_KEY_get0_group(a->ecdsa),
- EC_KEY_get0_group(b->ecdsa), NULL) != 0 ||
- EC_POINT_cmp(EC_KEY_get0_group(a->ecdsa),
- EC_KEY_get0_public_key(a->ecdsa),
- EC_KEY_get0_public_key(b->ecdsa), NULL) != 0)
- return 0;
- return 1;
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- case KEY_ED25519_SK:
- case KEY_ED25519_SK_CERT:
- if (a->sk_application == NULL || b->sk_application == NULL)
- return 0;
- if (strcmp(a->sk_application, b->sk_application) != 0)
- return 0;
- /* FALLTHROUGH */
- case KEY_ED25519:
- case KEY_ED25519_CERT:
- return a->ed25519_pk != NULL && b->ed25519_pk != NULL &&
- memcmp(a->ed25519_pk, b->ed25519_pk, ED25519_PK_SZ) == 0;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- return a->xmss_pk != NULL && b->xmss_pk != NULL &&
- sshkey_xmss_pklen(a) == sshkey_xmss_pklen(b) &&
- memcmp(a->xmss_pk, b->xmss_pk, sshkey_xmss_pklen(a)) == 0;
- #endif /* WITH_XMSS */
- default:
- return 0;
- }
- /* NOTREACHED */
- }
- int
- sshkey_equal(const struct sshkey *a, const struct sshkey *b)
- {
- if (a == NULL || b == NULL || a->type != b->type)
- return 0;
- if (sshkey_is_cert(a)) {
- if (!cert_compare(a->cert, b->cert))
- return 0;
- }
- return sshkey_equal_public(a, b);
- }
- static int
- to_blob_buf(const struct sshkey *key, struct sshbuf *b, int force_plain,
- enum sshkey_serialize_rep opts)
- {
- int type, ret = SSH_ERR_INTERNAL_ERROR;
- const char *typename;
- #ifdef WITH_OPENSSL
- const BIGNUM *rsa_n, *rsa_e, *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key;
- #endif /* WITH_OPENSSL */
- if (key == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if (sshkey_is_cert(key)) {
- if (key->cert == NULL)
- return SSH_ERR_EXPECTED_CERT;
- if (sshbuf_len(key->cert->certblob) == 0)
- return SSH_ERR_KEY_LACKS_CERTBLOB;
- }
- type = force_plain ? sshkey_type_plain(key->type) : key->type;
- typename = sshkey_ssh_name_from_type_nid(type, key->ecdsa_nid);
- switch (type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA_CERT:
- case KEY_ECDSA_CERT:
- case KEY_ECDSA_SK_CERT:
- case KEY_RSA_CERT:
- #endif /* WITH_OPENSSL */
- case KEY_ED25519_CERT:
- case KEY_ED25519_SK_CERT:
- #ifdef WITH_XMSS
- case KEY_XMSS_CERT:
- #endif /* WITH_XMSS */
- /* Use the existing blob */
- /* XXX modified flag? */
- if ((ret = sshbuf_putb(b, key->cert->certblob)) != 0)
- return ret;
- break;
- #ifdef WITH_OPENSSL
- case KEY_DSA:
- if (key->dsa == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- DSA_get0_pqg(key->dsa, &dsa_p, &dsa_q, &dsa_g);
- DSA_get0_key(key->dsa, &dsa_pub_key, NULL);
- if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
- (ret = sshbuf_put_bignum2(b, dsa_p)) != 0 ||
- (ret = sshbuf_put_bignum2(b, dsa_q)) != 0 ||
- (ret = sshbuf_put_bignum2(b, dsa_g)) != 0 ||
- (ret = sshbuf_put_bignum2(b, dsa_pub_key)) != 0)
- return ret;
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
- case KEY_ECDSA_SK:
- if (key->ecdsa == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
- (ret = sshbuf_put_cstring(b,
- sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
- (ret = sshbuf_put_eckey(b, key->ecdsa)) != 0)
- return ret;
- if (type == KEY_ECDSA_SK) {
- if ((ret = sshbuf_put_cstring(b,
- key->sk_application)) != 0)
- return ret;
- }
- break;
- # endif
- case KEY_RSA:
- if (key->rsa == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- RSA_get0_key(key->rsa, &rsa_n, &rsa_e, NULL);
- if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
- (ret = sshbuf_put_bignum2(b, rsa_e)) != 0 ||
- (ret = sshbuf_put_bignum2(b, rsa_n)) != 0)
- return ret;
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- case KEY_ED25519_SK:
- if (key->ed25519_pk == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
- (ret = sshbuf_put_string(b,
- key->ed25519_pk, ED25519_PK_SZ)) != 0)
- return ret;
- if (type == KEY_ED25519_SK) {
- if ((ret = sshbuf_put_cstring(b,
- key->sk_application)) != 0)
- return ret;
- }
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- if (key->xmss_name == NULL || key->xmss_pk == NULL ||
- sshkey_xmss_pklen(key) == 0)
- return SSH_ERR_INVALID_ARGUMENT;
- if ((ret = sshbuf_put_cstring(b, typename)) != 0 ||
- (ret = sshbuf_put_cstring(b, key->xmss_name)) != 0 ||
- (ret = sshbuf_put_string(b,
- key->xmss_pk, sshkey_xmss_pklen(key))) != 0 ||
- (ret = sshkey_xmss_serialize_pk_info(key, b, opts)) != 0)
- return ret;
- break;
- #endif /* WITH_XMSS */
- default:
- return SSH_ERR_KEY_TYPE_UNKNOWN;
- }
- return 0;
- }
- int
- sshkey_putb(const struct sshkey *key, struct sshbuf *b)
- {
- return to_blob_buf(key, b, 0, SSHKEY_SERIALIZE_DEFAULT);
- }
- int
- sshkey_puts_opts(const struct sshkey *key, struct sshbuf *b,
- enum sshkey_serialize_rep opts)
- {
- struct sshbuf *tmp;
- int r;
- if ((tmp = sshbuf_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- r = to_blob_buf(key, tmp, 0, opts);
- if (r == 0)
- r = sshbuf_put_stringb(b, tmp);
- sshbuf_free(tmp);
- return r;
- }
- int
- sshkey_puts(const struct sshkey *key, struct sshbuf *b)
- {
- return sshkey_puts_opts(key, b, SSHKEY_SERIALIZE_DEFAULT);
- }
- int
- sshkey_putb_plain(const struct sshkey *key, struct sshbuf *b)
- {
- return to_blob_buf(key, b, 1, SSHKEY_SERIALIZE_DEFAULT);
- }
- static int
- to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp, int force_plain,
- enum sshkey_serialize_rep opts)
- {
- int ret = SSH_ERR_INTERNAL_ERROR;
- size_t len;
- struct sshbuf *b = NULL;
- if (lenp != NULL)
- *lenp = 0;
- if (blobp != NULL)
- *blobp = NULL;
- if ((b = sshbuf_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- if ((ret = to_blob_buf(key, b, force_plain, opts)) != 0)
- goto out;
- len = sshbuf_len(b);
- if (lenp != NULL)
- *lenp = len;
- if (blobp != NULL) {
- if ((*blobp = malloc(len)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- memcpy(*blobp, sshbuf_ptr(b), len);
- }
- ret = 0;
- out:
- sshbuf_free(b);
- return ret;
- }
- int
- sshkey_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
- {
- return to_blob(key, blobp, lenp, 0, SSHKEY_SERIALIZE_DEFAULT);
- }
- int
- sshkey_plain_to_blob(const struct sshkey *key, u_char **blobp, size_t *lenp)
- {
- return to_blob(key, blobp, lenp, 1, SSHKEY_SERIALIZE_DEFAULT);
- }
- int
- sshkey_fingerprint_raw(const struct sshkey *k, int dgst_alg,
- u_char **retp, size_t *lenp)
- {
- u_char *blob = NULL, *ret = NULL;
- size_t blob_len = 0;
- int r = SSH_ERR_INTERNAL_ERROR;
- if (retp != NULL)
- *retp = NULL;
- if (lenp != NULL)
- *lenp = 0;
- if (ssh_digest_bytes(dgst_alg) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = to_blob(k, &blob, &blob_len, 1, SSHKEY_SERIALIZE_DEFAULT))
- != 0)
- goto out;
- if ((ret = calloc(1, SSH_DIGEST_MAX_LENGTH)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if ((r = ssh_digest_memory(dgst_alg, blob, blob_len,
- ret, SSH_DIGEST_MAX_LENGTH)) != 0)
- goto out;
- /* success */
- if (retp != NULL) {
- *retp = ret;
- ret = NULL;
- }
- if (lenp != NULL)
- *lenp = ssh_digest_bytes(dgst_alg);
- r = 0;
- out:
- free(ret);
- if (blob != NULL)
- freezero(blob, blob_len);
- return r;
- }
- static char *
- fingerprint_b64(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
- {
- char *ret;
- size_t plen = strlen(alg) + 1;
- size_t rlen = ((dgst_raw_len + 2) / 3) * 4 + plen + 1;
- if (dgst_raw_len > 65536 || (ret = calloc(1, rlen)) == NULL)
- return NULL;
- strlcpy(ret, alg, rlen);
- strlcat(ret, ":", rlen);
- if (dgst_raw_len == 0)
- return ret;
- if (b64_ntop(dgst_raw, dgst_raw_len, ret + plen, rlen - plen) == -1) {
- freezero(ret, rlen);
- return NULL;
- }
- /* Trim padding characters from end */
- ret[strcspn(ret, "=")] = '\0';
- return ret;
- }
- static char *
- fingerprint_hex(const char *alg, u_char *dgst_raw, size_t dgst_raw_len)
- {
- char *retval, hex[5];
- size_t i, rlen = dgst_raw_len * 3 + strlen(alg) + 2;
- if (dgst_raw_len > 65536 || (retval = calloc(1, rlen)) == NULL)
- return NULL;
- strlcpy(retval, alg, rlen);
- strlcat(retval, ":", rlen);
- for (i = 0; i < dgst_raw_len; i++) {
- snprintf(hex, sizeof(hex), "%s%02x",
- i > 0 ? ":" : "", dgst_raw[i]);
- strlcat(retval, hex, rlen);
- }
- return retval;
- }
- static char *
- fingerprint_bubblebabble(u_char *dgst_raw, size_t dgst_raw_len)
- {
- char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
- char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
- 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
- u_int i, j = 0, rounds, seed = 1;
- char *retval;
- rounds = (dgst_raw_len / 2) + 1;
- if ((retval = calloc(rounds, 6)) == NULL)
- return NULL;
- retval[j++] = 'x';
- for (i = 0; i < rounds; i++) {
- u_int idx0, idx1, idx2, idx3, idx4;
- if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
- idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
- seed) % 6;
- idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
- idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
- (seed / 6)) % 6;
- retval[j++] = vowels[idx0];
- retval[j++] = consonants[idx1];
- retval[j++] = vowels[idx2];
- if ((i + 1) < rounds) {
- idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
- idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
- retval[j++] = consonants[idx3];
- retval[j++] = '-';
- retval[j++] = consonants[idx4];
- seed = ((seed * 5) +
- ((((u_int)(dgst_raw[2 * i])) * 7) +
- ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
- }
- } else {
- idx0 = seed % 6;
- idx1 = 16;
- idx2 = seed / 6;
- retval[j++] = vowels[idx0];
- retval[j++] = consonants[idx1];
- retval[j++] = vowels[idx2];
- }
- }
- retval[j++] = 'x';
- retval[j++] = '\0';
- return retval;
- }
- /*
- * Draw an ASCII-Art representing the fingerprint so human brain can
- * profit from its built-in pattern recognition ability.
- * This technique is called "random art" and can be found in some
- * scientific publications like this original paper:
- *
- * "Hash Visualization: a New Technique to improve Real-World Security",
- * Perrig A. and Song D., 1999, International Workshop on Cryptographic
- * Techniques and E-Commerce (CrypTEC '99)
- * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
- *
- * The subject came up in a talk by Dan Kaminsky, too.
- *
- * If you see the picture is different, the key is different.
- * If the picture looks the same, you still know nothing.
- *
- * The algorithm used here is a worm crawling over a discrete plane,
- * leaving a trace (augmenting the field) everywhere it goes.
- * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
- * makes the respective movement vector be ignored for this turn.
- * Graphs are not unambiguous, because circles in graphs can be
- * walked in either direction.
- */
- /*
- * Field sizes for the random art. Have to be odd, so the starting point
- * can be in the exact middle of the picture, and FLDBASE should be >=8 .
- * Else pictures would be too dense, and drawing the frame would
- * fail, too, because the key type would not fit in anymore.
- */
- #define FLDBASE 8
- #define FLDSIZE_Y (FLDBASE + 1)
- #define FLDSIZE_X (FLDBASE * 2 + 1)
- static char *
- fingerprint_randomart(const char *alg, u_char *dgst_raw, size_t dgst_raw_len,
- const struct sshkey *k)
- {
- /*
- * Chars to be used after each other every time the worm
- * intersects with itself. Matter of taste.
- */
- char *augmentation_string = " .o+=*BOX@%&#/^SE";
- char *retval, *p, title[FLDSIZE_X], hash[FLDSIZE_X];
- u_char field[FLDSIZE_X][FLDSIZE_Y];
- size_t i, tlen, hlen;
- u_int b;
- int x, y, r;
- size_t len = strlen(augmentation_string) - 1;
- if ((retval = calloc((FLDSIZE_X + 3), (FLDSIZE_Y + 2))) == NULL)
- return NULL;
- /* initialize field */
- memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
- x = FLDSIZE_X / 2;
- y = FLDSIZE_Y / 2;
- /* process raw key */
- for (i = 0; i < dgst_raw_len; i++) {
- int input;
- /* each byte conveys four 2-bit move commands */
- input = dgst_raw[i];
- for (b = 0; b < 4; b++) {
- /* evaluate 2 bit, rest is shifted later */
- x += (input & 0x1) ? 1 : -1;
- y += (input & 0x2) ? 1 : -1;
- /* assure we are still in bounds */
- x = MAXIMUM(x, 0);
- y = MAXIMUM(y, 0);
- x = MINIMUM(x, FLDSIZE_X - 1);
- y = MINIMUM(y, FLDSIZE_Y - 1);
- /* augment the field */
- if (field[x][y] < len - 2)
- field[x][y]++;
- input = input >> 2;
- }
- }
- /* mark starting point and end point*/
- field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
- field[x][y] = len;
- /* assemble title */
- r = snprintf(title, sizeof(title), "[%s %u]",
- sshkey_type(k), sshkey_size(k));
- /* If [type size] won't fit, then try [type]; fits "[ED25519-CERT]" */
- if (r < 0 || r > (int)sizeof(title))
- r = snprintf(title, sizeof(title), "[%s]", sshkey_type(k));
- tlen = (r <= 0) ? 0 : strlen(title);
- /* assemble hash ID. */
- r = snprintf(hash, sizeof(hash), "[%s]", alg);
- hlen = (r <= 0) ? 0 : strlen(hash);
- /* output upper border */
- p = retval;
- *p++ = '+';
- for (i = 0; i < (FLDSIZE_X - tlen) / 2; i++)
- *p++ = '-';
- memcpy(p, title, tlen);
- p += tlen;
- for (i += tlen; i < FLDSIZE_X; i++)
- *p++ = '-';
- *p++ = '+';
- *p++ = '\n';
- /* output content */
- for (y = 0; y < FLDSIZE_Y; y++) {
- *p++ = '|';
- for (x = 0; x < FLDSIZE_X; x++)
- *p++ = augmentation_string[MINIMUM(field[x][y], len)];
- *p++ = '|';
- *p++ = '\n';
- }
- /* output lower border */
- *p++ = '+';
- for (i = 0; i < (FLDSIZE_X - hlen) / 2; i++)
- *p++ = '-';
- memcpy(p, hash, hlen);
- p += hlen;
- for (i += hlen; i < FLDSIZE_X; i++)
- *p++ = '-';
- *p++ = '+';
- return retval;
- }
- char *
- sshkey_fingerprint(const struct sshkey *k, int dgst_alg,
- enum sshkey_fp_rep dgst_rep)
- {
- char *retval = NULL;
- u_char *dgst_raw;
- size_t dgst_raw_len;
- if (sshkey_fingerprint_raw(k, dgst_alg, &dgst_raw, &dgst_raw_len) != 0)
- return NULL;
- switch (dgst_rep) {
- case SSH_FP_DEFAULT:
- if (dgst_alg == SSH_DIGEST_MD5) {
- retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
- dgst_raw, dgst_raw_len);
- } else {
- retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
- dgst_raw, dgst_raw_len);
- }
- break;
- case SSH_FP_HEX:
- retval = fingerprint_hex(ssh_digest_alg_name(dgst_alg),
- dgst_raw, dgst_raw_len);
- break;
- case SSH_FP_BASE64:
- retval = fingerprint_b64(ssh_digest_alg_name(dgst_alg),
- dgst_raw, dgst_raw_len);
- break;
- case SSH_FP_BUBBLEBABBLE:
- retval = fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
- break;
- case SSH_FP_RANDOMART:
- retval = fingerprint_randomart(ssh_digest_alg_name(dgst_alg),
- dgst_raw, dgst_raw_len, k);
- break;
- default:
- freezero(dgst_raw, dgst_raw_len);
- return NULL;
- }
- freezero(dgst_raw, dgst_raw_len);
- return retval;
- }
- static int
- peek_type_nid(const char *s, size_t l, int *nid)
- {
- const struct keytype *kt;
- for (kt = keytypes; kt->type != -1; kt++) {
- if (kt->name == NULL || strlen(kt->name) != l)
- continue;
- if (memcmp(s, kt->name, l) == 0) {
- *nid = -1;
- if (key_type_is_ecdsa_variant(kt->type))
- *nid = kt->nid;
- return kt->type;
- }
- }
- return KEY_UNSPEC;
- }
- /* XXX this can now be made const char * */
- int
- sshkey_read(struct sshkey *ret, char **cpp)
- {
- struct sshkey *k;
- char *cp, *blobcopy;
- size_t space;
- int r, type, curve_nid = -1;
- struct sshbuf *blob;
- if (ret == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- switch (ret->type) {
- case KEY_UNSPEC:
- case KEY_RSA:
- case KEY_DSA:
- case KEY_ECDSA:
- case KEY_ECDSA_SK:
- case KEY_ED25519:
- case KEY_ED25519_SK:
- case KEY_DSA_CERT:
- case KEY_ECDSA_CERT:
- case KEY_ECDSA_SK_CERT:
- case KEY_RSA_CERT:
- case KEY_ED25519_CERT:
- case KEY_ED25519_SK_CERT:
- #ifdef WITH_XMSS
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- #endif /* WITH_XMSS */
- break; /* ok */
- default:
- return SSH_ERR_INVALID_ARGUMENT;
- }
- /* Decode type */
- cp = *cpp;
- space = strcspn(cp, " \t");
- if (space == strlen(cp))
- return SSH_ERR_INVALID_FORMAT;
- if ((type = peek_type_nid(cp, space, &curve_nid)) == KEY_UNSPEC)
- return SSH_ERR_INVALID_FORMAT;
- /* skip whitespace */
- for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
- ;
- if (*cp == '\0')
- return SSH_ERR_INVALID_FORMAT;
- if (ret->type != KEY_UNSPEC && ret->type != type)
- return SSH_ERR_KEY_TYPE_MISMATCH;
- if ((blob = sshbuf_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- /* find end of keyblob and decode */
- space = strcspn(cp, " \t");
- if ((blobcopy = strndup(cp, space)) == NULL) {
- sshbuf_free(blob);
- return SSH_ERR_ALLOC_FAIL;
- }
- if ((r = sshbuf_b64tod(blob, blobcopy)) != 0) {
- free(blobcopy);
- sshbuf_free(blob);
- return r;
- }
- free(blobcopy);
- if ((r = sshkey_fromb(blob, &k)) != 0) {
- sshbuf_free(blob);
- return r;
- }
- sshbuf_free(blob);
- /* skip whitespace and leave cp at start of comment */
- for (cp += space; *cp == ' ' || *cp == '\t'; cp++)
- ;
- /* ensure type of blob matches type at start of line */
- if (k->type != type) {
- sshkey_free(k);
- return SSH_ERR_KEY_TYPE_MISMATCH;
- }
- if (key_type_is_ecdsa_variant(type) && curve_nid != k->ecdsa_nid) {
- sshkey_free(k);
- return SSH_ERR_EC_CURVE_MISMATCH;
- }
- /* Fill in ret from parsed key */
- ret->type = type;
- if (sshkey_is_cert(ret)) {
- if (!sshkey_is_cert(k)) {
- sshkey_free(k);
- return SSH_ERR_EXPECTED_CERT;
- }
- if (ret->cert != NULL)
- cert_free(ret->cert);
- ret->cert = k->cert;
- k->cert = NULL;
- }
- switch (sshkey_type_plain(ret->type)) {
- #ifdef WITH_OPENSSL
- case KEY_RSA:
- RSA_free(ret->rsa);
- ret->rsa = k->rsa;
- k->rsa = NULL;
- #ifdef DEBUG_PK
- RSA_print_fp(stderr, ret->rsa, 8);
- #endif
- break;
- case KEY_DSA:
- DSA_free(ret->dsa);
- ret->dsa = k->dsa;
- k->dsa = NULL;
- #ifdef DEBUG_PK
- DSA_print_fp(stderr, ret->dsa, 8);
- #endif
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
- EC_KEY_free(ret->ecdsa);
- ret->ecdsa = k->ecdsa;
- ret->ecdsa_nid = k->ecdsa_nid;
- k->ecdsa = NULL;
- k->ecdsa_nid = -1;
- #ifdef DEBUG_PK
- sshkey_dump_ec_key(ret->ecdsa);
- #endif
- break;
- case KEY_ECDSA_SK:
- EC_KEY_free(ret->ecdsa);
- ret->ecdsa = k->ecdsa;
- ret->ecdsa_nid = k->ecdsa_nid;
- ret->sk_application = k->sk_application;
- k->ecdsa = NULL;
- k->ecdsa_nid = -1;
- k->sk_application = NULL;
- #ifdef DEBUG_PK
- sshkey_dump_ec_key(ret->ecdsa);
- fprintf(stderr, "App: %s\n", ret->sk_application);
- #endif
- break;
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- freezero(ret->ed25519_pk, ED25519_PK_SZ);
- ret->ed25519_pk = k->ed25519_pk;
- k->ed25519_pk = NULL;
- #ifdef DEBUG_PK
- /* XXX */
- #endif
- break;
- case KEY_ED25519_SK:
- freezero(ret->ed25519_pk, ED25519_PK_SZ);
- ret->ed25519_pk = k->ed25519_pk;
- ret->sk_application = k->sk_application;
- k->ed25519_pk = NULL;
- k->sk_application = NULL;
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- free(ret->xmss_pk);
- ret->xmss_pk = k->xmss_pk;
- k->xmss_pk = NULL;
- free(ret->xmss_state);
- ret->xmss_state = k->xmss_state;
- k->xmss_state = NULL;
- free(ret->xmss_name);
- ret->xmss_name = k->xmss_name;
- k->xmss_name = NULL;
- free(ret->xmss_filename);
- ret->xmss_filename = k->xmss_filename;
- k->xmss_filename = NULL;
- #ifdef DEBUG_PK
- /* XXX */
- #endif
- break;
- #endif /* WITH_XMSS */
- default:
- sshkey_free(k);
- return SSH_ERR_INTERNAL_ERROR;
- }
- sshkey_free(k);
- /* success */
- *cpp = cp;
- return 0;
- }
- int
- sshkey_to_base64(const struct sshkey *key, char **b64p)
- {
- int r = SSH_ERR_INTERNAL_ERROR;
- struct sshbuf *b = NULL;
- char *uu = NULL;
- if (b64p != NULL)
- *b64p = NULL;
- if ((b = sshbuf_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- if ((r = sshkey_putb(key, b)) != 0)
- goto out;
- if ((uu = sshbuf_dtob64_string(b, 0)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- /* Success */
- if (b64p != NULL) {
- *b64p = uu;
- uu = NULL;
- }
- r = 0;
- out:
- sshbuf_free(b);
- free(uu);
- return r;
- }
- int
- sshkey_format_text(const struct sshkey *key, struct sshbuf *b)
- {
- int r = SSH_ERR_INTERNAL_ERROR;
- char *uu = NULL;
- if ((r = sshkey_to_base64(key, &uu)) != 0)
- goto out;
- if ((r = sshbuf_putf(b, "%s %s",
- sshkey_ssh_name(key), uu)) != 0)
- goto out;
- r = 0;
- out:
- free(uu);
- return r;
- }
- int
- sshkey_write(const struct sshkey *key, FILE *f)
- {
- struct sshbuf *b = NULL;
- int r = SSH_ERR_INTERNAL_ERROR;
- if ((b = sshbuf_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- if ((r = sshkey_format_text(key, b)) != 0)
- goto out;
- if (fwrite(sshbuf_ptr(b), sshbuf_len(b), 1, f) != 1) {
- if (feof(f))
- errno = EPIPE;
- r = SSH_ERR_SYSTEM_ERROR;
- goto out;
- }
- /* Success */
- r = 0;
- out:
- sshbuf_free(b);
- return r;
- }
- const char *
- sshkey_cert_type(const struct sshkey *k)
- {
- switch (k->cert->type) {
- case SSH2_CERT_TYPE_USER:
- return "user";
- case SSH2_CERT_TYPE_HOST:
- return "host";
- default:
- return "unknown";
- }
- }
- #ifdef WITH_OPENSSL
- static int
- rsa_generate_private_key(u_int bits, RSA **rsap)
- {
- RSA *private = NULL;
- BIGNUM *f4 = NULL;
- int ret = SSH_ERR_INTERNAL_ERROR;
- if (rsap == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if (bits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
- bits > SSHBUF_MAX_BIGNUM * 8)
- return SSH_ERR_KEY_LENGTH;
- *rsap = NULL;
- if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (!BN_set_word(f4, RSA_F4) ||
- !RSA_generate_key_ex(private, bits, f4, NULL)) {
- debug("!BN_set_word(f4, RSA_F4) || !RSA_generate_key_ex(private, bits, f4, NULL))");
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- *rsap = private;
- private = NULL;
- ret = 0;
- out:
- RSA_free(private);
- BN_free(f4);
- return ret;
- }
- static int
- dsa_generate_private_key(u_int bits, DSA **dsap)
- {
- DSA *private;
- int ret = SSH_ERR_INTERNAL_ERROR;
- if (dsap == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if (bits != 1024)
- return SSH_ERR_KEY_LENGTH;
- if ((private = DSA_new()) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- *dsap = NULL;
- if (!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL,
- NULL, NULL) || !DSA_generate_key(private)) {
- debug("(!DSA_generate_parameters_ex(private, bits, NULL, 0, NULL, NULL, NULL) || !DSA_generate_key(private))");
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- *dsap = private;
- private = NULL;
- ret = 0;
- out:
- DSA_free(private);
- return ret;
- }
- # ifdef OPENSSL_HAS_ECC
- int
- sshkey_ecdsa_key_to_nid(EC_KEY *k)
- {
- EC_GROUP *eg;
- int nids[] = {
- NID_X9_62_prime256v1,
- NID_secp384r1,
- # ifdef OPENSSL_HAS_NISTP521
- NID_secp521r1,
- # endif /* OPENSSL_HAS_NISTP521 */
- -1
- };
- int nid;
- u_int i;
- const EC_GROUP *g = EC_KEY_get0_group(k);
- /*
- * The group may be stored in a ASN.1 encoded private key in one of two
- * ways: as a "named group", which is reconstituted by ASN.1 object ID
- * or explicit group parameters encoded into the key blob. Only the
- * "named group" case sets the group NID for us, but we can figure
- * it out for the other case by comparing against all the groups that
- * are supported.
- */
- if ((nid = EC_GROUP_get_curve_name(g)) > 0)
- return nid;
- for (i = 0; nids[i] != -1; i++) {
- if ((eg = EC_GROUP_new_by_curve_name(nids[i])) == NULL)
- return -1;
- if (EC_GROUP_cmp(g, eg, NULL) == 0)
- break;
- EC_GROUP_free(eg);
- }
- if (nids[i] != -1) {
- /* Use the group with the NID attached */
- EC_GROUP_set_asn1_flag(eg, OPENSSL_EC_NAMED_CURVE);
- if (EC_KEY_set_group(k, eg) != 1) {
- EC_GROUP_free(eg);
- return -1;
- }
- }
- return nids[i];
- }
- static int
- ecdsa_generate_private_key(u_int bits, int *nid, EC_KEY **ecdsap)
- {
- EC_KEY *private;
- int ret = SSH_ERR_INTERNAL_ERROR;
- if (nid == NULL || ecdsap == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if ((*nid = sshkey_ecdsa_bits_to_nid(bits)) == -1)
- return SSH_ERR_KEY_LENGTH;
- *ecdsap = NULL;
- if ((private = EC_KEY_new_by_curve_name(*nid)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (EC_KEY_generate_key(private) != 1) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
- *ecdsap = private;
- private = NULL;
- ret = 0;
- out:
- EC_KEY_free(private);
- return ret;
- }
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- int
- sshkey_generate(int type, u_int bits, struct sshkey **keyp)
- {
- struct sshkey *k;
- int ret = SSH_ERR_INTERNAL_ERROR;
- if (keyp == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- *keyp = NULL;
- if ((k = sshkey_new(KEY_UNSPEC)) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- switch (type) {
- case KEY_ED25519:
- if ((k->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL ||
- (k->ed25519_sk = malloc(ED25519_SK_SZ)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- break;
- }
- crypto_sign_ed25519_keypair(k->ed25519_pk, k->ed25519_sk);
- ret = 0;
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- ret = sshkey_xmss_generate_private_key(k, bits);
- break;
- #endif /* WITH_XMSS */
- #ifdef WITH_OPENSSL
- case KEY_DSA:
- ret = dsa_generate_private_key(bits, &k->dsa);
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
- ret = ecdsa_generate_private_key(bits, &k->ecdsa_nid,
- &k->ecdsa);
- break;
- # endif /* OPENSSL_HAS_ECC */
- case KEY_RSA:
- ret = rsa_generate_private_key(bits, &k->rsa);
- break;
- #endif /* WITH_OPENSSL */
- default:
- ret = SSH_ERR_INVALID_ARGUMENT;
- }
- if (ret == 0) {
- k->type = type;
- *keyp = k;
- } else
- sshkey_free(k);
- return ret;
- }
- int
- sshkey_cert_copy(const struct sshkey *from_key, struct sshkey *to_key)
- {
- u_int i;
- const struct sshkey_cert *from;
- struct sshkey_cert *to;
- int r = SSH_ERR_INTERNAL_ERROR;
- if (to_key == NULL || (from = from_key->cert) == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if ((to = cert_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- if ((r = sshbuf_putb(to->certblob, from->certblob)) != 0 ||
- (r = sshbuf_putb(to->critical, from->critical)) != 0 ||
- (r = sshbuf_putb(to->extensions, from->extensions)) != 0)
- goto out;
- to->serial = from->serial;
- to->type = from->type;
- if (from->key_id == NULL)
- to->key_id = NULL;
- else if ((to->key_id = strdup(from->key_id)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- to->valid_after = from->valid_after;
- to->valid_before = from->valid_before;
- if (from->signature_key == NULL)
- to->signature_key = NULL;
- else if ((r = sshkey_from_private(from->signature_key,
- &to->signature_key)) != 0)
- goto out;
- if (from->signature_type != NULL &&
- (to->signature_type = strdup(from->signature_type)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (from->nprincipals > SSHKEY_CERT_MAX_PRINCIPALS) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if (from->nprincipals > 0) {
- if ((to->principals = calloc(from->nprincipals,
- sizeof(*to->principals))) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- for (i = 0; i < from->nprincipals; i++) {
- to->principals[i] = strdup(from->principals[i]);
- if (to->principals[i] == NULL) {
- to->nprincipals = i;
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- }
- }
- to->nprincipals = from->nprincipals;
- /* success */
- cert_free(to_key->cert);
- to_key->cert = to;
- to = NULL;
- r = 0;
- out:
- cert_free(to);
- return r;
- }
- int
- sshkey_from_private(const struct sshkey *k, struct sshkey **pkp)
- {
- struct sshkey *n = NULL;
- int r = SSH_ERR_INTERNAL_ERROR;
- #ifdef WITH_OPENSSL
- const BIGNUM *rsa_n, *rsa_e;
- BIGNUM *rsa_n_dup = NULL, *rsa_e_dup = NULL;
- const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key;
- BIGNUM *dsa_p_dup = NULL, *dsa_q_dup = NULL, *dsa_g_dup = NULL;
- BIGNUM *dsa_pub_key_dup = NULL;
- #endif /* WITH_OPENSSL */
- *pkp = NULL;
- if ((n = sshkey_new(k->type)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA:
- case KEY_DSA_CERT:
- DSA_get0_pqg(k->dsa, &dsa_p, &dsa_q, &dsa_g);
- DSA_get0_key(k->dsa, &dsa_pub_key, NULL);
- if ((dsa_p_dup = BN_dup(dsa_p)) == NULL ||
- (dsa_q_dup = BN_dup(dsa_q)) == NULL ||
- (dsa_g_dup = BN_dup(dsa_g)) == NULL ||
- (dsa_pub_key_dup = BN_dup(dsa_pub_key)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (!DSA_set0_pqg(n->dsa, dsa_p_dup, dsa_q_dup, dsa_g_dup)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- dsa_p_dup = dsa_q_dup = dsa_g_dup = NULL; /* transferred */
- if (!DSA_set0_key(n->dsa, dsa_pub_key_dup, NULL)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- dsa_pub_key_dup = NULL; /* transferred */
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
- case KEY_ECDSA_CERT:
- case KEY_ECDSA_SK:
- case KEY_ECDSA_SK_CERT:
- n->ecdsa_nid = k->ecdsa_nid;
- n->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
- if (n->ecdsa == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (EC_KEY_set_public_key(n->ecdsa,
- EC_KEY_get0_public_key(k->ecdsa)) != 1) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if (k->type != KEY_ECDSA_SK && k->type != KEY_ECDSA_SK_CERT)
- break;
- /* Append security-key application string */
- if ((n->sk_application = strdup(k->sk_application)) == NULL)
- goto out;
- break;
- # endif /* OPENSSL_HAS_ECC */
- case KEY_RSA:
- case KEY_RSA_CERT:
- RSA_get0_key(k->rsa, &rsa_n, &rsa_e, NULL);
- if ((rsa_n_dup = BN_dup(rsa_n)) == NULL ||
- (rsa_e_dup = BN_dup(rsa_e)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (!RSA_set0_key(n->rsa, rsa_n_dup, rsa_e_dup, NULL)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- rsa_n_dup = rsa_e_dup = NULL; /* transferred */
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- case KEY_ED25519_CERT:
- case KEY_ED25519_SK:
- case KEY_ED25519_SK_CERT:
- if (k->ed25519_pk != NULL) {
- if ((n->ed25519_pk = malloc(ED25519_PK_SZ)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- memcpy(n->ed25519_pk, k->ed25519_pk, ED25519_PK_SZ);
- }
- if (k->type != KEY_ED25519_SK &&
- k->type != KEY_ED25519_SK_CERT)
- break;
- /* Append security-key application string */
- if ((n->sk_application = strdup(k->sk_application)) == NULL)
- goto out;
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- if ((r = sshkey_xmss_init(n, k->xmss_name)) != 0)
- goto out;
- if (k->xmss_pk != NULL) {
- u_int32_t left;
- size_t pklen = sshkey_xmss_pklen(k);
- if (pklen == 0 || sshkey_xmss_pklen(n) != pklen) {
- r = SSH_ERR_INTERNAL_ERROR;
- goto out;
- }
- if ((n->xmss_pk = malloc(pklen)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- memcpy(n->xmss_pk, k->xmss_pk, pklen);
- /* simulate number of signatures left on pubkey */
- left = sshkey_xmss_signatures_left(k);
- if (left)
- sshkey_xmss_enable_maxsign(n, left);
- }
- break;
- #endif /* WITH_XMSS */
- default:
- r = SSH_ERR_KEY_TYPE_UNKNOWN;
- goto out;
- }
- if (sshkey_is_cert(k) && (r = sshkey_cert_copy(k, n)) != 0)
- goto out;
- /* success */
- *pkp = n;
- n = NULL;
- r = 0;
- out:
- sshkey_free(n);
- #ifdef WITH_OPENSSL
- BN_clear_free(rsa_n_dup);
- BN_clear_free(rsa_e_dup);
- BN_clear_free(dsa_p_dup);
- BN_clear_free(dsa_q_dup);
- BN_clear_free(dsa_g_dup);
- BN_clear_free(dsa_pub_key_dup);
- #endif
- return r;
- }
- int
- sshkey_is_shielded(struct sshkey *k)
- {
- return k != NULL && k->shielded_private != NULL;
- }
- int
- sshkey_shield_private(struct sshkey *k)
- {
- struct sshbuf *prvbuf = NULL;
- u_char *prekey = NULL, *enc = NULL, keyiv[SSH_DIGEST_MAX_LENGTH];
- struct sshcipher_ctx *cctx = NULL;
- const struct sshcipher *cipher;
- size_t i, enclen = 0;
- struct sshkey *kswap = NULL, tmp;
- int r = SSH_ERR_INTERNAL_ERROR;
- #ifdef DEBUG_PK
- fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
- #endif
- if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
- ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
- r = SSH_ERR_INTERNAL_ERROR;
- goto out;
- }
- /* Prepare a random pre-key, and from it an ephemeral key */
- if ((prekey = malloc(SSHKEY_SHIELD_PREKEY_LEN)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- arc4random_buf(prekey, SSHKEY_SHIELD_PREKEY_LEN);
- if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
- prekey, SSHKEY_SHIELD_PREKEY_LEN,
- keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
- goto out;
- #ifdef DEBUG_PK
- fprintf(stderr, "%s: key+iv\n", __func__);
- sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
- stderr);
- #endif
- if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
- keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 1)) != 0)
- goto out;
- /* Serialise and encrypt the private key using the ephemeral key */
- if ((prvbuf = sshbuf_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (sshkey_is_shielded(k) && (r = sshkey_unshield_private(k)) != 0)
- goto out;
- if ((r = sshkey_private_serialize_opt(k, prvbuf,
- SSHKEY_SERIALIZE_SHIELD)) != 0)
- goto out;
- /* pad to cipher blocksize */
- i = 0;
- while (sshbuf_len(prvbuf) % cipher_blocksize(cipher)) {
- if ((r = sshbuf_put_u8(prvbuf, ++i & 0xff)) != 0)
- goto out;
- }
- #ifdef DEBUG_PK
- fprintf(stderr, "%s: serialised\n", __func__);
- sshbuf_dump(prvbuf, stderr);
- #endif
- /* encrypt */
- enclen = sshbuf_len(prvbuf);
- if ((enc = malloc(enclen)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if ((r = cipher_crypt(cctx, 0, enc,
- sshbuf_ptr(prvbuf), sshbuf_len(prvbuf), 0, 0)) != 0)
- goto out;
- #ifdef DEBUG_PK
- fprintf(stderr, "%s: encrypted\n", __func__);
- sshbuf_dump_data(enc, enclen, stderr);
- #endif
- /* Make a scrubbed, public-only copy of our private key argument */
- if ((r = sshkey_from_private(k, &kswap)) != 0)
- goto out;
- /* Swap the private key out (it will be destroyed below) */
- tmp = *kswap;
- *kswap = *k;
- *k = tmp;
- /* Insert the shielded key into our argument */
- k->shielded_private = enc;
- k->shielded_len = enclen;
- k->shield_prekey = prekey;
- k->shield_prekey_len = SSHKEY_SHIELD_PREKEY_LEN;
- enc = prekey = NULL; /* transferred */
- enclen = 0;
- /* preserve key fields that are required for correct operation */
- k->sk_flags = kswap->sk_flags;
- /* success */
- r = 0;
- out:
- /* XXX behaviour on error - invalidate original private key? */
- cipher_free(cctx);
- explicit_bzero(keyiv, sizeof(keyiv));
- explicit_bzero(&tmp, sizeof(tmp));
- freezero(enc, enclen);
- freezero(prekey, SSHKEY_SHIELD_PREKEY_LEN);
- sshkey_free(kswap);
- sshbuf_free(prvbuf);
- return r;
- }
- int
- sshkey_unshield_private(struct sshkey *k)
- {
- struct sshbuf *prvbuf = NULL;
- u_char pad, *cp, keyiv[SSH_DIGEST_MAX_LENGTH];
- struct sshcipher_ctx *cctx = NULL;
- const struct sshcipher *cipher;
- size_t i;
- struct sshkey *kswap = NULL, tmp;
- int r = SSH_ERR_INTERNAL_ERROR;
- #ifdef DEBUG_PK
- fprintf(stderr, "%s: entering for %s\n", __func__, sshkey_ssh_name(k));
- #endif
- if (!sshkey_is_shielded(k))
- return 0; /* nothing to do */
- if ((cipher = cipher_by_name(SSHKEY_SHIELD_CIPHER)) == NULL) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if (cipher_keylen(cipher) + cipher_ivlen(cipher) >
- ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH)) {
- r = SSH_ERR_INTERNAL_ERROR;
- goto out;
- }
- /* check size of shielded key blob */
- if (k->shielded_len < cipher_blocksize(cipher) ||
- (k->shielded_len % cipher_blocksize(cipher)) != 0) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* Calculate the ephemeral key from the prekey */
- if ((r = ssh_digest_memory(SSHKEY_SHIELD_PREKEY_HASH,
- k->shield_prekey, k->shield_prekey_len,
- keyiv, SSH_DIGEST_MAX_LENGTH)) != 0)
- goto out;
- if ((r = cipher_init(&cctx, cipher, keyiv, cipher_keylen(cipher),
- keyiv + cipher_keylen(cipher), cipher_ivlen(cipher), 0)) != 0)
- goto out;
- #ifdef DEBUG_PK
- fprintf(stderr, "%s: key+iv\n", __func__);
- sshbuf_dump_data(keyiv, ssh_digest_bytes(SSHKEY_SHIELD_PREKEY_HASH),
- stderr);
- #endif
- /* Decrypt and parse the shielded private key using the ephemeral key */
- if ((prvbuf = sshbuf_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if ((r = sshbuf_reserve(prvbuf, k->shielded_len, &cp)) != 0)
- goto out;
- /* decrypt */
- #ifdef DEBUG_PK
- fprintf(stderr, "%s: encrypted\n", __func__);
- sshbuf_dump_data(k->shielded_private, k->shielded_len, stderr);
- #endif
- if ((r = cipher_crypt(cctx, 0, cp,
- k->shielded_private, k->shielded_len, 0, 0)) != 0)
- goto out;
- #ifdef DEBUG_PK
- fprintf(stderr, "%s: serialised\n", __func__);
- sshbuf_dump(prvbuf, stderr);
- #endif
- /* Parse private key */
- if ((r = sshkey_private_deserialize(prvbuf, &kswap)) != 0)
- goto out;
- /* Check deterministic padding */
- i = 0;
- while (sshbuf_len(prvbuf)) {
- if ((r = sshbuf_get_u8(prvbuf, &pad)) != 0)
- goto out;
- if (pad != (++i & 0xff)) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- }
- /* Swap the parsed key back into place */
- tmp = *kswap;
- *kswap = *k;
- *k = tmp;
- /* success */
- r = 0;
- out:
- cipher_free(cctx);
- explicit_bzero(keyiv, sizeof(keyiv));
- explicit_bzero(&tmp, sizeof(tmp));
- sshkey_free(kswap);
- sshbuf_free(prvbuf);
- return r;
- }
- static int
- cert_parse(struct sshbuf *b, struct sshkey *key, struct sshbuf *certbuf)
- {
- struct sshbuf *principals = NULL, *crit = NULL;
- struct sshbuf *exts = NULL, *ca = NULL;
- u_char *sig = NULL;
- size_t signed_len = 0, slen = 0, kidlen = 0;
- int ret = SSH_ERR_INTERNAL_ERROR;
- /* Copy the entire key blob for verification and later serialisation */
- if ((ret = sshbuf_putb(key->cert->certblob, certbuf)) != 0)
- return ret;
- /* Parse body of certificate up to signature */
- if ((ret = sshbuf_get_u64(b, &key->cert->serial)) != 0 ||
- (ret = sshbuf_get_u32(b, &key->cert->type)) != 0 ||
- (ret = sshbuf_get_cstring(b, &key->cert->key_id, &kidlen)) != 0 ||
- (ret = sshbuf_froms(b, &principals)) != 0 ||
- (ret = sshbuf_get_u64(b, &key->cert->valid_after)) != 0 ||
- (ret = sshbuf_get_u64(b, &key->cert->valid_before)) != 0 ||
- (ret = sshbuf_froms(b, &crit)) != 0 ||
- (ret = sshbuf_froms(b, &exts)) != 0 ||
- (ret = sshbuf_get_string_direct(b, NULL, NULL)) != 0 ||
- (ret = sshbuf_froms(b, &ca)) != 0) {
- /* XXX debug print error for ret */
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* Signature is left in the buffer so we can calculate this length */
- signed_len = sshbuf_len(key->cert->certblob) - sshbuf_len(b);
- if ((ret = sshbuf_get_string(b, &sig, &slen)) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if (key->cert->type != SSH2_CERT_TYPE_USER &&
- key->cert->type != SSH2_CERT_TYPE_HOST) {
- ret = SSH_ERR_KEY_CERT_UNKNOWN_TYPE;
- goto out;
- }
- /* Parse principals section */
- while (sshbuf_len(principals) > 0) {
- char *principal = NULL;
- char **oprincipals = NULL;
- if (key->cert->nprincipals >= SSHKEY_CERT_MAX_PRINCIPALS) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if ((ret = sshbuf_get_cstring(principals, &principal,
- NULL)) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- oprincipals = key->cert->principals;
- key->cert->principals = recallocarray(key->cert->principals,
- key->cert->nprincipals, key->cert->nprincipals + 1,
- sizeof(*key->cert->principals));
- if (key->cert->principals == NULL) {
- free(principal);
- key->cert->principals = oprincipals;
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- key->cert->principals[key->cert->nprincipals++] = principal;
- }
- /*
- * Stash a copies of the critical options and extensions sections
- * for later use.
- */
- if ((ret = sshbuf_putb(key->cert->critical, crit)) != 0 ||
- (exts != NULL &&
- (ret = sshbuf_putb(key->cert->extensions, exts)) != 0))
- goto out;
- /*
- * Validate critical options and extensions sections format.
- */
- while (sshbuf_len(crit) != 0) {
- if ((ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0 ||
- (ret = sshbuf_get_string_direct(crit, NULL, NULL)) != 0) {
- sshbuf_reset(key->cert->critical);
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- }
- while (exts != NULL && sshbuf_len(exts) != 0) {
- if ((ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0 ||
- (ret = sshbuf_get_string_direct(exts, NULL, NULL)) != 0) {
- sshbuf_reset(key->cert->extensions);
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- }
- /* Parse CA key and check signature */
- if (sshkey_from_blob_internal(ca, &key->cert->signature_key, 0) != 0) {
- ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
- goto out;
- }
- if (!sshkey_type_is_valid_ca(key->cert->signature_key->type)) {
- ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
- goto out;
- }
- if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
- sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0, NULL)) != 0)
- goto out;
- if ((ret = sshkey_get_sigtype(sig, slen,
- &key->cert->signature_type)) != 0)
- goto out;
- /* Success */
- ret = 0;
- out:
- sshbuf_free(ca);
- sshbuf_free(crit);
- sshbuf_free(exts);
- sshbuf_free(principals);
- free(sig);
- return ret;
- }
- #ifdef WITH_OPENSSL
- static int
- check_rsa_length(const RSA *rsa)
- {
- const BIGNUM *rsa_n;
- RSA_get0_key(rsa, &rsa_n, NULL, NULL);
- if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
- return SSH_ERR_KEY_LENGTH;
- return 0;
- }
- #endif
- static int
- sshkey_from_blob_internal(struct sshbuf *b, struct sshkey **keyp,
- int allow_cert)
- {
- int type, ret = SSH_ERR_INTERNAL_ERROR;
- char *ktype = NULL, *curve = NULL, *xmss_name = NULL;
- struct sshkey *key = NULL;
- size_t len;
- u_char *pk = NULL;
- struct sshbuf *copy;
- #if defined(WITH_OPENSSL)
- BIGNUM *rsa_n = NULL, *rsa_e = NULL;
- BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL, *dsa_pub_key = NULL;
- # if defined(OPENSSL_HAS_ECC)
- EC_POINT *q = NULL;
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- #ifdef DEBUG_PK /* XXX */
- sshbuf_dump(b, stderr);
- #endif
- if (keyp != NULL)
- *keyp = NULL;
- if ((copy = sshbuf_fromb(b)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- type = sshkey_type_from_name(ktype);
- if (!allow_cert && sshkey_type_is_cert(type)) {
- ret = SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
- goto out;
- }
- switch (type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA_CERT:
- /* Skip nonce */
- if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* FALLTHROUGH */
- case KEY_RSA:
- if ((key = sshkey_new(type)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (sshbuf_get_bignum2(b, &rsa_e) != 0 ||
- sshbuf_get_bignum2(b, &rsa_n) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, NULL)) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- rsa_n = rsa_e = NULL; /* transferred */
- if ((ret = check_rsa_length(key->rsa)) != 0)
- goto out;
- #ifdef DEBUG_PK
- RSA_print_fp(stderr, key->rsa, 8);
- #endif
- break;
- case KEY_DSA_CERT:
- /* Skip nonce */
- if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* FALLTHROUGH */
- case KEY_DSA:
- if ((key = sshkey_new(type)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (sshbuf_get_bignum2(b, &dsa_p) != 0 ||
- sshbuf_get_bignum2(b, &dsa_q) != 0 ||
- sshbuf_get_bignum2(b, &dsa_g) != 0 ||
- sshbuf_get_bignum2(b, &dsa_pub_key) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g)) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- dsa_p = dsa_q = dsa_g = NULL; /* transferred */
- if (!DSA_set0_key(key->dsa, dsa_pub_key, NULL)) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- dsa_pub_key = NULL; /* transferred */
- #ifdef DEBUG_PK
- DSA_print_fp(stderr, key->dsa, 8);
- #endif
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_CERT:
- case KEY_ECDSA_SK_CERT:
- /* Skip nonce */
- if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* FALLTHROUGH */
- case KEY_ECDSA:
- case KEY_ECDSA_SK:
- if ((key = sshkey_new(type)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- key->ecdsa_nid = sshkey_ecdsa_nid_from_name(ktype);
- if (sshbuf_get_cstring(b, &curve, NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if (key->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
- ret = SSH_ERR_EC_CURVE_MISMATCH;
- goto out;
- }
- EC_KEY_free(key->ecdsa);
- if ((key->ecdsa = EC_KEY_new_by_curve_name(key->ecdsa_nid))
- == NULL) {
- ret = SSH_ERR_EC_CURVE_INVALID;
- goto out;
- }
- if ((q = EC_POINT_new(EC_KEY_get0_group(key->ecdsa))) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (sshbuf_get_ec(b, q, EC_KEY_get0_group(key->ecdsa)) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if (sshkey_ec_validate_public(EC_KEY_get0_group(key->ecdsa),
- q) != 0) {
- ret = SSH_ERR_KEY_INVALID_EC_VALUE;
- goto out;
- }
- if (EC_KEY_set_public_key(key->ecdsa, q) != 1) {
- /* XXX assume it is a allocation error */
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- #ifdef DEBUG_PK
- sshkey_dump_ec_point(EC_KEY_get0_group(key->ecdsa), q);
- #endif
- if (type == KEY_ECDSA_SK || type == KEY_ECDSA_SK_CERT) {
- /* Parse additional security-key application string */
- if (sshbuf_get_cstring(b, &key->sk_application,
- NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- #ifdef DEBUG_PK
- fprintf(stderr, "App: %s\n", key->sk_application);
- #endif
- }
- break;
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- case KEY_ED25519_CERT:
- case KEY_ED25519_SK_CERT:
- /* Skip nonce */
- if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* FALLTHROUGH */
- case KEY_ED25519:
- case KEY_ED25519_SK:
- if ((ret = sshbuf_get_string(b, &pk, &len)) != 0)
- goto out;
- if (len != ED25519_PK_SZ) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if ((key = sshkey_new(type)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (type == KEY_ED25519_SK || type == KEY_ED25519_SK_CERT) {
- /* Parse additional security-key application string */
- if (sshbuf_get_cstring(b, &key->sk_application,
- NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- #ifdef DEBUG_PK
- fprintf(stderr, "App: %s\n", key->sk_application);
- #endif
- }
- key->ed25519_pk = pk;
- pk = NULL;
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS_CERT:
- /* Skip nonce */
- if (sshbuf_get_string_direct(b, NULL, NULL) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* FALLTHROUGH */
- case KEY_XMSS:
- if ((ret = sshbuf_get_cstring(b, &xmss_name, NULL)) != 0)
- goto out;
- if ((key = sshkey_new(type)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if ((ret = sshkey_xmss_init(key, xmss_name)) != 0)
- goto out;
- if ((ret = sshbuf_get_string(b, &pk, &len)) != 0)
- goto out;
- if (len == 0 || len != sshkey_xmss_pklen(key)) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- key->xmss_pk = pk;
- pk = NULL;
- if (type != KEY_XMSS_CERT &&
- (ret = sshkey_xmss_deserialize_pk_info(key, b)) != 0)
- goto out;
- break;
- #endif /* WITH_XMSS */
- case KEY_UNSPEC:
- default:
- ret = SSH_ERR_KEY_TYPE_UNKNOWN;
- goto out;
- }
- /* Parse certificate potion */
- if (sshkey_is_cert(key) && (ret = cert_parse(b, key, copy)) != 0)
- goto out;
- if (key != NULL && sshbuf_len(b) != 0) {
- ret = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- ret = 0;
- if (keyp != NULL) {
- *keyp = key;
- key = NULL;
- }
- out:
- sshbuf_free(copy);
- sshkey_free(key);
- free(xmss_name);
- free(ktype);
- free(curve);
- free(pk);
- #if defined(WITH_OPENSSL)
- BN_clear_free(rsa_n);
- BN_clear_free(rsa_e);
- BN_clear_free(dsa_p);
- BN_clear_free(dsa_q);
- BN_clear_free(dsa_g);
- BN_clear_free(dsa_pub_key);
- # if defined(OPENSSL_HAS_ECC)
- EC_POINT_free(q);
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- return ret;
- }
- int
- sshkey_from_blob(const u_char *blob, size_t blen, struct sshkey **keyp)
- {
- struct sshbuf *b;
- int r;
- if ((b = sshbuf_from(blob, blen)) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- r = sshkey_from_blob_internal(b, keyp, 1);
- sshbuf_free(b);
- return r;
- }
- int
- sshkey_fromb(struct sshbuf *b, struct sshkey **keyp)
- {
- return sshkey_from_blob_internal(b, keyp, 1);
- }
- int
- sshkey_froms(struct sshbuf *buf, struct sshkey **keyp)
- {
- struct sshbuf *b;
- int r;
- if ((r = sshbuf_froms(buf, &b)) != 0)
- return r;
- r = sshkey_from_blob_internal(b, keyp, 1);
- sshbuf_free(b);
- return r;
- }
- int
- sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
- {
- int r;
- struct sshbuf *b = NULL;
- char *sigtype = NULL;
- if (sigtypep != NULL)
- *sigtypep = NULL;
- if ((b = sshbuf_from(sig, siglen)) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- if ((r = sshbuf_get_cstring(b, &sigtype, NULL)) != 0)
- goto out;
- /* success */
- if (sigtypep != NULL) {
- *sigtypep = sigtype;
- sigtype = NULL;
- }
- r = 0;
- out:
- free(sigtype);
- sshbuf_free(b);
- return r;
- }
- /*
- *
- * Checks whether a certificate's signature type is allowed.
- * Returns 0 (success) if the certificate signature type appears in the
- * "allowed" pattern-list, or the key is not a certificate to begin with.
- * Otherwise returns a ssherr.h code.
- */
- int
- sshkey_check_cert_sigtype(const struct sshkey *key, const char *allowed)
- {
- if (key == NULL || allowed == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if (!sshkey_type_is_cert(key->type))
- return 0;
- if (key->cert == NULL || key->cert->signature_type == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if (match_pattern_list(key->cert->signature_type, allowed, 0) != 1)
- return SSH_ERR_SIGN_ALG_UNSUPPORTED;
- return 0;
- }
- /*
- * Returns the expected signature algorithm for a given public key algorithm.
- */
- const char *
- sshkey_sigalg_by_name(const char *name)
- {
- const struct keytype *kt;
- for (kt = keytypes; kt->type != -1; kt++) {
- if (strcmp(kt->name, name) != 0)
- continue;
- if (kt->sigalg != NULL)
- return kt->sigalg;
- if (!kt->cert)
- return kt->name;
- return sshkey_ssh_name_from_type_nid(
- sshkey_type_plain(kt->type), kt->nid);
- }
- return NULL;
- }
- /*
- * Verifies that the signature algorithm appearing inside the signature blob
- * matches that which was requested.
- */
- int
- sshkey_check_sigtype(const u_char *sig, size_t siglen,
- const char *requested_alg)
- {
- const char *expected_alg;
- char *sigtype = NULL;
- int r;
- if (requested_alg == NULL)
- return 0;
- if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0)
- return r;
- r = strcmp(expected_alg, sigtype) == 0;
- free(sigtype);
- return r ? 0 : SSH_ERR_SIGN_ALG_UNSUPPORTED;
- }
- int
- sshkey_sign(struct sshkey *key,
- u_char **sigp, size_t *lenp,
- const u_char *data, size_t datalen,
- const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
- {
- int was_shielded = sshkey_is_shielded(key);
- int r2, r = SSH_ERR_INTERNAL_ERROR;
- if (sigp != NULL)
- *sigp = NULL;
- if (lenp != NULL)
- *lenp = 0;
- if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
- return SSH_ERR_INVALID_ARGUMENT;
- if ((r = sshkey_unshield_private(key)) != 0)
- return r;
- switch (key->type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA_CERT:
- case KEY_DSA:
- r = ssh_dss_sign(key, sigp, lenp, data, datalen, compat);
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_CERT:
- case KEY_ECDSA:
- r = ssh_ecdsa_sign(key, sigp, lenp, data, datalen, compat);
- break;
- # endif /* OPENSSL_HAS_ECC */
- case KEY_RSA_CERT:
- case KEY_RSA:
- r = ssh_rsa_sign(key, sigp, lenp, data, datalen, alg);
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- case KEY_ED25519_CERT:
- r = ssh_ed25519_sign(key, sigp, lenp, data, datalen, compat);
- break;
- case KEY_ED25519_SK:
- case KEY_ED25519_SK_CERT:
- case KEY_ECDSA_SK_CERT:
- case KEY_ECDSA_SK:
- r = sshsk_sign(sk_provider, key, sigp, lenp, data,
- datalen, compat, sk_pin);
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- r = ssh_xmss_sign(key, sigp, lenp, data, datalen, compat);
- break;
- #endif /* WITH_XMSS */
- default:
- r = SSH_ERR_KEY_TYPE_UNKNOWN;
- break;
- }
- if (was_shielded && (r2 = sshkey_shield_private(key)) != 0)
- return r2;
- return r;
- }
- /*
- * ssh_key_verify returns 0 for a correct signature and < 0 on error.
- * If "alg" specified, then the signature must use that algorithm.
- */
- int
- sshkey_verify(const struct sshkey *key,
- const u_char *sig, size_t siglen,
- const u_char *data, size_t dlen, const char *alg, u_int compat,
- struct sshkey_sig_details **detailsp)
- {
- if (detailsp != NULL)
- *detailsp = NULL;
- if (siglen == 0 || dlen > SSH_KEY_MAX_SIGN_DATA_SIZE)
- return SSH_ERR_INVALID_ARGUMENT;
- switch (key->type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA_CERT:
- case KEY_DSA:
- return ssh_dss_verify(key, sig, siglen, data, dlen, compat);
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_CERT:
- case KEY_ECDSA:
- return ssh_ecdsa_verify(key, sig, siglen, data, dlen, compat);
- case KEY_ECDSA_SK_CERT:
- case KEY_ECDSA_SK:
- return ssh_ecdsa_sk_verify(key, sig, siglen, data, dlen,
- compat, detailsp);
- # endif /* OPENSSL_HAS_ECC */
- case KEY_RSA_CERT:
- case KEY_RSA:
- return ssh_rsa_verify(key, sig, siglen, data, dlen, alg);
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- case KEY_ED25519_CERT:
- return ssh_ed25519_verify(key, sig, siglen, data, dlen, compat);
- case KEY_ED25519_SK:
- case KEY_ED25519_SK_CERT:
- return ssh_ed25519_sk_verify(key, sig, siglen, data, dlen,
- compat, detailsp);
- #ifdef WITH_XMSS
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- return ssh_xmss_verify(key, sig, siglen, data, dlen, compat);
- #endif /* WITH_XMSS */
- default:
- return SSH_ERR_KEY_TYPE_UNKNOWN;
- }
- }
- /* Convert a plain key to their _CERT equivalent */
- int
- sshkey_to_certified(struct sshkey *k)
- {
- int newtype;
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA:
- newtype = KEY_RSA_CERT;
- break;
- case KEY_DSA:
- newtype = KEY_DSA_CERT;
- break;
- case KEY_ECDSA:
- newtype = KEY_ECDSA_CERT;
- break;
- case KEY_ECDSA_SK:
- newtype = KEY_ECDSA_SK_CERT;
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519_SK:
- newtype = KEY_ED25519_SK_CERT;
- break;
- case KEY_ED25519:
- newtype = KEY_ED25519_CERT;
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- newtype = KEY_XMSS_CERT;
- break;
- #endif /* WITH_XMSS */
- default:
- return SSH_ERR_INVALID_ARGUMENT;
- }
- if ((k->cert = cert_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- k->type = newtype;
- return 0;
- }
- /* Convert a certificate to its raw key equivalent */
- int
- sshkey_drop_cert(struct sshkey *k)
- {
- if (!sshkey_type_is_cert(k->type))
- return SSH_ERR_KEY_TYPE_UNKNOWN;
- cert_free(k->cert);
- k->cert = NULL;
- k->type = sshkey_type_plain(k->type);
- return 0;
- }
- /* Sign a certified key, (re-)generating the signed certblob. */
- int
- sshkey_certify_custom(struct sshkey *k, struct sshkey *ca, const char *alg,
- const char *sk_provider, const char *sk_pin,
- sshkey_certify_signer *signer, void *signer_ctx)
- {
- struct sshbuf *principals = NULL;
- u_char *ca_blob = NULL, *sig_blob = NULL, nonce[32];
- size_t i, ca_len, sig_len;
- int ret = SSH_ERR_INTERNAL_ERROR;
- struct sshbuf *cert = NULL;
- char *sigtype = NULL;
- #ifdef WITH_OPENSSL
- const BIGNUM *rsa_n, *rsa_e, *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key;
- #endif /* WITH_OPENSSL */
- if (k == NULL || k->cert == NULL ||
- k->cert->certblob == NULL || ca == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if (!sshkey_is_cert(k))
- return SSH_ERR_KEY_TYPE_UNKNOWN;
- if (!sshkey_type_is_valid_ca(ca->type))
- return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
- /*
- * If no alg specified as argument but a signature_type was set,
- * then prefer that. If both were specified, then they must match.
- */
- if (alg == NULL)
- alg = k->cert->signature_type;
- else if (k->cert->signature_type != NULL &&
- strcmp(alg, k->cert->signature_type) != 0)
- return SSH_ERR_INVALID_ARGUMENT;
- /*
- * If no signing algorithm or signature_type was specified and we're
- * using a RSA key, then default to a good signature algorithm.
- */
- if (alg == NULL && ca->type == KEY_RSA)
- alg = "rsa-sha2-512";
- if ((ret = sshkey_to_blob(ca, &ca_blob, &ca_len)) != 0)
- return SSH_ERR_KEY_CERT_INVALID_SIGN_KEY;
- cert = k->cert->certblob; /* for readability */
- sshbuf_reset(cert);
- if ((ret = sshbuf_put_cstring(cert, sshkey_ssh_name(k))) != 0)
- goto out;
- /* -v01 certs put nonce first */
- arc4random_buf(&nonce, sizeof(nonce));
- if ((ret = sshbuf_put_string(cert, nonce, sizeof(nonce))) != 0)
- goto out;
- /* XXX this substantially duplicates to_blob(); refactor */
- switch (k->type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA_CERT:
- DSA_get0_pqg(k->dsa, &dsa_p, &dsa_q, &dsa_g);
- DSA_get0_key(k->dsa, &dsa_pub_key, NULL);
- if ((ret = sshbuf_put_bignum2(cert, dsa_p)) != 0 ||
- (ret = sshbuf_put_bignum2(cert, dsa_q)) != 0 ||
- (ret = sshbuf_put_bignum2(cert, dsa_g)) != 0 ||
- (ret = sshbuf_put_bignum2(cert, dsa_pub_key)) != 0)
- goto out;
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA_CERT:
- case KEY_ECDSA_SK_CERT:
- if ((ret = sshbuf_put_cstring(cert,
- sshkey_curve_nid_to_name(k->ecdsa_nid))) != 0 ||
- (ret = sshbuf_put_ec(cert,
- EC_KEY_get0_public_key(k->ecdsa),
- EC_KEY_get0_group(k->ecdsa))) != 0)
- goto out;
- if (k->type == KEY_ECDSA_SK_CERT) {
- if ((ret = sshbuf_put_cstring(cert,
- k->sk_application)) != 0)
- goto out;
- }
- break;
- # endif /* OPENSSL_HAS_ECC */
- case KEY_RSA_CERT:
- RSA_get0_key(k->rsa, &rsa_n, &rsa_e, NULL);
- if ((ret = sshbuf_put_bignum2(cert, rsa_e)) != 0 ||
- (ret = sshbuf_put_bignum2(cert, rsa_n)) != 0)
- goto out;
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519_CERT:
- case KEY_ED25519_SK_CERT:
- if ((ret = sshbuf_put_string(cert,
- k->ed25519_pk, ED25519_PK_SZ)) != 0)
- goto out;
- if (k->type == KEY_ED25519_SK_CERT) {
- if ((ret = sshbuf_put_cstring(cert,
- k->sk_application)) != 0)
- goto out;
- }
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS_CERT:
- if (k->xmss_name == NULL) {
- ret = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((ret = sshbuf_put_cstring(cert, k->xmss_name)) ||
- (ret = sshbuf_put_string(cert,
- k->xmss_pk, sshkey_xmss_pklen(k))) != 0)
- goto out;
- break;
- #endif /* WITH_XMSS */
- default:
- ret = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((ret = sshbuf_put_u64(cert, k->cert->serial)) != 0 ||
- (ret = sshbuf_put_u32(cert, k->cert->type)) != 0 ||
- (ret = sshbuf_put_cstring(cert, k->cert->key_id)) != 0)
- goto out;
- if ((principals = sshbuf_new()) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- for (i = 0; i < k->cert->nprincipals; i++) {
- if ((ret = sshbuf_put_cstring(principals,
- k->cert->principals[i])) != 0)
- goto out;
- }
- if ((ret = sshbuf_put_stringb(cert, principals)) != 0 ||
- (ret = sshbuf_put_u64(cert, k->cert->valid_after)) != 0 ||
- (ret = sshbuf_put_u64(cert, k->cert->valid_before)) != 0 ||
- (ret = sshbuf_put_stringb(cert, k->cert->critical)) != 0 ||
- (ret = sshbuf_put_stringb(cert, k->cert->extensions)) != 0 ||
- (ret = sshbuf_put_string(cert, NULL, 0)) != 0 || /* Reserved */
- (ret = sshbuf_put_string(cert, ca_blob, ca_len)) != 0)
- goto out;
- /* Sign the whole mess */
- if ((ret = signer(ca, &sig_blob, &sig_len, sshbuf_ptr(cert),
- sshbuf_len(cert), alg, sk_provider, sk_pin, 0, signer_ctx)) != 0)
- goto out;
- /* Check and update signature_type against what was actually used */
- if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0)
- goto out;
- if (alg != NULL && strcmp(alg, sigtype) != 0) {
- ret = SSH_ERR_SIGN_ALG_UNSUPPORTED;
- goto out;
- }
- if (k->cert->signature_type == NULL) {
- k->cert->signature_type = sigtype;
- sigtype = NULL;
- }
- /* Append signature and we are done */
- if ((ret = sshbuf_put_string(cert, sig_blob, sig_len)) != 0)
- goto out;
- ret = 0;
- out:
- if (ret != 0)
- sshbuf_reset(cert);
- free(sig_blob);
- free(ca_blob);
- free(sigtype);
- sshbuf_free(principals);
- return ret;
- }
- static int
- default_key_sign(struct sshkey *key, u_char **sigp, size_t *lenp,
- const u_char *data, size_t datalen,
- const char *alg, const char *sk_provider, const char *sk_pin,
- u_int compat, void *ctx)
- {
- if (ctx != NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- return sshkey_sign(key, sigp, lenp, data, datalen, alg,
- sk_provider, sk_pin, compat);
- }
- int
- sshkey_certify(struct sshkey *k, struct sshkey *ca, const char *alg,
- const char *sk_provider, const char *sk_pin)
- {
- return sshkey_certify_custom(k, ca, alg, sk_provider, sk_pin,
- default_key_sign, NULL);
- }
- int
- sshkey_cert_check_authority(const struct sshkey *k,
- int want_host, int require_principal, int wildcard_pattern,
- const char *name, const char **reason)
- {
- u_int i, principal_matches;
- time_t now = time(NULL);
- if (reason == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if (!sshkey_is_cert(k)) {
- *reason = "Key is not a certificate";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- if (want_host) {
- if (k->cert->type != SSH2_CERT_TYPE_HOST) {
- *reason = "Certificate invalid: not a host certificate";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- } else {
- if (k->cert->type != SSH2_CERT_TYPE_USER) {
- *reason = "Certificate invalid: not a user certificate";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- }
- if (now < 0) {
- /* yikes - system clock before epoch! */
- *reason = "Certificate invalid: not yet valid";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- if ((u_int64_t)now < k->cert->valid_after) {
- *reason = "Certificate invalid: not yet valid";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- if ((u_int64_t)now >= k->cert->valid_before) {
- *reason = "Certificate invalid: expired";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- if (k->cert->nprincipals == 0) {
- if (require_principal) {
- *reason = "Certificate lacks principal list";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- } else if (name != NULL) {
- principal_matches = 0;
- for (i = 0; i < k->cert->nprincipals; i++) {
- if (wildcard_pattern) {
- if (match_pattern(k->cert->principals[i],
- name)) {
- principal_matches = 1;
- break;
- }
- } else if (strcmp(name, k->cert->principals[i]) == 0) {
- principal_matches = 1;
- break;
- }
- }
- if (!principal_matches) {
- *reason = "Certificate invalid: name is not a listed "
- "principal";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- }
- return 0;
- }
- int
- sshkey_cert_check_host(const struct sshkey *key, const char *host,
- int wildcard_principals, const char *ca_sign_algorithms,
- const char **reason)
- {
- int r;
- if ((r = sshkey_cert_check_authority(key, 1, 0, wildcard_principals,
- host, reason)) != 0)
- return r;
- if (sshbuf_len(key->cert->critical) != 0) {
- *reason = "Certificate contains unsupported critical options";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- if (ca_sign_algorithms != NULL &&
- (r = sshkey_check_cert_sigtype(key, ca_sign_algorithms)) != 0) {
- *reason = "Certificate signed with disallowed algorithm";
- return SSH_ERR_KEY_CERT_INVALID;
- }
- return 0;
- }
- size_t
- sshkey_format_cert_validity(const struct sshkey_cert *cert, char *s, size_t l)
- {
- char from[32], to[32], ret[128];
- time_t tt;
- struct tm *tm;
- *from = *to = '\0';
- if (cert->valid_after == 0 &&
- cert->valid_before == 0xffffffffffffffffULL)
- return strlcpy(s, "forever", l);
- if (cert->valid_after != 0) {
- /* XXX revisit INT_MAX in 2038 :) */
- tt = cert->valid_after > INT_MAX ?
- INT_MAX : cert->valid_after;
- tm = localtime(&tt);
- strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
- }
- if (cert->valid_before != 0xffffffffffffffffULL) {
- /* XXX revisit INT_MAX in 2038 :) */
- tt = cert->valid_before > INT_MAX ?
- INT_MAX : cert->valid_before;
- tm = localtime(&tt);
- strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
- }
- if (cert->valid_after == 0)
- snprintf(ret, sizeof(ret), "before %s", to);
- else if (cert->valid_before == 0xffffffffffffffffULL)
- snprintf(ret, sizeof(ret), "after %s", from);
- else
- snprintf(ret, sizeof(ret), "from %s to %s", from, to);
- return strlcpy(s, ret, l);
- }
- int
- sshkey_private_serialize_opt(struct sshkey *key, struct sshbuf *buf,
- enum sshkey_serialize_rep opts)
- {
- int r = SSH_ERR_INTERNAL_ERROR;
- int was_shielded = sshkey_is_shielded(key);
- struct sshbuf *b = NULL;
- #ifdef WITH_OPENSSL
- const BIGNUM *rsa_n, *rsa_e, *rsa_d, *rsa_iqmp, *rsa_p, *rsa_q;
- const BIGNUM *dsa_p, *dsa_q, *dsa_g, *dsa_pub_key, *dsa_priv_key;
- #endif /* WITH_OPENSSL */
- if ((r = sshkey_unshield_private(key)) != 0)
- return r;
- if ((b = sshbuf_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- if ((r = sshbuf_put_cstring(b, sshkey_ssh_name(key))) != 0)
- goto out;
- switch (key->type) {
- #ifdef WITH_OPENSSL
- case KEY_RSA:
- RSA_get0_key(key->rsa, &rsa_n, &rsa_e, &rsa_d);
- RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
- RSA_get0_crt_params(key->rsa, NULL, NULL, &rsa_iqmp);
- if ((r = sshbuf_put_bignum2(b, rsa_n)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_e)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_d)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_iqmp)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_p)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_q)) != 0)
- goto out;
- break;
- case KEY_RSA_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- RSA_get0_key(key->rsa, NULL, NULL, &rsa_d);
- RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
- RSA_get0_crt_params(key->rsa, NULL, NULL, &rsa_iqmp);
- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_d)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_iqmp)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_p)) != 0 ||
- (r = sshbuf_put_bignum2(b, rsa_q)) != 0)
- goto out;
- break;
- case KEY_DSA:
- DSA_get0_pqg(key->dsa, &dsa_p, &dsa_q, &dsa_g);
- DSA_get0_key(key->dsa, &dsa_pub_key, &dsa_priv_key);
- if ((r = sshbuf_put_bignum2(b, dsa_p)) != 0 ||
- (r = sshbuf_put_bignum2(b, dsa_q)) != 0 ||
- (r = sshbuf_put_bignum2(b, dsa_g)) != 0 ||
- (r = sshbuf_put_bignum2(b, dsa_pub_key)) != 0 ||
- (r = sshbuf_put_bignum2(b, dsa_priv_key)) != 0)
- goto out;
- break;
- case KEY_DSA_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- DSA_get0_key(key->dsa, NULL, &dsa_priv_key);
- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
- (r = sshbuf_put_bignum2(b, dsa_priv_key)) != 0)
- goto out;
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
- if ((r = sshbuf_put_cstring(b,
- sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
- (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 ||
- (r = sshbuf_put_bignum2(b,
- EC_KEY_get0_private_key(key->ecdsa))) != 0)
- goto out;
- break;
- case KEY_ECDSA_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
- (r = sshbuf_put_bignum2(b,
- EC_KEY_get0_private_key(key->ecdsa))) != 0)
- goto out;
- break;
- case KEY_ECDSA_SK:
- if ((r = sshbuf_put_cstring(b,
- sshkey_curve_nid_to_name(key->ecdsa_nid))) != 0 ||
- (r = sshbuf_put_eckey(b, key->ecdsa)) != 0 ||
- (r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
- (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
- (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
- (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
- goto out;
- break;
- case KEY_ECDSA_SK_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
- (r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
- (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
- (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
- (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
- goto out;
- break;
- # endif /* OPENSSL_HAS_ECC */
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- if ((r = sshbuf_put_string(b, key->ed25519_pk,
- ED25519_PK_SZ)) != 0 ||
- (r = sshbuf_put_string(b, key->ed25519_sk,
- ED25519_SK_SZ)) != 0)
- goto out;
- break;
- case KEY_ED25519_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
- (r = sshbuf_put_string(b, key->ed25519_pk,
- ED25519_PK_SZ)) != 0 ||
- (r = sshbuf_put_string(b, key->ed25519_sk,
- ED25519_SK_SZ)) != 0)
- goto out;
- break;
- case KEY_ED25519_SK:
- if ((r = sshbuf_put_string(b, key->ed25519_pk,
- ED25519_PK_SZ)) != 0 ||
- (r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
- (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
- (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
- (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
- goto out;
- break;
- case KEY_ED25519_SK_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
- (r = sshbuf_put_string(b, key->ed25519_pk,
- ED25519_PK_SZ)) != 0 ||
- (r = sshbuf_put_cstring(b, key->sk_application)) != 0 ||
- (r = sshbuf_put_u8(b, key->sk_flags)) != 0 ||
- (r = sshbuf_put_stringb(b, key->sk_key_handle)) != 0 ||
- (r = sshbuf_put_stringb(b, key->sk_reserved)) != 0)
- goto out;
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- if (key->xmss_name == NULL) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_put_cstring(b, key->xmss_name)) != 0 ||
- (r = sshbuf_put_string(b, key->xmss_pk,
- sshkey_xmss_pklen(key))) != 0 ||
- (r = sshbuf_put_string(b, key->xmss_sk,
- sshkey_xmss_sklen(key))) != 0 ||
- (r = sshkey_xmss_serialize_state_opt(key, b, opts)) != 0)
- goto out;
- break;
- case KEY_XMSS_CERT:
- if (key->cert == NULL || sshbuf_len(key->cert->certblob) == 0 ||
- key->xmss_name == NULL) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_put_stringb(b, key->cert->certblob)) != 0 ||
- (r = sshbuf_put_cstring(b, key->xmss_name)) != 0 ||
- (r = sshbuf_put_string(b, key->xmss_pk,
- sshkey_xmss_pklen(key))) != 0 ||
- (r = sshbuf_put_string(b, key->xmss_sk,
- sshkey_xmss_sklen(key))) != 0 ||
- (r = sshkey_xmss_serialize_state_opt(key, b, opts)) != 0)
- goto out;
- break;
- #endif /* WITH_XMSS */
- default:
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- /*
- * success (but we still need to append the output to buf after
- * possibly re-shielding the private key)
- */
- r = 0;
- out:
- if (was_shielded)
- r = sshkey_shield_private(key);
- if (r == 0)
- r = sshbuf_putb(buf, b);
- sshbuf_free(b);
- return r;
- }
- int
- sshkey_private_serialize(struct sshkey *key, struct sshbuf *b)
- {
- return sshkey_private_serialize_opt(key, b,
- SSHKEY_SERIALIZE_DEFAULT);
- }
- int
- sshkey_private_deserialize(struct sshbuf *buf, struct sshkey **kp)
- {
- char *tname = NULL, *curve = NULL, *xmss_name = NULL;
- char *expect_sk_application = NULL;
- struct sshkey *k = NULL;
- size_t pklen = 0, sklen = 0;
- int type, r = SSH_ERR_INTERNAL_ERROR;
- u_char *ed25519_pk = NULL, *ed25519_sk = NULL;
- u_char *expect_ed25519_pk = NULL;
- u_char *xmss_pk = NULL, *xmss_sk = NULL;
- #ifdef WITH_OPENSSL
- BIGNUM *exponent = NULL;
- BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
- BIGNUM *rsa_iqmp = NULL, *rsa_p = NULL, *rsa_q = NULL;
- BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
- BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL;
- #endif /* WITH_OPENSSL */
- if (kp != NULL)
- *kp = NULL;
- if ((r = sshbuf_get_cstring(buf, &tname, NULL)) != 0)
- goto out;
- type = sshkey_type_from_name(tname);
- if (sshkey_type_is_cert(type)) {
- /*
- * Certificate key private keys begin with the certificate
- * itself. Make sure this matches the type of the enclosing
- * private key.
- */
- if ((r = sshkey_froms(buf, &k)) != 0)
- goto out;
- if (k->type != type) {
- r = SSH_ERR_KEY_CERT_MISMATCH;
- goto out;
- }
- /* For ECDSA keys, the group must match too */
- if (k->type == KEY_ECDSA &&
- k->ecdsa_nid != sshkey_ecdsa_nid_from_name(tname)) {
- r = SSH_ERR_KEY_CERT_MISMATCH;
- goto out;
- }
- /*
- * Several fields are redundant between certificate and
- * private key body, we require these to match.
- */
- expect_sk_application = k->sk_application;
- expect_ed25519_pk = k->ed25519_pk;
- k->sk_application = NULL;
- k->ed25519_pk = NULL;
- } else {
- if ((k = sshkey_new(type)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- }
- switch (type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA:
- if ((r = sshbuf_get_bignum2(buf, &dsa_p)) != 0 ||
- (r = sshbuf_get_bignum2(buf, &dsa_q)) != 0 ||
- (r = sshbuf_get_bignum2(buf, &dsa_g)) != 0 ||
- (r = sshbuf_get_bignum2(buf, &dsa_pub_key)) != 0)
- goto out;
- if (!DSA_set0_pqg(k->dsa, dsa_p, dsa_q, dsa_g)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- dsa_p = dsa_q = dsa_g = NULL; /* transferred */
- if (!DSA_set0_key(k->dsa, dsa_pub_key, NULL)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- dsa_pub_key = NULL; /* transferred */
- /* FALLTHROUGH */
- case KEY_DSA_CERT:
- if ((r = sshbuf_get_bignum2(buf, &dsa_priv_key)) != 0)
- goto out;
- if (!DSA_set0_key(k->dsa, NULL, dsa_priv_key)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- dsa_priv_key = NULL; /* transferred */
- break;
- # ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
- if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0)
- goto out;
- if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
- r = SSH_ERR_EC_CURVE_MISMATCH;
- goto out;
- }
- k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
- if (k->ecdsa == NULL) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0)
- goto out;
- /* FALLTHROUGH */
- case KEY_ECDSA_CERT:
- if ((r = sshbuf_get_bignum2(buf, &exponent)) != 0)
- goto out;
- if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
- EC_KEY_get0_public_key(k->ecdsa))) != 0 ||
- (r = sshkey_ec_validate_private(k->ecdsa)) != 0)
- goto out;
- break;
- case KEY_ECDSA_SK:
- if ((k->ecdsa_nid = sshkey_ecdsa_nid_from_name(tname)) == -1) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_get_cstring(buf, &curve, NULL)) != 0)
- goto out;
- if (k->ecdsa_nid != sshkey_curve_name_to_nid(curve)) {
- r = SSH_ERR_EC_CURVE_MISMATCH;
- goto out;
- }
- if ((k->sk_key_handle = sshbuf_new()) == NULL ||
- (k->sk_reserved = sshbuf_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
- if (k->ecdsa == NULL) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if ((r = sshbuf_get_eckey(buf, k->ecdsa)) != 0 ||
- (r = sshbuf_get_cstring(buf, &k->sk_application,
- NULL)) != 0 ||
- (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
- (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
- (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
- goto out;
- if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
- EC_KEY_get0_public_key(k->ecdsa))) != 0)
- goto out;
- break;
- case KEY_ECDSA_SK_CERT:
- if ((k->sk_key_handle = sshbuf_new()) == NULL ||
- (k->sk_reserved = sshbuf_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if ((r = sshbuf_get_cstring(buf, &k->sk_application,
- NULL)) != 0 ||
- (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
- (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
- (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
- goto out;
- if ((r = sshkey_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
- EC_KEY_get0_public_key(k->ecdsa))) != 0)
- goto out;
- break;
- # endif /* OPENSSL_HAS_ECC */
- case KEY_RSA:
- if ((r = sshbuf_get_bignum2(buf, &rsa_n)) != 0 ||
- (r = sshbuf_get_bignum2(buf, &rsa_e)) != 0)
- goto out;
- if (!RSA_set0_key(k->rsa, rsa_n, rsa_e, NULL)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- rsa_n = rsa_e = NULL; /* transferred */
- /* FALLTHROUGH */
- case KEY_RSA_CERT:
- if ((r = sshbuf_get_bignum2(buf, &rsa_d)) != 0 ||
- (r = sshbuf_get_bignum2(buf, &rsa_iqmp)) != 0 ||
- (r = sshbuf_get_bignum2(buf, &rsa_p)) != 0 ||
- (r = sshbuf_get_bignum2(buf, &rsa_q)) != 0)
- goto out;
- if (!RSA_set0_key(k->rsa, NULL, NULL, rsa_d)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- rsa_d = NULL; /* transferred */
- if (!RSA_set0_factors(k->rsa, rsa_p, rsa_q)) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- rsa_p = rsa_q = NULL; /* transferred */
- if ((r = check_rsa_length(k->rsa)) != 0)
- goto out;
- if ((r = ssh_rsa_complete_crt_parameters(k, rsa_iqmp)) != 0)
- goto out;
- break;
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- case KEY_ED25519_CERT:
- if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0 ||
- (r = sshbuf_get_string(buf, &ed25519_sk, &sklen)) != 0)
- goto out;
- if (pklen != ED25519_PK_SZ || sklen != ED25519_SK_SZ) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- k->ed25519_pk = ed25519_pk;
- k->ed25519_sk = ed25519_sk;
- ed25519_pk = ed25519_sk = NULL; /* transferred */
- break;
- case KEY_ED25519_SK:
- case KEY_ED25519_SK_CERT:
- if ((r = sshbuf_get_string(buf, &ed25519_pk, &pklen)) != 0)
- goto out;
- if (pklen != ED25519_PK_SZ) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if ((k->sk_key_handle = sshbuf_new()) == NULL ||
- (k->sk_reserved = sshbuf_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if ((r = sshbuf_get_cstring(buf, &k->sk_application,
- NULL)) != 0 ||
- (r = sshbuf_get_u8(buf, &k->sk_flags)) != 0 ||
- (r = sshbuf_get_stringb(buf, k->sk_key_handle)) != 0 ||
- (r = sshbuf_get_stringb(buf, k->sk_reserved)) != 0)
- goto out;
- k->ed25519_pk = ed25519_pk;
- ed25519_pk = NULL; /* transferred */
- break;
- #ifdef WITH_XMSS
- case KEY_XMSS:
- case KEY_XMSS_CERT:
- if ((r = sshbuf_get_cstring(buf, &xmss_name, NULL)) != 0 ||
- (r = sshbuf_get_string(buf, &xmss_pk, &pklen)) != 0 ||
- (r = sshbuf_get_string(buf, &xmss_sk, &sklen)) != 0)
- goto out;
- if (type == KEY_XMSS &&
- (r = sshkey_xmss_init(k, xmss_name)) != 0)
- goto out;
- if (pklen != sshkey_xmss_pklen(k) ||
- sklen != sshkey_xmss_sklen(k)) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- k->xmss_pk = xmss_pk;
- k->xmss_sk = xmss_sk;
- xmss_pk = xmss_sk = NULL;
- /* optional internal state */
- if ((r = sshkey_xmss_deserialize_state_opt(k, buf)) != 0)
- goto out;
- break;
- #endif /* WITH_XMSS */
- default:
- r = SSH_ERR_KEY_TYPE_UNKNOWN;
- goto out;
- }
- #ifdef WITH_OPENSSL
- /* enable blinding */
- switch (k->type) {
- case KEY_RSA:
- case KEY_RSA_CERT:
- if (RSA_blinding_on(k->rsa, NULL) != 1) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- break;
- }
- #endif /* WITH_OPENSSL */
- if ((expect_sk_application != NULL && (k->sk_application == NULL ||
- strcmp(expect_sk_application, k->sk_application) != 0)) ||
- (expect_ed25519_pk != NULL && (k->ed25519_pk == NULL ||
- memcmp(expect_ed25519_pk, k->ed25519_pk, ED25519_PK_SZ) != 0))) {
- r = SSH_ERR_KEY_CERT_MISMATCH;
- goto out;
- }
- /* success */
- r = 0;
- if (kp != NULL) {
- *kp = k;
- k = NULL;
- }
- out:
- free(tname);
- free(curve);
- #ifdef WITH_OPENSSL
- BN_clear_free(exponent);
- BN_clear_free(dsa_p);
- BN_clear_free(dsa_q);
- BN_clear_free(dsa_g);
- BN_clear_free(dsa_pub_key);
- BN_clear_free(dsa_priv_key);
- BN_clear_free(rsa_n);
- BN_clear_free(rsa_e);
- BN_clear_free(rsa_d);
- BN_clear_free(rsa_p);
- BN_clear_free(rsa_q);
- BN_clear_free(rsa_iqmp);
- #endif /* WITH_OPENSSL */
- sshkey_free(k);
- freezero(ed25519_pk, pklen);
- freezero(ed25519_sk, sklen);
- free(xmss_name);
- freezero(xmss_pk, pklen);
- freezero(xmss_sk, sklen);
- free(expect_sk_application);
- free(expect_ed25519_pk);
- return r;
- }
- #if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
- int
- sshkey_ec_validate_public(const EC_GROUP *group, const EC_POINT *public)
- {
- EC_POINT *nq = NULL;
- BIGNUM *order = NULL, *x = NULL, *y = NULL, *tmp = NULL;
- int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
- /*
- * NB. This assumes OpenSSL has already verified that the public
- * point lies on the curve. This is done by EC_POINT_oct2point()
- * implicitly calling EC_POINT_is_on_curve(). If this code is ever
- * reachable with public points not unmarshalled using
- * EC_POINT_oct2point then the caller will need to explicitly check.
- */
- /*
- * We shouldn't ever hit this case because bignum_get_ecpoint()
- * refuses to load GF2m points.
- */
- if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
- NID_X9_62_prime_field)
- goto out;
- /* Q != infinity */
- if (EC_POINT_is_at_infinity(group, public))
- goto out;
- if ((x = BN_new()) == NULL ||
- (y = BN_new()) == NULL ||
- (order = BN_new()) == NULL ||
- (tmp = BN_new()) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
- if (EC_GROUP_get_order(group, order, NULL) != 1 ||
- EC_POINT_get_affine_coordinates_GFp(group, public,
- x, y, NULL) != 1) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if (BN_num_bits(x) <= BN_num_bits(order) / 2 ||
- BN_num_bits(y) <= BN_num_bits(order) / 2)
- goto out;
- /* nQ == infinity (n == order of subgroup) */
- if ((nq = EC_POINT_new(group)) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (EC_POINT_mul(group, nq, NULL, public, order, NULL) != 1) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if (EC_POINT_is_at_infinity(group, nq) != 1)
- goto out;
- /* x < order - 1, y < order - 1 */
- if (!BN_sub(tmp, order, BN_value_one())) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if (BN_cmp(x, tmp) >= 0 || BN_cmp(y, tmp) >= 0)
- goto out;
- ret = 0;
- out:
- BN_clear_free(x);
- BN_clear_free(y);
- BN_clear_free(order);
- BN_clear_free(tmp);
- EC_POINT_free(nq);
- return ret;
- }
- int
- sshkey_ec_validate_private(const EC_KEY *key)
- {
- BIGNUM *order = NULL, *tmp = NULL;
- int ret = SSH_ERR_KEY_INVALID_EC_VALUE;
- if ((order = BN_new()) == NULL || (tmp = BN_new()) == NULL) {
- ret = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- /* log2(private) > log2(order)/2 */
- if (EC_GROUP_get_order(EC_KEY_get0_group(key), order, NULL) != 1) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if (BN_num_bits(EC_KEY_get0_private_key(key)) <=
- BN_num_bits(order) / 2)
- goto out;
- /* private < order - 1 */
- if (!BN_sub(tmp, order, BN_value_one())) {
- ret = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if (BN_cmp(EC_KEY_get0_private_key(key), tmp) >= 0)
- goto out;
- ret = 0;
- out:
- BN_clear_free(order);
- BN_clear_free(tmp);
- return ret;
- }
- void
- sshkey_dump_ec_point(const EC_GROUP *group, const EC_POINT *point)
- {
- BIGNUM *x = NULL, *y = NULL;
- if (point == NULL) {
- fputs("point=(NULL)\n", stderr);
- return;
- }
- if ((x = BN_new()) == NULL || (y = BN_new()) == NULL) {
- fprintf(stderr, "%s: BN_new failed\n", __func__);
- goto out;
- }
- if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
- NID_X9_62_prime_field) {
- fprintf(stderr, "%s: group is not a prime field\n", __func__);
- goto out;
- }
- if (EC_POINT_get_affine_coordinates_GFp(group, point,
- x, y, NULL) != 1) {
- fprintf(stderr, "%s: EC_POINT_get_affine_coordinates_GFp\n",
- __func__);
- goto out;
- }
- fputs("x=", stderr);
- BN_print_fp(stderr, x);
- fputs("\ny=", stderr);
- BN_print_fp(stderr, y);
- fputs("\n", stderr);
- out:
- BN_clear_free(x);
- BN_clear_free(y);
- }
- void
- sshkey_dump_ec_key(const EC_KEY *key)
- {
- const BIGNUM *exponent;
- sshkey_dump_ec_point(EC_KEY_get0_group(key),
- EC_KEY_get0_public_key(key));
- fputs("exponent=", stderr);
- if ((exponent = EC_KEY_get0_private_key(key)) == NULL)
- fputs("(NULL)", stderr);
- else
- BN_print_fp(stderr, EC_KEY_get0_private_key(key));
- fputs("\n", stderr);
- }
- #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
- static int
- sshkey_private_to_blob2(struct sshkey *prv, struct sshbuf *blob,
- const char *passphrase, const char *comment, const char *ciphername,
- int rounds)
- {
- u_char *cp, *key = NULL, *pubkeyblob = NULL;
- u_char salt[SALT_LEN];
- char *b64 = NULL;
- size_t i, pubkeylen, keylen, ivlen, blocksize, authlen;
- u_int check;
- int r = SSH_ERR_INTERNAL_ERROR;
- struct sshcipher_ctx *ciphercontext = NULL;
- const struct sshcipher *cipher;
- const char *kdfname = KDFNAME;
- struct sshbuf *encoded = NULL, *encrypted = NULL, *kdf = NULL;
- if (rounds <= 0)
- rounds = DEFAULT_ROUNDS;
- if (passphrase == NULL || !strlen(passphrase)) {
- ciphername = "none";
- kdfname = "none";
- } else if (ciphername == NULL)
- ciphername = DEFAULT_CIPHERNAME;
- if ((cipher = cipher_by_name(ciphername)) == NULL) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((kdf = sshbuf_new()) == NULL ||
- (encoded = sshbuf_new()) == NULL ||
- (encrypted = sshbuf_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- blocksize = cipher_blocksize(cipher);
- keylen = cipher_keylen(cipher);
- ivlen = cipher_ivlen(cipher);
- authlen = cipher_authlen(cipher);
- if ((key = calloc(1, keylen + ivlen)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (strcmp(kdfname, "bcrypt") == 0) {
- arc4random_buf(salt, SALT_LEN);
- if (bcrypt_pbkdf(passphrase, strlen(passphrase),
- salt, SALT_LEN, key, keylen + ivlen, rounds) < 0) {
- r = SSH_ERR_INVALID_ARGUMENT;
- goto out;
- }
- if ((r = sshbuf_put_string(kdf, salt, SALT_LEN)) != 0 ||
- (r = sshbuf_put_u32(kdf, rounds)) != 0)
- goto out;
- } else if (strcmp(kdfname, "none") != 0) {
- /* Unsupported KDF type */
- r = SSH_ERR_KEY_UNKNOWN_CIPHER;
- goto out;
- }
- if ((r = cipher_init(&ciphercontext, cipher, key, keylen,
- key + keylen, ivlen, 1)) != 0)
- goto out;
- if ((r = sshbuf_put(encoded, AUTH_MAGIC, sizeof(AUTH_MAGIC))) != 0 ||
- (r = sshbuf_put_cstring(encoded, ciphername)) != 0 ||
- (r = sshbuf_put_cstring(encoded, kdfname)) != 0 ||
- (r = sshbuf_put_stringb(encoded, kdf)) != 0 ||
- (r = sshbuf_put_u32(encoded, 1)) != 0 || /* number of keys */
- (r = sshkey_to_blob(prv, &pubkeyblob, &pubkeylen)) != 0 ||
- (r = sshbuf_put_string(encoded, pubkeyblob, pubkeylen)) != 0)
- goto out;
- /* set up the buffer that will be encrypted */
- /* Random check bytes */
- check = arc4random();
- if ((r = sshbuf_put_u32(encrypted, check)) != 0 ||
- (r = sshbuf_put_u32(encrypted, check)) != 0)
- goto out;
- /* append private key and comment*/
- if ((r = sshkey_private_serialize_opt(prv, encrypted,
- SSHKEY_SERIALIZE_FULL)) != 0 ||
- (r = sshbuf_put_cstring(encrypted, comment)) != 0)
- goto out;
- /* padding */
- i = 0;
- while (sshbuf_len(encrypted) % blocksize) {
- if ((r = sshbuf_put_u8(encrypted, ++i & 0xff)) != 0)
- goto out;
- }
- /* length in destination buffer */
- if ((r = sshbuf_put_u32(encoded, sshbuf_len(encrypted))) != 0)
- goto out;
- /* encrypt */
- if ((r = sshbuf_reserve(encoded,
- sshbuf_len(encrypted) + authlen, &cp)) != 0)
- goto out;
- if ((r = cipher_crypt(ciphercontext, 0, cp,
- sshbuf_ptr(encrypted), sshbuf_len(encrypted), 0, authlen)) != 0)
- goto out;
- sshbuf_reset(blob);
- /* assemble uuencoded key */
- if ((r = sshbuf_put(blob, MARK_BEGIN, MARK_BEGIN_LEN)) != 0 ||
- (r = sshbuf_dtob64(encoded, blob, 1)) != 0 ||
- (r = sshbuf_put(blob, MARK_END, MARK_END_LEN)) != 0)
- goto out;
- /* success */
- r = 0;
- out:
- sshbuf_free(kdf);
- sshbuf_free(encoded);
- sshbuf_free(encrypted);
- cipher_free(ciphercontext);
- explicit_bzero(salt, sizeof(salt));
- if (key != NULL)
- freezero(key, keylen + ivlen);
- if (pubkeyblob != NULL)
- freezero(pubkeyblob, pubkeylen);
- if (b64 != NULL)
- freezero(b64, strlen(b64));
- return r;
- }
- static int
- private2_uudecode(struct sshbuf *blob, struct sshbuf **decodedp)
- {
- const u_char *cp;
- size_t encoded_len;
- int r;
- u_char last;
- struct sshbuf *encoded = NULL, *decoded = NULL;
- if (blob == NULL || decodedp == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- *decodedp = NULL;
- if ((encoded = sshbuf_new()) == NULL ||
- (decoded = sshbuf_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- /* check preamble */
- cp = sshbuf_ptr(blob);
- encoded_len = sshbuf_len(blob);
- if (encoded_len < (MARK_BEGIN_LEN + MARK_END_LEN) ||
- memcmp(cp, MARK_BEGIN, MARK_BEGIN_LEN) != 0) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- cp += MARK_BEGIN_LEN;
- encoded_len -= MARK_BEGIN_LEN;
- /* Look for end marker, removing whitespace as we go */
- while (encoded_len > 0) {
- if (*cp != '\n' && *cp != '\r') {
- if ((r = sshbuf_put_u8(encoded, *cp)) != 0)
- goto out;
- }
- last = *cp;
- encoded_len--;
- cp++;
- if (last == '\n') {
- if (encoded_len >= MARK_END_LEN &&
- memcmp(cp, MARK_END, MARK_END_LEN) == 0) {
- /* \0 terminate */
- if ((r = sshbuf_put_u8(encoded, 0)) != 0)
- goto out;
- break;
- }
- }
- }
- if (encoded_len == 0) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* decode base64 */
- if ((r = sshbuf_b64tod(decoded, (char *)sshbuf_ptr(encoded))) != 0)
- goto out;
- /* check magic */
- if (sshbuf_len(decoded) < sizeof(AUTH_MAGIC) ||
- memcmp(sshbuf_ptr(decoded), AUTH_MAGIC, sizeof(AUTH_MAGIC))) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* success */
- *decodedp = decoded;
- decoded = NULL;
- r = 0;
- out:
- sshbuf_free(encoded);
- sshbuf_free(decoded);
- return r;
- }
- static int
- private2_decrypt(struct sshbuf *decoded, const char *passphrase,
- struct sshbuf **decryptedp, struct sshkey **pubkeyp)
- {
- char *ciphername = NULL, *kdfname = NULL;
- const struct sshcipher *cipher = NULL;
- int r = SSH_ERR_INTERNAL_ERROR;
- size_t keylen = 0, ivlen = 0, authlen = 0, slen = 0;
- struct sshbuf *kdf = NULL, *decrypted = NULL;
- struct sshcipher_ctx *ciphercontext = NULL;
- struct sshkey *pubkey = NULL;
- u_char *key = NULL, *salt = NULL, *dp;
- u_int blocksize, rounds, nkeys, encrypted_len, check1, check2;
- if (decoded == NULL || decryptedp == NULL || pubkeyp == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- *decryptedp = NULL;
- *pubkeyp = NULL;
- if ((decrypted = sshbuf_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- /* parse public portion of key */
- if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
- (r = sshbuf_get_cstring(decoded, &ciphername, NULL)) != 0 ||
- (r = sshbuf_get_cstring(decoded, &kdfname, NULL)) != 0 ||
- (r = sshbuf_froms(decoded, &kdf)) != 0 ||
- (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
- goto out;
- if (nkeys != 1) {
- /* XXX only one key supported at present */
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if ((r = sshkey_froms(decoded, &pubkey)) != 0 ||
- (r = sshbuf_get_u32(decoded, &encrypted_len)) != 0)
- goto out;
- if ((cipher = cipher_by_name(ciphername)) == NULL) {
- r = SSH_ERR_KEY_UNKNOWN_CIPHER;
- goto out;
- }
- if (strcmp(kdfname, "none") != 0 && strcmp(kdfname, "bcrypt") != 0) {
- r = SSH_ERR_KEY_UNKNOWN_CIPHER;
- goto out;
- }
- if (strcmp(kdfname, "none") == 0 && strcmp(ciphername, "none") != 0) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- if ((passphrase == NULL || strlen(passphrase) == 0) &&
- strcmp(kdfname, "none") != 0) {
- /* passphrase required */
- r = SSH_ERR_KEY_WRONG_PASSPHRASE;
- goto out;
- }
- /* check size of encrypted key blob */
- blocksize = cipher_blocksize(cipher);
- if (encrypted_len < blocksize || (encrypted_len % blocksize) != 0) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* setup key */
- keylen = cipher_keylen(cipher);
- ivlen = cipher_ivlen(cipher);
- authlen = cipher_authlen(cipher);
- if ((key = calloc(1, keylen + ivlen)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (strcmp(kdfname, "bcrypt") == 0) {
- if ((r = sshbuf_get_string(kdf, &salt, &slen)) != 0 ||
- (r = sshbuf_get_u32(kdf, &rounds)) != 0)
- goto out;
- if (bcrypt_pbkdf(passphrase, strlen(passphrase), salt, slen,
- key, keylen + ivlen, rounds) < 0) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- }
- /* check that an appropriate amount of auth data is present */
- if (sshbuf_len(decoded) < authlen ||
- sshbuf_len(decoded) - authlen < encrypted_len) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* decrypt private portion of key */
- if ((r = sshbuf_reserve(decrypted, encrypted_len, &dp)) != 0 ||
- (r = cipher_init(&ciphercontext, cipher, key, keylen,
- key + keylen, ivlen, 0)) != 0)
- goto out;
- if ((r = cipher_crypt(ciphercontext, 0, dp, sshbuf_ptr(decoded),
- encrypted_len, 0, authlen)) != 0) {
- /* an integrity error here indicates an incorrect passphrase */
- if (r == SSH_ERR_MAC_INVALID)
- r = SSH_ERR_KEY_WRONG_PASSPHRASE;
- goto out;
- }
- if ((r = sshbuf_consume(decoded, encrypted_len + authlen)) != 0)
- goto out;
- /* there should be no trailing data */
- if (sshbuf_len(decoded) != 0) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* check check bytes */
- if ((r = sshbuf_get_u32(decrypted, &check1)) != 0 ||
- (r = sshbuf_get_u32(decrypted, &check2)) != 0)
- goto out;
- if (check1 != check2) {
- r = SSH_ERR_KEY_WRONG_PASSPHRASE;
- goto out;
- }
- /* success */
- *decryptedp = decrypted;
- decrypted = NULL;
- *pubkeyp = pubkey;
- pubkey = NULL;
- r = 0;
- out:
- cipher_free(ciphercontext);
- free(ciphername);
- free(kdfname);
- sshkey_free(pubkey);
- if (salt != NULL) {
- explicit_bzero(salt, slen);
- free(salt);
- }
- if (key != NULL) {
- explicit_bzero(key, keylen + ivlen);
- free(key);
- }
- sshbuf_free(kdf);
- sshbuf_free(decrypted);
- return r;
- }
- /* Check deterministic padding after private key */
- static int
- private2_check_padding(struct sshbuf *decrypted)
- {
- u_char pad;
- size_t i;
- int r = SSH_ERR_INTERNAL_ERROR;
- i = 0;
- while (sshbuf_len(decrypted)) {
- if ((r = sshbuf_get_u8(decrypted, &pad)) != 0)
- goto out;
- if (pad != (++i & 0xff)) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- }
- /* success */
- r = 0;
- out:
- explicit_bzero(&pad, sizeof(pad));
- explicit_bzero(&i, sizeof(i));
- return r;
- }
- static int
- sshkey_parse_private2(struct sshbuf *blob, int type, const char *passphrase,
- struct sshkey **keyp, char **commentp)
- {
- char *comment = NULL;
- int r = SSH_ERR_INTERNAL_ERROR;
- struct sshbuf *decoded = NULL, *decrypted = NULL;
- struct sshkey *k = NULL, *pubkey = NULL;
- if (keyp != NULL)
- *keyp = NULL;
- if (commentp != NULL)
- *commentp = NULL;
- /* Undo base64 encoding and decrypt the private section */
- if ((r = private2_uudecode(blob, &decoded)) != 0 ||
- (r = private2_decrypt(decoded, passphrase,
- &decrypted, &pubkey)) != 0)
- goto out;
- if (type != KEY_UNSPEC &&
- sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
- r = SSH_ERR_KEY_TYPE_MISMATCH;
- goto out;
- }
- /* Load the private key and comment */
- if ((r = sshkey_private_deserialize(decrypted, &k)) != 0 ||
- (r = sshbuf_get_cstring(decrypted, &comment, NULL)) != 0)
- goto out;
- /* Check deterministic padding after private section */
- if ((r = private2_check_padding(decrypted)) != 0)
- goto out;
- /* Check that the public key in the envelope matches the private key */
- if (!sshkey_equal(pubkey, k)) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* success */
- r = 0;
- if (keyp != NULL) {
- *keyp = k;
- k = NULL;
- }
- if (commentp != NULL) {
- *commentp = comment;
- comment = NULL;
- }
- out:
- free(comment);
- sshbuf_free(decoded);
- sshbuf_free(decrypted);
- sshkey_free(k);
- sshkey_free(pubkey);
- return r;
- }
- static int
- sshkey_parse_private2_pubkey(struct sshbuf *blob, int type,
- struct sshkey **keyp)
- {
- int r = SSH_ERR_INTERNAL_ERROR;
- struct sshbuf *decoded = NULL;
- struct sshkey *pubkey = NULL;
- u_int nkeys = 0;
- if (keyp != NULL)
- *keyp = NULL;
- if ((r = private2_uudecode(blob, &decoded)) != 0)
- goto out;
- /* parse public key from unencrypted envelope */
- if ((r = sshbuf_consume(decoded, sizeof(AUTH_MAGIC))) != 0 ||
- (r = sshbuf_skip_string(decoded)) != 0 || /* cipher */
- (r = sshbuf_skip_string(decoded)) != 0 || /* KDF alg */
- (r = sshbuf_skip_string(decoded)) != 0 || /* KDF hint */
- (r = sshbuf_get_u32(decoded, &nkeys)) != 0)
- goto out;
- if (nkeys != 1) {
- /* XXX only one key supported at present */
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- /* Parse the public key */
- if ((r = sshkey_froms(decoded, &pubkey)) != 0)
- goto out;
- if (type != KEY_UNSPEC &&
- sshkey_type_plain(type) != sshkey_type_plain(pubkey->type)) {
- r = SSH_ERR_KEY_TYPE_MISMATCH;
- goto out;
- }
- /* success */
- r = 0;
- if (keyp != NULL) {
- *keyp = pubkey;
- pubkey = NULL;
- }
- out:
- sshbuf_free(decoded);
- sshkey_free(pubkey);
- return r;
- }
- #ifdef WITH_OPENSSL
- /* convert SSH v2 key to PEM or PKCS#8 format */
- static int
- sshkey_private_to_blob_pem_pkcs8(struct sshkey *key, struct sshbuf *buf,
- int format, const char *_passphrase, const char *comment)
- {
- int was_shielded = sshkey_is_shielded(key);
- int success, r;
- int blen, len = strlen(_passphrase);
- u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
- const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
- char *bptr;
- BIO *bio = NULL;
- struct sshbuf *blob;
- EVP_PKEY *pkey = NULL;
- if (len > 0 && len <= 4)
- return SSH_ERR_PASSPHRASE_TOO_SHORT;
- if ((blob = sshbuf_new()) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- if ((bio = BIO_new(BIO_s_mem())) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if (format == SSHKEY_PRIVATE_PKCS8 && (pkey = EVP_PKEY_new()) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- if ((r = sshkey_unshield_private(key)) != 0)
- goto out;
- switch (key->type) {
- case KEY_DSA:
- if (format == SSHKEY_PRIVATE_PEM) {
- success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
- cipher, passphrase, len, NULL, NULL);
- } else {
- success = EVP_PKEY_set1_DSA(pkey, key->dsa);
- }
- break;
- #ifdef OPENSSL_HAS_ECC
- case KEY_ECDSA:
- if (format == SSHKEY_PRIVATE_PEM) {
- success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
- cipher, passphrase, len, NULL, NULL);
- } else {
- success = EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa);
- }
- break;
- #endif
- case KEY_RSA:
- if (format == SSHKEY_PRIVATE_PEM) {
- success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
- cipher, passphrase, len, NULL, NULL);
- } else {
- success = EVP_PKEY_set1_RSA(pkey, key->rsa);
- }
- break;
- default:
- success = 0;
- break;
- }
- if (success == 0) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if (format == SSHKEY_PRIVATE_PKCS8) {
- if ((success = PEM_write_bio_PrivateKey(bio, pkey, cipher,
- passphrase, len, NULL, NULL)) == 0) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- }
- if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0) {
- r = SSH_ERR_INTERNAL_ERROR;
- goto out;
- }
- if ((r = sshbuf_put(blob, bptr, blen)) != 0)
- goto out;
- r = 0;
- out:
- if (was_shielded)
- r = sshkey_shield_private(key);
- if (r == 0)
- r = sshbuf_putb(buf, blob);
- EVP_PKEY_free(pkey);
- sshbuf_free(blob);
- BIO_free(bio);
- return r;
- }
- #endif /* WITH_OPENSSL */
- /* Serialise "key" to buffer "blob" */
- int
- sshkey_private_to_fileblob(struct sshkey *key, struct sshbuf *blob,
- const char *passphrase, const char *comment,
- int format, const char *openssh_format_cipher, int openssh_format_rounds)
- {
- switch (key->type) {
- #ifdef WITH_OPENSSL
- case KEY_DSA:
- case KEY_ECDSA:
- case KEY_RSA:
- break; /* see below */
- #endif /* WITH_OPENSSL */
- case KEY_ED25519:
- case KEY_ED25519_SK:
- #ifdef WITH_XMSS
- case KEY_XMSS:
- #endif /* WITH_XMSS */
- #ifdef WITH_OPENSSL
- case KEY_ECDSA_SK:
- #endif /* WITH_OPENSSL */
- return sshkey_private_to_blob2(key, blob, passphrase,
- comment, openssh_format_cipher, openssh_format_rounds);
- default:
- return SSH_ERR_KEY_TYPE_UNKNOWN;
- }
- #ifdef WITH_OPENSSL
- switch (format) {
- case SSHKEY_PRIVATE_OPENSSH:
- return sshkey_private_to_blob2(key, blob, passphrase,
- comment, openssh_format_cipher, openssh_format_rounds);
- case SSHKEY_PRIVATE_PEM:
- case SSHKEY_PRIVATE_PKCS8:
- return sshkey_private_to_blob_pem_pkcs8(key, blob,
- format, passphrase, comment);
- default:
- return SSH_ERR_INVALID_ARGUMENT;
- }
- #endif /* WITH_OPENSSL */
- }
- #ifdef WITH_OPENSSL
- static int
- translate_libcrypto_error(unsigned long pem_err)
- {
- int pem_reason = ERR_GET_REASON(pem_err);
- switch (ERR_GET_LIB(pem_err)) {
- case ERR_LIB_PEM:
- switch (pem_reason) {
- case PEM_R_BAD_PASSWORD_READ:
- case PEM_R_PROBLEMS_GETTING_PASSWORD:
- case PEM_R_BAD_DECRYPT:
- return SSH_ERR_KEY_WRONG_PASSPHRASE;
- default:
- return SSH_ERR_INVALID_FORMAT;
- }
- case ERR_LIB_EVP:
- switch (pem_reason) {
- case EVP_R_BAD_DECRYPT:
- return SSH_ERR_KEY_WRONG_PASSPHRASE;
- #ifdef EVP_R_BN_DECODE_ERROR
- case EVP_R_BN_DECODE_ERROR:
- #endif
- case EVP_R_DECODE_ERROR:
- #ifdef EVP_R_PRIVATE_KEY_DECODE_ERROR
- case EVP_R_PRIVATE_KEY_DECODE_ERROR:
- #endif
- return SSH_ERR_INVALID_FORMAT;
- default:
- return SSH_ERR_LIBCRYPTO_ERROR;
- }
- case ERR_LIB_ASN1:
- return SSH_ERR_INVALID_FORMAT;
- }
- return SSH_ERR_LIBCRYPTO_ERROR;
- }
- static void
- clear_libcrypto_errors(void)
- {
- while (ERR_get_error() != 0)
- ;
- }
- /*
- * Translate OpenSSL error codes to determine whether
- * passphrase is required/incorrect.
- */
- static int
- convert_libcrypto_error(void)
- {
- /*
- * Some password errors are reported at the beginning
- * of the error queue.
- */
- if (translate_libcrypto_error(ERR_peek_error()) ==
- SSH_ERR_KEY_WRONG_PASSPHRASE)
- return SSH_ERR_KEY_WRONG_PASSPHRASE;
- return translate_libcrypto_error(ERR_peek_last_error());
- }
- static int
- pem_passphrase_cb(char *buf, int size, int rwflag, void *u)
- {
- char *p = (char *)u;
- size_t len;
- if (p == NULL || (len = strlen(p)) == 0)
- return -1;
- if (size < 0 || len > (size_t)size)
- return -1;
- memcpy(buf, p, len);
- return (int)len;
- }
- static int
- sshkey_parse_private_pem_fileblob(struct sshbuf *blob, int type,
- const char *passphrase, struct sshkey **keyp)
- {
- EVP_PKEY *pk = NULL;
- struct sshkey *prv = NULL;
- BIO *bio = NULL;
- int r;
- if (keyp != NULL)
- *keyp = NULL;
- if ((bio = BIO_new(BIO_s_mem())) == NULL || sshbuf_len(blob) > INT_MAX)
- return SSH_ERR_ALLOC_FAIL;
- if (BIO_write(bio, sshbuf_ptr(blob), sshbuf_len(blob)) !=
- (int)sshbuf_len(blob)) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- clear_libcrypto_errors();
- if ((pk = PEM_read_bio_PrivateKey(bio, NULL, pem_passphrase_cb,
- (char *)passphrase)) == NULL) {
- /*
- * libcrypto may return various ASN.1 errors when attempting
- * to parse a key with an incorrect passphrase.
- * Treat all format errors as "incorrect passphrase" if a
- * passphrase was supplied.
- */
- if (passphrase != NULL && *passphrase != '\0')
- r = SSH_ERR_KEY_WRONG_PASSPHRASE;
- else
- r = convert_libcrypto_error();
- goto out;
- }
- if (EVP_PKEY_base_id(pk) == EVP_PKEY_RSA &&
- (type == KEY_UNSPEC || type == KEY_RSA)) {
- if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- prv->rsa = EVP_PKEY_get1_RSA(pk);
- prv->type = KEY_RSA;
- #ifdef DEBUG_PK
- RSA_print_fp(stderr, prv->rsa, 8);
- #endif
- if (RSA_blinding_on(prv->rsa, NULL) != 1) {
- r = SSH_ERR_LIBCRYPTO_ERROR;
- goto out;
- }
- if ((r = check_rsa_length(prv->rsa)) != 0)
- goto out;
- } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_DSA &&
- (type == KEY_UNSPEC || type == KEY_DSA)) {
- if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- prv->dsa = EVP_PKEY_get1_DSA(pk);
- prv->type = KEY_DSA;
- #ifdef DEBUG_PK
- DSA_print_fp(stderr, prv->dsa, 8);
- #endif
- #ifdef OPENSSL_HAS_ECC
- } else if (EVP_PKEY_base_id(pk) == EVP_PKEY_EC &&
- (type == KEY_UNSPEC || type == KEY_ECDSA)) {
- if ((prv = sshkey_new(KEY_UNSPEC)) == NULL) {
- r = SSH_ERR_ALLOC_FAIL;
- goto out;
- }
- prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
- prv->type = KEY_ECDSA;
- prv->ecdsa_nid = sshkey_ecdsa_key_to_nid(prv->ecdsa);
- if (prv->ecdsa_nid == -1 ||
- sshkey_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
- sshkey_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
- EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
- sshkey_ec_validate_private(prv->ecdsa) != 0) {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- # ifdef DEBUG_PK
- if (prv != NULL && prv->ecdsa != NULL)
- sshkey_dump_ec_key(prv->ecdsa);
- # endif
- #endif /* OPENSSL_HAS_ECC */
- } else {
- r = SSH_ERR_INVALID_FORMAT;
- goto out;
- }
- r = 0;
- if (keyp != NULL) {
- *keyp = prv;
- prv = NULL;
- }
- out:
- BIO_free(bio);
- EVP_PKEY_free(pk);
- sshkey_free(prv);
- return r;
- }
- #endif /* WITH_OPENSSL */
- int
- sshkey_parse_private_fileblob_type(struct sshbuf *blob, int type,
- const char *passphrase, struct sshkey **keyp, char **commentp)
- {
- int r = SSH_ERR_INTERNAL_ERROR;
- if (keyp != NULL)
- *keyp = NULL;
- if (commentp != NULL)
- *commentp = NULL;
- switch (type) {
- case KEY_ED25519:
- case KEY_XMSS:
- /* No fallback for new-format-only keys */
- return sshkey_parse_private2(blob, type, passphrase,
- keyp, commentp);
- default:
- r = sshkey_parse_private2(blob, type, passphrase, keyp,
- commentp);
- /* Only fallback to PEM parser if a format error occurred. */
- if (r != SSH_ERR_INVALID_FORMAT)
- return r;
- #ifdef WITH_OPENSSL
- return sshkey_parse_private_pem_fileblob(blob, type,
- passphrase, keyp);
- #else
- return SSH_ERR_INVALID_FORMAT;
- #endif /* WITH_OPENSSL */
- }
- }
- int
- sshkey_parse_private_fileblob(struct sshbuf *buffer, const char *passphrase,
- struct sshkey **keyp, char **commentp)
- {
- if (keyp != NULL)
- *keyp = NULL;
- if (commentp != NULL)
- *commentp = NULL;
- return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
- passphrase, keyp, commentp);
- }
- void
- sshkey_sig_details_free(struct sshkey_sig_details *details)
- {
- freezero(details, sizeof(*details));
- }
- int
- sshkey_parse_pubkey_from_private_fileblob_type(struct sshbuf *blob, int type,
- struct sshkey **pubkeyp)
- {
- int r = SSH_ERR_INTERNAL_ERROR;
- if (pubkeyp != NULL)
- *pubkeyp = NULL;
- /* only new-format private keys bundle a public key inside */
- if ((r = sshkey_parse_private2_pubkey(blob, type, pubkeyp)) != 0)
- return r;
- return 0;
- }
- #ifdef WITH_XMSS
- /*
- * serialize the key with the current state and forward the state
- * maxsign times.
- */
- int
- sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
- u_int32_t maxsign, sshkey_printfn *pr)
- {
- int r, rupdate;
- if (maxsign == 0 ||
- sshkey_type_plain(k->type) != KEY_XMSS)
- return sshkey_private_serialize_opt(k, b,
- SSHKEY_SERIALIZE_DEFAULT);
- if ((r = sshkey_xmss_get_state(k, pr)) != 0 ||
- (r = sshkey_private_serialize_opt(k, b,
- SSHKEY_SERIALIZE_STATE)) != 0 ||
- (r = sshkey_xmss_forward_state(k, maxsign)) != 0)
- goto out;
- r = 0;
- out:
- if ((rupdate = sshkey_xmss_update_state(k, pr)) != 0) {
- if (r == 0)
- r = rupdate;
- }
- return r;
- }
- u_int32_t
- sshkey_signatures_left(const struct sshkey *k)
- {
- if (sshkey_type_plain(k->type) == KEY_XMSS)
- return sshkey_xmss_signatures_left(k);
- return 0;
- }
- int
- sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
- {
- if (sshkey_type_plain(k->type) != KEY_XMSS)
- return SSH_ERR_INVALID_ARGUMENT;
- return sshkey_xmss_enable_maxsign(k, maxsign);
- }
- int
- sshkey_set_filename(struct sshkey *k, const char *filename)
- {
- if (k == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if (sshkey_type_plain(k->type) != KEY_XMSS)
- return 0;
- if (filename == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- if ((k->xmss_filename = strdup(filename)) == NULL)
- return SSH_ERR_ALLOC_FAIL;
- return 0;
- }
- #else
- int
- sshkey_private_serialize_maxsign(struct sshkey *k, struct sshbuf *b,
- u_int32_t maxsign, sshkey_printfn *pr)
- {
- return sshkey_private_serialize_opt(k, b, SSHKEY_SERIALIZE_DEFAULT);
- }
- u_int32_t
- sshkey_signatures_left(const struct sshkey *k)
- {
- return 0;
- }
- int
- sshkey_enable_maxsign(struct sshkey *k, u_int32_t maxsign)
- {
- return SSH_ERR_INVALID_ARGUMENT;
- }
- int
- sshkey_set_filename(struct sshkey *k, const char *filename)
- {
- if (k == NULL)
- return SSH_ERR_INVALID_ARGUMENT;
- return 0;
- }
- #endif /* WITH_XMSS */
|