ssl_load.c 181 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783
  1. /* ssl_load.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. /*
  26. * WOLFSSL_SYS_CA_CERTS
  27. * Enables ability to load system CA certs from the OS via
  28. * wolfSSL_CTX_load_system_CA_certs.
  29. */
  30. #ifdef WOLFSSL_SYS_CA_CERTS
  31. #ifdef _WIN32
  32. #include <windows.h>
  33. #include <wincrypt.h>
  34. /* mingw gcc does not support pragma comment, and the
  35. * linking with crypt32 is handled in configure.ac */
  36. #if !defined(__MINGW32__) && !defined(__MINGW64__)
  37. #pragma comment(lib, "crypt32")
  38. #endif
  39. #endif
  40. #if defined(__APPLE__) && defined(HAVE_SECURITY_SECTRUSTSETTINGS_H)
  41. #include <Security/SecTrustSettings.h>
  42. #endif
  43. #endif /* WOLFSSL_SYS_CA_CERTS */
  44. #if !defined(WOLFSSL_SSL_LOAD_INCLUDED)
  45. #ifndef WOLFSSL_IGNORE_FILE_WARN
  46. #warning ssl_bn.c does not need to be compiled separately from ssl.c
  47. #endif
  48. #else
  49. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  50. /* PSK field of context when it exists. */
  51. #define CTX_HAVE_PSK(ctx) (ctx)->havePSK
  52. /* PSK field of ssl when it exists. */
  53. #define SSL_HAVE_PSK(ssl) (ssl)->options.havePSK
  54. #else
  55. /* Have PSK value when no field. */
  56. #define CTX_HAVE_PSK(ctx) 0
  57. /* Have PSK value when no field. */
  58. #define SSL_HAVE_PSK(ssl) 0
  59. #endif
  60. #ifdef NO_RSA
  61. /* Boolean for RSA available. */
  62. #define WOLFSSL_HAVE_RSA 0
  63. #else
  64. /* Boolean for RSA available. */
  65. #define WOLFSSL_HAVE_RSA 1
  66. #endif
  67. #ifndef NO_CERTS
  68. /* Private key size from ssl. */
  69. #define SSL_KEY_SZ(ssl) (ssl)->buffers.keySz
  70. #else
  71. /* Private key size not available. */
  72. #define SSL_KEY_SZ(ssl) 0
  73. #endif
  74. #ifdef HAVE_ANON
  75. /* Anonymous ciphersuite allowed field in context. */
  76. #define CTX_USE_ANON(ctx) (ctx)->useAnon
  77. #else
  78. /* Anonymous ciphersuite allowed field not in context. */
  79. #define CTX_USE_ANON(ctx) 0
  80. #endif
  81. #ifdef HAVE_PK_CALLBACKS
  82. #define WOLFSSL_IS_PRIV_PK_SET(ctx, ssl) \
  83. wolfSSL_CTX_IsPrivatePkSet(((ssl) == NULL) ? (ctx) : (ssl)->ctx)
  84. #else
  85. #define WOLFSSL_IS_PRIV_PK_SET(ctx, ssl) 0
  86. #endif
  87. /* Get the heap from the context or the ssl depending on which is available. */
  88. #define WOLFSSL_HEAP(ctx, ssl) \
  89. (((ctx) != NULL) ? (ctx)->heap : (((ssl) != NULL) ? (ssl)->heap : NULL))
  90. #ifndef NO_CERTS
  91. /* Get DER encoding from data in a buffer as a DerBuffer.
  92. *
  93. * @param [in] buff Buffer containing data.
  94. * @param [in] len Length of data in buffer.
  95. * @param [in] format Format of data:
  96. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  97. * @param [in] type Type of data:
  98. * CERT_TYPE, CA_TYPE, TRUSTED_PEER_TYPE,
  99. * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
  100. * @param [in, out] info Info for encryption.
  101. * @param [in] heap Dynamic memory allocation hint.
  102. * @param [out] der Holds DER encoded data.
  103. * @return 0 on success.
  104. * @return NOT_COMPILED_IN when format is PEM and PEM not supported.
  105. * @return ASN_PARSE_E when format is ASN.1 and invalid DER encoding.
  106. * @return MEMORY_E when dynamic memory allocation fails.
  107. */
  108. static int DataToDerBuffer(const unsigned char* buff, word32 len, int format,
  109. int type, EncryptedInfo* info, void* heap, DerBuffer** der)
  110. {
  111. int ret;
  112. info->consumed = 0;
  113. /* Data in buffer has PEM format - extract DER data. */
  114. if (format == WOLFSSL_FILETYPE_PEM) {
  115. #ifdef WOLFSSL_PEM_TO_DER
  116. ret = PemToDer(buff, len, type, der, heap, info, NULL);
  117. if (ret != 0) {
  118. FreeDer(der);
  119. }
  120. #else
  121. ret = NOT_COMPILED_IN;
  122. #endif
  123. }
  124. /* Data in buffer is ASN.1 format - get first SEQ or OCT into der. */
  125. else {
  126. int length;
  127. word32 inOutIdx = 0;
  128. /* Get length of SEQ including header. */
  129. if ((info->consumed = wolfssl_der_length(buff, len)) > 0) {
  130. ret = 0;
  131. }
  132. /* Private keys may be wrapped in OCT when PKCS#8 wrapper removed.
  133. * TODO: is this really needed? */
  134. else if ((type == PRIVATEKEY_TYPE) &&
  135. (GetOctetString(buff, &inOutIdx, &length, len) >= 0)) {
  136. /* Include octet string DER header. */
  137. info->consumed = length + inOutIdx;
  138. ret = 0;
  139. }
  140. else {
  141. ret = ASN_PARSE_E;
  142. }
  143. if (ret == 0) {
  144. ret = AllocCopyDer(der, buff, (word32)info->consumed, type, heap);
  145. }
  146. }
  147. return ret;
  148. }
  149. /* Process a user's certificate.
  150. *
  151. * Puts the 3-byte length before certificate data as required for TLS.
  152. * CA certificates are added to the certificate manager.
  153. *
  154. * @param [in] cm Certificate manager.
  155. * @param [in, out] pDer DER encoded data.
  156. * @param [in] type Type of data. Valid values:
  157. * CERT_TYPE, CA_TYPE or TRUSTED_PEER_TYPE.
  158. * @param [in] verify How to verify certificate.
  159. * @param [out] chainBuffer Buffer to hold chain of certificates.
  160. * @param [in, out] pIdx On in, current index into chainBuffer.
  161. * On out, index after certificate added.
  162. * @param [in] bufferSz Size of buffer in bytes.
  163. * @return 0 on success.
  164. * @return BUFFER_E if chain buffer not big enough to hold certificate.
  165. */
  166. static int ProcessUserCert(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer,
  167. int type, int verify, byte* chainBuffer, word32* pIdx, word32 bufferSz)
  168. {
  169. int ret = 0;
  170. word32 idx = *pIdx;
  171. DerBuffer* der = *pDer;
  172. /* Check there is space for certificate in chainBuffer. */
  173. if ((ret == 0) && ((idx + der->length + CERT_HEADER_SZ) > bufferSz)) {
  174. WOLFSSL_MSG(" Cert Chain bigger than buffer. "
  175. "Consider increasing MAX_CHAIN_DEPTH");
  176. ret = BUFFER_E;
  177. }
  178. if (ret == 0) {
  179. /* 3-byte length. */
  180. c32to24(der->length, &chainBuffer[idx]);
  181. idx += CERT_HEADER_SZ;
  182. /* Add complete DER encoded certificate. */
  183. XMEMCPY(&chainBuffer[idx], der->buffer, der->length);
  184. idx += der->length;
  185. if (type == CA_TYPE) {
  186. /* Add CA to certificate manager */
  187. ret = AddCA(cm, pDer, WOLFSSL_USER_CA, verify);
  188. if (ret == 1) {
  189. ret = 0;
  190. }
  191. }
  192. }
  193. /* Update the index into chainBuffer. */
  194. *pIdx = idx;
  195. return ret;
  196. }
  197. /* Store the certificate chain buffer aganst WOLFSSL_CTX or WOLFSSL object.
  198. *
  199. * @param [in, out] ctx SSL context object.
  200. * @param [in, out] ssl SSL object.
  201. * @param [in] chainBuffer Buffer containing chain of certificates.
  202. * @param [in] len Length, in bytes, of data in buffer.
  203. * @param [in] cnt Number of certificates in chain.
  204. * @param [in] type Type of data. Valid values:
  205. * CERT_TYPE, CA_TYPE or CHAIN_CERT_TYPE.
  206. * @param [in] heap Dynamic memory allocation hint.
  207. * @return 0 on success.
  208. * @return MEMORY_E when dynamic memory allocation fails.
  209. */
  210. static int ProcessUserChainRetain(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  211. const byte* chainBuffer, word32 len, int cnt, int type, void* heap)
  212. {
  213. int ret = 0;
  214. (void)cnt;
  215. /* Store in SSL object if available. */
  216. if (ssl != NULL) {
  217. /* Dispose of old chain if not reference to context's. */
  218. if (ssl->buffers.weOwnCertChain) {
  219. FreeDer(&ssl->buffers.certChain);
  220. }
  221. /* Allocate and copy the buffer into SSL object. */
  222. ret = AllocCopyDer(&ssl->buffers.certChain, chainBuffer, len, type,
  223. heap);
  224. ssl->buffers.weOwnCertChain = (ret == 0);
  225. #ifdef WOLFSSL_TLS13
  226. /* Update count of certificates in chain. */
  227. ssl->buffers.certChainCnt = cnt;
  228. #endif
  229. }
  230. /* Store in SSL context object if available. */
  231. else if (ctx != NULL) {
  232. /* Dispose of old chain and allocate and copy in new chain. */
  233. FreeDer(&ctx->certChain);
  234. /* Allocate and copy the buffer into SSL context object. */
  235. ret = AllocCopyDer(&ctx->certChain, chainBuffer, len, type, heap);
  236. #ifdef WOLFSSL_TLS13
  237. /* Update count of certificates in chain. */
  238. ctx->certChainCnt = cnt;
  239. #endif
  240. }
  241. return ret;
  242. }
  243. /* Process user cert chain to pass during the TLS handshake.
  244. *
  245. * If not a certificate type then data is ignored.
  246. *
  247. * @param [in, out] ctx SSL context object.
  248. * @param [in, out] ssl SSL object.
  249. * @param [in] buff Buffer holding certificates.
  250. * @param [in] sz Length of data in buffer.
  251. * @param [in] format Format of the certificate:
  252. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1
  253. * @param [in] type Type of certificate:
  254. * CA_TYPE, CERT_TYPE or CHAIN_CERT_TYPE
  255. * @param [out] used Number of bytes from buff used.
  256. * @param [in, out] info Encryption information.
  257. * @param [in] verify How to verify certificate.
  258. * @return 0 on success.
  259. * @return BAD_FUNC_ARG when type is CA_TYPE and ctx is NULL.
  260. * @return MEMORY_E when dynamic memory allocation fails.
  261. */
  262. static int ProcessUserChain(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  263. const unsigned char* buff, long sz, int format, int type, long* used,
  264. EncryptedInfo* info, int verify)
  265. {
  266. int ret = 0;
  267. void* heap = WOLFSSL_HEAP(ctx, ssl);
  268. WOLFSSL_ENTER("ProcessUserChain");
  269. /* Validate parameters. */
  270. if ((type == CA_TYPE) && (ctx == NULL)) {
  271. WOLFSSL_MSG("Need context for CA load");
  272. ret = BAD_FUNC_ARG;
  273. }
  274. /* Ignore non-certificate types. */
  275. if ((ret == 0) && (type != CERT_TYPE) && (type != CHAIN_CERT_TYPE) &&
  276. (type != CA_TYPE)) {
  277. WOLFSSL_MSG("File type not a certificate");
  278. }
  279. /* Check we haven't consumed all the data. */
  280. else if ((ret == 0) && (info->consumed >= sz)) {
  281. WOLFSSL_MSG("Already consumed data");
  282. }
  283. else if (ret == 0) {
  284. #ifndef WOLFSSL_SMALL_STACK
  285. byte stackBuffer[FILE_BUFFER_SIZE];
  286. #endif
  287. StaticBuffer chain;
  288. long consumed = info->consumed;
  289. word32 idx = 0;
  290. int gotOne = 0;
  291. int cnt = 0;
  292. /* Calculate max possible size, including max headers */
  293. long maxSz = (sz - consumed) + (CERT_HEADER_SZ * MAX_CHAIN_DEPTH);
  294. /* Setup buffer to hold chain. */
  295. #ifdef WOLFSSL_SMALL_STACK
  296. static_buffer_init(&chain);
  297. #else
  298. static_buffer_init(&chain, stackBuffer, FILE_BUFFER_SIZE);
  299. #endif
  300. /* Make buffer big enough to support maximum size. */
  301. ret = static_buffer_set_size(&chain, (word32)maxSz, heap,
  302. DYNAMIC_TYPE_FILE);
  303. WOLFSSL_MSG("Processing Cert Chain");
  304. /* Keep parsing certificates will data available. */
  305. while ((ret == 0) && (consumed < sz)) {
  306. DerBuffer* part = NULL;
  307. /* Get a certificate as DER. */
  308. ret = DataToDerBuffer(buff + consumed, (word32)(sz - consumed),
  309. format, type, info, heap, &part);
  310. if (ret == 0) {
  311. /* Process the user certificate. */
  312. ret = ProcessUserCert(ctx->cm, &part, type, verify,
  313. chain.buffer, &idx, (word32)maxSz);
  314. }
  315. /* PEM may have trailing data that can be ignored. */
  316. if ((ret == ASN_NO_PEM_HEADER) && gotOne) {
  317. WOLFSSL_MSG("We got one good cert, so stuff at end ok");
  318. ret = 0;
  319. break;
  320. }
  321. /* Certificate data handled. */
  322. FreeDer(&part);
  323. if (ret == 0) {
  324. /* Update consumed length. */
  325. consumed += info->consumed;
  326. WOLFSSL_MSG(" Consumed another Cert in Chain");
  327. /* Update whether we got a user certificate. */
  328. gotOne |= (type != CA_TYPE);
  329. /* Update count of certificates added to chain. */
  330. cnt++;
  331. }
  332. }
  333. if (used != NULL) {
  334. /* Return the total consumed length. */
  335. *used = consumed;
  336. }
  337. /* Check whether there is data in the chain buffer. */
  338. if ((ret == 0) && (idx > 0)) {
  339. /* Put the chain buffer against the SSL or SSL context object. */
  340. ret = ProcessUserChainRetain(ctx, ssl, chain.buffer, idx, cnt, type,
  341. heap);
  342. }
  343. /* Dispose of chain buffer. */
  344. static_buffer_free(&chain, heap, DYNAMIC_TYPE_FILE);
  345. }
  346. WOLFSSL_LEAVE("ProcessUserChain", ret);
  347. return ret;
  348. }
  349. #ifndef NO_RSA
  350. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  351. (HAVE_FIPS_VERSION > 2))
  352. /* See if DER data is an RSA private key.
  353. *
  354. * Checks size meets minimum RSA key size.
  355. * This implementation uses less dynamic memory.
  356. *
  357. * @param [in, out] ctx SSL context object.
  358. * @param [in, out] ssl SSL object.
  359. * @param [in] der DER encoding.
  360. * @param [in, out] keyFormat On in, expected format. 0 means unknown.
  361. * @param [in] devId Device identifier.
  362. * @param [out] keyType Type of key.
  363. * @param [out] keySize Size of key.
  364. * @return 0 on success or not an RSA key and format unknown.
  365. * @return RSA_KEY_SIZE_E when key size doesn't meet minimum required.
  366. */
  367. static int ProcessBufferTryDecodeRsa(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  368. DerBuffer* der, int* keyFormat, int devId, byte* keyType, int* keySize)
  369. {
  370. int ret;
  371. word32 idx;
  372. int keySz = 0;
  373. (void)devId;
  374. /* Validate we have an RSA private key and get key size. */
  375. idx = 0;
  376. ret = wc_RsaPrivateKeyValidate(der->buffer, &idx, &keySz, der->length);
  377. #ifdef WOLF_PRIVATE_KEY_ID
  378. /* If that didn't work then maybe a public key if device ID or callback. */
  379. if ((ret != 0) && ((devId != INVALID_DEVID) ||
  380. WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) {
  381. word32 nSz;
  382. /* Decode as an RSA public key. */
  383. idx = 0;
  384. ret = wc_RsaPublicKeyDecode_ex(der->buffer, &idx, der->length, NULL,
  385. &nSz, NULL, NULL);
  386. if (ret == 0) {
  387. keySz = (int)nSz;
  388. }
  389. }
  390. #endif
  391. if (ret == 0) {
  392. /* Get the minimum RSA key size from SSL or SSL context object. */
  393. int minRsaSz = ssl ? ssl->options.minRsaKeySz : ctx->minRsaKeySz;
  394. /* Format, type and size are known. */
  395. *keyFormat = RSAk;
  396. *keyType = rsa_sa_algo;
  397. *keySize = keySz;
  398. /* Check that the size of the RSA key is enough. */
  399. if (keySz < minRsaSz) {
  400. WOLFSSL_MSG("Private Key size too small");
  401. ret = RSA_KEY_SIZE_E;
  402. }
  403. /* No static ECC key possible. */
  404. if ((ssl != NULL) && (ssl->options.side == WOLFSSL_SERVER_END)) {
  405. ssl->options.haveStaticECC = 0;
  406. }
  407. }
  408. /* Not an RSA key but check whether we know what it is. */
  409. else if (*keyFormat == 0) {
  410. WOLFSSL_MSG("Not an RSA key");
  411. /* Format unknown so keep trying. */
  412. ret = 0;
  413. }
  414. return ret;
  415. }
  416. #else
  417. /* See if DER data is an RSA private key.
  418. *
  419. * Checks size meets minimum RSA key size.
  420. * This implementation uses more dynamic memory but supports older FIPS.
  421. *
  422. * @param [in, out] ctx SSL context object.
  423. * @param [in, out] ssl SSL object.
  424. * @param [in] der DER encoding.
  425. * @param [in, out] keyFormat On in, expected format. 0 means unknown.
  426. * @param [in] heap Dynamic memory allocation hint.
  427. * @param [in] devId Device identifier.
  428. * @param [out] keyType Type of key.
  429. * @param [out] keySize Size of key.
  430. * @return 0 on success or not an RSA key and format unknown.
  431. * @return RSA_KEY_SIZE_E when key size doesn't meet minimum required.
  432. */
  433. static int ProcessBufferTryDecodeRsa(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  434. DerBuffer* der, int* keyFormat, void* heap, int devId, byte* keyType,
  435. int* keySize)
  436. {
  437. int ret;
  438. word32 idx;
  439. /* make sure RSA key can be used */
  440. #ifdef WOLFSSL_SMALL_STACK
  441. RsaKey* key;
  442. #else
  443. RsaKey key[1];
  444. #endif
  445. #ifdef WOLFSSL_SMALL_STACK
  446. /* Allocate an RSA key to parse into so we can get size. */
  447. key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA);
  448. if (key == NULL)
  449. return MEMORY_E;
  450. #endif
  451. /* Initialize the RSA key. */
  452. ret = wc_InitRsaKey_ex(key, heap, devId);
  453. if (ret == 0) {
  454. /* Check we have an RSA private key. */
  455. idx = 0;
  456. ret = wc_RsaPrivateKeyDecode(der->buffer, &idx, key, der->length);
  457. #ifdef WOLF_PRIVATE_KEY_ID
  458. /* If that didn't work then maybe a public key if device ID or callback.
  459. */
  460. if ((ret != 0) && ((devId != INVALID_DEVID) ||
  461. WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) {
  462. /* If that didn't work then maybe a public key if device ID or
  463. * callback. */
  464. idx = 0;
  465. ret = wc_RsaPublicKeyDecode(der->buffer, &idx, key, der->length);
  466. }
  467. #endif
  468. if (ret == 0) {
  469. /* Get the minimum RSA key size from SSL or SSL context object. */
  470. int minRsaSz = ssl ? ssl->options.minRsaKeySz : ctx->minRsaKeySz;
  471. int keySz = wc_RsaEncryptSize((RsaKey*)key);
  472. /* Format is known. */
  473. *keyFormat = RSAk;
  474. *keyType = rsa_sa_algo;
  475. *keySize = keySz;
  476. /* Check that the size of the RSA key is enough. */
  477. if (keySz < minRsaSz) {
  478. WOLFSSL_MSG("Private Key size too small");
  479. ret = RSA_KEY_SIZE_E;
  480. }
  481. /* No static ECC key possible. */
  482. if ((ssl != NULL) && (ssl->options.side == WOLFSSL_SERVER_END)) {
  483. ssl->options.haveStaticECC = 0;
  484. }
  485. }
  486. /* Not an RSA key but check whether we know what it is. */
  487. else if (*keyFormat == 0) {
  488. WOLFSSL_MSG("Not an RSA key");
  489. /* Format unknown so keep trying. */
  490. ret = 0;
  491. }
  492. /* Free dynamically allocated data in key. */
  493. wc_FreeRsaKey(key);
  494. }
  495. #ifdef WOLFSSL_SMALL_STACK
  496. /* Dispose of allocated key. */
  497. XFREE(key, heap, DYNAMIC_TYPE_RSA);
  498. #endif
  499. return ret;
  500. }
  501. #endif
  502. #endif /* !NO_RSA */
  503. #ifdef HAVE_ECC
  504. /* See if DER data is an ECC private key.
  505. *
  506. * Checks size meets minimum ECC key size.
  507. *
  508. * @param [in, out] ctx SSL context object.
  509. * @param [in, out] ssl SSL object.
  510. * @param [in] der DER encoding.
  511. * @param [in, out] keyFormat On in, expected format. 0 means unknown.
  512. * @param [in] heap Dynamic memory allocation hint.
  513. * @param [in] devId Device identifier.
  514. * @param [out] keyType Type of key.
  515. * @param [out] keySize Size of key.
  516. * @return 0 on success or not an ECC key and format unknown.
  517. * @return ECC_KEY_SIZE_E when ECC key size doesn't meet minimum required.
  518. */
  519. static int ProcessBufferTryDecodeEcc(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  520. DerBuffer* der, int* keyFormat, void* heap, int devId, byte* keyType,
  521. int* keySize)
  522. {
  523. int ret = 0;
  524. word32 idx;
  525. /* make sure ECC key can be used */
  526. #ifdef WOLFSSL_SMALL_STACK
  527. ecc_key* key;
  528. #else
  529. ecc_key key[1];
  530. #endif
  531. #ifdef WOLFSSL_SMALL_STACK
  532. /* Allocate an ECC key to parse into. */
  533. key = (ecc_key*)XMALLOC(sizeof(ecc_key), heap, DYNAMIC_TYPE_ECC);
  534. if (key == NULL)
  535. return MEMORY_E;
  536. #endif
  537. /* Initialize ECC key. */
  538. if (wc_ecc_init_ex(key, heap, devId) == 0) {
  539. /* Decode as an ECC private key. */
  540. idx = 0;
  541. ret = wc_EccPrivateKeyDecode(der->buffer, &idx, key, der->length);
  542. #ifdef WOLF_PRIVATE_KEY_ID
  543. /* If that didn't work then maybe a public key if device ID or callback.
  544. */
  545. if ((ret != 0) && ((devId != INVALID_DEVID) ||
  546. WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) {
  547. /* Decode as an ECC public key. */
  548. idx = 0;
  549. ret = wc_EccPublicKeyDecode(der->buffer, &idx, key, der->length);
  550. }
  551. #endif
  552. if (ret == 0) {
  553. /* Get the minimum ECC key size from SSL or SSL context object. */
  554. int minKeySz = ssl ? ssl->options.minEccKeySz : ctx->minEccKeySz;
  555. int keySz = wc_ecc_size(key);
  556. /* Format is known. */
  557. *keyFormat = ECDSAk;
  558. #ifdef WOLFSSL_SM2
  559. if (key->dp->id == ECC_SM2P256V1) {
  560. *keyType = sm2_sa_algo;
  561. }
  562. else
  563. #endif
  564. {
  565. *keyType = ecc_dsa_sa_algo;
  566. }
  567. *keySize = keySz;
  568. /* Check that the size of the ECC key is enough. */
  569. if (keySz < minKeySz) {
  570. WOLFSSL_MSG("ECC private key too small");
  571. ret = ECC_KEY_SIZE_E;
  572. }
  573. /* Static ECC key possible. */
  574. if (ssl) {
  575. ssl->options.haveStaticECC = 1;
  576. }
  577. else {
  578. ctx->haveStaticECC = 1;
  579. }
  580. }
  581. /* Not an ECC key but check whether we know what it is. */
  582. else if (*keyFormat == 0) {
  583. WOLFSSL_MSG("Not an ECC key");
  584. /* Format unknown so keep trying. */
  585. ret = 0;
  586. }
  587. /* Free dynamically allocated data in key. */
  588. wc_ecc_free(key);
  589. }
  590. #ifdef WOLFSSL_SMALL_STACK
  591. /* Dispose of allocated key. */
  592. XFREE(key, heap, DYNAMIC_TYPE_ECC);
  593. #endif
  594. return ret;
  595. }
  596. #endif /* HAVE_ECC */
  597. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  598. /* See if DER data is an Ed25519 private key.
  599. *
  600. * Checks size meets minimum ECC key size.
  601. *
  602. * @param [in, out] ctx SSL context object.
  603. * @param [in, out] ssl SSL object.
  604. * @param [in] der DER encoding.
  605. * @param [in, out] keyFormat On in, expected format. 0 means unknown.
  606. * @param [in] heap Dynamic memory allocation hint.
  607. * @param [in] devId Device identifier.
  608. * @param [out] keyType Type of key.
  609. * @param [out] keySize Size of key.
  610. * @return 0 on success or not an Ed25519 key and format unknown.
  611. * @return ECC_KEY_SIZE_E when key size doesn't meet minimum required.
  612. */
  613. static int ProcessBufferTryDecodeEd25519(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  614. DerBuffer* der, int* keyFormat, void* heap, int devId, byte* keyType,
  615. int* keySize)
  616. {
  617. int ret;
  618. word32 idx;
  619. /* make sure Ed25519 key can be used */
  620. #ifdef WOLFSSL_SMALL_STACK
  621. ed25519_key* key;
  622. #else
  623. ed25519_key key[1];
  624. #endif
  625. #ifdef WOLFSSL_SMALL_STACK
  626. /* Allocate an Ed25519 key to parse into. */
  627. key = (ed25519_key*)XMALLOC(sizeof(ed25519_key), heap,
  628. DYNAMIC_TYPE_ED25519);
  629. if (key == NULL)
  630. return MEMORY_E;
  631. #endif
  632. /* Initialize Ed25519 key. */
  633. ret = wc_ed25519_init_ex(key, heap, devId);
  634. if (ret == 0) {
  635. /* Decode as an Ed25519 private key. */
  636. idx = 0;
  637. ret = wc_Ed25519PrivateKeyDecode(der->buffer, &idx, key, der->length);
  638. #ifdef WOLF_PRIVATE_KEY_ID
  639. /* If that didn't work then maybe a public key if device ID or callback.
  640. */
  641. if ((ret != 0) && ((devId != INVALID_DEVID) ||
  642. WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) {
  643. /* Decode as an Ed25519 public key. */
  644. idx = 0;
  645. ret = wc_Ed25519PublicKeyDecode(der->buffer, &idx, key,
  646. der->length);
  647. }
  648. #endif
  649. if (ret == 0) {
  650. /* Get the minimum ECC key size from SSL or SSL context object. */
  651. int minKeySz = ssl ? ssl->options.minEccKeySz : ctx->minEccKeySz;
  652. /* Format is known. */
  653. *keyFormat = ED25519k;
  654. *keyType = ed25519_sa_algo;
  655. *keySize = ED25519_KEY_SIZE;
  656. /* Check that the size of the ECC key is enough. */
  657. if (ED25519_KEY_SIZE < minKeySz) {
  658. WOLFSSL_MSG("ED25519 private key too small");
  659. ret = ECC_KEY_SIZE_E;
  660. }
  661. if (ssl != NULL) {
  662. #if !defined(WOLFSSL_NO_CLIENT_AUTH) && !defined(NO_ED25519_CLIENT_AUTH)
  663. /* Ed25519 requires caching enabled for tracking message
  664. * hash used in EdDSA_Update for signing */
  665. ssl->options.cacheMessages = 1;
  666. #endif
  667. }
  668. }
  669. /* Not an Ed25519 key but check whether we know what it is. */
  670. else if (*keyFormat == 0) {
  671. WOLFSSL_MSG("Not an Ed25519 key");
  672. /* Format unknown so keep trying. */
  673. ret = 0;
  674. }
  675. /* Free dynamically allocated data in key. */
  676. wc_ed25519_free(key);
  677. }
  678. #ifdef WOLFSSL_SMALL_STACK
  679. /* Dispose of allocated key. */
  680. XFREE(key, heap, DYNAMIC_TYPE_ED25519);
  681. #endif
  682. return ret;
  683. }
  684. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_IMPORT */
  685. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  686. /* See if DER data is an Ed448 private key.
  687. *
  688. * Checks size meets minimum ECC key size.
  689. *
  690. * @param [in, out] ctx SSL context object.
  691. * @param [in, out] ssl SSL object.
  692. * @param [in] der DER encoding.
  693. * @param [in, out] keyFormat On in, expected format. 0 means unknown.
  694. * @param [in] heap Dynamic memory allocation hint.
  695. * @param [in] devId Device identifier.
  696. * @param [out] keyType Type of key.
  697. * @param [out] keySize Size of key.
  698. * @return 0 on success or not an Ed448 key and format unknown.
  699. * @return ECC_KEY_SIZE_E when key size doesn't meet minimum required.
  700. */
  701. static int ProcessBufferTryDecodeEd448(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  702. DerBuffer* der, int* keyFormat, void* heap, int devId, byte* keyType,
  703. int* keySize)
  704. {
  705. int ret;
  706. word32 idx;
  707. /* make sure Ed448 key can be used */
  708. #ifdef WOLFSSL_SMALL_STACK
  709. ed448_key* key = NULL;
  710. #else
  711. ed448_key key[1];
  712. #endif
  713. #ifdef WOLFSSL_SMALL_STACK
  714. /* Allocate an Ed448 key to parse into. */
  715. key = (ed448_key*)XMALLOC(sizeof(ed448_key), heap, DYNAMIC_TYPE_ED448);
  716. if (key == NULL)
  717. return MEMORY_E;
  718. #endif
  719. /* Initialize Ed448 key. */
  720. ret = wc_ed448_init_ex(key, heap, devId);
  721. if (ret == 0) {
  722. /* Decode as an Ed448 private key. */
  723. idx = 0;
  724. ret = wc_Ed448PrivateKeyDecode(der->buffer, &idx, key, der->length);
  725. #ifdef WOLF_PRIVATE_KEY_ID
  726. /* If that didn't work then maybe a public key if device ID or callback.
  727. */
  728. if ((ret != 0) && ((devId != INVALID_DEVID) ||
  729. WOLFSSL_IS_PRIV_PK_SET(ctx, ssl))) {
  730. /* Decode as an Ed448 public key. */
  731. idx = 0;
  732. ret = wc_Ed448PublicKeyDecode(der->buffer, &idx, key, der->length);
  733. }
  734. #endif
  735. if (ret == 0) {
  736. /* Get the minimum ECC key size from SSL or SSL context object. */
  737. int minKeySz = ssl ? ssl->options.minEccKeySz : ctx->minEccKeySz;
  738. /* Format is known. */
  739. *keyFormat = ED448k;
  740. *keyType = ed448_sa_algo;
  741. *keySize = ED448_KEY_SIZE;
  742. /* Check that the size of the ECC key is enough. */
  743. if (ED448_KEY_SIZE < minKeySz) {
  744. WOLFSSL_MSG("ED448 private key too small");
  745. ret = ECC_KEY_SIZE_E;
  746. }
  747. if (ssl != NULL) {
  748. /* Ed448 requires caching enabled for tracking message
  749. * hash used in EdDSA_Update for signing */
  750. ssl->options.cacheMessages = 1;
  751. }
  752. }
  753. /* Not an Ed448 key but check whether we know what it is. */
  754. else if (*keyFormat == 0) {
  755. WOLFSSL_MSG("Not an Ed448 key");
  756. /* Format unknown so keep trying. */
  757. ret = 0;
  758. }
  759. /* Free dynamically allocated data in key. */
  760. wc_ed448_free(key);
  761. }
  762. #ifdef WOLFSSL_SMALL_STACK
  763. /* Dispose of allocated key. */
  764. XFREE(key, heap, DYNAMIC_TYPE_ED448);
  765. #endif
  766. return ret;
  767. }
  768. #endif /* HAVE_ED448 && HAVE_ED448_KEY_IMPORT */
  769. #if defined(HAVE_PQC)
  770. #if defined(HAVE_FALCON)
  771. /* See if DER data is an Falcon private key.
  772. *
  773. * Checks size meets minimum Falcon key size.
  774. *
  775. * @param [in, out] ctx SSL context object.
  776. * @param [in, out] ssl SSL object.
  777. * @param [in] der DER encoding.
  778. * @param [in, out] keyFormat On in, expected format. 0 means unknown.
  779. * @param [in] heap Dynamic memory allocation hint.
  780. * @param [in] devId Device identifier.
  781. * @param [out] keyType Type of key.
  782. * @param [out] keySize Size of key.
  783. * @return 0 on success or not an Falcon key and format unknown.
  784. * @return FALCON_KEY_SIZE_E when key size doesn't meet minimum required.
  785. */
  786. static int ProcessBufferTryDecodeFalcon(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  787. DerBuffer* der, int* keyFormat, void* heap, byte* keyType, int* keySize)
  788. {
  789. int ret;
  790. falcon_key* key;
  791. /* Allocate a Falcon key to parse into. */
  792. key = (falcon_key*)XMALLOC(sizeof(falcon_key), heap, DYNAMIC_TYPE_FALCON);
  793. if (key == NULL) {
  794. return MEMORY_E;
  795. }
  796. /* Initialize Falcon key. */
  797. ret = wc_falcon_init(key);
  798. if (ret == 0) {
  799. /* Set up key to parse the format specified. */
  800. if (*keyFormat == FALCON_LEVEL1k) {
  801. ret = wc_falcon_set_level(key, 1);
  802. }
  803. else if (*keyFormat == FALCON_LEVEL5k) {
  804. ret = wc_falcon_set_level(key, 5);
  805. }
  806. else {
  807. /* What if *keyformat is 0? We might want to do something more
  808. * graceful here. */
  809. /* TODO: get the size of the private key for different formats and
  810. * compare with DER length. */
  811. wc_falcon_free(key);
  812. ret = ALGO_ID_E;
  813. }
  814. }
  815. if (ret == 0) {
  816. /* Decode as a Falcon private key. */
  817. ret = wc_falcon_import_private_only(der->buffer, der->length, key);
  818. if (ret == 0) {
  819. /* Get the minimum Falcon key size from SSL or SSL context object.
  820. */
  821. int minKeySz = ssl ? ssl->options.minFalconKeySz :
  822. ctx->minFalconKeySz;
  823. /* Format is known. */
  824. if (*keyFormat == FALCON_LEVEL1k) {
  825. *keyType = falcon_level1_sa_algo;
  826. *keySize = FALCON_LEVEL1_KEY_SIZE;
  827. }
  828. else {
  829. *keyType = falcon_level5_sa_algo;
  830. *keySize = FALCON_LEVEL5_KEY_SIZE;
  831. }
  832. /* Check that the size of the Falcon key is enough. */
  833. if (*keySize < minKeySz) {
  834. WOLFSSL_MSG("Falcon private key too small");
  835. ret = FALCON_KEY_SIZE_E;
  836. }
  837. }
  838. /* Not a Falcon key but check whether we know what it is. */
  839. else if (*keyFormat == 0) {
  840. WOLFSSL_MSG("Not a Falcon key");
  841. /* Format unknown so keep trying. */
  842. ret = 0;
  843. }
  844. /* Free dynamically allocated data in key. */
  845. wc_falcon_free(key);
  846. }
  847. /* Dispose of allocated key. */
  848. XFREE(key, heap, DYNAMIC_TYPE_FALCON);
  849. return ret;
  850. }
  851. #endif
  852. #if defined(HAVE_DILITHIUM)
  853. /* See if DER data is an Dilithium private key.
  854. *
  855. * Checks size meets minimum Falcon key size.
  856. *
  857. * @param [in, out] ctx SSL context object.
  858. * @param [in, out] ssl SSL object.
  859. * @param [in] der DER encoding.
  860. * @param [in, out] keyFormat On in, expected format. 0 means unknown.
  861. * @param [in] heap Dynamic memory allocation hint.
  862. * @param [in] devId Device identifier.
  863. * @param [out] keyType Type of key.
  864. * @param [out] keySize Size of key.
  865. * @return 0 on success or not a Dilithium key and format unknown.
  866. * @return DILITHIUM_KEY_SIZE_E when key size doesn't meet minimum required.
  867. */
  868. static int ProcessBufferTryDecodeDilithium(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  869. DerBuffer* der, int* keyFormat, void* heap, byte* keyType, int* keySize)
  870. {
  871. int ret;
  872. dilithium_key* key;
  873. /* Allocate a Dilithium key to parse into. */
  874. key = (dilithium_key*)XMALLOC(sizeof(dilithium_key), heap,
  875. DYNAMIC_TYPE_DILITHIUM);
  876. if (key == NULL) {
  877. return MEMORY_E;
  878. }
  879. /* Initialize Dilithium key. */
  880. ret = wc_dilithium_init(key);
  881. if (ret == 0) {
  882. /* Set up key to parse the format specified. */
  883. if (*keyFormat == DILITHIUM_LEVEL2k) {
  884. ret = wc_dilithium_set_level(key, 2);
  885. }
  886. else if (*keyFormat == DILITHIUM_LEVEL3k) {
  887. ret = wc_dilithium_set_level(key, 3);
  888. }
  889. else if (*keyFormat == DILITHIUM_LEVEL5k) {
  890. ret = wc_dilithium_set_level(key, 5);
  891. }
  892. else {
  893. /* What if *keyformat is 0? We might want to do something more
  894. * graceful here. */
  895. /* TODO: get the size of the private key for different formats and
  896. * compare with DER length. */
  897. wc_dilithium_free(key);
  898. ret = ALGO_ID_E;
  899. }
  900. }
  901. if (ret == 0) {
  902. /* Decode as a Dilithium private key. */
  903. ret = wc_dilithium_import_private_only(der->buffer, der->length, key);
  904. if (ret == 0) {
  905. /* Get the minimum Dilithium key size from SSL or SSL context
  906. * object. */
  907. int minKeySz = ssl ? ssl->options.minDilithiumKeySz :
  908. ctx->minDilithiumKeySz;
  909. /* Format is known. */
  910. if (*keyFormat == DILITHIUM_LEVEL2k) {
  911. *keyType = dilithium_level2_sa_algo;
  912. *keySize = DILITHIUM_LEVEL2_KEY_SIZE;
  913. }
  914. else if (*keyFormat == DILITHIUM_LEVEL3k) {
  915. *keyType = dilithium_level3_sa_algo;
  916. *keySize = DILITHIUM_LEVEL3_KEY_SIZE;
  917. }
  918. else if (*keyFormat == DILITHIUM_LEVEL5k) {
  919. *keyType = dilithium_level5_sa_algo;
  920. *keySize = DILITHIUM_LEVEL5_KEY_SIZE;
  921. }
  922. /* Check that the size of the Dilithium key is enough. */
  923. if (*keySize < minKeySz) {
  924. WOLFSSL_MSG("Dilithium private key too small");
  925. ret = DILITHIUM_KEY_SIZE_E;
  926. }
  927. }
  928. /* Not a Dilithium key but check whether we know what it is. */
  929. else if (*keyFormat == 0) {
  930. WOLFSSL_MSG("Not a Dilithium key");
  931. /* Format unknown so keep trying. */
  932. ret = 0;
  933. }
  934. /* Free dynamically allocated data in key. */
  935. wc_dilithium_free(key);
  936. }
  937. /* Dispose of allocated key. */
  938. XFREE(key, heap, DYNAMIC_TYPE_DILITHIUM);
  939. return ret;
  940. }
  941. #endif /* HAVE_DILITHIUM */
  942. #endif /* HAVE_PQC */
  943. /* Try to decode DER data is a known private key.
  944. *
  945. * Checks size meets minimum for key type.
  946. *
  947. * @param [in, out] ctx SSL context object.
  948. * @param [in, out] ssl SSL object.
  949. * @param [in] der DER encoding.
  950. * @param [in, out] keyFormat On in, expected format. 0 means unknown.
  951. * @param [in] heap Dynamic memory allocation hint.
  952. * @param [out] type Type of key:
  953. * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
  954. * @return 0 on success.
  955. * @return BAD_FUNC_ARG when der or keyFormat is NULL.
  956. * @return BAD_FUNC_ARG when ctx and ssl are NULL.
  957. * @return WOLFSSL_BAD_FILE when unable to identify the key format.
  958. */
  959. static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  960. DerBuffer* der, int* keyFormat, void* heap, int type)
  961. {
  962. int ret = 0;
  963. int devId = wolfSSL_CTX_GetDevId(ctx, ssl);
  964. byte* keyType = NULL;
  965. int* keySz = NULL;
  966. (void)heap;
  967. (void)devId;
  968. (void)type;
  969. /* Validate parameters. */
  970. if ((der == NULL) || (keyFormat == NULL)) {
  971. ret = BAD_FUNC_ARG;
  972. }
  973. /* Must have an SSL context or SSL object to use. */
  974. if ((ret == 0) && (ctx == NULL) && (ssl == NULL)) {
  975. ret = BAD_FUNC_ARG;
  976. }
  977. if (ret == 0) {
  978. /* Determine where to put key type and size in SSL or context object. */
  979. #ifdef WOLFSSL_DUAL_ALG_CERTS
  980. if (type == ALT_PRIVATEKEY_TYPE) {
  981. if (ssl != NULL) {
  982. keyType = &ssl->buffers.altKeyType;
  983. keySz = &ssl->buffers.altKeySz;
  984. }
  985. else {
  986. keyType = &ctx->altPrivateKeyType;
  987. keySz = &ctx->altPrivateKeySz;
  988. }
  989. }
  990. else
  991. #endif
  992. /* Type is PRIVATEKEY_TYPE. */
  993. if (ssl != NULL) {
  994. keyType = &ssl->buffers.keyType;
  995. keySz = &ssl->buffers.keySz;
  996. }
  997. else {
  998. keyType = &ctx->privateKeyType;
  999. keySz = &ctx->privateKeySz;
  1000. }
  1001. }
  1002. #ifndef NO_RSA
  1003. /* Try RSA if key format is RSA or yet unknown. */
  1004. if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == RSAk))) {
  1005. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  1006. (HAVE_FIPS_VERSION > 2))
  1007. ret = ProcessBufferTryDecodeRsa(ctx, ssl, der, keyFormat, devId,
  1008. keyType, keySz);
  1009. #else
  1010. ret = ProcessBufferTryDecodeRsa(ctx, ssl, der, keyFormat, heap, devId,
  1011. keyType, keySz);
  1012. #endif
  1013. }
  1014. #endif
  1015. #ifdef HAVE_ECC
  1016. /* Try ECC if key format is ECDSA or SM2, or yet unknown. */
  1017. if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == ECDSAk)
  1018. #ifdef WOLFSSL_SM2
  1019. || (*keyFormat == SM2k)
  1020. #endif
  1021. )) {
  1022. ret = ProcessBufferTryDecodeEcc(ctx, ssl, der, keyFormat, heap, devId,
  1023. keyType, keySz);
  1024. }
  1025. #endif /* HAVE_ECC */
  1026. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  1027. /* Try Ed25519 if key format is Ed25519 or yet unknown. */
  1028. if ((ret == 0) && ((*keyFormat == 0 || *keyFormat == ED25519k))) {
  1029. ret = ProcessBufferTryDecodeEd25519(ctx, ssl, der, keyFormat, heap,
  1030. devId, keyType, keySz);
  1031. }
  1032. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_IMPORT */
  1033. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  1034. /* Try Ed448 if key format is Ed448 or yet unknown. */
  1035. if ((ret == 0) && ((*keyFormat == 0 || *keyFormat == ED448k))) {
  1036. ret = ProcessBufferTryDecodeEd448(ctx, ssl, der, keyFormat, heap, devId,
  1037. keyType, keySz);
  1038. }
  1039. #endif /* HAVE_ED448 && HAVE_ED448_KEY_IMPORT */
  1040. #if defined(HAVE_PQC)
  1041. #if defined(HAVE_FALCON)
  1042. /* Try Falcon if key format is Falcon level 1k or 5k or yet unknown. */
  1043. if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == FALCON_LEVEL1k) ||
  1044. (*keyFormat == FALCON_LEVEL5k))) {
  1045. ret = ProcessBufferTryDecodeFalcon(ctx, ssl, der, keyFormat, heap,
  1046. keyType, keySz);
  1047. }
  1048. #endif /* HAVE_FALCON */
  1049. #if defined(HAVE_DILITHIUM)
  1050. /* Try Falcon if key format is Dilithium level 2k, 3k or 5k or yet unknown.
  1051. */
  1052. if ((ret == 0) && ((*keyFormat == 0) || (*keyFormat == DILITHIUM_LEVEL2k) ||
  1053. (*keyFormat == DILITHIUM_LEVEL3k) ||
  1054. (*keyFormat == DILITHIUM_LEVEL5k))) {
  1055. ret = ProcessBufferTryDecodeDilithium(ctx, ssl, der, keyFormat, heap,
  1056. keyType, keySz);
  1057. }
  1058. #endif /* HAVE_DILITHIUM */
  1059. #endif /* HAVE_PQC */
  1060. /* Check we know the format. */
  1061. if ((ret == 0) && (*keyFormat == 0)) {
  1062. WOLFSSL_MSG("Not a supported key type");
  1063. /* Not supported key format. */
  1064. ret = WOLFSSL_BAD_FILE;
  1065. }
  1066. return ret;
  1067. }
  1068. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
  1069. /* Decrypt PKCS#8 private key.
  1070. *
  1071. * @param [in] info Encryption information.
  1072. * @param [in] der DER encoded data.
  1073. * @param [in] heap Dynamic memory allocation hint.
  1074. * @return 0 on success.
  1075. * @return MEMORY_E when dynamic memory allocation fails.
  1076. */
  1077. static int ProcessBufferPrivPkcs8Dec(EncryptedInfo* info, DerBuffer* der,
  1078. void* heap)
  1079. {
  1080. int ret = 0;
  1081. word32 algId;
  1082. int passwordSz = NAME_SZ;
  1083. #ifndef WOLFSSL_SMALL_STACK
  1084. char password[NAME_SZ];
  1085. #else
  1086. char* password;
  1087. #endif
  1088. (void)heap;
  1089. #ifdef WOLFSSL_SMALL_STACK
  1090. /* Allocate memory for password. */
  1091. password = (char*)XMALLOC(passwordSz, heap, DYNAMIC_TYPE_STRING);
  1092. if (password == NULL) {
  1093. ret = MEMORY_E;
  1094. }
  1095. #endif
  1096. if (ret == 0) {
  1097. /* Get password. */
  1098. ret = info->passwd_cb(password, passwordSz, PEM_PASS_READ,
  1099. info->passwd_userdata);
  1100. }
  1101. if (ret >= 0) {
  1102. /* Returned value is password size. */
  1103. passwordSz = ret;
  1104. #ifdef WOLFSSL_CHECK_MEM_ZERO
  1105. wc_MemZero_Add("ProcessBuffer password", password, passwordSz);
  1106. #endif
  1107. /* Decrypt PKCS#8 private key inline and get algorithm id. */
  1108. ret = ToTraditionalEnc(der->buffer, der->length, password, passwordSz,
  1109. &algId);
  1110. }
  1111. if (ret >= 0) {
  1112. /* Zero out encrypted data not overwritten. */
  1113. ForceZero(der->buffer + ret, der->length - ret);
  1114. /* Set decrypted data length. */
  1115. der->length = ret;
  1116. }
  1117. /* Ensure password is zeroized. */
  1118. ForceZero(password, passwordSz);
  1119. #ifdef WOLFSSL_SMALL_STACK
  1120. /* Dispose of password memory. */
  1121. XFREE(password, heap, DYNAMIC_TYPE_STRING);
  1122. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  1123. wc_MemZero_Check(password, NAME_SZ);
  1124. #endif
  1125. return ret;
  1126. }
  1127. #endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */
  1128. /* Put the DER into the SSL or SSL context object.
  1129. *
  1130. * Precondition: ctx or ssl is not NULL.
  1131. * Precondition: Must be a private key type.
  1132. *
  1133. * @param [in, out] ctx SSL context object.
  1134. * @param [in, out] ssl SSL object.
  1135. * @param [in] der DER encoding.
  1136. */
  1137. static void ProcessBufferPrivKeyHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  1138. DerBuffer** der, int type)
  1139. {
  1140. (void)type;
  1141. #ifdef WOLFSSL_DUAL_ALG_CERTS
  1142. if (type == ALT_PRIVATEKEY_TYPE) {
  1143. /* Put in alternate private key fields of objects. */
  1144. if (ssl != NULL) {
  1145. /* Dispose of previous key if not context's. */
  1146. if (ssl->buffers.weOwnAltKey) {
  1147. FreeDer(&ssl->buffers.altKey);
  1148. }
  1149. ssl->buffers.altKeyId = 0;
  1150. ssl->buffers.altKeyLabel = 0;
  1151. ssl->buffers.altKeyDevId = INVALID_DEVID;
  1152. /* Store key by reference and own it. */
  1153. ssl->buffers.altKey = *der;
  1154. #ifdef WOLFSSL_CHECK_MEM_ZERO
  1155. wc_MemZero_Add("SSL Buffers key", (*der)->buffer, (*der)->length);
  1156. #endif
  1157. ssl->buffers.weOwnAltKey = 1;
  1158. }
  1159. else if (ctx != NULL) {
  1160. /* Dispose of previous key. */
  1161. FreeDer(&ctx->altPrivateKey);
  1162. ctx->altPrivateKeyId = 0;
  1163. ctx->altPrivateKeyLabel = 0;
  1164. ctx->altPrivateKeyDevId = INVALID_DEVID;
  1165. /* Store key by reference. */
  1166. ctx->altPrivateKey = *der;
  1167. #ifdef WOLFSSL_CHECK_MEM_ZERO
  1168. wc_MemZero_Add("CTX private key", (*der)->buffer, (*der)->length);
  1169. #endif
  1170. }
  1171. }
  1172. else
  1173. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  1174. if (ssl != NULL) {
  1175. /* Dispose of previous key if not context's. */
  1176. if (ssl->buffers.weOwnKey) {
  1177. FreeDer(&ssl->buffers.key);
  1178. }
  1179. ssl->buffers.keyId = 0;
  1180. ssl->buffers.keyLabel = 0;
  1181. ssl->buffers.keyDevId = INVALID_DEVID;
  1182. /* Store key by reference and own it. */
  1183. ssl->buffers.key = *der;
  1184. #ifdef WOLFSSL_CHECK_MEM_ZERO
  1185. wc_MemZero_Add("SSL Buffers key", (*der)->buffer, (*der)->length);
  1186. #endif
  1187. ssl->buffers.weOwnKey = 1;
  1188. }
  1189. else if (ctx != NULL) {
  1190. /* Dispose of previous key. */
  1191. FreeDer(&ctx->privateKey);
  1192. ctx->privateKeyId = 0;
  1193. ctx->privateKeyLabel = 0;
  1194. ctx->privateKeyDevId = INVALID_DEVID;
  1195. /* Store key by reference. */
  1196. ctx->privateKey = *der;
  1197. #ifdef WOLFSSL_CHECK_MEM_ZERO
  1198. wc_MemZero_Add("CTX private key", (*der)->buffer, (*der)->length);
  1199. #endif
  1200. }
  1201. }
  1202. /* Decode private key.
  1203. *
  1204. * Precondition: ctx or ssl is not NULL.
  1205. * Precondition: Must be a private key type.
  1206. *
  1207. * @param [in, out] ctx SSL context object.
  1208. * @param [in, out] ssl SSL object.
  1209. * @param [in] der DER encoding.
  1210. * @param [in] format Original format of data.
  1211. * @param [in] info Encryption information.
  1212. * @param [in] heap Dynamic memory allocation hint.
  1213. * @param [in] type Type of data:
  1214. * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
  1215. * @return 0 on success.
  1216. * @return WOLFSSL_BAD_FILE when not able to decode.
  1217. */
  1218. static int ProcessBufferPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  1219. DerBuffer* der, int format, EncryptedInfo* info, void* heap, int type)
  1220. {
  1221. int ret;
  1222. int keyFormat = 0;
  1223. #if (defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)) || \
  1224. defined(HAVE_PKCS8)
  1225. word32 algId = 0;
  1226. #endif
  1227. (void)info;
  1228. (void)format;
  1229. #ifdef HAVE_PKCS8
  1230. /* Try and remove PKCS8 header and get algorithm id. */
  1231. ret = ToTraditional_ex(der->buffer, der->length, &algId);
  1232. if (ret > 0) {
  1233. /* Header stripped inline. */
  1234. der->length = ret;
  1235. keyFormat = algId;
  1236. }
  1237. #endif
  1238. /* Put the data into the SSL or SSL context object. */
  1239. ProcessBufferPrivKeyHandleDer(ctx, ssl, &der, type);
  1240. /* Try to decode the DER data. */
  1241. ret = ProcessBufferTryDecode(ctx, ssl, der, &keyFormat, heap, type);
  1242. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
  1243. /* If private key type PKCS8 header wasn't already removed (algId == 0). */
  1244. if (((ret != 0) || (keyFormat == 0)) && (format != WOLFSSL_FILETYPE_PEM) &&
  1245. (info->passwd_cb != NULL) && (algId == 0)) {
  1246. /* Try to decrypt DER data as a PKCS#8 private key. */
  1247. ret = ProcessBufferPrivPkcs8Dec(info, der, heap);
  1248. if (ret >= 0) {
  1249. /* Try to decode decrypted data. */
  1250. ret = ProcessBufferTryDecode(ctx, ssl, der, &keyFormat, heap, type);
  1251. }
  1252. }
  1253. #endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */
  1254. /* Check if we were able to determine key format. */
  1255. if ((ret == 0) && (keyFormat == 0)) {
  1256. #ifdef OPENSSL_EXTRA
  1257. /* Decryption password is probably wrong. */
  1258. if (info->passwd_cb) {
  1259. EVPerr(0, EVP_R_BAD_DECRYPT);
  1260. }
  1261. #endif
  1262. WOLFSSL_ERROR(WOLFSSL_BAD_FILE);
  1263. /* Unable to decode DER data. */
  1264. ret = WOLFSSL_BAD_FILE;
  1265. }
  1266. return ret;
  1267. }
  1268. /* Use the key OID to determine have options.
  1269. *
  1270. * @param [in, out] ctx SSL context object.
  1271. * @param [in, out] ssl SSL object.
  1272. * @param [in] keyOID OID for public/private key.
  1273. */
  1274. static void wolfssl_set_have_from_key_oid(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  1275. int keyOID)
  1276. {
  1277. /* Set which private key algorithm available based on key OID. */
  1278. switch (keyOID) {
  1279. case ECDSAk:
  1280. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
  1281. case SM2k:
  1282. #endif
  1283. #ifdef HAVE_ED25519
  1284. case ED25519k:
  1285. #endif
  1286. #ifdef HAVE_ED448
  1287. case ED448k:
  1288. #endif
  1289. if (ssl != NULL) {
  1290. ssl->options.haveECC = 1;
  1291. }
  1292. else {
  1293. ctx->haveECC = 1;
  1294. }
  1295. break;
  1296. #ifndef NO_RSA
  1297. case RSAk:
  1298. #ifdef WC_RSA_PSS
  1299. case RSAPSSk:
  1300. #endif
  1301. if (ssl != NULL) {
  1302. ssl->options.haveRSA = 1;
  1303. }
  1304. else {
  1305. ctx->haveRSA = 1;
  1306. }
  1307. break;
  1308. #endif
  1309. #ifdef HAVE_PQC
  1310. #ifdef HAVE_FALCON
  1311. case FALCON_LEVEL1k:
  1312. case FALCON_LEVEL5k:
  1313. if (ssl != NULL) {
  1314. ssl->options.haveFalconSig = 1;
  1315. }
  1316. else {
  1317. ctx->haveFalconSig = 1;
  1318. }
  1319. break;
  1320. #endif /* HAVE_FALCON */
  1321. #ifdef HAVE_DILITHIUM
  1322. case DILITHIUM_LEVEL2k:
  1323. case DILITHIUM_LEVEL3k:
  1324. case DILITHIUM_LEVEL5k:
  1325. if (ssl != NULL) {
  1326. ssl->options.haveDilithiumSig = 1;
  1327. }
  1328. else {
  1329. ctx->haveDilithiumSig = 1;
  1330. }
  1331. break;
  1332. #endif /* HAVE_DILITHIUM */
  1333. #endif /* HAVE_PQC */
  1334. default:
  1335. WOLFSSL_MSG("Cert key not supported");
  1336. break;
  1337. }
  1338. }
  1339. /* Set which private key algorithm we have against SSL or SSL context object.
  1340. *
  1341. * Precondition: ctx or ssl is not NULL.
  1342. *
  1343. * @param [in, out] ctx SSL context object.
  1344. * @param [in, out] ssl SSL object.
  1345. * @param [in] cert Decode certificate.
  1346. */
  1347. static void ProcessBufferCertSetHave(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  1348. DecodedCert* cert)
  1349. {
  1350. if (ssl != NULL) {
  1351. /* Reset signatures we have in SSL. */
  1352. ssl->options.haveECDSAsig = 0;
  1353. ssl->options.haveFalconSig = 0;
  1354. ssl->options.haveDilithiumSig = 0;
  1355. }
  1356. /* Set which signature we have based on the type in the cert. */
  1357. switch (cert->signatureOID) {
  1358. case CTC_SHAwECDSA:
  1359. case CTC_SHA256wECDSA:
  1360. case CTC_SHA384wECDSA:
  1361. case CTC_SHA512wECDSA:
  1362. #ifdef HAVE_ED25519
  1363. case CTC_ED25519:
  1364. #endif
  1365. #ifdef HAVE_ED448
  1366. case CTC_ED448:
  1367. #endif
  1368. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
  1369. case CTC_SM3wSM2:
  1370. #endif
  1371. WOLFSSL_MSG("ECDSA/ED25519/ED448 cert signature");
  1372. if (ssl) {
  1373. ssl->options.haveECDSAsig = 1;
  1374. }
  1375. else if (ctx) {
  1376. ctx->haveECDSAsig = 1;
  1377. }
  1378. break;
  1379. #ifdef HAVE_PQC
  1380. #ifdef HAVE_FALCON
  1381. case CTC_FALCON_LEVEL1:
  1382. case CTC_FALCON_LEVEL5:
  1383. WOLFSSL_MSG("Falcon cert signature");
  1384. if (ssl) {
  1385. ssl->options.haveFalconSig = 1;
  1386. }
  1387. else if (ctx) {
  1388. ctx->haveFalconSig = 1;
  1389. }
  1390. break;
  1391. #endif
  1392. #ifdef HAVE_DILITHIUM
  1393. case CTC_DILITHIUM_LEVEL2:
  1394. case CTC_DILITHIUM_LEVEL3:
  1395. case CTC_DILITHIUM_LEVEL5:
  1396. WOLFSSL_MSG("Dilithium cert signature");
  1397. if (ssl) {
  1398. ssl->options.haveDilithiumSig = 1;
  1399. }
  1400. else if (ctx) {
  1401. ctx->haveDilithiumSig = 1;
  1402. }
  1403. break;
  1404. #endif
  1405. #endif
  1406. default:
  1407. WOLFSSL_MSG("Cert signature not supported");
  1408. break;
  1409. }
  1410. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
  1411. (defined(HAVE_PQC) && defined(HAVE_LIBOQS)) || !defined(NO_RSA)
  1412. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  1413. /* Set the private key curve OID. */
  1414. if (ssl != NULL) {
  1415. ssl->pkCurveOID = cert->pkCurveOID;
  1416. }
  1417. else if (ctx) {
  1418. ctx->pkCurveOID = cert->pkCurveOID;
  1419. }
  1420. #endif
  1421. #ifndef WC_STRICT_SIG
  1422. wolfssl_set_have_from_key_oid(ctx, ssl, cert->keyOID);
  1423. #else
  1424. /* Set whether ECC is available baed on signature available. */
  1425. if (ssl != NULL) {
  1426. ssl->options.haveECC = ssl->options.haveECDSAsig;
  1427. }
  1428. else if (ctx) {
  1429. ctx->haveECC = ctx->haveECDSAsig;
  1430. }
  1431. #endif /* !WC_STRICT_SIG */
  1432. #endif
  1433. }
  1434. /* Check key size is valid.
  1435. *
  1436. * Precondition: ctx or ssl is not NULL.
  1437. *
  1438. * @param [in] min Minimum key size.
  1439. * @param [in] max Maximum key size.
  1440. * @param [in] keySz Key size.
  1441. * @param [in] err Error value to return when key size is invalid.
  1442. * @return 0 on success.
  1443. * @return err when verifying and min is less than 0 or key size is invalid.
  1444. */
  1445. #define CHECK_KEY_SZ(min, max, keySz, err) \
  1446. (((min) < 0) || ((keySz) < (min)) || ((keySz) > (max))) ? (err) : 0
  1447. /* Check public key in certificate.
  1448. *
  1449. * @param [in, out] ctx SSL context object.
  1450. * @param [in, out] ssl SSL object.
  1451. * @param [in] cert Certificate object.
  1452. * @return 0 on success.
  1453. * @return Non-zero when an error occurred.
  1454. */
  1455. static int ProcessBufferCertPublicKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  1456. DecodedCert* cert, int checkKeySz)
  1457. {
  1458. int ret = 0;
  1459. #ifdef WOLF_PRIVATE_KEY_ID
  1460. byte keyType = 0;
  1461. #endif
  1462. int keySz = 0;
  1463. #ifndef NO_RSA
  1464. word32 idx;
  1465. #endif
  1466. /* Get key size and check unless not verifying. */
  1467. switch (cert->keyOID) {
  1468. #ifndef NO_RSA
  1469. #ifdef WC_RSA_PSS
  1470. case RSAPSSk:
  1471. #endif
  1472. case RSAk:
  1473. #ifdef WOLF_PRIVATE_KEY_ID
  1474. keyType = rsa_sa_algo;
  1475. #endif
  1476. /* Determine RSA key size by parsing public key */
  1477. idx = 0;
  1478. ret = wc_RsaPublicKeyDecode_ex(cert->publicKey, &idx,
  1479. cert->pubKeySize, NULL, (word32*)&keySz, NULL, NULL);
  1480. if ((ret == 0) && checkKeySz) {
  1481. ret = CHECK_KEY_SZ(ssl ? ssl->options.minRsaKeySz :
  1482. ctx->minRsaKeySz, RSA_MAX_SIZE / 8, keySz, RSA_KEY_SIZE_E);
  1483. }
  1484. break;
  1485. #endif /* !NO_RSA */
  1486. #ifdef HAVE_ECC
  1487. case ECDSAk:
  1488. #ifdef WOLF_PRIVATE_KEY_ID
  1489. keyType = ecc_dsa_sa_algo;
  1490. #endif
  1491. /* Determine ECC key size based on curve */
  1492. #ifdef WOLFSSL_CUSTOM_CURVES
  1493. if ((cert->pkCurveOID == 0) && (cert->pkCurveSize != 0)) {
  1494. keySz = cert->pkCurveSize;
  1495. }
  1496. else
  1497. #endif
  1498. {
  1499. keySz = wc_ecc_get_curve_size_from_id(wc_ecc_get_oid(
  1500. cert->pkCurveOID, NULL, NULL));
  1501. }
  1502. if (checkKeySz) {
  1503. ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz :
  1504. ctx->minEccKeySz, (MAX_ECC_BITS + 7) / 8, keySz,
  1505. ECC_KEY_SIZE_E);
  1506. }
  1507. break;
  1508. #endif /* HAVE_ECC */
  1509. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
  1510. case SM2k:
  1511. #ifdef WOLF_PRIVATE_KEY_ID
  1512. keyType = sm2_sa_algo;
  1513. #endif
  1514. /* Determine ECC key size based on curve */
  1515. keySz = WOLFSSL_SM2_KEY_BITS / 8;
  1516. if (checkKeySz) {
  1517. ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz :
  1518. ctx->minEccKeySz, (MAX_ECC_BITS + 7) / 8, keySz,
  1519. ECC_KEY_SIZE_E);
  1520. }
  1521. break;
  1522. #endif /* HAVE_ED25519 */
  1523. #ifdef HAVE_ED25519
  1524. case ED25519k:
  1525. #ifdef WOLF_PRIVATE_KEY_ID
  1526. keyType = ed25519_sa_algo;
  1527. #endif
  1528. /* ED25519 is fixed key size */
  1529. keySz = ED25519_KEY_SIZE;
  1530. if (checkKeySz) {
  1531. ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz :
  1532. ctx->minEccKeySz, ED25519_KEY_SIZE, keySz, ECC_KEY_SIZE_E);
  1533. }
  1534. break;
  1535. #endif /* HAVE_ED25519 */
  1536. #ifdef HAVE_ED448
  1537. case ED448k:
  1538. #ifdef WOLF_PRIVATE_KEY_ID
  1539. keyType = ed448_sa_algo;
  1540. #endif
  1541. /* ED448 is fixed key size */
  1542. keySz = ED448_KEY_SIZE;
  1543. if (checkKeySz) {
  1544. ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz :
  1545. ctx->minEccKeySz, ED448_KEY_SIZE, keySz, ECC_KEY_SIZE_E);
  1546. }
  1547. break;
  1548. #endif /* HAVE_ED448 */
  1549. #if defined(HAVE_PQC)
  1550. #if defined(HAVE_FALCON)
  1551. case FALCON_LEVEL1k:
  1552. #ifdef WOLF_PRIVATE_KEY_ID
  1553. keyType = falcon_level1_sa_algo;
  1554. #endif
  1555. /* Falcon is fixed key size */
  1556. keySz = FALCON_LEVEL1_KEY_SIZE;
  1557. if (checkKeySz) {
  1558. ret = CHECK_KEY_SZ(ssl ? ssl->options.minFalconKeySz :
  1559. ctx->minFalconKeySz, FALCON_MAX_KEY_SIZE, keySz,
  1560. FALCON_KEY_SIZE_E);
  1561. }
  1562. break;
  1563. case FALCON_LEVEL5k:
  1564. #ifdef WOLF_PRIVATE_KEY_ID
  1565. keyType = falcon_level5_sa_algo;
  1566. #endif
  1567. /* Falcon is fixed key size */
  1568. keySz = FALCON_MAX_KEY_SIZE;
  1569. if (checkKeySz) {
  1570. ret = CHECK_KEY_SZ(ssl ? ssl->options.minFalconKeySz :
  1571. ctx->minFalconKeySz, FALCON_MAX_KEY_SIZE, keySz,
  1572. FALCON_KEY_SIZE_E);
  1573. }
  1574. break;
  1575. #endif /* HAVE_FALCON */
  1576. #if defined(HAVE_DILITHIUM)
  1577. case DILITHIUM_LEVEL2k:
  1578. #ifdef WOLF_PRIVATE_KEY_ID
  1579. keyType = dilithium_level2_sa_algo;
  1580. #endif
  1581. /* Dilithium is fixed key size */
  1582. keySz = DILITHIUM_MAX_KEY_SIZE;
  1583. if (checkKeySz) {
  1584. ret = CHECK_KEY_SZ(ssl ? ssl->options.minDilithiumKeySz :
  1585. ctx->minDilithiumKeySz, DILITHIUM_MAX_KEY_SIZE, keySz,
  1586. DILITHIUM_KEY_SIZE_E);
  1587. }
  1588. break;
  1589. case DILITHIUM_LEVEL3k:
  1590. #ifdef WOLF_PRIVATE_KEY_ID
  1591. keyType = dilithium_level3_sa_algo;
  1592. #endif
  1593. /* Dilithium is fixed key size */
  1594. keySz = DILITHIUM_MAX_KEY_SIZE;
  1595. if (checkKeySz) {
  1596. ret = CHECK_KEY_SZ(ssl ? ssl->options.minDilithiumKeySz :
  1597. ctx->minDilithiumKeySz, DILITHIUM_MAX_KEY_SIZE, keySz,
  1598. DILITHIUM_KEY_SIZE_E);
  1599. }
  1600. break;
  1601. case DILITHIUM_LEVEL5k:
  1602. #ifdef WOLF_PRIVATE_KEY_ID
  1603. keyType = dilithium_level5_sa_algo;
  1604. #endif
  1605. /* Dilithium is fixed key size */
  1606. keySz = DILITHIUM_MAX_KEY_SIZE;
  1607. if (checkKeySz) {
  1608. ret = CHECK_KEY_SZ(ssl ? ssl->options.minDilithiumKeySz :
  1609. ctx->minDilithiumKeySz, DILITHIUM_MAX_KEY_SIZE, keySz,
  1610. DILITHIUM_KEY_SIZE_E);
  1611. }
  1612. break;
  1613. #endif /* HAVE_DILITHIUM */
  1614. #endif /* HAVE_PQC */
  1615. default:
  1616. WOLFSSL_MSG("No key size check done on public key in certificate");
  1617. break;
  1618. }
  1619. #ifdef WOLF_PRIVATE_KEY_ID
  1620. /* Store the type and key size as there may not be a private key set. */
  1621. if (ssl != NULL) {
  1622. ssl->buffers.keyType = keyType;
  1623. ssl->buffers.keySz = keySz;
  1624. }
  1625. else {
  1626. ctx->privateKeyType = keyType;
  1627. ctx->privateKeySz = keySz;
  1628. }
  1629. #endif
  1630. return ret;
  1631. }
  1632. #ifdef WOLFSSL_DUAL_ALG_CERTS
  1633. static int ProcessBufferCertAltPublicKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  1634. DecodedCert* cert, int checkKeySz)
  1635. {
  1636. int ret = 0;
  1637. byte keyType = 0;
  1638. int keySz = 0;
  1639. #ifndef NO_RSA
  1640. word32 idx;
  1641. #endif
  1642. /* Check alternative key size of cert. */
  1643. switch (cert->sapkiOID) {
  1644. /* No OID set. */
  1645. case 0:
  1646. if (cert->sapkiLen != 0) {
  1647. /* Have the alternative key data but no OID. */
  1648. ret = NOT_COMPILED_IN;
  1649. }
  1650. break;
  1651. #ifndef NO_RSA
  1652. #ifdef WC_RSA_PSS
  1653. case RSAPSSk:
  1654. #endif
  1655. case RSAk:
  1656. keyType = rsa_sa_algo;
  1657. /* Determine RSA key size by parsing public key */
  1658. idx = 0;
  1659. ret = wc_RsaPublicKeyDecode_ex(cert->sapkiDer, &idx,
  1660. cert->sapkiLen, NULL, (word32*)&keySz, NULL, NULL);
  1661. if ((ret == 0) && checkKeySz) {
  1662. ret = CHECK_KEY_SZ(ssl ? ssl->options.minRsaKeySz :
  1663. ctx->minRsaKeySz, RSA_MAX_SIZE / 8, keySz, RSA_KEY_SIZE_E);
  1664. }
  1665. break;
  1666. #endif /* !NO_RSA */
  1667. #ifdef HAVE_ECC
  1668. case ECDSAk:
  1669. {
  1670. #ifdef WOLFSSL_SMALL_STACK
  1671. ecc_key* temp_key = NULL;
  1672. #else
  1673. ecc_key temp_key[1];
  1674. #endif
  1675. keyType = ecc_dsa_sa_algo;
  1676. #ifdef WOLFSSL_SMALL_STACK
  1677. temp_key = (ecc_key*)XMALLOC(sizeof(ecc_key), heap,
  1678. DYNAMIC_TYPE_ECC);
  1679. if (temp_key == NULL) {
  1680. ret = MEMORY_E;
  1681. }
  1682. #endif
  1683. /* Determine ECC key size. We have to decode the sapki for
  1684. * that. */
  1685. if (ret == 0) {
  1686. ret = wc_ecc_init_ex(temp_key, heap, INVALID_DEVID);
  1687. if (ret == 0) {
  1688. idx = 0;
  1689. ret = wc_EccPublicKeyDecode(cert->sapkiDer, &idx, temp_key,
  1690. cert->sapkiLen);
  1691. if (ret == 0) {
  1692. keySz = wc_ecc_size(temp_key);
  1693. }
  1694. wc_ecc_free(temp_key);
  1695. }
  1696. }
  1697. #ifdef WOLFSSL_SMALL_STACK
  1698. XFREE(temp_key, heap, DYNAMIC_TYPE_ECC);
  1699. #endif
  1700. if ((ret == 0) && checkKeySz) {
  1701. ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz :
  1702. ctx->minEccKeySz, (MAX_ECC_BITS + 7) / 8, keySz,
  1703. ECC_KEY_SIZE_E);
  1704. }
  1705. break;
  1706. }
  1707. #endif /* HAVE_ECC */
  1708. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SM3)
  1709. case SM2k:
  1710. keyType = sm2_sa_algo;
  1711. /* Determine ECC key size based on curve */
  1712. keySz = WOLFSSL_SM2_KEY_BITS / 8;
  1713. if (checkKeySz) {
  1714. ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz :
  1715. ctx->minEccKeySz, (MAX_ECC_BITS + 7) / 8, keySz,
  1716. ECC_KEY_SIZE_E);
  1717. }
  1718. break;
  1719. #endif /* HAVE_ED25519 */
  1720. #ifdef HAVE_ED25519
  1721. case ED25519k:
  1722. keyType = ed25519_sa_algo;
  1723. /* ED25519 is fixed key size */
  1724. keySz = ED25519_KEY_SIZE;
  1725. if (checkKeySz) {
  1726. ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz :
  1727. ctx->minEccKeySz, ED25519_KEY_SIZE, keySz, ECC_KEY_SIZE_E);
  1728. }
  1729. break;
  1730. #endif /* HAVE_ED25519 */
  1731. #ifdef HAVE_ED448
  1732. case ED448k:
  1733. keyType = ed448_sa_algo;
  1734. /* ED448 is fixed key size */
  1735. keySz = ED448_KEY_SIZE;
  1736. if (checkKeySz) {
  1737. ret = CHECK_KEY_SZ(ssl ? ssl->options.minEccKeySz :
  1738. ctx->minEccKeySz, ED448_KEY_SIZE, keySz, ECC_KEY_SIZE_E);
  1739. }
  1740. break;
  1741. #endif /* HAVE_ED448 */
  1742. #if defined(HAVE_PQC)
  1743. #if defined(HAVE_FALCON)
  1744. case FALCON_LEVEL1k:
  1745. keyType = falcon_level1_sa_algo;
  1746. /* Falcon is fixed key size */
  1747. keySz = FALCON_LEVEL1_KEY_SIZE;
  1748. if (checkKeySz) {
  1749. ret = CHECK_KEY_SZ(ssl ? ssl->options.minFalconKeySz :
  1750. ctx->minFalconKeySz, FALCON_MAX_KEY_SIZE, keySz,
  1751. FALCON_KEY_SIZE_E);
  1752. }
  1753. break;
  1754. case FALCON_LEVEL5k:
  1755. keyType = falcon_level5_sa_algo;
  1756. /* Falcon is fixed key size */
  1757. keySz = FALCON_LEVEL5_KEY_SIZE;
  1758. if (checkKeySz) {
  1759. ret = CHECK_KEY_SZ(ssl ? ssl->options.minFalconKeySz :
  1760. ctx->minFalconKeySz, FALCON_MAX_KEY_SIZE, keySz,
  1761. FALCON_KEY_SIZE_E);
  1762. }
  1763. break;
  1764. #endif /* HAVE_FALCON */
  1765. #if defined(HAVE_DILITHIUM)
  1766. case DILITHIUM_LEVEL2k:
  1767. keyType = dilithium_level2_sa_algo;
  1768. /* Dilithium is fixed key size */
  1769. keySz = DILITHIUM_LEVEL2_KEY_SIZE;
  1770. if (checkKeySz) {
  1771. ret = CHECK_KEY_SZ(ssl ? ssl->options.minDilithiumKeySz :
  1772. ctx->minDilithiumKeySz, DILITHIUM_MAX_KEY_SIZE, keySz,
  1773. DILITHIUM_KEY_SIZE_E);
  1774. }
  1775. break;
  1776. case DILITHIUM_LEVEL3k:
  1777. keyType = dilithium_level3_sa_algo;
  1778. /* Dilithium is fixed key size */
  1779. keySz = DILITHIUM_LEVEL3_KEY_SIZE;
  1780. if (checkKeySz) {
  1781. ret = CHECK_KEY_SZ(ssl ? ssl->options.minDilithiumKeySz :
  1782. ctx->minDilithiumKeySz, DILITHIUM_MAX_KEY_SIZE, keySz,
  1783. DILITHIUM_KEY_SIZE_E);
  1784. }
  1785. break;
  1786. case DILITHIUM_LEVEL5k:
  1787. keyType = dilithium_level5_sa_algo;
  1788. /* Dilithium is fixed key size */
  1789. keySz = DILITHIUM_LEVEL5_KEY_SIZE;
  1790. if (checkKeySz) {
  1791. ret = CHECK_KEY_SZ(ssl ? ssl->options.minDilithiumKeySz :
  1792. ctx->minDilithiumKeySz, DILITHIUM_MAX_KEY_SIZE, keySz,
  1793. DILITHIUM_KEY_SIZE_E);
  1794. }
  1795. break;
  1796. #endif /* HAVE_DILITHIUM */
  1797. #endif /* HAVE_PQC */
  1798. default:
  1799. /* In this case, there was an OID that we didn't recognize.
  1800. * This is an error. Use not compiled in because likely the
  1801. * given algorithm was not enabled. */
  1802. ret = NOT_COMPILED_IN;
  1803. WOLFSSL_MSG("No alt key size check done on certificate");
  1804. break;
  1805. }
  1806. if (ssl != NULL) {
  1807. ssl->buffers.altKeyType = (byte)keyType;
  1808. ssl->buffers.altKeySz = keySz;
  1809. }
  1810. else if (ctx != NULL) {
  1811. ctx->altPrivateKeyType = (byte)keyType;
  1812. ctx->altPrivateKeySz = keySz;
  1813. }
  1814. return ret;
  1815. }
  1816. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  1817. /* Parse the certificate and pull out information for TLS handshake.
  1818. *
  1819. * @param [in, out] ctx SSL context object.
  1820. * @param [in, out] ssl SSL object.
  1821. * @param [in] der DER encoded X509 certificate.
  1822. * @return 0 on success.
  1823. * @return MEMORY_E when dynamic memory allocation fails.
  1824. * @return WOLFSSL_BAD_FILE when decoding certificate fails.
  1825. */
  1826. static int ProcessBufferCert(WOLFSSL_CTX* ctx, WOLFSSL* ssl, DerBuffer* der)
  1827. {
  1828. int ret = 0;
  1829. void* heap = WOLFSSL_HEAP(ctx, ssl);
  1830. #if defined(HAVE_RPK)
  1831. RpkState* rpkState = ssl ? &ssl->options.rpkState : &ctx->rpkState;
  1832. #endif
  1833. #ifdef WOLFSSL_SMALL_STACK
  1834. DecodedCert* cert;
  1835. #else
  1836. DecodedCert cert[1];
  1837. #endif
  1838. #ifdef WOLFSSL_SMALL_STACK
  1839. /* Allocate memory for certificate to be decoded into. */
  1840. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), heap, DYNAMIC_TYPE_DCERT);
  1841. if (cert == NULL) {
  1842. ret = MEMORY_E;
  1843. }
  1844. if (ret == 0)
  1845. #endif
  1846. {
  1847. /* Get device id from SSL context or SSL object. */
  1848. int devId = wolfSSL_CTX_GetDevId(ctx, ssl);
  1849. WOLFSSL_MSG("Checking cert signature type");
  1850. /* Initialize certificate object. */
  1851. InitDecodedCert_ex(cert, der->buffer, der->length, heap, devId);
  1852. /* Decode up to and including public key. */
  1853. if (DecodeToKey(cert, 0) < 0) {
  1854. WOLFSSL_MSG("Decode to key failed");
  1855. ret = WOLFSSL_BAD_FILE;
  1856. }
  1857. if (ret == 0) {
  1858. int checkKeySz = 1;
  1859. #if defined(HAVE_RPK)
  1860. /* Store whether the crtificate is a raw public key. */
  1861. rpkState->isRPKLoaded = cert->isRPK;
  1862. #endif /* HAVE_RPK */
  1863. /* Set which private key algorithm we have. */
  1864. ProcessBufferCertSetHave(ctx, ssl, cert);
  1865. /* Don't check if verification is disabled for SSL. */
  1866. if ((ssl != NULL) && ssl->options.verifyNone) {
  1867. checkKeySz = 0;
  1868. }
  1869. /* Don't check if no SSL object verification is disabled for SSL
  1870. * context. */
  1871. else if ((ssl == NULL) && ctx->verifyNone) {
  1872. checkKeySz = 0;
  1873. }
  1874. /* Check public key size. */
  1875. ret = ProcessBufferCertPublicKey(ctx, ssl, cert, checkKeySz);
  1876. #ifdef WOLFSSL_DUAL_ALG_CERTS
  1877. if (ret == 0) {
  1878. ret = ProcessBufferCertAltPublicKey(ctx, ssl, cert, checkKeySz);
  1879. }
  1880. #endif
  1881. }
  1882. }
  1883. /* Dispose of dynamic memory in certificate object. */
  1884. FreeDecodedCert(cert);
  1885. #ifdef WOLFSSL_SMALL_STACK
  1886. /* Dispose of certificate object. */
  1887. XFREE(cert, heap, DYNAMIC_TYPE_DCERT);
  1888. #endif
  1889. return ret;
  1890. }
  1891. /* Handle storing the DER encoding of the certificate.
  1892. *
  1893. * Do not free der outside of this function.
  1894. *
  1895. * @param [in, out] ctx SSL context object.
  1896. * @param [in, out] ssl SSL object.
  1897. * @param [in] der DER encoded certificate.
  1898. * @param [in] type Type of data:
  1899. * CERT_TYPE, CA_TYPE or TRUSTED_PEER_TYPE.
  1900. * @param [in] verify What verification to do.
  1901. * @return 0 on success.
  1902. * @return BAD_FUNC_ARG when type is CA_TYPE and ctx is NULL.
  1903. * @return WOLFSSL_BAD_CERTTYPE when data type is not supported.
  1904. */
  1905. static int ProcessBufferCertHandleDer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  1906. DerBuffer* der, int type, int verify)
  1907. {
  1908. int ret = 0;
  1909. /* CA certificate to verify with. */
  1910. if (type == CA_TYPE) {
  1911. /* verify CA unless user set to no verify */
  1912. ret = AddCA(ctx->cm, &der, WOLFSSL_USER_CA, verify);
  1913. if (ret == 1) {
  1914. ret = 0;
  1915. }
  1916. }
  1917. #ifdef WOLFSSL_TRUST_PEER_CERT
  1918. /* Trusted certificate to verify peer with. */
  1919. else if (type == TRUSTED_PEER_TYPE) {
  1920. WOLFSSL_CERT_MANAGER* cm;
  1921. /* Get certificate manager to add certificate to. */
  1922. if (ctx != NULL) {
  1923. cm = ctx->cm;
  1924. }
  1925. else {
  1926. SSL_CM_WARNING(ssl);
  1927. cm = SSL_CM(ssl);
  1928. }
  1929. /* Add certificate as a trusted peer. */
  1930. ret = AddTrustedPeer(cm, &der, verify);
  1931. if (ret != 1) {
  1932. WOLFSSL_MSG("Error adding trusted peer");
  1933. }
  1934. }
  1935. #endif /* WOLFSSL_TRUST_PEER_CERT */
  1936. /* Leaf certificate - our certificate. */
  1937. else if (type == CERT_TYPE) {
  1938. if (ssl != NULL) {
  1939. /* Free previous certificate if we own it. */
  1940. if (ssl->buffers.weOwnCert) {
  1941. FreeDer(&ssl->buffers.certificate);
  1942. #ifdef KEEP_OUR_CERT
  1943. /* Dispose of X509 version of certificate. */
  1944. wolfSSL_X509_free(ssl->ourCert);
  1945. ssl->ourCert = NULL;
  1946. #endif
  1947. }
  1948. /* Store certificate as ours. */
  1949. ssl->buffers.certificate = der;
  1950. #ifdef KEEP_OUR_CERT
  1951. ssl->keepCert = 1; /* hold cert for ssl lifetime */
  1952. #endif
  1953. /* We have to free the certificate buffer. */
  1954. ssl->buffers.weOwnCert = 1;
  1955. /* ourCert is created on demand. */
  1956. }
  1957. else if (ctx != NULL) {
  1958. /* Free previous certificate. */
  1959. FreeDer(&ctx->certificate); /* Make sure previous is free'd */
  1960. #ifdef KEEP_OUR_CERT
  1961. /* Dispose of X509 version of certificate if we own it. */
  1962. if (ctx->ownOurCert) {
  1963. wolfSSL_X509_free(ctx->ourCert);
  1964. }
  1965. ctx->ourCert = NULL;
  1966. #endif
  1967. /* Store certificate as ours. */
  1968. ctx->certificate = der;
  1969. /* ourCert is created on demand. */
  1970. }
  1971. }
  1972. else {
  1973. /* Dispose of DER buffer. */
  1974. FreeDer(&der);
  1975. /* Not a certificate type supported. */
  1976. ret = WOLFSSL_BAD_CERTTYPE;
  1977. }
  1978. return ret;
  1979. }
  1980. /* Process certificate based on type.
  1981. *
  1982. * @param [in, out] ctx SSL context object.
  1983. * @param [in, out] ssl SSL object.
  1984. * @param [in] buff Buffer holding original data.
  1985. * @param [in] sz Size of data in buffer.
  1986. * @param [in] der DER encoding of certificate.
  1987. * @param [in] format Format of data.
  1988. * @param [in] type Type of data:
  1989. * CERT_TYPE, CA_TYPE or TRUSTED_PEER_TYPE.
  1990. * @param [in] verify What verification to do.
  1991. * @return 0 on success.
  1992. * @return WOLFSSL_FATAL_ERROR on failure.
  1993. */
  1994. static int ProcessBufferCertTypes(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  1995. const unsigned char* buff, long sz, DerBuffer* der, int format, int type,
  1996. int verify)
  1997. {
  1998. int ret;
  1999. (void)buff;
  2000. (void)sz;
  2001. (void)format;
  2002. ret = ProcessBufferCertHandleDer(ctx, ssl, der, type, verify);
  2003. if ((ret == 0) && (type == CERT_TYPE)) {
  2004. /* Process leaf certificate. */
  2005. ret = ProcessBufferCert(ctx, ssl, der);
  2006. }
  2007. #if !defined(NO_WOLFSSL_CM_VERIFY) && (!defined(NO_WOLFSSL_CLIENT) || \
  2008. !defined(WOLFSSL_NO_CLIENT_AUTH))
  2009. /* Hand bad CA or user certificate to callback. */
  2010. if ((ret < 0) && ((type == CA_TYPE) || (type == CERT_TYPE))) {
  2011. /* Check for verification callback that may override error. */
  2012. if ((ctx != NULL) && (ctx->cm != NULL) &&
  2013. (ctx->cm->verifyCallback != NULL)) {
  2014. /* Verify and use callback. */
  2015. ret = CM_VerifyBuffer_ex(ctx->cm, buff, sz, format, ret);
  2016. /* Convert error. */
  2017. if (ret == 0) {
  2018. ret = WOLFSSL_FATAL_ERROR;
  2019. }
  2020. if (ret == 1) {
  2021. ret = 0;
  2022. }
  2023. }
  2024. }
  2025. #endif /* NO_WOLFSSL_CM_VERIFY */
  2026. return ret;
  2027. }
  2028. /* Reset the cipher suites based on updated private key or certificate.
  2029. *
  2030. * @param [in, out] ctx SSL context object.
  2031. * @param [in, out] ssl SSL object.
  2032. * @param [in] type Type of certificate.
  2033. * @return 0 on success.
  2034. * @return WOLFSSL_FATAL_ERROR when allocation fails.
  2035. */
  2036. static int ProcessBufferResetSuites(WOLFSSL_CTX* ctx, WOLFSSL* ssl, int type)
  2037. {
  2038. int ret = 0;
  2039. /* Reset suites of SSL object. */
  2040. if (ssl != NULL) {
  2041. if (ssl->options.side == WOLFSSL_SERVER_END) {
  2042. /* Allocate memory for suites. */
  2043. if (AllocateSuites(ssl) != 0) {
  2044. ret = WOLFSSL_FATAL_ERROR;
  2045. }
  2046. else {
  2047. /* Determine cipher suites based on what we have. */
  2048. InitSuites(ssl->suites, ssl->version, ssl->buffers.keySz,
  2049. WOLFSSL_HAVE_RSA, SSL_HAVE_PSK(ssl), ssl->options.haveDH,
  2050. ssl->options.haveECDSAsig, ssl->options.haveECC, TRUE,
  2051. ssl->options.haveStaticECC, ssl->options.haveFalconSig,
  2052. ssl->options.haveDilithiumSig, ssl->options.useAnon, TRUE,
  2053. ssl->options.side);
  2054. }
  2055. }
  2056. }
  2057. /* Reset suites of SSL context object. */
  2058. else if ((type == CERT_TYPE) && (ctx->method->side == WOLFSSL_SERVER_END)) {
  2059. /* Allocate memory for suites. */
  2060. if (AllocateCtxSuites(ctx) != 0) {
  2061. ret = WOLFSSL_FATAL_ERROR;
  2062. }
  2063. else {
  2064. /* Determine cipher suites based on what we have. */
  2065. InitSuites(ctx->suites, ctx->method->version, ctx->privateKeySz,
  2066. WOLFSSL_HAVE_RSA, CTX_HAVE_PSK(ctx), ctx->haveDH,
  2067. ctx->haveECDSAsig, ctx->haveECC, TRUE, ctx->haveStaticECC,
  2068. ctx->haveFalconSig, ctx->haveDilithiumSig, CTX_USE_ANON(ctx),
  2069. TRUE, ctx->method->side);
  2070. }
  2071. }
  2072. return ret;
  2073. }
  2074. #ifndef WOLFSSL_DUAL_ALG_CERTS
  2075. /* Determine whether the type is for a private key. */
  2076. #define IS_PRIVKEY_TYPE(type) ((type) == PRIVATEKEY_TYPE)
  2077. #else
  2078. /* Determine whether the type is for a private key. */
  2079. #define IS_PRIVKEY_TYPE(type) (((type) == PRIVATEKEY_TYPE) || \
  2080. ((type) == ALT_PRIVATEKEY_TYPE))
  2081. #endif
  2082. /* Process a buffer of data.
  2083. *
  2084. * Data type is a private key or a certificate.
  2085. * The format can be ASN.1 (DER) or PEM.
  2086. *
  2087. * @param [in, out] ctx SSL context object.
  2088. * @param [in] buff Buffer holding data.
  2089. * @param [in] sz Size of data in buffer.
  2090. * @param [in] format Format of data:
  2091. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  2092. * @param [in] type Type of data:
  2093. * CERT_TYPE, CA_TYPE, TRUSTED_PEER_TYPE,
  2094. * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
  2095. * @param [in, out] ssl SSL object.
  2096. * @param [out] used Number of bytes consumed.
  2097. * @param [in[ userChain Whether this certificate is for user's chain.
  2098. * @param [in] verify How to verify certificate.
  2099. * @return 1 on success.
  2100. * @return Less than 1 on failure.
  2101. */
  2102. int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
  2103. int format, int type, WOLFSSL* ssl, long* used, int userChain, int verify)
  2104. {
  2105. DerBuffer* der = NULL;
  2106. int ret = 0;
  2107. void* heap = WOLFSSL_HEAP(ctx, ssl);
  2108. #ifdef WOLFSSL_SMALL_STACK
  2109. EncryptedInfo* info = NULL;
  2110. #else
  2111. EncryptedInfo info[1];
  2112. #endif
  2113. WOLFSSL_ENTER("ProcessBuffer");
  2114. /* Check data format is supported. */
  2115. if ((format != WOLFSSL_FILETYPE_ASN1) && (format != WOLFSSL_FILETYPE_PEM)) {
  2116. ret = WOLFSSL_BAD_FILETYPE;
  2117. }
  2118. /* Need an object to store certificate into. */
  2119. if ((ret == 0) && (ctx == NULL) && (ssl == NULL)) {
  2120. ret = BAD_FUNC_ARG;
  2121. }
  2122. /* CA certificates go into the SSL context object. */
  2123. if ((ret == 0) && (ctx == NULL) && (type == CA_TYPE)) {
  2124. ret = BAD_FUNC_ARG;
  2125. }
  2126. /* This API does not handle CHAIN_CERT_TYPE */
  2127. if ((ret == 0) && (type == CHAIN_CERT_TYPE)) {
  2128. ret = BAD_FUNC_ARG;
  2129. }
  2130. #ifdef WOLFSSL_SMALL_STACK
  2131. if (ret == 0) {
  2132. /* Allocate memory for encryption information. */
  2133. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), heap,
  2134. DYNAMIC_TYPE_ENCRYPTEDINFO);
  2135. if (info == NULL) {
  2136. ret = MEMORY_E;
  2137. }
  2138. }
  2139. #endif
  2140. if (ret == 0) {
  2141. /* Initialize encryption information. */
  2142. XMEMSET(info, 0, sizeof(EncryptedInfo));
  2143. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
  2144. if (ctx != NULL) {
  2145. info->passwd_cb = ctx->passwd_cb;
  2146. info->passwd_userdata = ctx->passwd_userdata;
  2147. }
  2148. #endif
  2149. /* Get the DER data for a private key or certificate. */
  2150. ret = DataToDerBuffer(buff, (word32)sz, format, type, info, heap, &der);
  2151. if (used != NULL) {
  2152. /* Update to amount used/consumed. */
  2153. *used = info->consumed;
  2154. }
  2155. #ifdef WOLFSSL_SMALL_STACK
  2156. if (ret != 0) {
  2157. /* Info no longer needed as loading failed. */
  2158. XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);
  2159. }
  2160. #endif
  2161. }
  2162. if ((ret == 0) && IS_PRIVKEY_TYPE(type)) {
  2163. /* Process the private key. */
  2164. ret = ProcessBufferPrivateKey(ctx, ssl, der, format, info, heap, type);
  2165. #ifdef WOLFSSL_SMALL_STACK
  2166. /* Info no longer needed - keep max memory usage down. */
  2167. XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);
  2168. #endif
  2169. }
  2170. else if (ret == 0) {
  2171. /* Processing a cerificate. */
  2172. if (userChain) {
  2173. /* Take original buffer and add to user chain to send in TLS
  2174. * handshake. */
  2175. ret = ProcessUserChain(ctx, ssl, buff, sz, format, type, used, info,
  2176. verify);
  2177. /* Additional chain is optional */
  2178. if (ret == ASN_NO_PEM_HEADER) {
  2179. unsigned long pemErr = 0;
  2180. CLEAR_ASN_NO_PEM_HEADER_ERROR(pemErr);
  2181. ret = 0;
  2182. }
  2183. }
  2184. #ifdef WOLFSSL_SMALL_STACK
  2185. /* Info no longer needed - keep max memory usage down. */
  2186. XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);
  2187. #endif
  2188. if (ret == 0) {
  2189. /* Process the different types of certificates. */
  2190. ret = ProcessBufferCertTypes(ctx, ssl, buff, sz, der, format, type,
  2191. verify);
  2192. }
  2193. }
  2194. /* Reset suites if this is a private key or user certificate. */
  2195. if ((ret == 0) && ((type == PRIVATEKEY_TYPE) || (type == CERT_TYPE))) {
  2196. ret = ProcessBufferResetSuites(ctx, ssl, type);
  2197. }
  2198. /* Convert return code. */
  2199. if (ret == 0) {
  2200. ret = 1;
  2201. }
  2202. else if (ret == WOLFSSL_FATAL_ERROR) {
  2203. ret = 0;
  2204. }
  2205. WOLFSSL_LEAVE("ProcessBuffer", ret);
  2206. return ret;
  2207. }
  2208. #if defined(WOLFSSL_WPAS) && defined(HAVE_CRL)
  2209. /* Try to parse data as a PEM CRL.
  2210. *
  2211. * @param [in] ctx SSL context object.
  2212. * @param [in] buff Buffer containing potential CRL in PEM format.
  2213. * @param [in] sz Amount of data in buffer remaining.
  2214. * @param [out] consumed Number of bytes in buffer was the CRL.
  2215. * @return 0 on success.
  2216. */
  2217. static int ProcessChainBufferCRL(WOLFSSL_CTX* ctx, const unsigned char* buff,
  2218. long sz, long* consumed)
  2219. {
  2220. int ret;
  2221. DerBuffer* der = NULL;
  2222. EncryptedInfo info;
  2223. WOLFSSL_MSG("Trying a CRL");
  2224. ret = PemToDer(buff, sz, CRL_TYPE, &der, NULL, &info, NULL);
  2225. if (ret == 0) {
  2226. WOLFSSL_MSG(" Processed a CRL");
  2227. wolfSSL_CertManagerLoadCRLBuffer(ctx->cm, der->buffer, der->length,
  2228. WOLFSSL_FILETYPE_ASN1);
  2229. FreeDer(&der);
  2230. *consumed = info.consumed;
  2231. }
  2232. return ret;
  2233. }
  2234. #endif
  2235. /* Process all chain certificates (and CRLs) in the PEM data.
  2236. *
  2237. * @param [in, out] ctx SSL context object.
  2238. * @param [in, out] ssl SSL object.
  2239. * @param [in] buff Buffer containing PEM data.
  2240. * @param [in] sz Size of data in buffer.
  2241. * @param [in] type Type of data.
  2242. * @param [in] verify How to verify certificate.
  2243. * @return 1 on success.
  2244. * @return 0 on failure.
  2245. * @return MEMORY_E when dynamic memory allocation fails.
  2246. */
  2247. static int ProcessChainBuffer(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  2248. const unsigned char* buff, long sz, int type, int verify)
  2249. {
  2250. int ret = 0;
  2251. long used = 0;
  2252. int gotOne = 0;
  2253. WOLFSSL_MSG("Processing CA PEM file");
  2254. /* Keep processing file while no errors and data to parse. */
  2255. while ((ret >= 0) && (used < sz)) {
  2256. long consumed = 0;
  2257. /* Process the buffer. */
  2258. ret = ProcessBuffer(ctx, buff + used, sz - used, WOLFSSL_FILETYPE_PEM,
  2259. type, ssl, &consumed, 0, verify);
  2260. /* Memory allocation failure is fatal. */
  2261. if (ret == MEMORY_E) {
  2262. gotOne = 0;
  2263. }
  2264. /* Other error parsing. */
  2265. else if (ret < 0) {
  2266. #if defined(WOLFSSL_WPAS) && defined(HAVE_CRL)
  2267. /* Try parsing a CRL. */
  2268. if (ProcessChainBufferCRL(ctx, buff + used, sz - used,
  2269. &consumed) == 0) {
  2270. ret = 0;
  2271. }
  2272. else
  2273. #endif
  2274. /* Check whether we made progress. */
  2275. if (consumed > 0) {
  2276. WOLFSSL_ERROR(ret);
  2277. WOLFSSL_MSG("CA Parse failed, with progress in file.");
  2278. WOLFSSL_MSG("Search for other certs in file");
  2279. /* Check if we have more data to parse to recover. */
  2280. if (used + consumed < sz) {
  2281. ret = 0;
  2282. }
  2283. }
  2284. else {
  2285. /* No progress in parsing being made - stop here. */
  2286. WOLFSSL_MSG("CA Parse failed, no progress in file.");
  2287. WOLFSSL_MSG("Do not continue search for other certs in file");
  2288. }
  2289. }
  2290. else {
  2291. /* Got a certificate out. */
  2292. WOLFSSL_MSG(" Processed a CA");
  2293. gotOne = 1;
  2294. }
  2295. /* Update used count. */
  2296. used += consumed;
  2297. }
  2298. /* May have other unparsable data but did we get a certificate? */
  2299. if (gotOne) {
  2300. WOLFSSL_MSG("Processed at least one valid CA. Other stuff OK");
  2301. ret = 1;
  2302. }
  2303. return ret;
  2304. }
  2305. /* Get verify settings for AddCA from SSL context. */
  2306. #define GET_VERIFY_SETTING_CTX(ctx) \
  2307. ((ctx) && (ctx)->verifyNone ? NO_VERIFY : VERIFY)
  2308. /* Get verify settings for AddCA from SSL. */
  2309. #define GET_VERIFY_SETTING_SSL(ssl) \
  2310. ((ssl)->options.verifyNone ? NO_VERIFY : VERIFY)
  2311. #ifndef NO_FILESYSTEM
  2312. /* Process data from a file as private keys, CRL or certificates.
  2313. *
  2314. * @param [in, out] ctx SSL context object.
  2315. * @param [in] fname Name of file to read.
  2316. * @param [in] format Format of data:
  2317. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  2318. * @param [in] type Type of data:
  2319. * CERT_TYPE, CA_TYPE, TRUSTED_PEER_TYPE,
  2320. * PRIVATEKEY_TYPE or ALT_PRIVATEKEY_TYPE.
  2321. * @param [in, out] ssl SSL object.
  2322. * @param [in] userChain Whether file contains chain of certificates.
  2323. * @param [in, out] crl CRL object to load data into.
  2324. * @param [in] verify How to verify certificates.
  2325. * @return 1 on success.
  2326. * @return WOLFSSL_BAD_FILE when reading the file fails.
  2327. * @return WOLFSSL_BAD_CERTTYPE when unable to detect certificate type.
  2328. */
  2329. int ProcessFile(WOLFSSL_CTX* ctx, const char* fname, int format, int type,
  2330. WOLFSSL* ssl, int userChain, WOLFSSL_CRL* crl, int verify)
  2331. {
  2332. int ret = 0;
  2333. #ifndef WOLFSSL_SMALL_STACK
  2334. byte stackBuffer[FILE_BUFFER_SIZE];
  2335. #endif
  2336. StaticBuffer content;
  2337. long sz = 0;
  2338. void* heap = WOLFSSL_HEAP(ctx, ssl);
  2339. (void)crl;
  2340. (void)heap;
  2341. #ifdef WOLFSSL_SMALL_STACK
  2342. static_buffer_init(&content);
  2343. #else
  2344. static_buffer_init(&content, stackBuffer, FILE_BUFFER_SIZE);
  2345. #endif
  2346. /* Read file into static buffer. */
  2347. ret = wolfssl_read_file_static(fname, &content, heap, DYNAMIC_TYPE_FILE,
  2348. &sz);
  2349. if ((ret == 0) && (type == DETECT_CERT_TYPE) &&
  2350. (format != WOLFSSL_FILETYPE_PEM)) {
  2351. WOLFSSL_MSG("Cannot detect certificate type when not PEM");
  2352. ret = WOLFSSL_BAD_CERTTYPE;
  2353. }
  2354. /* Try to detect type by parsing cert header and footer. */
  2355. if ((ret == 0) && (type == DETECT_CERT_TYPE)) {
  2356. #ifndef NO_CODING
  2357. const char* header = NULL;
  2358. const char* footer = NULL;
  2359. /* Look for CA header and footer - same as CERT_TYPE. */
  2360. if (wc_PemGetHeaderFooter(CA_TYPE, &header, &footer) == 0 &&
  2361. (XSTRNSTR((char*)content.buffer, header, (word32)sz) != NULL)) {
  2362. type = CA_TYPE;
  2363. }
  2364. #ifdef HAVE_CRL
  2365. /* Look for CRL header and footer. */
  2366. else if (wc_PemGetHeaderFooter(CRL_TYPE, &header, &footer) == 0 &&
  2367. (XSTRNSTR((char*)content.buffer, header, (word32)sz) != NULL)) {
  2368. type = CRL_TYPE;
  2369. }
  2370. #endif
  2371. /* Look for cert header and footer - same as CA_TYPE. */
  2372. else if (wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer) == 0 &&
  2373. (XSTRNSTR((char*)content.buffer, header, (word32)sz) !=
  2374. NULL)) {
  2375. type = CERT_TYPE;
  2376. }
  2377. else
  2378. #endif
  2379. {
  2380. /* Not a header that we support. */
  2381. WOLFSSL_MSG("Failed to detect certificate type");
  2382. ret = WOLFSSL_BAD_CERTTYPE;
  2383. }
  2384. }
  2385. if (ret == 0) {
  2386. /* When CA or trusted peer and PEM - process as a chain buffer. */
  2387. if (((type == CA_TYPE) || (type == TRUSTED_PEER_TYPE)) &&
  2388. (format == WOLFSSL_FILETYPE_PEM)) {
  2389. ret = ProcessChainBuffer(ctx, ssl, content.buffer, sz, type,
  2390. verify);
  2391. }
  2392. #ifdef HAVE_CRL
  2393. else if (type == CRL_TYPE) {
  2394. /* Load the CRL. */
  2395. ret = BufferLoadCRL(crl, content.buffer, sz, format, verify);
  2396. }
  2397. #endif
  2398. #ifdef WOLFSSL_DUAL_ALG_CERTS
  2399. else if (type == PRIVATEKEY_TYPE) {
  2400. /* Load all other certificate types. */
  2401. ret = ProcessBuffer(ctx, content.buffer, sz, format, type, ssl,
  2402. NULL, userChain, verify);
  2403. if ((ret == 1) && (consumed < sz)) {
  2404. ret = ProcessBuffer(ctx, content.buffer + consumed,
  2405. sz - consumed, format, ALT_PRIVATEKEY_TYPE, ssl, NULL, 0,
  2406. verify);
  2407. }
  2408. }
  2409. #endif
  2410. else {
  2411. /* Load all other certificate types. */
  2412. ret = ProcessBuffer(ctx, content.buffer, sz, format, type, ssl,
  2413. NULL, userChain, verify);
  2414. }
  2415. }
  2416. /* Dispose of dynamically allocated data. */
  2417. static_buffer_free(&content, heap, DYNAMIC_TYPE_FILE);
  2418. return ret;
  2419. }
  2420. #ifndef NO_WOLFSSL_DIR
  2421. /* Load file when filename is in the path.
  2422. *
  2423. * @param [in, out] ctx SSL context object.
  2424. * @param [in] name Name of file.
  2425. * @param [in] verify How to verify a certificate.
  2426. * @param [in] flags Flags representing options for loading.
  2427. * @param [in, out] failCount Number of files that failed to load.
  2428. * @param [in, out] successCount Number of files successfully loaded.
  2429. * @return 1 on success.
  2430. * @return Not 1 when loading PEM certificate failed.
  2431. */
  2432. static int wolfssl_ctx_load_path_file(WOLFSSL_CTX* ctx, const char* name,
  2433. int verify, int flags, int* failCount, int* successCount)
  2434. {
  2435. int ret;
  2436. /* Attempt to load file as a CA. */
  2437. ret = ProcessFile(ctx, name, WOLFSSL_FILETYPE_PEM, CA_TYPE, NULL, 0, NULL,
  2438. verify);
  2439. if (ret != 1) {
  2440. /* When ignoring errors or loading PEM only and no PEM. don't fail. */
  2441. if ((flags & WOLFSSL_LOAD_FLAG_IGNORE_ERR) ||
  2442. ((flags & WOLFSSL_LOAD_FLAG_PEM_CA_ONLY) &&
  2443. (ret == ASN_NO_PEM_HEADER))) {
  2444. unsigned long err = 0;
  2445. CLEAR_ASN_NO_PEM_HEADER_ERROR(err);
  2446. #if defined(WOLFSSL_QT)
  2447. ret = 1;
  2448. #endif
  2449. }
  2450. else {
  2451. WOLFSSL_ERROR(ret);
  2452. WOLFSSL_MSG("Load CA file failed, continuing");
  2453. /* Add to fail count. */
  2454. (*failCount)++;
  2455. }
  2456. }
  2457. else {
  2458. #if defined(WOLFSSL_TRUST_PEER_CERT) && defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2459. /* Try loading as a trusted peer certificate. */
  2460. ret = wolfSSL_CTX_trust_peer_cert(ctx, name, WOLFSSL_FILETYPE_PEM);
  2461. if (ret != 1) {
  2462. WOLFSSL_MSG("wolfSSL_CTX_trust_peer_cert error. "
  2463. "Ignoring this error.");
  2464. }
  2465. #endif
  2466. /* Add to success count. */
  2467. (*successCount)++;
  2468. }
  2469. return ret;
  2470. }
  2471. /* Load PEM formatted CA files from a path.
  2472. *
  2473. * @param [in, out] ctx SSL context object.
  2474. * @param [in] path Path to directory to read.
  2475. * @param [in] flags Flags representing options for loading.
  2476. * @param [in] verify How to verify a certificate.
  2477. * @param [in] successCount Number of files successfully loaded.
  2478. * @return 1 on success.
  2479. * @return 0 on failure.
  2480. * @return MEMORY_E when dynamic memory allocation fails.
  2481. */
  2482. static int wolfssl_ctx_load_path(WOLFSSL_CTX* ctx, const char* path,
  2483. word32 flags, int verify, int successCount)
  2484. {
  2485. int ret = 1;
  2486. char* name = NULL;
  2487. int fileRet;
  2488. int failCount = 0;
  2489. #ifdef WOLFSSL_SMALL_STACK
  2490. ReadDirCtx* readCtx;
  2491. #else
  2492. ReadDirCtx readCtx[1];
  2493. #endif
  2494. #ifdef WOLFSSL_SMALL_STACK
  2495. /* Allocate memory for directory reading context. */
  2496. readCtx = (ReadDirCtx*)XMALLOC(sizeof(ReadDirCtx), ctx->heap,
  2497. DYNAMIC_TYPE_DIRCTX);
  2498. if (readCtx == NULL) {
  2499. ret = MEMORY_E;
  2500. }
  2501. #endif
  2502. if (ret == 1) {
  2503. /* Get name of first file in path. */
  2504. fileRet = wc_ReadDirFirst(readCtx, path, &name);
  2505. /* While getting filename doesn't fail and name returned, process file.
  2506. */
  2507. while ((fileRet == 0) && (name != NULL)) {
  2508. WOLFSSL_MSG(name);
  2509. /* Load file. */
  2510. ret = wolfssl_ctx_load_path_file(ctx, name, verify, flags,
  2511. &failCount, &successCount);
  2512. /* Get next filenmae. */
  2513. fileRet = wc_ReadDirNext(readCtx, path, &name);
  2514. }
  2515. /* Cleanup directory reading context. */
  2516. wc_ReadDirClose(readCtx);
  2517. /* When not WOLFSSL_QT, ret is always overwritten. */
  2518. (void)ret;
  2519. /* Return real directory read failure error codes. */
  2520. if (fileRet != WC_READDIR_NOFILE) {
  2521. ret = fileRet;
  2522. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  2523. /* Ignore bad path error when flag set. */
  2524. if ((ret == BAD_PATH_ERROR) &&
  2525. (flags & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR)) {
  2526. /* QSslSocket always loads certs in system folder
  2527. * when it is initialized.
  2528. * Compliant with OpenSSL when flag set.
  2529. */
  2530. ret = 1;
  2531. }
  2532. else {
  2533. /* qssl socket wants to know errors. */
  2534. WOLFSSL_ERROR(ret);
  2535. }
  2536. #endif
  2537. }
  2538. /* Report failure if no files successfully loaded or there were
  2539. * failures. */
  2540. else if ((successCount == 0) || (failCount > 0)) {
  2541. /* Use existing error code if exists. */
  2542. #if defined(WOLFSSL_QT)
  2543. /* Compliant with OpenSSL when flag set. */
  2544. if (!(flags & WOLFSSL_LOAD_FLAG_IGNORE_ZEROFILE))
  2545. #endif
  2546. {
  2547. /* Return 0 when no files loaded. */
  2548. ret = 0;
  2549. }
  2550. }
  2551. else {
  2552. /* We loaded something so it is a success. */
  2553. ret = 1;
  2554. }
  2555. #ifdef WOLFSSL_SMALL_STACK
  2556. /* Dispose of dynamically allocated memory. */
  2557. XFREE(readCtx, ctx->heap, DYNAMIC_TYPE_DIRCTX);
  2558. #endif
  2559. }
  2560. return ret;
  2561. }
  2562. #endif
  2563. /* Load a file and/or files in path
  2564. *
  2565. * No c_rehash.
  2566. *
  2567. * @param [in, out] ctx SSL context object.
  2568. * @param [in] file Name of file to load. May be NULL.
  2569. * @param [in] path Path to directory containing PEM CA files.
  2570. * May be NULL.
  2571. * @param [in] flags Flags representing options for loading.
  2572. * @return 1 on success.
  2573. * @return 0 on failure.
  2574. * @return NOT_COMPILED_IN when directory reading not supported and path is
  2575. * not NULL.
  2576. * @return Other negative on error.
  2577. */
  2578. int wolfSSL_CTX_load_verify_locations_ex(WOLFSSL_CTX* ctx, const char* file,
  2579. const char* path, word32 flags)
  2580. {
  2581. int ret = 1;
  2582. #ifndef NO_WOLFSSL_DIR
  2583. int successCount = 0;
  2584. #endif
  2585. int verify = WOLFSSL_VERIFY_DEFAULT;
  2586. WOLFSSL_MSG("wolfSSL_CTX_load_verify_locations_ex");
  2587. /* Validate parameters. */
  2588. if ((ctx == NULL) || ((file == NULL) && (path == NULL))) {
  2589. ret = 0;
  2590. }
  2591. if (ret == 1) {
  2592. /* Get setting on how to verify certificates. */
  2593. verify = GET_VERIFY_SETTING_CTX(ctx);
  2594. /* Overwrite setting when flag set. */
  2595. if (flags & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) {
  2596. verify = VERIFY_SKIP_DATE;
  2597. }
  2598. if (file != NULL) {
  2599. /* Load the PEM formatted CA file. */
  2600. ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, CA_TYPE, NULL, 0,
  2601. NULL, verify);
  2602. #ifndef NO_WOLFSSL_DIR
  2603. if (ret == 1) {
  2604. /* Include success in overall count. */
  2605. successCount++;
  2606. }
  2607. #endif
  2608. #if defined(WOLFSSL_TRUST_PEER_CERT) && defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2609. /* Load CA as a trusted peer certificate. */
  2610. ret = wolfSSL_CTX_trust_peer_cert(ctx, file, WOLFSSL_FILETYPE_PEM);
  2611. if (ret != 1) {
  2612. WOLFSSL_MSG("wolfSSL_CTX_trust_peer_cert error");
  2613. }
  2614. #endif
  2615. }
  2616. }
  2617. if ((ret == 1) && (path != NULL)) {
  2618. #ifndef NO_WOLFSSL_DIR
  2619. /* Load CA files form path. */
  2620. ret = wolfssl_ctx_load_path(ctx, path, flags, verify, successCount);
  2621. #else
  2622. /* Loading a path not supported. */
  2623. ret = NOT_COMPILED_IN;
  2624. (void)flags;
  2625. #endif
  2626. }
  2627. return ret;
  2628. }
  2629. /* Load a file and/or files in path
  2630. *
  2631. * No c_rehash.
  2632. *
  2633. * @param [in, out] ctx SSL context object.
  2634. * @param [in] file Name of file to load. May be NULL.
  2635. * @param [in] path Path to directory containing PEM CA files.
  2636. * May be NULL.
  2637. * @return 1 on success.
  2638. * @return 0 on failure.
  2639. */
  2640. WOLFSSL_ABI
  2641. int wolfSSL_CTX_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
  2642. const char* path)
  2643. {
  2644. /* Load using default flags/options. */
  2645. int ret = wolfSSL_CTX_load_verify_locations_ex(ctx, file, path,
  2646. WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS);
  2647. /* Return 1 on success or 0 on failure. */
  2648. return WS_RETURN_CODE(ret, 0);
  2649. }
  2650. #ifdef WOLFSSL_SYS_CA_CERTS
  2651. #ifdef USE_WINDOWS_API
  2652. /* Load CA certificate from Windows store.
  2653. *
  2654. * Assumes loaded is 0.
  2655. *
  2656. * @param [in, out] ctx SSL context object.
  2657. * @param [out] loaded Whether CA certificates were loaded.
  2658. * @return 1 on success.
  2659. * @return 0 on failure.
  2660. */
  2661. static int LoadSystemCaCertsWindows(WOLFSSL_CTX* ctx, byte* loaded)
  2662. {
  2663. int ret = 1;
  2664. word32 i;
  2665. HANDLE handle = NULL;
  2666. PCCERT_CONTEXT certCtx = NULL;
  2667. LPCSTR storeNames[2] = {"ROOT", "CA"};
  2668. HCRYPTPROV_LEGACY hProv = (HCRYPTPROV_LEGACY)NULL;
  2669. if ((ctx == NULL) || (loaded == NULL)) {
  2670. ret = 0;
  2671. }
  2672. for (i = 0; (ret == 0) && (i < sizeof(storeNames)/sizeof(*storeNames));
  2673. ++i) {
  2674. handle = CertOpenSystemStoreA(hProv, storeNames[i]);
  2675. if (handle != NULL) {
  2676. while ((certCtx = CertEnumCertificatesInStore(handle, certCtx))
  2677. != NULL) {
  2678. if (certCtx->dwCertEncodingType == X509_ASN_ENCODING) {
  2679. if (ProcessBuffer(ctx, certCtx->pbCertEncoded,
  2680. certCtx->cbCertEncoded, WOLFSSL_FILETYPE_ASN1,
  2681. CA_TYPE, NULL, NULL, 0,
  2682. GET_VERIFY_SETTING_CTX(ctx)) == 1) {
  2683. /*
  2684. * Set "loaded" as long as we've loaded one CA
  2685. * cert.
  2686. */
  2687. *loaded = 1;
  2688. }
  2689. }
  2690. }
  2691. }
  2692. else {
  2693. WOLFSSL_MSG_EX("Failed to open cert store %s.", storeNames[i]);
  2694. }
  2695. if (handle != NULL && !CertCloseStore(handle, 0)) {
  2696. WOLFSSL_MSG_EX("Failed to close cert store %s.", storeNames[i]);
  2697. ret = 0;
  2698. }
  2699. }
  2700. return ret;
  2701. }
  2702. #elif defined(__APPLE__)
  2703. #if defined(HAVE_SECURITY_SECTRUSTSETTINGS_H) \
  2704. && !defined(WOLFSSL_APPLE_NATIVE_CERT_VALIDATION)
  2705. /* Manually obtains certificates from the system trust store and loads them
  2706. * directly into wolfSSL "the old way".
  2707. *
  2708. * As of MacOS 14.0 we are still able to use this method to access system
  2709. * certificates. Accessibility of this API is indicated by the presence of the
  2710. * Security/SecTrustSettings.h header. In the likely event that Apple removes
  2711. * access to this API on Macs, this function should be removed and the
  2712. * DoAppleNativeCertValidation() routine should be used for all devices.
  2713. *
  2714. * Assumes loaded is 0.
  2715. *
  2716. * @param [in, out] ctx SSL context object.
  2717. * @param [out] loaded Whether CA certificates were loaded.
  2718. * @return 1 on success.
  2719. * @return 0 on failure.
  2720. */
  2721. static int LoadSystemCaCertsMac(WOLFSSL_CTX* ctx, byte* loaded)
  2722. {
  2723. int ret = 1;
  2724. word32 i;
  2725. const unsigned int trustDomains[] = {
  2726. kSecTrustSettingsDomainUser,
  2727. kSecTrustSettingsDomainAdmin,
  2728. kSecTrustSettingsDomainSystem
  2729. };
  2730. CFArrayRef certs;
  2731. OSStatus stat;
  2732. CFIndex numCerts;
  2733. CFDataRef der;
  2734. CFIndex j;
  2735. if ((ctx == NULL) || (loaded == NULL)) {
  2736. ret = 0;
  2737. }
  2738. for (i = 0; (ret == 1) && (i < sizeof(trustDomains)/sizeof(*trustDomains));
  2739. ++i) {
  2740. stat = SecTrustSettingsCopyCertificates(
  2741. (SecTrustSettingsDomain)trustDomains[i], &certs);
  2742. if (stat == errSecSuccess) {
  2743. numCerts = CFArrayGetCount(certs);
  2744. for (j = 0; j < numCerts; ++j) {
  2745. der = SecCertificateCopyData((SecCertificateRef)
  2746. CFArrayGetValueAtIndex(certs, j));
  2747. if (der != NULL) {
  2748. if (ProcessBuffer(ctx, CFDataGetBytePtr(der),
  2749. CFDataGetLength(der), WOLFSSL_FILETYPE_ASN1,
  2750. CA_TYPE, NULL, NULL, 0,
  2751. GET_VERIFY_SETTING_CTX(ctx)) == 1) {
  2752. /*
  2753. * Set "loaded" as long as we've loaded one CA
  2754. * cert.
  2755. */
  2756. *loaded = 1;
  2757. }
  2758. CFRelease(der);
  2759. }
  2760. }
  2761. CFRelease(certs);
  2762. }
  2763. else if (stat == errSecNoTrustSettings) {
  2764. WOLFSSL_MSG_EX("No trust settings for domain %d, moving to next "
  2765. "domain.", trustDomains[i]);
  2766. }
  2767. else {
  2768. WOLFSSL_MSG_EX("SecTrustSettingsCopyCertificates failed with"
  2769. " status %d.", stat);
  2770. ret = 0;
  2771. break;
  2772. }
  2773. }
  2774. return ret;
  2775. }
  2776. #endif /* defined(HAVE_SECURITY_SECTRUSTSETTINGS_H) */
  2777. #else
  2778. /* Potential system CA certs directories on Linux/Unix distros. */
  2779. static const char* systemCaDirs[] = {
  2780. #if defined(__ANDROID__) || defined(ANDROID)
  2781. "/system/etc/security/cacerts" /* Android */
  2782. #else
  2783. "/etc/ssl/certs", /* Debian, Ubuntu, Gentoo, others */
  2784. "/etc/pki/ca-trust/source/anchors", /* Fedora, RHEL */
  2785. "/etc/pki/tls/certs" /* Older RHEL */
  2786. #endif
  2787. };
  2788. /* Get CA directory list.
  2789. *
  2790. * @param [out] num Number of CA directories.
  2791. * @return CA directory list.
  2792. * @return NULL when num is NULL.
  2793. */
  2794. const char** wolfSSL_get_system_CA_dirs(word32* num)
  2795. {
  2796. const char** ret;
  2797. /* Validate parameters. */
  2798. if (num == NULL) {
  2799. ret = NULL;
  2800. }
  2801. else {
  2802. ret = systemCaDirs;
  2803. *num = sizeof(systemCaDirs)/sizeof(*systemCaDirs);
  2804. }
  2805. return ret;
  2806. }
  2807. /* Load CA certificate from default system directories.
  2808. *
  2809. * Assumes loaded is 0.
  2810. *
  2811. * @param [in, out] ctx SSL context object.
  2812. * @param [out] loaded Whether CA certificates were loaded.
  2813. * @return 1 on success.
  2814. * @return 0 on failure.
  2815. */
  2816. static int LoadSystemCaCertsNix(WOLFSSL_CTX* ctx, byte* loaded) {
  2817. int ret = 1;
  2818. word32 i;
  2819. if ((ctx == NULL) || (loaded == NULL)) {
  2820. ret = 0;
  2821. }
  2822. for (i = 0; (ret == 1) && (i < sizeof(systemCaDirs)/sizeof(*systemCaDirs));
  2823. ++i) {
  2824. WOLFSSL_MSG_EX("Attempting to load system CA certs from %s.",
  2825. systemCaDirs[i]);
  2826. /*
  2827. * We want to keep trying to load more CA certs even if one cert in
  2828. * the directory is bad and can't be used (e.g. if one is expired),
  2829. * so we use WOLFSSL_LOAD_FLAG_IGNORE_ERR.
  2830. */
  2831. if (wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, systemCaDirs[i],
  2832. WOLFSSL_LOAD_FLAG_IGNORE_ERR) != 1) {
  2833. WOLFSSL_MSG_EX("Failed to load CA certs from %s, trying "
  2834. "next possible location.", systemCaDirs[i]);
  2835. }
  2836. else {
  2837. WOLFSSL_MSG_EX("Loaded CA certs from %s.",
  2838. systemCaDirs[i]);
  2839. *loaded = 1;
  2840. /* Stop searching after we've loaded one directory. */
  2841. break;
  2842. }
  2843. }
  2844. return ret;
  2845. }
  2846. #endif
  2847. /* Load CA certificates from system defined locations.
  2848. *
  2849. * @param [in, out] ctx SSL context object.
  2850. * @return 1 on success.
  2851. * @return 0 on failure.
  2852. * @return WOLFSSL_BAD_PATH when no error but no certificates loaded.
  2853. */
  2854. int wolfSSL_CTX_load_system_CA_certs(WOLFSSL_CTX* ctx)
  2855. {
  2856. int ret;
  2857. byte loaded = 0;
  2858. WOLFSSL_ENTER("wolfSSL_CTX_load_system_CA_certs");
  2859. #ifdef USE_WINDOWS_API
  2860. ret = LoadSystemCaCertsWindows(ctx, &loaded);
  2861. #elif defined(__APPLE__)
  2862. #if defined(HAVE_SECURITY_SECTRUSTSETTINGS_H) \
  2863. && !defined(WOLFSSL_APPLE_NATIVE_CERT_VALIDATION)
  2864. /* As of MacOS 14.0 we are still able to access system certificates and
  2865. * load them manually into wolfSSL "the old way". Accessibility of this API
  2866. * is indicated by the presence of the Security/SecTrustSettings.h header */
  2867. ret = LoadSystemCaCertsMac(ctx, &loaded);
  2868. #elif defined(WOLFSSL_APPLE_NATIVE_CERT_VALIDATION)
  2869. /* For other Apple devices, Apple has removed the ability to obtain
  2870. * certificates from the trust store, so we can't use wolfSSL's built-in
  2871. * certificate validation mechanisms anymore. We instead must call into the
  2872. * Security Framework APIs to authenticate peer certificates when received.
  2873. * (see src/internal.c:DoAppleNativeCertValidation()).
  2874. * Thus, there is no CA "loading" required, but to keep behavior consistent
  2875. * with the current API (not using system CA certs unless this function has
  2876. * been called), we simply set a flag indicating that the new apple trust
  2877. * verification routine should be used later */
  2878. ctx->doAppleNativeCertValidationFlag = 1;
  2879. ret = 1;
  2880. loaded = 1;
  2881. #if FIPS_VERSION_GE(2,0) /* Gate back to cert 3389 FIPS modules */
  2882. #warning "Cryptographic operations may occur outside the FIPS module boundary" \
  2883. "Please review FIPS claims for cryptography on this Apple device"
  2884. #endif /* FIPS_VERSION_GE(2,0) */
  2885. #else
  2886. /* HAVE_SECURITY_SECXXX_H macros are set by autotools or CMake when searching
  2887. * system for the required SDK headers. If building with user_settings.h, you
  2888. * will need to manually define WOLFSSL_APPLE_NATIVE_CERT_VALIDATION
  2889. * and ensure the appropriate Security.framework headers and libraries are
  2890. * visible to your compiler */
  2891. #error "WOLFSSL_SYS_CA_CERTS on Apple devices requires Security.framework" \
  2892. " header files to be detected, or a manual override with" \
  2893. " WOLFSSL_APPLE_NATIVE_CERT_VALIDATION"
  2894. #endif
  2895. #else
  2896. ret = LoadSystemCaCertsNix(ctx, &loaded);
  2897. #endif
  2898. /* If we didn't fail but didn't load then we error out. */
  2899. if ((ret == 1) && (!loaded)) {
  2900. ret = WOLFSSL_BAD_PATH;
  2901. }
  2902. WOLFSSL_LEAVE("wolfSSL_CTX_load_system_CA_certs", ret);
  2903. return ret;
  2904. }
  2905. #endif /* WOLFSSL_SYS_CA_CERTS */
  2906. #ifdef WOLFSSL_TRUST_PEER_CERT
  2907. /* Load a trusted peer certificate into SSL context.
  2908. *
  2909. * @param [in, out] ctx SSL context object.
  2910. * @param [in] file Name of peer certificate file.
  2911. * @param [in] format Format of data:
  2912. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  2913. * @return 1 on success.
  2914. * @return 0 when ctx or file is NULL.
  2915. */
  2916. int wolfSSL_CTX_trust_peer_cert(WOLFSSL_CTX* ctx, const char* file, int format)
  2917. {
  2918. int ret;
  2919. WOLFSSL_ENTER("wolfSSL_CTX_trust_peer_cert");
  2920. /* Validate parameters. */
  2921. if ((ctx == NULL) || (file == NULL)) {
  2922. ret = 0;
  2923. }
  2924. else {
  2925. ret = ProcessFile(ctx, file, format, TRUSTED_PEER_TYPE, NULL, 0, NULL,
  2926. GET_VERIFY_SETTING_CTX(ctx));
  2927. }
  2928. return ret;
  2929. }
  2930. /* Load a trusted peer certificate into SSL.
  2931. *
  2932. * @param [in, out] ssl SSL object.
  2933. * @param [in] file Name of peer certificate file.
  2934. * @param [in] format Format of data:
  2935. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  2936. * @return 1 on success.
  2937. * @return 0 when ssl or file is NULL.
  2938. */
  2939. int wolfSSL_trust_peer_cert(WOLFSSL* ssl, const char* file, int format)
  2940. {
  2941. int ret;
  2942. WOLFSSL_ENTER("wolfSSL_trust_peer_cert");
  2943. /* Validate parameters. */
  2944. if ((ssl == NULL) || (file == NULL)) {
  2945. ret = 0;
  2946. }
  2947. else {
  2948. ret = ProcessFile(NULL, file, format, TRUSTED_PEER_TYPE, ssl, 0, NULL,
  2949. GET_VERIFY_SETTING_SSL(ssl));
  2950. }
  2951. return ret;
  2952. }
  2953. #endif /* WOLFSSL_TRUST_PEER_CERT */
  2954. #ifdef WOLFSSL_DER_LOAD
  2955. /* Load a CA certificate into SSL context.
  2956. *
  2957. * @param [in, out] ctx SSL context object.
  2958. * @param [in] file Name of peer certificate file.
  2959. * @param [in] format Format of data:
  2960. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  2961. * @return 1 on success.
  2962. * @return 0 on failure.
  2963. */
  2964. int wolfSSL_CTX_der_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
  2965. int format)
  2966. {
  2967. int ret;
  2968. WOLFSSL_ENTER("wolfSSL_CTX_der_load_verify_locations");
  2969. /* Validate parameters. */
  2970. if ((ctx == NULL) || (file == NULL)) {
  2971. ret = 0;
  2972. }
  2973. else {
  2974. ret = ProcessFile(ctx, file, format, CA_TYPE, NULL, 0, NULL,
  2975. GET_VERIFY_SETTING_CTX(ctx));
  2976. }
  2977. /* Return 1 on success or 0 on failure. */
  2978. return WS_RC(ret);
  2979. }
  2980. #endif /* WOLFSSL_DER_LOAD */
  2981. /* Load a user certificate into SSL context.
  2982. *
  2983. * @param [in, out] ctx SSL context object.
  2984. * @param [in] file Name of user certificate file.
  2985. * @param [in] format Format of data:
  2986. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  2987. * @return 1 on success.
  2988. * @return 0 on failure.
  2989. */
  2990. WOLFSSL_ABI
  2991. int wolfSSL_CTX_use_certificate_file(WOLFSSL_CTX* ctx, const char* file,
  2992. int format)
  2993. {
  2994. int ret;
  2995. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_file");
  2996. ret = ProcessFile(ctx, file, format, CERT_TYPE, NULL, 0, NULL,
  2997. GET_VERIFY_SETTING_CTX(ctx));
  2998. /* Return 1 on success or 0 on failure. */
  2999. return WS_RC(ret);
  3000. }
  3001. /* Load a private key into SSL context.
  3002. *
  3003. * @param [in, out] ctx SSL context object.
  3004. * @param [in] file Name of private key file.
  3005. * @param [in] format Format of data:
  3006. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3007. * @return 1 on success.
  3008. * @return 0 on failure.
  3009. */
  3010. WOLFSSL_ABI
  3011. int wolfSSL_CTX_use_PrivateKey_file(WOLFSSL_CTX* ctx, const char* file,
  3012. int format)
  3013. {
  3014. int ret;
  3015. WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_file");
  3016. ret = ProcessFile(ctx, file, format, PRIVATEKEY_TYPE, NULL, 0, NULL,
  3017. GET_VERIFY_SETTING_CTX(ctx));
  3018. /* Return 1 on success or 0 on failure. */
  3019. return WS_RC(ret);
  3020. }
  3021. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3022. /* Load an alternative private key into SSL context.
  3023. *
  3024. * @param [in, out] ctx SSL context object.
  3025. * @param [in] file Name of private key file.
  3026. * @param [in] format Format of data:
  3027. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3028. * @return 1 on success.
  3029. * @return 0 on failure.
  3030. */
  3031. int wolfSSL_CTX_use_AltPrivateKey_file(WOLFSSL_CTX* ctx, const char* file,
  3032. int format)
  3033. {
  3034. int ret;
  3035. WOLFSSL_ENTER("wolfSSL_CTX_use_AltPrivateKey_file");
  3036. ret = ProcessFile(ctx, file, format, ALT_PRIVATEKEY_TYPE, NULL, 0, NULL,
  3037. GET_VERIFY_SETTING_CTX(ctx));
  3038. /* Return 1 on success or 0 on failure. */
  3039. return WS_RC(ret);
  3040. }
  3041. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  3042. /* Load a PEM certificate chain into SSL context.
  3043. *
  3044. * @param [in, out] ctx SSL context object.
  3045. * @param [in] file Name of PEM certificate chain file.
  3046. * @return 1 on success.
  3047. * @return 0 on failure.
  3048. */
  3049. WOLFSSL_ABI
  3050. int wolfSSL_CTX_use_certificate_chain_file(WOLFSSL_CTX* ctx, const char* file)
  3051. {
  3052. int ret;
  3053. /* process up to MAX_CHAIN_DEPTH plus subject cert */
  3054. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_file");
  3055. ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, CERT_TYPE, NULL, 1, NULL,
  3056. GET_VERIFY_SETTING_CTX(ctx));
  3057. /* Return 1 on success or 0 on failure. */
  3058. return WS_RC(ret);
  3059. }
  3060. /* Load certificate chain into SSL context.
  3061. *
  3062. * Processes up to MAX_CHAIN_DEPTH plus subject cert.
  3063. *
  3064. * @param [in, out] ctx SSL context object.
  3065. * @param [in] file Name of private key file.
  3066. * @param [in] format Format of data:
  3067. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3068. * @return 1 on success.
  3069. * @return 0 on failure.
  3070. */
  3071. int wolfSSL_CTX_use_certificate_chain_file_format(WOLFSSL_CTX* ctx,
  3072. const char* file, int format)
  3073. {
  3074. int ret;
  3075. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_file_format");
  3076. ret = ProcessFile(ctx, file, format, CERT_TYPE, NULL, 1, NULL,
  3077. GET_VERIFY_SETTING_CTX(ctx));
  3078. /* Return 1 on success or 0 on failure. */
  3079. return WS_RC(ret);
  3080. }
  3081. #endif /* NO_FILESYSTEM */
  3082. #ifdef OPENSSL_EXTRA
  3083. /* Load a private key into SSL.
  3084. *
  3085. * @param [in, out] ssl SSL object.
  3086. * @param [in] pkey EVP private key.
  3087. * @return 1 on success.
  3088. * @return 0 on failure.
  3089. */
  3090. int wolfSSL_use_PrivateKey(WOLFSSL* ssl, WOLFSSL_EVP_PKEY* pkey)
  3091. {
  3092. int ret;
  3093. WOLFSSL_ENTER("wolfSSL_use_PrivateKey");
  3094. /* Validate parameters. */
  3095. if ((ssl == NULL) || (pkey == NULL)) {
  3096. ret = 0;
  3097. }
  3098. else {
  3099. /* Get DER encoded key data from EVP private key. */
  3100. ret = wolfSSL_use_PrivateKey_buffer(ssl, (unsigned char*)pkey->pkey.ptr,
  3101. pkey->pkey_sz, WOLFSSL_FILETYPE_ASN1);
  3102. }
  3103. return ret;
  3104. }
  3105. /* Load a DER encoded private key in a buffer into SSL.
  3106. *
  3107. * @param [in] pri Indicates type of private key. Ignored.
  3108. * @param [in, out] ssl SSL object.
  3109. * @param [in] der Buffer holding DER encoded private key.
  3110. * @param [in] derSz Size of data in bytes.
  3111. * @return 1 on success.
  3112. * @return 0 on failure.
  3113. */
  3114. int wolfSSL_use_PrivateKey_ASN1(int pri, WOLFSSL* ssl, const unsigned char* der,
  3115. long derSz)
  3116. {
  3117. int ret;
  3118. WOLFSSL_ENTER("wolfSSL_use_PrivateKey_ASN1");
  3119. (void)pri;
  3120. /* Validate parameters. */
  3121. if ((ssl == NULL) || (der == NULL)) {
  3122. ret = 0;
  3123. }
  3124. else {
  3125. ret = wolfSSL_use_PrivateKey_buffer(ssl, der, derSz,
  3126. WOLFSSL_FILETYPE_ASN1);
  3127. }
  3128. return ret;
  3129. }
  3130. /* Load a DER encoded private key in a buffer into SSL context.
  3131. *
  3132. * @param [in] pri Indicates type of private key. Ignored.
  3133. * @param [in, out] ctx SSL context object.
  3134. * @param [in] der Buffer holding DER encoded private key.
  3135. * @param [in] derSz Size of data in bytes.
  3136. * @return 1 on success.
  3137. * @return 0 on failure.
  3138. */
  3139. int wolfSSL_CTX_use_PrivateKey_ASN1(int pri, WOLFSSL_CTX* ctx,
  3140. unsigned char* der, long derSz)
  3141. {
  3142. int ret;
  3143. WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_ASN1");
  3144. (void)pri;
  3145. /* Validate parameters. */
  3146. if ((ctx == NULL) || (der == NULL)) {
  3147. ret = 0;
  3148. }
  3149. else {
  3150. ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, derSz,
  3151. WOLFSSL_FILETYPE_ASN1);
  3152. }
  3153. return ret;
  3154. }
  3155. #ifndef NO_RSA
  3156. /* Load a DER encoded RSA private key in a buffer into SSL.
  3157. *
  3158. * @param [in, out] ssl SSL object.
  3159. * @param [in] der Buffer holding DER encoded RSA private key.
  3160. * @param [in] derSz Size of data in bytes.
  3161. * @return 1 on success.
  3162. * @return 0 on failure.
  3163. */
  3164. int wolfSSL_use_RSAPrivateKey_ASN1(WOLFSSL* ssl, unsigned char* der, long derSz)
  3165. {
  3166. int ret;
  3167. WOLFSSL_ENTER("wolfSSL_use_RSAPrivateKey_ASN1");
  3168. /* Validate parameters. */
  3169. if ((ssl == NULL) || (der == NULL)) {
  3170. ret = 0;
  3171. }
  3172. else {
  3173. ret = wolfSSL_use_PrivateKey_buffer(ssl, der, derSz,
  3174. WOLFSSL_FILETYPE_ASN1);
  3175. }
  3176. return ret;
  3177. }
  3178. #endif
  3179. /* Load a certificate into SSL.
  3180. *
  3181. * @param [in, out] ssl SSL object.
  3182. * @param [in] x509 X509 certificate object.
  3183. * @return 1 on success.
  3184. * @return 0 on failure.
  3185. */
  3186. int wolfSSL_use_certificate(WOLFSSL* ssl, WOLFSSL_X509* x509)
  3187. {
  3188. int ret;
  3189. WOLFSSL_ENTER("wolfSSL_use_certificate");
  3190. /* Validate parameters. */
  3191. if ((ssl == NULL) || (x509 == NULL) || (x509->derCert == NULL)) {
  3192. ret = 0;
  3193. }
  3194. else {
  3195. long idx = 0;
  3196. /* Get DER encoded certificate data from X509 object. */
  3197. ret = ProcessBuffer(NULL, x509->derCert->buffer, x509->derCert->length,
  3198. WOLFSSL_FILETYPE_ASN1, CERT_TYPE, ssl, &idx, 0,
  3199. GET_VERIFY_SETTING_SSL(ssl));
  3200. }
  3201. /* Return 1 on success or 0 on failure. */
  3202. return WS_RC(ret);
  3203. }
  3204. #endif /* OPENSSL_EXTRA */
  3205. /* Load a DER encoded certificate in a buffer into SSL.
  3206. *
  3207. * @param [in, out] ssl SSL object.
  3208. * @param [in] der Buffer holding DER encoded certificate.
  3209. * @param [in] derSz Size of data in bytes.
  3210. * @return 1 on success.
  3211. * @return 0 on failure.
  3212. */
  3213. int wolfSSL_use_certificate_ASN1(WOLFSSL* ssl, const unsigned char* der,
  3214. int derSz)
  3215. {
  3216. int ret;
  3217. WOLFSSL_ENTER("wolfSSL_use_certificate_ASN1");
  3218. /* Validate parameters. */
  3219. if ((ssl == NULL) || (der == NULL)) {
  3220. ret = 0;
  3221. }
  3222. else {
  3223. long idx = 0;
  3224. ret = ProcessBuffer(NULL, der, derSz, WOLFSSL_FILETYPE_ASN1, CERT_TYPE,
  3225. ssl, &idx, 0, GET_VERIFY_SETTING_SSL(ssl));
  3226. }
  3227. /* Return 1 on success or 0 on failure. */
  3228. return WS_RC(ret);
  3229. }
  3230. #ifndef NO_FILESYSTEM
  3231. /* Load a certificate from a file into SSL.
  3232. *
  3233. * @param [in, out] ssl SSL object.
  3234. * @param [in] file Name of file.
  3235. * @param [in] format Format of data:
  3236. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3237. * @return 1 on success.
  3238. * @return 0 on failure.
  3239. * @return BAD_FUNC_ARG when ssl is NULL.
  3240. */
  3241. WOLFSSL_ABI
  3242. int wolfSSL_use_certificate_file(WOLFSSL* ssl, const char* file, int format)
  3243. {
  3244. int ret;
  3245. WOLFSSL_ENTER("wolfSSL_use_certificate_file");
  3246. /* Validate parameters. */
  3247. if (ssl == NULL) {
  3248. ret = BAD_FUNC_ARG;
  3249. }
  3250. else {
  3251. ret = ProcessFile(ssl->ctx, file, format, CERT_TYPE, ssl, 0, NULL,
  3252. GET_VERIFY_SETTING_SSL(ssl));
  3253. /* Return 1 on success or 0 on failure. */
  3254. ret = WS_RC(ret);
  3255. }
  3256. return ret;
  3257. }
  3258. /* Load a private key from a file into SSL.
  3259. *
  3260. * @param [in, out] ssl SSL object.
  3261. * @param [in] file Name of file.
  3262. * @param [in] format Format of data:
  3263. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3264. * @return 1 on success.
  3265. * @return 0 on failure.
  3266. * @return BAD_FUNC_ARG when ssl is NULL.
  3267. */
  3268. WOLFSSL_ABI
  3269. int wolfSSL_use_PrivateKey_file(WOLFSSL* ssl, const char* file, int format)
  3270. {
  3271. int ret;
  3272. WOLFSSL_ENTER("wolfSSL_use_PrivateKey_file");
  3273. /* Validate parameters. */
  3274. if (ssl == NULL) {
  3275. ret = BAD_FUNC_ARG;
  3276. }
  3277. else {
  3278. ret = ProcessFile(ssl->ctx, file, format, PRIVATEKEY_TYPE, ssl, 0, NULL,
  3279. GET_VERIFY_SETTING_SSL(ssl));
  3280. /* Return 1 on success or 0 on failure. */
  3281. ret = WS_RC(ret);
  3282. }
  3283. return ret;
  3284. }
  3285. /* Load a PEM encoded certificate chain from a file into SSL.
  3286. *
  3287. * Process up to MAX_CHAIN_DEPTH plus subject cert.
  3288. *
  3289. * @param [in, out] ssl SSL object.
  3290. * @param [in] file Name of file.
  3291. * @return 1 on success.
  3292. * @return 0 on failure.
  3293. * @return BAD_FUNC_ARG when ssl is NULL.
  3294. */
  3295. WOLFSSL_ABI
  3296. int wolfSSL_use_certificate_chain_file(WOLFSSL* ssl, const char* file)
  3297. {
  3298. int ret;
  3299. WOLFSSL_ENTER("wolfSSL_use_certificate_chain_file");
  3300. /* Validate parameters. */
  3301. if (ssl == NULL) {
  3302. ret = BAD_FUNC_ARG;
  3303. }
  3304. else {
  3305. ret = ProcessFile(ssl->ctx, file, WOLFSSL_FILETYPE_PEM, CERT_TYPE, ssl,
  3306. 1, NULL, GET_VERIFY_SETTING_SSL(ssl));
  3307. /* Return 1 on success or 0 on failure. */
  3308. ret = WS_RC(ret);
  3309. }
  3310. return ret;
  3311. }
  3312. /* Load a certificate chain from a file into SSL.
  3313. *
  3314. * @param [in, out] ssl SSL object.
  3315. * @param [in] file Name of file.
  3316. * @param [in] format Format of data:
  3317. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3318. * @return 1 on success.
  3319. * @return 0 on failure.
  3320. * @return BAD_FUNC_ARG when ssl is NULL.
  3321. */
  3322. int wolfSSL_use_certificate_chain_file_format(WOLFSSL* ssl, const char* file,
  3323. int format)
  3324. {
  3325. int ret;
  3326. /* process up to MAX_CHAIN_DEPTH plus subject cert */
  3327. WOLFSSL_ENTER("wolfSSL_use_certificate_chain_file_format");
  3328. /* Validate parameters. */
  3329. if (ssl == NULL) {
  3330. ret = BAD_FUNC_ARG;
  3331. }
  3332. else {
  3333. ret = ProcessFile(ssl->ctx, file, format, CERT_TYPE, ssl, 1, NULL,
  3334. GET_VERIFY_SETTING_SSL(ssl));
  3335. /* Return 1 on success or 0 on failure. */
  3336. ret = WS_RC(ret);
  3337. }
  3338. return ret;
  3339. }
  3340. #endif /* !NO_FILESYSTEM */
  3341. #ifdef OPENSSL_EXTRA
  3342. #ifndef NO_FILESYSTEM
  3343. /* Load an RSA private key from a file into SSL context.
  3344. *
  3345. * @param [in, out] ctx SSL context object.
  3346. * @param [in] file Name of file.
  3347. * @param [in] format Format of data:
  3348. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3349. * @return 1 on success.
  3350. * @return 0 on failure.
  3351. */
  3352. int wolfSSL_CTX_use_RSAPrivateKey_file(WOLFSSL_CTX* ctx,const char* file,
  3353. int format)
  3354. {
  3355. WOLFSSL_ENTER("wolfSSL_CTX_use_RSAPrivateKey_file");
  3356. return wolfSSL_CTX_use_PrivateKey_file(ctx, file, format);
  3357. }
  3358. /* Load an RSA private key from a file into SSL.
  3359. *
  3360. * @param [in, out] ssl SSL object.
  3361. * @param [in] file Name of file.
  3362. * @param [in] format Format of data:
  3363. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3364. * @return 1 on success.
  3365. * @return 0 on failure.
  3366. * @return BAD_FUNC_ARG when ssl is NULL.
  3367. */
  3368. int wolfSSL_use_RSAPrivateKey_file(WOLFSSL* ssl, const char* file, int format)
  3369. {
  3370. WOLFSSL_ENTER("wolfSSL_use_RSAPrivateKey_file");
  3371. return wolfSSL_use_PrivateKey_file(ssl, file, format);
  3372. }
  3373. #endif /* NO_FILESYSTEM */
  3374. #endif /* OPENSSL_EXTRA */
  3375. /* Load a buffer of certificate/s into SSL context.
  3376. *
  3377. * @param [in, out] ctx SSL context object.
  3378. * @param [in] in Buffer holding certificate or private key.
  3379. * @param [in] sz Length of data in buffer in bytes.
  3380. * @param [in] format Format of data:
  3381. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3382. * @param [in] userChain Whether file contains chain of certificates.
  3383. * @param [in] flags Flags representing options for loading.
  3384. * @return 1 on success.
  3385. * @return 0 on failure.
  3386. * @return Negative on error.
  3387. */
  3388. int wolfSSL_CTX_load_verify_buffer_ex(WOLFSSL_CTX* ctx, const unsigned char* in,
  3389. long sz, int format, int userChain, word32 flags)
  3390. {
  3391. int ret;
  3392. int verify;
  3393. WOLFSSL_ENTER("wolfSSL_CTX_load_verify_buffer_ex");
  3394. /* Get setting on how to verify certificates. */
  3395. verify = GET_VERIFY_SETTING_CTX(ctx);
  3396. /* Overwrite setting when flag set. */
  3397. if (flags & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) {
  3398. verify = VERIFY_SKIP_DATE;
  3399. }
  3400. /* When PEM, treat as certificate chain of CA certificates. */
  3401. if (format == WOLFSSL_FILETYPE_PEM) {
  3402. ret = ProcessChainBuffer(ctx, NULL, in, sz, CA_TYPE, verify);
  3403. }
  3404. /* When DER, load the CA certificate. */
  3405. else {
  3406. ret = ProcessBuffer(ctx, in, sz, format, CA_TYPE, NULL, NULL,
  3407. userChain, verify);
  3408. }
  3409. #if defined(WOLFSSL_TRUST_PEER_CERT) && defined(OPENSSL_COMPATIBLE_DEFAULTS)
  3410. if (ret == 1) {
  3411. /* Load certificate/s as trusted peer certificate. */
  3412. ret = wolfSSL_CTX_trust_peer_buffer(ctx, in, sz, format);
  3413. }
  3414. #endif
  3415. WOLFSSL_LEAVE("wolfSSL_CTX_load_verify_buffer_ex", ret);
  3416. return ret;
  3417. }
  3418. /* Load a buffer of certificate/s into SSL context.
  3419. *
  3420. * @param [in, out] ctx SSL context object.
  3421. * @param [in] in Buffer holding certificate or private key.
  3422. * @param [in] sz Length of data in buffer in bytes.
  3423. * @param [in] format Format of data:
  3424. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3425. * @return 1 on success.
  3426. * @return 0 on failure.
  3427. * @return Negative on error.
  3428. */
  3429. int wolfSSL_CTX_load_verify_buffer(WOLFSSL_CTX* ctx, const unsigned char* in,
  3430. long sz, int format)
  3431. {
  3432. return wolfSSL_CTX_load_verify_buffer_ex(ctx, in, sz, format, 0,
  3433. WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS);
  3434. }
  3435. /* Load a buffer of certificate chain into SSL context.
  3436. *
  3437. * @param [in, out] ctx SSL context object.
  3438. * @param [in] in Buffer holding certificate chain.
  3439. * @param [in] sz Length of data in buffer in bytes.
  3440. * @param [in] format Format of data:
  3441. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3442. * @return 1 on success.
  3443. * @return 0 on failure.
  3444. * @return Negative on error.
  3445. */
  3446. int wolfSSL_CTX_load_verify_chain_buffer_format(WOLFSSL_CTX* ctx,
  3447. const unsigned char* in, long sz, int format)
  3448. {
  3449. return wolfSSL_CTX_load_verify_buffer_ex(ctx, in, sz, format, 1,
  3450. WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS);
  3451. }
  3452. #ifdef WOLFSSL_TRUST_PEER_CERT
  3453. /* Load a buffer of certificate/s into SSL context.
  3454. *
  3455. * @param [in, out] ctx SSL context object.
  3456. * @param [in] in Buffer holding certificate/s.
  3457. * @param [in] sz Length of data in buffer in bytes.
  3458. * @param [in] format Format of data:
  3459. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3460. * @return 1 on success.
  3461. * @return 0 on failure.
  3462. * @return BAD_FUNC_ARG when ctx or in is NULL, or sz is less than zero.
  3463. */
  3464. int wolfSSL_CTX_trust_peer_buffer(WOLFSSL_CTX* ctx, const unsigned char* in,
  3465. long sz, int format)
  3466. {
  3467. int ret;
  3468. int verify;
  3469. WOLFSSL_ENTER("wolfSSL_CTX_trust_peer_buffer");
  3470. /* Validate parameters. */
  3471. if ((ctx == NULL) || (in == NULL) || (sz < 0)) {
  3472. ret = BAD_FUNC_ARG;
  3473. }
  3474. else {
  3475. #if WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY
  3476. verify = VERIFY_SKIP_DATE;
  3477. #else
  3478. verify = GET_VERIFY_SETTING_CTX(ctx);
  3479. #endif
  3480. /* When PEM, treat as certificate chain of trusted peer certificates. */
  3481. if (format == WOLFSSL_FILETYPE_PEM) {
  3482. ret = ProcessChainBuffer(ctx, NULL, in, sz, TRUSTED_PEER_TYPE,
  3483. verify);
  3484. }
  3485. /* When DER, load the trusted peer certificate. */
  3486. else {
  3487. ret = ProcessBuffer(ctx, in, sz, format, TRUSTED_PEER_TYPE, NULL,
  3488. NULL, 0, verify);
  3489. }
  3490. }
  3491. return ret;
  3492. }
  3493. #endif /* WOLFSSL_TRUST_PEER_CERT */
  3494. /* Load a certificate in a buffer into SSL context.
  3495. *
  3496. * @param [in, out] ctx SSL context object.
  3497. * @param [in] in Buffer holding certificate.
  3498. * @param [in] sz Size of data in bytes.
  3499. * @param [in] format Format of data:
  3500. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3501. * @return 1 on success.
  3502. * @return 0 on failure.
  3503. * @return Negative on error.
  3504. */
  3505. int wolfSSL_CTX_use_certificate_buffer(WOLFSSL_CTX* ctx,
  3506. const unsigned char* in, long sz, int format)
  3507. {
  3508. int ret;
  3509. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_buffer");
  3510. ret = ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 0,
  3511. GET_VERIFY_SETTING_CTX(ctx));
  3512. WOLFSSL_LEAVE("wolfSSL_CTX_use_certificate_buffer", ret);
  3513. return ret;
  3514. }
  3515. /* Load a private key in a buffer into SSL context.
  3516. *
  3517. * @param [in, out] ctx SSL context object.
  3518. * @param [in] in Buffer holding private key.
  3519. * @param [in] sz Size of data in bytes.
  3520. * @param [in] format Format of data:
  3521. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3522. * @return 1 on success.
  3523. * @return 0 on failure.
  3524. * @return Negative on error.
  3525. */
  3526. int wolfSSL_CTX_use_PrivateKey_buffer(WOLFSSL_CTX* ctx, const unsigned char* in,
  3527. long sz, int format)
  3528. {
  3529. int ret;
  3530. long consumed = 0;
  3531. WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_buffer");
  3532. ret = ProcessBuffer(ctx, in, sz, format, PRIVATEKEY_TYPE, NULL, &consumed,
  3533. 0, GET_VERIFY_SETTING_CTX(ctx));
  3534. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3535. if ((ret == 1) && (consumed < sz)) {
  3536. /* When support for dual algorithm certificates is enabled, the
  3537. * buffer may contain both the primary and the alternative
  3538. * private key. Hence, we have to parse both of them.
  3539. */
  3540. ret = ProcessBuffer(ctx, in + consumed, sz - consumed, format,
  3541. ALT_PRIVATEKEY_TYPE, NULL, NULL, 0, GET_VERIFY_SETTING_CTX(ctx));
  3542. }
  3543. #endif
  3544. (void)consumed;
  3545. WOLFSSL_LEAVE("wolfSSL_CTX_use_PrivateKey_buffer", ret);
  3546. return ret;
  3547. }
  3548. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3549. int wolfSSL_CTX_use_AltPrivateKey_buffer(WOLFSSL_CTX* ctx,
  3550. const unsigned char* in, long sz, int format)
  3551. {
  3552. int ret;
  3553. WOLFSSL_ENTER("wolfSSL_CTX_use_AltPrivateKey_buffer");
  3554. ret = ProcessBuffer(ctx, in, sz, format, ALT_PRIVATEKEY_TYPE, NULL,
  3555. NULL, 0, GET_VERIFY_SETTING_CTX(ctx));
  3556. WOLFSSL_LEAVE("wolfSSL_CTX_use_AltPrivateKey_buffer", ret);
  3557. return ret;
  3558. }
  3559. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  3560. #ifdef WOLF_PRIVATE_KEY_ID
  3561. /* Load the id of a private key into SSL context.
  3562. *
  3563. * @param [in, out] ctx SSL context object.
  3564. * @param [in] id Buffer holding id.
  3565. * @param [in] sz Size of data in bytes.
  3566. * @param [in] devId Device identifier.
  3567. * @return 1 on success.
  3568. * @return 0 on failure.
  3569. */
  3570. int wolfSSL_CTX_use_PrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id,
  3571. long sz, int devId)
  3572. {
  3573. int ret = 1;
  3574. /* Dispose of old private key and allocate and copy in id. */
  3575. FreeDer(&ctx->privateKey);
  3576. if (AllocCopyDer(&ctx->privateKey, id, (word32)sz, PRIVATEKEY_TYPE,
  3577. ctx->heap) != 0) {
  3578. ret = 0;
  3579. }
  3580. if (ret == 1) {
  3581. /* Private key is an id. */
  3582. ctx->privateKeyId = 1;
  3583. ctx->privateKeyLabel = 0;
  3584. /* Set private key device id to be one passed in or for SSL context. */
  3585. if (devId != INVALID_DEVID) {
  3586. ctx->privateKeyDevId = devId;
  3587. }
  3588. else {
  3589. ctx->privateKeyDevId = ctx->devId;
  3590. }
  3591. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3592. /* Set the ID for the alternative key, too. User can still override that
  3593. * afterwards. */
  3594. ret = wolfSSL_CTX_use_AltPrivateKey_id(ctx, id, sz, devId, keySz);
  3595. #endif
  3596. }
  3597. return ret;
  3598. }
  3599. /* Load the id of a private key into SSL context and set key size.
  3600. *
  3601. * @param [in, out] ctx SSL context object.
  3602. * @param [in] id Buffer holding id.
  3603. * @param [in] sz Size of data in bytes.
  3604. * @param [in] devId Device identifier.
  3605. * @param [in] keySz Size of key.
  3606. * @return 1 on success.
  3607. * @return 0 on failure.
  3608. */
  3609. int wolfSSL_CTX_use_PrivateKey_id(WOLFSSL_CTX* ctx, const unsigned char* id,
  3610. long sz, int devId, long keySz)
  3611. {
  3612. int ret = wolfSSL_CTX_use_PrivateKey_Id(ctx, id, sz, devId);
  3613. if (ret == 1) {
  3614. /* Set the key size which normally is calculated during decoding. */
  3615. ctx->privateKeySz = (word32)keySz;
  3616. }
  3617. return ret;
  3618. }
  3619. /* Load the label name of a private key into SSL context.
  3620. *
  3621. * @param [in, out] ctx SSL context object.
  3622. * @param [in] label Buffer holding label.
  3623. * @param [in] devId Device identifier.
  3624. * @return 1 on success.
  3625. * @return 0 on failure.
  3626. */
  3627. int wolfSSL_CTX_use_PrivateKey_Label(WOLFSSL_CTX* ctx, const char* label,
  3628. int devId)
  3629. {
  3630. int ret = 1;
  3631. word32 sz = (word32)XSTRLEN(label) + 1;
  3632. /* Dispose of old private key and allocate and copy in label. */
  3633. FreeDer(&ctx->privateKey);
  3634. if (AllocCopyDer(&ctx->privateKey, (const byte*)label, (word32)sz,
  3635. PRIVATEKEY_TYPE, ctx->heap) != 0) {
  3636. ret = 0;
  3637. }
  3638. if (ret == 1) {
  3639. /* Private key is a label. */
  3640. ctx->privateKeyId = 0;
  3641. ctx->privateKeyLabel = 1;
  3642. /* Set private key device id to be one passed in or for SSL context. */
  3643. if (devId != INVALID_DEVID) {
  3644. ctx->privateKeyDevId = devId;
  3645. }
  3646. else {
  3647. ctx->privateKeyDevId = ctx->devId;
  3648. }
  3649. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3650. /* Set the ID for the alternative key, too. User can still override that
  3651. * afterwards. */
  3652. ret = wolfSSL_CTX_use_AltPrivateKey_Label(ctx, label, devId);
  3653. #endif
  3654. }
  3655. return ret;
  3656. }
  3657. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3658. int wolfSSL_CTX_use_AltPrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id,
  3659. long sz, int devId)
  3660. {
  3661. int ret = 1;
  3662. if ((ctx == NULL) || (id == NULL)) {
  3663. ret = 0;
  3664. }
  3665. if (ret == 1) {
  3666. FreeDer(&ctx->altPrivateKey);
  3667. if (AllocDer(&ctx->altPrivateKey, (word32)sz, ALT_PRIVATEKEY_TYPE,
  3668. ctx->heap) != 0) {
  3669. ret = 0;
  3670. }
  3671. }
  3672. if (ret == 1) {
  3673. XMEMCPY(ctx->altPrivateKey->buffer, id, sz);
  3674. ctx->altPrivateKeyId = 1;
  3675. if (devId != INVALID_DEVID) {
  3676. ctx->altPrivateKeyDevId = devId;
  3677. }
  3678. else {
  3679. ctx->altPrivateKeyDevId = ctx->devId;
  3680. }
  3681. }
  3682. return ret;
  3683. }
  3684. int wolfSSL_CTX_use_AltPrivateKey_id(WOLFSSL_CTX* ctx, const unsigned char* id,
  3685. long sz, int devId, long keySz)
  3686. {
  3687. int ret = wolfSSL_CTX_use_AltPrivateKey_Id(ctx, id, sz, devId);
  3688. if (ret == 1) {
  3689. ctx->altPrivateKeySz = (word32)keySz;
  3690. }
  3691. return ret;
  3692. }
  3693. int wolfSSL_CTX_use_AltPrivateKey_Label(WOLFSSL_CTX* ctx, const char* label,
  3694. int devId)
  3695. {
  3696. int ret = 1;
  3697. word32 sz;
  3698. if ((ctx == NULL) || (label == NULL)) {
  3699. ret = 0;
  3700. }
  3701. if (ret == 1) {
  3702. sz = (word32)XSTRLEN(label) + 1;
  3703. FreeDer(&ctx->altPrivateKey);
  3704. if (AllocDer(&ctx->altPrivateKey, (word32)sz, ALT_PRIVATEKEY_TYPE,
  3705. ctx->heap) != 0) {
  3706. ret = 0;
  3707. }
  3708. }
  3709. if (ret == 1) {
  3710. XMEMCPY(ctx->altPrivateKey->buffer, label, sz);
  3711. ctx->altPrivateKeyLabel = 1;
  3712. if (devId != INVALID_DEVID) {
  3713. ctx->altPrivateKeyDevId = devId;
  3714. }
  3715. else {
  3716. ctx->altPrivateKeyDevId = ctx->devId;
  3717. }
  3718. }
  3719. return ret;
  3720. }
  3721. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  3722. #endif /* WOLF_PRIVATE_KEY_ID */
  3723. /* Load a certificate chain in a buffer into SSL context.
  3724. *
  3725. * @param [in, out] ctx SSL context object.
  3726. * @param [in] in Buffer holding DER encoded certificate chain.
  3727. * @param [in] sz Size of data in bytes.
  3728. * @param [in] format Format of data:
  3729. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3730. * @return 1 on success.
  3731. * @return 0 on failure.
  3732. * @return Negative on error.
  3733. */
  3734. int wolfSSL_CTX_use_certificate_chain_buffer_format(WOLFSSL_CTX* ctx,
  3735. const unsigned char* in, long sz, int format)
  3736. {
  3737. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_buffer_format");
  3738. return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 1,
  3739. GET_VERIFY_SETTING_CTX(ctx));
  3740. }
  3741. /* Load a PEM encoded certificate chain in a buffer into SSL context.
  3742. *
  3743. * @param [in, out] ctx SSL context object.
  3744. * @param [in] in Buffer holding DER encoded certificate chain.
  3745. * @param [in] sz Size of data in bytes.
  3746. * @return 1 on success.
  3747. * @return 0 on failure.
  3748. * @return Negative on error.
  3749. */
  3750. int wolfSSL_CTX_use_certificate_chain_buffer(WOLFSSL_CTX* ctx,
  3751. const unsigned char* in, long sz)
  3752. {
  3753. return wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, in, sz,
  3754. WOLFSSL_FILETYPE_PEM);
  3755. }
  3756. /* Load a user certificate in a buffer into SSL.
  3757. *
  3758. * @param [in, out] ssl SSL object.
  3759. * @param [in] in Buffer holding user certificate.
  3760. * @param [in] sz Size of data in bytes.
  3761. * @param [in] format Format of data:
  3762. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3763. * @return 1 on success.
  3764. * @return 0 on failure.
  3765. * @return BAD_FUNC_ARG when ssl is NULL.
  3766. */
  3767. int wolfSSL_use_certificate_buffer(WOLFSSL* ssl, const unsigned char* in,
  3768. long sz, int format)
  3769. {
  3770. int ret;
  3771. WOLFSSL_ENTER("wolfSSL_use_certificate_buffer");
  3772. /* Validate parameters. */
  3773. if (ssl == NULL) {
  3774. ret = BAD_FUNC_ARG;
  3775. }
  3776. else {
  3777. ret = ProcessBuffer(ssl->ctx, in, sz, format, CERT_TYPE, ssl, NULL, 0,
  3778. GET_VERIFY_SETTING_SSL(ssl));
  3779. }
  3780. return ret;
  3781. }
  3782. /* Load a private key in a buffer into SSL.
  3783. *
  3784. * @param [in, out] ssl SSL object.
  3785. * @param [in] in Buffer holding private key.
  3786. * @param [in] sz Size of data in bytes.
  3787. * @param [in] format Format of data:
  3788. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  3789. * @return 1 on success.
  3790. * @return 0 on failure.
  3791. * @return BAD_FUNC_ARG when ssl is NULL.
  3792. */
  3793. int wolfSSL_use_PrivateKey_buffer(WOLFSSL* ssl, const unsigned char* in,
  3794. long sz, int format)
  3795. {
  3796. int ret;
  3797. long consumed = 0;
  3798. WOLFSSL_ENTER("wolfSSL_use_PrivateKey_buffer");
  3799. /* Validate parameters. */
  3800. if (ssl == NULL) {
  3801. ret = BAD_FUNC_ARG;
  3802. }
  3803. else {
  3804. ret = ProcessBuffer(ssl->ctx, in, sz, format, PRIVATEKEY_TYPE, ssl,
  3805. &consumed, 0, GET_VERIFY_SETTING_SSL(ssl));
  3806. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3807. if ((ret == 1) && (consumed < sz)) {
  3808. /* When support for dual algorithm certificates is enabled, the
  3809. * buffer may contain both the primary and the alternative
  3810. * private key. Hence, we have to parse both of them.
  3811. */
  3812. ret = ProcessBuffer(ssl->ctx, in + consumed, sz - consumed, format,
  3813. ALT_PRIVATEKEY_TYPE, ssl, NULL, 0, GET_VERIFY_SETTING_SSL(ssl));
  3814. }
  3815. #endif
  3816. }
  3817. return ret;
  3818. }
  3819. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3820. int wolfSSL_use_AltPrivateKey_buffer(WOLFSSL* ssl, const unsigned char* in,
  3821. long sz, int format)
  3822. {
  3823. int ret;
  3824. WOLFSSL_ENTER("wolfSSL_use_AltPrivateKey_buffer");
  3825. ret = ProcessBuffer(ssl->ctx, in, sz, format, ALT_PRIVATEKEY_TYPE, ssl,
  3826. NULL, 0, GET_VERIFY_SETTING_SSL(ssl));
  3827. WOLFSSL_LEAVE("wolfSSL_use_AltPrivateKey_buffer", ret);
  3828. return ret;
  3829. }
  3830. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  3831. #ifdef WOLF_PRIVATE_KEY_ID
  3832. /* Load the id of a private key into SSL.
  3833. *
  3834. * @param [in, out] ssl SSL object.
  3835. * @param [in] id Buffer holding id.
  3836. * @param [in] sz Size of data in bytes.
  3837. * @param [in] devId Device identifier.
  3838. * @return 1 on success.
  3839. * @return 0 on failure.
  3840. */
  3841. int wolfSSL_use_PrivateKey_Id(WOLFSSL* ssl, const unsigned char* id,
  3842. long sz, int devId)
  3843. {
  3844. int ret = 1;
  3845. /* Dispose of old private key if owned and allocate and copy in id. */
  3846. if (ssl->buffers.weOwnKey) {
  3847. FreeDer(&ssl->buffers.key);
  3848. }
  3849. if (AllocCopyDer(&ssl->buffers.key, id, (word32)sz, PRIVATEKEY_TYPE,
  3850. ssl->heap) != 0) {
  3851. ret = 0;
  3852. }
  3853. if (ret == 1) {
  3854. /* Buffer now ours. */
  3855. ssl->buffers.weOwnKey = 1;
  3856. /* Private key is an id. */
  3857. ssl->buffers.keyId = 1;
  3858. ssl->buffers.keyLabel = 0;
  3859. /* Set private key device id to be one passed in or for SSL. */
  3860. if (devId != INVALID_DEVID) {
  3861. ssl->buffers.keyDevId = devId;
  3862. }
  3863. else {
  3864. ssl->buffers.keyDevId = ssl->devId;
  3865. }
  3866. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3867. /* Set the ID for the alternative key, too. User can still override that
  3868. * afterwards. */
  3869. ret = wolfSSL_use_AltPrivateKey_Id(ssl, id, sz, devId);
  3870. #endif
  3871. }
  3872. return ret;
  3873. }
  3874. /* Load the id of a private key into SSL and set key size.
  3875. *
  3876. * @param [in, out] ssl SSL object.
  3877. * @param [in] id Buffer holding id.
  3878. * @param [in] sz Size of data in bytes.
  3879. * @param [in] devId Device identifier.
  3880. * @param [in] keySz Size of key.
  3881. * @return 1 on success.
  3882. * @return 0 on failure.
  3883. */
  3884. int wolfSSL_use_PrivateKey_id(WOLFSSL* ssl, const unsigned char* id,
  3885. long sz, int devId, long keySz)
  3886. {
  3887. int ret = wolfSSL_use_PrivateKey_Id(ssl, id, sz, devId);
  3888. if (ret == 1) {
  3889. /* Set the key size which normally is calculated during decoding. */
  3890. ssl->buffers.keySz = (word32)keySz;
  3891. }
  3892. return ret;
  3893. }
  3894. /* Load the label name of a private key into SSL.
  3895. *
  3896. * @param [in, out] ssl SSL object.
  3897. * @param [in] label Buffer holding label.
  3898. * @param [in] devId Device identifier.
  3899. * @return 1 on success.
  3900. * @return 0 on failure.
  3901. */
  3902. int wolfSSL_use_PrivateKey_Label(WOLFSSL* ssl, const char* label, int devId)
  3903. {
  3904. int ret = 1;
  3905. word32 sz = (word32)XSTRLEN(label) + 1;
  3906. /* Dispose of old private key if owned and allocate and copy in label. */
  3907. if (ssl->buffers.weOwnKey) {
  3908. FreeDer(&ssl->buffers.key);
  3909. }
  3910. if (AllocCopyDer(&ssl->buffers.key, (const byte*)label, (word32)sz,
  3911. PRIVATEKEY_TYPE, ssl->heap) != 0) {
  3912. ret = 0;
  3913. }
  3914. if (ret == 1) {
  3915. /* Buffer now ours. */
  3916. ssl->buffers.weOwnKey = 1;
  3917. /* Private key is a label. */
  3918. ssl->buffers.keyId = 0;
  3919. ssl->buffers.keyLabel = 1;
  3920. /* Set private key device id to be one passed in or for SSL. */
  3921. if (devId != INVALID_DEVID) {
  3922. ssl->buffers.keyDevId = devId;
  3923. }
  3924. else {
  3925. ssl->buffers.keyDevId = ssl->devId;
  3926. }
  3927. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3928. /* Set the label for the alternative key, too. User can still override
  3929. * that afterwards. */
  3930. ret = wolfSSL_use_AltPrivateKey_Label(ssl, label, devId);
  3931. #endif
  3932. }
  3933. return ret;
  3934. }
  3935. #ifdef WOLFSSL_DUAL_ALG_CERTS
  3936. int wolfSSL_use_AltPrivateKey_Id(WOLFSSL* ssl, const unsigned char* id, long sz,
  3937. int devId)
  3938. {
  3939. int ret = 1;
  3940. if ((ssl == NULL) || (id == NULL)) {
  3941. ret = 0;
  3942. }
  3943. if (ret == 1) {
  3944. if (ssl->buffers.weOwnAltKey) {
  3945. FreeDer(&ssl->buffers.altKey);
  3946. }
  3947. if (AllocDer(&ssl->buffers.altKey, (word32)sz, ALT_PRIVATEKEY_TYPE,
  3948. ssl->heap) == 0) {
  3949. ret = 0;
  3950. }
  3951. }
  3952. if (ret == 1) {
  3953. XMEMCPY(ssl->buffers.altKey->buffer, id, sz);
  3954. ssl->buffers.weOwnAltKey = 1;
  3955. ssl->buffers.altKeyId = 1;
  3956. if (devId != INVALID_DEVID) {
  3957. ssl->buffers.altKeyDevId = devId;
  3958. }
  3959. else {
  3960. ssl->buffers.altKeyDevId = ssl->devId;
  3961. }
  3962. }
  3963. return ret;
  3964. }
  3965. int wolfSSL_use_AltPrivateKey_id(WOLFSSL* ssl, const unsigned char* id, long sz,
  3966. int devId, long keySz)
  3967. {
  3968. int ret = wolfSSL_use_AltPrivateKey_Id(ssl, id, sz, devId);
  3969. if (ret == 1) {
  3970. ssl->buffers.altKeySz = (word32)keySz;
  3971. }
  3972. return ret;
  3973. }
  3974. int wolfSSL_use_AltPrivateKey_Label(WOLFSSL* ssl, const char* label, int devId)
  3975. {
  3976. int ret = 1;
  3977. word32 sz;
  3978. if ((ssl == NULL) || (label == NULL)) {
  3979. ret = 0;
  3980. }
  3981. if (ret == 1) {
  3982. sz = (word32)XSTRLEN(label) + 1;
  3983. if (ssl->buffers.weOwnAltKey)
  3984. FreeDer(&ssl->buffers.altKey);
  3985. if (AllocDer(&ssl->buffers.altKey, (word32)sz, ALT_PRIVATEKEY_TYPE,
  3986. ssl->heap) == 0) {
  3987. ret = 0;
  3988. }
  3989. }
  3990. if (ret == 1) {
  3991. XMEMCPY(ssl->buffers.altKey->buffer, label, sz);
  3992. ssl->buffers.weOwnAltKey = 1;
  3993. ssl->buffers.altKeyLabel = 1;
  3994. if (devId != INVALID_DEVID) {
  3995. ssl->buffers.altKeyDevId = devId;
  3996. }
  3997. else {
  3998. ssl->buffers.altKeyDevId = ssl->devId;
  3999. }
  4000. }
  4001. return ret;
  4002. }
  4003. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  4004. #endif /* WOLF_PRIVATE_KEY_ID */
  4005. /* Load a certificate chain in a buffer into SSL.
  4006. *
  4007. * @param [in, out] ssl SSL object.
  4008. * @param [in] in Buffer holding DER encoded certificate chain.
  4009. * @param [in] sz Size of data in bytes.
  4010. * @param [in] format Format of data:
  4011. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  4012. * @return 1 on success.
  4013. * @return 0 on failure.
  4014. * @return BAD_FUNC_ARG when ssl is NULL.
  4015. */
  4016. int wolfSSL_use_certificate_chain_buffer_format(WOLFSSL* ssl,
  4017. const unsigned char* in, long sz, int format)
  4018. {
  4019. int ret;
  4020. WOLFSSL_ENTER("wolfSSL_use_certificate_chain_buffer_format");
  4021. /* Validate parameters. */
  4022. if (ssl == NULL) {
  4023. ret = BAD_FUNC_ARG;
  4024. }
  4025. else {
  4026. ret = ProcessBuffer(ssl->ctx, in, sz, format, CERT_TYPE, ssl, NULL, 1,
  4027. GET_VERIFY_SETTING_SSL(ssl));
  4028. }
  4029. return ret;
  4030. }
  4031. /* Load a PEM encoded certificate chain in a buffer into SSL.
  4032. *
  4033. * @param [in, out] ssl SSL object.
  4034. * @param [in] in Buffer holding DER encoded certificate chain.
  4035. * @param [in] sz Size of data in bytes.
  4036. * @return 1 on success.
  4037. * @return 0 on failure.
  4038. * @return Negative on error.
  4039. */
  4040. int wolfSSL_use_certificate_chain_buffer(WOLFSSL* ssl, const unsigned char* in,
  4041. long sz)
  4042. {
  4043. return wolfSSL_use_certificate_chain_buffer_format(ssl, in, sz,
  4044. WOLFSSL_FILETYPE_PEM);
  4045. }
  4046. #if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \
  4047. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \
  4048. defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \
  4049. defined(WOLFSSL_HAPROXY)
  4050. /* Add certificate to chain.
  4051. *
  4052. * @param [in, out] chain Buffer holding encoded certificate for TLS.
  4053. * @param [in] weOwn Indicates we need to free chain if repleced.
  4054. * @param [in] cert Buffer holding DER encoded certificate.
  4055. * @param [in] certSz Size of DER encoded certificate in bytes.
  4056. * @param [in] heap Dynamic memory allocation hint.
  4057. * @return 1 on success.
  4058. * @return 0 on failure.
  4059. */
  4060. static int wolfssl_add_to_chain(DerBuffer** chain, int weOwn, const byte* cert,
  4061. word32 certSz, void* heap)
  4062. {
  4063. int res = 1;
  4064. int ret;
  4065. DerBuffer* oldChain = *chain;
  4066. DerBuffer* newChain = NULL;
  4067. word32 len = 0;
  4068. if (oldChain != NULL) {
  4069. /* Get length of previous chain. */
  4070. len = oldChain->length;
  4071. }
  4072. /* Allocate DER buffer bug enough to hold old and new certificates. */
  4073. ret = AllocDer(&newChain, len + CERT_HEADER_SZ + certSz, CERT_TYPE, heap);
  4074. if (ret != 0) {
  4075. WOLFSSL_MSG("AllocDer error");
  4076. res = 0;
  4077. }
  4078. if (res == 1) {
  4079. if (oldChain != NULL) {
  4080. /* Place old chain in new buffer. */
  4081. XMEMCPY(newChain->buffer, oldChain->buffer, len);
  4082. }
  4083. /* Append length and DER encoded certificate. */
  4084. c32to24(certSz, newChain->buffer + len);
  4085. XMEMCPY(newChain->buffer + len + CERT_HEADER_SZ, cert, certSz);
  4086. /* Dispose of old chain if we own it. */
  4087. if (weOwn) {
  4088. FreeDer(chain);
  4089. }
  4090. /* Replace chain. */
  4091. *chain = newChain;
  4092. }
  4093. return res;
  4094. }
  4095. #endif
  4096. #ifdef OPENSSL_EXTRA
  4097. /* Add a certificate to end of chain sent in TLS handshake.
  4098. *
  4099. * @param [in, out] ctx SSL context.
  4100. * @param [in] der Buffer holding DER encoded certificate.
  4101. * @param [in] derSz Size of data in buffer.
  4102. * @return 1 on success.
  4103. * @return 0 on failure.
  4104. */
  4105. static int wolfssl_ctx_add_to_chain(WOLFSSL_CTX* ctx, const byte* der,
  4106. int derSz)
  4107. {
  4108. int res = 1;
  4109. int ret;
  4110. DerBuffer* derBuffer = NULL;
  4111. /* Create a DER buffer from DER encoding. */
  4112. ret = AllocCopyDer(&derBuffer, der, derSz, CERT_TYPE, ctx->heap);
  4113. if (ret != 0) {
  4114. WOLFSSL_MSG("Memory Error");
  4115. res = 0;
  4116. }
  4117. if (res == 1) {
  4118. /* Add a user CA certificate to the certificate manager. */
  4119. res = AddCA(ctx->cm, &derBuffer, WOLFSSL_USER_CA,
  4120. GET_VERIFY_SETTING_CTX(ctx));
  4121. if (res != 1) {
  4122. res = 0;
  4123. }
  4124. }
  4125. if (res == 1) {
  4126. /* Add chain to DER buffer. */
  4127. res = wolfssl_add_to_chain(&ctx->certChain, 1, der, derSz, ctx->heap);
  4128. #ifdef WOLFSSL_TLS13
  4129. /* Update count of certificates. */
  4130. ctx->certChainCnt++;
  4131. #endif
  4132. }
  4133. return res;
  4134. }
  4135. /* Add a certificate to chain sent in TLS handshake.
  4136. *
  4137. * @param [in, out] ctx SSL context.
  4138. * @param [in] x509 X509 certificate object.
  4139. * @return 1 on success.
  4140. * @return 0 on failure.
  4141. */
  4142. long wolfSSL_CTX_add_extra_chain_cert(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509)
  4143. {
  4144. int ret = 1;
  4145. int derSz = 0;
  4146. const byte* der = NULL;
  4147. WOLFSSL_ENTER("wolfSSL_CTX_add_extra_chain_cert");
  4148. /* Validate parameters. */
  4149. if ((ctx == NULL) || (x509 == NULL)) {
  4150. WOLFSSL_MSG("Bad Argument");
  4151. ret = 0;
  4152. }
  4153. if (ret == 1) {
  4154. /* Get the DER encoding of the certificate from the X509 object. */
  4155. der = wolfSSL_X509_get_der(x509, &derSz);
  4156. /* Validate buffer. */
  4157. if ((der == NULL) || (derSz <= 0)) {
  4158. WOLFSSL_MSG("Error getting X509 DER");
  4159. ret = 0;
  4160. }
  4161. }
  4162. if ((ret == 1) && (ctx->certificate == NULL)) {
  4163. WOLFSSL_ENTER("wolfSSL_use_certificate_chain_buffer_format");
  4164. /* Process buffer makes first certificate the leaf. */
  4165. ret = ProcessBuffer(ctx, der, derSz, WOLFSSL_FILETYPE_ASN1, CERT_TYPE,
  4166. NULL, NULL, 1, GET_VERIFY_SETTING_CTX(ctx));
  4167. if (ret != 1) {
  4168. ret = 0;
  4169. }
  4170. }
  4171. else if (ret == 1) {
  4172. /* Add certificate to existing chain. */
  4173. ret = wolfssl_ctx_add_to_chain(ctx, der, derSz);
  4174. }
  4175. if (ret == 1) {
  4176. /* On success WOLFSSL_X509 memory is responsibility of SSL context. */
  4177. wolfSSL_X509_free(x509);
  4178. }
  4179. WOLFSSL_LEAVE("wolfSSL_CTX_add_extra_chain_cert", ret);
  4180. return ret;
  4181. }
  4182. #endif /* OPENSSL_EXTRA */
  4183. #if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \
  4184. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \
  4185. defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \
  4186. defined(WOLFSSL_HAPROXY)
  4187. /* Load a certificate into SSL context.
  4188. *
  4189. * @param [in, out] ctx SSL context object.
  4190. * @param [in] x509 X509 certificate object.
  4191. * @return 1 on success.
  4192. * @return 0 on failure.
  4193. */
  4194. int wolfSSL_CTX_use_certificate(WOLFSSL_CTX *ctx, WOLFSSL_X509 *x)
  4195. {
  4196. int res = 1;
  4197. int ret;
  4198. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate");
  4199. /* Validate parameters. */
  4200. if ((ctx == NULL) || (x == NULL) || (x->derCert == NULL)) {
  4201. WOLFSSL_MSG("Bad parameter");
  4202. res = 0;
  4203. }
  4204. if (res == 1) {
  4205. /* Replace certificate buffer with one holding the new certificate. */
  4206. FreeDer(&ctx->certificate);
  4207. ret = AllocCopyDer(&ctx->certificate, x->derCert->buffer,
  4208. x->derCert->length, CERT_TYPE, ctx->heap);
  4209. if (ret != 0) {
  4210. res = 0;
  4211. }
  4212. }
  4213. #ifdef KEEP_OUR_CERT
  4214. if (res == 1) {
  4215. /* Dispose of our certificate if it is ours. */
  4216. if ((ctx->ourCert != NULL) && ctx->ownOurCert) {
  4217. wolfSSL_X509_free(ctx->ourCert);
  4218. }
  4219. #ifndef WOLFSSL_X509_STORE_CERTS
  4220. /* Keep a reference to the new certificate. */
  4221. ctx->ourCert = x;
  4222. if (wolfSSL_X509_up_ref(x) != 1) {
  4223. res = 0;
  4224. }
  4225. #else
  4226. /* Keep a copy of the new certificate. */
  4227. ctx->ourCert = wolfSSL_X509_d2i_ex(NULL, x->derCert->buffer,
  4228. x->derCert->length, ctx->heap);
  4229. if (ctx->ourCert == NULL) {
  4230. res = 0;
  4231. }
  4232. #endif
  4233. /* Now own our certificate. */
  4234. ctx->ownOurCert = 1;
  4235. }
  4236. #endif
  4237. if (res == 1) {
  4238. /* Set have options based on public key OID. */
  4239. wolfssl_set_have_from_key_oid(ctx, NULL, x->pubKeyOID);
  4240. }
  4241. return res;
  4242. }
  4243. /* Add the certificate to the chain in the SSL context and own the X509 object.
  4244. *
  4245. * @param [in, out] ctx SSL context object.
  4246. * @param [in] x509 X509 certificate object.
  4247. * @return 1 on success.
  4248. * @return 0 on failure.
  4249. */
  4250. int wolfSSL_CTX_add0_chain_cert(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509)
  4251. {
  4252. int ret;
  4253. WOLFSSL_ENTER("wolfSSL_CTX_add0_chain_cert");
  4254. /* Add certificate to chain and copy or up reference it. */
  4255. ret = wolfSSL_CTX_add1_chain_cert(ctx, x509);
  4256. if (ret == 1) {
  4257. /* Down reference or free original now as we own certificate. */
  4258. wolfSSL_X509_free(x509);
  4259. }
  4260. return ret;
  4261. }
  4262. /* Add the certificate to the chain in the SSL context.
  4263. *
  4264. * X509 object copied or up referenced.
  4265. *
  4266. * @param [in, out] ctx SSL context object.
  4267. * @param [in] x509 X509 certificate object.
  4268. * @return 1 on success.
  4269. * @return 0 on failure.
  4270. */
  4271. int wolfSSL_CTX_add1_chain_cert(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509)
  4272. {
  4273. int ret = 1;
  4274. WOLFSSL_ENTER("wolfSSL_CTX_add1_chain_cert");
  4275. /* Validate parameters. */
  4276. if ((ctx == NULL) || (x509 == NULL) || (x509->derCert == NULL)) {
  4277. ret = 0;
  4278. }
  4279. /* Check if we already have set a certificate. */
  4280. if ((ret == 1) && (ctx->certificate == NULL)) {
  4281. /* Use the certificate. */
  4282. ret = wolfSSL_CTX_use_certificate(ctx, x509);
  4283. }
  4284. /* Increate reference count as we will store it. */
  4285. else if ((ret == 1) && ((ret = wolfSSL_X509_up_ref(x509)) == 1)) {
  4286. /* Load the DER encoding. */
  4287. ret = wolfSSL_CTX_load_verify_buffer(ctx, x509->derCert->buffer,
  4288. x509->derCert->length, WOLFSSL_FILETYPE_ASN1);
  4289. if (ret == 1) {
  4290. /* Add DER encoding to chain. */
  4291. ret = wolfssl_add_to_chain(&ctx->certChain, 1,
  4292. x509->derCert->buffer, x509->derCert->length, ctx->heap);
  4293. }
  4294. /* Store cert in stack to free it later. */
  4295. if ((ret == 1) && (ctx->x509Chain == NULL)) {
  4296. /* Create a stack for certificates. */
  4297. ctx->x509Chain = wolfSSL_sk_X509_new_null();
  4298. if (ctx->x509Chain == NULL) {
  4299. WOLFSSL_MSG("wolfSSL_sk_X509_new_null error");
  4300. ret = 0;
  4301. }
  4302. }
  4303. if (ret == 1) {
  4304. /* Push the X509 object onto stack. */
  4305. ret = wolfSSL_sk_X509_push(ctx->x509Chain, x509);
  4306. }
  4307. if (ret != 1) {
  4308. /* Decrease reference count on error as we didn't store it. */
  4309. wolfSSL_X509_free(x509);
  4310. }
  4311. }
  4312. return WS_RC(ret);
  4313. }
  4314. #ifdef KEEP_OUR_CERT
  4315. /* Add the certificate to the chain in the SSL and own the X509 object.
  4316. *
  4317. * @param [in, out] ssl SSL object.
  4318. * @param [in] x509 X509 certificate object.
  4319. * @return 1 on success.
  4320. * @return 0 on failure.
  4321. */
  4322. int wolfSSL_add0_chain_cert(WOLFSSL* ssl, WOLFSSL_X509* x509)
  4323. {
  4324. int ret = 1;
  4325. WOLFSSL_ENTER("wolfSSL_add0_chain_cert");
  4326. /* Validate parameters. */
  4327. if ((ssl == NULL) || (ssl->ctx == NULL) || (x509 == NULL) ||
  4328. (x509->derCert == NULL)) {
  4329. ret = 0;
  4330. }
  4331. /* Check if we already have set a certificate. */
  4332. if ((ret == 1) && (ssl->buffers.certificate == NULL)) {
  4333. /* Use the certificate. */
  4334. ret = wolfSSL_use_certificate(ssl, x509);
  4335. if (ret == 1) {
  4336. /* Dispose of old certificate if we own it. */
  4337. if (ssl->buffers.weOwnCert) {
  4338. wolfSSL_X509_free(ssl->ourCert);
  4339. }
  4340. /* Store cert to free it later. */
  4341. ssl->ourCert = x509;
  4342. ssl->buffers.weOwnCert = 1;
  4343. }
  4344. }
  4345. else if (ret == 1) {
  4346. /* Add DER encoding to chain. */
  4347. ret = wolfssl_add_to_chain(&ssl->buffers.certChain,
  4348. ssl->buffers.weOwnCertChain, x509->derCert->buffer,
  4349. x509->derCert->length, ssl->heap);
  4350. if (ret == 1) {
  4351. /* We now own cert chain. */
  4352. ssl->buffers.weOwnCertChain = 1;
  4353. /* Create a stack to put certificate into. */
  4354. if (ssl->ourCertChain == NULL) {
  4355. ssl->ourCertChain = wolfSSL_sk_X509_new_null();
  4356. if (ssl->ourCertChain == NULL) {
  4357. WOLFSSL_MSG("wolfSSL_sk_X509_new_null error");
  4358. ret = 0;
  4359. }
  4360. }
  4361. }
  4362. if (ret == 1) {
  4363. /* Push X509 object onto stack to be freed. */
  4364. ret = wolfSSL_sk_X509_push(ssl->ourCertChain, x509);
  4365. if (ret != 1) {
  4366. /* Free it now on error. */
  4367. wolfSSL_X509_free(x509);
  4368. }
  4369. }
  4370. }
  4371. return WS_RC(ret);
  4372. }
  4373. /* Add the certificate to the chain in the SSL.
  4374. *
  4375. * X509 object is up referenced.
  4376. *
  4377. * @param [in, out] ssl SSL object.
  4378. * @param [in] x509 X509 certificate object.
  4379. * @return 1 on success.
  4380. * @return 0 on failure.
  4381. */
  4382. int wolfSSL_add1_chain_cert(WOLFSSL* ssl, WOLFSSL_X509* x509)
  4383. {
  4384. int ret = 1;
  4385. WOLFSSL_ENTER("wolfSSL_add1_chain_cert");
  4386. /* Validate parameters. */
  4387. if ((ssl == NULL) || (ssl->ctx == NULL) || (x509 == NULL) ||
  4388. (x509->derCert == NULL)) {
  4389. ret = 0;
  4390. }
  4391. /* Increase reference count on X509 object before adding. */
  4392. if ((ret == 1) && ((ret == wolfSSL_X509_up_ref(x509)) == 1)) {
  4393. /* Add this to the chain. */
  4394. if ((ret = wolfSSL_add0_chain_cert(ssl, x509)) != 1) {
  4395. /* Decrease reference count on error as not stored. */
  4396. wolfSSL_X509_free(x509);
  4397. }
  4398. }
  4399. return ret;
  4400. }
  4401. #endif /* KEEP_OUR_CERT */
  4402. #endif /* OPENSSL_EXTRA, HAVE_LIGHTY, WOLFSSL_MYSQL_COMPATIBLE, HAVE_STUNNEL,
  4403. WOLFSSL_NGINX, HAVE_POCO_LIB, WOLFSSL_HAPROXY */
  4404. #ifdef OPENSSL_EXTRA
  4405. /* Load a private key into SSL context.
  4406. *
  4407. * @param [in, out] ctx SSL context object.
  4408. * @param [in] pkey EVP private key.
  4409. * @return 1 on success.
  4410. * @return 0 on failure.
  4411. */
  4412. int wolfSSL_CTX_use_PrivateKey(WOLFSSL_CTX *ctx, WOLFSSL_EVP_PKEY *pkey)
  4413. {
  4414. int ret = 1;
  4415. WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey");
  4416. /* Validate parameters. */
  4417. if ((ctx == NULL) || (pkey == NULL) || (pkey->pkey.ptr == NULL)) {
  4418. ret = 0;
  4419. }
  4420. if (ret == 1) {
  4421. switch (pkey->type) {
  4422. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  4423. case EVP_PKEY_RSA:
  4424. WOLFSSL_MSG("populating RSA key");
  4425. ret = PopulateRSAEvpPkeyDer(pkey);
  4426. break;
  4427. #endif /* (WOLFSSL_KEY_GEN || OPENSSL_EXTRA) && !NO_RSA */
  4428. #if !defined(HAVE_SELFTEST) && (defined(WOLFSSL_KEY_GEN) || \
  4429. defined(WOLFSSL_CERT_GEN)) && !defined(NO_DSA)
  4430. case EVP_PKEY_DSA:
  4431. break;
  4432. #endif /* !HAVE_SELFTEST && (WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN) &&
  4433. * !NO_DSA */
  4434. #ifdef HAVE_ECC
  4435. case EVP_PKEY_EC:
  4436. WOLFSSL_MSG("populating ECC key");
  4437. ret = ECC_populate_EVP_PKEY(pkey, pkey->ecc);
  4438. break;
  4439. #endif
  4440. default:
  4441. ret = 0;
  4442. }
  4443. }
  4444. if (ret == 1) {
  4445. /* ptr for WOLFSSL_EVP_PKEY struct is expected to be DER format */
  4446. ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx,
  4447. (const unsigned char*)pkey->pkey.ptr, pkey->pkey_sz,
  4448. SSL_FILETYPE_ASN1);
  4449. }
  4450. return ret;
  4451. }
  4452. #endif /* OPENSSL_EXTRA */
  4453. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || \
  4454. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_QT)
  4455. /* Load a DER encoded certificate in a buffer into SSL context.
  4456. *
  4457. * @param [in, out] ctx SSL context object.
  4458. * @param [in] der Buffer holding DER encoded certificate.
  4459. * @param [in] derSz Size of data in bytes.
  4460. * @return 1 on success.
  4461. * @return 0 on failure.
  4462. */
  4463. int wolfSSL_CTX_use_certificate_ASN1(WOLFSSL_CTX *ctx, int derSz,
  4464. const unsigned char *der)
  4465. {
  4466. int ret = 1;
  4467. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_ASN1");
  4468. /* Validate parameters. */
  4469. if ((ctx == NULL) || (der == NULL)) {
  4470. ret = 0;
  4471. }
  4472. /* Load DER encoded cerificate into SSL context. */
  4473. if ((ret == 1) && (wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  4474. WOLFSSL_FILETYPE_ASN1) != 1)) {
  4475. ret = 0;
  4476. }
  4477. return ret;
  4478. }
  4479. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  4480. /* Load an RSA private key into SSL context.
  4481. *
  4482. * @param [in, out] ctx SSL context object.
  4483. * @param [in] rsa RSA private key.
  4484. * @return 1 on success.
  4485. * @return 0 on failure.
  4486. * @return BAD_FUNC_ARG when ctx or rsa is NULL.
  4487. * @return MEMORY_E when dynamic memory allocation fails.
  4488. */
  4489. int wolfSSL_CTX_use_RSAPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL_RSA* rsa)
  4490. {
  4491. int ret = 1;
  4492. int derSize;
  4493. unsigned char* der = NULL;
  4494. unsigned char* p;
  4495. WOLFSSL_ENTER("wolfSSL_CTX_use_RSAPrivateKey");
  4496. /* Validate parameters. */
  4497. if ((ctx == NULL) || (rsa == NULL)) {
  4498. WOLFSSL_MSG("one or more inputs were NULL");
  4499. ret = BAD_FUNC_ARG;
  4500. }
  4501. /* Get DER encoding size. */
  4502. if ((ret == 1) && ((derSize = wolfSSL_i2d_RSAPrivateKey(rsa, NULL)) <= 0)) {
  4503. ret = 0;
  4504. }
  4505. if (ret == 1) {
  4506. /* Allocate memory to hold DER encoding.. */
  4507. der = (unsigned char*)XMALLOC(derSize, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4508. if (der == NULL) {
  4509. WOLFSSL_MSG("Malloc failure");
  4510. ret = MEMORY_E;
  4511. }
  4512. }
  4513. if (ret == 1) {
  4514. /* Pointer passed in is modified.. */
  4515. p = der;
  4516. /* Encode the RSA key as DER into buffer and get size. */
  4517. if ((derSize = wolfSSL_i2d_RSAPrivateKey(rsa, &p)) <= 0) {
  4518. WOLFSSL_MSG("wolfSSL_i2d_RSAPrivateKey() failure");
  4519. ret = 0;
  4520. }
  4521. }
  4522. if (ret == 1) {
  4523. /* Load DER encoded cerificate into SSL context. */
  4524. ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, derSize,
  4525. SSL_FILETYPE_ASN1);
  4526. if (ret != WOLFSSL_SUCCESS) {
  4527. WOLFSSL_MSG("wolfSSL_CTX_USE_PrivateKey_buffer() failure");
  4528. ret = 0;
  4529. }
  4530. }
  4531. /* Dispos of dynamically allocated data. */
  4532. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4533. return ret;
  4534. }
  4535. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  4536. #endif /* OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY || WOLFSSL_QT */
  4537. #endif /* !NO_CERTS */
  4538. #ifdef OPENSSL_EXTRA
  4539. #ifdef WOLFSSL_SYS_CA_CERTS
  4540. /* Use the default paths to look for CA certificate.
  4541. *
  4542. * This is an OpenSSL compatibility layer function, but it doesn't mirror
  4543. * the exact functionality of its OpenSSL counterpart. We don't support the
  4544. * notion of an "OpenSSL directory". This function will attempt to load the
  4545. * environment variables SSL_CERT_DIR and SSL_CERT_FILE, if either are
  4546. * found, they will be loaded. Otherwise, it will act as a wrapper around
  4547. * our native wolfSSL_CTX_load_system_CA_certs function. This function does
  4548. * conform to OpenSSL's return value conventions.
  4549. *
  4550. * @param [in] ctx SSL context object.
  4551. * @return 1 on success.
  4552. * @return 0 on failure.
  4553. * @return WOLFSSL_FATAL_ERROR when using a filesystem is not supported.
  4554. */
  4555. int wolfSSL_CTX_set_default_verify_paths(WOLFSSL_CTX* ctx)
  4556. {
  4557. int ret;
  4558. #ifdef XGETENV
  4559. char* certDir;
  4560. char* certFile;
  4561. word32 flags;
  4562. #endif
  4563. WOLFSSL_ENTER("wolfSSL_CTX_set_default_verify_paths");
  4564. #ifdef XGETENV
  4565. certDir = XGETENV("SSL_CERT_DIR");
  4566. certFile = XGETENV("SSL_CERT_FILE");
  4567. flags = WOLFSSL_LOAD_FLAG_PEM_CA_ONLY;
  4568. if ((certDir != NULL) || (certFile != NULL)) {
  4569. if (certDir != NULL) {
  4570. /* We want to keep trying to load more CA certs even if one cert in
  4571. * the directory is bad and can't be used (e.g. if one is
  4572. * expired), so we use WOLFSSL_LOAD_FLAG_IGNORE_ERR.
  4573. */
  4574. flags |= WOLFSSL_LOAD_FLAG_IGNORE_ERR;
  4575. }
  4576. /* Load CA certificates from environment variable locations. */
  4577. ret = wolfSSL_CTX_load_verify_locations_ex(ctx, certFile, certDir,
  4578. flags);
  4579. if (ret != 1) {
  4580. WOLFSSL_MSG_EX("Failed to load CA certs from SSL_CERT_FILE: %s"
  4581. " SSL_CERT_DIR: %s. Error: %d", certFile,
  4582. certDir, ret);
  4583. ret = 0;
  4584. }
  4585. }
  4586. else
  4587. #endif
  4588. {
  4589. #ifdef NO_FILESYSTEM
  4590. WOLFSSL_MSG("wolfSSL_CTX_set_default_verify_paths not supported"
  4591. " with NO_FILESYSTEM enabled");
  4592. ret = WOLFSSL_FATAL_ERROR;
  4593. #else
  4594. /* Load the system CA certificates. */
  4595. ret = wolfSSL_CTX_load_system_CA_certs(ctx);
  4596. if (ret == WOLFSSL_BAD_PATH) {
  4597. /* OpenSSL doesn't treat the lack of a system CA cert directory as a
  4598. * failure. We do the same here.
  4599. */
  4600. ret = 1;
  4601. }
  4602. #endif
  4603. }
  4604. WOLFSSL_LEAVE("wolfSSL_CTX_set_default_verify_paths", ret);
  4605. return ret;
  4606. }
  4607. #endif /* WOLFSSL_SYS_CA_CERTS */
  4608. #endif /* OPENSSL_EXTRA */
  4609. #ifndef NO_DH
  4610. /* Set the temporary DH parameters against the SSL.
  4611. *
  4612. * @param [in, out] ssl SSL object.
  4613. * @param [in] p Buffer holding prime.
  4614. * @param [in] pSz Length of prime in bytes.
  4615. * @param [in] g Buffer holding generator.
  4616. * @param [in] gSz Length of generator in bytes.
  4617. * @return 1 on success.
  4618. * @return 0 on failure.
  4619. * @return DH_KEY_SIZE_E when the prime is too short or long.
  4620. * @return SIDE_ERROR when the SSL is for a client.
  4621. */
  4622. static int wolfssl_set_tmp_dh(WOLFSSL* ssl, unsigned char* p, int pSz,
  4623. unsigned char* g, int gSz)
  4624. {
  4625. int ret = 1;
  4626. /* Check the size of the prime meets the requirements of the SSL. */
  4627. if (((word16)pSz < ssl->options.minDhKeySz) ||
  4628. ((word16)pSz > ssl->options.maxDhKeySz)) {
  4629. ret = DH_KEY_SIZE_E;
  4630. }
  4631. /* Only able to set DH parameters on server. */
  4632. if ((ret == 1) && (ssl->options.side == WOLFSSL_CLIENT_END)) {
  4633. ret = SIDE_ERROR;
  4634. }
  4635. if (ret == 1) {
  4636. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
  4637. !defined(HAVE_SELFTEST)
  4638. /* New DH parameters not tested for validity. */
  4639. ssl->options.dhKeyTested = 0;
  4640. /* New DH parameters must be tested for validity before use. */
  4641. ssl->options.dhDoKeyTest = 1;
  4642. #endif
  4643. /* Dispose of old DH parameters if we own it. */
  4644. if (ssl->buffers.weOwnDH) {
  4645. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  4646. DYNAMIC_TYPE_PUBLIC_KEY);
  4647. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  4648. DYNAMIC_TYPE_PUBLIC_KEY);
  4649. }
  4650. /* Assign the buffers and lengths to SSL. */
  4651. ssl->buffers.serverDH_P.buffer = p;
  4652. ssl->buffers.serverDH_G.buffer = g;
  4653. ssl->buffers.serverDH_P.length = pSz;
  4654. ssl->buffers.serverDH_G.length = gSz;
  4655. /* We own the buffers. */
  4656. ssl->buffers.weOwnDH = 1;
  4657. /* We have a DH parameters to use. */
  4658. ssl->options.haveDH = 1;
  4659. }
  4660. /* Allocate space for cipher suites. */
  4661. if ((ret == 1) && (AllocateSuites(ssl) != 0)) {
  4662. ret = 0;
  4663. }
  4664. if (ret == 1) {
  4665. /* Reset the cipher suites based on having a DH parameters now. */
  4666. InitSuites(ssl->suites, ssl->version, SSL_KEY_SZ(ssl),
  4667. WOLFSSL_HAVE_RSA, SSL_HAVE_PSK(ssl), ssl->options.haveDH,
  4668. ssl->options.haveECDSAsig, ssl->options.haveECC, TRUE,
  4669. ssl->options.haveStaticECC, ssl->options.haveFalconSig,
  4670. ssl->options.haveDilithiumSig, ssl->options.useAnon, TRUE,
  4671. ssl->options.side);
  4672. }
  4673. return ret;
  4674. }
  4675. /* Set the temporary DH parameters against the SSL.
  4676. *
  4677. * @param [in, out] ssl SSL object.
  4678. * @param [in] p Buffer holding prime.
  4679. * @param [in] pSz Length of prime in bytes.
  4680. * @param [in] g Buffer holding generator.
  4681. * @param [in] gSz Length of generator in bytes.
  4682. * @return 1 on success.
  4683. * @return 0 on failure.
  4684. * @return DH_KEY_SIZE_E when the prime is too short or long.
  4685. * @return SIDE_ERROR when the SSL is for a client.
  4686. * @return MEMORY_E when dynamic memory allocation fails.
  4687. */
  4688. int wolfSSL_SetTmpDH(WOLFSSL* ssl, const unsigned char* p, int pSz,
  4689. const unsigned char* g, int gSz)
  4690. {
  4691. int ret = 1;
  4692. byte* pAlloc = NULL;
  4693. byte* gAlloc = NULL;
  4694. WOLFSSL_ENTER("wolfSSL_SetTmpDH");
  4695. /* Validate parameters. */
  4696. if ((ssl == NULL) || (p == NULL) || (g == NULL)) {
  4697. ret = 0;
  4698. }
  4699. if (ret == 1) {
  4700. /* Allocate buffers for p and g to be assigned into SSL. */
  4701. pAlloc = (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4702. gAlloc = (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4703. if ((pAlloc == NULL) || (gAlloc == NULL)) {
  4704. XFREE(pAlloc, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4705. XFREE(gAlloc, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4706. ret = MEMORY_E;
  4707. }
  4708. }
  4709. if (ret == 1) {
  4710. /* Copy p and g into allocated buffers. */
  4711. XMEMCPY(pAlloc, p, pSz);
  4712. XMEMCPY(gAlloc, g, gSz);
  4713. /* Set the buffers into SSL. */
  4714. ret = wolfssl_set_tmp_dh(ssl, pAlloc, pSz, gAlloc, gSz);
  4715. }
  4716. if (ret != 1) {
  4717. /* Free the allocated buffers if not assigned into SSL. */
  4718. XFREE(pAlloc, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4719. XFREE(gAlloc, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4720. }
  4721. WOLFSSL_LEAVE("wolfSSL_SetTmpDH", ret);
  4722. return ret;
  4723. }
  4724. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
  4725. !defined(HAVE_SELFTEST)
  4726. /* Check the DH parameters is valid.
  4727. *
  4728. * @param [in] p Buffer holding prime.
  4729. * @param [in] pSz Length of prime in bytes.
  4730. * @param [in] g Buffer holding generator.
  4731. * @param [in] gSz Length of generator in bytes.
  4732. * @return 1 on success.
  4733. * @return DH_CHECK_PUB_E when p is not a prime.
  4734. * @return BAD_FUNC_ARG when p or g is NULL, or pSz or gSz is 0.
  4735. * @return MEMORY_E when dynamic memory allocation fails.
  4736. */
  4737. static int wolfssl_check_dh_key(unsigned char* p, int pSz, unsigned char* g,
  4738. int gSz)
  4739. {
  4740. WC_RNG rng;
  4741. int ret = 0;
  4742. #ifndef WOLFSSL_SMALL_STACK
  4743. DhKey checkKey[1];
  4744. #else
  4745. DhKey *checkKey;
  4746. #endif
  4747. #ifdef WOLFSSL_SMALL_STACK
  4748. checkKey = (DhKey*)XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
  4749. if (checkKey == NULL) {
  4750. ret = MEMORY_E;
  4751. }
  4752. #endif
  4753. /* Initialize a new random number generator. */
  4754. if ((ret == 0) && ((ret = wc_InitRng(&rng)) == 0)) {
  4755. /* Initialize a DH object. */
  4756. if ((ret = wc_InitDhKey(checkKey)) == 0) {
  4757. /* Check DH parameters. */
  4758. ret = wc_DhSetCheckKey(checkKey, p, pSz, g, gSz, NULL, 0, 0, &rng);
  4759. /* Dispose of DH object. */
  4760. wc_FreeDhKey(checkKey);
  4761. }
  4762. /* Dispose of random number generator. */
  4763. wc_FreeRng(&rng);
  4764. }
  4765. #ifdef WOLFSSL_SMALL_STACK
  4766. /* Dispose of dynamically allocated data. */
  4767. XFREE(checkKey, NULL, DYNAMIC_TYPE_DH);
  4768. #endif
  4769. /* Convert wolfCrypt return code to 1 on success and ret on failure. */
  4770. return WC_TO_WS_RC(ret);
  4771. }
  4772. #endif
  4773. /* Set the temporary DH parameters against the SSL context.
  4774. *
  4775. * @param [in, out] ctx SSL context object.
  4776. * @param [in] p Buffer holding prime.
  4777. * @param [in] pSz Length of prime in bytes.
  4778. * @param [in] g Buffer holding generator.
  4779. * @param [in] gSz Length of generator in bytes.
  4780. * @return 1 on success.
  4781. * @return 0 on failure.
  4782. * @return DH_KEY_SIZE_E when the prime is too short or long.
  4783. * @return SIDE_ERROR when the SSL is for a client.
  4784. * @return BAD_FUNC_ARG when ctx, p or g is NULL.
  4785. * @return DH_CHECK_PUB_E when p is not a prime.
  4786. * @return MEMORY_E when dynamic memory allocation fails.
  4787. */
  4788. static int wolfssl_ctx_set_tmp_dh(WOLFSSL_CTX* ctx, unsigned char* p, int pSz,
  4789. unsigned char* g, int gSz)
  4790. {
  4791. int ret = 1;
  4792. WOLFSSL_ENTER("wolfSSL_CTX_SetTmpDH");
  4793. /* Check the size of the prime meets the requirements of the SSL context. */
  4794. if (((word16)pSz < ctx->minDhKeySz) || ((word16)pSz > ctx->maxDhKeySz)) {
  4795. ret = DH_KEY_SIZE_E;
  4796. }
  4797. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
  4798. !defined(HAVE_SELFTEST)
  4799. if (ret == 1) {
  4800. /* Test DH parameters for validity. */
  4801. ret = wolfssl_check_dh_key(p, pSz, g, gSz);
  4802. /* Record as whether tested based on result of validity test. */
  4803. ctx->dhKeyTested = (ret == 1);
  4804. }
  4805. #endif
  4806. if (ret == 1) {
  4807. /* Dispose of old DH parameters. */
  4808. XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4809. XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4810. /* Assign the buffers and lengths to SSL context. */
  4811. ctx->serverDH_P.buffer = p;
  4812. ctx->serverDH_G.buffer = g;
  4813. ctx->serverDH_P.length = pSz;
  4814. ctx->serverDH_G.length = gSz;
  4815. /* We have a DH parameters to use. */
  4816. ctx->haveDH = 1;
  4817. }
  4818. WOLFSSL_LEAVE("wolfSSL_CTX_SetTmpDH", 0);
  4819. return ret;
  4820. }
  4821. /* Set the temporary DH parameters against the SSL context.
  4822. *
  4823. * @param [in, out] ctx SSL context object.
  4824. * @param [in] p Buffer holding prime.
  4825. * @param [in] pSz Length of prime in bytes.
  4826. * @param [in] g Buffer holding generator.
  4827. * @param [in] gSz Length of generator in bytes.
  4828. * @return 1 on success.
  4829. * @return 0 on failure.
  4830. * @return DH_KEY_SIZE_E when the prime is too short or long.
  4831. * @return SIDE_ERROR when the SSL is for a client.
  4832. * @return BAD_FUNC_ARG when ctx, p or g is NULL.
  4833. * @return DH_CHECK_PUB_E when p is not a prime.
  4834. */
  4835. int wolfSSL_CTX_SetTmpDH(WOLFSSL_CTX* ctx, const unsigned char* p, int pSz,
  4836. const unsigned char* g, int gSz)
  4837. {
  4838. int ret = 1;
  4839. byte* pAlloc = NULL;
  4840. byte* gAlloc = NULL;
  4841. /* Validate parameters. */
  4842. if ((ctx == NULL) || (p == NULL) || (g == NULL)) {
  4843. ret = BAD_FUNC_ARG;
  4844. }
  4845. if (ret == 1) {
  4846. /* Allocate buffers for p and g to be assigned into SSL context. */
  4847. pAlloc = (byte*)XMALLOC(pSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4848. gAlloc = (byte*)XMALLOC(gSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4849. if ((pAlloc == NULL) || (gAlloc == NULL)) {
  4850. XFREE(pAlloc, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4851. XFREE(gAlloc, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4852. ret = MEMORY_E;
  4853. }
  4854. }
  4855. if (ret == 1) {
  4856. /* Copy p and g into allocated buffers. */
  4857. XMEMCPY(pAlloc, p, pSz);
  4858. XMEMCPY(gAlloc, g, gSz);
  4859. /* Set the buffers into SSL context. */
  4860. ret = wolfssl_ctx_set_tmp_dh(ctx, pAlloc, pSz, gAlloc, gSz);
  4861. }
  4862. if (ret != 1) {
  4863. /* Free the allocated buffers if not assigned into SSL context. */
  4864. XFREE(pAlloc, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4865. XFREE(gAlloc, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4866. }
  4867. return ret;
  4868. }
  4869. #ifdef OPENSSL_EXTRA
  4870. /* Set the temporary DH parameters against the SSL.
  4871. *
  4872. * @param [in, out] ssl SSL object.
  4873. * @param [in] dh DH object.
  4874. * @return 1 on success.
  4875. * @return 0 on failure.
  4876. * @return WOLFSSL_FATAL_ERROR on failure.
  4877. * @return BAD_FUNC_ARG when ssl or dh is NULL.
  4878. * @return DH_KEY_SIZE_E when the prime is too short or long.
  4879. * @return SIDE_ERROR when the SSL is for a client.
  4880. */
  4881. long wolfSSL_set_tmp_dh(WOLFSSL *ssl, WOLFSSL_DH *dh)
  4882. {
  4883. int ret = 1;
  4884. byte* p = NULL;
  4885. byte* g = NULL;
  4886. int pSz = 0;
  4887. int gSz = 0;
  4888. WOLFSSL_ENTER("wolfSSL_set_tmp_dh");
  4889. /* Validate parameters. */
  4890. if ((ssl == NULL) || (dh == NULL)) {
  4891. ret = BAD_FUNC_ARG;
  4892. }
  4893. if (ret == 1) {
  4894. /* Get needed size for p and g. */
  4895. pSz = wolfSSL_BN_bn2bin(dh->p, NULL);
  4896. gSz = wolfSSL_BN_bn2bin(dh->g, NULL);
  4897. /* Validate p and g size. */
  4898. if ((pSz <= 0) || (gSz <= 0)) {
  4899. ret = WOLFSSL_FATAL_ERROR;
  4900. }
  4901. }
  4902. if (ret == 1) {
  4903. /* Allocate buffers for p and g to be assigned into SSL. */
  4904. p = (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4905. g = (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4906. if ((p == NULL) || (g == NULL)) {
  4907. ret = MEMORY_E;
  4908. }
  4909. }
  4910. if (ret == 1) {
  4911. /* Encode p and g and get sizes. */
  4912. pSz = wolfSSL_BN_bn2bin(dh->p, p);
  4913. gSz = wolfSSL_BN_bn2bin(dh->g, g);
  4914. /* Check encoding worked. */
  4915. if ((pSz <= 0) || (gSz <= 0)) {
  4916. ret = WOLFSSL_FATAL_ERROR;
  4917. }
  4918. }
  4919. if (ret == 1) {
  4920. /* Set the buffers into SSL. */
  4921. ret = wolfssl_set_tmp_dh(ssl, p, pSz, g, gSz);
  4922. }
  4923. if (ret != 1) {
  4924. /* Free the allocated buffers if not assigned into SSL. */
  4925. XFREE(p, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4926. XFREE(g, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4927. }
  4928. return ret;
  4929. }
  4930. /* Set the temporary DH parameters object against the SSL context.
  4931. *
  4932. * @param [in, out] ctx SSL context object.
  4933. * @param [in] dh DH object.
  4934. * @return 1 on success.
  4935. * @return 0 on failure.
  4936. * @return DH_KEY_SIZE_E when the prime is too short or long.
  4937. * @return SIDE_ERROR when the SSL is for a client.
  4938. * @return BAD_FUNC_ARG when ctx, p or g is NULL.
  4939. * @return DH_CHECK_PUB_E when p is not a prime.
  4940. */
  4941. long wolfSSL_CTX_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL_DH* dh)
  4942. {
  4943. int ret = 1;
  4944. int pSz = 0;
  4945. int gSz = 0;
  4946. byte* p = NULL;
  4947. byte* g = NULL;
  4948. WOLFSSL_ENTER("wolfSSL_CTX_set_tmp_dh");
  4949. /* Validate parameters. */
  4950. if ((ctx == NULL) || (dh == NULL)) {
  4951. ret = BAD_FUNC_ARG;
  4952. }
  4953. if (ret == 1) {
  4954. /* Get needed size for p and g. */
  4955. pSz = wolfSSL_BN_bn2bin(dh->p, NULL);
  4956. gSz = wolfSSL_BN_bn2bin(dh->g, NULL);
  4957. /* Validate p and g size. */
  4958. if ((pSz <= 0) || (gSz <= 0)) {
  4959. ret = WOLFSSL_FATAL_ERROR;
  4960. }
  4961. }
  4962. if (ret == 1) {
  4963. /* Allocate buffers for p and g to be assigned into SSL. */
  4964. p = (byte*)XMALLOC(pSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4965. g = (byte*)XMALLOC(gSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4966. if ((p == NULL) || (g == NULL)) {
  4967. ret = MEMORY_E;
  4968. }
  4969. }
  4970. if (ret == 1) {
  4971. /* Encode p and g and get sizes. */
  4972. pSz = wolfSSL_BN_bn2bin(dh->p, p);
  4973. gSz = wolfSSL_BN_bn2bin(dh->g, g);
  4974. /* Check encoding worked. */
  4975. if ((pSz < 0) && (gSz < 0)) {
  4976. ret = WOLFSSL_FATAL_ERROR;
  4977. }
  4978. }
  4979. if (ret == 1) {
  4980. /* Set the buffers into SSL context. */
  4981. ret = wolfssl_ctx_set_tmp_dh(ctx, p, pSz, g, gSz);
  4982. }
  4983. if (ret != 1) {
  4984. /* Free the allocated buffers if not assigned into SSL. */
  4985. XFREE(p, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4986. XFREE(g, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  4987. }
  4988. return ret;
  4989. }
  4990. #endif /* OPENSSL_EXTRA */
  4991. #ifndef NO_CERTS
  4992. /* Set the temporary DH parameters against the SSL context or SSL.
  4993. *
  4994. * @param [in, out] ctx SSL context object.
  4995. * @param [in, out] ssl SSL object.
  4996. * @oaram [in] buf Buffer holding encoded DH parameters.
  4997. * @param [in] sz Size of encoded DH parameters.
  4998. * @param [in] format Format of data:
  4999. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  5000. * @return 1 on success.
  5001. * @return 0 on failure.
  5002. * @return BAD_FUNC_ARG when ctx and ssl NULL or buf is NULL.
  5003. * @return NOT_COMPLED_IN when format is PEM but PEM is not supported.
  5004. * @return WOLFSSL_BAD_FILETYPE if format is not supported.
  5005. */
  5006. static int ws_ctx_ssl_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  5007. const unsigned char* buf, long sz, int format)
  5008. {
  5009. DerBuffer* der = NULL;
  5010. int res = 1;
  5011. int ret;
  5012. /* p and g size to allocate set to maximum valid size. */
  5013. word32 pSz = MAX_DH_SIZE;
  5014. word32 gSz = MAX_DH_SIZE;
  5015. byte* p = NULL;
  5016. byte* g = NULL;
  5017. void* heap = WOLFSSL_HEAP(ctx, ssl);
  5018. /* Validate parameters. */
  5019. if (((ctx == NULL) && (ssl == NULL)) || (buf == NULL)) {
  5020. res = BAD_FUNC_ARG;
  5021. }
  5022. /* Check format is supported. */
  5023. if ((res == 1) && (format != WOLFSSL_FILETYPE_ASN1)) {
  5024. if (format != WOLFSSL_FILETYPE_PEM) {
  5025. res = WOLFSSL_BAD_FILETYPE;
  5026. }
  5027. #ifndef WOLFSSL_PEM_TO_DER
  5028. else {
  5029. res = NOT_COMPILED_IN;
  5030. }
  5031. #endif
  5032. }
  5033. /* PemToDer allocates its own DER buffer. */
  5034. if ((res == 1) && (format != WOLFSSL_FILETYPE_PEM)) {
  5035. /* Create an empty DER buffer. */
  5036. ret = AllocDer(&der, 0, DH_PARAM_TYPE, heap);
  5037. if (ret == 0) {
  5038. /* Assign encoded DH parameters to DER buffer. */
  5039. der->buffer = (byte*)buf;
  5040. der->length = (word32)sz;
  5041. }
  5042. else {
  5043. res = ret;
  5044. }
  5045. }
  5046. if (res == 1) {
  5047. /* Allocate enough memory to p and g to support valid use cases. */
  5048. p = (byte*)XMALLOC(pSz, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5049. g = (byte*)XMALLOC(gSz, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5050. if ((p == NULL) || (g == NULL)) {
  5051. res = MEMORY_E;
  5052. }
  5053. }
  5054. #ifdef WOLFSSL_PEM_TO_DER
  5055. if ((res == 1) && (format == WOLFSSL_FILETYPE_PEM)) {
  5056. /* Convert from PEM to DER. */
  5057. /* Try converting DH parameters from PEM to DER. */
  5058. ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, heap, NULL, NULL);
  5059. if (ret < 0) {
  5060. /* Otherwise, try converting X9.43 format DH parameters. */
  5061. ret = PemToDer(buf, sz, X942_PARAM_TYPE, &der, heap, NULL, NULL);
  5062. }
  5063. #if defined(WOLFSSL_WPAS) && !defined(NO_DSA)
  5064. if (ret < 0) {
  5065. /* Otherwise, try converting DSA parameters. */
  5066. ret = PemToDer(buf, sz, DSA_PARAM_TYPE, &der, heap, NULL, NULL);
  5067. }
  5068. #endif /* WOLFSSL_WPAS && !NO_DSA */
  5069. if (ret < 0) {
  5070. /* Return error from conversion. */
  5071. res = ret;
  5072. }
  5073. }
  5074. #endif /* WOLFSSL_PEM_TO_DER */
  5075. if (res == 1) {
  5076. /* Get the p and g from the DER encoded parameters. */
  5077. if (wc_DhParamsLoad(der->buffer, der->length, p, &pSz, g, &gSz) < 0) {
  5078. res = WOLFSSL_BAD_FILETYPE;
  5079. }
  5080. else if (ssl != NULL) {
  5081. /* Set p and g into SSL. */
  5082. res = wolfssl_set_tmp_dh(ssl, p, pSz, g, gSz);
  5083. }
  5084. else {
  5085. /* Set p and g into SSL context. */
  5086. res = wolfssl_ctx_set_tmp_dh(ctx, p, pSz, g, gSz);
  5087. }
  5088. }
  5089. /* Dispose of the DER buffer. */
  5090. FreeDer(&der);
  5091. if (res != 1) {
  5092. /* Free the allocated buffers if not assigned into SSL or context. */
  5093. XFREE(p, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5094. XFREE(g, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5095. }
  5096. return res;
  5097. }
  5098. /* Set the temporary DH parameters against the SSL.
  5099. *
  5100. * @param [in, out] ssl SSL object.
  5101. * @oaram [in] buf Buffer holding encoded DH parameters.
  5102. * @param [in] sz Size of encoded DH parameters.
  5103. * @param [in] format Format of data:
  5104. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  5105. * @return 1 on success.
  5106. * @return BAD_FUNC_ARG when ssl or buf is NULL.
  5107. * @return NOT_COMPLED_IN when format is PEM but PEM is not supported.
  5108. * @return WOLFSSL_BAD_FILETYPE if format is not supported.
  5109. */
  5110. int wolfSSL_SetTmpDH_buffer(WOLFSSL* ssl, const unsigned char* buf, long sz,
  5111. int format)
  5112. {
  5113. return ws_ctx_ssl_set_tmp_dh(NULL, ssl, buf, sz, format);
  5114. }
  5115. /* Set the temporary DH parameters against the SSL context.
  5116. *
  5117. * @param [in, out] ctx SSL context object.
  5118. * @oaram [in] buf Buffer holding encoded DH parameters.
  5119. * @param [in] sz Size of encoded DH parameters.
  5120. * @param [in] format Format of data:
  5121. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  5122. * @return 1 on success.
  5123. * @return BAD_FUNC_ARG when ctx or buf is NULL.
  5124. * @return NOT_COMPLED_IN when format is PEM but PEM is not supported.
  5125. * @return WOLFSSL_BAD_FILETYPE if format is not supported.
  5126. */
  5127. int wolfSSL_CTX_SetTmpDH_buffer(WOLFSSL_CTX* ctx, const unsigned char* buf,
  5128. long sz, int format)
  5129. {
  5130. return ws_ctx_ssl_set_tmp_dh(ctx, NULL, buf, sz, format);
  5131. }
  5132. #ifndef NO_FILESYSTEM
  5133. /* Set the temporary DH parameters file against the SSL context or SSL.
  5134. *
  5135. * @param [in, out] ctx SSL context object.
  5136. * @param [in, out] ssl SSL object.
  5137. * @param [in] fname Name of file to load.
  5138. * @param [in] format Format of data:
  5139. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  5140. * @return 1 on success.
  5141. * @return BAD_FUNC_ARG when ctx and ssl NULL or fname is NULL.
  5142. * @return NOT_COMPLED_IN when format is PEM but PEM is not supported.
  5143. * @return WOLFSSL_BAD_FILETYPE if format is not supported.
  5144. */
  5145. static int ws_ctx_ssl_set_tmp_dh_file(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  5146. const char* fname, int format)
  5147. {
  5148. int res = 1;
  5149. int ret;
  5150. #ifndef WOLFSSL_SMALL_STACK
  5151. byte stackBuffer[FILE_BUFFER_SIZE];
  5152. #endif
  5153. StaticBuffer dhFile;
  5154. long sz = 0;
  5155. void* heap = WOLFSSL_HEAP(ctx, ssl);
  5156. /* Setup buffer to hold file contents. */
  5157. #ifdef WOLFSSL_SMALL_STACK
  5158. static_buffer_init(&dhFile);
  5159. #else
  5160. static_buffer_init(&dhFile, stackBuffer, FILE_BUFFER_SIZE);
  5161. #endif
  5162. /* Validate parameters. */
  5163. if (((ctx == NULL) && (ssl == NULL)) || (fname == NULL)) {
  5164. res = BAD_FUNC_ARG;
  5165. }
  5166. if (res == 1) {
  5167. /* Read file into static buffer. */
  5168. ret = wolfssl_read_file_static(fname, &dhFile, heap, DYNAMIC_TYPE_FILE,
  5169. &sz);
  5170. if (ret != 0) {
  5171. res = ret;
  5172. }
  5173. }
  5174. if (res == 1) {
  5175. if (ssl != NULL) {
  5176. /* Set encoded DH paramters into SSL. */
  5177. res = wolfSSL_SetTmpDH_buffer(ssl, dhFile.buffer, sz, format);
  5178. }
  5179. else {
  5180. /* Set encoded DH paramters into SSL context. */
  5181. res = wolfSSL_CTX_SetTmpDH_buffer(ctx, dhFile.buffer, sz, format);
  5182. }
  5183. }
  5184. /* Dispose of any dynamically allocated data. */
  5185. static_buffer_free(&dhFile, heap, DYNAMIC_TYPE_FILE);
  5186. return res;
  5187. }
  5188. /* Set the temporary DH parameters file against the SSL.
  5189. *
  5190. * @param [in, out] ssl SSL object.
  5191. * @param [in] fname Name of file to load.
  5192. * @param [in] format Format of data:
  5193. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  5194. * @return 1 on success.
  5195. * @return BAD_FUNC_ARG when ssl or fname is NULL.
  5196. * @return NOT_COMPLED_IN when format is PEM but PEM is not supported.
  5197. * @return WOLFSSL_BAD_FILETYPE if format is not supported.
  5198. */
  5199. int wolfSSL_SetTmpDH_file(WOLFSSL* ssl, const char* fname, int format)
  5200. {
  5201. return ws_ctx_ssl_set_tmp_dh_file(NULL, ssl, fname, format);
  5202. }
  5203. /* Set the temporary DH parameters file against the SSL context.
  5204. *
  5205. * @param [in, out] ctx SSL context object.
  5206. * @param [in] fname Name of file to load.
  5207. * @param [in] format Format of data:
  5208. * WOLFSSL_FILETYPE_PEM or WOLFSSL_FILETYPE_ASN1.
  5209. * @return 1 on success.
  5210. * @return BAD_FUNC_ARG when ctx or fname is NULL.
  5211. * @return NOT_COMPLED_IN when format is PEM but PEM is not supported.
  5212. * @return WOLFSSL_BAD_FILETYPE if format is not supported.
  5213. */
  5214. int wolfSSL_CTX_SetTmpDH_file(WOLFSSL_CTX* ctx, const char* fname, int format)
  5215. {
  5216. return ws_ctx_ssl_set_tmp_dh_file(ctx, NULL, fname, format);
  5217. }
  5218. #endif /* NO_FILESYSTEM */
  5219. #endif /* NO_CERTS */
  5220. #endif /* !NO_DH */
  5221. #endif /* !WOLFSSL_SSL_LOAD_INCLUDED */