ssl.c 693 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616
  1. /* ssl.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. #if defined(OPENSSL_EXTRA) && !defined(_WIN32)
  26. /* turn on GNU extensions for XISASCII */
  27. #undef _GNU_SOURCE
  28. #define _GNU_SOURCE
  29. #endif
  30. #if !defined(WOLFCRYPT_ONLY) || defined(OPENSSL_EXTRA) || \
  31. defined(OPENSSL_EXTRA_X509_SMALL)
  32. #include <wolfssl/internal.h>
  33. #include <wolfssl/error-ssl.h>
  34. #include <wolfssl/wolfcrypt/coding.h>
  35. #include <wolfssl/wolfcrypt/kdf.h>
  36. #ifdef NO_INLINE
  37. #include <wolfssl/wolfcrypt/misc.h>
  38. #else
  39. #define WOLFSSL_MISC_INCLUDED
  40. #include <wolfcrypt/src/misc.c>
  41. #endif
  42. #ifdef HAVE_ERRNO_H
  43. #include <errno.h>
  44. #endif
  45. #if !defined(WOLFSSL_ALLOW_NO_SUITES) && !defined(WOLFCRYPT_ONLY)
  46. #if defined(NO_DH) && !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA) \
  47. && !defined(WOLFSSL_STATIC_DH) && !defined(WOLFSSL_STATIC_PSK) \
  48. && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448)
  49. #error "No cipher suites defined because DH disabled, ECC disabled, "
  50. "and no static suites defined. Please see top of README"
  51. #endif
  52. #ifdef WOLFSSL_CERT_GEN
  53. /* need access to Cert struct for creating certificate */
  54. #include <wolfssl/wolfcrypt/asn_public.h>
  55. #endif
  56. #endif
  57. #if !defined(WOLFCRYPT_ONLY) && (defined(OPENSSL_EXTRA) \
  58. || defined(OPENSSL_EXTRA_X509_SMALL) \
  59. || defined(HAVE_WEBSERVER) || defined(WOLFSSL_KEY_GEN))
  60. #include <wolfssl/openssl/evp.h>
  61. /* openssl headers end, wolfssl internal headers next */
  62. #endif
  63. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  64. #ifndef NO_RSA
  65. #include <wolfssl/wolfcrypt/rsa.h>
  66. #endif
  67. #ifdef OPENSSL_EXTRA
  68. /* openssl headers begin */
  69. #include <wolfssl/openssl/ssl.h>
  70. #include <wolfssl/openssl/aes.h>
  71. #ifndef WOLFCRYPT_ONLY
  72. #include <wolfssl/openssl/hmac.h>
  73. #include <wolfssl/openssl/cmac.h>
  74. #endif
  75. #include <wolfssl/openssl/crypto.h>
  76. #include <wolfssl/openssl/des.h>
  77. #include <wolfssl/openssl/bn.h>
  78. #include <wolfssl/openssl/buffer.h>
  79. #include <wolfssl/openssl/dh.h>
  80. #include <wolfssl/openssl/rsa.h>
  81. #include <wolfssl/openssl/fips_rand.h>
  82. #include <wolfssl/openssl/pem.h>
  83. #include <wolfssl/openssl/ec.h>
  84. #include <wolfssl/openssl/ec25519.h>
  85. #include <wolfssl/openssl/ed25519.h>
  86. #include <wolfssl/openssl/ec448.h>
  87. #include <wolfssl/openssl/ed448.h>
  88. #include <wolfssl/openssl/ecdsa.h>
  89. #include <wolfssl/openssl/ecdh.h>
  90. #include <wolfssl/openssl/err.h>
  91. #include <wolfssl/openssl/modes.h>
  92. #include <wolfssl/openssl/opensslv.h>
  93. #include <wolfssl/openssl/rc4.h>
  94. #include <wolfssl/openssl/stack.h>
  95. #include <wolfssl/openssl/x509_vfy.h>
  96. /* openssl headers end, wolfssl internal headers next */
  97. #include <wolfssl/wolfcrypt/hmac.h>
  98. #include <wolfssl/wolfcrypt/random.h>
  99. #include <wolfssl/wolfcrypt/des3.h>
  100. #include <wolfssl/wolfcrypt/ecc.h>
  101. #include <wolfssl/wolfcrypt/md4.h>
  102. #include <wolfssl/wolfcrypt/md5.h>
  103. #include <wolfssl/wolfcrypt/arc4.h>
  104. #include <wolfssl/wolfcrypt/curve25519.h>
  105. #include <wolfssl/wolfcrypt/ed25519.h>
  106. #include <wolfssl/wolfcrypt/curve448.h>
  107. #if defined(HAVE_PQC)
  108. #if defined(HAVE_FALCON)
  109. #include <wolfssl/wolfcrypt/falcon.h>
  110. #endif /* HAVE_FALCON */
  111. #if defined(HAVE_DILITHIUM)
  112. #include <wolfssl/wolfcrypt/dilithium.h>
  113. #endif /* HAVE_DILITHIUM */
  114. #if defined(HAVE_SPHINCS)
  115. #include <wolfssl/wolfcrypt/sphincs.h>
  116. #endif /* HAVE_SPHINCS */
  117. #endif /* HAVE_PQC */
  118. #if defined(OPENSSL_ALL) || defined(HAVE_STUNNEL)
  119. #ifdef HAVE_OCSP
  120. #include <wolfssl/openssl/ocsp.h>
  121. #endif
  122. #include <wolfssl/openssl/lhash.h>
  123. #include <wolfssl/openssl/txt_db.h>
  124. #endif /* WITH_STUNNEL */
  125. #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
  126. #include <wolfssl/wolfcrypt/sha512.h>
  127. #endif
  128. #if defined(WOLFCRYPT_HAVE_SRP) && !defined(NO_SHA256) \
  129. && !defined(WC_NO_RNG)
  130. #include <wolfssl/wolfcrypt/srp.h>
  131. #endif
  132. #endif
  133. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  134. #include <wolfssl/openssl/x509v3.h>
  135. int wolfssl_bn_get_value(WOLFSSL_BIGNUM* bn, mp_int* mpi);
  136. int wolfssl_bn_set_value(WOLFSSL_BIGNUM** bn, mp_int* mpi);
  137. #endif
  138. #if defined(WOLFSSL_QT)
  139. #include <wolfssl/wolfcrypt/sha.h>
  140. #endif
  141. #ifdef NO_ASN
  142. #include <wolfssl/wolfcrypt/dh.h>
  143. #endif
  144. #endif /* !WOLFCRYPT_ONLY || OPENSSL_EXTRA */
  145. /*
  146. * OPENSSL_COMPATIBLE_DEFAULTS:
  147. * Enable default behaviour that is compatible with OpenSSL. For example
  148. * SSL_CTX by default doesn't verify the loaded certs. Enabling this
  149. * should make porting to new projects easier.
  150. * WOLFSSL_CHECK_ALERT_ON_ERR:
  151. * Check for alerts during the handshake in the event of an error.
  152. * NO_SESSION_CACHE_REF:
  153. * wolfSSL_get_session on a client will return a reference to the internal
  154. * ClientCache by default for backwards compatibility. This define will
  155. * make wolfSSL_get_session return a reference to ssl->session. The returned
  156. * pointer will be freed with the related WOLFSSL object.
  157. * SESSION_CACHE_DYNAMIC_MEM:
  158. * Dynamically allocate sessions for the session cache from the heap, as
  159. * opposed to the default which allocates from the stack. Allocates
  160. * memory only when a session is added to the cache, frees memory after the
  161. * session is no longer being used. Recommended for memory-constrained
  162. * systems.
  163. * WOLFSSL_SYS_CA_CERTS
  164. * Enables ability to load system CA certs from the OS via
  165. * wolfSSL_CTX_load_system_CA_certs.
  166. */
  167. #define WOLFSSL_SSL_MISC_INCLUDED
  168. #include "src/ssl_misc.c"
  169. #define WOLFSSL_EVP_INCLUDED
  170. #include "wolfcrypt/src/evp.c"
  171. /* Crypto code uses EVP APIs. */
  172. #define WOLFSSL_SSL_CRYPTO_INCLUDED
  173. #include "src/ssl_crypto.c"
  174. #ifndef WOLFCRYPT_ONLY
  175. #define WOLFSSL_SSL_CERTMAN_INCLUDED
  176. #include "src/ssl_certman.c"
  177. #define WOLFSSL_SSL_SESS_INCLUDED
  178. #include "src/ssl_sess.c"
  179. #endif
  180. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  181. !defined(WOLFCRYPT_ONLY)
  182. /* Convert shortname to NID.
  183. *
  184. * For OpenSSL compatibility.
  185. *
  186. * This function shouldn't exist!
  187. * Uses defines in wolfssl/openssl/evp.h.
  188. * Uses EccEnumToNID which uses defines in wolfssl/openssl/ec.h.
  189. *
  190. * @param [in] sn Short name of OID.
  191. * @return NID corresponding to shortname on success.
  192. * @return NID_undef when not recognized.
  193. */
  194. int wc_OBJ_sn2nid(const char *sn)
  195. {
  196. const struct {
  197. const char *sn;
  198. int nid;
  199. } sn2nid[] = {
  200. #ifndef NO_CERTS
  201. {WOLFSSL_COMMON_NAME, NID_commonName},
  202. {WOLFSSL_COUNTRY_NAME, NID_countryName},
  203. {WOLFSSL_LOCALITY_NAME, NID_localityName},
  204. {WOLFSSL_STATE_NAME, NID_stateOrProvinceName},
  205. {WOLFSSL_ORG_NAME, NID_organizationName},
  206. {WOLFSSL_ORGUNIT_NAME, NID_organizationalUnitName},
  207. #ifdef WOLFSSL_CERT_NAME_ALL
  208. {WOLFSSL_NAME, NID_name},
  209. {WOLFSSL_INITIALS, NID_initials},
  210. {WOLFSSL_GIVEN_NAME, NID_givenName},
  211. {WOLFSSL_DNQUALIFIER, NID_dnQualifier},
  212. #endif
  213. {WOLFSSL_EMAIL_ADDR, NID_emailAddress},
  214. #endif
  215. {"SHA1", NID_sha1},
  216. {NULL, -1}};
  217. int i;
  218. #ifdef HAVE_ECC
  219. char curveName[ECC_MAXNAME + 1];
  220. int eccEnum;
  221. #endif
  222. WOLFSSL_ENTER("wc_OBJ_sn2nid");
  223. for(i=0; sn2nid[i].sn != NULL; i++) {
  224. if (XSTRCMP(sn, sn2nid[i].sn) == 0) {
  225. return sn2nid[i].nid;
  226. }
  227. }
  228. #ifdef HAVE_ECC
  229. if (XSTRLEN(sn) > ECC_MAXNAME)
  230. return NID_undef;
  231. /* Nginx uses this OpenSSL string. */
  232. if (XSTRCMP(sn, "prime256v1") == 0)
  233. sn = "SECP256R1";
  234. /* OpenSSL allows lowercase curve names */
  235. for (i = 0; i < (int)(sizeof(curveName) - 1) && *sn; i++) {
  236. curveName[i] = (char)XTOUPPER((unsigned char) *sn++);
  237. }
  238. curveName[i] = '\0';
  239. /* find based on name and return NID */
  240. for (i = 0;
  241. #ifndef WOLFSSL_ECC_CURVE_STATIC
  242. ecc_sets[i].size != 0 && ecc_sets[i].name != NULL;
  243. #else
  244. ecc_sets[i].size != 0;
  245. #endif
  246. i++) {
  247. if (XSTRCMP(curveName, ecc_sets[i].name) == 0) {
  248. eccEnum = ecc_sets[i].id;
  249. /* Convert enum value in ecc_curve_id to OpenSSL NID */
  250. return EccEnumToNID(eccEnum);
  251. }
  252. }
  253. #endif /* HAVE_ECC */
  254. return NID_undef;
  255. }
  256. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  257. #ifndef WOLFCRYPT_ONLY
  258. #if !defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC) || \
  259. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && !defined(NO_DSA))
  260. #define HAVE_GLOBAL_RNG /* consolidate flags for using globalRNG */
  261. static WC_RNG globalRNG;
  262. static volatile int initGlobalRNG = 0;
  263. static WC_MAYBE_UNUSED wolfSSL_Mutex globalRNGMutex
  264. WOLFSSL_MUTEX_INITIALIZER_CLAUSE(globalRNGMutex);
  265. #ifndef WOLFSSL_MUTEX_INITIALIZER
  266. static int globalRNGMutex_valid = 0;
  267. #endif
  268. #if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
  269. static WOLFSSL_DRBG_CTX* gDrbgDefCtx = NULL;
  270. #endif
  271. WC_RNG* wolfssl_get_global_rng(void)
  272. {
  273. WC_RNG* ret = NULL;
  274. if (initGlobalRNG == 0)
  275. WOLFSSL_MSG("Global RNG no Init");
  276. else
  277. ret = &globalRNG;
  278. return ret;
  279. }
  280. /* Make a global RNG and return.
  281. *
  282. * @return Global RNG on success.
  283. * @return NULL on error.
  284. */
  285. WC_RNG* wolfssl_make_global_rng(void)
  286. {
  287. WC_RNG* ret;
  288. #ifdef HAVE_GLOBAL_RNG
  289. /* Get the global random number generator instead. */
  290. ret = wolfssl_get_global_rng();
  291. #ifdef OPENSSL_EXTRA
  292. if (ret == NULL) {
  293. /* Create a global random if possible. */
  294. (void)wolfSSL_RAND_Init();
  295. ret = wolfssl_get_global_rng();
  296. }
  297. #endif
  298. #else
  299. WOLFSSL_ERROR_MSG("Bad RNG Init");
  300. ret = NULL;
  301. #endif
  302. return ret;
  303. }
  304. /* Too many defines to check explicitly - prototype it and always include
  305. * for RSA, DH, ECC and DSA for BN. */
  306. WC_RNG* wolfssl_make_rng(WC_RNG* rng, int* local);
  307. /* Make a random number generator or get global if possible.
  308. *
  309. * Global may not be available and NULL will be returned.
  310. *
  311. * @param [in, out] rng Local random number generator.
  312. * @param [out] local Local random number generator returned.
  313. * @return NULL on failure.
  314. * @return A random number generator object.
  315. */
  316. WC_RNG* wolfssl_make_rng(WC_RNG* rng, int* local)
  317. {
  318. WC_RNG* ret = NULL;
  319. /* Assume not local until one created. */
  320. *local = 0;
  321. #ifdef WOLFSSL_SMALL_STACK
  322. /* Allocate RNG object . */
  323. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  324. #endif
  325. /* Check we have a local RNG object and initialize. */
  326. if ((rng != NULL) && (wc_InitRng(rng) == 0)) {
  327. ret = rng;
  328. *local = 1;
  329. }
  330. if (ret == NULL) {
  331. #ifdef HAVE_GLOBAL_RNG
  332. WOLFSSL_MSG("Bad RNG Init, trying global");
  333. #endif
  334. ret = wolfssl_make_global_rng();
  335. }
  336. if (ret != rng) {
  337. #ifdef WOLFSSL_SMALL_STACK
  338. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  339. #endif
  340. }
  341. return ret;
  342. }
  343. #endif
  344. #ifdef OPENSSL_EXTRA
  345. /* WOLFSSL_NO_OPENSSL_RAND_CB: Allows way to reduce code size for
  346. * OPENSSL_EXTRA where RAND callbacks are not used */
  347. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  348. static const WOLFSSL_RAND_METHOD* gRandMethods = NULL;
  349. static wolfSSL_Mutex gRandMethodMutex
  350. WOLFSSL_MUTEX_INITIALIZER_CLAUSE(gRandMethodMutex);
  351. #ifndef WOLFSSL_MUTEX_INITIALIZER
  352. static int gRandMethodsInit = 0;
  353. #endif
  354. #endif /* !WOLFSSL_NO_OPENSSL_RAND_CB */
  355. #endif /* OPENSSL_EXTRA */
  356. #define WOLFSSL_SSL_BN_INCLUDED
  357. #include "src/ssl_bn.c"
  358. #ifndef OPENSSL_EXTRA_NO_ASN1
  359. #define WOLFSSL_SSL_ASN1_INCLUDED
  360. #include "src/ssl_asn1.c"
  361. #endif /* OPENSSL_EXTRA_NO_ASN1 */
  362. #define WOLFSSL_PK_INCLUDED
  363. #include "src/pk.c"
  364. #include <wolfssl/wolfcrypt/hpke.h>
  365. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  366. const WOLF_EC_NIST_NAME kNistCurves[] = {
  367. {XSTR_SIZEOF("P-192"), "P-192", NID_X9_62_prime192v1},
  368. {XSTR_SIZEOF("P-256"), "P-256", NID_X9_62_prime256v1},
  369. {XSTR_SIZEOF("P-112"), "P-112", NID_secp112r1},
  370. {XSTR_SIZEOF("P-112-2"), "P-112-2", NID_secp112r2},
  371. {XSTR_SIZEOF("P-128"), "P-128", NID_secp128r1},
  372. {XSTR_SIZEOF("P-128-2"), "P-128-2", NID_secp128r2},
  373. {XSTR_SIZEOF("P-160"), "P-160", NID_secp160r1},
  374. {XSTR_SIZEOF("P-160-2"), "P-160-2", NID_secp160r2},
  375. {XSTR_SIZEOF("P-224"), "P-224", NID_secp224r1},
  376. {XSTR_SIZEOF("P-384"), "P-384", NID_secp384r1},
  377. {XSTR_SIZEOF("P-521"), "P-521", NID_secp521r1},
  378. {XSTR_SIZEOF("K-160"), "K-160", NID_secp160k1},
  379. {XSTR_SIZEOF("K-192"), "K-192", NID_secp192k1},
  380. {XSTR_SIZEOF("K-224"), "K-224", NID_secp224k1},
  381. {XSTR_SIZEOF("K-256"), "K-256", NID_secp256k1},
  382. {XSTR_SIZEOF("B-160"), "B-160", NID_brainpoolP160r1},
  383. {XSTR_SIZEOF("B-192"), "B-192", NID_brainpoolP192r1},
  384. {XSTR_SIZEOF("B-224"), "B-224", NID_brainpoolP224r1},
  385. {XSTR_SIZEOF("B-256"), "B-256", NID_brainpoolP256r1},
  386. {XSTR_SIZEOF("B-320"), "B-320", NID_brainpoolP320r1},
  387. {XSTR_SIZEOF("B-384"), "B-384", NID_brainpoolP384r1},
  388. {XSTR_SIZEOF("B-512"), "B-512", NID_brainpoolP512r1},
  389. #ifdef HAVE_PQC
  390. {XSTR_SIZEOF("KYBER_LEVEL1"), "KYBER_LEVEL1", WOLFSSL_KYBER_LEVEL1},
  391. {XSTR_SIZEOF("KYBER_LEVEL3"), "KYBER_LEVEL3", WOLFSSL_KYBER_LEVEL3},
  392. {XSTR_SIZEOF("KYBER_LEVEL5"), "KYBER_LEVEL5", WOLFSSL_KYBER_LEVEL5},
  393. #ifdef HAVE_LIBOQS
  394. {XSTR_SIZEOF("P256_KYBER_LEVEL1"), "P256_KYBER_LEVEL1",
  395. WOLFSSL_P256_KYBER_LEVEL1},
  396. {XSTR_SIZEOF("P384_KYBER_LEVEL3"), "P384_KYBER_LEVEL3",
  397. WOLFSSL_P384_KYBER_LEVEL3},
  398. {XSTR_SIZEOF("P521_KYBER_LEVEL5"), "P521_KYBER_LEVEL5",
  399. WOLFSSL_P521_KYBER_LEVEL5},
  400. #endif
  401. #endif
  402. #ifdef WOLFSSL_SM2
  403. {XSTR_SIZEOF("SM2"), "SM2", NID_sm2},
  404. #endif
  405. {0, NULL, 0},
  406. };
  407. #endif
  408. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  409. /* create the hpke key and ech config to send to clients */
  410. int wolfSSL_CTX_GenerateEchConfig(WOLFSSL_CTX* ctx, const char* publicName,
  411. word16 kemId, word16 kdfId, word16 aeadId)
  412. {
  413. int ret = 0;
  414. word16 encLen = DHKEM_X25519_ENC_LEN;
  415. #ifdef WOLFSSL_SMALL_STACK
  416. Hpke* hpke = NULL;
  417. WC_RNG* rng;
  418. #else
  419. Hpke hpke[1];
  420. WC_RNG rng[1];
  421. #endif
  422. if (ctx == NULL || publicName == NULL)
  423. return BAD_FUNC_ARG;
  424. #ifdef WOLFSSL_SMALL_STACK
  425. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), ctx->heap, DYNAMIC_TYPE_RNG);
  426. if (rng == NULL)
  427. return MEMORY_E;
  428. #endif
  429. ret = wc_InitRng(rng);
  430. if (ret != 0) {
  431. #ifdef WOLFSSL_SMALL_STACK
  432. XFREE(rng, ctx->heap, DYNAMIC_TYPE_RNG);
  433. #endif
  434. return ret;
  435. }
  436. ctx->echConfigs = (WOLFSSL_EchConfig*)XMALLOC(sizeof(WOLFSSL_EchConfig),
  437. ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  438. if (ctx->echConfigs == NULL)
  439. ret = MEMORY_E;
  440. else
  441. XMEMSET(ctx->echConfigs, 0, sizeof(WOLFSSL_EchConfig));
  442. /* set random config id */
  443. if (ret == 0)
  444. ret = wc_RNG_GenerateByte(rng, &ctx->echConfigs->configId);
  445. /* if 0 is selected for algorithms use default, may change with draft */
  446. if (kemId == 0)
  447. kemId = DHKEM_X25519_HKDF_SHA256;
  448. if (kdfId == 0)
  449. kdfId = HKDF_SHA256;
  450. if (aeadId == 0)
  451. aeadId = HPKE_AES_128_GCM;
  452. if (ret == 0) {
  453. /* set the kem id */
  454. ctx->echConfigs->kemId = kemId;
  455. /* set the cipher suite, only 1 for now */
  456. ctx->echConfigs->numCipherSuites = 1;
  457. ctx->echConfigs->cipherSuites = (EchCipherSuite*)XMALLOC(
  458. sizeof(EchCipherSuite), ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  459. if (ctx->echConfigs->cipherSuites == NULL) {
  460. ret = MEMORY_E;
  461. }
  462. else {
  463. ctx->echConfigs->cipherSuites[0].kdfId = kdfId;
  464. ctx->echConfigs->cipherSuites[0].aeadId = aeadId;
  465. }
  466. }
  467. #ifdef WOLFSSL_SMALL_STACK
  468. if (ret == 0) {
  469. hpke = (Hpke*)XMALLOC(sizeof(Hpke), ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  470. if (hpke == NULL)
  471. ret = MEMORY_E;
  472. }
  473. #endif
  474. if (ret == 0)
  475. ret = wc_HpkeInit(hpke, kemId, kdfId, aeadId, ctx->heap);
  476. /* generate the receiver private key */
  477. if (ret == 0)
  478. ret = wc_HpkeGenerateKeyPair(hpke, &ctx->echConfigs->receiverPrivkey,
  479. rng);
  480. /* done with RNG */
  481. wc_FreeRng(rng);
  482. /* serialize the receiver key */
  483. if (ret == 0)
  484. ret = wc_HpkeSerializePublicKey(hpke, ctx->echConfigs->receiverPrivkey,
  485. ctx->echConfigs->receiverPubkey, &encLen);
  486. if (ret == 0) {
  487. ctx->echConfigs->publicName = (char*)XMALLOC(XSTRLEN(publicName) + 1,
  488. ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  489. if (ctx->echConfigs->publicName == NULL) {
  490. ret = MEMORY_E;
  491. }
  492. else {
  493. XMEMCPY(ctx->echConfigs->publicName, publicName,
  494. XSTRLEN(publicName) + 1);
  495. }
  496. }
  497. if (ret != 0) {
  498. if (ctx->echConfigs) {
  499. XFREE(ctx->echConfigs->cipherSuites, ctx->heap,
  500. DYNAMIC_TYPE_TMP_BUFFER);
  501. XFREE(ctx->echConfigs->publicName, ctx->heap,
  502. DYNAMIC_TYPE_TMP_BUFFER);
  503. XFREE(ctx->echConfigs, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  504. /* set to null to avoid double free in cleanup */
  505. ctx->echConfigs = NULL;
  506. }
  507. }
  508. if (ret == 0)
  509. ret = WOLFSSL_SUCCESS;
  510. #ifdef WOLFSSL_SMALL_STACK
  511. XFREE(hpke, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
  512. XFREE(rng, ctx->heap, DYNAMIC_TYPE_RNG);
  513. #endif
  514. return ret;
  515. }
  516. /* get the ech configs that the server context is using */
  517. int wolfSSL_CTX_GetEchConfigs(WOLFSSL_CTX* ctx, byte* output,
  518. word32* outputLen) {
  519. if (ctx == NULL || outputLen == NULL)
  520. return BAD_FUNC_ARG;
  521. /* if we don't have ech configs */
  522. if (ctx->echConfigs == NULL) {
  523. return WOLFSSL_FATAL_ERROR;
  524. }
  525. return GetEchConfigsEx(ctx->echConfigs, output, outputLen);
  526. }
  527. /* set the ech config from base64 for our client ssl object, base64 is the
  528. * format ech configs are sent using dns records */
  529. int wolfSSL_SetEchConfigsBase64(WOLFSSL* ssl, char* echConfigs64,
  530. word32 echConfigs64Len)
  531. {
  532. int ret = 0;
  533. word32 decodedLen = echConfigs64Len * 3 / 4 + 1;
  534. byte* decodedConfigs;
  535. if (ssl == NULL || echConfigs64 == NULL || echConfigs64Len == 0)
  536. return BAD_FUNC_ARG;
  537. /* already have ech configs */
  538. if (ssl->options.useEch == 1) {
  539. return WOLFSSL_FATAL_ERROR;
  540. }
  541. decodedConfigs = (byte*)XMALLOC(decodedLen, ssl->heap,
  542. DYNAMIC_TYPE_TMP_BUFFER);
  543. if (decodedConfigs == NULL)
  544. return MEMORY_E;
  545. decodedConfigs[decodedLen - 1] = 0;
  546. /* decode the echConfigs */
  547. ret = Base64_Decode((byte*)echConfigs64, echConfigs64Len,
  548. decodedConfigs, &decodedLen);
  549. if (ret != 0) {
  550. XFREE(decodedConfigs, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  551. return ret;
  552. }
  553. ret = wolfSSL_SetEchConfigs(ssl, decodedConfigs, decodedLen);
  554. XFREE(decodedConfigs, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  555. return ret;
  556. }
  557. /* set the ech config from a raw buffer, this is the format ech configs are
  558. * sent using retry_configs from the ech server */
  559. int wolfSSL_SetEchConfigs(WOLFSSL* ssl, const byte* echConfigs,
  560. word32 echConfigsLen)
  561. {
  562. int ret = 0;
  563. int i;
  564. int j;
  565. word16 totalLength;
  566. word16 version;
  567. word16 length;
  568. word16 hpkePubkeyLen;
  569. word16 cipherSuitesLen;
  570. word16 publicNameLen;
  571. WOLFSSL_EchConfig* configList = NULL;
  572. WOLFSSL_EchConfig* workingConfig = NULL;
  573. WOLFSSL_EchConfig* lastConfig = NULL;
  574. byte* echConfig = NULL;
  575. if (ssl == NULL || echConfigs == NULL || echConfigsLen == 0)
  576. return BAD_FUNC_ARG;
  577. /* already have ech configs */
  578. if (ssl->options.useEch == 1) {
  579. return WOLFSSL_FATAL_ERROR;
  580. }
  581. /* check that the total length is well formed */
  582. ato16(echConfigs, &totalLength);
  583. if (totalLength != echConfigsLen - 2) {
  584. return WOLFSSL_FATAL_ERROR;
  585. }
  586. /* skip the total length uint16_t */
  587. i = 2;
  588. do {
  589. echConfig = (byte*)echConfigs + i;
  590. ato16(echConfig, &version);
  591. ato16(echConfig + 2, &length);
  592. /* if the version does not match */
  593. if (version != TLSX_ECH) {
  594. /* we hit the end of the configs */
  595. if ( (word32)i + 2 >= echConfigsLen ) {
  596. break;
  597. }
  598. /* skip this config, +4 for version and length */
  599. i += length + 4;
  600. continue;
  601. }
  602. /* check if the length will overrun the buffer */
  603. if ((word32)i + length + 4 > echConfigsLen) {
  604. break;
  605. }
  606. if (workingConfig == NULL) {
  607. workingConfig =
  608. (WOLFSSL_EchConfig*)XMALLOC(sizeof(WOLFSSL_EchConfig),
  609. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  610. configList = workingConfig;
  611. if (workingConfig != NULL) {
  612. workingConfig->next = NULL;
  613. }
  614. }
  615. else {
  616. lastConfig = workingConfig;
  617. workingConfig->next =
  618. (WOLFSSL_EchConfig*)XMALLOC(sizeof(WOLFSSL_EchConfig),
  619. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  620. workingConfig = workingConfig->next;
  621. }
  622. if (workingConfig == NULL) {
  623. ret = MEMORY_E;
  624. break;
  625. }
  626. XMEMSET(workingConfig, 0, sizeof(WOLFSSL_EchConfig));
  627. /* rawLen */
  628. workingConfig->rawLen = length + 4;
  629. /* raw body */
  630. workingConfig->raw = (byte*)XMALLOC(workingConfig->rawLen,
  631. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  632. if (workingConfig->raw == NULL) {
  633. ret = MEMORY_E;
  634. break;
  635. }
  636. XMEMCPY(workingConfig->raw, echConfig, workingConfig->rawLen);
  637. /* skip over version and length */
  638. echConfig += 4;
  639. /* configId, 1 byte */
  640. workingConfig->configId = *(echConfig);
  641. echConfig++;
  642. /* kemId, 2 bytes */
  643. ato16(echConfig, &workingConfig->kemId);
  644. echConfig += 2;
  645. /* hpke public_key length, 2 bytes */
  646. ato16(echConfig, &hpkePubkeyLen);
  647. echConfig += 2;
  648. /* hpke public_key */
  649. XMEMCPY(workingConfig->receiverPubkey, echConfig, hpkePubkeyLen);
  650. echConfig += hpkePubkeyLen;
  651. /* cipherSuitesLen */
  652. ato16(echConfig, &cipherSuitesLen);
  653. workingConfig->cipherSuites = (EchCipherSuite*)XMALLOC(cipherSuitesLen,
  654. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  655. if (workingConfig->cipherSuites == NULL) {
  656. ret = MEMORY_E;
  657. break;
  658. }
  659. echConfig += 2;
  660. workingConfig->numCipherSuites = cipherSuitesLen / 4;
  661. /* cipherSuites */
  662. for (j = 0; j < workingConfig->numCipherSuites; j++) {
  663. ato16(echConfig + j * 4, &workingConfig->cipherSuites[j].kdfId);
  664. ato16(echConfig + j * 4 + 2,
  665. &workingConfig->cipherSuites[j].aeadId);
  666. }
  667. echConfig += cipherSuitesLen;
  668. /* publicNameLen */
  669. ato16(echConfig, &publicNameLen);
  670. workingConfig->publicName = (char*)XMALLOC(publicNameLen + 1,
  671. ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  672. if (workingConfig->publicName == NULL) {
  673. ret = MEMORY_E;
  674. break;
  675. }
  676. echConfig += 2;
  677. /* publicName */
  678. XMEMCPY(workingConfig->publicName, echConfig, publicNameLen);
  679. /* null terminated */
  680. workingConfig->publicName[publicNameLen] = 0;
  681. /* add length to go to next config, +4 for version and length */
  682. i += length + 4;
  683. /* check that we support this config */
  684. for (j = 0; j < HPKE_SUPPORTED_KEM_LEN; j++) {
  685. if (hpkeSupportedKem[j] == workingConfig->kemId)
  686. break;
  687. }
  688. /* if we don't support the kem or at least one cipher suite */
  689. if (j >= HPKE_SUPPORTED_KEM_LEN ||
  690. EchConfigGetSupportedCipherSuite(workingConfig) < 0)
  691. {
  692. XFREE(workingConfig->cipherSuites, ssl->heap,
  693. DYNAMIC_TYPE_TMP_BUFFER);
  694. XFREE(workingConfig->publicName, ssl->heap,
  695. DYNAMIC_TYPE_TMP_BUFFER);
  696. XFREE(workingConfig->raw, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  697. workingConfig = lastConfig;
  698. }
  699. } while ((word32)i < echConfigsLen);
  700. /* if we found valid configs */
  701. if (ret == 0 && configList != NULL) {
  702. ssl->options.useEch = 1;
  703. ssl->echConfigs = configList;
  704. return WOLFSSL_SUCCESS;
  705. }
  706. workingConfig = configList;
  707. while (workingConfig != NULL) {
  708. lastConfig = workingConfig;
  709. workingConfig = workingConfig->next;
  710. XFREE(lastConfig->cipherSuites, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  711. XFREE(lastConfig->publicName, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  712. XFREE(lastConfig->raw, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  713. XFREE(lastConfig, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  714. }
  715. if (ret == 0)
  716. return WOLFSSL_FATAL_ERROR;
  717. return ret;
  718. }
  719. /* get the raw ech config from our struct */
  720. int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
  721. {
  722. int i;
  723. word16 totalLen = 0;
  724. if (config == NULL || (output == NULL && outputLen == NULL))
  725. return BAD_FUNC_ARG;
  726. /* 2 for version */
  727. totalLen += 2;
  728. /* 2 for length */
  729. totalLen += 2;
  730. /* 1 for configId */
  731. totalLen += 1;
  732. /* 2 for kemId */
  733. totalLen += 2;
  734. /* 2 for hpke_len */
  735. totalLen += 2;
  736. /* hpke_pub_key */
  737. switch (config->kemId) {
  738. case DHKEM_P256_HKDF_SHA256:
  739. totalLen += DHKEM_P256_ENC_LEN;
  740. break;
  741. case DHKEM_P384_HKDF_SHA384:
  742. totalLen += DHKEM_P384_ENC_LEN;
  743. break;
  744. case DHKEM_P521_HKDF_SHA512:
  745. totalLen += DHKEM_P521_ENC_LEN;
  746. break;
  747. case DHKEM_X25519_HKDF_SHA256:
  748. totalLen += DHKEM_X25519_ENC_LEN;
  749. break;
  750. case DHKEM_X448_HKDF_SHA512:
  751. totalLen += DHKEM_X448_ENC_LEN;
  752. break;
  753. }
  754. /* cipherSuitesLen */
  755. totalLen += 2;
  756. /* cipherSuites */
  757. totalLen += config->numCipherSuites * 4;
  758. /* public name len */
  759. totalLen += 2;
  760. /* public name */
  761. totalLen += XSTRLEN(config->publicName);
  762. /* trailing zeros */
  763. totalLen += 2;
  764. if (output == NULL) {
  765. *outputLen = totalLen;
  766. return LENGTH_ONLY_E;
  767. }
  768. if (totalLen > *outputLen) {
  769. *outputLen = totalLen;
  770. return INPUT_SIZE_E;
  771. }
  772. /* version */
  773. c16toa(TLSX_ECH, output);
  774. output += 2;
  775. /* length - 4 for version and length itself */
  776. c16toa(totalLen - 4, output);
  777. output += 2;
  778. /* configId */
  779. *output = config->configId;
  780. output++;
  781. /* kemId */
  782. c16toa(config->kemId, output);
  783. output += 2;
  784. /* length and key itself */
  785. switch (config->kemId) {
  786. case DHKEM_P256_HKDF_SHA256:
  787. c16toa(DHKEM_P256_ENC_LEN, output);
  788. output += 2;
  789. XMEMCPY(output, config->receiverPubkey, DHKEM_P256_ENC_LEN);
  790. output += DHKEM_P256_ENC_LEN;
  791. break;
  792. case DHKEM_P384_HKDF_SHA384:
  793. c16toa(DHKEM_P384_ENC_LEN, output);
  794. output += 2;
  795. XMEMCPY(output, config->receiverPubkey, DHKEM_P384_ENC_LEN);
  796. output += DHKEM_P384_ENC_LEN;
  797. break;
  798. case DHKEM_P521_HKDF_SHA512:
  799. c16toa(DHKEM_P521_ENC_LEN, output);
  800. output += 2;
  801. XMEMCPY(output, config->receiverPubkey, DHKEM_P521_ENC_LEN);
  802. output += DHKEM_P521_ENC_LEN;
  803. break;
  804. case DHKEM_X25519_HKDF_SHA256:
  805. c16toa(DHKEM_X25519_ENC_LEN, output);
  806. output += 2;
  807. XMEMCPY(output, config->receiverPubkey, DHKEM_X25519_ENC_LEN);
  808. output += DHKEM_X25519_ENC_LEN;
  809. break;
  810. case DHKEM_X448_HKDF_SHA512:
  811. c16toa(DHKEM_X448_ENC_LEN, output);
  812. output += 2;
  813. XMEMCPY(output, config->receiverPubkey, DHKEM_X448_ENC_LEN);
  814. output += DHKEM_X448_ENC_LEN;
  815. break;
  816. }
  817. /* cipherSuites len */
  818. c16toa(config->numCipherSuites * 4, output);
  819. output += 2;
  820. /* cipherSuites */
  821. for (i = 0; i < config->numCipherSuites; i++) {
  822. c16toa(config->cipherSuites[i].kdfId, output);
  823. output += 2;
  824. c16toa(config->cipherSuites[i].aeadId, output);
  825. output += 2;
  826. }
  827. /* publicName len */
  828. c16toa(XSTRLEN(config->publicName), output);
  829. output += 2;
  830. /* publicName */
  831. XMEMCPY(output, config->publicName,
  832. XSTRLEN(config->publicName));
  833. output += XSTRLEN(config->publicName);
  834. /* terminating zeros */
  835. c16toa(0, output);
  836. /* output += 2; */
  837. *outputLen = totalLen;
  838. return 0;
  839. }
  840. /* wrapper function to get ech configs from application code */
  841. int wolfSSL_GetEchConfigs(WOLFSSL* ssl, byte* output, word32* outputLen)
  842. {
  843. if (ssl == NULL || outputLen == NULL)
  844. return BAD_FUNC_ARG;
  845. /* if we don't have ech configs */
  846. if (ssl->options.useEch != 1) {
  847. return WOLFSSL_FATAL_ERROR;
  848. }
  849. return GetEchConfigsEx(ssl->echConfigs, output, outputLen);
  850. }
  851. /* get the raw ech configs from our linked list of ech config structs */
  852. int GetEchConfigsEx(WOLFSSL_EchConfig* configs, byte* output, word32* outputLen)
  853. {
  854. int ret = 0;
  855. WOLFSSL_EchConfig* workingConfig = NULL;
  856. byte* outputStart = output;
  857. word32 totalLen = 2;
  858. word32 workingOutputLen;
  859. if (configs == NULL || outputLen == NULL)
  860. return BAD_FUNC_ARG;
  861. workingOutputLen = *outputLen - totalLen;
  862. /* skip over total length which we fill in later */
  863. if (output != NULL)
  864. output += 2;
  865. workingConfig = configs;
  866. while (workingConfig != NULL) {
  867. /* get this config */
  868. ret = GetEchConfig(workingConfig, output, &workingOutputLen);
  869. if (output != NULL)
  870. output += workingOutputLen;
  871. /* add this config's length to the total length */
  872. totalLen += workingOutputLen;
  873. if (totalLen > *outputLen)
  874. workingOutputLen = 0;
  875. else
  876. workingOutputLen = *outputLen - totalLen;
  877. /* only error we break on, other 2 we need to keep finding length */
  878. if (ret == BAD_FUNC_ARG)
  879. return BAD_FUNC_ARG;
  880. workingConfig = workingConfig->next;
  881. }
  882. if (output == NULL) {
  883. *outputLen = totalLen;
  884. return LENGTH_ONLY_E;
  885. }
  886. if (totalLen > *outputLen) {
  887. *outputLen = totalLen;
  888. return INPUT_SIZE_E;
  889. }
  890. /* total size -2 for size itself */
  891. c16toa(totalLen - 2, outputStart);
  892. *outputLen = totalLen;
  893. return WOLFSSL_SUCCESS;
  894. }
  895. #endif /* WOLFSSL_TLS13 && HAVE_ECH */
  896. #if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_FSPSM_TLS)
  897. #include <wolfssl/wolfcrypt/port/Renesas/renesas_cmn.h>
  898. #endif
  899. /* prevent multiple mutex initializations */
  900. static volatile WOLFSSL_GLOBAL int initRefCount = 0;
  901. /* init ref count mutex */
  902. static WOLFSSL_GLOBAL wolfSSL_Mutex inits_count_mutex
  903. WOLFSSL_MUTEX_INITIALIZER_CLAUSE(inits_count_mutex);
  904. #ifndef WOLFSSL_MUTEX_INITIALIZER
  905. static WOLFSSL_GLOBAL int inits_count_mutex_valid = 0;
  906. #endif
  907. /* Create a new WOLFSSL_CTX struct and return the pointer to created struct.
  908. WOLFSSL_METHOD pointer passed in is given to ctx to manage.
  909. This function frees the passed in WOLFSSL_METHOD struct on failure and on
  910. success is freed when ctx is freed.
  911. */
  912. WOLFSSL_CTX* wolfSSL_CTX_new_ex(WOLFSSL_METHOD* method, void* heap)
  913. {
  914. WOLFSSL_CTX* ctx = NULL;
  915. WOLFSSL_ENTER("wolfSSL_CTX_new_ex");
  916. if (initRefCount == 0) {
  917. /* user no longer forced to call Init themselves */
  918. int ret = wolfSSL_Init();
  919. if (ret != WOLFSSL_SUCCESS) {
  920. WOLFSSL_MSG("wolfSSL_Init failed");
  921. WOLFSSL_LEAVE("wolfSSL_CTX_new_ex", 0);
  922. if (method != NULL) {
  923. XFREE(method, heap, DYNAMIC_TYPE_METHOD);
  924. }
  925. return NULL;
  926. }
  927. }
  928. if (method == NULL)
  929. return ctx;
  930. ctx = (WOLFSSL_CTX*)XMALLOC(sizeof(WOLFSSL_CTX), heap, DYNAMIC_TYPE_CTX);
  931. if (ctx) {
  932. int ret;
  933. ret = InitSSL_Ctx(ctx, method, heap);
  934. #ifdef WOLFSSL_STATIC_MEMORY
  935. if (heap != NULL) {
  936. ctx->onHeapHint = 1; /* free the memory back to heap when done */
  937. }
  938. #endif
  939. if (ret < 0) {
  940. WOLFSSL_MSG("Init CTX failed");
  941. wolfSSL_CTX_free(ctx);
  942. ctx = NULL;
  943. }
  944. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  945. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  946. else {
  947. ctx->srp = (Srp*)XMALLOC(sizeof(Srp), heap, DYNAMIC_TYPE_SRP);
  948. if (ctx->srp == NULL){
  949. WOLFSSL_MSG("Init CTX failed");
  950. wolfSSL_CTX_free(ctx);
  951. return NULL;
  952. }
  953. XMEMSET(ctx->srp, 0, sizeof(Srp));
  954. }
  955. #endif
  956. }
  957. else {
  958. WOLFSSL_MSG("Alloc CTX failed, method freed");
  959. XFREE(method, heap, DYNAMIC_TYPE_METHOD);
  960. }
  961. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  962. if (ctx) {
  963. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
  964. wolfSSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
  965. if (wolfSSL_CTX_set_min_proto_version(ctx,
  966. (method->version.major == DTLS_MAJOR) ?
  967. DTLS1_VERSION : SSL3_VERSION) != WOLFSSL_SUCCESS ||
  968. #ifdef HAVE_ANON
  969. wolfSSL_CTX_allow_anon_cipher(ctx) != WOLFSSL_SUCCESS ||
  970. #endif
  971. wolfSSL_CTX_set_group_messages(ctx) != WOLFSSL_SUCCESS) {
  972. WOLFSSL_MSG("Setting OpenSSL CTX defaults failed");
  973. wolfSSL_CTX_free(ctx);
  974. ctx = NULL;
  975. }
  976. }
  977. #endif
  978. WOLFSSL_LEAVE("wolfSSL_CTX_new_ex", 0);
  979. return ctx;
  980. }
  981. WOLFSSL_ABI
  982. WOLFSSL_CTX* wolfSSL_CTX_new(WOLFSSL_METHOD* method)
  983. {
  984. #ifdef WOLFSSL_HEAP_TEST
  985. /* if testing the heap hint then set top level CTX to have test value */
  986. return wolfSSL_CTX_new_ex(method, (void*)WOLFSSL_HEAP_TEST);
  987. #else
  988. return wolfSSL_CTX_new_ex(method, NULL);
  989. #endif
  990. }
  991. /* increases CTX reference count to track proper time to "free" */
  992. int wolfSSL_CTX_up_ref(WOLFSSL_CTX* ctx)
  993. {
  994. int ret;
  995. wolfSSL_RefInc(&ctx->ref, &ret);
  996. #ifdef WOLFSSL_REFCNT_ERROR_RETURN
  997. return ((ret == 0) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE);
  998. #else
  999. (void)ret;
  1000. return WOLFSSL_SUCCESS;
  1001. #endif
  1002. }
  1003. WOLFSSL_ABI
  1004. void wolfSSL_CTX_free(WOLFSSL_CTX* ctx)
  1005. {
  1006. WOLFSSL_ENTER("wolfSSL_CTX_free");
  1007. if (ctx) {
  1008. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  1009. && !defined(NO_SHA256) && !defined(WC_NO_RNG)
  1010. if (ctx->srp != NULL) {
  1011. if (ctx->srp_password != NULL){
  1012. XFREE(ctx->srp_password, ctx->heap, DYNAMIC_TYPE_SRP);
  1013. ctx->srp_password = NULL;
  1014. }
  1015. wc_SrpTerm(ctx->srp);
  1016. XFREE(ctx->srp, ctx->heap, DYNAMIC_TYPE_SRP);
  1017. ctx->srp = NULL;
  1018. }
  1019. #endif
  1020. FreeSSL_Ctx(ctx);
  1021. }
  1022. WOLFSSL_LEAVE("wolfSSL_CTX_free", 0);
  1023. }
  1024. #ifdef HAVE_ENCRYPT_THEN_MAC
  1025. /**
  1026. * Sets whether Encrypt-Then-MAC extension can be negotiated against context.
  1027. * The default value: enabled.
  1028. *
  1029. * ctx SSL/TLS context.
  1030. * set Whether to allow or not: 1 is allow and 0 is disallow.
  1031. * returns WOLFSSL_SUCCESS
  1032. */
  1033. int wolfSSL_CTX_AllowEncryptThenMac(WOLFSSL_CTX *ctx, int set)
  1034. {
  1035. ctx->disallowEncThenMac = !set;
  1036. return WOLFSSL_SUCCESS;
  1037. }
  1038. /**
  1039. * Sets whether Encrypt-Then-MAC extension can be negotiated against context.
  1040. * The default value comes from context.
  1041. *
  1042. * ctx SSL/TLS context.
  1043. * set Whether to allow or not: 1 is allow and 0 is disallow.
  1044. * returns WOLFSSL_SUCCESS
  1045. */
  1046. int wolfSSL_AllowEncryptThenMac(WOLFSSL *ssl, int set)
  1047. {
  1048. ssl->options.disallowEncThenMac = !set;
  1049. return WOLFSSL_SUCCESS;
  1050. }
  1051. #endif
  1052. #ifdef SINGLE_THREADED
  1053. /* no locking in single threaded mode, allow a CTX level rng to be shared with
  1054. * WOLFSSL objects, WOLFSSL_SUCCESS on ok */
  1055. int wolfSSL_CTX_new_rng(WOLFSSL_CTX* ctx)
  1056. {
  1057. WC_RNG* rng;
  1058. int ret;
  1059. if (ctx == NULL) {
  1060. return BAD_FUNC_ARG;
  1061. }
  1062. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), ctx->heap, DYNAMIC_TYPE_RNG);
  1063. if (rng == NULL) {
  1064. return MEMORY_E;
  1065. }
  1066. #ifndef HAVE_FIPS
  1067. ret = wc_InitRng_ex(rng, ctx->heap, ctx->devId);
  1068. #else
  1069. ret = wc_InitRng(rng);
  1070. #endif
  1071. if (ret != 0) {
  1072. XFREE(rng, ctx->heap, DYNAMIC_TYPE_RNG);
  1073. return ret;
  1074. }
  1075. ctx->rng = rng;
  1076. return WOLFSSL_SUCCESS;
  1077. }
  1078. #endif
  1079. WOLFSSL_ABI
  1080. WOLFSSL* wolfSSL_new(WOLFSSL_CTX* ctx)
  1081. {
  1082. WOLFSSL* ssl = NULL;
  1083. int ret = 0;
  1084. WOLFSSL_ENTER("wolfSSL_new");
  1085. if (ctx == NULL) {
  1086. WOLFSSL_MSG("wolfSSL_new ctx is null");
  1087. return NULL;
  1088. }
  1089. ssl = (WOLFSSL*) XMALLOC(sizeof(WOLFSSL), ctx->heap, DYNAMIC_TYPE_SSL);
  1090. if (ssl == NULL) {
  1091. WOLFSSL_MSG_EX("ssl xmalloc failed to allocate %d bytes",
  1092. (int)sizeof(WOLFSSL));
  1093. }
  1094. else {
  1095. ret = InitSSL(ssl, ctx, 0);
  1096. if (ret < 0) {
  1097. WOLFSSL_MSG_EX("wolfSSL_new failed during InitSSL. err = %d", ret);
  1098. FreeSSL(ssl, ctx->heap);
  1099. ssl = NULL;
  1100. }
  1101. else if (ret == 0) {
  1102. WOLFSSL_MSG("wolfSSL_new InitSSL success");
  1103. }
  1104. else {
  1105. /* Only success (0) or negative values should ever be seen. */
  1106. WOLFSSL_MSG_EX("WARNING: wolfSSL_new unexpected InitSSL return"
  1107. " value = %d", ret);
  1108. } /* InitSSL check */
  1109. } /* ssl XMALLOC success */
  1110. WOLFSSL_LEAVE("wolfSSL_new InitSSL =", ret);
  1111. (void)ret;
  1112. return ssl;
  1113. }
  1114. WOLFSSL_ABI
  1115. void wolfSSL_free(WOLFSSL* ssl)
  1116. {
  1117. WOLFSSL_ENTER("wolfSSL_free");
  1118. if (ssl) {
  1119. WOLFSSL_MSG_EX("Free SSL: %p", (wc_ptr_t)ssl);
  1120. FreeSSL(ssl, ssl->ctx->heap);
  1121. }
  1122. else {
  1123. WOLFSSL_MSG("Free SSL: wolfSSL_free already null");
  1124. }
  1125. WOLFSSL_LEAVE("wolfSSL_free", 0);
  1126. }
  1127. int wolfSSL_is_server(WOLFSSL* ssl)
  1128. {
  1129. if (ssl == NULL)
  1130. return BAD_FUNC_ARG;
  1131. return ssl->options.side == WOLFSSL_SERVER_END;
  1132. }
  1133. #ifdef HAVE_WRITE_DUP
  1134. /*
  1135. * Release resources around WriteDup object
  1136. *
  1137. * ssl WOLFSSL object
  1138. *
  1139. * no return, destruction so make best attempt
  1140. */
  1141. void FreeWriteDup(WOLFSSL* ssl)
  1142. {
  1143. int doFree = 0;
  1144. WOLFSSL_ENTER("FreeWriteDup");
  1145. if (ssl->dupWrite) {
  1146. if (wc_LockMutex(&ssl->dupWrite->dupMutex) == 0) {
  1147. ssl->dupWrite->dupCount--;
  1148. if (ssl->dupWrite->dupCount == 0) {
  1149. doFree = 1;
  1150. } else {
  1151. WOLFSSL_MSG("WriteDup count not zero, no full free");
  1152. }
  1153. wc_UnLockMutex(&ssl->dupWrite->dupMutex);
  1154. }
  1155. }
  1156. if (doFree) {
  1157. WOLFSSL_MSG("Doing WriteDup full free, count to zero");
  1158. wc_FreeMutex(&ssl->dupWrite->dupMutex);
  1159. XFREE(ssl->dupWrite, ssl->heap, DYNAMIC_TYPE_WRITEDUP);
  1160. }
  1161. }
  1162. /*
  1163. * duplicate existing ssl members into dup needed for writing
  1164. *
  1165. * dup write only WOLFSSL
  1166. * ssl existing WOLFSSL
  1167. *
  1168. * 0 on success
  1169. */
  1170. static int DupSSL(WOLFSSL* dup, WOLFSSL* ssl)
  1171. {
  1172. word16 tmp_weOwnRng;
  1173. /* shared dupWrite setup */
  1174. ssl->dupWrite = (WriteDup*)XMALLOC(sizeof(WriteDup), ssl->heap,
  1175. DYNAMIC_TYPE_WRITEDUP);
  1176. if (ssl->dupWrite == NULL) {
  1177. return MEMORY_E;
  1178. }
  1179. XMEMSET(ssl->dupWrite, 0, sizeof(WriteDup));
  1180. if (wc_InitMutex(&ssl->dupWrite->dupMutex) != 0) {
  1181. XFREE(ssl->dupWrite, ssl->heap, DYNAMIC_TYPE_WRITEDUP);
  1182. ssl->dupWrite = NULL;
  1183. return BAD_MUTEX_E;
  1184. }
  1185. ssl->dupWrite->dupCount = 2; /* both sides have a count to start */
  1186. dup->dupWrite = ssl->dupWrite; /* each side uses */
  1187. tmp_weOwnRng = dup->options.weOwnRng;
  1188. /* copy write parts over to dup writer */
  1189. XMEMCPY(&dup->specs, &ssl->specs, sizeof(CipherSpecs));
  1190. XMEMCPY(&dup->options, &ssl->options, sizeof(Options));
  1191. XMEMCPY(&dup->keys, &ssl->keys, sizeof(Keys));
  1192. XMEMCPY(&dup->encrypt, &ssl->encrypt, sizeof(Ciphers));
  1193. XMEMCPY(&dup->version, &ssl->version, sizeof(ProtocolVersion));
  1194. XMEMCPY(&dup->chVersion, &ssl->chVersion, sizeof(ProtocolVersion));
  1195. #ifdef HAVE_ONE_TIME_AUTH
  1196. #ifdef HAVE_POLY1305
  1197. if (ssl->auth.setup && ssl->auth.poly1305 != NULL) {
  1198. dup->auth.poly1305 = (Poly1305*)XMALLOC(sizeof(Poly1305), dup->heap,
  1199. DYNAMIC_TYPE_CIPHER);
  1200. if (dup->auth.poly1305 == NULL)
  1201. return MEMORY_E;
  1202. dup->auth.setup = 1;
  1203. }
  1204. #endif
  1205. #endif
  1206. /* dup side now owns encrypt/write ciphers */
  1207. XMEMSET(&ssl->encrypt, 0, sizeof(Ciphers));
  1208. dup->IOCB_WriteCtx = ssl->IOCB_WriteCtx;
  1209. dup->CBIOSend = ssl->CBIOSend;
  1210. #ifdef OPENSSL_EXTRA
  1211. dup->cbioFlag = ssl->cbioFlag;
  1212. #endif
  1213. dup->wfd = ssl->wfd;
  1214. dup->wflags = ssl->wflags;
  1215. #ifndef WOLFSSL_AEAD_ONLY
  1216. dup->hmac = ssl->hmac;
  1217. #endif
  1218. #ifdef HAVE_TRUNCATED_HMAC
  1219. dup->truncated_hmac = ssl->truncated_hmac;
  1220. #endif
  1221. /* Restore rng option */
  1222. dup->options.weOwnRng = tmp_weOwnRng;
  1223. /* unique side dup setup */
  1224. dup->dupSide = WRITE_DUP_SIDE;
  1225. ssl->dupSide = READ_DUP_SIDE;
  1226. return 0;
  1227. }
  1228. /*
  1229. * duplicate a WOLFSSL object post handshake for writing only
  1230. * turn existing object into read only. Allows concurrent access from two
  1231. * different threads.
  1232. *
  1233. * ssl existing WOLFSSL object
  1234. *
  1235. * return dup'd WOLFSSL object on success
  1236. */
  1237. WOLFSSL* wolfSSL_write_dup(WOLFSSL* ssl)
  1238. {
  1239. WOLFSSL* dup = NULL;
  1240. int ret = 0;
  1241. (void)ret;
  1242. WOLFSSL_ENTER("wolfSSL_write_dup");
  1243. if (ssl == NULL) {
  1244. return ssl;
  1245. }
  1246. if (ssl->options.handShakeDone == 0) {
  1247. WOLFSSL_MSG("wolfSSL_write_dup called before handshake complete");
  1248. return NULL;
  1249. }
  1250. if (ssl->dupWrite) {
  1251. WOLFSSL_MSG("wolfSSL_write_dup already called once");
  1252. return NULL;
  1253. }
  1254. dup = (WOLFSSL*) XMALLOC(sizeof(WOLFSSL), ssl->ctx->heap, DYNAMIC_TYPE_SSL);
  1255. if (dup) {
  1256. if ( (ret = InitSSL(dup, ssl->ctx, 1)) < 0) {
  1257. FreeSSL(dup, ssl->ctx->heap);
  1258. dup = NULL;
  1259. } else if ( (ret = DupSSL(dup, ssl)) < 0) {
  1260. FreeSSL(dup, ssl->ctx->heap);
  1261. dup = NULL;
  1262. }
  1263. }
  1264. WOLFSSL_LEAVE("wolfSSL_write_dup", ret);
  1265. return dup;
  1266. }
  1267. /*
  1268. * Notify write dup side of fatal error or close notify
  1269. *
  1270. * ssl WOLFSSL object
  1271. * err Notify err
  1272. *
  1273. * 0 on success
  1274. */
  1275. int NotifyWriteSide(WOLFSSL* ssl, int err)
  1276. {
  1277. int ret;
  1278. WOLFSSL_ENTER("NotifyWriteSide");
  1279. ret = wc_LockMutex(&ssl->dupWrite->dupMutex);
  1280. if (ret == 0) {
  1281. ssl->dupWrite->dupErr = err;
  1282. ret = wc_UnLockMutex(&ssl->dupWrite->dupMutex);
  1283. }
  1284. return ret;
  1285. }
  1286. #endif /* HAVE_WRITE_DUP */
  1287. #ifdef HAVE_POLY1305
  1288. /* set if to use old poly 1 for yes 0 to use new poly */
  1289. int wolfSSL_use_old_poly(WOLFSSL* ssl, int value)
  1290. {
  1291. (void)ssl;
  1292. (void)value;
  1293. #ifndef WOLFSSL_NO_TLS12
  1294. WOLFSSL_ENTER("wolfSSL_use_old_poly");
  1295. WOLFSSL_MSG("Warning SSL connection auto detects old/new and this function"
  1296. "is depreciated");
  1297. ssl->options.oldPoly = (word16)value;
  1298. WOLFSSL_LEAVE("wolfSSL_use_old_poly", 0);
  1299. #endif
  1300. return 0;
  1301. }
  1302. #endif
  1303. WOLFSSL_ABI
  1304. int wolfSSL_set_fd(WOLFSSL* ssl, int fd)
  1305. {
  1306. int ret;
  1307. WOLFSSL_ENTER("wolfSSL_set_fd");
  1308. if (ssl == NULL) {
  1309. return BAD_FUNC_ARG;
  1310. }
  1311. ret = wolfSSL_set_read_fd(ssl, fd);
  1312. if (ret == WOLFSSL_SUCCESS) {
  1313. ret = wolfSSL_set_write_fd(ssl, fd);
  1314. }
  1315. return ret;
  1316. }
  1317. #ifdef WOLFSSL_DTLS
  1318. int wolfSSL_set_dtls_fd_connected(WOLFSSL* ssl, int fd)
  1319. {
  1320. int ret;
  1321. WOLFSSL_ENTER("wolfSSL_set_dtls_fd_connected");
  1322. if (ssl == NULL) {
  1323. return BAD_FUNC_ARG;
  1324. }
  1325. ret = wolfSSL_set_fd(ssl, fd);
  1326. if (ret == WOLFSSL_SUCCESS)
  1327. ssl->buffers.dtlsCtx.connected = 1;
  1328. return ret;
  1329. }
  1330. #endif
  1331. int wolfSSL_set_read_fd(WOLFSSL* ssl, int fd)
  1332. {
  1333. WOLFSSL_ENTER("wolfSSL_set_read_fd");
  1334. if (ssl == NULL) {
  1335. return BAD_FUNC_ARG;
  1336. }
  1337. ssl->rfd = fd; /* not used directly to allow IO callbacks */
  1338. ssl->IOCB_ReadCtx = &ssl->rfd;
  1339. #ifdef WOLFSSL_DTLS
  1340. ssl->buffers.dtlsCtx.connected = 0;
  1341. if (ssl->options.dtls) {
  1342. ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx;
  1343. ssl->buffers.dtlsCtx.rfd = fd;
  1344. }
  1345. #endif
  1346. WOLFSSL_LEAVE("wolfSSL_set_read_fd", WOLFSSL_SUCCESS);
  1347. return WOLFSSL_SUCCESS;
  1348. }
  1349. int wolfSSL_set_write_fd(WOLFSSL* ssl, int fd)
  1350. {
  1351. WOLFSSL_ENTER("wolfSSL_set_write_fd");
  1352. if (ssl == NULL) {
  1353. return BAD_FUNC_ARG;
  1354. }
  1355. ssl->wfd = fd; /* not used directly to allow IO callbacks */
  1356. ssl->IOCB_WriteCtx = &ssl->wfd;
  1357. #ifdef WOLFSSL_DTLS
  1358. ssl->buffers.dtlsCtx.connected = 0;
  1359. if (ssl->options.dtls) {
  1360. ssl->IOCB_WriteCtx = &ssl->buffers.dtlsCtx;
  1361. ssl->buffers.dtlsCtx.wfd = fd;
  1362. }
  1363. #endif
  1364. WOLFSSL_LEAVE("wolfSSL_set_write_fd", WOLFSSL_SUCCESS);
  1365. return WOLFSSL_SUCCESS;
  1366. }
  1367. /**
  1368. * Get the name of cipher at priority level passed in.
  1369. */
  1370. char* wolfSSL_get_cipher_list(int priority)
  1371. {
  1372. const CipherSuiteInfo* ciphers = GetCipherNames();
  1373. if (priority >= GetCipherNamesSize() || priority < 0) {
  1374. return 0;
  1375. }
  1376. return (char*)ciphers[priority].name;
  1377. }
  1378. /**
  1379. * Get the name of cipher at priority level passed in.
  1380. */
  1381. char* wolfSSL_get_cipher_list_ex(WOLFSSL* ssl, int priority)
  1382. {
  1383. if (ssl == NULL) {
  1384. return NULL;
  1385. }
  1386. else {
  1387. const char* cipher;
  1388. if ((cipher = wolfSSL_get_cipher_name_internal(ssl)) != NULL) {
  1389. if (priority == 0) {
  1390. return (char*)cipher;
  1391. }
  1392. else {
  1393. return NULL;
  1394. }
  1395. }
  1396. else {
  1397. return wolfSSL_get_cipher_list(priority);
  1398. }
  1399. }
  1400. }
  1401. int wolfSSL_get_ciphers(char* buf, int len)
  1402. {
  1403. const CipherSuiteInfo* ciphers = GetCipherNames();
  1404. int ciphersSz = GetCipherNamesSize();
  1405. int i;
  1406. if (buf == NULL || len <= 0)
  1407. return BAD_FUNC_ARG;
  1408. /* Add each member to the buffer delimited by a : */
  1409. for (i = 0; i < ciphersSz; i++) {
  1410. int cipherNameSz = (int)XSTRLEN(ciphers[i].name);
  1411. if (cipherNameSz + 1 < len) {
  1412. XSTRNCPY(buf, ciphers[i].name, len);
  1413. buf += cipherNameSz;
  1414. if (i < ciphersSz - 1)
  1415. *buf++ = ':';
  1416. *buf = 0;
  1417. len -= cipherNameSz + 1;
  1418. }
  1419. else
  1420. return BUFFER_E;
  1421. }
  1422. return WOLFSSL_SUCCESS;
  1423. }
  1424. #ifndef NO_ERROR_STRINGS
  1425. /* places a list of all supported cipher suites in TLS_* format into "buf"
  1426. * return WOLFSSL_SUCCESS on success */
  1427. int wolfSSL_get_ciphers_iana(char* buf, int len)
  1428. {
  1429. const CipherSuiteInfo* ciphers = GetCipherNames();
  1430. int ciphersSz = GetCipherNamesSize();
  1431. int i;
  1432. int cipherNameSz;
  1433. if (buf == NULL || len <= 0)
  1434. return BAD_FUNC_ARG;
  1435. /* Add each member to the buffer delimited by a : */
  1436. for (i = 0; i < ciphersSz; i++) {
  1437. #ifndef NO_CIPHER_SUITE_ALIASES
  1438. if (ciphers[i].flags & WOLFSSL_CIPHER_SUITE_FLAG_NAMEALIAS)
  1439. continue;
  1440. #endif
  1441. cipherNameSz = (int)XSTRLEN(ciphers[i].name_iana);
  1442. if (cipherNameSz + 1 < len) {
  1443. XSTRNCPY(buf, ciphers[i].name_iana, len);
  1444. buf += cipherNameSz;
  1445. if (i < ciphersSz - 1)
  1446. *buf++ = ':';
  1447. *buf = 0;
  1448. len -= cipherNameSz + 1;
  1449. }
  1450. else
  1451. return BUFFER_E;
  1452. }
  1453. return WOLFSSL_SUCCESS;
  1454. }
  1455. #endif /* NO_ERROR_STRINGS */
  1456. const char* wolfSSL_get_shared_ciphers(WOLFSSL* ssl, char* buf, int len)
  1457. {
  1458. const char* cipher;
  1459. if (ssl == NULL)
  1460. return NULL;
  1461. cipher = wolfSSL_get_cipher_name_iana(ssl);
  1462. len = min(len, (int)(XSTRLEN(cipher) + 1));
  1463. XMEMCPY(buf, cipher, len);
  1464. return buf;
  1465. }
  1466. int wolfSSL_get_fd(const WOLFSSL* ssl)
  1467. {
  1468. int fd = -1;
  1469. WOLFSSL_ENTER("wolfSSL_get_fd");
  1470. if (ssl) {
  1471. fd = ssl->rfd;
  1472. }
  1473. WOLFSSL_LEAVE("wolfSSL_get_fd", fd);
  1474. return fd;
  1475. }
  1476. int wolfSSL_dtls(WOLFSSL* ssl)
  1477. {
  1478. int dtlsOpt = 0;
  1479. if (ssl)
  1480. dtlsOpt = ssl->options.dtls;
  1481. return dtlsOpt;
  1482. }
  1483. #if !defined(NO_CERTS)
  1484. /* Set whether mutual authentication is required for connections.
  1485. * Server side only.
  1486. *
  1487. * ctx The SSL/TLS CTX object.
  1488. * req 1 to indicate required and 0 when not.
  1489. * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and
  1490. * 0 on success.
  1491. */
  1492. int wolfSSL_CTX_mutual_auth(WOLFSSL_CTX* ctx, int req)
  1493. {
  1494. if (ctx == NULL)
  1495. return BAD_FUNC_ARG;
  1496. if (ctx->method->side == WOLFSSL_CLIENT_END)
  1497. return SIDE_ERROR;
  1498. ctx->mutualAuth = (byte)req;
  1499. return 0;
  1500. }
  1501. /* Set whether mutual authentication is required for the connection.
  1502. * Server side only.
  1503. *
  1504. * ssl The SSL/TLS object.
  1505. * req 1 to indicate required and 0 when not.
  1506. * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3,
  1507. * SIDE_ERROR when not a client and 0 on success.
  1508. */
  1509. int wolfSSL_mutual_auth(WOLFSSL* ssl, int req)
  1510. {
  1511. if (ssl == NULL)
  1512. return BAD_FUNC_ARG;
  1513. if (ssl->options.side == WOLFSSL_SERVER_END)
  1514. return SIDE_ERROR;
  1515. ssl->options.mutualAuth = (word16)req;
  1516. return 0;
  1517. }
  1518. #endif /* NO_CERTS */
  1519. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  1520. int wolfSSL_CTX_set_AcceptFilter(
  1521. WOLFSSL_CTX *ctx,
  1522. NetworkFilterCallback_t AcceptFilter,
  1523. void *AcceptFilter_arg)
  1524. {
  1525. if (ctx == NULL)
  1526. return BAD_FUNC_ARG;
  1527. ctx->AcceptFilter = AcceptFilter;
  1528. ctx->AcceptFilter_arg = AcceptFilter_arg;
  1529. return 0;
  1530. }
  1531. int wolfSSL_set_AcceptFilter(
  1532. WOLFSSL *ssl,
  1533. NetworkFilterCallback_t AcceptFilter,
  1534. void *AcceptFilter_arg)
  1535. {
  1536. if (ssl == NULL)
  1537. return BAD_FUNC_ARG;
  1538. ssl->AcceptFilter = AcceptFilter;
  1539. ssl->AcceptFilter_arg = AcceptFilter_arg;
  1540. return 0;
  1541. }
  1542. int wolfSSL_CTX_set_ConnectFilter(
  1543. WOLFSSL_CTX *ctx,
  1544. NetworkFilterCallback_t ConnectFilter,
  1545. void *ConnectFilter_arg)
  1546. {
  1547. if (ctx == NULL)
  1548. return BAD_FUNC_ARG;
  1549. ctx->ConnectFilter = ConnectFilter;
  1550. ctx->ConnectFilter_arg = ConnectFilter_arg;
  1551. return 0;
  1552. }
  1553. int wolfSSL_set_ConnectFilter(
  1554. WOLFSSL *ssl,
  1555. NetworkFilterCallback_t ConnectFilter,
  1556. void *ConnectFilter_arg)
  1557. {
  1558. if (ssl == NULL)
  1559. return BAD_FUNC_ARG;
  1560. ssl->ConnectFilter = ConnectFilter;
  1561. ssl->ConnectFilter_arg = ConnectFilter_arg;
  1562. return 0;
  1563. }
  1564. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  1565. #ifndef WOLFSSL_LEANPSK
  1566. #if defined(WOLFSSL_DTLS) && defined(XINET_PTON) && \
  1567. !defined(WOLFSSL_NO_SOCK) && defined(HAVE_SOCKADDR)
  1568. void* wolfSSL_dtls_create_peer(int port, char* ip)
  1569. {
  1570. SOCKADDR_IN *addr;
  1571. addr = (SOCKADDR_IN*)XMALLOC(sizeof(*addr), NULL,
  1572. DYNAMIC_TYPE_SOCKADDR);
  1573. if (addr == NULL) {
  1574. return NULL;
  1575. }
  1576. addr->sin_family = AF_INET;
  1577. addr->sin_port = XHTONS((word16)port);
  1578. if (XINET_PTON(AF_INET, ip, &addr->sin_addr) < 1) {
  1579. XFREE(addr, NULL, DYNAMIC_TYPE_SOCKADDR);
  1580. return NULL;
  1581. }
  1582. return addr;
  1583. }
  1584. int wolfSSL_dtls_free_peer(void* addr)
  1585. {
  1586. XFREE(addr, NULL, DYNAMIC_TYPE_SOCKADDR);
  1587. return WOLFSSL_SUCCESS;
  1588. }
  1589. #endif
  1590. int wolfSSL_dtls_set_peer(WOLFSSL* ssl, void* peer, unsigned int peerSz)
  1591. {
  1592. #ifdef WOLFSSL_DTLS
  1593. void* sa;
  1594. if (ssl == NULL)
  1595. return WOLFSSL_FAILURE;
  1596. if (peer == NULL || peerSz == 0) {
  1597. if (ssl->buffers.dtlsCtx.peer.sa != NULL)
  1598. XFREE(ssl->buffers.dtlsCtx.peer.sa,ssl->heap,DYNAMIC_TYPE_SOCKADDR);
  1599. ssl->buffers.dtlsCtx.peer.sa = NULL;
  1600. ssl->buffers.dtlsCtx.peer.sz = 0;
  1601. ssl->buffers.dtlsCtx.peer.bufSz = 0;
  1602. ssl->buffers.dtlsCtx.userSet = 0;
  1603. return WOLFSSL_SUCCESS;
  1604. }
  1605. sa = (void*)XMALLOC(peerSz, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
  1606. if (sa != NULL) {
  1607. if (ssl->buffers.dtlsCtx.peer.sa != NULL) {
  1608. XFREE(ssl->buffers.dtlsCtx.peer.sa,ssl->heap,DYNAMIC_TYPE_SOCKADDR);
  1609. ssl->buffers.dtlsCtx.peer.sa = NULL;
  1610. }
  1611. XMEMCPY(sa, peer, peerSz);
  1612. ssl->buffers.dtlsCtx.peer.sa = sa;
  1613. ssl->buffers.dtlsCtx.peer.sz = peerSz;
  1614. ssl->buffers.dtlsCtx.peer.bufSz = peerSz;
  1615. ssl->buffers.dtlsCtx.userSet = 1;
  1616. return WOLFSSL_SUCCESS;
  1617. }
  1618. return WOLFSSL_FAILURE;
  1619. #else
  1620. (void)ssl;
  1621. (void)peer;
  1622. (void)peerSz;
  1623. return WOLFSSL_NOT_IMPLEMENTED;
  1624. #endif
  1625. }
  1626. int wolfSSL_dtls_get_peer(WOLFSSL* ssl, void* peer, unsigned int* peerSz)
  1627. {
  1628. #ifdef WOLFSSL_DTLS
  1629. if (ssl == NULL) {
  1630. return WOLFSSL_FAILURE;
  1631. }
  1632. if (peer != NULL && peerSz != NULL
  1633. && *peerSz >= ssl->buffers.dtlsCtx.peer.sz
  1634. && ssl->buffers.dtlsCtx.peer.sa != NULL) {
  1635. *peerSz = ssl->buffers.dtlsCtx.peer.sz;
  1636. XMEMCPY(peer, ssl->buffers.dtlsCtx.peer.sa, *peerSz);
  1637. return WOLFSSL_SUCCESS;
  1638. }
  1639. return WOLFSSL_FAILURE;
  1640. #else
  1641. (void)ssl;
  1642. (void)peer;
  1643. (void)peerSz;
  1644. return WOLFSSL_NOT_IMPLEMENTED;
  1645. #endif
  1646. }
  1647. #if defined(WOLFSSL_SCTP) && defined(WOLFSSL_DTLS)
  1648. int wolfSSL_CTX_dtls_set_sctp(WOLFSSL_CTX* ctx)
  1649. {
  1650. WOLFSSL_ENTER("wolfSSL_CTX_dtls_set_sctp");
  1651. if (ctx == NULL)
  1652. return BAD_FUNC_ARG;
  1653. ctx->dtlsSctp = 1;
  1654. return WOLFSSL_SUCCESS;
  1655. }
  1656. int wolfSSL_dtls_set_sctp(WOLFSSL* ssl)
  1657. {
  1658. WOLFSSL_ENTER("wolfSSL_dtls_set_sctp");
  1659. if (ssl == NULL)
  1660. return BAD_FUNC_ARG;
  1661. ssl->options.dtlsSctp = 1;
  1662. return WOLFSSL_SUCCESS;
  1663. }
  1664. #endif /* WOLFSSL_DTLS && WOLFSSL_SCTP */
  1665. #if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
  1666. defined(WOLFSSL_DTLS)
  1667. int wolfSSL_CTX_dtls_set_mtu(WOLFSSL_CTX* ctx, word16 newMtu)
  1668. {
  1669. if (ctx == NULL || newMtu > MAX_RECORD_SIZE)
  1670. return BAD_FUNC_ARG;
  1671. ctx->dtlsMtuSz = newMtu;
  1672. return WOLFSSL_SUCCESS;
  1673. }
  1674. int wolfSSL_dtls_set_mtu(WOLFSSL* ssl, word16 newMtu)
  1675. {
  1676. if (ssl == NULL)
  1677. return BAD_FUNC_ARG;
  1678. if (newMtu > MAX_RECORD_SIZE) {
  1679. ssl->error = BAD_FUNC_ARG;
  1680. return WOLFSSL_FAILURE;
  1681. }
  1682. ssl->dtlsMtuSz = newMtu;
  1683. return WOLFSSL_SUCCESS;
  1684. }
  1685. #endif /* WOLFSSL_DTLS && (WOLFSSL_SCTP || WOLFSSL_DTLS_MTU) */
  1686. #ifdef WOLFSSL_SRTP
  1687. static const WOLFSSL_SRTP_PROTECTION_PROFILE gSrtpProfiles[] = {
  1688. /* AES CCM 128, Salt:112-bits, Auth HMAC-SHA1 Tag: 80-bits
  1689. * (master_key:128bits + master_salt:112bits) * 2 = 480 bits (60) */
  1690. {"SRTP_AES128_CM_SHA1_80", SRTP_AES128_CM_SHA1_80,
  1691. (((128 + 112) * 2) / 8) },
  1692. /* AES CCM 128, Salt:112-bits, Auth HMAC-SHA1 Tag: 32-bits
  1693. * (master_key:128bits + master_salt:112bits) * 2 = 480 bits (60) */
  1694. {"SRTP_AES128_CM_SHA1_32", SRTP_AES128_CM_SHA1_32,
  1695. (((128 + 112) * 2) / 8) },
  1696. /* NULL Cipher, Salt:112-bits, Auth HMAC-SHA1 Tag 80-bits */
  1697. {"SRTP_NULL_SHA1_80", SRTP_NULL_SHA1_80, ((112 * 2) / 8)},
  1698. /* NULL Cipher, Salt:112-bits, Auth HMAC-SHA1 Tag 32-bits */
  1699. {"SRTP_NULL_SHA1_32", SRTP_NULL_SHA1_32, ((112 * 2) / 8)},
  1700. /* AES GCM 128, Salt: 96-bits, Auth GCM Tag 128-bits
  1701. * (master_key:128bits + master_salt:96bits) * 2 = 448 bits (56) */
  1702. {"SRTP_AEAD_AES_128_GCM", SRTP_AEAD_AES_128_GCM, (((128 + 96) * 2) / 8) },
  1703. /* AES GCM 256, Salt: 96-bits, Auth GCM Tag 128-bits
  1704. * (master_key:256bits + master_salt:96bits) * 2 = 704 bits (88) */
  1705. {"SRTP_AEAD_AES_256_GCM", SRTP_AEAD_AES_256_GCM, (((256 + 96) * 2) / 8) },
  1706. };
  1707. static const WOLFSSL_SRTP_PROTECTION_PROFILE* DtlsSrtpFindProfile(
  1708. const char* profile_str, word32 profile_str_len, unsigned long id)
  1709. {
  1710. int i;
  1711. const WOLFSSL_SRTP_PROTECTION_PROFILE* profile = NULL;
  1712. for (i=0;
  1713. i<(int)(sizeof(gSrtpProfiles)/sizeof(WOLFSSL_SRTP_PROTECTION_PROFILE));
  1714. i++) {
  1715. if (profile_str != NULL) {
  1716. word32 srtp_profile_len = (word32)XSTRLEN(gSrtpProfiles[i].name);
  1717. if (srtp_profile_len == profile_str_len &&
  1718. XMEMCMP(gSrtpProfiles[i].name, profile_str, profile_str_len)
  1719. == 0) {
  1720. profile = &gSrtpProfiles[i];
  1721. break;
  1722. }
  1723. }
  1724. else if (id != 0 && gSrtpProfiles[i].id == id) {
  1725. profile = &gSrtpProfiles[i];
  1726. break;
  1727. }
  1728. }
  1729. return profile;
  1730. }
  1731. /* profile_str: accepts ":" colon separated list of SRTP profiles */
  1732. static int DtlsSrtpSelProfiles(word16* id, const char* profile_str)
  1733. {
  1734. const WOLFSSL_SRTP_PROTECTION_PROFILE* profile;
  1735. const char *current, *next = NULL;
  1736. word32 length = 0, current_length;
  1737. *id = 0; /* reset destination ID's */
  1738. if (profile_str == NULL) {
  1739. return WOLFSSL_FAILURE;
  1740. }
  1741. /* loop on end of line or colon ":" */
  1742. next = profile_str;
  1743. length = (word32)XSTRLEN(profile_str);
  1744. do {
  1745. current = next;
  1746. next = XSTRSTR(current, ":");
  1747. if (next) {
  1748. current_length = (word32)(next - current);
  1749. ++next; /* ++ needed to skip ':' */
  1750. } else {
  1751. current_length = (word32)XSTRLEN(current);
  1752. }
  1753. if (current_length < length)
  1754. length = current_length;
  1755. profile = DtlsSrtpFindProfile(current, current_length, 0);
  1756. if (profile != NULL) {
  1757. *id |= (1 << profile->id); /* selected bit based on ID */
  1758. }
  1759. } while (next != NULL);
  1760. return WOLFSSL_SUCCESS;
  1761. }
  1762. int wolfSSL_CTX_set_tlsext_use_srtp(WOLFSSL_CTX* ctx, const char* profile_str)
  1763. {
  1764. int ret = WOLFSSL_FAILURE;
  1765. if (ctx != NULL) {
  1766. ret = DtlsSrtpSelProfiles(&ctx->dtlsSrtpProfiles, profile_str);
  1767. }
  1768. return ret;
  1769. }
  1770. int wolfSSL_set_tlsext_use_srtp(WOLFSSL* ssl, const char* profile_str)
  1771. {
  1772. int ret = WOLFSSL_FAILURE;
  1773. if (ssl != NULL) {
  1774. ret = DtlsSrtpSelProfiles(&ssl->dtlsSrtpProfiles, profile_str);
  1775. }
  1776. return ret;
  1777. }
  1778. const WOLFSSL_SRTP_PROTECTION_PROFILE* wolfSSL_get_selected_srtp_profile(
  1779. WOLFSSL* ssl)
  1780. {
  1781. const WOLFSSL_SRTP_PROTECTION_PROFILE* profile = NULL;
  1782. if (ssl) {
  1783. profile = DtlsSrtpFindProfile(NULL, 0, ssl->dtlsSrtpId);
  1784. }
  1785. return profile;
  1786. }
  1787. #ifndef NO_WOLFSSL_STUB
  1788. WOLF_STACK_OF(WOLFSSL_SRTP_PROTECTION_PROFILE)* wolfSSL_get_srtp_profiles(
  1789. WOLFSSL* ssl)
  1790. {
  1791. /* Not yet implemented - should return list of available SRTP profiles
  1792. * ssl->dtlsSrtpProfiles */
  1793. (void)ssl;
  1794. return NULL;
  1795. }
  1796. #endif
  1797. #define DTLS_SRTP_KEYING_MATERIAL_LABEL "EXTRACTOR-dtls_srtp"
  1798. int wolfSSL_export_dtls_srtp_keying_material(WOLFSSL* ssl,
  1799. unsigned char* out, size_t* olen)
  1800. {
  1801. const WOLFSSL_SRTP_PROTECTION_PROFILE* profile = NULL;
  1802. if (ssl == NULL || olen == NULL) {
  1803. return BAD_FUNC_ARG;
  1804. }
  1805. profile = DtlsSrtpFindProfile(NULL, 0, ssl->dtlsSrtpId);
  1806. if (profile == NULL) {
  1807. WOLFSSL_MSG("Not using DTLS SRTP");
  1808. return EXT_MISSING;
  1809. }
  1810. if (out == NULL) {
  1811. *olen = profile->kdfBits;
  1812. return LENGTH_ONLY_E;
  1813. }
  1814. if (*olen < (size_t)profile->kdfBits) {
  1815. return BUFFER_E;
  1816. }
  1817. return wolfSSL_export_keying_material(ssl, out, profile->kdfBits,
  1818. DTLS_SRTP_KEYING_MATERIAL_LABEL,
  1819. XSTR_SIZEOF(DTLS_SRTP_KEYING_MATERIAL_LABEL), NULL, 0, 0);
  1820. }
  1821. #endif /* WOLFSSL_SRTP */
  1822. #ifdef WOLFSSL_DTLS_DROP_STATS
  1823. int wolfSSL_dtls_get_drop_stats(WOLFSSL* ssl,
  1824. word32* macDropCount, word32* replayDropCount)
  1825. {
  1826. int ret;
  1827. WOLFSSL_ENTER("wolfSSL_dtls_get_drop_stats");
  1828. if (ssl == NULL)
  1829. ret = BAD_FUNC_ARG;
  1830. else {
  1831. ret = WOLFSSL_SUCCESS;
  1832. if (macDropCount != NULL)
  1833. *macDropCount = ssl->macDropCount;
  1834. if (replayDropCount != NULL)
  1835. *replayDropCount = ssl->replayDropCount;
  1836. }
  1837. WOLFSSL_LEAVE("wolfSSL_dtls_get_drop_stats", ret);
  1838. return ret;
  1839. }
  1840. #endif /* WOLFSSL_DTLS_DROP_STATS */
  1841. #if defined(WOLFSSL_MULTICAST)
  1842. int wolfSSL_CTX_mcast_set_member_id(WOLFSSL_CTX* ctx, word16 id)
  1843. {
  1844. int ret = 0;
  1845. WOLFSSL_ENTER("wolfSSL_CTX_mcast_set_member_id");
  1846. if (ctx == NULL || id > 255)
  1847. ret = BAD_FUNC_ARG;
  1848. if (ret == 0) {
  1849. ctx->haveEMS = 0;
  1850. ctx->haveMcast = 1;
  1851. ctx->mcastID = (byte)id;
  1852. #ifndef WOLFSSL_USER_IO
  1853. ctx->CBIORecv = EmbedReceiveFromMcast;
  1854. #endif /* WOLFSSL_USER_IO */
  1855. ret = WOLFSSL_SUCCESS;
  1856. }
  1857. WOLFSSL_LEAVE("wolfSSL_CTX_mcast_set_member_id", ret);
  1858. return ret;
  1859. }
  1860. int wolfSSL_mcast_get_max_peers(void)
  1861. {
  1862. return WOLFSSL_MULTICAST_PEERS;
  1863. }
  1864. #ifdef WOLFSSL_DTLS
  1865. static WC_INLINE word32 UpdateHighwaterMark(word32 cur, word32 first,
  1866. word32 second, word32 high)
  1867. {
  1868. word32 newCur = 0;
  1869. if (cur < first)
  1870. newCur = first;
  1871. else if (cur < second)
  1872. newCur = second;
  1873. else if (cur < high)
  1874. newCur = high;
  1875. return newCur;
  1876. }
  1877. #endif /* WOLFSSL_DTLS */
  1878. int wolfSSL_set_secret(WOLFSSL* ssl, word16 epoch,
  1879. const byte* preMasterSecret, word32 preMasterSz,
  1880. const byte* clientRandom, const byte* serverRandom,
  1881. const byte* suite)
  1882. {
  1883. int ret = 0;
  1884. WOLFSSL_ENTER("wolfSSL_set_secret");
  1885. if (ssl == NULL || preMasterSecret == NULL ||
  1886. preMasterSz == 0 || preMasterSz > ENCRYPT_LEN ||
  1887. clientRandom == NULL || serverRandom == NULL || suite == NULL) {
  1888. ret = BAD_FUNC_ARG;
  1889. }
  1890. if (ret == 0 && ssl->arrays->preMasterSecret == NULL) {
  1891. ssl->arrays->preMasterSz = ENCRYPT_LEN;
  1892. ssl->arrays->preMasterSecret = (byte*)XMALLOC(ENCRYPT_LEN, ssl->heap,
  1893. DYNAMIC_TYPE_SECRET);
  1894. if (ssl->arrays->preMasterSecret == NULL) {
  1895. ret = MEMORY_E;
  1896. }
  1897. }
  1898. if (ret == 0) {
  1899. XMEMCPY(ssl->arrays->preMasterSecret, preMasterSecret, preMasterSz);
  1900. XMEMSET(ssl->arrays->preMasterSecret + preMasterSz, 0,
  1901. ENCRYPT_LEN - preMasterSz);
  1902. ssl->arrays->preMasterSz = preMasterSz;
  1903. XMEMCPY(ssl->arrays->clientRandom, clientRandom, RAN_LEN);
  1904. XMEMCPY(ssl->arrays->serverRandom, serverRandom, RAN_LEN);
  1905. ssl->options.cipherSuite0 = suite[0];
  1906. ssl->options.cipherSuite = suite[1];
  1907. ret = SetCipherSpecs(ssl);
  1908. }
  1909. if (ret == 0)
  1910. ret = MakeTlsMasterSecret(ssl);
  1911. if (ret == 0) {
  1912. ssl->keys.encryptionOn = 1;
  1913. ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE);
  1914. }
  1915. if (ret == 0) {
  1916. if (ssl->options.dtls) {
  1917. #ifdef WOLFSSL_DTLS
  1918. WOLFSSL_DTLS_PEERSEQ* peerSeq;
  1919. int i;
  1920. ssl->keys.dtls_epoch = epoch;
  1921. for (i = 0, peerSeq = ssl->keys.peerSeq;
  1922. i < WOLFSSL_DTLS_PEERSEQ_SZ;
  1923. i++, peerSeq++) {
  1924. peerSeq->nextEpoch = epoch;
  1925. peerSeq->prevSeq_lo = peerSeq->nextSeq_lo;
  1926. peerSeq->prevSeq_hi = peerSeq->nextSeq_hi;
  1927. peerSeq->nextSeq_lo = 0;
  1928. peerSeq->nextSeq_hi = 0;
  1929. XMEMCPY(peerSeq->prevWindow, peerSeq->window, DTLS_SEQ_SZ);
  1930. XMEMSET(peerSeq->window, 0, DTLS_SEQ_SZ);
  1931. peerSeq->highwaterMark = UpdateHighwaterMark(0,
  1932. ssl->ctx->mcastFirstSeq,
  1933. ssl->ctx->mcastSecondSeq,
  1934. ssl->ctx->mcastMaxSeq);
  1935. }
  1936. #else
  1937. (void)epoch;
  1938. #endif
  1939. }
  1940. FreeHandshakeResources(ssl);
  1941. ret = WOLFSSL_SUCCESS;
  1942. }
  1943. else {
  1944. if (ssl)
  1945. ssl->error = ret;
  1946. ret = WOLFSSL_FATAL_ERROR;
  1947. }
  1948. WOLFSSL_LEAVE("wolfSSL_set_secret", ret);
  1949. return ret;
  1950. }
  1951. #ifdef WOLFSSL_DTLS
  1952. int wolfSSL_mcast_peer_add(WOLFSSL* ssl, word16 peerId, int sub)
  1953. {
  1954. WOLFSSL_DTLS_PEERSEQ* p = NULL;
  1955. int ret = WOLFSSL_SUCCESS;
  1956. int i;
  1957. WOLFSSL_ENTER("wolfSSL_mcast_peer_add");
  1958. if (ssl == NULL || peerId > 255)
  1959. return BAD_FUNC_ARG;
  1960. if (!sub) {
  1961. /* Make sure it isn't already present, while keeping the first
  1962. * open spot. */
  1963. for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) {
  1964. if (ssl->keys.peerSeq[i].peerId == INVALID_PEER_ID)
  1965. p = &ssl->keys.peerSeq[i];
  1966. if (ssl->keys.peerSeq[i].peerId == peerId) {
  1967. WOLFSSL_MSG("Peer ID already in multicast peer list.");
  1968. p = NULL;
  1969. }
  1970. }
  1971. if (p != NULL) {
  1972. XMEMSET(p, 0, sizeof(WOLFSSL_DTLS_PEERSEQ));
  1973. p->peerId = peerId;
  1974. p->highwaterMark = UpdateHighwaterMark(0,
  1975. ssl->ctx->mcastFirstSeq,
  1976. ssl->ctx->mcastSecondSeq,
  1977. ssl->ctx->mcastMaxSeq);
  1978. }
  1979. else {
  1980. WOLFSSL_MSG("No room in peer list.");
  1981. ret = -1;
  1982. }
  1983. }
  1984. else {
  1985. for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) {
  1986. if (ssl->keys.peerSeq[i].peerId == peerId)
  1987. p = &ssl->keys.peerSeq[i];
  1988. }
  1989. if (p != NULL) {
  1990. p->peerId = INVALID_PEER_ID;
  1991. }
  1992. else {
  1993. WOLFSSL_MSG("Peer not found in list.");
  1994. }
  1995. }
  1996. WOLFSSL_LEAVE("wolfSSL_mcast_peer_add", ret);
  1997. return ret;
  1998. }
  1999. /* If peerId is in the list of peers and its last sequence number is non-zero,
  2000. * return 1, otherwise return 0. */
  2001. int wolfSSL_mcast_peer_known(WOLFSSL* ssl, unsigned short peerId)
  2002. {
  2003. int known = 0;
  2004. int i;
  2005. WOLFSSL_ENTER("wolfSSL_mcast_peer_known");
  2006. if (ssl == NULL || peerId > 255) {
  2007. return BAD_FUNC_ARG;
  2008. }
  2009. for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) {
  2010. if (ssl->keys.peerSeq[i].peerId == peerId) {
  2011. if (ssl->keys.peerSeq[i].nextSeq_hi ||
  2012. ssl->keys.peerSeq[i].nextSeq_lo) {
  2013. known = 1;
  2014. }
  2015. break;
  2016. }
  2017. }
  2018. WOLFSSL_LEAVE("wolfSSL_mcast_peer_known", known);
  2019. return known;
  2020. }
  2021. int wolfSSL_CTX_mcast_set_highwater_cb(WOLFSSL_CTX* ctx, word32 maxSeq,
  2022. word32 first, word32 second,
  2023. CallbackMcastHighwater cb)
  2024. {
  2025. if (ctx == NULL || (second && first > second) ||
  2026. first > maxSeq || second > maxSeq || cb == NULL) {
  2027. return BAD_FUNC_ARG;
  2028. }
  2029. ctx->mcastHwCb = cb;
  2030. ctx->mcastFirstSeq = first;
  2031. ctx->mcastSecondSeq = second;
  2032. ctx->mcastMaxSeq = maxSeq;
  2033. return WOLFSSL_SUCCESS;
  2034. }
  2035. int wolfSSL_mcast_set_highwater_ctx(WOLFSSL* ssl, void* ctx)
  2036. {
  2037. if (ssl == NULL || ctx == NULL)
  2038. return BAD_FUNC_ARG;
  2039. ssl->mcastHwCbCtx = ctx;
  2040. return WOLFSSL_SUCCESS;
  2041. }
  2042. #endif /* WOLFSSL_DTLS */
  2043. #endif /* WOLFSSL_MULTICAST */
  2044. #endif /* WOLFSSL_LEANPSK */
  2045. /* return underlying connect or accept, WOLFSSL_SUCCESS on ok */
  2046. int wolfSSL_negotiate(WOLFSSL* ssl)
  2047. {
  2048. int err = WOLFSSL_FATAL_ERROR;
  2049. WOLFSSL_ENTER("wolfSSL_negotiate");
  2050. if (ssl == NULL)
  2051. return WOLFSSL_FATAL_ERROR;
  2052. #ifndef NO_WOLFSSL_SERVER
  2053. if (ssl->options.side == WOLFSSL_SERVER_END) {
  2054. #ifdef WOLFSSL_TLS13
  2055. if (IsAtLeastTLSv1_3(ssl->version))
  2056. err = wolfSSL_accept_TLSv13(ssl);
  2057. else
  2058. #endif
  2059. err = wolfSSL_accept(ssl);
  2060. }
  2061. #endif
  2062. #ifndef NO_WOLFSSL_CLIENT
  2063. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  2064. #ifdef WOLFSSL_TLS13
  2065. if (IsAtLeastTLSv1_3(ssl->version))
  2066. err = wolfSSL_connect_TLSv13(ssl);
  2067. else
  2068. #endif
  2069. err = wolfSSL_connect(ssl);
  2070. }
  2071. #endif
  2072. (void)ssl;
  2073. WOLFSSL_LEAVE("wolfSSL_negotiate", err);
  2074. return err;
  2075. }
  2076. WOLFSSL_ABI
  2077. WC_RNG* wolfSSL_GetRNG(WOLFSSL* ssl)
  2078. {
  2079. if (ssl) {
  2080. return ssl->rng;
  2081. }
  2082. return NULL;
  2083. }
  2084. #ifndef WOLFSSL_LEANPSK
  2085. /* object size based on build */
  2086. int wolfSSL_GetObjectSize(void)
  2087. {
  2088. #ifdef SHOW_SIZES
  2089. printf("sizeof suites = %lu\n", (unsigned long)sizeof(Suites));
  2090. printf("sizeof ciphers(2) = %lu\n", (unsigned long)sizeof(Ciphers));
  2091. #ifndef NO_RC4
  2092. printf("\tsizeof arc4 = %lu\n", (unsigned long)sizeof(Arc4));
  2093. #endif
  2094. printf("\tsizeof aes = %lu\n", (unsigned long)sizeof(Aes));
  2095. #ifndef NO_DES3
  2096. printf("\tsizeof des3 = %lu\n", (unsigned long)sizeof(Des3));
  2097. #endif
  2098. #ifdef HAVE_CHACHA
  2099. printf("\tsizeof chacha = %lu\n", (unsigned long)sizeof(ChaCha));
  2100. #endif
  2101. #ifdef WOLFSSL_SM4
  2102. printf("\tsizeof sm4 = %lu\n", (unsigned long)sizeof(Sm4));
  2103. #endif
  2104. printf("sizeof cipher specs = %lu\n", (unsigned long)
  2105. sizeof(CipherSpecs));
  2106. printf("sizeof keys = %lu\n", (unsigned long)sizeof(Keys));
  2107. printf("sizeof Hashes(2) = %lu\n", (unsigned long)sizeof(Hashes));
  2108. #ifndef NO_MD5
  2109. printf("\tsizeof MD5 = %lu\n", (unsigned long)sizeof(wc_Md5));
  2110. #endif
  2111. #ifndef NO_SHA
  2112. printf("\tsizeof SHA = %lu\n", (unsigned long)sizeof(wc_Sha));
  2113. #endif
  2114. #ifdef WOLFSSL_SHA224
  2115. printf("\tsizeof SHA224 = %lu\n", (unsigned long)sizeof(wc_Sha224));
  2116. #endif
  2117. #ifndef NO_SHA256
  2118. printf("\tsizeof SHA256 = %lu\n", (unsigned long)sizeof(wc_Sha256));
  2119. #endif
  2120. #ifdef WOLFSSL_SHA384
  2121. printf("\tsizeof SHA384 = %lu\n", (unsigned long)sizeof(wc_Sha384));
  2122. #endif
  2123. #ifdef WOLFSSL_SHA384
  2124. printf("\tsizeof SHA512 = %lu\n", (unsigned long)sizeof(wc_Sha512));
  2125. #endif
  2126. #ifdef WOLFSSL_SM3
  2127. printf("\tsizeof sm3 = %lu\n", (unsigned long)sizeof(Sm3));
  2128. #endif
  2129. printf("sizeof Buffers = %lu\n", (unsigned long)sizeof(Buffers));
  2130. printf("sizeof Options = %lu\n", (unsigned long)sizeof(Options));
  2131. printf("sizeof Arrays = %lu\n", (unsigned long)sizeof(Arrays));
  2132. #ifndef NO_RSA
  2133. printf("sizeof RsaKey = %lu\n", (unsigned long)sizeof(RsaKey));
  2134. #endif
  2135. #ifdef HAVE_ECC
  2136. printf("sizeof ecc_key = %lu\n", (unsigned long)sizeof(ecc_key));
  2137. #endif
  2138. printf("sizeof WOLFSSL_CIPHER = %lu\n", (unsigned long)
  2139. sizeof(WOLFSSL_CIPHER));
  2140. printf("sizeof WOLFSSL_SESSION = %lu\n", (unsigned long)
  2141. sizeof(WOLFSSL_SESSION));
  2142. printf("sizeof WOLFSSL = %lu\n", (unsigned long)sizeof(WOLFSSL));
  2143. printf("sizeof WOLFSSL_CTX = %lu\n", (unsigned long)
  2144. sizeof(WOLFSSL_CTX));
  2145. #endif
  2146. return sizeof(WOLFSSL);
  2147. }
  2148. int wolfSSL_CTX_GetObjectSize(void)
  2149. {
  2150. return sizeof(WOLFSSL_CTX);
  2151. }
  2152. int wolfSSL_METHOD_GetObjectSize(void)
  2153. {
  2154. return sizeof(WOLFSSL_METHOD);
  2155. }
  2156. #endif
  2157. #ifdef WOLFSSL_STATIC_MEMORY
  2158. int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx,
  2159. wolfSSL_method_func method, unsigned char* buf, unsigned int sz, int flag,
  2160. int maxSz)
  2161. {
  2162. WOLFSSL_HEAP_HINT* hint = NULL;
  2163. if (ctx == NULL || buf == NULL) {
  2164. return BAD_FUNC_ARG;
  2165. }
  2166. if (*ctx == NULL && method == NULL) {
  2167. return BAD_FUNC_ARG;
  2168. }
  2169. /* If there is a heap already, capture it in hint. */
  2170. if (*ctx && (*ctx)->heap != NULL) {
  2171. hint = (*ctx)->heap;
  2172. }
  2173. if (wc_LoadStaticMemory(&hint, buf, sz, flag, maxSz)) {
  2174. WOLFSSL_MSG("Error loading static memory");
  2175. return WOLFSSL_FAILURE;
  2176. }
  2177. if (*ctx) {
  2178. if ((*ctx)->heap == NULL) {
  2179. (*ctx)->heap = (void*)hint;
  2180. }
  2181. }
  2182. else {
  2183. /* create ctx if needed */
  2184. *ctx = wolfSSL_CTX_new_ex(method(hint), hint);
  2185. if (*ctx == NULL) {
  2186. WOLFSSL_MSG("Error creating ctx");
  2187. return WOLFSSL_FAILURE;
  2188. }
  2189. }
  2190. return WOLFSSL_SUCCESS;
  2191. }
  2192. int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
  2193. {
  2194. if (ssl == NULL) {
  2195. return BAD_FUNC_ARG;
  2196. }
  2197. WOLFSSL_ENTER("wolfSSL_is_static_memory");
  2198. /* fill out statistics if wanted and WOLFMEM_TRACK_STATS flag */
  2199. if (mem_stats != NULL && ssl->heap != NULL) {
  2200. WOLFSSL_HEAP_HINT* hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
  2201. WOLFSSL_HEAP* heap = hint->memory;
  2202. if (heap->flag & WOLFMEM_TRACK_STATS && hint->stats != NULL) {
  2203. XMEMCPY(mem_stats, hint->stats, sizeof(WOLFSSL_MEM_CONN_STATS));
  2204. }
  2205. }
  2206. return (ssl->heap) ? 1 : 0;
  2207. }
  2208. int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats)
  2209. {
  2210. if (ctx == NULL) {
  2211. return BAD_FUNC_ARG;
  2212. }
  2213. WOLFSSL_ENTER("wolfSSL_CTX_is_static_memory");
  2214. /* fill out statistics if wanted */
  2215. if (mem_stats != NULL && ctx->heap != NULL) {
  2216. WOLFSSL_HEAP* heap = ((WOLFSSL_HEAP_HINT*)(ctx->heap))->memory;
  2217. if (wolfSSL_GetMemStats(heap, mem_stats) != 1) {
  2218. return MEMORY_E;
  2219. }
  2220. }
  2221. return (ctx->heap) ? 1 : 0;
  2222. }
  2223. #endif /* WOLFSSL_STATIC_MEMORY */
  2224. /* return max record layer size plaintext input size */
  2225. int wolfSSL_GetMaxOutputSize(WOLFSSL* ssl)
  2226. {
  2227. WOLFSSL_ENTER("wolfSSL_GetMaxOutputSize");
  2228. if (ssl == NULL)
  2229. return BAD_FUNC_ARG;
  2230. if (ssl->options.handShakeState != HANDSHAKE_DONE) {
  2231. WOLFSSL_MSG("Handshake not complete yet");
  2232. return BAD_FUNC_ARG;
  2233. }
  2234. return wolfSSL_GetMaxFragSize(ssl, OUTPUT_RECORD_SIZE);
  2235. }
  2236. /* return record layer size of plaintext input size */
  2237. int wolfSSL_GetOutputSize(WOLFSSL* ssl, int inSz)
  2238. {
  2239. int maxSize;
  2240. WOLFSSL_ENTER("wolfSSL_GetOutputSize");
  2241. if (inSz < 0)
  2242. return BAD_FUNC_ARG;
  2243. maxSize = wolfSSL_GetMaxOutputSize(ssl);
  2244. if (maxSize < 0)
  2245. return maxSize; /* error */
  2246. if (inSz > maxSize)
  2247. return INPUT_SIZE_E;
  2248. return BuildMessage(ssl, NULL, 0, NULL, inSz, application_data, 0, 1, 0,
  2249. CUR_ORDER);
  2250. }
  2251. #ifdef HAVE_ECC
  2252. int wolfSSL_CTX_SetMinEccKey_Sz(WOLFSSL_CTX* ctx, short keySz)
  2253. {
  2254. if (ctx == NULL || keySz < 0 || keySz % 8 != 0) {
  2255. WOLFSSL_MSG("Key size must be divisible by 8 or ctx was null");
  2256. return BAD_FUNC_ARG;
  2257. }
  2258. ctx->minEccKeySz = keySz / 8;
  2259. #ifndef NO_CERTS
  2260. ctx->cm->minEccKeySz = keySz / 8;
  2261. #endif
  2262. return WOLFSSL_SUCCESS;
  2263. }
  2264. int wolfSSL_SetMinEccKey_Sz(WOLFSSL* ssl, short keySz)
  2265. {
  2266. if (ssl == NULL || keySz < 0 || keySz % 8 != 0) {
  2267. WOLFSSL_MSG("Key size must be divisible by 8 or ssl was null");
  2268. return BAD_FUNC_ARG;
  2269. }
  2270. ssl->options.minEccKeySz = keySz / 8;
  2271. return WOLFSSL_SUCCESS;
  2272. }
  2273. #endif /* HAVE_ECC */
  2274. #ifndef NO_RSA
  2275. int wolfSSL_CTX_SetMinRsaKey_Sz(WOLFSSL_CTX* ctx, short keySz)
  2276. {
  2277. if (ctx == NULL || keySz < 0 || keySz % 8 != 0) {
  2278. WOLFSSL_MSG("Key size must be divisible by 8 or ctx was null");
  2279. return BAD_FUNC_ARG;
  2280. }
  2281. ctx->minRsaKeySz = keySz / 8;
  2282. ctx->cm->minRsaKeySz = keySz / 8;
  2283. return WOLFSSL_SUCCESS;
  2284. }
  2285. int wolfSSL_SetMinRsaKey_Sz(WOLFSSL* ssl, short keySz)
  2286. {
  2287. if (ssl == NULL || keySz < 0 || keySz % 8 != 0) {
  2288. WOLFSSL_MSG("Key size must be divisible by 8 or ssl was null");
  2289. return BAD_FUNC_ARG;
  2290. }
  2291. ssl->options.minRsaKeySz = keySz / 8;
  2292. return WOLFSSL_SUCCESS;
  2293. }
  2294. #endif /* !NO_RSA */
  2295. #ifndef NO_DH
  2296. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
  2297. !defined(HAVE_SELFTEST)
  2298. /* Enables or disables the session's DH key prime test. */
  2299. int wolfSSL_SetEnableDhKeyTest(WOLFSSL* ssl, int enable)
  2300. {
  2301. WOLFSSL_ENTER("wolfSSL_SetEnableDhKeyTest");
  2302. if (ssl == NULL)
  2303. return BAD_FUNC_ARG;
  2304. if (!enable)
  2305. ssl->options.dhDoKeyTest = 0;
  2306. else
  2307. ssl->options.dhDoKeyTest = 1;
  2308. WOLFSSL_LEAVE("wolfSSL_SetEnableDhKeyTest", WOLFSSL_SUCCESS);
  2309. return WOLFSSL_SUCCESS;
  2310. }
  2311. #endif
  2312. int wolfSSL_CTX_SetMinDhKey_Sz(WOLFSSL_CTX* ctx, word16 keySz_bits)
  2313. {
  2314. if (ctx == NULL || keySz_bits > 16000 || keySz_bits % 8 != 0)
  2315. return BAD_FUNC_ARG;
  2316. ctx->minDhKeySz = keySz_bits / 8;
  2317. return WOLFSSL_SUCCESS;
  2318. }
  2319. int wolfSSL_SetMinDhKey_Sz(WOLFSSL* ssl, word16 keySz_bits)
  2320. {
  2321. if (ssl == NULL || keySz_bits > 16000 || keySz_bits % 8 != 0)
  2322. return BAD_FUNC_ARG;
  2323. ssl->options.minDhKeySz = keySz_bits / 8;
  2324. return WOLFSSL_SUCCESS;
  2325. }
  2326. int wolfSSL_CTX_SetMaxDhKey_Sz(WOLFSSL_CTX* ctx, word16 keySz_bits)
  2327. {
  2328. if (ctx == NULL || keySz_bits > 16000 || keySz_bits % 8 != 0)
  2329. return BAD_FUNC_ARG;
  2330. ctx->maxDhKeySz = keySz_bits / 8;
  2331. return WOLFSSL_SUCCESS;
  2332. }
  2333. int wolfSSL_SetMaxDhKey_Sz(WOLFSSL* ssl, word16 keySz_bits)
  2334. {
  2335. if (ssl == NULL || keySz_bits > 16000 || keySz_bits % 8 != 0)
  2336. return BAD_FUNC_ARG;
  2337. ssl->options.maxDhKeySz = keySz_bits / 8;
  2338. return WOLFSSL_SUCCESS;
  2339. }
  2340. int wolfSSL_GetDhKey_Sz(WOLFSSL* ssl)
  2341. {
  2342. if (ssl == NULL)
  2343. return BAD_FUNC_ARG;
  2344. return (ssl->options.dhKeySz * 8);
  2345. }
  2346. #endif /* !NO_DH */
  2347. WOLFSSL_ABI
  2348. int wolfSSL_write(WOLFSSL* ssl, const void* data, int sz)
  2349. {
  2350. int ret;
  2351. WOLFSSL_ENTER("wolfSSL_write");
  2352. if (ssl == NULL || data == NULL || sz < 0)
  2353. return BAD_FUNC_ARG;
  2354. #ifdef WOLFSSL_QUIC
  2355. if (WOLFSSL_IS_QUIC(ssl)) {
  2356. WOLFSSL_MSG("SSL_write() on QUIC not allowed");
  2357. return BAD_FUNC_ARG;
  2358. }
  2359. #endif
  2360. #ifdef HAVE_WRITE_DUP
  2361. { /* local variable scope */
  2362. int dupErr = 0; /* local copy */
  2363. ret = 0;
  2364. if (ssl->dupWrite && ssl->dupSide == READ_DUP_SIDE) {
  2365. WOLFSSL_MSG("Read dup side cannot write");
  2366. return WRITE_DUP_WRITE_E;
  2367. }
  2368. if (ssl->dupWrite) {
  2369. if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) {
  2370. return BAD_MUTEX_E;
  2371. }
  2372. dupErr = ssl->dupWrite->dupErr;
  2373. ret = wc_UnLockMutex(&ssl->dupWrite->dupMutex);
  2374. }
  2375. if (ret != 0) {
  2376. ssl->error = ret; /* high priority fatal error */
  2377. return WOLFSSL_FATAL_ERROR;
  2378. }
  2379. if (dupErr != 0) {
  2380. WOLFSSL_MSG("Write dup error from other side");
  2381. ssl->error = dupErr;
  2382. return WOLFSSL_FATAL_ERROR;
  2383. }
  2384. }
  2385. #endif
  2386. #ifdef HAVE_ERRNO_H
  2387. errno = 0;
  2388. #endif
  2389. #ifdef OPENSSL_EXTRA
  2390. if (ssl->CBIS != NULL) {
  2391. ssl->CBIS(ssl, SSL_CB_WRITE, WOLFSSL_SUCCESS);
  2392. ssl->cbmode = SSL_CB_WRITE;
  2393. }
  2394. #endif
  2395. ret = SendData(ssl, data, sz);
  2396. WOLFSSL_LEAVE("wolfSSL_write", ret);
  2397. if (ret < 0)
  2398. return WOLFSSL_FATAL_ERROR;
  2399. else
  2400. return ret;
  2401. }
  2402. static int wolfSSL_read_internal(WOLFSSL* ssl, void* data, int sz, int peek)
  2403. {
  2404. int ret;
  2405. WOLFSSL_ENTER("wolfSSL_read_internal");
  2406. if (ssl == NULL || data == NULL || sz < 0)
  2407. return BAD_FUNC_ARG;
  2408. #ifdef WOLFSSL_QUIC
  2409. if (WOLFSSL_IS_QUIC(ssl)) {
  2410. WOLFSSL_MSG("SSL_read() on QUIC not allowed");
  2411. return BAD_FUNC_ARG;
  2412. }
  2413. #endif
  2414. #if defined(WOLFSSL_ERROR_CODE_OPENSSL) && defined(OPENSSL_EXTRA)
  2415. /* This additional logic is meant to simulate following openSSL behavior:
  2416. * After bidirectional SSL_shutdown complete, SSL_read returns 0 and
  2417. * SSL_get_error_code returns SSL_ERROR_ZERO_RETURN.
  2418. * This behavior is used to know the disconnect of the underlying
  2419. * transport layer.
  2420. *
  2421. * In this logic, CBIORecv is called with a read size of 0 to check the
  2422. * transport layer status. It also returns WOLFSSL_FAILURE so that
  2423. * SSL_read does not return a positive number on failure.
  2424. */
  2425. /* make sure bidirectional TLS shutdown completes */
  2426. if (ssl->error == WOLFSSL_ERROR_SYSCALL || ssl->options.shutdownDone) {
  2427. /* ask the underlying transport the connection is closed */
  2428. if (ssl->CBIORecv(ssl, (char*)data, 0, ssl->IOCB_ReadCtx) ==
  2429. WOLFSSL_CBIO_ERR_CONN_CLOSE) {
  2430. ssl->options.isClosed = 1;
  2431. ssl->error = WOLFSSL_ERROR_ZERO_RETURN;
  2432. }
  2433. return WOLFSSL_FAILURE;
  2434. }
  2435. #endif
  2436. #ifdef HAVE_WRITE_DUP
  2437. if (ssl->dupWrite && ssl->dupSide == WRITE_DUP_SIDE) {
  2438. WOLFSSL_MSG("Write dup side cannot read");
  2439. return WRITE_DUP_READ_E;
  2440. }
  2441. #endif
  2442. #ifdef HAVE_ERRNO_H
  2443. errno = 0;
  2444. #endif
  2445. ret = ReceiveData(ssl, (byte*)data, sz, peek);
  2446. #ifdef HAVE_WRITE_DUP
  2447. if (ssl->dupWrite) {
  2448. if (ssl->error != 0 && ssl->error != WANT_READ
  2449. #ifdef WOLFSSL_ASYNC_CRYPT
  2450. && ssl->error != WC_PENDING_E
  2451. #endif
  2452. ) {
  2453. int notifyErr;
  2454. WOLFSSL_MSG("Notifying write side of fatal read error");
  2455. notifyErr = NotifyWriteSide(ssl, ssl->error);
  2456. if (notifyErr < 0) {
  2457. ret = ssl->error = notifyErr;
  2458. }
  2459. }
  2460. }
  2461. #endif
  2462. WOLFSSL_LEAVE("wolfSSL_read_internal", ret);
  2463. if (ret < 0)
  2464. return WOLFSSL_FATAL_ERROR;
  2465. else
  2466. return ret;
  2467. }
  2468. int wolfSSL_peek(WOLFSSL* ssl, void* data, int sz)
  2469. {
  2470. WOLFSSL_ENTER("wolfSSL_peek");
  2471. return wolfSSL_read_internal(ssl, data, sz, TRUE);
  2472. }
  2473. WOLFSSL_ABI
  2474. int wolfSSL_read(WOLFSSL* ssl, void* data, int sz)
  2475. {
  2476. WOLFSSL_ENTER("wolfSSL_read");
  2477. #ifdef OPENSSL_EXTRA
  2478. if (ssl == NULL) {
  2479. return BAD_FUNC_ARG;
  2480. }
  2481. if (ssl->CBIS != NULL) {
  2482. ssl->CBIS(ssl, SSL_CB_READ, WOLFSSL_SUCCESS);
  2483. ssl->cbmode = SSL_CB_READ;
  2484. }
  2485. #endif
  2486. return wolfSSL_read_internal(ssl, data, sz, FALSE);
  2487. }
  2488. #ifdef WOLFSSL_MULTICAST
  2489. int wolfSSL_mcast_read(WOLFSSL* ssl, word16* id, void* data, int sz)
  2490. {
  2491. int ret = 0;
  2492. WOLFSSL_ENTER("wolfSSL_mcast_read");
  2493. if (ssl == NULL)
  2494. return BAD_FUNC_ARG;
  2495. ret = wolfSSL_read_internal(ssl, data, sz, FALSE);
  2496. if (ssl->options.dtls && ssl->options.haveMcast && id != NULL)
  2497. *id = ssl->keys.curPeerId;
  2498. return ret;
  2499. }
  2500. #endif /* WOLFSSL_MULTICAST */
  2501. /* helpers to set the device id, WOLFSSL_SUCCESS on ok */
  2502. WOLFSSL_ABI
  2503. int wolfSSL_SetDevId(WOLFSSL* ssl, int devId)
  2504. {
  2505. if (ssl == NULL)
  2506. return BAD_FUNC_ARG;
  2507. ssl->devId = devId;
  2508. return WOLFSSL_SUCCESS;
  2509. }
  2510. WOLFSSL_ABI
  2511. int wolfSSL_CTX_SetDevId(WOLFSSL_CTX* ctx, int devId)
  2512. {
  2513. if (ctx == NULL)
  2514. return BAD_FUNC_ARG;
  2515. ctx->devId = devId;
  2516. return WOLFSSL_SUCCESS;
  2517. }
  2518. /* helpers to get device id and heap */
  2519. WOLFSSL_ABI
  2520. int wolfSSL_CTX_GetDevId(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
  2521. {
  2522. int devId = INVALID_DEVID;
  2523. if (ssl != NULL)
  2524. devId = ssl->devId;
  2525. if (ctx != NULL && devId == INVALID_DEVID)
  2526. devId = ctx->devId;
  2527. return devId;
  2528. }
  2529. void* wolfSSL_CTX_GetHeap(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
  2530. {
  2531. void* heap = NULL;
  2532. if (ctx != NULL)
  2533. heap = ctx->heap;
  2534. else if (ssl != NULL)
  2535. heap = ssl->heap;
  2536. return heap;
  2537. }
  2538. #ifdef HAVE_SNI
  2539. WOLFSSL_ABI
  2540. int wolfSSL_UseSNI(WOLFSSL* ssl, byte type, const void* data, word16 size)
  2541. {
  2542. if (ssl == NULL)
  2543. return BAD_FUNC_ARG;
  2544. return TLSX_UseSNI(&ssl->extensions, type, data, size, ssl->heap);
  2545. }
  2546. WOLFSSL_ABI
  2547. int wolfSSL_CTX_UseSNI(WOLFSSL_CTX* ctx, byte type, const void* data,
  2548. word16 size)
  2549. {
  2550. if (ctx == NULL)
  2551. return BAD_FUNC_ARG;
  2552. return TLSX_UseSNI(&ctx->extensions, type, data, size, ctx->heap);
  2553. }
  2554. #ifndef NO_WOLFSSL_SERVER
  2555. void wolfSSL_SNI_SetOptions(WOLFSSL* ssl, byte type, byte options)
  2556. {
  2557. if (ssl && ssl->extensions)
  2558. TLSX_SNI_SetOptions(ssl->extensions, type, options);
  2559. }
  2560. void wolfSSL_CTX_SNI_SetOptions(WOLFSSL_CTX* ctx, byte type, byte options)
  2561. {
  2562. if (ctx && ctx->extensions)
  2563. TLSX_SNI_SetOptions(ctx->extensions, type, options);
  2564. }
  2565. byte wolfSSL_SNI_Status(WOLFSSL* ssl, byte type)
  2566. {
  2567. return TLSX_SNI_Status(ssl ? ssl->extensions : NULL, type);
  2568. }
  2569. word16 wolfSSL_SNI_GetRequest(WOLFSSL* ssl, byte type, void** data)
  2570. {
  2571. if (data)
  2572. *data = NULL;
  2573. if (ssl && ssl->extensions)
  2574. return TLSX_SNI_GetRequest(ssl->extensions, type, data);
  2575. return 0;
  2576. }
  2577. int wolfSSL_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  2578. byte type, byte* sni, word32* inOutSz)
  2579. {
  2580. if (clientHello && helloSz > 0 && sni && inOutSz && *inOutSz > 0)
  2581. return TLSX_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
  2582. return BAD_FUNC_ARG;
  2583. }
  2584. #endif /* NO_WOLFSSL_SERVER */
  2585. #endif /* HAVE_SNI */
  2586. #ifdef HAVE_TRUSTED_CA
  2587. int wolfSSL_UseTrustedCA(WOLFSSL* ssl, byte type,
  2588. const byte* certId, word32 certIdSz)
  2589. {
  2590. if (ssl == NULL)
  2591. return BAD_FUNC_ARG;
  2592. if (type == WOLFSSL_TRUSTED_CA_PRE_AGREED) {
  2593. if (certId != NULL || certIdSz != 0)
  2594. return BAD_FUNC_ARG;
  2595. }
  2596. else if (type == WOLFSSL_TRUSTED_CA_X509_NAME) {
  2597. if (certId == NULL || certIdSz == 0)
  2598. return BAD_FUNC_ARG;
  2599. }
  2600. #ifndef NO_SHA
  2601. else if (type == WOLFSSL_TRUSTED_CA_KEY_SHA1 ||
  2602. type == WOLFSSL_TRUSTED_CA_CERT_SHA1) {
  2603. if (certId == NULL || certIdSz != WC_SHA_DIGEST_SIZE)
  2604. return BAD_FUNC_ARG;
  2605. }
  2606. #endif
  2607. else
  2608. return BAD_FUNC_ARG;
  2609. return TLSX_UseTrustedCA(&ssl->extensions,
  2610. type, certId, certIdSz, ssl->heap);
  2611. }
  2612. #endif /* HAVE_TRUSTED_CA */
  2613. #ifdef HAVE_MAX_FRAGMENT
  2614. #ifndef NO_WOLFSSL_CLIENT
  2615. int wolfSSL_UseMaxFragment(WOLFSSL* ssl, byte mfl)
  2616. {
  2617. if (ssl == NULL)
  2618. return BAD_FUNC_ARG;
  2619. #ifdef WOLFSSL_ALLOW_MAX_FRAGMENT_ADJUST
  2620. /* The following is a non-standard way to reconfigure the max packet size
  2621. post-handshake for wolfSSL_write/wolfSSL_read */
  2622. if (ssl->options.handShakeState == HANDSHAKE_DONE) {
  2623. switch (mfl) {
  2624. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2625. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2626. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2627. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2628. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2629. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2630. default: ssl->max_fragment = MAX_RECORD_SIZE; break;
  2631. }
  2632. return WOLFSSL_SUCCESS;
  2633. }
  2634. #endif /* WOLFSSL_MAX_FRAGMENT_ADJUST */
  2635. /* This call sets the max fragment TLS extension, which gets sent to server.
  2636. The server_hello response is what sets the `ssl->max_fragment` in
  2637. TLSX_MFL_Parse */
  2638. return TLSX_UseMaxFragment(&ssl->extensions, mfl, ssl->heap);
  2639. }
  2640. int wolfSSL_CTX_UseMaxFragment(WOLFSSL_CTX* ctx, byte mfl)
  2641. {
  2642. if (ctx == NULL)
  2643. return BAD_FUNC_ARG;
  2644. return TLSX_UseMaxFragment(&ctx->extensions, mfl, ctx->heap);
  2645. }
  2646. #endif /* NO_WOLFSSL_CLIENT */
  2647. #endif /* HAVE_MAX_FRAGMENT */
  2648. #ifdef HAVE_TRUNCATED_HMAC
  2649. #ifndef NO_WOLFSSL_CLIENT
  2650. int wolfSSL_UseTruncatedHMAC(WOLFSSL* ssl)
  2651. {
  2652. if (ssl == NULL)
  2653. return BAD_FUNC_ARG;
  2654. return TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2655. }
  2656. int wolfSSL_CTX_UseTruncatedHMAC(WOLFSSL_CTX* ctx)
  2657. {
  2658. if (ctx == NULL)
  2659. return BAD_FUNC_ARG;
  2660. return TLSX_UseTruncatedHMAC(&ctx->extensions, ctx->heap);
  2661. }
  2662. #endif /* NO_WOLFSSL_CLIENT */
  2663. #endif /* HAVE_TRUNCATED_HMAC */
  2664. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2665. int wolfSSL_UseOCSPStapling(WOLFSSL* ssl, byte status_type, byte options)
  2666. {
  2667. WOLFSSL_ENTER("wolfSSL_UseOCSPStapling");
  2668. if (ssl == NULL || ssl->options.side != WOLFSSL_CLIENT_END)
  2669. return BAD_FUNC_ARG;
  2670. return TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2671. options, NULL, ssl->heap, ssl->devId);
  2672. }
  2673. int wolfSSL_CTX_UseOCSPStapling(WOLFSSL_CTX* ctx, byte status_type,
  2674. byte options)
  2675. {
  2676. WOLFSSL_ENTER("wolfSSL_CTX_UseOCSPStapling");
  2677. if (ctx == NULL || ctx->method->side != WOLFSSL_CLIENT_END)
  2678. return BAD_FUNC_ARG;
  2679. return TLSX_UseCertificateStatusRequest(&ctx->extensions, status_type,
  2680. options, NULL, ctx->heap, ctx->devId);
  2681. }
  2682. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2683. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2684. int wolfSSL_UseOCSPStaplingV2(WOLFSSL* ssl, byte status_type, byte options)
  2685. {
  2686. if (ssl == NULL || ssl->options.side != WOLFSSL_CLIENT_END)
  2687. return BAD_FUNC_ARG;
  2688. return TLSX_UseCertificateStatusRequestV2(&ssl->extensions, status_type,
  2689. options, ssl->heap, ssl->devId);
  2690. }
  2691. int wolfSSL_CTX_UseOCSPStaplingV2(WOLFSSL_CTX* ctx, byte status_type,
  2692. byte options)
  2693. {
  2694. if (ctx == NULL || ctx->method->side != WOLFSSL_CLIENT_END)
  2695. return BAD_FUNC_ARG;
  2696. return TLSX_UseCertificateStatusRequestV2(&ctx->extensions, status_type,
  2697. options, ctx->heap, ctx->devId);
  2698. }
  2699. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  2700. /* Elliptic Curves */
  2701. #if defined(HAVE_SUPPORTED_CURVES)
  2702. static int isValidCurveGroup(word16 name)
  2703. {
  2704. switch (name) {
  2705. case WOLFSSL_ECC_SECP160K1:
  2706. case WOLFSSL_ECC_SECP160R1:
  2707. case WOLFSSL_ECC_SECP160R2:
  2708. case WOLFSSL_ECC_SECP192K1:
  2709. case WOLFSSL_ECC_SECP192R1:
  2710. case WOLFSSL_ECC_SECP224K1:
  2711. case WOLFSSL_ECC_SECP224R1:
  2712. case WOLFSSL_ECC_SECP256K1:
  2713. case WOLFSSL_ECC_SECP256R1:
  2714. case WOLFSSL_ECC_SECP384R1:
  2715. case WOLFSSL_ECC_SECP521R1:
  2716. case WOLFSSL_ECC_BRAINPOOLP256R1:
  2717. case WOLFSSL_ECC_BRAINPOOLP384R1:
  2718. case WOLFSSL_ECC_BRAINPOOLP512R1:
  2719. case WOLFSSL_ECC_SM2P256V1:
  2720. case WOLFSSL_ECC_X25519:
  2721. case WOLFSSL_ECC_X448:
  2722. case WOLFSSL_FFDHE_2048:
  2723. case WOLFSSL_FFDHE_3072:
  2724. case WOLFSSL_FFDHE_4096:
  2725. case WOLFSSL_FFDHE_6144:
  2726. case WOLFSSL_FFDHE_8192:
  2727. #ifdef HAVE_PQC
  2728. case WOLFSSL_KYBER_LEVEL1:
  2729. case WOLFSSL_KYBER_LEVEL3:
  2730. case WOLFSSL_KYBER_LEVEL5:
  2731. #ifdef HAVE_LIBOQS
  2732. case WOLFSSL_P256_KYBER_LEVEL1:
  2733. case WOLFSSL_P384_KYBER_LEVEL3:
  2734. case WOLFSSL_P521_KYBER_LEVEL5:
  2735. #endif
  2736. #endif
  2737. return 1;
  2738. default:
  2739. return 0;
  2740. }
  2741. }
  2742. int wolfSSL_UseSupportedCurve(WOLFSSL* ssl, word16 name)
  2743. {
  2744. if (ssl == NULL || !isValidCurveGroup(name))
  2745. return BAD_FUNC_ARG;
  2746. ssl->options.userCurves = 1;
  2747. #if defined(NO_TLS)
  2748. return WOLFSSL_FAILURE;
  2749. #else
  2750. return TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
  2751. #endif /* NO_TLS */
  2752. }
  2753. int wolfSSL_CTX_UseSupportedCurve(WOLFSSL_CTX* ctx, word16 name)
  2754. {
  2755. if (ctx == NULL || !isValidCurveGroup(name))
  2756. return BAD_FUNC_ARG;
  2757. ctx->userCurves = 1;
  2758. #if defined(NO_TLS)
  2759. return WOLFSSL_FAILURE;
  2760. #else
  2761. return TLSX_UseSupportedCurve(&ctx->extensions, name, ctx->heap);
  2762. #endif /* NO_TLS */
  2763. }
  2764. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13)
  2765. int wolfSSL_CTX_set1_groups(WOLFSSL_CTX* ctx, int* groups,
  2766. int count)
  2767. {
  2768. int i;
  2769. int _groups[WOLFSSL_MAX_GROUP_COUNT];
  2770. WOLFSSL_ENTER("wolfSSL_CTX_set1_groups");
  2771. if (count == 0) {
  2772. WOLFSSL_MSG("Group count is zero");
  2773. return WOLFSSL_FAILURE;
  2774. }
  2775. for (i = 0; i < count; i++) {
  2776. if (isValidCurveGroup((word16)groups[i])) {
  2777. _groups[i] = groups[i];
  2778. }
  2779. #ifdef HAVE_ECC
  2780. else {
  2781. /* groups may be populated with curve NIDs */
  2782. int oid = nid2oid(groups[i], oidCurveType);
  2783. int name = (int)GetCurveByOID(oid);
  2784. if (name == 0) {
  2785. WOLFSSL_MSG("Invalid group name");
  2786. return WOLFSSL_FAILURE;
  2787. }
  2788. _groups[i] = name;
  2789. }
  2790. #else
  2791. else {
  2792. WOLFSSL_MSG("Invalid group name");
  2793. return WOLFSSL_FAILURE;
  2794. }
  2795. #endif
  2796. }
  2797. return wolfSSL_CTX_set_groups(ctx, _groups, count) == WOLFSSL_SUCCESS ?
  2798. WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  2799. }
  2800. int wolfSSL_set1_groups(WOLFSSL* ssl, int* groups, int count)
  2801. {
  2802. int i;
  2803. int _groups[WOLFSSL_MAX_GROUP_COUNT];
  2804. WOLFSSL_ENTER("wolfSSL_CTX_set1_groups");
  2805. if (count == 0) {
  2806. WOLFSSL_MSG("Group count is zero");
  2807. return WOLFSSL_FAILURE;
  2808. }
  2809. for (i = 0; i < count; i++) {
  2810. if (isValidCurveGroup((word16)groups[i])) {
  2811. _groups[i] = groups[i];
  2812. }
  2813. #ifdef HAVE_ECC
  2814. else {
  2815. /* groups may be populated with curve NIDs */
  2816. int oid = nid2oid(groups[i], oidCurveType);
  2817. int name = (int)GetCurveByOID(oid);
  2818. if (name == 0) {
  2819. WOLFSSL_MSG("Invalid group name");
  2820. return WOLFSSL_FAILURE;
  2821. }
  2822. _groups[i] = name;
  2823. }
  2824. #else
  2825. else {
  2826. WOLFSSL_MSG("Invalid group name");
  2827. return WOLFSSL_FAILURE;
  2828. }
  2829. #endif
  2830. }
  2831. return wolfSSL_set_groups(ssl, _groups, count) == WOLFSSL_SUCCESS ?
  2832. WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  2833. }
  2834. #endif /* OPENSSL_EXTRA && WOLFSSL_TLS13 */
  2835. #endif /* HAVE_SUPPORTED_CURVES */
  2836. /* Application-Layer Protocol Negotiation */
  2837. #ifdef HAVE_ALPN
  2838. WOLFSSL_ABI
  2839. int wolfSSL_UseALPN(WOLFSSL* ssl, char *protocol_name_list,
  2840. word32 protocol_name_listSz, byte options)
  2841. {
  2842. char *list, *ptr, **token;
  2843. word16 len;
  2844. int idx = 0;
  2845. int ret = WOLFSSL_FAILURE;
  2846. WOLFSSL_ENTER("wolfSSL_UseALPN");
  2847. if (ssl == NULL || protocol_name_list == NULL)
  2848. return BAD_FUNC_ARG;
  2849. if (protocol_name_listSz > (WOLFSSL_MAX_ALPN_NUMBER *
  2850. WOLFSSL_MAX_ALPN_PROTO_NAME_LEN +
  2851. WOLFSSL_MAX_ALPN_NUMBER)) {
  2852. WOLFSSL_MSG("Invalid arguments, protocol name list too long");
  2853. return BAD_FUNC_ARG;
  2854. }
  2855. if (!(options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) &&
  2856. !(options & WOLFSSL_ALPN_FAILED_ON_MISMATCH)) {
  2857. WOLFSSL_MSG("Invalid arguments, options not supported");
  2858. return BAD_FUNC_ARG;
  2859. }
  2860. list = (char *)XMALLOC(protocol_name_listSz+1, ssl->heap,
  2861. DYNAMIC_TYPE_ALPN);
  2862. if (list == NULL) {
  2863. WOLFSSL_MSG("Memory failure");
  2864. return MEMORY_ERROR;
  2865. }
  2866. token = (char **)XMALLOC(sizeof(char *) * (WOLFSSL_MAX_ALPN_NUMBER+1),
  2867. ssl->heap, DYNAMIC_TYPE_ALPN);
  2868. if (token == NULL) {
  2869. XFREE(list, ssl->heap, DYNAMIC_TYPE_ALPN);
  2870. WOLFSSL_MSG("Memory failure");
  2871. return MEMORY_ERROR;
  2872. }
  2873. XMEMSET(token, 0, sizeof(char *) * (WOLFSSL_MAX_ALPN_NUMBER+1));
  2874. XSTRNCPY(list, protocol_name_list, protocol_name_listSz);
  2875. list[protocol_name_listSz] = '\0';
  2876. /* read all protocol name from the list */
  2877. token[idx] = XSTRTOK(list, ",", &ptr);
  2878. while (idx < WOLFSSL_MAX_ALPN_NUMBER && token[idx] != NULL)
  2879. token[++idx] = XSTRTOK(NULL, ",", &ptr);
  2880. /* add protocol name list in the TLS extension in reverse order */
  2881. while ((idx--) > 0) {
  2882. len = (word16)XSTRLEN(token[idx]);
  2883. ret = TLSX_UseALPN(&ssl->extensions, token[idx], len, options,
  2884. ssl->heap);
  2885. if (ret != WOLFSSL_SUCCESS) {
  2886. WOLFSSL_MSG("TLSX_UseALPN failure");
  2887. break;
  2888. }
  2889. }
  2890. XFREE(token, ssl->heap, DYNAMIC_TYPE_ALPN);
  2891. XFREE(list, ssl->heap, DYNAMIC_TYPE_ALPN);
  2892. return ret;
  2893. }
  2894. int wolfSSL_ALPN_GetProtocol(WOLFSSL* ssl, char **protocol_name, word16 *size)
  2895. {
  2896. return TLSX_ALPN_GetRequest(ssl ? ssl->extensions : NULL,
  2897. (void **)protocol_name, size);
  2898. }
  2899. int wolfSSL_ALPN_GetPeerProtocol(WOLFSSL* ssl, char **list, word16 *listSz)
  2900. {
  2901. int i, len;
  2902. char *p;
  2903. byte *s;
  2904. if (ssl == NULL || list == NULL || listSz == NULL)
  2905. return BAD_FUNC_ARG;
  2906. if (ssl->alpn_peer_requested == NULL
  2907. || ssl->alpn_peer_requested_length == 0)
  2908. return BUFFER_ERROR;
  2909. /* ssl->alpn_peer_requested are the original bytes sent in a ClientHello,
  2910. * formatted as (len-byte chars+)+. To turn n protocols into a
  2911. * comma-separated C string, one needs (n-1) commas and a final 0 byte
  2912. * which has the same length as the original.
  2913. * The returned length is the strlen() of the C string, so -1 of that. */
  2914. *listSz = ssl->alpn_peer_requested_length-1;
  2915. *list = p = (char *)XMALLOC(ssl->alpn_peer_requested_length, ssl->heap,
  2916. DYNAMIC_TYPE_TLSX);
  2917. if (p == NULL)
  2918. return MEMORY_ERROR;
  2919. for (i = 0, s = ssl->alpn_peer_requested;
  2920. i < ssl->alpn_peer_requested_length;
  2921. p += len, i += len)
  2922. {
  2923. if (i)
  2924. *p++ = ',';
  2925. len = s[i++];
  2926. /* guard against bad length bytes. */
  2927. if (i + len > ssl->alpn_peer_requested_length) {
  2928. XFREE(*list, ssl->heap, DYNAMIC_TYPE_TLSX);
  2929. *list = NULL;
  2930. return WOLFSSL_FAILURE;
  2931. }
  2932. XMEMCPY(p, s + i, len);
  2933. }
  2934. *p = 0;
  2935. return WOLFSSL_SUCCESS;
  2936. }
  2937. /* used to free memory allocated by wolfSSL_ALPN_GetPeerProtocol */
  2938. int wolfSSL_ALPN_FreePeerProtocol(WOLFSSL* ssl, char **list)
  2939. {
  2940. if (ssl == NULL) {
  2941. return BAD_FUNC_ARG;
  2942. }
  2943. XFREE(*list, ssl->heap, DYNAMIC_TYPE_TLSX);
  2944. *list = NULL;
  2945. return WOLFSSL_SUCCESS;
  2946. }
  2947. #endif /* HAVE_ALPN */
  2948. /* Secure Renegotiation */
  2949. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  2950. /* user is forcing ability to use secure renegotiation, we discourage it */
  2951. int wolfSSL_UseSecureRenegotiation(WOLFSSL* ssl)
  2952. {
  2953. int ret = BAD_FUNC_ARG;
  2954. #if defined(NO_TLS)
  2955. (void)ssl;
  2956. #else
  2957. if (ssl)
  2958. ret = TLSX_UseSecureRenegotiation(&ssl->extensions, ssl->heap);
  2959. if (ret == WOLFSSL_SUCCESS) {
  2960. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_RENEGOTIATION_INFO);
  2961. if (extension)
  2962. ssl->secure_renegotiation = (SecureRenegotiation*)extension->data;
  2963. }
  2964. #endif /* !NO_TLS */
  2965. return ret;
  2966. }
  2967. int wolfSSL_CTX_UseSecureRenegotiation(WOLFSSL_CTX* ctx)
  2968. {
  2969. if (ctx == NULL)
  2970. return BAD_FUNC_ARG;
  2971. ctx->useSecureReneg = 1;
  2972. return WOLFSSL_SUCCESS;
  2973. }
  2974. #ifdef HAVE_SECURE_RENEGOTIATION
  2975. /* do a secure renegotiation handshake, user forced, we discourage */
  2976. static int _Rehandshake(WOLFSSL* ssl)
  2977. {
  2978. int ret;
  2979. if (ssl == NULL)
  2980. return BAD_FUNC_ARG;
  2981. if (IsAtLeastTLSv1_3(ssl->version)) {
  2982. WOLFSSL_MSG("Secure Renegotiation not supported in TLS 1.3");
  2983. return SECURE_RENEGOTIATION_E;
  2984. }
  2985. if (ssl->secure_renegotiation == NULL) {
  2986. WOLFSSL_MSG("Secure Renegotiation not forced on by user");
  2987. return SECURE_RENEGOTIATION_E;
  2988. }
  2989. if (ssl->secure_renegotiation->enabled == 0) {
  2990. WOLFSSL_MSG("Secure Renegotiation not enabled at extension level");
  2991. return SECURE_RENEGOTIATION_E;
  2992. }
  2993. #ifdef WOLFSSL_DTLS
  2994. if (ssl->options.dtls && ssl->keys.dtls_epoch == 0xFFFF) {
  2995. WOLFSSL_MSG("Secure Renegotiation not allowed. Epoch would wrap");
  2996. return SECURE_RENEGOTIATION_E;
  2997. }
  2998. #endif
  2999. /* If the client started the renegotiation, the server will already
  3000. * have processed the client's hello. */
  3001. if (ssl->options.side != WOLFSSL_SERVER_END ||
  3002. ssl->options.acceptState != ACCEPT_FIRST_REPLY_DONE) {
  3003. if (ssl->options.handShakeState != HANDSHAKE_DONE) {
  3004. if (!ssl->options.handShakeDone) {
  3005. WOLFSSL_MSG("Can't renegotiate until initial "
  3006. "handshake complete");
  3007. return SECURE_RENEGOTIATION_E;
  3008. }
  3009. else {
  3010. WOLFSSL_MSG("Renegotiation already started. "
  3011. "Moving it forward.");
  3012. ret = wolfSSL_negotiate(ssl);
  3013. if (ret == WOLFSSL_SUCCESS)
  3014. ssl->secure_rene_count++;
  3015. return ret;
  3016. }
  3017. }
  3018. /* reset handshake states */
  3019. ssl->options.sendVerify = 0;
  3020. ssl->options.serverState = NULL_STATE;
  3021. ssl->options.clientState = NULL_STATE;
  3022. ssl->options.connectState = CONNECT_BEGIN;
  3023. ssl->options.acceptState = ACCEPT_BEGIN_RENEG;
  3024. ssl->options.handShakeState = NULL_STATE;
  3025. ssl->options.processReply = 0; /* TODO, move states in internal.h */
  3026. XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
  3027. ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED;
  3028. #if !defined(NO_WOLFSSL_SERVER)
  3029. if (ssl->options.side == WOLFSSL_SERVER_END) {
  3030. ret = SendHelloRequest(ssl);
  3031. if (ret != 0) {
  3032. ssl->error = ret;
  3033. return WOLFSSL_FATAL_ERROR;
  3034. }
  3035. }
  3036. #endif /* !NO_WOLFSSL_SERVER */
  3037. ret = InitHandshakeHashes(ssl);
  3038. if (ret != 0) {
  3039. ssl->error = ret;
  3040. return WOLFSSL_FATAL_ERROR;
  3041. }
  3042. }
  3043. ret = wolfSSL_negotiate(ssl);
  3044. if (ret == WOLFSSL_SUCCESS)
  3045. ssl->secure_rene_count++;
  3046. return ret;
  3047. }
  3048. /* do a secure renegotiation handshake, user forced, we discourage */
  3049. int wolfSSL_Rehandshake(WOLFSSL* ssl)
  3050. {
  3051. int ret;
  3052. WOLFSSL_ENTER("wolfSSL_Rehandshake");
  3053. if (ssl == NULL)
  3054. return WOLFSSL_FAILURE;
  3055. #ifdef HAVE_SESSION_TICKET
  3056. ret = WOLFSSL_SUCCESS;
  3057. #endif
  3058. if (ssl->options.side == WOLFSSL_SERVER_END) {
  3059. /* Reset option to send certificate verify. */
  3060. ssl->options.sendVerify = 0;
  3061. /* Reset resuming flag to do full secure handshake. */
  3062. ssl->options.resuming = 0;
  3063. }
  3064. else {
  3065. /* Reset resuming flag to do full secure handshake. */
  3066. ssl->options.resuming = 0;
  3067. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_CLIENT)
  3068. /* Clearing the ticket. */
  3069. ret = wolfSSL_UseSessionTicket(ssl);
  3070. #endif
  3071. }
  3072. /* CLIENT/SERVER: Reset peer authentication for full secure handshake. */
  3073. ssl->options.peerAuthGood = 0;
  3074. #ifdef HAVE_SESSION_TICKET
  3075. if (ret == WOLFSSL_SUCCESS)
  3076. #endif
  3077. ret = _Rehandshake(ssl);
  3078. return ret;
  3079. }
  3080. #ifndef NO_WOLFSSL_CLIENT
  3081. /* do a secure resumption handshake, user forced, we discourage */
  3082. int wolfSSL_SecureResume(WOLFSSL* ssl)
  3083. {
  3084. WOLFSSL_ENTER("wolfSSL_SecureResume");
  3085. if (ssl == NULL)
  3086. return BAD_FUNC_ARG;
  3087. if (ssl->options.side == WOLFSSL_SERVER_END) {
  3088. ssl->error = SIDE_ERROR;
  3089. return WOLFSSL_FATAL_ERROR;
  3090. }
  3091. return _Rehandshake(ssl);
  3092. }
  3093. #endif /* NO_WOLFSSL_CLIENT */
  3094. #endif /* HAVE_SECURE_RENEGOTIATION */
  3095. long wolfSSL_SSL_get_secure_renegotiation_support(WOLFSSL* ssl)
  3096. {
  3097. WOLFSSL_ENTER("wolfSSL_SSL_get_secure_renegotiation_support");
  3098. if (!ssl || !ssl->secure_renegotiation)
  3099. return WOLFSSL_FAILURE;
  3100. return ssl->secure_renegotiation->enabled;
  3101. }
  3102. #endif /* HAVE_SECURE_RENEGOTIATION_INFO */
  3103. #if defined(HAVE_SESSION_TICKET)
  3104. /* Session Ticket */
  3105. #if !defined(NO_WOLFSSL_SERVER)
  3106. int wolfSSL_CTX_NoTicketTLSv12(WOLFSSL_CTX* ctx)
  3107. {
  3108. if (ctx == NULL)
  3109. return BAD_FUNC_ARG;
  3110. ctx->noTicketTls12 = 1;
  3111. return WOLFSSL_SUCCESS;
  3112. }
  3113. int wolfSSL_NoTicketTLSv12(WOLFSSL* ssl)
  3114. {
  3115. if (ssl == NULL)
  3116. return BAD_FUNC_ARG;
  3117. ssl->options.noTicketTls12 = 1;
  3118. return WOLFSSL_SUCCESS;
  3119. }
  3120. /* WOLFSSL_SUCCESS on ok */
  3121. int wolfSSL_CTX_set_TicketEncCb(WOLFSSL_CTX* ctx, SessionTicketEncCb cb)
  3122. {
  3123. if (ctx == NULL)
  3124. return BAD_FUNC_ARG;
  3125. ctx->ticketEncCb = cb;
  3126. return WOLFSSL_SUCCESS;
  3127. }
  3128. /* set hint interval, WOLFSSL_SUCCESS on ok */
  3129. int wolfSSL_CTX_set_TicketHint(WOLFSSL_CTX* ctx, int hint)
  3130. {
  3131. if (ctx == NULL)
  3132. return BAD_FUNC_ARG;
  3133. ctx->ticketHint = hint;
  3134. return WOLFSSL_SUCCESS;
  3135. }
  3136. /* set user context, WOLFSSL_SUCCESS on ok */
  3137. int wolfSSL_CTX_set_TicketEncCtx(WOLFSSL_CTX* ctx, void* userCtx)
  3138. {
  3139. if (ctx == NULL)
  3140. return BAD_FUNC_ARG;
  3141. ctx->ticketEncCtx = userCtx;
  3142. return WOLFSSL_SUCCESS;
  3143. }
  3144. /* get user context - returns userCtx on success, NULL on failure */
  3145. void* wolfSSL_CTX_get_TicketEncCtx(WOLFSSL_CTX* ctx)
  3146. {
  3147. if (ctx == NULL)
  3148. return NULL;
  3149. return ctx->ticketEncCtx;
  3150. }
  3151. #ifdef WOLFSSL_TLS13
  3152. /* set the maximum number of tickets to send
  3153. * return WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on fail
  3154. */
  3155. int wolfSSL_CTX_set_num_tickets(WOLFSSL_CTX* ctx, size_t mxTickets)
  3156. {
  3157. if (ctx == NULL)
  3158. return WOLFSSL_FAILURE;
  3159. ctx->maxTicketTls13 = (unsigned int)mxTickets;
  3160. return WOLFSSL_SUCCESS;
  3161. }
  3162. /* get the maximum number of tickets to send
  3163. * return number of tickets set to be sent
  3164. */
  3165. size_t wolfSSL_CTX_get_num_tickets(WOLFSSL_CTX* ctx)
  3166. {
  3167. if (ctx == NULL)
  3168. return 0;
  3169. return (size_t)ctx->maxTicketTls13;
  3170. }
  3171. #endif /* WOLFSSL_TLS13 */
  3172. #endif /* !NO_WOLFSSL_SERVER */
  3173. #if !defined(NO_WOLFSSL_CLIENT)
  3174. int wolfSSL_UseSessionTicket(WOLFSSL* ssl)
  3175. {
  3176. if (ssl == NULL)
  3177. return BAD_FUNC_ARG;
  3178. return TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  3179. }
  3180. int wolfSSL_CTX_UseSessionTicket(WOLFSSL_CTX* ctx)
  3181. {
  3182. if (ctx == NULL)
  3183. return BAD_FUNC_ARG;
  3184. return TLSX_UseSessionTicket(&ctx->extensions, NULL, ctx->heap);
  3185. }
  3186. int wolfSSL_get_SessionTicket(WOLFSSL* ssl, byte* buf, word32* bufSz)
  3187. {
  3188. if (ssl == NULL || buf == NULL || bufSz == NULL || *bufSz == 0)
  3189. return BAD_FUNC_ARG;
  3190. if (ssl->session->ticketLen <= *bufSz) {
  3191. XMEMCPY(buf, ssl->session->ticket, ssl->session->ticketLen);
  3192. *bufSz = ssl->session->ticketLen;
  3193. }
  3194. else
  3195. *bufSz = 0;
  3196. return WOLFSSL_SUCCESS;
  3197. }
  3198. int wolfSSL_set_SessionTicket(WOLFSSL* ssl, const byte* buf,
  3199. word32 bufSz)
  3200. {
  3201. if (ssl == NULL || (buf == NULL && bufSz > 0))
  3202. return BAD_FUNC_ARG;
  3203. if (bufSz > 0) {
  3204. /* Ticket will fit into static ticket */
  3205. if (bufSz <= SESSION_TICKET_LEN) {
  3206. if (ssl->session->ticketLenAlloc > 0) {
  3207. XFREE(ssl->session->ticket, ssl->session->heap,
  3208. DYNAMIC_TYPE_SESSION_TICK);
  3209. ssl->session->ticketLenAlloc = 0;
  3210. ssl->session->ticket = ssl->session->staticTicket;
  3211. }
  3212. }
  3213. else { /* Ticket requires dynamic ticket storage */
  3214. /* is dyn buffer big enough */
  3215. if (ssl->session->ticketLen < bufSz) {
  3216. if (ssl->session->ticketLenAlloc > 0) {
  3217. XFREE(ssl->session->ticket, ssl->session->heap,
  3218. DYNAMIC_TYPE_SESSION_TICK);
  3219. }
  3220. ssl->session->ticket = (byte*)XMALLOC(bufSz, ssl->session->heap,
  3221. DYNAMIC_TYPE_SESSION_TICK);
  3222. if(ssl->session->ticket == NULL) {
  3223. ssl->session->ticket = ssl->session->staticTicket;
  3224. ssl->session->ticketLenAlloc = 0;
  3225. return MEMORY_ERROR;
  3226. }
  3227. ssl->session->ticketLenAlloc = (word16)bufSz;
  3228. }
  3229. }
  3230. XMEMCPY(ssl->session->ticket, buf, bufSz);
  3231. }
  3232. ssl->session->ticketLen = (word16)bufSz;
  3233. return WOLFSSL_SUCCESS;
  3234. }
  3235. int wolfSSL_set_SessionTicket_cb(WOLFSSL* ssl,
  3236. CallbackSessionTicket cb, void* ctx)
  3237. {
  3238. if (ssl == NULL)
  3239. return BAD_FUNC_ARG;
  3240. ssl->session_ticket_cb = cb;
  3241. ssl->session_ticket_ctx = ctx;
  3242. return WOLFSSL_SUCCESS;
  3243. }
  3244. #endif /* !NO_WOLFSSL_CLIENT */
  3245. #endif /* HAVE_SESSION_TICKET */
  3246. #ifdef HAVE_EXTENDED_MASTER
  3247. #ifndef NO_WOLFSSL_CLIENT
  3248. int wolfSSL_CTX_DisableExtendedMasterSecret(WOLFSSL_CTX* ctx)
  3249. {
  3250. if (ctx == NULL)
  3251. return BAD_FUNC_ARG;
  3252. ctx->haveEMS = 0;
  3253. return WOLFSSL_SUCCESS;
  3254. }
  3255. int wolfSSL_DisableExtendedMasterSecret(WOLFSSL* ssl)
  3256. {
  3257. if (ssl == NULL)
  3258. return BAD_FUNC_ARG;
  3259. ssl->options.haveEMS = 0;
  3260. return WOLFSSL_SUCCESS;
  3261. }
  3262. #endif
  3263. #endif
  3264. #ifndef WOLFSSL_LEANPSK
  3265. int wolfSSL_send(WOLFSSL* ssl, const void* data, int sz, int flags)
  3266. {
  3267. int ret;
  3268. int oldFlags;
  3269. WOLFSSL_ENTER("wolfSSL_send");
  3270. if (ssl == NULL || data == NULL || sz < 0)
  3271. return BAD_FUNC_ARG;
  3272. oldFlags = ssl->wflags;
  3273. ssl->wflags = flags;
  3274. ret = wolfSSL_write(ssl, data, sz);
  3275. ssl->wflags = oldFlags;
  3276. WOLFSSL_LEAVE("wolfSSL_send", ret);
  3277. return ret;
  3278. }
  3279. int wolfSSL_recv(WOLFSSL* ssl, void* data, int sz, int flags)
  3280. {
  3281. int ret;
  3282. int oldFlags;
  3283. WOLFSSL_ENTER("wolfSSL_recv");
  3284. if (ssl == NULL || data == NULL || sz < 0)
  3285. return BAD_FUNC_ARG;
  3286. oldFlags = ssl->rflags;
  3287. ssl->rflags = flags;
  3288. ret = wolfSSL_read(ssl, data, sz);
  3289. ssl->rflags = oldFlags;
  3290. WOLFSSL_LEAVE("wolfSSL_recv", ret);
  3291. return ret;
  3292. }
  3293. #endif
  3294. /* WOLFSSL_SUCCESS on ok */
  3295. WOLFSSL_ABI
  3296. int wolfSSL_shutdown(WOLFSSL* ssl)
  3297. {
  3298. int ret = WOLFSSL_FATAL_ERROR;
  3299. WOLFSSL_ENTER("wolfSSL_shutdown");
  3300. if (ssl == NULL)
  3301. return WOLFSSL_FATAL_ERROR;
  3302. if (ssl->options.quietShutdown) {
  3303. WOLFSSL_MSG("quiet shutdown, no close notify sent");
  3304. ret = WOLFSSL_SUCCESS;
  3305. }
  3306. else {
  3307. /* try to send close notify, not an error if can't */
  3308. if (!ssl->options.isClosed && !ssl->options.connReset &&
  3309. !ssl->options.sentNotify) {
  3310. ssl->error = SendAlert(ssl, alert_warning, close_notify);
  3311. if (ssl->error < 0) {
  3312. WOLFSSL_ERROR(ssl->error);
  3313. return WOLFSSL_FATAL_ERROR;
  3314. }
  3315. ssl->options.sentNotify = 1; /* don't send close_notify twice */
  3316. if (ssl->options.closeNotify) {
  3317. ret = WOLFSSL_SUCCESS;
  3318. ssl->options.shutdownDone = 1;
  3319. }
  3320. else {
  3321. ret = WOLFSSL_SHUTDOWN_NOT_DONE;
  3322. WOLFSSL_LEAVE("wolfSSL_shutdown", ret);
  3323. return ret;
  3324. }
  3325. }
  3326. #ifdef WOLFSSL_SHUTDOWNONCE
  3327. if (ssl->options.isClosed || ssl->options.connReset) {
  3328. /* Shutdown has already occurred.
  3329. * Caller is free to ignore this error. */
  3330. return SSL_SHUTDOWN_ALREADY_DONE_E;
  3331. }
  3332. #endif
  3333. /* call wolfSSL_shutdown again for bidirectional shutdown */
  3334. if (ssl->options.sentNotify && !ssl->options.closeNotify) {
  3335. ret = ProcessReply(ssl);
  3336. if ((ret == ZERO_RETURN) || (ret == SOCKET_ERROR_E)) {
  3337. /* simulate OpenSSL behavior */
  3338. ssl->options.shutdownDone = 1;
  3339. /* Clear error */
  3340. ssl->error = WOLFSSL_ERROR_NONE;
  3341. ret = WOLFSSL_SUCCESS;
  3342. } else if (ret == MEMORY_E) {
  3343. ret = WOLFSSL_FATAL_ERROR;
  3344. } else if (ssl->error == WOLFSSL_ERROR_NONE) {
  3345. ret = WOLFSSL_SHUTDOWN_NOT_DONE;
  3346. } else {
  3347. WOLFSSL_ERROR(ssl->error);
  3348. ret = WOLFSSL_FATAL_ERROR;
  3349. }
  3350. }
  3351. }
  3352. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  3353. /* reset WOLFSSL structure state for possible reuse */
  3354. if (ret == WOLFSSL_SUCCESS) {
  3355. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  3356. WOLFSSL_MSG("could not clear WOLFSSL");
  3357. ret = WOLFSSL_FATAL_ERROR;
  3358. }
  3359. }
  3360. #endif
  3361. WOLFSSL_LEAVE("wolfSSL_shutdown", ret);
  3362. return ret;
  3363. }
  3364. /* get current error state value */
  3365. int wolfSSL_state(WOLFSSL* ssl)
  3366. {
  3367. if (ssl == NULL) {
  3368. return BAD_FUNC_ARG;
  3369. }
  3370. return ssl->error;
  3371. }
  3372. WOLFSSL_ABI
  3373. int wolfSSL_get_error(WOLFSSL* ssl, int ret)
  3374. {
  3375. WOLFSSL_ENTER("wolfSSL_get_error");
  3376. if (ret > 0)
  3377. return WOLFSSL_ERROR_NONE;
  3378. if (ssl == NULL)
  3379. return BAD_FUNC_ARG;
  3380. WOLFSSL_LEAVE("wolfSSL_get_error", ssl->error);
  3381. /* make sure converted types are handled in SetErrorString() too */
  3382. if (ssl->error == WANT_READ)
  3383. return WOLFSSL_ERROR_WANT_READ; /* convert to OpenSSL type */
  3384. else if (ssl->error == WANT_WRITE)
  3385. return WOLFSSL_ERROR_WANT_WRITE; /* convert to OpenSSL type */
  3386. else if (ssl->error == ZERO_RETURN || ssl->options.shutdownDone)
  3387. return WOLFSSL_ERROR_ZERO_RETURN; /* convert to OpenSSL type */
  3388. #ifdef OPENSSL_EXTRA
  3389. else if (ssl->error == SOCKET_PEER_CLOSED_E)
  3390. return WOLFSSL_ERROR_SYSCALL; /* convert to OpenSSL type */
  3391. #endif
  3392. #if defined(WOLFSSL_HAPROXY)
  3393. return GetX509Error(ssl->error);
  3394. #else
  3395. return (ssl->error);
  3396. #endif
  3397. }
  3398. /* retrieve alert history, WOLFSSL_SUCCESS on ok */
  3399. int wolfSSL_get_alert_history(WOLFSSL* ssl, WOLFSSL_ALERT_HISTORY *h)
  3400. {
  3401. if (ssl && h) {
  3402. *h = ssl->alert_history;
  3403. }
  3404. return WOLFSSL_SUCCESS;
  3405. }
  3406. #ifdef OPENSSL_EXTRA
  3407. /* returns SSL_WRITING, SSL_READING or SSL_NOTHING */
  3408. int wolfSSL_want(WOLFSSL* ssl)
  3409. {
  3410. int rw_state = SSL_NOTHING;
  3411. if (ssl) {
  3412. if (ssl->error == WANT_READ)
  3413. rw_state = SSL_READING;
  3414. else if (ssl->error == WANT_WRITE)
  3415. rw_state = SSL_WRITING;
  3416. }
  3417. return rw_state;
  3418. }
  3419. #endif
  3420. /* return TRUE if current error is want read */
  3421. int wolfSSL_want_read(WOLFSSL* ssl)
  3422. {
  3423. WOLFSSL_ENTER("wolfSSL_want_read");
  3424. if (ssl->error == WANT_READ)
  3425. return 1;
  3426. return 0;
  3427. }
  3428. /* return TRUE if current error is want write */
  3429. int wolfSSL_want_write(WOLFSSL* ssl)
  3430. {
  3431. WOLFSSL_ENTER("wolfSSL_want_write");
  3432. if (ssl->error == WANT_WRITE)
  3433. return 1;
  3434. return 0;
  3435. }
  3436. char* wolfSSL_ERR_error_string(unsigned long errNumber, char* data)
  3437. {
  3438. WOLFSSL_ENTER("wolfSSL_ERR_error_string");
  3439. if (data) {
  3440. SetErrorString((int)errNumber, data);
  3441. return data;
  3442. }
  3443. else {
  3444. static char tmp[WOLFSSL_MAX_ERROR_SZ] = {0};
  3445. SetErrorString((int)errNumber, tmp);
  3446. return tmp;
  3447. }
  3448. }
  3449. void wolfSSL_ERR_error_string_n(unsigned long e, char* buf, unsigned long len)
  3450. {
  3451. WOLFSSL_ENTER("wolfSSL_ERR_error_string_n");
  3452. if (len >= WOLFSSL_MAX_ERROR_SZ)
  3453. wolfSSL_ERR_error_string(e, buf);
  3454. else {
  3455. WOLFSSL_MSG("Error buffer too short, truncating");
  3456. if (len) {
  3457. char tmp[WOLFSSL_MAX_ERROR_SZ];
  3458. wolfSSL_ERR_error_string(e, tmp);
  3459. XMEMCPY(buf, tmp, len-1);
  3460. buf[len-1] = '\0';
  3461. }
  3462. }
  3463. }
  3464. /* don't free temporary arrays at end of handshake */
  3465. void wolfSSL_KeepArrays(WOLFSSL* ssl)
  3466. {
  3467. if (ssl)
  3468. ssl->options.saveArrays = 1;
  3469. }
  3470. /* user doesn't need temporary arrays anymore, Free */
  3471. void wolfSSL_FreeArrays(WOLFSSL* ssl)
  3472. {
  3473. if (ssl && ssl->options.handShakeState == HANDSHAKE_DONE) {
  3474. ssl->options.saveArrays = 0;
  3475. FreeArrays(ssl, 1);
  3476. }
  3477. }
  3478. /* Set option to indicate that the resources are not to be freed after
  3479. * handshake.
  3480. *
  3481. * ssl The SSL/TLS object.
  3482. * returns BAD_FUNC_ARG when ssl is NULL and 0 on success.
  3483. */
  3484. int wolfSSL_KeepHandshakeResources(WOLFSSL* ssl)
  3485. {
  3486. if (ssl == NULL)
  3487. return BAD_FUNC_ARG;
  3488. ssl->options.keepResources = 1;
  3489. return 0;
  3490. }
  3491. /* Free the handshake resources after handshake.
  3492. *
  3493. * ssl The SSL/TLS object.
  3494. * returns BAD_FUNC_ARG when ssl is NULL and 0 on success.
  3495. */
  3496. int wolfSSL_FreeHandshakeResources(WOLFSSL* ssl)
  3497. {
  3498. if (ssl == NULL)
  3499. return BAD_FUNC_ARG;
  3500. FreeHandshakeResources(ssl);
  3501. return 0;
  3502. }
  3503. /* Use the client's order of preference when matching cipher suites.
  3504. *
  3505. * ssl The SSL/TLS context object.
  3506. * returns BAD_FUNC_ARG when ssl is NULL and 0 on success.
  3507. */
  3508. int wolfSSL_CTX_UseClientSuites(WOLFSSL_CTX* ctx)
  3509. {
  3510. if (ctx == NULL)
  3511. return BAD_FUNC_ARG;
  3512. ctx->useClientOrder = 1;
  3513. return 0;
  3514. }
  3515. /* Use the client's order of preference when matching cipher suites.
  3516. *
  3517. * ssl The SSL/TLS object.
  3518. * returns BAD_FUNC_ARG when ssl is NULL and 0 on success.
  3519. */
  3520. int wolfSSL_UseClientSuites(WOLFSSL* ssl)
  3521. {
  3522. if (ssl == NULL)
  3523. return BAD_FUNC_ARG;
  3524. ssl->options.useClientOrder = 1;
  3525. return 0;
  3526. }
  3527. #ifdef WOLFSSL_DTLS
  3528. const byte* wolfSSL_GetDtlsMacSecret(WOLFSSL* ssl, int verify, int epochOrder)
  3529. {
  3530. #ifndef WOLFSSL_AEAD_ONLY
  3531. Keys* keys = NULL;
  3532. (void)epochOrder;
  3533. if (ssl == NULL)
  3534. return NULL;
  3535. #ifdef HAVE_SECURE_RENEGOTIATION
  3536. switch (epochOrder) {
  3537. case PEER_ORDER:
  3538. if (IsDtlsMsgSCRKeys(ssl))
  3539. keys = &ssl->secure_renegotiation->tmp_keys;
  3540. else
  3541. keys = &ssl->keys;
  3542. break;
  3543. case PREV_ORDER:
  3544. keys = &ssl->keys;
  3545. break;
  3546. case CUR_ORDER:
  3547. if (DtlsUseSCRKeys(ssl))
  3548. keys = &ssl->secure_renegotiation->tmp_keys;
  3549. else
  3550. keys = &ssl->keys;
  3551. break;
  3552. default:
  3553. WOLFSSL_MSG("Unknown epoch order");
  3554. return NULL;
  3555. }
  3556. #else
  3557. keys = &ssl->keys;
  3558. #endif
  3559. if ( (ssl->options.side == WOLFSSL_CLIENT_END && !verify) ||
  3560. (ssl->options.side == WOLFSSL_SERVER_END && verify) )
  3561. return keys->client_write_MAC_secret;
  3562. else
  3563. return keys->server_write_MAC_secret;
  3564. #else
  3565. (void)ssl;
  3566. (void)verify;
  3567. (void)epochOrder;
  3568. return NULL;
  3569. #endif
  3570. }
  3571. #endif /* WOLFSSL_DTLS */
  3572. const byte* wolfSSL_GetMacSecret(WOLFSSL* ssl, int verify)
  3573. {
  3574. #ifndef WOLFSSL_AEAD_ONLY
  3575. if (ssl == NULL)
  3576. return NULL;
  3577. if ( (ssl->options.side == WOLFSSL_CLIENT_END && !verify) ||
  3578. (ssl->options.side == WOLFSSL_SERVER_END && verify) )
  3579. return ssl->keys.client_write_MAC_secret;
  3580. else
  3581. return ssl->keys.server_write_MAC_secret;
  3582. #else
  3583. (void)ssl;
  3584. (void)verify;
  3585. return NULL;
  3586. #endif
  3587. }
  3588. int wolfSSL_GetSide(WOLFSSL* ssl)
  3589. {
  3590. if (ssl)
  3591. return ssl->options.side;
  3592. return BAD_FUNC_ARG;
  3593. }
  3594. #ifdef ATOMIC_USER
  3595. void wolfSSL_CTX_SetMacEncryptCb(WOLFSSL_CTX* ctx, CallbackMacEncrypt cb)
  3596. {
  3597. if (ctx)
  3598. ctx->MacEncryptCb = cb;
  3599. }
  3600. void wolfSSL_SetMacEncryptCtx(WOLFSSL* ssl, void *ctx)
  3601. {
  3602. if (ssl)
  3603. ssl->MacEncryptCtx = ctx;
  3604. }
  3605. void* wolfSSL_GetMacEncryptCtx(WOLFSSL* ssl)
  3606. {
  3607. if (ssl)
  3608. return ssl->MacEncryptCtx;
  3609. return NULL;
  3610. }
  3611. void wolfSSL_CTX_SetDecryptVerifyCb(WOLFSSL_CTX* ctx, CallbackDecryptVerify cb)
  3612. {
  3613. if (ctx)
  3614. ctx->DecryptVerifyCb = cb;
  3615. }
  3616. void wolfSSL_SetDecryptVerifyCtx(WOLFSSL* ssl, void *ctx)
  3617. {
  3618. if (ssl)
  3619. ssl->DecryptVerifyCtx = ctx;
  3620. }
  3621. void* wolfSSL_GetDecryptVerifyCtx(WOLFSSL* ssl)
  3622. {
  3623. if (ssl)
  3624. return ssl->DecryptVerifyCtx;
  3625. return NULL;
  3626. }
  3627. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  3628. /**
  3629. * Set the callback, against the context, that encrypts then MACs.
  3630. *
  3631. * ctx SSL/TLS context.
  3632. * cb Callback function to use with Encrypt-Then-MAC.
  3633. */
  3634. void wolfSSL_CTX_SetEncryptMacCb(WOLFSSL_CTX* ctx, CallbackEncryptMac cb)
  3635. {
  3636. if (ctx)
  3637. ctx->EncryptMacCb = cb;
  3638. }
  3639. /**
  3640. * Set the context to use with callback that encrypts then MACs.
  3641. *
  3642. * ssl SSL/TLS object.
  3643. * ctx Callback function's context.
  3644. */
  3645. void wolfSSL_SetEncryptMacCtx(WOLFSSL* ssl, void *ctx)
  3646. {
  3647. if (ssl)
  3648. ssl->EncryptMacCtx = ctx;
  3649. }
  3650. /**
  3651. * Get the context being used with callback that encrypts then MACs.
  3652. *
  3653. * ssl SSL/TLS object.
  3654. * returns callback function's context or NULL if SSL/TLS object is NULL.
  3655. */
  3656. void* wolfSSL_GetEncryptMacCtx(WOLFSSL* ssl)
  3657. {
  3658. if (ssl)
  3659. return ssl->EncryptMacCtx;
  3660. return NULL;
  3661. }
  3662. /**
  3663. * Set the callback, against the context, that MAC verifies then decrypts.
  3664. *
  3665. * ctx SSL/TLS context.
  3666. * cb Callback function to use with Encrypt-Then-MAC.
  3667. */
  3668. void wolfSSL_CTX_SetVerifyDecryptCb(WOLFSSL_CTX* ctx, CallbackVerifyDecrypt cb)
  3669. {
  3670. if (ctx)
  3671. ctx->VerifyDecryptCb = cb;
  3672. }
  3673. /**
  3674. * Set the context to use with callback that MAC verifies then decrypts.
  3675. *
  3676. * ssl SSL/TLS object.
  3677. * ctx Callback function's context.
  3678. */
  3679. void wolfSSL_SetVerifyDecryptCtx(WOLFSSL* ssl, void *ctx)
  3680. {
  3681. if (ssl)
  3682. ssl->VerifyDecryptCtx = ctx;
  3683. }
  3684. /**
  3685. * Get the context being used with callback that MAC verifies then decrypts.
  3686. *
  3687. * ssl SSL/TLS object.
  3688. * returns callback function's context or NULL if SSL/TLS object is NULL.
  3689. */
  3690. void* wolfSSL_GetVerifyDecryptCtx(WOLFSSL* ssl)
  3691. {
  3692. if (ssl)
  3693. return ssl->VerifyDecryptCtx;
  3694. return NULL;
  3695. }
  3696. #endif /* HAVE_ENCRYPT_THEN_MAC !WOLFSSL_AEAD_ONLY */
  3697. const byte* wolfSSL_GetClientWriteKey(WOLFSSL* ssl)
  3698. {
  3699. if (ssl)
  3700. return ssl->keys.client_write_key;
  3701. return NULL;
  3702. }
  3703. const byte* wolfSSL_GetClientWriteIV(WOLFSSL* ssl)
  3704. {
  3705. if (ssl)
  3706. return ssl->keys.client_write_IV;
  3707. return NULL;
  3708. }
  3709. const byte* wolfSSL_GetServerWriteKey(WOLFSSL* ssl)
  3710. {
  3711. if (ssl)
  3712. return ssl->keys.server_write_key;
  3713. return NULL;
  3714. }
  3715. const byte* wolfSSL_GetServerWriteIV(WOLFSSL* ssl)
  3716. {
  3717. if (ssl)
  3718. return ssl->keys.server_write_IV;
  3719. return NULL;
  3720. }
  3721. int wolfSSL_GetKeySize(WOLFSSL* ssl)
  3722. {
  3723. if (ssl)
  3724. return ssl->specs.key_size;
  3725. return BAD_FUNC_ARG;
  3726. }
  3727. int wolfSSL_GetIVSize(WOLFSSL* ssl)
  3728. {
  3729. if (ssl)
  3730. return ssl->specs.iv_size;
  3731. return BAD_FUNC_ARG;
  3732. }
  3733. int wolfSSL_GetBulkCipher(WOLFSSL* ssl)
  3734. {
  3735. if (ssl)
  3736. return ssl->specs.bulk_cipher_algorithm;
  3737. return BAD_FUNC_ARG;
  3738. }
  3739. int wolfSSL_GetCipherType(WOLFSSL* ssl)
  3740. {
  3741. if (ssl == NULL)
  3742. return BAD_FUNC_ARG;
  3743. #ifndef WOLFSSL_AEAD_ONLY
  3744. if (ssl->specs.cipher_type == block)
  3745. return WOLFSSL_BLOCK_TYPE;
  3746. if (ssl->specs.cipher_type == stream)
  3747. return WOLFSSL_STREAM_TYPE;
  3748. #endif
  3749. if (ssl->specs.cipher_type == aead)
  3750. return WOLFSSL_AEAD_TYPE;
  3751. return -1;
  3752. }
  3753. int wolfSSL_GetCipherBlockSize(WOLFSSL* ssl)
  3754. {
  3755. if (ssl == NULL)
  3756. return BAD_FUNC_ARG;
  3757. return ssl->specs.block_size;
  3758. }
  3759. int wolfSSL_GetAeadMacSize(WOLFSSL* ssl)
  3760. {
  3761. if (ssl == NULL)
  3762. return BAD_FUNC_ARG;
  3763. return ssl->specs.aead_mac_size;
  3764. }
  3765. int wolfSSL_IsTLSv1_1(WOLFSSL* ssl)
  3766. {
  3767. if (ssl == NULL)
  3768. return BAD_FUNC_ARG;
  3769. if (ssl->options.tls1_1)
  3770. return 1;
  3771. return 0;
  3772. }
  3773. int wolfSSL_GetHmacSize(WOLFSSL* ssl)
  3774. {
  3775. /* AEAD ciphers don't have HMAC keys */
  3776. if (ssl)
  3777. return (ssl->specs.cipher_type != aead) ? ssl->specs.hash_size : 0;
  3778. return BAD_FUNC_ARG;
  3779. }
  3780. #ifdef WORD64_AVAILABLE
  3781. int wolfSSL_GetPeerSequenceNumber(WOLFSSL* ssl, word64 *seq)
  3782. {
  3783. if ((ssl == NULL) || (seq == NULL))
  3784. return BAD_FUNC_ARG;
  3785. *seq = ((word64)ssl->keys.peer_sequence_number_hi << 32) |
  3786. ssl->keys.peer_sequence_number_lo;
  3787. return !(*seq);
  3788. }
  3789. int wolfSSL_GetSequenceNumber(WOLFSSL* ssl, word64 *seq)
  3790. {
  3791. if ((ssl == NULL) || (seq == NULL))
  3792. return BAD_FUNC_ARG;
  3793. *seq = ((word64)ssl->keys.sequence_number_hi << 32) |
  3794. ssl->keys.sequence_number_lo;
  3795. return !(*seq);
  3796. }
  3797. #endif
  3798. #endif /* ATOMIC_USER */
  3799. #ifndef NO_CERTS
  3800. WOLFSSL_CERT_MANAGER* wolfSSL_CTX_GetCertManager(WOLFSSL_CTX* ctx)
  3801. {
  3802. WOLFSSL_CERT_MANAGER* cm = NULL;
  3803. if (ctx)
  3804. cm = ctx->cm;
  3805. return cm;
  3806. }
  3807. #endif /* NO_CERTS */
  3808. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) \
  3809. && defined(XFPRINTF)
  3810. void wolfSSL_ERR_print_errors_fp(XFILE fp, int err)
  3811. {
  3812. char data[WOLFSSL_MAX_ERROR_SZ + 1];
  3813. WOLFSSL_ENTER("wolfSSL_ERR_print_errors_fp");
  3814. SetErrorString(err, data);
  3815. if (XFPRINTF(fp, "%s", data) < 0)
  3816. WOLFSSL_MSG("fprintf failed in wolfSSL_ERR_print_errors_fp");
  3817. }
  3818. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  3819. void wolfSSL_ERR_dump_errors_fp(XFILE fp)
  3820. {
  3821. wc_ERR_print_errors_fp(fp);
  3822. }
  3823. void wolfSSL_ERR_print_errors_cb (int (*cb)(const char *str, size_t len,
  3824. void *u), void *u)
  3825. {
  3826. wc_ERR_print_errors_cb(cb, u);
  3827. }
  3828. #endif
  3829. #endif /* !NO_FILESYSTEM && !NO_STDIO_FILESYSTEM && XFPRINTF */
  3830. /*
  3831. * TODO This ssl parameter needs to be changed to const once our ABI checker
  3832. * stops flagging qualifier additions as ABI breaking.
  3833. */
  3834. WOLFSSL_ABI
  3835. int wolfSSL_pending(WOLFSSL* ssl)
  3836. {
  3837. WOLFSSL_ENTER("wolfSSL_pending");
  3838. if (ssl == NULL)
  3839. return WOLFSSL_FAILURE;
  3840. return ssl->buffers.clearOutputBuffer.length;
  3841. }
  3842. int wolfSSL_has_pending(const WOLFSSL* ssl)
  3843. {
  3844. WOLFSSL_ENTER("wolfSSL_has_pending");
  3845. if (ssl == NULL)
  3846. return WOLFSSL_FAILURE;
  3847. return ssl->buffers.clearOutputBuffer.length > 0;
  3848. }
  3849. #ifndef WOLFSSL_LEANPSK
  3850. /* turn on handshake group messages for context */
  3851. int wolfSSL_CTX_set_group_messages(WOLFSSL_CTX* ctx)
  3852. {
  3853. if (ctx == NULL)
  3854. return BAD_FUNC_ARG;
  3855. ctx->groupMessages = 1;
  3856. return WOLFSSL_SUCCESS;
  3857. }
  3858. #endif
  3859. #ifndef NO_WOLFSSL_CLIENT
  3860. /* connect enough to get peer cert chain */
  3861. int wolfSSL_connect_cert(WOLFSSL* ssl)
  3862. {
  3863. int ret;
  3864. if (ssl == NULL)
  3865. return WOLFSSL_FAILURE;
  3866. ssl->options.certOnly = 1;
  3867. ret = wolfSSL_connect(ssl);
  3868. ssl->options.certOnly = 0;
  3869. return ret;
  3870. }
  3871. #endif
  3872. #ifndef WOLFSSL_LEANPSK
  3873. /* turn on handshake group messages for ssl object */
  3874. int wolfSSL_set_group_messages(WOLFSSL* ssl)
  3875. {
  3876. if (ssl == NULL)
  3877. return BAD_FUNC_ARG;
  3878. ssl->options.groupMessages = 1;
  3879. return WOLFSSL_SUCCESS;
  3880. }
  3881. /* make minVersion the internal equivalent SSL version */
  3882. static int SetMinVersionHelper(byte* minVersion, int version)
  3883. {
  3884. #ifdef NO_TLS
  3885. (void)minVersion;
  3886. #endif
  3887. switch (version) {
  3888. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  3889. case WOLFSSL_SSLV3:
  3890. *minVersion = SSLv3_MINOR;
  3891. break;
  3892. #endif
  3893. #ifndef NO_TLS
  3894. #ifndef NO_OLD_TLS
  3895. #ifdef WOLFSSL_ALLOW_TLSV10
  3896. case WOLFSSL_TLSV1:
  3897. *minVersion = TLSv1_MINOR;
  3898. break;
  3899. #endif
  3900. case WOLFSSL_TLSV1_1:
  3901. *minVersion = TLSv1_1_MINOR;
  3902. break;
  3903. #endif
  3904. #ifndef WOLFSSL_NO_TLS12
  3905. case WOLFSSL_TLSV1_2:
  3906. *minVersion = TLSv1_2_MINOR;
  3907. break;
  3908. #endif
  3909. #endif
  3910. #ifdef WOLFSSL_TLS13
  3911. case WOLFSSL_TLSV1_3:
  3912. *minVersion = TLSv1_3_MINOR;
  3913. break;
  3914. #endif
  3915. #ifdef WOLFSSL_DTLS
  3916. case WOLFSSL_DTLSV1:
  3917. *minVersion = DTLS_MINOR;
  3918. break;
  3919. case WOLFSSL_DTLSV1_2:
  3920. *minVersion = DTLSv1_2_MINOR;
  3921. break;
  3922. #ifdef WOLFSSL_DTLS13
  3923. case WOLFSSL_DTLSV1_3:
  3924. *minVersion = DTLSv1_3_MINOR;
  3925. break;
  3926. #endif /* WOLFSSL_DTLS13 */
  3927. #endif /* WOLFSSL_DTLS */
  3928. default:
  3929. WOLFSSL_MSG("Bad function argument");
  3930. return BAD_FUNC_ARG;
  3931. }
  3932. return WOLFSSL_SUCCESS;
  3933. }
  3934. /* Set minimum downgrade version allowed, WOLFSSL_SUCCESS on ok */
  3935. WOLFSSL_ABI
  3936. int wolfSSL_CTX_SetMinVersion(WOLFSSL_CTX* ctx, int version)
  3937. {
  3938. WOLFSSL_ENTER("wolfSSL_CTX_SetMinVersion");
  3939. if (ctx == NULL) {
  3940. WOLFSSL_MSG("Bad function argument");
  3941. return BAD_FUNC_ARG;
  3942. }
  3943. return SetMinVersionHelper(&ctx->minDowngrade, version);
  3944. }
  3945. /* Set minimum downgrade version allowed, WOLFSSL_SUCCESS on ok */
  3946. int wolfSSL_SetMinVersion(WOLFSSL* ssl, int version)
  3947. {
  3948. WOLFSSL_ENTER("wolfSSL_SetMinVersion");
  3949. if (ssl == NULL) {
  3950. WOLFSSL_MSG("Bad function argument");
  3951. return BAD_FUNC_ARG;
  3952. }
  3953. return SetMinVersionHelper(&ssl->options.minDowngrade, version);
  3954. }
  3955. /* Function to get version as WOLFSSL_ enum value for wolfSSL_SetVersion */
  3956. int wolfSSL_GetVersion(const WOLFSSL* ssl)
  3957. {
  3958. if (ssl == NULL)
  3959. return BAD_FUNC_ARG;
  3960. if (ssl->version.major == SSLv3_MAJOR) {
  3961. switch (ssl->version.minor) {
  3962. case SSLv3_MINOR :
  3963. return WOLFSSL_SSLV3;
  3964. case TLSv1_MINOR :
  3965. return WOLFSSL_TLSV1;
  3966. case TLSv1_1_MINOR :
  3967. return WOLFSSL_TLSV1_1;
  3968. case TLSv1_2_MINOR :
  3969. return WOLFSSL_TLSV1_2;
  3970. case TLSv1_3_MINOR :
  3971. return WOLFSSL_TLSV1_3;
  3972. default:
  3973. break;
  3974. }
  3975. }
  3976. return VERSION_ERROR;
  3977. }
  3978. int wolfSSL_SetVersion(WOLFSSL* ssl, int version)
  3979. {
  3980. word16 haveRSA = 1;
  3981. word16 havePSK = 0;
  3982. int keySz = 0;
  3983. WOLFSSL_ENTER("wolfSSL_SetVersion");
  3984. if (ssl == NULL) {
  3985. WOLFSSL_MSG("Bad function argument");
  3986. return BAD_FUNC_ARG;
  3987. }
  3988. switch (version) {
  3989. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  3990. case WOLFSSL_SSLV3:
  3991. ssl->version = MakeSSLv3();
  3992. break;
  3993. #endif
  3994. #ifndef NO_TLS
  3995. #ifndef NO_OLD_TLS
  3996. #ifdef WOLFSSL_ALLOW_TLSV10
  3997. case WOLFSSL_TLSV1:
  3998. ssl->version = MakeTLSv1();
  3999. break;
  4000. #endif
  4001. case WOLFSSL_TLSV1_1:
  4002. ssl->version = MakeTLSv1_1();
  4003. break;
  4004. #endif
  4005. #ifndef WOLFSSL_NO_TLS12
  4006. case WOLFSSL_TLSV1_2:
  4007. ssl->version = MakeTLSv1_2();
  4008. break;
  4009. #endif
  4010. #ifdef WOLFSSL_TLS13
  4011. case WOLFSSL_TLSV1_3:
  4012. ssl->version = MakeTLSv1_3();
  4013. break;
  4014. #endif /* WOLFSSL_TLS13 */
  4015. #endif
  4016. default:
  4017. WOLFSSL_MSG("Bad function argument");
  4018. return BAD_FUNC_ARG;
  4019. }
  4020. #ifdef NO_RSA
  4021. haveRSA = 0;
  4022. #endif
  4023. #ifndef NO_PSK
  4024. havePSK = ssl->options.havePSK;
  4025. #endif
  4026. #ifndef NO_CERTS
  4027. keySz = ssl->buffers.keySz;
  4028. #endif
  4029. if (AllocateSuites(ssl) != 0)
  4030. return WOLFSSL_FAILURE;
  4031. InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
  4032. ssl->options.haveDH, ssl->options.haveECDSAsig,
  4033. ssl->options.haveECC, TRUE, ssl->options.haveStaticECC,
  4034. ssl->options.haveFalconSig, ssl->options.haveDilithiumSig,
  4035. ssl->options.useAnon, TRUE, ssl->options.side);
  4036. return WOLFSSL_SUCCESS;
  4037. }
  4038. #endif /* !leanpsk */
  4039. #ifndef NO_CERTS
  4040. /* hash is the SHA digest of name, just use first 32 bits as hash */
  4041. static WC_INLINE word32 HashSigner(const byte* hash)
  4042. {
  4043. return MakeWordFromHash(hash) % CA_TABLE_SIZE;
  4044. }
  4045. /* does CA already exist on signer list */
  4046. int AlreadySigner(WOLFSSL_CERT_MANAGER* cm, byte* hash)
  4047. {
  4048. Signer* signers;
  4049. int ret = 0;
  4050. word32 row;
  4051. if (cm == NULL || hash == NULL) {
  4052. return ret;
  4053. }
  4054. row = HashSigner(hash);
  4055. if (wc_LockMutex(&cm->caLock) != 0) {
  4056. return ret;
  4057. }
  4058. signers = cm->caTable[row];
  4059. while (signers) {
  4060. byte* subjectHash;
  4061. #ifndef NO_SKID
  4062. subjectHash = signers->subjectKeyIdHash;
  4063. #else
  4064. subjectHash = signers->subjectNameHash;
  4065. #endif
  4066. if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
  4067. ret = 1; /* success */
  4068. break;
  4069. }
  4070. signers = signers->next;
  4071. }
  4072. wc_UnLockMutex(&cm->caLock);
  4073. return ret;
  4074. }
  4075. #ifdef WOLFSSL_TRUST_PEER_CERT
  4076. /* hash is the SHA digest of name, just use first 32 bits as hash */
  4077. static WC_INLINE word32 TrustedPeerHashSigner(const byte* hash)
  4078. {
  4079. return MakeWordFromHash(hash) % TP_TABLE_SIZE;
  4080. }
  4081. /* does trusted peer already exist on signer list */
  4082. int AlreadyTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DecodedCert* cert)
  4083. {
  4084. TrustedPeerCert* tp;
  4085. int ret = 0;
  4086. word32 row = TrustedPeerHashSigner(cert->subjectHash);
  4087. if (wc_LockMutex(&cm->tpLock) != 0)
  4088. return ret;
  4089. tp = cm->tpTable[row];
  4090. while (tp) {
  4091. if (XMEMCMP(cert->subjectHash, tp->subjectNameHash,
  4092. SIGNER_DIGEST_SIZE) == 0)
  4093. ret = 1;
  4094. #ifndef NO_SKID
  4095. if (cert->extSubjKeyIdSet) {
  4096. /* Compare SKID as well if available */
  4097. if (ret == 1 && XMEMCMP(cert->extSubjKeyId, tp->subjectKeyIdHash,
  4098. SIGNER_DIGEST_SIZE) != 0)
  4099. ret = 0;
  4100. }
  4101. #endif
  4102. if (ret == 1)
  4103. break;
  4104. tp = tp->next;
  4105. }
  4106. wc_UnLockMutex(&cm->tpLock);
  4107. return ret;
  4108. }
  4109. /* return Trusted Peer if found, otherwise NULL
  4110. type is what to match on
  4111. */
  4112. TrustedPeerCert* GetTrustedPeer(void* vp, DecodedCert* cert)
  4113. {
  4114. WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
  4115. TrustedPeerCert* ret = NULL;
  4116. TrustedPeerCert* tp = NULL;
  4117. word32 row;
  4118. if (cm == NULL || cert == NULL)
  4119. return NULL;
  4120. row = TrustedPeerHashSigner(cert->subjectHash);
  4121. if (wc_LockMutex(&cm->tpLock) != 0)
  4122. return ret;
  4123. tp = cm->tpTable[row];
  4124. while (tp) {
  4125. if (XMEMCMP(cert->subjectHash, tp->subjectNameHash,
  4126. SIGNER_DIGEST_SIZE) == 0)
  4127. ret = tp;
  4128. #ifndef NO_SKID
  4129. if (cert->extSubjKeyIdSet) {
  4130. /* Compare SKID as well if available */
  4131. if (ret != NULL && XMEMCMP(cert->extSubjKeyId, tp->subjectKeyIdHash,
  4132. SIGNER_DIGEST_SIZE) != 0)
  4133. ret = NULL;
  4134. }
  4135. #endif
  4136. if (ret != NULL)
  4137. break;
  4138. tp = tp->next;
  4139. }
  4140. wc_UnLockMutex(&cm->tpLock);
  4141. return ret;
  4142. }
  4143. int MatchTrustedPeer(TrustedPeerCert* tp, DecodedCert* cert)
  4144. {
  4145. if (tp == NULL || cert == NULL)
  4146. return BAD_FUNC_ARG;
  4147. /* subject key id or subject hash has been compared when searching
  4148. tpTable for the cert from function GetTrustedPeer */
  4149. /* compare signatures */
  4150. if (tp->sigLen == cert->sigLength) {
  4151. if (XMEMCMP(tp->sig, cert->signature, cert->sigLength)) {
  4152. return WOLFSSL_FAILURE;
  4153. }
  4154. }
  4155. else {
  4156. return WOLFSSL_FAILURE;
  4157. }
  4158. return WOLFSSL_SUCCESS;
  4159. }
  4160. #endif /* WOLFSSL_TRUST_PEER_CERT */
  4161. /* return CA if found, otherwise NULL */
  4162. Signer* GetCA(void* vp, byte* hash)
  4163. {
  4164. WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
  4165. Signer* ret = NULL;
  4166. Signer* signers;
  4167. word32 row = 0;
  4168. if (cm == NULL || hash == NULL)
  4169. return NULL;
  4170. row = HashSigner(hash);
  4171. if (wc_LockMutex(&cm->caLock) != 0)
  4172. return ret;
  4173. signers = cm->caTable[row];
  4174. while (signers) {
  4175. byte* subjectHash;
  4176. #ifndef NO_SKID
  4177. subjectHash = signers->subjectKeyIdHash;
  4178. #else
  4179. subjectHash = signers->subjectNameHash;
  4180. #endif
  4181. if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
  4182. ret = signers;
  4183. break;
  4184. }
  4185. signers = signers->next;
  4186. }
  4187. wc_UnLockMutex(&cm->caLock);
  4188. return ret;
  4189. }
  4190. #ifdef WOLFSSL_AKID_NAME
  4191. Signer* GetCAByAKID(void* vp, const byte* issuer, word32 issuerSz,
  4192. const byte* serial, word32 serialSz)
  4193. {
  4194. WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
  4195. Signer* ret = NULL;
  4196. Signer* signers;
  4197. byte nameHash[SIGNER_DIGEST_SIZE];
  4198. byte serialHash[SIGNER_DIGEST_SIZE];
  4199. word32 row;
  4200. if (cm == NULL || issuer == NULL || issuerSz == 0 ||
  4201. serial == NULL || serialSz == 0)
  4202. return NULL;
  4203. if (CalcHashId(issuer, issuerSz, nameHash) != 0 ||
  4204. CalcHashId(serial, serialSz, serialHash) != 0)
  4205. return NULL;
  4206. if (wc_LockMutex(&cm->caLock) != 0)
  4207. return ret;
  4208. /* Unfortunately we need to look through the entire table */
  4209. for (row = 0; row < CA_TABLE_SIZE && ret == NULL; row++) {
  4210. for (signers = cm->caTable[row]; signers != NULL;
  4211. signers = signers->next) {
  4212. if (XMEMCMP(signers->subjectNameHash, nameHash, SIGNER_DIGEST_SIZE)
  4213. == 0 && XMEMCMP(signers->serialHash, serialHash,
  4214. SIGNER_DIGEST_SIZE) == 0) {
  4215. ret = signers;
  4216. break;
  4217. }
  4218. }
  4219. }
  4220. wc_UnLockMutex(&cm->caLock);
  4221. return ret;
  4222. }
  4223. #endif
  4224. #ifndef NO_SKID
  4225. /* return CA if found, otherwise NULL. Walk through hash table. */
  4226. Signer* GetCAByName(void* vp, byte* hash)
  4227. {
  4228. WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
  4229. Signer* ret = NULL;
  4230. Signer* signers;
  4231. word32 row;
  4232. if (cm == NULL)
  4233. return NULL;
  4234. if (wc_LockMutex(&cm->caLock) != 0)
  4235. return ret;
  4236. for (row = 0; row < CA_TABLE_SIZE && ret == NULL; row++) {
  4237. signers = cm->caTable[row];
  4238. while (signers && ret == NULL) {
  4239. if (XMEMCMP(hash, signers->subjectNameHash,
  4240. SIGNER_DIGEST_SIZE) == 0) {
  4241. ret = signers;
  4242. }
  4243. signers = signers->next;
  4244. }
  4245. }
  4246. wc_UnLockMutex(&cm->caLock);
  4247. return ret;
  4248. }
  4249. #endif
  4250. #ifdef WOLFSSL_TRUST_PEER_CERT
  4251. /* add a trusted peer cert to linked list */
  4252. int AddTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int verify)
  4253. {
  4254. int ret = 0;
  4255. int row = 0;
  4256. TrustedPeerCert* peerCert;
  4257. DecodedCert* cert;
  4258. DerBuffer* der = *pDer;
  4259. WOLFSSL_MSG("Adding a Trusted Peer Cert");
  4260. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cm->heap,
  4261. DYNAMIC_TYPE_DCERT);
  4262. if (cert == NULL) {
  4263. FreeDer(&der);
  4264. return MEMORY_E;
  4265. }
  4266. InitDecodedCert(cert, der->buffer, der->length, cm->heap);
  4267. if ((ret = ParseCert(cert, TRUSTED_PEER_TYPE, verify, cm)) != 0) {
  4268. FreeDecodedCert(cert);
  4269. XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
  4270. FreeDer(&der);
  4271. return ret;
  4272. }
  4273. WOLFSSL_MSG("\tParsed new trusted peer cert");
  4274. peerCert = (TrustedPeerCert*)XMALLOC(sizeof(TrustedPeerCert), cm->heap,
  4275. DYNAMIC_TYPE_CERT);
  4276. if (peerCert == NULL) {
  4277. FreeDecodedCert(cert);
  4278. XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
  4279. FreeDer(&der);
  4280. return MEMORY_E;
  4281. }
  4282. XMEMSET(peerCert, 0, sizeof(TrustedPeerCert));
  4283. #ifndef IGNORE_NAME_CONSTRAINTS
  4284. if (peerCert->permittedNames)
  4285. FreeNameSubtrees(peerCert->permittedNames, cm->heap);
  4286. if (peerCert->excludedNames)
  4287. FreeNameSubtrees(peerCert->excludedNames, cm->heap);
  4288. #endif
  4289. if (AlreadyTrustedPeer(cm, cert)) {
  4290. WOLFSSL_MSG("\tAlready have this CA, not adding again");
  4291. FreeTrustedPeer(peerCert, cm->heap);
  4292. (void)ret;
  4293. }
  4294. else {
  4295. /* add trusted peer signature */
  4296. peerCert->sigLen = cert->sigLength;
  4297. peerCert->sig = (byte *)XMALLOC(cert->sigLength, cm->heap,
  4298. DYNAMIC_TYPE_SIGNATURE);
  4299. if (peerCert->sig == NULL) {
  4300. FreeDecodedCert(cert);
  4301. XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
  4302. FreeTrustedPeer(peerCert, cm->heap);
  4303. FreeDer(&der);
  4304. return MEMORY_E;
  4305. }
  4306. XMEMCPY(peerCert->sig, cert->signature, cert->sigLength);
  4307. /* add trusted peer name */
  4308. peerCert->nameLen = cert->subjectCNLen;
  4309. peerCert->name = cert->subjectCN;
  4310. #ifndef IGNORE_NAME_CONSTRAINTS
  4311. peerCert->permittedNames = cert->permittedNames;
  4312. peerCert->excludedNames = cert->excludedNames;
  4313. #endif
  4314. /* add SKID when available and hash of name */
  4315. #ifndef NO_SKID
  4316. XMEMCPY(peerCert->subjectKeyIdHash, cert->extSubjKeyId,
  4317. SIGNER_DIGEST_SIZE);
  4318. #endif
  4319. XMEMCPY(peerCert->subjectNameHash, cert->subjectHash,
  4320. SIGNER_DIGEST_SIZE);
  4321. /* If Key Usage not set, all uses valid. */
  4322. peerCert->next = NULL;
  4323. cert->subjectCN = 0;
  4324. #ifndef IGNORE_NAME_CONSTRAINTS
  4325. cert->permittedNames = NULL;
  4326. cert->excludedNames = NULL;
  4327. #endif
  4328. row = TrustedPeerHashSigner(peerCert->subjectNameHash);
  4329. if (wc_LockMutex(&cm->tpLock) == 0) {
  4330. peerCert->next = cm->tpTable[row];
  4331. cm->tpTable[row] = peerCert; /* takes ownership */
  4332. wc_UnLockMutex(&cm->tpLock);
  4333. }
  4334. else {
  4335. WOLFSSL_MSG("\tTrusted Peer Cert Mutex Lock failed");
  4336. FreeDecodedCert(cert);
  4337. XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
  4338. FreeTrustedPeer(peerCert, cm->heap);
  4339. FreeDer(&der);
  4340. return BAD_MUTEX_E;
  4341. }
  4342. }
  4343. WOLFSSL_MSG("\tFreeing parsed trusted peer cert");
  4344. FreeDecodedCert(cert);
  4345. XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
  4346. WOLFSSL_MSG("\tFreeing der trusted peer cert");
  4347. FreeDer(&der);
  4348. WOLFSSL_MSG("\t\tOK Freeing der trusted peer cert");
  4349. WOLFSSL_LEAVE("AddTrustedPeer", ret);
  4350. return WOLFSSL_SUCCESS;
  4351. }
  4352. #endif /* WOLFSSL_TRUST_PEER_CERT */
  4353. /* owns der, internal now uses too */
  4354. /* type flag ids from user or from chain received during verify
  4355. don't allow chain ones to be added w/o isCA extension */
  4356. int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify)
  4357. {
  4358. int ret;
  4359. Signer* signer = NULL;
  4360. word32 row;
  4361. byte* subjectHash;
  4362. #ifdef WOLFSSL_SMALL_STACK
  4363. DecodedCert* cert = NULL;
  4364. #else
  4365. DecodedCert cert[1];
  4366. #endif
  4367. DerBuffer* der = *pDer;
  4368. WOLFSSL_MSG("Adding a CA");
  4369. if (cm == NULL) {
  4370. FreeDer(pDer);
  4371. return BAD_FUNC_ARG;
  4372. }
  4373. #ifdef WOLFSSL_SMALL_STACK
  4374. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  4375. DYNAMIC_TYPE_DCERT);
  4376. if (cert == NULL) {
  4377. FreeDer(pDer);
  4378. return MEMORY_E;
  4379. }
  4380. #endif
  4381. InitDecodedCert(cert, der->buffer, der->length, cm->heap);
  4382. ret = ParseCert(cert, CA_TYPE, verify, cm);
  4383. WOLFSSL_MSG("\tParsed new CA");
  4384. #ifndef NO_SKID
  4385. subjectHash = cert->extSubjKeyId;
  4386. #else
  4387. subjectHash = cert->subjectHash;
  4388. #endif
  4389. /* check CA key size */
  4390. if (verify) {
  4391. switch (cert->keyOID) {
  4392. #ifndef NO_RSA
  4393. #ifdef WC_RSA_PSS
  4394. case RSAPSSk:
  4395. #endif
  4396. case RSAk:
  4397. if (cm->minRsaKeySz < 0 ||
  4398. cert->pubKeySize < (word16)cm->minRsaKeySz) {
  4399. ret = RSA_KEY_SIZE_E;
  4400. WOLFSSL_MSG("\tCA RSA key size error");
  4401. }
  4402. break;
  4403. #endif /* !NO_RSA */
  4404. #ifdef HAVE_ECC
  4405. case ECDSAk:
  4406. if (cm->minEccKeySz < 0 ||
  4407. cert->pubKeySize < (word16)cm->minEccKeySz) {
  4408. ret = ECC_KEY_SIZE_E;
  4409. WOLFSSL_MSG("\tCA ECC key size error");
  4410. }
  4411. break;
  4412. #endif /* HAVE_ECC */
  4413. #ifdef HAVE_ED25519
  4414. case ED25519k:
  4415. if (cm->minEccKeySz < 0 ||
  4416. ED25519_KEY_SIZE < (word16)cm->minEccKeySz) {
  4417. ret = ECC_KEY_SIZE_E;
  4418. WOLFSSL_MSG("\tCA ECC key size error");
  4419. }
  4420. break;
  4421. #endif /* HAVE_ED25519 */
  4422. #ifdef HAVE_ED448
  4423. case ED448k:
  4424. if (cm->minEccKeySz < 0 ||
  4425. ED448_KEY_SIZE < (word16)cm->minEccKeySz) {
  4426. ret = ECC_KEY_SIZE_E;
  4427. WOLFSSL_MSG("\tCA ECC key size error");
  4428. }
  4429. break;
  4430. #endif /* HAVE_ED448 */
  4431. #if defined(HAVE_PQC)
  4432. #if defined(HAVE_FALCON)
  4433. case FALCON_LEVEL1k:
  4434. if (cm->minFalconKeySz < 0 ||
  4435. FALCON_LEVEL1_KEY_SIZE < (word16)cm->minFalconKeySz) {
  4436. ret = FALCON_KEY_SIZE_E;
  4437. WOLFSSL_MSG("\tCA Falcon level 1 key size error");
  4438. }
  4439. break;
  4440. case FALCON_LEVEL5k:
  4441. if (cm->minFalconKeySz < 0 ||
  4442. FALCON_LEVEL5_KEY_SIZE < (word16)cm->minFalconKeySz) {
  4443. ret = FALCON_KEY_SIZE_E;
  4444. WOLFSSL_MSG("\tCA Falcon level 5 key size error");
  4445. }
  4446. break;
  4447. #endif /* HAVE_FALCON */
  4448. #if defined(HAVE_DILITHIUM)
  4449. case DILITHIUM_LEVEL2k:
  4450. if (cm->minDilithiumKeySz < 0 ||
  4451. DILITHIUM_LEVEL2_KEY_SIZE < (word16)cm->minDilithiumKeySz) {
  4452. ret = DILITHIUM_KEY_SIZE_E;
  4453. WOLFSSL_MSG("\tCA Dilithium level 2 key size error");
  4454. }
  4455. break;
  4456. case DILITHIUM_LEVEL3k:
  4457. if (cm->minDilithiumKeySz < 0 ||
  4458. DILITHIUM_LEVEL3_KEY_SIZE < (word16)cm->minDilithiumKeySz) {
  4459. ret = DILITHIUM_KEY_SIZE_E;
  4460. WOLFSSL_MSG("\tCA Dilithium level 3 key size error");
  4461. }
  4462. break;
  4463. case DILITHIUM_LEVEL5k:
  4464. if (cm->minDilithiumKeySz < 0 ||
  4465. DILITHIUM_LEVEL5_KEY_SIZE < (word16)cm->minDilithiumKeySz) {
  4466. ret = DILITHIUM_KEY_SIZE_E;
  4467. WOLFSSL_MSG("\tCA Dilithium level 5 key size error");
  4468. }
  4469. break;
  4470. #endif /* HAVE_DILITHIUM */
  4471. #endif /* HAVE_PQC */
  4472. default:
  4473. WOLFSSL_MSG("\tNo key size check done on CA");
  4474. break; /* no size check if key type is not in switch */
  4475. }
  4476. }
  4477. if (ret == 0 && cert->isCA == 0 && type != WOLFSSL_USER_CA) {
  4478. WOLFSSL_MSG("\tCan't add as CA if not actually one");
  4479. ret = NOT_CA_ERROR;
  4480. }
  4481. #ifndef ALLOW_INVALID_CERTSIGN
  4482. else if (ret == 0 && cert->isCA == 1 && type != WOLFSSL_USER_CA &&
  4483. !cert->selfSigned && (cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) {
  4484. /* Intermediate CA certs are required to have the keyCertSign
  4485. * extension set. User loaded root certs are not. */
  4486. WOLFSSL_MSG("\tDoesn't have key usage certificate signing");
  4487. ret = NOT_CA_ERROR;
  4488. }
  4489. #endif
  4490. else if (ret == 0 && AlreadySigner(cm, subjectHash)) {
  4491. WOLFSSL_MSG("\tAlready have this CA, not adding again");
  4492. (void)ret;
  4493. }
  4494. else if (ret == 0) {
  4495. /* take over signer parts */
  4496. signer = MakeSigner(cm->heap);
  4497. if (!signer)
  4498. ret = MEMORY_ERROR;
  4499. }
  4500. #if defined(WOLFSSL_AKID_NAME) || defined(HAVE_CRL)
  4501. if (ret == 0 && signer != NULL)
  4502. ret = CalcHashId(cert->serial, cert->serialSz, signer->serialHash);
  4503. #endif
  4504. if (ret == 0 && signer != NULL) {
  4505. #ifdef WOLFSSL_SIGNER_DER_CERT
  4506. ret = AllocDer(&signer->derCert, der->length, der->type, NULL);
  4507. }
  4508. if (ret == 0 && signer != NULL) {
  4509. XMEMCPY(signer->derCert->buffer, der->buffer, der->length);
  4510. #endif
  4511. signer->keyOID = cert->keyOID;
  4512. if (cert->pubKeyStored) {
  4513. signer->publicKey = cert->publicKey;
  4514. signer->pubKeySize = cert->pubKeySize;
  4515. }
  4516. #ifdef WOLFSSL_DUAL_ALG_CERTS
  4517. if (cert->extSapkiSet && cert->sapkiLen > 0) {
  4518. /* Allocated space for alternative public key. */
  4519. signer->sapkiDer = (byte*)XMALLOC(cert->sapkiLen, cm->heap,
  4520. DYNAMIC_TYPE_PUBLIC_KEY);
  4521. if (signer->sapkiDer == NULL) {
  4522. ret = MEMORY_E;
  4523. }
  4524. else {
  4525. XMEMCPY(signer->sapkiDer, cert->sapkiDer, cert->sapkiLen);
  4526. signer->sapkiLen = cert->sapkiLen;
  4527. signer->sapkiOID = cert->sapkiOID;
  4528. }
  4529. }
  4530. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  4531. if (cert->subjectCNStored) {
  4532. signer->nameLen = cert->subjectCNLen;
  4533. signer->name = cert->subjectCN;
  4534. }
  4535. signer->maxPathLen = cert->maxPathLen;
  4536. signer->selfSigned = cert->selfSigned;
  4537. #ifndef IGNORE_NAME_CONSTRAINTS
  4538. signer->permittedNames = cert->permittedNames;
  4539. signer->excludedNames = cert->excludedNames;
  4540. #endif
  4541. #ifndef NO_SKID
  4542. XMEMCPY(signer->subjectKeyIdHash, cert->extSubjKeyId,
  4543. SIGNER_DIGEST_SIZE);
  4544. #endif
  4545. XMEMCPY(signer->subjectNameHash, cert->subjectHash,
  4546. SIGNER_DIGEST_SIZE);
  4547. #if defined(HAVE_OCSP) || defined(HAVE_CRL)
  4548. XMEMCPY(signer->issuerNameHash, cert->issuerHash,
  4549. SIGNER_DIGEST_SIZE);
  4550. #endif
  4551. #ifdef HAVE_OCSP
  4552. XMEMCPY(signer->subjectKeyHash, cert->subjectKeyHash,
  4553. KEYID_SIZE);
  4554. #endif
  4555. signer->keyUsage = cert->extKeyUsageSet ? cert->extKeyUsage
  4556. : 0xFFFF;
  4557. signer->next = NULL; /* If Key Usage not set, all uses valid. */
  4558. cert->publicKey = 0; /* in case lock fails don't free here. */
  4559. cert->subjectCN = 0;
  4560. #ifndef IGNORE_NAME_CONSTRAINTS
  4561. cert->permittedNames = NULL;
  4562. cert->excludedNames = NULL;
  4563. #endif
  4564. signer->type = (byte)type;
  4565. #ifndef NO_SKID
  4566. row = HashSigner(signer->subjectKeyIdHash);
  4567. #else
  4568. row = HashSigner(signer->subjectNameHash);
  4569. #endif
  4570. if (wc_LockMutex(&cm->caLock) == 0) {
  4571. signer->next = cm->caTable[row];
  4572. cm->caTable[row] = signer; /* takes ownership */
  4573. wc_UnLockMutex(&cm->caLock);
  4574. if (cm->caCacheCallback)
  4575. cm->caCacheCallback(der->buffer, (int)der->length, type);
  4576. }
  4577. else {
  4578. WOLFSSL_MSG("\tCA Mutex Lock failed");
  4579. ret = BAD_MUTEX_E;
  4580. }
  4581. }
  4582. #if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_FSPSM_TLS)
  4583. /* Verify CA by TSIP so that generated tsip key is going to be able to */
  4584. /* be used for peer's cert verification */
  4585. /* TSIP is only able to handle USER CA, and only one CA. */
  4586. /* Therefore, it doesn't need to call TSIP again if there is already */
  4587. /* verified CA. */
  4588. if ( ret == 0 && signer != NULL ) {
  4589. signer->cm_idx = row;
  4590. if (type == WOLFSSL_USER_CA) {
  4591. if ((ret = wc_Renesas_cmn_RootCertVerify(cert->source, cert->maxIdx,
  4592. cert->sigCtx.CertAtt.pubkey_n_start,
  4593. cert->sigCtx.CertAtt.pubkey_n_len - 1,
  4594. cert->sigCtx.CertAtt.pubkey_e_start,
  4595. cert->sigCtx.CertAtt.pubkey_e_len - 1,
  4596. row/* cm index */))
  4597. < 0)
  4598. WOLFSSL_MSG("Renesas_RootCertVerify() failed");
  4599. else
  4600. WOLFSSL_MSG("Renesas_RootCertVerify() succeed or skipped");
  4601. }
  4602. }
  4603. #endif /* TSIP or SCE */
  4604. WOLFSSL_MSG("\tFreeing Parsed CA");
  4605. FreeDecodedCert(cert);
  4606. if (ret != 0 && signer != NULL)
  4607. FreeSigner(signer, cm->heap);
  4608. #ifdef WOLFSSL_SMALL_STACK
  4609. XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
  4610. #endif
  4611. WOLFSSL_MSG("\tFreeing der CA");
  4612. FreeDer(pDer);
  4613. WOLFSSL_MSG("\t\tOK Freeing der CA");
  4614. WOLFSSL_LEAVE("AddCA", ret);
  4615. return ret == 0 ? WOLFSSL_SUCCESS : ret;
  4616. }
  4617. #endif /* !NO_CERTS */
  4618. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  4619. static int wolfSSL_RAND_InitMutex(void);
  4620. #endif
  4621. #if defined(OPENSSL_EXTRA) && defined(HAVE_ATEXIT)
  4622. static void AtExitCleanup(void)
  4623. {
  4624. if (initRefCount > 0) {
  4625. initRefCount = 1;
  4626. (void)wolfSSL_Cleanup();
  4627. }
  4628. }
  4629. #endif
  4630. WOLFSSL_ABI
  4631. int wolfSSL_Init(void)
  4632. {
  4633. int ret = WOLFSSL_SUCCESS;
  4634. #if !defined(NO_SESSION_CACHE) && defined(ENABLE_SESSION_CACHE_ROW_LOCK)
  4635. int i;
  4636. #endif
  4637. WOLFSSL_ENTER("wolfSSL_Init");
  4638. #ifndef WOLFSSL_MUTEX_INITIALIZER
  4639. if (inits_count_mutex_valid == 0) {
  4640. if (wc_InitMutex(&inits_count_mutex) != 0) {
  4641. WOLFSSL_MSG("Bad Init Mutex count");
  4642. return BAD_MUTEX_E;
  4643. }
  4644. else {
  4645. inits_count_mutex_valid = 1;
  4646. }
  4647. }
  4648. #endif /* !WOLFSSL_MUTEX_INITIALIZER */
  4649. if (wc_LockMutex(&inits_count_mutex) != 0) {
  4650. WOLFSSL_MSG("Bad Lock Mutex count");
  4651. return BAD_MUTEX_E;
  4652. }
  4653. #if FIPS_VERSION_GE(5,1)
  4654. if ((ret == WOLFSSL_SUCCESS) && (initRefCount == 0)) {
  4655. ret = wolfCrypt_SetPrivateKeyReadEnable_fips(1, WC_KEYTYPE_ALL);
  4656. if (ret == 0)
  4657. ret = WOLFSSL_SUCCESS;
  4658. }
  4659. #endif
  4660. if ((ret == WOLFSSL_SUCCESS) && (initRefCount == 0)) {
  4661. /* Initialize crypto for use with TLS connection */
  4662. if (wolfCrypt_Init() != 0) {
  4663. WOLFSSL_MSG("Bad wolfCrypt Init");
  4664. ret = WC_INIT_E;
  4665. }
  4666. #if defined(HAVE_GLOBAL_RNG) && !defined(WOLFSSL_MUTEX_INITIALIZER)
  4667. if (ret == WOLFSSL_SUCCESS) {
  4668. if (wc_InitMutex(&globalRNGMutex) != 0) {
  4669. WOLFSSL_MSG("Bad Init Mutex rng");
  4670. ret = BAD_MUTEX_E;
  4671. }
  4672. else {
  4673. globalRNGMutex_valid = 1;
  4674. }
  4675. }
  4676. #endif
  4677. #ifdef WC_RNG_SEED_CB
  4678. wc_SetSeed_Cb(wc_GenerateSeed);
  4679. #endif
  4680. #ifdef OPENSSL_EXTRA
  4681. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  4682. if ((ret == WOLFSSL_SUCCESS) && (wolfSSL_RAND_InitMutex() != 0)) {
  4683. ret = BAD_MUTEX_E;
  4684. }
  4685. #endif
  4686. if ((ret == WOLFSSL_SUCCESS) &&
  4687. (wolfSSL_RAND_seed(NULL, 0) != WOLFSSL_SUCCESS)) {
  4688. WOLFSSL_MSG("wolfSSL_RAND_seed failed");
  4689. ret = WC_INIT_E;
  4690. }
  4691. #endif
  4692. #ifndef NO_SESSION_CACHE
  4693. #ifdef ENABLE_SESSION_CACHE_ROW_LOCK
  4694. for (i = 0; i < SESSION_ROWS; ++i) {
  4695. SessionCache[i].lock_valid = 0;
  4696. }
  4697. for (i = 0; (ret == WOLFSSL_SUCCESS) && (i < SESSION_ROWS); ++i) {
  4698. if (wc_InitRwLock(&SessionCache[i].row_lock) != 0) {
  4699. WOLFSSL_MSG("Bad Init Mutex session");
  4700. ret = BAD_MUTEX_E;
  4701. }
  4702. else {
  4703. SessionCache[i].lock_valid = 1;
  4704. }
  4705. }
  4706. #else
  4707. if (ret == WOLFSSL_SUCCESS) {
  4708. if (wc_InitRwLock(&session_lock) != 0) {
  4709. WOLFSSL_MSG("Bad Init Mutex session");
  4710. ret = BAD_MUTEX_E;
  4711. }
  4712. else {
  4713. session_lock_valid = 1;
  4714. }
  4715. }
  4716. #endif
  4717. #ifndef NO_CLIENT_CACHE
  4718. #ifndef WOLFSSL_MUTEX_INITIALIZER
  4719. if (ret == WOLFSSL_SUCCESS) {
  4720. if (wc_InitMutex(&clisession_mutex) != 0) {
  4721. WOLFSSL_MSG("Bad Init Mutex session");
  4722. ret = BAD_MUTEX_E;
  4723. }
  4724. else {
  4725. clisession_mutex_valid = 1;
  4726. }
  4727. }
  4728. #endif
  4729. #endif
  4730. #endif
  4731. #if defined(OPENSSL_EXTRA) && defined(HAVE_ATEXIT)
  4732. /* OpenSSL registers cleanup using atexit */
  4733. if ((ret == WOLFSSL_SUCCESS) && (atexit(AtExitCleanup) != 0)) {
  4734. WOLFSSL_MSG("Bad atexit registration");
  4735. ret = WC_INIT_E;
  4736. }
  4737. #endif
  4738. }
  4739. if (ret == WOLFSSL_SUCCESS) {
  4740. initRefCount++;
  4741. }
  4742. else {
  4743. initRefCount = 1; /* Force cleanup */
  4744. }
  4745. wc_UnLockMutex(&inits_count_mutex);
  4746. if (ret != WOLFSSL_SUCCESS) {
  4747. (void)wolfSSL_Cleanup(); /* Ignore any error from cleanup */
  4748. }
  4749. return ret;
  4750. }
  4751. #define WOLFSSL_SSL_LOAD_INCLUDED
  4752. #include <src/ssl_load.c>
  4753. #ifndef NO_CERTS
  4754. #ifdef HAVE_CRL
  4755. int wolfSSL_CTX_LoadCRLBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
  4756. long sz, int type)
  4757. {
  4758. WOLFSSL_ENTER("wolfSSL_CTX_LoadCRLBuffer");
  4759. if (ctx == NULL)
  4760. return BAD_FUNC_ARG;
  4761. return wolfSSL_CertManagerLoadCRLBuffer(ctx->cm, buff, sz, type);
  4762. }
  4763. int wolfSSL_LoadCRLBuffer(WOLFSSL* ssl, const unsigned char* buff,
  4764. long sz, int type)
  4765. {
  4766. WOLFSSL_ENTER("wolfSSL_LoadCRLBuffer");
  4767. if (ssl == NULL || ssl->ctx == NULL)
  4768. return BAD_FUNC_ARG;
  4769. SSL_CM_WARNING(ssl);
  4770. return wolfSSL_CertManagerLoadCRLBuffer(SSL_CM(ssl), buff, sz, type);
  4771. }
  4772. #endif /* HAVE_CRL */
  4773. #ifdef HAVE_OCSP
  4774. int wolfSSL_EnableOCSP(WOLFSSL* ssl, int options)
  4775. {
  4776. WOLFSSL_ENTER("wolfSSL_EnableOCSP");
  4777. if (ssl) {
  4778. SSL_CM_WARNING(ssl);
  4779. return wolfSSL_CertManagerEnableOCSP(SSL_CM(ssl), options);
  4780. }
  4781. else
  4782. return BAD_FUNC_ARG;
  4783. }
  4784. int wolfSSL_DisableOCSP(WOLFSSL* ssl)
  4785. {
  4786. WOLFSSL_ENTER("wolfSSL_DisableOCSP");
  4787. if (ssl) {
  4788. SSL_CM_WARNING(ssl);
  4789. return wolfSSL_CertManagerDisableOCSP(SSL_CM(ssl));
  4790. }
  4791. else
  4792. return BAD_FUNC_ARG;
  4793. }
  4794. int wolfSSL_EnableOCSPStapling(WOLFSSL* ssl)
  4795. {
  4796. WOLFSSL_ENTER("wolfSSL_EnableOCSPStapling");
  4797. if (ssl) {
  4798. SSL_CM_WARNING(ssl);
  4799. return wolfSSL_CertManagerEnableOCSPStapling(SSL_CM(ssl));
  4800. }
  4801. else
  4802. return BAD_FUNC_ARG;
  4803. }
  4804. int wolfSSL_DisableOCSPStapling(WOLFSSL* ssl)
  4805. {
  4806. WOLFSSL_ENTER("wolfSSL_DisableOCSPStapling");
  4807. if (ssl) {
  4808. SSL_CM_WARNING(ssl);
  4809. return wolfSSL_CertManagerDisableOCSPStapling(SSL_CM(ssl));
  4810. }
  4811. else
  4812. return BAD_FUNC_ARG;
  4813. }
  4814. int wolfSSL_SetOCSP_OverrideURL(WOLFSSL* ssl, const char* url)
  4815. {
  4816. WOLFSSL_ENTER("wolfSSL_SetOCSP_OverrideURL");
  4817. if (ssl) {
  4818. SSL_CM_WARNING(ssl);
  4819. return wolfSSL_CertManagerSetOCSPOverrideURL(SSL_CM(ssl), url);
  4820. }
  4821. else
  4822. return BAD_FUNC_ARG;
  4823. }
  4824. int wolfSSL_SetOCSP_Cb(WOLFSSL* ssl,
  4825. CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
  4826. {
  4827. WOLFSSL_ENTER("wolfSSL_SetOCSP_Cb");
  4828. if (ssl) {
  4829. SSL_CM_WARNING(ssl);
  4830. ssl->ocspIOCtx = ioCbCtx; /* use SSL specific ioCbCtx */
  4831. return wolfSSL_CertManagerSetOCSP_Cb(SSL_CM(ssl),
  4832. ioCb, respFreeCb, NULL);
  4833. }
  4834. else
  4835. return BAD_FUNC_ARG;
  4836. }
  4837. int wolfSSL_CTX_EnableOCSP(WOLFSSL_CTX* ctx, int options)
  4838. {
  4839. WOLFSSL_ENTER("wolfSSL_CTX_EnableOCSP");
  4840. if (ctx)
  4841. return wolfSSL_CertManagerEnableOCSP(ctx->cm, options);
  4842. else
  4843. return BAD_FUNC_ARG;
  4844. }
  4845. int wolfSSL_CTX_DisableOCSP(WOLFSSL_CTX* ctx)
  4846. {
  4847. WOLFSSL_ENTER("wolfSSL_CTX_DisableOCSP");
  4848. if (ctx)
  4849. return wolfSSL_CertManagerDisableOCSP(ctx->cm);
  4850. else
  4851. return BAD_FUNC_ARG;
  4852. }
  4853. int wolfSSL_CTX_SetOCSP_OverrideURL(WOLFSSL_CTX* ctx, const char* url)
  4854. {
  4855. WOLFSSL_ENTER("wolfSSL_SetOCSP_OverrideURL");
  4856. if (ctx)
  4857. return wolfSSL_CertManagerSetOCSPOverrideURL(ctx->cm, url);
  4858. else
  4859. return BAD_FUNC_ARG;
  4860. }
  4861. int wolfSSL_CTX_SetOCSP_Cb(WOLFSSL_CTX* ctx, CbOCSPIO ioCb,
  4862. CbOCSPRespFree respFreeCb, void* ioCbCtx)
  4863. {
  4864. WOLFSSL_ENTER("wolfSSL_CTX_SetOCSP_Cb");
  4865. if (ctx)
  4866. return wolfSSL_CertManagerSetOCSP_Cb(ctx->cm, ioCb,
  4867. respFreeCb, ioCbCtx);
  4868. else
  4869. return BAD_FUNC_ARG;
  4870. }
  4871. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  4872. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4873. int wolfSSL_CTX_EnableOCSPStapling(WOLFSSL_CTX* ctx)
  4874. {
  4875. WOLFSSL_ENTER("wolfSSL_CTX_EnableOCSPStapling");
  4876. if (ctx)
  4877. return wolfSSL_CertManagerEnableOCSPStapling(ctx->cm);
  4878. else
  4879. return BAD_FUNC_ARG;
  4880. }
  4881. int wolfSSL_CTX_DisableOCSPStapling(WOLFSSL_CTX* ctx)
  4882. {
  4883. WOLFSSL_ENTER("wolfSSL_CTX_DisableOCSPStapling");
  4884. if (ctx)
  4885. return wolfSSL_CertManagerDisableOCSPStapling(ctx->cm);
  4886. else
  4887. return BAD_FUNC_ARG;
  4888. }
  4889. int wolfSSL_CTX_EnableOCSPMustStaple(WOLFSSL_CTX* ctx)
  4890. {
  4891. WOLFSSL_ENTER("wolfSSL_CTX_EnableOCSPMustStaple");
  4892. if (ctx)
  4893. return wolfSSL_CertManagerEnableOCSPMustStaple(ctx->cm);
  4894. else
  4895. return BAD_FUNC_ARG;
  4896. }
  4897. int wolfSSL_CTX_DisableOCSPMustStaple(WOLFSSL_CTX* ctx)
  4898. {
  4899. WOLFSSL_ENTER("wolfSSL_CTX_DisableOCSPMustStaple");
  4900. if (ctx)
  4901. return wolfSSL_CertManagerDisableOCSPMustStaple(ctx->cm);
  4902. else
  4903. return BAD_FUNC_ARG;
  4904. }
  4905. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST || \
  4906. * HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  4907. #endif /* HAVE_OCSP */
  4908. #ifdef HAVE_CRL
  4909. int wolfSSL_EnableCRL(WOLFSSL* ssl, int options)
  4910. {
  4911. WOLFSSL_ENTER("wolfSSL_EnableCRL");
  4912. if (ssl) {
  4913. SSL_CM_WARNING(ssl);
  4914. return wolfSSL_CertManagerEnableCRL(SSL_CM(ssl), options);
  4915. }
  4916. else
  4917. return BAD_FUNC_ARG;
  4918. }
  4919. int wolfSSL_DisableCRL(WOLFSSL* ssl)
  4920. {
  4921. WOLFSSL_ENTER("wolfSSL_DisableCRL");
  4922. if (ssl) {
  4923. SSL_CM_WARNING(ssl);
  4924. return wolfSSL_CertManagerDisableCRL(SSL_CM(ssl));
  4925. }
  4926. else
  4927. return BAD_FUNC_ARG;
  4928. }
  4929. #ifndef NO_FILESYSTEM
  4930. int wolfSSL_LoadCRL(WOLFSSL* ssl, const char* path, int type, int monitor)
  4931. {
  4932. WOLFSSL_ENTER("wolfSSL_LoadCRL");
  4933. if (ssl) {
  4934. SSL_CM_WARNING(ssl);
  4935. return wolfSSL_CertManagerLoadCRL(SSL_CM(ssl), path, type, monitor);
  4936. }
  4937. else
  4938. return BAD_FUNC_ARG;
  4939. }
  4940. int wolfSSL_LoadCRLFile(WOLFSSL* ssl, const char* file, int type)
  4941. {
  4942. WOLFSSL_ENTER("wolfSSL_LoadCRLFile");
  4943. if (ssl) {
  4944. SSL_CM_WARNING(ssl);
  4945. return wolfSSL_CertManagerLoadCRLFile(SSL_CM(ssl), file, type);
  4946. }
  4947. else
  4948. return BAD_FUNC_ARG;
  4949. }
  4950. #endif
  4951. int wolfSSL_SetCRL_Cb(WOLFSSL* ssl, CbMissingCRL cb)
  4952. {
  4953. WOLFSSL_ENTER("wolfSSL_SetCRL_Cb");
  4954. if (ssl) {
  4955. SSL_CM_WARNING(ssl);
  4956. return wolfSSL_CertManagerSetCRL_Cb(SSL_CM(ssl), cb);
  4957. }
  4958. else
  4959. return BAD_FUNC_ARG;
  4960. }
  4961. #ifdef HAVE_CRL_IO
  4962. int wolfSSL_SetCRL_IOCb(WOLFSSL* ssl, CbCrlIO cb)
  4963. {
  4964. WOLFSSL_ENTER("wolfSSL_SetCRL_Cb");
  4965. if (ssl) {
  4966. SSL_CM_WARNING(ssl);
  4967. return wolfSSL_CertManagerSetCRL_IOCb(SSL_CM(ssl), cb);
  4968. }
  4969. else
  4970. return BAD_FUNC_ARG;
  4971. }
  4972. #endif
  4973. int wolfSSL_CTX_EnableCRL(WOLFSSL_CTX* ctx, int options)
  4974. {
  4975. WOLFSSL_ENTER("wolfSSL_CTX_EnableCRL");
  4976. if (ctx)
  4977. return wolfSSL_CertManagerEnableCRL(ctx->cm, options);
  4978. else
  4979. return BAD_FUNC_ARG;
  4980. }
  4981. int wolfSSL_CTX_DisableCRL(WOLFSSL_CTX* ctx)
  4982. {
  4983. WOLFSSL_ENTER("wolfSSL_CTX_DisableCRL");
  4984. if (ctx)
  4985. return wolfSSL_CertManagerDisableCRL(ctx->cm);
  4986. else
  4987. return BAD_FUNC_ARG;
  4988. }
  4989. #ifndef NO_FILESYSTEM
  4990. int wolfSSL_CTX_LoadCRL(WOLFSSL_CTX* ctx, const char* path,
  4991. int type, int monitor)
  4992. {
  4993. WOLFSSL_ENTER("wolfSSL_CTX_LoadCRL");
  4994. if (ctx)
  4995. return wolfSSL_CertManagerLoadCRL(ctx->cm, path, type, monitor);
  4996. else
  4997. return BAD_FUNC_ARG;
  4998. }
  4999. int wolfSSL_CTX_LoadCRLFile(WOLFSSL_CTX* ctx, const char* file,
  5000. int type)
  5001. {
  5002. WOLFSSL_ENTER("wolfSSL_CTX_LoadCRL");
  5003. if (ctx)
  5004. return wolfSSL_CertManagerLoadCRLFile(ctx->cm, file, type);
  5005. else
  5006. return BAD_FUNC_ARG;
  5007. }
  5008. #endif
  5009. int wolfSSL_CTX_SetCRL_Cb(WOLFSSL_CTX* ctx, CbMissingCRL cb)
  5010. {
  5011. WOLFSSL_ENTER("wolfSSL_CTX_SetCRL_Cb");
  5012. if (ctx)
  5013. return wolfSSL_CertManagerSetCRL_Cb(ctx->cm, cb);
  5014. else
  5015. return BAD_FUNC_ARG;
  5016. }
  5017. #ifdef HAVE_CRL_IO
  5018. int wolfSSL_CTX_SetCRL_IOCb(WOLFSSL_CTX* ctx, CbCrlIO cb)
  5019. {
  5020. WOLFSSL_ENTER("wolfSSL_CTX_SetCRL_IOCb");
  5021. if (ctx)
  5022. return wolfSSL_CertManagerSetCRL_IOCb(ctx->cm, cb);
  5023. else
  5024. return BAD_FUNC_ARG;
  5025. }
  5026. #endif
  5027. #endif /* HAVE_CRL */
  5028. /* Sets the max chain depth when verifying a certificate chain. Default depth
  5029. * is set to MAX_CHAIN_DEPTH.
  5030. *
  5031. * ctx WOLFSSL_CTX structure to set depth in
  5032. * depth max depth
  5033. */
  5034. void wolfSSL_CTX_set_verify_depth(WOLFSSL_CTX *ctx, int depth) {
  5035. WOLFSSL_ENTER("wolfSSL_CTX_set_verify_depth");
  5036. if (ctx == NULL || depth < 0 || depth > MAX_CHAIN_DEPTH) {
  5037. WOLFSSL_MSG("Bad depth argument, too large or less than 0");
  5038. return;
  5039. }
  5040. ctx->verifyDepth = (byte)depth;
  5041. }
  5042. /* get cert chaining depth using ssl struct */
  5043. long wolfSSL_get_verify_depth(WOLFSSL* ssl)
  5044. {
  5045. if(ssl == NULL) {
  5046. return BAD_FUNC_ARG;
  5047. }
  5048. #ifndef OPENSSL_EXTRA
  5049. return MAX_CHAIN_DEPTH;
  5050. #else
  5051. return ssl->options.verifyDepth;
  5052. #endif
  5053. }
  5054. /* get cert chaining depth using ctx struct */
  5055. long wolfSSL_CTX_get_verify_depth(WOLFSSL_CTX* ctx)
  5056. {
  5057. if (ctx == NULL) {
  5058. return BAD_FUNC_ARG;
  5059. }
  5060. #ifndef OPENSSL_EXTRA
  5061. return MAX_CHAIN_DEPTH;
  5062. #else
  5063. return ctx->verifyDepth;
  5064. #endif
  5065. }
  5066. #ifndef NO_CHECK_PRIVATE_KEY
  5067. #ifdef WOLF_PRIVATE_KEY_ID
  5068. /* Check private against public in certificate for match using external
  5069. * device with given devId */
  5070. static int check_cert_key_dev(word32 keyOID, byte* privKey, word32 privSz,
  5071. const byte* pubKey, word32 pubSz, int label, int id, void* heap, int devId)
  5072. {
  5073. int ret = 0;
  5074. int type = 0;
  5075. void *pkey = NULL;
  5076. if (privKey == NULL) {
  5077. return MISSING_KEY;
  5078. }
  5079. #ifndef NO_RSA
  5080. if (keyOID == RSAk) {
  5081. type = DYNAMIC_TYPE_RSA;
  5082. }
  5083. #ifdef WC_RSA_PSS
  5084. if (keyOID == RSAPSSk) {
  5085. type = DYNAMIC_TYPE_RSA;
  5086. }
  5087. #endif
  5088. #endif
  5089. #ifdef HAVE_ECC
  5090. if (keyOID == ECDSAk) {
  5091. type = DYNAMIC_TYPE_ECC;
  5092. }
  5093. #endif
  5094. #if defined(HAVE_PQC) && defined(HAVE_DILITHIUM)
  5095. if ((keyOID == DILITHIUM_LEVEL2k) ||
  5096. (keyOID == DILITHIUM_LEVEL3k) ||
  5097. (keyOID == DILITHIUM_LEVEL5k)) {
  5098. type = DYNAMIC_TYPE_DILITHIUM;
  5099. }
  5100. #endif
  5101. #if defined(HAVE_PQC) && defined(HAVE_FALCON)
  5102. if ((keyOID == FALCON_LEVEL1k) ||
  5103. (keyOID == FALCON_LEVEL5k)) {
  5104. type = DYNAMIC_TYPE_FALCON;
  5105. }
  5106. #endif
  5107. ret = CreateDevPrivateKey(&pkey, privKey, privSz, type, label, id,
  5108. heap, devId);
  5109. #ifdef WOLF_CRYPTO_CB
  5110. if (ret == 0) {
  5111. #ifndef NO_RSA
  5112. if (keyOID == RSAk
  5113. #ifdef WC_RSA_PSS
  5114. || keyOID == RSAPSSk
  5115. #endif
  5116. ) {
  5117. ret = wc_CryptoCb_RsaCheckPrivKey((RsaKey*)pkey, pubKey, pubSz);
  5118. }
  5119. #endif
  5120. #ifdef HAVE_ECC
  5121. if (keyOID == ECDSAk) {
  5122. ret = wc_CryptoCb_EccCheckPrivKey((ecc_key*)pkey, pubKey, pubSz);
  5123. }
  5124. #endif
  5125. #if defined(HAVE_PQC) && defined(HAVE_DILITHIUM)
  5126. if ((keyOID == DILITHIUM_LEVEL2k) ||
  5127. (keyOID == DILITHIUM_LEVEL3k) ||
  5128. (keyOID == DILITHIUM_LEVEL5k)) {
  5129. ret = wc_CryptoCb_PqcSignatureCheckPrivKey(pkey,
  5130. WC_PQC_SIG_TYPE_DILITHIUM,
  5131. pubKey, pubSz);
  5132. }
  5133. #endif
  5134. #if defined(HAVE_PQC) && defined(HAVE_FALCON)
  5135. if ((keyOID == FALCON_LEVEL1k) ||
  5136. (keyOID == FALCON_LEVEL5k)) {
  5137. ret = wc_CryptoCb_PqcSignatureCheckPrivKey(pkey,
  5138. WC_PQC_SIG_TYPE_FALCON,
  5139. pubKey, pubSz);
  5140. }
  5141. #endif
  5142. }
  5143. #else
  5144. /* devId was set, don't check, for now */
  5145. /* TODO: Add callback for private key check? */
  5146. (void) pubKey;
  5147. (void) pubSz;
  5148. #endif
  5149. if (pkey != NULL) {
  5150. #ifndef NO_RSA
  5151. if (keyOID == RSAk
  5152. #ifdef WC_RSA_PSS
  5153. || keyOID == RSAPSSk
  5154. #endif
  5155. ) {
  5156. wc_FreeRsaKey((RsaKey*)pkey);
  5157. }
  5158. #endif
  5159. #ifdef HAVE_ECC
  5160. if (keyOID == ECDSAk) {
  5161. wc_ecc_free((ecc_key*)pkey);
  5162. }
  5163. #endif
  5164. #if defined(HAVE_PQC) && defined(HAVE_DILITHIUM)
  5165. if ((keyOID == DILITHIUM_LEVEL2k) ||
  5166. (keyOID == DILITHIUM_LEVEL3k) ||
  5167. (keyOID == DILITHIUM_LEVEL5k)) {
  5168. wc_dilithium_free((dilithium_key*)pkey);
  5169. }
  5170. #endif
  5171. #if defined(HAVE_PQC) && defined(HAVE_FALCON)
  5172. if ((keyOID == FALCON_LEVEL1k) ||
  5173. (keyOID == FALCON_LEVEL5k)) {
  5174. wc_falcon_free((falcon_key*)pkey);
  5175. }
  5176. #endif
  5177. XFREE(pkey, heap, type);
  5178. }
  5179. return ret;
  5180. }
  5181. #endif /* WOLF_PRIVATE_KEY_ID */
  5182. /* Check private against public in certificate for match
  5183. *
  5184. * Returns WOLFSSL_SUCCESS on good private key
  5185. * WOLFSSL_FAILURE if mismatched */
  5186. static int check_cert_key(DerBuffer* cert, DerBuffer* key, DerBuffer* altKey,
  5187. void* heap, int devId, int isKeyLabel, int isKeyId, int altDevId,
  5188. int isAltKeyLabel, int isAltKeyId)
  5189. {
  5190. #ifdef WOLFSSL_SMALL_STACK
  5191. DecodedCert* der = NULL;
  5192. #else
  5193. DecodedCert der[1];
  5194. #endif
  5195. word32 size;
  5196. byte* buff;
  5197. int ret = WOLFSSL_FAILURE;
  5198. WOLFSSL_ENTER("check_cert_key");
  5199. if (cert == NULL || key == NULL) {
  5200. return WOLFSSL_FAILURE;
  5201. }
  5202. #ifdef WOLFSSL_SMALL_STACK
  5203. der = (DecodedCert*)XMALLOC(sizeof(DecodedCert), heap, DYNAMIC_TYPE_DCERT);
  5204. if (der == NULL)
  5205. return MEMORY_E;
  5206. #endif
  5207. size = cert->length;
  5208. buff = cert->buffer;
  5209. InitDecodedCert_ex(der, buff, size, heap, devId);
  5210. if (ParseCertRelative(der, CERT_TYPE, NO_VERIFY, NULL) != 0) {
  5211. FreeDecodedCert(der);
  5212. #ifdef WOLFSSL_SMALL_STACK
  5213. XFREE(der, heap, DYNAMIC_TYPE_DCERT);
  5214. #endif
  5215. return WOLFSSL_FAILURE;
  5216. }
  5217. size = key->length;
  5218. buff = key->buffer;
  5219. #ifdef WOLF_PRIVATE_KEY_ID
  5220. if (devId != INVALID_DEVID) {
  5221. ret = check_cert_key_dev(der->keyOID, buff, size, der->publicKey,
  5222. der->pubKeySize, isKeyLabel, isKeyId, heap,
  5223. devId);
  5224. if (ret != CRYPTOCB_UNAVAILABLE) {
  5225. ret = (ret == 0) ? WOLFSSL_SUCCESS: WOLFSSL_FAILURE;
  5226. }
  5227. }
  5228. else {
  5229. /* fall through if unavailable */
  5230. ret = CRYPTOCB_UNAVAILABLE;
  5231. }
  5232. if (ret == CRYPTOCB_UNAVAILABLE)
  5233. #endif /* WOLF_PRIVATE_KEY_ID */
  5234. {
  5235. ret = wc_CheckPrivateKeyCert(buff, size, der, 0);
  5236. ret = (ret == 1) ? WOLFSSL_SUCCESS: WOLFSSL_FAILURE;
  5237. }
  5238. #ifdef WOLFSSL_DUAL_ALG_CERTS
  5239. if (ret == WOLFSSL_SUCCESS && der->extSapkiSet && der->sapkiDer != NULL) {
  5240. /* Certificate contains an alternative public key. Hence, we also
  5241. * need an alternative private key. */
  5242. if (altKey == NULL) {
  5243. ret = MISSING_KEY;
  5244. buff = NULL;
  5245. size = 0;
  5246. }
  5247. else {
  5248. size = altKey->length;
  5249. buff = altKey->buffer;
  5250. }
  5251. #ifdef WOLF_PRIVATE_KEY_ID
  5252. if (ret == WOLFSSL_SUCCESS && altDevId != INVALID_DEVID) {
  5253. /* We have to decode the public key first */
  5254. word32 idx = 0;
  5255. /* Dilithium has the largest public key at the moment */
  5256. word32 pubKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  5257. byte* decodedPubKey = (byte*)XMALLOC(pubKeyLen, heap,
  5258. DYNAMIC_TYPE_PUBLIC_KEY);
  5259. if (decodedPubKey == NULL) {
  5260. ret = MEMORY_E;
  5261. }
  5262. if (ret == WOLFSSL_SUCCESS) {
  5263. if (der->sapkiOID == RSAk || der->sapkiOID == ECDSAk) {
  5264. /* Simply copy the data */
  5265. XMEMCPY(decodedPubKey, der->sapkiDer, der->sapkiLen);
  5266. pubKeyLen = der->sapkiLen;
  5267. ret = 0;
  5268. }
  5269. else {
  5270. ret = DecodeAsymKeyPublic(der->sapkiDer, &idx,
  5271. der->sapkiLen, decodedPubKey,
  5272. &pubKeyLen, der->sapkiOID);
  5273. }
  5274. }
  5275. if (ret == 0) {
  5276. ret = check_cert_key_dev(der->sapkiOID, buff, size,
  5277. decodedPubKey, pubKeyLen,
  5278. isAltKeyLabel, isAltKeyId,
  5279. heap, altDevId);
  5280. }
  5281. XFREE(decodedPubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5282. if (ret != CRYPTOCB_UNAVAILABLE) {
  5283. ret = (ret == 0) ? WOLFSSL_SUCCESS: WOLFSSL_FAILURE;
  5284. }
  5285. }
  5286. else {
  5287. /* fall through if unavailable */
  5288. ret = CRYPTOCB_UNAVAILABLE;
  5289. }
  5290. if (ret == CRYPTOCB_UNAVAILABLE)
  5291. #endif /* WOLF_PRIVATE_KEY_ID */
  5292. {
  5293. ret = wc_CheckPrivateKeyCert(buff, size, der, 1);
  5294. ret = (ret == 1) ? WOLFSSL_SUCCESS: WOLFSSL_FAILURE;
  5295. }
  5296. }
  5297. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  5298. FreeDecodedCert(der);
  5299. #ifdef WOLFSSL_SMALL_STACK
  5300. XFREE(der, heap, DYNAMIC_TYPE_DCERT);
  5301. #endif
  5302. (void)devId;
  5303. (void)isKeyLabel;
  5304. (void)isKeyId;
  5305. (void)altKey;
  5306. (void)altDevId;
  5307. (void)isAltKeyLabel;
  5308. (void)isAltKeyId;
  5309. return ret;
  5310. }
  5311. /* Check private against public in certificate for match
  5312. *
  5313. * ctx WOLFSSL_CTX structure to check private key in
  5314. *
  5315. * Returns WOLFSSL_SUCCESS on good private key
  5316. * WOLFSSL_FAILURE if mismatched. */
  5317. int wolfSSL_CTX_check_private_key(const WOLFSSL_CTX* ctx)
  5318. {
  5319. if (ctx == NULL) {
  5320. return WOLFSSL_FAILURE;
  5321. }
  5322. #ifdef WOLFSSL_DUAL_ALG_CERTS
  5323. return check_cert_key(ctx->certificate, ctx->privateKey, ctx->altPrivateKey,
  5324. ctx->heap, ctx->privateKeyDevId, ctx->privateKeyLabel,
  5325. ctx->privateKeyId, ctx->altPrivateKeyDevId, ctx->altPrivateKeyLabel,
  5326. ctx->altPrivateKeyId);
  5327. #else
  5328. return check_cert_key(ctx->certificate, ctx->privateKey, NULL, ctx->heap,
  5329. ctx->privateKeyDevId, ctx->privateKeyLabel, ctx->privateKeyId,
  5330. INVALID_DEVID, 0, 0);
  5331. #endif
  5332. }
  5333. #endif /* !NO_CHECK_PRIVATE_KEY */
  5334. #ifdef OPENSSL_ALL
  5335. /**
  5336. * Return the private key of the WOLFSSL_CTX struct
  5337. * @return WOLFSSL_EVP_PKEY* The caller doesn *NOT*` free the returned object.
  5338. */
  5339. WOLFSSL_EVP_PKEY* wolfSSL_CTX_get0_privatekey(const WOLFSSL_CTX* ctx)
  5340. {
  5341. const unsigned char *key;
  5342. int type;
  5343. WOLFSSL_ENTER("wolfSSL_CTX_get0_privatekey");
  5344. if (ctx == NULL || ctx->privateKey == NULL ||
  5345. ctx->privateKey->buffer == NULL) {
  5346. WOLFSSL_MSG("Bad parameter or key not set");
  5347. return NULL;
  5348. }
  5349. switch (ctx->privateKeyType) {
  5350. #ifndef NO_RSA
  5351. case rsa_sa_algo:
  5352. type = EVP_PKEY_RSA;
  5353. break;
  5354. #endif
  5355. #ifdef HAVE_ECC
  5356. case ecc_dsa_sa_algo:
  5357. type = EVP_PKEY_EC;
  5358. break;
  5359. #endif
  5360. #ifdef WOLFSSL_SM2
  5361. case sm2_sa_algo:
  5362. type = EVP_PKEY_EC;
  5363. break;
  5364. #endif
  5365. default:
  5366. /* Other key types not supported either as ssl private keys
  5367. * or in the EVP layer */
  5368. WOLFSSL_MSG("Unsupported key type");
  5369. return NULL;
  5370. }
  5371. key = ctx->privateKey->buffer;
  5372. if (ctx->privateKeyPKey != NULL)
  5373. return ctx->privateKeyPKey;
  5374. else
  5375. return wolfSSL_d2i_PrivateKey(type,
  5376. (WOLFSSL_EVP_PKEY**)&ctx->privateKeyPKey, &key,
  5377. (long)ctx->privateKey->length);
  5378. }
  5379. #endif
  5380. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  5381. #if !defined(NO_RSA)
  5382. static int d2iTryRsaKey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
  5383. long memSz, int priv)
  5384. {
  5385. WOLFSSL_EVP_PKEY* pkey;
  5386. word32 keyIdx = 0;
  5387. int isRsaKey;
  5388. int ret = 1;
  5389. #ifndef WOLFSSL_SMALL_STACK
  5390. RsaKey rsa[1];
  5391. #else
  5392. RsaKey *rsa = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
  5393. if (rsa == NULL)
  5394. return 0;
  5395. #endif
  5396. XMEMSET(rsa, 0, sizeof(RsaKey));
  5397. if (wc_InitRsaKey(rsa, NULL) != 0) {
  5398. #ifdef WOLFSSL_SMALL_STACK
  5399. XFREE(rsa, NULL, DYNAMIC_TYPE_RSA);
  5400. #endif
  5401. return 0;
  5402. }
  5403. /* test if RSA key */
  5404. if (priv) {
  5405. isRsaKey =
  5406. (wc_RsaPrivateKeyDecode(mem, &keyIdx, rsa, (word32)memSz) == 0);
  5407. }
  5408. else {
  5409. isRsaKey =
  5410. (wc_RsaPublicKeyDecode(mem, &keyIdx, rsa, (word32)memSz) == 0);
  5411. }
  5412. wc_FreeRsaKey(rsa);
  5413. #ifdef WOLFSSL_SMALL_STACK
  5414. XFREE(rsa, NULL, DYNAMIC_TYPE_RSA);
  5415. #endif
  5416. if (!isRsaKey) {
  5417. return -1;
  5418. }
  5419. if (*out != NULL) {
  5420. pkey = *out;
  5421. }
  5422. else {
  5423. pkey = wolfSSL_EVP_PKEY_new();
  5424. if (pkey == NULL) {
  5425. WOLFSSL_MSG("RSA wolfSSL_EVP_PKEY_new error");
  5426. return 0;
  5427. }
  5428. }
  5429. pkey->pkey_sz = keyIdx;
  5430. pkey->pkey.ptr = (char*)XMALLOC(memSz, NULL,
  5431. priv ? DYNAMIC_TYPE_PRIVATE_KEY :
  5432. DYNAMIC_TYPE_PUBLIC_KEY);
  5433. if (pkey->pkey.ptr == NULL) {
  5434. ret = 0;
  5435. }
  5436. if (ret == 1) {
  5437. XMEMCPY(pkey->pkey.ptr, mem, keyIdx);
  5438. pkey->type = EVP_PKEY_RSA;
  5439. pkey->ownRsa = 1;
  5440. pkey->rsa = wolfssl_rsa_d2i(NULL, mem, memSz,
  5441. priv ? WOLFSSL_RSA_LOAD_PRIVATE : WOLFSSL_RSA_LOAD_PUBLIC);
  5442. if (pkey->rsa == NULL) {
  5443. ret = 0;
  5444. }
  5445. }
  5446. if (ret == 1) {
  5447. *out = pkey;
  5448. }
  5449. if ((ret == 0) && (*out == NULL)) {
  5450. wolfSSL_EVP_PKEY_free(pkey);
  5451. }
  5452. return ret;
  5453. }
  5454. #endif /* !NO_RSA */
  5455. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA)
  5456. static int d2iTryEccKey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
  5457. long memSz, int priv)
  5458. {
  5459. WOLFSSL_EVP_PKEY* pkey;
  5460. word32 keyIdx = 0;
  5461. int isEccKey;
  5462. int ret = 1;
  5463. #ifndef WOLFSSL_SMALL_STACK
  5464. ecc_key ecc[1];
  5465. #else
  5466. ecc_key *ecc = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL,
  5467. DYNAMIC_TYPE_ECC);
  5468. if (ecc == NULL)
  5469. return 0;
  5470. #endif
  5471. XMEMSET(ecc, 0, sizeof(ecc_key));
  5472. if (wc_ecc_init(ecc) != 0) {
  5473. #ifdef WOLFSSL_SMALL_STACK
  5474. XFREE(ecc, NULL, DYNAMIC_TYPE_ECC);
  5475. #endif
  5476. return 0;
  5477. }
  5478. if (priv) {
  5479. isEccKey =
  5480. (wc_EccPrivateKeyDecode(mem, &keyIdx, ecc, (word32)memSz) == 0);
  5481. }
  5482. else {
  5483. isEccKey =
  5484. (wc_EccPublicKeyDecode(mem, &keyIdx, ecc, (word32)memSz) == 0);
  5485. }
  5486. wc_ecc_free(ecc);
  5487. #ifdef WOLFSSL_SMALL_STACK
  5488. XFREE(ecc, NULL, DYNAMIC_TYPE_ECC);
  5489. #endif
  5490. if (!isEccKey) {
  5491. return -1;
  5492. }
  5493. if (*out != NULL) {
  5494. pkey = *out;
  5495. }
  5496. else {
  5497. pkey = wolfSSL_EVP_PKEY_new();
  5498. if (pkey == NULL) {
  5499. WOLFSSL_MSG("ECC wolfSSL_EVP_PKEY_new error");
  5500. return 0;
  5501. }
  5502. }
  5503. pkey->pkey_sz = keyIdx;
  5504. pkey->pkey.ptr = (char*)XMALLOC(keyIdx, NULL,
  5505. priv ? DYNAMIC_TYPE_PRIVATE_KEY :
  5506. DYNAMIC_TYPE_PUBLIC_KEY);
  5507. if (pkey->pkey.ptr == NULL) {
  5508. ret = 0;
  5509. }
  5510. if (ret == 1) {
  5511. XMEMCPY(pkey->pkey.ptr, mem, keyIdx);
  5512. pkey->type = EVP_PKEY_EC;
  5513. pkey->ownEcc = 1;
  5514. pkey->ecc = wolfSSL_EC_KEY_new();
  5515. if (pkey->ecc == NULL) {
  5516. ret = 0;
  5517. }
  5518. }
  5519. if ((ret == 1) && (wolfSSL_EC_KEY_LoadDer_ex(pkey->ecc,
  5520. (const unsigned char*)pkey->pkey.ptr,
  5521. pkey->pkey_sz, priv ? WOLFSSL_RSA_LOAD_PRIVATE
  5522. : WOLFSSL_RSA_LOAD_PUBLIC) != 1)) {
  5523. ret = 0;
  5524. }
  5525. if (ret == 1) {
  5526. *out = pkey;
  5527. }
  5528. if ((ret == 0) && (*out == NULL)) {
  5529. wolfSSL_EVP_PKEY_free(pkey);
  5530. }
  5531. return ret;
  5532. }
  5533. #endif /* HAVE_ECC && OPENSSL_EXTRA */
  5534. #if !defined(NO_DSA)
  5535. static int d2iTryDsaKey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
  5536. long memSz, int priv)
  5537. {
  5538. WOLFSSL_EVP_PKEY* pkey;
  5539. word32 keyIdx = 0;
  5540. int isDsaKey;
  5541. int ret = 1;
  5542. #ifndef WOLFSSL_SMALL_STACK
  5543. DsaKey dsa[1];
  5544. #else
  5545. DsaKey *dsa = (DsaKey*)XMALLOC(sizeof(DsaKey), NULL, DYNAMIC_TYPE_DSA);
  5546. if (dsa == NULL)
  5547. return 0;
  5548. #endif
  5549. XMEMSET(dsa, 0, sizeof(DsaKey));
  5550. if (wc_InitDsaKey(dsa) != 0) {
  5551. #ifdef WOLFSSL_SMALL_STACK
  5552. XFREE(dsa, NULL, DYNAMIC_TYPE_DSA);
  5553. #endif
  5554. return 0;
  5555. }
  5556. if (priv) {
  5557. isDsaKey =
  5558. (wc_DsaPrivateKeyDecode(mem, &keyIdx, dsa, (word32)memSz) == 0);
  5559. }
  5560. else {
  5561. isDsaKey =
  5562. (wc_DsaPublicKeyDecode(mem, &keyIdx, dsa, (word32)memSz) == 0);
  5563. }
  5564. wc_FreeDsaKey(dsa);
  5565. #ifdef WOLFSSL_SMALL_STACK
  5566. XFREE(dsa, NULL, DYNAMIC_TYPE_DSA);
  5567. #endif
  5568. /* test if DSA key */
  5569. if (!isDsaKey) {
  5570. return -1;
  5571. }
  5572. if (*out != NULL) {
  5573. pkey = *out;
  5574. }
  5575. else {
  5576. pkey = wolfSSL_EVP_PKEY_new();
  5577. if (pkey == NULL) {
  5578. WOLFSSL_MSG("DSA wolfSSL_EVP_PKEY_new error");
  5579. return 0;
  5580. }
  5581. }
  5582. pkey->pkey_sz = keyIdx;
  5583. pkey->pkey.ptr = (char*)XMALLOC(memSz, NULL,
  5584. priv ? DYNAMIC_TYPE_PRIVATE_KEY :
  5585. DYNAMIC_TYPE_PUBLIC_KEY);
  5586. if (pkey->pkey.ptr == NULL) {
  5587. ret = 0;
  5588. }
  5589. if (ret == 1) {
  5590. XMEMCPY(pkey->pkey.ptr, mem, keyIdx);
  5591. pkey->type = EVP_PKEY_DSA;
  5592. pkey->ownDsa = 1;
  5593. pkey->dsa = wolfSSL_DSA_new();
  5594. if (pkey->dsa == NULL) {
  5595. ret = 0;
  5596. }
  5597. }
  5598. if ((ret == 1) && (wolfSSL_DSA_LoadDer_ex(pkey->dsa,
  5599. (const unsigned char*)pkey->pkey.ptr,
  5600. pkey->pkey_sz, priv ? WOLFSSL_RSA_LOAD_PRIVATE
  5601. : WOLFSSL_RSA_LOAD_PUBLIC) != 1)) {
  5602. ret = 0;
  5603. }
  5604. if (ret == 1) {
  5605. *out = pkey;
  5606. }
  5607. if ((ret == 0) && (*out == NULL)) {
  5608. wolfSSL_EVP_PKEY_free(pkey);
  5609. }
  5610. return ret;
  5611. }
  5612. #endif /* NO_DSA */
  5613. #if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
  5614. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  5615. (HAVE_FIPS_VERSION > 2))
  5616. static int d2iTryDhKey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
  5617. long memSz, int priv)
  5618. {
  5619. WOLFSSL_EVP_PKEY* pkey;
  5620. int isDhKey;
  5621. word32 keyIdx = 0;
  5622. int ret = 1;
  5623. #ifndef WOLFSSL_SMALL_STACK
  5624. DhKey dh[1];
  5625. #else
  5626. DhKey *dh = (DhKey*)XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
  5627. if (dh == NULL)
  5628. return 0;
  5629. #endif
  5630. XMEMSET(dh, 0, sizeof(DhKey));
  5631. if (wc_InitDhKey(dh) != 0) {
  5632. #ifdef WOLFSSL_SMALL_STACK
  5633. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  5634. #endif
  5635. return 0;
  5636. }
  5637. isDhKey = (wc_DhKeyDecode(mem, &keyIdx, dh, (word32)memSz) == 0);
  5638. wc_FreeDhKey(dh);
  5639. #ifdef WOLFSSL_SMALL_STACK
  5640. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  5641. #endif
  5642. /* test if DH key */
  5643. if (!isDhKey) {
  5644. return -1;
  5645. }
  5646. if (*out != NULL) {
  5647. pkey = *out;
  5648. }
  5649. else {
  5650. pkey = wolfSSL_EVP_PKEY_new();
  5651. if (pkey == NULL) {
  5652. WOLFSSL_MSG("DH wolfSSL_EVP_PKEY_new error");
  5653. return 0;
  5654. }
  5655. }
  5656. pkey->pkey_sz = (int)memSz;
  5657. pkey->pkey.ptr = (char*)XMALLOC(memSz, NULL,
  5658. priv ? DYNAMIC_TYPE_PRIVATE_KEY :
  5659. DYNAMIC_TYPE_PUBLIC_KEY);
  5660. if (pkey->pkey.ptr == NULL) {
  5661. ret = 0;
  5662. }
  5663. if (ret == 1) {
  5664. XMEMCPY(pkey->pkey.ptr, mem, memSz);
  5665. pkey->type = EVP_PKEY_DH;
  5666. pkey->ownDh = 1;
  5667. pkey->dh = wolfSSL_DH_new();
  5668. if (pkey->dh == NULL) {
  5669. ret = 0;
  5670. }
  5671. }
  5672. if ((ret == 1) && (wolfSSL_DH_LoadDer(pkey->dh,
  5673. (const unsigned char*)pkey->pkey.ptr,
  5674. pkey->pkey_sz) != WOLFSSL_SUCCESS)) {
  5675. ret = 0;
  5676. }
  5677. if (ret == 1) {
  5678. *out = pkey;
  5679. }
  5680. if ((ret == 0) && (*out == NULL)) {
  5681. wolfSSL_EVP_PKEY_free(pkey);
  5682. }
  5683. return ret;
  5684. }
  5685. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  5686. #endif /* !NO_DH && (WOLFSSL_QT || OPENSSL_ALL) */
  5687. #if !defined(NO_DH) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  5688. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  5689. (HAVE_FIPS_VERSION > 2))
  5690. static int d2iTryAltDhKey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
  5691. long memSz, int priv)
  5692. {
  5693. WOLFSSL_EVP_PKEY* pkey;
  5694. word32 keyIdx = 0;
  5695. DhKey* key = NULL;
  5696. int elements;
  5697. int ret;
  5698. #ifndef WOLFSSL_SMALL_STACK
  5699. DhKey dh[1];
  5700. #else
  5701. DhKey* dh = (DhKey*)XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
  5702. if (dh == NULL)
  5703. return 0;
  5704. #endif
  5705. XMEMSET(dh, 0, sizeof(DhKey));
  5706. /* test if DH-public key */
  5707. if (wc_InitDhKey(dh) != 0) {
  5708. #ifdef WOLFSSL_SMALL_STACK
  5709. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  5710. #endif
  5711. return 0;
  5712. }
  5713. ret = wc_DhKeyDecode(mem, &keyIdx, dh, (word32)memSz);
  5714. wc_FreeDhKey(dh);
  5715. #ifdef WOLFSSL_SMALL_STACK
  5716. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  5717. #endif
  5718. if (ret != 0) {
  5719. return -1;
  5720. }
  5721. if (*out != NULL) {
  5722. pkey = *out;
  5723. }
  5724. else {
  5725. pkey = wolfSSL_EVP_PKEY_new();
  5726. if (pkey == NULL) {
  5727. return 0;
  5728. }
  5729. }
  5730. ret = 1;
  5731. pkey->type = EVP_PKEY_DH;
  5732. pkey->pkey_sz = (int)memSz;
  5733. pkey->pkey.ptr = (char*)XMALLOC(memSz, NULL,
  5734. priv ? DYNAMIC_TYPE_PRIVATE_KEY :
  5735. DYNAMIC_TYPE_PUBLIC_KEY);
  5736. if (pkey->pkey.ptr == NULL) {
  5737. ret = 0;
  5738. }
  5739. if (ret == 1) {
  5740. XMEMCPY(pkey->pkey.ptr, mem, memSz);
  5741. pkey->ownDh = 1;
  5742. pkey->dh = wolfSSL_DH_new();
  5743. if (pkey->dh == NULL) {
  5744. ret = 0;
  5745. }
  5746. }
  5747. if (ret == 1) {
  5748. key = (DhKey*)pkey->dh->internal;
  5749. keyIdx = 0;
  5750. if (wc_DhKeyDecode(mem, &keyIdx, key, (word32)memSz) != 0) {
  5751. ret = 0;
  5752. }
  5753. }
  5754. if (ret == 1) {
  5755. elements = ELEMENT_P | ELEMENT_G | ELEMENT_Q | ELEMENT_PUB;
  5756. if (priv) {
  5757. elements |= ELEMENT_PRV;
  5758. }
  5759. if (SetDhExternal_ex(pkey->dh, elements) != WOLFSSL_SUCCESS ) {
  5760. ret = 0;
  5761. }
  5762. }
  5763. if (ret == 1) {
  5764. *out = pkey;
  5765. }
  5766. if ((ret == 0) && (*out == NULL)) {
  5767. wolfSSL_EVP_PKEY_free(pkey);
  5768. }
  5769. return ret;
  5770. }
  5771. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  5772. #endif /* !NO_DH && OPENSSL_EXTRA && WOLFSSL_DH_EXTRA */
  5773. #ifdef HAVE_PQC
  5774. #ifdef HAVE_FALCON
  5775. static int d2iTryFalconKey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
  5776. long memSz, int priv)
  5777. {
  5778. WOLFSSL_EVP_PKEY* pkey;
  5779. int isFalcon = 0;
  5780. #ifndef WOLFSSL_SMALL_STACK
  5781. falcon_key falcon[1];
  5782. #else
  5783. falcon_key *falcon = (falcon_key *)XMALLOC(sizeof(falcon_key), NULL,
  5784. DYNAMIC_TYPE_FALCON);
  5785. if (falcon == NULL) {
  5786. return 0;
  5787. }
  5788. #endif
  5789. if (wc_falcon_init(falcon) != 0) {
  5790. #ifdef WOLFSSL_SMALL_STACK
  5791. XFREE(falcon, NULL, DYNAMIC_TYPE_FALCON);
  5792. #endif
  5793. return 0;
  5794. }
  5795. /* test if Falcon key */
  5796. if (priv) {
  5797. /* Try level 1 */
  5798. isFalcon = ((wc_falcon_set_level(falcon, 1) == 0) &&
  5799. (wc_falcon_import_private_only(mem, (word32)memSz,
  5800. falcon) == 0));
  5801. if (!isFalcon) {
  5802. /* Try level 5 */
  5803. isFalcon = ((wc_falcon_set_level(falcon, 5) == 0) &&
  5804. (wc_falcon_import_private_only(mem, (word32)memSz,
  5805. falcon) == 0));
  5806. }
  5807. }
  5808. else {
  5809. /* Try level 1 */
  5810. isFalcon = ((wc_falcon_set_level(falcon, 1) == 0) &&
  5811. (wc_falcon_import_public(mem, (word32)memSz, falcon) == 0));
  5812. if (!isFalcon) {
  5813. /* Try level 5 */
  5814. isFalcon = ((wc_falcon_set_level(falcon, 5) == 0) &&
  5815. (wc_falcon_import_public(mem, (word32)memSz,
  5816. falcon) == 0));
  5817. }
  5818. }
  5819. wc_falcon_free(falcon);
  5820. #ifdef WOLFSSL_SMALL_STACK
  5821. XFREE(falcon, NULL, DYNAMIC_TYPE_FALCON);
  5822. #endif
  5823. if (!isFalcon) {
  5824. return -1;
  5825. }
  5826. if (*out != NULL) {
  5827. pkey = *out;
  5828. }
  5829. else {
  5830. /* Create a fake Falcon EVP_PKEY. In the future, we might integrate
  5831. * Falcon into the compatibility layer. */
  5832. pkey = wolfSSL_EVP_PKEY_new();
  5833. if (pkey == NULL) {
  5834. WOLFSSL_MSG("Falcon wolfSSL_EVP_PKEY_new error");
  5835. return 0;
  5836. }
  5837. }
  5838. pkey->type = EVP_PKEY_FALCON;
  5839. pkey->pkey.ptr = NULL;
  5840. pkey->pkey_sz = 0;
  5841. *out = pkey;
  5842. return 1;
  5843. }
  5844. #endif /* HAVE_FALCON */
  5845. #ifdef HAVE_DILITHIUM
  5846. static int d2iTryDilithiumKey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
  5847. long memSz, int priv)
  5848. {
  5849. WOLFSSL_EVP_PKEY* pkey;
  5850. int isDilithium = 0;
  5851. #ifndef WOLFSSL_SMALL_STACK
  5852. dilithium_key dilithium[1];
  5853. #else
  5854. dilithium_key *dilithium = (dilithium_key *)
  5855. XMALLOC(sizeof(dilithium_key), NULL, DYNAMIC_TYPE_DILITHIUM);
  5856. if (dilithium == NULL) {
  5857. return 0;
  5858. }
  5859. #endif
  5860. if (wc_dilithium_init(dilithium) != 0) {
  5861. #ifdef WOLFSSL_SMALL_STACK
  5862. XFREE(dilithium, NULL, DYNAMIC_TYPE_DILITHIUM);
  5863. #endif
  5864. return 0;
  5865. }
  5866. /* Test if Dilithium key. Try all levels. */
  5867. if (priv) {
  5868. isDilithium = ((wc_dilithium_set_level(dilithium, 2) == 0) &&
  5869. (wc_dilithium_import_private_only(mem,
  5870. (word32)memSz, dilithium) == 0));
  5871. if (!isDilithium) {
  5872. isDilithium = ((wc_dilithium_set_level(dilithium, 3) == 0) &&
  5873. (wc_dilithium_import_private_only(mem,
  5874. (word32)memSz, dilithium) == 0));
  5875. }
  5876. if (!isDilithium) {
  5877. isDilithium = ((wc_dilithium_set_level(dilithium, 5) == 0) &&
  5878. (wc_dilithium_import_private_only(mem,
  5879. (word32)memSz, dilithium) == 0));
  5880. }
  5881. }
  5882. else {
  5883. isDilithium = ((wc_dilithium_set_level(dilithium, 2) == 0) &&
  5884. (wc_dilithium_import_public(mem, (word32)memSz,
  5885. dilithium) == 0));
  5886. if (!isDilithium) {
  5887. isDilithium = ((wc_dilithium_set_level(dilithium, 3) == 0) &&
  5888. (wc_dilithium_import_public(mem, (word32)memSz,
  5889. dilithium) == 0));
  5890. }
  5891. if (!isDilithium) {
  5892. isDilithium = ((wc_dilithium_set_level(dilithium, 5) == 0) &&
  5893. (wc_dilithium_import_public(mem, (word32)memSz,
  5894. dilithium) == 0));
  5895. }
  5896. }
  5897. wc_dilithium_free(dilithium);
  5898. #ifdef WOLFSSL_SMALL_STACK
  5899. XFREE(dilithium, NULL, DYNAMIC_TYPE_DILITHIUM);
  5900. #endif
  5901. if (!isDilithium) {
  5902. return -1;
  5903. }
  5904. if (*out != NULL) {
  5905. pkey = *out;
  5906. }
  5907. else {
  5908. /* Create a fake Dilithium EVP_PKEY. In the future, we might
  5909. * integrate Dilithium into the compatibility layer. */
  5910. pkey = wolfSSL_EVP_PKEY_new();
  5911. if (pkey == NULL) {
  5912. WOLFSSL_MSG("Dilithium wolfSSL_EVP_PKEY_new error");
  5913. return 0;
  5914. }
  5915. }
  5916. pkey->type = EVP_PKEY_DILITHIUM;
  5917. pkey->pkey.ptr = NULL;
  5918. pkey->pkey_sz = 0;
  5919. *out = pkey;
  5920. return 1;
  5921. }
  5922. #endif /* HAVE_DILITHIUM */
  5923. #endif /* HAVE_PQC */
  5924. static WOLFSSL_EVP_PKEY* d2iGenericKey(WOLFSSL_EVP_PKEY** out,
  5925. const unsigned char** in, long inSz, int priv)
  5926. {
  5927. WOLFSSL_EVP_PKEY* pkey = NULL;
  5928. WOLFSSL_ENTER("d2iGenericKey");
  5929. if (in == NULL || *in == NULL || inSz < 0) {
  5930. WOLFSSL_MSG("Bad argument");
  5931. return NULL;
  5932. }
  5933. if ((out != NULL) && (*out != NULL)) {
  5934. pkey = *out;
  5935. }
  5936. #if !defined(NO_RSA)
  5937. if (d2iTryRsaKey(&pkey, *in, inSz, priv) >= 0) {
  5938. ;
  5939. }
  5940. else
  5941. #endif /* NO_RSA */
  5942. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA)
  5943. if (d2iTryEccKey(&pkey, *in, inSz, priv) >= 0) {
  5944. ;
  5945. }
  5946. else
  5947. #endif /* HAVE_ECC && OPENSSL_EXTRA */
  5948. #if !defined(NO_DSA)
  5949. if (d2iTryDsaKey(&pkey, *in, inSz, priv) >= 0) {
  5950. ;
  5951. }
  5952. else
  5953. #endif /* NO_DSA */
  5954. #if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
  5955. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  5956. (HAVE_FIPS_VERSION > 2))
  5957. if (d2iTryDhKey(&pkey, *in, inSz, priv) >= 0) {
  5958. ;
  5959. }
  5960. else
  5961. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  5962. #endif /* !NO_DH && (WOLFSSL_QT || OPENSSL_ALL) */
  5963. #if !defined(NO_DH) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  5964. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  5965. (HAVE_FIPS_VERSION > 2))
  5966. if (d2iTryAltDhKey(&pkey, *in, inSz, priv) >= 0) {
  5967. ;
  5968. }
  5969. else
  5970. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  5971. #endif /* !NO_DH && OPENSSL_EXTRA && WOLFSSL_DH_EXTRA */
  5972. #ifdef HAVE_PQC
  5973. #ifdef HAVE_FALCON
  5974. if (d2iTryFalconKey(&pkey, *in, inSz, priv) >= 0) {
  5975. ;
  5976. }
  5977. else
  5978. #endif /* HAVE_FALCON */
  5979. #ifdef HAVE_DILITHIUM
  5980. if (d2iTryDilithiumKey(&pkey, *in, inSz, priv) >= 0) {
  5981. ;
  5982. }
  5983. else
  5984. #endif /* HAVE_DILITHIUM */
  5985. #endif /* HAVE_PQC */
  5986. {
  5987. WOLFSSL_MSG("wolfSSL_d2i_PUBKEY couldn't determine key type");
  5988. }
  5989. if ((pkey != NULL) && (out != NULL)) {
  5990. *out = pkey;
  5991. }
  5992. return pkey;
  5993. }
  5994. #endif /* OPENSSL_EXTRA || WPA_SMALL */
  5995. #ifdef OPENSSL_EXTRA
  5996. WOLFSSL_PKCS8_PRIV_KEY_INFO* wolfSSL_d2i_PKCS8_PKEY(
  5997. WOLFSSL_PKCS8_PRIV_KEY_INFO** pkey, const unsigned char** keyBuf,
  5998. long keyLen)
  5999. {
  6000. WOLFSSL_PKCS8_PRIV_KEY_INFO* pkcs8 = NULL;
  6001. #ifdef WOLFSSL_PEM_TO_DER
  6002. int ret;
  6003. DerBuffer* der = NULL;
  6004. if (keyBuf == NULL || *keyBuf == NULL || keyLen <= 0) {
  6005. WOLFSSL_MSG("Bad key PEM/DER args");
  6006. return NULL;
  6007. }
  6008. ret = PemToDer(*keyBuf, keyLen, PRIVATEKEY_TYPE, &der, NULL, NULL, NULL);
  6009. if (ret < 0) {
  6010. WOLFSSL_MSG("Not PEM format");
  6011. ret = AllocDer(&der, (word32)keyLen, PRIVATEKEY_TYPE, NULL);
  6012. if (ret == 0) {
  6013. XMEMCPY(der->buffer, *keyBuf, keyLen);
  6014. }
  6015. }
  6016. if (ret == 0) {
  6017. /* Verify this is PKCS8 Key */
  6018. word32 inOutIdx = 0;
  6019. word32 algId;
  6020. ret = ToTraditionalInline_ex(der->buffer, &inOutIdx, der->length,
  6021. &algId);
  6022. if (ret >= 0) {
  6023. ret = 0; /* good DER */
  6024. }
  6025. }
  6026. if (ret == 0) {
  6027. pkcs8 = wolfSSL_EVP_PKEY_new();
  6028. if (pkcs8 == NULL)
  6029. ret = MEMORY_E;
  6030. }
  6031. if (ret == 0) {
  6032. pkcs8->pkey.ptr = (char*)XMALLOC(der->length, NULL,
  6033. DYNAMIC_TYPE_PUBLIC_KEY);
  6034. if (pkcs8->pkey.ptr == NULL)
  6035. ret = MEMORY_E;
  6036. }
  6037. if (ret == 0) {
  6038. XMEMCPY(pkcs8->pkey.ptr, der->buffer, der->length);
  6039. pkcs8->pkey_sz = der->length;
  6040. }
  6041. FreeDer(&der);
  6042. if (ret != 0) {
  6043. wolfSSL_EVP_PKEY_free(pkcs8);
  6044. pkcs8 = NULL;
  6045. }
  6046. if (pkey != NULL) {
  6047. *pkey = pkcs8;
  6048. }
  6049. #else
  6050. (void)bio;
  6051. (void)pkey;
  6052. #endif /* WOLFSSL_PEM_TO_DER */
  6053. return pkcs8;
  6054. }
  6055. #ifndef NO_BIO
  6056. /* put SSL type in extra for now, not very common */
  6057. /* Converts a DER format key read from "bio" to a PKCS8 structure.
  6058. *
  6059. * bio input bio to read DER from
  6060. * pkey If not NULL then this pointer will be overwritten with a new PKCS8
  6061. * structure.
  6062. *
  6063. * returns a WOLFSSL_PKCS8_PRIV_KEY_INFO pointer on success and NULL in fail
  6064. * case.
  6065. */
  6066. WOLFSSL_PKCS8_PRIV_KEY_INFO* wolfSSL_d2i_PKCS8_PKEY_bio(WOLFSSL_BIO* bio,
  6067. WOLFSSL_PKCS8_PRIV_KEY_INFO** pkey)
  6068. {
  6069. WOLFSSL_PKCS8_PRIV_KEY_INFO* pkcs8 = NULL;
  6070. #ifdef WOLFSSL_PEM_TO_DER
  6071. unsigned char* mem = NULL;
  6072. int memSz;
  6073. WOLFSSL_ENTER("wolfSSL_d2i_PKCS8_PKEY_bio");
  6074. if (bio == NULL) {
  6075. return NULL;
  6076. }
  6077. if ((memSz = wolfSSL_BIO_get_mem_data(bio, &mem)) < 0) {
  6078. return NULL;
  6079. }
  6080. pkcs8 = wolfSSL_d2i_PKCS8_PKEY(pkey, (const unsigned char**)&mem, memSz);
  6081. #else
  6082. (void)bio;
  6083. (void)pkey;
  6084. #endif /* WOLFSSL_PEM_TO_DER */
  6085. return pkcs8;
  6086. }
  6087. /* expecting DER format public key
  6088. *
  6089. * bio input bio to read DER from
  6090. * out If not NULL then this pointer will be overwritten with a new
  6091. * WOLFSSL_EVP_PKEY pointer
  6092. *
  6093. * returns a WOLFSSL_EVP_PKEY pointer on success and NULL in fail case.
  6094. */
  6095. WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY_bio(WOLFSSL_BIO* bio,
  6096. WOLFSSL_EVP_PKEY** out)
  6097. {
  6098. unsigned char* mem;
  6099. long memSz;
  6100. WOLFSSL_EVP_PKEY* pkey = NULL;
  6101. WOLFSSL_ENTER("wolfSSL_d2i_PUBKEY_bio");
  6102. if (bio == NULL) {
  6103. return NULL;
  6104. }
  6105. (void)out;
  6106. memSz = wolfSSL_BIO_get_len(bio);
  6107. if (memSz <= 0) {
  6108. return NULL;
  6109. }
  6110. mem = (unsigned char*)XMALLOC(memSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  6111. if (mem == NULL) {
  6112. return NULL;
  6113. }
  6114. if (wolfSSL_BIO_read(bio, mem, (int)memSz) == memSz) {
  6115. pkey = wolfSSL_d2i_PUBKEY(NULL, (const unsigned char**)&mem, memSz);
  6116. if (out != NULL && pkey != NULL) {
  6117. *out = pkey;
  6118. }
  6119. }
  6120. XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  6121. return pkey;
  6122. }
  6123. #endif /* !NO_BIO */
  6124. /* Converts a DER encoded public key to a WOLFSSL_EVP_PKEY structure.
  6125. *
  6126. * out pointer to new WOLFSSL_EVP_PKEY structure. Can be NULL
  6127. * in DER buffer to convert
  6128. * inSz size of in buffer
  6129. *
  6130. * returns a pointer to a new WOLFSSL_EVP_PKEY structure on success and NULL
  6131. * on fail
  6132. */
  6133. WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY(WOLFSSL_EVP_PKEY** out,
  6134. const unsigned char** in, long inSz)
  6135. {
  6136. WOLFSSL_ENTER("wolfSSL_d2i_PUBKEY");
  6137. return d2iGenericKey(out, in, inSz, 0);
  6138. }
  6139. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN) && \
  6140. !defined(NO_PWDBASED)
  6141. /* helper function to get raw pointer to DER buffer from WOLFSSL_EVP_PKEY */
  6142. static int wolfSSL_EVP_PKEY_get_der(const WOLFSSL_EVP_PKEY* key,
  6143. unsigned char** der)
  6144. {
  6145. int sz;
  6146. word16 pkcs8HeaderSz;
  6147. if (!key || !key->pkey_sz)
  6148. return WOLFSSL_FATAL_ERROR;
  6149. /* return the key without PKCS8 for compatibility */
  6150. /* if pkcs8HeaderSz is invalid, use 0 and return all of pkey */
  6151. pkcs8HeaderSz = 0;
  6152. if (key->pkey_sz > key->pkcs8HeaderSz)
  6153. pkcs8HeaderSz = key->pkcs8HeaderSz;
  6154. sz = key->pkey_sz - pkcs8HeaderSz;
  6155. if (der) {
  6156. unsigned char* pt = (unsigned char*)key->pkey.ptr;
  6157. if (*der) {
  6158. /* since this function signature has no size value passed in it is
  6159. * assumed that the user has allocated a large enough buffer */
  6160. XMEMCPY(*der, pt + pkcs8HeaderSz, sz);
  6161. *der += sz;
  6162. }
  6163. else {
  6164. *der = (unsigned char*)XMALLOC(sz, NULL, DYNAMIC_TYPE_OPENSSL);
  6165. if (*der == NULL) {
  6166. return WOLFSSL_FATAL_ERROR;
  6167. }
  6168. XMEMCPY(*der, pt + pkcs8HeaderSz, sz);
  6169. }
  6170. }
  6171. return sz;
  6172. }
  6173. int wolfSSL_i2d_PUBKEY(const WOLFSSL_EVP_PKEY *key, unsigned char **der)
  6174. {
  6175. return wolfSSL_i2d_PublicKey(key, der);
  6176. }
  6177. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_ASN && !NO_PWDBASED */
  6178. static WOLFSSL_EVP_PKEY* _d2i_PublicKey(int type, WOLFSSL_EVP_PKEY** out,
  6179. const unsigned char **in, long inSz, int priv)
  6180. {
  6181. int ret = 0;
  6182. word32 idx = 0, algId;
  6183. word16 pkcs8HeaderSz = 0;
  6184. WOLFSSL_EVP_PKEY* local;
  6185. int opt = 0;
  6186. (void)opt;
  6187. if (in == NULL || inSz < 0) {
  6188. WOLFSSL_MSG("Bad argument");
  6189. return NULL;
  6190. }
  6191. if (priv == 1) {
  6192. /* Check if input buffer has PKCS8 header. In the case that it does not
  6193. * have a PKCS8 header then do not error out. */
  6194. if ((ret = ToTraditionalInline_ex((const byte*)(*in), &idx,
  6195. (word32)inSz, &algId)) > 0) {
  6196. WOLFSSL_MSG("Found PKCS8 header");
  6197. pkcs8HeaderSz = (word16)idx;
  6198. if ((type == EVP_PKEY_RSA && algId != RSAk
  6199. #ifdef WC_RSA_PSS
  6200. && algId != RSAPSSk
  6201. #endif
  6202. ) ||
  6203. (type == EVP_PKEY_EC && algId != ECDSAk) ||
  6204. (type == EVP_PKEY_DSA && algId != DSAk) ||
  6205. (type == EVP_PKEY_DH && algId != DHk)) {
  6206. WOLFSSL_MSG("PKCS8 does not match EVP key type");
  6207. return NULL;
  6208. }
  6209. (void)idx; /* not used */
  6210. }
  6211. else {
  6212. if (ret != ASN_PARSE_E) {
  6213. WOLFSSL_MSG("Unexpected error with trying to remove PKCS8 "
  6214. "header");
  6215. return NULL;
  6216. }
  6217. }
  6218. }
  6219. if (out != NULL && *out != NULL) {
  6220. wolfSSL_EVP_PKEY_free(*out);
  6221. *out = NULL;
  6222. }
  6223. local = wolfSSL_EVP_PKEY_new();
  6224. if (local == NULL) {
  6225. return NULL;
  6226. }
  6227. local->type = type;
  6228. local->pkey_sz = (int)inSz;
  6229. local->pkcs8HeaderSz = pkcs8HeaderSz;
  6230. local->pkey.ptr = (char*)XMALLOC(inSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  6231. if (local->pkey.ptr == NULL) {
  6232. wolfSSL_EVP_PKEY_free(local);
  6233. local = NULL;
  6234. return NULL;
  6235. }
  6236. else {
  6237. XMEMCPY(local->pkey.ptr, *in, inSz);
  6238. }
  6239. switch (type) {
  6240. #ifndef NO_RSA
  6241. case EVP_PKEY_RSA:
  6242. opt = priv ? WOLFSSL_RSA_LOAD_PRIVATE : WOLFSSL_RSA_LOAD_PUBLIC;
  6243. local->ownRsa = 1;
  6244. local->rsa = wolfssl_rsa_d2i(NULL,
  6245. (const unsigned char*)local->pkey.ptr, local->pkey_sz, opt);
  6246. if (local->rsa == NULL) {
  6247. wolfSSL_EVP_PKEY_free(local);
  6248. return NULL;
  6249. }
  6250. break;
  6251. #endif /* NO_RSA */
  6252. #ifdef HAVE_ECC
  6253. case EVP_PKEY_EC:
  6254. local->ownEcc = 1;
  6255. local->ecc = wolfSSL_EC_KEY_new();
  6256. if (local->ecc == NULL) {
  6257. wolfSSL_EVP_PKEY_free(local);
  6258. return NULL;
  6259. }
  6260. opt = priv ? WOLFSSL_EC_KEY_LOAD_PRIVATE :
  6261. WOLFSSL_EC_KEY_LOAD_PUBLIC;
  6262. if (wolfSSL_EC_KEY_LoadDer_ex(local->ecc,
  6263. (const unsigned char*)local->pkey.ptr, local->pkey_sz,
  6264. opt)
  6265. != WOLFSSL_SUCCESS) {
  6266. wolfSSL_EVP_PKEY_free(local);
  6267. return NULL;
  6268. }
  6269. break;
  6270. #endif /* HAVE_ECC */
  6271. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  6272. #ifndef NO_DSA
  6273. case EVP_PKEY_DSA:
  6274. local->ownDsa = 1;
  6275. local->dsa = wolfSSL_DSA_new();
  6276. if (local->dsa == NULL) {
  6277. wolfSSL_EVP_PKEY_free(local);
  6278. return NULL;
  6279. }
  6280. opt = priv ? WOLFSSL_DSA_LOAD_PRIVATE : WOLFSSL_DSA_LOAD_PUBLIC;
  6281. if (wolfSSL_DSA_LoadDer_ex(local->dsa,
  6282. (const unsigned char*)local->pkey.ptr, local->pkey_sz,
  6283. opt)
  6284. != WOLFSSL_SUCCESS) {
  6285. wolfSSL_EVP_PKEY_free(local);
  6286. return NULL;
  6287. }
  6288. break;
  6289. #endif /* NO_DSA */
  6290. #ifndef NO_DH
  6291. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  6292. case EVP_PKEY_DH:
  6293. local->ownDh = 1;
  6294. local->dh = wolfSSL_DH_new();
  6295. if (local->dh == NULL) {
  6296. wolfSSL_EVP_PKEY_free(local);
  6297. return NULL;
  6298. }
  6299. if (wolfSSL_DH_LoadDer(local->dh,
  6300. (const unsigned char*)local->pkey.ptr, local->pkey_sz)
  6301. != WOLFSSL_SUCCESS) {
  6302. wolfSSL_EVP_PKEY_free(local);
  6303. return NULL;
  6304. }
  6305. break;
  6306. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  6307. #endif /* HAVE_DH */
  6308. #endif /* WOLFSSL_QT || OPENSSL_ALL || WOLFSSL_OPENSSH */
  6309. default:
  6310. WOLFSSL_MSG("Unsupported key type");
  6311. wolfSSL_EVP_PKEY_free(local);
  6312. return NULL;
  6313. }
  6314. /* advance pointer with success */
  6315. if (local != NULL) {
  6316. if (local->pkey_sz <= (int)inSz) {
  6317. *in += local->pkey_sz;
  6318. }
  6319. if (out != NULL) {
  6320. *out = local;
  6321. }
  6322. }
  6323. return local;
  6324. }
  6325. WOLFSSL_EVP_PKEY* wolfSSL_d2i_PublicKey(int type, WOLFSSL_EVP_PKEY** out,
  6326. const unsigned char **in, long inSz)
  6327. {
  6328. WOLFSSL_ENTER("wolfSSL_d2i_PublicKey");
  6329. return _d2i_PublicKey(type, out, in, inSz, 0);
  6330. }
  6331. /* Reads in a DER format key. If PKCS8 headers are found they are stripped off.
  6332. *
  6333. * type type of key
  6334. * out newly created WOLFSSL_EVP_PKEY structure
  6335. * in pointer to input key DER
  6336. * inSz size of in buffer
  6337. *
  6338. * On success a non null pointer is returned and the pointer in is advanced the
  6339. * same number of bytes read.
  6340. */
  6341. WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey(int type, WOLFSSL_EVP_PKEY** out,
  6342. const unsigned char **in, long inSz)
  6343. {
  6344. WOLFSSL_ENTER("wolfSSL_d2i_PrivateKey");
  6345. return _d2i_PublicKey(type, out, in, inSz, 1);
  6346. }
  6347. #ifdef WOLF_PRIVATE_KEY_ID
  6348. /* Create an EVP structure for use with crypto callbacks */
  6349. WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_id(int type, WOLFSSL_EVP_PKEY** out,
  6350. void* heap, int devId)
  6351. {
  6352. WOLFSSL_EVP_PKEY* local;
  6353. if (out != NULL && *out != NULL) {
  6354. wolfSSL_EVP_PKEY_free(*out);
  6355. *out = NULL;
  6356. }
  6357. local = wolfSSL_EVP_PKEY_new_ex(heap);
  6358. if (local == NULL) {
  6359. return NULL;
  6360. }
  6361. local->type = type;
  6362. local->pkey_sz = 0;
  6363. local->pkcs8HeaderSz = 0;
  6364. switch (type) {
  6365. #ifndef NO_RSA
  6366. case EVP_PKEY_RSA:
  6367. {
  6368. RsaKey* key;
  6369. local->ownRsa = 1;
  6370. local->rsa = wolfSSL_RSA_new_ex(heap, devId);
  6371. if (local->rsa == NULL) {
  6372. wolfSSL_EVP_PKEY_free(local);
  6373. return NULL;
  6374. }
  6375. key = (RsaKey*)local->rsa->internal;
  6376. #ifdef WOLF_CRYPTO_CB
  6377. key->devId = devId;
  6378. #endif
  6379. (void)key;
  6380. local->rsa->inSet = 1;
  6381. break;
  6382. }
  6383. #endif /* !NO_RSA */
  6384. #ifdef HAVE_ECC
  6385. case EVP_PKEY_EC:
  6386. {
  6387. ecc_key* key;
  6388. local->ownEcc = 1;
  6389. local->ecc = wolfSSL_EC_KEY_new_ex(heap, devId);
  6390. if (local->ecc == NULL) {
  6391. wolfSSL_EVP_PKEY_free(local);
  6392. return NULL;
  6393. }
  6394. key = (ecc_key*)local->ecc->internal;
  6395. #ifdef WOLF_CRYPTO_CB
  6396. key->devId = devId;
  6397. #endif
  6398. key->type = ECC_PRIVATEKEY;
  6399. /* key is required to have a key size / curve set, although
  6400. * actual one used is determined by devId callback function */
  6401. wc_ecc_set_curve(key, ECDHE_SIZE, ECC_CURVE_DEF);
  6402. local->ecc->inSet = 1;
  6403. break;
  6404. }
  6405. #endif /* HAVE_ECC */
  6406. default:
  6407. WOLFSSL_MSG("Unsupported private key id type");
  6408. wolfSSL_EVP_PKEY_free(local);
  6409. return NULL;
  6410. }
  6411. if (local != NULL && out != NULL) {
  6412. *out = local;
  6413. }
  6414. return local;
  6415. }
  6416. #endif /* WOLF_PRIVATE_KEY_ID */
  6417. #ifndef NO_CERTS /* // NOLINT(readability-redundant-preprocessor) */
  6418. #ifndef NO_CHECK_PRIVATE_KEY
  6419. /* Check private against public in certificate for match
  6420. *
  6421. * ssl WOLFSSL structure to check private key in
  6422. *
  6423. * Returns WOLFSSL_SUCCESS on good private key
  6424. * WOLFSSL_FAILURE if mismatched. */
  6425. int wolfSSL_check_private_key(const WOLFSSL* ssl)
  6426. {
  6427. if (ssl == NULL) {
  6428. return WOLFSSL_FAILURE;
  6429. }
  6430. #ifdef WOLFSSL_DUAL_ALG_CERTS
  6431. return check_cert_key(ssl->buffers.certificate, ssl->buffers.key,
  6432. ssl->buffers.altKey, ssl->heap, ssl->buffers.keyDevId,
  6433. ssl->buffers.keyLabel, ssl->buffers.keyId, ssl->buffers.altKeyDevId,
  6434. ssl->buffers.altKeyLabel, ssl->buffers.altKeyId);
  6435. #else
  6436. return check_cert_key(ssl->buffers.certificate, ssl->buffers.key, NULL,
  6437. ssl->heap, ssl->buffers.keyDevId, ssl->buffers.keyLabel,
  6438. ssl->buffers.keyId, INVALID_DEVID, 0, 0);
  6439. #endif
  6440. }
  6441. #endif /* !NO_CHECK_PRIVATE_KEY */
  6442. #endif /* !NO_CERTS */
  6443. #endif /* OPENSSL_EXTRA */
  6444. #if defined(HAVE_RPK)
  6445. /* Confirm that all the byte data in the buffer is unique.
  6446. * return 1 if all the byte data in the buffer is unique, otherwise 0.
  6447. */
  6448. static int isArrayUnique(const char* buf, size_t len)
  6449. {
  6450. size_t i, j;
  6451. /* check the array is unique */
  6452. for (i = 0; i < len -1; ++i) {
  6453. for (j = i+ 1; j < len; ++j) {
  6454. if (buf[i] == buf[j]) {
  6455. return 0;
  6456. }
  6457. }
  6458. }
  6459. return 1;
  6460. }
  6461. /* Set user preference for the client_cert_type exetnsion.
  6462. * Takes byte array containing cert types the caller can provide to its peer.
  6463. * Cert types are in preferred order in the array.
  6464. */
  6465. WOLFSSL_API int wolfSSL_CTX_set_client_cert_type(WOLFSSL_CTX* ctx,
  6466. const char* buf, int bufLen)
  6467. {
  6468. int i;
  6469. if (ctx == NULL || bufLen > MAX_CLIENT_CERT_TYPE_CNT) {
  6470. return BAD_FUNC_ARG;
  6471. }
  6472. /* if buf is set to NULL or bufLen is set to zero, it defaults the setting*/
  6473. if (buf == NULL || bufLen == 0) {
  6474. ctx->rpkConfig.preferred_ClientCertTypeCnt = 1;
  6475. ctx->rpkConfig.preferred_ClientCertTypes[0]= WOLFSSL_CERT_TYPE_X509;
  6476. ctx->rpkConfig.preferred_ClientCertTypes[1]= WOLFSSL_CERT_TYPE_X509;
  6477. return WOLFSSL_SUCCESS;
  6478. }
  6479. if (!isArrayUnique(buf, bufLen))
  6480. return BAD_FUNC_ARG;
  6481. for (i = 0; i < bufLen; i++){
  6482. if (buf[i] != WOLFSSL_CERT_TYPE_RPK && buf[i] != WOLFSSL_CERT_TYPE_X509)
  6483. return BAD_FUNC_ARG;
  6484. ctx->rpkConfig.preferred_ClientCertTypes[i] = buf[i];
  6485. }
  6486. ctx->rpkConfig.preferred_ClientCertTypeCnt = bufLen;
  6487. return WOLFSSL_SUCCESS;
  6488. }
  6489. /* Set user preference for the server_cert_type exetnsion.
  6490. * Takes byte array containing cert types the caller can provide to its peer.
  6491. * Cert types are in preferred order in the array.
  6492. */
  6493. WOLFSSL_API int wolfSSL_CTX_set_server_cert_type(WOLFSSL_CTX* ctx,
  6494. const char* buf, int bufLen)
  6495. {
  6496. int i;
  6497. if (ctx == NULL || bufLen > MAX_SERVER_CERT_TYPE_CNT) {
  6498. return BAD_FUNC_ARG;
  6499. }
  6500. /* if buf is set to NULL or bufLen is set to zero, it defaults the setting*/
  6501. if (buf == NULL || bufLen == 0) {
  6502. ctx->rpkConfig.preferred_ServerCertTypeCnt = 1;
  6503. ctx->rpkConfig.preferred_ServerCertTypes[0]= WOLFSSL_CERT_TYPE_X509;
  6504. ctx->rpkConfig.preferred_ServerCertTypes[1]= WOLFSSL_CERT_TYPE_X509;
  6505. return WOLFSSL_SUCCESS;
  6506. }
  6507. if (!isArrayUnique(buf, bufLen))
  6508. return BAD_FUNC_ARG;
  6509. for (i = 0; i < bufLen; i++){
  6510. if (buf[i] != WOLFSSL_CERT_TYPE_RPK && buf[i] != WOLFSSL_CERT_TYPE_X509)
  6511. return BAD_FUNC_ARG;
  6512. ctx->rpkConfig.preferred_ServerCertTypes[i] = buf[i];
  6513. }
  6514. ctx->rpkConfig.preferred_ServerCertTypeCnt = bufLen;
  6515. return WOLFSSL_SUCCESS;
  6516. }
  6517. /* Set user preference for the client_cert_type exetnsion.
  6518. * Takes byte array containing cert types the caller can provide to its peer.
  6519. * Cert types are in preferred order in the array.
  6520. */
  6521. WOLFSSL_API int wolfSSL_set_client_cert_type(WOLFSSL* ssl,
  6522. const char* buf, int bufLen)
  6523. {
  6524. int i;
  6525. if (ssl == NULL || bufLen > MAX_CLIENT_CERT_TYPE_CNT) {
  6526. return BAD_FUNC_ARG;
  6527. }
  6528. /* if buf is set to NULL or bufLen is set to zero, it defaults the setting*/
  6529. if (buf == NULL || bufLen == 0) {
  6530. ssl->options.rpkConfig.preferred_ClientCertTypeCnt = 1;
  6531. ssl->options.rpkConfig.preferred_ClientCertTypes[0]
  6532. = WOLFSSL_CERT_TYPE_X509;
  6533. ssl->options.rpkConfig.preferred_ClientCertTypes[1]
  6534. = WOLFSSL_CERT_TYPE_X509;
  6535. return WOLFSSL_SUCCESS;
  6536. }
  6537. if (!isArrayUnique(buf, bufLen))
  6538. return BAD_FUNC_ARG;
  6539. for (i = 0; i < bufLen; i++){
  6540. if (buf[i] != WOLFSSL_CERT_TYPE_RPK && buf[i] != WOLFSSL_CERT_TYPE_X509)
  6541. return BAD_FUNC_ARG;
  6542. ssl->options.rpkConfig.preferred_ClientCertTypes[i] = buf[i];
  6543. }
  6544. ssl->options.rpkConfig.preferred_ClientCertTypeCnt = bufLen;
  6545. return WOLFSSL_SUCCESS;
  6546. }
  6547. /* Set user preference for the server_cert_type exetnsion.
  6548. * Takes byte array containing cert types the caller can provide to its peer.
  6549. * Cert types are in preferred order in the array.
  6550. */
  6551. WOLFSSL_API int wolfSSL_set_server_cert_type(WOLFSSL* ssl,
  6552. const char* buf, int bufLen)
  6553. {
  6554. int i;
  6555. if (ssl == NULL || bufLen > MAX_SERVER_CERT_TYPE_CNT) {
  6556. return BAD_FUNC_ARG;
  6557. }
  6558. /* if buf is set to NULL or bufLen is set to zero, it defaults the setting*/
  6559. if (buf == NULL || bufLen == 0) {
  6560. ssl->options.rpkConfig.preferred_ServerCertTypeCnt = 1;
  6561. ssl->options.rpkConfig.preferred_ServerCertTypes[0]
  6562. = WOLFSSL_CERT_TYPE_X509;
  6563. ssl->options.rpkConfig.preferred_ServerCertTypes[1]
  6564. = WOLFSSL_CERT_TYPE_X509;
  6565. return WOLFSSL_SUCCESS;
  6566. }
  6567. if (!isArrayUnique(buf, bufLen))
  6568. return BAD_FUNC_ARG;
  6569. for (i = 0; i < bufLen; i++){
  6570. if (buf[i] != WOLFSSL_CERT_TYPE_RPK && buf[i] != WOLFSSL_CERT_TYPE_X509)
  6571. return BAD_FUNC_ARG;
  6572. ssl->options.rpkConfig.preferred_ServerCertTypes[i] = buf[i];
  6573. }
  6574. ssl->options.rpkConfig.preferred_ServerCertTypeCnt = bufLen;
  6575. return WOLFSSL_SUCCESS;
  6576. }
  6577. /* get negotiated certificate type value and return it to the second parameter.
  6578. * cert type value:
  6579. * -1: WOLFSSL_CERT_TYPE_UNKNOWN
  6580. * 0: WOLFSSL_CERT_TYPE_X509
  6581. * 2: WOLFSSL_CERT_TYPE_RPK
  6582. * return WOLFSSL_SUCCESS on success, otherwise negative value.
  6583. * in case no negotiation performed, it returns WOLFSSL_SUCCESS and -1 is for
  6584. * cert type.
  6585. */
  6586. WOLFSSL_API int wolfSSL_get_negotiated_client_cert_type(WOLFSSL* ssl, int* tp)
  6587. {
  6588. int ret = WOLFSSL_SUCCESS;
  6589. if (ssl == NULL || tp == NULL)
  6590. return BAD_FUNC_ARG;
  6591. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  6592. if (ssl->options.rpkState.received_ClientCertTypeCnt == 1)
  6593. *tp = ssl->options.rpkState.received_ClientCertTypes[0];
  6594. else
  6595. *tp = WOLFSSL_CERT_TYPE_UNKNOWN;
  6596. }
  6597. else {
  6598. if (ssl->options.rpkState.sending_ClientCertTypeCnt == 1)
  6599. *tp = ssl->options.rpkState.sending_ClientCertTypes[0];
  6600. else
  6601. *tp = WOLFSSL_CERT_TYPE_UNKNOWN;
  6602. }
  6603. return ret;
  6604. }
  6605. /* get negotiated certificate type value and return it to the second parameter.
  6606. * cert type value:
  6607. * -1: WOLFSSL_CERT_TYPE_UNKNOWN
  6608. * 0: WOLFSSL_CERT_TYPE_X509
  6609. * 2: WOLFSSL_CERT_TYPE_RPK
  6610. * return WOLFSSL_SUCCESS on success, otherwise negative value.
  6611. * in case no negotiation performed, it returns WOLFSSL_SUCCESS and -1 is for
  6612. * cert type.
  6613. */
  6614. WOLFSSL_API int wolfSSL_get_negotiated_server_cert_type(WOLFSSL* ssl, int* tp)
  6615. {
  6616. int ret = WOLFSSL_SUCCESS;
  6617. if (ssl == NULL || tp == NULL)
  6618. return BAD_FUNC_ARG;
  6619. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  6620. if (ssl->options.rpkState.received_ServerCertTypeCnt == 1)
  6621. *tp = ssl->options.rpkState.received_ServerCertTypes[0];
  6622. else
  6623. *tp = WOLFSSL_CERT_TYPE_UNKNOWN;
  6624. }
  6625. else {
  6626. if (ssl->options.rpkState.sending_ServerCertTypeCnt == 1)
  6627. *tp = ssl->options.rpkState.sending_ServerCertTypes[0];
  6628. else
  6629. *tp = WOLFSSL_CERT_TYPE_UNKNOWN;
  6630. }
  6631. return ret;
  6632. }
  6633. #endif /* HAVE_RPK */
  6634. #ifdef HAVE_ECC
  6635. /* Set Temp CTX EC-DHE size in octets, can be 14 - 66 (112 - 521 bit) */
  6636. int wolfSSL_CTX_SetTmpEC_DHE_Sz(WOLFSSL_CTX* ctx, word16 sz)
  6637. {
  6638. if (ctx == NULL)
  6639. return BAD_FUNC_ARG;
  6640. /* if 0 then get from loaded private key */
  6641. if (sz == 0) {
  6642. /* applies only to ECDSA */
  6643. if (ctx->privateKeyType != ecc_dsa_sa_algo)
  6644. return WOLFSSL_SUCCESS;
  6645. if (ctx->privateKeySz == 0) {
  6646. WOLFSSL_MSG("Must set private key/cert first");
  6647. return BAD_FUNC_ARG;
  6648. }
  6649. sz = (word16)ctx->privateKeySz;
  6650. }
  6651. /* check size */
  6652. #if ECC_MIN_KEY_SZ > 0
  6653. if (sz < ECC_MINSIZE)
  6654. return BAD_FUNC_ARG;
  6655. #endif
  6656. if (sz > ECC_MAXSIZE)
  6657. return BAD_FUNC_ARG;
  6658. ctx->eccTempKeySz = sz;
  6659. return WOLFSSL_SUCCESS;
  6660. }
  6661. /* Set Temp SSL EC-DHE size in octets, can be 14 - 66 (112 - 521 bit) */
  6662. int wolfSSL_SetTmpEC_DHE_Sz(WOLFSSL* ssl, word16 sz)
  6663. {
  6664. if (ssl == NULL)
  6665. return BAD_FUNC_ARG;
  6666. /* check size */
  6667. #if ECC_MIN_KEY_SZ > 0
  6668. if (sz < ECC_MINSIZE)
  6669. return BAD_FUNC_ARG;
  6670. #endif
  6671. if (sz > ECC_MAXSIZE)
  6672. return BAD_FUNC_ARG;
  6673. ssl->eccTempKeySz = sz;
  6674. return WOLFSSL_SUCCESS;
  6675. }
  6676. #endif /* HAVE_ECC */
  6677. typedef struct {
  6678. byte verifyPeer:1;
  6679. byte verifyNone:1;
  6680. byte failNoCert:1;
  6681. byte failNoCertxPSK:1;
  6682. byte verifyPostHandshake:1;
  6683. } SetVerifyOptions;
  6684. static SetVerifyOptions ModeToVerifyOptions(int mode)
  6685. {
  6686. SetVerifyOptions opts;
  6687. XMEMSET(&opts, 0, sizeof(SetVerifyOptions));
  6688. if (mode != WOLFSSL_VERIFY_DEFAULT) {
  6689. opts.verifyNone = (mode == WOLFSSL_VERIFY_NONE);
  6690. if (!opts.verifyNone) {
  6691. opts.verifyPeer =
  6692. (mode & WOLFSSL_VERIFY_PEER) != 0;
  6693. opts.failNoCertxPSK =
  6694. (mode & WOLFSSL_VERIFY_FAIL_EXCEPT_PSK) != 0;
  6695. opts.failNoCert =
  6696. (mode & WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT) != 0;
  6697. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  6698. opts.verifyPostHandshake =
  6699. (mode & WOLFSSL_VERIFY_POST_HANDSHAKE) != 0;
  6700. #endif
  6701. }
  6702. }
  6703. return opts;
  6704. }
  6705. WOLFSSL_ABI
  6706. void wolfSSL_CTX_set_verify(WOLFSSL_CTX* ctx, int mode, VerifyCallback vc)
  6707. {
  6708. SetVerifyOptions opts;
  6709. WOLFSSL_ENTER("wolfSSL_CTX_set_verify");
  6710. if (ctx == NULL)
  6711. return;
  6712. opts = ModeToVerifyOptions(mode);
  6713. ctx->verifyNone = opts.verifyNone;
  6714. ctx->verifyPeer = opts.verifyPeer;
  6715. ctx->failNoCert = opts.failNoCert;
  6716. ctx->failNoCertxPSK = opts.failNoCertxPSK;
  6717. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  6718. ctx->verifyPostHandshake = opts.verifyPostHandshake;
  6719. #endif
  6720. ctx->verifyCallback = vc;
  6721. }
  6722. #ifdef OPENSSL_ALL
  6723. void wolfSSL_CTX_set_cert_verify_callback(WOLFSSL_CTX* ctx,
  6724. CertVerifyCallback cb, void* arg)
  6725. {
  6726. WOLFSSL_ENTER("wolfSSL_CTX_set_cert_verify_callback");
  6727. if (ctx == NULL)
  6728. return;
  6729. ctx->verifyCertCb = cb;
  6730. ctx->verifyCertCbArg = arg;
  6731. }
  6732. #endif
  6733. void wolfSSL_set_verify(WOLFSSL* ssl, int mode, VerifyCallback vc)
  6734. {
  6735. SetVerifyOptions opts;
  6736. WOLFSSL_ENTER("wolfSSL_set_verify");
  6737. if (ssl == NULL)
  6738. return;
  6739. opts = ModeToVerifyOptions(mode);
  6740. ssl->options.verifyNone = opts.verifyNone;
  6741. ssl->options.verifyPeer = opts.verifyPeer;
  6742. ssl->options.failNoCert = opts.failNoCert;
  6743. ssl->options.failNoCertxPSK = opts.failNoCertxPSK;
  6744. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  6745. ssl->options.verifyPostHandshake = opts.verifyPostHandshake;
  6746. #endif
  6747. ssl->verifyCallback = vc;
  6748. }
  6749. void wolfSSL_set_verify_result(WOLFSSL *ssl, long v)
  6750. {
  6751. WOLFSSL_ENTER("wolfSSL_set_verify_result");
  6752. if (ssl == NULL)
  6753. return;
  6754. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  6755. defined(OPENSSL_ALL)
  6756. ssl->peerVerifyRet = v;
  6757. #else
  6758. (void)v;
  6759. WOLFSSL_STUB("wolfSSL_set_verify_result");
  6760. #endif
  6761. }
  6762. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  6763. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  6764. /* For TLS v1.3 send handshake messages after handshake completes. */
  6765. /* Returns 1=WOLFSSL_SUCCESS or 0=WOLFSSL_FAILURE */
  6766. int wolfSSL_verify_client_post_handshake(WOLFSSL* ssl)
  6767. {
  6768. int ret = wolfSSL_request_certificate(ssl);
  6769. if (ret != WOLFSSL_SUCCESS) {
  6770. if (!IsAtLeastTLSv1_3(ssl->version)) {
  6771. /* specific error of wrong version expected */
  6772. WOLFSSL_ERROR(UNSUPPORTED_PROTO_VERSION);
  6773. }
  6774. else {
  6775. WOLFSSL_ERROR(ret); /* log the error in the error queue */
  6776. }
  6777. }
  6778. return (ret == WOLFSSL_SUCCESS) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  6779. }
  6780. int wolfSSL_CTX_set_post_handshake_auth(WOLFSSL_CTX* ctx, int val)
  6781. {
  6782. int ret = wolfSSL_CTX_allow_post_handshake_auth(ctx);
  6783. if (ret == 0) {
  6784. ctx->postHandshakeAuth = (val != 0);
  6785. }
  6786. return (ret == 0) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  6787. }
  6788. int wolfSSL_set_post_handshake_auth(WOLFSSL* ssl, int val)
  6789. {
  6790. int ret = wolfSSL_allow_post_handshake_auth(ssl);
  6791. if (ret == 0) {
  6792. ssl->options.postHandshakeAuth = (val != 0);
  6793. }
  6794. return (ret == 0) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  6795. }
  6796. #endif /* OPENSSL_EXTRA && !NO_CERTS && WOLFSSL_TLS13 &&
  6797. * WOLFSSL_POST_HANDSHAKE_AUTH */
  6798. /* store user ctx for verify callback */
  6799. void wolfSSL_SetCertCbCtx(WOLFSSL* ssl, void* ctx)
  6800. {
  6801. WOLFSSL_ENTER("wolfSSL_SetCertCbCtx");
  6802. if (ssl)
  6803. ssl->verifyCbCtx = ctx;
  6804. }
  6805. /* store user ctx for verify callback */
  6806. void wolfSSL_CTX_SetCertCbCtx(WOLFSSL_CTX* ctx, void* userCtx)
  6807. {
  6808. WOLFSSL_ENTER("wolfSSL_CTX_SetCertCbCtx");
  6809. if (ctx)
  6810. ctx->verifyCbCtx = userCtx;
  6811. }
  6812. /* store context CA Cache addition callback */
  6813. void wolfSSL_CTX_SetCACb(WOLFSSL_CTX* ctx, CallbackCACache cb)
  6814. {
  6815. if (ctx && ctx->cm)
  6816. ctx->cm->caCacheCallback = cb;
  6817. }
  6818. #if defined(PERSIST_CERT_CACHE)
  6819. #if !defined(NO_FILESYSTEM)
  6820. /* Persist cert cache to file */
  6821. int wolfSSL_CTX_save_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
  6822. {
  6823. WOLFSSL_ENTER("wolfSSL_CTX_save_cert_cache");
  6824. if (ctx == NULL || fname == NULL)
  6825. return BAD_FUNC_ARG;
  6826. return CM_SaveCertCache(ctx->cm, fname);
  6827. }
  6828. /* Persist cert cache from file */
  6829. int wolfSSL_CTX_restore_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
  6830. {
  6831. WOLFSSL_ENTER("wolfSSL_CTX_restore_cert_cache");
  6832. if (ctx == NULL || fname == NULL)
  6833. return BAD_FUNC_ARG;
  6834. return CM_RestoreCertCache(ctx->cm, fname);
  6835. }
  6836. #endif /* NO_FILESYSTEM */
  6837. /* Persist cert cache to memory */
  6838. int wolfSSL_CTX_memsave_cert_cache(WOLFSSL_CTX* ctx, void* mem,
  6839. int sz, int* used)
  6840. {
  6841. WOLFSSL_ENTER("wolfSSL_CTX_memsave_cert_cache");
  6842. if (ctx == NULL || mem == NULL || used == NULL || sz <= 0)
  6843. return BAD_FUNC_ARG;
  6844. return CM_MemSaveCertCache(ctx->cm, mem, sz, used);
  6845. }
  6846. /* Restore cert cache from memory */
  6847. int wolfSSL_CTX_memrestore_cert_cache(WOLFSSL_CTX* ctx, const void* mem, int sz)
  6848. {
  6849. WOLFSSL_ENTER("wolfSSL_CTX_memrestore_cert_cache");
  6850. if (ctx == NULL || mem == NULL || sz <= 0)
  6851. return BAD_FUNC_ARG;
  6852. return CM_MemRestoreCertCache(ctx->cm, mem, sz);
  6853. }
  6854. /* get how big the the cert cache save buffer needs to be */
  6855. int wolfSSL_CTX_get_cert_cache_memsize(WOLFSSL_CTX* ctx)
  6856. {
  6857. WOLFSSL_ENTER("wolfSSL_CTX_get_cert_cache_memsize");
  6858. if (ctx == NULL)
  6859. return BAD_FUNC_ARG;
  6860. return CM_GetCertCacheMemSize(ctx->cm);
  6861. }
  6862. #endif /* PERSIST_CERT_CACHE */
  6863. #endif /* !NO_CERTS */
  6864. void wolfSSL_load_error_strings(void)
  6865. {
  6866. /* compatibility only */
  6867. }
  6868. int wolfSSL_library_init(void)
  6869. {
  6870. WOLFSSL_ENTER("wolfSSL_library_init");
  6871. if (wolfSSL_Init() == WOLFSSL_SUCCESS)
  6872. return WOLFSSL_SUCCESS;
  6873. else
  6874. return WOLFSSL_FATAL_ERROR;
  6875. }
  6876. #ifdef HAVE_SECRET_CALLBACK
  6877. int wolfSSL_set_session_secret_cb(WOLFSSL* ssl, SessionSecretCb cb, void* ctx)
  6878. {
  6879. WOLFSSL_ENTER("wolfSSL_set_session_secret_cb");
  6880. if (ssl == NULL)
  6881. return WOLFSSL_FATAL_ERROR;
  6882. ssl->sessionSecretCb = cb;
  6883. ssl->sessionSecretCtx = ctx;
  6884. if (cb != NULL) {
  6885. /* If using a pre-set key, assume session resumption. */
  6886. ssl->session->sessionIDSz = 0;
  6887. ssl->options.resuming = 1;
  6888. }
  6889. return WOLFSSL_SUCCESS;
  6890. }
  6891. int wolfSSL_set_secret_cb(WOLFSSL* ssl, TlsSecretCb cb, void* ctx)
  6892. {
  6893. WOLFSSL_ENTER("wolfSSL_set_secret_cb");
  6894. if (ssl == NULL)
  6895. return WOLFSSL_FATAL_ERROR;
  6896. ssl->tlsSecretCb = cb;
  6897. ssl->tlsSecretCtx = ctx;
  6898. return WOLFSSL_SUCCESS;
  6899. }
  6900. #ifdef SHOW_SECRETS
  6901. int tlsShowSecrets(WOLFSSL* ssl, void* secret, int secretSz,
  6902. void* ctx)
  6903. {
  6904. /* Wireshark Pre-Master-Secret Format:
  6905. * CLIENT_RANDOM <clientrandom> <mastersecret>
  6906. */
  6907. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  6908. int i, pmsPos = 0;
  6909. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  6910. byte clientRandom[RAN_LEN];
  6911. int clientRandomSz;
  6912. (void)ctx;
  6913. clientRandomSz = (int)wolfSSL_get_client_random(ssl, clientRandom,
  6914. sizeof(clientRandom));
  6915. if (clientRandomSz <= 0) {
  6916. printf("Error getting server random %d\n", clientRandomSz);
  6917. return BAD_FUNC_ARG;
  6918. }
  6919. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  6920. CLIENT_RANDOM_LABEL);
  6921. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  6922. for (i = 0; i < clientRandomSz; i++) {
  6923. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  6924. clientRandom[i]);
  6925. pmsPos += 2;
  6926. }
  6927. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  6928. pmsPos += 1;
  6929. for (i = 0; i < secretSz; i++) {
  6930. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  6931. ((byte*)secret)[i]);
  6932. pmsPos += 2;
  6933. }
  6934. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  6935. pmsPos += 1;
  6936. /* print master secret */
  6937. puts(pmsBuf);
  6938. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  6939. {
  6940. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  6941. if (f != XBADFILE) {
  6942. XFWRITE(pmsBuf, 1, pmsPos, f);
  6943. XFCLOSE(f);
  6944. }
  6945. }
  6946. #endif
  6947. return 0;
  6948. }
  6949. #endif /* SHOW_SECRETS */
  6950. #endif
  6951. #ifdef OPENSSL_EXTRA
  6952. /*
  6953. * check if the list has TLS13 and pre-TLS13 suites
  6954. * @param list cipher suite list that user want to set
  6955. * @return mixed: 0, only pre-TLS13: 1, only TLS13: 2
  6956. */
  6957. static int CheckcipherList(const char* list)
  6958. {
  6959. int ret;
  6960. int findTLSv13Suites = 0;
  6961. int findbeforeSuites = 0;
  6962. byte cipherSuite0;
  6963. byte cipherSuite1;
  6964. int flags;
  6965. char* next = (char*)list;
  6966. do {
  6967. char* current = next;
  6968. char name[MAX_SUITE_NAME + 1];
  6969. word32 length = MAX_SUITE_NAME;
  6970. word32 current_length;
  6971. next = XSTRSTR(next, ":");
  6972. current_length = (!next) ? (word32)XSTRLEN(current)
  6973. : (word32)(next - current);
  6974. if (current_length < length) {
  6975. length = current_length;
  6976. }
  6977. XMEMCPY(name, current, length);
  6978. name[length] = 0;
  6979. if (XSTRCMP(name, "ALL") == 0 || XSTRCMP(name, "DEFAULT") == 0 ||
  6980. XSTRCMP(name, "HIGH") == 0) {
  6981. findTLSv13Suites = 1;
  6982. findbeforeSuites = 1;
  6983. break;
  6984. }
  6985. ret = wolfSSL_get_cipher_suite_from_name(name, &cipherSuite0,
  6986. &cipherSuite1, &flags);
  6987. if (ret == 0) {
  6988. if (cipherSuite0 == TLS13_BYTE) {
  6989. /* TLSv13 suite */
  6990. findTLSv13Suites = 1;
  6991. }
  6992. else {
  6993. findbeforeSuites = 1;
  6994. }
  6995. }
  6996. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  6997. /* check if mixed due to names like RSA:ECDHE+AESGCM etc. */
  6998. if (ret != 0) {
  6999. char* subStr = name;
  7000. char* subStrNext;
  7001. do {
  7002. subStrNext = XSTRSTR(subStr, "+");
  7003. if ((XSTRCMP(subStr, "ECDHE") == 0) ||
  7004. (XSTRCMP(subStr, "RSA") == 0)) {
  7005. return 0;
  7006. }
  7007. if (subStrNext && (XSTRLEN(subStrNext) > 0)) {
  7008. subStr = subStrNext + 1; /* +1 to skip past '+' */
  7009. }
  7010. } while (subStrNext != NULL);
  7011. }
  7012. #endif
  7013. if (findTLSv13Suites == 1 && findbeforeSuites == 1) {
  7014. /* list has mixed suites */
  7015. return 0;
  7016. }
  7017. }
  7018. while (next++); /* ++ needed to skip ':' */
  7019. if (findTLSv13Suites == 0 && findbeforeSuites == 1) {
  7020. ret = 1;/* only before TLSv13 suites */
  7021. }
  7022. else if (findTLSv13Suites == 1 && findbeforeSuites == 0) {
  7023. ret = 2;/* only TLSv13 suties */
  7024. }
  7025. else {
  7026. ret = 0;/* handle as mixed */
  7027. }
  7028. return ret;
  7029. }
  7030. /* parse some bulk lists like !eNULL / !aNULL
  7031. *
  7032. * returns WOLFSSL_SUCCESS on success and sets the cipher suite list
  7033. */
  7034. static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  7035. Suites* suites, const char* list)
  7036. {
  7037. int ret = 0;
  7038. int listattribute = 0;
  7039. int tls13Only = 0;
  7040. #ifndef WOLFSSL_SMALL_STACK
  7041. byte suitesCpy[WOLFSSL_MAX_SUITE_SZ];
  7042. #else
  7043. byte* suitesCpy = NULL;
  7044. #endif
  7045. word16 suitesCpySz = 0;
  7046. word16 i = 0;
  7047. word16 j = 0;
  7048. if (suites == NULL || list == NULL) {
  7049. WOLFSSL_MSG("NULL argument");
  7050. return WOLFSSL_FAILURE;
  7051. }
  7052. listattribute = CheckcipherList(list);
  7053. if (listattribute == 0) {
  7054. /* list has mixed(pre-TLSv13 and TLSv13) suites
  7055. * update cipher suites the same as before
  7056. */
  7057. return (SetCipherList_ex(ctx, ssl, suites, list)) ? WOLFSSL_SUCCESS :
  7058. WOLFSSL_FAILURE;
  7059. }
  7060. else if (listattribute == 1) {
  7061. /* list has only pre-TLSv13 suites.
  7062. * Only update before TLSv13 suites.
  7063. */
  7064. tls13Only = 0;
  7065. }
  7066. else if (listattribute == 2) {
  7067. /* list has only TLSv13 suites. Only update TLv13 suites
  7068. * simulate set_ciphersuites() compatibility layer API
  7069. */
  7070. tls13Only = 1;
  7071. if ((ctx != NULL && !IsAtLeastTLSv1_3(ctx->method->version)) ||
  7072. (ssl != NULL && !IsAtLeastTLSv1_3(ssl->version))) {
  7073. /* Silently ignore TLS 1.3 ciphers if we don't support it. */
  7074. return WOLFSSL_SUCCESS;
  7075. }
  7076. }
  7077. /* list contains ciphers either only for TLS 1.3 or <= TLS 1.2 */
  7078. if (suites->suiteSz == 0) {
  7079. WOLFSSL_MSG("Warning suites->suiteSz = 0 set to WOLFSSL_MAX_SUITE_SZ");
  7080. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  7081. }
  7082. #ifdef WOLFSSL_SMALL_STACK
  7083. if (suites->suiteSz > 0) {
  7084. suitesCpy = (byte*)XMALLOC(suites->suiteSz, NULL,
  7085. DYNAMIC_TYPE_TMP_BUFFER);
  7086. if (suitesCpy == NULL) {
  7087. return WOLFSSL_FAILURE;
  7088. }
  7089. XMEMSET(suitesCpy, 0, suites->suiteSz);
  7090. }
  7091. #else
  7092. XMEMSET(suitesCpy, 0, sizeof(suitesCpy));
  7093. #endif
  7094. if (suites->suiteSz > 0)
  7095. XMEMCPY(suitesCpy, suites->suites, suites->suiteSz);
  7096. suitesCpySz = suites->suiteSz;
  7097. ret = SetCipherList_ex(ctx, ssl, suites, list);
  7098. if (ret != 1) {
  7099. #ifdef WOLFSSL_SMALL_STACK
  7100. XFREE(suitesCpy, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7101. #endif
  7102. return WOLFSSL_FAILURE;
  7103. }
  7104. for (i = 0; i < suitesCpySz &&
  7105. suites->suiteSz <= (WOLFSSL_MAX_SUITE_SZ - SUITE_LEN); i += 2) {
  7106. /* Check for duplicates */
  7107. int duplicate = 0;
  7108. for (j = 0; j < suites->suiteSz; j += 2) {
  7109. if (suitesCpy[i] == suites->suites[j] &&
  7110. suitesCpy[i+1] == suites->suites[j+1]) {
  7111. duplicate = 1;
  7112. break;
  7113. }
  7114. }
  7115. if (!duplicate) {
  7116. if (tls13Only) {
  7117. /* Updating TLS 1.3 ciphers */
  7118. if (suitesCpy[i] != TLS13_BYTE) {
  7119. /* Only copy over <= TLS 1.2 ciphers */
  7120. /* TLS 1.3 ciphers take precedence */
  7121. suites->suites[suites->suiteSz++] = suitesCpy[i];
  7122. suites->suites[suites->suiteSz++] = suitesCpy[i+1];
  7123. }
  7124. }
  7125. else {
  7126. /* Updating <= TLS 1.2 ciphers */
  7127. if (suitesCpy[i] == TLS13_BYTE) {
  7128. /* Only copy over TLS 1.3 ciphers */
  7129. /* TLS 1.3 ciphers take precedence */
  7130. XMEMMOVE(suites->suites + SUITE_LEN, suites->suites,
  7131. suites->suiteSz);
  7132. suites->suites[0] = suitesCpy[i];
  7133. suites->suites[1] = suitesCpy[i+1];
  7134. suites->suiteSz += 2;
  7135. }
  7136. }
  7137. }
  7138. }
  7139. #ifdef WOLFSSL_SMALL_STACK
  7140. XFREE(suitesCpy, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7141. #endif
  7142. return ret;
  7143. }
  7144. #endif
  7145. int wolfSSL_CTX_set_cipher_list(WOLFSSL_CTX* ctx, const char* list)
  7146. {
  7147. WOLFSSL_ENTER("wolfSSL_CTX_set_cipher_list");
  7148. if (ctx == NULL)
  7149. return WOLFSSL_FAILURE;
  7150. if (AllocateCtxSuites(ctx) != 0)
  7151. return WOLFSSL_FAILURE;
  7152. #ifdef OPENSSL_EXTRA
  7153. return wolfSSL_parse_cipher_list(ctx, NULL, ctx->suites, list);
  7154. #else
  7155. return (SetCipherList(ctx, ctx->suites, list)) ?
  7156. WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  7157. #endif
  7158. }
  7159. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  7160. int wolfSSL_CTX_set_cipher_list_bytes(WOLFSSL_CTX* ctx, const byte* list,
  7161. const int listSz)
  7162. {
  7163. WOLFSSL_ENTER("wolfSSL_CTX_set_cipher_list_bytes");
  7164. if (ctx == NULL)
  7165. return WOLFSSL_FAILURE;
  7166. if (AllocateCtxSuites(ctx) != 0)
  7167. return WOLFSSL_FAILURE;
  7168. return (SetCipherListFromBytes(ctx, ctx->suites, list, listSz)) ?
  7169. WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  7170. }
  7171. #endif /* OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES */
  7172. int wolfSSL_set_cipher_list(WOLFSSL* ssl, const char* list)
  7173. {
  7174. WOLFSSL_ENTER("wolfSSL_set_cipher_list");
  7175. if (ssl == NULL || ssl->ctx == NULL) {
  7176. return WOLFSSL_FAILURE;
  7177. }
  7178. if (AllocateSuites(ssl) != 0)
  7179. return WOLFSSL_FAILURE;
  7180. #ifdef OPENSSL_EXTRA
  7181. return wolfSSL_parse_cipher_list(NULL, ssl, ssl->suites, list);
  7182. #else
  7183. return (SetCipherList_ex(NULL, ssl, ssl->suites, list)) ?
  7184. WOLFSSL_SUCCESS :
  7185. WOLFSSL_FAILURE;
  7186. #endif
  7187. }
  7188. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  7189. int wolfSSL_set_cipher_list_bytes(WOLFSSL* ssl, const byte* list,
  7190. const int listSz)
  7191. {
  7192. WOLFSSL_ENTER("wolfSSL_set_cipher_list_bytes");
  7193. if (ssl == NULL || ssl->ctx == NULL) {
  7194. return WOLFSSL_FAILURE;
  7195. }
  7196. if (AllocateSuites(ssl) != 0)
  7197. return WOLFSSL_FAILURE;
  7198. return (SetCipherListFromBytes(ssl->ctx, ssl->suites, list, listSz))
  7199. ? WOLFSSL_SUCCESS
  7200. : WOLFSSL_FAILURE;
  7201. }
  7202. #endif /* OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES */
  7203. #ifdef HAVE_KEYING_MATERIAL
  7204. #define TLS_PRF_LABEL_CLIENT_FINISHED "client finished"
  7205. #define TLS_PRF_LABEL_SERVER_FINISHED "server finished"
  7206. #define TLS_PRF_LABEL_MASTER_SECRET "master secret"
  7207. #define TLS_PRF_LABEL_EXT_MASTER_SECRET "extended master secret"
  7208. #define TLS_PRF_LABEL_KEY_EXPANSION "key expansion"
  7209. static const struct ForbiddenLabels {
  7210. const char* label;
  7211. size_t labelLen;
  7212. } forbiddenLabels[] = {
  7213. {TLS_PRF_LABEL_CLIENT_FINISHED, XSTR_SIZEOF(TLS_PRF_LABEL_CLIENT_FINISHED)},
  7214. {TLS_PRF_LABEL_SERVER_FINISHED, XSTR_SIZEOF(TLS_PRF_LABEL_SERVER_FINISHED)},
  7215. {TLS_PRF_LABEL_MASTER_SECRET, XSTR_SIZEOF(TLS_PRF_LABEL_MASTER_SECRET)},
  7216. {TLS_PRF_LABEL_EXT_MASTER_SECRET,
  7217. XSTR_SIZEOF(TLS_PRF_LABEL_EXT_MASTER_SECRET)},
  7218. {TLS_PRF_LABEL_KEY_EXPANSION, XSTR_SIZEOF(TLS_PRF_LABEL_KEY_EXPANSION)},
  7219. {NULL, 0},
  7220. };
  7221. /**
  7222. * Implement RFC 5705
  7223. * TLS 1.3 uses a different exporter definition (section 7.5 of RFC 8446)
  7224. * @return WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on error
  7225. */
  7226. int wolfSSL_export_keying_material(WOLFSSL *ssl,
  7227. unsigned char *out, size_t outLen,
  7228. const char *label, size_t labelLen,
  7229. const unsigned char *context, size_t contextLen,
  7230. int use_context)
  7231. {
  7232. byte* seed = NULL;
  7233. word32 seedLen;
  7234. const struct ForbiddenLabels* fl;
  7235. WOLFSSL_ENTER("wolfSSL_export_keying_material");
  7236. if (ssl == NULL || out == NULL || label == NULL ||
  7237. (use_context && contextLen && context == NULL)) {
  7238. WOLFSSL_MSG("Bad argument");
  7239. return WOLFSSL_FAILURE;
  7240. }
  7241. /* clientRandom + serverRandom
  7242. * OR
  7243. * clientRandom + serverRandom + ctx len encoding + ctx */
  7244. seedLen = !use_context ? (word32)SEED_LEN :
  7245. (word32)SEED_LEN + 2 + (word32)contextLen;
  7246. if (ssl->options.saveArrays == 0 || ssl->arrays == NULL) {
  7247. WOLFSSL_MSG("To export keying material wolfSSL needs to keep handshake "
  7248. "data. Call wolfSSL_KeepArrays before attempting to "
  7249. "export keyid material.");
  7250. return WOLFSSL_FAILURE;
  7251. }
  7252. /* check forbidden labels */
  7253. for (fl = &forbiddenLabels[0]; fl->label != NULL; fl++) {
  7254. if (labelLen >= fl->labelLen &&
  7255. XMEMCMP(label, fl->label, fl->labelLen) == 0) {
  7256. WOLFSSL_MSG("Forbidden label");
  7257. return WOLFSSL_FAILURE;
  7258. }
  7259. }
  7260. #ifdef WOLFSSL_TLS13
  7261. if (IsAtLeastTLSv1_3(ssl->version)) {
  7262. /* Path for TLS 1.3 */
  7263. if (!use_context) {
  7264. contextLen = 0;
  7265. context = (byte*)""; /* Give valid pointer for 0 length memcpy */
  7266. }
  7267. if (Tls13_Exporter(ssl, out, (word32)outLen, label, labelLen,
  7268. context, contextLen) != 0) {
  7269. WOLFSSL_MSG("Tls13_Exporter error");
  7270. return WOLFSSL_FAILURE;
  7271. }
  7272. return WOLFSSL_SUCCESS;
  7273. }
  7274. #endif
  7275. /* Path for <=TLS 1.2 */
  7276. seed = (byte*)XMALLOC(seedLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7277. if (seed == NULL) {
  7278. WOLFSSL_MSG("malloc error");
  7279. return WOLFSSL_FAILURE;
  7280. }
  7281. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  7282. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  7283. if (use_context) {
  7284. /* Encode len in big endian */
  7285. seed[SEED_LEN ] = (contextLen >> 8) & 0xFF;
  7286. seed[SEED_LEN + 1] = (contextLen) & 0xFF;
  7287. if (contextLen) {
  7288. /* 0 length context is allowed */
  7289. XMEMCPY(seed + SEED_LEN + 2, context, contextLen);
  7290. }
  7291. }
  7292. PRIVATE_KEY_UNLOCK();
  7293. if (wc_PRF_TLS(out, (word32)outLen, ssl->arrays->masterSecret, SECRET_LEN,
  7294. (byte*)label, (word32)labelLen, seed, seedLen,
  7295. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm, ssl->heap,
  7296. ssl->devId) != 0) {
  7297. WOLFSSL_MSG("wc_PRF_TLS error");
  7298. PRIVATE_KEY_LOCK();
  7299. XFREE(seed, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7300. return WOLFSSL_FAILURE;
  7301. }
  7302. PRIVATE_KEY_LOCK();
  7303. XFREE(seed, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7304. return WOLFSSL_SUCCESS;
  7305. }
  7306. #endif /* HAVE_KEYING_MATERIAL */
  7307. int wolfSSL_dtls_get_using_nonblock(WOLFSSL* ssl)
  7308. {
  7309. int useNb = 0;
  7310. if (ssl == NULL)
  7311. return WOLFSSL_FAILURE;
  7312. WOLFSSL_ENTER("wolfSSL_dtls_get_using_nonblock");
  7313. if (ssl->options.dtls) {
  7314. #ifdef WOLFSSL_DTLS
  7315. useNb = ssl->options.dtlsUseNonblock;
  7316. #endif
  7317. }
  7318. else {
  7319. WOLFSSL_MSG("wolfSSL_dtls_get_using_nonblock() is "
  7320. "DEPRECATED for non-DTLS use.");
  7321. }
  7322. return useNb;
  7323. }
  7324. #ifndef WOLFSSL_LEANPSK
  7325. void wolfSSL_dtls_set_using_nonblock(WOLFSSL* ssl, int nonblock)
  7326. {
  7327. (void)nonblock;
  7328. WOLFSSL_ENTER("wolfSSL_dtls_set_using_nonblock");
  7329. if (ssl == NULL)
  7330. return;
  7331. if (ssl->options.dtls) {
  7332. #ifdef WOLFSSL_DTLS
  7333. ssl->options.dtlsUseNonblock = (nonblock != 0);
  7334. #endif
  7335. }
  7336. else {
  7337. WOLFSSL_MSG("wolfSSL_dtls_set_using_nonblock() is "
  7338. "DEPRECATED for non-DTLS use.");
  7339. }
  7340. }
  7341. #ifdef WOLFSSL_DTLS
  7342. int wolfSSL_dtls_get_current_timeout(WOLFSSL* ssl)
  7343. {
  7344. int timeout = 0;
  7345. if (ssl)
  7346. timeout = ssl->dtls_timeout;
  7347. WOLFSSL_LEAVE("wolfSSL_dtls_get_current_timeout", timeout);
  7348. return timeout;
  7349. }
  7350. #ifdef WOLFSSL_DTLS13
  7351. /*
  7352. * This API returns 1 when the user should set a short timeout for receiving
  7353. * data. It is recommended that it is at most 1/4 the value returned by
  7354. * wolfSSL_dtls_get_current_timeout().
  7355. */
  7356. int wolfSSL_dtls13_use_quick_timeout(WOLFSSL* ssl)
  7357. {
  7358. return ssl->dtls13FastTimeout;
  7359. }
  7360. /*
  7361. * When this is set, a DTLS 1.3 connection will send acks immediately when a
  7362. * disruption is detected to shortcut timeouts. This results in potentially
  7363. * more traffic but may make the handshake quicker.
  7364. */
  7365. void wolfSSL_dtls13_set_send_more_acks(WOLFSSL* ssl, int value)
  7366. {
  7367. if (ssl != NULL)
  7368. ssl->options.dtls13SendMoreAcks = !!value;
  7369. }
  7370. #endif /* WOLFSSL_DTLS13 */
  7371. int wolfSSL_DTLSv1_get_timeout(WOLFSSL* ssl, WOLFSSL_TIMEVAL* timeleft)
  7372. {
  7373. if (ssl && timeleft) {
  7374. XMEMSET(timeleft, 0, sizeof(WOLFSSL_TIMEVAL));
  7375. timeleft->tv_sec = ssl->dtls_timeout;
  7376. }
  7377. return 0;
  7378. }
  7379. #ifndef NO_WOLFSSL_STUB
  7380. int wolfSSL_DTLSv1_handle_timeout(WOLFSSL* ssl)
  7381. {
  7382. WOLFSSL_STUB("SSL_DTLSv1_handle_timeout");
  7383. (void)ssl;
  7384. return 0;
  7385. }
  7386. #endif
  7387. #ifndef NO_WOLFSSL_STUB
  7388. void wolfSSL_DTLSv1_set_initial_timeout_duration(WOLFSSL* ssl,
  7389. word32 duration_ms)
  7390. {
  7391. WOLFSSL_STUB("SSL_DTLSv1_set_initial_timeout_duration");
  7392. (void)ssl;
  7393. (void)duration_ms;
  7394. }
  7395. #endif
  7396. /* user may need to alter init dtls recv timeout, WOLFSSL_SUCCESS on ok */
  7397. int wolfSSL_dtls_set_timeout_init(WOLFSSL* ssl, int timeout)
  7398. {
  7399. if (ssl == NULL || timeout < 0)
  7400. return BAD_FUNC_ARG;
  7401. if (timeout > ssl->dtls_timeout_max) {
  7402. WOLFSSL_MSG("Can't set dtls timeout init greater than dtls timeout "
  7403. "max");
  7404. return BAD_FUNC_ARG;
  7405. }
  7406. ssl->dtls_timeout_init = timeout;
  7407. ssl->dtls_timeout = timeout;
  7408. return WOLFSSL_SUCCESS;
  7409. }
  7410. /* user may need to alter max dtls recv timeout, WOLFSSL_SUCCESS on ok */
  7411. int wolfSSL_dtls_set_timeout_max(WOLFSSL* ssl, int timeout)
  7412. {
  7413. if (ssl == NULL || timeout < 0)
  7414. return BAD_FUNC_ARG;
  7415. if (timeout < ssl->dtls_timeout_init) {
  7416. WOLFSSL_MSG("Can't set dtls timeout max less than dtls timeout init");
  7417. return BAD_FUNC_ARG;
  7418. }
  7419. ssl->dtls_timeout_max = timeout;
  7420. return WOLFSSL_SUCCESS;
  7421. }
  7422. int wolfSSL_dtls_got_timeout(WOLFSSL* ssl)
  7423. {
  7424. int result = WOLFSSL_SUCCESS;
  7425. WOLFSSL_ENTER("wolfSSL_dtls_got_timeout");
  7426. if (ssl == NULL)
  7427. return WOLFSSL_FATAL_ERROR;
  7428. #ifdef WOLFSSL_DTLS13
  7429. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)) {
  7430. result = Dtls13RtxTimeout(ssl);
  7431. if (result < 0) {
  7432. if (result == WANT_WRITE)
  7433. ssl->dtls13SendingAckOrRtx = 1;
  7434. ssl->error = result;
  7435. WOLFSSL_ERROR(result);
  7436. return WOLFSSL_FATAL_ERROR;
  7437. }
  7438. return WOLFSSL_SUCCESS;
  7439. }
  7440. #endif /* WOLFSSL_DTLS13 */
  7441. if ((IsSCR(ssl) || !ssl->options.handShakeDone)) {
  7442. if (DtlsMsgPoolTimeout(ssl) < 0){
  7443. ssl->error = SOCKET_ERROR_E;
  7444. WOLFSSL_ERROR(ssl->error);
  7445. result = WOLFSSL_FATAL_ERROR;
  7446. }
  7447. else if ((result = DtlsMsgPoolSend(ssl, 0)) < 0) {
  7448. ssl->error = result;
  7449. WOLFSSL_ERROR(result);
  7450. result = WOLFSSL_FATAL_ERROR;
  7451. }
  7452. else {
  7453. /* Reset return value to success */
  7454. result = WOLFSSL_SUCCESS;
  7455. }
  7456. }
  7457. WOLFSSL_LEAVE("wolfSSL_dtls_got_timeout", result);
  7458. return result;
  7459. }
  7460. /* retransmit all the saves messages, WOLFSSL_SUCCESS on ok */
  7461. int wolfSSL_dtls_retransmit(WOLFSSL* ssl)
  7462. {
  7463. WOLFSSL_ENTER("wolfSSL_dtls_retransmit");
  7464. if (ssl == NULL)
  7465. return WOLFSSL_FATAL_ERROR;
  7466. if (!ssl->options.handShakeDone) {
  7467. int result = DtlsMsgPoolSend(ssl, 0);
  7468. if (result < 0) {
  7469. ssl->error = result;
  7470. WOLFSSL_ERROR(result);
  7471. return WOLFSSL_FATAL_ERROR;
  7472. }
  7473. }
  7474. return 0;
  7475. }
  7476. #endif /* DTLS */
  7477. #endif /* LEANPSK */
  7478. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  7479. /* Not an SSL function, return 0 for success, error code otherwise */
  7480. /* Prereq: ssl's RNG needs to be initialized. */
  7481. int wolfSSL_DTLS_SetCookieSecret(WOLFSSL* ssl,
  7482. const byte* secret, word32 secretSz)
  7483. {
  7484. int ret = 0;
  7485. WOLFSSL_ENTER("wolfSSL_DTLS_SetCookieSecret");
  7486. if (ssl == NULL) {
  7487. WOLFSSL_MSG("need a SSL object");
  7488. return BAD_FUNC_ARG;
  7489. }
  7490. if (secret != NULL && secretSz == 0) {
  7491. WOLFSSL_MSG("can't have a new secret without a size");
  7492. return BAD_FUNC_ARG;
  7493. }
  7494. /* If secretSz is 0, use the default size. */
  7495. if (secretSz == 0)
  7496. secretSz = COOKIE_SECRET_SZ;
  7497. if (secretSz != ssl->buffers.dtlsCookieSecret.length) {
  7498. byte* newSecret;
  7499. if (ssl->buffers.dtlsCookieSecret.buffer != NULL) {
  7500. ForceZero(ssl->buffers.dtlsCookieSecret.buffer,
  7501. ssl->buffers.dtlsCookieSecret.length);
  7502. XFREE(ssl->buffers.dtlsCookieSecret.buffer,
  7503. ssl->heap, DYNAMIC_TYPE_COOKIE_PWD);
  7504. }
  7505. newSecret = (byte*)XMALLOC(secretSz, ssl->heap,DYNAMIC_TYPE_COOKIE_PWD);
  7506. if (newSecret == NULL) {
  7507. ssl->buffers.dtlsCookieSecret.buffer = NULL;
  7508. ssl->buffers.dtlsCookieSecret.length = 0;
  7509. WOLFSSL_MSG("couldn't allocate new cookie secret");
  7510. return MEMORY_ERROR;
  7511. }
  7512. ssl->buffers.dtlsCookieSecret.buffer = newSecret;
  7513. ssl->buffers.dtlsCookieSecret.length = secretSz;
  7514. #ifdef WOLFSSL_CHECK_MEM_ZERO
  7515. wc_MemZero_Add("wolfSSL_DTLS_SetCookieSecret secret",
  7516. ssl->buffers.dtlsCookieSecret.buffer,
  7517. ssl->buffers.dtlsCookieSecret.length);
  7518. #endif
  7519. }
  7520. /* If the supplied secret is NULL, randomly generate a new secret. */
  7521. if (secret == NULL) {
  7522. ret = wc_RNG_GenerateBlock(ssl->rng,
  7523. ssl->buffers.dtlsCookieSecret.buffer, secretSz);
  7524. }
  7525. else
  7526. XMEMCPY(ssl->buffers.dtlsCookieSecret.buffer, secret, secretSz);
  7527. WOLFSSL_LEAVE("wolfSSL_DTLS_SetCookieSecret", 0);
  7528. return ret;
  7529. }
  7530. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
  7531. /* EITHER SIDE METHODS */
  7532. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  7533. WOLFSSL_METHOD* wolfSSLv23_method(void)
  7534. {
  7535. return wolfSSLv23_method_ex(NULL);
  7536. }
  7537. WOLFSSL_METHOD* wolfSSLv23_method_ex(void* heap)
  7538. {
  7539. WOLFSSL_METHOD* m = NULL;
  7540. WOLFSSL_ENTER("wolfSSLv23_method");
  7541. #if !defined(NO_WOLFSSL_CLIENT)
  7542. m = wolfSSLv23_client_method_ex(heap);
  7543. #elif !defined(NO_WOLFSSL_SERVER)
  7544. m = wolfSSLv23_server_method_ex(heap);
  7545. #else
  7546. (void)heap;
  7547. #endif
  7548. if (m != NULL) {
  7549. m->side = WOLFSSL_NEITHER_END;
  7550. }
  7551. return m;
  7552. }
  7553. #ifndef NO_OLD_TLS
  7554. #ifdef WOLFSSL_ALLOW_SSLV3
  7555. WOLFSSL_METHOD* wolfSSLv3_method(void)
  7556. {
  7557. return wolfSSLv3_method_ex(NULL);
  7558. }
  7559. WOLFSSL_METHOD* wolfSSLv3_method_ex(void* heap)
  7560. {
  7561. WOLFSSL_METHOD* m = NULL;
  7562. WOLFSSL_ENTER("wolfSSLv3_method_ex");
  7563. #if !defined(NO_WOLFSSL_CLIENT)
  7564. m = wolfSSLv3_client_method_ex(heap);
  7565. #elif !defined(NO_WOLFSSL_SERVER)
  7566. m = wolfSSLv3_server_method_ex(heap);
  7567. #endif
  7568. if (m != NULL) {
  7569. m->side = WOLFSSL_NEITHER_END;
  7570. }
  7571. return m;
  7572. }
  7573. #endif
  7574. #endif
  7575. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  7576. /* client only parts */
  7577. #ifndef NO_WOLFSSL_CLIENT
  7578. #if defined(OPENSSL_EXTRA) && !defined(NO_OLD_TLS)
  7579. WOLFSSL_METHOD* wolfSSLv2_client_method(void)
  7580. {
  7581. WOLFSSL_STUB("wolfSSLv2_client_method");
  7582. return NULL;
  7583. }
  7584. #endif
  7585. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  7586. WOLFSSL_METHOD* wolfSSLv3_client_method(void)
  7587. {
  7588. return wolfSSLv3_client_method_ex(NULL);
  7589. }
  7590. WOLFSSL_METHOD* wolfSSLv3_client_method_ex(void* heap)
  7591. {
  7592. WOLFSSL_METHOD* method =
  7593. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  7594. heap, DYNAMIC_TYPE_METHOD);
  7595. (void)heap;
  7596. WOLFSSL_ENTER("wolfSSLv3_client_method_ex");
  7597. if (method)
  7598. InitSSL_Method(method, MakeSSLv3());
  7599. return method;
  7600. }
  7601. #endif /* WOLFSSL_ALLOW_SSLV3 && !NO_OLD_TLS */
  7602. WOLFSSL_METHOD* wolfSSLv23_client_method(void)
  7603. {
  7604. return wolfSSLv23_client_method_ex(NULL);
  7605. }
  7606. WOLFSSL_METHOD* wolfSSLv23_client_method_ex(void* heap)
  7607. {
  7608. WOLFSSL_METHOD* method =
  7609. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  7610. heap, DYNAMIC_TYPE_METHOD);
  7611. (void)heap;
  7612. WOLFSSL_ENTER("wolfSSLv23_client_method_ex");
  7613. if (method) {
  7614. #if !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || \
  7615. defined(WOLFSSL_SHA512)
  7616. #if defined(WOLFSSL_TLS13)
  7617. InitSSL_Method(method, MakeTLSv1_3());
  7618. #elif !defined(WOLFSSL_NO_TLS12)
  7619. InitSSL_Method(method, MakeTLSv1_2());
  7620. #elif !defined(NO_OLD_TLS)
  7621. InitSSL_Method(method, MakeTLSv1_1());
  7622. #endif
  7623. #else
  7624. #ifndef NO_OLD_TLS
  7625. InitSSL_Method(method, MakeTLSv1_1());
  7626. #endif
  7627. #endif
  7628. #if !defined(NO_OLD_TLS) || defined(WOLFSSL_TLS13)
  7629. method->downgrade = 1;
  7630. #endif
  7631. }
  7632. return method;
  7633. }
  7634. /* please see note at top of README if you get an error from connect */
  7635. WOLFSSL_ABI
  7636. int wolfSSL_connect(WOLFSSL* ssl)
  7637. {
  7638. #if !(defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS) && \
  7639. defined(WOLFSSL_TLS13))
  7640. int neededState;
  7641. byte advanceState;
  7642. #endif
  7643. int ret = 0;
  7644. (void)ret;
  7645. #ifdef HAVE_ERRNO_H
  7646. errno = 0;
  7647. #endif
  7648. if (ssl == NULL)
  7649. return BAD_FUNC_ARG;
  7650. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  7651. if (ssl->options.side == WOLFSSL_NEITHER_END) {
  7652. ssl->error = InitSSL_Side(ssl, WOLFSSL_CLIENT_END);
  7653. if (ssl->error != WOLFSSL_SUCCESS) {
  7654. WOLFSSL_ERROR(ssl->error);
  7655. return WOLFSSL_FATAL_ERROR;
  7656. }
  7657. ssl->error = 0; /* expected to be zero here */
  7658. }
  7659. #ifdef OPENSSL_EXTRA
  7660. if (ssl->CBIS != NULL) {
  7661. ssl->CBIS(ssl, SSL_ST_CONNECT, WOLFSSL_SUCCESS);
  7662. ssl->cbmode = SSL_CB_WRITE;
  7663. }
  7664. #endif
  7665. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  7666. #if defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS) && \
  7667. defined(WOLFSSL_TLS13)
  7668. return wolfSSL_connect_TLSv13(ssl);
  7669. #else
  7670. #ifdef WOLFSSL_TLS13
  7671. if (ssl->options.tls1_3) {
  7672. WOLFSSL_MSG("TLS 1.3");
  7673. return wolfSSL_connect_TLSv13(ssl);
  7674. }
  7675. #endif
  7676. WOLFSSL_MSG("TLS 1.2 or lower");
  7677. WOLFSSL_ENTER("wolfSSL_connect");
  7678. /* make sure this wolfSSL object has arrays and rng setup. Protects
  7679. * case where the WOLFSSL object is reused via wolfSSL_clear() */
  7680. if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) {
  7681. return ret;
  7682. }
  7683. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  7684. if ((ssl->ConnectFilter != NULL) &&
  7685. (ssl->options.connectState == CONNECT_BEGIN)) {
  7686. wolfSSL_netfilter_decision_t res;
  7687. if ((ssl->ConnectFilter(ssl, ssl->ConnectFilter_arg, &res) ==
  7688. WOLFSSL_SUCCESS) &&
  7689. (res == WOLFSSL_NETFILTER_REJECT)) {
  7690. ssl->error = SOCKET_FILTERED_E;
  7691. WOLFSSL_ERROR(ssl->error);
  7692. return WOLFSSL_FATAL_ERROR;
  7693. }
  7694. }
  7695. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  7696. if (ssl->options.side != WOLFSSL_CLIENT_END) {
  7697. ssl->error = SIDE_ERROR;
  7698. WOLFSSL_ERROR(ssl->error);
  7699. return WOLFSSL_FATAL_ERROR;
  7700. }
  7701. #ifdef WOLFSSL_DTLS
  7702. if (ssl->version.major == DTLS_MAJOR) {
  7703. ssl->options.dtls = 1;
  7704. ssl->options.tls = 1;
  7705. ssl->options.tls1_1 = 1;
  7706. ssl->options.dtlsStateful = 1;
  7707. }
  7708. #endif
  7709. /* fragOffset is non-zero when sending fragments. On the last
  7710. * fragment, fragOffset is zero again, and the state can be
  7711. * advanced. */
  7712. advanceState = ssl->fragOffset == 0 &&
  7713. (ssl->options.connectState == CONNECT_BEGIN ||
  7714. ssl->options.connectState == HELLO_AGAIN ||
  7715. (ssl->options.connectState >= FIRST_REPLY_DONE &&
  7716. ssl->options.connectState <= FIRST_REPLY_FOURTH));
  7717. #ifdef WOLFSSL_DTLS13
  7718. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  7719. advanceState = advanceState && !ssl->dtls13SendingAckOrRtx;
  7720. #endif /* WOLFSSL_DTLS13 */
  7721. if (ssl->buffers.outputBuffer.length > 0
  7722. #ifdef WOLFSSL_ASYNC_CRYPT
  7723. /* do not send buffered or advance state if last error was an
  7724. async pending operation */
  7725. && ssl->error != WC_PENDING_E
  7726. #endif
  7727. ) {
  7728. ret = SendBuffered(ssl);
  7729. if (ret == 0) {
  7730. if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) {
  7731. if (advanceState) {
  7732. ssl->options.connectState++;
  7733. WOLFSSL_MSG("connect state: Advanced from last "
  7734. "buffered fragment send");
  7735. #ifdef WOLFSSL_ASYNC_IO
  7736. /* Cleanup async */
  7737. FreeAsyncCtx(ssl, 0);
  7738. #endif
  7739. }
  7740. }
  7741. else {
  7742. WOLFSSL_MSG("connect state: "
  7743. "Not advanced, more fragments to send");
  7744. }
  7745. }
  7746. else {
  7747. ssl->error = ret;
  7748. WOLFSSL_ERROR(ssl->error);
  7749. return WOLFSSL_FATAL_ERROR;
  7750. }
  7751. #ifdef WOLFSSL_DTLS13
  7752. if (ssl->options.dtls)
  7753. ssl->dtls13SendingAckOrRtx = 0;
  7754. #endif /* WOLFSSL_DTLS13 */
  7755. }
  7756. ret = RetrySendAlert(ssl);
  7757. if (ret != 0) {
  7758. ssl->error = ret;
  7759. WOLFSSL_ERROR(ssl->error);
  7760. return WOLFSSL_FATAL_ERROR;
  7761. }
  7762. switch (ssl->options.connectState) {
  7763. case CONNECT_BEGIN :
  7764. /* always send client hello first */
  7765. if ( (ssl->error = SendClientHello(ssl)) != 0) {
  7766. WOLFSSL_ERROR(ssl->error);
  7767. return WOLFSSL_FATAL_ERROR;
  7768. }
  7769. ssl->options.connectState = CLIENT_HELLO_SENT;
  7770. WOLFSSL_MSG("connect state: CLIENT_HELLO_SENT");
  7771. FALL_THROUGH;
  7772. case CLIENT_HELLO_SENT :
  7773. neededState = ssl->options.resuming ? SERVER_FINISHED_COMPLETE :
  7774. SERVER_HELLODONE_COMPLETE;
  7775. #ifdef WOLFSSL_DTLS
  7776. /* In DTLS, when resuming, we can go straight to FINISHED,
  7777. * or do a cookie exchange and then skip to FINISHED, assume
  7778. * we need the cookie exchange first. */
  7779. if (IsDtlsNotSctpMode(ssl))
  7780. neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
  7781. #endif
  7782. /* get response */
  7783. WOLFSSL_MSG("Server state up to needed state.");
  7784. while (ssl->options.serverState < neededState) {
  7785. WOLFSSL_MSG("Progressing server state...");
  7786. #ifdef WOLFSSL_TLS13
  7787. if (ssl->options.tls1_3)
  7788. return wolfSSL_connect_TLSv13(ssl);
  7789. #endif
  7790. WOLFSSL_MSG("ProcessReply...");
  7791. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  7792. WOLFSSL_ERROR(ssl->error);
  7793. return WOLFSSL_FATAL_ERROR;
  7794. }
  7795. /* if resumption failed, reset needed state */
  7796. else if (neededState == SERVER_FINISHED_COMPLETE) {
  7797. if (!ssl->options.resuming) {
  7798. #ifdef WOLFSSL_DTLS
  7799. if (IsDtlsNotSctpMode(ssl))
  7800. neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
  7801. else
  7802. #endif
  7803. neededState = SERVER_HELLODONE_COMPLETE;
  7804. }
  7805. }
  7806. WOLFSSL_MSG("ProcessReply done.");
  7807. #ifdef WOLFSSL_DTLS13
  7808. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version)
  7809. && ssl->dtls13Rtx.sendAcks == 1
  7810. && ssl->options.seenUnifiedHdr) {
  7811. /* we aren't negotiated the version yet, so we aren't sure
  7812. * the other end can speak v1.3. On the other side we have
  7813. * received a unified records, assuming that the
  7814. * ServerHello got lost, we will send an empty ACK. In case
  7815. * the server is a DTLS with version less than 1.3, it
  7816. * should just ignore the message */
  7817. ssl->dtls13Rtx.sendAcks = 0;
  7818. if ((ssl->error = SendDtls13Ack(ssl)) < 0) {
  7819. if (ssl->error == WANT_WRITE)
  7820. ssl->dtls13SendingAckOrRtx = 1;
  7821. WOLFSSL_ERROR(ssl->error);
  7822. return WOLFSSL_FATAL_ERROR;
  7823. }
  7824. }
  7825. #endif /* WOLFSSL_DTLS13 */
  7826. }
  7827. ssl->options.connectState = HELLO_AGAIN;
  7828. WOLFSSL_MSG("connect state: HELLO_AGAIN");
  7829. FALL_THROUGH;
  7830. case HELLO_AGAIN :
  7831. #ifdef WOLFSSL_TLS13
  7832. if (ssl->options.tls1_3)
  7833. return wolfSSL_connect_TLSv13(ssl);
  7834. #endif
  7835. #ifdef WOLFSSL_DTLS
  7836. if (ssl->options.serverState ==
  7837. SERVER_HELLOVERIFYREQUEST_COMPLETE) {
  7838. if (IsDtlsNotSctpMode(ssl)) {
  7839. /* re-init hashes, exclude first hello and verify request */
  7840. if ((ssl->error = InitHandshakeHashes(ssl)) != 0) {
  7841. WOLFSSL_ERROR(ssl->error);
  7842. return WOLFSSL_FATAL_ERROR;
  7843. }
  7844. if ( (ssl->error = SendClientHello(ssl)) != 0) {
  7845. WOLFSSL_ERROR(ssl->error);
  7846. return WOLFSSL_FATAL_ERROR;
  7847. }
  7848. }
  7849. }
  7850. #endif
  7851. ssl->options.connectState = HELLO_AGAIN_REPLY;
  7852. WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY");
  7853. FALL_THROUGH;
  7854. case HELLO_AGAIN_REPLY :
  7855. #ifdef WOLFSSL_DTLS
  7856. if (IsDtlsNotSctpMode(ssl)) {
  7857. neededState = ssl->options.resuming ?
  7858. SERVER_FINISHED_COMPLETE : SERVER_HELLODONE_COMPLETE;
  7859. /* get response */
  7860. while (ssl->options.serverState < neededState) {
  7861. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  7862. WOLFSSL_ERROR(ssl->error);
  7863. return WOLFSSL_FATAL_ERROR;
  7864. }
  7865. /* if resumption failed, reset needed state */
  7866. if (neededState == SERVER_FINISHED_COMPLETE) {
  7867. if (!ssl->options.resuming)
  7868. neededState = SERVER_HELLODONE_COMPLETE;
  7869. }
  7870. }
  7871. }
  7872. #endif
  7873. ssl->options.connectState = FIRST_REPLY_DONE;
  7874. WOLFSSL_MSG("connect state: FIRST_REPLY_DONE");
  7875. FALL_THROUGH;
  7876. case FIRST_REPLY_DONE :
  7877. if (ssl->options.certOnly)
  7878. return WOLFSSL_SUCCESS;
  7879. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH)
  7880. #ifdef WOLFSSL_TLS13
  7881. if (ssl->options.tls1_3)
  7882. return wolfSSL_connect_TLSv13(ssl);
  7883. #endif
  7884. if (ssl->options.sendVerify) {
  7885. if ( (ssl->error = SendCertificate(ssl)) != 0) {
  7886. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  7887. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  7888. #endif
  7889. WOLFSSL_ERROR(ssl->error);
  7890. return WOLFSSL_FATAL_ERROR;
  7891. }
  7892. WOLFSSL_MSG("sent: certificate");
  7893. }
  7894. #endif
  7895. ssl->options.connectState = FIRST_REPLY_FIRST;
  7896. WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST");
  7897. FALL_THROUGH;
  7898. case FIRST_REPLY_FIRST :
  7899. #ifdef WOLFSSL_TLS13
  7900. if (ssl->options.tls1_3)
  7901. return wolfSSL_connect_TLSv13(ssl);
  7902. #endif
  7903. if (!ssl->options.resuming) {
  7904. if ( (ssl->error = SendClientKeyExchange(ssl)) != 0) {
  7905. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  7906. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  7907. #endif
  7908. #ifdef WOLFSSL_EXTRA_ALERTS
  7909. if (ssl->error == NO_PEER_KEY ||
  7910. ssl->error == PSK_KEY_ERROR) {
  7911. SendAlert(ssl, alert_fatal, handshake_failure);
  7912. }
  7913. #endif
  7914. WOLFSSL_ERROR(ssl->error);
  7915. return WOLFSSL_FATAL_ERROR;
  7916. }
  7917. WOLFSSL_MSG("sent: client key exchange");
  7918. }
  7919. ssl->options.connectState = FIRST_REPLY_SECOND;
  7920. WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND");
  7921. FALL_THROUGH;
  7922. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  7923. case FIRST_REPLY_SECOND :
  7924. /* CLIENT: Fail-safe for Server Authentication. */
  7925. if (!ssl->options.peerAuthGood) {
  7926. WOLFSSL_MSG("Server authentication did not happen");
  7927. ssl->error = NO_PEER_VERIFY;
  7928. return WOLFSSL_FATAL_ERROR;
  7929. }
  7930. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH)
  7931. if (ssl->options.sendVerify) {
  7932. if ( (ssl->error = SendCertificateVerify(ssl)) != 0) {
  7933. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  7934. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  7935. #endif
  7936. WOLFSSL_ERROR(ssl->error);
  7937. return WOLFSSL_FATAL_ERROR;
  7938. }
  7939. WOLFSSL_MSG("sent: certificate verify");
  7940. }
  7941. #endif /* !NO_CERTS && !WOLFSSL_NO_CLIENT_AUTH */
  7942. ssl->options.connectState = FIRST_REPLY_THIRD;
  7943. WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD");
  7944. FALL_THROUGH;
  7945. case FIRST_REPLY_THIRD :
  7946. if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
  7947. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  7948. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  7949. #endif
  7950. WOLFSSL_ERROR(ssl->error);
  7951. return WOLFSSL_FATAL_ERROR;
  7952. }
  7953. WOLFSSL_MSG("sent: change cipher spec");
  7954. ssl->options.connectState = FIRST_REPLY_FOURTH;
  7955. WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH");
  7956. FALL_THROUGH;
  7957. case FIRST_REPLY_FOURTH :
  7958. if ( (ssl->error = SendFinished(ssl)) != 0) {
  7959. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  7960. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  7961. #endif
  7962. WOLFSSL_ERROR(ssl->error);
  7963. return WOLFSSL_FATAL_ERROR;
  7964. }
  7965. WOLFSSL_MSG("sent: finished");
  7966. ssl->options.connectState = FINISHED_DONE;
  7967. WOLFSSL_MSG("connect state: FINISHED_DONE");
  7968. FALL_THROUGH;
  7969. #ifdef WOLFSSL_DTLS13
  7970. case WAIT_FINISHED_ACK:
  7971. ssl->options.connectState = FINISHED_DONE;
  7972. FALL_THROUGH;
  7973. #endif /* WOLFSSL_DTLS13 */
  7974. case FINISHED_DONE :
  7975. /* get response */
  7976. while (ssl->options.serverState < SERVER_FINISHED_COMPLETE)
  7977. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  7978. WOLFSSL_ERROR(ssl->error);
  7979. return WOLFSSL_FATAL_ERROR;
  7980. }
  7981. ssl->options.connectState = SECOND_REPLY_DONE;
  7982. WOLFSSL_MSG("connect state: SECOND_REPLY_DONE");
  7983. FALL_THROUGH;
  7984. case SECOND_REPLY_DONE:
  7985. #ifndef NO_HANDSHAKE_DONE_CB
  7986. if (ssl->hsDoneCb) {
  7987. int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
  7988. if (cbret < 0) {
  7989. ssl->error = cbret;
  7990. WOLFSSL_MSG("HandShake Done Cb don't continue error");
  7991. return WOLFSSL_FATAL_ERROR;
  7992. }
  7993. }
  7994. #endif /* NO_HANDSHAKE_DONE_CB */
  7995. if (!ssl->options.dtls) {
  7996. if (!ssl->options.keepResources) {
  7997. FreeHandshakeResources(ssl);
  7998. }
  7999. }
  8000. #ifdef WOLFSSL_DTLS
  8001. else {
  8002. ssl->options.dtlsHsRetain = 1;
  8003. }
  8004. #endif /* WOLFSSL_DTLS */
  8005. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SECURE_RENEGOTIATION)
  8006. /* This may be necessary in async so that we don't try to
  8007. * renegotiate again */
  8008. if (ssl->secure_renegotiation &&
  8009. ssl->secure_renegotiation->startScr) {
  8010. ssl->secure_renegotiation->startScr = 0;
  8011. }
  8012. #endif /* WOLFSSL_ASYNC_CRYPT && HAVE_SECURE_RENEGOTIATION */
  8013. #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT)
  8014. /* Free the remaining async context if not using it for crypto */
  8015. FreeAsyncCtx(ssl, 1);
  8016. #endif
  8017. ssl->error = 0; /* clear the error */
  8018. WOLFSSL_LEAVE("wolfSSL_connect", WOLFSSL_SUCCESS);
  8019. return WOLFSSL_SUCCESS;
  8020. #endif /* !WOLFSSL_NO_TLS12 || !NO_OLD_TLS */
  8021. default:
  8022. WOLFSSL_MSG("Unknown connect state ERROR");
  8023. return WOLFSSL_FATAL_ERROR; /* unknown connect state */
  8024. }
  8025. #endif /* !WOLFSSL_NO_TLS12 || !NO_OLD_TLS || !WOLFSSL_TLS13 */
  8026. }
  8027. #endif /* NO_WOLFSSL_CLIENT */
  8028. /* server only parts */
  8029. #ifndef NO_WOLFSSL_SERVER
  8030. #if defined(OPENSSL_EXTRA) && !defined(NO_OLD_TLS)
  8031. WOLFSSL_METHOD* wolfSSLv2_server_method(void)
  8032. {
  8033. WOLFSSL_STUB("wolfSSLv2_server_method");
  8034. return 0;
  8035. }
  8036. #endif
  8037. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  8038. WOLFSSL_METHOD* wolfSSLv3_server_method(void)
  8039. {
  8040. return wolfSSLv3_server_method_ex(NULL);
  8041. }
  8042. WOLFSSL_METHOD* wolfSSLv3_server_method_ex(void* heap)
  8043. {
  8044. WOLFSSL_METHOD* method =
  8045. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  8046. heap, DYNAMIC_TYPE_METHOD);
  8047. (void)heap;
  8048. WOLFSSL_ENTER("wolfSSLv3_server_method_ex");
  8049. if (method) {
  8050. InitSSL_Method(method, MakeSSLv3());
  8051. method->side = WOLFSSL_SERVER_END;
  8052. }
  8053. return method;
  8054. }
  8055. #endif /* WOLFSSL_ALLOW_SSLV3 && !NO_OLD_TLS */
  8056. WOLFSSL_METHOD* wolfSSLv23_server_method(void)
  8057. {
  8058. return wolfSSLv23_server_method_ex(NULL);
  8059. }
  8060. WOLFSSL_METHOD* wolfSSLv23_server_method_ex(void* heap)
  8061. {
  8062. WOLFSSL_METHOD* method =
  8063. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  8064. heap, DYNAMIC_TYPE_METHOD);
  8065. (void)heap;
  8066. WOLFSSL_ENTER("wolfSSLv23_server_method_ex");
  8067. if (method) {
  8068. #if !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || \
  8069. defined(WOLFSSL_SHA512)
  8070. #ifdef WOLFSSL_TLS13
  8071. InitSSL_Method(method, MakeTLSv1_3());
  8072. #elif !defined(WOLFSSL_NO_TLS12)
  8073. InitSSL_Method(method, MakeTLSv1_2());
  8074. #elif !defined(NO_OLD_TLS)
  8075. InitSSL_Method(method, MakeTLSv1_1());
  8076. #endif
  8077. #else
  8078. #ifndef NO_OLD_TLS
  8079. InitSSL_Method(method, MakeTLSv1_1());
  8080. #else
  8081. #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
  8082. #endif
  8083. #endif
  8084. #if !defined(NO_OLD_TLS) || defined(WOLFSSL_TLS13)
  8085. method->downgrade = 1;
  8086. #endif
  8087. method->side = WOLFSSL_SERVER_END;
  8088. }
  8089. return method;
  8090. }
  8091. WOLFSSL_ABI
  8092. int wolfSSL_accept(WOLFSSL* ssl)
  8093. {
  8094. #if !(defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS) && \
  8095. defined(WOLFSSL_TLS13))
  8096. word16 havePSK = 0;
  8097. word16 haveAnon = 0;
  8098. word16 haveMcast = 0;
  8099. #endif
  8100. int ret = 0;
  8101. (void)ret;
  8102. if (ssl == NULL)
  8103. return WOLFSSL_FATAL_ERROR;
  8104. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  8105. if (ssl->options.side == WOLFSSL_NEITHER_END) {
  8106. WOLFSSL_MSG("Setting WOLFSSL_SSL to be server side");
  8107. ssl->error = InitSSL_Side(ssl, WOLFSSL_SERVER_END);
  8108. if (ssl->error != WOLFSSL_SUCCESS) {
  8109. WOLFSSL_ERROR(ssl->error);
  8110. return WOLFSSL_FATAL_ERROR;
  8111. }
  8112. ssl->error = 0; /* expected to be zero here */
  8113. }
  8114. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  8115. #if defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS) && defined(WOLFSSL_TLS13)
  8116. return wolfSSL_accept_TLSv13(ssl);
  8117. #else
  8118. #ifdef WOLFSSL_TLS13
  8119. if (ssl->options.tls1_3)
  8120. return wolfSSL_accept_TLSv13(ssl);
  8121. #endif
  8122. WOLFSSL_ENTER("wolfSSL_accept");
  8123. /* make sure this wolfSSL object has arrays and rng setup. Protects
  8124. * case where the WOLFSSL object is reused via wolfSSL_clear() */
  8125. if ((ret = ReinitSSL(ssl, ssl->ctx, 0)) != 0) {
  8126. return ret;
  8127. }
  8128. #ifdef WOLFSSL_WOLFSENTRY_HOOKS
  8129. if ((ssl->AcceptFilter != NULL) &&
  8130. ((ssl->options.acceptState == ACCEPT_BEGIN)
  8131. #ifdef HAVE_SECURE_RENEGOTIATION
  8132. || (ssl->options.acceptState == ACCEPT_BEGIN_RENEG)
  8133. #endif
  8134. ))
  8135. {
  8136. wolfSSL_netfilter_decision_t res;
  8137. if ((ssl->AcceptFilter(ssl, ssl->AcceptFilter_arg, &res) ==
  8138. WOLFSSL_SUCCESS) &&
  8139. (res == WOLFSSL_NETFILTER_REJECT)) {
  8140. ssl->error = SOCKET_FILTERED_E;
  8141. WOLFSSL_ERROR(ssl->error);
  8142. return WOLFSSL_FATAL_ERROR;
  8143. }
  8144. }
  8145. #endif /* WOLFSSL_WOLFSENTRY_HOOKS */
  8146. #ifdef HAVE_ERRNO_H
  8147. errno = 0;
  8148. #endif
  8149. #ifndef NO_PSK
  8150. havePSK = ssl->options.havePSK;
  8151. #endif
  8152. (void)havePSK;
  8153. #ifdef HAVE_ANON
  8154. haveAnon = ssl->options.useAnon;
  8155. #endif
  8156. (void)haveAnon;
  8157. #ifdef WOLFSSL_MULTICAST
  8158. haveMcast = ssl->options.haveMcast;
  8159. #endif
  8160. (void)haveMcast;
  8161. if (ssl->options.side != WOLFSSL_SERVER_END) {
  8162. ssl->error = SIDE_ERROR;
  8163. WOLFSSL_ERROR(ssl->error);
  8164. return WOLFSSL_FATAL_ERROR;
  8165. }
  8166. #ifndef NO_CERTS
  8167. /* in case used set_accept_state after init */
  8168. if (!havePSK && !haveAnon && !haveMcast) {
  8169. #ifdef OPENSSL_EXTRA
  8170. if (ssl->ctx->certSetupCb != NULL) {
  8171. WOLFSSL_MSG("CertSetupCb set. server cert and "
  8172. "key not checked");
  8173. }
  8174. else
  8175. #endif
  8176. {
  8177. if (!ssl->buffers.certificate ||
  8178. !ssl->buffers.certificate->buffer) {
  8179. WOLFSSL_MSG("accept error: server cert required");
  8180. ssl->error = NO_PRIVATE_KEY;
  8181. WOLFSSL_ERROR(ssl->error);
  8182. return WOLFSSL_FATAL_ERROR;
  8183. }
  8184. if (!ssl->buffers.key || !ssl->buffers.key->buffer) {
  8185. /* allow no private key if using existing key */
  8186. #ifdef WOLF_PRIVATE_KEY_ID
  8187. if (ssl->devId != INVALID_DEVID
  8188. #ifdef HAVE_PK_CALLBACKS
  8189. || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)
  8190. #endif
  8191. ) {
  8192. WOLFSSL_MSG("Allowing no server private key "
  8193. "(external)");
  8194. }
  8195. else
  8196. #endif
  8197. {
  8198. WOLFSSL_MSG("accept error: server key required");
  8199. ssl->error = NO_PRIVATE_KEY;
  8200. WOLFSSL_ERROR(ssl->error);
  8201. return WOLFSSL_FATAL_ERROR;
  8202. }
  8203. }
  8204. }
  8205. }
  8206. #endif
  8207. #ifdef WOLFSSL_DTLS
  8208. if (ssl->version.major == DTLS_MAJOR) {
  8209. ssl->options.dtls = 1;
  8210. ssl->options.tls = 1;
  8211. ssl->options.tls1_1 = 1;
  8212. if (!IsDtlsNotSctpMode(ssl) || !IsDtlsNotSrtpMode(ssl) ||
  8213. IsSCR(ssl))
  8214. ssl->options.dtlsStateful = 1;
  8215. }
  8216. #endif
  8217. if (ssl->buffers.outputBuffer.length > 0
  8218. #ifdef WOLFSSL_ASYNC_CRYPT
  8219. /* do not send buffered or advance state if last error was an
  8220. async pending operation */
  8221. && ssl->error != WC_PENDING_E
  8222. #endif
  8223. ) {
  8224. ret = SendBuffered(ssl);
  8225. if (ret == 0) {
  8226. /* fragOffset is non-zero when sending fragments. On the last
  8227. * fragment, fragOffset is zero again, and the state can be
  8228. * advanced. */
  8229. if (ssl->fragOffset == 0 && !ssl->options.buildingMsg) {
  8230. if (ssl->options.acceptState == ACCEPT_FIRST_REPLY_DONE ||
  8231. ssl->options.acceptState == SERVER_HELLO_SENT ||
  8232. ssl->options.acceptState == CERT_SENT ||
  8233. ssl->options.acceptState == CERT_STATUS_SENT ||
  8234. ssl->options.acceptState == KEY_EXCHANGE_SENT ||
  8235. ssl->options.acceptState == CERT_REQ_SENT ||
  8236. ssl->options.acceptState == ACCEPT_SECOND_REPLY_DONE ||
  8237. ssl->options.acceptState == TICKET_SENT ||
  8238. ssl->options.acceptState == CHANGE_CIPHER_SENT) {
  8239. ssl->options.acceptState++;
  8240. WOLFSSL_MSG("accept state: Advanced from last "
  8241. "buffered fragment send");
  8242. #ifdef WOLFSSL_ASYNC_IO
  8243. /* Cleanup async */
  8244. FreeAsyncCtx(ssl, 0);
  8245. #endif
  8246. }
  8247. }
  8248. else {
  8249. WOLFSSL_MSG("accept state: "
  8250. "Not advanced, more fragments to send");
  8251. }
  8252. }
  8253. else {
  8254. ssl->error = ret;
  8255. WOLFSSL_ERROR(ssl->error);
  8256. return WOLFSSL_FATAL_ERROR;
  8257. }
  8258. #ifdef WOLFSSL_DTLS13
  8259. if (ssl->options.dtls)
  8260. ssl->dtls13SendingAckOrRtx = 0;
  8261. #endif /* WOLFSSL_DTLS13 */
  8262. }
  8263. ret = RetrySendAlert(ssl);
  8264. if (ret != 0) {
  8265. ssl->error = ret;
  8266. WOLFSSL_ERROR(ssl->error);
  8267. return WOLFSSL_FATAL_ERROR;
  8268. }
  8269. switch (ssl->options.acceptState) {
  8270. case ACCEPT_BEGIN :
  8271. #ifdef HAVE_SECURE_RENEGOTIATION
  8272. case ACCEPT_BEGIN_RENEG:
  8273. #endif
  8274. /* get response */
  8275. while (ssl->options.clientState < CLIENT_HELLO_COMPLETE)
  8276. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  8277. WOLFSSL_ERROR(ssl->error);
  8278. return WOLFSSL_FATAL_ERROR;
  8279. }
  8280. #ifdef WOLFSSL_TLS13
  8281. ssl->options.acceptState = ACCEPT_CLIENT_HELLO_DONE;
  8282. WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE");
  8283. FALL_THROUGH;
  8284. case ACCEPT_CLIENT_HELLO_DONE :
  8285. if (ssl->options.tls1_3) {
  8286. return wolfSSL_accept_TLSv13(ssl);
  8287. }
  8288. #endif
  8289. ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE;
  8290. WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
  8291. FALL_THROUGH;
  8292. case ACCEPT_FIRST_REPLY_DONE :
  8293. if ( (ssl->error = SendServerHello(ssl)) != 0) {
  8294. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8295. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  8296. #endif
  8297. WOLFSSL_ERROR(ssl->error);
  8298. return WOLFSSL_FATAL_ERROR;
  8299. }
  8300. ssl->options.acceptState = SERVER_HELLO_SENT;
  8301. WOLFSSL_MSG("accept state SERVER_HELLO_SENT");
  8302. FALL_THROUGH;
  8303. case SERVER_HELLO_SENT :
  8304. #ifdef WOLFSSL_TLS13
  8305. if (ssl->options.tls1_3) {
  8306. return wolfSSL_accept_TLSv13(ssl);
  8307. }
  8308. #endif
  8309. #ifndef NO_CERTS
  8310. if (!ssl->options.resuming)
  8311. if ( (ssl->error = SendCertificate(ssl)) != 0) {
  8312. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8313. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  8314. #endif
  8315. WOLFSSL_ERROR(ssl->error);
  8316. return WOLFSSL_FATAL_ERROR;
  8317. }
  8318. #endif
  8319. ssl->options.acceptState = CERT_SENT;
  8320. WOLFSSL_MSG("accept state CERT_SENT");
  8321. FALL_THROUGH;
  8322. case CERT_SENT :
  8323. #ifndef NO_CERTS
  8324. if (!ssl->options.resuming)
  8325. if ( (ssl->error = SendCertificateStatus(ssl)) != 0) {
  8326. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8327. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  8328. #endif
  8329. WOLFSSL_ERROR(ssl->error);
  8330. return WOLFSSL_FATAL_ERROR;
  8331. }
  8332. #endif
  8333. ssl->options.acceptState = CERT_STATUS_SENT;
  8334. WOLFSSL_MSG("accept state CERT_STATUS_SENT");
  8335. FALL_THROUGH;
  8336. case CERT_STATUS_SENT :
  8337. #ifdef WOLFSSL_TLS13
  8338. if (ssl->options.tls1_3) {
  8339. return wolfSSL_accept_TLSv13(ssl);
  8340. }
  8341. #endif
  8342. if (!ssl->options.resuming)
  8343. if ( (ssl->error = SendServerKeyExchange(ssl)) != 0) {
  8344. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8345. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  8346. #endif
  8347. WOLFSSL_ERROR(ssl->error);
  8348. return WOLFSSL_FATAL_ERROR;
  8349. }
  8350. ssl->options.acceptState = KEY_EXCHANGE_SENT;
  8351. WOLFSSL_MSG("accept state KEY_EXCHANGE_SENT");
  8352. FALL_THROUGH;
  8353. case KEY_EXCHANGE_SENT :
  8354. #ifndef NO_CERTS
  8355. if (!ssl->options.resuming) {
  8356. if (ssl->options.verifyPeer) {
  8357. if ( (ssl->error = SendCertificateRequest(ssl)) != 0) {
  8358. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8359. /* See if an alert was sent. */
  8360. ProcessReplyEx(ssl, 1);
  8361. #endif
  8362. WOLFSSL_ERROR(ssl->error);
  8363. return WOLFSSL_FATAL_ERROR;
  8364. }
  8365. }
  8366. else {
  8367. /* SERVER: Peer auth good if not verifying client. */
  8368. ssl->options.peerAuthGood = 1;
  8369. }
  8370. }
  8371. #endif
  8372. ssl->options.acceptState = CERT_REQ_SENT;
  8373. WOLFSSL_MSG("accept state CERT_REQ_SENT");
  8374. FALL_THROUGH;
  8375. case CERT_REQ_SENT :
  8376. if (!ssl->options.resuming)
  8377. if ( (ssl->error = SendServerHelloDone(ssl)) != 0) {
  8378. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8379. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  8380. #endif
  8381. WOLFSSL_ERROR(ssl->error);
  8382. return WOLFSSL_FATAL_ERROR;
  8383. }
  8384. ssl->options.acceptState = SERVER_HELLO_DONE;
  8385. WOLFSSL_MSG("accept state SERVER_HELLO_DONE");
  8386. FALL_THROUGH;
  8387. case SERVER_HELLO_DONE :
  8388. if (!ssl->options.resuming) {
  8389. while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
  8390. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  8391. WOLFSSL_ERROR(ssl->error);
  8392. return WOLFSSL_FATAL_ERROR;
  8393. }
  8394. }
  8395. ssl->options.acceptState = ACCEPT_SECOND_REPLY_DONE;
  8396. WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
  8397. FALL_THROUGH;
  8398. case ACCEPT_SECOND_REPLY_DONE :
  8399. #ifndef NO_CERTS
  8400. /* SERVER: When not resuming and verifying peer but no certificate
  8401. * received and not failing when not received then peer auth good.
  8402. */
  8403. if (!ssl->options.resuming && ssl->options.verifyPeer &&
  8404. !ssl->options.havePeerCert && !ssl->options.failNoCert) {
  8405. ssl->options.peerAuthGood = 1;
  8406. }
  8407. #endif /* !NO_CERTS */
  8408. #ifdef WOLFSSL_NO_CLIENT_AUTH
  8409. if (!ssl->options.resuming) {
  8410. ssl->options.peerAuthGood = 1;
  8411. }
  8412. #endif
  8413. #ifdef HAVE_SESSION_TICKET
  8414. if (ssl->options.createTicket && !ssl->options.noTicketTls12) {
  8415. if ( (ssl->error = SendTicket(ssl)) != 0) {
  8416. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8417. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  8418. #endif
  8419. WOLFSSL_MSG("Thought we need ticket but failed");
  8420. WOLFSSL_ERROR(ssl->error);
  8421. return WOLFSSL_FATAL_ERROR;
  8422. }
  8423. }
  8424. #endif /* HAVE_SESSION_TICKET */
  8425. ssl->options.acceptState = TICKET_SENT;
  8426. WOLFSSL_MSG("accept state TICKET_SENT");
  8427. FALL_THROUGH;
  8428. case TICKET_SENT:
  8429. /* SERVER: Fail-safe for CLient Authentication. */
  8430. if (!ssl->options.peerAuthGood) {
  8431. WOLFSSL_MSG("Client authentication did not happen");
  8432. return WOLFSSL_FATAL_ERROR;
  8433. }
  8434. if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
  8435. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8436. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  8437. #endif
  8438. WOLFSSL_ERROR(ssl->error);
  8439. return WOLFSSL_FATAL_ERROR;
  8440. }
  8441. ssl->options.acceptState = CHANGE_CIPHER_SENT;
  8442. WOLFSSL_MSG("accept state CHANGE_CIPHER_SENT");
  8443. FALL_THROUGH;
  8444. case CHANGE_CIPHER_SENT :
  8445. if ( (ssl->error = SendFinished(ssl)) != 0) {
  8446. #ifdef WOLFSSL_CHECK_ALERT_ON_ERR
  8447. ProcessReplyEx(ssl, 1); /* See if an alert was sent. */
  8448. #endif
  8449. WOLFSSL_ERROR(ssl->error);
  8450. return WOLFSSL_FATAL_ERROR;
  8451. }
  8452. ssl->options.acceptState = ACCEPT_FINISHED_DONE;
  8453. WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE");
  8454. FALL_THROUGH;
  8455. case ACCEPT_FINISHED_DONE :
  8456. if (ssl->options.resuming) {
  8457. while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) {
  8458. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  8459. WOLFSSL_ERROR(ssl->error);
  8460. return WOLFSSL_FATAL_ERROR;
  8461. }
  8462. }
  8463. }
  8464. ssl->options.acceptState = ACCEPT_THIRD_REPLY_DONE;
  8465. WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE");
  8466. FALL_THROUGH;
  8467. case ACCEPT_THIRD_REPLY_DONE :
  8468. #ifndef NO_HANDSHAKE_DONE_CB
  8469. if (ssl->hsDoneCb) {
  8470. int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
  8471. if (cbret < 0) {
  8472. ssl->error = cbret;
  8473. WOLFSSL_MSG("HandShake Done Cb don't continue error");
  8474. return WOLFSSL_FATAL_ERROR;
  8475. }
  8476. }
  8477. #endif /* NO_HANDSHAKE_DONE_CB */
  8478. if (!ssl->options.dtls) {
  8479. if (!ssl->options.keepResources) {
  8480. FreeHandshakeResources(ssl);
  8481. }
  8482. }
  8483. #ifdef WOLFSSL_DTLS
  8484. else {
  8485. ssl->options.dtlsHsRetain = 1;
  8486. }
  8487. #endif /* WOLFSSL_DTLS */
  8488. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_SECURE_RENEGOTIATION)
  8489. /* This may be necessary in async so that we don't try to
  8490. * renegotiate again */
  8491. if (ssl->secure_renegotiation &&
  8492. ssl->secure_renegotiation->startScr) {
  8493. ssl->secure_renegotiation->startScr = 0;
  8494. }
  8495. #endif /* WOLFSSL_ASYNC_CRYPT && HAVE_SECURE_RENEGOTIATION */
  8496. #if defined(WOLFSSL_ASYNC_IO) && !defined(WOLFSSL_ASYNC_CRYPT)
  8497. /* Free the remaining async context if not using it for crypto */
  8498. FreeAsyncCtx(ssl, 1);
  8499. #endif
  8500. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  8501. if (ssl->dtls_export) {
  8502. if ((ssl->error = wolfSSL_send_session(ssl)) != 0) {
  8503. WOLFSSL_MSG("Export DTLS session error");
  8504. WOLFSSL_ERROR(ssl->error);
  8505. return WOLFSSL_FATAL_ERROR;
  8506. }
  8507. }
  8508. #endif
  8509. ssl->error = 0; /* clear the error */
  8510. WOLFSSL_LEAVE("wolfSSL_accept", WOLFSSL_SUCCESS);
  8511. return WOLFSSL_SUCCESS;
  8512. default :
  8513. WOLFSSL_MSG("Unknown accept state ERROR");
  8514. return WOLFSSL_FATAL_ERROR;
  8515. }
  8516. #endif /* !WOLFSSL_NO_TLS12 */
  8517. }
  8518. #endif /* NO_WOLFSSL_SERVER */
  8519. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  8520. int wolfDTLS_SetChGoodCb(WOLFSSL* ssl, ClientHelloGoodCb cb, void* user_ctx)
  8521. {
  8522. WOLFSSL_ENTER("wolfDTLS_SetChGoodCb");
  8523. if (ssl == NULL)
  8524. return BAD_FUNC_ARG;
  8525. ssl->chGoodCb = cb;
  8526. ssl->chGoodCtx = user_ctx;
  8527. return WOLFSSL_SUCCESS;
  8528. }
  8529. #endif
  8530. #ifndef NO_HANDSHAKE_DONE_CB
  8531. int wolfSSL_SetHsDoneCb(WOLFSSL* ssl, HandShakeDoneCb cb, void* user_ctx)
  8532. {
  8533. WOLFSSL_ENTER("wolfSSL_SetHsDoneCb");
  8534. if (ssl == NULL)
  8535. return BAD_FUNC_ARG;
  8536. ssl->hsDoneCb = cb;
  8537. ssl->hsDoneCtx = user_ctx;
  8538. return WOLFSSL_SUCCESS;
  8539. }
  8540. #endif /* NO_HANDSHAKE_DONE_CB */
  8541. WOLFSSL_ABI
  8542. int wolfSSL_Cleanup(void)
  8543. {
  8544. int ret = WOLFSSL_SUCCESS; /* Only the first error will be returned */
  8545. int release = 0;
  8546. #if !defined(NO_SESSION_CACHE)
  8547. int i;
  8548. int j;
  8549. #endif
  8550. WOLFSSL_ENTER("wolfSSL_Cleanup");
  8551. #ifndef WOLFSSL_MUTEX_INITIALIZER
  8552. if (inits_count_mutex_valid == 1) {
  8553. #endif
  8554. if (wc_LockMutex(&inits_count_mutex) != 0) {
  8555. WOLFSSL_MSG("Bad Lock Mutex count");
  8556. return BAD_MUTEX_E;
  8557. }
  8558. #ifndef WOLFSSL_MUTEX_INITIALIZER
  8559. }
  8560. #endif
  8561. if (initRefCount > 0) {
  8562. --initRefCount;
  8563. if (initRefCount == 0)
  8564. release = 1;
  8565. }
  8566. #ifndef WOLFSSL_MUTEX_INITIALIZER
  8567. if (inits_count_mutex_valid == 1) {
  8568. #endif
  8569. wc_UnLockMutex(&inits_count_mutex);
  8570. #ifndef WOLFSSL_MUTEX_INITIALIZER
  8571. }
  8572. #endif
  8573. if (!release)
  8574. return ret;
  8575. #ifdef OPENSSL_EXTRA
  8576. wolfSSL_BN_free_one();
  8577. #endif
  8578. #ifndef NO_SESSION_CACHE
  8579. #ifdef ENABLE_SESSION_CACHE_ROW_LOCK
  8580. for (i = 0; i < SESSION_ROWS; ++i) {
  8581. if ((SessionCache[i].lock_valid == 1) &&
  8582. (wc_FreeRwLock(&SessionCache[i].row_lock) != 0)) {
  8583. if (ret == WOLFSSL_SUCCESS)
  8584. ret = BAD_MUTEX_E;
  8585. }
  8586. SessionCache[i].lock_valid = 0;
  8587. }
  8588. #else
  8589. if ((session_lock_valid == 1) && (wc_FreeRwLock(&session_lock) != 0)) {
  8590. if (ret == WOLFSSL_SUCCESS)
  8591. ret = BAD_MUTEX_E;
  8592. }
  8593. session_lock_valid = 0;
  8594. #endif
  8595. for (i = 0; i < SESSION_ROWS; i++) {
  8596. for (j = 0; j < SESSIONS_PER_ROW; j++) {
  8597. #ifdef SESSION_CACHE_DYNAMIC_MEM
  8598. if (SessionCache[i].Sessions[j]) {
  8599. EvictSessionFromCache(SessionCache[i].Sessions[j]);
  8600. XFREE(SessionCache[i].Sessions[j], SessionCache[i].heap,
  8601. DYNAMIC_TYPE_SESSION);
  8602. SessionCache[i].Sessions[j] = NULL;
  8603. }
  8604. #else
  8605. EvictSessionFromCache(&SessionCache[i].Sessions[j]);
  8606. #endif
  8607. }
  8608. }
  8609. #ifndef NO_CLIENT_CACHE
  8610. #ifndef WOLFSSL_MUTEX_INITIALIZER
  8611. if ((clisession_mutex_valid == 1) &&
  8612. (wc_FreeMutex(&clisession_mutex) != 0)) {
  8613. if (ret == WOLFSSL_SUCCESS)
  8614. ret = BAD_MUTEX_E;
  8615. }
  8616. clisession_mutex_valid = 0;
  8617. #endif
  8618. #endif
  8619. #endif /* !NO_SESSION_CACHE */
  8620. #ifndef WOLFSSL_MUTEX_INITIALIZER
  8621. if ((inits_count_mutex_valid == 1) &&
  8622. (wc_FreeMutex(&inits_count_mutex) != 0)) {
  8623. if (ret == WOLFSSL_SUCCESS)
  8624. ret = BAD_MUTEX_E;
  8625. }
  8626. inits_count_mutex_valid = 0;
  8627. #endif
  8628. #ifdef OPENSSL_EXTRA
  8629. wolfSSL_RAND_Cleanup();
  8630. #endif
  8631. if (wolfCrypt_Cleanup() != 0) {
  8632. WOLFSSL_MSG("Error with wolfCrypt_Cleanup call");
  8633. if (ret == WOLFSSL_SUCCESS)
  8634. ret = WC_CLEANUP_E;
  8635. }
  8636. #if FIPS_VERSION_GE(5,1)
  8637. if (wolfCrypt_SetPrivateKeyReadEnable_fips(0, WC_KEYTYPE_ALL) < 0) {
  8638. if (ret == WOLFSSL_SUCCESS)
  8639. ret = WC_CLEANUP_E;
  8640. }
  8641. #endif
  8642. #ifdef HAVE_GLOBAL_RNG
  8643. #ifndef WOLFSSL_MUTEX_INITIALIZER
  8644. if ((globalRNGMutex_valid == 1) && (wc_FreeMutex(&globalRNGMutex) != 0)) {
  8645. if (ret == WOLFSSL_SUCCESS)
  8646. ret = BAD_MUTEX_E;
  8647. }
  8648. globalRNGMutex_valid = 0;
  8649. #endif /* !WOLFSSL_MUTEX_INITIALIZER */
  8650. #if defined(OPENSSL_EXTRA) && defined(HAVE_HASHDRBG)
  8651. wolfSSL_FIPS_drbg_free(gDrbgDefCtx);
  8652. gDrbgDefCtx = NULL;
  8653. #endif
  8654. #endif
  8655. #if defined(HAVE_EX_DATA) && \
  8656. (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  8657. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  8658. defined(HAVE_LIGHTY)) || defined(HAVE_EX_DATA) || \
  8659. defined(WOLFSSL_WPAS_SMALL)
  8660. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  8661. crypto_ex_cb_ctx_session = NULL;
  8662. #endif
  8663. #ifdef WOLFSSL_MEM_FAIL_COUNT
  8664. wc_MemFailCount_Free();
  8665. #endif
  8666. return ret;
  8667. }
  8668. /* call before SSL_connect, if verifying will add name check to
  8669. date check and signature check */
  8670. WOLFSSL_ABI
  8671. int wolfSSL_check_domain_name(WOLFSSL* ssl, const char* dn)
  8672. {
  8673. WOLFSSL_ENTER("wolfSSL_check_domain_name");
  8674. if (ssl == NULL || dn == NULL) {
  8675. WOLFSSL_MSG("Bad function argument: NULL");
  8676. return WOLFSSL_FAILURE;
  8677. }
  8678. if (ssl->buffers.domainName.buffer)
  8679. XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  8680. ssl->buffers.domainName.length = (word32)XSTRLEN(dn);
  8681. ssl->buffers.domainName.buffer = (byte*)XMALLOC(
  8682. ssl->buffers.domainName.length + 1, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  8683. if (ssl->buffers.domainName.buffer) {
  8684. unsigned char* domainName = ssl->buffers.domainName.buffer;
  8685. XMEMCPY(domainName, dn, ssl->buffers.domainName.length);
  8686. domainName[ssl->buffers.domainName.length] = '\0';
  8687. return WOLFSSL_SUCCESS;
  8688. }
  8689. else {
  8690. ssl->error = MEMORY_ERROR;
  8691. return WOLFSSL_FAILURE;
  8692. }
  8693. }
  8694. /* turn on wolfSSL zlib compression
  8695. returns WOLFSSL_SUCCESS for success, else error (not built in)
  8696. */
  8697. int wolfSSL_set_compression(WOLFSSL* ssl)
  8698. {
  8699. WOLFSSL_ENTER("wolfSSL_set_compression");
  8700. (void)ssl;
  8701. #ifdef HAVE_LIBZ
  8702. ssl->options.usingCompression = 1;
  8703. return WOLFSSL_SUCCESS;
  8704. #else
  8705. return NOT_COMPILED_IN;
  8706. #endif
  8707. }
  8708. #ifndef USE_WINDOWS_API
  8709. #ifndef NO_WRITEV
  8710. /* simulate writev semantics, doesn't actually do block at a time though
  8711. because of SSL_write behavior and because front adds may be small */
  8712. int wolfSSL_writev(WOLFSSL* ssl, const struct iovec* iov, int iovcnt)
  8713. {
  8714. #ifdef WOLFSSL_SMALL_STACK
  8715. byte staticBuffer[1]; /* force heap usage */
  8716. #else
  8717. byte staticBuffer[FILE_BUFFER_SIZE];
  8718. #endif
  8719. byte* myBuffer = staticBuffer;
  8720. int dynamic = 0;
  8721. int sending = 0;
  8722. int idx = 0;
  8723. int i;
  8724. int ret;
  8725. WOLFSSL_ENTER("wolfSSL_writev");
  8726. for (i = 0; i < iovcnt; i++)
  8727. sending += (int)iov[i].iov_len;
  8728. if (sending > (int)sizeof(staticBuffer)) {
  8729. myBuffer = (byte*)XMALLOC(sending, ssl->heap,
  8730. DYNAMIC_TYPE_WRITEV);
  8731. if (!myBuffer)
  8732. return MEMORY_ERROR;
  8733. dynamic = 1;
  8734. }
  8735. for (i = 0; i < iovcnt; i++) {
  8736. XMEMCPY(&myBuffer[idx], iov[i].iov_base, iov[i].iov_len);
  8737. idx += (int)iov[i].iov_len;
  8738. }
  8739. /* myBuffer may not be initialized fully, but the span up to the
  8740. * sending length will be.
  8741. */
  8742. PRAGMA_GCC_DIAG_PUSH
  8743. PRAGMA_GCC("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
  8744. ret = wolfSSL_write(ssl, myBuffer, sending);
  8745. PRAGMA_GCC_DIAG_POP
  8746. if (dynamic)
  8747. XFREE(myBuffer, ssl->heap, DYNAMIC_TYPE_WRITEV);
  8748. return ret;
  8749. }
  8750. #endif
  8751. #endif
  8752. #ifdef WOLFSSL_CALLBACKS
  8753. typedef struct itimerval Itimerval;
  8754. /* don't keep calling simple functions while setting up timer and signals
  8755. if no inlining these are the next best */
  8756. #define AddTimes(a, b, c) \
  8757. do { \
  8758. (c).tv_sec = (a).tv_sec + (b).tv_sec; \
  8759. (c).tv_usec = (a).tv_usec + (b).tv_usec;\
  8760. if ((c).tv_usec >= 1000000) { \
  8761. (c).tv_sec++; \
  8762. (c).tv_usec -= 1000000; \
  8763. } \
  8764. } while (0)
  8765. #define SubtractTimes(a, b, c) \
  8766. do { \
  8767. (c).tv_sec = (a).tv_sec - (b).tv_sec; \
  8768. (c).tv_usec = (a).tv_usec - (b).tv_usec;\
  8769. if ((c).tv_usec < 0) { \
  8770. (c).tv_sec--; \
  8771. (c).tv_usec += 1000000; \
  8772. } \
  8773. } while (0)
  8774. #define CmpTimes(a, b, cmp) \
  8775. (((a).tv_sec == (b).tv_sec) ? \
  8776. ((a).tv_usec cmp (b).tv_usec) : \
  8777. ((a).tv_sec cmp (b).tv_sec)) \
  8778. /* do nothing handler */
  8779. static void myHandler(int signo)
  8780. {
  8781. (void)signo;
  8782. return;
  8783. }
  8784. static int wolfSSL_ex_wrapper(WOLFSSL* ssl, HandShakeCallBack hsCb,
  8785. TimeoutCallBack toCb, WOLFSSL_TIMEVAL timeout)
  8786. {
  8787. int ret = WOLFSSL_FATAL_ERROR;
  8788. int oldTimerOn = 0; /* was timer already on */
  8789. WOLFSSL_TIMEVAL startTime;
  8790. WOLFSSL_TIMEVAL endTime;
  8791. WOLFSSL_TIMEVAL totalTime;
  8792. Itimerval myTimeout;
  8793. Itimerval oldTimeout; /* if old timer adjust from total time to reset */
  8794. struct sigaction act, oact;
  8795. #define ERR_OUT(x) { ssl->hsInfoOn = 0; ssl->toInfoOn = 0; return x; }
  8796. if (hsCb) {
  8797. ssl->hsInfoOn = 1;
  8798. InitHandShakeInfo(&ssl->handShakeInfo, ssl);
  8799. }
  8800. if (toCb) {
  8801. ssl->toInfoOn = 1;
  8802. InitTimeoutInfo(&ssl->timeoutInfo);
  8803. if (gettimeofday(&startTime, 0) < 0)
  8804. ERR_OUT(GETTIME_ERROR);
  8805. /* use setitimer to simulate getitimer, init 0 myTimeout */
  8806. myTimeout.it_interval.tv_sec = 0;
  8807. myTimeout.it_interval.tv_usec = 0;
  8808. myTimeout.it_value.tv_sec = 0;
  8809. myTimeout.it_value.tv_usec = 0;
  8810. if (setitimer(ITIMER_REAL, &myTimeout, &oldTimeout) < 0)
  8811. ERR_OUT(SETITIMER_ERROR);
  8812. if (oldTimeout.it_value.tv_sec || oldTimeout.it_value.tv_usec) {
  8813. oldTimerOn = 1;
  8814. /* is old timer going to expire before ours */
  8815. if (CmpTimes(oldTimeout.it_value, timeout, <)) {
  8816. timeout.tv_sec = oldTimeout.it_value.tv_sec;
  8817. timeout.tv_usec = oldTimeout.it_value.tv_usec;
  8818. }
  8819. }
  8820. myTimeout.it_value.tv_sec = timeout.tv_sec;
  8821. myTimeout.it_value.tv_usec = timeout.tv_usec;
  8822. /* set up signal handler, don't restart socket send/recv */
  8823. act.sa_handler = myHandler;
  8824. sigemptyset(&act.sa_mask);
  8825. act.sa_flags = 0;
  8826. #ifdef SA_INTERRUPT
  8827. act.sa_flags |= SA_INTERRUPT;
  8828. #endif
  8829. if (sigaction(SIGALRM, &act, &oact) < 0)
  8830. ERR_OUT(SIGACT_ERROR);
  8831. if (setitimer(ITIMER_REAL, &myTimeout, 0) < 0)
  8832. ERR_OUT(SETITIMER_ERROR);
  8833. }
  8834. /* do main work */
  8835. #ifndef NO_WOLFSSL_CLIENT
  8836. if (ssl->options.side == WOLFSSL_CLIENT_END)
  8837. ret = wolfSSL_connect(ssl);
  8838. #endif
  8839. #ifndef NO_WOLFSSL_SERVER
  8840. if (ssl->options.side == WOLFSSL_SERVER_END)
  8841. ret = wolfSSL_accept(ssl);
  8842. #endif
  8843. /* do callbacks */
  8844. if (toCb) {
  8845. if (oldTimerOn) {
  8846. if (gettimeofday(&endTime, 0) < 0)
  8847. ERR_OUT(SYSLIB_FAILED_E);
  8848. SubtractTimes(endTime, startTime, totalTime);
  8849. /* adjust old timer for elapsed time */
  8850. if (CmpTimes(totalTime, oldTimeout.it_value, <))
  8851. SubtractTimes(oldTimeout.it_value, totalTime,
  8852. oldTimeout.it_value);
  8853. else {
  8854. /* reset value to interval, may be off */
  8855. oldTimeout.it_value.tv_sec = oldTimeout.it_interval.tv_sec;
  8856. oldTimeout.it_value.tv_usec =oldTimeout.it_interval.tv_usec;
  8857. }
  8858. /* keep iter the same whether there or not */
  8859. }
  8860. /* restore old handler */
  8861. if (sigaction(SIGALRM, &oact, 0) < 0)
  8862. ret = SIGACT_ERROR; /* more pressing error, stomp */
  8863. else
  8864. /* use old settings which may turn off (expired or not there) */
  8865. if (setitimer(ITIMER_REAL, &oldTimeout, 0) < 0)
  8866. ret = SETITIMER_ERROR;
  8867. /* if we had a timeout call callback */
  8868. if (ssl->timeoutInfo.timeoutName[0]) {
  8869. ssl->timeoutInfo.timeoutValue.tv_sec = timeout.tv_sec;
  8870. ssl->timeoutInfo.timeoutValue.tv_usec = timeout.tv_usec;
  8871. (toCb)(&ssl->timeoutInfo);
  8872. }
  8873. ssl->toInfoOn = 0;
  8874. }
  8875. /* clean up buffers allocated by AddPacketInfo */
  8876. FreeTimeoutInfo(&ssl->timeoutInfo, ssl->heap);
  8877. if (hsCb) {
  8878. FinishHandShakeInfo(&ssl->handShakeInfo);
  8879. (hsCb)(&ssl->handShakeInfo);
  8880. ssl->hsInfoOn = 0;
  8881. }
  8882. return ret;
  8883. }
  8884. #ifndef NO_WOLFSSL_CLIENT
  8885. int wolfSSL_connect_ex(WOLFSSL* ssl, HandShakeCallBack hsCb,
  8886. TimeoutCallBack toCb, WOLFSSL_TIMEVAL timeout)
  8887. {
  8888. WOLFSSL_ENTER("wolfSSL_connect_ex");
  8889. return wolfSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
  8890. }
  8891. #endif
  8892. #ifndef NO_WOLFSSL_SERVER
  8893. int wolfSSL_accept_ex(WOLFSSL* ssl, HandShakeCallBack hsCb,
  8894. TimeoutCallBack toCb, WOLFSSL_TIMEVAL timeout)
  8895. {
  8896. WOLFSSL_ENTER("wolfSSL_accept_ex");
  8897. return wolfSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
  8898. }
  8899. #endif
  8900. #endif /* WOLFSSL_CALLBACKS */
  8901. #ifndef NO_PSK
  8902. void wolfSSL_CTX_set_psk_client_callback(WOLFSSL_CTX* ctx,
  8903. wc_psk_client_callback cb)
  8904. {
  8905. WOLFSSL_ENTER("wolfSSL_CTX_set_psk_client_callback");
  8906. if (ctx == NULL)
  8907. return;
  8908. ctx->havePSK = 1;
  8909. ctx->client_psk_cb = cb;
  8910. }
  8911. void wolfSSL_set_psk_client_callback(WOLFSSL* ssl,wc_psk_client_callback cb)
  8912. {
  8913. byte haveRSA = 1;
  8914. int keySz = 0;
  8915. WOLFSSL_ENTER("wolfSSL_set_psk_client_callback");
  8916. if (ssl == NULL)
  8917. return;
  8918. ssl->options.havePSK = 1;
  8919. ssl->options.client_psk_cb = cb;
  8920. #ifdef NO_RSA
  8921. haveRSA = 0;
  8922. #endif
  8923. #ifndef NO_CERTS
  8924. keySz = ssl->buffers.keySz;
  8925. #endif
  8926. if (AllocateSuites(ssl) != 0)
  8927. return;
  8928. InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE,
  8929. ssl->options.haveDH, ssl->options.haveECDSAsig,
  8930. ssl->options.haveECC, TRUE, ssl->options.haveStaticECC,
  8931. ssl->options.haveFalconSig, ssl->options.haveDilithiumSig,
  8932. ssl->options.useAnon, TRUE, ssl->options.side);
  8933. }
  8934. #ifdef OPENSSL_EXTRA
  8935. /**
  8936. * set call back function for psk session use
  8937. * @param ssl a pointer to WOLFSSL structure
  8938. * @param cb a function pointer to wc_psk_use_session_cb
  8939. * @return none
  8940. */
  8941. void wolfSSL_set_psk_use_session_callback(WOLFSSL* ssl,
  8942. wc_psk_use_session_cb_func cb)
  8943. {
  8944. WOLFSSL_ENTER("wolfSSL_set_psk_use_session_callback");
  8945. if (ssl != NULL) {
  8946. ssl->options.havePSK = 1;
  8947. ssl->options.session_psk_cb = cb;
  8948. }
  8949. WOLFSSL_LEAVE("wolfSSL_set_psk_use_session_callback", WOLFSSL_SUCCESS);
  8950. }
  8951. #endif
  8952. void wolfSSL_CTX_set_psk_server_callback(WOLFSSL_CTX* ctx,
  8953. wc_psk_server_callback cb)
  8954. {
  8955. WOLFSSL_ENTER("wolfSSL_CTX_set_psk_server_callback");
  8956. if (ctx == NULL)
  8957. return;
  8958. ctx->havePSK = 1;
  8959. ctx->server_psk_cb = cb;
  8960. }
  8961. void wolfSSL_set_psk_server_callback(WOLFSSL* ssl,wc_psk_server_callback cb)
  8962. {
  8963. byte haveRSA = 1;
  8964. int keySz = 0;
  8965. WOLFSSL_ENTER("wolfSSL_set_psk_server_callback");
  8966. if (ssl == NULL)
  8967. return;
  8968. ssl->options.havePSK = 1;
  8969. ssl->options.server_psk_cb = cb;
  8970. #ifdef NO_RSA
  8971. haveRSA = 0;
  8972. #endif
  8973. #ifndef NO_CERTS
  8974. keySz = ssl->buffers.keySz;
  8975. #endif
  8976. if (AllocateSuites(ssl) != 0)
  8977. return;
  8978. InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE,
  8979. ssl->options.haveDH, ssl->options.haveECDSAsig,
  8980. ssl->options.haveECC, TRUE, ssl->options.haveStaticECC,
  8981. ssl->options.haveFalconSig, ssl->options.haveDilithiumSig,
  8982. ssl->options.useAnon, TRUE, ssl->options.side);
  8983. }
  8984. const char* wolfSSL_get_psk_identity_hint(const WOLFSSL* ssl)
  8985. {
  8986. WOLFSSL_ENTER("wolfSSL_get_psk_identity_hint");
  8987. if (ssl == NULL || ssl->arrays == NULL)
  8988. return NULL;
  8989. return ssl->arrays->server_hint;
  8990. }
  8991. const char* wolfSSL_get_psk_identity(const WOLFSSL* ssl)
  8992. {
  8993. WOLFSSL_ENTER("wolfSSL_get_psk_identity");
  8994. if (ssl == NULL || ssl->arrays == NULL)
  8995. return NULL;
  8996. return ssl->arrays->client_identity;
  8997. }
  8998. int wolfSSL_CTX_use_psk_identity_hint(WOLFSSL_CTX* ctx, const char* hint)
  8999. {
  9000. WOLFSSL_ENTER("wolfSSL_CTX_use_psk_identity_hint");
  9001. if (hint == 0)
  9002. ctx->server_hint[0] = '\0';
  9003. else {
  9004. /* Qt does not call CTX_set_*_psk_callbacks where havePSK is set */
  9005. #ifdef WOLFSSL_QT
  9006. ctx->havePSK=1;
  9007. #endif
  9008. XSTRNCPY(ctx->server_hint, hint, MAX_PSK_ID_LEN);
  9009. ctx->server_hint[MAX_PSK_ID_LEN] = '\0'; /* null term */
  9010. }
  9011. return WOLFSSL_SUCCESS;
  9012. }
  9013. int wolfSSL_use_psk_identity_hint(WOLFSSL* ssl, const char* hint)
  9014. {
  9015. WOLFSSL_ENTER("wolfSSL_use_psk_identity_hint");
  9016. if (ssl == NULL || ssl->arrays == NULL)
  9017. return WOLFSSL_FAILURE;
  9018. if (hint == 0)
  9019. ssl->arrays->server_hint[0] = 0;
  9020. else {
  9021. XSTRNCPY(ssl->arrays->server_hint, hint,
  9022. sizeof(ssl->arrays->server_hint)-1);
  9023. ssl->arrays->server_hint[sizeof(ssl->arrays->server_hint)-1] = '\0';
  9024. }
  9025. return WOLFSSL_SUCCESS;
  9026. }
  9027. void* wolfSSL_get_psk_callback_ctx(WOLFSSL* ssl)
  9028. {
  9029. return ssl ? ssl->options.psk_ctx : NULL;
  9030. }
  9031. void* wolfSSL_CTX_get_psk_callback_ctx(WOLFSSL_CTX* ctx)
  9032. {
  9033. return ctx ? ctx->psk_ctx : NULL;
  9034. }
  9035. int wolfSSL_set_psk_callback_ctx(WOLFSSL* ssl, void* psk_ctx)
  9036. {
  9037. if (ssl == NULL)
  9038. return WOLFSSL_FAILURE;
  9039. ssl->options.psk_ctx = psk_ctx;
  9040. return WOLFSSL_SUCCESS;
  9041. }
  9042. int wolfSSL_CTX_set_psk_callback_ctx(WOLFSSL_CTX* ctx, void* psk_ctx)
  9043. {
  9044. if (ctx == NULL)
  9045. return WOLFSSL_FAILURE;
  9046. ctx->psk_ctx = psk_ctx;
  9047. return WOLFSSL_SUCCESS;
  9048. }
  9049. #endif /* NO_PSK */
  9050. #ifdef HAVE_ANON
  9051. int wolfSSL_CTX_allow_anon_cipher(WOLFSSL_CTX* ctx)
  9052. {
  9053. WOLFSSL_ENTER("wolfSSL_CTX_allow_anon_cipher");
  9054. if (ctx == NULL)
  9055. return WOLFSSL_FAILURE;
  9056. ctx->useAnon = 1;
  9057. return WOLFSSL_SUCCESS;
  9058. }
  9059. #endif /* HAVE_ANON */
  9060. #ifndef NO_CERTS
  9061. /* unload any certs or keys that SSL owns, leave CTX as is
  9062. WOLFSSL_SUCCESS on ok */
  9063. int wolfSSL_UnloadCertsKeys(WOLFSSL* ssl)
  9064. {
  9065. if (ssl == NULL) {
  9066. WOLFSSL_MSG("Null function arg");
  9067. return BAD_FUNC_ARG;
  9068. }
  9069. if (ssl->buffers.weOwnCert && !ssl->keepCert) {
  9070. WOLFSSL_MSG("Unloading cert");
  9071. FreeDer(&ssl->buffers.certificate);
  9072. #ifdef KEEP_OUR_CERT
  9073. wolfSSL_X509_free(ssl->ourCert);
  9074. ssl->ourCert = NULL;
  9075. #endif
  9076. ssl->buffers.weOwnCert = 0;
  9077. }
  9078. if (ssl->buffers.weOwnCertChain) {
  9079. WOLFSSL_MSG("Unloading cert chain");
  9080. FreeDer(&ssl->buffers.certChain);
  9081. ssl->buffers.weOwnCertChain = 0;
  9082. }
  9083. if (ssl->buffers.weOwnKey) {
  9084. WOLFSSL_MSG("Unloading key");
  9085. ForceZero(ssl->buffers.key->buffer, ssl->buffers.key->length);
  9086. FreeDer(&ssl->buffers.key);
  9087. ssl->buffers.weOwnKey = 0;
  9088. }
  9089. #ifdef WOLFSSL_DUAL_ALG_CERTS
  9090. if (ssl->buffers.weOwnAltKey) {
  9091. WOLFSSL_MSG("Unloading alt key");
  9092. ForceZero(ssl->buffers.altKey->buffer, ssl->buffers.altKey->length);
  9093. FreeDer(&ssl->buffers.altKey);
  9094. ssl->buffers.weOwnAltKey = 0;
  9095. }
  9096. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  9097. return WOLFSSL_SUCCESS;
  9098. }
  9099. int wolfSSL_CTX_UnloadCAs(WOLFSSL_CTX* ctx)
  9100. {
  9101. WOLFSSL_ENTER("wolfSSL_CTX_UnloadCAs");
  9102. if (ctx == NULL)
  9103. return BAD_FUNC_ARG;
  9104. return wolfSSL_CertManagerUnloadCAs(ctx->cm);
  9105. }
  9106. int wolfSSL_CTX_UnloadIntermediateCerts(WOLFSSL_CTX* ctx)
  9107. {
  9108. WOLFSSL_ENTER("wolfSSL_CTX_UnloadIntermediateCerts");
  9109. if (ctx == NULL)
  9110. return BAD_FUNC_ARG;
  9111. if (ctx->ref.count > 1) {
  9112. WOLFSSL_MSG("ctx object must have a ref count of 1 before "
  9113. "unloading intermediate certs");
  9114. return BAD_STATE_E;
  9115. }
  9116. return wolfSSL_CertManagerUnloadIntermediateCerts(ctx->cm);
  9117. }
  9118. #ifdef WOLFSSL_TRUST_PEER_CERT
  9119. int wolfSSL_CTX_Unload_trust_peers(WOLFSSL_CTX* ctx)
  9120. {
  9121. WOLFSSL_ENTER("wolfSSL_CTX_Unload_trust_peers");
  9122. if (ctx == NULL)
  9123. return BAD_FUNC_ARG;
  9124. return wolfSSL_CertManagerUnload_trust_peers(ctx->cm);
  9125. }
  9126. #ifdef WOLFSSL_LOCAL_X509_STORE
  9127. int wolfSSL_Unload_trust_peers(WOLFSSL* ssl)
  9128. {
  9129. WOLFSSL_ENTER("wolfSSL_CTX_Unload_trust_peers");
  9130. if (ssl == NULL)
  9131. return BAD_FUNC_ARG;
  9132. SSL_CM_WARNING(ssl);
  9133. return wolfSSL_CertManagerUnload_trust_peers(SSL_CM(ssl));
  9134. }
  9135. #endif /* WOLFSSL_LOCAL_X509_STORE */
  9136. #endif /* WOLFSSL_TRUST_PEER_CERT */
  9137. /* old NO_FILESYSTEM end */
  9138. #endif /* !NO_CERTS */
  9139. #ifdef OPENSSL_EXTRA
  9140. int wolfSSL_add_all_algorithms(void)
  9141. {
  9142. WOLFSSL_ENTER("wolfSSL_add_all_algorithms");
  9143. if (initRefCount != 0 || wolfSSL_Init() == WOLFSSL_SUCCESS)
  9144. return WOLFSSL_SUCCESS;
  9145. else
  9146. return WOLFSSL_FATAL_ERROR;
  9147. }
  9148. int wolfSSL_OpenSSL_add_all_algorithms_noconf(void)
  9149. {
  9150. WOLFSSL_ENTER("wolfSSL_OpenSSL_add_all_algorithms_noconf");
  9151. if (wolfSSL_add_all_algorithms() == WOLFSSL_FATAL_ERROR)
  9152. return WOLFSSL_FATAL_ERROR;
  9153. return WOLFSSL_SUCCESS;
  9154. }
  9155. int wolfSSL_OpenSSL_add_all_algorithms_conf(void)
  9156. {
  9157. WOLFSSL_ENTER("wolfSSL_OpenSSL_add_all_algorithms_conf");
  9158. /* This function is currently the same as
  9159. wolfSSL_OpenSSL_add_all_algorithms_noconf since we do not employ
  9160. the use of a wolfssl.cnf type configuration file and is only used for
  9161. OpenSSL compatibility. */
  9162. if (wolfSSL_add_all_algorithms() == WOLFSSL_FATAL_ERROR) {
  9163. return WOLFSSL_FATAL_ERROR;
  9164. }
  9165. return WOLFSSL_SUCCESS;
  9166. }
  9167. #endif
  9168. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  9169. defined(WOLFSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  9170. void wolfSSL_CTX_set_quiet_shutdown(WOLFSSL_CTX* ctx, int mode)
  9171. {
  9172. WOLFSSL_ENTER("wolfSSL_CTX_set_quiet_shutdown");
  9173. if (mode)
  9174. ctx->quietShutdown = 1;
  9175. }
  9176. void wolfSSL_set_quiet_shutdown(WOLFSSL* ssl, int mode)
  9177. {
  9178. WOLFSSL_ENTER("wolfSSL_set_quiet_shutdown");
  9179. if (mode)
  9180. ssl->options.quietShutdown = 1;
  9181. }
  9182. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL ||
  9183. WOLFSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  9184. #ifdef OPENSSL_EXTRA
  9185. #ifndef NO_BIO
  9186. void wolfSSL_set_bio(WOLFSSL* ssl, WOLFSSL_BIO* rd, WOLFSSL_BIO* wr)
  9187. {
  9188. WOLFSSL_ENTER("wolfSSL_set_bio");
  9189. if (ssl == NULL) {
  9190. WOLFSSL_MSG("Bad argument, ssl was NULL");
  9191. return;
  9192. }
  9193. /* free any existing WOLFSSL_BIOs in use but don't free those in
  9194. * a chain */
  9195. if (ssl->biord != NULL) {
  9196. if (ssl->biord != ssl->biowr) {
  9197. if (ssl->biowr != NULL && ssl->biowr->prev != NULL)
  9198. wolfSSL_BIO_free(ssl->biowr);
  9199. ssl->biowr = NULL;
  9200. }
  9201. if (ssl->biord->prev != NULL)
  9202. wolfSSL_BIO_free(ssl->biord);
  9203. ssl->biord = NULL;
  9204. }
  9205. /* set flag obviously */
  9206. if (rd && !(rd->flags & WOLFSSL_BIO_FLAG_READ))
  9207. rd->flags |= WOLFSSL_BIO_FLAG_READ;
  9208. if (wr && !(wr->flags & WOLFSSL_BIO_FLAG_WRITE))
  9209. wr->flags |= WOLFSSL_BIO_FLAG_WRITE;
  9210. ssl->biord = rd;
  9211. ssl->biowr = wr;
  9212. /* set SSL to use BIO callbacks instead */
  9213. if (((ssl->cbioFlag & WOLFSSL_CBIO_RECV) == 0)) {
  9214. ssl->CBIORecv = BioReceive;
  9215. }
  9216. if (((ssl->cbioFlag & WOLFSSL_CBIO_SEND) == 0)) {
  9217. ssl->CBIOSend = BioSend;
  9218. }
  9219. /* User programs should always retry reading from these BIOs */
  9220. if (rd) {
  9221. /* User writes to rd */
  9222. BIO_set_retry_write(rd);
  9223. }
  9224. if (wr) {
  9225. /* User reads from wr */
  9226. BIO_set_retry_read(wr);
  9227. }
  9228. }
  9229. #endif /* !NO_BIO */
  9230. #endif /* OPENSSL_EXTRA */
  9231. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  9232. void wolfSSL_CTX_set_client_CA_list(WOLFSSL_CTX* ctx,
  9233. WOLF_STACK_OF(WOLFSSL_X509_NAME)* names)
  9234. {
  9235. WOLFSSL_ENTER("wolfSSL_CTX_set_client_CA_list");
  9236. if (ctx != NULL) {
  9237. wolfSSL_sk_X509_NAME_pop_free(ctx->client_ca_names, NULL);
  9238. ctx->client_ca_names = names;
  9239. }
  9240. }
  9241. void wolfSSL_set_client_CA_list(WOLFSSL* ssl,
  9242. WOLF_STACK_OF(WOLFSSL_X509_NAME)* names)
  9243. {
  9244. WOLFSSL_ENTER("wolfSSL_set_client_CA_list");
  9245. if (ssl != NULL) {
  9246. if (ssl->client_ca_names != ssl->ctx->client_ca_names)
  9247. wolfSSL_sk_X509_NAME_pop_free(ssl->client_ca_names, NULL);
  9248. ssl->client_ca_names = names;
  9249. }
  9250. }
  9251. #ifdef OPENSSL_EXTRA
  9252. /* registers client cert callback, called during handshake if server
  9253. requests client auth but user has not loaded client cert/key */
  9254. void wolfSSL_CTX_set_client_cert_cb(WOLFSSL_CTX *ctx, client_cert_cb cb)
  9255. {
  9256. WOLFSSL_ENTER("wolfSSL_CTX_set_client_cert_cb");
  9257. if (ctx != NULL) {
  9258. ctx->CBClientCert = cb;
  9259. }
  9260. }
  9261. void wolfSSL_CTX_set_cert_cb(WOLFSSL_CTX* ctx,
  9262. CertSetupCallback cb, void *arg)
  9263. {
  9264. WOLFSSL_ENTER("wolfSSL_CTX_set_cert_cb");
  9265. if (ctx == NULL)
  9266. return;
  9267. ctx->certSetupCb = cb;
  9268. ctx->certSetupCbArg = arg;
  9269. }
  9270. int wolfSSL_get_client_suites_sigalgs(const WOLFSSL* ssl,
  9271. const byte** suites, word16* suiteSz,
  9272. const byte** hashSigAlgo, word16* hashSigAlgoSz)
  9273. {
  9274. WOLFSSL_ENTER("wolfSSL_get_client_suites_sigalgs");
  9275. if (suites != NULL)
  9276. *suites = NULL;
  9277. if (suiteSz != NULL)
  9278. *suiteSz = 0;
  9279. if (hashSigAlgo != NULL)
  9280. *hashSigAlgo = NULL;
  9281. if (hashSigAlgoSz != NULL)
  9282. *hashSigAlgoSz = 0;
  9283. if (ssl != NULL && ssl->clSuites != NULL) {
  9284. if (suites != NULL && suiteSz != NULL) {
  9285. *suites = ssl->clSuites->suites;
  9286. *suiteSz = ssl->clSuites->suiteSz;
  9287. }
  9288. if (hashSigAlgo != NULL && hashSigAlgoSz != NULL) {
  9289. *hashSigAlgo = ssl->clSuites->hashSigAlgo;
  9290. *hashSigAlgoSz = ssl->clSuites->hashSigAlgoSz;
  9291. }
  9292. return WOLFSSL_SUCCESS;
  9293. }
  9294. return WOLFSSL_FAILURE;
  9295. }
  9296. WOLFSSL_CIPHERSUITE_INFO wolfSSL_get_ciphersuite_info(byte first,
  9297. byte second)
  9298. {
  9299. WOLFSSL_CIPHERSUITE_INFO info;
  9300. info.rsaAuth = (byte)(CipherRequires(first, second, REQUIRES_RSA) ||
  9301. CipherRequires(first, second, REQUIRES_RSA_SIG));
  9302. info.eccAuth = (byte)(CipherRequires(first, second, REQUIRES_ECC) ||
  9303. /* Static ECC ciphers may require RSA for authentication */
  9304. (CipherRequires(first, second, REQUIRES_ECC_STATIC) &&
  9305. !CipherRequires(first, second, REQUIRES_RSA_SIG)));
  9306. info.eccStatic =
  9307. (byte)CipherRequires(first, second, REQUIRES_ECC_STATIC);
  9308. info.psk = (byte)CipherRequires(first, second, REQUIRES_PSK);
  9309. return info;
  9310. }
  9311. /**
  9312. * @param first First byte of the hash and signature algorithm
  9313. * @param second Second byte of the hash and signature algorithm
  9314. * @param hashAlgo The enum wc_HashType of the MAC algorithm
  9315. * @param sigAlgo The enum Key_Sum of the authentication algorithm
  9316. */
  9317. int wolfSSL_get_sigalg_info(byte first, byte second,
  9318. int* hashAlgo, int* sigAlgo)
  9319. {
  9320. byte input[2];
  9321. byte hashType;
  9322. byte sigType;
  9323. if (hashAlgo == NULL || sigAlgo == NULL)
  9324. return BAD_FUNC_ARG;
  9325. input[0] = first;
  9326. input[1] = second;
  9327. DecodeSigAlg(input, &hashType, &sigType);
  9328. /* cast so that compiler reminds us of unimplemented values */
  9329. switch ((enum SignatureAlgorithm)sigType) {
  9330. case anonymous_sa_algo:
  9331. *sigAlgo = ANONk;
  9332. break;
  9333. case rsa_sa_algo:
  9334. *sigAlgo = RSAk;
  9335. break;
  9336. case dsa_sa_algo:
  9337. *sigAlgo = DSAk;
  9338. break;
  9339. case ecc_dsa_sa_algo:
  9340. *sigAlgo = ECDSAk;
  9341. break;
  9342. case rsa_pss_sa_algo:
  9343. *sigAlgo = RSAPSSk;
  9344. break;
  9345. case ed25519_sa_algo:
  9346. *sigAlgo = ED25519k;
  9347. break;
  9348. case rsa_pss_pss_algo:
  9349. *sigAlgo = RSAPSSk;
  9350. break;
  9351. case ed448_sa_algo:
  9352. *sigAlgo = ED448k;
  9353. break;
  9354. case falcon_level1_sa_algo:
  9355. *sigAlgo = FALCON_LEVEL1k;
  9356. break;
  9357. case falcon_level5_sa_algo:
  9358. *sigAlgo = FALCON_LEVEL5k;
  9359. break;
  9360. case dilithium_level2_sa_algo:
  9361. *sigAlgo = DILITHIUM_LEVEL2k;
  9362. break;
  9363. case dilithium_level3_sa_algo:
  9364. *sigAlgo = DILITHIUM_LEVEL3k;
  9365. break;
  9366. case dilithium_level5_sa_algo:
  9367. *sigAlgo = DILITHIUM_LEVEL5k;
  9368. break;
  9369. case sm2_sa_algo:
  9370. *sigAlgo = SM2k;
  9371. break;
  9372. case invalid_sa_algo:
  9373. default:
  9374. *hashAlgo = WC_HASH_TYPE_NONE;
  9375. *sigAlgo = 0;
  9376. return BAD_FUNC_ARG;
  9377. }
  9378. /* cast so that compiler reminds us of unimplemented values */
  9379. switch((enum wc_MACAlgorithm)hashType) {
  9380. case no_mac:
  9381. case rmd_mac: /* Don't have a RIPEMD type in wc_HashType */
  9382. *hashAlgo = WC_HASH_TYPE_NONE;
  9383. break;
  9384. case md5_mac:
  9385. *hashAlgo = WC_HASH_TYPE_MD5;
  9386. break;
  9387. case sha_mac:
  9388. *hashAlgo = WC_HASH_TYPE_SHA;
  9389. break;
  9390. case sha224_mac:
  9391. *hashAlgo = WC_HASH_TYPE_SHA224;
  9392. break;
  9393. case sha256_mac:
  9394. *hashAlgo = WC_HASH_TYPE_SHA256;
  9395. break;
  9396. case sha384_mac:
  9397. *hashAlgo = WC_HASH_TYPE_SHA384;
  9398. break;
  9399. case sha512_mac:
  9400. *hashAlgo = WC_HASH_TYPE_SHA512;
  9401. break;
  9402. case blake2b_mac:
  9403. *hashAlgo = WC_HASH_TYPE_BLAKE2B;
  9404. break;
  9405. case sm3_mac:
  9406. #ifdef WOLFSSL_SM3
  9407. *hashAlgo = WC_HASH_TYPE_SM3;
  9408. #else
  9409. *hashAlgo = WC_HASH_TYPE_NONE;
  9410. #endif
  9411. break;
  9412. default:
  9413. *hashAlgo = WC_HASH_TYPE_NONE;
  9414. *sigAlgo = 0;
  9415. return BAD_FUNC_ARG;
  9416. }
  9417. return 0;
  9418. }
  9419. /**
  9420. * Internal wrapper for calling certSetupCb
  9421. * @param ssl The SSL/TLS Object
  9422. * @return 0 on success
  9423. */
  9424. int CertSetupCbWrapper(WOLFSSL* ssl)
  9425. {
  9426. int ret = 0;
  9427. if (ssl->ctx->certSetupCb != NULL) {
  9428. WOLFSSL_MSG("Calling user cert setup callback");
  9429. ret = ssl->ctx->certSetupCb(ssl, ssl->ctx->certSetupCbArg);
  9430. if (ret == 1) {
  9431. WOLFSSL_MSG("User cert callback returned success");
  9432. ret = 0;
  9433. }
  9434. else if (ret == 0) {
  9435. SendAlert(ssl, alert_fatal, internal_error);
  9436. ret = CLIENT_CERT_CB_ERROR;
  9437. }
  9438. else if (ret < 0) {
  9439. ret = WOLFSSL_ERROR_WANT_X509_LOOKUP;
  9440. }
  9441. else {
  9442. WOLFSSL_MSG("Unexpected user callback return");
  9443. ret = CLIENT_CERT_CB_ERROR;
  9444. }
  9445. }
  9446. return ret;
  9447. }
  9448. #endif /* OPENSSL_EXTRA */
  9449. #endif /* OPENSSL_EXTRA || WOLFSSL_EXTRA || HAVE_WEBSERVER */
  9450. #ifndef WOLFSSL_NO_CA_NAMES
  9451. WOLF_STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_CTX_get_client_CA_list(
  9452. const WOLFSSL_CTX *ctx)
  9453. {
  9454. WOLFSSL_ENTER("wolfSSL_CTX_get_client_CA_list");
  9455. if (ctx == NULL) {
  9456. WOLFSSL_MSG("Bad argument passed to "
  9457. "wolfSSL_CTX_get_client_CA_list");
  9458. return NULL;
  9459. }
  9460. return ctx->client_ca_names;
  9461. }
  9462. /* returns the CA's set on server side or the CA's sent from server when
  9463. * on client side */
  9464. WOLF_STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_get_client_CA_list(
  9465. const WOLFSSL* ssl)
  9466. {
  9467. WOLFSSL_ENTER("wolfSSL_get_client_CA_list");
  9468. if (ssl == NULL) {
  9469. WOLFSSL_MSG("Bad argument passed to wolfSSL_get_client_CA_list");
  9470. return NULL;
  9471. }
  9472. return SSL_CA_NAMES(ssl);
  9473. }
  9474. #if !defined(NO_CERTS)
  9475. int wolfSSL_CTX_add_client_CA(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509)
  9476. {
  9477. WOLFSSL_X509_NAME *nameCopy = NULL;
  9478. WOLFSSL_ENTER("wolfSSL_CTX_add_client_CA");
  9479. if (ctx == NULL || x509 == NULL){
  9480. WOLFSSL_MSG("Bad argument");
  9481. return WOLFSSL_FAILURE;
  9482. }
  9483. if (ctx->client_ca_names == NULL) {
  9484. ctx->client_ca_names = wolfSSL_sk_X509_NAME_new(NULL);
  9485. if (ctx->client_ca_names == NULL) {
  9486. WOLFSSL_MSG("wolfSSL_sk_X509_NAME_new error");
  9487. return WOLFSSL_FAILURE;
  9488. }
  9489. }
  9490. nameCopy = wolfSSL_X509_NAME_dup(wolfSSL_X509_get_subject_name(x509));
  9491. if (nameCopy == NULL) {
  9492. WOLFSSL_MSG("wolfSSL_X509_NAME_dup error");
  9493. return WOLFSSL_FAILURE;
  9494. }
  9495. if (wolfSSL_sk_X509_NAME_push(ctx->client_ca_names, nameCopy) !=
  9496. WOLFSSL_SUCCESS) {
  9497. WOLFSSL_MSG("wolfSSL_sk_X509_NAME_push error");
  9498. wolfSSL_X509_NAME_free(nameCopy);
  9499. return WOLFSSL_FAILURE;
  9500. }
  9501. return WOLFSSL_SUCCESS;
  9502. }
  9503. #endif
  9504. #ifndef NO_BIO
  9505. #if !defined(NO_RSA) && !defined(NO_CERTS)
  9506. WOLF_STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_load_client_CA_file(
  9507. const char* fname)
  9508. {
  9509. /* The webserver build is using this to load a CA into the server
  9510. * for client authentication as an option. Have this return NULL in
  9511. * that case. If OPENSSL_EXTRA is enabled, go ahead and include
  9512. * the function. */
  9513. #ifdef OPENSSL_EXTRA
  9514. WOLFSSL_STACK *list = NULL;
  9515. WOLFSSL_BIO* bio = NULL;
  9516. WOLFSSL_X509 *cert = NULL;
  9517. WOLFSSL_X509_NAME *nameCopy = NULL;
  9518. unsigned long err = WOLFSSL_FAILURE;
  9519. WOLFSSL_ENTER("wolfSSL_load_client_CA_file");
  9520. bio = wolfSSL_BIO_new_file(fname, "rb");
  9521. if (bio == NULL) {
  9522. WOLFSSL_MSG("wolfSSL_BIO_new_file error");
  9523. goto cleanup;
  9524. }
  9525. list = wolfSSL_sk_X509_NAME_new(NULL);
  9526. if (list == NULL) {
  9527. WOLFSSL_MSG("wolfSSL_sk_X509_NAME_new error");
  9528. goto cleanup;
  9529. }
  9530. /* Read each certificate in the chain out of the file. */
  9531. while (wolfSSL_PEM_read_bio_X509(bio, &cert, NULL, NULL) != NULL) {
  9532. /* Need a persistent copy of the subject name. */
  9533. nameCopy = wolfSSL_X509_NAME_dup(
  9534. wolfSSL_X509_get_subject_name(cert));
  9535. if (nameCopy == NULL) {
  9536. WOLFSSL_MSG("wolfSSL_X509_NAME_dup error");
  9537. goto cleanup;
  9538. }
  9539. /*
  9540. * Original cert will be freed so make sure not to try to access
  9541. * it in the future.
  9542. */
  9543. nameCopy->x509 = NULL;
  9544. if (wolfSSL_sk_X509_NAME_push(list, nameCopy) !=
  9545. WOLFSSL_SUCCESS) {
  9546. WOLFSSL_MSG("wolfSSL_sk_X509_NAME_push error");
  9547. /* Do free in loop because nameCopy is now responsibility
  9548. * of list to free and adding jumps to cleanup after this
  9549. * might result in a double free. */
  9550. wolfSSL_X509_NAME_free(nameCopy);
  9551. goto cleanup;
  9552. }
  9553. wolfSSL_X509_free(cert);
  9554. cert = NULL;
  9555. }
  9556. CLEAR_ASN_NO_PEM_HEADER_ERROR(err);
  9557. err = WOLFSSL_SUCCESS;
  9558. cleanup:
  9559. wolfSSL_X509_free(cert);
  9560. wolfSSL_BIO_free(bio);
  9561. if (err != WOLFSSL_SUCCESS) {
  9562. /* We failed so return NULL */
  9563. wolfSSL_sk_X509_NAME_pop_free(list, NULL);
  9564. list = NULL;
  9565. }
  9566. return list;
  9567. #else
  9568. (void)fname;
  9569. return NULL;
  9570. #endif
  9571. }
  9572. #endif
  9573. #endif /* !NO_BIO */
  9574. #endif /* OPENSSL_EXTRA || WOLFSSL_EXTRA */
  9575. #ifdef OPENSSL_EXTRA
  9576. #if defined(WOLFCRYPT_HAVE_SRP) && !defined(NO_SHA256) \
  9577. && !defined(WC_NO_RNG)
  9578. static const byte srp_N[] = {
  9579. 0xEE, 0xAF, 0x0A, 0xB9, 0xAD, 0xB3, 0x8D, 0xD6, 0x9C, 0x33, 0xF8,
  9580. 0x0A, 0xFA, 0x8F, 0xC5, 0xE8, 0x60, 0x72, 0x61, 0x87, 0x75, 0xFF,
  9581. 0x3C, 0x0B, 0x9E, 0xA2, 0x31, 0x4C, 0x9C, 0x25, 0x65, 0x76, 0xD6,
  9582. 0x74, 0xDF, 0x74, 0x96, 0xEA, 0x81, 0xD3, 0x38, 0x3B, 0x48, 0x13,
  9583. 0xD6, 0x92, 0xC6, 0xE0, 0xE0, 0xD5, 0xD8, 0xE2, 0x50, 0xB9, 0x8B,
  9584. 0xE4, 0x8E, 0x49, 0x5C, 0x1D, 0x60, 0x89, 0xDA, 0xD1, 0x5D, 0xC7,
  9585. 0xD7, 0xB4, 0x61, 0x54, 0xD6, 0xB6, 0xCE, 0x8E, 0xF4, 0xAD, 0x69,
  9586. 0xB1, 0x5D, 0x49, 0x82, 0x55, 0x9B, 0x29, 0x7B, 0xCF, 0x18, 0x85,
  9587. 0xC5, 0x29, 0xF5, 0x66, 0x66, 0x0E, 0x57, 0xEC, 0x68, 0xED, 0xBC,
  9588. 0x3C, 0x05, 0x72, 0x6C, 0xC0, 0x2F, 0xD4, 0xCB, 0xF4, 0x97, 0x6E,
  9589. 0xAA, 0x9A, 0xFD, 0x51, 0x38, 0xFE, 0x83, 0x76, 0x43, 0x5B, 0x9F,
  9590. 0xC6, 0x1D, 0x2F, 0xC0, 0xEB, 0x06, 0xE3
  9591. };
  9592. static const byte srp_g[] = {
  9593. 0x02
  9594. };
  9595. int wolfSSL_CTX_set_srp_username(WOLFSSL_CTX* ctx, char* username)
  9596. {
  9597. int r = 0;
  9598. SrpSide srp_side = SRP_CLIENT_SIDE;
  9599. byte salt[SRP_SALT_SIZE];
  9600. WOLFSSL_ENTER("wolfSSL_CTX_set_srp_username");
  9601. if (ctx == NULL || ctx->srp == NULL || username==NULL)
  9602. return WOLFSSL_FAILURE;
  9603. if (ctx->method->side == WOLFSSL_SERVER_END){
  9604. srp_side = SRP_SERVER_SIDE;
  9605. } else if (ctx->method->side == WOLFSSL_CLIENT_END){
  9606. srp_side = SRP_CLIENT_SIDE;
  9607. } else {
  9608. WOLFSSL_MSG("Init CTX failed");
  9609. return WOLFSSL_FAILURE;
  9610. }
  9611. if (wc_SrpInit(ctx->srp, SRP_TYPE_SHA256, srp_side) < 0) {
  9612. WOLFSSL_MSG("Init SRP CTX failed");
  9613. XFREE(ctx->srp, ctx->heap, DYNAMIC_TYPE_SRP);
  9614. ctx->srp = NULL;
  9615. return WOLFSSL_FAILURE;
  9616. }
  9617. r = wc_SrpSetUsername(ctx->srp, (const byte*)username,
  9618. (word32)XSTRLEN(username));
  9619. if (r < 0) {
  9620. WOLFSSL_MSG("fail to set srp username.");
  9621. return WOLFSSL_FAILURE;
  9622. }
  9623. /* if wolfSSL_CTX_set_srp_password has already been called, */
  9624. /* execute wc_SrpSetPassword here */
  9625. if (ctx->srp_password != NULL) {
  9626. WC_RNG rng;
  9627. if (wc_InitRng(&rng) < 0){
  9628. WOLFSSL_MSG("wc_InitRng failed");
  9629. return WOLFSSL_FAILURE;
  9630. }
  9631. XMEMSET(salt, 0, sizeof(salt)/sizeof(salt[0]));
  9632. r = wc_RNG_GenerateBlock(&rng, salt, sizeof(salt)/sizeof(salt[0]));
  9633. wc_FreeRng(&rng);
  9634. if (r < 0) {
  9635. WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
  9636. return WOLFSSL_FAILURE;
  9637. }
  9638. if (wc_SrpSetParams(ctx->srp, srp_N, sizeof(srp_N)/sizeof(srp_N[0]),
  9639. srp_g, sizeof(srp_g)/sizeof(srp_g[0]),
  9640. salt, sizeof(salt)/sizeof(salt[0])) < 0) {
  9641. WOLFSSL_MSG("wc_SrpSetParam failed");
  9642. return WOLFSSL_FAILURE;
  9643. }
  9644. r = wc_SrpSetPassword(ctx->srp,
  9645. (const byte*)ctx->srp_password,
  9646. (word32)XSTRLEN((char *)ctx->srp_password));
  9647. if (r < 0) {
  9648. WOLFSSL_MSG("fail to set srp password.");
  9649. return WOLFSSL_FAILURE;
  9650. }
  9651. XFREE(ctx->srp_password, ctx->heap, DYNAMIC_TYPE_SRP);
  9652. ctx->srp_password = NULL;
  9653. }
  9654. return WOLFSSL_SUCCESS;
  9655. }
  9656. int wolfSSL_CTX_set_srp_password(WOLFSSL_CTX* ctx, char* password)
  9657. {
  9658. int r;
  9659. byte salt[SRP_SALT_SIZE];
  9660. WOLFSSL_ENTER("wolfSSL_CTX_set_srp_password");
  9661. if (ctx == NULL || ctx->srp == NULL || password == NULL)
  9662. return WOLFSSL_FAILURE;
  9663. if (ctx->srp->user != NULL) {
  9664. WC_RNG rng;
  9665. if (wc_InitRng(&rng) < 0) {
  9666. WOLFSSL_MSG("wc_InitRng failed");
  9667. return WOLFSSL_FAILURE;
  9668. }
  9669. XMEMSET(salt, 0, sizeof(salt)/sizeof(salt[0]));
  9670. r = wc_RNG_GenerateBlock(&rng, salt, sizeof(salt)/sizeof(salt[0]));
  9671. wc_FreeRng(&rng);
  9672. if (r < 0) {
  9673. WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
  9674. return WOLFSSL_FAILURE;
  9675. }
  9676. if (wc_SrpSetParams(ctx->srp, srp_N, sizeof(srp_N)/sizeof(srp_N[0]),
  9677. srp_g, sizeof(srp_g)/sizeof(srp_g[0]),
  9678. salt, sizeof(salt)/sizeof(salt[0])) < 0){
  9679. WOLFSSL_MSG("wc_SrpSetParam failed");
  9680. wc_FreeRng(&rng);
  9681. return WOLFSSL_FAILURE;
  9682. }
  9683. r = wc_SrpSetPassword(ctx->srp, (const byte*)password,
  9684. (word32)XSTRLEN(password));
  9685. if (r < 0) {
  9686. WOLFSSL_MSG("wc_SrpSetPassword failed.");
  9687. wc_FreeRng(&rng);
  9688. return WOLFSSL_FAILURE;
  9689. }
  9690. if (ctx->srp_password != NULL){
  9691. XFREE(ctx->srp_password,NULL,
  9692. DYNAMIC_TYPE_SRP);
  9693. ctx->srp_password = NULL;
  9694. }
  9695. wc_FreeRng(&rng);
  9696. } else {
  9697. /* save password for wolfSSL_set_srp_username */
  9698. if (ctx->srp_password != NULL)
  9699. XFREE(ctx->srp_password,ctx->heap, DYNAMIC_TYPE_SRP);
  9700. ctx->srp_password = (byte*)XMALLOC(XSTRLEN(password) + 1, ctx->heap,
  9701. DYNAMIC_TYPE_SRP);
  9702. if (ctx->srp_password == NULL){
  9703. WOLFSSL_MSG("memory allocation error");
  9704. return WOLFSSL_FAILURE;
  9705. }
  9706. XMEMCPY(ctx->srp_password, password, XSTRLEN(password) + 1);
  9707. }
  9708. return WOLFSSL_SUCCESS;
  9709. }
  9710. /**
  9711. * The modulus passed to wc_SrpSetParams in ssl.c is constant so check
  9712. * that the requested strength is less than or equal to the size of the
  9713. * static modulus size.
  9714. * @param ctx Not used
  9715. * @param strength Minimum number of bits for the modulus
  9716. * @return 1 if strength is less than or equal to static modulus
  9717. * 0 if strength is greater than static modulus
  9718. */
  9719. int wolfSSL_CTX_set_srp_strength(WOLFSSL_CTX *ctx, int strength)
  9720. {
  9721. (void)ctx;
  9722. WOLFSSL_ENTER("wolfSSL_CTX_set_srp_strength");
  9723. if (strength > (int)(sizeof(srp_N)*8)) {
  9724. WOLFSSL_MSG("Bad Parameter");
  9725. return WOLFSSL_FAILURE;
  9726. }
  9727. return WOLFSSL_SUCCESS;
  9728. }
  9729. char* wolfSSL_get_srp_username(WOLFSSL *ssl)
  9730. {
  9731. if (ssl && ssl->ctx && ssl->ctx->srp) {
  9732. return (char*) ssl->ctx->srp->user;
  9733. }
  9734. return NULL;
  9735. }
  9736. #endif /* WOLFCRYPT_HAVE_SRP && !NO_SHA256 && !WC_NO_RNG */
  9737. /* keyblock size in bytes or -1 */
  9738. int wolfSSL_get_keyblock_size(WOLFSSL* ssl)
  9739. {
  9740. if (ssl == NULL)
  9741. return WOLFSSL_FATAL_ERROR;
  9742. return 2 * (ssl->specs.key_size + ssl->specs.iv_size +
  9743. ssl->specs.hash_size);
  9744. }
  9745. #endif /* OPENSSL_EXTRA */
  9746. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA) || \
  9747. defined(WOLFSSL_WPAS_SMALL)
  9748. /* store keys returns WOLFSSL_SUCCESS or -1 on error */
  9749. int wolfSSL_get_keys(WOLFSSL* ssl, unsigned char** ms, unsigned int* msLen,
  9750. unsigned char** sr, unsigned int* srLen,
  9751. unsigned char** cr, unsigned int* crLen)
  9752. {
  9753. if (ssl == NULL || ssl->arrays == NULL)
  9754. return WOLFSSL_FATAL_ERROR;
  9755. *ms = ssl->arrays->masterSecret;
  9756. *sr = ssl->arrays->serverRandom;
  9757. *cr = ssl->arrays->clientRandom;
  9758. *msLen = SECRET_LEN;
  9759. *srLen = RAN_LEN;
  9760. *crLen = RAN_LEN;
  9761. return WOLFSSL_SUCCESS;
  9762. }
  9763. void wolfSSL_set_accept_state(WOLFSSL* ssl)
  9764. {
  9765. WOLFSSL_ENTER("wolfSSL_set_accept_state");
  9766. if (ssl == NULL)
  9767. return;
  9768. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  9769. #ifdef HAVE_ECC
  9770. #ifdef WOLFSSL_SMALL_STACK
  9771. ecc_key* key = NULL;
  9772. #else
  9773. ecc_key key[1];
  9774. #endif
  9775. word32 idx = 0;
  9776. #ifdef WOLFSSL_SMALL_STACK
  9777. key = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  9778. DYNAMIC_TYPE_ECC);
  9779. if (key == NULL) {
  9780. WOLFSSL_MSG("Error allocating memory for ecc_key");
  9781. }
  9782. #endif
  9783. if (ssl->options.haveStaticECC && ssl->buffers.key != NULL) {
  9784. if (wc_ecc_init(key) >= 0) {
  9785. if (wc_EccPrivateKeyDecode(ssl->buffers.key->buffer, &idx,
  9786. key, ssl->buffers.key->length) != 0) {
  9787. ssl->options.haveECDSAsig = 0;
  9788. ssl->options.haveECC = 0;
  9789. ssl->options.haveStaticECC = 0;
  9790. }
  9791. wc_ecc_free(key);
  9792. }
  9793. }
  9794. #ifdef WOLFSSL_SMALL_STACK
  9795. XFREE(key, ssl->heap, DYNAMIC_TYPE_ECC);
  9796. #endif
  9797. #endif
  9798. #ifndef NO_DH
  9799. if (!ssl->options.haveDH && ssl->ctx->haveDH) {
  9800. ssl->buffers.serverDH_P = ssl->ctx->serverDH_P;
  9801. ssl->buffers.serverDH_G = ssl->ctx->serverDH_G;
  9802. ssl->options.haveDH = 1;
  9803. }
  9804. #endif
  9805. }
  9806. if (InitSSL_Side(ssl, WOLFSSL_SERVER_END) != WOLFSSL_SUCCESS) {
  9807. WOLFSSL_MSG("Error initializing server side");
  9808. }
  9809. }
  9810. #endif /* OPENSSL_EXTRA || WOLFSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  9811. /* return true if connection established */
  9812. int wolfSSL_is_init_finished(const WOLFSSL* ssl)
  9813. {
  9814. if (ssl == NULL)
  9815. return 0;
  9816. /* Can't use ssl->options.connectState and ssl->options.acceptState
  9817. * because they differ in meaning for TLS <=1.2 and 1.3 */
  9818. if (ssl->options.handShakeState == HANDSHAKE_DONE)
  9819. return 1;
  9820. return 0;
  9821. }
  9822. #ifdef OPENSSL_EXTRA
  9823. void wolfSSL_CTX_set_tmp_rsa_callback(WOLFSSL_CTX* ctx,
  9824. WOLFSSL_RSA*(*f)(WOLFSSL*, int, int))
  9825. {
  9826. /* wolfSSL verifies all these internally */
  9827. (void)ctx;
  9828. (void)f;
  9829. }
  9830. void wolfSSL_set_shutdown(WOLFSSL* ssl, int opt)
  9831. {
  9832. WOLFSSL_ENTER("wolfSSL_set_shutdown");
  9833. if(ssl==NULL) {
  9834. WOLFSSL_MSG("Shutdown not set. ssl is null");
  9835. return;
  9836. }
  9837. ssl->options.sentNotify = (opt&WOLFSSL_SENT_SHUTDOWN) > 0;
  9838. ssl->options.closeNotify = (opt&WOLFSSL_RECEIVED_SHUTDOWN) > 0;
  9839. }
  9840. #endif
  9841. long wolfSSL_CTX_get_options(WOLFSSL_CTX* ctx)
  9842. {
  9843. WOLFSSL_ENTER("wolfSSL_CTX_get_options");
  9844. WOLFSSL_MSG("wolfSSL options are set through API calls and macros");
  9845. if(ctx == NULL)
  9846. return BAD_FUNC_ARG;
  9847. return ctx->mask;
  9848. }
  9849. /* forward declaration */
  9850. static long wolf_set_options(long old_op, long op);
  9851. long wolfSSL_CTX_set_options(WOLFSSL_CTX* ctx, long opt)
  9852. {
  9853. WOLFSSL_ENTER("wolfSSL_CTX_set_options");
  9854. if (ctx == NULL)
  9855. return BAD_FUNC_ARG;
  9856. ctx->mask = wolf_set_options(ctx->mask, opt);
  9857. #if defined(HAVE_SESSION_TICKET) && (defined(OPENSSL_EXTRA) \
  9858. || defined(HAVE_WEBSERVER) || defined(WOLFSSL_WPAS_SMALL))
  9859. if ((ctx->mask & WOLFSSL_OP_NO_TICKET) == WOLFSSL_OP_NO_TICKET) {
  9860. ctx->noTicketTls12 = 1;
  9861. }
  9862. /* This code is here for documentation purpose. You must not turn off
  9863. * session tickets with the WOLFSSL_OP_NO_TICKET option for TLSv1.3.
  9864. * Because we need to support both stateful and stateless tickets.
  9865. #ifdef WOLFSSL_TLS13
  9866. if ((ctx->mask & WOLFSSL_OP_NO_TICKET) == WOLFSSL_OP_NO_TICKET) {
  9867. ctx->noTicketTls13 = 1;
  9868. }
  9869. #endif
  9870. */
  9871. #endif
  9872. return ctx->mask;
  9873. }
  9874. long wolfSSL_CTX_clear_options(WOLFSSL_CTX* ctx, long opt)
  9875. {
  9876. WOLFSSL_ENTER("wolfSSL_CTX_clear_options");
  9877. if(ctx == NULL)
  9878. return BAD_FUNC_ARG;
  9879. ctx->mask &= ~opt;
  9880. return ctx->mask;
  9881. }
  9882. #ifdef OPENSSL_EXTRA
  9883. int wolfSSL_set_rfd(WOLFSSL* ssl, int rfd)
  9884. {
  9885. WOLFSSL_ENTER("wolfSSL_set_rfd");
  9886. ssl->rfd = rfd; /* not used directly to allow IO callbacks */
  9887. ssl->IOCB_ReadCtx = &ssl->rfd;
  9888. #ifdef WOLFSSL_DTLS
  9889. if (ssl->options.dtls) {
  9890. ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx;
  9891. ssl->buffers.dtlsCtx.rfd = rfd;
  9892. }
  9893. #endif
  9894. return WOLFSSL_SUCCESS;
  9895. }
  9896. int wolfSSL_set_wfd(WOLFSSL* ssl, int wfd)
  9897. {
  9898. WOLFSSL_ENTER("wolfSSL_set_wfd");
  9899. ssl->wfd = wfd; /* not used directly to allow IO callbacks */
  9900. ssl->IOCB_WriteCtx = &ssl->wfd;
  9901. return WOLFSSL_SUCCESS;
  9902. }
  9903. #endif /* OPENSSL_EXTRA */
  9904. #if !defined(NO_CERTS) && (defined(OPENSSL_EXTRA) || \
  9905. defined(WOLFSSL_WPAS_SMALL))
  9906. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  9907. /**
  9908. * Implemented in a similar way that ngx_ssl_ocsp_validate does it when
  9909. * SSL_get0_verified_chain is not available.
  9910. * @param ssl WOLFSSL object to extract certs from
  9911. * @return Stack of verified certs
  9912. */
  9913. WOLF_STACK_OF(WOLFSSL_X509) *wolfSSL_get0_verified_chain(const WOLFSSL *ssl)
  9914. {
  9915. WOLF_STACK_OF(WOLFSSL_X509)* chain = NULL;
  9916. WOLFSSL_X509_STORE_CTX* storeCtx = NULL;
  9917. WOLFSSL_X509* peerCert = NULL;
  9918. WOLFSSL_ENTER("wolfSSL_get0_verified_chain");
  9919. if (ssl == NULL || ssl->ctx == NULL) {
  9920. WOLFSSL_MSG("Bad parameter");
  9921. return NULL;
  9922. }
  9923. peerCert = wolfSSL_get_peer_certificate((WOLFSSL*)ssl);
  9924. if (peerCert == NULL) {
  9925. WOLFSSL_MSG("wolfSSL_get_peer_certificate error");
  9926. return NULL;
  9927. }
  9928. /* wolfSSL_get_peer_certificate returns a copy. We want the internal
  9929. * member so that we don't have to worry about free'ing it. We call
  9930. * wolfSSL_get_peer_certificate so that we don't have to worry about
  9931. * setting up the internal pointer. */
  9932. wolfSSL_X509_free(peerCert);
  9933. peerCert = (WOLFSSL_X509*)&ssl->peerCert;
  9934. chain = wolfSSL_get_peer_cert_chain(ssl);
  9935. if (chain == NULL) {
  9936. WOLFSSL_MSG("wolfSSL_get_peer_cert_chain error");
  9937. return NULL;
  9938. }
  9939. storeCtx = wolfSSL_X509_STORE_CTX_new();
  9940. if (storeCtx == NULL) {
  9941. WOLFSSL_MSG("wolfSSL_X509_STORE_CTX_new error");
  9942. return NULL;
  9943. }
  9944. if (wolfSSL_X509_STORE_CTX_init(storeCtx, SSL_STORE(ssl),
  9945. peerCert, chain) != WOLFSSL_SUCCESS) {
  9946. WOLFSSL_MSG("wolfSSL_X509_STORE_CTX_init error");
  9947. wolfSSL_X509_STORE_CTX_free(storeCtx);
  9948. return NULL;
  9949. }
  9950. if (wolfSSL_X509_verify_cert(storeCtx) <= 0) {
  9951. WOLFSSL_MSG("wolfSSL_X509_verify_cert error");
  9952. wolfSSL_X509_STORE_CTX_free(storeCtx);
  9953. return NULL;
  9954. }
  9955. wolfSSL_X509_STORE_CTX_free(storeCtx);
  9956. return chain;
  9957. }
  9958. #endif /* SESSION_CERTS && OPENSSL_EXTRA */
  9959. WOLFSSL_X509_STORE* wolfSSL_CTX_get_cert_store(WOLFSSL_CTX* ctx)
  9960. {
  9961. if (ctx == NULL) {
  9962. return NULL;
  9963. }
  9964. if (ctx->x509_store_pt != NULL)
  9965. return ctx->x509_store_pt;
  9966. return &ctx->x509_store;
  9967. }
  9968. void wolfSSL_CTX_set_cert_store(WOLFSSL_CTX* ctx, WOLFSSL_X509_STORE* str)
  9969. {
  9970. WOLFSSL_ENTER("wolfSSL_CTX_set_cert_store");
  9971. if (ctx == NULL || str == NULL || ctx->cm == str->cm) {
  9972. return;
  9973. }
  9974. if (wolfSSL_CertManager_up_ref(str->cm) != WOLFSSL_SUCCESS) {
  9975. WOLFSSL_MSG("wolfSSL_CertManager_up_ref error");
  9976. return;
  9977. }
  9978. /* free cert manager if have one */
  9979. if (ctx->cm != NULL) {
  9980. wolfSSL_CertManagerFree(ctx->cm);
  9981. }
  9982. ctx->cm = str->cm;
  9983. ctx->x509_store.cm = str->cm;
  9984. /* free existing store if it exists */
  9985. wolfSSL_X509_STORE_free(ctx->x509_store_pt);
  9986. ctx->x509_store.cache = str->cache;
  9987. ctx->x509_store_pt = str; /* take ownership of store and free it
  9988. with CTX free */
  9989. ctx->cm->x509_store_p = ctx->x509_store_pt;/* CTX has ownership
  9990. and free it with CTX free*/
  9991. }
  9992. #ifdef OPENSSL_ALL
  9993. int wolfSSL_CTX_set1_verify_cert_store(WOLFSSL_CTX* ctx,
  9994. WOLFSSL_X509_STORE* str)
  9995. {
  9996. WOLFSSL_ENTER("wolfSSL_CTX_set1_verify_cert_store");
  9997. if (ctx == NULL || str == NULL) {
  9998. WOLFSSL_MSG("Bad parameter");
  9999. return WOLFSSL_FAILURE;
  10000. }
  10001. /* NO-OP when setting existing store */
  10002. if (str == CTX_STORE(ctx))
  10003. return WOLFSSL_SUCCESS;
  10004. if (wolfSSL_X509_STORE_up_ref(str) != WOLFSSL_SUCCESS) {
  10005. WOLFSSL_MSG("wolfSSL_X509_STORE_up_ref error");
  10006. return WOLFSSL_FAILURE;
  10007. }
  10008. /* free existing store if it exists */
  10009. wolfSSL_X509_STORE_free(ctx->x509_store_pt);
  10010. ctx->x509_store_pt = str; /* take ownership of store and free it
  10011. with CTX free */
  10012. return WOLFSSL_SUCCESS;
  10013. }
  10014. #endif
  10015. int wolfSSL_set0_verify_cert_store(WOLFSSL *ssl, WOLFSSL_X509_STORE* str)
  10016. {
  10017. WOLFSSL_ENTER("wolfSSL_set0_verify_cert_store");
  10018. if (ssl == NULL || str == NULL) {
  10019. WOLFSSL_MSG("Bad parameter");
  10020. return WOLFSSL_FAILURE;
  10021. }
  10022. /* NO-OP when setting existing store */
  10023. if (str == SSL_STORE(ssl))
  10024. return WOLFSSL_SUCCESS;
  10025. /* free existing store if it exists */
  10026. wolfSSL_X509_STORE_free(ssl->x509_store_pt);
  10027. if (str == ssl->ctx->x509_store_pt)
  10028. ssl->x509_store_pt = NULL; /* if setting ctx store then just revert
  10029. to using that instead */
  10030. else
  10031. ssl->x509_store_pt = str; /* take ownership of store and free it
  10032. with SSL free */
  10033. return WOLFSSL_SUCCESS;
  10034. }
  10035. int wolfSSL_set1_verify_cert_store(WOLFSSL *ssl, WOLFSSL_X509_STORE* str)
  10036. {
  10037. WOLFSSL_ENTER("wolfSSL_set1_verify_cert_store");
  10038. if (ssl == NULL || str == NULL) {
  10039. WOLFSSL_MSG("Bad parameter");
  10040. return WOLFSSL_FAILURE;
  10041. }
  10042. /* NO-OP when setting existing store */
  10043. if (str == SSL_STORE(ssl))
  10044. return WOLFSSL_SUCCESS;
  10045. if (wolfSSL_X509_STORE_up_ref(str) != WOLFSSL_SUCCESS) {
  10046. WOLFSSL_MSG("wolfSSL_X509_STORE_up_ref error");
  10047. return WOLFSSL_FAILURE;
  10048. }
  10049. /* free existing store if it exists */
  10050. wolfSSL_X509_STORE_free(ssl->x509_store_pt);
  10051. if (str == ssl->ctx->x509_store_pt)
  10052. ssl->x509_store_pt = NULL; /* if setting ctx store then just revert
  10053. to using that instead */
  10054. else
  10055. ssl->x509_store_pt = str; /* take ownership of store and free it
  10056. with SSL free */
  10057. return WOLFSSL_SUCCESS;
  10058. }
  10059. #endif /* !NO_CERTS && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  10060. #ifdef WOLFSSL_ENCRYPTED_KEYS
  10061. void wolfSSL_CTX_set_default_passwd_cb_userdata(WOLFSSL_CTX* ctx,
  10062. void* userdata)
  10063. {
  10064. WOLFSSL_ENTER("wolfSSL_CTX_set_default_passwd_cb_userdata");
  10065. if (ctx)
  10066. ctx->passwd_userdata = userdata;
  10067. }
  10068. void wolfSSL_CTX_set_default_passwd_cb(WOLFSSL_CTX* ctx, wc_pem_password_cb*
  10069. cb)
  10070. {
  10071. WOLFSSL_ENTER("wolfSSL_CTX_set_default_passwd_cb");
  10072. if (ctx)
  10073. ctx->passwd_cb = cb;
  10074. }
  10075. wc_pem_password_cb* wolfSSL_CTX_get_default_passwd_cb(WOLFSSL_CTX *ctx)
  10076. {
  10077. if (ctx == NULL || ctx->passwd_cb == NULL) {
  10078. return NULL;
  10079. }
  10080. return ctx->passwd_cb;
  10081. }
  10082. void* wolfSSL_CTX_get_default_passwd_cb_userdata(WOLFSSL_CTX *ctx)
  10083. {
  10084. if (ctx == NULL) {
  10085. return NULL;
  10086. }
  10087. return ctx->passwd_userdata;
  10088. }
  10089. #endif /* WOLFSSL_ENCRYPTED_KEYS */
  10090. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_MEMCACHED)
  10091. unsigned long wolfSSL_ERR_get_error(void)
  10092. {
  10093. WOLFSSL_ENTER("wolfSSL_ERR_get_error");
  10094. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  10095. return wc_GetErrorNodeErr();
  10096. #else
  10097. return (unsigned long)(0 - NOT_COMPILED_IN);
  10098. #endif
  10099. }
  10100. #endif
  10101. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  10102. int wolfSSL_num_locks(void)
  10103. {
  10104. return 0;
  10105. }
  10106. void wolfSSL_set_locking_callback(mutex_cb* f)
  10107. {
  10108. WOLFSSL_ENTER("wolfSSL_set_locking_callback");
  10109. if (wc_SetMutexCb(f) != 0) {
  10110. WOLFSSL_MSG("Error when setting mutex call back");
  10111. }
  10112. }
  10113. mutex_cb* wolfSSL_get_locking_callback(void)
  10114. {
  10115. WOLFSSL_ENTER("wolfSSL_get_locking_callback");
  10116. return wc_GetMutexCb();
  10117. }
  10118. typedef unsigned long (idCb)(void);
  10119. static idCb* inner_idCb = NULL;
  10120. unsigned long wolfSSL_thread_id(void)
  10121. {
  10122. if (inner_idCb != NULL) {
  10123. return inner_idCb();
  10124. }
  10125. else {
  10126. return 0;
  10127. }
  10128. }
  10129. void wolfSSL_set_id_callback(unsigned long (*f)(void))
  10130. {
  10131. inner_idCb = f;
  10132. }
  10133. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  10134. #ifndef NO_BIO
  10135. /* print out and clear all errors */
  10136. void wolfSSL_ERR_print_errors(WOLFSSL_BIO* bio)
  10137. {
  10138. const char* file = NULL;
  10139. const char* reason = NULL;
  10140. int ret;
  10141. int line = 0;
  10142. char buf[WOLFSSL_MAX_ERROR_SZ * 2];
  10143. WOLFSSL_ENTER("wolfSSL_ERR_print_errors");
  10144. if (bio == NULL) {
  10145. WOLFSSL_MSG("BIO passed in was null");
  10146. return;
  10147. }
  10148. do {
  10149. ret = wc_PeekErrorNode(0, &file, &reason, &line);
  10150. if (ret >= 0) {
  10151. const char* r = wolfSSL_ERR_reason_error_string(0 - ret);
  10152. if (XSNPRINTF(buf, sizeof(buf),
  10153. "error:%d:wolfSSL library:%s:%s:%d\n",
  10154. ret, r, file, line)
  10155. >= (int)sizeof(buf))
  10156. {
  10157. WOLFSSL_MSG("Buffer overrun formatting error message");
  10158. }
  10159. wolfSSL_BIO_write(bio, buf, (int)XSTRLEN(buf));
  10160. wc_RemoveErrorNode(0);
  10161. }
  10162. } while (ret >= 0);
  10163. if (wolfSSL_BIO_write(bio, "", 1) != 1) {
  10164. WOLFSSL_MSG("Issue writing final string terminator");
  10165. }
  10166. }
  10167. #endif /* !NO_BIO */
  10168. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  10169. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  10170. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL) || \
  10171. defined(HAVE_SECRET_CALLBACK)
  10172. #if !defined(NO_WOLFSSL_SERVER)
  10173. /* Return the amount of random bytes copied over or error case.
  10174. * ssl : ssl struct after handshake
  10175. * out : buffer to hold random bytes
  10176. * outSz : either 0 (return max buffer sz) or size of out buffer
  10177. */
  10178. size_t wolfSSL_get_server_random(const WOLFSSL *ssl, unsigned char *out,
  10179. size_t outSz)
  10180. {
  10181. size_t size;
  10182. /* return max size of buffer */
  10183. if (outSz == 0) {
  10184. return RAN_LEN;
  10185. }
  10186. if (ssl == NULL || out == NULL) {
  10187. return 0;
  10188. }
  10189. if (ssl->arrays == NULL) {
  10190. WOLFSSL_MSG("Arrays struct not saved after handshake");
  10191. return 0;
  10192. }
  10193. if (outSz > RAN_LEN) {
  10194. size = RAN_LEN;
  10195. }
  10196. else {
  10197. size = outSz;
  10198. }
  10199. XMEMCPY(out, ssl->arrays->serverRandom, size);
  10200. return size;
  10201. }
  10202. #endif /* !NO_WOLFSSL_SERVER */
  10203. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL || HAVE_SECRET_CALLBACK */
  10204. #ifdef OPENSSL_EXTRA
  10205. #if !defined(NO_WOLFSSL_SERVER)
  10206. /* Used to get the peer ephemeral public key sent during the connection
  10207. * NOTE: currently wolfSSL_KeepHandshakeResources(WOLFSSL* ssl) must be called
  10208. * before the ephemeral key is stored.
  10209. * return WOLFSSL_SUCCESS on success */
  10210. int wolfSSL_get_peer_tmp_key(const WOLFSSL* ssl, WOLFSSL_EVP_PKEY** pkey)
  10211. {
  10212. WOLFSSL_EVP_PKEY* ret = NULL;
  10213. WOLFSSL_ENTER("wolfSSL_get_server_tmp_key");
  10214. if (ssl == NULL || pkey == NULL) {
  10215. WOLFSSL_MSG("Bad argument passed in");
  10216. return WOLFSSL_FAILURE;
  10217. }
  10218. #ifdef HAVE_ECC
  10219. if (ssl->peerEccKey != NULL) {
  10220. unsigned char* der;
  10221. const unsigned char* pt;
  10222. unsigned int derSz = 0;
  10223. int sz;
  10224. PRIVATE_KEY_UNLOCK();
  10225. if (wc_ecc_export_x963(ssl->peerEccKey, NULL, &derSz) !=
  10226. LENGTH_ONLY_E) {
  10227. WOLFSSL_MSG("get ecc der size failed");
  10228. PRIVATE_KEY_LOCK();
  10229. return WOLFSSL_FAILURE;
  10230. }
  10231. PRIVATE_KEY_LOCK();
  10232. derSz += MAX_SEQ_SZ + (2 * MAX_ALGO_SZ) + MAX_SEQ_SZ + TRAILING_ZERO;
  10233. der = (unsigned char*)XMALLOC(derSz, ssl->heap, DYNAMIC_TYPE_KEY);
  10234. if (der == NULL) {
  10235. WOLFSSL_MSG("Memory error");
  10236. return WOLFSSL_FAILURE;
  10237. }
  10238. if ((sz = wc_EccPublicKeyToDer(ssl->peerEccKey, der, derSz, 1)) <= 0) {
  10239. WOLFSSL_MSG("get ecc der failed");
  10240. XFREE(der, ssl->heap, DYNAMIC_TYPE_KEY);
  10241. return WOLFSSL_FAILURE;
  10242. }
  10243. pt = der; /* in case pointer gets advanced */
  10244. ret = wolfSSL_d2i_PUBKEY(NULL, &pt, sz);
  10245. XFREE(der, ssl->heap, DYNAMIC_TYPE_KEY);
  10246. }
  10247. #endif
  10248. *pkey = ret;
  10249. #ifdef HAVE_ECC
  10250. if (ret != NULL)
  10251. return WOLFSSL_SUCCESS;
  10252. else
  10253. #endif
  10254. return WOLFSSL_FAILURE;
  10255. }
  10256. #endif /* !NO_WOLFSSL_SERVER */
  10257. /**
  10258. * This function checks if any compiled in protocol versions are
  10259. * left enabled after calls to set_min or set_max API.
  10260. * @param major The SSL/TLS major version
  10261. * @return WOLFSSL_SUCCESS on valid settings and WOLFSSL_FAILURE when no
  10262. * protocol versions are left enabled.
  10263. */
  10264. static int CheckSslMethodVersion(byte major, unsigned long options)
  10265. {
  10266. int sanityConfirmed = 0;
  10267. (void)options;
  10268. switch (major) {
  10269. #ifndef NO_TLS
  10270. case SSLv3_MAJOR:
  10271. #ifdef WOLFSSL_ALLOW_SSLV3
  10272. if (!(options & WOLFSSL_OP_NO_SSLv3)) {
  10273. sanityConfirmed = 1;
  10274. }
  10275. #endif
  10276. #ifndef NO_OLD_TLS
  10277. if (!(options & WOLFSSL_OP_NO_TLSv1))
  10278. sanityConfirmed = 1;
  10279. if (!(options & WOLFSSL_OP_NO_TLSv1_1))
  10280. sanityConfirmed = 1;
  10281. #endif
  10282. #ifndef WOLFSSL_NO_TLS12
  10283. if (!(options & WOLFSSL_OP_NO_TLSv1_2))
  10284. sanityConfirmed = 1;
  10285. #endif
  10286. #ifdef WOLFSSL_TLS13
  10287. if (!(options & WOLFSSL_OP_NO_TLSv1_3))
  10288. sanityConfirmed = 1;
  10289. #endif
  10290. break;
  10291. #endif
  10292. #ifdef WOLFSSL_DTLS
  10293. case DTLS_MAJOR:
  10294. sanityConfirmed = 1;
  10295. break;
  10296. #endif
  10297. default:
  10298. WOLFSSL_MSG("Invalid major version");
  10299. return WOLFSSL_FAILURE;
  10300. }
  10301. if (!sanityConfirmed) {
  10302. WOLFSSL_MSG("All compiled in TLS versions disabled");
  10303. return WOLFSSL_FAILURE;
  10304. }
  10305. return WOLFSSL_SUCCESS;
  10306. }
  10307. /**
  10308. * protoVerTbl holds (D)TLS version numbers in ascending order.
  10309. * Except DTLS versions, the newer version is located in the latter part of
  10310. * the table. This table is referred by wolfSSL_CTX_set_min_proto_version and
  10311. * wolfSSL_CTX_set_max_proto_version.
  10312. */
  10313. static const int protoVerTbl[] = {
  10314. SSL3_VERSION,
  10315. TLS1_VERSION,
  10316. TLS1_1_VERSION,
  10317. TLS1_2_VERSION,
  10318. TLS1_3_VERSION,
  10319. DTLS1_VERSION,
  10320. DTLS1_2_VERSION
  10321. };
  10322. /* number of protocol versions listed in protoVerTbl */
  10323. #define NUMBER_OF_PROTOCOLS (sizeof(protoVerTbl)/sizeof(int))
  10324. /**
  10325. * wolfSSL_CTX_set_min_proto_version attempts to set the minimum protocol
  10326. * version to use by SSL objects created from this WOLFSSL_CTX.
  10327. * This API guarantees that a version of SSL/TLS lower than specified
  10328. * here will not be allowed. If the version specified is not compiled in
  10329. * then this API sets the lowest compiled in protocol version.
  10330. * This API also accept 0 as version, to set the minimum version automatically.
  10331. * CheckSslMethodVersion() is called to check if any remaining protocol versions
  10332. * are enabled.
  10333. * @param ctx The wolfSSL CONTEXT factory for spawning SSL/TLS objects
  10334. * @param version Any of the following
  10335. * * 0
  10336. * * SSL3_VERSION
  10337. * * TLS1_VERSION
  10338. * * TLS1_1_VERSION
  10339. * * TLS1_2_VERSION
  10340. * * TLS1_3_VERSION
  10341. * * DTLS1_VERSION
  10342. * * DTLS1_2_VERSION
  10343. * @return WOLFSSL_SUCCESS on valid settings and WOLFSSL_FAILURE when no
  10344. * protocol versions are left enabled.
  10345. */
  10346. static int Set_CTX_min_proto_version(WOLFSSL_CTX* ctx, int version)
  10347. {
  10348. WOLFSSL_ENTER("wolfSSL_CTX_set_min_proto_version_ex");
  10349. if (ctx == NULL) {
  10350. return WOLFSSL_FAILURE;
  10351. }
  10352. switch (version) {
  10353. #ifndef NO_TLS
  10354. case SSL3_VERSION:
  10355. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  10356. ctx->minDowngrade = SSLv3_MINOR;
  10357. break;
  10358. #endif
  10359. case TLS1_VERSION:
  10360. #ifdef WOLFSSL_ALLOW_TLSV10
  10361. ctx->minDowngrade = TLSv1_MINOR;
  10362. break;
  10363. #endif
  10364. case TLS1_1_VERSION:
  10365. #ifndef NO_OLD_TLS
  10366. ctx->minDowngrade = TLSv1_1_MINOR;
  10367. break;
  10368. #endif
  10369. case TLS1_2_VERSION:
  10370. #ifndef WOLFSSL_NO_TLS12
  10371. ctx->minDowngrade = TLSv1_2_MINOR;
  10372. break;
  10373. #endif
  10374. case TLS1_3_VERSION:
  10375. #ifdef WOLFSSL_TLS13
  10376. ctx->minDowngrade = TLSv1_3_MINOR;
  10377. break;
  10378. #endif
  10379. #endif
  10380. #ifdef WOLFSSL_DTLS
  10381. case DTLS1_VERSION:
  10382. #ifndef NO_OLD_TLS
  10383. ctx->minDowngrade = DTLS_MINOR;
  10384. break;
  10385. #endif
  10386. case DTLS1_2_VERSION:
  10387. ctx->minDowngrade = DTLSv1_2_MINOR;
  10388. break;
  10389. #endif
  10390. default:
  10391. WOLFSSL_MSG("Unrecognized protocol version or not compiled in");
  10392. return WOLFSSL_FAILURE;
  10393. }
  10394. switch (version) {
  10395. #ifndef NO_TLS
  10396. case TLS1_3_VERSION:
  10397. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2);
  10398. FALL_THROUGH;
  10399. case TLS1_2_VERSION:
  10400. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_1);
  10401. FALL_THROUGH;
  10402. case TLS1_1_VERSION:
  10403. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1);
  10404. FALL_THROUGH;
  10405. case TLS1_VERSION:
  10406. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_SSLv3);
  10407. break;
  10408. case SSL3_VERSION:
  10409. case SSL2_VERSION:
  10410. /* Nothing to do here */
  10411. break;
  10412. #endif
  10413. #ifdef WOLFSSL_DTLS
  10414. case DTLS1_VERSION:
  10415. case DTLS1_2_VERSION:
  10416. break;
  10417. #endif
  10418. default:
  10419. WOLFSSL_MSG("Unrecognized protocol version or not compiled in");
  10420. return WOLFSSL_FAILURE;
  10421. }
  10422. return CheckSslMethodVersion(ctx->method->version.major, ctx->mask);
  10423. }
  10424. /* Sets the min protocol version allowed with WOLFSSL_CTX
  10425. * returns WOLFSSL_SUCCESS on success */
  10426. int wolfSSL_CTX_set_min_proto_version(WOLFSSL_CTX* ctx, int version)
  10427. {
  10428. int ret;
  10429. int proto = 0;
  10430. int maxProto = 0;
  10431. int i;
  10432. int idx = 0;
  10433. WOLFSSL_ENTER("wolfSSL_CTX_set_min_proto_version");
  10434. if (ctx == NULL) {
  10435. return WOLFSSL_FAILURE;
  10436. }
  10437. if (version != 0) {
  10438. proto = version;
  10439. ctx->minProto = 0; /* turn min proto flag off */
  10440. for (i = 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) {
  10441. if (protoVerTbl[i] == version) {
  10442. break;
  10443. }
  10444. }
  10445. }
  10446. else {
  10447. /* when 0 is specified as version, try to find out the min version */
  10448. for (i = 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) {
  10449. ret = Set_CTX_min_proto_version(ctx, protoVerTbl[i]);
  10450. if (ret == WOLFSSL_SUCCESS) {
  10451. proto = protoVerTbl[i];
  10452. ctx->minProto = 1; /* turn min proto flag on */
  10453. break;
  10454. }
  10455. }
  10456. }
  10457. /* check case where max > min , if so then clear the NO_* options
  10458. * i is the index into the table for proto version used, see if the max
  10459. * proto version index found is smaller */
  10460. maxProto = wolfSSL_CTX_get_max_proto_version(ctx);
  10461. for (idx = 0; (unsigned)idx < NUMBER_OF_PROTOCOLS; idx++) {
  10462. if (protoVerTbl[idx] == maxProto) {
  10463. break;
  10464. }
  10465. }
  10466. if (idx < i) {
  10467. wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_TLSv1 |
  10468. WOLFSSL_OP_NO_TLSv1_1 | WOLFSSL_OP_NO_TLSv1_2 |
  10469. WOLFSSL_OP_NO_TLSv1_3);
  10470. }
  10471. ret = Set_CTX_min_proto_version(ctx, proto);
  10472. return ret;
  10473. }
  10474. /**
  10475. * wolfSSL_CTX_set_max_proto_version attempts to set the maximum protocol
  10476. * version to use by SSL objects created from this WOLFSSL_CTX.
  10477. * This API guarantees that a version of SSL/TLS higher than specified
  10478. * here will not be allowed. If the version specified is not compiled in
  10479. * then this API sets the highest compiled in protocol version.
  10480. * This API also accept 0 as version, to set the maximum version automatically.
  10481. * CheckSslMethodVersion() is called to check if any remaining protocol versions
  10482. * are enabled.
  10483. * @param ctx The wolfSSL CONTEXT factory for spawning SSL/TLS objects
  10484. * @param ver Any of the following
  10485. * * 0
  10486. * * SSL3_VERSION
  10487. * * TLS1_VERSION
  10488. * * TLS1_1_VERSION
  10489. * * TLS1_2_VERSION
  10490. * * TLS1_3_VERSION
  10491. * * DTLS1_VERSION
  10492. * * DTLS1_2_VERSION
  10493. * @return WOLFSSL_SUCCESS on valid settings and WOLFSSL_FAILURE when no
  10494. * protocol versions are left enabled.
  10495. */
  10496. static int Set_CTX_max_proto_version(WOLFSSL_CTX* ctx, int ver)
  10497. {
  10498. int ret;
  10499. WOLFSSL_ENTER("Set_CTX_max_proto_version");
  10500. if (!ctx || !ctx->method) {
  10501. WOLFSSL_MSG("Bad parameter");
  10502. return WOLFSSL_FAILURE;
  10503. }
  10504. switch (ver) {
  10505. case SSL2_VERSION:
  10506. WOLFSSL_MSG("wolfSSL does not support SSLv2");
  10507. return WOLFSSL_FAILURE;
  10508. #ifndef NO_TLS
  10509. case SSL3_VERSION:
  10510. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1);
  10511. FALL_THROUGH;
  10512. case TLS1_VERSION:
  10513. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_1);
  10514. FALL_THROUGH;
  10515. case TLS1_1_VERSION:
  10516. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2);
  10517. FALL_THROUGH;
  10518. case TLS1_2_VERSION:
  10519. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_3);
  10520. FALL_THROUGH;
  10521. case TLS1_3_VERSION:
  10522. /* Nothing to do here */
  10523. break;
  10524. #endif
  10525. #ifdef WOLFSSL_DTLS
  10526. case DTLS1_VERSION:
  10527. case DTLS1_2_VERSION:
  10528. break;
  10529. #endif
  10530. default:
  10531. WOLFSSL_MSG("Unrecognized protocol version or not compiled in");
  10532. return WOLFSSL_FAILURE;
  10533. }
  10534. ret = CheckSslMethodVersion(ctx->method->version.major, ctx->mask);
  10535. if (ret == WOLFSSL_SUCCESS) {
  10536. /* Check the major */
  10537. switch (ver) {
  10538. #ifndef NO_TLS
  10539. case SSL3_VERSION:
  10540. case TLS1_VERSION:
  10541. case TLS1_1_VERSION:
  10542. case TLS1_2_VERSION:
  10543. case TLS1_3_VERSION:
  10544. if (ctx->method->version.major != SSLv3_MAJOR) {
  10545. WOLFSSL_MSG("Mismatched protocol version");
  10546. return WOLFSSL_FAILURE;
  10547. }
  10548. break;
  10549. #endif
  10550. #ifdef WOLFSSL_DTLS
  10551. case DTLS1_VERSION:
  10552. case DTLS1_2_VERSION:
  10553. if (ctx->method->version.major != DTLS_MAJOR) {
  10554. WOLFSSL_MSG("Mismatched protocol version");
  10555. return WOLFSSL_FAILURE;
  10556. }
  10557. break;
  10558. #endif
  10559. }
  10560. /* Update the method */
  10561. switch (ver) {
  10562. case SSL2_VERSION:
  10563. WOLFSSL_MSG("wolfSSL does not support SSLv2");
  10564. return WOLFSSL_FAILURE;
  10565. #ifndef NO_TLS
  10566. case SSL3_VERSION:
  10567. ctx->method->version.minor = SSLv3_MINOR;
  10568. break;
  10569. case TLS1_VERSION:
  10570. ctx->method->version.minor = TLSv1_MINOR;
  10571. break;
  10572. case TLS1_1_VERSION:
  10573. ctx->method->version.minor = TLSv1_1_MINOR;
  10574. break;
  10575. case TLS1_2_VERSION:
  10576. ctx->method->version.minor = TLSv1_2_MINOR;
  10577. break;
  10578. case TLS1_3_VERSION:
  10579. ctx->method->version.minor = TLSv1_3_MINOR;
  10580. break;
  10581. #endif
  10582. #ifdef WOLFSSL_DTLS
  10583. case DTLS1_VERSION:
  10584. ctx->method->version.minor = DTLS_MINOR;
  10585. break;
  10586. case DTLS1_2_VERSION:
  10587. ctx->method->version.minor = DTLSv1_2_MINOR;
  10588. break;
  10589. #endif
  10590. default:
  10591. WOLFSSL_MSG("Unrecognized protocol version or not compiled in");
  10592. return WOLFSSL_FAILURE;
  10593. }
  10594. }
  10595. return ret;
  10596. }
  10597. /* Sets the max protocol version allowed with WOLFSSL_CTX
  10598. * returns WOLFSSL_SUCCESS on success */
  10599. int wolfSSL_CTX_set_max_proto_version(WOLFSSL_CTX* ctx, int version)
  10600. {
  10601. int i;
  10602. int ret = WOLFSSL_FAILURE;
  10603. int minProto;
  10604. WOLFSSL_ENTER("wolfSSL_CTX_set_max_proto_version");
  10605. if (ctx == NULL) {
  10606. return ret;
  10607. }
  10608. /* clear out flags and reset min protocol version */
  10609. minProto = wolfSSL_CTX_get_min_proto_version(ctx);
  10610. wolfSSL_CTX_clear_options(ctx,
  10611. WOLFSSL_OP_NO_TLSv1 | WOLFSSL_OP_NO_TLSv1_1 |
  10612. WOLFSSL_OP_NO_TLSv1_2 | WOLFSSL_OP_NO_TLSv1_3);
  10613. wolfSSL_CTX_set_min_proto_version(ctx, minProto);
  10614. if (version != 0) {
  10615. ctx->maxProto = 0; /* turn max proto flag off */
  10616. return Set_CTX_max_proto_version(ctx, version);
  10617. }
  10618. /* when 0 is specified as version, try to find out the min version from
  10619. * the bottom to top of the protoverTbl.
  10620. */
  10621. for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) {
  10622. ret = Set_CTX_max_proto_version(ctx, protoVerTbl[i]);
  10623. if (ret == WOLFSSL_SUCCESS) {
  10624. ctx->maxProto = 1; /* turn max proto flag on */
  10625. break;
  10626. }
  10627. }
  10628. return ret;
  10629. }
  10630. static int Set_SSL_min_proto_version(WOLFSSL* ssl, int ver)
  10631. {
  10632. WOLFSSL_ENTER("Set_SSL_min_proto_version");
  10633. if (ssl == NULL) {
  10634. return WOLFSSL_FAILURE;
  10635. }
  10636. switch (ver) {
  10637. #ifndef NO_TLS
  10638. case SSL3_VERSION:
  10639. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  10640. ssl->options.minDowngrade = SSLv3_MINOR;
  10641. break;
  10642. #endif
  10643. case TLS1_VERSION:
  10644. #ifdef WOLFSSL_ALLOW_TLSV10
  10645. ssl->options.minDowngrade = TLSv1_MINOR;
  10646. break;
  10647. #endif
  10648. case TLS1_1_VERSION:
  10649. #ifndef NO_OLD_TLS
  10650. ssl->options.minDowngrade = TLSv1_1_MINOR;
  10651. break;
  10652. #endif
  10653. case TLS1_2_VERSION:
  10654. #ifndef WOLFSSL_NO_TLS12
  10655. ssl->options.minDowngrade = TLSv1_2_MINOR;
  10656. break;
  10657. #endif
  10658. case TLS1_3_VERSION:
  10659. #ifdef WOLFSSL_TLS13
  10660. ssl->options.minDowngrade = TLSv1_3_MINOR;
  10661. break;
  10662. #endif
  10663. #endif
  10664. #ifdef WOLFSSL_DTLS
  10665. case DTLS1_VERSION:
  10666. #ifndef NO_OLD_TLS
  10667. ssl->options.minDowngrade = DTLS_MINOR;
  10668. break;
  10669. #endif
  10670. case DTLS1_2_VERSION:
  10671. ssl->options.minDowngrade = DTLSv1_2_MINOR;
  10672. break;
  10673. #endif
  10674. default:
  10675. WOLFSSL_MSG("Unrecognized protocol version or not compiled in");
  10676. return WOLFSSL_FAILURE;
  10677. }
  10678. switch (ver) {
  10679. #ifndef NO_TLS
  10680. case TLS1_3_VERSION:
  10681. ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_2;
  10682. FALL_THROUGH;
  10683. case TLS1_2_VERSION:
  10684. ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_1;
  10685. FALL_THROUGH;
  10686. case TLS1_1_VERSION:
  10687. ssl->options.mask |= WOLFSSL_OP_NO_TLSv1;
  10688. FALL_THROUGH;
  10689. case TLS1_VERSION:
  10690. ssl->options.mask |= WOLFSSL_OP_NO_SSLv3;
  10691. break;
  10692. case SSL3_VERSION:
  10693. case SSL2_VERSION:
  10694. /* Nothing to do here */
  10695. break;
  10696. #endif
  10697. #ifdef WOLFSSL_DTLS
  10698. case DTLS1_VERSION:
  10699. case DTLS1_2_VERSION:
  10700. break;
  10701. #endif
  10702. default:
  10703. WOLFSSL_MSG("Unrecognized protocol version or not compiled in");
  10704. return WOLFSSL_FAILURE;
  10705. }
  10706. return CheckSslMethodVersion(ssl->version.major, ssl->options.mask);
  10707. }
  10708. int wolfSSL_set_min_proto_version(WOLFSSL* ssl, int version)
  10709. {
  10710. int i;
  10711. int ret = WOLFSSL_FAILURE;;
  10712. WOLFSSL_ENTER("wolfSSL_set_min_proto_version");
  10713. if (ssl == NULL) {
  10714. return WOLFSSL_FAILURE;
  10715. }
  10716. if (version != 0) {
  10717. return Set_SSL_min_proto_version(ssl, version);
  10718. }
  10719. /* when 0 is specified as version, try to find out the min version */
  10720. for (i= 0; (unsigned)i < NUMBER_OF_PROTOCOLS; i++) {
  10721. ret = Set_SSL_min_proto_version(ssl, protoVerTbl[i]);
  10722. if (ret == WOLFSSL_SUCCESS)
  10723. break;
  10724. }
  10725. return ret;
  10726. }
  10727. static int Set_SSL_max_proto_version(WOLFSSL* ssl, int ver)
  10728. {
  10729. WOLFSSL_ENTER("Set_SSL_max_proto_version");
  10730. if (!ssl) {
  10731. WOLFSSL_MSG("Bad parameter");
  10732. return WOLFSSL_FAILURE;
  10733. }
  10734. switch (ver) {
  10735. case SSL2_VERSION:
  10736. WOLFSSL_MSG("wolfSSL does not support SSLv2");
  10737. return WOLFSSL_FAILURE;
  10738. #ifndef NO_TLS
  10739. case SSL3_VERSION:
  10740. ssl->options.mask |= WOLFSSL_OP_NO_TLSv1;
  10741. FALL_THROUGH;
  10742. case TLS1_VERSION:
  10743. ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_1;
  10744. FALL_THROUGH;
  10745. case TLS1_1_VERSION:
  10746. ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_2;
  10747. FALL_THROUGH;
  10748. case TLS1_2_VERSION:
  10749. ssl->options.mask |= WOLFSSL_OP_NO_TLSv1_3;
  10750. FALL_THROUGH;
  10751. case TLS1_3_VERSION:
  10752. /* Nothing to do here */
  10753. break;
  10754. #endif
  10755. #ifdef WOLFSSL_DTLS
  10756. case DTLS1_VERSION:
  10757. case DTLS1_2_VERSION:
  10758. break;
  10759. #endif
  10760. default:
  10761. WOLFSSL_MSG("Unrecognized protocol version or not compiled in");
  10762. return WOLFSSL_FAILURE;
  10763. }
  10764. return CheckSslMethodVersion(ssl->version.major, ssl->options.mask);
  10765. }
  10766. int wolfSSL_set_max_proto_version(WOLFSSL* ssl, int version)
  10767. {
  10768. int i;
  10769. int ret = WOLFSSL_FAILURE;;
  10770. WOLFSSL_ENTER("wolfSSL_set_max_proto_version");
  10771. if (ssl == NULL) {
  10772. return WOLFSSL_FAILURE;
  10773. }
  10774. if (version != 0) {
  10775. return Set_SSL_max_proto_version(ssl, version);
  10776. }
  10777. /* when 0 is specified as version, try to find out the min version from
  10778. * the bottom to top of the protoverTbl.
  10779. */
  10780. for (i = NUMBER_OF_PROTOCOLS -1; i >= 0; i--) {
  10781. ret = Set_SSL_max_proto_version(ssl, protoVerTbl[i]);
  10782. if (ret == WOLFSSL_SUCCESS)
  10783. break;
  10784. }
  10785. return ret;
  10786. }
  10787. static int GetMinProtoVersion(int minDowngrade)
  10788. {
  10789. int ret;
  10790. switch (minDowngrade) {
  10791. #ifndef NO_OLD_TLS
  10792. #ifdef WOLFSSL_ALLOW_SSLV3
  10793. case SSLv3_MINOR:
  10794. ret = SSL3_VERSION;
  10795. break;
  10796. #endif
  10797. #ifdef WOLFSSL_ALLOW_TLSV10
  10798. case TLSv1_MINOR:
  10799. ret = TLS1_VERSION;
  10800. break;
  10801. #endif
  10802. case TLSv1_1_MINOR:
  10803. ret = TLS1_1_VERSION;
  10804. break;
  10805. #endif
  10806. #ifndef WOLFSSL_NO_TLS12
  10807. case TLSv1_2_MINOR:
  10808. ret = TLS1_2_VERSION;
  10809. break;
  10810. #endif
  10811. #ifdef WOLFSSL_TLS13
  10812. case TLSv1_3_MINOR:
  10813. ret = TLS1_3_VERSION;
  10814. break;
  10815. #endif
  10816. default:
  10817. ret = 0;
  10818. break;
  10819. }
  10820. return ret;
  10821. }
  10822. int wolfSSL_CTX_get_min_proto_version(WOLFSSL_CTX* ctx)
  10823. {
  10824. int ret = 0;
  10825. WOLFSSL_ENTER("wolfSSL_CTX_get_min_proto_version");
  10826. if (ctx != NULL) {
  10827. if (ctx->minProto) {
  10828. ret = 0;
  10829. }
  10830. else {
  10831. ret = GetMinProtoVersion(ctx->minDowngrade);
  10832. }
  10833. }
  10834. else {
  10835. ret = GetMinProtoVersion(WOLFSSL_MIN_DOWNGRADE);
  10836. }
  10837. WOLFSSL_LEAVE("wolfSSL_CTX_get_min_proto_version", ret);
  10838. return ret;
  10839. }
  10840. /* returns the maximum allowed protocol version given the 'options' used
  10841. * returns WOLFSSL_FATAL_ERROR on no match */
  10842. static int GetMaxProtoVersion(long options)
  10843. {
  10844. #ifndef NO_TLS
  10845. #ifdef WOLFSSL_TLS13
  10846. if (!(options & WOLFSSL_OP_NO_TLSv1_3))
  10847. return TLS1_3_VERSION;
  10848. #endif
  10849. #ifndef WOLFSSL_NO_TLS12
  10850. if (!(options & WOLFSSL_OP_NO_TLSv1_2))
  10851. return TLS1_2_VERSION;
  10852. #endif
  10853. #ifndef NO_OLD_TLS
  10854. if (!(options & WOLFSSL_OP_NO_TLSv1_1))
  10855. return TLS1_1_VERSION;
  10856. #ifdef WOLFSSL_ALLOW_TLSV10
  10857. if (!(options & WOLFSSL_OP_NO_TLSv1))
  10858. return TLS1_VERSION;
  10859. #endif
  10860. #ifdef WOLFSSL_ALLOW_SSLV3
  10861. if (!(options & WOLFSSL_OP_NO_SSLv3))
  10862. return SSL3_VERSION;
  10863. #endif
  10864. #endif
  10865. #else
  10866. (void)options;
  10867. #endif /* NO_TLS */
  10868. return WOLFSSL_FATAL_ERROR;
  10869. }
  10870. /* returns the maximum protocol version for 'ctx' */
  10871. int wolfSSL_CTX_get_max_proto_version(WOLFSSL_CTX* ctx)
  10872. {
  10873. int ret = 0;
  10874. long options = 0; /* default to nothing set */
  10875. WOLFSSL_ENTER("wolfSSL_CTX_get_max_proto_version");
  10876. if (ctx != NULL) {
  10877. options = wolfSSL_CTX_get_options(ctx);
  10878. }
  10879. if ((ctx != NULL) && ctx->maxProto) {
  10880. ret = 0;
  10881. }
  10882. else {
  10883. ret = GetMaxProtoVersion(options);
  10884. }
  10885. WOLFSSL_LEAVE("wolfSSL_CTX_get_max_proto_version", ret);
  10886. if (ret == WOLFSSL_FATAL_ERROR) {
  10887. WOLFSSL_MSG("Error getting max proto version");
  10888. ret = 0; /* setting ret to 0 to match compat return */
  10889. }
  10890. return ret;
  10891. }
  10892. #endif /* OPENSSL_EXTRA */
  10893. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL) || \
  10894. defined(HAVE_SECRET_CALLBACK)
  10895. #if !defined(NO_WOLFSSL_CLIENT)
  10896. /* Return the amount of random bytes copied over or error case.
  10897. * ssl : ssl struct after handshake
  10898. * out : buffer to hold random bytes
  10899. * outSz : either 0 (return max buffer sz) or size of out buffer
  10900. */
  10901. size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out,
  10902. size_t outSz)
  10903. {
  10904. size_t size;
  10905. /* return max size of buffer */
  10906. if (outSz == 0) {
  10907. return RAN_LEN;
  10908. }
  10909. if (ssl == NULL || out == NULL) {
  10910. return 0;
  10911. }
  10912. if (ssl->arrays == NULL) {
  10913. WOLFSSL_MSG("Arrays struct not saved after handshake");
  10914. return 0;
  10915. }
  10916. if (outSz > RAN_LEN) {
  10917. size = RAN_LEN;
  10918. }
  10919. else {
  10920. size = outSz;
  10921. }
  10922. XMEMCPY(out, ssl->arrays->clientRandom, size);
  10923. return size;
  10924. }
  10925. #endif /* !NO_WOLFSSL_CLIENT */
  10926. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL || HAVE_SECRET_CALLBACK */
  10927. #ifdef OPENSSL_EXTRA
  10928. unsigned long wolfSSLeay(void)
  10929. {
  10930. return SSLEAY_VERSION_NUMBER;
  10931. }
  10932. unsigned long wolfSSL_OpenSSL_version_num(void)
  10933. {
  10934. return OPENSSL_VERSION_NUMBER;
  10935. }
  10936. const char* wolfSSLeay_version(int type)
  10937. {
  10938. (void)type;
  10939. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  10940. return wolfSSL_OpenSSL_version(type);
  10941. #else
  10942. return wolfSSL_OpenSSL_version();
  10943. #endif
  10944. }
  10945. #endif /* OPENSSL_EXTRA */
  10946. #ifdef OPENSSL_EXTRA
  10947. void wolfSSL_ERR_free_strings(void)
  10948. {
  10949. /* handled internally */
  10950. }
  10951. void wolfSSL_cleanup_all_ex_data(void)
  10952. {
  10953. /* nothing to do here */
  10954. }
  10955. #endif /* OPENSSL_EXTRA */
  10956. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) || \
  10957. defined(HAVE_CURL)
  10958. void wolfSSL_ERR_clear_error(void)
  10959. {
  10960. WOLFSSL_ENTER("wolfSSL_ERR_clear_error");
  10961. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  10962. wc_ClearErrorNodes();
  10963. #endif
  10964. }
  10965. #endif
  10966. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  10967. int wolfSSL_clear(WOLFSSL* ssl)
  10968. {
  10969. WOLFSSL_ENTER("wolfSSL_clear");
  10970. if (ssl == NULL) {
  10971. return WOLFSSL_FAILURE;
  10972. }
  10973. if (!ssl->options.handShakeDone) {
  10974. /* Only reset the session if we didn't complete a handshake */
  10975. wolfSSL_FreeSession(ssl->ctx, ssl->session);
  10976. ssl->session = wolfSSL_NewSession(ssl->heap);
  10977. if (ssl->session == NULL) {
  10978. return WOLFSSL_FAILURE;
  10979. }
  10980. }
  10981. /* reset error */
  10982. ssl->error = 0;
  10983. /* reset option bits */
  10984. ssl->options.isClosed = 0;
  10985. ssl->options.connReset = 0;
  10986. ssl->options.sentNotify = 0;
  10987. ssl->options.closeNotify = 0;
  10988. ssl->options.sendVerify = 0;
  10989. ssl->options.serverState = NULL_STATE;
  10990. ssl->options.clientState = NULL_STATE;
  10991. ssl->options.connectState = CONNECT_BEGIN;
  10992. ssl->options.acceptState = ACCEPT_BEGIN;
  10993. ssl->options.handShakeState = NULL_STATE;
  10994. ssl->options.handShakeDone = 0;
  10995. ssl->options.processReply = 0; /* doProcessInit */
  10996. ssl->options.havePeerVerify = 0;
  10997. ssl->options.havePeerCert = 0;
  10998. ssl->options.peerAuthGood = 0;
  10999. ssl->options.tls1_3 = 0;
  11000. ssl->options.haveSessionId = 0;
  11001. ssl->options.tls = 0;
  11002. ssl->options.tls1_1 = 0;
  11003. #ifdef WOLFSSL_DTLS
  11004. ssl->options.dtlsStateful = 0;
  11005. #endif
  11006. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11007. ssl->options.noPskDheKe = 0;
  11008. #ifdef HAVE_SUPPORTED_CURVES
  11009. ssl->options.onlyPskDheKe = 0;
  11010. #endif
  11011. #endif
  11012. #ifdef HAVE_SESSION_TICKET
  11013. #ifdef WOLFSSL_TLS13
  11014. ssl->options.ticketsSent = 0;
  11015. #endif
  11016. ssl->options.rejectTicket = 0;
  11017. #endif
  11018. #ifdef WOLFSSL_EARLY_DATA
  11019. ssl->earlyData = no_early_data;
  11020. ssl->earlyDataSz = 0;
  11021. #endif
  11022. #if defined(HAVE_TLS_EXTENSIONS) && !defined(NO_TLS)
  11023. TLSX_FreeAll(ssl->extensions, ssl->heap);
  11024. ssl->extensions = NULL;
  11025. #endif
  11026. if (ssl->keys.encryptionOn) {
  11027. ForceZero(ssl->buffers.inputBuffer.buffer -
  11028. ssl->buffers.inputBuffer.offset,
  11029. ssl->buffers.inputBuffer.bufferSize);
  11030. #ifdef WOLFSSL_CHECK_MEM_ZERO
  11031. wc_MemZero_Check(ssl->buffers.inputBuffer.buffer -
  11032. ssl->buffers.inputBuffer.offset,
  11033. ssl->buffers.inputBuffer.bufferSize);
  11034. #endif
  11035. }
  11036. ssl->keys.encryptionOn = 0;
  11037. XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
  11038. if (InitSSL_Suites(ssl) != WOLFSSL_SUCCESS)
  11039. return WOLFSSL_FAILURE;
  11040. if (InitHandshakeHashes(ssl) != 0)
  11041. return WOLFSSL_FAILURE;
  11042. #ifdef KEEP_PEER_CERT
  11043. FreeX509(&ssl->peerCert);
  11044. InitX509(&ssl->peerCert, 0, ssl->heap);
  11045. #endif
  11046. #ifdef WOLFSSL_QUIC
  11047. wolfSSL_quic_clear(ssl);
  11048. #endif
  11049. return WOLFSSL_SUCCESS;
  11050. }
  11051. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  11052. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_MEMCACHED)
  11053. long wolfSSL_CTX_set_mode(WOLFSSL_CTX* ctx, long mode)
  11054. {
  11055. /* WOLFSSL_MODE_ACCEPT_MOVING_WRITE_BUFFER is wolfSSL default mode */
  11056. WOLFSSL_ENTER("wolfSSL_CTX_set_mode");
  11057. switch(mode) {
  11058. case SSL_MODE_ENABLE_PARTIAL_WRITE:
  11059. ctx->partialWrite = 1;
  11060. break;
  11061. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  11062. case SSL_MODE_RELEASE_BUFFERS:
  11063. WOLFSSL_MSG("SSL_MODE_RELEASE_BUFFERS not implemented.");
  11064. break;
  11065. #endif
  11066. case SSL_MODE_AUTO_RETRY:
  11067. ctx->autoRetry = 1;
  11068. break;
  11069. default:
  11070. WOLFSSL_MSG("Mode Not Implemented");
  11071. }
  11072. /* SSL_MODE_AUTO_RETRY
  11073. * Should not return -1 with renegotiation on read/write */
  11074. return mode;
  11075. }
  11076. long wolfSSL_CTX_clear_mode(WOLFSSL_CTX* ctx, long mode)
  11077. {
  11078. /* WOLFSSL_MODE_ACCEPT_MOVING_WRITE_BUFFER is wolfSSL default mode */
  11079. WOLFSSL_ENTER("wolfSSL_CTX_clear_mode");
  11080. switch(mode) {
  11081. case SSL_MODE_ENABLE_PARTIAL_WRITE:
  11082. ctx->partialWrite = 0;
  11083. break;
  11084. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  11085. case SSL_MODE_RELEASE_BUFFERS:
  11086. WOLFSSL_MSG("SSL_MODE_RELEASE_BUFFERS not implemented.");
  11087. break;
  11088. #endif
  11089. case SSL_MODE_AUTO_RETRY:
  11090. ctx->autoRetry = 0;
  11091. break;
  11092. default:
  11093. WOLFSSL_MSG("Mode Not Implemented");
  11094. }
  11095. /* SSL_MODE_AUTO_RETRY
  11096. * Should not return -1 with renegotiation on read/write */
  11097. return 0;
  11098. }
  11099. #endif
  11100. #ifdef OPENSSL_EXTRA
  11101. #ifndef NO_WOLFSSL_STUB
  11102. long wolfSSL_SSL_get_mode(WOLFSSL* ssl)
  11103. {
  11104. /* TODO: */
  11105. (void)ssl;
  11106. WOLFSSL_STUB("SSL_get_mode");
  11107. return 0;
  11108. }
  11109. #endif
  11110. #ifndef NO_WOLFSSL_STUB
  11111. long wolfSSL_CTX_get_mode(WOLFSSL_CTX* ctx)
  11112. {
  11113. /* TODO: */
  11114. (void)ctx;
  11115. WOLFSSL_STUB("SSL_CTX_get_mode");
  11116. return 0;
  11117. }
  11118. #endif
  11119. #ifndef NO_WOLFSSL_STUB
  11120. void wolfSSL_CTX_set_default_read_ahead(WOLFSSL_CTX* ctx, int m)
  11121. {
  11122. /* TODO: maybe? */
  11123. (void)ctx;
  11124. (void)m;
  11125. WOLFSSL_STUB("SSL_CTX_set_default_read_ahead");
  11126. }
  11127. #endif
  11128. /* returns the unsigned error value and increments the pointer into the
  11129. * error queue.
  11130. *
  11131. * file pointer to file name
  11132. * line gets set to line number of error when not NULL
  11133. */
  11134. unsigned long wolfSSL_ERR_get_error_line(const char** file, int* line)
  11135. {
  11136. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  11137. int ret = wc_PullErrorNode(file, NULL, line);
  11138. if (ret < 0) {
  11139. if (ret == BAD_STATE_E) return 0; /* no errors in queue */
  11140. WOLFSSL_MSG("Issue getting error node");
  11141. WOLFSSL_LEAVE("wolfSSL_ERR_get_error_line", ret);
  11142. ret = 0 - ret; /* return absolute value of error */
  11143. /* panic and try to clear out nodes */
  11144. wc_ClearErrorNodes();
  11145. }
  11146. return (unsigned long)ret;
  11147. #else
  11148. (void)file;
  11149. (void)line;
  11150. return 0;
  11151. #endif
  11152. }
  11153. #if (defined(DEBUG_WOLFSSL) || defined(OPENSSL_EXTRA)) && \
  11154. (!defined(_WIN32) && !defined(NO_ERROR_QUEUE))
  11155. static const char WOLFSSL_SYS_ACCEPT_T[] = "accept";
  11156. static const char WOLFSSL_SYS_BIND_T[] = "bind";
  11157. static const char WOLFSSL_SYS_CONNECT_T[] = "connect";
  11158. static const char WOLFSSL_SYS_FOPEN_T[] = "fopen";
  11159. static const char WOLFSSL_SYS_FREAD_T[] = "fread";
  11160. static const char WOLFSSL_SYS_GETADDRINFO_T[] = "getaddrinfo";
  11161. static const char WOLFSSL_SYS_GETSOCKOPT_T[] = "getsockopt";
  11162. static const char WOLFSSL_SYS_GETSOCKNAME_T[] = "getsockname";
  11163. static const char WOLFSSL_SYS_GETHOSTBYNAME_T[] = "gethostbyname";
  11164. static const char WOLFSSL_SYS_GETNAMEINFO_T[] = "getnameinfo";
  11165. static const char WOLFSSL_SYS_GETSERVBYNAME_T[] = "getservbyname";
  11166. static const char WOLFSSL_SYS_IOCTLSOCKET_T[] = "ioctlsocket";
  11167. static const char WOLFSSL_SYS_LISTEN_T[] = "listen";
  11168. static const char WOLFSSL_SYS_OPENDIR_T[] = "opendir";
  11169. static const char WOLFSSL_SYS_SETSOCKOPT_T[] = "setsockopt";
  11170. static const char WOLFSSL_SYS_SOCKET_T[] = "socket";
  11171. /* switch with int mapped to function name for compatibility */
  11172. static const char* wolfSSL_ERR_sys_func(int fun)
  11173. {
  11174. switch (fun) {
  11175. case WOLFSSL_SYS_ACCEPT: return WOLFSSL_SYS_ACCEPT_T;
  11176. case WOLFSSL_SYS_BIND: return WOLFSSL_SYS_BIND_T;
  11177. case WOLFSSL_SYS_CONNECT: return WOLFSSL_SYS_CONNECT_T;
  11178. case WOLFSSL_SYS_FOPEN: return WOLFSSL_SYS_FOPEN_T;
  11179. case WOLFSSL_SYS_FREAD: return WOLFSSL_SYS_FREAD_T;
  11180. case WOLFSSL_SYS_GETADDRINFO: return WOLFSSL_SYS_GETADDRINFO_T;
  11181. case WOLFSSL_SYS_GETSOCKOPT: return WOLFSSL_SYS_GETSOCKOPT_T;
  11182. case WOLFSSL_SYS_GETSOCKNAME: return WOLFSSL_SYS_GETSOCKNAME_T;
  11183. case WOLFSSL_SYS_GETHOSTBYNAME: return WOLFSSL_SYS_GETHOSTBYNAME_T;
  11184. case WOLFSSL_SYS_GETNAMEINFO: return WOLFSSL_SYS_GETNAMEINFO_T;
  11185. case WOLFSSL_SYS_GETSERVBYNAME: return WOLFSSL_SYS_GETSERVBYNAME_T;
  11186. case WOLFSSL_SYS_IOCTLSOCKET: return WOLFSSL_SYS_IOCTLSOCKET_T;
  11187. case WOLFSSL_SYS_LISTEN: return WOLFSSL_SYS_LISTEN_T;
  11188. case WOLFSSL_SYS_OPENDIR: return WOLFSSL_SYS_OPENDIR_T;
  11189. case WOLFSSL_SYS_SETSOCKOPT: return WOLFSSL_SYS_SETSOCKOPT_T;
  11190. case WOLFSSL_SYS_SOCKET: return WOLFSSL_SYS_SOCKET_T;
  11191. default:
  11192. return "NULL";
  11193. }
  11194. }
  11195. #endif /* DEBUG_WOLFSSL */
  11196. void wolfSSL_ERR_put_error(int lib, int fun, int err, const char* file,
  11197. int line)
  11198. {
  11199. WOLFSSL_ENTER("wolfSSL_ERR_put_error");
  11200. #if !defined(DEBUG_WOLFSSL) && !defined(OPENSSL_EXTRA)
  11201. (void)fun;
  11202. (void)err;
  11203. (void)file;
  11204. (void)line;
  11205. WOLFSSL_MSG("Not compiled in debug mode");
  11206. #elif defined(OPENSSL_EXTRA) && \
  11207. (defined(_WIN32) || defined(NO_ERROR_QUEUE))
  11208. (void)fun;
  11209. (void)file;
  11210. (void)line;
  11211. WOLFSSL_ERROR(err);
  11212. #else
  11213. WOLFSSL_ERROR_LINE(err, wolfSSL_ERR_sys_func(fun), (unsigned int)line,
  11214. file, NULL);
  11215. #endif
  11216. (void)lib;
  11217. }
  11218. /* Similar to wolfSSL_ERR_get_error_line but takes in a flags argument for
  11219. * more flexibility.
  11220. *
  11221. * file output pointer to file where error happened
  11222. * line output to line number of error
  11223. * data output data. Is a string if ERR_TXT_STRING flag is used
  11224. * flags output format of output
  11225. *
  11226. * Returns the error value or 0 if no errors are in the queue
  11227. */
  11228. unsigned long wolfSSL_ERR_get_error_line_data(const char** file, int* line,
  11229. const char** data, int *flags)
  11230. {
  11231. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  11232. int ret;
  11233. WOLFSSL_ENTER("wolfSSL_ERR_get_error_line_data");
  11234. if (flags != NULL)
  11235. *flags = ERR_TXT_STRING; /* Clear the flags */
  11236. ret = wc_PullErrorNode(file, data, line);
  11237. if (ret < 0) {
  11238. if (ret == BAD_STATE_E) return 0; /* no errors in queue */
  11239. WOLFSSL_MSG("Error with pulling error node!");
  11240. WOLFSSL_LEAVE("wolfSSL_ERR_get_error_line_data", ret);
  11241. ret = 0 - ret; /* return absolute value of error */
  11242. /* panic and try to clear out nodes */
  11243. wc_ClearErrorNodes();
  11244. }
  11245. return (unsigned long)ret;
  11246. #else
  11247. WOLFSSL_ENTER("wolfSSL_ERR_get_error_line_data");
  11248. WOLFSSL_MSG("Error queue turned off, can not get error line");
  11249. (void)file;
  11250. (void)line;
  11251. (void)data;
  11252. (void)flags;
  11253. return 0;
  11254. #endif
  11255. }
  11256. #endif /* OPENSSL_EXTRA */
  11257. #if (defined(KEEP_PEER_CERT) && defined(SESSION_CERTS)) || \
  11258. (defined(OPENSSL_EXTRA) && defined(SESSION_CERTS))
  11259. /* Decode the X509 DER encoded certificate into a WOLFSSL_X509 object.
  11260. *
  11261. * x509 WOLFSSL_X509 object to decode into.
  11262. * in X509 DER data.
  11263. * len Length of the X509 DER data.
  11264. * returns the new certificate on success, otherwise NULL.
  11265. */
  11266. static int DecodeToX509(WOLFSSL_X509* x509, const byte* in, int len)
  11267. {
  11268. int ret;
  11269. #ifdef WOLFSSL_SMALL_STACK
  11270. DecodedCert* cert;
  11271. #else
  11272. DecodedCert cert[1];
  11273. #endif
  11274. if (x509 == NULL || in == NULL || len <= 0)
  11275. return BAD_FUNC_ARG;
  11276. #ifdef WOLFSSL_SMALL_STACK
  11277. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  11278. DYNAMIC_TYPE_DCERT);
  11279. if (cert == NULL)
  11280. return MEMORY_E;
  11281. #endif
  11282. /* Create a DecodedCert object and copy fields into WOLFSSL_X509 object.
  11283. */
  11284. InitDecodedCert(cert, (byte*)in, len, NULL);
  11285. if ((ret = ParseCertRelative(cert, CERT_TYPE, 0, NULL)) == 0) {
  11286. /* Check if x509 was not previously initialized by wolfSSL_X509_new() */
  11287. if (x509->dynamicMemory != TRUE)
  11288. InitX509(x509, 0, NULL);
  11289. ret = CopyDecodedToX509(x509, cert);
  11290. }
  11291. FreeDecodedCert(cert);
  11292. #ifdef WOLFSSL_SMALL_STACK
  11293. XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
  11294. #endif
  11295. return ret;
  11296. }
  11297. #endif /* (KEEP_PEER_CERT & SESSION_CERTS) || (OPENSSL_EXTRA & SESSION_CERTS) */
  11298. #ifdef KEEP_PEER_CERT
  11299. WOLFSSL_ABI
  11300. WOLFSSL_X509* wolfSSL_get_peer_certificate(WOLFSSL* ssl)
  11301. {
  11302. WOLFSSL_X509* ret = NULL;
  11303. WOLFSSL_ENTER("wolfSSL_get_peer_certificate");
  11304. if (ssl != NULL) {
  11305. if (ssl->peerCert.issuer.sz)
  11306. ret = wolfSSL_X509_dup(&ssl->peerCert);
  11307. #ifdef SESSION_CERTS
  11308. else if (ssl->session->chain.count > 0) {
  11309. if (DecodeToX509(&ssl->peerCert,
  11310. ssl->session->chain.certs[0].buffer,
  11311. ssl->session->chain.certs[0].length) == 0) {
  11312. ret = wolfSSL_X509_dup(&ssl->peerCert);
  11313. }
  11314. }
  11315. #endif
  11316. }
  11317. WOLFSSL_LEAVE("wolfSSL_get_peer_certificate", ret != NULL);
  11318. return ret;
  11319. }
  11320. #endif /* KEEP_PEER_CERT */
  11321. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  11322. /* Return stack of peer certs.
  11323. * Caller does not need to free return. The stack is Free'd when WOLFSSL* ssl
  11324. * is.
  11325. */
  11326. WOLF_STACK_OF(WOLFSSL_X509)* wolfSSL_get_peer_cert_chain(const WOLFSSL* ssl)
  11327. {
  11328. WOLFSSL_ENTER("wolfSSL_get_peer_cert_chain");
  11329. if (ssl == NULL)
  11330. return NULL;
  11331. /* Try to populate if NULL or empty */
  11332. if (ssl->peerCertChain == NULL ||
  11333. wolfSSL_sk_X509_num(ssl->peerCertChain) == 0)
  11334. wolfSSL_set_peer_cert_chain((WOLFSSL*) ssl);
  11335. return ssl->peerCertChain;
  11336. }
  11337. #ifndef WOLFSSL_QT
  11338. static int x509GetIssuerFromCM(WOLFSSL_X509 **issuer, WOLFSSL_CERT_MANAGER* cm,
  11339. WOLFSSL_X509 *x);
  11340. /**
  11341. * Recursively push the issuer CA chain onto the stack
  11342. * @param cm The cert manager that is queried for the issuer
  11343. * @param x This cert's issuer will be queried in cm
  11344. * @param sk The issuer is pushed onto this stack
  11345. * @return WOLFSSL_SUCCESS on success
  11346. * WOLFSSL_FAILURE on no issuer found
  11347. * WOLFSSL_FATAL_ERROR on a fatal error
  11348. */
  11349. static int PushCAx509Chain(WOLFSSL_CERT_MANAGER* cm,
  11350. WOLFSSL_X509 *x, WOLFSSL_STACK* sk)
  11351. {
  11352. WOLFSSL_X509* issuer[MAX_CHAIN_DEPTH];
  11353. int i;
  11354. int push = 1;
  11355. int ret = WOLFSSL_SUCCESS;
  11356. for (i = 0; i < MAX_CHAIN_DEPTH; i++) {
  11357. if (x509GetIssuerFromCM(&issuer[i], cm, x)
  11358. != WOLFSSL_SUCCESS)
  11359. break;
  11360. x = issuer[i];
  11361. }
  11362. if (i == 0) /* No further chain found */
  11363. return WOLFSSL_FAILURE;
  11364. i--;
  11365. for (; i >= 0; i--) {
  11366. if (push) {
  11367. if (wolfSSL_sk_X509_push(sk, issuer[i]) != WOLFSSL_SUCCESS) {
  11368. wolfSSL_X509_free(issuer[i]);
  11369. ret = WOLFSSL_FATAL_ERROR;
  11370. push = 0; /* Free the rest of the unpushed certs */
  11371. }
  11372. }
  11373. else {
  11374. wolfSSL_X509_free(issuer[i]);
  11375. }
  11376. }
  11377. return ret;
  11378. }
  11379. #endif /* !WOLFSSL_QT */
  11380. /* Builds up and creates a stack of peer certificates for ssl->peerCertChain
  11381. based off of the ssl session chain. Attempts to place CA certificates
  11382. at the bottom of the stack. Returns stack of WOLFSSL_X509 certs or
  11383. NULL on failure */
  11384. WOLF_STACK_OF(WOLFSSL_X509)* wolfSSL_set_peer_cert_chain(WOLFSSL* ssl)
  11385. {
  11386. WOLFSSL_STACK* sk;
  11387. WOLFSSL_X509* x509;
  11388. int i = 0;
  11389. int ret;
  11390. WOLFSSL_ENTER("wolfSSL_set_peer_cert_chain");
  11391. if ((ssl == NULL) || (ssl->session->chain.count == 0))
  11392. return NULL;
  11393. sk = wolfSSL_sk_X509_new_null();
  11394. i = ssl->session->chain.count-1;
  11395. for (; i >= 0; i--) {
  11396. x509 = wolfSSL_X509_new_ex(ssl->heap);
  11397. if (x509 == NULL) {
  11398. WOLFSSL_MSG("Error Creating X509");
  11399. wolfSSL_sk_X509_pop_free(sk, NULL);
  11400. return NULL;
  11401. }
  11402. ret = DecodeToX509(x509, ssl->session->chain.certs[i].buffer,
  11403. ssl->session->chain.certs[i].length);
  11404. #if !defined(WOLFSSL_QT)
  11405. if (ret == 0 && i == ssl->session->chain.count-1) {
  11406. /* On the last element in the chain try to add the CA chain
  11407. * first if we have one for this cert */
  11408. SSL_CM_WARNING(ssl);
  11409. if (PushCAx509Chain(SSL_CM(ssl), x509, sk)
  11410. == WOLFSSL_FATAL_ERROR) {
  11411. ret = WOLFSSL_FATAL_ERROR;
  11412. }
  11413. }
  11414. #endif
  11415. if (ret != 0 || wolfSSL_sk_X509_push(sk, x509) != WOLFSSL_SUCCESS) {
  11416. WOLFSSL_MSG("Error decoding cert");
  11417. wolfSSL_X509_free(x509);
  11418. wolfSSL_sk_X509_pop_free(sk, NULL);
  11419. return NULL;
  11420. }
  11421. }
  11422. if (sk == NULL) {
  11423. WOLFSSL_MSG("Null session chain");
  11424. }
  11425. #if defined(OPENSSL_ALL)
  11426. else if (ssl->options.side == WOLFSSL_SERVER_END) {
  11427. /* to be compliant with openssl
  11428. first element is kept as peer cert on server side.*/
  11429. wolfSSL_sk_X509_pop(sk);
  11430. }
  11431. #endif
  11432. if (ssl->peerCertChain != NULL)
  11433. wolfSSL_sk_X509_pop_free(ssl->peerCertChain, NULL);
  11434. /* This is Free'd when ssl is Free'd */
  11435. ssl->peerCertChain = sk;
  11436. return sk;
  11437. }
  11438. #endif /* SESSION_CERTS && OPENSSL_EXTRA */
  11439. #ifndef NO_CERTS
  11440. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  11441. /* create a generic wolfSSL stack node
  11442. * returns a new WOLFSSL_STACK structure on success */
  11443. WOLFSSL_STACK* wolfSSL_sk_new_node(void* heap)
  11444. {
  11445. WOLFSSL_STACK* sk;
  11446. WOLFSSL_ENTER("wolfSSL_sk_new_node");
  11447. sk = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), heap,
  11448. DYNAMIC_TYPE_OPENSSL);
  11449. if (sk != NULL) {
  11450. XMEMSET(sk, 0, sizeof(*sk));
  11451. sk->heap = heap;
  11452. }
  11453. return sk;
  11454. }
  11455. /* free's node but does not free internal data such as in->data.x509 */
  11456. void wolfSSL_sk_free_node(WOLFSSL_STACK* in)
  11457. {
  11458. if (in != NULL) {
  11459. XFREE(in, in->heap, DYNAMIC_TYPE_OPENSSL);
  11460. }
  11461. }
  11462. /* pushes node "in" onto "stack" and returns pointer to the new stack on success
  11463. * also handles internal "num" for number of nodes on stack
  11464. * return WOLFSSL_SUCCESS on success
  11465. */
  11466. int wolfSSL_sk_push_node(WOLFSSL_STACK** stack, WOLFSSL_STACK* in)
  11467. {
  11468. if (stack == NULL || in == NULL) {
  11469. return WOLFSSL_FAILURE;
  11470. }
  11471. if (*stack == NULL) {
  11472. in->num = 1;
  11473. *stack = in;
  11474. return WOLFSSL_SUCCESS;
  11475. }
  11476. in->num = (*stack)->num + 1;
  11477. in->next = *stack;
  11478. *stack = in;
  11479. return WOLFSSL_SUCCESS;
  11480. }
  11481. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  11482. static WC_INLINE int compare_WOLFSSL_CIPHER(
  11483. WOLFSSL_CIPHER *a,
  11484. WOLFSSL_CIPHER *b)
  11485. {
  11486. if ((a->cipherSuite0 == b->cipherSuite0) &&
  11487. (a->cipherSuite == b->cipherSuite) &&
  11488. (a->ssl == b->ssl) &&
  11489. (XMEMCMP(a->description, b->description, sizeof a->description) == 0) &&
  11490. (a->offset == b->offset) &&
  11491. (a->in_stack == b->in_stack) &&
  11492. (a->bits == b->bits))
  11493. return 0;
  11494. else
  11495. return -1;
  11496. }
  11497. #endif /* OPENSSL_ALL || WOLFSSL_QT */
  11498. /* return 1 on success 0 on fail */
  11499. int wolfSSL_sk_push(WOLFSSL_STACK* sk, const void *data)
  11500. {
  11501. WOLFSSL_STACK* node;
  11502. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  11503. WOLFSSL_CIPHER ciph;
  11504. #endif
  11505. WOLFSSL_ENTER("wolfSSL_sk_push");
  11506. if (!sk) {
  11507. return WOLFSSL_FAILURE;
  11508. }
  11509. /* Check if empty data */
  11510. switch (sk->type) {
  11511. case STACK_TYPE_CIPHER:
  11512. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  11513. /* check if entire struct is zero */
  11514. XMEMSET(&ciph, 0, sizeof(WOLFSSL_CIPHER));
  11515. if (compare_WOLFSSL_CIPHER(&sk->data.cipher, &ciph) == 0) {
  11516. sk->data.cipher = *(WOLFSSL_CIPHER*)data;
  11517. sk->num = 1;
  11518. if (sk->hash_fn) {
  11519. sk->hash = sk->hash_fn(&sk->data.cipher);
  11520. }
  11521. return WOLFSSL_SUCCESS;
  11522. }
  11523. break;
  11524. #endif
  11525. case STACK_TYPE_X509:
  11526. case STACK_TYPE_GEN_NAME:
  11527. case STACK_TYPE_BIO:
  11528. case STACK_TYPE_OBJ:
  11529. case STACK_TYPE_STRING:
  11530. case STACK_TYPE_ACCESS_DESCRIPTION:
  11531. case STACK_TYPE_X509_EXT:
  11532. case STACK_TYPE_X509_REQ_ATTR:
  11533. case STACK_TYPE_NULL:
  11534. case STACK_TYPE_X509_NAME:
  11535. case STACK_TYPE_X509_NAME_ENTRY:
  11536. case STACK_TYPE_CONF_VALUE:
  11537. case STACK_TYPE_X509_INFO:
  11538. case STACK_TYPE_BY_DIR_entry:
  11539. case STACK_TYPE_BY_DIR_hash:
  11540. case STACK_TYPE_X509_OBJ:
  11541. case STACK_TYPE_DIST_POINT:
  11542. case STACK_TYPE_X509_CRL:
  11543. default:
  11544. /* All other types are pointers */
  11545. if (!sk->data.generic) {
  11546. sk->data.generic = (void*)data;
  11547. sk->num = 1;
  11548. #ifdef OPENSSL_ALL
  11549. if (sk->hash_fn) {
  11550. sk->hash = sk->hash_fn(sk->data.generic);
  11551. }
  11552. #endif
  11553. return WOLFSSL_SUCCESS;
  11554. }
  11555. break;
  11556. }
  11557. /* stack already has value(s) create a new node and add more */
  11558. node = wolfSSL_sk_new_node(sk->heap);
  11559. if (!node) {
  11560. WOLFSSL_MSG("Memory error");
  11561. return WOLFSSL_FAILURE;
  11562. }
  11563. /* push new x509 onto head of stack */
  11564. node->next = sk->next;
  11565. node->type = sk->type;
  11566. sk->next = node;
  11567. sk->num += 1;
  11568. #ifdef OPENSSL_ALL
  11569. node->hash_fn = sk->hash_fn;
  11570. node->hash = sk->hash;
  11571. sk->hash = 0;
  11572. #endif
  11573. switch (sk->type) {
  11574. case STACK_TYPE_CIPHER:
  11575. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  11576. node->data.cipher = sk->data.cipher;
  11577. sk->data.cipher = *(WOLFSSL_CIPHER*)data;
  11578. if (sk->hash_fn) {
  11579. sk->hash = sk->hash_fn(&sk->data.cipher);
  11580. }
  11581. break;
  11582. #endif
  11583. case STACK_TYPE_X509:
  11584. case STACK_TYPE_GEN_NAME:
  11585. case STACK_TYPE_BIO:
  11586. case STACK_TYPE_OBJ:
  11587. case STACK_TYPE_STRING:
  11588. case STACK_TYPE_ACCESS_DESCRIPTION:
  11589. case STACK_TYPE_X509_EXT:
  11590. case STACK_TYPE_X509_REQ_ATTR:
  11591. case STACK_TYPE_NULL:
  11592. case STACK_TYPE_X509_NAME:
  11593. case STACK_TYPE_X509_NAME_ENTRY:
  11594. case STACK_TYPE_CONF_VALUE:
  11595. case STACK_TYPE_X509_INFO:
  11596. case STACK_TYPE_BY_DIR_entry:
  11597. case STACK_TYPE_BY_DIR_hash:
  11598. case STACK_TYPE_X509_OBJ:
  11599. case STACK_TYPE_DIST_POINT:
  11600. case STACK_TYPE_X509_CRL:
  11601. default:
  11602. /* All other types are pointers */
  11603. node->data.generic = sk->data.generic;
  11604. sk->data.generic = (void*)data;
  11605. #ifdef OPENSSL_ALL
  11606. if (sk->hash_fn) {
  11607. sk->hash = sk->hash_fn(sk->data.generic);
  11608. }
  11609. #endif
  11610. break;
  11611. }
  11612. return WOLFSSL_SUCCESS;
  11613. }
  11614. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  11615. #ifdef OPENSSL_EXTRA
  11616. /* returns the node at index "idx", NULL if not found */
  11617. WOLFSSL_STACK* wolfSSL_sk_get_node(WOLFSSL_STACK* sk, int idx)
  11618. {
  11619. int i;
  11620. WOLFSSL_STACK* ret = NULL;
  11621. WOLFSSL_STACK* current;
  11622. current = sk;
  11623. for (i = 0; i <= idx && current != NULL; i++) {
  11624. if (i == idx) {
  11625. ret = current;
  11626. break;
  11627. }
  11628. current = current->next;
  11629. }
  11630. return ret;
  11631. }
  11632. #endif /* OPENSSL_EXTRA */
  11633. #ifdef OPENSSL_EXTRA
  11634. #if defined(OPENSSL_ALL)
  11635. void *wolfSSL_lh_retrieve(WOLFSSL_STACK *sk, void *data)
  11636. {
  11637. unsigned long hash;
  11638. WOLFSSL_ENTER("wolfSSL_lh_retrieve");
  11639. if (!sk || !data) {
  11640. WOLFSSL_MSG("Bad parameters");
  11641. return NULL;
  11642. }
  11643. if (!sk->hash_fn) {
  11644. WOLFSSL_MSG("No hash function defined");
  11645. return NULL;
  11646. }
  11647. hash = sk->hash_fn(data);
  11648. while (sk) {
  11649. /* Calc hash if not done so yet */
  11650. if (!sk->hash) {
  11651. switch (sk->type) {
  11652. case STACK_TYPE_CIPHER:
  11653. sk->hash = sk->hash_fn(&sk->data.cipher);
  11654. break;
  11655. case STACK_TYPE_X509:
  11656. case STACK_TYPE_GEN_NAME:
  11657. case STACK_TYPE_BIO:
  11658. case STACK_TYPE_OBJ:
  11659. case STACK_TYPE_STRING:
  11660. case STACK_TYPE_ACCESS_DESCRIPTION:
  11661. case STACK_TYPE_X509_EXT:
  11662. case STACK_TYPE_X509_REQ_ATTR:
  11663. case STACK_TYPE_NULL:
  11664. case STACK_TYPE_X509_NAME:
  11665. case STACK_TYPE_X509_NAME_ENTRY:
  11666. case STACK_TYPE_CONF_VALUE:
  11667. case STACK_TYPE_X509_INFO:
  11668. case STACK_TYPE_BY_DIR_entry:
  11669. case STACK_TYPE_BY_DIR_hash:
  11670. case STACK_TYPE_X509_OBJ:
  11671. case STACK_TYPE_DIST_POINT:
  11672. case STACK_TYPE_X509_CRL:
  11673. default:
  11674. sk->hash = sk->hash_fn(sk->data.generic);
  11675. break;
  11676. }
  11677. }
  11678. if (sk->hash == hash) {
  11679. switch (sk->type) {
  11680. case STACK_TYPE_CIPHER:
  11681. return &sk->data.cipher;
  11682. case STACK_TYPE_X509:
  11683. case STACK_TYPE_GEN_NAME:
  11684. case STACK_TYPE_BIO:
  11685. case STACK_TYPE_OBJ:
  11686. case STACK_TYPE_STRING:
  11687. case STACK_TYPE_ACCESS_DESCRIPTION:
  11688. case STACK_TYPE_X509_EXT:
  11689. case STACK_TYPE_X509_REQ_ATTR:
  11690. case STACK_TYPE_NULL:
  11691. case STACK_TYPE_X509_NAME:
  11692. case STACK_TYPE_X509_NAME_ENTRY:
  11693. case STACK_TYPE_CONF_VALUE:
  11694. case STACK_TYPE_X509_INFO:
  11695. case STACK_TYPE_BY_DIR_entry:
  11696. case STACK_TYPE_BY_DIR_hash:
  11697. case STACK_TYPE_X509_OBJ:
  11698. case STACK_TYPE_DIST_POINT:
  11699. case STACK_TYPE_X509_CRL:
  11700. default:
  11701. return sk->data.generic;
  11702. }
  11703. }
  11704. sk = sk->next;
  11705. }
  11706. return NULL;
  11707. }
  11708. #endif /* OPENSSL_ALL */
  11709. #endif /* OPENSSL_EXTRA */
  11710. /* OPENSSL_EXTRA is needed for wolfSSL_X509_d21 function
  11711. KEEP_OUR_CERT is to insure ability for returning ssl certificate */
  11712. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  11713. defined(KEEP_OUR_CERT)
  11714. WOLFSSL_X509* wolfSSL_get_certificate(WOLFSSL* ssl)
  11715. {
  11716. if (ssl == NULL) {
  11717. return NULL;
  11718. }
  11719. if (ssl->buffers.weOwnCert) {
  11720. if (ssl->ourCert == NULL) {
  11721. if (ssl->buffers.certificate == NULL) {
  11722. WOLFSSL_MSG("Certificate buffer not set!");
  11723. return NULL;
  11724. }
  11725. #ifndef WOLFSSL_X509_STORE_CERTS
  11726. ssl->ourCert = wolfSSL_X509_d2i_ex(NULL,
  11727. ssl->buffers.certificate->buffer,
  11728. ssl->buffers.certificate->length,
  11729. ssl->heap);
  11730. #endif
  11731. }
  11732. return ssl->ourCert;
  11733. }
  11734. else { /* if cert not owned get parent ctx cert or return null */
  11735. if (ssl->ctx) {
  11736. if (ssl->ctx->ourCert == NULL) {
  11737. if (ssl->ctx->certificate == NULL) {
  11738. WOLFSSL_MSG("Ctx Certificate buffer not set!");
  11739. return NULL;
  11740. }
  11741. #ifndef WOLFSSL_X509_STORE_CERTS
  11742. ssl->ctx->ourCert = wolfSSL_X509_d2i_ex(NULL,
  11743. ssl->ctx->certificate->buffer,
  11744. ssl->ctx->certificate->length,
  11745. ssl->heap);
  11746. #endif
  11747. ssl->ctx->ownOurCert = 1;
  11748. }
  11749. return ssl->ctx->ourCert;
  11750. }
  11751. }
  11752. return NULL;
  11753. }
  11754. WOLFSSL_X509* wolfSSL_CTX_get0_certificate(WOLFSSL_CTX* ctx)
  11755. {
  11756. if (ctx) {
  11757. if (ctx->ourCert == NULL) {
  11758. if (ctx->certificate == NULL) {
  11759. WOLFSSL_MSG("Ctx Certificate buffer not set!");
  11760. return NULL;
  11761. }
  11762. #ifndef WOLFSSL_X509_STORE_CERTS
  11763. ctx->ourCert = wolfSSL_X509_d2i_ex(NULL,
  11764. ctx->certificate->buffer,
  11765. ctx->certificate->length, ctx->heap);
  11766. #endif
  11767. ctx->ownOurCert = 1;
  11768. }
  11769. return ctx->ourCert;
  11770. }
  11771. return NULL;
  11772. }
  11773. #endif /* OPENSSL_EXTRA && KEEP_OUR_CERT */
  11774. #endif /* NO_CERTS */
  11775. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  11776. void wolfSSL_set_connect_state(WOLFSSL* ssl)
  11777. {
  11778. WOLFSSL_ENTER("wolfSSL_set_connect_state");
  11779. if (ssl == NULL) {
  11780. WOLFSSL_MSG("WOLFSSL struct pointer passed in was null");
  11781. return;
  11782. }
  11783. #ifndef NO_DH
  11784. /* client creates its own DH parameters on handshake */
  11785. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  11786. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  11787. DYNAMIC_TYPE_PUBLIC_KEY);
  11788. }
  11789. ssl->buffers.serverDH_P.buffer = NULL;
  11790. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  11791. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  11792. DYNAMIC_TYPE_PUBLIC_KEY);
  11793. }
  11794. ssl->buffers.serverDH_G.buffer = NULL;
  11795. #endif
  11796. if (InitSSL_Side(ssl, WOLFSSL_CLIENT_END) != WOLFSSL_SUCCESS) {
  11797. WOLFSSL_MSG("Error initializing client side");
  11798. }
  11799. }
  11800. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  11801. int wolfSSL_get_shutdown(const WOLFSSL* ssl)
  11802. {
  11803. int isShutdown = 0;
  11804. WOLFSSL_ENTER("wolfSSL_get_shutdown");
  11805. if (ssl) {
  11806. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  11807. if (ssl->options.shutdownDone) {
  11808. /* The SSL object was possibly cleared with wolfSSL_clear after
  11809. * a successful shutdown. Simulate a response for a full
  11810. * bidirectional shutdown. */
  11811. isShutdown = WOLFSSL_SENT_SHUTDOWN | WOLFSSL_RECEIVED_SHUTDOWN;
  11812. }
  11813. else
  11814. #endif
  11815. {
  11816. /* in OpenSSL, WOLFSSL_SENT_SHUTDOWN = 1, when closeNotifySent *
  11817. * WOLFSSL_RECEIVED_SHUTDOWN = 2, from close notify or fatal err */
  11818. if (ssl->options.sentNotify)
  11819. isShutdown |= WOLFSSL_SENT_SHUTDOWN;
  11820. if (ssl->options.closeNotify||ssl->options.connReset)
  11821. isShutdown |= WOLFSSL_RECEIVED_SHUTDOWN;
  11822. }
  11823. }
  11824. WOLFSSL_LEAVE("wolfSSL_get_shutdown", isShutdown);
  11825. return isShutdown;
  11826. }
  11827. int wolfSSL_session_reused(WOLFSSL* ssl)
  11828. {
  11829. int resuming = 0;
  11830. WOLFSSL_ENTER("wolfSSL_session_reused");
  11831. if (ssl) {
  11832. #ifndef HAVE_SECURE_RENEGOTIATION
  11833. resuming = ssl->options.resuming;
  11834. #else
  11835. resuming = ssl->options.resuming || ssl->options.resumed;
  11836. #endif
  11837. }
  11838. WOLFSSL_LEAVE("wolfSSL_session_reused", resuming);
  11839. return resuming;
  11840. }
  11841. /* helper function that takes in a protocol version struct and returns string */
  11842. static const char* wolfSSL_internal_get_version(const ProtocolVersion* version)
  11843. {
  11844. WOLFSSL_ENTER("wolfSSL_get_version");
  11845. if (version == NULL) {
  11846. return "Bad arg";
  11847. }
  11848. if (version->major == SSLv3_MAJOR) {
  11849. switch (version->minor) {
  11850. case SSLv3_MINOR :
  11851. return "SSLv3";
  11852. case TLSv1_MINOR :
  11853. return "TLSv1";
  11854. case TLSv1_1_MINOR :
  11855. return "TLSv1.1";
  11856. case TLSv1_2_MINOR :
  11857. return "TLSv1.2";
  11858. case TLSv1_3_MINOR :
  11859. return "TLSv1.3";
  11860. default:
  11861. return "unknown";
  11862. }
  11863. }
  11864. #ifdef WOLFSSL_DTLS
  11865. else if (version->major == DTLS_MAJOR) {
  11866. switch (version->minor) {
  11867. case DTLS_MINOR :
  11868. return "DTLS";
  11869. case DTLSv1_2_MINOR :
  11870. return "DTLSv1.2";
  11871. case DTLSv1_3_MINOR :
  11872. return "DTLSv1.3";
  11873. default:
  11874. return "unknown";
  11875. }
  11876. }
  11877. #endif /* WOLFSSL_DTLS */
  11878. return "unknown";
  11879. }
  11880. const char* wolfSSL_get_version(const WOLFSSL* ssl)
  11881. {
  11882. if (ssl == NULL) {
  11883. WOLFSSL_MSG("Bad argument");
  11884. return "unknown";
  11885. }
  11886. return wolfSSL_internal_get_version(&ssl->version);
  11887. }
  11888. /* current library version */
  11889. const char* wolfSSL_lib_version(void)
  11890. {
  11891. return LIBWOLFSSL_VERSION_STRING;
  11892. }
  11893. #ifdef OPENSSL_EXTRA
  11894. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  11895. const char* wolfSSL_OpenSSL_version(int a)
  11896. {
  11897. (void)a;
  11898. return "wolfSSL " LIBWOLFSSL_VERSION_STRING;
  11899. }
  11900. #else
  11901. const char* wolfSSL_OpenSSL_version(void)
  11902. {
  11903. return "wolfSSL " LIBWOLFSSL_VERSION_STRING;
  11904. }
  11905. #endif /* WOLFSSL_QT */
  11906. #endif
  11907. /* current library version in hex */
  11908. word32 wolfSSL_lib_version_hex(void)
  11909. {
  11910. return LIBWOLFSSL_VERSION_HEX;
  11911. }
  11912. int wolfSSL_get_current_cipher_suite(WOLFSSL* ssl)
  11913. {
  11914. WOLFSSL_ENTER("wolfSSL_get_current_cipher_suite");
  11915. if (ssl)
  11916. return (ssl->options.cipherSuite0 << 8) | ssl->options.cipherSuite;
  11917. return 0;
  11918. }
  11919. WOLFSSL_CIPHER* wolfSSL_get_current_cipher(WOLFSSL* ssl)
  11920. {
  11921. WOLFSSL_ENTER("wolfSSL_get_current_cipher");
  11922. if (ssl) {
  11923. ssl->cipher.cipherSuite0 = ssl->options.cipherSuite0;
  11924. ssl->cipher.cipherSuite = ssl->options.cipherSuite;
  11925. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  11926. ssl->cipher.bits = ssl->specs.key_size * 8;
  11927. #endif
  11928. return &ssl->cipher;
  11929. }
  11930. else
  11931. return NULL;
  11932. }
  11933. const char* wolfSSL_CIPHER_get_name(const WOLFSSL_CIPHER* cipher)
  11934. {
  11935. WOLFSSL_ENTER("wolfSSL_CIPHER_get_name");
  11936. if (cipher == NULL) {
  11937. return NULL;
  11938. }
  11939. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  11940. !defined(WOLFSSL_QT)
  11941. return GetCipherNameIana(cipher->cipherSuite0, cipher->cipherSuite);
  11942. #else
  11943. return wolfSSL_get_cipher_name_from_suite(cipher->cipherSuite0,
  11944. cipher->cipherSuite);
  11945. #endif
  11946. }
  11947. const char* wolfSSL_CIPHER_get_version(const WOLFSSL_CIPHER* cipher)
  11948. {
  11949. WOLFSSL_ENTER("wolfSSL_CIPHER_get_version");
  11950. if (cipher == NULL || cipher->ssl == NULL) {
  11951. return NULL;
  11952. }
  11953. return wolfSSL_get_version(cipher->ssl);
  11954. }
  11955. const char* wolfSSL_get_cipher(WOLFSSL* ssl)
  11956. {
  11957. WOLFSSL_ENTER("wolfSSL_get_cipher");
  11958. return wolfSSL_CIPHER_get_name(wolfSSL_get_current_cipher(ssl));
  11959. }
  11960. /* gets cipher name in the format DHE-RSA-... rather then TLS_DHE... */
  11961. const char* wolfSSL_get_cipher_name(WOLFSSL* ssl)
  11962. {
  11963. /* get access to cipher_name_idx in internal.c */
  11964. return wolfSSL_get_cipher_name_internal(ssl);
  11965. }
  11966. const char* wolfSSL_get_cipher_name_from_suite(const byte cipherSuite0,
  11967. const byte cipherSuite)
  11968. {
  11969. return GetCipherNameInternal(cipherSuite0, cipherSuite);
  11970. }
  11971. const char* wolfSSL_get_cipher_name_iana_from_suite(const byte cipherSuite0,
  11972. const byte cipherSuite)
  11973. {
  11974. return GetCipherNameIana(cipherSuite0, cipherSuite);
  11975. }
  11976. int wolfSSL_get_cipher_suite_from_name(const char* name, byte* cipherSuite0,
  11977. byte* cipherSuite, int *flags) {
  11978. if ((name == NULL) ||
  11979. (cipherSuite0 == NULL) ||
  11980. (cipherSuite == NULL) ||
  11981. (flags == NULL))
  11982. return BAD_FUNC_ARG;
  11983. return GetCipherSuiteFromName(name, cipherSuite0, cipherSuite, flags);
  11984. }
  11985. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  11986. /* Creates and returns a new WOLFSSL_CIPHER stack. */
  11987. WOLFSSL_STACK* wolfSSL_sk_new_cipher(void)
  11988. {
  11989. WOLFSSL_STACK* sk;
  11990. WOLFSSL_ENTER("wolfSSL_sk_new_cipher");
  11991. sk = wolfSSL_sk_new_null();
  11992. if (sk == NULL)
  11993. return NULL;
  11994. sk->type = STACK_TYPE_CIPHER;
  11995. return sk;
  11996. }
  11997. /* return 1 on success 0 on fail */
  11998. int wolfSSL_sk_CIPHER_push(WOLF_STACK_OF(WOLFSSL_CIPHER)* sk,
  11999. WOLFSSL_CIPHER* cipher)
  12000. {
  12001. return wolfSSL_sk_push(sk, cipher);
  12002. }
  12003. #ifndef NO_WOLFSSL_STUB
  12004. WOLFSSL_CIPHER* wolfSSL_sk_CIPHER_pop(WOLF_STACK_OF(WOLFSSL_CIPHER)* sk)
  12005. {
  12006. WOLFSSL_STUB("wolfSSL_sk_CIPHER_pop");
  12007. (void)sk;
  12008. return NULL;
  12009. }
  12010. #endif /* NO_WOLFSSL_STUB */
  12011. #endif /* WOLFSSL_QT || OPENSSL_ALL */
  12012. word32 wolfSSL_CIPHER_get_id(const WOLFSSL_CIPHER* cipher)
  12013. {
  12014. word16 cipher_id = 0;
  12015. WOLFSSL_ENTER("wolfSSL_CIPHER_get_id");
  12016. if (cipher && cipher->ssl) {
  12017. cipher_id = (cipher->ssl->options.cipherSuite0 << 8) |
  12018. cipher->ssl->options.cipherSuite;
  12019. }
  12020. return cipher_id;
  12021. }
  12022. const WOLFSSL_CIPHER* wolfSSL_get_cipher_by_value(word16 value)
  12023. {
  12024. const WOLFSSL_CIPHER* cipher = NULL;
  12025. byte cipherSuite0, cipherSuite;
  12026. WOLFSSL_ENTER("wolfSSL_get_cipher_by_value");
  12027. /* extract cipher id information */
  12028. cipherSuite = (value & 0xFF);
  12029. cipherSuite0 = ((value >> 8) & 0xFF);
  12030. /* TODO: lookup by cipherSuite0 / cipherSuite */
  12031. (void)cipherSuite0;
  12032. (void)cipherSuite;
  12033. return cipher;
  12034. }
  12035. #if defined(OPENSSL_EXTRA)
  12036. /* Free the structure for WOLFSSL_CIPHER stack
  12037. *
  12038. * sk stack to free nodes in
  12039. */
  12040. void wolfSSL_sk_CIPHER_free(WOLF_STACK_OF(WOLFSSL_CIPHER)* sk)
  12041. {
  12042. WOLFSSL_ENTER("wolfSSL_sk_CIPHER_free");
  12043. wolfSSL_sk_free(sk);
  12044. }
  12045. #endif /* OPENSSL_ALL */
  12046. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) || \
  12047. !defined(NO_DH)
  12048. #ifdef HAVE_FFDHE
  12049. static const char* wolfssl_ffdhe_name(word16 group)
  12050. {
  12051. const char* str = NULL;
  12052. switch (group) {
  12053. case WOLFSSL_FFDHE_2048:
  12054. str = "FFDHE_2048";
  12055. break;
  12056. case WOLFSSL_FFDHE_3072:
  12057. str = "FFDHE_3072";
  12058. break;
  12059. case WOLFSSL_FFDHE_4096:
  12060. str = "FFDHE_4096";
  12061. break;
  12062. case WOLFSSL_FFDHE_6144:
  12063. str = "FFDHE_6144";
  12064. break;
  12065. case WOLFSSL_FFDHE_8192:
  12066. str = "FFDHE_8192";
  12067. break;
  12068. default:
  12069. break;
  12070. }
  12071. return str;
  12072. }
  12073. #endif
  12074. /* Return the name of the curve used for key exchange as a printable string.
  12075. *
  12076. * ssl The SSL/TLS object.
  12077. * returns NULL if ECDH was not used, otherwise the name as a string.
  12078. */
  12079. const char* wolfSSL_get_curve_name(WOLFSSL* ssl)
  12080. {
  12081. const char* cName = NULL;
  12082. WOLFSSL_ENTER("wolfSSL_get_curve_name");
  12083. if (ssl == NULL)
  12084. return NULL;
  12085. #if defined(WOLFSSL_TLS13) && defined(HAVE_PQC)
  12086. /* Check for post-quantum groups. Return now because we do not want the ECC
  12087. * check to override this result in the case of a hybrid. */
  12088. if (IsAtLeastTLSv1_3(ssl->version)) {
  12089. switch (ssl->namedGroup) {
  12090. #ifdef HAVE_LIBOQS
  12091. case WOLFSSL_KYBER_LEVEL1:
  12092. return "KYBER_LEVEL1";
  12093. case WOLFSSL_KYBER_LEVEL3:
  12094. return "KYBER_LEVEL3";
  12095. case WOLFSSL_KYBER_LEVEL5:
  12096. return "KYBER_LEVEL5";
  12097. case WOLFSSL_P256_KYBER_LEVEL1:
  12098. return "P256_KYBER_LEVEL1";
  12099. case WOLFSSL_P384_KYBER_LEVEL3:
  12100. return "P384_KYBER_LEVEL3";
  12101. case WOLFSSL_P521_KYBER_LEVEL5:
  12102. return "P521_KYBER_LEVEL5";
  12103. #elif defined(HAVE_PQM4)
  12104. case WOLFSSL_KYBER_LEVEL1:
  12105. return "KYBER_LEVEL1";
  12106. #elif defined(WOLFSSL_WC_KYBER)
  12107. #ifdef WOLFSSL_KYBER512
  12108. case WOLFSSL_KYBER_LEVEL1:
  12109. return "KYBER_LEVEL1";
  12110. #endif
  12111. #ifdef WOLFSSL_KYBER768
  12112. case WOLFSSL_KYBER_LEVEL3:
  12113. return "KYBER_LEVEL3";
  12114. #endif
  12115. #ifdef WOLFSSL_KYBER1024
  12116. case WOLFSSL_KYBER_LEVEL5:
  12117. return "KYBER_LEVEL5";
  12118. #endif
  12119. #endif
  12120. }
  12121. }
  12122. #endif /* WOLFSSL_TLS13 && HAVE_PQC */
  12123. #ifdef HAVE_FFDHE
  12124. if (ssl->namedGroup != 0) {
  12125. cName = wolfssl_ffdhe_name(ssl->namedGroup);
  12126. }
  12127. #endif
  12128. #ifdef HAVE_CURVE25519
  12129. if (ssl->ecdhCurveOID == ECC_X25519_OID && cName == NULL) {
  12130. cName = "X25519";
  12131. }
  12132. #endif
  12133. #ifdef HAVE_CURVE448
  12134. if (ssl->ecdhCurveOID == ECC_X448_OID && cName == NULL) {
  12135. cName = "X448";
  12136. }
  12137. #endif
  12138. #ifdef HAVE_ECC
  12139. if (ssl->ecdhCurveOID != 0 && cName == NULL) {
  12140. cName = wc_ecc_get_name(wc_ecc_get_oid(ssl->ecdhCurveOID, NULL,
  12141. NULL));
  12142. }
  12143. #endif
  12144. return cName;
  12145. }
  12146. #endif
  12147. #ifdef OPENSSL_EXTRA
  12148. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  12149. /* return authentication NID corresponding to cipher suite
  12150. * @param cipher a pointer to WOLFSSL_CIPHER
  12151. * return NID if found, NID_undef if not found
  12152. */
  12153. int wolfSSL_CIPHER_get_auth_nid(const WOLFSSL_CIPHER* cipher)
  12154. {
  12155. static const struct authnid {
  12156. const char* alg_name;
  12157. const int nid;
  12158. } authnid_tbl[] = {
  12159. {"RSA", NID_auth_rsa},
  12160. {"PSK", NID_auth_psk},
  12161. {"SRP", NID_auth_srp},
  12162. {"ECDSA", NID_auth_ecdsa},
  12163. {"None", NID_auth_null},
  12164. {NULL, NID_undef}
  12165. };
  12166. const char* authStr;
  12167. char n[MAX_SEGMENTS][MAX_SEGMENT_SZ] = {{0}};
  12168. if (GetCipherSegment(cipher, n) == NULL) {
  12169. WOLFSSL_MSG("no suitable cipher name found");
  12170. return NID_undef;
  12171. }
  12172. authStr = GetCipherAuthStr(n);
  12173. if (authStr != NULL) {
  12174. const struct authnid* sa;
  12175. for(sa = authnid_tbl; sa->alg_name != NULL; sa++) {
  12176. if (XSTRCMP(sa->alg_name, authStr) == 0) {
  12177. return sa->nid;
  12178. }
  12179. }
  12180. }
  12181. return NID_undef;
  12182. }
  12183. /* return cipher NID corresponding to cipher suite
  12184. * @param cipher a pointer to WOLFSSL_CIPHER
  12185. * return NID if found, NID_undef if not found
  12186. */
  12187. int wolfSSL_CIPHER_get_cipher_nid(const WOLFSSL_CIPHER* cipher)
  12188. {
  12189. static const struct ciphernid {
  12190. const char* alg_name;
  12191. const int nid;
  12192. } ciphernid_tbl[] = {
  12193. {"AESGCM(256)", NID_aes_256_gcm},
  12194. {"AESGCM(128)", NID_aes_128_gcm},
  12195. {"AESCCM(128)", NID_aes_128_ccm},
  12196. {"AES(128)", NID_aes_128_cbc},
  12197. {"AES(256)", NID_aes_256_cbc},
  12198. {"CAMELLIA(256)", NID_camellia_256_cbc},
  12199. {"CAMELLIA(128)", NID_camellia_128_cbc},
  12200. {"RC4", NID_rc4},
  12201. {"3DES", NID_des_ede3_cbc},
  12202. {"CHACHA20/POLY1305(256)", NID_chacha20_poly1305},
  12203. {"None", NID_undef},
  12204. {NULL, NID_undef}
  12205. };
  12206. const char* encStr;
  12207. char n[MAX_SEGMENTS][MAX_SEGMENT_SZ] = {{0}};
  12208. WOLFSSL_ENTER("wolfSSL_CIPHER_get_cipher_nid");
  12209. if (GetCipherSegment(cipher, n) == NULL) {
  12210. WOLFSSL_MSG("no suitable cipher name found");
  12211. return NID_undef;
  12212. }
  12213. encStr = GetCipherEncStr(n);
  12214. if (encStr != NULL) {
  12215. const struct ciphernid* c;
  12216. for(c = ciphernid_tbl; c->alg_name != NULL; c++) {
  12217. if (XSTRCMP(c->alg_name, encStr) == 0) {
  12218. return c->nid;
  12219. }
  12220. }
  12221. }
  12222. return NID_undef;
  12223. }
  12224. /* return digest NID corresponding to cipher suite
  12225. * @param cipher a pointer to WOLFSSL_CIPHER
  12226. * return NID if found, NID_undef if not found
  12227. */
  12228. int wolfSSL_CIPHER_get_digest_nid(const WOLFSSL_CIPHER* cipher)
  12229. {
  12230. static const struct macnid {
  12231. const char* alg_name;
  12232. const int nid;
  12233. } macnid_tbl[] = {
  12234. {"SHA1", NID_sha1},
  12235. {"SHA256", NID_sha256},
  12236. {"SHA384", NID_sha384},
  12237. {NULL, NID_undef}
  12238. };
  12239. const char* name;
  12240. const char* macStr;
  12241. char n[MAX_SEGMENTS][MAX_SEGMENT_SZ] = {{0}};
  12242. (void)name;
  12243. WOLFSSL_ENTER("wolfSSL_CIPHER_get_digest_nid");
  12244. if ((name = GetCipherSegment(cipher, n)) == NULL) {
  12245. WOLFSSL_MSG("no suitable cipher name found");
  12246. return NID_undef;
  12247. }
  12248. /* in MD5 case, NID will be NID_md5 */
  12249. if (XSTRSTR(name, "MD5") != NULL) {
  12250. return NID_md5;
  12251. }
  12252. macStr = GetCipherMacStr(n);
  12253. if (macStr != NULL) {
  12254. const struct macnid* mc;
  12255. for(mc = macnid_tbl; mc->alg_name != NULL; mc++) {
  12256. if (XSTRCMP(mc->alg_name, macStr) == 0) {
  12257. return mc->nid;
  12258. }
  12259. }
  12260. }
  12261. return NID_undef;
  12262. }
  12263. /* return key exchange NID corresponding to cipher suite
  12264. * @param cipher a pointer to WOLFSSL_CIPHER
  12265. * return NID if found, NID_undef if not found
  12266. */
  12267. int wolfSSL_CIPHER_get_kx_nid(const WOLFSSL_CIPHER* cipher)
  12268. {
  12269. static const struct kxnid {
  12270. const char* name;
  12271. const int nid;
  12272. } kxnid_table[] = {
  12273. {"ECDHEPSK", NID_kx_ecdhe_psk},
  12274. {"ECDH", NID_kx_ecdhe},
  12275. {"DHEPSK", NID_kx_dhe_psk},
  12276. {"DH", NID_kx_dhe},
  12277. {"RSAPSK", NID_kx_rsa_psk},
  12278. {"SRP", NID_kx_srp},
  12279. {"EDH", NID_kx_dhe},
  12280. {"RSA", NID_kx_rsa},
  12281. {NULL, NID_undef}
  12282. };
  12283. const char* keaStr;
  12284. char n[MAX_SEGMENTS][MAX_SEGMENT_SZ] = {{0}};
  12285. WOLFSSL_ENTER("wolfSSL_CIPHER_get_kx_nid");
  12286. if (GetCipherSegment(cipher, n) == NULL) {
  12287. WOLFSSL_MSG("no suitable cipher name found");
  12288. return NID_undef;
  12289. }
  12290. /* in TLS 1.3 case, NID will be NID_kx_any */
  12291. if (XSTRCMP(n[0], "TLS13") == 0) {
  12292. return NID_kx_any;
  12293. }
  12294. keaStr = GetCipherKeaStr(n);
  12295. if (keaStr != NULL) {
  12296. const struct kxnid* k;
  12297. for(k = kxnid_table; k->name != NULL; k++) {
  12298. if (XSTRCMP(k->name, keaStr) == 0) {
  12299. return k->nid;
  12300. }
  12301. }
  12302. }
  12303. return NID_undef;
  12304. }
  12305. /* check if cipher suite is AEAD
  12306. * @param cipher a pointer to WOLFSSL_CIPHER
  12307. * return 1 if cipher is AEAD, 0 otherwise
  12308. */
  12309. int wolfSSL_CIPHER_is_aead(const WOLFSSL_CIPHER* cipher)
  12310. {
  12311. char n[MAX_SEGMENTS][MAX_SEGMENT_SZ] = {{0}};
  12312. WOLFSSL_ENTER("wolfSSL_CIPHER_is_aead");
  12313. if (GetCipherSegment(cipher, n) == NULL) {
  12314. WOLFSSL_MSG("no suitable cipher name found");
  12315. return NID_undef;
  12316. }
  12317. return IsCipherAEAD(n);
  12318. }
  12319. /* Creates cipher->description based on cipher->offset
  12320. * cipher->offset is set in wolfSSL_get_ciphers_compat when it is added
  12321. * to a stack of ciphers.
  12322. * @param [in] cipher: A cipher from a stack of ciphers.
  12323. * return WOLFSSL_SUCCESS if cipher->description is set, else WOLFSSL_FAILURE
  12324. */
  12325. int wolfSSL_sk_CIPHER_description(WOLFSSL_CIPHER* cipher)
  12326. {
  12327. int strLen;
  12328. unsigned long offset;
  12329. char* dp;
  12330. const char* name;
  12331. const char *keaStr, *authStr, *encStr, *macStr, *protocol;
  12332. char n[MAX_SEGMENTS][MAX_SEGMENT_SZ] = {{0}};
  12333. int len = MAX_DESCRIPTION_SZ-1;
  12334. const CipherSuiteInfo* cipher_names;
  12335. ProtocolVersion pv;
  12336. WOLFSSL_ENTER("wolfSSL_sk_CIPHER_description");
  12337. if (cipher == NULL)
  12338. return WOLFSSL_FAILURE;
  12339. dp = cipher->description;
  12340. if (dp == NULL)
  12341. return WOLFSSL_FAILURE;
  12342. cipher_names = GetCipherNames();
  12343. offset = cipher->offset;
  12344. if (offset >= (unsigned long)GetCipherNamesSize())
  12345. return WOLFSSL_FAILURE;
  12346. pv.major = cipher_names[offset].major;
  12347. pv.minor = cipher_names[offset].minor;
  12348. protocol = wolfSSL_internal_get_version(&pv);
  12349. if ((name = GetCipherSegment(cipher, n)) == NULL) {
  12350. WOLFSSL_MSG("no suitable cipher name found");
  12351. return WOLFSSL_FAILURE;
  12352. }
  12353. /* keaStr */
  12354. keaStr = GetCipherKeaStr(n);
  12355. /* authStr */
  12356. authStr = GetCipherAuthStr(n);
  12357. /* encStr */
  12358. encStr = GetCipherEncStr(n);
  12359. if ((cipher->bits = SetCipherBits(encStr)) == WOLFSSL_FAILURE) {
  12360. WOLFSSL_MSG("Cipher Bits Not Set.");
  12361. }
  12362. /* macStr */
  12363. macStr = GetCipherMacStr(n);
  12364. /* Build up the string by copying onto the end. */
  12365. XSTRNCPY(dp, name, len);
  12366. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12367. len -= strLen; dp += strLen;
  12368. XSTRNCPY(dp, " ", len);
  12369. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12370. len -= strLen; dp += strLen;
  12371. XSTRNCPY(dp, protocol, len);
  12372. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12373. len -= strLen; dp += strLen;
  12374. XSTRNCPY(dp, " Kx=", len);
  12375. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12376. len -= strLen; dp += strLen;
  12377. XSTRNCPY(dp, keaStr, len);
  12378. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12379. len -= strLen; dp += strLen;
  12380. XSTRNCPY(dp, " Au=", len);
  12381. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12382. len -= strLen; dp += strLen;
  12383. XSTRNCPY(dp, authStr, len);
  12384. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12385. len -= strLen; dp += strLen;
  12386. XSTRNCPY(dp, " Enc=", len);
  12387. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12388. len -= strLen; dp += strLen;
  12389. XSTRNCPY(dp, encStr, len);
  12390. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12391. len -= strLen; dp += strLen;
  12392. XSTRNCPY(dp, " Mac=", len);
  12393. dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
  12394. len -= strLen; dp += strLen;
  12395. XSTRNCPY(dp, macStr, len);
  12396. dp[len-1] = '\0';
  12397. return WOLFSSL_SUCCESS;
  12398. }
  12399. #endif /* OPENSSL_ALL || WOLFSSL_QT */
  12400. static WC_INLINE const char* wolfssl_kea_to_string(int kea)
  12401. {
  12402. const char* keaStr;
  12403. switch (kea) {
  12404. case no_kea:
  12405. keaStr = "None";
  12406. break;
  12407. #ifndef NO_RSA
  12408. case rsa_kea:
  12409. keaStr = "RSA";
  12410. break;
  12411. #endif
  12412. #ifndef NO_DH
  12413. case diffie_hellman_kea:
  12414. keaStr = "DHE";
  12415. break;
  12416. #endif
  12417. case fortezza_kea:
  12418. keaStr = "FZ";
  12419. break;
  12420. #ifndef NO_PSK
  12421. case psk_kea:
  12422. keaStr = "PSK";
  12423. break;
  12424. #ifndef NO_DH
  12425. case dhe_psk_kea:
  12426. keaStr = "DHEPSK";
  12427. break;
  12428. #endif
  12429. #ifdef HAVE_ECC
  12430. case ecdhe_psk_kea:
  12431. keaStr = "ECDHEPSK";
  12432. break;
  12433. #endif
  12434. #endif
  12435. #ifdef HAVE_ECC
  12436. case ecc_diffie_hellman_kea:
  12437. keaStr = "ECDHE";
  12438. break;
  12439. case ecc_static_diffie_hellman_kea:
  12440. keaStr = "ECDH";
  12441. break;
  12442. #endif
  12443. default:
  12444. keaStr = "unknown";
  12445. break;
  12446. }
  12447. return keaStr;
  12448. }
  12449. static WC_INLINE const char* wolfssl_sigalg_to_string(int sig_algo)
  12450. {
  12451. const char* authStr;
  12452. switch (sig_algo) {
  12453. case anonymous_sa_algo:
  12454. authStr = "None";
  12455. break;
  12456. #ifndef NO_RSA
  12457. case rsa_sa_algo:
  12458. authStr = "RSA";
  12459. break;
  12460. #ifdef WC_RSA_PSS
  12461. case rsa_pss_sa_algo:
  12462. authStr = "RSA-PSS";
  12463. break;
  12464. #endif
  12465. #endif
  12466. #ifndef NO_DSA
  12467. case dsa_sa_algo:
  12468. authStr = "DSA";
  12469. break;
  12470. #endif
  12471. #ifdef HAVE_ECC
  12472. case ecc_dsa_sa_algo:
  12473. authStr = "ECDSA";
  12474. break;
  12475. #endif
  12476. #ifdef WOLFSSL_SM2
  12477. case sm2_sa_algo:
  12478. authStr = "SM2";
  12479. break;
  12480. #endif
  12481. #ifdef HAVE_ED25519
  12482. case ed25519_sa_algo:
  12483. authStr = "Ed25519";
  12484. break;
  12485. #endif
  12486. #ifdef HAVE_ED448
  12487. case ed448_sa_algo:
  12488. authStr = "Ed448";
  12489. break;
  12490. #endif
  12491. default:
  12492. authStr = "unknown";
  12493. break;
  12494. }
  12495. return authStr;
  12496. }
  12497. static WC_INLINE const char* wolfssl_cipher_to_string(int cipher, int key_size)
  12498. {
  12499. const char* encStr;
  12500. (void)key_size;
  12501. switch (cipher) {
  12502. case wolfssl_cipher_null:
  12503. encStr = "None";
  12504. break;
  12505. #ifndef NO_RC4
  12506. case wolfssl_rc4:
  12507. encStr = "RC4(128)";
  12508. break;
  12509. #endif
  12510. #ifndef NO_DES3
  12511. case wolfssl_triple_des:
  12512. encStr = "3DES(168)";
  12513. break;
  12514. #endif
  12515. #ifndef NO_AES
  12516. case wolfssl_aes:
  12517. if (key_size == 128)
  12518. encStr = "AES(128)";
  12519. else if (key_size == 256)
  12520. encStr = "AES(256)";
  12521. else
  12522. encStr = "AES(?)";
  12523. break;
  12524. #ifdef HAVE_AESGCM
  12525. case wolfssl_aes_gcm:
  12526. if (key_size == 128)
  12527. encStr = "AESGCM(128)";
  12528. else if (key_size == 256)
  12529. encStr = "AESGCM(256)";
  12530. else
  12531. encStr = "AESGCM(?)";
  12532. break;
  12533. #endif
  12534. #ifdef HAVE_AESCCM
  12535. case wolfssl_aes_ccm:
  12536. if (key_size == 128)
  12537. encStr = "AESCCM(128)";
  12538. else if (key_size == 256)
  12539. encStr = "AESCCM(256)";
  12540. else
  12541. encStr = "AESCCM(?)";
  12542. break;
  12543. #endif
  12544. #endif
  12545. #ifdef HAVE_CHACHA
  12546. case wolfssl_chacha:
  12547. encStr = "CHACHA20/POLY1305(256)";
  12548. break;
  12549. #endif
  12550. #ifdef HAVE_ARIA
  12551. case wolfssl_aria_gcm:
  12552. if (key_size == 128)
  12553. encStr = "Aria(128)";
  12554. else if (key_size == 192)
  12555. encStr = "Aria(192)";
  12556. else if (key_size == 256)
  12557. encStr = "Aria(256)";
  12558. else
  12559. encStr = "Aria(?)";
  12560. break;
  12561. #endif
  12562. #ifdef HAVE_CAMELLIA
  12563. case wolfssl_camellia:
  12564. if (key_size == 128)
  12565. encStr = "Camellia(128)";
  12566. else if (key_size == 256)
  12567. encStr = "Camellia(256)";
  12568. else
  12569. encStr = "Camellia(?)";
  12570. break;
  12571. #endif
  12572. default:
  12573. encStr = "unknown";
  12574. break;
  12575. }
  12576. return encStr;
  12577. }
  12578. static WC_INLINE const char* wolfssl_mac_to_string(int mac)
  12579. {
  12580. const char* macStr;
  12581. switch (mac) {
  12582. case no_mac:
  12583. macStr = "None";
  12584. break;
  12585. #ifndef NO_MD5
  12586. case md5_mac:
  12587. macStr = "MD5";
  12588. break;
  12589. #endif
  12590. #ifndef NO_SHA
  12591. case sha_mac:
  12592. macStr = "SHA1";
  12593. break;
  12594. #endif
  12595. #ifdef HAVE_SHA224
  12596. case sha224_mac:
  12597. macStr = "SHA224";
  12598. break;
  12599. #endif
  12600. #ifndef NO_SHA256
  12601. case sha256_mac:
  12602. macStr = "SHA256";
  12603. break;
  12604. #endif
  12605. #ifdef HAVE_SHA384
  12606. case sha384_mac:
  12607. macStr = "SHA384";
  12608. break;
  12609. #endif
  12610. #ifdef HAVE_SHA512
  12611. case sha512_mac:
  12612. macStr = "SHA512";
  12613. break;
  12614. #endif
  12615. default:
  12616. macStr = "unknown";
  12617. break;
  12618. }
  12619. return macStr;
  12620. }
  12621. char* wolfSSL_CIPHER_description(const WOLFSSL_CIPHER* cipher, char* in,
  12622. int len)
  12623. {
  12624. char *ret = in;
  12625. const char *keaStr, *authStr, *encStr, *macStr;
  12626. size_t strLen;
  12627. WOLFSSL_ENTER("wolfSSL_CIPHER_description");
  12628. if (cipher == NULL || in == NULL)
  12629. return NULL;
  12630. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  12631. /* if cipher is in the stack from wolfSSL_get_ciphers_compat then
  12632. * Return the description based on cipher_names[cipher->offset]
  12633. */
  12634. if (cipher->in_stack == TRUE) {
  12635. wolfSSL_sk_CIPHER_description((WOLFSSL_CIPHER*)cipher);
  12636. XSTRNCPY(in,cipher->description,len);
  12637. return ret;
  12638. }
  12639. #endif
  12640. /* Get the cipher description based on the SSL session cipher */
  12641. keaStr = wolfssl_kea_to_string(cipher->ssl->specs.kea);
  12642. authStr = wolfssl_sigalg_to_string(cipher->ssl->specs.sig_algo);
  12643. encStr = wolfssl_cipher_to_string(cipher->ssl->specs.bulk_cipher_algorithm,
  12644. cipher->ssl->specs.key_size);
  12645. macStr = wolfssl_mac_to_string(cipher->ssl->specs.mac_algorithm);
  12646. /* Build up the string by copying onto the end. */
  12647. XSTRNCPY(in, wolfSSL_CIPHER_get_name(cipher), len);
  12648. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12649. XSTRNCPY(in, " ", len);
  12650. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12651. XSTRNCPY(in, wolfSSL_get_version(cipher->ssl), len);
  12652. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12653. XSTRNCPY(in, " Kx=", len);
  12654. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12655. XSTRNCPY(in, keaStr, len);
  12656. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12657. XSTRNCPY(in, " Au=", len);
  12658. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12659. XSTRNCPY(in, authStr, len);
  12660. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12661. XSTRNCPY(in, " Enc=", len);
  12662. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12663. XSTRNCPY(in, encStr, len);
  12664. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12665. XSTRNCPY(in, " Mac=", len);
  12666. in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
  12667. XSTRNCPY(in, macStr, len);
  12668. in[len-1] = '\0';
  12669. return ret;
  12670. }
  12671. #ifndef NO_WOLFSSL_STUB
  12672. int wolfSSL_OCSP_parse_url(char* url, char** host, char** port, char** path,
  12673. int* ssl)
  12674. {
  12675. (void)url;
  12676. (void)host;
  12677. (void)port;
  12678. (void)path;
  12679. (void)ssl;
  12680. WOLFSSL_STUB("OCSP_parse_url");
  12681. return 0;
  12682. }
  12683. #endif
  12684. #ifndef NO_WOLFSSL_STUB
  12685. WOLFSSL_COMP_METHOD* wolfSSL_COMP_zlib(void)
  12686. {
  12687. WOLFSSL_STUB("COMP_zlib");
  12688. return 0;
  12689. }
  12690. #endif
  12691. #ifndef NO_WOLFSSL_STUB
  12692. WOLFSSL_COMP_METHOD* wolfSSL_COMP_rle(void)
  12693. {
  12694. WOLFSSL_STUB("COMP_rle");
  12695. return 0;
  12696. }
  12697. #endif
  12698. #ifndef NO_WOLFSSL_STUB
  12699. int wolfSSL_COMP_add_compression_method(int method, void* data)
  12700. {
  12701. (void)method;
  12702. (void)data;
  12703. WOLFSSL_STUB("COMP_add_compression_method");
  12704. return 0;
  12705. }
  12706. #endif
  12707. /* wolfSSL_set_dynlock_create_callback
  12708. * CRYPTO_set_dynlock_create_callback has been deprecated since openSSL 1.0.1.
  12709. * This function exists for compatibility purposes because wolfSSL satisfies
  12710. * thread safety without relying on the callback.
  12711. */
  12712. void wolfSSL_set_dynlock_create_callback(WOLFSSL_dynlock_value* (*f)(
  12713. const char*, int))
  12714. {
  12715. WOLFSSL_STUB("CRYPTO_set_dynlock_create_callback");
  12716. (void)f;
  12717. }
  12718. /* wolfSSL_set_dynlock_lock_callback
  12719. * CRYPTO_set_dynlock_lock_callback has been deprecated since openSSL 1.0.1.
  12720. * This function exists for compatibility purposes because wolfSSL satisfies
  12721. * thread safety without relying on the callback.
  12722. */
  12723. void wolfSSL_set_dynlock_lock_callback(
  12724. void (*f)(int, WOLFSSL_dynlock_value*, const char*, int))
  12725. {
  12726. WOLFSSL_STUB("CRYPTO_set_set_dynlock_lock_callback");
  12727. (void)f;
  12728. }
  12729. /* wolfSSL_set_dynlock_destroy_callback
  12730. * CRYPTO_set_dynlock_destroy_callback has been deprecated since openSSL 1.0.1.
  12731. * This function exists for compatibility purposes because wolfSSL satisfies
  12732. * thread safety without relying on the callback.
  12733. */
  12734. void wolfSSL_set_dynlock_destroy_callback(
  12735. void (*f)(WOLFSSL_dynlock_value*, const char*, int))
  12736. {
  12737. WOLFSSL_STUB("CRYPTO_set_set_dynlock_destroy_callback");
  12738. (void)f;
  12739. }
  12740. #endif /* OPENSSL_EXTRA */
  12741. #ifdef OPENSSL_EXTRA
  12742. #ifndef NO_CERTS
  12743. #if !defined(NO_ASN) && !defined(NO_PWDBASED)
  12744. /* Copies unencrypted DER key buffer into "der". If "der" is null then the size
  12745. * of buffer needed is returned. If *der == NULL then it allocates a buffer.
  12746. * NOTE: This also advances the "der" pointer to be at the end of buffer.
  12747. *
  12748. * Returns size of key buffer on success
  12749. */
  12750. int wolfSSL_i2d_PrivateKey(const WOLFSSL_EVP_PKEY* key, unsigned char** der)
  12751. {
  12752. return wolfSSL_EVP_PKEY_get_der(key, der);
  12753. }
  12754. int wolfSSL_i2d_PublicKey(const WOLFSSL_EVP_PKEY *key, unsigned char **der)
  12755. {
  12756. #if !defined(NO_RSA) || defined(HAVE_ECC)
  12757. #ifdef HAVE_ECC
  12758. unsigned char *local_der = NULL;
  12759. word32 local_derSz = 0;
  12760. unsigned char *pub_der = NULL;
  12761. ecc_key *eccKey = NULL;
  12762. word32 inOutIdx = 0;
  12763. #endif
  12764. word32 pub_derSz = 0;
  12765. int ret;
  12766. int key_type = 0;
  12767. if (key == NULL) {
  12768. return WOLFSSL_FATAL_ERROR;
  12769. }
  12770. key_type = key->type;
  12771. if ((key_type != EVP_PKEY_EC) && (key_type != EVP_PKEY_RSA)) {
  12772. return WOLFSSL_FATAL_ERROR;
  12773. }
  12774. #ifndef NO_RSA
  12775. if (key_type == EVP_PKEY_RSA) {
  12776. return wolfSSL_i2d_RSAPublicKey(key->rsa, der);
  12777. }
  12778. #endif
  12779. /* Now that RSA is taken care of, we only need to consider the ECC case. */
  12780. #ifdef HAVE_ECC
  12781. /* We need to get the DER, then convert it to a public key. But what we get
  12782. * might be a buffered private key so we need to decode it and then encode
  12783. * the public part. */
  12784. ret = wolfSSL_EVP_PKEY_get_der(key, &local_der);
  12785. if (ret <= 0) {
  12786. /* In this case, there was no buffered DER at all. This could be the
  12787. * case where the key that was passed in was generated. So now we
  12788. * have to create the local DER. */
  12789. local_derSz = wolfSSL_i2d_ECPrivateKey(key->ecc, &local_der);
  12790. if (local_derSz == 0) {
  12791. ret = WOLFSSL_FATAL_ERROR;
  12792. }
  12793. } else {
  12794. local_derSz = ret;
  12795. ret = 0;
  12796. }
  12797. if (ret == 0) {
  12798. eccKey = (ecc_key *)XMALLOC(sizeof(*eccKey), NULL, DYNAMIC_TYPE_ECC);
  12799. if (eccKey == NULL) {
  12800. WOLFSSL_MSG("Failed to allocate key buffer.");
  12801. ret = WOLFSSL_FATAL_ERROR;
  12802. }
  12803. }
  12804. if (ret == 0) {
  12805. ret = wc_ecc_init(eccKey);
  12806. }
  12807. if (ret == 0) {
  12808. ret = wc_EccPublicKeyDecode(local_der, &inOutIdx, eccKey, local_derSz);
  12809. if (ret < 0) {
  12810. /* We now try again as x.963 [point type][x][opt y]. */
  12811. ret = wc_ecc_import_x963(local_der, local_derSz, eccKey);
  12812. }
  12813. }
  12814. if (ret == 0) {
  12815. pub_derSz = wc_EccPublicKeyDerSize(eccKey, 0);
  12816. if ((int)pub_derSz <= 0) {
  12817. ret = WOLFSSL_FAILURE;
  12818. }
  12819. }
  12820. if (ret == 0) {
  12821. pub_der = (unsigned char*)XMALLOC(pub_derSz, NULL,
  12822. DYNAMIC_TYPE_PUBLIC_KEY);
  12823. if (pub_der == NULL) {
  12824. WOLFSSL_MSG("Failed to allocate output buffer.");
  12825. ret = WOLFSSL_FATAL_ERROR;
  12826. }
  12827. }
  12828. if (ret == 0) {
  12829. pub_derSz = wc_EccPublicKeyToDer(eccKey, pub_der, pub_derSz, 0);
  12830. if ((int)pub_derSz <= 0) {
  12831. ret = WOLFSSL_FATAL_ERROR;
  12832. }
  12833. }
  12834. /* This block is for actually returning the DER of the public key */
  12835. if ((ret == 0) && (der != NULL)) {
  12836. if (*der == NULL) {
  12837. *der = (unsigned char*)XMALLOC(pub_derSz, NULL,
  12838. DYNAMIC_TYPE_PUBLIC_KEY);
  12839. if (*der == NULL) {
  12840. WOLFSSL_MSG("Failed to allocate output buffer.");
  12841. ret = WOLFSSL_FATAL_ERROR;
  12842. }
  12843. if (ret == 0) {
  12844. XMEMCPY(*der, pub_der, pub_derSz);
  12845. }
  12846. }
  12847. else {
  12848. XMEMCPY(*der, pub_der, pub_derSz);
  12849. *der += pub_derSz;
  12850. }
  12851. }
  12852. XFREE(pub_der, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  12853. XFREE(local_der, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  12854. wc_ecc_free(eccKey);
  12855. XFREE(eccKey, NULL, DYNAMIC_TYPE_ECC);
  12856. #else
  12857. ret = WOLFSSL_FATAL_ERROR;
  12858. #endif /* HAVE_ECC */
  12859. if (ret == 0) {
  12860. return pub_derSz;
  12861. }
  12862. return ret;
  12863. #else
  12864. return WOLFSSL_FATAL_ERROR;
  12865. #endif /* !NO_RSA || HAVE_ECC */
  12866. }
  12867. #endif /* !NO_ASN && !NO_PWDBASED */
  12868. #endif /* !NO_CERTS */
  12869. #endif /* OPENSSL_EXTRA */
  12870. #ifdef OPENSSL_EXTRA
  12871. /* Sets the DNS hostname to name.
  12872. * Hostname is cleared if name is NULL or empty. */
  12873. int wolfSSL_set1_host(WOLFSSL * ssl, const char* name)
  12874. {
  12875. if (ssl == NULL) {
  12876. return WOLFSSL_FAILURE;
  12877. }
  12878. return wolfSSL_X509_VERIFY_PARAM_set1_host(ssl->param, name, 0);
  12879. }
  12880. /******************************************************************************
  12881. * wolfSSL_CTX_set1_param - set a pointer to the SSL verification parameters
  12882. *
  12883. * RETURNS:
  12884. * WOLFSSL_SUCCESS on success, otherwise returns WOLFSSL_FAILURE
  12885. * Note: Returns WOLFSSL_SUCCESS, in case either parameter is NULL,
  12886. * same as openssl.
  12887. */
  12888. int wolfSSL_CTX_set1_param(WOLFSSL_CTX* ctx, WOLFSSL_X509_VERIFY_PARAM *vpm)
  12889. {
  12890. if (ctx == NULL || vpm == NULL)
  12891. return WOLFSSL_SUCCESS;
  12892. return wolfSSL_X509_VERIFY_PARAM_set1(ctx->param, vpm);
  12893. }
  12894. /******************************************************************************
  12895. * wolfSSL_CTX/_get0_param - return a pointer to the SSL verification parameters
  12896. *
  12897. * RETURNS:
  12898. * returns pointer to the SSL verification parameters on success,
  12899. * otherwise returns NULL
  12900. */
  12901. WOLFSSL_X509_VERIFY_PARAM* wolfSSL_CTX_get0_param(WOLFSSL_CTX* ctx)
  12902. {
  12903. if (ctx == NULL) {
  12904. return NULL;
  12905. }
  12906. return ctx->param;
  12907. }
  12908. WOLFSSL_X509_VERIFY_PARAM* wolfSSL_get0_param(WOLFSSL* ssl)
  12909. {
  12910. if (ssl == NULL) {
  12911. return NULL;
  12912. }
  12913. return ssl->param;
  12914. }
  12915. #endif /* OPENSSL_EXTRA */
  12916. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  12917. /* Gets an index to store SSL structure at.
  12918. *
  12919. * Returns positive index on success and negative values on failure
  12920. */
  12921. int wolfSSL_get_ex_data_X509_STORE_CTX_idx(void)
  12922. {
  12923. WOLFSSL_ENTER("wolfSSL_get_ex_data_X509_STORE_CTX_idx");
  12924. /* store SSL at index 0 */
  12925. return 0;
  12926. }
  12927. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  12928. #ifdef OPENSSL_EXTRA
  12929. /* Sets a function callback that will send information about the state of all
  12930. * WOLFSSL objects that have been created by the WOLFSSL_CTX structure passed
  12931. * in.
  12932. *
  12933. * ctx WOLFSSL_CTX structure to set callback function in
  12934. * f callback function to use
  12935. */
  12936. void wolfSSL_CTX_set_info_callback(WOLFSSL_CTX* ctx,
  12937. void (*f)(const WOLFSSL* ssl, int type, int val))
  12938. {
  12939. WOLFSSL_ENTER("wolfSSL_CTX_set_info_callback");
  12940. if (ctx == NULL) {
  12941. WOLFSSL_MSG("Bad function argument");
  12942. }
  12943. else {
  12944. ctx->CBIS = f;
  12945. }
  12946. }
  12947. void wolfSSL_set_info_callback(WOLFSSL* ssl,
  12948. void (*f)(const WOLFSSL* ssl, int type, int val))
  12949. {
  12950. WOLFSSL_ENTER("wolfSSL_set_info_callback");
  12951. if (ssl == NULL) {
  12952. WOLFSSL_MSG("Bad function argument");
  12953. }
  12954. else {
  12955. ssl->CBIS = f;
  12956. }
  12957. }
  12958. unsigned long wolfSSL_ERR_peek_error(void)
  12959. {
  12960. WOLFSSL_ENTER("wolfSSL_ERR_peek_error");
  12961. return wolfSSL_ERR_peek_error_line_data(NULL, NULL, NULL, NULL);
  12962. }
  12963. int wolfSSL_ERR_GET_LIB(unsigned long err)
  12964. {
  12965. unsigned long value;
  12966. value = (err & 0xFFFFFFL);
  12967. switch (value) {
  12968. case -SSL_R_HTTP_REQUEST:
  12969. return ERR_LIB_SSL;
  12970. case -ASN_NO_PEM_HEADER:
  12971. case PEM_R_NO_START_LINE:
  12972. case PEM_R_PROBLEMS_GETTING_PASSWORD:
  12973. case PEM_R_BAD_PASSWORD_READ:
  12974. case PEM_R_BAD_DECRYPT:
  12975. return ERR_LIB_PEM;
  12976. case EVP_R_BAD_DECRYPT:
  12977. case EVP_R_BN_DECODE_ERROR:
  12978. case EVP_R_DECODE_ERROR:
  12979. case EVP_R_PRIVATE_KEY_DECODE_ERROR:
  12980. return ERR_LIB_EVP;
  12981. case ASN1_R_HEADER_TOO_LONG:
  12982. return ERR_LIB_ASN1;
  12983. default:
  12984. return 0;
  12985. }
  12986. }
  12987. /* This function is to find global error values that are the same through out
  12988. * all library version. With wolfSSL having only one set of error codes the
  12989. * return value is pretty straight forward. The only thing needed is all wolfSSL
  12990. * error values are typically negative.
  12991. *
  12992. * Returns the error reason
  12993. */
  12994. int wolfSSL_ERR_GET_REASON(unsigned long err)
  12995. {
  12996. int ret = (int)err;
  12997. WOLFSSL_ENTER("wolfSSL_ERR_GET_REASON");
  12998. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  12999. /* Nginx looks for this error to know to stop parsing certificates.
  13000. * Same for HAProxy. */
  13001. if (err == ((ERR_LIB_PEM << 24) | PEM_R_NO_START_LINE) ||
  13002. ((err & 0xFFFFFFL) == -ASN_NO_PEM_HEADER) ||
  13003. ((err & 0xFFFL) == PEM_R_NO_START_LINE ))
  13004. return PEM_R_NO_START_LINE;
  13005. if (err == ((ERR_LIB_SSL << 24) | -SSL_R_HTTP_REQUEST))
  13006. return SSL_R_HTTP_REQUEST;
  13007. #endif
  13008. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  13009. if (err == ((ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG))
  13010. return ASN1_R_HEADER_TOO_LONG;
  13011. #endif
  13012. /* check if error value is in range of wolfSSL errors */
  13013. ret = 0 - ret; /* setting as negative value */
  13014. /* wolfCrypt range is less than MAX (-100)
  13015. wolfSSL range is MIN (-300) and lower */
  13016. if (ret < MAX_CODE_E && ret > MIN_CODE_E) {
  13017. return ret;
  13018. }
  13019. else {
  13020. WOLFSSL_MSG("Not in range of typical error values");
  13021. ret = (int)err;
  13022. }
  13023. return ret;
  13024. }
  13025. /* returns a string that describes the alert
  13026. *
  13027. * alertID the alert value to look up
  13028. */
  13029. const char* wolfSSL_alert_type_string_long(int alertID)
  13030. {
  13031. WOLFSSL_ENTER("wolfSSL_alert_type_string_long");
  13032. return AlertTypeToString(alertID);
  13033. }
  13034. const char* wolfSSL_alert_desc_string_long(int alertID)
  13035. {
  13036. WOLFSSL_ENTER("wolfSSL_alert_desc_string_long");
  13037. return AlertTypeToString(alertID);
  13038. }
  13039. #define STATE_STRINGS_PROTO(s) \
  13040. { \
  13041. {"SSLv3 " s, \
  13042. "SSLv3 " s, \
  13043. "SSLv3 " s}, \
  13044. {"TLSv1 " s, \
  13045. "TLSv1 " s, \
  13046. "TLSv1 " s}, \
  13047. {"TLSv1_1 " s, \
  13048. "TLSv1_1 " s, \
  13049. "TLSv1_1 " s}, \
  13050. {"TLSv1_2 " s, \
  13051. "TLSv1_2 " s, \
  13052. "TLSv1_2 " s}, \
  13053. {"TLSv1_3 " s, \
  13054. "TLSv1_3 " s, \
  13055. "TLSv1_3 " s}, \
  13056. {"DTLSv1 " s, \
  13057. "DTLSv1 " s, \
  13058. "DTLSv1 " s}, \
  13059. {"DTLSv1_2 " s, \
  13060. "DTLSv1_2 " s, \
  13061. "DTLSv1_2 " s}, \
  13062. {"DTLSv1_3 " s, \
  13063. "DTLSv1_3 " s, \
  13064. "DTLSv1_3 " s}, \
  13065. }
  13066. #define STATE_STRINGS_PROTO_RW(s) \
  13067. { \
  13068. {"SSLv3 read " s, \
  13069. "SSLv3 write " s, \
  13070. "SSLv3 " s}, \
  13071. {"TLSv1 read " s, \
  13072. "TLSv1 write " s, \
  13073. "TLSv1 " s}, \
  13074. {"TLSv1_1 read " s, \
  13075. "TLSv1_1 write " s, \
  13076. "TLSv1_1 " s}, \
  13077. {"TLSv1_2 read " s, \
  13078. "TLSv1_2 write " s, \
  13079. "TLSv1_2 " s}, \
  13080. {"TLSv1_3 read " s, \
  13081. "TLSv1_3 write " s, \
  13082. "TLSv1_3 " s}, \
  13083. {"DTLSv1 read " s, \
  13084. "DTLSv1 write " s, \
  13085. "DTLSv1 " s}, \
  13086. {"DTLSv1_2 read " s, \
  13087. "DTLSv1_2 write " s, \
  13088. "DTLSv1_2 " s}, \
  13089. {"DTLSv1_3 read " s, \
  13090. "DTLSv1_3 write " s, \
  13091. "DTLSv1_3 " s}, \
  13092. }
  13093. /* Gets the current state of the WOLFSSL structure
  13094. *
  13095. * ssl WOLFSSL structure to get state of
  13096. *
  13097. * Returns a human readable string of the WOLFSSL structure state
  13098. */
  13099. const char* wolfSSL_state_string_long(const WOLFSSL* ssl)
  13100. {
  13101. static const char* OUTPUT_STR[24][8][3] = {
  13102. STATE_STRINGS_PROTO("Initialization"),
  13103. STATE_STRINGS_PROTO_RW("Server Hello Request"),
  13104. STATE_STRINGS_PROTO_RW("Server Hello Verify Request"),
  13105. STATE_STRINGS_PROTO_RW("Server Hello Retry Request"),
  13106. STATE_STRINGS_PROTO_RW("Server Hello"),
  13107. STATE_STRINGS_PROTO_RW("Server Certificate Status"),
  13108. STATE_STRINGS_PROTO_RW("Server Encrypted Extensions"),
  13109. STATE_STRINGS_PROTO_RW("Server Session Ticket"),
  13110. STATE_STRINGS_PROTO_RW("Server Certificate Request"),
  13111. STATE_STRINGS_PROTO_RW("Server Cert"),
  13112. STATE_STRINGS_PROTO_RW("Server Key Exchange"),
  13113. STATE_STRINGS_PROTO_RW("Server Hello Done"),
  13114. STATE_STRINGS_PROTO_RW("Server Change CipherSpec"),
  13115. STATE_STRINGS_PROTO_RW("Server Finished"),
  13116. STATE_STRINGS_PROTO_RW("server Key Update"),
  13117. STATE_STRINGS_PROTO_RW("Client Hello"),
  13118. STATE_STRINGS_PROTO_RW("Client Key Exchange"),
  13119. STATE_STRINGS_PROTO_RW("Client Cert"),
  13120. STATE_STRINGS_PROTO_RW("Client Change CipherSpec"),
  13121. STATE_STRINGS_PROTO_RW("Client Certificate Verify"),
  13122. STATE_STRINGS_PROTO_RW("Client End Of Early Data"),
  13123. STATE_STRINGS_PROTO_RW("Client Finished"),
  13124. STATE_STRINGS_PROTO_RW("Client Key Update"),
  13125. STATE_STRINGS_PROTO("Handshake Done"),
  13126. };
  13127. enum ProtocolVer {
  13128. SSL_V3 = 0,
  13129. TLS_V1,
  13130. TLS_V1_1,
  13131. TLS_V1_2,
  13132. TLS_V1_3,
  13133. DTLS_V1,
  13134. DTLS_V1_2,
  13135. DTLS_V1_3,
  13136. UNKNOWN = 100
  13137. };
  13138. enum IOMode {
  13139. SS_READ = 0,
  13140. SS_WRITE,
  13141. SS_NEITHER
  13142. };
  13143. enum SslState {
  13144. ss_null_state = 0,
  13145. ss_server_hellorequest,
  13146. ss_server_helloverify,
  13147. ss_server_helloretryrequest,
  13148. ss_server_hello,
  13149. ss_server_certificatestatus,
  13150. ss_server_encryptedextensions,
  13151. ss_server_sessionticket,
  13152. ss_server_certrequest,
  13153. ss_server_cert,
  13154. ss_server_keyexchange,
  13155. ss_server_hellodone,
  13156. ss_server_changecipherspec,
  13157. ss_server_finished,
  13158. ss_server_keyupdate,
  13159. ss_client_hello,
  13160. ss_client_keyexchange,
  13161. ss_client_cert,
  13162. ss_client_changecipherspec,
  13163. ss_client_certverify,
  13164. ss_client_endofearlydata,
  13165. ss_client_finished,
  13166. ss_client_keyupdate,
  13167. ss_handshake_done
  13168. };
  13169. int protocol = 0;
  13170. int cbmode = 0;
  13171. int state = 0;
  13172. WOLFSSL_ENTER("wolfSSL_state_string_long");
  13173. if (ssl == NULL) {
  13174. WOLFSSL_MSG("Null argument passed in");
  13175. return NULL;
  13176. }
  13177. /* Get state of callback */
  13178. if (ssl->cbmode == SSL_CB_MODE_WRITE) {
  13179. cbmode = SS_WRITE;
  13180. }
  13181. else if (ssl->cbmode == SSL_CB_MODE_READ) {
  13182. cbmode = SS_READ;
  13183. }
  13184. else {
  13185. cbmode = SS_NEITHER;
  13186. }
  13187. /* Get protocol version */
  13188. switch (ssl->version.major) {
  13189. case SSLv3_MAJOR:
  13190. switch (ssl->version.minor) {
  13191. case SSLv3_MINOR:
  13192. protocol = SSL_V3;
  13193. break;
  13194. case TLSv1_MINOR:
  13195. protocol = TLS_V1;
  13196. break;
  13197. case TLSv1_1_MINOR:
  13198. protocol = TLS_V1_1;
  13199. break;
  13200. case TLSv1_2_MINOR:
  13201. protocol = TLS_V1_2;
  13202. break;
  13203. case TLSv1_3_MINOR:
  13204. protocol = TLS_V1_3;
  13205. break;
  13206. default:
  13207. protocol = UNKNOWN;
  13208. }
  13209. break;
  13210. case DTLS_MAJOR:
  13211. switch (ssl->version.minor) {
  13212. case DTLS_MINOR:
  13213. protocol = DTLS_V1;
  13214. break;
  13215. case DTLSv1_2_MINOR:
  13216. protocol = DTLS_V1_2;
  13217. break;
  13218. case DTLSv1_3_MINOR:
  13219. protocol = DTLS_V1_3;
  13220. break;
  13221. default:
  13222. protocol = UNKNOWN;
  13223. }
  13224. break;
  13225. default:
  13226. protocol = UNKNOWN;
  13227. }
  13228. /* accept process */
  13229. if (ssl->cbmode == SSL_CB_MODE_READ) {
  13230. state = ssl->cbtype;
  13231. switch (state) {
  13232. case hello_request:
  13233. state = ss_server_hellorequest;
  13234. break;
  13235. case client_hello:
  13236. state = ss_client_hello;
  13237. break;
  13238. case server_hello:
  13239. state = ss_server_hello;
  13240. break;
  13241. case hello_verify_request:
  13242. state = ss_server_helloverify;
  13243. break;
  13244. case session_ticket:
  13245. state = ss_server_sessionticket;
  13246. break;
  13247. case end_of_early_data:
  13248. state = ss_client_endofearlydata;
  13249. break;
  13250. case hello_retry_request:
  13251. state = ss_server_helloretryrequest;
  13252. break;
  13253. case encrypted_extensions:
  13254. state = ss_server_encryptedextensions;
  13255. break;
  13256. case certificate:
  13257. if (ssl->options.side == WOLFSSL_SERVER_END)
  13258. state = ss_client_cert;
  13259. else if (ssl->options.side == WOLFSSL_CLIENT_END)
  13260. state = ss_server_cert;
  13261. else {
  13262. WOLFSSL_MSG("Unknown State");
  13263. state = ss_null_state;
  13264. }
  13265. break;
  13266. case server_key_exchange:
  13267. state = ss_server_keyexchange;
  13268. break;
  13269. case certificate_request:
  13270. state = ss_server_certrequest;
  13271. break;
  13272. case server_hello_done:
  13273. state = ss_server_hellodone;
  13274. break;
  13275. case certificate_verify:
  13276. state = ss_client_certverify;
  13277. break;
  13278. case client_key_exchange:
  13279. state = ss_client_keyexchange;
  13280. break;
  13281. case finished:
  13282. if (ssl->options.side == WOLFSSL_SERVER_END)
  13283. state = ss_client_finished;
  13284. else if (ssl->options.side == WOLFSSL_CLIENT_END)
  13285. state = ss_server_finished;
  13286. else {
  13287. WOLFSSL_MSG("Unknown State");
  13288. state = ss_null_state;
  13289. }
  13290. break;
  13291. case certificate_status:
  13292. state = ss_server_certificatestatus;
  13293. break;
  13294. case key_update:
  13295. if (ssl->options.side == WOLFSSL_SERVER_END)
  13296. state = ss_client_keyupdate;
  13297. else if (ssl->options.side == WOLFSSL_CLIENT_END)
  13298. state = ss_server_keyupdate;
  13299. else {
  13300. WOLFSSL_MSG("Unknown State");
  13301. state = ss_null_state;
  13302. }
  13303. break;
  13304. case change_cipher_hs:
  13305. if (ssl->options.side == WOLFSSL_SERVER_END)
  13306. state = ss_client_changecipherspec;
  13307. else if (ssl->options.side == WOLFSSL_CLIENT_END)
  13308. state = ss_server_changecipherspec;
  13309. else {
  13310. WOLFSSL_MSG("Unknown State");
  13311. state = ss_null_state;
  13312. }
  13313. break;
  13314. default:
  13315. WOLFSSL_MSG("Unknown State");
  13316. state = ss_null_state;
  13317. }
  13318. }
  13319. else {
  13320. /* Send process */
  13321. if (ssl->options.side == WOLFSSL_SERVER_END)
  13322. state = ssl->options.serverState;
  13323. else
  13324. state = ssl->options.clientState;
  13325. switch (state) {
  13326. case SERVER_HELLOVERIFYREQUEST_COMPLETE:
  13327. state = ss_server_helloverify;
  13328. break;
  13329. case SERVER_HELLO_RETRY_REQUEST_COMPLETE:
  13330. state = ss_server_helloretryrequest;
  13331. break;
  13332. case SERVER_HELLO_COMPLETE:
  13333. state = ss_server_hello;
  13334. break;
  13335. case SERVER_ENCRYPTED_EXTENSIONS_COMPLETE:
  13336. state = ss_server_encryptedextensions;
  13337. break;
  13338. case SERVER_CERT_COMPLETE:
  13339. state = ss_server_cert;
  13340. break;
  13341. case SERVER_KEYEXCHANGE_COMPLETE:
  13342. state = ss_server_keyexchange;
  13343. break;
  13344. case SERVER_HELLODONE_COMPLETE:
  13345. state = ss_server_hellodone;
  13346. break;
  13347. case SERVER_CHANGECIPHERSPEC_COMPLETE:
  13348. state = ss_server_changecipherspec;
  13349. break;
  13350. case SERVER_FINISHED_COMPLETE:
  13351. state = ss_server_finished;
  13352. break;
  13353. case CLIENT_HELLO_RETRY:
  13354. case CLIENT_HELLO_COMPLETE:
  13355. state = ss_client_hello;
  13356. break;
  13357. case CLIENT_KEYEXCHANGE_COMPLETE:
  13358. state = ss_client_keyexchange;
  13359. break;
  13360. case CLIENT_CHANGECIPHERSPEC_COMPLETE:
  13361. state = ss_client_changecipherspec;
  13362. break;
  13363. case CLIENT_FINISHED_COMPLETE:
  13364. state = ss_client_finished;
  13365. break;
  13366. case HANDSHAKE_DONE:
  13367. state = ss_handshake_done;
  13368. break;
  13369. default:
  13370. WOLFSSL_MSG("Unknown State");
  13371. state = ss_null_state;
  13372. }
  13373. }
  13374. if (protocol == UNKNOWN) {
  13375. WOLFSSL_MSG("Unknown protocol");
  13376. return "";
  13377. }
  13378. else {
  13379. return OUTPUT_STR[state][protocol][cbmode];
  13380. }
  13381. }
  13382. #endif /* OPENSSL_EXTRA */
  13383. static long wolf_set_options(long old_op, long op)
  13384. {
  13385. /* if SSL_OP_ALL then turn all bug workarounds on */
  13386. if ((op & WOLFSSL_OP_ALL) == WOLFSSL_OP_ALL) {
  13387. WOLFSSL_MSG("\tSSL_OP_ALL");
  13388. }
  13389. /* by default cookie exchange is on with DTLS */
  13390. if ((op & WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE) {
  13391. WOLFSSL_MSG("\tSSL_OP_COOKIE_EXCHANGE : on by default");
  13392. }
  13393. if ((op & WOLFSSL_OP_NO_SSLv2) == WOLFSSL_OP_NO_SSLv2) {
  13394. WOLFSSL_MSG("\tWOLFSSL_OP_NO_SSLv2 : wolfSSL does not support SSLv2");
  13395. }
  13396. #ifdef SSL_OP_NO_TLSv1_3
  13397. if ((op & WOLFSSL_OP_NO_TLSv1_3) == WOLFSSL_OP_NO_TLSv1_3) {
  13398. WOLFSSL_MSG("\tSSL_OP_NO_TLSv1_3");
  13399. }
  13400. #endif
  13401. if ((op & WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2) {
  13402. WOLFSSL_MSG("\tSSL_OP_NO_TLSv1_2");
  13403. }
  13404. if ((op & WOLFSSL_OP_NO_TLSv1_1) == WOLFSSL_OP_NO_TLSv1_1) {
  13405. WOLFSSL_MSG("\tSSL_OP_NO_TLSv1_1");
  13406. }
  13407. if ((op & WOLFSSL_OP_NO_TLSv1) == WOLFSSL_OP_NO_TLSv1) {
  13408. WOLFSSL_MSG("\tSSL_OP_NO_TLSv1");
  13409. }
  13410. if ((op & WOLFSSL_OP_NO_SSLv3) == WOLFSSL_OP_NO_SSLv3) {
  13411. WOLFSSL_MSG("\tSSL_OP_NO_SSLv3");
  13412. }
  13413. if ((op & WOLFSSL_OP_CIPHER_SERVER_PREFERENCE) ==
  13414. WOLFSSL_OP_CIPHER_SERVER_PREFERENCE) {
  13415. WOLFSSL_MSG("\tWOLFSSL_OP_CIPHER_SERVER_PREFERENCE");
  13416. }
  13417. if ((op & WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION) {
  13418. #ifdef HAVE_LIBZ
  13419. WOLFSSL_MSG("SSL_OP_NO_COMPRESSION");
  13420. #else
  13421. WOLFSSL_MSG("SSL_OP_NO_COMPRESSION: compression not compiled in");
  13422. #endif
  13423. }
  13424. return old_op | op;
  13425. }
  13426. long wolfSSL_set_options(WOLFSSL* ssl, long op)
  13427. {
  13428. word16 haveRSA = 1;
  13429. word16 havePSK = 0;
  13430. int keySz = 0;
  13431. WOLFSSL_ENTER("wolfSSL_set_options");
  13432. if (ssl == NULL) {
  13433. return 0;
  13434. }
  13435. ssl->options.mask = wolf_set_options(ssl->options.mask, op);
  13436. if ((ssl->options.mask & WOLFSSL_OP_NO_TLSv1_3) == WOLFSSL_OP_NO_TLSv1_3) {
  13437. if (ssl->version.minor == TLSv1_3_MINOR)
  13438. ssl->version.minor = TLSv1_2_MINOR;
  13439. }
  13440. if ((ssl->options.mask & WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2) {
  13441. if (ssl->version.minor == TLSv1_2_MINOR)
  13442. ssl->version.minor = TLSv1_1_MINOR;
  13443. }
  13444. if ((ssl->options.mask & WOLFSSL_OP_NO_TLSv1_1) == WOLFSSL_OP_NO_TLSv1_1) {
  13445. if (ssl->version.minor == TLSv1_1_MINOR)
  13446. ssl->version.minor = TLSv1_MINOR;
  13447. }
  13448. if ((ssl->options.mask & WOLFSSL_OP_NO_TLSv1) == WOLFSSL_OP_NO_TLSv1) {
  13449. if (ssl->version.minor == TLSv1_MINOR)
  13450. ssl->version.minor = SSLv3_MINOR;
  13451. }
  13452. if ((ssl->options.mask & WOLFSSL_OP_NO_COMPRESSION)
  13453. == WOLFSSL_OP_NO_COMPRESSION) {
  13454. #ifdef HAVE_LIBZ
  13455. ssl->options.usingCompression = 0;
  13456. #endif
  13457. }
  13458. #if defined(HAVE_SESSION_TICKET) && (defined(OPENSSL_EXTRA) \
  13459. || defined(HAVE_WEBSERVER) || defined(WOLFSSL_WPAS_SMALL))
  13460. if ((ssl->options.mask & WOLFSSL_OP_NO_TICKET) == WOLFSSL_OP_NO_TICKET) {
  13461. ssl->options.noTicketTls12 = 1;
  13462. }
  13463. #endif
  13464. /* in the case of a version change the cipher suites should be reset */
  13465. #ifndef NO_PSK
  13466. havePSK = ssl->options.havePSK;
  13467. #endif
  13468. #ifdef NO_RSA
  13469. haveRSA = 0;
  13470. #endif
  13471. #ifndef NO_CERTS
  13472. keySz = ssl->buffers.keySz;
  13473. #endif
  13474. if (ssl->options.side != WOLFSSL_NEITHER_END) {
  13475. if (AllocateSuites(ssl) != 0)
  13476. return 0;
  13477. InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
  13478. ssl->options.haveDH, ssl->options.haveECDSAsig,
  13479. ssl->options.haveECC, TRUE, ssl->options.haveStaticECC,
  13480. ssl->options.haveFalconSig, ssl->options.haveDilithiumSig,
  13481. ssl->options.useAnon, TRUE, ssl->options.side);
  13482. }
  13483. return ssl->options.mask;
  13484. }
  13485. long wolfSSL_get_options(const WOLFSSL* ssl)
  13486. {
  13487. WOLFSSL_ENTER("wolfSSL_get_options");
  13488. if(ssl == NULL)
  13489. return WOLFSSL_FAILURE;
  13490. return ssl->options.mask;
  13491. }
  13492. #if defined(HAVE_SECURE_RENEGOTIATION) \
  13493. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  13494. /* clears the counter for number of renegotiations done
  13495. * returns the current count before it is cleared */
  13496. long wolfSSL_clear_num_renegotiations(WOLFSSL *s)
  13497. {
  13498. long total;
  13499. WOLFSSL_ENTER("wolfSSL_clear_num_renegotiations");
  13500. if (s == NULL)
  13501. return 0;
  13502. total = s->secure_rene_count;
  13503. s->secure_rene_count = 0;
  13504. return total;
  13505. }
  13506. /* return the number of renegotiations since wolfSSL_new */
  13507. long wolfSSL_total_renegotiations(WOLFSSL *s)
  13508. {
  13509. WOLFSSL_ENTER("wolfSSL_total_renegotiations");
  13510. return wolfSSL_num_renegotiations(s);
  13511. }
  13512. /* return the number of renegotiations since wolfSSL_new */
  13513. long wolfSSL_num_renegotiations(WOLFSSL* s)
  13514. {
  13515. if (s == NULL) {
  13516. return 0;
  13517. }
  13518. return s->secure_rene_count;
  13519. }
  13520. /* Is there a renegotiation currently in progress? */
  13521. int wolfSSL_SSL_renegotiate_pending(WOLFSSL *s)
  13522. {
  13523. return s && s->options.handShakeDone &&
  13524. s->options.handShakeState != HANDSHAKE_DONE ? 1 : 0;
  13525. }
  13526. #endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
  13527. #ifdef OPENSSL_EXTRA
  13528. long wolfSSL_clear_options(WOLFSSL* ssl, long opt)
  13529. {
  13530. WOLFSSL_ENTER("wolfSSL_clear_options");
  13531. if(ssl == NULL)
  13532. return WOLFSSL_FAILURE;
  13533. ssl->options.mask &= ~opt;
  13534. return ssl->options.mask;
  13535. }
  13536. #ifdef HAVE_PK_CALLBACKS
  13537. long wolfSSL_set_tlsext_debug_arg(WOLFSSL* ssl, void *arg)
  13538. {
  13539. if (ssl == NULL) {
  13540. return WOLFSSL_FAILURE;
  13541. }
  13542. ssl->loggingCtx = arg;
  13543. return WOLFSSL_SUCCESS;
  13544. }
  13545. #endif /* HAVE_PK_CALLBACKS */
  13546. /*** TBD ***/
  13547. #ifndef NO_WOLFSSL_STUB
  13548. int wolfSSL_sk_SSL_COMP_zero(WOLFSSL_STACK* st)
  13549. {
  13550. (void)st;
  13551. WOLFSSL_STUB("wolfSSL_sk_SSL_COMP_zero");
  13552. /* wolfSSL_set_options(ssl, SSL_OP_NO_COMPRESSION); */
  13553. return WOLFSSL_FAILURE;
  13554. }
  13555. #endif
  13556. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  13557. long wolfSSL_set_tlsext_status_type(WOLFSSL *s, int type)
  13558. {
  13559. WOLFSSL_ENTER("wolfSSL_set_tlsext_status_type");
  13560. if (s == NULL){
  13561. return BAD_FUNC_ARG;
  13562. }
  13563. if (type == TLSEXT_STATUSTYPE_ocsp){
  13564. int r = TLSX_UseCertificateStatusRequest(&s->extensions, (byte)type, 0,
  13565. s, s->heap, s->devId);
  13566. return (long)r;
  13567. } else {
  13568. WOLFSSL_MSG(
  13569. "SSL_set_tlsext_status_type only supports TLSEXT_STATUSTYPE_ocsp type.");
  13570. return WOLFSSL_FAILURE;
  13571. }
  13572. }
  13573. long wolfSSL_get_tlsext_status_type(WOLFSSL *s)
  13574. {
  13575. TLSX* extension;
  13576. if (s == NULL)
  13577. return WOLFSSL_FATAL_ERROR;
  13578. extension = TLSX_Find(s->extensions, TLSX_STATUS_REQUEST);
  13579. return extension != NULL ? TLSEXT_STATUSTYPE_ocsp : WOLFSSL_FATAL_ERROR;
  13580. }
  13581. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  13582. #ifndef NO_WOLFSSL_STUB
  13583. long wolfSSL_get_tlsext_status_exts(WOLFSSL *s, void *arg)
  13584. {
  13585. (void)s;
  13586. (void)arg;
  13587. WOLFSSL_STUB("wolfSSL_get_tlsext_status_exts");
  13588. return WOLFSSL_FAILURE;
  13589. }
  13590. #endif
  13591. /*** TBD ***/
  13592. #ifndef NO_WOLFSSL_STUB
  13593. long wolfSSL_set_tlsext_status_exts(WOLFSSL *s, void *arg)
  13594. {
  13595. (void)s;
  13596. (void)arg;
  13597. WOLFSSL_STUB("wolfSSL_set_tlsext_status_exts");
  13598. return WOLFSSL_FAILURE;
  13599. }
  13600. #endif
  13601. /*** TBD ***/
  13602. #ifndef NO_WOLFSSL_STUB
  13603. long wolfSSL_get_tlsext_status_ids(WOLFSSL *s, void *arg)
  13604. {
  13605. (void)s;
  13606. (void)arg;
  13607. WOLFSSL_STUB("wolfSSL_get_tlsext_status_ids");
  13608. return WOLFSSL_FAILURE;
  13609. }
  13610. #endif
  13611. /*** TBD ***/
  13612. #ifndef NO_WOLFSSL_STUB
  13613. long wolfSSL_set_tlsext_status_ids(WOLFSSL *s, void *arg)
  13614. {
  13615. (void)s;
  13616. (void)arg;
  13617. WOLFSSL_STUB("wolfSSL_set_tlsext_status_ids");
  13618. return WOLFSSL_FAILURE;
  13619. }
  13620. #endif
  13621. #ifndef NO_WOLFSSL_STUB
  13622. /*** TBD ***/
  13623. WOLFSSL_EVP_PKEY *wolfSSL_get_privatekey(const WOLFSSL *ssl)
  13624. {
  13625. (void)ssl;
  13626. WOLFSSL_STUB("SSL_get_privatekey");
  13627. return NULL;
  13628. }
  13629. #endif
  13630. #ifndef NO_WOLFSSL_STUB
  13631. /*** TBD ***/
  13632. void SSL_CTX_set_tmp_dh_callback(WOLFSSL_CTX *ctx,
  13633. WOLFSSL_DH *(*dh) (WOLFSSL *ssl, int is_export, int keylength))
  13634. {
  13635. (void)ctx;
  13636. (void)dh;
  13637. WOLFSSL_STUB("SSL_CTX_set_tmp_dh_callback");
  13638. }
  13639. #endif
  13640. #ifndef NO_WOLFSSL_STUB
  13641. /*** TBD ***/
  13642. WOLF_STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
  13643. {
  13644. WOLFSSL_STUB("SSL_COMP_get_compression_methods");
  13645. return NULL;
  13646. }
  13647. #endif
  13648. int wolfSSL_sk_SSL_CIPHER_num(const WOLF_STACK_OF(WOLFSSL_CIPHER)* p)
  13649. {
  13650. WOLFSSL_ENTER("wolfSSL_sk_SSL_CIPHER_num");
  13651. if (p == NULL) {
  13652. return WOLFSSL_FATAL_ERROR;
  13653. }
  13654. return (int)p->num;
  13655. }
  13656. WOLFSSL_CIPHER* wolfSSL_sk_SSL_CIPHER_value(WOLFSSL_STACK* sk, int i)
  13657. {
  13658. WOLFSSL_ENTER("wolfSSL_sk_SSL_CIPHER_value");
  13659. return (WOLFSSL_CIPHER*)wolfSSL_sk_value(sk, i);
  13660. }
  13661. #if !defined(NETOS)
  13662. void ERR_load_SSL_strings(void)
  13663. {
  13664. }
  13665. #endif
  13666. #ifdef HAVE_OCSP
  13667. long wolfSSL_get_tlsext_status_ocsp_resp(WOLFSSL *s, unsigned char **resp)
  13668. {
  13669. if (s == NULL || resp == NULL)
  13670. return 0;
  13671. *resp = s->ocspResp;
  13672. return s->ocspRespSz;
  13673. }
  13674. long wolfSSL_set_tlsext_status_ocsp_resp(WOLFSSL *s, unsigned char *resp,
  13675. int len)
  13676. {
  13677. if (s == NULL)
  13678. return WOLFSSL_FAILURE;
  13679. s->ocspResp = resp;
  13680. s->ocspRespSz = len;
  13681. return WOLFSSL_SUCCESS;
  13682. }
  13683. #endif /* HAVE_OCSP */
  13684. #ifdef HAVE_MAX_FRAGMENT
  13685. #ifndef NO_WOLFSSL_CLIENT
  13686. /**
  13687. * Set max fragment tls extension
  13688. * @param c a pointer to WOLFSSL_CTX object
  13689. * @param mode maximum fragment length mode
  13690. * @return 1 on success, otherwise 0 or negative error code
  13691. */
  13692. int wolfSSL_CTX_set_tlsext_max_fragment_length(WOLFSSL_CTX *c,
  13693. unsigned char mode)
  13694. {
  13695. if (c == NULL || (mode < WOLFSSL_MFL_2_9 || mode > WOLFSSL_MFL_2_12 ))
  13696. return BAD_FUNC_ARG;
  13697. return wolfSSL_CTX_UseMaxFragment(c, mode);
  13698. }
  13699. /**
  13700. * Set max fragment tls extension
  13701. * @param c a pointer to WOLFSSL object
  13702. * @param mode maximum fragment length mode
  13703. * @return 1 on success, otherwise 0 or negative error code
  13704. */
  13705. int wolfSSL_set_tlsext_max_fragment_length(WOLFSSL *s, unsigned char mode)
  13706. {
  13707. if (s == NULL || (mode < WOLFSSL_MFL_2_9 || mode > WOLFSSL_MFL_2_12 ))
  13708. return BAD_FUNC_ARG;
  13709. return wolfSSL_UseMaxFragment(s, mode);
  13710. }
  13711. #endif /* NO_WOLFSSL_CLIENT */
  13712. #endif /* HAVE_MAX_FRAGMENT */
  13713. #endif /* OPENSSL_EXTRA */
  13714. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  13715. size_t wolfSSL_get_finished(const WOLFSSL *ssl, void *buf, size_t count)
  13716. {
  13717. byte len = 0;
  13718. WOLFSSL_ENTER("wolfSSL_get_finished");
  13719. if (!ssl || !buf || count < TLS_FINISHED_SZ) {
  13720. WOLFSSL_MSG("Bad parameter");
  13721. return WOLFSSL_FAILURE;
  13722. }
  13723. if (ssl->options.side == WOLFSSL_SERVER_END) {
  13724. len = ssl->serverFinished_len;
  13725. XMEMCPY(buf, ssl->serverFinished, len);
  13726. }
  13727. else {
  13728. len = ssl->clientFinished_len;
  13729. XMEMCPY(buf, ssl->clientFinished, len);
  13730. }
  13731. return len;
  13732. }
  13733. size_t wolfSSL_get_peer_finished(const WOLFSSL *ssl, void *buf, size_t count)
  13734. {
  13735. byte len = 0;
  13736. WOLFSSL_ENTER("wolfSSL_get_peer_finished");
  13737. if (!ssl || !buf || count < TLS_FINISHED_SZ) {
  13738. WOLFSSL_MSG("Bad parameter");
  13739. return WOLFSSL_FAILURE;
  13740. }
  13741. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  13742. len = ssl->serverFinished_len;
  13743. XMEMCPY(buf, ssl->serverFinished, len);
  13744. }
  13745. else {
  13746. len = ssl->clientFinished_len;
  13747. XMEMCPY(buf, ssl->clientFinished, len);
  13748. }
  13749. return len;
  13750. }
  13751. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  13752. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  13753. defined(OPENSSL_ALL)
  13754. long wolfSSL_get_verify_result(const WOLFSSL *ssl)
  13755. {
  13756. if (ssl == NULL) {
  13757. return WOLFSSL_FAILURE;
  13758. }
  13759. return ssl->peerVerifyRet;
  13760. }
  13761. #endif
  13762. #ifdef OPENSSL_EXTRA
  13763. #ifndef NO_WOLFSSL_STUB
  13764. /* shows the number of accepts attempted by CTX in it's lifetime */
  13765. long wolfSSL_CTX_sess_accept(WOLFSSL_CTX* ctx)
  13766. {
  13767. WOLFSSL_STUB("wolfSSL_CTX_sess_accept");
  13768. (void)ctx;
  13769. return 0;
  13770. }
  13771. #endif
  13772. #ifndef NO_WOLFSSL_STUB
  13773. /* shows the number of connects attempted CTX in it's lifetime */
  13774. long wolfSSL_CTX_sess_connect(WOLFSSL_CTX* ctx)
  13775. {
  13776. WOLFSSL_STUB("wolfSSL_CTX_sess_connect");
  13777. (void)ctx;
  13778. return 0;
  13779. }
  13780. #endif
  13781. #ifndef NO_WOLFSSL_STUB
  13782. /* shows the number of accepts completed by CTX in it's lifetime */
  13783. long wolfSSL_CTX_sess_accept_good(WOLFSSL_CTX* ctx)
  13784. {
  13785. WOLFSSL_STUB("wolfSSL_CTX_sess_accept_good");
  13786. (void)ctx;
  13787. return 0;
  13788. }
  13789. #endif
  13790. #ifndef NO_WOLFSSL_STUB
  13791. /* shows the number of connects completed by CTX in it's lifetime */
  13792. long wolfSSL_CTX_sess_connect_good(WOLFSSL_CTX* ctx)
  13793. {
  13794. WOLFSSL_STUB("wolfSSL_CTX_sess_connect_good");
  13795. (void)ctx;
  13796. return 0;
  13797. }
  13798. #endif
  13799. #ifndef NO_WOLFSSL_STUB
  13800. /* shows the number of renegotiation accepts attempted by CTX */
  13801. long wolfSSL_CTX_sess_accept_renegotiate(WOLFSSL_CTX* ctx)
  13802. {
  13803. WOLFSSL_STUB("wolfSSL_CTX_sess_accept_renegotiate");
  13804. (void)ctx;
  13805. return 0;
  13806. }
  13807. #endif
  13808. #ifndef NO_WOLFSSL_STUB
  13809. /* shows the number of renegotiation accepts attempted by CTX */
  13810. long wolfSSL_CTX_sess_connect_renegotiate(WOLFSSL_CTX* ctx)
  13811. {
  13812. WOLFSSL_STUB("wolfSSL_CTX_sess_connect_renegotiate");
  13813. (void)ctx;
  13814. return 0;
  13815. }
  13816. #endif
  13817. #ifndef NO_WOLFSSL_STUB
  13818. long wolfSSL_CTX_sess_hits(WOLFSSL_CTX* ctx)
  13819. {
  13820. WOLFSSL_STUB("wolfSSL_CTX_sess_hits");
  13821. (void)ctx;
  13822. return 0;
  13823. }
  13824. #endif
  13825. #ifndef NO_WOLFSSL_STUB
  13826. long wolfSSL_CTX_sess_cb_hits(WOLFSSL_CTX* ctx)
  13827. {
  13828. WOLFSSL_STUB("wolfSSL_CTX_sess_cb_hits");
  13829. (void)ctx;
  13830. return 0;
  13831. }
  13832. #endif
  13833. #ifndef NO_WOLFSSL_STUB
  13834. long wolfSSL_CTX_sess_cache_full(WOLFSSL_CTX* ctx)
  13835. {
  13836. WOLFSSL_STUB("wolfSSL_CTX_sess_cache_full");
  13837. (void)ctx;
  13838. return 0;
  13839. }
  13840. #endif
  13841. #ifndef NO_WOLFSSL_STUB
  13842. long wolfSSL_CTX_sess_misses(WOLFSSL_CTX* ctx)
  13843. {
  13844. WOLFSSL_STUB("wolfSSL_CTX_sess_misses");
  13845. (void)ctx;
  13846. return 0;
  13847. }
  13848. #endif
  13849. #ifndef NO_WOLFSSL_STUB
  13850. long wolfSSL_CTX_sess_timeouts(WOLFSSL_CTX* ctx)
  13851. {
  13852. WOLFSSL_STUB("wolfSSL_CTX_sess_timeouts");
  13853. (void)ctx;
  13854. return 0;
  13855. }
  13856. #endif
  13857. #ifndef NO_CERTS
  13858. long wolfSSL_CTX_set_tlsext_status_arg(WOLFSSL_CTX* ctx, void* arg)
  13859. {
  13860. if (ctx == NULL || ctx->cm == NULL) {
  13861. return WOLFSSL_FAILURE;
  13862. }
  13863. ctx->cm->ocspIOCtx = arg;
  13864. return WOLFSSL_SUCCESS;
  13865. }
  13866. #endif /* !NO_CERTS */
  13867. int wolfSSL_get_read_ahead(const WOLFSSL* ssl)
  13868. {
  13869. if (ssl == NULL) {
  13870. return WOLFSSL_FAILURE;
  13871. }
  13872. return ssl->readAhead;
  13873. }
  13874. int wolfSSL_set_read_ahead(WOLFSSL* ssl, int v)
  13875. {
  13876. if (ssl == NULL) {
  13877. return WOLFSSL_FAILURE;
  13878. }
  13879. ssl->readAhead = (byte)v;
  13880. return WOLFSSL_SUCCESS;
  13881. }
  13882. int wolfSSL_CTX_get_read_ahead(WOLFSSL_CTX* ctx)
  13883. {
  13884. if (ctx == NULL) {
  13885. return WOLFSSL_FAILURE;
  13886. }
  13887. return ctx->readAhead;
  13888. }
  13889. int wolfSSL_CTX_set_read_ahead(WOLFSSL_CTX* ctx, int v)
  13890. {
  13891. if (ctx == NULL) {
  13892. return WOLFSSL_FAILURE;
  13893. }
  13894. ctx->readAhead = (byte)v;
  13895. return WOLFSSL_SUCCESS;
  13896. }
  13897. long wolfSSL_CTX_set_tlsext_opaque_prf_input_callback_arg(WOLFSSL_CTX* ctx,
  13898. void* arg)
  13899. {
  13900. if (ctx == NULL) {
  13901. return WOLFSSL_FAILURE;
  13902. }
  13903. ctx->userPRFArg = arg;
  13904. return WOLFSSL_SUCCESS;
  13905. }
  13906. #endif /* OPENSSL_EXTRA */
  13907. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  13908. int wolfSSL_sk_num(const WOLFSSL_STACK* sk)
  13909. {
  13910. WOLFSSL_ENTER("wolfSSL_sk_num");
  13911. if (sk == NULL)
  13912. return 0;
  13913. return (int)sk->num;
  13914. }
  13915. void* wolfSSL_sk_value(const WOLFSSL_STACK* sk, int i)
  13916. {
  13917. WOLFSSL_ENTER("wolfSSL_sk_value");
  13918. for (; sk != NULL && i > 0; i--)
  13919. sk = sk->next;
  13920. if (sk == NULL)
  13921. return NULL;
  13922. switch (sk->type) {
  13923. case STACK_TYPE_X509:
  13924. return (void*)sk->data.x509;
  13925. case STACK_TYPE_GEN_NAME:
  13926. return (void*)sk->data.gn;
  13927. case STACK_TYPE_BIO:
  13928. return (void*)sk->data.bio;
  13929. case STACK_TYPE_OBJ:
  13930. return (void*)sk->data.obj;
  13931. case STACK_TYPE_STRING:
  13932. return (void*)sk->data.string;
  13933. case STACK_TYPE_CIPHER:
  13934. return (void*)&sk->data.cipher;
  13935. case STACK_TYPE_ACCESS_DESCRIPTION:
  13936. return (void*)sk->data.access;
  13937. case STACK_TYPE_X509_EXT:
  13938. return (void*)sk->data.ext;
  13939. case STACK_TYPE_X509_REQ_ATTR:
  13940. return (void*)sk->data.generic;
  13941. case STACK_TYPE_NULL:
  13942. return (void*)sk->data.generic;
  13943. case STACK_TYPE_X509_NAME:
  13944. return (void*)sk->data.name;
  13945. case STACK_TYPE_X509_NAME_ENTRY:
  13946. return (void*)sk->data.name_entry;
  13947. case STACK_TYPE_CONF_VALUE:
  13948. #ifdef OPENSSL_EXTRA
  13949. return (void*)sk->data.conf;
  13950. #else
  13951. return NULL;
  13952. #endif
  13953. case STACK_TYPE_X509_INFO:
  13954. return (void*)sk->data.info;
  13955. case STACK_TYPE_BY_DIR_entry:
  13956. return (void*)sk->data.dir_entry;
  13957. case STACK_TYPE_BY_DIR_hash:
  13958. return (void*)sk->data.dir_hash;
  13959. case STACK_TYPE_X509_OBJ:
  13960. return (void*)sk->data.x509_obj;
  13961. case STACK_TYPE_DIST_POINT:
  13962. return (void*)sk->data.dp;
  13963. case STACK_TYPE_X509_CRL:
  13964. return (void*)sk->data.crl;
  13965. default:
  13966. return (void*)sk->data.generic;
  13967. }
  13968. }
  13969. /* copies over data of "in" to "out" */
  13970. static void wolfSSL_CIPHER_copy(WOLFSSL_CIPHER* in, WOLFSSL_CIPHER* out)
  13971. {
  13972. if (in == NULL || out == NULL)
  13973. return;
  13974. *out = *in;
  13975. }
  13976. WOLFSSL_STACK* wolfSSL_sk_dup(WOLFSSL_STACK* sk)
  13977. {
  13978. WOLFSSL_STACK* ret = NULL;
  13979. WOLFSSL_STACK* last = NULL;
  13980. WOLFSSL_ENTER("wolfSSL_sk_dup");
  13981. while (sk) {
  13982. WOLFSSL_STACK* cur = wolfSSL_sk_new_node(sk->heap);
  13983. if (!cur) {
  13984. WOLFSSL_MSG("wolfSSL_sk_new_node error");
  13985. goto error;
  13986. }
  13987. if (!ret) {
  13988. /* Set first node */
  13989. ret = cur;
  13990. }
  13991. if (last) {
  13992. last->next = cur;
  13993. }
  13994. XMEMCPY(cur, sk, sizeof(WOLFSSL_STACK));
  13995. /* We will allocate new memory for this */
  13996. XMEMSET(&cur->data, 0, sizeof(cur->data));
  13997. cur->next = NULL;
  13998. switch (sk->type) {
  13999. case STACK_TYPE_X509:
  14000. if (!sk->data.x509)
  14001. break;
  14002. cur->data.x509 = wolfSSL_X509_dup(sk->data.x509);
  14003. if (!cur->data.x509) {
  14004. WOLFSSL_MSG("wolfSSL_X509_dup error");
  14005. goto error;
  14006. }
  14007. break;
  14008. case STACK_TYPE_CIPHER:
  14009. wolfSSL_CIPHER_copy(&sk->data.cipher, &cur->data.cipher);
  14010. break;
  14011. case STACK_TYPE_GEN_NAME:
  14012. if (!sk->data.gn)
  14013. break;
  14014. cur->data.gn = wolfSSL_GENERAL_NAME_dup(sk->data.gn);
  14015. if (!cur->data.gn) {
  14016. WOLFSSL_MSG("wolfSSL_GENERAL_NAME_new error");
  14017. goto error;
  14018. }
  14019. break;
  14020. case STACK_TYPE_OBJ:
  14021. if (!sk->data.obj)
  14022. break;
  14023. cur->data.obj = wolfSSL_ASN1_OBJECT_dup(sk->data.obj);
  14024. if (!cur->data.obj) {
  14025. WOLFSSL_MSG("wolfSSL_ASN1_OBJECT_dup error");
  14026. goto error;
  14027. }
  14028. break;
  14029. case STACK_TYPE_BIO:
  14030. case STACK_TYPE_STRING:
  14031. case STACK_TYPE_ACCESS_DESCRIPTION:
  14032. case STACK_TYPE_X509_EXT:
  14033. case STACK_TYPE_X509_REQ_ATTR:
  14034. case STACK_TYPE_NULL:
  14035. case STACK_TYPE_X509_NAME:
  14036. case STACK_TYPE_X509_NAME_ENTRY:
  14037. case STACK_TYPE_CONF_VALUE:
  14038. case STACK_TYPE_X509_INFO:
  14039. case STACK_TYPE_BY_DIR_entry:
  14040. case STACK_TYPE_BY_DIR_hash:
  14041. case STACK_TYPE_X509_OBJ:
  14042. case STACK_TYPE_DIST_POINT:
  14043. case STACK_TYPE_X509_CRL:
  14044. default:
  14045. WOLFSSL_MSG("Unsupported stack type");
  14046. goto error;
  14047. }
  14048. sk = sk->next;
  14049. last = cur;
  14050. }
  14051. return ret;
  14052. error:
  14053. if (ret) {
  14054. wolfSSL_sk_GENERAL_NAME_free(ret);
  14055. }
  14056. return NULL;
  14057. }
  14058. WOLFSSL_STACK* wolfSSL_shallow_sk_dup(WOLFSSL_STACK* sk)
  14059. {
  14060. WOLFSSL_STACK* ret = NULL;
  14061. WOLFSSL_STACK** prev = &ret;
  14062. WOLFSSL_ENTER("wolfSSL_shallow_sk_dup");
  14063. for (; sk != NULL; sk = sk->next) {
  14064. WOLFSSL_STACK* cur = wolfSSL_sk_new_node(sk->heap);
  14065. if (!cur) {
  14066. WOLFSSL_MSG("wolfSSL_sk_new_node error");
  14067. goto error;
  14068. }
  14069. XMEMCPY(cur, sk, sizeof(WOLFSSL_STACK));
  14070. cur->next = NULL;
  14071. *prev = cur;
  14072. prev = &cur->next;
  14073. }
  14074. return ret;
  14075. error:
  14076. if (ret) {
  14077. wolfSSL_sk_free(ret);
  14078. }
  14079. return NULL;
  14080. }
  14081. /* Free the just the stack structure */
  14082. void wolfSSL_sk_free(WOLFSSL_STACK* sk)
  14083. {
  14084. WOLFSSL_ENTER("wolfSSL_sk_free");
  14085. while (sk != NULL) {
  14086. WOLFSSL_STACK* next = sk->next;
  14087. XFREE(sk, NULL, DYNAMIC_TYPE_OPENSSL);
  14088. sk = next;
  14089. }
  14090. }
  14091. /* Frees each node in the stack and frees the stack.
  14092. */
  14093. void wolfSSL_sk_GENERIC_pop_free(WOLFSSL_STACK* sk,
  14094. void (*f) (void*))
  14095. {
  14096. WOLFSSL_ENTER("wolfSSL_sk_GENERIC_pop_free");
  14097. wolfSSL_sk_pop_free(sk, (wolfSSL_sk_freefunc)f);
  14098. }
  14099. /* return 1 on success 0 on fail */
  14100. int wolfSSL_sk_GENERIC_push(WOLFSSL_STACK* sk, void* generic)
  14101. {
  14102. WOLFSSL_ENTER("wolfSSL_sk_GENERIC_push");
  14103. return wolfSSL_sk_push(sk, generic);
  14104. }
  14105. void wolfSSL_sk_GENERIC_free(WOLFSSL_STACK* sk)
  14106. {
  14107. wolfSSL_sk_free(sk);
  14108. }
  14109. /* Pop off data from the stack. Checks that the type matches the stack type.
  14110. *
  14111. * @param [in, out] sk Stack of objects.
  14112. * @param [in] type Type of stack.
  14113. * @return Object on success.
  14114. * @return NULL when stack is NULL or no nodes left in stack.
  14115. */
  14116. void* wolfssl_sk_pop_type(WOLFSSL_STACK* sk, WOLF_STACK_TYPE type)
  14117. {
  14118. WOLFSSL_STACK* node;
  14119. void* data = NULL;
  14120. /* Check we have a stack passed in of the right type. */
  14121. if ((sk != NULL) && (sk->type == type)) {
  14122. /* Get the next node to become the new first node. */
  14123. node = sk->next;
  14124. /* Get the ASN.1 OBJECT_ID object in the first node. */
  14125. data = sk->data.generic;
  14126. /* Check whether there is a next node. */
  14127. if (node != NULL) {
  14128. /* Move content out of next node into current node. */
  14129. sk->data.obj = node->data.obj;
  14130. sk->next = node->next;
  14131. /* Dispose of node. */
  14132. XFREE(node, NULL, DYNAMIC_TYPE_ASN1);
  14133. }
  14134. else {
  14135. /* No more nodes - clear out data. */
  14136. sk->data.obj = NULL;
  14137. }
  14138. /* Decrement count as long as we thought we had nodes. */
  14139. if (sk->num > 0) {
  14140. sk->num -= 1;
  14141. }
  14142. }
  14143. return data;
  14144. }
  14145. /* Free all nodes in a stack including the pushed objects */
  14146. void wolfSSL_sk_pop_free(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk,
  14147. wolfSSL_sk_freefunc func)
  14148. {
  14149. WOLFSSL_ENTER("wolfSSL_sk_pop_free");
  14150. if (sk == NULL) {
  14151. /* pop_free can be called with NULL, do not print bad argument */
  14152. return;
  14153. }
  14154. #if defined(WOLFSSL_QT)
  14155. /* In Qt v15.5, it calls OPENSSL_sk_free(xxx, OPENSSL_sk_free).
  14156. * By using OPENSSL_sk_free for free causes access violation.
  14157. * Therefore, switching free func to wolfSSL_ACCESS_DESCRIPTION_free
  14158. * is needed even the func isn't NULL.
  14159. */
  14160. if (sk->type == STACK_TYPE_ACCESS_DESCRIPTION) {
  14161. func = (wolfSSL_sk_freefunc)wolfSSL_ACCESS_DESCRIPTION_free;
  14162. }
  14163. #endif
  14164. if (func == NULL) {
  14165. switch(sk->type) {
  14166. case STACK_TYPE_ACCESS_DESCRIPTION:
  14167. #if defined(OPENSSL_ALL)
  14168. func = (wolfSSL_sk_freefunc)wolfSSL_ACCESS_DESCRIPTION_free;
  14169. #endif
  14170. break;
  14171. case STACK_TYPE_X509:
  14172. func = (wolfSSL_sk_freefunc)wolfSSL_X509_free;
  14173. break;
  14174. case STACK_TYPE_X509_OBJ:
  14175. #ifdef OPENSSL_ALL
  14176. func = (wolfSSL_sk_freefunc)wolfSSL_X509_OBJECT_free;
  14177. #endif
  14178. break;
  14179. case STACK_TYPE_OBJ:
  14180. func = (wolfSSL_sk_freefunc)wolfSSL_ASN1_OBJECT_free;
  14181. break;
  14182. case STACK_TYPE_DIST_POINT:
  14183. #ifdef OPENSSL_EXTRA
  14184. func = (wolfSSL_sk_freefunc)wolfSSL_DIST_POINT_free;
  14185. #endif
  14186. break;
  14187. case STACK_TYPE_GEN_NAME:
  14188. func = (wolfSSL_sk_freefunc)wolfSSL_GENERAL_NAME_free;
  14189. break;
  14190. case STACK_TYPE_STRING:
  14191. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  14192. defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  14193. func = (wolfSSL_sk_freefunc)wolfSSL_WOLFSSL_STRING_free;
  14194. #endif
  14195. break;
  14196. case STACK_TYPE_X509_NAME:
  14197. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) \
  14198. && !defined(WOLFCRYPT_ONLY)
  14199. func = (wolfSSL_sk_freefunc)wolfSSL_X509_NAME_free;
  14200. #endif
  14201. break;
  14202. case STACK_TYPE_X509_NAME_ENTRY:
  14203. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) \
  14204. && !defined(WOLFCRYPT_ONLY)
  14205. func = (wolfSSL_sk_freefunc)wolfSSL_X509_NAME_ENTRY_free;
  14206. #endif
  14207. break;
  14208. case STACK_TYPE_X509_EXT:
  14209. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  14210. func = (wolfSSL_sk_freefunc)wolfSSL_X509_EXTENSION_free;
  14211. #endif
  14212. break;
  14213. case STACK_TYPE_X509_REQ_ATTR:
  14214. #if defined(OPENSSL_ALL) && \
  14215. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_REQ))
  14216. func = (wolfSSL_sk_freefunc)wolfSSL_X509_ATTRIBUTE_free;
  14217. #endif
  14218. break;
  14219. case STACK_TYPE_CONF_VALUE:
  14220. #if defined(OPENSSL_ALL)
  14221. func = (wolfSSL_sk_freefunc)wolfSSL_X509V3_conf_free;
  14222. #endif
  14223. break;
  14224. case STACK_TYPE_X509_INFO:
  14225. #if defined(OPENSSL_ALL)
  14226. func = (wolfSSL_sk_freefunc)wolfSSL_X509_INFO_free;
  14227. #endif
  14228. break;
  14229. case STACK_TYPE_BIO:
  14230. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA)
  14231. func = (wolfSSL_sk_freefunc)wolfSSL_BIO_vfree;
  14232. #endif
  14233. break;
  14234. case STACK_TYPE_BY_DIR_entry:
  14235. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  14236. func = (wolfSSL_sk_freefunc)wolfSSL_BY_DIR_entry_free;
  14237. #endif
  14238. break;
  14239. case STACK_TYPE_BY_DIR_hash:
  14240. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  14241. func = (wolfSSL_sk_freefunc)wolfSSL_BY_DIR_HASH_free;
  14242. #endif
  14243. break;
  14244. case STACK_TYPE_X509_CRL:
  14245. #if defined(HAVE_CRL) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  14246. func = (wolfSSL_sk_freefunc)wolfSSL_X509_CRL_free;
  14247. #endif
  14248. break;
  14249. case STACK_TYPE_CIPHER:
  14250. case STACK_TYPE_NULL:
  14251. default:
  14252. break;
  14253. }
  14254. }
  14255. while (sk != NULL) {
  14256. WOLFSSL_STACK* next = sk->next;
  14257. if (func != NULL) {
  14258. if (sk->type != STACK_TYPE_CIPHER)
  14259. func(sk->data.generic);
  14260. }
  14261. XFREE(sk, NULL, DYNAMIC_TYPE_OPENSSL);
  14262. sk = next;
  14263. }
  14264. }
  14265. /* Creates a new stack of the requested type.
  14266. *
  14267. * @param [in] type Type of stack.
  14268. * @return Empty stack on success.
  14269. * @return NULL when dynamic memory allocation fails.
  14270. */
  14271. WOLFSSL_STACK* wolfssl_sk_new_type(WOLF_STACK_TYPE type)
  14272. {
  14273. WOLFSSL_STACK* sk;
  14274. /* Allocate a new stack - first node. */
  14275. sk = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
  14276. DYNAMIC_TYPE_OPENSSL);
  14277. if (sk == NULL) {
  14278. WOLFSSL_MSG("WOLFSSL_STACK memory error");
  14279. }
  14280. else {
  14281. /* Clear node and set type. */
  14282. XMEMSET(sk, 0, sizeof(WOLFSSL_STACK));
  14283. sk->type = type;
  14284. }
  14285. return sk;
  14286. }
  14287. /* Creates and returns a new null stack. */
  14288. WOLFSSL_STACK* wolfSSL_sk_new_null(void)
  14289. {
  14290. WOLFSSL_ENTER("wolfSSL_sk_new_null");
  14291. return wolfssl_sk_new_type(STACK_TYPE_NULL);
  14292. }
  14293. int wolfSSL_sk_SSL_COMP_num(WOLF_STACK_OF(WOLFSSL_COMP)* sk)
  14294. {
  14295. if (sk == NULL)
  14296. return 0;
  14297. return (int)sk->num;
  14298. }
  14299. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  14300. #ifdef OPENSSL_EXTRA
  14301. #if defined(HAVE_EX_DATA) && !defined(NO_FILESYSTEM)
  14302. int wolfSSL_cmp_peer_cert_to_file(WOLFSSL* ssl, const char *fname)
  14303. {
  14304. int ret = WOLFSSL_FATAL_ERROR;
  14305. WOLFSSL_ENTER("wolfSSL_cmp_peer_cert_to_file");
  14306. if (ssl != NULL && fname != NULL)
  14307. {
  14308. #ifdef WOLFSSL_SMALL_STACK
  14309. byte staticBuffer[1]; /* force heap usage */
  14310. #else
  14311. byte staticBuffer[FILE_BUFFER_SIZE];
  14312. #endif
  14313. byte* myBuffer = staticBuffer;
  14314. int dynamic = 0;
  14315. XFILE file;
  14316. long sz = 0;
  14317. WOLFSSL_CTX* ctx = ssl->ctx;
  14318. WOLFSSL_X509* peer_cert = &ssl->peerCert;
  14319. DerBuffer* fileDer = NULL;
  14320. file = XFOPEN(fname, "rb");
  14321. if (file == XBADFILE)
  14322. return WOLFSSL_BAD_FILE;
  14323. if (XFSEEK(file, 0, XSEEK_END) != 0) {
  14324. XFCLOSE(file);
  14325. return WOLFSSL_BAD_FILE;
  14326. }
  14327. sz = XFTELL(file);
  14328. if (XFSEEK(file, 0, XSEEK_SET) != 0) {
  14329. XFCLOSE(file);
  14330. return WOLFSSL_BAD_FILE;
  14331. }
  14332. if (sz > MAX_WOLFSSL_FILE_SIZE || sz < 0) {
  14333. WOLFSSL_MSG("cmp_peer_cert_to_file size error");
  14334. XFCLOSE(file);
  14335. return WOLFSSL_BAD_FILE;
  14336. }
  14337. if (sz > (long)sizeof(staticBuffer)) {
  14338. WOLFSSL_MSG("Getting dynamic buffer");
  14339. myBuffer = (byte*)XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
  14340. dynamic = 1;
  14341. }
  14342. if ((myBuffer != NULL) &&
  14343. (sz > 0) &&
  14344. (XFREAD(myBuffer, 1, sz, file) == (size_t)sz) &&
  14345. (PemToDer(myBuffer, (long)sz, CERT_TYPE,
  14346. &fileDer, ctx->heap, NULL, NULL) == 0) &&
  14347. (fileDer->length != 0) &&
  14348. (fileDer->length == peer_cert->derCert->length) &&
  14349. (XMEMCMP(peer_cert->derCert->buffer, fileDer->buffer,
  14350. fileDer->length) == 0))
  14351. {
  14352. ret = 0;
  14353. }
  14354. FreeDer(&fileDer);
  14355. if (dynamic)
  14356. XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
  14357. XFCLOSE(file);
  14358. }
  14359. return ret;
  14360. }
  14361. #endif
  14362. #endif /* OPENSSL_EXTRA */
  14363. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  14364. const WOLFSSL_ObjectInfo wolfssl_object_info[] = {
  14365. #ifndef NO_CERTS
  14366. /* oidCertExtType */
  14367. { NID_basic_constraints, BASIC_CA_OID, oidCertExtType, "basicConstraints",
  14368. "X509v3 Basic Constraints"},
  14369. { NID_subject_alt_name, ALT_NAMES_OID, oidCertExtType, "subjectAltName",
  14370. "X509v3 Subject Alternative Name"},
  14371. { NID_crl_distribution_points, CRL_DIST_OID, oidCertExtType,
  14372. "crlDistributionPoints", "X509v3 CRL Distribution Points"},
  14373. { NID_info_access, AUTH_INFO_OID, oidCertExtType, "authorityInfoAccess",
  14374. "Authority Information Access"},
  14375. { NID_authority_key_identifier, AUTH_KEY_OID, oidCertExtType,
  14376. "authorityKeyIdentifier", "X509v3 Authority Key Identifier"},
  14377. { NID_subject_key_identifier, SUBJ_KEY_OID, oidCertExtType,
  14378. "subjectKeyIdentifier", "X509v3 Subject Key Identifier"},
  14379. { NID_key_usage, KEY_USAGE_OID, oidCertExtType, "keyUsage",
  14380. "X509v3 Key Usage"},
  14381. { NID_inhibit_any_policy, INHIBIT_ANY_OID, oidCertExtType,
  14382. "inhibitAnyPolicy", "X509v3 Inhibit Any Policy"},
  14383. { NID_ext_key_usage, EXT_KEY_USAGE_OID, oidCertExtType,
  14384. "extendedKeyUsage", "X509v3 Extended Key Usage"},
  14385. { NID_name_constraints, NAME_CONS_OID, oidCertExtType,
  14386. "nameConstraints", "X509v3 Name Constraints"},
  14387. { NID_certificate_policies, CERT_POLICY_OID, oidCertExtType,
  14388. "certificatePolicies", "X509v3 Certificate Policies"},
  14389. /* oidCertAuthInfoType */
  14390. { NID_ad_OCSP, AIA_OCSP_OID, oidCertAuthInfoType, "OCSP",
  14391. "OCSP"},
  14392. { NID_ad_ca_issuers, AIA_CA_ISSUER_OID, oidCertAuthInfoType,
  14393. "caIssuers", "CA Issuers"},
  14394. /* oidCertPolicyType */
  14395. { NID_any_policy, CP_ANY_OID, oidCertPolicyType, "anyPolicy",
  14396. "X509v3 Any Policy"},
  14397. /* oidCertAltNameType */
  14398. { NID_hw_name_oid, HW_NAME_OID, oidCertAltNameType, "Hardware name",""},
  14399. /* oidCertKeyUseType */
  14400. { NID_anyExtendedKeyUsage, EKU_ANY_OID, oidCertKeyUseType,
  14401. "anyExtendedKeyUsage", "Any Extended Key Usage"},
  14402. { EKU_SERVER_AUTH_OID, EKU_SERVER_AUTH_OID, oidCertKeyUseType,
  14403. "serverAuth", "TLS Web Server Authentication"},
  14404. { EKU_CLIENT_AUTH_OID, EKU_CLIENT_AUTH_OID, oidCertKeyUseType,
  14405. "clientAuth", "TLS Web Client Authentication"},
  14406. { EKU_OCSP_SIGN_OID, EKU_OCSP_SIGN_OID, oidCertKeyUseType,
  14407. "OCSPSigning", "OCSP Signing"},
  14408. /* oidCertNameType */
  14409. { NID_commonName, NID_commonName, oidCertNameType, "CN", "commonName"},
  14410. #if !defined(WOLFSSL_CERT_REQ)
  14411. { NID_surname, NID_surname, oidCertNameType, "SN", "surname"},
  14412. #endif
  14413. { NID_serialNumber, NID_serialNumber, oidCertNameType, "serialNumber",
  14414. "serialNumber"},
  14415. { NID_userId, NID_userId, oidCertNameType, "UID", "userid"},
  14416. { NID_countryName, NID_countryName, oidCertNameType, "C", "countryName"},
  14417. { NID_localityName, NID_localityName, oidCertNameType, "L", "localityName"},
  14418. { NID_stateOrProvinceName, NID_stateOrProvinceName, oidCertNameType, "ST",
  14419. "stateOrProvinceName"},
  14420. { NID_streetAddress, NID_streetAddress, oidCertNameType, "street",
  14421. "streetAddress"},
  14422. { NID_organizationName, NID_organizationName, oidCertNameType, "O",
  14423. "organizationName"},
  14424. { NID_organizationalUnitName, NID_organizationalUnitName, oidCertNameType,
  14425. "OU", "organizationalUnitName"},
  14426. { NID_emailAddress, NID_emailAddress, oidCertNameType, "emailAddress",
  14427. "emailAddress"},
  14428. { NID_domainComponent, NID_domainComponent, oidCertNameType, "DC",
  14429. "domainComponent"},
  14430. { NID_favouriteDrink, NID_favouriteDrink, oidCertNameType, "favouriteDrink",
  14431. "favouriteDrink"},
  14432. { NID_businessCategory, NID_businessCategory, oidCertNameType,
  14433. "businessCategory", "businessCategory"},
  14434. { NID_jurisdictionCountryName, NID_jurisdictionCountryName, oidCertNameType,
  14435. "jurisdictionC", "jurisdictionCountryName"},
  14436. { NID_jurisdictionStateOrProvinceName, NID_jurisdictionStateOrProvinceName,
  14437. oidCertNameType, "jurisdictionST", "jurisdictionStateOrProvinceName"},
  14438. { NID_postalCode, NID_postalCode, oidCertNameType, "postalCode",
  14439. "postalCode"},
  14440. { NID_userId, NID_userId, oidCertNameType, "UID", "userId"},
  14441. #if defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_NAME_ALL)
  14442. { NID_pkcs9_challengePassword, CHALLENGE_PASSWORD_OID,
  14443. oidCsrAttrType, "challengePassword", "challengePassword"},
  14444. { NID_pkcs9_contentType, PKCS9_CONTENT_TYPE_OID,
  14445. oidCsrAttrType, "contentType", "contentType" },
  14446. { NID_pkcs9_unstructuredName, UNSTRUCTURED_NAME_OID,
  14447. oidCsrAttrType, "unstructuredName", "unstructuredName" },
  14448. { NID_name, NAME_OID, oidCsrAttrType, "name", "name" },
  14449. { NID_surname, SURNAME_OID,
  14450. oidCsrAttrType, "surname", "surname" },
  14451. { NID_givenName, GIVEN_NAME_OID,
  14452. oidCsrAttrType, "givenName", "givenName" },
  14453. { NID_initials, INITIALS_OID,
  14454. oidCsrAttrType, "initials", "initials" },
  14455. { NID_dnQualifier, DNQUALIFIER_OID,
  14456. oidCsrAttrType, "dnQualifer", "dnQualifier" },
  14457. #endif
  14458. #endif
  14459. #ifdef OPENSSL_EXTRA /* OPENSSL_EXTRA_X509_SMALL only needs the above */
  14460. /* oidHashType */
  14461. #ifdef WOLFSSL_MD2
  14462. { NID_md2, MD2h, oidHashType, "MD2", "md2"},
  14463. #endif
  14464. #ifdef WOLFSSL_MD5
  14465. { NID_md5, MD5h, oidHashType, "MD5", "md5"},
  14466. #endif
  14467. #ifndef NO_SHA
  14468. { NID_sha1, SHAh, oidHashType, "SHA1", "sha1"},
  14469. #endif
  14470. #ifdef WOLFSSL_SHA224
  14471. { NID_sha224, SHA224h, oidHashType, "SHA224", "sha224"},
  14472. #endif
  14473. #ifndef NO_SHA256
  14474. { NID_sha256, SHA256h, oidHashType, "SHA256", "sha256"},
  14475. #endif
  14476. #ifdef WOLFSSL_SHA384
  14477. { NID_sha384, SHA384h, oidHashType, "SHA384", "sha384"},
  14478. #endif
  14479. #ifdef WOLFSSL_SHA512
  14480. { NID_sha512, SHA512h, oidHashType, "SHA512", "sha512"},
  14481. #endif
  14482. #ifdef WOLFSSL_SHA3
  14483. #ifndef WOLFSSL_NOSHA3_224
  14484. { NID_sha3_224, SHA3_224h, oidHashType, "SHA3-224", "sha3-224"},
  14485. #endif
  14486. #ifndef WOLFSSL_NOSHA3_256
  14487. { NID_sha3_256, SHA3_256h, oidHashType, "SHA3-256", "sha3-256"},
  14488. #endif
  14489. #ifndef WOLFSSL_NOSHA3_384
  14490. { NID_sha3_384, SHA3_384h, oidHashType, "SHA3-384", "sha3-384"},
  14491. #endif
  14492. #ifndef WOLFSSL_NOSHA3_512
  14493. { NID_sha3_512, SHA3_512h, oidHashType, "SHA3-512", "sha3-512"},
  14494. #endif
  14495. #endif /* WOLFSSL_SHA3 */
  14496. #ifdef WOLFSSL_SM3
  14497. { NID_sm3, SM3h, oidHashType, "SM3", "sm3"},
  14498. #endif
  14499. /* oidSigType */
  14500. #ifndef NO_DSA
  14501. #ifndef NO_SHA
  14502. { NID_dsaWithSHA1, CTC_SHAwDSA, oidSigType, "DSA-SHA1", "dsaWithSHA1"},
  14503. { NID_dsa_with_SHA256, CTC_SHA256wDSA, oidSigType, "dsa_with_SHA256",
  14504. "dsa_with_SHA256"},
  14505. #endif
  14506. #endif /* NO_DSA */
  14507. #ifndef NO_RSA
  14508. #ifdef WOLFSSL_MD2
  14509. { NID_md2WithRSAEncryption, CTC_MD2wRSA, oidSigType, "RSA-MD2",
  14510. "md2WithRSAEncryption"},
  14511. #endif
  14512. #ifndef NO_MD5
  14513. { NID_md5WithRSAEncryption, CTC_MD5wRSA, oidSigType, "RSA-MD5",
  14514. "md5WithRSAEncryption"},
  14515. #endif
  14516. #ifndef NO_SHA
  14517. { NID_sha1WithRSAEncryption, CTC_SHAwRSA, oidSigType, "RSA-SHA1",
  14518. "sha1WithRSAEncryption"},
  14519. #endif
  14520. #ifdef WOLFSSL_SHA224
  14521. { NID_sha224WithRSAEncryption, CTC_SHA224wRSA, oidSigType, "RSA-SHA224",
  14522. "sha224WithRSAEncryption"},
  14523. #endif
  14524. #ifndef NO_SHA256
  14525. { NID_sha256WithRSAEncryption, CTC_SHA256wRSA, oidSigType, "RSA-SHA256",
  14526. "sha256WithRSAEncryption"},
  14527. #endif
  14528. #ifdef WOLFSSL_SHA384
  14529. { NID_sha384WithRSAEncryption, CTC_SHA384wRSA, oidSigType, "RSA-SHA384",
  14530. "sha384WithRSAEncryption"},
  14531. #endif
  14532. #ifdef WOLFSSL_SHA512
  14533. { NID_sha512WithRSAEncryption, CTC_SHA512wRSA, oidSigType, "RSA-SHA512",
  14534. "sha512WithRSAEncryption"},
  14535. #endif
  14536. #ifdef WOLFSSL_SHA3
  14537. #ifndef WOLFSSL_NOSHA3_224
  14538. { NID_RSA_SHA3_224, CTC_SHA3_224wRSA, oidSigType, "RSA-SHA3-224",
  14539. "sha3-224WithRSAEncryption"},
  14540. #endif
  14541. #ifndef WOLFSSL_NOSHA3_256
  14542. { NID_RSA_SHA3_256, CTC_SHA3_256wRSA, oidSigType, "RSA-SHA3-256",
  14543. "sha3-256WithRSAEncryption"},
  14544. #endif
  14545. #ifndef WOLFSSL_NOSHA3_384
  14546. { NID_RSA_SHA3_384, CTC_SHA3_384wRSA, oidSigType, "RSA-SHA3-384",
  14547. "sha3-384WithRSAEncryption"},
  14548. #endif
  14549. #ifndef WOLFSSL_NOSHA3_512
  14550. { NID_RSA_SHA3_512, CTC_SHA3_512wRSA, oidSigType, "RSA-SHA3-512",
  14551. "sha3-512WithRSAEncryption"},
  14552. #endif
  14553. #endif
  14554. #ifdef WC_RSA_PSS
  14555. { NID_rsassaPss, CTC_RSASSAPSS, oidSigType, "RSASSA-PSS", "rsassaPss" },
  14556. #endif
  14557. #endif /* NO_RSA */
  14558. #ifdef HAVE_ECC
  14559. #ifndef NO_SHA
  14560. { NID_ecdsa_with_SHA1, CTC_SHAwECDSA, oidSigType, "ecdsa-with-SHA1",
  14561. "shaWithECDSA"},
  14562. #endif
  14563. #ifdef WOLFSSL_SHA224
  14564. { NID_ecdsa_with_SHA224, CTC_SHA224wECDSA, oidSigType,
  14565. "ecdsa-with-SHA224","sha224WithECDSA"},
  14566. #endif
  14567. #ifndef NO_SHA256
  14568. { NID_ecdsa_with_SHA256, CTC_SHA256wECDSA, oidSigType,
  14569. "ecdsa-with-SHA256","sha256WithECDSA"},
  14570. #endif
  14571. #ifdef WOLFSSL_SHA384
  14572. { NID_ecdsa_with_SHA384, CTC_SHA384wECDSA, oidSigType,
  14573. "ecdsa-with-SHA384","sha384WithECDSA"},
  14574. #endif
  14575. #ifdef WOLFSSL_SHA512
  14576. { NID_ecdsa_with_SHA512, CTC_SHA512wECDSA, oidSigType,
  14577. "ecdsa-with-SHA512","sha512WithECDSA"},
  14578. #endif
  14579. #ifdef WOLFSSL_SHA3
  14580. #ifndef WOLFSSL_NOSHA3_224
  14581. { NID_ecdsa_with_SHA3_224, CTC_SHA3_224wECDSA, oidSigType,
  14582. "id-ecdsa-with-SHA3-224", "ecdsa_with_SHA3-224"},
  14583. #endif
  14584. #ifndef WOLFSSL_NOSHA3_256
  14585. { NID_ecdsa_with_SHA3_256, CTC_SHA3_256wECDSA, oidSigType,
  14586. "id-ecdsa-with-SHA3-256", "ecdsa_with_SHA3-256"},
  14587. #endif
  14588. #ifndef WOLFSSL_NOSHA3_384
  14589. { NID_ecdsa_with_SHA3_384, CTC_SHA3_384wECDSA, oidSigType,
  14590. "id-ecdsa-with-SHA3-384", "ecdsa_with_SHA3-384"},
  14591. #endif
  14592. #ifndef WOLFSSL_NOSHA3_512
  14593. { NID_ecdsa_with_SHA3_512, CTC_SHA3_512wECDSA, oidSigType,
  14594. "id-ecdsa-with-SHA3-512", "ecdsa_with_SHA3-512"},
  14595. #endif
  14596. #endif
  14597. #endif /* HAVE_ECC */
  14598. /* oidKeyType */
  14599. #ifndef NO_DSA
  14600. { NID_dsa, DSAk, oidKeyType, "DSA", "dsaEncryption"},
  14601. #endif /* NO_DSA */
  14602. #ifndef NO_RSA
  14603. { NID_rsaEncryption, RSAk, oidKeyType, "rsaEncryption",
  14604. "rsaEncryption"},
  14605. #ifdef WC_RSA_PSS
  14606. { NID_rsassaPss, RSAPSSk, oidKeyType, "RSASSA-PSS", "rsassaPss"},
  14607. #endif
  14608. #endif /* NO_RSA */
  14609. #ifdef HAVE_ECC
  14610. { NID_X9_62_id_ecPublicKey, ECDSAk, oidKeyType, "id-ecPublicKey",
  14611. "id-ecPublicKey"},
  14612. #endif /* HAVE_ECC */
  14613. #ifndef NO_DH
  14614. { NID_dhKeyAgreement, DHk, oidKeyType, "dhKeyAgreement",
  14615. "dhKeyAgreement"},
  14616. #endif
  14617. #ifdef HAVE_ED448
  14618. { NID_ED448, ED448k, oidKeyType, "ED448", "ED448"},
  14619. #endif
  14620. #ifdef HAVE_ED25519
  14621. { NID_ED25519, ED25519k, oidKeyType, "ED25519", "ED25519"},
  14622. #endif
  14623. #ifdef HAVE_PQC
  14624. #ifdef HAVE_FALCON
  14625. { CTC_FALCON_LEVEL1, FALCON_LEVEL1k, oidKeyType, "Falcon Level 1",
  14626. "Falcon Level 1"},
  14627. { CTC_FALCON_LEVEL5, FALCON_LEVEL5k, oidKeyType, "Falcon Level 5",
  14628. "Falcon Level 5"},
  14629. #endif /* HAVE_FALCON */
  14630. #ifdef HAVE_DILITHIUM
  14631. { CTC_DILITHIUM_LEVEL2, DILITHIUM_LEVEL2k, oidKeyType,
  14632. "Dilithium Level 2", "Dilithium Level 2"},
  14633. { CTC_DILITHIUM_LEVEL3, DILITHIUM_LEVEL3k, oidKeyType,
  14634. "Dilithium Level 3", "Dilithium Level 3"},
  14635. { CTC_DILITHIUM_LEVEL5, DILITHIUM_LEVEL5k, oidKeyType,
  14636. "Dilithium Level 5", "Dilithium Level 5"},
  14637. #endif /* HAVE_DILITHIUM */
  14638. #endif /* HAVE_PQC */
  14639. /* oidCurveType */
  14640. #ifdef HAVE_ECC
  14641. { NID_X9_62_prime192v1, ECC_SECP192R1_OID, oidCurveType, "prime192v1",
  14642. "prime192v1"},
  14643. { NID_X9_62_prime192v2, ECC_PRIME192V2_OID, oidCurveType, "prime192v2",
  14644. "prime192v2"},
  14645. { NID_X9_62_prime192v3, ECC_PRIME192V3_OID, oidCurveType, "prime192v3",
  14646. "prime192v3"},
  14647. { NID_X9_62_prime239v1, ECC_PRIME239V1_OID, oidCurveType, "prime239v1",
  14648. "prime239v1"},
  14649. { NID_X9_62_prime239v2, ECC_PRIME239V2_OID, oidCurveType, "prime239v2",
  14650. "prime239v2"},
  14651. { NID_X9_62_prime239v3, ECC_PRIME239V3_OID, oidCurveType, "prime239v3",
  14652. "prime239v3"},
  14653. { NID_X9_62_prime256v1, ECC_SECP256R1_OID, oidCurveType, "prime256v1",
  14654. "prime256v1"},
  14655. { NID_secp112r1, ECC_SECP112R1_OID, oidCurveType, "secp112r1",
  14656. "secp112r1"},
  14657. { NID_secp112r2, ECC_SECP112R2_OID, oidCurveType, "secp112r2",
  14658. "secp112r2"},
  14659. { NID_secp128r1, ECC_SECP128R1_OID, oidCurveType, "secp128r1",
  14660. "secp128r1"},
  14661. { NID_secp128r2, ECC_SECP128R2_OID, oidCurveType, "secp128r2",
  14662. "secp128r2"},
  14663. { NID_secp160r1, ECC_SECP160R1_OID, oidCurveType, "secp160r1",
  14664. "secp160r1"},
  14665. { NID_secp160r2, ECC_SECP160R2_OID, oidCurveType, "secp160r2",
  14666. "secp160r2"},
  14667. { NID_secp224r1, ECC_SECP224R1_OID, oidCurveType, "secp224r1",
  14668. "secp224r1"},
  14669. { NID_secp384r1, ECC_SECP384R1_OID, oidCurveType, "secp384r1",
  14670. "secp384r1"},
  14671. { NID_secp521r1, ECC_SECP521R1_OID, oidCurveType, "secp521r1",
  14672. "secp521r1"},
  14673. { NID_secp160k1, ECC_SECP160K1_OID, oidCurveType, "secp160k1",
  14674. "secp160k1"},
  14675. { NID_secp192k1, ECC_SECP192K1_OID, oidCurveType, "secp192k1",
  14676. "secp192k1"},
  14677. { NID_secp224k1, ECC_SECP224K1_OID, oidCurveType, "secp224k1",
  14678. "secp224k1"},
  14679. { NID_secp256k1, ECC_SECP256K1_OID, oidCurveType, "secp256k1",
  14680. "secp256k1"},
  14681. { NID_brainpoolP160r1, ECC_BRAINPOOLP160R1_OID, oidCurveType,
  14682. "brainpoolP160r1", "brainpoolP160r1"},
  14683. { NID_brainpoolP192r1, ECC_BRAINPOOLP192R1_OID, oidCurveType,
  14684. "brainpoolP192r1", "brainpoolP192r1"},
  14685. { NID_brainpoolP224r1, ECC_BRAINPOOLP224R1_OID, oidCurveType,
  14686. "brainpoolP224r1", "brainpoolP224r1"},
  14687. { NID_brainpoolP256r1, ECC_BRAINPOOLP256R1_OID, oidCurveType,
  14688. "brainpoolP256r1", "brainpoolP256r1"},
  14689. { NID_brainpoolP320r1, ECC_BRAINPOOLP320R1_OID, oidCurveType,
  14690. "brainpoolP320r1", "brainpoolP320r1"},
  14691. { NID_brainpoolP384r1, ECC_BRAINPOOLP384R1_OID, oidCurveType,
  14692. "brainpoolP384r1", "brainpoolP384r1"},
  14693. { NID_brainpoolP512r1, ECC_BRAINPOOLP512R1_OID, oidCurveType,
  14694. "brainpoolP512r1", "brainpoolP512r1"},
  14695. #ifdef WOLFSSL_SM2
  14696. { NID_sm2, ECC_SM2P256V1_OID, oidCurveType, "sm2", "sm2"},
  14697. #endif
  14698. #endif /* HAVE_ECC */
  14699. /* oidBlkType */
  14700. #ifdef WOLFSSL_AES_128
  14701. { AES128CBCb, AES128CBCb, oidBlkType, "AES-128-CBC", "aes-128-cbc"},
  14702. #endif
  14703. #ifdef WOLFSSL_AES_192
  14704. { AES192CBCb, AES192CBCb, oidBlkType, "AES-192-CBC", "aes-192-cbc"},
  14705. #endif
  14706. #ifdef WOLFSSL_AES_256
  14707. { AES256CBCb, AES256CBCb, oidBlkType, "AES-256-CBC", "aes-256-cbc"},
  14708. #endif
  14709. #ifndef NO_DES3
  14710. { NID_des, DESb, oidBlkType, "DES-CBC", "des-cbc"},
  14711. { NID_des3, DES3b, oidBlkType, "DES-EDE3-CBC", "des-ede3-cbc"},
  14712. #endif /* !NO_DES3 */
  14713. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14714. { NID_chacha20_poly1305, NID_chacha20_poly1305, oidBlkType,
  14715. "ChaCha20-Poly1305", "chacha20-poly1305"},
  14716. #endif
  14717. /* oidOcspType */
  14718. #ifdef HAVE_OCSP
  14719. { NID_id_pkix_OCSP_basic, OCSP_BASIC_OID, oidOcspType,
  14720. "basicOCSPResponse", "Basic OCSP Response"},
  14721. { OCSP_NONCE_OID, OCSP_NONCE_OID, oidOcspType, "Nonce", "OCSP Nonce"},
  14722. #endif /* HAVE_OCSP */
  14723. #ifndef NO_PWDBASED
  14724. /* oidKdfType */
  14725. { PBKDF2_OID, PBKDF2_OID, oidKdfType, "PBKDFv2", "PBKDF2"},
  14726. /* oidPBEType */
  14727. { PBE_SHA1_RC4_128, PBE_SHA1_RC4_128, oidPBEType,
  14728. "PBE-SHA1-RC4-128", "pbeWithSHA1And128BitRC4"},
  14729. { PBE_SHA1_DES, PBE_SHA1_DES, oidPBEType, "PBE-SHA1-DES",
  14730. "pbeWithSHA1AndDES-CBC"},
  14731. { PBE_SHA1_DES3, PBE_SHA1_DES3, oidPBEType, "PBE-SHA1-3DES",
  14732. "pbeWithSHA1And3-KeyTripleDES-CBC"},
  14733. #endif
  14734. /* oidKeyWrapType */
  14735. #ifdef WOLFSSL_AES_128
  14736. { AES128_WRAP, AES128_WRAP, oidKeyWrapType, "AES-128 wrap",
  14737. "aes128-wrap"},
  14738. #endif
  14739. #ifdef WOLFSSL_AES_192
  14740. { AES192_WRAP, AES192_WRAP, oidKeyWrapType, "AES-192 wrap",
  14741. "aes192-wrap"},
  14742. #endif
  14743. #ifdef WOLFSSL_AES_256
  14744. { AES256_WRAP, AES256_WRAP, oidKeyWrapType, "AES-256 wrap",
  14745. "aes256-wrap"},
  14746. #endif
  14747. #ifndef NO_PKCS7
  14748. #ifndef NO_DH
  14749. /* oidCmsKeyAgreeType */
  14750. #ifndef NO_SHA
  14751. { dhSinglePass_stdDH_sha1kdf_scheme, dhSinglePass_stdDH_sha1kdf_scheme,
  14752. oidCmsKeyAgreeType, "dhSinglePass-stdDH-sha1kdf-scheme",
  14753. "dhSinglePass-stdDH-sha1kdf-scheme"},
  14754. #endif
  14755. #ifdef WOLFSSL_SHA224
  14756. { dhSinglePass_stdDH_sha224kdf_scheme,
  14757. dhSinglePass_stdDH_sha224kdf_scheme, oidCmsKeyAgreeType,
  14758. "dhSinglePass-stdDH-sha224kdf-scheme",
  14759. "dhSinglePass-stdDH-sha224kdf-scheme"},
  14760. #endif
  14761. #ifndef NO_SHA256
  14762. { dhSinglePass_stdDH_sha256kdf_scheme,
  14763. dhSinglePass_stdDH_sha256kdf_scheme, oidCmsKeyAgreeType,
  14764. "dhSinglePass-stdDH-sha256kdf-scheme",
  14765. "dhSinglePass-stdDH-sha256kdf-scheme"},
  14766. #endif
  14767. #ifdef WOLFSSL_SHA384
  14768. { dhSinglePass_stdDH_sha384kdf_scheme,
  14769. dhSinglePass_stdDH_sha384kdf_scheme, oidCmsKeyAgreeType,
  14770. "dhSinglePass-stdDH-sha384kdf-scheme",
  14771. "dhSinglePass-stdDH-sha384kdf-scheme"},
  14772. #endif
  14773. #ifdef WOLFSSL_SHA512
  14774. { dhSinglePass_stdDH_sha512kdf_scheme,
  14775. dhSinglePass_stdDH_sha512kdf_scheme, oidCmsKeyAgreeType,
  14776. "dhSinglePass-stdDH-sha512kdf-scheme",
  14777. "dhSinglePass-stdDH-sha512kdf-scheme"},
  14778. #endif
  14779. #endif
  14780. #endif
  14781. #if defined(WOLFSSL_APACHE_HTTPD)
  14782. /* "1.3.6.1.5.5.7.8.7" */
  14783. { NID_id_on_dnsSRV, NID_id_on_dnsSRV, oidCertNameType,
  14784. WOLFSSL_SN_DNS_SRV, WOLFSSL_LN_DNS_SRV },
  14785. /* "1.3.6.1.4.1.311.20.2.3" */
  14786. { NID_ms_upn, WOLFSSL_MS_UPN_SUM, oidCertExtType, WOLFSSL_SN_MS_UPN,
  14787. WOLFSSL_LN_MS_UPN },
  14788. /* "1.3.6.1.5.5.7.1.24" */
  14789. { NID_tlsfeature, WOLFSSL_TLS_FEATURE_SUM, oidTlsExtType,
  14790. WOLFSSL_SN_TLS_FEATURE, WOLFSSL_LN_TLS_FEATURE },
  14791. #endif
  14792. #endif /* OPENSSL_EXTRA */
  14793. };
  14794. #define WOLFSSL_OBJECT_INFO_SZ \
  14795. (sizeof(wolfssl_object_info) / sizeof(*wolfssl_object_info))
  14796. const size_t wolfssl_object_info_sz = WOLFSSL_OBJECT_INFO_SZ;
  14797. #endif
  14798. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  14799. /* Free the dynamically allocated data.
  14800. *
  14801. * p Pointer to dynamically allocated memory.
  14802. */
  14803. void wolfSSL_OPENSSL_free(void* p)
  14804. {
  14805. WOLFSSL_MSG("wolfSSL_OPENSSL_free");
  14806. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  14807. }
  14808. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  14809. #ifdef OPENSSL_EXTRA
  14810. void *wolfSSL_OPENSSL_malloc(size_t a)
  14811. {
  14812. return (void *)XMALLOC(a, NULL, DYNAMIC_TYPE_OPENSSL);
  14813. }
  14814. int wolfSSL_OPENSSL_hexchar2int(unsigned char c)
  14815. {
  14816. /* 'char' is unsigned on some platforms. */
  14817. return (int)(signed char)HexCharToByte((char)c);
  14818. }
  14819. unsigned char *wolfSSL_OPENSSL_hexstr2buf(const char *str, long *len)
  14820. {
  14821. unsigned char* targetBuf;
  14822. int srcDigitHigh = 0;
  14823. int srcDigitLow = 0;
  14824. size_t srcLen;
  14825. size_t srcIdx = 0;
  14826. long targetIdx = 0;
  14827. srcLen = XSTRLEN(str);
  14828. targetBuf = (unsigned char*)XMALLOC(srcLen / 2, NULL, DYNAMIC_TYPE_OPENSSL);
  14829. if (targetBuf == NULL) {
  14830. return NULL;
  14831. }
  14832. while (srcIdx < srcLen) {
  14833. if (str[srcIdx] == ':') {
  14834. srcIdx++;
  14835. continue;
  14836. }
  14837. srcDigitHigh = wolfSSL_OPENSSL_hexchar2int(str[srcIdx++]);
  14838. srcDigitLow = wolfSSL_OPENSSL_hexchar2int(str[srcIdx++]);
  14839. if (srcDigitHigh < 0 || srcDigitLow < 0) {
  14840. WOLFSSL_MSG("Invalid hex character.");
  14841. XFREE(targetBuf, NULL, DYNAMIC_TYPE_OPENSSL);
  14842. return NULL;
  14843. }
  14844. targetBuf[targetIdx++] = (unsigned char)((srcDigitHigh << 4) |
  14845. srcDigitLow );
  14846. }
  14847. if (len != NULL)
  14848. *len = targetIdx;
  14849. return targetBuf;
  14850. }
  14851. int wolfSSL_OPENSSL_init_ssl(word64 opts, const OPENSSL_INIT_SETTINGS *settings)
  14852. {
  14853. (void)opts;
  14854. (void)settings;
  14855. return wolfSSL_library_init();
  14856. }
  14857. int wolfSSL_OPENSSL_init_crypto(word64 opts,
  14858. const OPENSSL_INIT_SETTINGS* settings)
  14859. {
  14860. (void)opts;
  14861. (void)settings;
  14862. return wolfSSL_library_init();
  14863. }
  14864. /* Colon separated list of <public key>+<digest> algorithms.
  14865. * Replaces list in context.
  14866. */
  14867. int wolfSSL_CTX_set1_sigalgs_list(WOLFSSL_CTX* ctx, const char* list)
  14868. {
  14869. WOLFSSL_MSG("wolfSSL_CTX_set1_sigalg_list");
  14870. if (ctx == NULL || list == NULL) {
  14871. WOLFSSL_MSG("Bad function arguments");
  14872. return WOLFSSL_FAILURE;
  14873. }
  14874. if (AllocateCtxSuites(ctx) != 0)
  14875. return WOLFSSL_FAILURE;
  14876. return SetSuitesHashSigAlgo(ctx->suites, list);
  14877. }
  14878. /* Colon separated list of <public key>+<digest> algorithms.
  14879. * Replaces list in SSL.
  14880. */
  14881. int wolfSSL_set1_sigalgs_list(WOLFSSL* ssl, const char* list)
  14882. {
  14883. WOLFSSL_MSG("wolfSSL_set1_sigalg_list");
  14884. if (ssl == NULL || list == NULL) {
  14885. WOLFSSL_MSG("Bad function arguments");
  14886. return WOLFSSL_FAILURE;
  14887. }
  14888. if (AllocateSuites(ssl) != 0)
  14889. return WOLFSSL_FAILURE;
  14890. return SetSuitesHashSigAlgo(ssl->suites, list);
  14891. }
  14892. static int HashToNid(byte hashAlgo, int* nid)
  14893. {
  14894. int ret = WOLFSSL_SUCCESS;
  14895. /* Cast for compiler to check everything is implemented */
  14896. switch ((enum wc_MACAlgorithm)hashAlgo) {
  14897. case no_mac:
  14898. case rmd_mac:
  14899. *nid = NID_undef;
  14900. break;
  14901. case md5_mac:
  14902. *nid = NID_md5;
  14903. break;
  14904. case sha_mac:
  14905. *nid = NID_sha1;
  14906. break;
  14907. case sha224_mac:
  14908. *nid = NID_sha224;
  14909. break;
  14910. case sha256_mac:
  14911. *nid = NID_sha256;
  14912. break;
  14913. case sha384_mac:
  14914. *nid = NID_sha384;
  14915. break;
  14916. case sha512_mac:
  14917. *nid = NID_sha512;
  14918. break;
  14919. case blake2b_mac:
  14920. *nid = NID_blake2b512;
  14921. break;
  14922. case sm3_mac:
  14923. *nid = NID_sm3;
  14924. break;
  14925. default:
  14926. ret = WOLFSSL_FAILURE;
  14927. break;
  14928. }
  14929. return ret;
  14930. }
  14931. static int SaToNid(byte sa, int* nid)
  14932. {
  14933. int ret = WOLFSSL_SUCCESS;
  14934. /* Cast for compiler to check everything is implemented */
  14935. switch ((enum SignatureAlgorithm)sa) {
  14936. case anonymous_sa_algo:
  14937. *nid = NID_undef;
  14938. break;
  14939. case rsa_sa_algo:
  14940. *nid = NID_rsaEncryption;
  14941. break;
  14942. case dsa_sa_algo:
  14943. *nid = NID_dsa;
  14944. break;
  14945. case ecc_dsa_sa_algo:
  14946. *nid = NID_X9_62_id_ecPublicKey;
  14947. break;
  14948. case rsa_pss_sa_algo:
  14949. *nid = NID_rsassaPss;
  14950. break;
  14951. case ed25519_sa_algo:
  14952. #ifdef HAVE_ED25519
  14953. *nid = NID_ED25519;
  14954. #else
  14955. ret = WOLFSSL_FAILURE;
  14956. #endif
  14957. break;
  14958. case rsa_pss_pss_algo:
  14959. *nid = NID_rsassaPss;
  14960. break;
  14961. case ed448_sa_algo:
  14962. #ifdef HAVE_ED448
  14963. *nid = NID_ED448;
  14964. #else
  14965. ret = WOLFSSL_FAILURE;
  14966. #endif
  14967. break;
  14968. case falcon_level1_sa_algo:
  14969. *nid = CTC_FALCON_LEVEL1;
  14970. break;
  14971. case falcon_level5_sa_algo:
  14972. *nid = CTC_FALCON_LEVEL5;
  14973. break;
  14974. case dilithium_level2_sa_algo:
  14975. *nid = CTC_DILITHIUM_LEVEL2;
  14976. break;
  14977. case dilithium_level3_sa_algo:
  14978. *nid = CTC_DILITHIUM_LEVEL3;
  14979. break;
  14980. case dilithium_level5_sa_algo:
  14981. *nid = CTC_DILITHIUM_LEVEL5;
  14982. break;
  14983. case sm2_sa_algo:
  14984. *nid = NID_sm2;
  14985. break;
  14986. case invalid_sa_algo:
  14987. default:
  14988. ret = WOLFSSL_FAILURE;
  14989. break;
  14990. }
  14991. return ret;
  14992. }
  14993. /* This API returns the hash selected. */
  14994. int wolfSSL_get_signature_nid(WOLFSSL *ssl, int* nid)
  14995. {
  14996. WOLFSSL_MSG("wolfSSL_get_signature_nid");
  14997. if (ssl == NULL || nid == NULL) {
  14998. WOLFSSL_MSG("Bad function arguments");
  14999. return WOLFSSL_FAILURE;
  15000. }
  15001. return HashToNid(ssl->options.hashAlgo, nid);
  15002. }
  15003. /* This API returns the signature selected. */
  15004. int wolfSSL_get_signature_type_nid(const WOLFSSL* ssl, int* nid)
  15005. {
  15006. WOLFSSL_MSG("wolfSSL_get_signature_type_nid");
  15007. if (ssl == NULL || nid == NULL) {
  15008. WOLFSSL_MSG("Bad function arguments");
  15009. return WOLFSSL_FAILURE;
  15010. }
  15011. return SaToNid(ssl->options.sigAlgo, nid);
  15012. }
  15013. int wolfSSL_get_peer_signature_nid(WOLFSSL* ssl, int* nid)
  15014. {
  15015. WOLFSSL_MSG("wolfSSL_get_peer_signature_nid");
  15016. if (ssl == NULL || nid == NULL) {
  15017. WOLFSSL_MSG("Bad function arguments");
  15018. return WOLFSSL_FAILURE;
  15019. }
  15020. return HashToNid(ssl->options.peerHashAlgo, nid);
  15021. }
  15022. int wolfSSL_get_peer_signature_type_nid(const WOLFSSL* ssl, int* nid)
  15023. {
  15024. WOLFSSL_MSG("wolfSSL_get_peer_signature_type_nid");
  15025. if (ssl == NULL || nid == NULL) {
  15026. WOLFSSL_MSG("Bad function arguments");
  15027. return WOLFSSL_FAILURE;
  15028. }
  15029. return SaToNid(ssl->options.peerSigAlgo, nid);
  15030. }
  15031. #ifdef HAVE_ECC
  15032. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  15033. static int populate_groups(int* groups, int max_count, const char *list)
  15034. {
  15035. const char *end;
  15036. int count = 0;
  15037. const WOLF_EC_NIST_NAME* nist_name;
  15038. if (!groups || !list) {
  15039. return -1;
  15040. }
  15041. for (end = list; ; list = ++end) {
  15042. int len;
  15043. if (count > max_count) {
  15044. WOLFSSL_MSG("Too many curves in list");
  15045. return -1;
  15046. }
  15047. while (*end != ':' && *end != '\0') end++;
  15048. len = (int)(end - list); /* end points to char after end
  15049. * of curve name so no need for -1 */
  15050. if ((len < kNistCurves_MIN_NAME_LEN) ||
  15051. (len > kNistCurves_MAX_NAME_LEN)) {
  15052. WOLFSSL_MSG("Unrecognized curve name in list");
  15053. return -1;
  15054. }
  15055. for (nist_name = kNistCurves; nist_name->name != NULL; nist_name++) {
  15056. if (len == nist_name->name_len &&
  15057. XSTRNCMP(list, nist_name->name, nist_name->name_len) == 0) {
  15058. break;
  15059. }
  15060. }
  15061. if (!nist_name->name) {
  15062. WOLFSSL_MSG("Unrecognized curve name in list");
  15063. return -1;
  15064. }
  15065. groups[count++] = nist_name->nid;
  15066. if (*end == '\0') break;
  15067. }
  15068. return count;
  15069. }
  15070. int wolfSSL_CTX_set1_groups_list(WOLFSSL_CTX *ctx, const char *list)
  15071. {
  15072. int groups[WOLFSSL_MAX_GROUP_COUNT];
  15073. int count = 0;
  15074. if (!ctx || !list) {
  15075. return WOLFSSL_FAILURE;
  15076. }
  15077. if ((count = populate_groups(groups,
  15078. WOLFSSL_MAX_GROUP_COUNT, list)) == -1) {
  15079. return WOLFSSL_FAILURE;
  15080. }
  15081. return wolfSSL_CTX_set1_groups(ctx, groups, count);
  15082. }
  15083. int wolfSSL_set1_groups_list(WOLFSSL *ssl, const char *list)
  15084. {
  15085. int groups[WOLFSSL_MAX_GROUP_COUNT];
  15086. int count = 0;
  15087. if (!ssl || !list) {
  15088. return WOLFSSL_FAILURE;
  15089. }
  15090. if ((count = populate_groups(groups,
  15091. WOLFSSL_MAX_GROUP_COUNT, list)) == -1) {
  15092. return WOLFSSL_FAILURE;
  15093. }
  15094. return wolfSSL_set1_groups(ssl, groups, count);
  15095. }
  15096. #endif /* WOLFSSL_TLS13 */
  15097. #endif /* HAVE_ECC */
  15098. #endif /* OPENSSL_EXTRA */
  15099. #ifdef WOLFSSL_ALT_CERT_CHAINS
  15100. int wolfSSL_is_peer_alt_cert_chain(const WOLFSSL* ssl)
  15101. {
  15102. int isUsing = 0;
  15103. if (ssl)
  15104. isUsing = ssl->options.usingAltCertChain;
  15105. return isUsing;
  15106. }
  15107. #endif /* WOLFSSL_ALT_CERT_CHAINS */
  15108. #ifdef SESSION_CERTS
  15109. #ifdef WOLFSSL_ALT_CERT_CHAINS
  15110. /* Get peer's alternate certificate chain */
  15111. WOLFSSL_X509_CHAIN* wolfSSL_get_peer_alt_chain(WOLFSSL* ssl)
  15112. {
  15113. WOLFSSL_ENTER("wolfSSL_get_peer_alt_chain");
  15114. if (ssl)
  15115. return &ssl->session->altChain;
  15116. return 0;
  15117. }
  15118. #endif /* WOLFSSL_ALT_CERT_CHAINS */
  15119. /* Get peer's certificate chain */
  15120. WOLFSSL_X509_CHAIN* wolfSSL_get_peer_chain(WOLFSSL* ssl)
  15121. {
  15122. WOLFSSL_ENTER("wolfSSL_get_peer_chain");
  15123. if (ssl)
  15124. return &ssl->session->chain;
  15125. return 0;
  15126. }
  15127. /* Get peer's certificate chain total count */
  15128. int wolfSSL_get_chain_count(WOLFSSL_X509_CHAIN* chain)
  15129. {
  15130. WOLFSSL_ENTER("wolfSSL_get_chain_count");
  15131. if (chain)
  15132. return chain->count;
  15133. return 0;
  15134. }
  15135. /* Get peer's ASN.1 DER certificate at index (idx) length in bytes */
  15136. int wolfSSL_get_chain_length(WOLFSSL_X509_CHAIN* chain, int idx)
  15137. {
  15138. WOLFSSL_ENTER("wolfSSL_get_chain_length");
  15139. if (chain)
  15140. return chain->certs[idx].length;
  15141. return 0;
  15142. }
  15143. /* Get peer's ASN.1 DER certificate at index (idx) */
  15144. byte* wolfSSL_get_chain_cert(WOLFSSL_X509_CHAIN* chain, int idx)
  15145. {
  15146. WOLFSSL_ENTER("wolfSSL_get_chain_cert");
  15147. if (chain)
  15148. return chain->certs[idx].buffer;
  15149. return 0;
  15150. }
  15151. /* Get peer's wolfSSL X509 certificate at index (idx) */
  15152. WOLFSSL_X509* wolfSSL_get_chain_X509(WOLFSSL_X509_CHAIN* chain, int idx)
  15153. {
  15154. int ret = 0;
  15155. WOLFSSL_X509* x509 = NULL;
  15156. #ifdef WOLFSSL_SMALL_STACK
  15157. DecodedCert* cert = NULL;
  15158. #else
  15159. DecodedCert cert[1];
  15160. #endif
  15161. WOLFSSL_ENTER("wolfSSL_get_chain_X509");
  15162. if (chain != NULL) {
  15163. #ifdef WOLFSSL_SMALL_STACK
  15164. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  15165. DYNAMIC_TYPE_DCERT);
  15166. if (cert != NULL)
  15167. #endif
  15168. {
  15169. InitDecodedCert(cert, chain->certs[idx].buffer,
  15170. chain->certs[idx].length, NULL);
  15171. if ((ret = ParseCertRelative(cert, CERT_TYPE, 0, NULL)) != 0) {
  15172. WOLFSSL_MSG("Failed to parse cert");
  15173. }
  15174. else {
  15175. x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
  15176. DYNAMIC_TYPE_X509);
  15177. if (x509 == NULL) {
  15178. WOLFSSL_MSG("Failed alloc X509");
  15179. }
  15180. else {
  15181. InitX509(x509, 1, NULL);
  15182. if ((ret = CopyDecodedToX509(x509, cert)) != 0) {
  15183. WOLFSSL_MSG("Failed to copy decoded");
  15184. wolfSSL_X509_free(x509);
  15185. x509 = NULL;
  15186. }
  15187. }
  15188. }
  15189. FreeDecodedCert(cert);
  15190. #ifdef WOLFSSL_SMALL_STACK
  15191. XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
  15192. #endif
  15193. }
  15194. }
  15195. (void)ret;
  15196. return x509;
  15197. }
  15198. /* Get peer's PEM certificate at index (idx), output to buffer if inLen big
  15199. enough else return error (-1). If buffer is NULL only calculate
  15200. outLen. Output length is in *outLen WOLFSSL_SUCCESS on ok */
  15201. int wolfSSL_get_chain_cert_pem(WOLFSSL_X509_CHAIN* chain, int idx,
  15202. unsigned char* buf, int inLen, int* outLen)
  15203. {
  15204. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  15205. const char* header = NULL;
  15206. const char* footer = NULL;
  15207. int headerLen;
  15208. int footerLen;
  15209. int i;
  15210. int err;
  15211. word32 szNeeded = 0;
  15212. WOLFSSL_ENTER("wolfSSL_get_chain_cert_pem");
  15213. if (!chain || !outLen || idx < 0 || idx >= wolfSSL_get_chain_count(chain))
  15214. return BAD_FUNC_ARG;
  15215. err = wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer);
  15216. if (err != 0)
  15217. return err;
  15218. headerLen = (int)XSTRLEN(header);
  15219. footerLen = (int)XSTRLEN(footer);
  15220. /* Null output buffer return size needed in outLen */
  15221. if(!buf) {
  15222. if(Base64_Encode(chain->certs[idx].buffer, chain->certs[idx].length,
  15223. NULL, &szNeeded) != LENGTH_ONLY_E)
  15224. return WOLFSSL_FAILURE;
  15225. *outLen = szNeeded + headerLen + footerLen;
  15226. return LENGTH_ONLY_E;
  15227. }
  15228. /* don't even try if inLen too short */
  15229. if (inLen < headerLen + footerLen + chain->certs[idx].length)
  15230. return BAD_FUNC_ARG;
  15231. /* header */
  15232. if (XMEMCPY(buf, header, headerLen) == NULL)
  15233. return WOLFSSL_FATAL_ERROR;
  15234. i = headerLen;
  15235. /* body */
  15236. *outLen = inLen; /* input to Base64_Encode */
  15237. if ( (err = Base64_Encode(chain->certs[idx].buffer,
  15238. chain->certs[idx].length, buf + i, (word32*)outLen)) < 0)
  15239. return err;
  15240. i += *outLen;
  15241. /* footer */
  15242. if ( (i + footerLen) > inLen)
  15243. return BAD_FUNC_ARG;
  15244. if (XMEMCPY(buf + i, footer, footerLen) == NULL)
  15245. return WOLFSSL_FATAL_ERROR;
  15246. *outLen += headerLen + footerLen;
  15247. return WOLFSSL_SUCCESS;
  15248. #else
  15249. (void)chain;
  15250. (void)idx;
  15251. (void)buf;
  15252. (void)inLen;
  15253. (void)outLen;
  15254. return WOLFSSL_FAILURE;
  15255. #endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
  15256. }
  15257. #endif /* SESSION_CERTS */
  15258. #ifdef HAVE_FUZZER
  15259. void wolfSSL_SetFuzzerCb(WOLFSSL* ssl, CallbackFuzzer cbf, void* fCtx)
  15260. {
  15261. if (ssl) {
  15262. ssl->fuzzerCb = cbf;
  15263. ssl->fuzzerCtx = fCtx;
  15264. }
  15265. }
  15266. #endif
  15267. #ifndef NO_CERTS
  15268. #ifdef HAVE_PK_CALLBACKS
  15269. #ifdef HAVE_ECC
  15270. void wolfSSL_CTX_SetEccKeyGenCb(WOLFSSL_CTX* ctx, CallbackEccKeyGen cb)
  15271. {
  15272. if (ctx)
  15273. ctx->EccKeyGenCb = cb;
  15274. }
  15275. void wolfSSL_SetEccKeyGenCtx(WOLFSSL* ssl, void *ctx)
  15276. {
  15277. if (ssl)
  15278. ssl->EccKeyGenCtx = ctx;
  15279. }
  15280. void* wolfSSL_GetEccKeyGenCtx(WOLFSSL* ssl)
  15281. {
  15282. if (ssl)
  15283. return ssl->EccKeyGenCtx;
  15284. return NULL;
  15285. }
  15286. void wolfSSL_CTX_SetEccSignCtx(WOLFSSL_CTX* ctx, void *userCtx)
  15287. {
  15288. if (ctx)
  15289. ctx->EccSignCtx = userCtx;
  15290. }
  15291. void* wolfSSL_CTX_GetEccSignCtx(WOLFSSL_CTX* ctx)
  15292. {
  15293. if (ctx)
  15294. return ctx->EccSignCtx;
  15295. return NULL;
  15296. }
  15297. WOLFSSL_ABI
  15298. void wolfSSL_CTX_SetEccSignCb(WOLFSSL_CTX* ctx, CallbackEccSign cb)
  15299. {
  15300. if (ctx)
  15301. ctx->EccSignCb = cb;
  15302. }
  15303. void wolfSSL_SetEccSignCtx(WOLFSSL* ssl, void *ctx)
  15304. {
  15305. if (ssl)
  15306. ssl->EccSignCtx = ctx;
  15307. }
  15308. void* wolfSSL_GetEccSignCtx(WOLFSSL* ssl)
  15309. {
  15310. if (ssl)
  15311. return ssl->EccSignCtx;
  15312. return NULL;
  15313. }
  15314. void wolfSSL_CTX_SetEccVerifyCb(WOLFSSL_CTX* ctx, CallbackEccVerify cb)
  15315. {
  15316. if (ctx)
  15317. ctx->EccVerifyCb = cb;
  15318. }
  15319. void wolfSSL_SetEccVerifyCtx(WOLFSSL* ssl, void *ctx)
  15320. {
  15321. if (ssl)
  15322. ssl->EccVerifyCtx = ctx;
  15323. }
  15324. void* wolfSSL_GetEccVerifyCtx(WOLFSSL* ssl)
  15325. {
  15326. if (ssl)
  15327. return ssl->EccVerifyCtx;
  15328. return NULL;
  15329. }
  15330. void wolfSSL_CTX_SetEccSharedSecretCb(WOLFSSL_CTX* ctx,
  15331. CallbackEccSharedSecret cb)
  15332. {
  15333. if (ctx)
  15334. ctx->EccSharedSecretCb = cb;
  15335. }
  15336. void wolfSSL_SetEccSharedSecretCtx(WOLFSSL* ssl, void *ctx)
  15337. {
  15338. if (ssl)
  15339. ssl->EccSharedSecretCtx = ctx;
  15340. }
  15341. void* wolfSSL_GetEccSharedSecretCtx(WOLFSSL* ssl)
  15342. {
  15343. if (ssl)
  15344. return ssl->EccSharedSecretCtx;
  15345. return NULL;
  15346. }
  15347. #endif /* HAVE_ECC */
  15348. #ifdef HAVE_ED25519
  15349. void wolfSSL_CTX_SetEd25519SignCb(WOLFSSL_CTX* ctx, CallbackEd25519Sign cb)
  15350. {
  15351. if (ctx)
  15352. ctx->Ed25519SignCb = cb;
  15353. }
  15354. void wolfSSL_SetEd25519SignCtx(WOLFSSL* ssl, void *ctx)
  15355. {
  15356. if (ssl)
  15357. ssl->Ed25519SignCtx = ctx;
  15358. }
  15359. void* wolfSSL_GetEd25519SignCtx(WOLFSSL* ssl)
  15360. {
  15361. if (ssl)
  15362. return ssl->Ed25519SignCtx;
  15363. return NULL;
  15364. }
  15365. void wolfSSL_CTX_SetEd25519VerifyCb(WOLFSSL_CTX* ctx, CallbackEd25519Verify cb)
  15366. {
  15367. if (ctx)
  15368. ctx->Ed25519VerifyCb = cb;
  15369. }
  15370. void wolfSSL_SetEd25519VerifyCtx(WOLFSSL* ssl, void *ctx)
  15371. {
  15372. if (ssl)
  15373. ssl->Ed25519VerifyCtx = ctx;
  15374. }
  15375. void* wolfSSL_GetEd25519VerifyCtx(WOLFSSL* ssl)
  15376. {
  15377. if (ssl)
  15378. return ssl->Ed25519VerifyCtx;
  15379. return NULL;
  15380. }
  15381. #endif /* HAVE_ED25519 */
  15382. #ifdef HAVE_CURVE25519
  15383. void wolfSSL_CTX_SetX25519KeyGenCb(WOLFSSL_CTX* ctx,
  15384. CallbackX25519KeyGen cb)
  15385. {
  15386. if (ctx)
  15387. ctx->X25519KeyGenCb = cb;
  15388. }
  15389. void wolfSSL_SetX25519KeyGenCtx(WOLFSSL* ssl, void *ctx)
  15390. {
  15391. if (ssl)
  15392. ssl->X25519KeyGenCtx = ctx;
  15393. }
  15394. void* wolfSSL_GetX25519KeyGenCtx(WOLFSSL* ssl)
  15395. {
  15396. if (ssl)
  15397. return ssl->X25519KeyGenCtx;
  15398. return NULL;
  15399. }
  15400. void wolfSSL_CTX_SetX25519SharedSecretCb(WOLFSSL_CTX* ctx,
  15401. CallbackX25519SharedSecret cb)
  15402. {
  15403. if (ctx)
  15404. ctx->X25519SharedSecretCb = cb;
  15405. }
  15406. void wolfSSL_SetX25519SharedSecretCtx(WOLFSSL* ssl, void *ctx)
  15407. {
  15408. if (ssl)
  15409. ssl->X25519SharedSecretCtx = ctx;
  15410. }
  15411. void* wolfSSL_GetX25519SharedSecretCtx(WOLFSSL* ssl)
  15412. {
  15413. if (ssl)
  15414. return ssl->X25519SharedSecretCtx;
  15415. return NULL;
  15416. }
  15417. #endif /* HAVE_CURVE25519 */
  15418. #ifdef HAVE_ED448
  15419. void wolfSSL_CTX_SetEd448SignCb(WOLFSSL_CTX* ctx, CallbackEd448Sign cb)
  15420. {
  15421. if (ctx)
  15422. ctx->Ed448SignCb = cb;
  15423. }
  15424. void wolfSSL_SetEd448SignCtx(WOLFSSL* ssl, void *ctx)
  15425. {
  15426. if (ssl)
  15427. ssl->Ed448SignCtx = ctx;
  15428. }
  15429. void* wolfSSL_GetEd448SignCtx(WOLFSSL* ssl)
  15430. {
  15431. if (ssl)
  15432. return ssl->Ed448SignCtx;
  15433. return NULL;
  15434. }
  15435. void wolfSSL_CTX_SetEd448VerifyCb(WOLFSSL_CTX* ctx, CallbackEd448Verify cb)
  15436. {
  15437. if (ctx)
  15438. ctx->Ed448VerifyCb = cb;
  15439. }
  15440. void wolfSSL_SetEd448VerifyCtx(WOLFSSL* ssl, void *ctx)
  15441. {
  15442. if (ssl)
  15443. ssl->Ed448VerifyCtx = ctx;
  15444. }
  15445. void* wolfSSL_GetEd448VerifyCtx(WOLFSSL* ssl)
  15446. {
  15447. if (ssl)
  15448. return ssl->Ed448VerifyCtx;
  15449. return NULL;
  15450. }
  15451. #endif /* HAVE_ED448 */
  15452. #ifdef HAVE_CURVE448
  15453. void wolfSSL_CTX_SetX448KeyGenCb(WOLFSSL_CTX* ctx,
  15454. CallbackX448KeyGen cb)
  15455. {
  15456. if (ctx)
  15457. ctx->X448KeyGenCb = cb;
  15458. }
  15459. void wolfSSL_SetX448KeyGenCtx(WOLFSSL* ssl, void *ctx)
  15460. {
  15461. if (ssl)
  15462. ssl->X448KeyGenCtx = ctx;
  15463. }
  15464. void* wolfSSL_GetX448KeyGenCtx(WOLFSSL* ssl)
  15465. {
  15466. if (ssl)
  15467. return ssl->X448KeyGenCtx;
  15468. return NULL;
  15469. }
  15470. void wolfSSL_CTX_SetX448SharedSecretCb(WOLFSSL_CTX* ctx,
  15471. CallbackX448SharedSecret cb)
  15472. {
  15473. if (ctx)
  15474. ctx->X448SharedSecretCb = cb;
  15475. }
  15476. void wolfSSL_SetX448SharedSecretCtx(WOLFSSL* ssl, void *ctx)
  15477. {
  15478. if (ssl)
  15479. ssl->X448SharedSecretCtx = ctx;
  15480. }
  15481. void* wolfSSL_GetX448SharedSecretCtx(WOLFSSL* ssl)
  15482. {
  15483. if (ssl)
  15484. return ssl->X448SharedSecretCtx;
  15485. return NULL;
  15486. }
  15487. #endif /* HAVE_CURVE448 */
  15488. #ifndef NO_RSA
  15489. void wolfSSL_CTX_SetRsaSignCb(WOLFSSL_CTX* ctx, CallbackRsaSign cb)
  15490. {
  15491. if (ctx)
  15492. ctx->RsaSignCb = cb;
  15493. }
  15494. void wolfSSL_CTX_SetRsaSignCheckCb(WOLFSSL_CTX* ctx, CallbackRsaVerify cb)
  15495. {
  15496. if (ctx)
  15497. ctx->RsaSignCheckCb = cb;
  15498. }
  15499. void wolfSSL_SetRsaSignCtx(WOLFSSL* ssl, void *ctx)
  15500. {
  15501. if (ssl)
  15502. ssl->RsaSignCtx = ctx;
  15503. }
  15504. void* wolfSSL_GetRsaSignCtx(WOLFSSL* ssl)
  15505. {
  15506. if (ssl)
  15507. return ssl->RsaSignCtx;
  15508. return NULL;
  15509. }
  15510. void wolfSSL_CTX_SetRsaVerifyCb(WOLFSSL_CTX* ctx, CallbackRsaVerify cb)
  15511. {
  15512. if (ctx)
  15513. ctx->RsaVerifyCb = cb;
  15514. }
  15515. void wolfSSL_SetRsaVerifyCtx(WOLFSSL* ssl, void *ctx)
  15516. {
  15517. if (ssl)
  15518. ssl->RsaVerifyCtx = ctx;
  15519. }
  15520. void* wolfSSL_GetRsaVerifyCtx(WOLFSSL* ssl)
  15521. {
  15522. if (ssl)
  15523. return ssl->RsaVerifyCtx;
  15524. return NULL;
  15525. }
  15526. #ifdef WC_RSA_PSS
  15527. void wolfSSL_CTX_SetRsaPssSignCb(WOLFSSL_CTX* ctx, CallbackRsaPssSign cb)
  15528. {
  15529. if (ctx)
  15530. ctx->RsaPssSignCb = cb;
  15531. }
  15532. void wolfSSL_CTX_SetRsaPssSignCheckCb(WOLFSSL_CTX* ctx,
  15533. CallbackRsaPssVerify cb)
  15534. {
  15535. if (ctx)
  15536. ctx->RsaPssSignCheckCb = cb;
  15537. }
  15538. void wolfSSL_SetRsaPssSignCtx(WOLFSSL* ssl, void *ctx)
  15539. {
  15540. if (ssl)
  15541. ssl->RsaPssSignCtx = ctx;
  15542. }
  15543. void* wolfSSL_GetRsaPssSignCtx(WOLFSSL* ssl)
  15544. {
  15545. if (ssl)
  15546. return ssl->RsaPssSignCtx;
  15547. return NULL;
  15548. }
  15549. void wolfSSL_CTX_SetRsaPssVerifyCb(WOLFSSL_CTX* ctx, CallbackRsaPssVerify cb)
  15550. {
  15551. if (ctx)
  15552. ctx->RsaPssVerifyCb = cb;
  15553. }
  15554. void wolfSSL_SetRsaPssVerifyCtx(WOLFSSL* ssl, void *ctx)
  15555. {
  15556. if (ssl)
  15557. ssl->RsaPssVerifyCtx = ctx;
  15558. }
  15559. void* wolfSSL_GetRsaPssVerifyCtx(WOLFSSL* ssl)
  15560. {
  15561. if (ssl)
  15562. return ssl->RsaPssVerifyCtx;
  15563. return NULL;
  15564. }
  15565. #endif /* WC_RSA_PSS */
  15566. void wolfSSL_CTX_SetRsaEncCb(WOLFSSL_CTX* ctx, CallbackRsaEnc cb)
  15567. {
  15568. if (ctx)
  15569. ctx->RsaEncCb = cb;
  15570. }
  15571. void wolfSSL_SetRsaEncCtx(WOLFSSL* ssl, void *ctx)
  15572. {
  15573. if (ssl)
  15574. ssl->RsaEncCtx = ctx;
  15575. }
  15576. void* wolfSSL_GetRsaEncCtx(WOLFSSL* ssl)
  15577. {
  15578. if (ssl)
  15579. return ssl->RsaEncCtx;
  15580. return NULL;
  15581. }
  15582. void wolfSSL_CTX_SetRsaDecCb(WOLFSSL_CTX* ctx, CallbackRsaDec cb)
  15583. {
  15584. if (ctx)
  15585. ctx->RsaDecCb = cb;
  15586. }
  15587. void wolfSSL_SetRsaDecCtx(WOLFSSL* ssl, void *ctx)
  15588. {
  15589. if (ssl)
  15590. ssl->RsaDecCtx = ctx;
  15591. }
  15592. void* wolfSSL_GetRsaDecCtx(WOLFSSL* ssl)
  15593. {
  15594. if (ssl)
  15595. return ssl->RsaDecCtx;
  15596. return NULL;
  15597. }
  15598. #endif /* NO_RSA */
  15599. /* callback for premaster secret generation */
  15600. void wolfSSL_CTX_SetGenPreMasterCb(WOLFSSL_CTX* ctx, CallbackGenPreMaster cb)
  15601. {
  15602. if (ctx)
  15603. ctx->GenPreMasterCb = cb;
  15604. }
  15605. /* Set premaster secret generation callback context */
  15606. void wolfSSL_SetGenPreMasterCtx(WOLFSSL* ssl, void *ctx)
  15607. {
  15608. if (ssl)
  15609. ssl->GenPreMasterCtx = ctx;
  15610. }
  15611. /* Get premaster secret generation callback context */
  15612. void* wolfSSL_GetGenPreMasterCtx(WOLFSSL* ssl)
  15613. {
  15614. if (ssl)
  15615. return ssl->GenPreMasterCtx;
  15616. return NULL;
  15617. }
  15618. /* callback for master secret generation */
  15619. void wolfSSL_CTX_SetGenMasterSecretCb(WOLFSSL_CTX* ctx,
  15620. CallbackGenMasterSecret cb)
  15621. {
  15622. if (ctx)
  15623. ctx->GenMasterCb = cb;
  15624. }
  15625. /* Set master secret generation callback context */
  15626. void wolfSSL_SetGenMasterSecretCtx(WOLFSSL* ssl, void *ctx)
  15627. {
  15628. if (ssl)
  15629. ssl->GenMasterCtx = ctx;
  15630. }
  15631. /* Get master secret generation callback context */
  15632. void* wolfSSL_GetGenMasterSecretCtx(WOLFSSL* ssl)
  15633. {
  15634. if (ssl)
  15635. return ssl->GenMasterCtx;
  15636. return NULL;
  15637. }
  15638. /* callback for session key generation */
  15639. void wolfSSL_CTX_SetGenSessionKeyCb(WOLFSSL_CTX* ctx, CallbackGenSessionKey cb)
  15640. {
  15641. if (ctx)
  15642. ctx->GenSessionKeyCb = cb;
  15643. }
  15644. /* Set session key generation callback context */
  15645. void wolfSSL_SetGenSessionKeyCtx(WOLFSSL* ssl, void *ctx)
  15646. {
  15647. if (ssl)
  15648. ssl->GenSessionKeyCtx = ctx;
  15649. }
  15650. /* Get session key generation callback context */
  15651. void* wolfSSL_GetGenSessionKeyCtx(WOLFSSL* ssl)
  15652. {
  15653. if (ssl)
  15654. return ssl->GenSessionKeyCtx;
  15655. return NULL;
  15656. }
  15657. /* callback for setting encryption keys */
  15658. void wolfSSL_CTX_SetEncryptKeysCb(WOLFSSL_CTX* ctx, CallbackEncryptKeys cb)
  15659. {
  15660. if (ctx)
  15661. ctx->EncryptKeysCb = cb;
  15662. }
  15663. /* Set encryption keys callback context */
  15664. void wolfSSL_SetEncryptKeysCtx(WOLFSSL* ssl, void *ctx)
  15665. {
  15666. if (ssl)
  15667. ssl->EncryptKeysCtx = ctx;
  15668. }
  15669. /* Get encryption keys callback context */
  15670. void* wolfSSL_GetEncryptKeysCtx(WOLFSSL* ssl)
  15671. {
  15672. if (ssl)
  15673. return ssl->EncryptKeysCtx;
  15674. return NULL;
  15675. }
  15676. /* callback for Tls finished */
  15677. /* the callback can be used to build TLS Finished message if enabled */
  15678. void wolfSSL_CTX_SetTlsFinishedCb(WOLFSSL_CTX* ctx, CallbackTlsFinished cb)
  15679. {
  15680. if (ctx)
  15681. ctx->TlsFinishedCb = cb;
  15682. }
  15683. /* Set Tls finished callback context */
  15684. void wolfSSL_SetTlsFinishedCtx(WOLFSSL* ssl, void *ctx)
  15685. {
  15686. if (ssl)
  15687. ssl->TlsFinishedCtx = ctx;
  15688. }
  15689. /* Get Tls finished callback context */
  15690. void* wolfSSL_GetTlsFinishedCtx(WOLFSSL* ssl)
  15691. {
  15692. if (ssl)
  15693. return ssl->TlsFinishedCtx;
  15694. return NULL;
  15695. }
  15696. #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
  15697. /* callback for verify data */
  15698. void wolfSSL_CTX_SetVerifyMacCb(WOLFSSL_CTX* ctx, CallbackVerifyMac cb)
  15699. {
  15700. if (ctx)
  15701. ctx->VerifyMacCb = cb;
  15702. }
  15703. /* Set set keys callback context */
  15704. void wolfSSL_SetVerifyMacCtx(WOLFSSL* ssl, void *ctx)
  15705. {
  15706. if (ssl)
  15707. ssl->VerifyMacCtx = ctx;
  15708. }
  15709. /* Get set keys callback context */
  15710. void* wolfSSL_GetVerifyMacCtx(WOLFSSL* ssl)
  15711. {
  15712. if (ssl)
  15713. return ssl->VerifyMacCtx;
  15714. return NULL;
  15715. }
  15716. #endif /* !WOLFSSL_NO_TLS12 && !WOLFSSL_AEAD_ONLY */
  15717. void wolfSSL_CTX_SetHKDFExpandLabelCb(WOLFSSL_CTX* ctx,
  15718. CallbackHKDFExpandLabel cb)
  15719. {
  15720. if (ctx)
  15721. ctx->HKDFExpandLabelCb = cb;
  15722. }
  15723. #ifdef WOLFSSL_PUBLIC_ASN
  15724. void wolfSSL_CTX_SetProcessPeerCertCb(WOLFSSL_CTX* ctx,
  15725. CallbackProcessPeerCert cb)
  15726. {
  15727. if (ctx)
  15728. ctx->ProcessPeerCertCb = cb;
  15729. }
  15730. #endif /* WOLFSSL_PUBLIC_ASN */
  15731. void wolfSSL_CTX_SetProcessServerSigKexCb(WOLFSSL_CTX* ctx,
  15732. CallbackProcessServerSigKex cb)
  15733. {
  15734. if (ctx)
  15735. ctx->ProcessServerSigKexCb = cb;
  15736. }
  15737. void wolfSSL_CTX_SetPerformTlsRecordProcessingCb(WOLFSSL_CTX* ctx,
  15738. CallbackPerformTlsRecordProcessing cb)
  15739. {
  15740. if (ctx)
  15741. ctx->PerformTlsRecordProcessingCb = cb;
  15742. }
  15743. #endif /* HAVE_PK_CALLBACKS */
  15744. #endif /* NO_CERTS */
  15745. #if defined(HAVE_PK_CALLBACKS) && !defined(NO_DH)
  15746. void wolfSSL_CTX_SetDhGenerateKeyPair(WOLFSSL_CTX* ctx,
  15747. CallbackDhGenerateKeyPair cb) {
  15748. if (ctx)
  15749. ctx->DhGenerateKeyPairCb = cb;
  15750. }
  15751. void wolfSSL_CTX_SetDhAgreeCb(WOLFSSL_CTX* ctx, CallbackDhAgree cb)
  15752. {
  15753. if (ctx)
  15754. ctx->DhAgreeCb = cb;
  15755. }
  15756. void wolfSSL_SetDhAgreeCtx(WOLFSSL* ssl, void *ctx)
  15757. {
  15758. if (ssl)
  15759. ssl->DhAgreeCtx = ctx;
  15760. }
  15761. void* wolfSSL_GetDhAgreeCtx(WOLFSSL* ssl)
  15762. {
  15763. if (ssl)
  15764. return ssl->DhAgreeCtx;
  15765. return NULL;
  15766. }
  15767. #endif /* HAVE_PK_CALLBACKS && !NO_DH */
  15768. #if defined(HAVE_PK_CALLBACKS) && defined(HAVE_HKDF)
  15769. void wolfSSL_CTX_SetHKDFExtractCb(WOLFSSL_CTX* ctx, CallbackHKDFExtract cb)
  15770. {
  15771. if (ctx)
  15772. ctx->HkdfExtractCb = cb;
  15773. }
  15774. void wolfSSL_SetHKDFExtractCtx(WOLFSSL* ssl, void *ctx)
  15775. {
  15776. if (ssl)
  15777. ssl->HkdfExtractCtx = ctx;
  15778. }
  15779. void* wolfSSL_GetHKDFExtractCtx(WOLFSSL* ssl)
  15780. {
  15781. if (ssl)
  15782. return ssl->HkdfExtractCtx;
  15783. return NULL;
  15784. }
  15785. #endif /* HAVE_PK_CALLBACKS && HAVE_HKDF */
  15786. #ifdef WOLFSSL_HAVE_WOLFSCEP
  15787. /* Used by autoconf to see if wolfSCEP is available */
  15788. void wolfSSL_wolfSCEP(void) {}
  15789. #endif
  15790. #ifdef WOLFSSL_HAVE_CERT_SERVICE
  15791. /* Used by autoconf to see if cert service is available */
  15792. void wolfSSL_cert_service(void) {}
  15793. #endif
  15794. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  15795. !defined(WOLFCRYPT_ONLY)
  15796. /* NID variables are dependent on compatibility header files currently
  15797. *
  15798. * returns a pointer to a new WOLFSSL_ASN1_OBJECT struct on success and NULL
  15799. * on fail
  15800. */
  15801. WOLFSSL_ASN1_OBJECT* wolfSSL_OBJ_nid2obj(int id)
  15802. {
  15803. return wolfSSL_OBJ_nid2obj_ex(id, NULL);
  15804. }
  15805. WOLFSSL_LOCAL WOLFSSL_ASN1_OBJECT* wolfSSL_OBJ_nid2obj_ex(int id,
  15806. WOLFSSL_ASN1_OBJECT* arg_obj)
  15807. {
  15808. word32 oidSz = 0;
  15809. int nid = 0;
  15810. const byte* oid;
  15811. word32 type = 0;
  15812. WOLFSSL_ASN1_OBJECT* obj = arg_obj;
  15813. byte objBuf[MAX_OID_SZ + MAX_LENGTH_SZ + 1]; /* +1 for object tag */
  15814. word32 objSz = 0;
  15815. const char* sName = NULL;
  15816. int i;
  15817. #ifdef WOLFSSL_DEBUG_OPENSSL
  15818. WOLFSSL_ENTER("wolfSSL_OBJ_nid2obj");
  15819. #endif
  15820. for (i = 0; i < (int)WOLFSSL_OBJECT_INFO_SZ; i++) {
  15821. if (wolfssl_object_info[i].nid == id) {
  15822. nid = id;
  15823. id = wolfssl_object_info[i].id;
  15824. sName = wolfssl_object_info[i].sName;
  15825. type = wolfssl_object_info[i].type;
  15826. break;
  15827. }
  15828. }
  15829. if (i == (int)WOLFSSL_OBJECT_INFO_SZ) {
  15830. WOLFSSL_MSG("NID not in table");
  15831. #ifdef WOLFSSL_QT
  15832. sName = NULL;
  15833. type = id;
  15834. #else
  15835. return NULL;
  15836. #endif
  15837. }
  15838. #ifdef HAVE_ECC
  15839. if (type == 0 && wc_ecc_get_oid(id, &oid, &oidSz) > 0) {
  15840. type = oidCurveType;
  15841. }
  15842. #endif /* HAVE_ECC */
  15843. if (sName != NULL) {
  15844. if (XSTRLEN(sName) > WOLFSSL_MAX_SNAME - 1) {
  15845. WOLFSSL_MSG("Attempted short name is too large");
  15846. return NULL;
  15847. }
  15848. }
  15849. oid = OidFromId(id, type, &oidSz);
  15850. /* set object ID to buffer */
  15851. if (obj == NULL){
  15852. obj = wolfSSL_ASN1_OBJECT_new();
  15853. if (obj == NULL) {
  15854. WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
  15855. return NULL;
  15856. }
  15857. }
  15858. obj->nid = nid;
  15859. obj->type = id;
  15860. obj->grp = type;
  15861. obj->sName[0] = '\0';
  15862. if (sName != NULL) {
  15863. XMEMCPY(obj->sName, (char*)sName, XSTRLEN((char*)sName));
  15864. }
  15865. objBuf[0] = ASN_OBJECT_ID; objSz++;
  15866. objSz += SetLength(oidSz, objBuf + 1);
  15867. if (oidSz) {
  15868. XMEMCPY(objBuf + objSz, oid, oidSz);
  15869. objSz += oidSz;
  15870. }
  15871. if (obj->objSz == 0 || objSz != obj->objSz) {
  15872. obj->objSz = objSz;
  15873. if(((obj->dynamic & WOLFSSL_ASN1_DYNAMIC_DATA) != 0) ||
  15874. (obj->obj == NULL)) {
  15875. if (obj->obj != NULL)
  15876. XFREE((byte*)obj->obj, NULL, DYNAMIC_TYPE_ASN1);
  15877. obj->obj = (byte*)XMALLOC(obj->objSz, NULL, DYNAMIC_TYPE_ASN1);
  15878. if (obj->obj == NULL) {
  15879. wolfSSL_ASN1_OBJECT_free(obj);
  15880. return NULL;
  15881. }
  15882. obj->dynamic |= WOLFSSL_ASN1_DYNAMIC_DATA;
  15883. }
  15884. else {
  15885. obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
  15886. }
  15887. }
  15888. XMEMCPY((byte*)obj->obj, objBuf, obj->objSz);
  15889. (void)type;
  15890. return obj;
  15891. }
  15892. static const char* oid_translate_num_to_str(const char* oid)
  15893. {
  15894. const struct oid_dict {
  15895. const char* num;
  15896. const char* desc;
  15897. } oid_dict[] = {
  15898. { "2.5.29.37.0", "Any Extended Key Usage" },
  15899. { "1.3.6.1.5.5.7.3.1", "TLS Web Server Authentication" },
  15900. { "1.3.6.1.5.5.7.3.2", "TLS Web Client Authentication" },
  15901. { "1.3.6.1.5.5.7.3.3", "Code Signing" },
  15902. { "1.3.6.1.5.5.7.3.4", "E-mail Protection" },
  15903. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  15904. { "1.3.6.1.5.5.7.3.9", "OCSP Signing" },
  15905. { NULL, NULL }
  15906. };
  15907. const struct oid_dict* idx;
  15908. for (idx = oid_dict; idx->num != NULL; idx++) {
  15909. if (!XSTRCMP(oid, idx->num)) {
  15910. return idx->desc;
  15911. }
  15912. }
  15913. return NULL;
  15914. }
  15915. static int wolfssl_obj2txt_numeric(char *buf, int bufLen,
  15916. const WOLFSSL_ASN1_OBJECT *a)
  15917. {
  15918. int bufSz;
  15919. int length;
  15920. word32 idx = 0;
  15921. byte tag;
  15922. if (GetASNTag(a->obj, &idx, &tag, a->objSz) != 0) {
  15923. return WOLFSSL_FAILURE;
  15924. }
  15925. if (tag != ASN_OBJECT_ID) {
  15926. WOLFSSL_MSG("Bad ASN1 Object");
  15927. return WOLFSSL_FAILURE;
  15928. }
  15929. if (GetLength((const byte*)a->obj, &idx, &length,
  15930. a->objSz) < 0 || length < 0) {
  15931. return ASN_PARSE_E;
  15932. }
  15933. if (bufLen < MAX_OID_STRING_SZ) {
  15934. bufSz = bufLen - 1;
  15935. }
  15936. else {
  15937. bufSz = MAX_OID_STRING_SZ;
  15938. }
  15939. if ((bufSz = DecodePolicyOID(buf, (word32)bufSz, a->obj + idx,
  15940. (word32)length)) <= 0) {
  15941. WOLFSSL_MSG("Error decoding OID");
  15942. return WOLFSSL_FAILURE;
  15943. }
  15944. buf[bufSz] = '\0';
  15945. return bufSz;
  15946. }
  15947. /* If no_name is one then use numerical form, otherwise short name.
  15948. *
  15949. * Returns the buffer size on success, WOLFSSL_FAILURE on error
  15950. */
  15951. int wolfSSL_OBJ_obj2txt(char *buf, int bufLen, const WOLFSSL_ASN1_OBJECT *a,
  15952. int no_name)
  15953. {
  15954. int bufSz;
  15955. const char* desc;
  15956. const char* name;
  15957. WOLFSSL_ENTER("wolfSSL_OBJ_obj2txt");
  15958. if (buf == NULL || bufLen <= 1 || a == NULL) {
  15959. WOLFSSL_MSG("Bad input argument");
  15960. return WOLFSSL_FAILURE;
  15961. }
  15962. if (no_name == 1) {
  15963. return wolfssl_obj2txt_numeric(buf, bufLen, a);
  15964. }
  15965. /* return long name unless using x509small, then return short name */
  15966. #if defined(OPENSSL_EXTRA_X509_SMALL) && !defined(OPENSSL_EXTRA)
  15967. name = a->sName;
  15968. #else
  15969. name = wolfSSL_OBJ_nid2ln(wolfSSL_OBJ_obj2nid(a));
  15970. #endif
  15971. if (name == NULL) {
  15972. WOLFSSL_MSG("Name not found");
  15973. bufSz = 0;
  15974. }
  15975. else if (XSTRLEN(name) + 1 < (word32)bufLen - 1) {
  15976. bufSz = (int)XSTRLEN(name);
  15977. }
  15978. else {
  15979. bufSz = bufLen - 1;
  15980. }
  15981. if (bufSz) {
  15982. XMEMCPY(buf, name, bufSz);
  15983. }
  15984. else if (a->type == GEN_DNS || a->type == GEN_EMAIL ||
  15985. a->type == GEN_URI) {
  15986. bufSz = (int)XSTRLEN((const char*)a->obj);
  15987. XMEMCPY(buf, a->obj, min(bufSz, bufLen));
  15988. }
  15989. else if ((bufSz = wolfssl_obj2txt_numeric(buf, bufLen, a)) > 0) {
  15990. if ((desc = oid_translate_num_to_str(buf))) {
  15991. bufSz = (int)XSTRLEN(desc);
  15992. bufSz = min(bufSz, bufLen - 1);
  15993. XMEMCPY(buf, desc, bufSz);
  15994. }
  15995. }
  15996. else {
  15997. bufSz = 0;
  15998. }
  15999. buf[bufSz] = '\0';
  16000. return bufSz;
  16001. }
  16002. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  16003. #if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \
  16004. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \
  16005. defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \
  16006. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS_SMALL)
  16007. /* Returns the long name that corresponds with an ASN1_OBJECT nid value.
  16008. * n : NID value of ASN1_OBJECT to search */
  16009. const char* wolfSSL_OBJ_nid2ln(int n)
  16010. {
  16011. const WOLFSSL_ObjectInfo *obj_info = wolfssl_object_info;
  16012. size_t i;
  16013. WOLFSSL_ENTER("wolfSSL_OBJ_nid2ln");
  16014. for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++, obj_info++) {
  16015. if (obj_info->nid == n) {
  16016. return obj_info->lName;
  16017. }
  16018. }
  16019. WOLFSSL_MSG("NID not found in table");
  16020. return NULL;
  16021. }
  16022. #endif /* OPENSSL_EXTRA, HAVE_LIGHTY, WOLFSSL_MYSQL_COMPATIBLE, HAVE_STUNNEL,
  16023. WOLFSSL_NGINX, HAVE_POCO_LIB, WOLFSSL_HAPROXY, WOLFSSL_WPAS_SMALL */
  16024. #if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \
  16025. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \
  16026. defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \
  16027. defined(WOLFSSL_HAPROXY)
  16028. /* Return the corresponding short name for the nid <n>.
  16029. * or NULL if short name can't be found.
  16030. */
  16031. const char * wolfSSL_OBJ_nid2sn(int n) {
  16032. const WOLFSSL_ObjectInfo *obj_info = wolfssl_object_info;
  16033. size_t i;
  16034. WOLFSSL_ENTER("wolfSSL_OBJ_nid2sn");
  16035. if (n == NID_md5) {
  16036. /* NID_surname == NID_md5 and NID_surname comes before NID_md5 in
  16037. * wolfssl_object_info. As a result, the loop below will incorrectly
  16038. * return "SN" instead of "MD5." NID_surname isn't the true OpenSSL
  16039. * NID, but other functions rely on this table and modifying it to
  16040. * conform with OpenSSL's NIDs isn't trivial. */
  16041. return "MD5";
  16042. }
  16043. for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++, obj_info++) {
  16044. if (obj_info->nid == n) {
  16045. return obj_info->sName;
  16046. }
  16047. }
  16048. WOLFSSL_MSG_EX("SN not found (nid:%d)",n);
  16049. return NULL;
  16050. }
  16051. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  16052. int wolfSSL_OBJ_sn2nid(const char *sn) {
  16053. WOLFSSL_ENTER("wolfSSL_OBJ_sn2nid");
  16054. if (sn == NULL)
  16055. return NID_undef;
  16056. return wc_OBJ_sn2nid(sn);
  16057. }
  16058. #endif
  16059. size_t wolfSSL_OBJ_length(const WOLFSSL_ASN1_OBJECT* o)
  16060. {
  16061. size_t ret = 0;
  16062. int err = 0;
  16063. word32 idx = 0;
  16064. int len = 0;
  16065. WOLFSSL_ENTER("wolfSSL_OBJ_length");
  16066. if (o == NULL || o->obj == NULL) {
  16067. WOLFSSL_MSG("Bad argument.");
  16068. err = 1;
  16069. }
  16070. if (err == 0 && GetASNObjectId(o->obj, &idx, &len, o->objSz)) {
  16071. WOLFSSL_MSG("Error parsing ASN.1 header.");
  16072. err = 1;
  16073. }
  16074. if (err == 0) {
  16075. ret = len;
  16076. }
  16077. WOLFSSL_LEAVE("wolfSSL_OBJ_length", (int)ret);
  16078. return ret;
  16079. }
  16080. const unsigned char* wolfSSL_OBJ_get0_data(const WOLFSSL_ASN1_OBJECT* o)
  16081. {
  16082. const unsigned char* ret = NULL;
  16083. int err = 0;
  16084. word32 idx = 0;
  16085. int len = 0;
  16086. WOLFSSL_ENTER("wolfSSL_OBJ_get0_data");
  16087. if (o == NULL || o->obj == NULL) {
  16088. WOLFSSL_MSG("Bad argument.");
  16089. err = 1;
  16090. }
  16091. if (err == 0 && GetASNObjectId(o->obj, &idx, &len, o->objSz)) {
  16092. WOLFSSL_MSG("Error parsing ASN.1 header.");
  16093. err = 1;
  16094. }
  16095. if (err == 0) {
  16096. ret = o->obj + idx;
  16097. }
  16098. return ret;
  16099. }
  16100. /* Gets the NID value that corresponds with the ASN1 object.
  16101. *
  16102. * o ASN1 object to get NID of
  16103. *
  16104. * Return NID on success and a negative value on failure
  16105. */
  16106. int wolfSSL_OBJ_obj2nid(const WOLFSSL_ASN1_OBJECT *o)
  16107. {
  16108. word32 oid = 0;
  16109. word32 idx = 0;
  16110. int ret;
  16111. #ifdef WOLFSSL_DEBUG_OPENSSL
  16112. WOLFSSL_ENTER("wolfSSL_OBJ_obj2nid");
  16113. #endif
  16114. if (o == NULL) {
  16115. return -1;
  16116. }
  16117. #ifdef WOLFSSL_QT
  16118. if (o->grp == oidCertExtType) {
  16119. /* If nid is an unknown extension, return NID_undef */
  16120. if (wolfSSL_OBJ_nid2sn(o->nid) == NULL)
  16121. return NID_undef;
  16122. }
  16123. #endif
  16124. if (o->nid > 0)
  16125. return o->nid;
  16126. if ((ret = GetObjectId(o->obj, &idx, &oid, o->grp, o->objSz)) < 0) {
  16127. if (ret == ASN_OBJECT_ID_E) {
  16128. /* Put ASN object tag in front and try again */
  16129. int len = SetObjectId(o->objSz, NULL) + o->objSz;
  16130. byte* buf = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16131. if (!buf) {
  16132. WOLFSSL_MSG("malloc error");
  16133. return -1;
  16134. }
  16135. idx = SetObjectId(o->objSz, buf);
  16136. XMEMCPY(buf + idx, o->obj, o->objSz);
  16137. idx = 0;
  16138. ret = GetObjectId(buf, &idx, &oid, o->grp, len);
  16139. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16140. if (ret < 0) {
  16141. WOLFSSL_MSG("Issue getting OID of object");
  16142. return -1;
  16143. }
  16144. }
  16145. else {
  16146. WOLFSSL_MSG("Issue getting OID of object");
  16147. return -1;
  16148. }
  16149. }
  16150. return oid2nid(oid, o->grp);
  16151. }
  16152. /* Return the corresponding NID for the long name <ln>
  16153. * or NID_undef if NID can't be found.
  16154. */
  16155. int wolfSSL_OBJ_ln2nid(const char *ln)
  16156. {
  16157. const WOLFSSL_ObjectInfo *obj_info = wolfssl_object_info;
  16158. size_t lnlen;
  16159. WOLFSSL_ENTER("wolfSSL_OBJ_ln2nid");
  16160. if (ln && (lnlen = XSTRLEN(ln)) > 0) {
  16161. /* Accept input like "/commonName=" */
  16162. if (ln[0] == '/') {
  16163. ln++;
  16164. lnlen--;
  16165. }
  16166. if (lnlen) {
  16167. size_t i;
  16168. if (ln[lnlen-1] == '=') {
  16169. lnlen--;
  16170. }
  16171. for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++, obj_info++) {
  16172. if (lnlen == XSTRLEN(obj_info->lName) &&
  16173. XSTRNCMP(ln, obj_info->lName, lnlen) == 0) {
  16174. return obj_info->nid;
  16175. }
  16176. }
  16177. }
  16178. }
  16179. return NID_undef;
  16180. }
  16181. /* compares two objects, return 0 if equal */
  16182. int wolfSSL_OBJ_cmp(const WOLFSSL_ASN1_OBJECT* a,
  16183. const WOLFSSL_ASN1_OBJECT* b)
  16184. {
  16185. WOLFSSL_ENTER("wolfSSL_OBJ_cmp");
  16186. if (a && b && a->obj && b->obj) {
  16187. if (a->objSz == b->objSz) {
  16188. return XMEMCMP(a->obj, b->obj, a->objSz);
  16189. }
  16190. else if (a->type == EXT_KEY_USAGE_OID ||
  16191. b->type == EXT_KEY_USAGE_OID) {
  16192. /* Special case for EXT_KEY_USAGE_OID so that
  16193. * cmp will be treated as a substring search */
  16194. /* Used in libest to check for id-kp-cmcRA in
  16195. * EXT_KEY_USAGE extension */
  16196. unsigned int idx;
  16197. const byte* s; /* shorter */
  16198. unsigned int sLen;
  16199. const byte* l; /* longer */
  16200. unsigned int lLen;
  16201. if (a->objSz > b->objSz) {
  16202. s = b->obj; sLen = b->objSz;
  16203. l = a->obj; lLen = a->objSz;
  16204. }
  16205. else {
  16206. s = a->obj; sLen = a->objSz;
  16207. l = b->obj; lLen = b->objSz;
  16208. }
  16209. for (idx = 0; idx <= lLen - sLen; idx++) {
  16210. if (XMEMCMP(l + idx, s, sLen) == 0) {
  16211. /* Found substring */
  16212. return 0;
  16213. }
  16214. }
  16215. }
  16216. }
  16217. return WOLFSSL_FATAL_ERROR;
  16218. }
  16219. #endif /* OPENSSL_EXTRA, HAVE_LIGHTY, WOLFSSL_MYSQL_COMPATIBLE, HAVE_STUNNEL,
  16220. WOLFSSL_NGINX, HAVE_POCO_LIB, WOLFSSL_HAPROXY */
  16221. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  16222. defined(HAVE_LIGHTY) || defined(WOLFSSL_MYSQL_COMPATIBLE) || \
  16223. defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  16224. defined(HAVE_POCO_LIB) || defined(WOLFSSL_HAPROXY)
  16225. /* Gets the NID value that is related to the OID string passed in. Example
  16226. * string would be "2.5.29.14" for subject key ID.
  16227. *
  16228. * returns NID value on success and NID_undef on error
  16229. */
  16230. int wolfSSL_OBJ_txt2nid(const char* s)
  16231. {
  16232. unsigned int i;
  16233. #ifdef WOLFSSL_CERT_EXT
  16234. int ret;
  16235. unsigned int sum = 0;
  16236. unsigned int outSz = MAX_OID_SZ;
  16237. unsigned char out[MAX_OID_SZ];
  16238. #endif
  16239. WOLFSSL_ENTER("wolfSSL_OBJ_txt2nid");
  16240. if (s == NULL) {
  16241. return NID_undef;
  16242. }
  16243. #ifdef WOLFSSL_CERT_EXT
  16244. ret = EncodePolicyOID(out, &outSz, s, NULL);
  16245. if (ret == 0) {
  16246. /* sum OID */
  16247. for (i = 0; i < outSz; i++) {
  16248. sum += out[i];
  16249. }
  16250. }
  16251. #endif /* WOLFSSL_CERT_EXT */
  16252. /* get the group that the OID's sum is in
  16253. * @TODO possible conflict with multiples */
  16254. for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++) {
  16255. int len;
  16256. #ifdef WOLFSSL_CERT_EXT
  16257. if (ret == 0) {
  16258. if (wolfssl_object_info[i].id == (int)sum) {
  16259. return wolfssl_object_info[i].nid;
  16260. }
  16261. }
  16262. #endif
  16263. /* try as a short name */
  16264. len = (int)XSTRLEN(s);
  16265. if ((int)XSTRLEN(wolfssl_object_info[i].sName) == len &&
  16266. XSTRNCMP(wolfssl_object_info[i].sName, s, len) == 0) {
  16267. return wolfssl_object_info[i].nid;
  16268. }
  16269. /* try as a long name */
  16270. if ((int)XSTRLEN(wolfssl_object_info[i].lName) == len &&
  16271. XSTRNCMP(wolfssl_object_info[i].lName, s, len) == 0) {
  16272. return wolfssl_object_info[i].nid;
  16273. }
  16274. }
  16275. return NID_undef;
  16276. }
  16277. #endif
  16278. #if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \
  16279. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \
  16280. defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \
  16281. defined(WOLFSSL_HAPROXY)
  16282. /* Creates new ASN1_OBJECT from short name, long name, or text
  16283. * representation of oid. If no_name is 0, then short name, long name, and
  16284. * numerical value of oid are interpreted. If no_name is 1, then only the
  16285. * numerical value of the oid is interpreted.
  16286. *
  16287. * Returns pointer to ASN1_OBJECT on success, or NULL on error.
  16288. */
  16289. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  16290. WOLFSSL_ASN1_OBJECT* wolfSSL_OBJ_txt2obj(const char* s, int no_name)
  16291. {
  16292. int i, ret;
  16293. int nid = NID_undef;
  16294. unsigned int outSz = MAX_OID_SZ;
  16295. unsigned char out[MAX_OID_SZ];
  16296. WOLFSSL_ASN1_OBJECT* obj;
  16297. WOLFSSL_ENTER("wolfSSL_OBJ_txt2obj");
  16298. if (s == NULL)
  16299. return NULL;
  16300. /* If s is numerical value, try to sum oid */
  16301. ret = EncodePolicyOID(out, &outSz, s, NULL);
  16302. if (ret == 0 && outSz > 0) {
  16303. /* If numerical encode succeeded then just
  16304. * create object from that because sums are
  16305. * not unique and can cause confusion. */
  16306. obj = wolfSSL_ASN1_OBJECT_new();
  16307. if (obj == NULL) {
  16308. WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
  16309. return NULL;
  16310. }
  16311. obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
  16312. obj->obj = (byte*)XMALLOC(1 + MAX_LENGTH_SZ + outSz, NULL,
  16313. DYNAMIC_TYPE_ASN1);
  16314. if (obj->obj == NULL) {
  16315. wolfSSL_ASN1_OBJECT_free(obj);
  16316. return NULL;
  16317. }
  16318. obj->dynamic |= WOLFSSL_ASN1_DYNAMIC_DATA;
  16319. i = SetObjectId(outSz, (byte*)obj->obj);
  16320. XMEMCPY((byte*)obj->obj + i, out, outSz);
  16321. obj->objSz = i + outSz;
  16322. return obj;
  16323. }
  16324. /* TODO: update short names in wolfssl_object_info and check OID sums
  16325. are correct */
  16326. for (i = 0; i < (int)WOLFSSL_OBJECT_INFO_SZ; i++) {
  16327. /* Short name, long name, and numerical value are interpreted */
  16328. if (no_name == 0 &&
  16329. ((XSTRCMP(s, wolfssl_object_info[i].sName) == 0) ||
  16330. (XSTRCMP(s, wolfssl_object_info[i].lName) == 0)))
  16331. {
  16332. nid = wolfssl_object_info[i].nid;
  16333. }
  16334. }
  16335. if (nid != NID_undef)
  16336. return wolfSSL_OBJ_nid2obj(nid);
  16337. return NULL;
  16338. }
  16339. #endif
  16340. /* compatibility function. Its intended use is to remove OID's from an
  16341. * internal table that have been added with OBJ_create. wolfSSL manages its
  16342. * own internal OID values and does not currently support OBJ_create. */
  16343. void wolfSSL_OBJ_cleanup(void)
  16344. {
  16345. WOLFSSL_ENTER("wolfSSL_OBJ_cleanup");
  16346. }
  16347. #ifndef NO_WOLFSSL_STUB
  16348. int wolfSSL_OBJ_create(const char *oid, const char *sn, const char *ln)
  16349. {
  16350. (void)oid;
  16351. (void)sn;
  16352. (void)ln;
  16353. WOLFSSL_STUB("wolfSSL_OBJ_create");
  16354. return WOLFSSL_FAILURE;
  16355. }
  16356. #endif
  16357. void wolfSSL_set_verify_depth(WOLFSSL *ssl, int depth)
  16358. {
  16359. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  16360. WOLFSSL_ENTER("wolfSSL_set_verify_depth");
  16361. ssl->options.verifyDepth = (byte)depth;
  16362. #endif
  16363. }
  16364. #endif /* OPENSSL_ALL || HAVE_LIGHTY || WOLFSSL_MYSQL_COMPATIBLE ||
  16365. HAVE_STUNNEL || WOLFSSL_NGINX || HAVE_POCO_LIB || WOLFSSL_HAPROXY */
  16366. #ifdef OPENSSL_EXTRA
  16367. /* wolfSSL uses negative values for error states. This function returns an
  16368. * unsigned type so the value returned is the absolute value of the error.
  16369. */
  16370. unsigned long wolfSSL_ERR_peek_last_error_line(const char **file, int *line)
  16371. {
  16372. WOLFSSL_ENTER("wolfSSL_ERR_peek_last_error");
  16373. (void)line;
  16374. (void)file;
  16375. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  16376. {
  16377. int ret;
  16378. if ((ret = wc_PeekErrorNode(-1, file, NULL, line)) < 0) {
  16379. WOLFSSL_MSG("Issue peeking at error node in queue");
  16380. return 0;
  16381. }
  16382. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) \
  16383. || defined(WOLFSSL_HAPROXY)
  16384. if (ret == -ASN_NO_PEM_HEADER)
  16385. return (ERR_LIB_PEM << 24) | PEM_R_NO_START_LINE;
  16386. #endif
  16387. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  16388. if (ret == ASN1_R_HEADER_TOO_LONG) {
  16389. return (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG;
  16390. }
  16391. #endif
  16392. return (unsigned long)ret;
  16393. }
  16394. #else
  16395. return (unsigned long)(0 - NOT_COMPILED_IN);
  16396. #endif
  16397. }
  16398. #endif /* OPENSSL_EXTRA */
  16399. #if defined(HAVE_EX_DATA) && \
  16400. (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  16401. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  16402. defined(HAVE_LIGHTY)) || defined(HAVE_EX_DATA) || \
  16403. defined(WOLFSSL_WPAS_SMALL)
  16404. CRYPTO_EX_cb_ctx* crypto_ex_cb_ctx_session = NULL;
  16405. static int crypto_ex_cb_new(CRYPTO_EX_cb_ctx** dst, long ctx_l, void* ctx_ptr,
  16406. WOLFSSL_CRYPTO_EX_new* new_func, WOLFSSL_CRYPTO_EX_dup* dup_func,
  16407. WOLFSSL_CRYPTO_EX_free* free_func)
  16408. {
  16409. CRYPTO_EX_cb_ctx* new_ctx = (CRYPTO_EX_cb_ctx*)XMALLOC(
  16410. sizeof(CRYPTO_EX_cb_ctx), NULL, DYNAMIC_TYPE_OPENSSL);
  16411. if (new_ctx == NULL)
  16412. return -1;
  16413. new_ctx->ctx_l = ctx_l;
  16414. new_ctx->ctx_ptr = ctx_ptr;
  16415. new_ctx->new_func = new_func;
  16416. new_ctx->free_func = free_func;
  16417. new_ctx->dup_func = dup_func;
  16418. new_ctx->next = NULL;
  16419. /* Push to end of list */
  16420. while (*dst != NULL)
  16421. dst = &(*dst)->next;
  16422. *dst = new_ctx;
  16423. return 0;
  16424. }
  16425. void crypto_ex_cb_free(CRYPTO_EX_cb_ctx* cb_ctx)
  16426. {
  16427. while (cb_ctx != NULL) {
  16428. CRYPTO_EX_cb_ctx* next = cb_ctx->next;
  16429. XFREE(cb_ctx, NULL, DYNAMIC_TYPE_OPENSSL);
  16430. cb_ctx = next;
  16431. }
  16432. }
  16433. void crypto_ex_cb_setup_new_data(void *new_obj, CRYPTO_EX_cb_ctx* cb_ctx,
  16434. WOLFSSL_CRYPTO_EX_DATA* ex_data)
  16435. {
  16436. int idx = 0;
  16437. for (; cb_ctx != NULL; idx++, cb_ctx = cb_ctx->next) {
  16438. if (cb_ctx->new_func != NULL)
  16439. cb_ctx->new_func(new_obj, NULL, ex_data, idx, cb_ctx->ctx_l,
  16440. cb_ctx->ctx_ptr);
  16441. }
  16442. }
  16443. int crypto_ex_cb_dup_data(const WOLFSSL_CRYPTO_EX_DATA *in,
  16444. WOLFSSL_CRYPTO_EX_DATA *out, CRYPTO_EX_cb_ctx* cb_ctx)
  16445. {
  16446. int idx = 0;
  16447. for (; cb_ctx != NULL; idx++, cb_ctx = cb_ctx->next) {
  16448. if (cb_ctx->dup_func != NULL) {
  16449. void* ptr = wolfSSL_CRYPTO_get_ex_data(in, idx);
  16450. if (!cb_ctx->dup_func(out, in,
  16451. &ptr, idx,
  16452. cb_ctx->ctx_l, cb_ctx->ctx_ptr)) {
  16453. return WOLFSSL_FAILURE;
  16454. }
  16455. wolfSSL_CRYPTO_set_ex_data(out, idx, ptr);
  16456. }
  16457. }
  16458. return WOLFSSL_SUCCESS;
  16459. }
  16460. void crypto_ex_cb_free_data(void *obj, CRYPTO_EX_cb_ctx* cb_ctx,
  16461. WOLFSSL_CRYPTO_EX_DATA* ex_data)
  16462. {
  16463. int idx = 0;
  16464. for (; cb_ctx != NULL; idx++, cb_ctx = cb_ctx->next) {
  16465. if (cb_ctx->free_func != NULL)
  16466. cb_ctx->free_func(obj, NULL, ex_data, idx, cb_ctx->ctx_l,
  16467. cb_ctx->ctx_ptr);
  16468. }
  16469. }
  16470. /**
  16471. * get_ex_new_index is a helper function for the following
  16472. * xx_get_ex_new_index functions:
  16473. * - wolfSSL_CRYPTO_get_ex_new_index
  16474. * - wolfSSL_CTX_get_ex_new_index
  16475. * - wolfSSL_get_ex_new_index
  16476. * Issues a unique index number for the specified class-index.
  16477. * Returns an index number greater or equal to zero on success,
  16478. * -1 on failure.
  16479. */
  16480. int wolfssl_get_ex_new_index(int class_index, long ctx_l, void* ctx_ptr,
  16481. WOLFSSL_CRYPTO_EX_new* new_func, WOLFSSL_CRYPTO_EX_dup* dup_func,
  16482. WOLFSSL_CRYPTO_EX_free* free_func)
  16483. {
  16484. /* index counter for each class index*/
  16485. static int ctx_idx = 0;
  16486. static int ssl_idx = 0;
  16487. static int ssl_session_idx = 0;
  16488. static int x509_idx = 0;
  16489. int idx = -1;
  16490. switch(class_index) {
  16491. case WOLF_CRYPTO_EX_INDEX_SSL:
  16492. WOLFSSL_CRYPTO_EX_DATA_IGNORE_PARAMS(ctx_l, ctx_ptr, new_func,
  16493. dup_func, free_func);
  16494. idx = ssl_idx++;
  16495. break;
  16496. case WOLF_CRYPTO_EX_INDEX_SSL_CTX:
  16497. WOLFSSL_CRYPTO_EX_DATA_IGNORE_PARAMS(ctx_l, ctx_ptr, new_func,
  16498. dup_func, free_func);
  16499. idx = ctx_idx++;
  16500. break;
  16501. case WOLF_CRYPTO_EX_INDEX_X509:
  16502. WOLFSSL_CRYPTO_EX_DATA_IGNORE_PARAMS(ctx_l, ctx_ptr, new_func,
  16503. dup_func, free_func);
  16504. idx = x509_idx++;
  16505. break;
  16506. case WOLF_CRYPTO_EX_INDEX_SSL_SESSION:
  16507. if (crypto_ex_cb_new(&crypto_ex_cb_ctx_session, ctx_l, ctx_ptr,
  16508. new_func, dup_func, free_func) != 0)
  16509. return -1;
  16510. idx = ssl_session_idx++;
  16511. break;
  16512. /* following class indexes are not supoprted */
  16513. case WOLF_CRYPTO_EX_INDEX_X509_STORE:
  16514. case WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX:
  16515. case WOLF_CRYPTO_EX_INDEX_DH:
  16516. case WOLF_CRYPTO_EX_INDEX_DSA:
  16517. case WOLF_CRYPTO_EX_INDEX_EC_KEY:
  16518. case WOLF_CRYPTO_EX_INDEX_RSA:
  16519. case WOLF_CRYPTO_EX_INDEX_ENGINE:
  16520. case WOLF_CRYPTO_EX_INDEX_UI:
  16521. case WOLF_CRYPTO_EX_INDEX_BIO:
  16522. case WOLF_CRYPTO_EX_INDEX_APP:
  16523. case WOLF_CRYPTO_EX_INDEX_UI_METHOD:
  16524. case WOLF_CRYPTO_EX_INDEX_DRBG:
  16525. default:
  16526. break;
  16527. }
  16528. if (idx >= MAX_EX_DATA)
  16529. return -1;
  16530. return idx;
  16531. }
  16532. #endif /* HAVE_EX_DATA || WOLFSSL_WPAS_SMALL */
  16533. #if defined(HAVE_EX_DATA) || defined(WOLFSSL_WPAS_SMALL)
  16534. void* wolfSSL_CTX_get_ex_data(const WOLFSSL_CTX* ctx, int idx)
  16535. {
  16536. WOLFSSL_ENTER("wolfSSL_CTX_get_ex_data");
  16537. #ifdef HAVE_EX_DATA
  16538. if(ctx != NULL) {
  16539. return wolfSSL_CRYPTO_get_ex_data(&ctx->ex_data, idx);
  16540. }
  16541. #else
  16542. (void)ctx;
  16543. (void)idx;
  16544. #endif
  16545. return NULL;
  16546. }
  16547. int wolfSSL_CTX_get_ex_new_index(long idx, void* arg,
  16548. WOLFSSL_CRYPTO_EX_new* new_func,
  16549. WOLFSSL_CRYPTO_EX_dup* dup_func,
  16550. WOLFSSL_CRYPTO_EX_free* free_func)
  16551. {
  16552. WOLFSSL_ENTER("wolfSSL_CTX_get_ex_new_index");
  16553. return wolfssl_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX, idx, arg,
  16554. new_func, dup_func, free_func);
  16555. }
  16556. /* Return the index that can be used for the WOLFSSL structure to store
  16557. * application data.
  16558. *
  16559. */
  16560. int wolfSSL_get_ex_new_index(long argValue, void* arg,
  16561. WOLFSSL_CRYPTO_EX_new* cb1, WOLFSSL_CRYPTO_EX_dup* cb2,
  16562. WOLFSSL_CRYPTO_EX_free* cb3)
  16563. {
  16564. WOLFSSL_ENTER("wolfSSL_get_ex_new_index");
  16565. return wolfssl_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL, argValue, arg,
  16566. cb1, cb2, cb3);
  16567. }
  16568. int wolfSSL_CTX_set_ex_data(WOLFSSL_CTX* ctx, int idx, void* data)
  16569. {
  16570. WOLFSSL_ENTER("wolfSSL_CTX_set_ex_data");
  16571. #ifdef HAVE_EX_DATA
  16572. if (ctx != NULL)
  16573. {
  16574. return wolfSSL_CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  16575. }
  16576. #else
  16577. (void)ctx;
  16578. (void)idx;
  16579. (void)data;
  16580. #endif
  16581. return WOLFSSL_FAILURE;
  16582. }
  16583. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  16584. int wolfSSL_CTX_set_ex_data_with_cleanup(
  16585. WOLFSSL_CTX* ctx,
  16586. int idx,
  16587. void* data,
  16588. wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
  16589. {
  16590. WOLFSSL_ENTER("wolfSSL_CTX_set_ex_data_with_cleanup");
  16591. if (ctx != NULL)
  16592. {
  16593. return wolfSSL_CRYPTO_set_ex_data_with_cleanup(&ctx->ex_data, idx, data,
  16594. cleanup_routine);
  16595. }
  16596. return WOLFSSL_FAILURE;
  16597. }
  16598. #endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
  16599. #endif /* defined(HAVE_EX_DATA) || defined(WOLFSSL_WPAS_SMALL) */
  16600. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  16601. /* Returns char* to app data stored in ex[0].
  16602. *
  16603. * ssl WOLFSSL structure to get app data from
  16604. */
  16605. void* wolfSSL_get_app_data(const WOLFSSL *ssl)
  16606. {
  16607. /* checkout exdata stuff... */
  16608. WOLFSSL_ENTER("wolfSSL_get_app_data");
  16609. return wolfSSL_get_ex_data(ssl, 0);
  16610. }
  16611. /* Set ex array 0 to have app data
  16612. *
  16613. * ssl WOLFSSL struct to set app data in
  16614. * arg data to be stored
  16615. *
  16616. * Returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
  16617. */
  16618. int wolfSSL_set_app_data(WOLFSSL *ssl, void* arg) {
  16619. WOLFSSL_ENTER("wolfSSL_set_app_data");
  16620. return wolfSSL_set_ex_data(ssl, 0, arg);
  16621. }
  16622. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  16623. #if defined(HAVE_EX_DATA) || defined(OPENSSL_EXTRA) || \
  16624. defined(OPENSSL_EXTRA_X509_SMALL) || defined(WOLFSSL_WPAS_SMALL)
  16625. int wolfSSL_set_ex_data(WOLFSSL* ssl, int idx, void* data)
  16626. {
  16627. WOLFSSL_ENTER("wolfSSL_set_ex_data");
  16628. #ifdef HAVE_EX_DATA
  16629. if (ssl != NULL)
  16630. {
  16631. return wolfSSL_CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
  16632. }
  16633. #else
  16634. WOLFSSL_MSG("HAVE_EX_DATA macro is not defined");
  16635. (void)ssl;
  16636. (void)idx;
  16637. (void)data;
  16638. #endif
  16639. return WOLFSSL_FAILURE;
  16640. }
  16641. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  16642. int wolfSSL_set_ex_data_with_cleanup(
  16643. WOLFSSL* ssl,
  16644. int idx,
  16645. void* data,
  16646. wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
  16647. {
  16648. WOLFSSL_ENTER("wolfSSL_set_ex_data_with_cleanup");
  16649. if (ssl != NULL)
  16650. {
  16651. return wolfSSL_CRYPTO_set_ex_data_with_cleanup(&ssl->ex_data, idx, data,
  16652. cleanup_routine);
  16653. }
  16654. return WOLFSSL_FAILURE;
  16655. }
  16656. #endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
  16657. void* wolfSSL_get_ex_data(const WOLFSSL* ssl, int idx)
  16658. {
  16659. WOLFSSL_ENTER("wolfSSL_get_ex_data");
  16660. #ifdef HAVE_EX_DATA
  16661. if (ssl != NULL) {
  16662. return wolfSSL_CRYPTO_get_ex_data(&ssl->ex_data, idx);
  16663. }
  16664. #else
  16665. WOLFSSL_MSG("HAVE_EX_DATA macro is not defined");
  16666. (void)ssl;
  16667. (void)idx;
  16668. #endif
  16669. return 0;
  16670. }
  16671. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL || WOLFSSL_WPAS_SMALL */
  16672. #if defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL) \
  16673. || defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_EXTRA)
  16674. /* returns the enum value associated with handshake state
  16675. *
  16676. * ssl the WOLFSSL structure to get state of
  16677. */
  16678. int wolfSSL_get_state(const WOLFSSL* ssl)
  16679. {
  16680. WOLFSSL_ENTER("wolfSSL_get_state");
  16681. if (ssl == NULL) {
  16682. WOLFSSL_MSG("Null argument passed in");
  16683. return WOLFSSL_FAILURE;
  16684. }
  16685. return ssl->options.handShakeState;
  16686. }
  16687. #endif /* HAVE_LIGHTY || HAVE_STUNNEL || WOLFSSL_MYSQL_COMPATIBLE */
  16688. #ifdef OPENSSL_EXTRA
  16689. void wolfSSL_certs_clear(WOLFSSL* ssl)
  16690. {
  16691. WOLFSSL_ENTER("wolfSSL_certs_clear");
  16692. if (ssl == NULL)
  16693. return;
  16694. /* ctx still owns certificate, certChain, key, dh, and cm */
  16695. if (ssl->buffers.weOwnCert)
  16696. FreeDer(&ssl->buffers.certificate);
  16697. ssl->buffers.certificate = NULL;
  16698. if (ssl->buffers.weOwnCertChain)
  16699. FreeDer(&ssl->buffers.certChain);
  16700. ssl->buffers.certChain = NULL;
  16701. #ifdef WOLFSSL_TLS13
  16702. ssl->buffers.certChainCnt = 0;
  16703. #endif
  16704. if (ssl->buffers.weOwnKey)
  16705. FreeDer(&ssl->buffers.key);
  16706. ssl->buffers.key = NULL;
  16707. ssl->buffers.keyType = 0;
  16708. ssl->buffers.keyId = 0;
  16709. ssl->buffers.keyLabel = 0;
  16710. ssl->buffers.keySz = 0;
  16711. ssl->buffers.keyDevId = 0;
  16712. #ifdef WOLFSSL_DUAL_ALG_CERTS
  16713. if (ssl->buffers.weOwnAltKey)
  16714. FreeDer(&ssl->buffers.altKey);
  16715. ssl->buffers.altKey = NULL;
  16716. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  16717. }
  16718. #endif
  16719. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  16720. || defined(WOLFSSL_NGINX) || defined(WOLFSSL_QT)
  16721. long wolfSSL_ctrl(WOLFSSL* ssl, int cmd, long opt, void* pt)
  16722. {
  16723. WOLFSSL_ENTER("wolfSSL_ctrl");
  16724. if (ssl == NULL)
  16725. return BAD_FUNC_ARG;
  16726. switch (cmd) {
  16727. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_QT) || \
  16728. defined(OPENSSL_ALL)
  16729. #ifdef HAVE_SNI
  16730. case SSL_CTRL_SET_TLSEXT_HOSTNAME:
  16731. WOLFSSL_MSG("Entering Case: SSL_CTRL_SET_TLSEXT_HOSTNAME.");
  16732. if (pt == NULL) {
  16733. WOLFSSL_MSG("Passed in NULL Host Name.");
  16734. break;
  16735. }
  16736. return wolfSSL_set_tlsext_host_name(ssl, (const char*) pt);
  16737. #endif /* HAVE_SNI */
  16738. #endif /* WOLFSSL_NGINX || WOLFSSL_QT || OPENSSL_ALL */
  16739. default:
  16740. WOLFSSL_MSG("Case not implemented.");
  16741. }
  16742. (void)opt;
  16743. (void)pt;
  16744. return WOLFSSL_FAILURE;
  16745. }
  16746. long wolfSSL_CTX_ctrl(WOLFSSL_CTX* ctx, int cmd, long opt, void* pt)
  16747. {
  16748. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16749. long ctrl_opt;
  16750. #endif
  16751. long ret = WOLFSSL_SUCCESS;
  16752. WOLFSSL_ENTER("wolfSSL_CTX_ctrl");
  16753. if (ctx == NULL)
  16754. return WOLFSSL_FAILURE;
  16755. switch (cmd) {
  16756. case SSL_CTRL_CHAIN:
  16757. #ifdef SESSION_CERTS
  16758. {
  16759. /*
  16760. * We don't care about opt here because a copy of the certificate is
  16761. * stored anyway so increasing the reference counter is not necessary.
  16762. * Just check to make sure that it is set to one of the correct values.
  16763. */
  16764. WOLF_STACK_OF(WOLFSSL_X509)* sk = (WOLF_STACK_OF(WOLFSSL_X509)*) pt;
  16765. WOLFSSL_X509* x509;
  16766. int i;
  16767. if (opt != 0 && opt != 1) {
  16768. ret = WOLFSSL_FAILURE;
  16769. break;
  16770. }
  16771. /* Clear certificate chain */
  16772. FreeDer(&ctx->certChain);
  16773. if (sk) {
  16774. for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
  16775. x509 = wolfSSL_sk_X509_value(sk, i);
  16776. /* Prevent wolfSSL_CTX_add_extra_chain_cert from freeing cert */
  16777. if (wolfSSL_X509_up_ref(x509) != 1) {
  16778. WOLFSSL_MSG("Error increasing reference count");
  16779. continue;
  16780. }
  16781. if (wolfSSL_CTX_add_extra_chain_cert(ctx, x509) !=
  16782. WOLFSSL_SUCCESS) {
  16783. WOLFSSL_MSG("Error adding certificate to context");
  16784. /* Decrease reference count on failure */
  16785. wolfSSL_X509_free(x509);
  16786. }
  16787. }
  16788. }
  16789. /* Free previous chain */
  16790. wolfSSL_sk_X509_pop_free(ctx->x509Chain, NULL);
  16791. ctx->x509Chain = sk;
  16792. if (sk && opt == 1) {
  16793. /* up all refs when opt == 1 */
  16794. for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
  16795. x509 = wolfSSL_sk_X509_value(sk, i);
  16796. if (wolfSSL_X509_up_ref(x509) != 1) {
  16797. WOLFSSL_MSG("Error increasing reference count");
  16798. continue;
  16799. }
  16800. }
  16801. }
  16802. }
  16803. #else
  16804. WOLFSSL_MSG("Session certificates not compiled in");
  16805. ret = WOLFSSL_FAILURE;
  16806. #endif
  16807. break;
  16808. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16809. case SSL_CTRL_OPTIONS:
  16810. WOLFSSL_MSG("Entering Case: SSL_CTRL_OPTIONS.");
  16811. ctrl_opt = wolfSSL_CTX_set_options(ctx, opt);
  16812. #ifdef WOLFSSL_QT
  16813. /* Set whether to use client or server cipher preference */
  16814. if ((ctrl_opt & WOLFSSL_OP_CIPHER_SERVER_PREFERENCE)
  16815. == WOLFSSL_OP_CIPHER_SERVER_PREFERENCE) {
  16816. WOLFSSL_MSG("Using Server's Cipher Preference.");
  16817. ctx->useClientOrder = FALSE;
  16818. } else {
  16819. WOLFSSL_MSG("Using Client's Cipher Preference.");
  16820. ctx->useClientOrder = TRUE;
  16821. }
  16822. #endif /* WOLFSSL_QT */
  16823. return ctrl_opt;
  16824. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  16825. case SSL_CTRL_EXTRA_CHAIN_CERT:
  16826. WOLFSSL_MSG("Entering Case: SSL_CTRL_EXTRA_CHAIN_CERT.");
  16827. if (pt == NULL) {
  16828. WOLFSSL_MSG("Passed in x509 pointer NULL.");
  16829. ret = WOLFSSL_FAILURE;
  16830. break;
  16831. }
  16832. return wolfSSL_CTX_add_extra_chain_cert(ctx, (WOLFSSL_X509*)pt);
  16833. #ifndef NO_DH
  16834. case SSL_CTRL_SET_TMP_DH:
  16835. WOLFSSL_MSG("Entering Case: SSL_CTRL_SET_TMP_DH.");
  16836. if (pt == NULL) {
  16837. WOLFSSL_MSG("Passed in DH pointer NULL.");
  16838. ret = WOLFSSL_FAILURE;
  16839. break;
  16840. }
  16841. return wolfSSL_CTX_set_tmp_dh(ctx, (WOLFSSL_DH*)pt);
  16842. #endif
  16843. #ifdef HAVE_ECC
  16844. case SSL_CTRL_SET_TMP_ECDH:
  16845. WOLFSSL_MSG("Entering Case: SSL_CTRL_SET_TMP_ECDH.");
  16846. if (pt == NULL) {
  16847. WOLFSSL_MSG("Passed in ECDH pointer NULL.");
  16848. ret = WOLFSSL_FAILURE;
  16849. break;
  16850. }
  16851. return wolfSSL_SSL_CTX_set_tmp_ecdh(ctx, (WOLFSSL_EC_KEY*)pt);
  16852. #endif
  16853. case SSL_CTRL_MODE:
  16854. wolfSSL_CTX_set_mode(ctx,opt);
  16855. break;
  16856. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  16857. WOLFSSL_MSG("set min proto version");
  16858. return wolfSSL_CTX_set_min_proto_version(ctx, (int)opt);
  16859. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  16860. WOLFSSL_MSG("set max proto version");
  16861. return wolfSSL_CTX_set_max_proto_version(ctx, (int)opt);
  16862. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  16863. WOLFSSL_MSG("get min proto version");
  16864. return wolfSSL_CTX_get_min_proto_version(ctx);
  16865. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  16866. WOLFSSL_MSG("get max proto version");
  16867. return wolfSSL_CTX_get_max_proto_version(ctx);
  16868. default:
  16869. WOLFSSL_MSG("CTX_ctrl cmd not implemented");
  16870. ret = WOLFSSL_FAILURE;
  16871. break;
  16872. }
  16873. (void)ctx;
  16874. (void)cmd;
  16875. (void)opt;
  16876. (void)pt;
  16877. WOLFSSL_LEAVE("wolfSSL_CTX_ctrl", (int)ret);
  16878. return ret;
  16879. }
  16880. #ifndef WOLFSSL_NO_STUB
  16881. long wolfSSL_CTX_callback_ctrl(WOLFSSL_CTX* ctx, int cmd, void (*fp)(void))
  16882. {
  16883. (void) ctx;
  16884. (void) cmd;
  16885. (void) fp;
  16886. WOLFSSL_STUB("wolfSSL_CTX_callback_ctrl");
  16887. return WOLFSSL_FAILURE;
  16888. }
  16889. #endif /* WOLFSSL_NO_STUB */
  16890. #ifndef NO_WOLFSSL_STUB
  16891. long wolfSSL_CTX_clear_extra_chain_certs(WOLFSSL_CTX* ctx)
  16892. {
  16893. return wolfSSL_CTX_ctrl(ctx, SSL_CTRL_CLEAR_EXTRA_CHAIN_CERTS, 0L, NULL);
  16894. }
  16895. #endif
  16896. /* Returns the verifyCallback from the ssl structure if successful.
  16897. Returns NULL otherwise. */
  16898. VerifyCallback wolfSSL_get_verify_callback(WOLFSSL* ssl)
  16899. {
  16900. WOLFSSL_ENTER("wolfSSL_get_verify_callback");
  16901. if (ssl) {
  16902. return ssl->verifyCallback;
  16903. }
  16904. return NULL;
  16905. }
  16906. #ifndef NO_BIO
  16907. /* Converts EVP_PKEY data from a bio buffer to a WOLFSSL_EVP_PKEY structure.
  16908. Returns pointer to private EVP_PKEY struct upon success, NULL if there
  16909. is a failure.*/
  16910. WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_bio(WOLFSSL_BIO* bio,
  16911. WOLFSSL_EVP_PKEY** out)
  16912. {
  16913. unsigned char* mem = NULL;
  16914. int memSz = 0;
  16915. WOLFSSL_EVP_PKEY* key = NULL;
  16916. unsigned char* extraBioMem = NULL;
  16917. WOLFSSL_ENTER("wolfSSL_d2i_PrivateKey_bio");
  16918. if (bio == NULL) {
  16919. return NULL;
  16920. }
  16921. (void)out;
  16922. memSz = wolfSSL_BIO_get_len(bio);
  16923. if (memSz <= 0) {
  16924. WOLFSSL_MSG("wolfSSL_BIO_get_len() failure");
  16925. return NULL;
  16926. }
  16927. mem = (unsigned char*)XMALLOC(memSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  16928. if (mem == NULL) {
  16929. WOLFSSL_MSG("Malloc failure");
  16930. return NULL;
  16931. }
  16932. if (wolfSSL_BIO_read(bio, (unsigned char*)mem, memSz) == memSz) {
  16933. int extraBioMemSz;
  16934. int derLength;
  16935. /* Determines key type and returns the new private EVP_PKEY object */
  16936. if ((key = wolfSSL_d2i_PrivateKey_EVP(NULL, &mem, (long)memSz)) ==
  16937. NULL) {
  16938. WOLFSSL_MSG("wolfSSL_d2i_PrivateKey_EVP() failure");
  16939. XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  16940. return NULL;
  16941. }
  16942. /* Write extra data back into bio object if necessary. */
  16943. derLength = key->pkey_sz;
  16944. extraBioMemSz = (memSz - derLength);
  16945. if (extraBioMemSz > 0) {
  16946. int i;
  16947. int j = 0;
  16948. extraBioMem = (unsigned char *)XMALLOC(extraBioMemSz, NULL,
  16949. DYNAMIC_TYPE_TMP_BUFFER);
  16950. if (extraBioMem == NULL) {
  16951. WOLFSSL_MSG("Malloc failure");
  16952. XFREE((unsigned char*)extraBioMem, bio->heap,
  16953. DYNAMIC_TYPE_TMP_BUFFER);
  16954. XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  16955. return NULL;
  16956. }
  16957. for (i = derLength; i < memSz; i++) {
  16958. *(extraBioMem + j) = *(mem + i);
  16959. j++;
  16960. }
  16961. wolfSSL_BIO_write(bio, extraBioMem, extraBioMemSz);
  16962. if (wolfSSL_BIO_get_len(bio) <= 0) {
  16963. WOLFSSL_MSG("Failed to write memory to bio");
  16964. XFREE((unsigned char*)extraBioMem, bio->heap,
  16965. DYNAMIC_TYPE_TMP_BUFFER);
  16966. XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  16967. return NULL;
  16968. }
  16969. XFREE((unsigned char*)extraBioMem, bio->heap,
  16970. DYNAMIC_TYPE_TMP_BUFFER);
  16971. }
  16972. if (out != NULL) {
  16973. *out = key;
  16974. }
  16975. }
  16976. XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  16977. return key;
  16978. }
  16979. #endif /* !NO_BIO */
  16980. #endif /* OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY || WOLFSSL_QT */
  16981. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || \
  16982. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX) || \
  16983. defined(WOLFSSL_QT) || defined(WOLFSSL_WPAS_SMALL)
  16984. /* Converts a DER encoded private key to a WOLFSSL_EVP_PKEY structure.
  16985. * returns a pointer to a new WOLFSSL_EVP_PKEY structure on success and NULL
  16986. * on fail */
  16987. WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_EVP(WOLFSSL_EVP_PKEY** out,
  16988. unsigned char** in, long inSz)
  16989. {
  16990. WOLFSSL_ENTER("wolfSSL_d2i_PrivateKey_EVP");
  16991. return d2iGenericKey(out, (const unsigned char**)in, inSz, 1);
  16992. }
  16993. #endif /* OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY || WOLFSSL_QT ||
  16994. * WOLFSSL_WPAS_SMALL*/
  16995. /* stunnel compatibility functions*/
  16996. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  16997. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  16998. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  16999. defined(WOLFSSL_OPENSSH)))
  17000. void wolfSSL_ERR_remove_thread_state(void* pid)
  17001. {
  17002. (void) pid;
  17003. return;
  17004. }
  17005. #ifndef NO_FILESYSTEM
  17006. /***TBD ***/
  17007. void wolfSSL_print_all_errors_fp(XFILE fp)
  17008. {
  17009. (void)fp;
  17010. }
  17011. #endif /* !NO_FILESYSTEM */
  17012. #endif /* OPENSSL_ALL || OPENSSL_EXTRA || HAVE_STUNNEL || WOLFSSL_NGINX ||
  17013. HAVE_LIGHTY || WOLFSSL_HAPROXY || WOLFSSL_OPENSSH */
  17014. /* Note: This is a huge section of API's - through
  17015. * wolfSSL_X509_OBJECT_get0_X509_CRL */
  17016. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  17017. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  17018. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  17019. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  17020. #if defined(USE_WOLFSSL_MEMORY) && !defined(WOLFSSL_DEBUG_MEMORY) && \
  17021. !defined(WOLFSSL_STATIC_MEMORY)
  17022. static wolfSSL_OSSL_Malloc_cb ossl_malloc = NULL;
  17023. static wolfSSL_OSSL_Free_cb ossl_free = NULL;
  17024. static wolfSSL_OSSL_Realloc_cb ossl_realloc = NULL;
  17025. static void* OSSL_Malloc(size_t size)
  17026. {
  17027. if (ossl_malloc != NULL)
  17028. return ossl_malloc(size, NULL, 0);
  17029. else
  17030. return NULL;
  17031. }
  17032. static void OSSL_Free(void *ptr)
  17033. {
  17034. if (ossl_free != NULL)
  17035. ossl_free(ptr, NULL, 0);
  17036. }
  17037. static void* OSSL_Realloc(void *ptr, size_t size)
  17038. {
  17039. if (ossl_realloc != NULL)
  17040. return ossl_realloc(ptr, size, NULL, 0);
  17041. else
  17042. return NULL;
  17043. }
  17044. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_DEBUG_MEMORY &&
  17045. * !WOLFSSL_STATIC_MEMORY */
  17046. int wolfSSL_CRYPTO_set_mem_functions(
  17047. wolfSSL_OSSL_Malloc_cb m,
  17048. wolfSSL_OSSL_Realloc_cb r,
  17049. wolfSSL_OSSL_Free_cb f)
  17050. {
  17051. #if defined(USE_WOLFSSL_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
  17052. #ifdef WOLFSSL_DEBUG_MEMORY
  17053. WOLFSSL_MSG("mem functions will receive function name instead of "
  17054. "file name");
  17055. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)m, (wolfSSL_Free_cb)f,
  17056. (wolfSSL_Realloc_cb)r) == 0)
  17057. return WOLFSSL_SUCCESS;
  17058. #else
  17059. WOLFSSL_MSG("wolfSSL was compiled without WOLFSSL_DEBUG_MEMORY mem "
  17060. "functions will receive a NULL file name and 0 for the "
  17061. "line number.");
  17062. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)OSSL_Malloc,
  17063. (wolfSSL_Free_cb)OSSL_Free, (wolfSSL_Realloc_cb)OSSL_Realloc) == 0) {
  17064. ossl_malloc = m;
  17065. ossl_free = f;
  17066. ossl_realloc = r;
  17067. return WOLFSSL_SUCCESS;
  17068. }
  17069. #endif
  17070. else
  17071. return WOLFSSL_FAILURE;
  17072. #else
  17073. (void)m;
  17074. (void)r;
  17075. (void)f;
  17076. WOLFSSL_MSG("wolfSSL allocator callback functions not compiled in");
  17077. return WOLFSSL_FAILURE;
  17078. #endif
  17079. }
  17080. int wolfSSL_ERR_load_ERR_strings(void)
  17081. {
  17082. return WOLFSSL_SUCCESS;
  17083. }
  17084. void wolfSSL_ERR_load_crypto_strings(void)
  17085. {
  17086. WOLFSSL_ENTER("wolfSSL_ERR_load_crypto_strings");
  17087. /* Do nothing */
  17088. return;
  17089. }
  17090. int wolfSSL_FIPS_mode(void)
  17091. {
  17092. #ifdef HAVE_FIPS
  17093. return 1;
  17094. #else
  17095. return 0;
  17096. #endif
  17097. }
  17098. int wolfSSL_FIPS_mode_set(int r)
  17099. {
  17100. #ifdef HAVE_FIPS
  17101. if (r == 0) {
  17102. WOLFSSL_MSG("Cannot disable FIPS at runtime.");
  17103. return WOLFSSL_FAILURE;
  17104. }
  17105. return WOLFSSL_SUCCESS;
  17106. #else
  17107. if (r == 0) {
  17108. return WOLFSSL_SUCCESS;
  17109. }
  17110. WOLFSSL_MSG("Cannot enable FIPS. This isn't the wolfSSL FIPS code.");
  17111. return WOLFSSL_FAILURE;
  17112. #endif
  17113. }
  17114. int wolfSSL_CIPHER_get_bits(const WOLFSSL_CIPHER *c, int *alg_bits)
  17115. {
  17116. int ret = WOLFSSL_FAILURE;
  17117. WOLFSSL_ENTER("wolfSSL_CIPHER_get_bits");
  17118. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  17119. (void)alg_bits;
  17120. if (c!= NULL)
  17121. ret = c->bits;
  17122. #else
  17123. if (c != NULL && c->ssl != NULL) {
  17124. ret = 8 * c->ssl->specs.key_size;
  17125. if (alg_bits != NULL) {
  17126. *alg_bits = ret;
  17127. }
  17128. }
  17129. #endif
  17130. return ret;
  17131. }
  17132. /* returns value less than 0 on fail to match
  17133. * On a successful match the priority level found is returned
  17134. */
  17135. int wolfSSL_sk_SSL_CIPHER_find(
  17136. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk, const WOLFSSL_CIPHER* toFind)
  17137. {
  17138. WOLFSSL_STACK* next;
  17139. int i, sz;
  17140. if (sk == NULL || toFind == NULL) {
  17141. return WOLFSSL_FATAL_ERROR;
  17142. }
  17143. sz = wolfSSL_sk_SSL_CIPHER_num(sk);
  17144. next = sk;
  17145. for (i = 0; i < sz && next != NULL; i++) {
  17146. if (next->data.cipher.cipherSuite0 == toFind->cipherSuite0 &&
  17147. next->data.cipher.cipherSuite == toFind->cipherSuite) {
  17148. return sz - i; /* reverse because stack pushed highest on first */
  17149. }
  17150. next = next->next;
  17151. }
  17152. return WOLFSSL_FATAL_ERROR;
  17153. }
  17154. /* free's all nodes in the stack and there data */
  17155. void wolfSSL_sk_SSL_CIPHER_free(WOLF_STACK_OF(WOLFSSL_CIPHER)* sk)
  17156. {
  17157. WOLFSSL_ENTER("wolfSSL_sk_SSL_CIPHER_free");
  17158. wolfSSL_sk_free(sk);
  17159. }
  17160. #ifdef HAVE_SNI
  17161. int wolfSSL_set_tlsext_host_name(WOLFSSL* ssl, const char* host_name)
  17162. {
  17163. int ret;
  17164. WOLFSSL_ENTER("wolfSSL_set_tlsext_host_name");
  17165. ret = wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  17166. host_name, (word16)XSTRLEN(host_name));
  17167. WOLFSSL_LEAVE("wolfSSL_set_tlsext_host_name", ret);
  17168. return ret;
  17169. }
  17170. #ifndef NO_WOLFSSL_SERVER
  17171. const char * wolfSSL_get_servername(WOLFSSL* ssl, byte type)
  17172. {
  17173. void * serverName = NULL;
  17174. if (ssl == NULL)
  17175. return NULL;
  17176. TLSX_SNI_GetRequest(ssl->extensions, type, &serverName);
  17177. return (const char *)serverName;
  17178. }
  17179. #endif /* NO_WOLFSSL_SERVER */
  17180. #endif /* HAVE_SNI */
  17181. WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
  17182. {
  17183. int ret;
  17184. /* This method requires some explanation. Its sibling is
  17185. * int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
  17186. * which re-inits the WOLFSSL* with all settings in the new CTX.
  17187. * That one is the right one to use *before* a handshake is started.
  17188. *
  17189. * This method was added by OpenSSL to be used *during* the handshake, e.g.
  17190. * when a server inspects the SNI in a ClientHello callback and
  17191. * decides which set of certificates to use.
  17192. *
  17193. * Since, at the time the SNI callback is run, some decisions on
  17194. * Extensions or the ServerHello might already have been taken, this
  17195. * method is very restricted in what it does:
  17196. * - changing the server certificate(s)
  17197. * - changing the server id for session handling
  17198. * and everything else in WOLFSSL* needs to remain untouched.
  17199. */
  17200. WOLFSSL_ENTER("wolfSSL_set_SSL_CTX");
  17201. if (ssl == NULL || ctx == NULL)
  17202. return NULL;
  17203. if (ssl->ctx == ctx)
  17204. return ssl->ctx;
  17205. wolfSSL_RefInc(&ctx->ref, &ret);
  17206. #ifdef WOLFSSL_REFCNT_ERROR_RETURN
  17207. if (ret != 0) {
  17208. /* can only fail on serious stuff, like mutex not working
  17209. * or ctx refcount out of whack. */
  17210. return NULL;
  17211. }
  17212. #else
  17213. (void)ret;
  17214. #endif
  17215. if (ssl->ctx != NULL)
  17216. wolfSSL_CTX_free(ssl->ctx);
  17217. ssl->ctx = ctx;
  17218. #ifndef NO_CERTS
  17219. /* ctx owns certificate, certChain and key */
  17220. ssl->buffers.certificate = ctx->certificate;
  17221. ssl->buffers.certChain = ctx->certChain;
  17222. #ifdef WOLFSSL_TLS13
  17223. ssl->buffers.certChainCnt = ctx->certChainCnt;
  17224. #endif
  17225. ssl->buffers.key = ctx->privateKey;
  17226. ssl->buffers.keyType = ctx->privateKeyType;
  17227. ssl->buffers.keyId = ctx->privateKeyId;
  17228. ssl->buffers.keyLabel = ctx->privateKeyLabel;
  17229. ssl->buffers.keySz = ctx->privateKeySz;
  17230. ssl->buffers.keyDevId = ctx->privateKeyDevId;
  17231. /* flags indicating what certs/keys are available */
  17232. ssl->options.haveRSA = ctx->haveRSA;
  17233. ssl->options.haveDH = ctx->haveDH;
  17234. ssl->options.haveECDSAsig = ctx->haveECDSAsig;
  17235. ssl->options.haveECC = ctx->haveECC;
  17236. ssl->options.haveStaticECC = ctx->haveStaticECC;
  17237. ssl->options.haveFalconSig = ctx->haveFalconSig;
  17238. ssl->options.haveDilithiumSig = ctx->haveDilithiumSig;
  17239. #ifdef WOLFSSL_DUAL_ALG_CERTS
  17240. ssl->buffers.altKey = ctx->altPrivateKey;
  17241. ssl->buffers.altKeySz = ctx->altPrivateKeySz;
  17242. ssl->buffers.altKeyType = ctx->altPrivateKeyType;
  17243. #endif /* WOLFSSL_DUAL_ALG_CERTS */
  17244. #endif
  17245. #ifdef WOLFSSL_SESSION_ID_CTX
  17246. /* copy over application session context ID */
  17247. ssl->sessionCtxSz = ctx->sessionCtxSz;
  17248. XMEMCPY(ssl->sessionCtx, ctx->sessionCtx, ctx->sessionCtxSz);
  17249. #endif
  17250. return ssl->ctx;
  17251. }
  17252. VerifyCallback wolfSSL_CTX_get_verify_callback(WOLFSSL_CTX* ctx)
  17253. {
  17254. WOLFSSL_ENTER("wolfSSL_CTX_get_verify_callback");
  17255. if(ctx)
  17256. return ctx->verifyCallback;
  17257. return NULL;
  17258. }
  17259. #ifdef HAVE_SNI
  17260. void wolfSSL_CTX_set_servername_callback(WOLFSSL_CTX* ctx, CallbackSniRecv cb)
  17261. {
  17262. WOLFSSL_ENTER("wolfSSL_CTX_set_servername_callback");
  17263. if (ctx)
  17264. ctx->sniRecvCb = cb;
  17265. }
  17266. int wolfSSL_CTX_set_tlsext_servername_callback(WOLFSSL_CTX* ctx,
  17267. CallbackSniRecv cb)
  17268. {
  17269. WOLFSSL_ENTER("wolfSSL_CTX_set_tlsext_servername_callback");
  17270. if (ctx) {
  17271. ctx->sniRecvCb = cb;
  17272. return WOLFSSL_SUCCESS;
  17273. }
  17274. return WOLFSSL_FAILURE;
  17275. }
  17276. int wolfSSL_CTX_set_servername_arg(WOLFSSL_CTX* ctx, void* arg)
  17277. {
  17278. WOLFSSL_ENTER("wolfSSL_CTX_set_servername_arg");
  17279. if (ctx) {
  17280. ctx->sniRecvCbArg = arg;
  17281. return WOLFSSL_SUCCESS;
  17282. }
  17283. return WOLFSSL_FAILURE;
  17284. }
  17285. #endif /* HAVE_SNI */
  17286. #ifndef NO_BIO
  17287. void wolfSSL_ERR_load_BIO_strings(void) {
  17288. WOLFSSL_ENTER("wolfSSL_ERR_load_BIO_strings");
  17289. /* do nothing */
  17290. }
  17291. #endif
  17292. #ifndef NO_WOLFSSL_STUB
  17293. /* Set THREADID callback, return 1 on success, 0 on error */
  17294. int wolfSSL_THREADID_set_callback(
  17295. void(*threadid_func)(WOLFSSL_CRYPTO_THREADID*))
  17296. {
  17297. WOLFSSL_ENTER("wolfSSL_THREADID_set_callback");
  17298. WOLFSSL_STUB("CRYPTO_THREADID_set_callback");
  17299. (void)threadid_func;
  17300. return 1;
  17301. }
  17302. #endif
  17303. #ifndef NO_WOLFSSL_STUB
  17304. void wolfSSL_THREADID_set_numeric(void* id, unsigned long val)
  17305. {
  17306. WOLFSSL_ENTER("wolfSSL_THREADID_set_numeric");
  17307. WOLFSSL_STUB("CRYPTO_THREADID_set_numeric");
  17308. (void)id;
  17309. (void)val;
  17310. return;
  17311. }
  17312. #endif
  17313. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (HAVE_STUNNEL || WOLFSSL_NGINX ||
  17314. * HAVE_LIGHTY || WOLFSSL_HAPROXY || WOLFSSL_OPENSSH ||
  17315. * HAVE_SBLIM_SFCB)) */
  17316. #if defined(OPENSSL_EXTRA)
  17317. int wolfSSL_CRYPTO_memcmp(const void *a, const void *b, size_t size)
  17318. {
  17319. if (!a || !b)
  17320. return 0;
  17321. return ConstantCompare((const byte*)a, (const byte*)b, (int)size);
  17322. }
  17323. unsigned long wolfSSL_ERR_peek_last_error(void)
  17324. {
  17325. WOLFSSL_ENTER("wolfSSL_ERR_peek_last_error");
  17326. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  17327. {
  17328. int ret;
  17329. if ((ret = wc_PeekErrorNode(-1, NULL, NULL, NULL)) < 0) {
  17330. WOLFSSL_MSG("Issue peeking at error node in queue");
  17331. return 0;
  17332. }
  17333. if (ret == -ASN_NO_PEM_HEADER)
  17334. return (ERR_LIB_PEM << 24) | PEM_R_NO_START_LINE;
  17335. #if defined(WOLFSSL_PYTHON)
  17336. if (ret == ASN1_R_HEADER_TOO_LONG)
  17337. return (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG;
  17338. #endif
  17339. return (unsigned long)ret;
  17340. }
  17341. #else
  17342. return (unsigned long)(0 - NOT_COMPILED_IN);
  17343. #endif
  17344. }
  17345. #endif /* OPENSSL_EXTRA */
  17346. int wolfSSL_version(WOLFSSL* ssl)
  17347. {
  17348. WOLFSSL_ENTER("wolfSSL_version");
  17349. if (ssl->version.major == SSLv3_MAJOR) {
  17350. switch (ssl->version.minor) {
  17351. case SSLv3_MINOR :
  17352. return SSL3_VERSION;
  17353. case TLSv1_MINOR :
  17354. return TLS1_VERSION;
  17355. case TLSv1_1_MINOR :
  17356. return TLS1_1_VERSION;
  17357. case TLSv1_2_MINOR :
  17358. return TLS1_2_VERSION;
  17359. case TLSv1_3_MINOR :
  17360. return TLS1_3_VERSION;
  17361. default:
  17362. return WOLFSSL_FAILURE;
  17363. }
  17364. }
  17365. else if (ssl->version.major == DTLS_MAJOR) {
  17366. switch (ssl->version.minor) {
  17367. case DTLS_MINOR :
  17368. return DTLS1_VERSION;
  17369. case DTLSv1_2_MINOR :
  17370. return DTLS1_2_VERSION;
  17371. case DTLSv1_3_MINOR:
  17372. return DTLS1_3_VERSION;
  17373. default:
  17374. return WOLFSSL_FAILURE;
  17375. }
  17376. }
  17377. return WOLFSSL_FAILURE;
  17378. }
  17379. WOLFSSL_CTX* wolfSSL_get_SSL_CTX(WOLFSSL* ssl)
  17380. {
  17381. WOLFSSL_ENTER("wolfSSL_get_SSL_CTX");
  17382. return ssl->ctx;
  17383. }
  17384. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && defined(HAVE_STUNNEL)) \
  17385. || defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX)
  17386. /* TODO: Doesn't currently track SSL_VERIFY_CLIENT_ONCE */
  17387. int wolfSSL_get_verify_mode(const WOLFSSL* ssl) {
  17388. int mode = 0;
  17389. WOLFSSL_ENTER("wolfSSL_get_verify_mode");
  17390. if (!ssl) {
  17391. return WOLFSSL_FAILURE;
  17392. }
  17393. if (ssl->options.verifyNone) {
  17394. mode = WOLFSSL_VERIFY_NONE;
  17395. }
  17396. else {
  17397. if (ssl->options.verifyPeer) {
  17398. mode |= WOLFSSL_VERIFY_PEER;
  17399. }
  17400. if (ssl->options.failNoCert) {
  17401. mode |= WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  17402. }
  17403. if (ssl->options.failNoCertxPSK) {
  17404. mode |= WOLFSSL_VERIFY_FAIL_EXCEPT_PSK;
  17405. }
  17406. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  17407. if (ssl->options.verifyPostHandshake) {
  17408. mode |= WOLFSSL_VERIFY_POST_HANDSHAKE;
  17409. }
  17410. #endif
  17411. }
  17412. WOLFSSL_LEAVE("wolfSSL_get_verify_mode", mode);
  17413. return mode;
  17414. }
  17415. int wolfSSL_CTX_get_verify_mode(const WOLFSSL_CTX* ctx)
  17416. {
  17417. int mode = 0;
  17418. WOLFSSL_ENTER("wolfSSL_CTX_get_verify_mode");
  17419. if (!ctx) {
  17420. return WOLFSSL_FAILURE;
  17421. }
  17422. if (ctx->verifyNone) {
  17423. mode = WOLFSSL_VERIFY_NONE;
  17424. }
  17425. else {
  17426. if (ctx->verifyPeer) {
  17427. mode |= WOLFSSL_VERIFY_PEER;
  17428. }
  17429. if (ctx->failNoCert) {
  17430. mode |= WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  17431. }
  17432. if (ctx->failNoCertxPSK) {
  17433. mode |= WOLFSSL_VERIFY_FAIL_EXCEPT_PSK;
  17434. }
  17435. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  17436. if (ctx->verifyPostHandshake) {
  17437. mode |= WOLFSSL_VERIFY_POST_HANDSHAKE;
  17438. }
  17439. #endif
  17440. }
  17441. WOLFSSL_LEAVE("wolfSSL_CTX_get_verify_mode", mode);
  17442. return mode;
  17443. }
  17444. #endif
  17445. #ifdef WOLFSSL_JNI
  17446. int wolfSSL_set_jobject(WOLFSSL* ssl, void* objPtr)
  17447. {
  17448. WOLFSSL_ENTER("wolfSSL_set_jobject");
  17449. if (ssl != NULL)
  17450. {
  17451. ssl->jObjectRef = objPtr;
  17452. return WOLFSSL_SUCCESS;
  17453. }
  17454. return WOLFSSL_FAILURE;
  17455. }
  17456. void* wolfSSL_get_jobject(WOLFSSL* ssl)
  17457. {
  17458. WOLFSSL_ENTER("wolfSSL_get_jobject");
  17459. if (ssl != NULL)
  17460. return ssl->jObjectRef;
  17461. return NULL;
  17462. }
  17463. #endif /* WOLFSSL_JNI */
  17464. #ifdef WOLFSSL_ASYNC_CRYPT
  17465. int wolfSSL_CTX_AsyncPoll(WOLFSSL_CTX* ctx, WOLF_EVENT** events, int maxEvents,
  17466. WOLF_EVENT_FLAG flags, int* eventCount)
  17467. {
  17468. if (ctx == NULL) {
  17469. return BAD_FUNC_ARG;
  17470. }
  17471. return wolfAsync_EventQueuePoll(&ctx->event_queue, NULL,
  17472. events, maxEvents, flags, eventCount);
  17473. }
  17474. int wolfSSL_AsyncPoll(WOLFSSL* ssl, WOLF_EVENT_FLAG flags)
  17475. {
  17476. int ret, eventCount = 0;
  17477. WOLF_EVENT* events[1];
  17478. if (ssl == NULL) {
  17479. return BAD_FUNC_ARG;
  17480. }
  17481. ret = wolfAsync_EventQueuePoll(&ssl->ctx->event_queue, ssl,
  17482. events, sizeof(events)/sizeof(events[0]), flags, &eventCount);
  17483. if (ret == 0) {
  17484. ret = eventCount;
  17485. }
  17486. return ret;
  17487. }
  17488. #endif /* WOLFSSL_ASYNC_CRYPT */
  17489. #ifdef OPENSSL_EXTRA
  17490. static int peek_ignore_err(int err)
  17491. {
  17492. switch(err) {
  17493. case -WANT_READ:
  17494. case -WANT_WRITE:
  17495. case -ZERO_RETURN:
  17496. case -WOLFSSL_ERROR_ZERO_RETURN:
  17497. case -SOCKET_PEER_CLOSED_E:
  17498. case -SOCKET_ERROR_E:
  17499. return 1;
  17500. default:
  17501. return 0;
  17502. }
  17503. }
  17504. unsigned long wolfSSL_ERR_peek_error_line_data(const char **file, int *line,
  17505. const char **data, int *flags)
  17506. {
  17507. unsigned long err;
  17508. WOLFSSL_ENTER("wolfSSL_ERR_peek_error_line_data");
  17509. err = wc_PeekErrorNodeLineData(file, line, data, flags, peek_ignore_err);
  17510. if (err == -ASN_NO_PEM_HEADER)
  17511. return (ERR_LIB_PEM << 24) | PEM_R_NO_START_LINE;
  17512. #ifdef OPENSSL_ALL
  17513. /* PARSE_ERROR is returned if an HTTP request is detected. */
  17514. else if (err == -SSL_R_HTTP_REQUEST)
  17515. return (ERR_LIB_SSL << 24) | -SSL_R_HTTP_REQUEST;
  17516. #endif
  17517. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  17518. else if (err == ASN1_R_HEADER_TOO_LONG)
  17519. return (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG;
  17520. #endif
  17521. return err;
  17522. }
  17523. #endif
  17524. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  17525. #if !defined(WOLFSSL_USER_IO)
  17526. /* converts an IPv6 or IPv4 address into an octet string for use with rfc3280
  17527. * example input would be "127.0.0.1" and the returned value would be 7F000001
  17528. */
  17529. WOLFSSL_ASN1_STRING* wolfSSL_a2i_IPADDRESS(const char* ipa)
  17530. {
  17531. int ipaSz = WOLFSSL_IP4_ADDR_LEN;
  17532. char buf[WOLFSSL_IP6_ADDR_LEN + 1]; /* plus 1 for terminator */
  17533. int af = WOLFSSL_IP4;
  17534. WOLFSSL_ASN1_STRING *ret = NULL;
  17535. if (ipa == NULL)
  17536. return NULL;
  17537. if (XSTRSTR(ipa, ":") != NULL) {
  17538. af = WOLFSSL_IP6;
  17539. ipaSz = WOLFSSL_IP6_ADDR_LEN;
  17540. }
  17541. buf[WOLFSSL_IP6_ADDR_LEN] = '\0';
  17542. if (XINET_PTON(af, ipa, (void*)buf) != 1) {
  17543. WOLFSSL_MSG("Error parsing IP address");
  17544. return NULL;
  17545. }
  17546. ret = wolfSSL_ASN1_STRING_new();
  17547. if (ret != NULL) {
  17548. if (wolfSSL_ASN1_STRING_set(ret, buf, ipaSz) != WOLFSSL_SUCCESS) {
  17549. WOLFSSL_MSG("Error setting the string");
  17550. wolfSSL_ASN1_STRING_free(ret);
  17551. ret = NULL;
  17552. }
  17553. }
  17554. return ret;
  17555. }
  17556. #endif /* !WOLFSSL_USER_IO */
  17557. /* Is the specified cipher suite a fake one used an an extension proxy? */
  17558. static WC_INLINE int SCSV_Check(byte suite0, byte suite)
  17559. {
  17560. (void)suite0;
  17561. (void)suite;
  17562. #ifdef HAVE_RENEGOTIATION_INDICATION
  17563. if (suite0 == CIPHER_BYTE && suite == TLS_EMPTY_RENEGOTIATION_INFO_SCSV)
  17564. return 1;
  17565. #endif
  17566. return 0;
  17567. }
  17568. static WC_INLINE int sslCipherMinMaxCheck(const WOLFSSL *ssl, byte suite0,
  17569. byte suite)
  17570. {
  17571. const CipherSuiteInfo* cipher_names = GetCipherNames();
  17572. int cipherSz = GetCipherNamesSize();
  17573. int i;
  17574. for (i = 0; i < cipherSz; i++)
  17575. if (cipher_names[i].cipherSuite0 == suite0 &&
  17576. cipher_names[i].cipherSuite == suite)
  17577. break;
  17578. if (i == cipherSz)
  17579. return 1;
  17580. /* Check min version */
  17581. if (cipher_names[i].minor < ssl->options.minDowngrade) {
  17582. if (ssl->options.minDowngrade <= TLSv1_2_MINOR &&
  17583. cipher_names[i].minor >= TLSv1_MINOR)
  17584. /* 1.0 ciphersuites are in general available in 1.1 and
  17585. * 1.1 ciphersuites are in general available in 1.2 */
  17586. return 0;
  17587. return 1;
  17588. }
  17589. /* Check max version */
  17590. switch (cipher_names[i].minor) {
  17591. case SSLv3_MINOR :
  17592. return ssl->options.mask & WOLFSSL_OP_NO_SSLv3;
  17593. case TLSv1_MINOR :
  17594. return ssl->options.mask & WOLFSSL_OP_NO_TLSv1;
  17595. case TLSv1_1_MINOR :
  17596. return ssl->options.mask & WOLFSSL_OP_NO_TLSv1_1;
  17597. case TLSv1_2_MINOR :
  17598. return ssl->options.mask & WOLFSSL_OP_NO_TLSv1_2;
  17599. case TLSv1_3_MINOR :
  17600. return ssl->options.mask & WOLFSSL_OP_NO_TLSv1_3;
  17601. default:
  17602. WOLFSSL_MSG("Unrecognized minor version");
  17603. return 1;
  17604. }
  17605. }
  17606. /* returns a pointer to internal cipher suite list. Should not be free'd by
  17607. * caller.
  17608. */
  17609. WOLF_STACK_OF(WOLFSSL_CIPHER) *wolfSSL_get_ciphers_compat(const WOLFSSL *ssl)
  17610. {
  17611. WOLF_STACK_OF(WOLFSSL_CIPHER)* ret = NULL;
  17612. const Suites* suites;
  17613. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  17614. const CipherSuiteInfo* cipher_names = GetCipherNames();
  17615. int cipherSz = GetCipherNamesSize();
  17616. #endif
  17617. WOLFSSL_ENTER("wolfSSL_get_ciphers_compat");
  17618. if (ssl == NULL)
  17619. return NULL;
  17620. suites = WOLFSSL_SUITES(ssl);
  17621. if (suites == NULL)
  17622. return NULL;
  17623. /* check if stack needs populated */
  17624. if (ssl->suitesStack == NULL) {
  17625. int i;
  17626. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  17627. int j;
  17628. /* higher priority of cipher suite will be on top of stack */
  17629. for (i = suites->suiteSz - 2; i >=0; i-=2) {
  17630. #else
  17631. for (i = 0; i < suites->suiteSz; i+=2) {
  17632. #endif
  17633. WOLFSSL_STACK* add;
  17634. /* A couple of suites are placeholders for special options,
  17635. * skip those. */
  17636. if (SCSV_Check(suites->suites[i], suites->suites[i+1])
  17637. || sslCipherMinMaxCheck(ssl, suites->suites[i],
  17638. suites->suites[i+1])) {
  17639. continue;
  17640. }
  17641. add = wolfSSL_sk_new_node(ssl->heap);
  17642. if (add != NULL) {
  17643. add->type = STACK_TYPE_CIPHER;
  17644. add->data.cipher.cipherSuite0 = suites->suites[i];
  17645. add->data.cipher.cipherSuite = suites->suites[i+1];
  17646. add->data.cipher.ssl = ssl;
  17647. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  17648. for (j = 0; j < cipherSz; j++) {
  17649. if (cipher_names[j].cipherSuite0 ==
  17650. add->data.cipher.cipherSuite0 &&
  17651. cipher_names[j].cipherSuite ==
  17652. add->data.cipher.cipherSuite) {
  17653. add->data.cipher.offset = j;
  17654. break;
  17655. }
  17656. }
  17657. #endif
  17658. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  17659. /* in_stack is checked in wolfSSL_CIPHER_description */
  17660. add->data.cipher.in_stack = 1;
  17661. #endif
  17662. add->next = ret;
  17663. if (ret != NULL) {
  17664. add->num = ret->num + 1;
  17665. }
  17666. else {
  17667. add->num = 1;
  17668. }
  17669. ret = add;
  17670. }
  17671. }
  17672. ((WOLFSSL*)ssl)->suitesStack = ret;
  17673. }
  17674. return ssl->suitesStack;
  17675. }
  17676. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
  17677. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  17678. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  17679. defined(HAVE_LIGHTY) || defined(HAVE_SECRET_CALLBACK)
  17680. long wolfSSL_SSL_CTX_get_timeout(const WOLFSSL_CTX *ctx)
  17681. {
  17682. WOLFSSL_ENTER("wolfSSL_SSL_CTX_get_timeout");
  17683. if (ctx == NULL)
  17684. return 0;
  17685. return ctx->timeout;
  17686. }
  17687. /* returns the time in seconds of the current timeout */
  17688. long wolfSSL_get_timeout(WOLFSSL* ssl)
  17689. {
  17690. WOLFSSL_ENTER("wolfSSL_get_timeout");
  17691. if (ssl == NULL)
  17692. return 0;
  17693. return ssl->timeout;
  17694. }
  17695. #endif
  17696. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) \
  17697. || defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY)
  17698. #ifdef HAVE_ECC
  17699. int wolfSSL_SSL_CTX_set_tmp_ecdh(WOLFSSL_CTX *ctx, WOLFSSL_EC_KEY *ecdh)
  17700. {
  17701. WOLFSSL_ENTER("wolfSSL_SSL_CTX_set_tmp_ecdh");
  17702. if (ctx == NULL || ecdh == NULL)
  17703. return BAD_FUNC_ARG;
  17704. ctx->ecdhCurveOID = ecdh->group->curve_oid;
  17705. return WOLFSSL_SUCCESS;
  17706. }
  17707. #endif
  17708. #ifndef NO_BIO
  17709. BIO *wolfSSL_SSL_get_rbio(const WOLFSSL *s)
  17710. {
  17711. WOLFSSL_ENTER("wolfSSL_SSL_get_rbio");
  17712. /* Nginx sets the buffer size if the read BIO is different to write BIO.
  17713. * The setting buffer size doesn't do anything so return NULL for both.
  17714. */
  17715. if (s == NULL)
  17716. return NULL;
  17717. return s->biord;
  17718. }
  17719. BIO *wolfSSL_SSL_get_wbio(const WOLFSSL *s)
  17720. {
  17721. WOLFSSL_ENTER("wolfSSL_SSL_get_wbio");
  17722. (void)s;
  17723. /* Nginx sets the buffer size if the read BIO is different to write BIO.
  17724. * The setting buffer size doesn't do anything so return NULL for both.
  17725. */
  17726. if (s == NULL)
  17727. return NULL;
  17728. return s->biowr;
  17729. }
  17730. #endif /* !NO_BIO */
  17731. int wolfSSL_SSL_do_handshake_internal(WOLFSSL *s)
  17732. {
  17733. WOLFSSL_ENTER("wolfSSL_SSL_do_handshake_internal");
  17734. if (s == NULL)
  17735. return WOLFSSL_FAILURE;
  17736. if (s->options.side == WOLFSSL_CLIENT_END) {
  17737. #ifndef NO_WOLFSSL_CLIENT
  17738. return wolfSSL_connect(s);
  17739. #else
  17740. WOLFSSL_MSG("Client not compiled in");
  17741. return WOLFSSL_FAILURE;
  17742. #endif
  17743. }
  17744. #ifndef NO_WOLFSSL_SERVER
  17745. return wolfSSL_accept(s);
  17746. #else
  17747. WOLFSSL_MSG("Server not compiled in");
  17748. return WOLFSSL_FAILURE;
  17749. #endif
  17750. }
  17751. int wolfSSL_SSL_do_handshake(WOLFSSL *s)
  17752. {
  17753. WOLFSSL_ENTER("wolfSSL_SSL_do_handshake");
  17754. #ifdef WOLFSSL_QUIC
  17755. if (WOLFSSL_IS_QUIC(s)) {
  17756. return wolfSSL_quic_do_handshake(s);
  17757. }
  17758. #endif
  17759. return wolfSSL_SSL_do_handshake_internal(s);
  17760. }
  17761. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  17762. int wolfSSL_SSL_in_init(const WOLFSSL *ssl)
  17763. #else
  17764. int wolfSSL_SSL_in_init(WOLFSSL *ssl)
  17765. #endif
  17766. {
  17767. WOLFSSL_ENTER("wolfSSL_SSL_in_init");
  17768. return !wolfSSL_is_init_finished(ssl);
  17769. }
  17770. int wolfSSL_SSL_in_before(const WOLFSSL *ssl)
  17771. {
  17772. WOLFSSL_ENTER("wolfSSL_SSL_in_before");
  17773. if (ssl == NULL)
  17774. return WOLFSSL_FAILURE;
  17775. return ssl->options.handShakeState == NULL_STATE;
  17776. }
  17777. int wolfSSL_SSL_in_connect_init(WOLFSSL* ssl)
  17778. {
  17779. WOLFSSL_ENTER("wolfSSL_SSL_in_connect_init");
  17780. if (ssl == NULL)
  17781. return WOLFSSL_FAILURE;
  17782. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  17783. return ssl->options.connectState > CONNECT_BEGIN &&
  17784. ssl->options.connectState < SECOND_REPLY_DONE;
  17785. }
  17786. return ssl->options.acceptState > ACCEPT_BEGIN &&
  17787. ssl->options.acceptState < ACCEPT_THIRD_REPLY_DONE;
  17788. }
  17789. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  17790. /* Expected return values from implementations of OpenSSL ticket key callback.
  17791. */
  17792. #define TICKET_KEY_CB_RET_FAILURE (-1)
  17793. #define TICKET_KEY_CB_RET_NOT_FOUND 0
  17794. #define TICKET_KEY_CB_RET_OK 1
  17795. #define TICKET_KEY_CB_RET_RENEW 2
  17796. /* Implementation of session ticket encryption/decryption using OpenSSL
  17797. * callback to initialize the cipher and HMAC.
  17798. *
  17799. * ssl The SSL/TLS object.
  17800. * keyName The key name - used to identify the key to be used.
  17801. * iv The IV to use.
  17802. * mac The MAC of the encrypted data.
  17803. * enc Encrypt ticket.
  17804. * encTicket The ticket data.
  17805. * encTicketLen The length of the ticket data.
  17806. * encLen The encrypted/decrypted ticket length - output length.
  17807. * ctx Ignored. Application specific data.
  17808. * returns WOLFSSL_TICKET_RET_OK to indicate success,
  17809. * WOLFSSL_TICKET_RET_CREATE if a new ticket is required and
  17810. * WOLFSSL_TICKET_RET_FATAL on error.
  17811. */
  17812. static int wolfSSL_TicketKeyCb(WOLFSSL* ssl,
  17813. unsigned char keyName[WOLFSSL_TICKET_NAME_SZ],
  17814. unsigned char iv[WOLFSSL_TICKET_IV_SZ],
  17815. unsigned char mac[WOLFSSL_TICKET_MAC_SZ],
  17816. int enc, unsigned char* encTicket,
  17817. int encTicketLen, int* encLen, void* ctx)
  17818. {
  17819. byte digest[WC_MAX_DIGEST_SIZE];
  17820. #ifdef WOLFSSL_SMALL_STACK
  17821. WOLFSSL_EVP_CIPHER_CTX *evpCtx;
  17822. #else
  17823. WOLFSSL_EVP_CIPHER_CTX evpCtx[1];
  17824. #endif
  17825. WOLFSSL_HMAC_CTX hmacCtx;
  17826. unsigned int mdSz = 0;
  17827. int len = 0;
  17828. int ret = WOLFSSL_TICKET_RET_FATAL;
  17829. int res;
  17830. int totalSz = 0;
  17831. (void)ctx;
  17832. WOLFSSL_ENTER("wolfSSL_TicketKeyCb");
  17833. if (ssl == NULL || ssl->ctx == NULL || ssl->ctx->ticketEncWrapCb == NULL) {
  17834. WOLFSSL_MSG("Bad parameter");
  17835. return WOLFSSL_TICKET_RET_FATAL;
  17836. }
  17837. #ifdef WOLFSSL_SMALL_STACK
  17838. evpCtx = (WOLFSSL_EVP_CIPHER_CTX *)XMALLOC(sizeof(*evpCtx), ssl->heap,
  17839. DYNAMIC_TYPE_TMP_BUFFER);
  17840. if (evpCtx == NULL) {
  17841. WOLFSSL_MSG("out of memory");
  17842. return WOLFSSL_TICKET_RET_FATAL;
  17843. }
  17844. #endif
  17845. /* Initialize the cipher and HMAC. */
  17846. wolfSSL_EVP_CIPHER_CTX_init(evpCtx);
  17847. if (wolfSSL_HMAC_CTX_Init(&hmacCtx) != WOLFSSL_SUCCESS) {
  17848. WOLFSSL_MSG("wolfSSL_HMAC_CTX_Init error");
  17849. #ifdef WOLFSSL_SMALL_STACK
  17850. XFREE(evpCtx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  17851. #endif
  17852. return WOLFSSL_TICKET_RET_FATAL;
  17853. }
  17854. res = ssl->ctx->ticketEncWrapCb(ssl, keyName,
  17855. iv, evpCtx, &hmacCtx, enc);
  17856. if (res != TICKET_KEY_CB_RET_OK && res != TICKET_KEY_CB_RET_RENEW) {
  17857. WOLFSSL_MSG("Ticket callback error");
  17858. ret = WOLFSSL_TICKET_RET_FATAL;
  17859. goto end;
  17860. }
  17861. if (wolfSSL_HMAC_size(&hmacCtx) > WOLFSSL_TICKET_MAC_SZ) {
  17862. WOLFSSL_MSG("Ticket cipher MAC size error");
  17863. goto end;
  17864. }
  17865. if (enc)
  17866. {
  17867. /* Encrypt in place. */
  17868. if (!wolfSSL_EVP_CipherUpdate(evpCtx, encTicket, &len,
  17869. encTicket, encTicketLen))
  17870. goto end;
  17871. totalSz = len;
  17872. if (totalSz > *encLen)
  17873. goto end;
  17874. if (!wolfSSL_EVP_EncryptFinal(evpCtx, &encTicket[len], &len))
  17875. goto end;
  17876. /* Total length of encrypted data. */
  17877. totalSz += len;
  17878. if (totalSz > *encLen)
  17879. goto end;
  17880. /* HMAC the encrypted data into the parameter 'mac'. */
  17881. if (!wolfSSL_HMAC_Update(&hmacCtx, encTicket, totalSz))
  17882. goto end;
  17883. if (!wolfSSL_HMAC_Final(&hmacCtx, mac, &mdSz))
  17884. goto end;
  17885. }
  17886. else
  17887. {
  17888. /* HMAC the encrypted data and compare it to the passed in data. */
  17889. if (!wolfSSL_HMAC_Update(&hmacCtx, encTicket, encTicketLen))
  17890. goto end;
  17891. if (!wolfSSL_HMAC_Final(&hmacCtx, digest, &mdSz))
  17892. goto end;
  17893. if (XMEMCMP(mac, digest, mdSz) != 0)
  17894. goto end;
  17895. /* Decrypt the ticket data in place. */
  17896. if (!wolfSSL_EVP_CipherUpdate(evpCtx, encTicket, &len,
  17897. encTicket, encTicketLen))
  17898. goto end;
  17899. totalSz = len;
  17900. if (totalSz > encTicketLen)
  17901. goto end;
  17902. if (!wolfSSL_EVP_DecryptFinal(evpCtx, &encTicket[len], &len))
  17903. goto end;
  17904. /* Total length of decrypted data. */
  17905. totalSz += len;
  17906. if (totalSz > encTicketLen)
  17907. goto end;
  17908. }
  17909. *encLen = totalSz;
  17910. if (res == TICKET_KEY_CB_RET_RENEW && !IsAtLeastTLSv1_3(ssl->version)
  17911. && !enc)
  17912. ret = WOLFSSL_TICKET_RET_CREATE;
  17913. else
  17914. ret = WOLFSSL_TICKET_RET_OK;
  17915. end:
  17916. (void)wc_HmacFree(&hmacCtx.hmac);
  17917. (void)wolfSSL_EVP_CIPHER_CTX_cleanup(evpCtx);
  17918. #ifdef WOLFSSL_SMALL_STACK
  17919. XFREE(evpCtx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  17920. #endif
  17921. return ret;
  17922. }
  17923. /* Set the callback to use when encrypting/decrypting tickets.
  17924. *
  17925. * ctx The SSL/TLS context object.
  17926. * cb The OpenSSL session ticket callback.
  17927. * returns WOLFSSL_SUCCESS to indicate success.
  17928. */
  17929. int wolfSSL_CTX_set_tlsext_ticket_key_cb(WOLFSSL_CTX *ctx, ticketCompatCb cb)
  17930. {
  17931. /* Set the ticket encryption callback to be a wrapper around OpenSSL
  17932. * callback.
  17933. */
  17934. ctx->ticketEncCb = wolfSSL_TicketKeyCb;
  17935. ctx->ticketEncWrapCb = cb;
  17936. return WOLFSSL_SUCCESS;
  17937. }
  17938. #endif /* HAVE_SESSION_TICKET */
  17939. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  17940. OPENSSL_EXTRA || HAVE_LIGHTY */
  17941. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  17942. !defined(NO_WOLFSSL_SERVER)
  17943. /* Serialize the session ticket encryption keys.
  17944. *
  17945. * @param [in] ctx SSL/TLS context object.
  17946. * @param [in] keys Buffer to hold session ticket keys.
  17947. * @param [in] keylen Length of buffer.
  17948. * @return WOLFSSL_SUCCESS on success.
  17949. * @return WOLFSSL_FAILURE when ctx is NULL, keys is NULL or keylen is not the
  17950. * correct length.
  17951. */
  17952. long wolfSSL_CTX_get_tlsext_ticket_keys(WOLFSSL_CTX *ctx,
  17953. unsigned char *keys, int keylen)
  17954. {
  17955. if (ctx == NULL || keys == NULL) {
  17956. return WOLFSSL_FAILURE;
  17957. }
  17958. if (keylen != WOLFSSL_TICKET_KEYS_SZ) {
  17959. return WOLFSSL_FAILURE;
  17960. }
  17961. XMEMCPY(keys, ctx->ticketKeyCtx.name, WOLFSSL_TICKET_NAME_SZ);
  17962. keys += WOLFSSL_TICKET_NAME_SZ;
  17963. XMEMCPY(keys, ctx->ticketKeyCtx.key[0], WOLFSSL_TICKET_KEY_SZ);
  17964. keys += WOLFSSL_TICKET_KEY_SZ;
  17965. XMEMCPY(keys, ctx->ticketKeyCtx.key[1], WOLFSSL_TICKET_KEY_SZ);
  17966. keys += WOLFSSL_TICKET_KEY_SZ;
  17967. c32toa(ctx->ticketKeyCtx.expirary[0], keys);
  17968. keys += OPAQUE32_LEN;
  17969. c32toa(ctx->ticketKeyCtx.expirary[1], keys);
  17970. return WOLFSSL_SUCCESS;
  17971. }
  17972. /* Deserialize the session ticket encryption keys.
  17973. *
  17974. * @param [in] ctx SSL/TLS context object.
  17975. * @param [in] keys Session ticket keys.
  17976. * @param [in] keylen Length of data.
  17977. * @return WOLFSSL_SUCCESS on success.
  17978. * @return WOLFSSL_FAILURE when ctx is NULL, keys is NULL or keylen is not the
  17979. * correct length.
  17980. */
  17981. long wolfSSL_CTX_set_tlsext_ticket_keys(WOLFSSL_CTX *ctx,
  17982. unsigned char *keys, int keylen)
  17983. {
  17984. if (ctx == NULL || keys == NULL) {
  17985. return WOLFSSL_FAILURE;
  17986. }
  17987. if (keylen != WOLFSSL_TICKET_KEYS_SZ) {
  17988. return WOLFSSL_FAILURE;
  17989. }
  17990. XMEMCPY(ctx->ticketKeyCtx.name, keys, WOLFSSL_TICKET_NAME_SZ);
  17991. keys += WOLFSSL_TICKET_NAME_SZ;
  17992. XMEMCPY(ctx->ticketKeyCtx.key[0], keys, WOLFSSL_TICKET_KEY_SZ);
  17993. keys += WOLFSSL_TICKET_KEY_SZ;
  17994. XMEMCPY(ctx->ticketKeyCtx.key[1], keys, WOLFSSL_TICKET_KEY_SZ);
  17995. keys += WOLFSSL_TICKET_KEY_SZ;
  17996. ato32(keys, &ctx->ticketKeyCtx.expirary[0]);
  17997. keys += OPAQUE32_LEN;
  17998. ato32(keys, &ctx->ticketKeyCtx.expirary[1]);
  17999. return WOLFSSL_SUCCESS;
  18000. }
  18001. #endif
  18002. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  18003. #ifdef HAVE_OCSP
  18004. /* Not an OpenSSL API. */
  18005. int wolfSSL_get_ocsp_response(WOLFSSL* ssl, byte** response)
  18006. {
  18007. *response = ssl->ocspResp;
  18008. return ssl->ocspRespSz;
  18009. }
  18010. /* Not an OpenSSL API. */
  18011. char* wolfSSL_get_ocsp_url(WOLFSSL* ssl)
  18012. {
  18013. return ssl->url;
  18014. }
  18015. /* Not an OpenSSL API. */
  18016. int wolfSSL_set_ocsp_url(WOLFSSL* ssl, char* url)
  18017. {
  18018. if (ssl == NULL)
  18019. return WOLFSSL_FAILURE;
  18020. ssl->url = url;
  18021. return WOLFSSL_SUCCESS;
  18022. }
  18023. #endif /* OCSP */
  18024. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
  18025. #if defined(HAVE_OCSP) && !defined(NO_ASN_TIME)
  18026. int wolfSSL_get_ocsp_producedDate(
  18027. WOLFSSL *ssl,
  18028. byte *producedDate,
  18029. size_t producedDate_space,
  18030. int *producedDateFormat)
  18031. {
  18032. if ((ssl->ocspProducedDateFormat != ASN_UTC_TIME) &&
  18033. (ssl->ocspProducedDateFormat != ASN_GENERALIZED_TIME))
  18034. return BAD_FUNC_ARG;
  18035. if ((producedDate == NULL) || (producedDateFormat == NULL))
  18036. return BAD_FUNC_ARG;
  18037. if (XSTRLEN((char *)ssl->ocspProducedDate) >= producedDate_space)
  18038. return BUFFER_E;
  18039. XSTRNCPY((char *)producedDate, (const char *)ssl->ocspProducedDate,
  18040. producedDate_space);
  18041. *producedDateFormat = ssl->ocspProducedDateFormat;
  18042. return 0;
  18043. }
  18044. int wolfSSL_get_ocsp_producedDate_tm(WOLFSSL *ssl, struct tm *produced_tm) {
  18045. int idx = 0;
  18046. if ((ssl->ocspProducedDateFormat != ASN_UTC_TIME) &&
  18047. (ssl->ocspProducedDateFormat != ASN_GENERALIZED_TIME))
  18048. return BAD_FUNC_ARG;
  18049. if (produced_tm == NULL)
  18050. return BAD_FUNC_ARG;
  18051. if (ExtractDate(ssl->ocspProducedDate,
  18052. (unsigned char)ssl->ocspProducedDateFormat, produced_tm, &idx))
  18053. return 0;
  18054. else
  18055. return ASN_PARSE_E;
  18056. }
  18057. #endif
  18058. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  18059. defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  18060. int wolfSSL_CTX_get_extra_chain_certs(WOLFSSL_CTX* ctx,
  18061. WOLF_STACK_OF(X509)** chain)
  18062. {
  18063. word32 idx;
  18064. word32 length;
  18065. WOLFSSL_STACK* node;
  18066. WOLFSSL_STACK* last = NULL;
  18067. if (ctx == NULL || chain == NULL) {
  18068. chain = NULL;
  18069. return WOLFSSL_FAILURE;
  18070. }
  18071. if (ctx->x509Chain != NULL) {
  18072. *chain = ctx->x509Chain;
  18073. return WOLFSSL_SUCCESS;
  18074. }
  18075. /* If there are no chains then success! */
  18076. *chain = NULL;
  18077. if (ctx->certChain == NULL || ctx->certChain->length == 0) {
  18078. return WOLFSSL_SUCCESS;
  18079. }
  18080. /* Create a new stack of WOLFSSL_X509 object from chain buffer. */
  18081. for (idx = 0; idx < ctx->certChain->length; ) {
  18082. node = wolfSSL_sk_X509_new_null();
  18083. if (node == NULL)
  18084. return WOLFSSL_FAILURE;
  18085. node->next = NULL;
  18086. /* 3 byte length | X509 DER data */
  18087. ato24(ctx->certChain->buffer + idx, &length);
  18088. idx += 3;
  18089. /* Create a new X509 from DER encoded data. */
  18090. node->data.x509 = wolfSSL_X509_d2i_ex(NULL,
  18091. ctx->certChain->buffer + idx, length, ctx->heap);
  18092. if (node->data.x509 == NULL) {
  18093. XFREE(node, NULL, DYNAMIC_TYPE_OPENSSL);
  18094. /* Return as much of the chain as we created. */
  18095. ctx->x509Chain = *chain;
  18096. return WOLFSSL_FAILURE;
  18097. }
  18098. idx += length;
  18099. /* Add object to the end of the stack. */
  18100. if (last == NULL) {
  18101. node->num = 1;
  18102. *chain = node;
  18103. }
  18104. else {
  18105. (*chain)->num++;
  18106. last->next = node;
  18107. }
  18108. last = node;
  18109. }
  18110. ctx->x509Chain = *chain;
  18111. return WOLFSSL_SUCCESS;
  18112. }
  18113. int wolfSSL_CTX_get_tlsext_status_cb(WOLFSSL_CTX* ctx, tlsextStatusCb* cb)
  18114. {
  18115. if (ctx == NULL || ctx->cm == NULL || cb == NULL)
  18116. return WOLFSSL_FAILURE;
  18117. #if !defined(NO_WOLFSSL_SERVER) && (defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  18118. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))
  18119. if (ctx->cm->ocsp_stapling == NULL)
  18120. return WOLFSSL_FAILURE;
  18121. *cb = ctx->cm->ocsp_stapling->statusCb;
  18122. #else
  18123. (void)cb;
  18124. *cb = NULL;
  18125. #endif
  18126. return WOLFSSL_SUCCESS;
  18127. }
  18128. int wolfSSL_CTX_set_tlsext_status_cb(WOLFSSL_CTX* ctx, tlsextStatusCb cb)
  18129. {
  18130. if (ctx == NULL || ctx->cm == NULL)
  18131. return WOLFSSL_FAILURE;
  18132. #if !defined(NO_WOLFSSL_SERVER) && (defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  18133. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))
  18134. /* Ensure stapling is on for callback to be used. */
  18135. wolfSSL_CTX_EnableOCSPStapling(ctx);
  18136. if (ctx->cm->ocsp_stapling == NULL)
  18137. return WOLFSSL_FAILURE;
  18138. ctx->cm->ocsp_stapling->statusCb = cb;
  18139. #else
  18140. (void)cb;
  18141. #endif
  18142. return WOLFSSL_SUCCESS;
  18143. }
  18144. int wolfSSL_CTX_get0_chain_certs(WOLFSSL_CTX *ctx,
  18145. WOLF_STACK_OF(WOLFSSL_X509) **sk)
  18146. {
  18147. WOLFSSL_ENTER("wolfSSL_CTX_get0_chain_certs");
  18148. if (ctx == NULL || sk == NULL) {
  18149. WOLFSSL_MSG("Bad parameter");
  18150. return WOLFSSL_FAILURE;
  18151. }
  18152. /* This function should return ctx->x509Chain if it is populated, otherwise
  18153. it should be populated from ctx->certChain. This matches the behavior of
  18154. wolfSSL_CTX_get_extra_chain_certs, so it is used directly. */
  18155. return wolfSSL_CTX_get_extra_chain_certs(ctx, sk);
  18156. }
  18157. #ifdef KEEP_OUR_CERT
  18158. int wolfSSL_get0_chain_certs(WOLFSSL *ssl,
  18159. WOLF_STACK_OF(WOLFSSL_X509) **sk)
  18160. {
  18161. WOLFSSL_ENTER("wolfSSL_get0_chain_certs");
  18162. if (ssl == NULL || sk == NULL) {
  18163. WOLFSSL_MSG("Bad parameter");
  18164. return WOLFSSL_FAILURE;
  18165. }
  18166. *sk = ssl->ourCertChain;
  18167. return WOLFSSL_SUCCESS;
  18168. }
  18169. #endif
  18170. WOLF_STACK_OF(WOLFSSL_STRING)* wolfSSL_sk_WOLFSSL_STRING_new(void)
  18171. {
  18172. WOLF_STACK_OF(WOLFSSL_STRING)* ret = wolfSSL_sk_new_node(NULL);
  18173. if (ret) {
  18174. ret->type = STACK_TYPE_STRING;
  18175. }
  18176. return ret;
  18177. }
  18178. void wolfSSL_WOLFSSL_STRING_free(WOLFSSL_STRING s)
  18179. {
  18180. WOLFSSL_ENTER("wolfSSL_WOLFSSL_STRING_free");
  18181. if (s != NULL)
  18182. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  18183. }
  18184. void wolfSSL_sk_WOLFSSL_STRING_free(WOLF_STACK_OF(WOLFSSL_STRING)* sk)
  18185. {
  18186. WOLFSSL_STACK* tmp;
  18187. WOLFSSL_ENTER("wolfSSL_sk_WOLFSSL_STRING_free");
  18188. if (sk == NULL)
  18189. return;
  18190. /* parse through stack freeing each node */
  18191. while (sk) {
  18192. tmp = sk->next;
  18193. XFREE(sk->data.string, NULL, DYNAMIC_TYPE_OPENSSL);
  18194. XFREE(sk, NULL, DYNAMIC_TYPE_OPENSSL);
  18195. sk = tmp;
  18196. }
  18197. }
  18198. WOLFSSL_STRING wolfSSL_sk_WOLFSSL_STRING_value(
  18199. WOLF_STACK_OF(WOLFSSL_STRING)* strings, int idx)
  18200. {
  18201. for (; idx > 0 && strings != NULL; idx--)
  18202. strings = strings->next;
  18203. if (strings == NULL)
  18204. return NULL;
  18205. return strings->data.string;
  18206. }
  18207. int wolfSSL_sk_WOLFSSL_STRING_num(WOLF_STACK_OF(WOLFSSL_STRING)* strings)
  18208. {
  18209. if (strings)
  18210. return (int)strings->num;
  18211. return 0;
  18212. }
  18213. #endif /* WOLFSSL_NGINX || WOLFSSL_HAPROXY || OPENSSL_EXTRA || OPENSSL_ALL */
  18214. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  18215. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY) || \
  18216. defined(WOLFSSL_QUIC)
  18217. #ifdef HAVE_ALPN
  18218. void wolfSSL_get0_alpn_selected(const WOLFSSL *ssl, const unsigned char **data,
  18219. unsigned int *len)
  18220. {
  18221. word16 nameLen;
  18222. if (ssl != NULL && data != NULL && len != NULL) {
  18223. TLSX_ALPN_GetRequest(ssl->extensions, (void **)data, &nameLen);
  18224. *len = nameLen;
  18225. }
  18226. }
  18227. int wolfSSL_select_next_proto(unsigned char **out, unsigned char *outLen,
  18228. const unsigned char *in, unsigned int inLen,
  18229. const unsigned char *clientNames,
  18230. unsigned int clientLen)
  18231. {
  18232. unsigned int i, j;
  18233. byte lenIn, lenClient;
  18234. if (out == NULL || outLen == NULL || in == NULL || clientNames == NULL)
  18235. return OPENSSL_NPN_UNSUPPORTED;
  18236. for (i = 0; i < inLen; i += lenIn) {
  18237. lenIn = in[i++];
  18238. for (j = 0; j < clientLen; j += lenClient) {
  18239. lenClient = clientNames[j++];
  18240. if (lenIn != lenClient)
  18241. continue;
  18242. if (XMEMCMP(in + i, clientNames + j, lenIn) == 0) {
  18243. *out = (unsigned char *)(in + i);
  18244. *outLen = lenIn;
  18245. return OPENSSL_NPN_NEGOTIATED;
  18246. }
  18247. }
  18248. }
  18249. *out = (unsigned char *)clientNames + 1;
  18250. *outLen = clientNames[0];
  18251. return OPENSSL_NPN_NO_OVERLAP;
  18252. }
  18253. void wolfSSL_set_alpn_select_cb(WOLFSSL *ssl,
  18254. int (*cb) (WOLFSSL *ssl,
  18255. const unsigned char **out,
  18256. unsigned char *outlen,
  18257. const unsigned char *in,
  18258. unsigned int inlen,
  18259. void *arg), void *arg)
  18260. {
  18261. if (ssl != NULL) {
  18262. ssl->alpnSelect = cb;
  18263. ssl->alpnSelectArg = arg;
  18264. }
  18265. }
  18266. void wolfSSL_CTX_set_alpn_select_cb(WOLFSSL_CTX *ctx,
  18267. int (*cb) (WOLFSSL *ssl,
  18268. const unsigned char **out,
  18269. unsigned char *outlen,
  18270. const unsigned char *in,
  18271. unsigned int inlen,
  18272. void *arg), void *arg)
  18273. {
  18274. if (ctx != NULL) {
  18275. ctx->alpnSelect = cb;
  18276. ctx->alpnSelectArg = arg;
  18277. }
  18278. }
  18279. void wolfSSL_CTX_set_next_protos_advertised_cb(WOLFSSL_CTX *s,
  18280. int (*cb) (WOLFSSL *ssl,
  18281. const unsigned char
  18282. **out,
  18283. unsigned int *outlen,
  18284. void *arg), void *arg)
  18285. {
  18286. (void)s;
  18287. (void)cb;
  18288. (void)arg;
  18289. WOLFSSL_STUB("wolfSSL_CTX_set_next_protos_advertised_cb");
  18290. }
  18291. void wolfSSL_CTX_set_next_proto_select_cb(WOLFSSL_CTX *s,
  18292. int (*cb) (WOLFSSL *ssl,
  18293. unsigned char **out,
  18294. unsigned char *outlen,
  18295. const unsigned char *in,
  18296. unsigned int inlen,
  18297. void *arg), void *arg)
  18298. {
  18299. (void)s;
  18300. (void)cb;
  18301. (void)arg;
  18302. WOLFSSL_STUB("wolfSSL_CTX_set_next_proto_select_cb");
  18303. }
  18304. void wolfSSL_get0_next_proto_negotiated(const WOLFSSL *s,
  18305. const unsigned char **data, unsigned *len)
  18306. {
  18307. (void)s;
  18308. (void)data;
  18309. (void)len;
  18310. WOLFSSL_STUB("wolfSSL_get0_next_proto_negotiated");
  18311. }
  18312. #endif /* HAVE_ALPN */
  18313. #endif /* WOLFSSL_NGINX / WOLFSSL_HAPROXY */
  18314. #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL)
  18315. int wolfSSL_curve_is_disabled(const WOLFSSL* ssl, word16 curve_id)
  18316. {
  18317. if (curve_id >= WOLFSSL_FFDHE_START) {
  18318. /* DH parameters are never disabled. */
  18319. return 0;
  18320. }
  18321. if (curve_id > WOLFSSL_ECC_MAX_AVAIL) {
  18322. WOLFSSL_MSG("Curve id out of supported range");
  18323. /* Disabled if not in valid range. */
  18324. return 1;
  18325. }
  18326. if (curve_id >= 32) {
  18327. /* 0 is for invalid and 1-14 aren't used otherwise. */
  18328. return (ssl->disabledCurves & (1U << (curve_id - 32))) != 0;
  18329. }
  18330. return (ssl->disabledCurves & (1U << curve_id)) != 0;
  18331. }
  18332. #if (defined(HAVE_ECC) || \
  18333. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448))
  18334. static int set_curves_list(WOLFSSL* ssl, WOLFSSL_CTX *ctx, const char* names)
  18335. {
  18336. int idx, start = 0, len, i, ret = WOLFSSL_FAILURE;
  18337. word16 curve;
  18338. word32 disabled;
  18339. char name[MAX_CURVE_NAME_SZ];
  18340. byte groups_len = 0;
  18341. #ifdef WOLFSSL_SMALL_STACK
  18342. void *heap = ssl? ssl->heap : ctx ? ctx->heap : NULL;
  18343. int *groups;
  18344. #else
  18345. int groups[WOLFSSL_MAX_GROUP_COUNT];
  18346. #endif
  18347. #ifdef WOLFSSL_SMALL_STACK
  18348. groups = (int*)XMALLOC(sizeof(int)*WOLFSSL_MAX_GROUP_COUNT,
  18349. heap, DYNAMIC_TYPE_TMP_BUFFER);
  18350. if (groups == NULL) {
  18351. ret = MEMORY_E;
  18352. goto leave;
  18353. }
  18354. #endif
  18355. for (idx = 1; names[idx-1] != '\0'; idx++) {
  18356. if (names[idx] != ':' && names[idx] != '\0')
  18357. continue;
  18358. len = idx - start;
  18359. if (len > MAX_CURVE_NAME_SZ - 1)
  18360. goto leave;
  18361. XMEMCPY(name, names + start, len);
  18362. name[len++] = 0;
  18363. /* Use XSTRNCMP to avoid valgrind error. */
  18364. if ((XSTRNCMP(name, "prime256v1", len) == 0) ||
  18365. (XSTRNCMP(name, "secp256r1", len) == 0) ||
  18366. (XSTRNCMP(name, "P-256", len) == 0))
  18367. {
  18368. curve = WOLFSSL_ECC_SECP256R1;
  18369. }
  18370. else if ((XSTRNCMP(name, "secp384r1", len) == 0) ||
  18371. (XSTRNCMP(name, "P-384", len) == 0))
  18372. {
  18373. curve = WOLFSSL_ECC_SECP384R1;
  18374. }
  18375. else if ((XSTRNCMP(name, "secp521r1", len) == 0) ||
  18376. (XSTRNCMP(name, "P-521", len) == 0))
  18377. {
  18378. curve = WOLFSSL_ECC_SECP521R1;
  18379. }
  18380. #ifdef WOLFSSL_SM2
  18381. else if ((XSTRNCMP(name, "sm2p256v1", len) == 0) ||
  18382. (XSTRNCMP(name, "SM2", len) == 0))
  18383. {
  18384. curve = WOLFSSL_ECC_SM2P256V1;
  18385. }
  18386. #endif
  18387. #ifdef HAVE_CURVE25519
  18388. else if (XSTRNCMP(name, "X25519", len) == 0)
  18389. {
  18390. curve = WOLFSSL_ECC_X25519;
  18391. }
  18392. #endif
  18393. #ifdef HAVE_CURVE448
  18394. else if (XSTRNCMP(name, "X448", len) == 0)
  18395. {
  18396. curve = WOLFSSL_ECC_X448;
  18397. }
  18398. #endif
  18399. else {
  18400. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(HAVE_ECC)
  18401. int nret;
  18402. const ecc_set_type *eccSet;
  18403. nret = wc_ecc_get_curve_idx_from_name(name);
  18404. if (nret < 0) {
  18405. WOLFSSL_MSG("Could not find name in set");
  18406. goto leave;
  18407. }
  18408. eccSet = wc_ecc_get_curve_params(ret);
  18409. if (eccSet == NULL) {
  18410. WOLFSSL_MSG("NULL set returned");
  18411. goto leave;
  18412. }
  18413. curve = GetCurveByOID(eccSet->oidSum);
  18414. #else
  18415. WOLFSSL_MSG("API not present to search farther using name");
  18416. goto leave;
  18417. #endif
  18418. }
  18419. if (curve >= WOLFSSL_ECC_MAX_AVAIL) {
  18420. WOLFSSL_MSG("curve value is not supported");
  18421. goto leave;
  18422. }
  18423. for (i = 0; i < groups_len; ++i) {
  18424. if (groups[i] == curve) {
  18425. /* silently drop duplicates */
  18426. break;
  18427. }
  18428. }
  18429. if (i >= groups_len) {
  18430. if (groups_len >= WOLFSSL_MAX_GROUP_COUNT) {
  18431. WOLFSSL_MSG_EX("setting %d or more supported "
  18432. "curves is not permitted", groups_len);
  18433. goto leave;
  18434. }
  18435. groups[groups_len++] = (int)curve;
  18436. }
  18437. start = idx + 1;
  18438. }
  18439. /* Disable all curves so that only the ones the user wants are enabled. */
  18440. disabled = 0xFFFFFFFFUL;
  18441. for (i = 0; i < groups_len; ++i) {
  18442. /* Switch the bit to off and therefore is enabled. */
  18443. curve = (word16)groups[i];
  18444. if (curve >= 32) {
  18445. /* 0 is for invalid and 1-14 aren't used otherwise. */
  18446. disabled &= ~(1U << (curve - 32));
  18447. }
  18448. else {
  18449. disabled &= ~(1U << curve);
  18450. }
  18451. #ifdef HAVE_SUPPORTED_CURVES
  18452. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_OLD_SET_CURVES_LIST)
  18453. /* using the wolfSSL API to set the groups, this will populate
  18454. * (ssl|ctx)->groups and reset any TLSX_SUPPORTED_GROUPS.
  18455. * The order in (ssl|ctx)->groups will then be respected
  18456. * when TLSX_KEY_SHARE needs to be established */
  18457. if ((ssl && wolfSSL_set_groups(ssl, groups, groups_len)
  18458. != WOLFSSL_SUCCESS)
  18459. || (ctx && wolfSSL_CTX_set_groups(ctx, groups, groups_len)
  18460. != WOLFSSL_SUCCESS)) {
  18461. WOLFSSL_MSG("Unable to set supported curve");
  18462. goto leave;
  18463. }
  18464. #elif !defined(NO_WOLFSSL_CLIENT)
  18465. /* set the supported curve so client TLS extension contains only the
  18466. * desired curves */
  18467. if ((ssl && wolfSSL_UseSupportedCurve(ssl, curve) != WOLFSSL_SUCCESS)
  18468. || (ctx && wolfSSL_CTX_UseSupportedCurve(ctx, curve)
  18469. != WOLFSSL_SUCCESS)) {
  18470. WOLFSSL_MSG("Unable to set supported curve");
  18471. goto leave;
  18472. }
  18473. #endif
  18474. #endif /* HAVE_SUPPORTED_CURVES */
  18475. }
  18476. if (ssl)
  18477. ssl->disabledCurves = disabled;
  18478. else
  18479. ctx->disabledCurves = disabled;
  18480. ret = WOLFSSL_SUCCESS;
  18481. leave:
  18482. #ifdef WOLFSSL_SMALL_STACK
  18483. if (groups)
  18484. XFREE((void*)groups, heap, DYNAMIC_TYPE_TMP_BUFFER);
  18485. #endif
  18486. return ret;
  18487. }
  18488. int wolfSSL_CTX_set1_curves_list(WOLFSSL_CTX* ctx, const char* names)
  18489. {
  18490. if (ctx == NULL || names == NULL) {
  18491. WOLFSSL_MSG("ctx or names was NULL");
  18492. return WOLFSSL_FAILURE;
  18493. }
  18494. return set_curves_list(NULL, ctx, names);
  18495. }
  18496. int wolfSSL_set1_curves_list(WOLFSSL* ssl, const char* names)
  18497. {
  18498. if (ssl == NULL || names == NULL) {
  18499. WOLFSSL_MSG("ssl or names was NULL");
  18500. return WOLFSSL_FAILURE;
  18501. }
  18502. return set_curves_list(ssl, NULL, names);
  18503. }
  18504. #endif /* (HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448) */
  18505. #endif /* OPENSSL_EXTRA || HAVE_CURL */
  18506. #ifdef OPENSSL_EXTRA
  18507. /* Sets a callback for when sending and receiving protocol messages.
  18508. * This callback is copied to all WOLFSSL objects created from the ctx.
  18509. *
  18510. * ctx WOLFSSL_CTX structure to set callback in
  18511. * cb callback to use
  18512. *
  18513. * return WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE with error case
  18514. */
  18515. int wolfSSL_CTX_set_msg_callback(WOLFSSL_CTX *ctx, SSL_Msg_Cb cb)
  18516. {
  18517. WOLFSSL_ENTER("wolfSSL_CTX_set_msg_callback");
  18518. if (ctx == NULL) {
  18519. WOLFSSL_MSG("Null ctx passed in");
  18520. return WOLFSSL_FAILURE;
  18521. }
  18522. ctx->protoMsgCb = cb;
  18523. return WOLFSSL_SUCCESS;
  18524. }
  18525. /* Sets a callback for when sending and receiving protocol messages.
  18526. *
  18527. * ssl WOLFSSL structure to set callback in
  18528. * cb callback to use
  18529. *
  18530. * return WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE with error case
  18531. */
  18532. int wolfSSL_set_msg_callback(WOLFSSL *ssl, SSL_Msg_Cb cb)
  18533. {
  18534. WOLFSSL_ENTER("wolfSSL_set_msg_callback");
  18535. if (ssl == NULL) {
  18536. return WOLFSSL_FAILURE;
  18537. }
  18538. if (cb != NULL) {
  18539. ssl->toInfoOn = 1;
  18540. }
  18541. ssl->protoMsgCb = cb;
  18542. return WOLFSSL_SUCCESS;
  18543. }
  18544. /* set the user argument to pass to the msg callback when called
  18545. * return WOLFSSL_SUCCESS on success */
  18546. int wolfSSL_CTX_set_msg_callback_arg(WOLFSSL_CTX *ctx, void* arg)
  18547. {
  18548. WOLFSSL_ENTER("wolfSSL_CTX_set_msg_callback_arg");
  18549. if (ctx == NULL) {
  18550. WOLFSSL_MSG("Null WOLFSSL_CTX passed in");
  18551. return WOLFSSL_FAILURE;
  18552. }
  18553. ctx->protoMsgCtx = arg;
  18554. return WOLFSSL_SUCCESS;
  18555. }
  18556. int wolfSSL_set_msg_callback_arg(WOLFSSL *ssl, void* arg)
  18557. {
  18558. WOLFSSL_ENTER("wolfSSL_set_msg_callback_arg");
  18559. if (ssl == NULL)
  18560. return WOLFSSL_FAILURE;
  18561. ssl->protoMsgCtx = arg;
  18562. return WOLFSSL_SUCCESS;
  18563. }
  18564. void *wolfSSL_OPENSSL_memdup(const void *data, size_t siz, const char* file,
  18565. int line)
  18566. {
  18567. void *ret;
  18568. (void)file;
  18569. (void)line;
  18570. if (data == NULL || siz >= INT_MAX)
  18571. return NULL;
  18572. ret = OPENSSL_malloc(siz);
  18573. if (ret == NULL) {
  18574. return NULL;
  18575. }
  18576. return XMEMCPY(ret, data, siz);
  18577. }
  18578. void wolfSSL_OPENSSL_cleanse(void *ptr, size_t len)
  18579. {
  18580. if (ptr)
  18581. ForceZero(ptr, (word32)len);
  18582. }
  18583. int wolfSSL_CTX_set_alpn_protos(WOLFSSL_CTX *ctx, const unsigned char *p,
  18584. unsigned int p_len)
  18585. {
  18586. WOLFSSL_ENTER("wolfSSL_CTX_set_alpn_protos");
  18587. if (ctx == NULL)
  18588. return BAD_FUNC_ARG;
  18589. if (ctx->alpn_cli_protos != NULL) {
  18590. XFREE((void*)ctx->alpn_cli_protos, ctx->heap, DYNAMIC_TYPE_OPENSSL);
  18591. }
  18592. ctx->alpn_cli_protos = (const unsigned char*)XMALLOC(p_len,
  18593. ctx->heap, DYNAMIC_TYPE_OPENSSL);
  18594. if (ctx->alpn_cli_protos == NULL) {
  18595. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  18596. /* 0 on success in OpenSSL, non-0 on failure in OpenSSL
  18597. * the function reverses the return value convention.
  18598. */
  18599. return 1;
  18600. #else
  18601. return WOLFSSL_FAILURE;
  18602. #endif
  18603. }
  18604. XMEMCPY((void*)ctx->alpn_cli_protos, p, p_len);
  18605. ctx->alpn_cli_protos_len = p_len;
  18606. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  18607. /* 0 on success in OpenSSL, non-0 on failure in OpenSSL
  18608. * the function reverses the return value convention.
  18609. */
  18610. return 0;
  18611. #else
  18612. return WOLFSSL_SUCCESS;
  18613. #endif
  18614. }
  18615. #ifdef HAVE_ALPN
  18616. #ifndef NO_BIO
  18617. /* Sets the ALPN extension protos
  18618. *
  18619. * example format is
  18620. * unsigned char p[] = {
  18621. * 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  18622. * };
  18623. *
  18624. * returns WOLFSSL_SUCCESS on success */
  18625. int wolfSSL_set_alpn_protos(WOLFSSL* ssl,
  18626. const unsigned char* p, unsigned int p_len)
  18627. {
  18628. WOLFSSL_BIO* bio;
  18629. char* pt = NULL;
  18630. unsigned int sz;
  18631. unsigned int idx = 0;
  18632. int alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
  18633. WOLFSSL_ENTER("wolfSSL_set_alpn_protos");
  18634. if (ssl == NULL || p_len <= 1) {
  18635. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  18636. /* 0 on success in OpenSSL, non-0 on failure in OpenSSL
  18637. * the function reverses the return value convention.
  18638. */
  18639. return 1;
  18640. #else
  18641. return WOLFSSL_FAILURE;
  18642. #endif
  18643. }
  18644. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  18645. if (bio == NULL) {
  18646. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  18647. /* 0 on success in OpenSSL, non-0 on failure in OpenSSL
  18648. * the function reverses the return value convention.
  18649. */
  18650. return 1;
  18651. #else
  18652. return WOLFSSL_FAILURE;
  18653. #endif
  18654. }
  18655. /* convert into comma separated list */
  18656. while (idx < p_len - 1) {
  18657. unsigned int i;
  18658. sz = p[idx++];
  18659. if (idx + sz > p_len) {
  18660. WOLFSSL_MSG("Bad list format");
  18661. wolfSSL_BIO_free(bio);
  18662. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  18663. /* 0 on success in OpenSSL, non-0 on failure in OpenSSL
  18664. * the function reverses the return value convention.
  18665. */
  18666. return 1;
  18667. #else
  18668. return WOLFSSL_FAILURE;
  18669. #endif
  18670. }
  18671. if (sz > 0) {
  18672. for (i = 0; i < sz; i++) {
  18673. wolfSSL_BIO_write(bio, &p[idx++], 1);
  18674. }
  18675. if (idx < p_len - 1)
  18676. wolfSSL_BIO_write(bio, ",", 1);
  18677. }
  18678. }
  18679. wolfSSL_BIO_write(bio, "\0", 1);
  18680. /* clears out all current ALPN extensions set */
  18681. TLSX_Remove(&ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL, ssl->heap);
  18682. if ((sz = wolfSSL_BIO_get_mem_data(bio, &pt)) > 0) {
  18683. wolfSSL_UseALPN(ssl, pt, sz, (byte) alpn_opt);
  18684. }
  18685. wolfSSL_BIO_free(bio);
  18686. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  18687. /* 0 on success in OpenSSL, non-0 on failure in OpenSSL
  18688. * the function reverses the return value convention.
  18689. */
  18690. return 0;
  18691. #else
  18692. return WOLFSSL_SUCCESS;
  18693. #endif
  18694. }
  18695. #endif /* !NO_BIO */
  18696. #endif /* HAVE_ALPN */
  18697. #endif /* OPENSSL_EXTRA */
  18698. #if defined(OPENSSL_EXTRA)
  18699. #ifndef NO_BIO
  18700. #define WOLFSSL_BIO_INCLUDED
  18701. #include "src/bio.c"
  18702. #endif
  18703. word32 nid2oid(int nid, int grp)
  18704. {
  18705. /* get OID type */
  18706. switch (grp) {
  18707. /* oidHashType */
  18708. case oidHashType:
  18709. switch (nid) {
  18710. #ifdef WOLFSSL_MD2
  18711. case NID_md2:
  18712. return MD2h;
  18713. #endif
  18714. #ifndef NO_MD5
  18715. case NID_md5:
  18716. return MD5h;
  18717. #endif
  18718. #ifndef NO_SHA
  18719. case NID_sha1:
  18720. return SHAh;
  18721. #endif
  18722. case NID_sha224:
  18723. return SHA224h;
  18724. #ifndef NO_SHA256
  18725. case NID_sha256:
  18726. return SHA256h;
  18727. #endif
  18728. #ifdef WOLFSSL_SHA384
  18729. case NID_sha384:
  18730. return SHA384h;
  18731. #endif
  18732. #ifdef WOLFSSL_SHA512
  18733. case NID_sha512:
  18734. return SHA512h;
  18735. #endif
  18736. #ifndef WOLFSSL_NOSHA3_224
  18737. case NID_sha3_224:
  18738. return SHA3_224h;
  18739. #endif
  18740. #ifndef WOLFSSL_NOSHA3_256
  18741. case NID_sha3_256:
  18742. return SHA3_256h;
  18743. #endif
  18744. #ifndef WOLFSSL_NOSHA3_384
  18745. case NID_sha3_384:
  18746. return SHA3_384h;
  18747. #endif
  18748. #ifndef WOLFSSL_NOSHA3_512
  18749. case NID_sha3_512:
  18750. return SHA3_512h;
  18751. #endif
  18752. }
  18753. break;
  18754. /* oidSigType */
  18755. case oidSigType:
  18756. switch (nid) {
  18757. #ifndef NO_DSA
  18758. case NID_dsaWithSHA1:
  18759. return CTC_SHAwDSA;
  18760. case NID_dsa_with_SHA256:
  18761. return CTC_SHA256wDSA;
  18762. #endif /* NO_DSA */
  18763. #ifndef NO_RSA
  18764. case NID_md2WithRSAEncryption:
  18765. return CTC_MD2wRSA;
  18766. case NID_md5WithRSAEncryption:
  18767. return CTC_MD5wRSA;
  18768. case NID_sha1WithRSAEncryption:
  18769. return CTC_SHAwRSA;
  18770. case NID_sha224WithRSAEncryption:
  18771. return CTC_SHA224wRSA;
  18772. case NID_sha256WithRSAEncryption:
  18773. return CTC_SHA256wRSA;
  18774. case NID_sha384WithRSAEncryption:
  18775. return CTC_SHA384wRSA;
  18776. case NID_sha512WithRSAEncryption:
  18777. return CTC_SHA512wRSA;
  18778. #ifdef WOLFSSL_SHA3
  18779. case NID_RSA_SHA3_224:
  18780. return CTC_SHA3_224wRSA;
  18781. case NID_RSA_SHA3_256:
  18782. return CTC_SHA3_256wRSA;
  18783. case NID_RSA_SHA3_384:
  18784. return CTC_SHA3_384wRSA;
  18785. case NID_RSA_SHA3_512:
  18786. return CTC_SHA3_512wRSA;
  18787. #endif
  18788. #endif /* NO_RSA */
  18789. #ifdef HAVE_ECC
  18790. case NID_ecdsa_with_SHA1:
  18791. return CTC_SHAwECDSA;
  18792. case NID_ecdsa_with_SHA224:
  18793. return CTC_SHA224wECDSA;
  18794. case NID_ecdsa_with_SHA256:
  18795. return CTC_SHA256wECDSA;
  18796. case NID_ecdsa_with_SHA384:
  18797. return CTC_SHA384wECDSA;
  18798. case NID_ecdsa_with_SHA512:
  18799. return CTC_SHA512wECDSA;
  18800. #ifdef WOLFSSL_SHA3
  18801. case NID_ecdsa_with_SHA3_224:
  18802. return CTC_SHA3_224wECDSA;
  18803. case NID_ecdsa_with_SHA3_256:
  18804. return CTC_SHA3_256wECDSA;
  18805. case NID_ecdsa_with_SHA3_384:
  18806. return CTC_SHA3_384wECDSA;
  18807. case NID_ecdsa_with_SHA3_512:
  18808. return CTC_SHA3_512wECDSA;
  18809. #endif
  18810. #endif /* HAVE_ECC */
  18811. }
  18812. break;
  18813. /* oidKeyType */
  18814. case oidKeyType:
  18815. switch (nid) {
  18816. #ifndef NO_DSA
  18817. case NID_dsa:
  18818. return DSAk;
  18819. #endif /* NO_DSA */
  18820. #ifndef NO_RSA
  18821. case NID_rsaEncryption:
  18822. return RSAk;
  18823. #endif /* NO_RSA */
  18824. #ifdef HAVE_ECC
  18825. case NID_X9_62_id_ecPublicKey:
  18826. return ECDSAk;
  18827. #endif /* HAVE_ECC */
  18828. }
  18829. break;
  18830. #ifdef HAVE_ECC
  18831. case oidCurveType:
  18832. switch (nid) {
  18833. case NID_X9_62_prime192v1:
  18834. return ECC_SECP192R1_OID;
  18835. case NID_X9_62_prime192v2:
  18836. return ECC_PRIME192V2_OID;
  18837. case NID_X9_62_prime192v3:
  18838. return ECC_PRIME192V3_OID;
  18839. case NID_X9_62_prime239v1:
  18840. return ECC_PRIME239V1_OID;
  18841. case NID_X9_62_prime239v2:
  18842. return ECC_PRIME239V2_OID;
  18843. case NID_X9_62_prime239v3:
  18844. return ECC_PRIME239V3_OID;
  18845. case NID_X9_62_prime256v1:
  18846. return ECC_SECP256R1_OID;
  18847. case NID_secp112r1:
  18848. return ECC_SECP112R1_OID;
  18849. case NID_secp112r2:
  18850. return ECC_SECP112R2_OID;
  18851. case NID_secp128r1:
  18852. return ECC_SECP128R1_OID;
  18853. case NID_secp128r2:
  18854. return ECC_SECP128R2_OID;
  18855. case NID_secp160r1:
  18856. return ECC_SECP160R1_OID;
  18857. case NID_secp160r2:
  18858. return ECC_SECP160R2_OID;
  18859. case NID_secp224r1:
  18860. return ECC_SECP224R1_OID;
  18861. case NID_secp384r1:
  18862. return ECC_SECP384R1_OID;
  18863. case NID_secp521r1:
  18864. return ECC_SECP521R1_OID;
  18865. case NID_secp160k1:
  18866. return ECC_SECP160K1_OID;
  18867. case NID_secp192k1:
  18868. return ECC_SECP192K1_OID;
  18869. case NID_secp224k1:
  18870. return ECC_SECP224K1_OID;
  18871. case NID_secp256k1:
  18872. return ECC_SECP256K1_OID;
  18873. case NID_brainpoolP160r1:
  18874. return ECC_BRAINPOOLP160R1_OID;
  18875. case NID_brainpoolP192r1:
  18876. return ECC_BRAINPOOLP192R1_OID;
  18877. case NID_brainpoolP224r1:
  18878. return ECC_BRAINPOOLP224R1_OID;
  18879. case NID_brainpoolP256r1:
  18880. return ECC_BRAINPOOLP256R1_OID;
  18881. case NID_brainpoolP320r1:
  18882. return ECC_BRAINPOOLP320R1_OID;
  18883. case NID_brainpoolP384r1:
  18884. return ECC_BRAINPOOLP384R1_OID;
  18885. case NID_brainpoolP512r1:
  18886. return ECC_BRAINPOOLP512R1_OID;
  18887. }
  18888. break;
  18889. #endif /* HAVE_ECC */
  18890. /* oidBlkType */
  18891. case oidBlkType:
  18892. switch (nid) {
  18893. #ifdef WOLFSSL_AES_128
  18894. case AES128CBCb:
  18895. return AES128CBCb;
  18896. #endif
  18897. #ifdef WOLFSSL_AES_192
  18898. case AES192CBCb:
  18899. return AES192CBCb;
  18900. #endif
  18901. #ifdef WOLFSSL_AES_256
  18902. case AES256CBCb:
  18903. return AES256CBCb;
  18904. #endif
  18905. #ifndef NO_DES3
  18906. case NID_des:
  18907. return DESb;
  18908. case NID_des3:
  18909. return DES3b;
  18910. #endif
  18911. }
  18912. break;
  18913. #ifdef HAVE_OCSP
  18914. case oidOcspType:
  18915. switch (nid) {
  18916. case NID_id_pkix_OCSP_basic:
  18917. return OCSP_BASIC_OID;
  18918. case OCSP_NONCE_OID:
  18919. return OCSP_NONCE_OID;
  18920. }
  18921. break;
  18922. #endif /* HAVE_OCSP */
  18923. /* oidCertExtType */
  18924. case oidCertExtType:
  18925. switch (nid) {
  18926. case NID_basic_constraints:
  18927. return BASIC_CA_OID;
  18928. case NID_subject_alt_name:
  18929. return ALT_NAMES_OID;
  18930. case NID_crl_distribution_points:
  18931. return CRL_DIST_OID;
  18932. case NID_info_access:
  18933. return AUTH_INFO_OID;
  18934. case NID_authority_key_identifier:
  18935. return AUTH_KEY_OID;
  18936. case NID_subject_key_identifier:
  18937. return SUBJ_KEY_OID;
  18938. case NID_inhibit_any_policy:
  18939. return INHIBIT_ANY_OID;
  18940. case NID_key_usage:
  18941. return KEY_USAGE_OID;
  18942. case NID_name_constraints:
  18943. return NAME_CONS_OID;
  18944. case NID_certificate_policies:
  18945. return CERT_POLICY_OID;
  18946. case NID_ext_key_usage:
  18947. return EXT_KEY_USAGE_OID;
  18948. }
  18949. break;
  18950. /* oidCertAuthInfoType */
  18951. case oidCertAuthInfoType:
  18952. switch (nid) {
  18953. case NID_ad_OCSP:
  18954. return AIA_OCSP_OID;
  18955. case NID_ad_ca_issuers:
  18956. return AIA_CA_ISSUER_OID;
  18957. }
  18958. break;
  18959. /* oidCertPolicyType */
  18960. case oidCertPolicyType:
  18961. switch (nid) {
  18962. case NID_any_policy:
  18963. return CP_ANY_OID;
  18964. }
  18965. break;
  18966. /* oidCertAltNameType */
  18967. case oidCertAltNameType:
  18968. switch (nid) {
  18969. case NID_hw_name_oid:
  18970. return HW_NAME_OID;
  18971. }
  18972. break;
  18973. /* oidCertKeyUseType */
  18974. case oidCertKeyUseType:
  18975. switch (nid) {
  18976. case NID_anyExtendedKeyUsage:
  18977. return EKU_ANY_OID;
  18978. case EKU_SERVER_AUTH_OID:
  18979. return EKU_SERVER_AUTH_OID;
  18980. case EKU_CLIENT_AUTH_OID:
  18981. return EKU_CLIENT_AUTH_OID;
  18982. case EKU_OCSP_SIGN_OID:
  18983. return EKU_OCSP_SIGN_OID;
  18984. }
  18985. break;
  18986. /* oidKdfType */
  18987. case oidKdfType:
  18988. switch (nid) {
  18989. case PBKDF2_OID:
  18990. return PBKDF2_OID;
  18991. }
  18992. break;
  18993. /* oidPBEType */
  18994. case oidPBEType:
  18995. switch (nid) {
  18996. case PBE_SHA1_RC4_128:
  18997. return PBE_SHA1_RC4_128;
  18998. case PBE_SHA1_DES:
  18999. return PBE_SHA1_DES;
  19000. case PBE_SHA1_DES3:
  19001. return PBE_SHA1_DES3;
  19002. }
  19003. break;
  19004. /* oidKeyWrapType */
  19005. case oidKeyWrapType:
  19006. switch (nid) {
  19007. #ifdef WOLFSSL_AES_128
  19008. case AES128_WRAP:
  19009. return AES128_WRAP;
  19010. #endif
  19011. #ifdef WOLFSSL_AES_192
  19012. case AES192_WRAP:
  19013. return AES192_WRAP;
  19014. #endif
  19015. #ifdef WOLFSSL_AES_256
  19016. case AES256_WRAP:
  19017. return AES256_WRAP;
  19018. #endif
  19019. }
  19020. break;
  19021. /* oidCmsKeyAgreeType */
  19022. case oidCmsKeyAgreeType:
  19023. switch (nid) {
  19024. #ifndef NO_SHA
  19025. case dhSinglePass_stdDH_sha1kdf_scheme:
  19026. return dhSinglePass_stdDH_sha1kdf_scheme;
  19027. #endif
  19028. #ifdef WOLFSSL_SHA224
  19029. case dhSinglePass_stdDH_sha224kdf_scheme:
  19030. return dhSinglePass_stdDH_sha224kdf_scheme;
  19031. #endif
  19032. #ifndef NO_SHA256
  19033. case dhSinglePass_stdDH_sha256kdf_scheme:
  19034. return dhSinglePass_stdDH_sha256kdf_scheme;
  19035. #endif
  19036. #ifdef WOLFSSL_SHA384
  19037. case dhSinglePass_stdDH_sha384kdf_scheme:
  19038. return dhSinglePass_stdDH_sha384kdf_scheme;
  19039. #endif
  19040. #ifdef WOLFSSL_SHA512
  19041. case dhSinglePass_stdDH_sha512kdf_scheme:
  19042. return dhSinglePass_stdDH_sha512kdf_scheme;
  19043. #endif
  19044. }
  19045. break;
  19046. /* oidCmsKeyAgreeType */
  19047. #ifdef WOLFSSL_CERT_REQ
  19048. case oidCsrAttrType:
  19049. switch (nid) {
  19050. case NID_pkcs9_contentType:
  19051. return PKCS9_CONTENT_TYPE_OID;
  19052. case NID_pkcs9_challengePassword:
  19053. return CHALLENGE_PASSWORD_OID;
  19054. case NID_serialNumber:
  19055. return SERIAL_NUMBER_OID;
  19056. case NID_userId:
  19057. return USER_ID_OID;
  19058. case NID_surname:
  19059. return SURNAME_OID;
  19060. }
  19061. break;
  19062. #endif
  19063. default:
  19064. WOLFSSL_MSG("NID not in table");
  19065. /* MSVC warns without the cast */
  19066. return (word32)-1;
  19067. }
  19068. /* MSVC warns without the cast */
  19069. return (word32)-1;
  19070. }
  19071. int oid2nid(word32 oid, int grp)
  19072. {
  19073. size_t i;
  19074. /* get OID type */
  19075. switch (grp) {
  19076. /* oidHashType */
  19077. case oidHashType:
  19078. switch (oid) {
  19079. #ifdef WOLFSSL_MD2
  19080. case MD2h:
  19081. return NID_md2;
  19082. #endif
  19083. #ifndef NO_MD5
  19084. case MD5h:
  19085. return NID_md5;
  19086. #endif
  19087. #ifndef NO_SHA
  19088. case SHAh:
  19089. return NID_sha1;
  19090. #endif
  19091. case SHA224h:
  19092. return NID_sha224;
  19093. #ifndef NO_SHA256
  19094. case SHA256h:
  19095. return NID_sha256;
  19096. #endif
  19097. #ifdef WOLFSSL_SHA384
  19098. case SHA384h:
  19099. return NID_sha384;
  19100. #endif
  19101. #ifdef WOLFSSL_SHA512
  19102. case SHA512h:
  19103. return NID_sha512;
  19104. #endif
  19105. }
  19106. break;
  19107. /* oidSigType */
  19108. case oidSigType:
  19109. switch (oid) {
  19110. #ifndef NO_DSA
  19111. case CTC_SHAwDSA:
  19112. return NID_dsaWithSHA1;
  19113. case CTC_SHA256wDSA:
  19114. return NID_dsa_with_SHA256;
  19115. #endif /* NO_DSA */
  19116. #ifndef NO_RSA
  19117. case CTC_MD2wRSA:
  19118. return NID_md2WithRSAEncryption;
  19119. case CTC_MD5wRSA:
  19120. return NID_md5WithRSAEncryption;
  19121. case CTC_SHAwRSA:
  19122. return NID_sha1WithRSAEncryption;
  19123. case CTC_SHA224wRSA:
  19124. return NID_sha224WithRSAEncryption;
  19125. case CTC_SHA256wRSA:
  19126. return NID_sha256WithRSAEncryption;
  19127. case CTC_SHA384wRSA:
  19128. return NID_sha384WithRSAEncryption;
  19129. case CTC_SHA512wRSA:
  19130. return NID_sha512WithRSAEncryption;
  19131. #ifdef WOLFSSL_SHA3
  19132. case CTC_SHA3_224wRSA:
  19133. return NID_RSA_SHA3_224;
  19134. case CTC_SHA3_256wRSA:
  19135. return NID_RSA_SHA3_256;
  19136. case CTC_SHA3_384wRSA:
  19137. return NID_RSA_SHA3_384;
  19138. case CTC_SHA3_512wRSA:
  19139. return NID_RSA_SHA3_512;
  19140. #endif
  19141. #ifdef WC_RSA_PSS
  19142. case CTC_RSASSAPSS:
  19143. return NID_rsassaPss;
  19144. #endif
  19145. #endif /* NO_RSA */
  19146. #ifdef HAVE_ECC
  19147. case CTC_SHAwECDSA:
  19148. return NID_ecdsa_with_SHA1;
  19149. case CTC_SHA224wECDSA:
  19150. return NID_ecdsa_with_SHA224;
  19151. case CTC_SHA256wECDSA:
  19152. return NID_ecdsa_with_SHA256;
  19153. case CTC_SHA384wECDSA:
  19154. return NID_ecdsa_with_SHA384;
  19155. case CTC_SHA512wECDSA:
  19156. return NID_ecdsa_with_SHA512;
  19157. #ifdef WOLFSSL_SHA3
  19158. case CTC_SHA3_224wECDSA:
  19159. return NID_ecdsa_with_SHA3_224;
  19160. case CTC_SHA3_256wECDSA:
  19161. return NID_ecdsa_with_SHA3_256;
  19162. case CTC_SHA3_384wECDSA:
  19163. return NID_ecdsa_with_SHA3_384;
  19164. case CTC_SHA3_512wECDSA:
  19165. return NID_ecdsa_with_SHA3_512;
  19166. #endif
  19167. #endif /* HAVE_ECC */
  19168. }
  19169. break;
  19170. /* oidKeyType */
  19171. case oidKeyType:
  19172. switch (oid) {
  19173. #ifndef NO_DSA
  19174. case DSAk:
  19175. return NID_dsa;
  19176. #endif /* NO_DSA */
  19177. #ifndef NO_RSA
  19178. case RSAk:
  19179. return NID_rsaEncryption;
  19180. #ifdef WC_RSA_PSS
  19181. case RSAPSSk:
  19182. return NID_rsassaPss;
  19183. #endif
  19184. #endif /* NO_RSA */
  19185. #ifdef HAVE_ECC
  19186. case ECDSAk:
  19187. return NID_X9_62_id_ecPublicKey;
  19188. #endif /* HAVE_ECC */
  19189. }
  19190. break;
  19191. #ifdef HAVE_ECC
  19192. case oidCurveType:
  19193. switch (oid) {
  19194. case ECC_SECP192R1_OID:
  19195. return NID_X9_62_prime192v1;
  19196. case ECC_PRIME192V2_OID:
  19197. return NID_X9_62_prime192v2;
  19198. case ECC_PRIME192V3_OID:
  19199. return NID_X9_62_prime192v3;
  19200. case ECC_PRIME239V1_OID:
  19201. return NID_X9_62_prime239v1;
  19202. case ECC_PRIME239V2_OID:
  19203. return NID_X9_62_prime239v2;
  19204. case ECC_PRIME239V3_OID:
  19205. return NID_X9_62_prime239v3;
  19206. case ECC_SECP256R1_OID:
  19207. return NID_X9_62_prime256v1;
  19208. case ECC_SECP112R1_OID:
  19209. return NID_secp112r1;
  19210. case ECC_SECP112R2_OID:
  19211. return NID_secp112r2;
  19212. case ECC_SECP128R1_OID:
  19213. return NID_secp128r1;
  19214. case ECC_SECP128R2_OID:
  19215. return NID_secp128r2;
  19216. case ECC_SECP160R1_OID:
  19217. return NID_secp160r1;
  19218. case ECC_SECP160R2_OID:
  19219. return NID_secp160r2;
  19220. case ECC_SECP224R1_OID:
  19221. return NID_secp224r1;
  19222. case ECC_SECP384R1_OID:
  19223. return NID_secp384r1;
  19224. case ECC_SECP521R1_OID:
  19225. return NID_secp521r1;
  19226. case ECC_SECP160K1_OID:
  19227. return NID_secp160k1;
  19228. case ECC_SECP192K1_OID:
  19229. return NID_secp192k1;
  19230. case ECC_SECP224K1_OID:
  19231. return NID_secp224k1;
  19232. case ECC_SECP256K1_OID:
  19233. return NID_secp256k1;
  19234. case ECC_BRAINPOOLP160R1_OID:
  19235. return NID_brainpoolP160r1;
  19236. case ECC_BRAINPOOLP192R1_OID:
  19237. return NID_brainpoolP192r1;
  19238. case ECC_BRAINPOOLP224R1_OID:
  19239. return NID_brainpoolP224r1;
  19240. case ECC_BRAINPOOLP256R1_OID:
  19241. return NID_brainpoolP256r1;
  19242. case ECC_BRAINPOOLP320R1_OID:
  19243. return NID_brainpoolP320r1;
  19244. case ECC_BRAINPOOLP384R1_OID:
  19245. return NID_brainpoolP384r1;
  19246. case ECC_BRAINPOOLP512R1_OID:
  19247. return NID_brainpoolP512r1;
  19248. }
  19249. break;
  19250. #endif /* HAVE_ECC */
  19251. /* oidBlkType */
  19252. case oidBlkType:
  19253. switch (oid) {
  19254. #ifdef WOLFSSL_AES_128
  19255. case AES128CBCb:
  19256. return AES128CBCb;
  19257. #endif
  19258. #ifdef WOLFSSL_AES_192
  19259. case AES192CBCb:
  19260. return AES192CBCb;
  19261. #endif
  19262. #ifdef WOLFSSL_AES_256
  19263. case AES256CBCb:
  19264. return AES256CBCb;
  19265. #endif
  19266. #ifndef NO_DES3
  19267. case DESb:
  19268. return NID_des;
  19269. case DES3b:
  19270. return NID_des3;
  19271. #endif
  19272. }
  19273. break;
  19274. #ifdef HAVE_OCSP
  19275. case oidOcspType:
  19276. switch (oid) {
  19277. case OCSP_BASIC_OID:
  19278. return NID_id_pkix_OCSP_basic;
  19279. case OCSP_NONCE_OID:
  19280. return OCSP_NONCE_OID;
  19281. }
  19282. break;
  19283. #endif /* HAVE_OCSP */
  19284. /* oidCertExtType */
  19285. case oidCertExtType:
  19286. switch (oid) {
  19287. case BASIC_CA_OID:
  19288. return NID_basic_constraints;
  19289. case ALT_NAMES_OID:
  19290. return NID_subject_alt_name;
  19291. case CRL_DIST_OID:
  19292. return NID_crl_distribution_points;
  19293. case AUTH_INFO_OID:
  19294. return NID_info_access;
  19295. case AUTH_KEY_OID:
  19296. return NID_authority_key_identifier;
  19297. case SUBJ_KEY_OID:
  19298. return NID_subject_key_identifier;
  19299. case INHIBIT_ANY_OID:
  19300. return NID_inhibit_any_policy;
  19301. case KEY_USAGE_OID:
  19302. return NID_key_usage;
  19303. case NAME_CONS_OID:
  19304. return NID_name_constraints;
  19305. case CERT_POLICY_OID:
  19306. return NID_certificate_policies;
  19307. case EXT_KEY_USAGE_OID:
  19308. return NID_ext_key_usage;
  19309. }
  19310. break;
  19311. /* oidCertAuthInfoType */
  19312. case oidCertAuthInfoType:
  19313. switch (oid) {
  19314. case AIA_OCSP_OID:
  19315. return NID_ad_OCSP;
  19316. case AIA_CA_ISSUER_OID:
  19317. return NID_ad_ca_issuers;
  19318. }
  19319. break;
  19320. /* oidCertPolicyType */
  19321. case oidCertPolicyType:
  19322. switch (oid) {
  19323. case CP_ANY_OID:
  19324. return NID_any_policy;
  19325. }
  19326. break;
  19327. /* oidCertAltNameType */
  19328. case oidCertAltNameType:
  19329. switch (oid) {
  19330. case HW_NAME_OID:
  19331. return NID_hw_name_oid;
  19332. }
  19333. break;
  19334. /* oidCertKeyUseType */
  19335. case oidCertKeyUseType:
  19336. switch (oid) {
  19337. case EKU_ANY_OID:
  19338. return NID_anyExtendedKeyUsage;
  19339. case EKU_SERVER_AUTH_OID:
  19340. return EKU_SERVER_AUTH_OID;
  19341. case EKU_CLIENT_AUTH_OID:
  19342. return EKU_CLIENT_AUTH_OID;
  19343. case EKU_OCSP_SIGN_OID:
  19344. return EKU_OCSP_SIGN_OID;
  19345. }
  19346. break;
  19347. /* oidKdfType */
  19348. case oidKdfType:
  19349. switch (oid) {
  19350. case PBKDF2_OID:
  19351. return PBKDF2_OID;
  19352. }
  19353. break;
  19354. /* oidPBEType */
  19355. case oidPBEType:
  19356. switch (oid) {
  19357. case PBE_SHA1_RC4_128:
  19358. return PBE_SHA1_RC4_128;
  19359. case PBE_SHA1_DES:
  19360. return PBE_SHA1_DES;
  19361. case PBE_SHA1_DES3:
  19362. return PBE_SHA1_DES3;
  19363. }
  19364. break;
  19365. /* oidKeyWrapType */
  19366. case oidKeyWrapType:
  19367. switch (oid) {
  19368. #ifdef WOLFSSL_AES_128
  19369. case AES128_WRAP:
  19370. return AES128_WRAP;
  19371. #endif
  19372. #ifdef WOLFSSL_AES_192
  19373. case AES192_WRAP:
  19374. return AES192_WRAP;
  19375. #endif
  19376. #ifdef WOLFSSL_AES_256
  19377. case AES256_WRAP:
  19378. return AES256_WRAP;
  19379. #endif
  19380. }
  19381. break;
  19382. /* oidCmsKeyAgreeType */
  19383. case oidCmsKeyAgreeType:
  19384. switch (oid) {
  19385. #ifndef NO_SHA
  19386. case dhSinglePass_stdDH_sha1kdf_scheme:
  19387. return dhSinglePass_stdDH_sha1kdf_scheme;
  19388. #endif
  19389. #ifdef WOLFSSL_SHA224
  19390. case dhSinglePass_stdDH_sha224kdf_scheme:
  19391. return dhSinglePass_stdDH_sha224kdf_scheme;
  19392. #endif
  19393. #ifndef NO_SHA256
  19394. case dhSinglePass_stdDH_sha256kdf_scheme:
  19395. return dhSinglePass_stdDH_sha256kdf_scheme;
  19396. #endif
  19397. #ifdef WOLFSSL_SHA384
  19398. case dhSinglePass_stdDH_sha384kdf_scheme:
  19399. return dhSinglePass_stdDH_sha384kdf_scheme;
  19400. #endif
  19401. #ifdef WOLFSSL_SHA512
  19402. case dhSinglePass_stdDH_sha512kdf_scheme:
  19403. return dhSinglePass_stdDH_sha512kdf_scheme;
  19404. #endif
  19405. }
  19406. break;
  19407. #ifdef WOLFSSL_CERT_REQ
  19408. case oidCsrAttrType:
  19409. switch (oid) {
  19410. case PKCS9_CONTENT_TYPE_OID:
  19411. return NID_pkcs9_contentType;
  19412. case CHALLENGE_PASSWORD_OID:
  19413. return NID_pkcs9_challengePassword;
  19414. case SERIAL_NUMBER_OID:
  19415. return NID_serialNumber;
  19416. case USER_ID_OID:
  19417. return NID_userId;
  19418. }
  19419. break;
  19420. #endif
  19421. default:
  19422. WOLFSSL_MSG("OID not in table");
  19423. }
  19424. /* If not found in above switch then try the table */
  19425. for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++) {
  19426. if (wolfssl_object_info[i].id == (int)oid) {
  19427. return wolfssl_object_info[i].nid;
  19428. }
  19429. }
  19430. return -1;
  19431. }
  19432. /* frees all nodes in the current threads error queue
  19433. *
  19434. * id thread id. ERR_remove_state is depreciated and id is ignored. The
  19435. * current threads queue will be free'd.
  19436. */
  19437. void wolfSSL_ERR_remove_state(unsigned long id)
  19438. {
  19439. WOLFSSL_ENTER("wolfSSL_ERR_remove_state");
  19440. (void)id;
  19441. if (wc_ERR_remove_state() != 0) {
  19442. WOLFSSL_MSG("Error with removing the state");
  19443. }
  19444. }
  19445. #endif /* OPENSSL_EXTRA */
  19446. #ifdef OPENSSL_ALL
  19447. #if !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  19448. static int bio_get_data(WOLFSSL_BIO* bio, byte** data)
  19449. {
  19450. int ret = 0;
  19451. byte* mem = NULL;
  19452. ret = wolfSSL_BIO_get_len(bio);
  19453. if (ret > 0) {
  19454. mem = (byte*)XMALLOC(ret, bio->heap, DYNAMIC_TYPE_OPENSSL);
  19455. if (mem == NULL) {
  19456. WOLFSSL_MSG("Memory error");
  19457. ret = MEMORY_E;
  19458. }
  19459. if (ret >= 0) {
  19460. if ((ret = wolfSSL_BIO_read(bio, mem, ret)) <= 0) {
  19461. XFREE(mem, bio->heap, DYNAMIC_TYPE_OPENSSL);
  19462. ret = MEMORY_E;
  19463. mem = NULL;
  19464. }
  19465. }
  19466. }
  19467. *data = mem;
  19468. return ret;
  19469. }
  19470. /* DER data is PKCS#8 encrypted. */
  19471. WOLFSSL_EVP_PKEY* wolfSSL_d2i_PKCS8PrivateKey_bio(WOLFSSL_BIO* bio,
  19472. WOLFSSL_EVP_PKEY** pkey,
  19473. wc_pem_password_cb* cb,
  19474. void* ctx)
  19475. {
  19476. int ret;
  19477. byte* der;
  19478. int len;
  19479. byte* p;
  19480. word32 algId;
  19481. WOLFSSL_EVP_PKEY* key;
  19482. if ((len = bio_get_data(bio, &der)) < 0)
  19483. return NULL;
  19484. if (cb != NULL) {
  19485. char password[NAME_SZ];
  19486. int passwordSz = cb(password, sizeof(password), PEM_PASS_READ, ctx);
  19487. if (passwordSz < 0) {
  19488. XFREE(der, bio->heap, DYNAMIC_TYPE_OPENSSL);
  19489. return NULL;
  19490. }
  19491. #ifdef WOLFSSL_CHECK_MEM_ZERO
  19492. wc_MemZero_Add("wolfSSL_d2i_PKCS8PrivateKey_bio password", password,
  19493. passwordSz);
  19494. #endif
  19495. ret = ToTraditionalEnc(der, len, password, passwordSz, &algId);
  19496. if (ret < 0) {
  19497. XFREE(der, bio->heap, DYNAMIC_TYPE_OPENSSL);
  19498. return NULL;
  19499. }
  19500. ForceZero(password, passwordSz);
  19501. #ifdef WOLFSSL_CHECK_MEM_ZERO
  19502. wc_MemZero_Check(password, passwordSz);
  19503. #endif
  19504. }
  19505. p = der;
  19506. key = wolfSSL_d2i_PrivateKey_EVP(pkey, &p, len);
  19507. XFREE(der, bio->heap, DYNAMIC_TYPE_OPENSSL);
  19508. return key;
  19509. }
  19510. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  19511. /* Detect which type of key it is before decoding. */
  19512. WOLFSSL_EVP_PKEY* wolfSSL_d2i_AutoPrivateKey(WOLFSSL_EVP_PKEY** pkey,
  19513. const unsigned char** pp,
  19514. long length)
  19515. {
  19516. int ret;
  19517. WOLFSSL_EVP_PKEY* key = NULL;
  19518. const byte* der = *pp;
  19519. word32 idx = 0;
  19520. int len = 0;
  19521. int cnt = 0;
  19522. word32 algId;
  19523. word32 keyLen = (word32)length;
  19524. /* Take off PKCS#8 wrapper if found. */
  19525. if ((len = ToTraditionalInline_ex(der, &idx, keyLen, &algId)) >= 0) {
  19526. der += idx;
  19527. keyLen = len;
  19528. }
  19529. idx = 0;
  19530. len = 0;
  19531. /* Use the number of elements in the outer sequence to determine key type.
  19532. */
  19533. ret = GetSequence(der, &idx, &len, keyLen);
  19534. if (ret >= 0) {
  19535. word32 end = idx + len;
  19536. while (ret >= 0 && idx < end) {
  19537. /* Skip type */
  19538. idx++;
  19539. /* Get length and skip over - keeping count */
  19540. len = 0;
  19541. ret = GetLength(der, &idx, &len, keyLen);
  19542. if (ret >= 0) {
  19543. if (idx + len > end)
  19544. ret = ASN_PARSE_E;
  19545. else {
  19546. idx += len;
  19547. cnt++;
  19548. }
  19549. }
  19550. }
  19551. }
  19552. if (ret >= 0) {
  19553. int type;
  19554. /* ECC includes version, private[, curve][, public key] */
  19555. if (cnt >= 2 && cnt <= 4)
  19556. type = EVP_PKEY_EC;
  19557. else
  19558. type = EVP_PKEY_RSA;
  19559. key = wolfSSL_d2i_PrivateKey(type, pkey, &der, keyLen);
  19560. *pp = der;
  19561. }
  19562. return key;
  19563. }
  19564. #endif /* OPENSSL_ALL */
  19565. #ifdef WOLFSSL_STATIC_EPHEMERAL
  19566. int wolfSSL_StaticEphemeralKeyLoad(WOLFSSL* ssl, int keyAlgo, void* keyPtr)
  19567. {
  19568. int ret;
  19569. word32 idx = 0;
  19570. DerBuffer* der = NULL;
  19571. if (ssl == NULL || ssl->ctx == NULL || keyPtr == NULL) {
  19572. return BAD_FUNC_ARG;
  19573. }
  19574. #ifndef SINGLE_THREADED
  19575. if (!ssl->ctx->staticKELockInit) {
  19576. return BUFFER_E; /* no keys set */
  19577. }
  19578. ret = wc_LockMutex(&ssl->ctx->staticKELock);
  19579. if (ret != 0) {
  19580. return ret;
  19581. }
  19582. #endif
  19583. ret = BUFFER_E; /* set default error */
  19584. switch (keyAlgo) {
  19585. #ifndef NO_DH
  19586. case WC_PK_TYPE_DH:
  19587. if (ssl != NULL)
  19588. der = ssl->staticKE.dhKey;
  19589. if (der == NULL)
  19590. der = ssl->ctx->staticKE.dhKey;
  19591. if (der != NULL) {
  19592. DhKey* key = (DhKey*)keyPtr;
  19593. WOLFSSL_MSG("Using static DH key");
  19594. ret = wc_DhKeyDecode(der->buffer, &idx, key, der->length);
  19595. }
  19596. break;
  19597. #endif
  19598. #ifdef HAVE_ECC
  19599. case WC_PK_TYPE_ECDH:
  19600. if (ssl != NULL)
  19601. der = ssl->staticKE.ecKey;
  19602. if (der == NULL)
  19603. der = ssl->ctx->staticKE.ecKey;
  19604. if (der != NULL) {
  19605. ecc_key* key = (ecc_key*)keyPtr;
  19606. WOLFSSL_MSG("Using static ECDH key");
  19607. ret = wc_EccPrivateKeyDecode(der->buffer, &idx, key,
  19608. der->length);
  19609. }
  19610. break;
  19611. #endif
  19612. #ifdef HAVE_CURVE25519
  19613. case WC_PK_TYPE_CURVE25519:
  19614. if (ssl != NULL)
  19615. der = ssl->staticKE.x25519Key;
  19616. if (der == NULL)
  19617. der = ssl->ctx->staticKE.x25519Key;
  19618. if (der != NULL) {
  19619. curve25519_key* key = (curve25519_key*)keyPtr;
  19620. WOLFSSL_MSG("Using static X25519 key");
  19621. ret = wc_Curve25519PrivateKeyDecode(der->buffer, &idx, key,
  19622. der->length);
  19623. }
  19624. break;
  19625. #endif
  19626. #ifdef HAVE_CURVE448
  19627. case WC_PK_TYPE_CURVE448:
  19628. if (ssl != NULL)
  19629. der = ssl->staticKE.x448Key;
  19630. if (der == NULL)
  19631. der = ssl->ctx->staticKE.x448Key;
  19632. if (der != NULL) {
  19633. curve448_key* key = (curve448_key*)keyPtr;
  19634. WOLFSSL_MSG("Using static X448 key");
  19635. ret = wc_Curve448PrivateKeyDecode(der->buffer, &idx, key,
  19636. der->length);
  19637. }
  19638. break;
  19639. #endif
  19640. default:
  19641. /* not supported */
  19642. ret = NOT_COMPILED_IN;
  19643. break;
  19644. }
  19645. #ifndef SINGLE_THREADED
  19646. wc_UnLockMutex(&ssl->ctx->staticKELock);
  19647. #endif
  19648. return ret;
  19649. }
  19650. static int SetStaticEphemeralKey(WOLFSSL_CTX* ctx,
  19651. StaticKeyExchangeInfo_t* staticKE, int keyAlgo, const char* key,
  19652. unsigned int keySz, int format, void* heap)
  19653. {
  19654. int ret = 0;
  19655. DerBuffer* der = NULL;
  19656. byte* keyBuf = NULL;
  19657. #ifndef NO_FILESYSTEM
  19658. const char* keyFile = NULL;
  19659. #endif
  19660. /* allow empty key to free buffer */
  19661. if (staticKE == NULL || (key == NULL && keySz > 0)) {
  19662. return BAD_FUNC_ARG;
  19663. }
  19664. WOLFSSL_ENTER("SetStaticEphemeralKey");
  19665. /* if just free'ing key then skip loading */
  19666. if (key != NULL) {
  19667. #ifndef NO_FILESYSTEM
  19668. /* load file from filesystem */
  19669. if (key != NULL && keySz == 0) {
  19670. size_t keyBufSz = 0;
  19671. keyFile = (const char*)key;
  19672. ret = wc_FileLoad(keyFile, &keyBuf, &keyBufSz, heap);
  19673. if (ret != 0) {
  19674. return ret;
  19675. }
  19676. keySz = (unsigned int)keyBufSz;
  19677. }
  19678. else
  19679. #endif
  19680. {
  19681. /* use as key buffer directly */
  19682. keyBuf = (byte*)key;
  19683. }
  19684. if (format == WOLFSSL_FILETYPE_PEM) {
  19685. #ifdef WOLFSSL_PEM_TO_DER
  19686. int keyFormat = 0;
  19687. ret = PemToDer(keyBuf, keySz, PRIVATEKEY_TYPE, &der,
  19688. heap, NULL, &keyFormat);
  19689. /* auto detect key type */
  19690. if (ret == 0 && keyAlgo == WC_PK_TYPE_NONE) {
  19691. if (keyFormat == ECDSAk)
  19692. keyAlgo = WC_PK_TYPE_ECDH;
  19693. else if (keyFormat == X25519k)
  19694. keyAlgo = WC_PK_TYPE_CURVE25519;
  19695. else
  19696. keyAlgo = WC_PK_TYPE_DH;
  19697. }
  19698. #else
  19699. ret = NOT_COMPILED_IN;
  19700. #endif
  19701. }
  19702. else {
  19703. /* Detect PK type (if required) */
  19704. #ifdef HAVE_ECC
  19705. if (keyAlgo == WC_PK_TYPE_NONE) {
  19706. word32 idx = 0;
  19707. ecc_key eccKey;
  19708. ret = wc_ecc_init_ex(&eccKey, heap, INVALID_DEVID);
  19709. if (ret == 0) {
  19710. ret = wc_EccPrivateKeyDecode(keyBuf, &idx, &eccKey, keySz);
  19711. if (ret == 0)
  19712. keyAlgo = WC_PK_TYPE_ECDH;
  19713. wc_ecc_free(&eccKey);
  19714. }
  19715. }
  19716. #endif
  19717. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  19718. if (keyAlgo == WC_PK_TYPE_NONE) {
  19719. word32 idx = 0;
  19720. DhKey dhKey;
  19721. ret = wc_InitDhKey_ex(&dhKey, heap, INVALID_DEVID);
  19722. if (ret == 0) {
  19723. ret = wc_DhKeyDecode(keyBuf, &idx, &dhKey, keySz);
  19724. if (ret == 0)
  19725. keyAlgo = WC_PK_TYPE_DH;
  19726. wc_FreeDhKey(&dhKey);
  19727. }
  19728. }
  19729. #endif
  19730. #ifdef HAVE_CURVE25519
  19731. if (keyAlgo == WC_PK_TYPE_NONE) {
  19732. word32 idx = 0;
  19733. curve25519_key x25519Key;
  19734. ret = wc_curve25519_init_ex(&x25519Key, heap, INVALID_DEVID);
  19735. if (ret == 0) {
  19736. ret = wc_Curve25519PrivateKeyDecode(keyBuf, &idx,
  19737. &x25519Key, keySz);
  19738. if (ret == 0)
  19739. keyAlgo = WC_PK_TYPE_CURVE25519;
  19740. wc_curve25519_free(&x25519Key);
  19741. }
  19742. }
  19743. #endif
  19744. #ifdef HAVE_CURVE448
  19745. if (keyAlgo == WC_PK_TYPE_NONE) {
  19746. word32 idx = 0;
  19747. curve448_key x448Key;
  19748. ret = wc_curve448_init(&x448Key);
  19749. if (ret == 0) {
  19750. ret = wc_Curve448PrivateKeyDecode(keyBuf, &idx, &x448Key,
  19751. keySz);
  19752. if (ret == 0)
  19753. keyAlgo = WC_PK_TYPE_CURVE448;
  19754. wc_curve448_free(&x448Key);
  19755. }
  19756. }
  19757. #endif
  19758. if (keyAlgo != WC_PK_TYPE_NONE) {
  19759. ret = AllocDer(&der, keySz, PRIVATEKEY_TYPE, heap);
  19760. if (ret == 0) {
  19761. XMEMCPY(der->buffer, keyBuf, keySz);
  19762. }
  19763. }
  19764. }
  19765. }
  19766. #ifndef NO_FILESYSTEM
  19767. /* done with keyFile buffer */
  19768. if (keyFile && keyBuf) {
  19769. XFREE(keyBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  19770. }
  19771. #endif
  19772. #ifndef SINGLE_THREADED
  19773. if (ret == 0 && !ctx->staticKELockInit) {
  19774. ret = wc_InitMutex(&ctx->staticKELock);
  19775. if (ret == 0) {
  19776. ctx->staticKELockInit = 1;
  19777. }
  19778. }
  19779. #endif
  19780. if (ret == 0
  19781. #ifndef SINGLE_THREADED
  19782. && (ret = wc_LockMutex(&ctx->staticKELock)) == 0
  19783. #endif
  19784. ) {
  19785. switch (keyAlgo) {
  19786. #ifndef NO_DH
  19787. case WC_PK_TYPE_DH:
  19788. FreeDer(&staticKE->dhKey);
  19789. staticKE->dhKey = der; der = NULL;
  19790. break;
  19791. #endif
  19792. #ifdef HAVE_ECC
  19793. case WC_PK_TYPE_ECDH:
  19794. FreeDer(&staticKE->ecKey);
  19795. staticKE->ecKey = der; der = NULL;
  19796. break;
  19797. #endif
  19798. #ifdef HAVE_CURVE25519
  19799. case WC_PK_TYPE_CURVE25519:
  19800. FreeDer(&staticKE->x25519Key);
  19801. staticKE->x25519Key = der; der = NULL;
  19802. break;
  19803. #endif
  19804. #ifdef HAVE_CURVE448
  19805. case WC_PK_TYPE_CURVE448:
  19806. FreeDer(&staticKE->x448Key);
  19807. staticKE->x448Key = der; der = NULL;
  19808. break;
  19809. #endif
  19810. default:
  19811. /* not supported */
  19812. ret = NOT_COMPILED_IN;
  19813. break;
  19814. }
  19815. #ifndef SINGLE_THREADED
  19816. wc_UnLockMutex(&ctx->staticKELock);
  19817. #endif
  19818. }
  19819. if (ret != 0) {
  19820. FreeDer(&der);
  19821. }
  19822. (void)ctx; /* not used for single threaded */
  19823. WOLFSSL_LEAVE("SetStaticEphemeralKey", ret);
  19824. return ret;
  19825. }
  19826. int wolfSSL_CTX_set_ephemeral_key(WOLFSSL_CTX* ctx, int keyAlgo,
  19827. const char* key, unsigned int keySz, int format)
  19828. {
  19829. if (ctx == NULL) {
  19830. return BAD_FUNC_ARG;
  19831. }
  19832. return SetStaticEphemeralKey(ctx, &ctx->staticKE, keyAlgo,
  19833. key, keySz, format, ctx->heap);
  19834. }
  19835. int wolfSSL_set_ephemeral_key(WOLFSSL* ssl, int keyAlgo,
  19836. const char* key, unsigned int keySz, int format)
  19837. {
  19838. if (ssl == NULL || ssl->ctx == NULL) {
  19839. return BAD_FUNC_ARG;
  19840. }
  19841. return SetStaticEphemeralKey(ssl->ctx, &ssl->staticKE, keyAlgo,
  19842. key, keySz, format, ssl->heap);
  19843. }
  19844. static int GetStaticEphemeralKey(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  19845. int keyAlgo, const unsigned char** key, unsigned int* keySz)
  19846. {
  19847. int ret = 0;
  19848. DerBuffer* der = NULL;
  19849. if (key) *key = NULL;
  19850. if (keySz) *keySz = 0;
  19851. #ifndef SINGLE_THREADED
  19852. if (ctx->staticKELockInit &&
  19853. (ret = wc_LockMutex(&ctx->staticKELock)) != 0) {
  19854. return ret;
  19855. }
  19856. #endif
  19857. switch (keyAlgo) {
  19858. #ifndef NO_DH
  19859. case WC_PK_TYPE_DH:
  19860. if (ssl != NULL)
  19861. der = ssl->staticKE.dhKey;
  19862. if (der == NULL)
  19863. der = ctx->staticKE.dhKey;
  19864. break;
  19865. #endif
  19866. #ifdef HAVE_ECC
  19867. case WC_PK_TYPE_ECDH:
  19868. if (ssl != NULL)
  19869. der = ssl->staticKE.ecKey;
  19870. if (der == NULL)
  19871. der = ctx->staticKE.ecKey;
  19872. break;
  19873. #endif
  19874. #ifdef HAVE_CURVE25519
  19875. case WC_PK_TYPE_CURVE25519:
  19876. if (ssl != NULL)
  19877. der = ssl->staticKE.x25519Key;
  19878. if (der == NULL)
  19879. der = ctx->staticKE.x25519Key;
  19880. break;
  19881. #endif
  19882. #ifdef HAVE_CURVE448
  19883. case WC_PK_TYPE_CURVE448:
  19884. if (ssl != NULL)
  19885. der = ssl->staticKE.x448Key;
  19886. if (der == NULL)
  19887. der = ctx->staticKE.x448Key;
  19888. break;
  19889. #endif
  19890. default:
  19891. /* not supported */
  19892. ret = NOT_COMPILED_IN;
  19893. break;
  19894. }
  19895. if (der) {
  19896. if (key)
  19897. *key = der->buffer;
  19898. if (keySz)
  19899. *keySz = der->length;
  19900. }
  19901. #ifndef SINGLE_THREADED
  19902. wc_UnLockMutex(&ctx->staticKELock);
  19903. #endif
  19904. return ret;
  19905. }
  19906. /* returns pointer to currently loaded static ephemeral as ASN.1 */
  19907. /* this can be converted to PEM using wc_DerToPem */
  19908. int wolfSSL_CTX_get_ephemeral_key(WOLFSSL_CTX* ctx, int keyAlgo,
  19909. const unsigned char** key, unsigned int* keySz)
  19910. {
  19911. if (ctx == NULL) {
  19912. return BAD_FUNC_ARG;
  19913. }
  19914. return GetStaticEphemeralKey(ctx, NULL, keyAlgo, key, keySz);
  19915. }
  19916. int wolfSSL_get_ephemeral_key(WOLFSSL* ssl, int keyAlgo,
  19917. const unsigned char** key, unsigned int* keySz)
  19918. {
  19919. if (ssl == NULL || ssl->ctx == NULL) {
  19920. return BAD_FUNC_ARG;
  19921. }
  19922. return GetStaticEphemeralKey(ssl->ctx, ssl, keyAlgo, key, keySz);
  19923. }
  19924. #endif /* WOLFSSL_STATIC_EPHEMERAL */
  19925. #if defined(OPENSSL_EXTRA)
  19926. /* wolfSSL_THREADID_current is provided as a compat API with
  19927. * CRYPTO_THREADID_current to register current thread id into given id object.
  19928. * However, CRYPTO_THREADID_current API has been deprecated and no longer
  19929. * exists in the OpenSSL 1.0.0 or later.This API only works as a stub
  19930. * like as existing wolfSSL_THREADID_set_numeric.
  19931. */
  19932. void wolfSSL_THREADID_current(WOLFSSL_CRYPTO_THREADID* id)
  19933. {
  19934. (void)id;
  19935. return;
  19936. }
  19937. /* wolfSSL_THREADID_hash is provided as a compatible API with
  19938. * CRYPTO_THREADID_hash which returns a hash value calculated from the
  19939. * specified thread id. However, CRYPTO_THREADID_hash API has been
  19940. * deprecated and no longer exists in the OpenSSL 1.0.0 or later.
  19941. * This API only works as a stub to returns 0. This behavior is
  19942. * equivalent to the latest OpenSSL CRYPTO_THREADID_hash.
  19943. */
  19944. unsigned long wolfSSL_THREADID_hash(const WOLFSSL_CRYPTO_THREADID* id)
  19945. {
  19946. (void)id;
  19947. return 0UL;
  19948. }
  19949. /* wolfSSL_CTX_set_ecdh_auto is provided as compatible API with
  19950. * SSL_CTX_set_ecdh_auto to enable auto ecdh curve selection functionality.
  19951. * Since this functionality is enabled by default in wolfSSL,
  19952. * this API exists as a stub.
  19953. */
  19954. int wolfSSL_CTX_set_ecdh_auto(WOLFSSL_CTX* ctx, int onoff)
  19955. {
  19956. (void)ctx;
  19957. (void)onoff;
  19958. return WOLFSSL_SUCCESS;
  19959. }
  19960. /**
  19961. * set security level (wolfSSL doesn't support security level)
  19962. * @param ctx a pointer to WOLFSSL_EVP_PKEY_CTX structure
  19963. * @param level security level
  19964. */
  19965. void wolfSSL_CTX_set_security_level(WOLFSSL_CTX* ctx, int level)
  19966. {
  19967. WOLFSSL_ENTER("wolfSSL_CTX_set_security_level");
  19968. (void)ctx;
  19969. (void)level;
  19970. }
  19971. /**
  19972. * get security level (wolfSSL doesn't support security level)
  19973. * @param ctx a pointer to WOLFSSL_EVP_PKEY_CTX structure
  19974. * @return always 0(level 0)
  19975. */
  19976. int wolfSSL_CTX_get_security_level(const WOLFSSL_CTX* ctx)
  19977. {
  19978. WOLFSSL_ENTER("wolfSSL_CTX_get_security_level");
  19979. (void)ctx;
  19980. return 0;
  19981. }
  19982. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  19983. /*
  19984. * This API accepts a user callback which puts key-log records into
  19985. * a KEY LOGFILE. The callback is stored into a CTX and propagated to
  19986. * each SSL object on its creation timing.
  19987. */
  19988. void wolfSSL_CTX_set_keylog_callback(WOLFSSL_CTX* ctx,
  19989. wolfSSL_CTX_keylog_cb_func cb)
  19990. {
  19991. WOLFSSL_ENTER("wolfSSL_CTX_set_keylog_callback");
  19992. /* stores the callback into WOLFSSL_CTX */
  19993. if (ctx != NULL) {
  19994. ctx->keyLogCb = cb;
  19995. }
  19996. }
  19997. wolfSSL_CTX_keylog_cb_func wolfSSL_CTX_get_keylog_callback(
  19998. const WOLFSSL_CTX* ctx)
  19999. {
  20000. WOLFSSL_ENTER("wolfSSL_CTX_get_keylog_callback");
  20001. if (ctx != NULL)
  20002. return ctx->keyLogCb;
  20003. else
  20004. return NULL;
  20005. }
  20006. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  20007. #endif /* OPENSSL_EXTRA */
  20008. #ifndef NO_CERT
  20009. #define WOLFSSL_X509_INCLUDED
  20010. #include "src/x509.c"
  20011. #endif
  20012. /*******************************************************************************
  20013. * START OF standard C library wrapping APIs
  20014. ******************************************************************************/
  20015. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  20016. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  20017. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  20018. defined(WOLFSSL_OPENSSH)))
  20019. #ifndef NO_WOLFSSL_STUB
  20020. int wolfSSL_CRYPTO_set_mem_ex_functions(void *(*m) (size_t, const char *, int),
  20021. void *(*r) (void *, size_t, const char *,
  20022. int), void (*f) (void *))
  20023. {
  20024. (void) m;
  20025. (void) r;
  20026. (void) f;
  20027. WOLFSSL_ENTER("wolfSSL_CRYPTO_set_mem_ex_functions");
  20028. WOLFSSL_STUB("CRYPTO_set_mem_ex_functions");
  20029. return WOLFSSL_FAILURE;
  20030. }
  20031. #endif
  20032. #endif
  20033. #if defined(OPENSSL_EXTRA)
  20034. /**
  20035. * free allocated memory resource
  20036. * @param str a pointer to resource to be freed
  20037. * @param file dummy argument
  20038. * @param line dummy argument
  20039. */
  20040. void wolfSSL_CRYPTO_free(void *str, const char *file, int line)
  20041. {
  20042. (void)file;
  20043. (void)line;
  20044. XFREE(str, 0, DYNAMIC_TYPE_TMP_BUFFER);
  20045. }
  20046. /**
  20047. * allocate memory with size of num
  20048. * @param num size of memory allocation to be malloced
  20049. * @param file dummy argument
  20050. * @param line dummy argument
  20051. * @return a pointer to allocated memory on succssesful, otherwise NULL
  20052. */
  20053. void *wolfSSL_CRYPTO_malloc(size_t num, const char *file, int line)
  20054. {
  20055. (void)file;
  20056. (void)line;
  20057. return XMALLOC(num, 0, DYNAMIC_TYPE_TMP_BUFFER);
  20058. }
  20059. #endif
  20060. /*******************************************************************************
  20061. * END OF standard C library wrapping APIs
  20062. ******************************************************************************/
  20063. /*******************************************************************************
  20064. * START OF EX_DATA APIs
  20065. ******************************************************************************/
  20066. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  20067. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  20068. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  20069. defined(WOLFSSL_OPENSSH)))
  20070. void wolfSSL_CRYPTO_cleanup_all_ex_data(void){
  20071. WOLFSSL_ENTER("CRYPTO_cleanup_all_ex_data");
  20072. }
  20073. #endif
  20074. #ifdef HAVE_EX_DATA
  20075. void* wolfSSL_CRYPTO_get_ex_data(const WOLFSSL_CRYPTO_EX_DATA* ex_data, int idx)
  20076. {
  20077. WOLFSSL_ENTER("wolfSSL_CTX_get_ex_data");
  20078. #ifdef MAX_EX_DATA
  20079. if(ex_data && idx < MAX_EX_DATA && idx >= 0) {
  20080. return ex_data->ex_data[idx];
  20081. }
  20082. #else
  20083. (void)ex_data;
  20084. (void)idx;
  20085. #endif
  20086. return NULL;
  20087. }
  20088. int wolfSSL_CRYPTO_set_ex_data(WOLFSSL_CRYPTO_EX_DATA* ex_data, int idx,
  20089. void *data)
  20090. {
  20091. WOLFSSL_ENTER("wolfSSL_CRYPTO_set_ex_data");
  20092. #ifdef MAX_EX_DATA
  20093. if (ex_data && idx < MAX_EX_DATA && idx >= 0) {
  20094. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  20095. if (ex_data->ex_data_cleanup_routines[idx]) {
  20096. if (ex_data->ex_data[idx])
  20097. ex_data->ex_data_cleanup_routines[idx](ex_data->ex_data[idx]);
  20098. ex_data->ex_data_cleanup_routines[idx] = NULL;
  20099. }
  20100. #endif
  20101. ex_data->ex_data[idx] = data;
  20102. return WOLFSSL_SUCCESS;
  20103. }
  20104. #else
  20105. (void)ex_data;
  20106. (void)idx;
  20107. (void)data;
  20108. #endif
  20109. return WOLFSSL_FAILURE;
  20110. }
  20111. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  20112. int wolfSSL_CRYPTO_set_ex_data_with_cleanup(
  20113. WOLFSSL_CRYPTO_EX_DATA* ex_data,
  20114. int idx,
  20115. void *data,
  20116. wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
  20117. {
  20118. WOLFSSL_ENTER("wolfSSL_CRYPTO_set_ex_data_with_cleanup");
  20119. if (ex_data && idx < MAX_EX_DATA && idx >= 0) {
  20120. if (ex_data->ex_data_cleanup_routines[idx] && ex_data->ex_data[idx])
  20121. ex_data->ex_data_cleanup_routines[idx](ex_data->ex_data[idx]);
  20122. ex_data->ex_data[idx] = data;
  20123. ex_data->ex_data_cleanup_routines[idx] = cleanup_routine;
  20124. return WOLFSSL_SUCCESS;
  20125. }
  20126. return WOLFSSL_FAILURE;
  20127. }
  20128. #endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
  20129. /**
  20130. * Issues unique index for the class specified by class_index.
  20131. * Other parameter except class_index are ignored.
  20132. * Currently, following class_index are accepted:
  20133. * - WOLF_CRYPTO_EX_INDEX_SSL
  20134. * - WOLF_CRYPTO_EX_INDEX_SSL_CTX
  20135. * - WOLF_CRYPTO_EX_INDEX_X509
  20136. * @param class_index index one of CRYPTO_EX_INDEX_xxx
  20137. * @param argp parameters to be saved
  20138. * @param argl parameters to be saved
  20139. * @param new_func a pointer to WOLFSSL_CRYPTO_EX_new
  20140. * @param dup_func a pointer to WOLFSSL_CRYPTO_EX_dup
  20141. * @param free_func a pointer to WOLFSSL_CRYPTO_EX_free
  20142. * @return index value grater or equal to zero on success, -1 on failure.
  20143. */
  20144. int wolfSSL_CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
  20145. WOLFSSL_CRYPTO_EX_new* new_func,
  20146. WOLFSSL_CRYPTO_EX_dup* dup_func,
  20147. WOLFSSL_CRYPTO_EX_free* free_func)
  20148. {
  20149. WOLFSSL_ENTER("wolfSSL_CRYPTO_get_ex_new_index");
  20150. return wolfssl_get_ex_new_index(class_index, argl, argp, new_func,
  20151. dup_func, free_func);
  20152. }
  20153. #endif /* HAVE_EX_DATA */
  20154. /*******************************************************************************
  20155. * END OF EX_DATA APIs
  20156. ******************************************************************************/
  20157. /*******************************************************************************
  20158. * START OF BUF_MEM API
  20159. ******************************************************************************/
  20160. #if defined(OPENSSL_EXTRA)
  20161. /* Begin functions for openssl/buffer.h */
  20162. WOLFSSL_BUF_MEM* wolfSSL_BUF_MEM_new(void)
  20163. {
  20164. WOLFSSL_BUF_MEM* buf;
  20165. buf = (WOLFSSL_BUF_MEM*)XMALLOC(sizeof(WOLFSSL_BUF_MEM), NULL,
  20166. DYNAMIC_TYPE_OPENSSL);
  20167. if (buf) {
  20168. XMEMSET(buf, 0, sizeof(WOLFSSL_BUF_MEM));
  20169. }
  20170. return buf;
  20171. }
  20172. /* non-compat API returns length of buffer on success */
  20173. int wolfSSL_BUF_MEM_grow_ex(WOLFSSL_BUF_MEM* buf, size_t len,
  20174. char zeroFill)
  20175. {
  20176. int len_int = (int)len;
  20177. int mx;
  20178. char* tmp;
  20179. /* verify provided arguments */
  20180. if (buf == NULL || len_int < 0) {
  20181. return 0; /* BAD_FUNC_ARG; */
  20182. }
  20183. /* check to see if fits in existing length */
  20184. if (buf->length > len) {
  20185. buf->length = len;
  20186. return len_int;
  20187. }
  20188. /* check to see if fits in max buffer */
  20189. if (buf->max >= len) {
  20190. if (buf->data != NULL && zeroFill) {
  20191. XMEMSET(&buf->data[buf->length], 0, len - buf->length);
  20192. }
  20193. buf->length = len;
  20194. return len_int;
  20195. }
  20196. /* expand size, to handle growth */
  20197. mx = (len_int + 3) / 3 * 4;
  20198. /* use realloc */
  20199. tmp = (char*)XREALLOC(buf->data, mx, NULL, DYNAMIC_TYPE_OPENSSL);
  20200. if (tmp == NULL) {
  20201. return 0; /* ERR_R_MALLOC_FAILURE; */
  20202. }
  20203. buf->data = tmp;
  20204. buf->max = mx;
  20205. if (zeroFill)
  20206. XMEMSET(&buf->data[buf->length], 0, len - buf->length);
  20207. buf->length = len;
  20208. return len_int;
  20209. }
  20210. /* returns length of buffer on success */
  20211. int wolfSSL_BUF_MEM_grow(WOLFSSL_BUF_MEM* buf, size_t len)
  20212. {
  20213. return wolfSSL_BUF_MEM_grow_ex(buf, len, 1);
  20214. }
  20215. /* non-compat API returns length of buffer on success */
  20216. int wolfSSL_BUF_MEM_resize(WOLFSSL_BUF_MEM* buf, size_t len)
  20217. {
  20218. char* tmp;
  20219. int mx;
  20220. /* verify provided arguments */
  20221. if (buf == NULL || len == 0 || (int)len <= 0) {
  20222. return 0; /* BAD_FUNC_ARG; */
  20223. }
  20224. if (len == buf->length)
  20225. return (int)len;
  20226. if (len > buf->length)
  20227. return wolfSSL_BUF_MEM_grow_ex(buf, len, 0);
  20228. /* expand size, to handle growth */
  20229. mx = ((int)len + 3) / 3 * 4;
  20230. /* We want to shrink the internal buffer */
  20231. tmp = (char*)XREALLOC(buf->data, mx, NULL, DYNAMIC_TYPE_OPENSSL);
  20232. if (tmp == NULL)
  20233. return 0;
  20234. buf->data = tmp;
  20235. buf->length = len;
  20236. buf->max = mx;
  20237. return (int)len;
  20238. }
  20239. void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf)
  20240. {
  20241. if (buf) {
  20242. if (buf->data) {
  20243. XFREE(buf->data, NULL, DYNAMIC_TYPE_OPENSSL);
  20244. buf->data = NULL;
  20245. }
  20246. buf->max = 0;
  20247. buf->length = 0;
  20248. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  20249. }
  20250. }
  20251. /* End Functions for openssl/buffer.h */
  20252. #endif /* OPENSSL_EXTRA */
  20253. /*******************************************************************************
  20254. * END OF BUF_MEM API
  20255. ******************************************************************************/
  20256. #define WOLFSSL_CONF_INCLUDED
  20257. #include <src/conf.c>
  20258. /*******************************************************************************
  20259. * START OF RAND API
  20260. ******************************************************************************/
  20261. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  20262. static int wolfSSL_RAND_InitMutex(void)
  20263. {
  20264. #ifndef WOLFSSL_MUTEX_INITIALIZER
  20265. if (gRandMethodsInit == 0) {
  20266. if (wc_InitMutex(&gRandMethodMutex) != 0) {
  20267. WOLFSSL_MSG("Bad Init Mutex rand methods");
  20268. return BAD_MUTEX_E;
  20269. }
  20270. gRandMethodsInit = 1;
  20271. }
  20272. #endif
  20273. return 0;
  20274. }
  20275. #endif
  20276. #ifdef OPENSSL_EXTRA
  20277. /* Checks if the global RNG has been created. If not then one is created.
  20278. *
  20279. * Returns WOLFSSL_SUCCESS when no error is encountered.
  20280. */
  20281. int wolfSSL_RAND_Init(void)
  20282. {
  20283. int ret = WOLFSSL_FAILURE;
  20284. #ifdef HAVE_GLOBAL_RNG
  20285. if (wc_LockMutex(&globalRNGMutex) == 0) {
  20286. if (initGlobalRNG == 0) {
  20287. ret = wc_InitRng(&globalRNG);
  20288. if (ret == 0) {
  20289. initGlobalRNG = 1;
  20290. ret = WOLFSSL_SUCCESS;
  20291. }
  20292. }
  20293. else {
  20294. /* GlobalRNG is already initialized */
  20295. ret = WOLFSSL_SUCCESS;
  20296. }
  20297. wc_UnLockMutex(&globalRNGMutex);
  20298. }
  20299. #endif
  20300. return ret;
  20301. }
  20302. /* WOLFSSL_SUCCESS on ok */
  20303. int wolfSSL_RAND_seed(const void* seed, int len)
  20304. {
  20305. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  20306. if (wolfSSL_RAND_InitMutex() == 0 && wc_LockMutex(&gRandMethodMutex) == 0) {
  20307. if (gRandMethods && gRandMethods->seed) {
  20308. int ret = gRandMethods->seed(seed, len);
  20309. wc_UnLockMutex(&gRandMethodMutex);
  20310. return ret;
  20311. }
  20312. wc_UnLockMutex(&gRandMethodMutex);
  20313. }
  20314. #else
  20315. (void)seed;
  20316. (void)len;
  20317. #endif
  20318. /* Make sure global shared RNG (globalRNG) is initialized */
  20319. return wolfSSL_RAND_Init();
  20320. }
  20321. /* Returns the path for reading seed data from.
  20322. * Uses the env variable $RANDFILE first if set, if not then used $HOME/.rnd
  20323. *
  20324. * Note uses stdlib by default unless XGETENV macro is overwritten
  20325. *
  20326. * fname buffer to hold path
  20327. * len length of fname buffer
  20328. *
  20329. * Returns a pointer to fname on success and NULL on failure
  20330. */
  20331. const char* wolfSSL_RAND_file_name(char* fname, unsigned long len)
  20332. {
  20333. #if !defined(NO_FILESYSTEM) && defined(XGETENV)
  20334. char* rt;
  20335. WOLFSSL_ENTER("wolfSSL_RAND_file_name");
  20336. if (fname == NULL) {
  20337. return NULL;
  20338. }
  20339. XMEMSET(fname, 0, len);
  20340. if ((rt = XGETENV("RANDFILE")) != NULL) {
  20341. if (len > XSTRLEN(rt)) {
  20342. XMEMCPY(fname, rt, XSTRLEN(rt));
  20343. }
  20344. else {
  20345. WOLFSSL_MSG("RANDFILE too large for buffer");
  20346. rt = NULL;
  20347. }
  20348. }
  20349. /* $RANDFILE was not set or is too large, check $HOME */
  20350. if (rt == NULL) {
  20351. const char ap[] = "/.rnd";
  20352. WOLFSSL_MSG("Environment variable RANDFILE not set");
  20353. if ((rt = XGETENV("HOME")) == NULL) {
  20354. WOLFSSL_MSG("Environment variable HOME not set");
  20355. return NULL;
  20356. }
  20357. if (len > XSTRLEN(rt) + XSTRLEN(ap)) {
  20358. fname[0] = '\0';
  20359. XSTRNCAT(fname, rt, len);
  20360. XSTRNCAT(fname, ap, len - XSTRLEN(rt));
  20361. return fname;
  20362. }
  20363. else {
  20364. WOLFSSL_MSG("HOME too large for buffer");
  20365. return NULL;
  20366. }
  20367. }
  20368. return fname;
  20369. #else
  20370. WOLFSSL_ENTER("wolfSSL_RAND_file_name");
  20371. WOLFSSL_MSG("RAND_file_name requires filesystem and getenv support, "
  20372. "not compiled in");
  20373. (void)fname;
  20374. (void)len;
  20375. return NULL;
  20376. #endif
  20377. }
  20378. /* Writes 1024 bytes from the RNG to the given file name.
  20379. *
  20380. * fname name of file to write to
  20381. *
  20382. * Returns the number of bytes written
  20383. */
  20384. int wolfSSL_RAND_write_file(const char* fname)
  20385. {
  20386. int bytes = 0;
  20387. WOLFSSL_ENTER("wolfSSL_RAND_write_file");
  20388. if (fname == NULL) {
  20389. return WOLFSSL_FAILURE;
  20390. }
  20391. #ifndef NO_FILESYSTEM
  20392. {
  20393. #ifndef WOLFSSL_SMALL_STACK
  20394. unsigned char buf[1024];
  20395. #else
  20396. unsigned char* buf = (unsigned char *)XMALLOC(1024, NULL,
  20397. DYNAMIC_TYPE_TMP_BUFFER);
  20398. if (buf == NULL) {
  20399. WOLFSSL_MSG("malloc failed");
  20400. return WOLFSSL_FAILURE;
  20401. }
  20402. #endif
  20403. bytes = 1024; /* default size of buf */
  20404. if (initGlobalRNG == 0 && wolfSSL_RAND_Init() != WOLFSSL_SUCCESS) {
  20405. WOLFSSL_MSG("No RNG to use");
  20406. #ifdef WOLFSSL_SMALL_STACK
  20407. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20408. #endif
  20409. return 0;
  20410. }
  20411. if (wc_RNG_GenerateBlock(&globalRNG, buf, bytes) != 0) {
  20412. WOLFSSL_MSG("Error generating random buffer");
  20413. bytes = 0;
  20414. }
  20415. else {
  20416. XFILE f;
  20417. #ifdef WOLFSSL_CHECK_MEM_ZERO
  20418. wc_MemZero_Add("wolfSSL_RAND_write_file buf", buf, bytes);
  20419. #endif
  20420. f = XFOPEN(fname, "wb");
  20421. if (f == XBADFILE) {
  20422. WOLFSSL_MSG("Error opening the file");
  20423. bytes = 0;
  20424. }
  20425. else {
  20426. size_t bytes_written = XFWRITE(buf, 1, bytes, f);
  20427. bytes = (int)bytes_written;
  20428. XFCLOSE(f);
  20429. }
  20430. }
  20431. ForceZero(buf, bytes);
  20432. #ifdef WOLFSSL_SMALL_STACK
  20433. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20434. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  20435. wc_MemZero_Check(buf, sizeof(buf));
  20436. #endif
  20437. }
  20438. #endif
  20439. return bytes;
  20440. }
  20441. #ifndef FREERTOS_TCP
  20442. /* These constant values are protocol values made by egd */
  20443. #if defined(USE_WOLFSSL_IO) && !defined(USE_WINDOWS_API) && \
  20444. !defined(HAVE_FIPS) && defined(HAVE_HASHDRBG) && !defined(NETOS) && \
  20445. defined(HAVE_SYS_UN_H)
  20446. #define WOLFSSL_EGD_NBLOCK 0x01
  20447. #include <sys/un.h>
  20448. #endif
  20449. /* This collects entropy from the path nm and seeds the global PRNG with it.
  20450. *
  20451. * nm is the file path to the egd server
  20452. *
  20453. * Returns the number of bytes read.
  20454. */
  20455. int wolfSSL_RAND_egd(const char* nm)
  20456. {
  20457. #ifdef WOLFSSL_EGD_NBLOCK
  20458. struct sockaddr_un rem;
  20459. int fd;
  20460. int ret = WOLFSSL_SUCCESS;
  20461. word32 bytes = 0;
  20462. word32 idx = 0;
  20463. #ifndef WOLFSSL_SMALL_STACK
  20464. unsigned char buf[256];
  20465. #else
  20466. unsigned char* buf;
  20467. buf = (unsigned char*)XMALLOC(256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20468. if (buf == NULL) {
  20469. WOLFSSL_MSG("Not enough memory");
  20470. return WOLFSSL_FATAL_ERROR;
  20471. }
  20472. #endif
  20473. XMEMSET(&rem, 0, sizeof(struct sockaddr_un));
  20474. if (nm == NULL) {
  20475. #ifdef WOLFSSL_SMALL_STACK
  20476. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20477. #endif
  20478. return WOLFSSL_FATAL_ERROR;
  20479. }
  20480. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  20481. if (fd < 0) {
  20482. WOLFSSL_MSG("Error creating socket");
  20483. #ifdef WOLFSSL_SMALL_STACK
  20484. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20485. #endif
  20486. return WOLFSSL_FATAL_ERROR;
  20487. }
  20488. rem.sun_family = AF_UNIX;
  20489. XSTRNCPY(rem.sun_path, nm, sizeof(rem.sun_path) - 1);
  20490. rem.sun_path[sizeof(rem.sun_path)-1] = '\0';
  20491. /* connect to egd server */
  20492. if (connect(fd, (struct sockaddr*)&rem, sizeof(struct sockaddr_un)) == -1) {
  20493. WOLFSSL_MSG("error connecting to egd server");
  20494. ret = WOLFSSL_FATAL_ERROR;
  20495. }
  20496. #ifdef WOLFSSL_CHECK_MEM_ZERO
  20497. if (ret == WOLFSSL_SUCCESS) {
  20498. wc_MemZero_Add("wolfSSL_RAND_egd buf", buf, 256);
  20499. }
  20500. #endif
  20501. while (ret == WOLFSSL_SUCCESS && bytes < 255 && idx + 2 < 256) {
  20502. buf[idx] = WOLFSSL_EGD_NBLOCK;
  20503. buf[idx + 1] = 255 - bytes; /* request 255 bytes from server */
  20504. ret = (int)write(fd, buf + idx, 2);
  20505. if (ret != 2) {
  20506. if (errno == EAGAIN) {
  20507. ret = WOLFSSL_SUCCESS;
  20508. continue;
  20509. }
  20510. WOLFSSL_MSG("error requesting entropy from egd server");
  20511. ret = WOLFSSL_FATAL_ERROR;
  20512. break;
  20513. }
  20514. /* attempting to read */
  20515. buf[idx] = 0;
  20516. ret = (int)read(fd, buf + idx, 256 - bytes);
  20517. if (ret == 0) {
  20518. WOLFSSL_MSG("error reading entropy from egd server");
  20519. ret = WOLFSSL_FATAL_ERROR;
  20520. break;
  20521. }
  20522. if (ret > 0 && buf[idx] > 0) {
  20523. bytes += buf[idx]; /* egd stores amount sent in first byte */
  20524. if (bytes + idx > 255 || buf[idx] > ret) {
  20525. WOLFSSL_MSG("Buffer error");
  20526. ret = WOLFSSL_FATAL_ERROR;
  20527. break;
  20528. }
  20529. XMEMMOVE(buf + idx, buf + idx + 1, buf[idx]);
  20530. idx = bytes;
  20531. ret = WOLFSSL_SUCCESS;
  20532. if (bytes >= 255) {
  20533. break;
  20534. }
  20535. }
  20536. else {
  20537. if (errno == EAGAIN || errno == EINTR) {
  20538. WOLFSSL_MSG("EGD would read");
  20539. ret = WOLFSSL_SUCCESS; /* try again */
  20540. }
  20541. else if (buf[idx] == 0) {
  20542. /* if egd returned 0 then there is no more entropy to be had.
  20543. Do not try more reads. */
  20544. ret = WOLFSSL_SUCCESS;
  20545. break;
  20546. }
  20547. else {
  20548. WOLFSSL_MSG("Error with read");
  20549. ret = WOLFSSL_FATAL_ERROR;
  20550. }
  20551. }
  20552. }
  20553. if (bytes > 0 && ret == WOLFSSL_SUCCESS) {
  20554. /* call to check global RNG is created */
  20555. if (wolfSSL_RAND_Init() != WOLFSSL_SUCCESS) {
  20556. WOLFSSL_MSG("Error with initializing global RNG structure");
  20557. ret = WOLFSSL_FATAL_ERROR;
  20558. }
  20559. else if (wc_RNG_DRBG_Reseed(&globalRNG, (const byte*) buf, bytes)
  20560. != 0) {
  20561. WOLFSSL_MSG("Error with reseeding DRBG structure");
  20562. ret = WOLFSSL_FATAL_ERROR;
  20563. }
  20564. #ifdef SHOW_SECRETS
  20565. else { /* print out entropy found only when no error occurred */
  20566. word32 i;
  20567. printf("EGD Entropy = ");
  20568. for (i = 0; i < bytes; i++) {
  20569. printf("%02X", buf[i]);
  20570. }
  20571. printf("\n");
  20572. }
  20573. #endif
  20574. }
  20575. ForceZero(buf, bytes);
  20576. #ifdef WOLFSSL_SMALL_STACK
  20577. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  20578. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  20579. wc_MemZero_Check(buf, 256);
  20580. #endif
  20581. close(fd);
  20582. if (ret == WOLFSSL_SUCCESS) {
  20583. return bytes;
  20584. }
  20585. else {
  20586. return ret;
  20587. }
  20588. #else
  20589. WOLFSSL_MSG("Type of socket needed is not available");
  20590. WOLFSSL_MSG("\tor using mode where DRBG API is not available");
  20591. (void)nm;
  20592. return WOLFSSL_FATAL_ERROR;
  20593. #endif /* WOLFSSL_EGD_NBLOCK */
  20594. }
  20595. #endif /* !FREERTOS_TCP */
  20596. void wolfSSL_RAND_Cleanup(void)
  20597. {
  20598. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  20599. if (wolfSSL_RAND_InitMutex() == 0 && wc_LockMutex(&gRandMethodMutex) == 0) {
  20600. if (gRandMethods && gRandMethods->cleanup)
  20601. gRandMethods->cleanup();
  20602. wc_UnLockMutex(&gRandMethodMutex);
  20603. }
  20604. #ifndef WOLFSSL_MUTEX_INITIALIZER
  20605. if (wc_FreeMutex(&gRandMethodMutex) == 0)
  20606. gRandMethodsInit = 0;
  20607. #endif
  20608. #endif
  20609. #ifdef HAVE_GLOBAL_RNG
  20610. if (wc_LockMutex(&globalRNGMutex) == 0) {
  20611. if (initGlobalRNG) {
  20612. wc_FreeRng(&globalRNG);
  20613. initGlobalRNG = 0;
  20614. }
  20615. wc_UnLockMutex(&globalRNGMutex);
  20616. }
  20617. #endif
  20618. }
  20619. /* returns WOLFSSL_SUCCESS if the bytes generated are valid otherwise
  20620. * WOLFSSL_FAILURE */
  20621. int wolfSSL_RAND_pseudo_bytes(unsigned char* buf, int num)
  20622. {
  20623. int ret;
  20624. int hash;
  20625. byte secret[DRBG_SEED_LEN]; /* secret length arbitrarily chosen */
  20626. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  20627. if (wolfSSL_RAND_InitMutex() == 0 && wc_LockMutex(&gRandMethodMutex) == 0) {
  20628. if (gRandMethods && gRandMethods->pseudorand) {
  20629. ret = gRandMethods->pseudorand(buf, num);
  20630. wc_UnLockMutex(&gRandMethodMutex);
  20631. return ret;
  20632. }
  20633. wc_UnLockMutex(&gRandMethodMutex);
  20634. }
  20635. #endif
  20636. #ifdef WOLFSSL_HAVE_PRF
  20637. #ifndef NO_SHA256
  20638. hash = WC_SHA256;
  20639. #elif defined(WOLFSSL_SHA384)
  20640. hash = WC_SHA384;
  20641. #elif !defined(NO_SHA)
  20642. hash = WC_SHA;
  20643. #elif !defined(NO_MD5)
  20644. hash = WC_MD5;
  20645. #endif
  20646. /* get secret value from source of entropy */
  20647. ret = wolfSSL_RAND_bytes(secret, DRBG_SEED_LEN);
  20648. /* uses input buffer to seed for pseudo random number generation, each
  20649. * thread will potentially have different results this way */
  20650. if (ret == WOLFSSL_SUCCESS) {
  20651. PRIVATE_KEY_UNLOCK();
  20652. ret = wc_PRF(buf, num, secret, DRBG_SEED_LEN, (const byte*)buf, num,
  20653. hash, NULL, INVALID_DEVID);
  20654. PRIVATE_KEY_LOCK();
  20655. ret = (ret == 0) ? WOLFSSL_SUCCESS: WOLFSSL_FAILURE;
  20656. }
  20657. #else
  20658. /* fall back to just doing wolfSSL_RAND_bytes if PRF not avialbale */
  20659. ret = wolfSSL_RAND_bytes(buf, num);
  20660. (void)hash;
  20661. (void)secret;
  20662. #endif
  20663. return ret;
  20664. }
  20665. /* returns WOLFSSL_SUCCESS if the bytes generated are valid otherwise
  20666. * WOLFSSL_FAILURE */
  20667. int wolfSSL_RAND_bytes(unsigned char* buf, int num)
  20668. {
  20669. int ret = 0;
  20670. WC_RNG* rng = NULL;
  20671. #ifdef WOLFSSL_SMALL_STACK
  20672. WC_RNG* tmpRNG = NULL;
  20673. #else
  20674. WC_RNG tmpRNG[1];
  20675. #endif
  20676. int initTmpRng = 0;
  20677. #ifdef HAVE_GLOBAL_RNG
  20678. int used_global = 0;
  20679. #endif
  20680. WOLFSSL_ENTER("wolfSSL_RAND_bytes");
  20681. /* sanity check */
  20682. if (buf == NULL || num < 0)
  20683. /* return code compliant with OpenSSL */
  20684. return 0;
  20685. /* if a RAND callback has been set try and use it */
  20686. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  20687. if (wolfSSL_RAND_InitMutex() == 0 && wc_LockMutex(&gRandMethodMutex) == 0) {
  20688. if (gRandMethods && gRandMethods->bytes) {
  20689. ret = gRandMethods->bytes(buf, num);
  20690. wc_UnLockMutex(&gRandMethodMutex);
  20691. return ret;
  20692. }
  20693. wc_UnLockMutex(&gRandMethodMutex);
  20694. }
  20695. #endif
  20696. #ifdef HAVE_GLOBAL_RNG
  20697. if (initGlobalRNG) {
  20698. if (wc_LockMutex(&globalRNGMutex) != 0) {
  20699. WOLFSSL_MSG("Bad Lock Mutex rng");
  20700. return ret;
  20701. }
  20702. /* the above access to initGlobalRNG is racey -- recheck it now that we
  20703. * have the lock.
  20704. */
  20705. if (initGlobalRNG) {
  20706. rng = &globalRNG;
  20707. used_global = 1;
  20708. }
  20709. else {
  20710. wc_UnLockMutex(&globalRNGMutex);
  20711. }
  20712. }
  20713. if (used_global == 0)
  20714. #endif
  20715. {
  20716. #ifdef WOLFSSL_SMALL_STACK
  20717. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  20718. if (tmpRNG == NULL)
  20719. return ret;
  20720. #endif
  20721. if (wc_InitRng(tmpRNG) == 0) {
  20722. rng = tmpRNG;
  20723. initTmpRng = 1;
  20724. }
  20725. }
  20726. if (rng) {
  20727. /* handles size greater than RNG_MAX_BLOCK_LEN */
  20728. int blockCount = num / RNG_MAX_BLOCK_LEN;
  20729. while (blockCount--) {
  20730. ret = wc_RNG_GenerateBlock(rng, buf, RNG_MAX_BLOCK_LEN);
  20731. if (ret != 0) {
  20732. WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
  20733. break;
  20734. }
  20735. num -= RNG_MAX_BLOCK_LEN;
  20736. buf += RNG_MAX_BLOCK_LEN;
  20737. }
  20738. if (ret == 0 && num)
  20739. ret = wc_RNG_GenerateBlock(rng, buf, num);
  20740. if (ret != 0)
  20741. WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
  20742. else
  20743. ret = WOLFSSL_SUCCESS;
  20744. }
  20745. #ifdef HAVE_GLOBAL_RNG
  20746. if (used_global == 1)
  20747. wc_UnLockMutex(&globalRNGMutex);
  20748. #endif
  20749. if (initTmpRng)
  20750. wc_FreeRng(tmpRNG);
  20751. #ifdef WOLFSSL_SMALL_STACK
  20752. if (tmpRNG)
  20753. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
  20754. #endif
  20755. return ret;
  20756. }
  20757. int wolfSSL_RAND_poll(void)
  20758. {
  20759. byte entropy[16];
  20760. int ret = 0;
  20761. word32 entropy_sz = 16;
  20762. WOLFSSL_ENTER("wolfSSL_RAND_poll");
  20763. if (initGlobalRNG == 0){
  20764. WOLFSSL_MSG("Global RNG no Init");
  20765. return WOLFSSL_FAILURE;
  20766. }
  20767. ret = wc_GenerateSeed(&globalRNG.seed, entropy, entropy_sz);
  20768. if (ret != 0){
  20769. WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
  20770. ret = WOLFSSL_FAILURE;
  20771. }else
  20772. ret = WOLFSSL_SUCCESS;
  20773. return ret;
  20774. }
  20775. /* If a valid struct is provided with function pointers, will override
  20776. RAND_seed, bytes, cleanup, add, pseudo_bytes and status. If a NULL
  20777. pointer is passed in, it will cancel any previous function overrides.
  20778. Returns WOLFSSL_SUCCESS on success, WOLFSSL_FAILURE on failure. */
  20779. int wolfSSL_RAND_set_rand_method(const WOLFSSL_RAND_METHOD *methods)
  20780. {
  20781. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  20782. if (wolfSSL_RAND_InitMutex() == 0 &&
  20783. wc_LockMutex(&gRandMethodMutex) == 0) {
  20784. gRandMethods = methods;
  20785. wc_UnLockMutex(&gRandMethodMutex);
  20786. return WOLFSSL_SUCCESS;
  20787. }
  20788. #else
  20789. (void)methods;
  20790. #endif
  20791. return WOLFSSL_FAILURE;
  20792. }
  20793. /* Returns WOLFSSL_SUCCESS if the RNG has been seeded with enough data */
  20794. int wolfSSL_RAND_status(void)
  20795. {
  20796. int ret = WOLFSSL_SUCCESS;
  20797. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  20798. if (wolfSSL_RAND_InitMutex() == 0 &&
  20799. wc_LockMutex(&gRandMethodMutex) == 0) {
  20800. if (gRandMethods && gRandMethods->status)
  20801. ret = gRandMethods->status();
  20802. wc_UnLockMutex(&gRandMethodMutex);
  20803. }
  20804. else {
  20805. ret = WOLFSSL_FAILURE;
  20806. }
  20807. #else
  20808. /* wolfCrypt provides enough seed internally, so return success */
  20809. #endif
  20810. return ret;
  20811. }
  20812. void wolfSSL_RAND_add(const void* add, int len, double entropy)
  20813. {
  20814. #ifndef WOLFSSL_NO_OPENSSL_RAND_CB
  20815. if (wolfSSL_RAND_InitMutex() == 0 &&
  20816. wc_LockMutex(&gRandMethodMutex) == 0) {
  20817. if (gRandMethods && gRandMethods->add) {
  20818. /* callback has return code, but RAND_add does not */
  20819. (void)gRandMethods->add(add, len, entropy);
  20820. }
  20821. wc_UnLockMutex(&gRandMethodMutex);
  20822. }
  20823. #else
  20824. /* wolfSSL seeds/adds internally, use explicit RNG if you want
  20825. to take control */
  20826. (void)add;
  20827. (void)len;
  20828. (void)entropy;
  20829. #endif
  20830. }
  20831. #ifndef NO_WOLFSSL_STUB
  20832. void wolfSSL_RAND_screen(void)
  20833. {
  20834. WOLFSSL_STUB("RAND_screen");
  20835. }
  20836. #endif
  20837. int wolfSSL_RAND_load_file(const char* fname, long len)
  20838. {
  20839. (void)fname;
  20840. /* wolfCrypt provides enough entropy internally or will report error */
  20841. if (len == -1)
  20842. return 1024;
  20843. else
  20844. return (int)len;
  20845. }
  20846. #endif /* OPENSSL_EXTRA */
  20847. /*******************************************************************************
  20848. * END OF RAND API
  20849. ******************************************************************************/
  20850. /*******************************************************************************
  20851. * START OF EVP_CIPHER API
  20852. ******************************************************************************/
  20853. #ifdef OPENSSL_EXTRA
  20854. /* store for external read of iv, WOLFSSL_SUCCESS on success */
  20855. int wolfSSL_StoreExternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx)
  20856. {
  20857. WOLFSSL_ENTER("wolfSSL_StoreExternalIV");
  20858. if (ctx == NULL) {
  20859. WOLFSSL_MSG("Bad function argument");
  20860. return WOLFSSL_FATAL_ERROR;
  20861. }
  20862. switch (ctx->cipherType) {
  20863. #ifndef NO_AES
  20864. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  20865. case AES_128_CBC_TYPE :
  20866. case AES_192_CBC_TYPE :
  20867. case AES_256_CBC_TYPE :
  20868. WOLFSSL_MSG("AES CBC");
  20869. XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, ctx->ivSz);
  20870. break;
  20871. #endif
  20872. #ifdef HAVE_AESGCM
  20873. case AES_128_GCM_TYPE :
  20874. case AES_192_GCM_TYPE :
  20875. case AES_256_GCM_TYPE :
  20876. WOLFSSL_MSG("AES GCM");
  20877. XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, ctx->ivSz);
  20878. break;
  20879. #endif /* HAVE_AESGCM */
  20880. #ifdef HAVE_AESCCM
  20881. case AES_128_CCM_TYPE :
  20882. case AES_192_CCM_TYPE :
  20883. case AES_256_CCM_TYPE :
  20884. WOLFSSL_MSG("AES CCM");
  20885. XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, ctx->ivSz);
  20886. break;
  20887. #endif /* HAVE_AESCCM */
  20888. #ifdef HAVE_AES_ECB
  20889. case AES_128_ECB_TYPE :
  20890. case AES_192_ECB_TYPE :
  20891. case AES_256_ECB_TYPE :
  20892. WOLFSSL_MSG("AES ECB");
  20893. break;
  20894. #endif
  20895. #ifdef WOLFSSL_AES_COUNTER
  20896. case AES_128_CTR_TYPE :
  20897. case AES_192_CTR_TYPE :
  20898. case AES_256_CTR_TYPE :
  20899. WOLFSSL_MSG("AES CTR");
  20900. XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  20901. break;
  20902. #endif /* WOLFSSL_AES_COUNTER */
  20903. #ifdef WOLFSSL_AES_CFB
  20904. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  20905. case AES_128_CFB1_TYPE:
  20906. case AES_192_CFB1_TYPE:
  20907. case AES_256_CFB1_TYPE:
  20908. WOLFSSL_MSG("AES CFB1");
  20909. break;
  20910. case AES_128_CFB8_TYPE:
  20911. case AES_192_CFB8_TYPE:
  20912. case AES_256_CFB8_TYPE:
  20913. WOLFSSL_MSG("AES CFB8");
  20914. break;
  20915. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  20916. case AES_128_CFB128_TYPE:
  20917. case AES_192_CFB128_TYPE:
  20918. case AES_256_CFB128_TYPE:
  20919. WOLFSSL_MSG("AES CFB128");
  20920. break;
  20921. #endif /* WOLFSSL_AES_CFB */
  20922. #if defined(WOLFSSL_AES_OFB)
  20923. case AES_128_OFB_TYPE:
  20924. case AES_192_OFB_TYPE:
  20925. case AES_256_OFB_TYPE:
  20926. WOLFSSL_MSG("AES OFB");
  20927. break;
  20928. #endif /* WOLFSSL_AES_OFB */
  20929. #ifdef WOLFSSL_AES_XTS
  20930. case AES_128_XTS_TYPE:
  20931. case AES_256_XTS_TYPE:
  20932. WOLFSSL_MSG("AES XTS");
  20933. break;
  20934. #endif /* WOLFSSL_AES_XTS */
  20935. #endif /* NO_AES */
  20936. #ifdef HAVE_ARIA
  20937. case ARIA_128_GCM_TYPE :
  20938. case ARIA_192_GCM_TYPE :
  20939. case ARIA_256_GCM_TYPE :
  20940. WOLFSSL_MSG("ARIA GCM");
  20941. XMEMCPY(ctx->iv, &ctx->cipher.aria.nonce, ARIA_BLOCK_SIZE);
  20942. break;
  20943. #endif /* HAVE_ARIA */
  20944. #ifndef NO_DES3
  20945. case DES_CBC_TYPE :
  20946. WOLFSSL_MSG("DES CBC");
  20947. XMEMCPY(ctx->iv, &ctx->cipher.des.reg, DES_BLOCK_SIZE);
  20948. break;
  20949. case DES_EDE3_CBC_TYPE :
  20950. WOLFSSL_MSG("DES EDE3 CBC");
  20951. XMEMCPY(ctx->iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
  20952. break;
  20953. #endif
  20954. #ifdef WOLFSSL_DES_ECB
  20955. case DES_ECB_TYPE :
  20956. WOLFSSL_MSG("DES ECB");
  20957. break;
  20958. case DES_EDE3_ECB_TYPE :
  20959. WOLFSSL_MSG("DES3 ECB");
  20960. break;
  20961. #endif
  20962. case ARC4_TYPE :
  20963. WOLFSSL_MSG("ARC4");
  20964. break;
  20965. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  20966. case CHACHA20_POLY1305_TYPE:
  20967. break;
  20968. #endif
  20969. #ifdef HAVE_CHACHA
  20970. case CHACHA20_TYPE:
  20971. break;
  20972. #endif
  20973. #ifdef WOLFSSL_SM4_ECB
  20974. case SM4_ECB_TYPE:
  20975. break;
  20976. #endif
  20977. #ifdef WOLFSSL_SM4_CBC
  20978. case SM4_CBC_TYPE:
  20979. WOLFSSL_MSG("SM4 CBC");
  20980. XMEMCPY(&ctx->cipher.sm4.iv, ctx->iv, SM4_BLOCK_SIZE);
  20981. break;
  20982. #endif
  20983. #ifdef WOLFSSL_SM4_CTR
  20984. case SM4_CTR_TYPE:
  20985. WOLFSSL_MSG("SM4 CTR");
  20986. XMEMCPY(&ctx->cipher.sm4.iv, ctx->iv, SM4_BLOCK_SIZE);
  20987. break;
  20988. #endif
  20989. #ifdef WOLFSSL_SM4_GCM
  20990. case SM4_GCM_TYPE:
  20991. WOLFSSL_MSG("SM4 GCM");
  20992. XMEMCPY(&ctx->cipher.sm4.iv, ctx->iv, SM4_BLOCK_SIZE);
  20993. break;
  20994. #endif
  20995. #ifdef WOLFSSL_SM4_CCM
  20996. case SM4_CCM_TYPE:
  20997. WOLFSSL_MSG("SM4 CCM");
  20998. XMEMCPY(&ctx->cipher.sm4.iv, ctx->iv, SM4_BLOCK_SIZE);
  20999. break;
  21000. #endif
  21001. case NULL_CIPHER_TYPE :
  21002. WOLFSSL_MSG("NULL");
  21003. break;
  21004. default: {
  21005. WOLFSSL_MSG("bad type");
  21006. return WOLFSSL_FATAL_ERROR;
  21007. }
  21008. }
  21009. return WOLFSSL_SUCCESS;
  21010. }
  21011. /* set internal IV from external, WOLFSSL_SUCCESS on success */
  21012. int wolfSSL_SetInternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx)
  21013. {
  21014. WOLFSSL_ENTER("wolfSSL_SetInternalIV");
  21015. if (ctx == NULL) {
  21016. WOLFSSL_MSG("Bad function argument");
  21017. return WOLFSSL_FATAL_ERROR;
  21018. }
  21019. switch (ctx->cipherType) {
  21020. #ifndef NO_AES
  21021. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  21022. case AES_128_CBC_TYPE :
  21023. case AES_192_CBC_TYPE :
  21024. case AES_256_CBC_TYPE :
  21025. WOLFSSL_MSG("AES CBC");
  21026. XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  21027. break;
  21028. #endif
  21029. #ifdef HAVE_AESGCM
  21030. case AES_128_GCM_TYPE :
  21031. case AES_192_GCM_TYPE :
  21032. case AES_256_GCM_TYPE :
  21033. WOLFSSL_MSG("AES GCM");
  21034. XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  21035. break;
  21036. #endif
  21037. #ifdef HAVE_AES_ECB
  21038. case AES_128_ECB_TYPE :
  21039. case AES_192_ECB_TYPE :
  21040. case AES_256_ECB_TYPE :
  21041. WOLFSSL_MSG("AES ECB");
  21042. break;
  21043. #endif
  21044. #ifdef WOLFSSL_AES_COUNTER
  21045. case AES_128_CTR_TYPE :
  21046. case AES_192_CTR_TYPE :
  21047. case AES_256_CTR_TYPE :
  21048. WOLFSSL_MSG("AES CTR");
  21049. XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  21050. break;
  21051. #endif
  21052. #endif /* NO_AES */
  21053. #ifdef HAVE_ARIA
  21054. case ARIA_128_GCM_TYPE :
  21055. case ARIA_192_GCM_TYPE :
  21056. case ARIA_256_GCM_TYPE :
  21057. WOLFSSL_MSG("ARIA GCM");
  21058. XMEMCPY(&ctx->cipher.aria.nonce, ctx->iv, ARIA_BLOCK_SIZE);
  21059. break;
  21060. #endif /* HAVE_ARIA */
  21061. #ifndef NO_DES3
  21062. case DES_CBC_TYPE :
  21063. WOLFSSL_MSG("DES CBC");
  21064. XMEMCPY(&ctx->cipher.des.reg, ctx->iv, DES_BLOCK_SIZE);
  21065. break;
  21066. case DES_EDE3_CBC_TYPE :
  21067. WOLFSSL_MSG("DES EDE3 CBC");
  21068. XMEMCPY(&ctx->cipher.des3.reg, ctx->iv, DES_BLOCK_SIZE);
  21069. break;
  21070. #endif
  21071. #ifdef WOLFSSL_DES_ECB
  21072. case DES_ECB_TYPE :
  21073. WOLFSSL_MSG("DES ECB");
  21074. break;
  21075. case DES_EDE3_ECB_TYPE :
  21076. WOLFSSL_MSG("DES3 ECB");
  21077. break;
  21078. #endif
  21079. case ARC4_TYPE :
  21080. WOLFSSL_MSG("ARC4");
  21081. break;
  21082. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  21083. case CHACHA20_POLY1305_TYPE:
  21084. break;
  21085. #endif
  21086. #ifdef HAVE_CHACHA
  21087. case CHACHA20_TYPE:
  21088. break;
  21089. #endif
  21090. #ifdef WOLFSSL_SM4_ECB
  21091. case SM4_ECB_TYPE:
  21092. break;
  21093. #endif
  21094. #ifdef WOLFSSL_SM4_CBC
  21095. case SM4_CBC_TYPE:
  21096. WOLFSSL_MSG("SM4 CBC");
  21097. XMEMCPY(ctx->iv, &ctx->cipher.sm4.iv, ctx->ivSz);
  21098. break;
  21099. #endif
  21100. #ifdef WOLFSSL_SM4_CTR
  21101. case SM4_CTR_TYPE:
  21102. WOLFSSL_MSG("SM4 CTR");
  21103. XMEMCPY(ctx->iv, &ctx->cipher.sm4.iv, ctx->ivSz);
  21104. break;
  21105. #endif
  21106. #ifdef WOLFSSL_SM4_GCM
  21107. case SM4_GCM_TYPE:
  21108. WOLFSSL_MSG("SM4 GCM");
  21109. XMEMCPY(ctx->iv, &ctx->cipher.sm4.iv, ctx->ivSz);
  21110. break;
  21111. #endif
  21112. #ifdef WOLFSSL_SM4_CCM
  21113. case SM4_CCM_TYPE:
  21114. WOLFSSL_MSG("SM4 CCM");
  21115. XMEMCPY(ctx->iv, &ctx->cipher.sm4.iv, ctx->ivSz);
  21116. break;
  21117. #endif
  21118. case NULL_CIPHER_TYPE :
  21119. WOLFSSL_MSG("NULL");
  21120. break;
  21121. default: {
  21122. WOLFSSL_MSG("bad type");
  21123. return WOLFSSL_FATAL_ERROR;
  21124. }
  21125. }
  21126. return WOLFSSL_SUCCESS;
  21127. }
  21128. #ifndef NO_DES3
  21129. void wolfSSL_3des_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
  21130. unsigned char* iv, int len)
  21131. {
  21132. (void)len;
  21133. WOLFSSL_MSG("wolfSSL_3des_iv");
  21134. if (ctx == NULL || iv == NULL) {
  21135. WOLFSSL_MSG("Bad function argument");
  21136. return;
  21137. }
  21138. if (doset)
  21139. wc_Des3_SetIV(&ctx->cipher.des3, iv); /* OpenSSL compat, no ret */
  21140. else
  21141. XMEMCPY(iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
  21142. }
  21143. #endif /* NO_DES3 */
  21144. #ifndef NO_AES
  21145. void wolfSSL_aes_ctr_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
  21146. unsigned char* iv, int len)
  21147. {
  21148. (void)len;
  21149. WOLFSSL_MSG("wolfSSL_aes_ctr_iv");
  21150. if (ctx == NULL || iv == NULL) {
  21151. WOLFSSL_MSG("Bad function argument");
  21152. return;
  21153. }
  21154. if (doset)
  21155. (void)wc_AesSetIV(&ctx->cipher.aes, iv); /* OpenSSL compat, no ret */
  21156. else
  21157. XMEMCPY(iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  21158. }
  21159. #endif /* NO_AES */
  21160. #endif /* OPENSSL_EXTRA */
  21161. /*******************************************************************************
  21162. * END OF EVP_CIPHER API
  21163. ******************************************************************************/
  21164. #ifndef NO_CERTS
  21165. #define WOLFSSL_X509_STORE_INCLUDED
  21166. #include <src/x509_str.c>
  21167. #define WOLFSSL_SSL_P7P12_INCLUDED
  21168. #include <src/ssl_p7p12.c>
  21169. #endif /* !NO_CERTS */
  21170. /*******************************************************************************
  21171. * BEGIN OPENSSL FIPS DRBG APIs
  21172. ******************************************************************************/
  21173. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  21174. int wolfSSL_FIPS_drbg_init(WOLFSSL_DRBG_CTX *ctx, int type, unsigned int flags)
  21175. {
  21176. int ret = WOLFSSL_FAILURE;
  21177. if (ctx != NULL) {
  21178. XMEMSET(ctx, 0, sizeof(WOLFSSL_DRBG_CTX));
  21179. ctx->type = type;
  21180. ctx->xflags = flags;
  21181. ctx->status = DRBG_STATUS_UNINITIALISED;
  21182. ret = WOLFSSL_SUCCESS;
  21183. }
  21184. return ret;
  21185. }
  21186. WOLFSSL_DRBG_CTX* wolfSSL_FIPS_drbg_new(int type, unsigned int flags)
  21187. {
  21188. int ret = WOLFSSL_FAILURE;
  21189. WOLFSSL_DRBG_CTX* ctx = (WOLFSSL_DRBG_CTX*)XMALLOC(sizeof(WOLFSSL_DRBG_CTX),
  21190. NULL, DYNAMIC_TYPE_OPENSSL);
  21191. ret = wolfSSL_FIPS_drbg_init(ctx, type, flags);
  21192. if (ret == WOLFSSL_SUCCESS && type != 0) {
  21193. ret = wolfSSL_FIPS_drbg_instantiate(ctx, NULL, 0);
  21194. }
  21195. if (ret != WOLFSSL_SUCCESS) {
  21196. WOLFSSL_ERROR(ret);
  21197. wolfSSL_FIPS_drbg_free(ctx);
  21198. ctx = NULL;
  21199. }
  21200. return ctx;
  21201. }
  21202. int wolfSSL_FIPS_drbg_instantiate(WOLFSSL_DRBG_CTX* ctx,
  21203. const unsigned char* pers, size_t perslen)
  21204. {
  21205. int ret = WOLFSSL_FAILURE;
  21206. if (ctx != NULL && ctx->rng == NULL) {
  21207. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21208. (defined(HAVE_FIPS) && FIPS_VERSION_GE(5,0)))
  21209. ctx->rng = wc_rng_new((byte*)pers, (word32)perslen, NULL);
  21210. #else
  21211. ctx->rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  21212. if (ctx->rng != NULL) {
  21213. #if defined(HAVE_FIPS) && FIPS_VERSION_GE(2,0)
  21214. ret = wc_InitRngNonce(ctx->rng, (byte*)pers, (word32)perslen);
  21215. #else
  21216. ret = wc_InitRng(ctx->rng);
  21217. (void)pers;
  21218. (void)perslen;
  21219. #endif
  21220. if (ret != 0) {
  21221. WOLFSSL_ERROR(ret);
  21222. XFREE(ctx->rng, NULL, DYNAMIC_TYPE_RNG);
  21223. ctx->rng = NULL;
  21224. }
  21225. }
  21226. #endif
  21227. }
  21228. if (ctx != NULL && ctx->rng != NULL) {
  21229. ctx->status = DRBG_STATUS_READY;
  21230. ret = WOLFSSL_SUCCESS;
  21231. }
  21232. return ret;
  21233. }
  21234. int wolfSSL_FIPS_drbg_set_callbacks(WOLFSSL_DRBG_CTX* ctx,
  21235. drbg_entropy_get entropy_get, drbg_entropy_clean entropy_clean,
  21236. size_t entropy_blocklen,
  21237. drbg_nonce_get none_get, drbg_nonce_clean nonce_clean)
  21238. {
  21239. int ret = WOLFSSL_FAILURE;
  21240. if (ctx != NULL) {
  21241. ctx->entropy_get = entropy_get;
  21242. ctx->entropy_clean = entropy_clean;
  21243. ctx->entropy_blocklen = entropy_blocklen;
  21244. ctx->none_get = none_get;
  21245. ctx->nonce_clean = nonce_clean;
  21246. ret = WOLFSSL_SUCCESS;
  21247. }
  21248. return ret;
  21249. }
  21250. void wolfSSL_FIPS_rand_add(const void* buf, int num, double entropy)
  21251. {
  21252. /* not implemented */
  21253. (void)buf;
  21254. (void)num;
  21255. (void)entropy;
  21256. }
  21257. int wolfSSL_FIPS_drbg_reseed(WOLFSSL_DRBG_CTX* ctx, const unsigned char* adin,
  21258. size_t adinlen)
  21259. {
  21260. int ret = WOLFSSL_FAILURE;
  21261. if (ctx != NULL && ctx->rng != NULL) {
  21262. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21263. (defined(HAVE_FIPS) && FIPS_VERSION_GE(2,0)))
  21264. if (wc_RNG_DRBG_Reseed(ctx->rng, adin, (word32)adinlen) == 0) {
  21265. ret = WOLFSSL_SUCCESS;
  21266. }
  21267. #else
  21268. ret = WOLFSSL_SUCCESS;
  21269. (void)adin;
  21270. (void)adinlen;
  21271. #endif
  21272. }
  21273. return ret;
  21274. }
  21275. int wolfSSL_FIPS_drbg_generate(WOLFSSL_DRBG_CTX* ctx, unsigned char* out,
  21276. size_t outlen, int prediction_resistance, const unsigned char* adin,
  21277. size_t adinlen)
  21278. {
  21279. int ret = WOLFSSL_FAILURE;
  21280. if (ctx != NULL && ctx->rng != NULL) {
  21281. ret = wc_RNG_GenerateBlock(ctx->rng, out, (word32)outlen);
  21282. if (ret == 0) {
  21283. ret = WOLFSSL_SUCCESS;
  21284. }
  21285. }
  21286. (void)prediction_resistance;
  21287. (void)adin;
  21288. (void)adinlen;
  21289. return ret;
  21290. }
  21291. int wolfSSL_FIPS_drbg_uninstantiate(WOLFSSL_DRBG_CTX *ctx)
  21292. {
  21293. if (ctx != NULL && ctx->rng != NULL) {
  21294. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21295. (defined(HAVE_FIPS) && FIPS_VERSION_GE(5,0)))
  21296. wc_rng_free(ctx->rng);
  21297. #else
  21298. wc_FreeRng(ctx->rng);
  21299. XFREE(ctx->rng, NULL, DYNAMIC_TYPE_RNG);
  21300. #endif
  21301. ctx->rng = NULL;
  21302. ctx->status = DRBG_STATUS_UNINITIALISED;
  21303. }
  21304. return WOLFSSL_SUCCESS;
  21305. }
  21306. void wolfSSL_FIPS_drbg_free(WOLFSSL_DRBG_CTX *ctx)
  21307. {
  21308. if (ctx != NULL) {
  21309. /* As safety check if free'ing the default drbg, then mark global NULL.
  21310. * Technically the user should not call free on the default drbg. */
  21311. if (ctx == gDrbgDefCtx) {
  21312. gDrbgDefCtx = NULL;
  21313. }
  21314. wolfSSL_FIPS_drbg_uninstantiate(ctx);
  21315. XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
  21316. }
  21317. }
  21318. WOLFSSL_DRBG_CTX* wolfSSL_FIPS_get_default_drbg(void)
  21319. {
  21320. if (gDrbgDefCtx == NULL) {
  21321. gDrbgDefCtx = wolfSSL_FIPS_drbg_new(0, 0);
  21322. }
  21323. return gDrbgDefCtx;
  21324. }
  21325. void wolfSSL_FIPS_get_timevec(unsigned char* buf, unsigned long* pctr)
  21326. {
  21327. /* not implemented */
  21328. (void)buf;
  21329. (void)pctr;
  21330. }
  21331. void* wolfSSL_FIPS_drbg_get_app_data(WOLFSSL_DRBG_CTX *ctx)
  21332. {
  21333. if (ctx != NULL) {
  21334. return ctx->app_data;
  21335. }
  21336. return NULL;
  21337. }
  21338. void wolfSSL_FIPS_drbg_set_app_data(WOLFSSL_DRBG_CTX *ctx, void *app_data)
  21339. {
  21340. if (ctx != NULL) {
  21341. ctx->app_data = app_data;
  21342. }
  21343. }
  21344. #endif
  21345. /*******************************************************************************
  21346. * END OF OPENSSL FIPS DRBG APIs
  21347. ******************************************************************************/
  21348. #endif /* !WOLFCRYPT_ONLY */