ssl.c 442 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136
  1. /* ssl.c
  2. *
  3. * Copyright (C) 2006-2015 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  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-1301, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef WOLFCRYPT_ONLY
  26. #ifdef HAVE_ERRNO_H
  27. #include <errno.h>
  28. #endif
  29. #include <wolfssl/internal.h>
  30. #include <wolfssl/error-ssl.h>
  31. #include <wolfssl/wolfcrypt/coding.h>
  32. #ifdef NO_INLINE
  33. #include <wolfssl/wolfcrypt/misc.h>
  34. #else
  35. #include <wolfcrypt/src/misc.c>
  36. #endif
  37. #ifndef WOLFSSL_ALLOW_NO_SUITES
  38. #if defined(NO_DH) && !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA) \
  39. && !defined(WOLFSSL_STATIC_DH) && !defined(WOLFSSL_STATIC_PSK)
  40. #error "No cipher suites defined because DH disabled, ECC disabled, and no static suites defined. Please see top of README"
  41. #endif
  42. #endif
  43. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  44. defined(WOLFSSL_KEY_GEN)
  45. #include <wolfssl/openssl/evp.h>
  46. /* openssl headers end, wolfssl internal headers next */
  47. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  48. #endif
  49. #ifdef OPENSSL_EXTRA
  50. /* openssl headers begin */
  51. #include <wolfssl/openssl/hmac.h>
  52. #include <wolfssl/openssl/crypto.h>
  53. #include <wolfssl/openssl/des.h>
  54. #include <wolfssl/openssl/bn.h>
  55. #include <wolfssl/openssl/dh.h>
  56. #include <wolfssl/openssl/rsa.h>
  57. #include <wolfssl/openssl/pem.h>
  58. #include <wolfssl/openssl/ec.h>
  59. #include <wolfssl/openssl/ec25519.h>
  60. #include <wolfssl/openssl/ed25519.h>
  61. #include <wolfssl/openssl/ecdsa.h>
  62. #include <wolfssl/openssl/ecdh.h>
  63. /* openssl headers end, wolfssl internal headers next */
  64. #include <wolfssl/wolfcrypt/hmac.h>
  65. #include <wolfssl/wolfcrypt/random.h>
  66. #include <wolfssl/wolfcrypt/des3.h>
  67. #include <wolfssl/wolfcrypt/md4.h>
  68. #include <wolfssl/wolfcrypt/md5.h>
  69. #include <wolfssl/wolfcrypt/arc4.h>
  70. #include <wolfssl/wolfcrypt/idea.h>
  71. #include <wolfssl/wolfcrypt/curve25519.h>
  72. #include <wolfssl/wolfcrypt/ed25519.h>
  73. #ifdef HAVE_STUNNEL
  74. #include <wolfssl/openssl/ocsp.h>
  75. #endif /* WITH_STUNNEL */
  76. #ifdef WOLFSSL_SHA512
  77. #include <wolfssl/wolfcrypt/sha512.h>
  78. #endif
  79. #endif
  80. #ifndef NO_FILESYSTEM
  81. #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR) \
  82. && !defined(EBSNET)
  83. #include <dirent.h>
  84. #include <sys/stat.h>
  85. #endif
  86. #ifdef EBSNET
  87. #include "vfapi.h"
  88. #include "vfile.h"
  89. #endif
  90. #endif /* NO_FILESYSTEM */
  91. #ifndef TRUE
  92. #define TRUE 1
  93. #endif
  94. #ifndef FALSE
  95. #define FALSE 0
  96. #endif
  97. #ifndef WOLFSSL_HAVE_MIN
  98. #define WOLFSSL_HAVE_MIN
  99. static INLINE word32 min(word32 a, word32 b)
  100. {
  101. return a > b ? b : a;
  102. }
  103. #endif /* WOLFSSSL_HAVE_MIN */
  104. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_HAVE_MAX)
  105. #define WOLFSSL_HAVE_MAX
  106. static INLINE word32 max(word32 a, word32 b)
  107. {
  108. return a > b ? a : b;
  109. }
  110. #endif /* WOLFSSL_DTLS && !WOLFSSL_HAVE_MAX */
  111. #ifndef WOLFSSL_LEANPSK
  112. char* mystrnstr(const char* s1, const char* s2, unsigned int n)
  113. {
  114. unsigned int s2_len = (unsigned int)XSTRLEN(s2);
  115. if (s2_len == 0)
  116. return (char*)s1;
  117. while (n >= s2_len && s1[0]) {
  118. if (s1[0] == s2[0])
  119. if (XMEMCMP(s1, s2, s2_len) == 0)
  120. return (char*)s1;
  121. s1++;
  122. n--;
  123. }
  124. return NULL;
  125. }
  126. #endif
  127. /* prevent multiple mutex initializations */
  128. static volatile int initRefCount = 0;
  129. static wolfSSL_Mutex count_mutex; /* init ref count mutex */
  130. WOLFSSL_CTX* wolfSSL_CTX_new(WOLFSSL_METHOD* method)
  131. {
  132. WOLFSSL_CTX* ctx = NULL;
  133. WOLFSSL_ENTER("WOLFSSL_CTX_new");
  134. if (initRefCount == 0)
  135. wolfSSL_Init(); /* user no longer forced to call Init themselves */
  136. if (method == NULL)
  137. return ctx;
  138. ctx = (WOLFSSL_CTX*) XMALLOC(sizeof(WOLFSSL_CTX), 0, DYNAMIC_TYPE_CTX);
  139. if (ctx) {
  140. if (InitSSL_Ctx(ctx, method) < 0) {
  141. WOLFSSL_MSG("Init CTX failed");
  142. wolfSSL_CTX_free(ctx);
  143. ctx = NULL;
  144. }
  145. }
  146. else {
  147. WOLFSSL_MSG("Alloc CTX failed, method freed");
  148. XFREE(method, NULL, DYNAMIC_TYPE_METHOD);
  149. }
  150. WOLFSSL_LEAVE("WOLFSSL_CTX_new", 0);
  151. return ctx;
  152. }
  153. void wolfSSL_CTX_free(WOLFSSL_CTX* ctx)
  154. {
  155. WOLFSSL_ENTER("SSL_CTX_free");
  156. if (ctx)
  157. FreeSSL_Ctx(ctx);
  158. WOLFSSL_LEAVE("SSL_CTX_free", 0);
  159. }
  160. WOLFSSL* wolfSSL_new(WOLFSSL_CTX* ctx)
  161. {
  162. WOLFSSL* ssl = NULL;
  163. int ret = 0;
  164. (void)ret;
  165. WOLFSSL_ENTER("SSL_new");
  166. if (ctx == NULL)
  167. return ssl;
  168. ssl = (WOLFSSL*) XMALLOC(sizeof(WOLFSSL), ctx->heap,DYNAMIC_TYPE_SSL);
  169. if (ssl)
  170. if ( (ret = InitSSL(ssl, ctx)) < 0) {
  171. FreeSSL(ssl);
  172. ssl = 0;
  173. }
  174. WOLFSSL_LEAVE("SSL_new", ret);
  175. return ssl;
  176. }
  177. void wolfSSL_free(WOLFSSL* ssl)
  178. {
  179. WOLFSSL_ENTER("SSL_free");
  180. if (ssl)
  181. FreeSSL(ssl);
  182. WOLFSSL_LEAVE("SSL_free", 0);
  183. }
  184. #ifdef HAVE_POLY1305
  185. /* set if to use old poly 1 for yes 0 to use new poly */
  186. int wolfSSL_use_old_poly(WOLFSSL* ssl, int value)
  187. {
  188. WOLFSSL_ENTER("SSL_use_old_poly");
  189. ssl->options.oldPoly = value;
  190. WOLFSSL_LEAVE("SSL_use_old_poly", 0);
  191. return 0;
  192. }
  193. #endif
  194. int wolfSSL_set_fd(WOLFSSL* ssl, int fd)
  195. {
  196. WOLFSSL_ENTER("SSL_set_fd");
  197. ssl->rfd = fd; /* not used directly to allow IO callbacks */
  198. ssl->wfd = fd;
  199. ssl->IOCB_ReadCtx = &ssl->rfd;
  200. ssl->IOCB_WriteCtx = &ssl->wfd;
  201. #ifdef WOLFSSL_DTLS
  202. if (ssl->options.dtls) {
  203. ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx;
  204. ssl->IOCB_WriteCtx = &ssl->buffers.dtlsCtx;
  205. ssl->buffers.dtlsCtx.fd = fd;
  206. }
  207. #endif
  208. WOLFSSL_LEAVE("SSL_set_fd", SSL_SUCCESS);
  209. return SSL_SUCCESS;
  210. }
  211. /**
  212. * Get the name of cipher at priotity level passed in.
  213. */
  214. char* wolfSSL_get_cipher_list(int priority)
  215. {
  216. const char* const* ciphers = GetCipherNames();
  217. if (priority >= GetCipherNamesSize() || priority < 0) {
  218. return 0;
  219. }
  220. return (char*)ciphers[priority];
  221. }
  222. int wolfSSL_get_ciphers(char* buf, int len)
  223. {
  224. const char* const* ciphers = GetCipherNames();
  225. int totalInc = 0;
  226. int step = 0;
  227. char delim = ':';
  228. int size = GetCipherNamesSize();
  229. int i;
  230. if (buf == NULL || len <= 0)
  231. return BAD_FUNC_ARG;
  232. /* Add each member to the buffer delimitted by a : */
  233. for (i = 0; i < size; i++) {
  234. step = (int)(XSTRLEN(ciphers[i]) + 1); /* delimiter */
  235. totalInc += step;
  236. /* Check to make sure buf is large enough and will not overflow */
  237. if (totalInc < len) {
  238. XSTRNCPY(buf, ciphers[i], XSTRLEN(ciphers[i]));
  239. buf += XSTRLEN(ciphers[i]);
  240. if (i < size - 1)
  241. *buf++ = delim;
  242. else
  243. *buf++ = '\0';
  244. }
  245. else
  246. return BUFFER_E;
  247. }
  248. return SSL_SUCCESS;
  249. }
  250. int wolfSSL_get_fd(const WOLFSSL* ssl)
  251. {
  252. WOLFSSL_ENTER("SSL_get_fd");
  253. WOLFSSL_LEAVE("SSL_get_fd", ssl->rfd);
  254. return ssl->rfd;
  255. }
  256. int wolfSSL_get_using_nonblock(WOLFSSL* ssl)
  257. {
  258. WOLFSSL_ENTER("wolfSSL_get_using_nonblock");
  259. WOLFSSL_LEAVE("wolfSSL_get_using_nonblock", ssl->options.usingNonblock);
  260. return ssl->options.usingNonblock;
  261. }
  262. int wolfSSL_dtls(WOLFSSL* ssl)
  263. {
  264. return ssl->options.dtls;
  265. }
  266. #ifndef WOLFSSL_LEANPSK
  267. void wolfSSL_set_using_nonblock(WOLFSSL* ssl, int nonblock)
  268. {
  269. WOLFSSL_ENTER("wolfSSL_set_using_nonblock");
  270. ssl->options.usingNonblock = (nonblock != 0);
  271. }
  272. int wolfSSL_dtls_set_peer(WOLFSSL* ssl, void* peer, unsigned int peerSz)
  273. {
  274. #ifdef WOLFSSL_DTLS
  275. void* sa = (void*)XMALLOC(peerSz, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
  276. if (sa != NULL) {
  277. if (ssl->buffers.dtlsCtx.peer.sa != NULL)
  278. XFREE(ssl->buffers.dtlsCtx.peer.sa,ssl->heap,DYNAMIC_TYPE_SOCKADDR);
  279. XMEMCPY(sa, peer, peerSz);
  280. ssl->buffers.dtlsCtx.peer.sa = sa;
  281. ssl->buffers.dtlsCtx.peer.sz = peerSz;
  282. return SSL_SUCCESS;
  283. }
  284. return SSL_FAILURE;
  285. #else
  286. (void)ssl;
  287. (void)peer;
  288. (void)peerSz;
  289. return SSL_NOT_IMPLEMENTED;
  290. #endif
  291. }
  292. int wolfSSL_dtls_get_peer(WOLFSSL* ssl, void* peer, unsigned int* peerSz)
  293. {
  294. #ifdef WOLFSSL_DTLS
  295. if (peer != NULL && peerSz != NULL
  296. && *peerSz >= ssl->buffers.dtlsCtx.peer.sz) {
  297. *peerSz = ssl->buffers.dtlsCtx.peer.sz;
  298. XMEMCPY(peer, ssl->buffers.dtlsCtx.peer.sa, *peerSz);
  299. return SSL_SUCCESS;
  300. }
  301. return SSL_FAILURE;
  302. #else
  303. (void)ssl;
  304. (void)peer;
  305. (void)peerSz;
  306. return SSL_NOT_IMPLEMENTED;
  307. #endif
  308. }
  309. #endif /* WOLFSSL_LEANPSK */
  310. /* return underlying connect or accept, SSL_SUCCESS on ok */
  311. int wolfSSL_negotiate(WOLFSSL* ssl)
  312. {
  313. int err = SSL_FATAL_ERROR;
  314. WOLFSSL_ENTER("wolfSSL_negotiate");
  315. #ifndef NO_WOLFSSL_SERVER
  316. if (ssl->options.side == WOLFSSL_SERVER_END)
  317. err = wolfSSL_accept(ssl);
  318. #endif
  319. #ifndef NO_WOLFSSL_CLIENT
  320. if (ssl->options.side == WOLFSSL_CLIENT_END)
  321. err = wolfSSL_connect(ssl);
  322. #endif
  323. WOLFSSL_LEAVE("wolfSSL_negotiate", err);
  324. return err;
  325. }
  326. #ifndef WOLFSSL_LEANPSK
  327. /* object size based on build */
  328. int wolfSSL_GetObjectSize(void)
  329. {
  330. #ifdef SHOW_SIZES
  331. printf("sizeof suites = %lu\n", sizeof(Suites));
  332. printf("sizeof ciphers(2) = %lu\n", sizeof(Ciphers));
  333. #ifndef NO_RC4
  334. printf(" sizeof arc4 = %lu\n", sizeof(Arc4));
  335. #endif
  336. printf(" sizeof aes = %lu\n", sizeof(Aes));
  337. #ifndef NO_DES3
  338. printf(" sizeof des3 = %lu\n", sizeof(Des3));
  339. #endif
  340. #ifndef NO_RABBIT
  341. printf(" sizeof rabbit = %lu\n", sizeof(Rabbit));
  342. #endif
  343. #ifdef HAVE_CHACHA
  344. printf(" sizeof chacha = %lu\n", sizeof(Chacha));
  345. #endif
  346. printf("sizeof cipher specs = %lu\n", sizeof(CipherSpecs));
  347. printf("sizeof keys = %lu\n", sizeof(Keys));
  348. printf("sizeof Hashes(2) = %lu\n", sizeof(Hashes));
  349. #ifndef NO_MD5
  350. printf(" sizeof MD5 = %lu\n", sizeof(Md5));
  351. #endif
  352. #ifndef NO_SHA
  353. printf(" sizeof SHA = %lu\n", sizeof(Sha));
  354. #endif
  355. #ifndef NO_SHA256
  356. printf(" sizeof SHA256 = %lu\n", sizeof(Sha256));
  357. #endif
  358. #ifdef WOLFSSL_SHA384
  359. printf(" sizeof SHA384 = %lu\n", sizeof(Sha384));
  360. #endif
  361. #ifdef WOLFSSL_SHA384
  362. printf(" sizeof SHA512 = %lu\n", sizeof(Sha512));
  363. #endif
  364. printf("sizeof Buffers = %lu\n", sizeof(Buffers));
  365. printf("sizeof Options = %lu\n", sizeof(Options));
  366. printf("sizeof Arrays = %lu\n", sizeof(Arrays));
  367. #ifndef NO_RSA
  368. printf("sizeof RsaKey = %lu\n", sizeof(RsaKey));
  369. #endif
  370. #ifdef HAVE_ECC
  371. printf("sizeof ecc_key = %lu\n", sizeof(ecc_key));
  372. #endif
  373. printf("sizeof WOLFSSL_CIPHER = %lu\n", sizeof(WOLFSSL_CIPHER));
  374. printf("sizeof WOLFSSL_SESSION = %lu\n", sizeof(WOLFSSL_SESSION));
  375. printf("sizeof WOLFSSL = %lu\n", sizeof(WOLFSSL));
  376. printf("sizeof WOLFSSL_CTX = %lu\n", sizeof(WOLFSSL_CTX));
  377. #endif
  378. return sizeof(WOLFSSL);
  379. }
  380. #endif
  381. #ifndef NO_DH
  382. /* server Diffie-Hellman parameters, SSL_SUCCESS on ok */
  383. int wolfSSL_SetTmpDH(WOLFSSL* ssl, const unsigned char* p, int pSz,
  384. const unsigned char* g, int gSz)
  385. {
  386. byte havePSK = 0;
  387. byte haveRSA = 1;
  388. WOLFSSL_ENTER("wolfSSL_SetTmpDH");
  389. if (ssl == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
  390. if (pSz < ssl->options.minDhKeySz)
  391. return DH_KEY_SIZE_E;
  392. if (ssl->options.side != WOLFSSL_SERVER_END)
  393. return SIDE_ERROR;
  394. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH)
  395. XFREE(ssl->buffers.serverDH_P.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  396. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH)
  397. XFREE(ssl->buffers.serverDH_G.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  398. ssl->buffers.weOwnDH = 1; /* SSL owns now */
  399. ssl->buffers.serverDH_P.buffer = (byte*)XMALLOC(pSz, ssl->ctx->heap,
  400. DYNAMIC_TYPE_DH);
  401. if (ssl->buffers.serverDH_P.buffer == NULL)
  402. return MEMORY_E;
  403. ssl->buffers.serverDH_G.buffer = (byte*)XMALLOC(gSz, ssl->ctx->heap,
  404. DYNAMIC_TYPE_DH);
  405. if (ssl->buffers.serverDH_G.buffer == NULL) {
  406. XFREE(ssl->buffers.serverDH_P.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  407. return MEMORY_E;
  408. }
  409. ssl->buffers.serverDH_P.length = pSz;
  410. ssl->buffers.serverDH_G.length = gSz;
  411. XMEMCPY(ssl->buffers.serverDH_P.buffer, p, pSz);
  412. XMEMCPY(ssl->buffers.serverDH_G.buffer, g, gSz);
  413. ssl->options.haveDH = 1;
  414. #ifndef NO_PSK
  415. havePSK = ssl->options.havePSK;
  416. #endif
  417. #ifdef NO_RSA
  418. haveRSA = 0;
  419. #endif
  420. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, ssl->options.haveDH,
  421. ssl->options.haveNTRU, ssl->options.haveECDSAsig,
  422. ssl->options.haveStaticECC, ssl->options.side);
  423. WOLFSSL_LEAVE("wolfSSL_SetTmpDH", 0);
  424. return SSL_SUCCESS;
  425. }
  426. /* server ctx Diffie-Hellman parameters, SSL_SUCCESS on ok */
  427. int wolfSSL_CTX_SetTmpDH(WOLFSSL_CTX* ctx, const unsigned char* p, int pSz,
  428. const unsigned char* g, int gSz)
  429. {
  430. WOLFSSL_ENTER("wolfSSL_CTX_SetTmpDH");
  431. if (ctx == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
  432. if (pSz < ctx->minDhKeySz)
  433. return DH_KEY_SIZE_E;
  434. XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  435. XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  436. ctx->serverDH_P.buffer = (byte*)XMALLOC(pSz, ctx->heap,DYNAMIC_TYPE_DH);
  437. if (ctx->serverDH_P.buffer == NULL)
  438. return MEMORY_E;
  439. ctx->serverDH_G.buffer = (byte*)XMALLOC(gSz, ctx->heap,DYNAMIC_TYPE_DH);
  440. if (ctx->serverDH_G.buffer == NULL) {
  441. XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  442. return MEMORY_E;
  443. }
  444. ctx->serverDH_P.length = pSz;
  445. ctx->serverDH_G.length = gSz;
  446. XMEMCPY(ctx->serverDH_P.buffer, p, pSz);
  447. XMEMCPY(ctx->serverDH_G.buffer, g, gSz);
  448. ctx->haveDH = 1;
  449. WOLFSSL_LEAVE("wolfSSL_CTX_SetTmpDH", 0);
  450. return SSL_SUCCESS;
  451. }
  452. int wolfSSL_CTX_SetMinDhKey_Sz(WOLFSSL_CTX* ctx, word16 keySz)
  453. {
  454. if (ctx == NULL || keySz > 16000 || keySz % 8 != 0)
  455. return BAD_FUNC_ARG;
  456. ctx->minDhKeySz = keySz / 8;
  457. return SSL_SUCCESS;
  458. }
  459. int wolfSSL_SetMinDhKey_Sz(WOLFSSL* ssl, word16 keySz)
  460. {
  461. if (ssl == NULL || keySz > 16000 || keySz % 8 != 0)
  462. return BAD_FUNC_ARG;
  463. ssl->options.minDhKeySz = keySz / 8;
  464. return SSL_SUCCESS;
  465. }
  466. int wolfSSL_GetDhKey_Sz(WOLFSSL* ssl)
  467. {
  468. if (ssl == NULL)
  469. return BAD_FUNC_ARG;
  470. return (ssl->options.dhKeySz * 8);
  471. }
  472. #endif /* !NO_DH */
  473. int wolfSSL_write(WOLFSSL* ssl, const void* data, int sz)
  474. {
  475. int ret;
  476. WOLFSSL_ENTER("SSL_write()");
  477. if (ssl == NULL || data == NULL || sz < 0)
  478. return BAD_FUNC_ARG;
  479. #ifdef HAVE_ERRNO_H
  480. errno = 0;
  481. #endif
  482. ret = SendData(ssl, data, sz);
  483. WOLFSSL_LEAVE("SSL_write()", ret);
  484. if (ret < 0)
  485. return SSL_FATAL_ERROR;
  486. else
  487. return ret;
  488. }
  489. static int wolfSSL_read_internal(WOLFSSL* ssl, void* data, int sz, int peek)
  490. {
  491. int ret;
  492. WOLFSSL_ENTER("wolfSSL_read_internal()");
  493. if (ssl == NULL || data == NULL || sz < 0)
  494. return BAD_FUNC_ARG;
  495. #ifdef HAVE_ERRNO_H
  496. errno = 0;
  497. #endif
  498. #ifdef WOLFSSL_DTLS
  499. if (ssl->options.dtls)
  500. ssl->dtls_expected_rx = max(sz + 100, MAX_MTU);
  501. #endif
  502. #ifdef HAVE_MAX_FRAGMENT
  503. ret = ReceiveData(ssl, (byte*)data,
  504. min(sz, min(ssl->max_fragment, OUTPUT_RECORD_SIZE)),peek);
  505. #else
  506. ret = ReceiveData(ssl, (byte*)data, min(sz, OUTPUT_RECORD_SIZE), peek);
  507. #endif
  508. WOLFSSL_LEAVE("wolfSSL_read_internal()", ret);
  509. if (ret < 0)
  510. return SSL_FATAL_ERROR;
  511. else
  512. return ret;
  513. }
  514. int wolfSSL_peek(WOLFSSL* ssl, void* data, int sz)
  515. {
  516. WOLFSSL_ENTER("wolfSSL_peek()");
  517. return wolfSSL_read_internal(ssl, data, sz, TRUE);
  518. }
  519. int wolfSSL_read(WOLFSSL* ssl, void* data, int sz)
  520. {
  521. WOLFSSL_ENTER("wolfSSL_read()");
  522. return wolfSSL_read_internal(ssl, data, sz, FALSE);
  523. }
  524. #ifdef HAVE_CAVIUM
  525. /* let's use cavium, SSL_SUCCESS on ok */
  526. int wolfSSL_UseCavium(WOLFSSL* ssl, int devId)
  527. {
  528. if (ssl == NULL)
  529. return BAD_FUNC_ARG;
  530. ssl->devId = devId;
  531. return SSL_SUCCESS;
  532. }
  533. /* let's use cavium, SSL_SUCCESS on ok */
  534. int wolfSSL_CTX_UseCavium(WOLFSSL_CTX* ctx, int devId)
  535. {
  536. if (ctx == NULL)
  537. return BAD_FUNC_ARG;
  538. ctx->devId = devId;
  539. return SSL_SUCCESS;
  540. }
  541. #endif /* HAVE_CAVIUM */
  542. #ifdef HAVE_SNI
  543. int wolfSSL_UseSNI(WOLFSSL* ssl, byte type, const void* data, word16 size)
  544. {
  545. if (ssl == NULL)
  546. return BAD_FUNC_ARG;
  547. return TLSX_UseSNI(&ssl->extensions, type, data, size);
  548. }
  549. int wolfSSL_CTX_UseSNI(WOLFSSL_CTX* ctx, byte type, const void* data,
  550. word16 size)
  551. {
  552. if (ctx == NULL)
  553. return BAD_FUNC_ARG;
  554. return TLSX_UseSNI(&ctx->extensions, type, data, size);
  555. }
  556. #ifndef NO_WOLFSSL_SERVER
  557. void wolfSSL_SNI_SetOptions(WOLFSSL* ssl, byte type, byte options)
  558. {
  559. if (ssl && ssl->extensions)
  560. TLSX_SNI_SetOptions(ssl->extensions, type, options);
  561. }
  562. void wolfSSL_CTX_SNI_SetOptions(WOLFSSL_CTX* ctx, byte type, byte options)
  563. {
  564. if (ctx && ctx->extensions)
  565. TLSX_SNI_SetOptions(ctx->extensions, type, options);
  566. }
  567. byte wolfSSL_SNI_Status(WOLFSSL* ssl, byte type)
  568. {
  569. return TLSX_SNI_Status(ssl ? ssl->extensions : NULL, type);
  570. }
  571. word16 wolfSSL_SNI_GetRequest(WOLFSSL* ssl, byte type, void** data)
  572. {
  573. if (data)
  574. *data = NULL;
  575. if (ssl && ssl->extensions)
  576. return TLSX_SNI_GetRequest(ssl->extensions, type, data);
  577. return 0;
  578. }
  579. int wolfSSL_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  580. byte type, byte* sni, word32* inOutSz)
  581. {
  582. if (clientHello && helloSz > 0 && sni && inOutSz && *inOutSz > 0)
  583. return TLSX_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
  584. return BAD_FUNC_ARG;
  585. }
  586. #endif /* NO_WOLFSSL_SERVER */
  587. #endif /* HAVE_SNI */
  588. #ifdef HAVE_MAX_FRAGMENT
  589. #ifndef NO_WOLFSSL_CLIENT
  590. int wolfSSL_UseMaxFragment(WOLFSSL* ssl, byte mfl)
  591. {
  592. if (ssl == NULL)
  593. return BAD_FUNC_ARG;
  594. return TLSX_UseMaxFragment(&ssl->extensions, mfl);
  595. }
  596. int wolfSSL_CTX_UseMaxFragment(WOLFSSL_CTX* ctx, byte mfl)
  597. {
  598. if (ctx == NULL)
  599. return BAD_FUNC_ARG;
  600. return TLSX_UseMaxFragment(&ctx->extensions, mfl);
  601. }
  602. #endif /* NO_WOLFSSL_CLIENT */
  603. #endif /* HAVE_MAX_FRAGMENT */
  604. #ifdef HAVE_TRUNCATED_HMAC
  605. #ifndef NO_WOLFSSL_CLIENT
  606. int wolfSSL_UseTruncatedHMAC(WOLFSSL* ssl)
  607. {
  608. if (ssl == NULL)
  609. return BAD_FUNC_ARG;
  610. return TLSX_UseTruncatedHMAC(&ssl->extensions);
  611. }
  612. int wolfSSL_CTX_UseTruncatedHMAC(WOLFSSL_CTX* ctx)
  613. {
  614. if (ctx == NULL)
  615. return BAD_FUNC_ARG;
  616. return TLSX_UseTruncatedHMAC(&ctx->extensions);
  617. }
  618. #endif /* NO_WOLFSSL_CLIENT */
  619. #endif /* HAVE_TRUNCATED_HMAC */
  620. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  621. int wolfSSL_UseCertificateStatusRequest(WOLFSSL* ssl, byte status_type,
  622. byte options)
  623. {
  624. if (ssl == NULL || ssl->options.side != WOLFSSL_CLIENT_END)
  625. return BAD_FUNC_ARG;
  626. return TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  627. options);
  628. }
  629. int wolfSSL_CTX_UseCertificateStatusRequest(WOLFSSL_CTX* ctx, byte status_type,
  630. byte options)
  631. {
  632. if (ctx == NULL || ctx->method->side != WOLFSSL_CLIENT_END)
  633. return BAD_FUNC_ARG;
  634. return TLSX_UseCertificateStatusRequest(&ctx->extensions, status_type,
  635. options);
  636. }
  637. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  638. /* Elliptic Curves */
  639. #ifdef HAVE_SUPPORTED_CURVES
  640. #ifndef NO_WOLFSSL_CLIENT
  641. int wolfSSL_UseSupportedCurve(WOLFSSL* ssl, word16 name)
  642. {
  643. if (ssl == NULL)
  644. return BAD_FUNC_ARG;
  645. switch (name) {
  646. case WOLFSSL_ECC_SECP160R1:
  647. case WOLFSSL_ECC_SECP192R1:
  648. case WOLFSSL_ECC_SECP224R1:
  649. case WOLFSSL_ECC_SECP256R1:
  650. case WOLFSSL_ECC_SECP384R1:
  651. case WOLFSSL_ECC_SECP521R1:
  652. break;
  653. default:
  654. return BAD_FUNC_ARG;
  655. }
  656. return TLSX_UseSupportedCurve(&ssl->extensions, name);
  657. }
  658. int wolfSSL_CTX_UseSupportedCurve(WOLFSSL_CTX* ctx, word16 name)
  659. {
  660. if (ctx == NULL)
  661. return BAD_FUNC_ARG;
  662. switch (name) {
  663. case WOLFSSL_ECC_SECP160R1:
  664. case WOLFSSL_ECC_SECP192R1:
  665. case WOLFSSL_ECC_SECP224R1:
  666. case WOLFSSL_ECC_SECP256R1:
  667. case WOLFSSL_ECC_SECP384R1:
  668. case WOLFSSL_ECC_SECP521R1:
  669. break;
  670. default:
  671. return BAD_FUNC_ARG;
  672. }
  673. return TLSX_UseSupportedCurve(&ctx->extensions, name);
  674. }
  675. #endif /* NO_WOLFSSL_CLIENT */
  676. #endif /* HAVE_SUPPORTED_CURVES */
  677. /* QSH quantum safe handshake */
  678. #ifdef HAVE_QSH
  679. /* returns 1 if QSH has been used 0 otherwise */
  680. int wolfSSL_isQSH(WOLFSSL* ssl)
  681. {
  682. /* if no ssl struct than QSH was not used */
  683. if (ssl == NULL)
  684. return 0;
  685. return ssl->isQSH;
  686. }
  687. int wolfSSL_UseSupportedQSH(WOLFSSL* ssl, word16 name)
  688. {
  689. if (ssl == NULL)
  690. return BAD_FUNC_ARG;
  691. switch (name) {
  692. #ifdef HAVE_NTRU
  693. case WOLFSSL_NTRU_EESS439:
  694. case WOLFSSL_NTRU_EESS593:
  695. case WOLFSSL_NTRU_EESS743:
  696. break;
  697. #endif
  698. default:
  699. return BAD_FUNC_ARG;
  700. }
  701. ssl->user_set_QSHSchemes = 1;
  702. return TLSX_UseQSHScheme(&ssl->extensions, name, NULL, 0);
  703. }
  704. #ifndef NO_WOLFSSL_CLIENT
  705. /* user control over sending client public key in hello
  706. when flag = 1 will send keys if flag is 0 or function is not called
  707. then will not send keys in the hello extension
  708. return 0 on success
  709. */
  710. int wolfSSL_UseClientQSHKeys(WOLFSSL* ssl, unsigned char flag)
  711. {
  712. if (ssl == NULL)
  713. return BAD_FUNC_ARG;
  714. ssl->sendQSHKeys = flag;
  715. return 0;
  716. }
  717. #endif /* NO_WOLFSSL_CLIENT */
  718. #endif /* HAVE_QSH */
  719. /* Application-Layer Procotol Negotiation */
  720. #ifdef HAVE_ALPN
  721. int wolfSSL_UseALPN(WOLFSSL* ssl, char *protocol_name_list,
  722. word32 protocol_name_listSz, byte options)
  723. {
  724. char *list, *ptr, *token[10];
  725. word16 len;
  726. int idx = 0;
  727. int ret = SSL_FAILURE;
  728. WOLFSSL_ENTER("wolfSSL_UseALPN");
  729. if (ssl == NULL || protocol_name_list == NULL)
  730. return BAD_FUNC_ARG;
  731. if (protocol_name_listSz > (WOLFSSL_MAX_ALPN_NUMBER *
  732. WOLFSSL_MAX_ALPN_PROTO_NAME_LEN +
  733. WOLFSSL_MAX_ALPN_NUMBER)) {
  734. WOLFSSL_MSG("Invalid arguments, protocol name list too long");
  735. return BAD_FUNC_ARG;
  736. }
  737. if (!(options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) &&
  738. !(options & WOLFSSL_ALPN_FAILED_ON_MISMATCH)) {
  739. WOLFSSL_MSG("Invalid arguments, options not supported");
  740. return BAD_FUNC_ARG;
  741. }
  742. list = (char *)XMALLOC(protocol_name_listSz+1, NULL,
  743. DYNAMIC_TYPE_TMP_BUFFER);
  744. if (list == NULL) {
  745. WOLFSSL_MSG("Memory failure");
  746. return MEMORY_ERROR;
  747. }
  748. XMEMSET(list, 0, protocol_name_listSz+1);
  749. XSTRNCPY(list, protocol_name_list, protocol_name_listSz);
  750. /* read all protocol name from the list */
  751. token[idx] = XSTRTOK(list, ",", &ptr);
  752. while (token[idx] != NULL)
  753. token[++idx] = XSTRTOK(NULL, ",", &ptr);
  754. /* add protocol name list in the TLS extension in reverse order */
  755. while ((idx--) > 0) {
  756. len = (word16)XSTRLEN(token[idx]);
  757. ret = TLSX_UseALPN(&ssl->extensions, token[idx], len, options);
  758. if (ret != SSL_SUCCESS) {
  759. WOLFSSL_MSG("TLSX_UseALPN failure");
  760. break;
  761. }
  762. }
  763. XFREE(list, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  764. return ret;
  765. }
  766. int wolfSSL_ALPN_GetProtocol(WOLFSSL* ssl, char **protocol_name, word16 *size)
  767. {
  768. return TLSX_ALPN_GetRequest(ssl ? ssl->extensions : NULL,
  769. (void **)protocol_name, size);
  770. }
  771. int wolfSSL_ALPN_GetPeerProtocol(WOLFSSL* ssl, char **list, word16 *listSz)
  772. {
  773. if (list == NULL || listSz == NULL)
  774. return BAD_FUNC_ARG;
  775. if (ssl->alpn_client_list == NULL)
  776. return BUFFER_ERROR;
  777. *listSz = (word16)XSTRLEN(ssl->alpn_client_list);
  778. if (*listSz == 0)
  779. return BUFFER_ERROR;
  780. *list = (char *)XMALLOC((*listSz)+1, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  781. if (*list == NULL)
  782. return MEMORY_ERROR;
  783. XSTRNCPY(*list, ssl->alpn_client_list, (*listSz)+1);
  784. (*list)[*listSz] = 0;
  785. return SSL_SUCCESS;
  786. }
  787. #endif /* HAVE_ALPN */
  788. /* Secure Renegotiation */
  789. #ifdef HAVE_SECURE_RENEGOTIATION
  790. /* user is forcing ability to use secure renegotiation, we discourage it */
  791. int wolfSSL_UseSecureRenegotiation(WOLFSSL* ssl)
  792. {
  793. int ret = BAD_FUNC_ARG;
  794. if (ssl)
  795. ret = TLSX_UseSecureRenegotiation(&ssl->extensions);
  796. if (ret == SSL_SUCCESS) {
  797. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_RENEGOTIATION_INFO);
  798. if (extension)
  799. ssl->secure_renegotiation = (SecureRenegotiation*)extension->data;
  800. }
  801. return ret;
  802. }
  803. /* do a secure renegotiation handshake, user forced, we discourage */
  804. int wolfSSL_Rehandshake(WOLFSSL* ssl)
  805. {
  806. int ret;
  807. if (ssl == NULL)
  808. return BAD_FUNC_ARG;
  809. if (ssl->secure_renegotiation == NULL) {
  810. WOLFSSL_MSG("Secure Renegotiation not forced on by user");
  811. return SECURE_RENEGOTIATION_E;
  812. }
  813. if (ssl->secure_renegotiation->enabled == 0) {
  814. WOLFSSL_MSG("Secure Renegotiation not enabled at extension level");
  815. return SECURE_RENEGOTIATION_E;
  816. }
  817. if (ssl->options.handShakeState != HANDSHAKE_DONE) {
  818. WOLFSSL_MSG("Can't renegotiate until previous handshake complete");
  819. return SECURE_RENEGOTIATION_E;
  820. }
  821. #ifndef NO_FORCE_SCR_SAME_SUITE
  822. /* force same suite */
  823. if (ssl->suites) {
  824. ssl->suites->suiteSz = SUITE_LEN;
  825. ssl->suites->suites[0] = ssl->options.cipherSuite0;
  826. ssl->suites->suites[1] = ssl->options.cipherSuite;
  827. }
  828. #endif
  829. /* reset handshake states */
  830. ssl->options.serverState = NULL_STATE;
  831. ssl->options.clientState = NULL_STATE;
  832. ssl->options.connectState = CONNECT_BEGIN;
  833. ssl->options.acceptState = ACCEPT_BEGIN;
  834. ssl->options.handShakeState = NULL_STATE;
  835. ssl->options.processReply = 0; /* TODO, move states in internal.h */
  836. XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
  837. ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED;
  838. #ifndef NO_OLD_TLS
  839. #ifndef NO_MD5
  840. wc_InitMd5(&ssl->hsHashes->hashMd5);
  841. #endif
  842. #ifndef NO_SHA
  843. ret = wc_InitSha(&ssl->hsHashes->hashSha);
  844. if (ret !=0)
  845. return ret;
  846. #endif
  847. #endif /* NO_OLD_TLS */
  848. #ifndef NO_SHA256
  849. ret = wc_InitSha256(&ssl->hsHashes->hashSha256);
  850. if (ret !=0)
  851. return ret;
  852. #endif
  853. #ifdef WOLFSSL_SHA384
  854. ret = wc_InitSha384(&ssl->hsHashes->hashSha384);
  855. if (ret !=0)
  856. return ret;
  857. #endif
  858. #ifdef WOLFSSL_SHA512
  859. ret = wc_InitSha512(&ssl->hsHashes->hashSha512);
  860. if (ret !=0)
  861. return ret;
  862. #endif
  863. ret = wolfSSL_negotiate(ssl);
  864. return ret;
  865. }
  866. #endif /* HAVE_SECURE_RENEGOTIATION */
  867. /* Session Ticket */
  868. #if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SESSION_TICKET)
  869. /* SSL_SUCCESS on ok */
  870. int wolfSSL_CTX_set_TicketEncCb(WOLFSSL_CTX* ctx, SessionTicketEncCb cb)
  871. {
  872. if (ctx == NULL)
  873. return BAD_FUNC_ARG;
  874. ctx->ticketEncCb = cb;
  875. return SSL_SUCCESS;
  876. }
  877. /* set hint interval, SSL_SUCCESS on ok */
  878. int wolfSSL_CTX_set_TicketHint(WOLFSSL_CTX* ctx, int hint)
  879. {
  880. if (ctx == NULL)
  881. return BAD_FUNC_ARG;
  882. ctx->ticketHint = hint;
  883. return SSL_SUCCESS;
  884. }
  885. /* set user context, SSL_SUCCESS on ok */
  886. int wolfSSL_CTX_set_TicketEncCtx(WOLFSSL_CTX* ctx, void* userCtx)
  887. {
  888. if (ctx == NULL)
  889. return BAD_FUNC_ARG;
  890. ctx->ticketEncCtx = userCtx;
  891. return SSL_SUCCESS;
  892. }
  893. #endif /* !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_SESSION_TICKET) */
  894. /* Session Ticket */
  895. #if !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_SESSION_TICKET)
  896. int wolfSSL_UseSessionTicket(WOLFSSL* ssl)
  897. {
  898. if (ssl == NULL)
  899. return BAD_FUNC_ARG;
  900. return TLSX_UseSessionTicket(&ssl->extensions, NULL);
  901. }
  902. int wolfSSL_CTX_UseSessionTicket(WOLFSSL_CTX* ctx)
  903. {
  904. if (ctx == NULL)
  905. return BAD_FUNC_ARG;
  906. return TLSX_UseSessionTicket(&ctx->extensions, NULL);
  907. }
  908. WOLFSSL_API int wolfSSL_get_SessionTicket(WOLFSSL* ssl,
  909. byte* buf, word32* bufSz)
  910. {
  911. if (ssl == NULL || buf == NULL || bufSz == NULL || *bufSz == 0)
  912. return BAD_FUNC_ARG;
  913. if (ssl->session.ticketLen <= *bufSz) {
  914. XMEMCPY(buf, ssl->session.ticket, ssl->session.ticketLen);
  915. *bufSz = ssl->session.ticketLen;
  916. }
  917. else
  918. *bufSz = 0;
  919. return SSL_SUCCESS;
  920. }
  921. WOLFSSL_API int wolfSSL_set_SessionTicket(WOLFSSL* ssl, byte* buf, word32 bufSz)
  922. {
  923. if (ssl == NULL || (buf == NULL && bufSz > 0))
  924. return BAD_FUNC_ARG;
  925. if (bufSz > 0)
  926. XMEMCPY(ssl->session.ticket, buf, bufSz);
  927. ssl->session.ticketLen = (word16)bufSz;
  928. return SSL_SUCCESS;
  929. }
  930. WOLFSSL_API int wolfSSL_set_SessionTicket_cb(WOLFSSL* ssl,
  931. CallbackSessionTicket cb, void* ctx)
  932. {
  933. if (ssl == NULL)
  934. return BAD_FUNC_ARG;
  935. ssl->session_ticket_cb = cb;
  936. ssl->session_ticket_ctx = ctx;
  937. return SSL_SUCCESS;
  938. }
  939. #endif
  940. #ifndef WOLFSSL_LEANPSK
  941. int wolfSSL_send(WOLFSSL* ssl, const void* data, int sz, int flags)
  942. {
  943. int ret;
  944. int oldFlags;
  945. WOLFSSL_ENTER("wolfSSL_send()");
  946. if (ssl == NULL || data == NULL || sz < 0)
  947. return BAD_FUNC_ARG;
  948. oldFlags = ssl->wflags;
  949. ssl->wflags = flags;
  950. ret = wolfSSL_write(ssl, data, sz);
  951. ssl->wflags = oldFlags;
  952. WOLFSSL_LEAVE("wolfSSL_send()", ret);
  953. return ret;
  954. }
  955. int wolfSSL_recv(WOLFSSL* ssl, void* data, int sz, int flags)
  956. {
  957. int ret;
  958. int oldFlags;
  959. WOLFSSL_ENTER("wolfSSL_recv()");
  960. if (ssl == NULL || data == NULL || sz < 0)
  961. return BAD_FUNC_ARG;
  962. oldFlags = ssl->rflags;
  963. ssl->rflags = flags;
  964. ret = wolfSSL_read(ssl, data, sz);
  965. ssl->rflags = oldFlags;
  966. WOLFSSL_LEAVE("wolfSSL_recv()", ret);
  967. return ret;
  968. }
  969. #endif
  970. /* SSL_SUCCESS on ok */
  971. int wolfSSL_shutdown(WOLFSSL* ssl)
  972. {
  973. int ret = SSL_FATAL_ERROR;
  974. byte tmp;
  975. WOLFSSL_ENTER("SSL_shutdown()");
  976. if (ssl == NULL)
  977. return SSL_FATAL_ERROR;
  978. if (ssl->options.quietShutdown) {
  979. WOLFSSL_MSG("quiet shutdown, no close notify sent");
  980. return SSL_SUCCESS;
  981. }
  982. /* try to send close notify, not an error if can't */
  983. if (!ssl->options.isClosed && !ssl->options.connReset &&
  984. !ssl->options.sentNotify) {
  985. ssl->error = SendAlert(ssl, alert_warning, close_notify);
  986. if (ssl->error < 0) {
  987. WOLFSSL_ERROR(ssl->error);
  988. return SSL_FATAL_ERROR;
  989. }
  990. ssl->options.sentNotify = 1; /* don't send close_notify twice */
  991. if (ssl->options.closeNotify)
  992. ret = SSL_SUCCESS;
  993. else
  994. ret = SSL_SHUTDOWN_NOT_DONE;
  995. WOLFSSL_LEAVE("SSL_shutdown()", ret);
  996. return ret;
  997. }
  998. /* call wolfSSL_shutdown again for bidirectional shudown */
  999. if (ssl->options.sentNotify && !ssl->options.closeNotify) {
  1000. ret = wolfSSL_read(ssl, &tmp, 0);
  1001. if (ret < 0) {
  1002. WOLFSSL_ERROR(ssl->error);
  1003. ret = SSL_FATAL_ERROR;
  1004. } else if (ssl->options.closeNotify) {
  1005. ssl->error = SSL_ERROR_SYSCALL; /* simulate OpenSSL behavior */
  1006. ret = SSL_SUCCESS;
  1007. }
  1008. }
  1009. WOLFSSL_LEAVE("SSL_shutdown()", ret);
  1010. return ret;
  1011. }
  1012. int wolfSSL_get_error(WOLFSSL* ssl, int ret)
  1013. {
  1014. WOLFSSL_ENTER("SSL_get_error");
  1015. if (ret > 0)
  1016. return SSL_ERROR_NONE;
  1017. if (ssl == NULL)
  1018. return BAD_FUNC_ARG;
  1019. WOLFSSL_LEAVE("SSL_get_error", ssl->error);
  1020. /* make sure converted types are handled in SetErrorString() too */
  1021. if (ssl->error == WANT_READ)
  1022. return SSL_ERROR_WANT_READ; /* convert to OpenSSL type */
  1023. else if (ssl->error == WANT_WRITE)
  1024. return SSL_ERROR_WANT_WRITE; /* convert to OpenSSL type */
  1025. else if (ssl->error == ZERO_RETURN)
  1026. return SSL_ERROR_ZERO_RETURN; /* convert to OpenSSL type */
  1027. return ssl->error;
  1028. }
  1029. /* retrive alert history, SSL_SUCCESS on ok */
  1030. int wolfSSL_get_alert_history(WOLFSSL* ssl, WOLFSSL_ALERT_HISTORY *h)
  1031. {
  1032. if (ssl && h) {
  1033. *h = ssl->alert_history;
  1034. }
  1035. return SSL_SUCCESS;
  1036. }
  1037. /* return TRUE if current error is want read */
  1038. int wolfSSL_want_read(WOLFSSL* ssl)
  1039. {
  1040. WOLFSSL_ENTER("SSL_want_read");
  1041. if (ssl->error == WANT_READ)
  1042. return 1;
  1043. return 0;
  1044. }
  1045. /* return TRUE if current error is want write */
  1046. int wolfSSL_want_write(WOLFSSL* ssl)
  1047. {
  1048. WOLFSSL_ENTER("SSL_want_write");
  1049. if (ssl->error == WANT_WRITE)
  1050. return 1;
  1051. return 0;
  1052. }
  1053. char* wolfSSL_ERR_error_string(unsigned long errNumber, char* data)
  1054. {
  1055. static const char* msg = "Please supply a buffer for error string";
  1056. WOLFSSL_ENTER("ERR_error_string");
  1057. if (data) {
  1058. SetErrorString((int)errNumber, data);
  1059. return data;
  1060. }
  1061. return (char*)msg;
  1062. }
  1063. void wolfSSL_ERR_error_string_n(unsigned long e, char* buf, unsigned long len)
  1064. {
  1065. WOLFSSL_ENTER("wolfSSL_ERR_error_string_n");
  1066. if (len >= WOLFSSL_MAX_ERROR_SZ)
  1067. wolfSSL_ERR_error_string(e, buf);
  1068. else {
  1069. char tmp[WOLFSSL_MAX_ERROR_SZ];
  1070. WOLFSSL_MSG("Error buffer too short, truncating");
  1071. if (len) {
  1072. wolfSSL_ERR_error_string(e, tmp);
  1073. XMEMCPY(buf, tmp, len-1);
  1074. buf[len-1] = '\0';
  1075. }
  1076. }
  1077. }
  1078. /* don't free temporary arrays at end of handshake */
  1079. void wolfSSL_KeepArrays(WOLFSSL* ssl)
  1080. {
  1081. if (ssl)
  1082. ssl->options.saveArrays = 1;
  1083. }
  1084. /* user doesn't need temporary arrays anymore, Free */
  1085. void wolfSSL_FreeArrays(WOLFSSL* ssl)
  1086. {
  1087. if (ssl && ssl->options.handShakeState == HANDSHAKE_DONE) {
  1088. ssl->options.saveArrays = 0;
  1089. FreeArrays(ssl, 1);
  1090. }
  1091. }
  1092. const byte* wolfSSL_GetMacSecret(WOLFSSL* ssl, int verify)
  1093. {
  1094. if (ssl == NULL)
  1095. return NULL;
  1096. if ( (ssl->options.side == WOLFSSL_CLIENT_END && !verify) ||
  1097. (ssl->options.side == WOLFSSL_SERVER_END && verify) )
  1098. return ssl->keys.client_write_MAC_secret;
  1099. else
  1100. return ssl->keys.server_write_MAC_secret;
  1101. }
  1102. #ifdef ATOMIC_USER
  1103. void wolfSSL_CTX_SetMacEncryptCb(WOLFSSL_CTX* ctx, CallbackMacEncrypt cb)
  1104. {
  1105. if (ctx)
  1106. ctx->MacEncryptCb = cb;
  1107. }
  1108. void wolfSSL_SetMacEncryptCtx(WOLFSSL* ssl, void *ctx)
  1109. {
  1110. if (ssl)
  1111. ssl->MacEncryptCtx = ctx;
  1112. }
  1113. void* wolfSSL_GetMacEncryptCtx(WOLFSSL* ssl)
  1114. {
  1115. if (ssl)
  1116. return ssl->MacEncryptCtx;
  1117. return NULL;
  1118. }
  1119. void wolfSSL_CTX_SetDecryptVerifyCb(WOLFSSL_CTX* ctx, CallbackDecryptVerify cb)
  1120. {
  1121. if (ctx)
  1122. ctx->DecryptVerifyCb = cb;
  1123. }
  1124. void wolfSSL_SetDecryptVerifyCtx(WOLFSSL* ssl, void *ctx)
  1125. {
  1126. if (ssl)
  1127. ssl->DecryptVerifyCtx = ctx;
  1128. }
  1129. void* wolfSSL_GetDecryptVerifyCtx(WOLFSSL* ssl)
  1130. {
  1131. if (ssl)
  1132. return ssl->DecryptVerifyCtx;
  1133. return NULL;
  1134. }
  1135. const byte* wolfSSL_GetClientWriteKey(WOLFSSL* ssl)
  1136. {
  1137. if (ssl)
  1138. return ssl->keys.client_write_key;
  1139. return NULL;
  1140. }
  1141. const byte* wolfSSL_GetClientWriteIV(WOLFSSL* ssl)
  1142. {
  1143. if (ssl)
  1144. return ssl->keys.client_write_IV;
  1145. return NULL;
  1146. }
  1147. const byte* wolfSSL_GetServerWriteKey(WOLFSSL* ssl)
  1148. {
  1149. if (ssl)
  1150. return ssl->keys.server_write_key;
  1151. return NULL;
  1152. }
  1153. const byte* wolfSSL_GetServerWriteIV(WOLFSSL* ssl)
  1154. {
  1155. if (ssl)
  1156. return ssl->keys.server_write_IV;
  1157. return NULL;
  1158. }
  1159. int wolfSSL_GetKeySize(WOLFSSL* ssl)
  1160. {
  1161. if (ssl)
  1162. return ssl->specs.key_size;
  1163. return BAD_FUNC_ARG;
  1164. }
  1165. int wolfSSL_GetIVSize(WOLFSSL* ssl)
  1166. {
  1167. if (ssl)
  1168. return ssl->specs.iv_size;
  1169. return BAD_FUNC_ARG;
  1170. }
  1171. int wolfSSL_GetBulkCipher(WOLFSSL* ssl)
  1172. {
  1173. if (ssl)
  1174. return ssl->specs.bulk_cipher_algorithm;
  1175. return BAD_FUNC_ARG;
  1176. }
  1177. int wolfSSL_GetCipherType(WOLFSSL* ssl)
  1178. {
  1179. if (ssl == NULL)
  1180. return BAD_FUNC_ARG;
  1181. if (ssl->specs.cipher_type == block)
  1182. return WOLFSSL_BLOCK_TYPE;
  1183. if (ssl->specs.cipher_type == stream)
  1184. return WOLFSSL_STREAM_TYPE;
  1185. if (ssl->specs.cipher_type == aead)
  1186. return WOLFSSL_AEAD_TYPE;
  1187. return -1;
  1188. }
  1189. int wolfSSL_GetCipherBlockSize(WOLFSSL* ssl)
  1190. {
  1191. if (ssl == NULL)
  1192. return BAD_FUNC_ARG;
  1193. return ssl->specs.block_size;
  1194. }
  1195. int wolfSSL_GetAeadMacSize(WOLFSSL* ssl)
  1196. {
  1197. if (ssl == NULL)
  1198. return BAD_FUNC_ARG;
  1199. return ssl->specs.aead_mac_size;
  1200. }
  1201. int wolfSSL_IsTLSv1_1(WOLFSSL* ssl)
  1202. {
  1203. if (ssl == NULL)
  1204. return BAD_FUNC_ARG;
  1205. if (ssl->options.tls1_1)
  1206. return 1;
  1207. return 0;
  1208. }
  1209. int wolfSSL_GetSide(WOLFSSL* ssl)
  1210. {
  1211. if (ssl)
  1212. return ssl->options.side;
  1213. return BAD_FUNC_ARG;
  1214. }
  1215. int wolfSSL_GetHmacSize(WOLFSSL* ssl)
  1216. {
  1217. /* AEAD ciphers don't have HMAC keys */
  1218. if (ssl)
  1219. return (ssl->specs.cipher_type != aead) ? ssl->specs.hash_size : 0;
  1220. return BAD_FUNC_ARG;
  1221. }
  1222. #endif /* ATOMIC_USER */
  1223. #ifndef NO_CERTS
  1224. WOLFSSL_CERT_MANAGER* wolfSSL_CertManagerNew(void)
  1225. {
  1226. WOLFSSL_CERT_MANAGER* cm = NULL;
  1227. WOLFSSL_ENTER("wolfSSL_CertManagerNew");
  1228. cm = (WOLFSSL_CERT_MANAGER*) XMALLOC(sizeof(WOLFSSL_CERT_MANAGER), 0,
  1229. DYNAMIC_TYPE_CERT_MANAGER);
  1230. if (cm) {
  1231. XMEMSET(cm, 0, sizeof(WOLFSSL_CERT_MANAGER));
  1232. if (InitMutex(&cm->caLock) != 0) {
  1233. WOLFSSL_MSG("Bad mutex init");
  1234. wolfSSL_CertManagerFree(cm);
  1235. return NULL;
  1236. }
  1237. }
  1238. return cm;
  1239. }
  1240. void wolfSSL_CertManagerFree(WOLFSSL_CERT_MANAGER* cm)
  1241. {
  1242. WOLFSSL_ENTER("wolfSSL_CertManagerFree");
  1243. if (cm) {
  1244. #ifdef HAVE_CRL
  1245. if (cm->crl)
  1246. FreeCRL(cm->crl, 1);
  1247. #endif
  1248. #ifdef HAVE_OCSP
  1249. if (cm->ocsp)
  1250. FreeOCSP(cm->ocsp, 1);
  1251. #endif
  1252. FreeSignerTable(cm->caTable, CA_TABLE_SIZE, NULL);
  1253. FreeMutex(&cm->caLock);
  1254. XFREE(cm, NULL, DYNAMIC_TYPE_CERT_MANAGER);
  1255. }
  1256. }
  1257. /* Unload the CA signer list */
  1258. int wolfSSL_CertManagerUnloadCAs(WOLFSSL_CERT_MANAGER* cm)
  1259. {
  1260. WOLFSSL_ENTER("wolfSSL_CertManagerUnloadCAs");
  1261. if (cm == NULL)
  1262. return BAD_FUNC_ARG;
  1263. if (LockMutex(&cm->caLock) != 0)
  1264. return BAD_MUTEX_E;
  1265. FreeSignerTable(cm->caTable, CA_TABLE_SIZE, NULL);
  1266. UnLockMutex(&cm->caLock);
  1267. return SSL_SUCCESS;
  1268. }
  1269. /* Return bytes written to buff or < 0 for error */
  1270. int wolfSSL_CertPemToDer(const unsigned char* pem, int pemSz,
  1271. unsigned char* buff, int buffSz, int type)
  1272. {
  1273. int eccKey = 0;
  1274. int ret;
  1275. buffer der;
  1276. #ifdef WOLFSSL_SMALL_STACK
  1277. EncryptedInfo* info = NULL;
  1278. #else
  1279. EncryptedInfo info[1];
  1280. #endif
  1281. WOLFSSL_ENTER("wolfSSL_CertPemToDer");
  1282. if (pem == NULL || buff == NULL || buffSz <= 0) {
  1283. WOLFSSL_MSG("Bad pem der args");
  1284. return BAD_FUNC_ARG;
  1285. }
  1286. if (type != CERT_TYPE && type != CA_TYPE && type != CERTREQ_TYPE) {
  1287. WOLFSSL_MSG("Bad cert type");
  1288. return BAD_FUNC_ARG;
  1289. }
  1290. #ifdef WOLFSSL_SMALL_STACK
  1291. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  1292. DYNAMIC_TYPE_TMP_BUFFER);
  1293. if (info == NULL)
  1294. return MEMORY_E;
  1295. #endif
  1296. info->set = 0;
  1297. info->ctx = NULL;
  1298. info->consumed = 0;
  1299. der.buffer = NULL;
  1300. ret = PemToDer(pem, pemSz, type, &der, NULL, info, &eccKey);
  1301. #ifdef WOLFSSL_SMALL_STACK
  1302. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1303. #endif
  1304. if (ret < 0) {
  1305. WOLFSSL_MSG("Bad Pem To Der");
  1306. }
  1307. else {
  1308. if (der.length <= (word32)buffSz) {
  1309. XMEMCPY(buff, der.buffer, der.length);
  1310. ret = der.length;
  1311. }
  1312. else {
  1313. WOLFSSL_MSG("Bad der length");
  1314. ret = BAD_FUNC_ARG;
  1315. }
  1316. }
  1317. XFREE(der.buffer, NULL, DYNAMIC_TYPE_KEY);
  1318. return ret;
  1319. }
  1320. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1321. static const char *EVP_AES_128_CBC = "AES-128-CBC";
  1322. static const char *EVP_AES_192_CBC = "AES-192-CBC";
  1323. static const char *EVP_AES_256_CBC = "AES-256-CBC";
  1324. #if defined(OPENSSL_EXTRA)
  1325. static const char *EVP_AES_128_CTR = "AES-128-CTR";
  1326. static const char *EVP_AES_192_CTR = "AES-192-CTR";
  1327. static const char *EVP_AES_256_CTR = "AES-256-CTR";
  1328. #endif
  1329. static const int EVP_AES_SIZE = 11;
  1330. static const char *EVP_DES_CBC = "DES-CBC";
  1331. static const int EVP_DES_SIZE = 7;
  1332. static const char *EVP_DES_EDE3_CBC = "DES-EDE3-CBC";
  1333. static const int EVP_DES_EDE3_SIZE = 12;
  1334. #ifdef HAVE_IDEA
  1335. static const char *EVP_IDEA_CBC = "IDEA-CBC";
  1336. static const int EVP_IDEA_SIZE = 8;
  1337. #endif
  1338. /* our KeyPemToDer password callback, password in userData */
  1339. static INLINE int OurPasswordCb(char* passwd, int sz, int rw, void* userdata)
  1340. {
  1341. (void)rw;
  1342. if (userdata == NULL)
  1343. return 0;
  1344. XSTRNCPY(passwd, (char*)userdata, sz);
  1345. return min((word32)sz, (word32)XSTRLEN((char*)userdata));
  1346. }
  1347. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  1348. /* Return bytes written to buff or < 0 for error */
  1349. int wolfSSL_KeyPemToDer(const unsigned char* pem, int pemSz,
  1350. unsigned char* buff, int buffSz, const char* pass)
  1351. {
  1352. int eccKey = 0;
  1353. int ret;
  1354. buffer der;
  1355. #ifdef WOLFSSL_SMALL_STACK
  1356. EncryptedInfo* info = NULL;
  1357. #else
  1358. EncryptedInfo info[1];
  1359. #endif
  1360. WOLFSSL_ENTER("wolfSSL_KeyPemToDer");
  1361. if (pem == NULL || buff == NULL || buffSz <= 0) {
  1362. WOLFSSL_MSG("Bad pem der args");
  1363. return BAD_FUNC_ARG;
  1364. }
  1365. #ifdef WOLFSSL_SMALL_STACK
  1366. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  1367. DYNAMIC_TYPE_TMP_BUFFER);
  1368. if (info == NULL)
  1369. return MEMORY_E;
  1370. #endif
  1371. info->set = 0;
  1372. info->ctx = NULL;
  1373. info->consumed = 0;
  1374. der.buffer = NULL;
  1375. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1376. if (pass) {
  1377. info->ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1378. if (info->ctx == NULL) {
  1379. #ifdef WOLFSSL_SMALL_STACK
  1380. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1381. #endif
  1382. return MEMORY_E;
  1383. }
  1384. wolfSSL_CTX_set_default_passwd_cb(info->ctx, OurPasswordCb);
  1385. wolfSSL_CTX_set_default_passwd_cb_userdata(info->ctx, (void*)pass);
  1386. }
  1387. #else
  1388. (void)pass;
  1389. #endif
  1390. ret = PemToDer(pem, pemSz, PRIVATEKEY_TYPE, &der, NULL, info, &eccKey);
  1391. if (info->ctx)
  1392. wolfSSL_CTX_free(info->ctx);
  1393. #ifdef WOLFSSL_SMALL_STACK
  1394. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1395. #endif
  1396. if (ret < 0) {
  1397. WOLFSSL_MSG("Bad Pem To Der");
  1398. }
  1399. else {
  1400. if (der.length <= (word32)buffSz) {
  1401. XMEMCPY(buff, der.buffer, der.length);
  1402. ret = der.length;
  1403. }
  1404. else {
  1405. WOLFSSL_MSG("Bad der length");
  1406. ret = BAD_FUNC_ARG;
  1407. }
  1408. }
  1409. XFREE(der.buffer, NULL, DYNAMIC_TYPE_KEY);
  1410. return ret;
  1411. }
  1412. #endif /* !NO_CERTS */
  1413. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  1414. void wolfSSL_ERR_print_errors_fp(FILE* fp, int err)
  1415. {
  1416. char data[WOLFSSL_MAX_ERROR_SZ + 1];
  1417. WOLFSSL_ENTER("wolfSSL_ERR_print_errors_fp");
  1418. SetErrorString(err, data);
  1419. fprintf(fp, "%s", data);
  1420. }
  1421. #endif
  1422. int wolfSSL_pending(WOLFSSL* ssl)
  1423. {
  1424. WOLFSSL_ENTER("SSL_pending");
  1425. return ssl->buffers.clearOutputBuffer.length;
  1426. }
  1427. #ifndef WOLFSSL_LEANPSK
  1428. /* turn on handshake group messages for context */
  1429. int wolfSSL_CTX_set_group_messages(WOLFSSL_CTX* ctx)
  1430. {
  1431. if (ctx == NULL)
  1432. return BAD_FUNC_ARG;
  1433. ctx->groupMessages = 1;
  1434. return SSL_SUCCESS;
  1435. }
  1436. #endif
  1437. #ifndef NO_WOLFSSL_CLIENT
  1438. /* connect enough to get peer cert chain */
  1439. int wolfSSL_connect_cert(WOLFSSL* ssl)
  1440. {
  1441. int ret;
  1442. if (ssl == NULL)
  1443. return SSL_FAILURE;
  1444. ssl->options.certOnly = 1;
  1445. ret = wolfSSL_connect(ssl);
  1446. ssl->options.certOnly = 0;
  1447. return ret;
  1448. }
  1449. #endif
  1450. #ifndef WOLFSSL_LEANPSK
  1451. /* turn on handshake group messages for ssl object */
  1452. int wolfSSL_set_group_messages(WOLFSSL* ssl)
  1453. {
  1454. if (ssl == NULL)
  1455. return BAD_FUNC_ARG;
  1456. ssl->options.groupMessages = 1;
  1457. return SSL_SUCCESS;
  1458. }
  1459. /* make minVersion the internal equivalent SSL version */
  1460. static int SetMinVersionHelper(byte* minVersion, int version)
  1461. {
  1462. switch (version) {
  1463. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  1464. case WOLFSSL_SSLV3:
  1465. *minVersion = SSLv3_MINOR;
  1466. break;
  1467. #endif
  1468. #ifndef NO_TLS
  1469. #ifndef NO_OLD_TLS
  1470. case WOLFSSL_TLSV1:
  1471. *minVersion = TLSv1_MINOR;
  1472. break;
  1473. case WOLFSSL_TLSV1_1:
  1474. *minVersion = TLSv1_1_MINOR;
  1475. break;
  1476. #endif
  1477. case WOLFSSL_TLSV1_2:
  1478. *minVersion = TLSv1_2_MINOR;
  1479. break;
  1480. #endif
  1481. default:
  1482. WOLFSSL_MSG("Bad function argument");
  1483. return BAD_FUNC_ARG;
  1484. }
  1485. return SSL_SUCCESS;
  1486. }
  1487. /* Set minimum downgrade version allowed, SSL_SUCCESS on ok */
  1488. int wolfSSL_CTX_SetMinVersion(WOLFSSL_CTX* ctx, int version)
  1489. {
  1490. WOLFSSL_ENTER("wolfSSL_CTX_SetMinVersion");
  1491. if (ctx == NULL) {
  1492. WOLFSSL_MSG("Bad function argument");
  1493. return BAD_FUNC_ARG;
  1494. }
  1495. return SetMinVersionHelper(&ctx->minDowngrade, version);
  1496. }
  1497. /* Set minimum downgrade version allowed, SSL_SUCCESS on ok */
  1498. int wolfSSL_SetMinVersion(WOLFSSL* ssl, int version)
  1499. {
  1500. WOLFSSL_ENTER("wolfSSL_SetMinVersion");
  1501. if (ssl == NULL) {
  1502. WOLFSSL_MSG("Bad function argument");
  1503. return BAD_FUNC_ARG;
  1504. }
  1505. return SetMinVersionHelper(&ssl->options.minDowngrade, version);
  1506. }
  1507. int wolfSSL_SetVersion(WOLFSSL* ssl, int version)
  1508. {
  1509. byte haveRSA = 1;
  1510. byte havePSK = 0;
  1511. WOLFSSL_ENTER("wolfSSL_SetVersion");
  1512. if (ssl == NULL) {
  1513. WOLFSSL_MSG("Bad function argument");
  1514. return BAD_FUNC_ARG;
  1515. }
  1516. switch (version) {
  1517. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  1518. case WOLFSSL_SSLV3:
  1519. ssl->version = MakeSSLv3();
  1520. break;
  1521. #endif
  1522. #ifndef NO_TLS
  1523. #ifndef NO_OLD_TLS
  1524. case WOLFSSL_TLSV1:
  1525. ssl->version = MakeTLSv1();
  1526. break;
  1527. case WOLFSSL_TLSV1_1:
  1528. ssl->version = MakeTLSv1_1();
  1529. break;
  1530. #endif
  1531. case WOLFSSL_TLSV1_2:
  1532. ssl->version = MakeTLSv1_2();
  1533. break;
  1534. #endif
  1535. default:
  1536. WOLFSSL_MSG("Bad function argument");
  1537. return BAD_FUNC_ARG;
  1538. }
  1539. #ifdef NO_RSA
  1540. haveRSA = 0;
  1541. #endif
  1542. #ifndef NO_PSK
  1543. havePSK = ssl->options.havePSK;
  1544. #endif
  1545. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, ssl->options.haveDH,
  1546. ssl->options.haveNTRU, ssl->options.haveECDSAsig,
  1547. ssl->options.haveStaticECC, ssl->options.side);
  1548. return SSL_SUCCESS;
  1549. }
  1550. #endif /* !leanpsk */
  1551. #if !defined(NO_CERTS) || !defined(NO_SESSION_CACHE)
  1552. /* Make a work from the front of random hash */
  1553. static INLINE word32 MakeWordFromHash(const byte* hashID)
  1554. {
  1555. return (hashID[0] << 24) | (hashID[1] << 16) | (hashID[2] << 8) |
  1556. hashID[3];
  1557. }
  1558. #endif /* !NO_CERTS || !NO_SESSION_CACHE */
  1559. #ifndef NO_CERTS
  1560. /* hash is the SHA digest of name, just use first 32 bits as hash */
  1561. static INLINE word32 HashSigner(const byte* hash)
  1562. {
  1563. return MakeWordFromHash(hash) % CA_TABLE_SIZE;
  1564. }
  1565. /* does CA already exist on signer list */
  1566. int AlreadySigner(WOLFSSL_CERT_MANAGER* cm, byte* hash)
  1567. {
  1568. Signer* signers;
  1569. int ret = 0;
  1570. word32 row = HashSigner(hash);
  1571. if (LockMutex(&cm->caLock) != 0)
  1572. return ret;
  1573. signers = cm->caTable[row];
  1574. while (signers) {
  1575. byte* subjectHash;
  1576. #ifndef NO_SKID
  1577. subjectHash = signers->subjectKeyIdHash;
  1578. #else
  1579. subjectHash = signers->subjectNameHash;
  1580. #endif
  1581. if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
  1582. ret = 1;
  1583. break;
  1584. }
  1585. signers = signers->next;
  1586. }
  1587. UnLockMutex(&cm->caLock);
  1588. return ret;
  1589. }
  1590. /* return CA if found, otherwise NULL */
  1591. Signer* GetCA(void* vp, byte* hash)
  1592. {
  1593. WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
  1594. Signer* ret = NULL;
  1595. Signer* signers;
  1596. word32 row = HashSigner(hash);
  1597. if (cm == NULL)
  1598. return NULL;
  1599. if (LockMutex(&cm->caLock) != 0)
  1600. return ret;
  1601. signers = cm->caTable[row];
  1602. while (signers) {
  1603. byte* subjectHash;
  1604. #ifndef NO_SKID
  1605. subjectHash = signers->subjectKeyIdHash;
  1606. #else
  1607. subjectHash = signers->subjectNameHash;
  1608. #endif
  1609. if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
  1610. ret = signers;
  1611. break;
  1612. }
  1613. signers = signers->next;
  1614. }
  1615. UnLockMutex(&cm->caLock);
  1616. return ret;
  1617. }
  1618. #ifndef NO_SKID
  1619. /* return CA if found, otherwise NULL. Walk through hash table. */
  1620. Signer* GetCAByName(void* vp, byte* hash)
  1621. {
  1622. WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
  1623. Signer* ret = NULL;
  1624. Signer* signers;
  1625. word32 row;
  1626. if (cm == NULL)
  1627. return NULL;
  1628. if (LockMutex(&cm->caLock) != 0)
  1629. return ret;
  1630. for (row = 0; row < CA_TABLE_SIZE && ret == NULL; row++) {
  1631. signers = cm->caTable[row];
  1632. while (signers && ret == NULL) {
  1633. if (XMEMCMP(hash, signers->subjectNameHash,
  1634. SIGNER_DIGEST_SIZE) == 0) {
  1635. ret = signers;
  1636. }
  1637. signers = signers->next;
  1638. }
  1639. }
  1640. UnLockMutex(&cm->caLock);
  1641. return ret;
  1642. }
  1643. #endif
  1644. /* owns der, internal now uses too */
  1645. /* type flag ids from user or from chain received during verify
  1646. don't allow chain ones to be added w/o isCA extension */
  1647. int AddCA(WOLFSSL_CERT_MANAGER* cm, buffer der, int type, int verify)
  1648. {
  1649. int ret;
  1650. Signer* signer = 0;
  1651. word32 row;
  1652. byte* subjectHash;
  1653. #ifdef WOLFSSL_SMALL_STACK
  1654. DecodedCert* cert = NULL;
  1655. #else
  1656. DecodedCert cert[1];
  1657. #endif
  1658. WOLFSSL_MSG("Adding a CA");
  1659. #ifdef WOLFSSL_SMALL_STACK
  1660. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  1661. DYNAMIC_TYPE_TMP_BUFFER);
  1662. if (cert == NULL)
  1663. return MEMORY_E;
  1664. #endif
  1665. InitDecodedCert(cert, der.buffer, der.length, cm->heap);
  1666. ret = ParseCert(cert, CA_TYPE, verify, cm);
  1667. WOLFSSL_MSG(" Parsed new CA");
  1668. #ifndef NO_SKID
  1669. subjectHash = cert->extSubjKeyId;
  1670. #else
  1671. subjectHash = cert->subjectHash;
  1672. #endif
  1673. if (ret == 0 && cert->isCA == 0 && type != WOLFSSL_USER_CA) {
  1674. WOLFSSL_MSG(" Can't add as CA if not actually one");
  1675. ret = NOT_CA_ERROR;
  1676. }
  1677. #ifndef ALLOW_INVALID_CERTSIGN
  1678. else if (ret == 0 && cert->isCA == 1 && type != WOLFSSL_USER_CA &&
  1679. (cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) {
  1680. /* Intermediate CA certs are required to have the keyCertSign
  1681. * extension set. User loaded root certs are not. */
  1682. WOLFSSL_MSG(" Doesn't have key usage certificate signing");
  1683. ret = NOT_CA_ERROR;
  1684. }
  1685. #endif
  1686. else if (ret == 0 && AlreadySigner(cm, subjectHash)) {
  1687. WOLFSSL_MSG(" Already have this CA, not adding again");
  1688. (void)ret;
  1689. }
  1690. else if (ret == 0) {
  1691. /* take over signer parts */
  1692. signer = MakeSigner(cm->heap);
  1693. if (!signer)
  1694. ret = MEMORY_ERROR;
  1695. else {
  1696. signer->keyOID = cert->keyOID;
  1697. signer->publicKey = cert->publicKey;
  1698. signer->pubKeySize = cert->pubKeySize;
  1699. signer->nameLen = cert->subjectCNLen;
  1700. signer->name = cert->subjectCN;
  1701. #ifndef IGNORE_NAME_CONSTRAINTS
  1702. signer->permittedNames = cert->permittedNames;
  1703. signer->excludedNames = cert->excludedNames;
  1704. #endif
  1705. #ifndef NO_SKID
  1706. XMEMCPY(signer->subjectKeyIdHash, cert->extSubjKeyId,
  1707. SIGNER_DIGEST_SIZE);
  1708. #endif
  1709. XMEMCPY(signer->subjectNameHash, cert->subjectHash,
  1710. SIGNER_DIGEST_SIZE);
  1711. signer->keyUsage = cert->extKeyUsageSet ? cert->extKeyUsage
  1712. : 0xFFFF;
  1713. signer->next = NULL; /* If Key Usage not set, all uses valid. */
  1714. cert->publicKey = 0; /* in case lock fails don't free here. */
  1715. cert->subjectCN = 0;
  1716. #ifndef IGNORE_NAME_CONSTRAINTS
  1717. cert->permittedNames = NULL;
  1718. cert->excludedNames = NULL;
  1719. #endif
  1720. #ifndef NO_SKID
  1721. row = HashSigner(signer->subjectKeyIdHash);
  1722. #else
  1723. row = HashSigner(signer->subjectNameHash);
  1724. #endif
  1725. if (LockMutex(&cm->caLock) == 0) {
  1726. signer->next = cm->caTable[row];
  1727. cm->caTable[row] = signer; /* takes ownership */
  1728. UnLockMutex(&cm->caLock);
  1729. if (cm->caCacheCallback)
  1730. cm->caCacheCallback(der.buffer, (int)der.length, type);
  1731. }
  1732. else {
  1733. WOLFSSL_MSG(" CA Mutex Lock failed");
  1734. ret = BAD_MUTEX_E;
  1735. FreeSigner(signer, cm->heap);
  1736. }
  1737. }
  1738. }
  1739. WOLFSSL_MSG(" Freeing Parsed CA");
  1740. FreeDecodedCert(cert);
  1741. #ifdef WOLFSSL_SMALL_STACK
  1742. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1743. #endif
  1744. WOLFSSL_MSG(" Freeing der CA");
  1745. XFREE(der.buffer, cm->heap, DYNAMIC_TYPE_CA);
  1746. WOLFSSL_MSG(" OK Freeing der CA");
  1747. WOLFSSL_LEAVE("AddCA", ret);
  1748. return ret == 0 ? SSL_SUCCESS : ret;
  1749. }
  1750. #endif /* !NO_CERTS */
  1751. #ifndef NO_SESSION_CACHE
  1752. /* basic config gives a cache with 33 sessions, adequate for clients and
  1753. embedded servers
  1754. MEDIUM_SESSION_CACHE allows 1055 sessions, adequate for servers that
  1755. aren't under heavy load, basically allows 200 new sessions per minute
  1756. BIG_SESSION_CACHE yields 20,027 sessions
  1757. HUGE_SESSION_CACHE yields 65,791 sessions, for servers under heavy load,
  1758. allows over 13,000 new sessions per minute or over 200 new sessions per
  1759. second
  1760. SMALL_SESSION_CACHE only stores 6 sessions, good for embedded clients
  1761. or systems where the default of nearly 3kB is too much RAM, this define
  1762. uses less than 500 bytes RAM
  1763. default SESSION_CACHE stores 33 sessions (no XXX_SESSION_CACHE defined)
  1764. */
  1765. #ifdef HUGE_SESSION_CACHE
  1766. #define SESSIONS_PER_ROW 11
  1767. #define SESSION_ROWS 5981
  1768. #elif defined(BIG_SESSION_CACHE)
  1769. #define SESSIONS_PER_ROW 7
  1770. #define SESSION_ROWS 2861
  1771. #elif defined(MEDIUM_SESSION_CACHE)
  1772. #define SESSIONS_PER_ROW 5
  1773. #define SESSION_ROWS 211
  1774. #elif defined(SMALL_SESSION_CACHE)
  1775. #define SESSIONS_PER_ROW 2
  1776. #define SESSION_ROWS 3
  1777. #else
  1778. #define SESSIONS_PER_ROW 3
  1779. #define SESSION_ROWS 11
  1780. #endif
  1781. typedef struct SessionRow {
  1782. int nextIdx; /* where to place next one */
  1783. int totalCount; /* sessions ever on this row */
  1784. WOLFSSL_SESSION Sessions[SESSIONS_PER_ROW];
  1785. } SessionRow;
  1786. static SessionRow SessionCache[SESSION_ROWS];
  1787. #if defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS)
  1788. static word32 PeakSessions;
  1789. #endif
  1790. static wolfSSL_Mutex session_mutex; /* SessionCache mutex */
  1791. #ifndef NO_CLIENT_CACHE
  1792. typedef struct ClientSession {
  1793. word16 serverRow; /* SessionCache Row id */
  1794. word16 serverIdx; /* SessionCache Idx (column) */
  1795. } ClientSession;
  1796. typedef struct ClientRow {
  1797. int nextIdx; /* where to place next one */
  1798. int totalCount; /* sessions ever on this row */
  1799. ClientSession Clients[SESSIONS_PER_ROW];
  1800. } ClientRow;
  1801. static ClientRow ClientCache[SESSION_ROWS]; /* Client Cache */
  1802. /* uses session mutex */
  1803. #endif /* NO_CLIENT_CACHE */
  1804. #endif /* NO_SESSION_CACHE */
  1805. int wolfSSL_Init(void)
  1806. {
  1807. int ret = SSL_SUCCESS;
  1808. WOLFSSL_ENTER("wolfSSL_Init");
  1809. if (initRefCount == 0) {
  1810. #ifndef NO_SESSION_CACHE
  1811. if (InitMutex(&session_mutex) != 0)
  1812. ret = BAD_MUTEX_E;
  1813. #endif
  1814. if (InitMutex(&count_mutex) != 0)
  1815. ret = BAD_MUTEX_E;
  1816. }
  1817. if (ret == SSL_SUCCESS) {
  1818. if (LockMutex(&count_mutex) != 0) {
  1819. WOLFSSL_MSG("Bad Lock Mutex count");
  1820. return BAD_MUTEX_E;
  1821. }
  1822. /* Initialize crypto for use with TLS connection */
  1823. if (wolfcrypt_Init() != 0)
  1824. ret = WC_FAILURE_E;
  1825. initRefCount++;
  1826. UnLockMutex(&count_mutex);
  1827. }
  1828. return ret;
  1829. }
  1830. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1831. /* SSL_SUCCESS if ok, <= 0 else */
  1832. static int wolfssl_decrypt_buffer_key(buffer* der, byte* password,
  1833. int passwordSz, EncryptedInfo* info)
  1834. {
  1835. int ret;
  1836. #ifdef WOLFSSL_SMALL_STACK
  1837. byte* key = NULL;
  1838. #else
  1839. byte key[AES_256_KEY_SIZE];
  1840. #endif
  1841. WOLFSSL_ENTER("wolfssl_decrypt_buffer_key");
  1842. if (der == NULL || password == NULL || info == NULL) {
  1843. WOLFSSL_MSG("bad arguments");
  1844. return SSL_FATAL_ERROR;
  1845. }
  1846. /* use file's salt for key derivation, hex decode first */
  1847. if (Base16_Decode(info->iv, info->ivSz, info->iv, &info->ivSz) != 0) {
  1848. WOLFSSL_MSG("base16 decode failed");
  1849. return SSL_FATAL_ERROR;
  1850. }
  1851. #ifndef NO_MD5
  1852. #ifdef WOLFSSL_SMALL_STACK
  1853. key = (byte*)XMALLOC(AES_256_KEY_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1854. if (key == NULL) {
  1855. WOLFSSL_MSG("memory failure");
  1856. return SSL_FATAL_ERROR;
  1857. }
  1858. #endif /* WOLFSSL_SMALL_STACK */
  1859. if ((ret = wolfSSL_EVP_BytesToKey(info->name, "MD5", info->iv,
  1860. password, passwordSz, 1, key, NULL)) <= 0) {
  1861. WOLFSSL_MSG("bytes to key failure");
  1862. #ifdef WOLFSSL_SMALL_STACK
  1863. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1864. #endif
  1865. return SSL_FATAL_ERROR;
  1866. }
  1867. #else
  1868. (void) passwordSz;
  1869. #endif /* NO_MD5 */
  1870. #ifndef NO_DES3
  1871. if (XSTRNCMP(info->name, EVP_DES_CBC, EVP_DES_SIZE) == 0)
  1872. ret = wc_Des_CbcDecryptWithKey(der->buffer, der->buffer, der->length,
  1873. key, info->iv);
  1874. else if (XSTRNCMP(info->name, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)
  1875. ret = wc_Des3_CbcDecryptWithKey(der->buffer, der->buffer, der->length,
  1876. key, info->iv);
  1877. #endif /* NO_DES3 */
  1878. #ifndef NO_AES
  1879. else if (XSTRNCMP(info->name, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)
  1880. ret = wc_AesCbcDecryptWithKey(der->buffer, der->buffer, der->length,
  1881. key, AES_128_KEY_SIZE, info->iv);
  1882. else if (XSTRNCMP(info->name, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)
  1883. ret = wc_AesCbcDecryptWithKey(der->buffer, der->buffer, der->length,
  1884. key, AES_192_KEY_SIZE, info->iv);
  1885. else if (XSTRNCMP(info->name, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)
  1886. ret = wc_AesCbcDecryptWithKey(der->buffer, der->buffer, der->length,
  1887. key, AES_256_KEY_SIZE, info->iv);
  1888. #endif /* NO_AES */
  1889. else
  1890. ret = SSL_BAD_FILE;
  1891. #ifdef WOLFSSL_SMALL_STACK
  1892. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1893. #endif
  1894. if (ret == MP_OKAY)
  1895. return SSL_SUCCESS;
  1896. else if (ret == SSL_BAD_FILE)
  1897. return SSL_BAD_FILE;
  1898. return SSL_FATAL_ERROR;
  1899. }
  1900. #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
  1901. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA)
  1902. static int wolfssl_encrypt_buffer_key(byte* der, word32 derSz, byte* password,
  1903. int passwordSz, EncryptedInfo* info)
  1904. {
  1905. int ret;
  1906. #ifdef WOLFSSL_SMALL_STACK
  1907. byte* key = NULL;
  1908. #else
  1909. byte key[AES_256_KEY_SIZE];
  1910. #endif
  1911. WOLFSSL_ENTER("wolfssl_encrypt_buffer_key");
  1912. if (der == NULL || password == NULL || info == NULL || info->ivSz == 0) {
  1913. WOLFSSL_MSG("bad arguments");
  1914. return SSL_FATAL_ERROR;
  1915. }
  1916. #ifndef NO_MD5
  1917. #ifdef WOLFSSL_SMALL_STACK
  1918. key = (byte*)XMALLOC(AES_256_KEY_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1919. if (key == NULL) {
  1920. WOLFSSL_MSG("memory failure");
  1921. return SSL_FATAL_ERROR;
  1922. }
  1923. #endif /* WOLFSSL_SMALL_STACK */
  1924. if ((ret = wolfSSL_EVP_BytesToKey(info->name, "MD5", info->iv,
  1925. password, passwordSz, 1, key, NULL)) <= 0) {
  1926. WOLFSSL_MSG("bytes to key failure");
  1927. #ifdef WOLFSSL_SMALL_STACK
  1928. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1929. #endif
  1930. return SSL_FATAL_ERROR;
  1931. }
  1932. #else
  1933. (void) passwordSz;
  1934. #endif /* NO_MD5 */
  1935. #ifndef NO_DES3
  1936. if (XSTRNCMP(info->name, EVP_DES_CBC, EVP_DES_SIZE) == 0)
  1937. ret = wc_Des_CbcEncryptWithKey(der, der, derSz, key, info->iv);
  1938. else if (XSTRNCMP(info->name, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)
  1939. ret = wc_Des3_CbcEncryptWithKey(der, der, derSz, key, info->iv);
  1940. #endif /* NO_DES3 */
  1941. #ifndef NO_AES
  1942. else if (XSTRNCMP(info->name, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)
  1943. ret = wc_AesCbcEncryptWithKey(der, der, derSz,
  1944. key, AES_128_KEY_SIZE, info->iv);
  1945. else if (XSTRNCMP(info->name, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)
  1946. ret = wc_AesCbcEncryptWithKey(der, der, derSz,
  1947. key, AES_192_KEY_SIZE, info->iv);
  1948. else if (XSTRNCMP(info->name, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)
  1949. ret = wc_AesCbcEncryptWithKey(der, der, derSz,
  1950. key, AES_256_KEY_SIZE, info->iv);
  1951. #endif /* NO_AES */
  1952. else
  1953. ret = SSL_BAD_FILE;
  1954. #ifdef WOLFSSL_SMALL_STACK
  1955. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1956. #endif
  1957. if (ret == MP_OKAY)
  1958. return SSL_SUCCESS;
  1959. else if (ret == SSL_BAD_FILE)
  1960. return SSL_BAD_FILE;
  1961. return SSL_FATAL_ERROR;
  1962. }
  1963. #endif /* defined(WOLFSSL_KEY_GEN) */
  1964. #ifndef NO_CERTS
  1965. /* Remove PEM header/footer, convert to ASN1, store any encrypted data
  1966. info->consumed tracks of PEM bytes consumed in case multiple parts */
  1967. int PemToDer(const unsigned char* buff, long longSz, int type,
  1968. buffer* der, void* heap, EncryptedInfo* info, int* eccKey)
  1969. {
  1970. const char* header = NULL;
  1971. const char* footer = NULL;
  1972. char* headerEnd;
  1973. char* footerEnd;
  1974. char* consumedEnd;
  1975. char* bufferEnd = (char*)(buff + longSz);
  1976. long neededSz;
  1977. int ret = 0;
  1978. int dynamicType = 0;
  1979. int sz = (int)longSz;
  1980. int encrypted_key = 0;
  1981. WOLFSSL_ENTER("PemToDer");
  1982. switch (type) {
  1983. case CA_TYPE: /* same as below */
  1984. case CERT_TYPE: header=BEGIN_CERT; footer=END_CERT; break;
  1985. case CRL_TYPE: header=BEGIN_X509_CRL; footer=END_X509_CRL; break;
  1986. case DH_PARAM_TYPE: header=BEGIN_DH_PARAM; footer=END_DH_PARAM; break;
  1987. case CERTREQ_TYPE: header=BEGIN_CERT_REQ; footer=END_CERT_REQ; break;
  1988. case DSA_TYPE: header=BEGIN_DSA_PRIV; footer=END_DSA_PRIV; break;
  1989. case ECC_TYPE: header=BEGIN_EC_PRIV; footer=END_EC_PRIV; break;
  1990. case RSA_TYPE: header=BEGIN_RSA_PRIV; footer=END_RSA_PRIV; break;
  1991. case PUBLICKEY_TYPE: header=BEGIN_PUB_KEY; footer=END_PUB_KEY; break;
  1992. default: header=BEGIN_RSA_PRIV; footer=END_RSA_PRIV; break;
  1993. }
  1994. switch (type) {
  1995. case CA_TYPE: dynamicType = DYNAMIC_TYPE_CA; break;
  1996. case CERT_TYPE: dynamicType = DYNAMIC_TYPE_CERT; break;
  1997. case CRL_TYPE: dynamicType = DYNAMIC_TYPE_CRL; break;
  1998. case DSA_TYPE: dynamicType = DYNAMIC_TYPE_DSA; break;
  1999. case ECC_TYPE: dynamicType = DYNAMIC_TYPE_ECC; break;
  2000. case RSA_TYPE: dynamicType = DYNAMIC_TYPE_RSA; break;
  2001. default: dynamicType = DYNAMIC_TYPE_KEY; break;
  2002. }
  2003. /* find header */
  2004. for (;;) {
  2005. headerEnd = XSTRNSTR((char*)buff, header, sz);
  2006. if (headerEnd || type != PRIVATEKEY_TYPE) {
  2007. break;
  2008. } else if (header == BEGIN_RSA_PRIV) {
  2009. header = BEGIN_PRIV_KEY; footer = END_PRIV_KEY;
  2010. } else if (header == BEGIN_PRIV_KEY) {
  2011. header = BEGIN_ENC_PRIV_KEY; footer = END_ENC_PRIV_KEY;
  2012. } else if (header == BEGIN_ENC_PRIV_KEY) {
  2013. header = BEGIN_EC_PRIV; footer = END_EC_PRIV;
  2014. } else if (header == BEGIN_EC_PRIV) {
  2015. header = BEGIN_DSA_PRIV; footer = END_DSA_PRIV;
  2016. } else
  2017. break;
  2018. }
  2019. if (!headerEnd) {
  2020. WOLFSSL_MSG("Couldn't find PEM header");
  2021. return SSL_NO_PEM_HEADER;
  2022. }
  2023. headerEnd += XSTRLEN(header);
  2024. /* eat end of line */
  2025. if (headerEnd[0] == '\n')
  2026. headerEnd++;
  2027. else if (headerEnd[1] == '\n')
  2028. headerEnd += 2;
  2029. else {
  2030. if (info)
  2031. info->consumed = (long)(headerEnd+2 - (char*)buff);
  2032. return SSL_BAD_FILE;
  2033. }
  2034. if (type == PRIVATEKEY_TYPE) {
  2035. if (eccKey)
  2036. *eccKey = header == BEGIN_EC_PRIV;
  2037. }
  2038. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  2039. {
  2040. /* remove encrypted header if there */
  2041. char encHeader[] = "Proc-Type";
  2042. char* line = XSTRNSTR(headerEnd, encHeader, PEM_LINE_LEN);
  2043. if (line) {
  2044. char* newline;
  2045. char* finish;
  2046. char* start = XSTRNSTR(line, "DES", PEM_LINE_LEN);
  2047. if (!start)
  2048. start = XSTRNSTR(line, "AES", PEM_LINE_LEN);
  2049. if (!start) return SSL_BAD_FILE;
  2050. if (!info) return SSL_BAD_FILE;
  2051. finish = XSTRNSTR(start, ",", PEM_LINE_LEN);
  2052. if (start && finish && (start < finish)) {
  2053. newline = XSTRNSTR(finish, "\r", PEM_LINE_LEN);
  2054. if (XMEMCPY(info->name, start, finish - start) == NULL)
  2055. return SSL_FATAL_ERROR;
  2056. info->name[finish - start] = 0;
  2057. if (XMEMCPY(info->iv, finish + 1, sizeof(info->iv)) == NULL)
  2058. return SSL_FATAL_ERROR;
  2059. if (!newline) newline = XSTRNSTR(finish, "\n", PEM_LINE_LEN);
  2060. if (newline && (newline > finish)) {
  2061. info->ivSz = (word32)(newline - (finish + 1));
  2062. info->set = 1;
  2063. }
  2064. else
  2065. return SSL_BAD_FILE;
  2066. }
  2067. else
  2068. return SSL_BAD_FILE;
  2069. /* eat blank line */
  2070. while (*newline == '\r' || *newline == '\n')
  2071. newline++;
  2072. headerEnd = newline;
  2073. encrypted_key = 1;
  2074. }
  2075. }
  2076. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  2077. /* find footer */
  2078. footerEnd = XSTRNSTR((char*)buff, footer, sz);
  2079. if (!footerEnd) {
  2080. if (info)
  2081. info->consumed = longSz; /* No more certs if no footer */
  2082. return SSL_BAD_FILE;
  2083. }
  2084. consumedEnd = footerEnd + XSTRLEN(footer);
  2085. if (consumedEnd < bufferEnd) { /* handle no end of line on last line */
  2086. /* eat end of line */
  2087. if (consumedEnd[0] == '\n')
  2088. consumedEnd++;
  2089. else if (consumedEnd[1] == '\n')
  2090. consumedEnd += 2;
  2091. else {
  2092. if (info)
  2093. info->consumed = (long)(consumedEnd+2 - (char*)buff);
  2094. return SSL_BAD_FILE;
  2095. }
  2096. }
  2097. if (info)
  2098. info->consumed = (long)(consumedEnd - (char*)buff);
  2099. /* set up der buffer */
  2100. neededSz = (long)(footerEnd - headerEnd);
  2101. if (neededSz > sz || neededSz < 0)
  2102. return SSL_BAD_FILE;
  2103. der->buffer = (byte*)XMALLOC(neededSz, heap, dynamicType);
  2104. if (!der->buffer)
  2105. return MEMORY_ERROR;
  2106. der->length = (word32)neededSz;
  2107. if (Base64_Decode((byte*)headerEnd, (word32)neededSz,
  2108. der->buffer, &der->length) < 0)
  2109. return SSL_BAD_FILE;
  2110. if (header == BEGIN_PRIV_KEY && !encrypted_key) {
  2111. /* pkcs8 key, convert and adjust length */
  2112. if ((ret = ToTraditional(der->buffer, der->length)) < 0)
  2113. return ret;
  2114. der->length = ret;
  2115. return 0;
  2116. }
  2117. #if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) && !defined(NO_PWDBASED)
  2118. if (encrypted_key || header == BEGIN_ENC_PRIV_KEY) {
  2119. int passwordSz;
  2120. #ifdef WOLFSSL_SMALL_STACK
  2121. char* password = NULL;
  2122. #else
  2123. char password[80];
  2124. #endif
  2125. if (!info || !info->ctx || !info->ctx->passwd_cb)
  2126. return SSL_BAD_FILE; /* no callback error */
  2127. #ifdef WOLFSSL_SMALL_STACK
  2128. password = (char*)XMALLOC(80, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2129. if (password == NULL)
  2130. return MEMORY_E;
  2131. #endif
  2132. passwordSz = info->ctx->passwd_cb(password, sizeof(password), 0,
  2133. info->ctx->userdata);
  2134. /* convert and adjust length */
  2135. if (header == BEGIN_ENC_PRIV_KEY) {
  2136. ret = ToTraditionalEnc(der->buffer, der->length,
  2137. password, passwordSz);
  2138. #ifdef WOLFSSL_SMALL_STACK
  2139. XFREE(password, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2140. #endif
  2141. if (ret < 0) {
  2142. XFREE(der->buffer, heap, dynamicType);
  2143. return ret;
  2144. }
  2145. der->length = ret;
  2146. }
  2147. /* decrypt the key */
  2148. else {
  2149. ret = wolfssl_decrypt_buffer_key(der, (byte*)password,
  2150. passwordSz, info);
  2151. #ifdef WOLFSSL_SMALL_STACK
  2152. XFREE(password, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2153. #endif
  2154. if (ret != SSL_SUCCESS) {
  2155. XFREE(der->buffer, heap, dynamicType);
  2156. return ret;
  2157. }
  2158. }
  2159. }
  2160. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER || NO_PWDBASED */
  2161. return 0;
  2162. }
  2163. /* process the buffer buff, legnth sz, into ctx of format and type
  2164. used tracks bytes consumed, userChain specifies a user cert chain
  2165. to pass during the handshake */
  2166. static int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
  2167. long sz, int format, int type, WOLFSSL* ssl,
  2168. long* used, int userChain)
  2169. {
  2170. buffer der; /* holds DER or RAW (for NTRU) */
  2171. int ret;
  2172. int dynamicType = 0;
  2173. int eccKey = 0;
  2174. int rsaKey = 0;
  2175. void* heap = ctx ? ctx->heap : NULL;
  2176. #ifdef WOLFSSL_SMALL_STACK
  2177. EncryptedInfo* info = NULL;
  2178. #else
  2179. EncryptedInfo info[1];
  2180. #endif
  2181. (void)dynamicType;
  2182. (void)rsaKey;
  2183. if (used)
  2184. *used = sz; /* used bytes default to sz, PEM chain may shorten*/
  2185. if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM
  2186. && format != SSL_FILETYPE_RAW)
  2187. return SSL_BAD_FILETYPE;
  2188. if (ctx == NULL && ssl == NULL)
  2189. return BAD_FUNC_ARG;
  2190. if (type == CA_TYPE)
  2191. dynamicType = DYNAMIC_TYPE_CA;
  2192. else if (type == CERT_TYPE)
  2193. dynamicType = DYNAMIC_TYPE_CERT;
  2194. else
  2195. dynamicType = DYNAMIC_TYPE_KEY;
  2196. #ifdef WOLFSSL_SMALL_STACK
  2197. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  2198. DYNAMIC_TYPE_TMP_BUFFER);
  2199. if (info == NULL)
  2200. return MEMORY_E;
  2201. #endif
  2202. info->set = 0;
  2203. info->ctx = ctx;
  2204. info->consumed = 0;
  2205. der.buffer = 0;
  2206. if (format == SSL_FILETYPE_PEM) {
  2207. ret = PemToDer(buff, sz, type, &der, heap, info, &eccKey);
  2208. if (used)
  2209. *used = info->consumed;
  2210. if (ret < 0) {
  2211. #ifdef WOLFSSL_SMALL_STACK
  2212. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2213. #endif
  2214. XFREE(der.buffer, heap, dynamicType);
  2215. return ret;
  2216. }
  2217. /* we may have a user cert chain, try to consume */
  2218. if (userChain && type == CERT_TYPE && info->consumed < sz) {
  2219. #ifdef WOLFSSL_SMALL_STACK
  2220. byte staticBuffer[1]; /* force heap usage */
  2221. #else
  2222. byte staticBuffer[FILE_BUFFER_SIZE]; /* tmp chain buffer */
  2223. #endif
  2224. byte* chainBuffer = staticBuffer;
  2225. byte* shrinked = NULL; /* shrinked to size chainBuffer
  2226. * or staticBuffer */
  2227. int dynamicBuffer = 0;
  2228. word32 bufferSz = sizeof(staticBuffer);
  2229. long consumed = info->consumed;
  2230. word32 idx = 0;
  2231. int gotOne = 0;
  2232. if ( (sz - consumed) > (int)bufferSz) {
  2233. WOLFSSL_MSG("Growing Tmp Chain Buffer");
  2234. bufferSz = (word32)(sz - consumed);
  2235. /* will shrink to actual size */
  2236. chainBuffer = (byte*)XMALLOC(bufferSz, heap, DYNAMIC_TYPE_FILE);
  2237. if (chainBuffer == NULL) {
  2238. #ifdef WOLFSSL_SMALL_STACK
  2239. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2240. #endif
  2241. XFREE(der.buffer, heap, dynamicType);
  2242. return MEMORY_E;
  2243. }
  2244. dynamicBuffer = 1;
  2245. }
  2246. WOLFSSL_MSG("Processing Cert Chain");
  2247. while (consumed < sz) {
  2248. buffer part;
  2249. info->consumed = 0;
  2250. part.buffer = 0;
  2251. ret = PemToDer(buff + consumed, sz - consumed, type, &part,
  2252. heap, info, &eccKey);
  2253. if (ret == 0) {
  2254. gotOne = 1;
  2255. if ( (idx + part.length) > bufferSz) {
  2256. WOLFSSL_MSG(" Cert Chain bigger than buffer");
  2257. ret = BUFFER_E;
  2258. }
  2259. else {
  2260. c32to24(part.length, &chainBuffer[idx]);
  2261. idx += CERT_HEADER_SZ;
  2262. XMEMCPY(&chainBuffer[idx], part.buffer,part.length);
  2263. idx += part.length;
  2264. consumed += info->consumed;
  2265. if (used)
  2266. *used += info->consumed;
  2267. }
  2268. }
  2269. XFREE(part.buffer, heap, dynamicType);
  2270. if (ret == SSL_NO_PEM_HEADER && gotOne) {
  2271. WOLFSSL_MSG("We got one good PEM so stuff at end ok");
  2272. break;
  2273. }
  2274. if (ret < 0) {
  2275. WOLFSSL_MSG(" Error in Cert in Chain");
  2276. if (dynamicBuffer)
  2277. XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
  2278. #ifdef WOLFSSL_SMALL_STACK
  2279. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2280. #endif
  2281. XFREE(der.buffer, heap, dynamicType);
  2282. return ret;
  2283. }
  2284. WOLFSSL_MSG(" Consumed another Cert in Chain");
  2285. }
  2286. WOLFSSL_MSG("Finished Processing Cert Chain");
  2287. /* only retain actual size used */
  2288. shrinked = (byte*)XMALLOC(idx, heap, dynamicType);
  2289. if (shrinked) {
  2290. if (ssl) {
  2291. if (ssl->buffers.certChain.buffer &&
  2292. ssl->buffers.weOwnCertChain) {
  2293. XFREE(ssl->buffers.certChain.buffer, heap, dynamicType);
  2294. }
  2295. ssl->buffers.certChain.buffer = shrinked;
  2296. ssl->buffers.certChain.length = idx;
  2297. XMEMCPY(ssl->buffers.certChain.buffer, chainBuffer,idx);
  2298. ssl->buffers.weOwnCertChain = 1;
  2299. } else if (ctx) {
  2300. if (ctx->certChain.buffer)
  2301. XFREE(ctx->certChain.buffer, heap, dynamicType);
  2302. ctx->certChain.buffer = shrinked;
  2303. ctx->certChain.length = idx;
  2304. XMEMCPY(ctx->certChain.buffer, chainBuffer, idx);
  2305. }
  2306. }
  2307. if (dynamicBuffer)
  2308. XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
  2309. if (shrinked == NULL) {
  2310. #ifdef WOLFSSL_SMALL_STACK
  2311. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2312. #endif
  2313. XFREE(der.buffer, heap, dynamicType);
  2314. return MEMORY_E;
  2315. }
  2316. }
  2317. }
  2318. else { /* ASN1 (DER) or RAW (NTRU) */
  2319. der.buffer = (byte*) XMALLOC(sz, heap, dynamicType);
  2320. if (!der.buffer) {
  2321. #ifdef WOLFSSL_SMALL_STACK
  2322. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2323. #endif
  2324. return MEMORY_ERROR;
  2325. }
  2326. XMEMCPY(der.buffer, buff, sz);
  2327. der.length = (word32)sz;
  2328. }
  2329. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  2330. /* for SSL_FILETYPE_PEM, PemToDer manage the decryption if required */
  2331. if (info->set && (format != SSL_FILETYPE_PEM)) {
  2332. /* decrypt */
  2333. int passwordSz;
  2334. #ifdef WOLFSSL_SMALL_STACK
  2335. char* password = NULL;
  2336. #else
  2337. char password[80];
  2338. #endif
  2339. #ifdef WOLFSSL_SMALL_STACK
  2340. password = (char*)XMALLOC(80, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2341. if (password == NULL)
  2342. ret = MEMORY_E;
  2343. else
  2344. #endif
  2345. if (!ctx || !ctx->passwd_cb) {
  2346. ret = NO_PASSWORD;
  2347. }
  2348. else {
  2349. passwordSz = ctx->passwd_cb(password, sizeof(password),
  2350. 0, ctx->userdata);
  2351. /* decrypt the key */
  2352. ret = wolfssl_decrypt_buffer_key(&der, (byte*)password,
  2353. passwordSz, info);
  2354. }
  2355. #ifdef WOLFSSL_SMALL_STACK
  2356. XFREE(password, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2357. #endif
  2358. if (ret != SSL_SUCCESS) {
  2359. #ifdef WOLFSSL_SMALL_STACK
  2360. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2361. #endif
  2362. XFREE(der.buffer, heap, dynamicType);
  2363. return ret;
  2364. }
  2365. }
  2366. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  2367. #ifdef WOLFSSL_SMALL_STACK
  2368. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2369. #endif
  2370. if (type == CA_TYPE) {
  2371. if (ctx == NULL) {
  2372. WOLFSSL_MSG("Need context for CA load");
  2373. XFREE(der.buffer, heap, dynamicType);
  2374. return BAD_FUNC_ARG;
  2375. }
  2376. /* verify CA unless user set to no verify */
  2377. return AddCA(ctx->cm, der, WOLFSSL_USER_CA, !ctx->verifyNone);
  2378. /* takes der over */
  2379. }
  2380. else if (type == CERT_TYPE) {
  2381. if (ssl) {
  2382. if (ssl->buffers.weOwnCert && ssl->buffers.certificate.buffer)
  2383. XFREE(ssl->buffers.certificate.buffer, heap, dynamicType);
  2384. ssl->buffers.certificate = der;
  2385. ssl->buffers.weOwnCert = 1;
  2386. }
  2387. else if (ctx) {
  2388. if (ctx->certificate.buffer)
  2389. XFREE(ctx->certificate.buffer, heap, dynamicType);
  2390. ctx->certificate = der; /* takes der over */
  2391. }
  2392. }
  2393. else if (type == PRIVATEKEY_TYPE) {
  2394. if (ssl) {
  2395. if (ssl->buffers.weOwnKey && ssl->buffers.key.buffer)
  2396. XFREE(ssl->buffers.key.buffer, heap, dynamicType);
  2397. ssl->buffers.key = der;
  2398. ssl->buffers.weOwnKey = 1;
  2399. }
  2400. else if (ctx) {
  2401. if (ctx->privateKey.buffer)
  2402. XFREE(ctx->privateKey.buffer, heap, dynamicType);
  2403. ctx->privateKey = der; /* takes der over */
  2404. }
  2405. }
  2406. else {
  2407. XFREE(der.buffer, heap, dynamicType);
  2408. return SSL_BAD_CERTTYPE;
  2409. }
  2410. if (type == PRIVATEKEY_TYPE && format != SSL_FILETYPE_RAW) {
  2411. #ifndef NO_RSA
  2412. if (!eccKey) {
  2413. /* make sure RSA key can be used */
  2414. word32 idx = 0;
  2415. #ifdef WOLFSSL_SMALL_STACK
  2416. RsaKey* key = NULL;
  2417. #else
  2418. RsaKey key[1];
  2419. #endif
  2420. #ifdef WOLFSSL_SMALL_STACK
  2421. key = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL,
  2422. DYNAMIC_TYPE_TMP_BUFFER);
  2423. if (key == NULL)
  2424. return MEMORY_E;
  2425. #endif
  2426. ret = wc_InitRsaKey(key, 0);
  2427. if (ret == 0) {
  2428. if (wc_RsaPrivateKeyDecode(der.buffer, &idx, key, der.length)
  2429. != 0) {
  2430. #ifdef HAVE_ECC
  2431. /* could have DER ECC (or pkcs8 ecc), no easy way to tell */
  2432. eccKey = 1; /* so try it out */
  2433. #endif
  2434. if (!eccKey)
  2435. ret = SSL_BAD_FILE;
  2436. } else {
  2437. rsaKey = 1;
  2438. (void)rsaKey; /* for no ecc builds */
  2439. }
  2440. }
  2441. wc_FreeRsaKey(key);
  2442. #ifdef WOLFSSL_SMALL_STACK
  2443. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2444. #endif
  2445. if (ret != 0)
  2446. return ret;
  2447. }
  2448. #endif
  2449. #ifdef HAVE_ECC
  2450. if (!rsaKey) {
  2451. /* make sure ECC key can be used */
  2452. word32 idx = 0;
  2453. ecc_key key;
  2454. wc_ecc_init(&key);
  2455. if (wc_EccPrivateKeyDecode(der.buffer,&idx,&key,der.length) != 0) {
  2456. wc_ecc_free(&key);
  2457. return SSL_BAD_FILE;
  2458. }
  2459. wc_ecc_free(&key);
  2460. eccKey = 1;
  2461. if (ctx)
  2462. ctx->haveStaticECC = 1;
  2463. if (ssl)
  2464. ssl->options.haveStaticECC = 1;
  2465. }
  2466. #endif /* HAVE_ECC */
  2467. }
  2468. else if (type == CERT_TYPE) {
  2469. #ifdef WOLFSSL_SMALL_STACK
  2470. DecodedCert* cert = NULL;
  2471. #else
  2472. DecodedCert cert[1];
  2473. #endif
  2474. #ifdef WOLFSSL_SMALL_STACK
  2475. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  2476. DYNAMIC_TYPE_TMP_BUFFER);
  2477. if (cert == NULL)
  2478. return MEMORY_E;
  2479. #endif
  2480. WOLFSSL_MSG("Checking cert signature type");
  2481. InitDecodedCert(cert, der.buffer, der.length, heap);
  2482. if (DecodeToKey(cert, 0) < 0) {
  2483. WOLFSSL_MSG("Decode to key failed");
  2484. #ifdef WOLFSSL_SMALL_STACK
  2485. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2486. #endif
  2487. return SSL_BAD_FILE;
  2488. }
  2489. switch (cert->signatureOID) {
  2490. case CTC_SHAwECDSA:
  2491. case CTC_SHA256wECDSA:
  2492. case CTC_SHA384wECDSA:
  2493. case CTC_SHA512wECDSA:
  2494. WOLFSSL_MSG("ECDSA cert signature");
  2495. if (ctx)
  2496. ctx->haveECDSAsig = 1;
  2497. if (ssl)
  2498. ssl->options.haveECDSAsig = 1;
  2499. break;
  2500. default:
  2501. WOLFSSL_MSG("Not ECDSA cert signature");
  2502. break;
  2503. }
  2504. #ifdef HAVE_ECC
  2505. if (ctx)
  2506. ctx->pkCurveOID = cert->pkCurveOID;
  2507. if (ssl)
  2508. ssl->pkCurveOID = cert->pkCurveOID;
  2509. #endif
  2510. FreeDecodedCert(cert);
  2511. #ifdef WOLFSSL_SMALL_STACK
  2512. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2513. #endif
  2514. }
  2515. return SSL_SUCCESS;
  2516. }
  2517. /* CA PEM file for verification, may have multiple/chain certs to process */
  2518. static int ProcessChainBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
  2519. long sz, int format, int type, WOLFSSL* ssl)
  2520. {
  2521. long used = 0;
  2522. int ret = 0;
  2523. int gotOne = 0;
  2524. WOLFSSL_MSG("Processing CA PEM file");
  2525. while (used < sz) {
  2526. long consumed = 0;
  2527. ret = ProcessBuffer(ctx, buff + used, sz - used, format, type, ssl,
  2528. &consumed, 0);
  2529. if (ret < 0)
  2530. {
  2531. if(consumed > 0) { /* Made progress in file */
  2532. WOLFSSL_ERROR(ret);
  2533. WOLFSSL_MSG("CA Parse failed, with progress in file.");
  2534. WOLFSSL_MSG("Search for other certs in file");
  2535. } else {
  2536. WOLFSSL_MSG("CA Parse failed, no progress in file.");
  2537. WOLFSSL_MSG("Do not continue search for other certs in file");
  2538. break;
  2539. }
  2540. } else {
  2541. WOLFSSL_MSG(" Processed a CA");
  2542. gotOne = 1;
  2543. }
  2544. used += consumed;
  2545. }
  2546. if(gotOne)
  2547. {
  2548. WOLFSSL_MSG("Processed at least one valid CA. Other stuff OK");
  2549. return SSL_SUCCESS;
  2550. }
  2551. return ret;
  2552. }
  2553. static INLINE WOLFSSL_METHOD* cm_pick_method(void)
  2554. {
  2555. #ifndef NO_WOLFSSL_CLIENT
  2556. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  2557. return wolfSSLv3_client_method();
  2558. #else
  2559. return wolfTLSv1_2_client_method();
  2560. #endif
  2561. #elif !defined(NO_WOLFSSL_SERVER)
  2562. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  2563. return wolfSSLv3_server_method();
  2564. #else
  2565. return wolfTLSv1_2_server_method();
  2566. #endif
  2567. #else
  2568. return NULL;
  2569. #endif
  2570. }
  2571. /* like load verify locations, 1 for success, < 0 for error */
  2572. int wolfSSL_CertManagerLoadCABuffer(WOLFSSL_CERT_MANAGER* cm,
  2573. const unsigned char* in, long sz, int format)
  2574. {
  2575. int ret = SSL_FATAL_ERROR;
  2576. WOLFSSL_CTX* tmp;
  2577. WOLFSSL_ENTER("wolfSSL_CertManagerLoadCABuffer");
  2578. if (cm == NULL) {
  2579. WOLFSSL_MSG("No CertManager error");
  2580. return ret;
  2581. }
  2582. tmp = wolfSSL_CTX_new(cm_pick_method());
  2583. if (tmp == NULL) {
  2584. WOLFSSL_MSG("CTX new failed");
  2585. return ret;
  2586. }
  2587. /* for tmp use */
  2588. wolfSSL_CertManagerFree(tmp->cm);
  2589. tmp->cm = cm;
  2590. ret = wolfSSL_CTX_load_verify_buffer(tmp, in, sz, format);
  2591. /* don't loose our good one */
  2592. tmp->cm = NULL;
  2593. wolfSSL_CTX_free(tmp);
  2594. return ret;
  2595. }
  2596. #ifdef HAVE_CRL
  2597. int wolfSSL_CertManagerLoadCRLBuffer(WOLFSSL_CERT_MANAGER* cm,
  2598. const unsigned char* buff, long sz, int type)
  2599. {
  2600. WOLFSSL_ENTER("wolfSSL_CertManagerLoadCRLBuffer");
  2601. if (cm == NULL)
  2602. return BAD_FUNC_ARG;
  2603. if (cm->crl == NULL) {
  2604. if (wolfSSL_CertManagerEnableCRL(cm, 0) != SSL_SUCCESS) {
  2605. WOLFSSL_MSG("Enable CRL failed");
  2606. return SSL_FATAL_ERROR;
  2607. }
  2608. }
  2609. return BufferLoadCRL(cm->crl, buff, sz, type);
  2610. }
  2611. #endif /* HAVE_CRL */
  2612. /* turn on CRL if off and compiled in, set options */
  2613. int wolfSSL_CertManagerEnableCRL(WOLFSSL_CERT_MANAGER* cm, int options)
  2614. {
  2615. int ret = SSL_SUCCESS;
  2616. (void)options;
  2617. WOLFSSL_ENTER("wolfSSL_CertManagerEnableCRL");
  2618. if (cm == NULL)
  2619. return BAD_FUNC_ARG;
  2620. #ifdef HAVE_CRL
  2621. if (cm->crl == NULL) {
  2622. cm->crl = (WOLFSSL_CRL*)XMALLOC(sizeof(WOLFSSL_CRL), cm->heap,
  2623. DYNAMIC_TYPE_CRL);
  2624. if (cm->crl == NULL)
  2625. return MEMORY_E;
  2626. if (InitCRL(cm->crl, cm) != 0) {
  2627. WOLFSSL_MSG("Init CRL failed");
  2628. FreeCRL(cm->crl, 1);
  2629. cm->crl = NULL;
  2630. return SSL_FAILURE;
  2631. }
  2632. }
  2633. cm->crlEnabled = 1;
  2634. if (options & WOLFSSL_CRL_CHECKALL)
  2635. cm->crlCheckAll = 1;
  2636. #else
  2637. ret = NOT_COMPILED_IN;
  2638. #endif
  2639. return ret;
  2640. }
  2641. int wolfSSL_CertManagerDisableCRL(WOLFSSL_CERT_MANAGER* cm)
  2642. {
  2643. WOLFSSL_ENTER("wolfSSL_CertManagerDisableCRL");
  2644. if (cm == NULL)
  2645. return BAD_FUNC_ARG;
  2646. cm->crlEnabled = 0;
  2647. return SSL_SUCCESS;
  2648. }
  2649. /* Verify the ceritficate, SSL_SUCCESS for ok, < 0 for error */
  2650. int wolfSSL_CertManagerVerifyBuffer(WOLFSSL_CERT_MANAGER* cm, const byte* buff,
  2651. long sz, int format)
  2652. {
  2653. int ret = 0;
  2654. buffer der;
  2655. #ifdef WOLFSSL_SMALL_STACK
  2656. DecodedCert* cert = NULL;
  2657. #else
  2658. DecodedCert cert[1];
  2659. #endif
  2660. WOLFSSL_ENTER("wolfSSL_CertManagerVerifyBuffer");
  2661. #ifdef WOLFSSL_SMALL_STACK
  2662. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  2663. DYNAMIC_TYPE_TMP_BUFFER);
  2664. if (cert == NULL)
  2665. return MEMORY_E;
  2666. #endif
  2667. der.buffer = NULL;
  2668. der.length = 0;
  2669. if (format == SSL_FILETYPE_PEM) {
  2670. int eccKey = 0; /* not used */
  2671. #ifdef WOLFSSL_SMALL_STACK
  2672. EncryptedInfo* info = NULL;
  2673. #else
  2674. EncryptedInfo info[1];
  2675. #endif
  2676. #ifdef WOLFSSL_SMALL_STACK
  2677. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  2678. DYNAMIC_TYPE_TMP_BUFFER);
  2679. if (info == NULL) {
  2680. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2681. return MEMORY_E;
  2682. }
  2683. #endif
  2684. info->set = 0;
  2685. info->ctx = NULL;
  2686. info->consumed = 0;
  2687. ret = PemToDer(buff, sz, CERT_TYPE, &der, cm->heap, info, &eccKey);
  2688. InitDecodedCert(cert, der.buffer, der.length, cm->heap);
  2689. #ifdef WOLFSSL_SMALL_STACK
  2690. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2691. #endif
  2692. }
  2693. else
  2694. InitDecodedCert(cert, (byte*)buff, (word32)sz, cm->heap);
  2695. if (ret == 0)
  2696. ret = ParseCertRelative(cert, CERT_TYPE, 1, cm);
  2697. #ifdef HAVE_CRL
  2698. if (ret == 0 && cm->crlEnabled)
  2699. ret = CheckCertCRL(cm->crl, cert);
  2700. #endif
  2701. FreeDecodedCert(cert);
  2702. XFREE(der.buffer, cm->heap, DYNAMIC_TYPE_CERT);
  2703. #ifdef WOLFSSL_SMALL_STACK
  2704. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2705. #endif
  2706. return ret == 0 ? SSL_SUCCESS : ret;
  2707. }
  2708. /* turn on OCSP if off and compiled in, set options */
  2709. int wolfSSL_CertManagerEnableOCSP(WOLFSSL_CERT_MANAGER* cm, int options)
  2710. {
  2711. int ret = SSL_SUCCESS;
  2712. (void)options;
  2713. WOLFSSL_ENTER("wolfSSL_CertManagerEnableOCSP");
  2714. if (cm == NULL)
  2715. return BAD_FUNC_ARG;
  2716. #ifdef HAVE_OCSP
  2717. if (cm->ocsp == NULL) {
  2718. cm->ocsp = (WOLFSSL_OCSP*)XMALLOC(sizeof(WOLFSSL_OCSP), cm->heap,
  2719. DYNAMIC_TYPE_OCSP);
  2720. if (cm->ocsp == NULL)
  2721. return MEMORY_E;
  2722. if (InitOCSP(cm->ocsp, cm) != 0) {
  2723. WOLFSSL_MSG("Init OCSP failed");
  2724. FreeOCSP(cm->ocsp, 1);
  2725. cm->ocsp = NULL;
  2726. return SSL_FAILURE;
  2727. }
  2728. }
  2729. cm->ocspEnabled = 1;
  2730. if (options & WOLFSSL_OCSP_URL_OVERRIDE)
  2731. cm->ocspUseOverrideURL = 1;
  2732. if (options & WOLFSSL_OCSP_NO_NONCE)
  2733. cm->ocspSendNonce = 0;
  2734. else
  2735. cm->ocspSendNonce = 1;
  2736. if (options & WOLFSSL_OCSP_CHECKALL)
  2737. cm->ocspCheckAll = 1;
  2738. #ifndef WOLFSSL_USER_IO
  2739. cm->ocspIOCb = EmbedOcspLookup;
  2740. cm->ocspRespFreeCb = EmbedOcspRespFree;
  2741. #endif /* WOLFSSL_USER_IO */
  2742. #else
  2743. ret = NOT_COMPILED_IN;
  2744. #endif
  2745. return ret;
  2746. }
  2747. int wolfSSL_CertManagerDisableOCSP(WOLFSSL_CERT_MANAGER* cm)
  2748. {
  2749. WOLFSSL_ENTER("wolfSSL_CertManagerDisableOCSP");
  2750. if (cm == NULL)
  2751. return BAD_FUNC_ARG;
  2752. cm->ocspEnabled = 0;
  2753. return SSL_SUCCESS;
  2754. }
  2755. #ifdef HAVE_OCSP
  2756. /* check CRL if enabled, SSL_SUCCESS */
  2757. int wolfSSL_CertManagerCheckOCSP(WOLFSSL_CERT_MANAGER* cm, byte* der, int sz)
  2758. {
  2759. int ret;
  2760. #ifdef WOLFSSL_SMALL_STACK
  2761. DecodedCert* cert = NULL;
  2762. #else
  2763. DecodedCert cert[1];
  2764. #endif
  2765. WOLFSSL_ENTER("wolfSSL_CertManagerCheckOCSP");
  2766. if (cm == NULL)
  2767. return BAD_FUNC_ARG;
  2768. if (cm->ocspEnabled == 0)
  2769. return SSL_SUCCESS;
  2770. #ifdef WOLFSSL_SMALL_STACK
  2771. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  2772. DYNAMIC_TYPE_TMP_BUFFER);
  2773. if (cert == NULL)
  2774. return MEMORY_E;
  2775. #endif
  2776. InitDecodedCert(cert, der, sz, NULL);
  2777. if ((ret = ParseCertRelative(cert, CERT_TYPE, NO_VERIFY, cm)) != 0) {
  2778. WOLFSSL_MSG("ParseCert failed");
  2779. }
  2780. else if ((ret = CheckCertOCSP(cm->ocsp, cert)) != 0) {
  2781. WOLFSSL_MSG("CheckCertOCSP failed");
  2782. }
  2783. FreeDecodedCert(cert);
  2784. #ifdef WOLFSSL_SMALL_STACK
  2785. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2786. #endif
  2787. return ret == 0 ? SSL_SUCCESS : ret;
  2788. }
  2789. int wolfSSL_CertManagerSetOCSPOverrideURL(WOLFSSL_CERT_MANAGER* cm,
  2790. const char* url)
  2791. {
  2792. WOLFSSL_ENTER("wolfSSL_CertManagerSetOCSPOverrideURL");
  2793. if (cm == NULL)
  2794. return BAD_FUNC_ARG;
  2795. XFREE(cm->ocspOverrideURL, cm->heap, 0);
  2796. if (url != NULL) {
  2797. int urlSz = (int)XSTRLEN(url) + 1;
  2798. cm->ocspOverrideURL = (char*)XMALLOC(urlSz, cm->heap, 0);
  2799. if (cm->ocspOverrideURL != NULL) {
  2800. XMEMCPY(cm->ocspOverrideURL, url, urlSz);
  2801. }
  2802. else
  2803. return MEMORY_E;
  2804. }
  2805. else
  2806. cm->ocspOverrideURL = NULL;
  2807. return SSL_SUCCESS;
  2808. }
  2809. int wolfSSL_CertManagerSetOCSP_Cb(WOLFSSL_CERT_MANAGER* cm,
  2810. CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
  2811. {
  2812. WOLFSSL_ENTER("wolfSSL_CertManagerSetOCSP_Cb");
  2813. if (cm == NULL)
  2814. return BAD_FUNC_ARG;
  2815. cm->ocspIOCb = ioCb;
  2816. cm->ocspRespFreeCb = respFreeCb;
  2817. cm->ocspIOCtx = ioCbCtx;
  2818. return SSL_SUCCESS;
  2819. }
  2820. int wolfSSL_EnableOCSP(WOLFSSL* ssl, int options)
  2821. {
  2822. WOLFSSL_ENTER("wolfSSL_EnableOCSP");
  2823. if (ssl)
  2824. return wolfSSL_CertManagerEnableOCSP(ssl->ctx->cm, options);
  2825. else
  2826. return BAD_FUNC_ARG;
  2827. }
  2828. int wolfSSL_DisableOCSP(WOLFSSL* ssl)
  2829. {
  2830. WOLFSSL_ENTER("wolfSSL_DisableOCSP");
  2831. if (ssl)
  2832. return wolfSSL_CertManagerDisableOCSP(ssl->ctx->cm);
  2833. else
  2834. return BAD_FUNC_ARG;
  2835. }
  2836. int wolfSSL_SetOCSP_OverrideURL(WOLFSSL* ssl, const char* url)
  2837. {
  2838. WOLFSSL_ENTER("wolfSSL_SetOCSP_OverrideURL");
  2839. if (ssl)
  2840. return wolfSSL_CertManagerSetOCSPOverrideURL(ssl->ctx->cm, url);
  2841. else
  2842. return BAD_FUNC_ARG;
  2843. }
  2844. int wolfSSL_SetOCSP_Cb(WOLFSSL* ssl,
  2845. CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
  2846. {
  2847. WOLFSSL_ENTER("wolfSSL_SetOCSP_Cb");
  2848. if (ssl)
  2849. return wolfSSL_CertManagerSetOCSP_Cb(ssl->ctx->cm,
  2850. ioCb, respFreeCb, ioCbCtx);
  2851. else
  2852. return BAD_FUNC_ARG;
  2853. }
  2854. int wolfSSL_CTX_EnableOCSP(WOLFSSL_CTX* ctx, int options)
  2855. {
  2856. WOLFSSL_ENTER("wolfSSL_CTX_EnableOCSP");
  2857. if (ctx)
  2858. return wolfSSL_CertManagerEnableOCSP(ctx->cm, options);
  2859. else
  2860. return BAD_FUNC_ARG;
  2861. }
  2862. int wolfSSL_CTX_DisableOCSP(WOLFSSL_CTX* ctx)
  2863. {
  2864. WOLFSSL_ENTER("wolfSSL_CTX_DisableOCSP");
  2865. if (ctx)
  2866. return wolfSSL_CertManagerDisableOCSP(ctx->cm);
  2867. else
  2868. return BAD_FUNC_ARG;
  2869. }
  2870. int wolfSSL_CTX_SetOCSP_OverrideURL(WOLFSSL_CTX* ctx, const char* url)
  2871. {
  2872. WOLFSSL_ENTER("wolfSSL_SetOCSP_OverrideURL");
  2873. if (ctx)
  2874. return wolfSSL_CertManagerSetOCSPOverrideURL(ctx->cm, url);
  2875. else
  2876. return BAD_FUNC_ARG;
  2877. }
  2878. int wolfSSL_CTX_SetOCSP_Cb(WOLFSSL_CTX* ctx, CbOCSPIO ioCb,
  2879. CbOCSPRespFree respFreeCb, void* ioCbCtx)
  2880. {
  2881. WOLFSSL_ENTER("wolfSSL_CTX_SetOCSP_Cb");
  2882. if (ctx)
  2883. return wolfSSL_CertManagerSetOCSP_Cb(ctx->cm, ioCb,
  2884. respFreeCb, ioCbCtx);
  2885. else
  2886. return BAD_FUNC_ARG;
  2887. }
  2888. #endif /* HAVE_OCSP */
  2889. #ifndef NO_FILESYSTEM
  2890. /* process a file with name fname into ctx of format and type
  2891. userChain specifies a user certificate chain to pass during handshake */
  2892. int ProcessFile(WOLFSSL_CTX* ctx, const char* fname, int format, int type,
  2893. WOLFSSL* ssl, int userChain, WOLFSSL_CRL* crl)
  2894. {
  2895. #ifdef WOLFSSL_SMALL_STACK
  2896. byte staticBuffer[1]; /* force heap usage */
  2897. #else
  2898. byte staticBuffer[FILE_BUFFER_SIZE];
  2899. #endif
  2900. byte* myBuffer = staticBuffer;
  2901. int dynamic = 0;
  2902. int ret;
  2903. long sz = 0;
  2904. XFILE file;
  2905. void* heapHint = ctx ? ctx->heap : NULL;
  2906. (void)crl;
  2907. (void)heapHint;
  2908. if (fname == NULL) return SSL_BAD_FILE;
  2909. file = XFOPEN(fname, "rb");
  2910. if (file == XBADFILE) return SSL_BAD_FILE;
  2911. XFSEEK(file, 0, XSEEK_END);
  2912. sz = XFTELL(file);
  2913. XREWIND(file);
  2914. if (sz > (long)sizeof(staticBuffer)) {
  2915. WOLFSSL_MSG("Getting dynamic buffer");
  2916. myBuffer = (byte*)XMALLOC(sz, heapHint, DYNAMIC_TYPE_FILE);
  2917. if (myBuffer == NULL) {
  2918. XFCLOSE(file);
  2919. return SSL_BAD_FILE;
  2920. }
  2921. dynamic = 1;
  2922. }
  2923. else if (sz < 0) {
  2924. XFCLOSE(file);
  2925. return SSL_BAD_FILE;
  2926. }
  2927. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  2928. ret = SSL_BAD_FILE;
  2929. else {
  2930. if (type == CA_TYPE && format == SSL_FILETYPE_PEM)
  2931. ret = ProcessChainBuffer(ctx, myBuffer, sz, format, type, ssl);
  2932. #ifdef HAVE_CRL
  2933. else if (type == CRL_TYPE)
  2934. ret = BufferLoadCRL(crl, myBuffer, sz, format);
  2935. #endif
  2936. else
  2937. ret = ProcessBuffer(ctx, myBuffer, sz, format, type, ssl, NULL,
  2938. userChain);
  2939. }
  2940. XFCLOSE(file);
  2941. if (dynamic)
  2942. XFREE(myBuffer, heapHint, DYNAMIC_TYPE_FILE);
  2943. return ret;
  2944. }
  2945. /* loads file then loads each file in path, no c_rehash */
  2946. int wolfSSL_CTX_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
  2947. const char* path)
  2948. {
  2949. int ret = SSL_SUCCESS;
  2950. WOLFSSL_ENTER("wolfSSL_CTX_load_verify_locations");
  2951. (void)path;
  2952. if (ctx == NULL || (file == NULL && path == NULL) )
  2953. return SSL_FAILURE;
  2954. if (file)
  2955. ret = ProcessFile(ctx, file, SSL_FILETYPE_PEM, CA_TYPE, NULL, 0, NULL);
  2956. if (ret == SSL_SUCCESS && path) {
  2957. /* try to load each regular file in path */
  2958. #ifdef USE_WINDOWS_API
  2959. WIN32_FIND_DATAA FindFileData;
  2960. HANDLE hFind;
  2961. #ifdef WOLFSSL_SMALL_STACK
  2962. char* name = NULL;
  2963. #else
  2964. char name[MAX_FILENAME_SZ];
  2965. #endif
  2966. #ifdef WOLFSSL_SMALL_STACK
  2967. name = (char*)XMALLOC(MAX_FILENAME_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2968. if (name == NULL)
  2969. return MEMORY_E;
  2970. #endif
  2971. XMEMSET(name, 0, MAX_FILENAME_SZ);
  2972. XSTRNCPY(name, path, MAX_FILENAME_SZ - 4);
  2973. XSTRNCAT(name, "\\*", 3);
  2974. hFind = FindFirstFileA(name, &FindFileData);
  2975. if (hFind == INVALID_HANDLE_VALUE) {
  2976. WOLFSSL_MSG("FindFirstFile for path verify locations failed");
  2977. #ifdef WOLFSSL_SMALL_STACK
  2978. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2979. #endif
  2980. return BAD_PATH_ERROR;
  2981. }
  2982. do {
  2983. if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
  2984. XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 3);
  2985. XSTRNCAT(name, "\\", 2);
  2986. XSTRNCAT(name, FindFileData.cFileName, MAX_FILENAME_SZ/2);
  2987. ret = ProcessFile(ctx, name, SSL_FILETYPE_PEM, CA_TYPE,
  2988. NULL, 0, NULL);
  2989. }
  2990. } while (ret == SSL_SUCCESS && FindNextFileA(hFind, &FindFileData));
  2991. #ifdef WOLFSSL_SMALL_STACK
  2992. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2993. #endif
  2994. FindClose(hFind);
  2995. #elif !defined(NO_WOLFSSL_DIR)
  2996. struct dirent* entry;
  2997. DIR* dir = opendir(path);
  2998. #ifdef WOLFSSL_SMALL_STACK
  2999. char* name = NULL;
  3000. #else
  3001. char name[MAX_FILENAME_SZ];
  3002. #endif
  3003. if (dir == NULL) {
  3004. WOLFSSL_MSG("opendir path verify locations failed");
  3005. return BAD_PATH_ERROR;
  3006. }
  3007. #ifdef WOLFSSL_SMALL_STACK
  3008. name = (char*)XMALLOC(MAX_FILENAME_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3009. if (name == NULL) {
  3010. closedir(dir);
  3011. return MEMORY_E;
  3012. }
  3013. #endif
  3014. while ( ret == SSL_SUCCESS && (entry = readdir(dir)) != NULL) {
  3015. struct stat s;
  3016. XMEMSET(name, 0, MAX_FILENAME_SZ);
  3017. XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 2);
  3018. XSTRNCAT(name, "/", 1);
  3019. XSTRNCAT(name, entry->d_name, MAX_FILENAME_SZ/2);
  3020. if (stat(name, &s) != 0) {
  3021. WOLFSSL_MSG("stat on name failed");
  3022. ret = BAD_PATH_ERROR;
  3023. } else if (s.st_mode & S_IFREG)
  3024. ret = ProcessFile(ctx, name, SSL_FILETYPE_PEM, CA_TYPE,
  3025. NULL, 0, NULL);
  3026. }
  3027. #ifdef WOLFSSL_SMALL_STACK
  3028. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3029. #endif
  3030. closedir(dir);
  3031. #endif
  3032. }
  3033. return ret;
  3034. }
  3035. /* Verify the ceritficate, SSL_SUCCESS for ok, < 0 for error */
  3036. int wolfSSL_CertManagerVerify(WOLFSSL_CERT_MANAGER* cm, const char* fname,
  3037. int format)
  3038. {
  3039. int ret = SSL_FATAL_ERROR;
  3040. #ifdef WOLFSSL_SMALL_STACK
  3041. byte staticBuffer[1]; /* force heap usage */
  3042. #else
  3043. byte staticBuffer[FILE_BUFFER_SIZE];
  3044. #endif
  3045. byte* myBuffer = staticBuffer;
  3046. int dynamic = 0;
  3047. long sz = 0;
  3048. XFILE file = XFOPEN(fname, "rb");
  3049. WOLFSSL_ENTER("wolfSSL_CertManagerVerify");
  3050. if (file == XBADFILE) return SSL_BAD_FILE;
  3051. XFSEEK(file, 0, XSEEK_END);
  3052. sz = XFTELL(file);
  3053. XREWIND(file);
  3054. if (sz > MAX_WOLFSSL_FILE_SIZE || sz < 0) {
  3055. WOLFSSL_MSG("CertManagerVerify file bad size");
  3056. XFCLOSE(file);
  3057. return SSL_BAD_FILE;
  3058. }
  3059. if (sz > (long)sizeof(staticBuffer)) {
  3060. WOLFSSL_MSG("Getting dynamic buffer");
  3061. myBuffer = (byte*) XMALLOC(sz, cm->heap, DYNAMIC_TYPE_FILE);
  3062. if (myBuffer == NULL) {
  3063. XFCLOSE(file);
  3064. return SSL_BAD_FILE;
  3065. }
  3066. dynamic = 1;
  3067. }
  3068. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  3069. ret = SSL_BAD_FILE;
  3070. else
  3071. ret = wolfSSL_CertManagerVerifyBuffer(cm, myBuffer, sz, format);
  3072. XFCLOSE(file);
  3073. if (dynamic)
  3074. XFREE(myBuffer, cm->heap, DYNAMIC_TYPE_FILE);
  3075. return ret;
  3076. }
  3077. /* like load verify locations, 1 for success, < 0 for error */
  3078. int wolfSSL_CertManagerLoadCA(WOLFSSL_CERT_MANAGER* cm, const char* file,
  3079. const char* path)
  3080. {
  3081. int ret = SSL_FATAL_ERROR;
  3082. WOLFSSL_CTX* tmp;
  3083. WOLFSSL_ENTER("wolfSSL_CertManagerLoadCA");
  3084. if (cm == NULL) {
  3085. WOLFSSL_MSG("No CertManager error");
  3086. return ret;
  3087. }
  3088. tmp = wolfSSL_CTX_new(cm_pick_method());
  3089. if (tmp == NULL) {
  3090. WOLFSSL_MSG("CTX new failed");
  3091. return ret;
  3092. }
  3093. /* for tmp use */
  3094. wolfSSL_CertManagerFree(tmp->cm);
  3095. tmp->cm = cm;
  3096. ret = wolfSSL_CTX_load_verify_locations(tmp, file, path);
  3097. /* don't loose our good one */
  3098. tmp->cm = NULL;
  3099. wolfSSL_CTX_free(tmp);
  3100. return ret;
  3101. }
  3102. int wolfSSL_CTX_check_private_key(WOLFSSL_CTX* ctx)
  3103. {
  3104. /* TODO: check private against public for RSA match */
  3105. (void)ctx;
  3106. WOLFSSL_ENTER("SSL_CTX_check_private_key");
  3107. return SSL_SUCCESS;
  3108. }
  3109. #ifdef HAVE_CRL
  3110. /* check CRL if enabled, SSL_SUCCESS */
  3111. int wolfSSL_CertManagerCheckCRL(WOLFSSL_CERT_MANAGER* cm, byte* der, int sz)
  3112. {
  3113. int ret = 0;
  3114. #ifdef WOLFSSL_SMALL_STACK
  3115. DecodedCert* cert = NULL;
  3116. #else
  3117. DecodedCert cert[1];
  3118. #endif
  3119. WOLFSSL_ENTER("wolfSSL_CertManagerCheckCRL");
  3120. if (cm == NULL)
  3121. return BAD_FUNC_ARG;
  3122. if (cm->crlEnabled == 0)
  3123. return SSL_SUCCESS;
  3124. #ifdef WOLFSSL_SMALL_STACK
  3125. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  3126. DYNAMIC_TYPE_TMP_BUFFER);
  3127. if (cert == NULL)
  3128. return MEMORY_E;
  3129. #endif
  3130. InitDecodedCert(cert, der, sz, NULL);
  3131. if ((ret = ParseCertRelative(cert, CERT_TYPE, NO_VERIFY, cm)) != 0) {
  3132. WOLFSSL_MSG("ParseCert failed");
  3133. }
  3134. else if ((ret = CheckCertCRL(cm->crl, cert)) != 0) {
  3135. WOLFSSL_MSG("CheckCertCRL failed");
  3136. }
  3137. FreeDecodedCert(cert);
  3138. #ifdef WOLFSSL_SMALL_STACK
  3139. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3140. #endif
  3141. return ret == 0 ? SSL_SUCCESS : ret;
  3142. }
  3143. int wolfSSL_CertManagerSetCRL_Cb(WOLFSSL_CERT_MANAGER* cm, CbMissingCRL cb)
  3144. {
  3145. WOLFSSL_ENTER("wolfSSL_CertManagerSetCRL_Cb");
  3146. if (cm == NULL)
  3147. return BAD_FUNC_ARG;
  3148. cm->cbMissingCRL = cb;
  3149. return SSL_SUCCESS;
  3150. }
  3151. int wolfSSL_CertManagerLoadCRL(WOLFSSL_CERT_MANAGER* cm, const char* path,
  3152. int type, int monitor)
  3153. {
  3154. WOLFSSL_ENTER("wolfSSL_CertManagerLoadCRL");
  3155. if (cm == NULL)
  3156. return BAD_FUNC_ARG;
  3157. if (cm->crl == NULL) {
  3158. if (wolfSSL_CertManagerEnableCRL(cm, 0) != SSL_SUCCESS) {
  3159. WOLFSSL_MSG("Enable CRL failed");
  3160. return SSL_FATAL_ERROR;
  3161. }
  3162. }
  3163. return LoadCRL(cm->crl, path, type, monitor);
  3164. }
  3165. int wolfSSL_EnableCRL(WOLFSSL* ssl, int options)
  3166. {
  3167. WOLFSSL_ENTER("wolfSSL_EnableCRL");
  3168. if (ssl)
  3169. return wolfSSL_CertManagerEnableCRL(ssl->ctx->cm, options);
  3170. else
  3171. return BAD_FUNC_ARG;
  3172. }
  3173. int wolfSSL_DisableCRL(WOLFSSL* ssl)
  3174. {
  3175. WOLFSSL_ENTER("wolfSSL_DisableCRL");
  3176. if (ssl)
  3177. return wolfSSL_CertManagerDisableCRL(ssl->ctx->cm);
  3178. else
  3179. return BAD_FUNC_ARG;
  3180. }
  3181. int wolfSSL_LoadCRL(WOLFSSL* ssl, const char* path, int type, int monitor)
  3182. {
  3183. WOLFSSL_ENTER("wolfSSL_LoadCRL");
  3184. if (ssl)
  3185. return wolfSSL_CertManagerLoadCRL(ssl->ctx->cm, path, type, monitor);
  3186. else
  3187. return BAD_FUNC_ARG;
  3188. }
  3189. int wolfSSL_SetCRL_Cb(WOLFSSL* ssl, CbMissingCRL cb)
  3190. {
  3191. WOLFSSL_ENTER("wolfSSL_SetCRL_Cb");
  3192. if (ssl)
  3193. return wolfSSL_CertManagerSetCRL_Cb(ssl->ctx->cm, cb);
  3194. else
  3195. return BAD_FUNC_ARG;
  3196. }
  3197. int wolfSSL_CTX_EnableCRL(WOLFSSL_CTX* ctx, int options)
  3198. {
  3199. WOLFSSL_ENTER("wolfSSL_CTX_EnableCRL");
  3200. if (ctx)
  3201. return wolfSSL_CertManagerEnableCRL(ctx->cm, options);
  3202. else
  3203. return BAD_FUNC_ARG;
  3204. }
  3205. int wolfSSL_CTX_DisableCRL(WOLFSSL_CTX* ctx)
  3206. {
  3207. WOLFSSL_ENTER("wolfSSL_CTX_DisableCRL");
  3208. if (ctx)
  3209. return wolfSSL_CertManagerDisableCRL(ctx->cm);
  3210. else
  3211. return BAD_FUNC_ARG;
  3212. }
  3213. int wolfSSL_CTX_LoadCRL(WOLFSSL_CTX* ctx, const char* path,
  3214. int type, int monitor)
  3215. {
  3216. WOLFSSL_ENTER("wolfSSL_CTX_LoadCRL");
  3217. if (ctx)
  3218. return wolfSSL_CertManagerLoadCRL(ctx->cm, path, type, monitor);
  3219. else
  3220. return BAD_FUNC_ARG;
  3221. }
  3222. int wolfSSL_CTX_SetCRL_Cb(WOLFSSL_CTX* ctx, CbMissingCRL cb)
  3223. {
  3224. WOLFSSL_ENTER("wolfSSL_CTX_SetCRL_Cb");
  3225. if (ctx)
  3226. return wolfSSL_CertManagerSetCRL_Cb(ctx->cm, cb);
  3227. else
  3228. return BAD_FUNC_ARG;
  3229. }
  3230. #endif /* HAVE_CRL */
  3231. #ifdef WOLFSSL_DER_LOAD
  3232. /* Add format parameter to allow DER load of CA files */
  3233. int wolfSSL_CTX_der_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
  3234. int format)
  3235. {
  3236. WOLFSSL_ENTER("wolfSSL_CTX_der_load_verify_locations");
  3237. if (ctx == NULL || file == NULL)
  3238. return SSL_FAILURE;
  3239. if (ProcessFile(ctx, file, format, CA_TYPE, NULL, 0, NULL) == SSL_SUCCESS)
  3240. return SSL_SUCCESS;
  3241. return SSL_FAILURE;
  3242. }
  3243. #endif /* WOLFSSL_DER_LOAD */
  3244. #ifdef WOLFSSL_CERT_GEN
  3245. /* load pem cert from file into der buffer, return der size or error */
  3246. int wolfSSL_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz)
  3247. {
  3248. #ifdef WOLFSSL_SMALL_STACK
  3249. EncryptedInfo* info = NULL;
  3250. byte staticBuffer[1]; /* force XMALLOC */
  3251. #else
  3252. EncryptedInfo info[1];
  3253. byte staticBuffer[FILE_BUFFER_SIZE];
  3254. #endif
  3255. byte* fileBuf = staticBuffer;
  3256. int dynamic = 0;
  3257. int ret = 0;
  3258. int ecc = 0;
  3259. long sz = 0;
  3260. XFILE file = XFOPEN(fileName, "rb");
  3261. buffer converted;
  3262. WOLFSSL_ENTER("wolfSSL_PemCertToDer");
  3263. if (file == XBADFILE)
  3264. ret = SSL_BAD_FILE;
  3265. else {
  3266. XFSEEK(file, 0, XSEEK_END);
  3267. sz = XFTELL(file);
  3268. XREWIND(file);
  3269. if (sz < 0) {
  3270. ret = SSL_BAD_FILE;
  3271. }
  3272. else if (sz > (long)sizeof(staticBuffer)) {
  3273. fileBuf = (byte*)XMALLOC(sz, 0, DYNAMIC_TYPE_FILE);
  3274. if (fileBuf == NULL)
  3275. ret = MEMORY_E;
  3276. else
  3277. dynamic = 1;
  3278. }
  3279. converted.buffer = 0;
  3280. if (ret == 0) {
  3281. if ( (ret = (int)XFREAD(fileBuf, sz, 1, file)) < 0)
  3282. ret = SSL_BAD_FILE;
  3283. else {
  3284. #ifdef WOLFSSL_SMALL_STACK
  3285. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  3286. DYNAMIC_TYPE_TMP_BUFFER);
  3287. if (info == NULL)
  3288. ret = MEMORY_E;
  3289. else
  3290. #endif
  3291. {
  3292. ret = PemToDer(fileBuf, sz, CA_TYPE, &converted,
  3293. 0, info, &ecc);
  3294. #ifdef WOLFSSL_SMALL_STACK
  3295. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3296. #endif
  3297. }
  3298. }
  3299. if (ret == 0) {
  3300. if (converted.length < (word32)derSz) {
  3301. XMEMCPY(derBuf, converted.buffer, converted.length);
  3302. ret = converted.length;
  3303. }
  3304. else
  3305. ret = BUFFER_E;
  3306. }
  3307. XFREE(converted.buffer, 0, DYNAMIC_TYPE_CA);
  3308. }
  3309. XFCLOSE(file);
  3310. if (dynamic)
  3311. XFREE(fileBuf, 0, DYNAMIC_TYPE_FILE);
  3312. }
  3313. return ret;
  3314. }
  3315. #endif /* WOLFSSL_CERT_GEN */
  3316. #ifdef WOLFSSL_CERT_EXT
  3317. /* load pem public key from file into der buffer, return der size or error */
  3318. int wolfSSL_PemPubKeyToDer(const char* fileName,
  3319. unsigned char* derBuf, int derSz)
  3320. {
  3321. #ifdef WOLFSSL_SMALL_STACK
  3322. byte staticBuffer[1]; /* force XMALLOC */
  3323. #else
  3324. byte staticBuffer[FILE_BUFFER_SIZE];
  3325. #endif
  3326. byte* fileBuf = staticBuffer;
  3327. int dynamic = 0;
  3328. int ret = 0;
  3329. long sz = 0;
  3330. XFILE file = XFOPEN(fileName, "rb");
  3331. buffer converted;
  3332. WOLFSSL_ENTER("wolfSSL_PemPubKeyToDer");
  3333. if (file == XBADFILE)
  3334. ret = SSL_BAD_FILE;
  3335. else {
  3336. XFSEEK(file, 0, XSEEK_END);
  3337. sz = XFTELL(file);
  3338. XREWIND(file);
  3339. if (sz < 0) {
  3340. ret = SSL_BAD_FILE;
  3341. }
  3342. else if (sz > (long)sizeof(staticBuffer)) {
  3343. fileBuf = (byte*)XMALLOC(sz, 0, DYNAMIC_TYPE_FILE);
  3344. if (fileBuf == NULL)
  3345. ret = MEMORY_E;
  3346. else
  3347. dynamic = 1;
  3348. }
  3349. converted.buffer = 0;
  3350. if (ret == 0) {
  3351. if ( (ret = (int)XFREAD(fileBuf, sz, 1, file)) < 0)
  3352. ret = SSL_BAD_FILE;
  3353. else
  3354. ret = PemToDer(fileBuf, sz, PUBLICKEY_TYPE, &converted,
  3355. 0, NULL, NULL);
  3356. if (ret == 0) {
  3357. if (converted.length < (word32)derSz) {
  3358. XMEMCPY(derBuf, converted.buffer, converted.length);
  3359. ret = converted.length;
  3360. }
  3361. else
  3362. ret = BUFFER_E;
  3363. }
  3364. XFREE(converted.buffer, 0, DYNAMIC_TYPE_CA);
  3365. }
  3366. XFCLOSE(file);
  3367. if (dynamic)
  3368. XFREE(fileBuf, 0, DYNAMIC_TYPE_FILE);
  3369. }
  3370. return ret;
  3371. }
  3372. /* Return bytes written to buff or < 0 for error */
  3373. int wolfSSL_PubKeyPemToDer(const unsigned char* pem, int pemSz,
  3374. unsigned char* buff, int buffSz)
  3375. {
  3376. int ret;
  3377. buffer der;
  3378. WOLFSSL_ENTER("wolfSSL_PubKeyPemToDer");
  3379. if (pem == NULL || buff == NULL || buffSz <= 0) {
  3380. WOLFSSL_MSG("Bad pem der args");
  3381. return BAD_FUNC_ARG;
  3382. }
  3383. der.buffer = NULL;
  3384. ret = PemToDer(pem, pemSz, PUBLICKEY_TYPE, &der, NULL, NULL, NULL);
  3385. if (ret < 0) {
  3386. WOLFSSL_MSG("Bad Pem To Der");
  3387. }
  3388. else {
  3389. if (der.length <= (word32)buffSz) {
  3390. XMEMCPY(buff, der.buffer, der.length);
  3391. ret = der.length;
  3392. }
  3393. else {
  3394. WOLFSSL_MSG("Bad der length");
  3395. ret = BAD_FUNC_ARG;
  3396. }
  3397. }
  3398. XFREE(der.buffer, NULL, DYNAMIC_TYPE_KEY);
  3399. return ret;
  3400. }
  3401. #endif /* WOLFSSL_CERT_EXT */
  3402. int wolfSSL_CTX_use_certificate_file(WOLFSSL_CTX* ctx, const char* file,
  3403. int format)
  3404. {
  3405. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_file");
  3406. if (ProcessFile(ctx, file, format, CERT_TYPE, NULL, 0, NULL) == SSL_SUCCESS)
  3407. return SSL_SUCCESS;
  3408. return SSL_FAILURE;
  3409. }
  3410. int wolfSSL_CTX_use_PrivateKey_file(WOLFSSL_CTX* ctx, const char* file,
  3411. int format)
  3412. {
  3413. WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_file");
  3414. if (ProcessFile(ctx, file, format, PRIVATEKEY_TYPE, NULL, 0, NULL)
  3415. == SSL_SUCCESS)
  3416. return SSL_SUCCESS;
  3417. return SSL_FAILURE;
  3418. }
  3419. /* get cert chaining depth using ssl struct */
  3420. long wolfSSL_get_verify_depth(WOLFSSL* ssl)
  3421. {
  3422. if(ssl == NULL) {
  3423. return BAD_FUNC_ARG;
  3424. }
  3425. return MAX_CHAIN_DEPTH;
  3426. }
  3427. /* get cert chaining depth using ctx struct */
  3428. long wolfSSL_CTX_get_verify_depth(WOLFSSL_CTX* ctx)
  3429. {
  3430. if(ctx == NULL) {
  3431. return BAD_FUNC_ARG;
  3432. }
  3433. return MAX_CHAIN_DEPTH;
  3434. }
  3435. int wolfSSL_CTX_use_certificate_chain_file(WOLFSSL_CTX* ctx, const char* file)
  3436. {
  3437. /* procces up to MAX_CHAIN_DEPTH plus subject cert */
  3438. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_file");
  3439. if (ProcessFile(ctx, file, SSL_FILETYPE_PEM,CERT_TYPE,NULL,1, NULL)
  3440. == SSL_SUCCESS)
  3441. return SSL_SUCCESS;
  3442. return SSL_FAILURE;
  3443. }
  3444. #ifndef NO_DH
  3445. /* server wrapper for ctx or ssl Diffie-Hellman parameters */
  3446. static int wolfSSL_SetTmpDH_buffer_wrapper(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  3447. const unsigned char* buf,
  3448. long sz, int format)
  3449. {
  3450. buffer der;
  3451. int ret = 0;
  3452. int weOwnDer = 0;
  3453. word32 pSz = MAX_DH_SIZE;
  3454. word32 gSz = MAX_DH_SIZE;
  3455. #ifdef WOLFSSL_SMALL_STACK
  3456. byte* p = NULL;
  3457. byte* g = NULL;
  3458. #else
  3459. byte p[MAX_DH_SIZE];
  3460. byte g[MAX_DH_SIZE];
  3461. #endif
  3462. der.buffer = (byte*)buf;
  3463. der.length = (word32)sz;
  3464. #ifdef WOLFSSL_SMALL_STACK
  3465. p = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3466. g = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3467. if (p == NULL || g == NULL) {
  3468. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3469. XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3470. return MEMORY_E;
  3471. }
  3472. #endif
  3473. if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM)
  3474. ret = SSL_BAD_FILETYPE;
  3475. else {
  3476. if (format == SSL_FILETYPE_PEM) {
  3477. der.buffer = NULL;
  3478. ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, ctx->heap, NULL,NULL);
  3479. weOwnDer = 1;
  3480. }
  3481. if (ret == 0) {
  3482. if (wc_DhParamsLoad(der.buffer, der.length, p, &pSz, g, &gSz) < 0)
  3483. ret = SSL_BAD_FILETYPE;
  3484. else if (ssl)
  3485. ret = wolfSSL_SetTmpDH(ssl, p, pSz, g, gSz);
  3486. else
  3487. ret = wolfSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
  3488. }
  3489. }
  3490. if (weOwnDer)
  3491. XFREE(der.buffer, ctx->heap, DYNAMIC_TYPE_KEY);
  3492. #ifdef WOLFSSL_SMALL_STACK
  3493. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3494. XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3495. #endif
  3496. return ret;
  3497. }
  3498. /* server Diffie-Hellman parameters, SSL_SUCCESS on ok */
  3499. int wolfSSL_SetTmpDH_buffer(WOLFSSL* ssl, const unsigned char* buf, long sz,
  3500. int format)
  3501. {
  3502. return wolfSSL_SetTmpDH_buffer_wrapper(ssl->ctx, ssl, buf, sz, format);
  3503. }
  3504. /* server ctx Diffie-Hellman parameters, SSL_SUCCESS on ok */
  3505. int wolfSSL_CTX_SetTmpDH_buffer(WOLFSSL_CTX* ctx, const unsigned char* buf,
  3506. long sz, int format)
  3507. {
  3508. return wolfSSL_SetTmpDH_buffer_wrapper(ctx, NULL, buf, sz, format);
  3509. }
  3510. /* server Diffie-Hellman parameters */
  3511. static int wolfSSL_SetTmpDH_file_wrapper(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
  3512. const char* fname, int format)
  3513. {
  3514. #ifdef WOLFSSL_SMALL_STACK
  3515. byte staticBuffer[1]; /* force heap usage */
  3516. #else
  3517. byte staticBuffer[FILE_BUFFER_SIZE];
  3518. #endif
  3519. byte* myBuffer = staticBuffer;
  3520. int dynamic = 0;
  3521. int ret;
  3522. long sz = 0;
  3523. XFILE file = XFOPEN(fname, "rb");
  3524. if (file == XBADFILE) return SSL_BAD_FILE;
  3525. XFSEEK(file, 0, XSEEK_END);
  3526. sz = XFTELL(file);
  3527. XREWIND(file);
  3528. if (sz > (long)sizeof(staticBuffer)) {
  3529. WOLFSSL_MSG("Getting dynamic buffer");
  3530. myBuffer = (byte*) XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
  3531. if (myBuffer == NULL) {
  3532. XFCLOSE(file);
  3533. return SSL_BAD_FILE;
  3534. }
  3535. dynamic = 1;
  3536. }
  3537. else if (sz < 0) {
  3538. XFCLOSE(file);
  3539. return SSL_BAD_FILE;
  3540. }
  3541. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  3542. ret = SSL_BAD_FILE;
  3543. else {
  3544. if (ssl)
  3545. ret = wolfSSL_SetTmpDH_buffer(ssl, myBuffer, sz, format);
  3546. else
  3547. ret = wolfSSL_CTX_SetTmpDH_buffer(ctx, myBuffer, sz, format);
  3548. }
  3549. XFCLOSE(file);
  3550. if (dynamic)
  3551. XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
  3552. return ret;
  3553. }
  3554. /* server Diffie-Hellman parameters */
  3555. int wolfSSL_SetTmpDH_file(WOLFSSL* ssl, const char* fname, int format)
  3556. {
  3557. return wolfSSL_SetTmpDH_file_wrapper(ssl->ctx, ssl, fname, format);
  3558. }
  3559. /* server Diffie-Hellman parameters */
  3560. int wolfSSL_CTX_SetTmpDH_file(WOLFSSL_CTX* ctx, const char* fname, int format)
  3561. {
  3562. return wolfSSL_SetTmpDH_file_wrapper(ctx, NULL, fname, format);
  3563. }
  3564. #endif /* NO_DH */
  3565. #ifdef OPENSSL_EXTRA
  3566. /* put SSL type in extra for now, not very common */
  3567. int wolfSSL_use_certificate_file(WOLFSSL* ssl, const char* file, int format)
  3568. {
  3569. WOLFSSL_ENTER("wolfSSL_use_certificate_file");
  3570. if (ProcessFile(ssl->ctx, file, format, CERT_TYPE,
  3571. ssl, 0, NULL) == SSL_SUCCESS)
  3572. return SSL_SUCCESS;
  3573. return SSL_FAILURE;
  3574. }
  3575. int wolfSSL_use_PrivateKey_file(WOLFSSL* ssl, const char* file, int format)
  3576. {
  3577. WOLFSSL_ENTER("wolfSSL_use_PrivateKey_file");
  3578. if (ProcessFile(ssl->ctx, file, format, PRIVATEKEY_TYPE,
  3579. ssl, 0, NULL) == SSL_SUCCESS)
  3580. return SSL_SUCCESS;
  3581. return SSL_FAILURE;
  3582. }
  3583. int wolfSSL_use_certificate_chain_file(WOLFSSL* ssl, const char* file)
  3584. {
  3585. /* procces up to MAX_CHAIN_DEPTH plus subject cert */
  3586. WOLFSSL_ENTER("wolfSSL_use_certificate_chain_file");
  3587. if (ProcessFile(ssl->ctx, file, SSL_FILETYPE_PEM, CERT_TYPE,
  3588. ssl, 1, NULL) == SSL_SUCCESS)
  3589. return SSL_SUCCESS;
  3590. return SSL_FAILURE;
  3591. }
  3592. #ifdef HAVE_ECC
  3593. /* Set Temp CTX EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
  3594. int wolfSSL_CTX_SetTmpEC_DHE_Sz(WOLFSSL_CTX* ctx, word16 sz)
  3595. {
  3596. if (ctx == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
  3597. return BAD_FUNC_ARG;
  3598. ctx->eccTempKeySz = sz;
  3599. return SSL_SUCCESS;
  3600. }
  3601. /* Set Temp SSL EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
  3602. int wolfSSL_SetTmpEC_DHE_Sz(WOLFSSL* ssl, word16 sz)
  3603. {
  3604. if (ssl == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
  3605. return BAD_FUNC_ARG;
  3606. ssl->eccTempKeySz = sz;
  3607. return SSL_SUCCESS;
  3608. }
  3609. #endif /* HAVE_ECC */
  3610. int wolfSSL_CTX_use_RSAPrivateKey_file(WOLFSSL_CTX* ctx,const char* file,
  3611. int format)
  3612. {
  3613. WOLFSSL_ENTER("SSL_CTX_use_RSAPrivateKey_file");
  3614. return wolfSSL_CTX_use_PrivateKey_file(ctx, file, format);
  3615. }
  3616. int wolfSSL_use_RSAPrivateKey_file(WOLFSSL* ssl, const char* file, int format)
  3617. {
  3618. WOLFSSL_ENTER("wolfSSL_use_RSAPrivateKey_file");
  3619. return wolfSSL_use_PrivateKey_file(ssl, file, format);
  3620. }
  3621. #endif /* OPENSSL_EXTRA */
  3622. #ifdef HAVE_NTRU
  3623. int wolfSSL_CTX_use_NTRUPrivateKey_file(WOLFSSL_CTX* ctx, const char* file)
  3624. {
  3625. WOLFSSL_ENTER("wolfSSL_CTX_use_NTRUPrivateKey_file");
  3626. if (ctx == NULL)
  3627. return SSL_FAILURE;
  3628. if (ProcessFile(ctx, file, SSL_FILETYPE_RAW, PRIVATEKEY_TYPE, NULL, 0, NULL)
  3629. == SSL_SUCCESS) {
  3630. ctx->haveNTRU = 1;
  3631. return SSL_SUCCESS;
  3632. }
  3633. return SSL_FAILURE;
  3634. }
  3635. #endif /* HAVE_NTRU */
  3636. #endif /* NO_FILESYSTEM */
  3637. void wolfSSL_CTX_set_verify(WOLFSSL_CTX* ctx, int mode, VerifyCallback vc)
  3638. {
  3639. WOLFSSL_ENTER("wolfSSL_CTX_set_verify");
  3640. if (mode & SSL_VERIFY_PEER) {
  3641. ctx->verifyPeer = 1;
  3642. ctx->verifyNone = 0; /* in case perviously set */
  3643. }
  3644. if (mode == SSL_VERIFY_NONE) {
  3645. ctx->verifyNone = 1;
  3646. ctx->verifyPeer = 0; /* in case previously set */
  3647. }
  3648. if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3649. ctx->failNoCert = 1;
  3650. ctx->verifyCallback = vc;
  3651. }
  3652. void wolfSSL_set_verify(WOLFSSL* ssl, int mode, VerifyCallback vc)
  3653. {
  3654. WOLFSSL_ENTER("wolfSSL_set_verify");
  3655. if (mode & SSL_VERIFY_PEER) {
  3656. ssl->options.verifyPeer = 1;
  3657. ssl->options.verifyNone = 0; /* in case perviously set */
  3658. }
  3659. if (mode == SSL_VERIFY_NONE) {
  3660. ssl->options.verifyNone = 1;
  3661. ssl->options.verifyPeer = 0; /* in case previously set */
  3662. }
  3663. if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3664. ssl->options.failNoCert = 1;
  3665. ssl->verifyCallback = vc;
  3666. }
  3667. /* store user ctx for verify callback */
  3668. void wolfSSL_SetCertCbCtx(WOLFSSL* ssl, void* ctx)
  3669. {
  3670. WOLFSSL_ENTER("wolfSSL_SetCertCbCtx");
  3671. if (ssl)
  3672. ssl->verifyCbCtx = ctx;
  3673. }
  3674. /* store context CA Cache addition callback */
  3675. void wolfSSL_CTX_SetCACb(WOLFSSL_CTX* ctx, CallbackCACache cb)
  3676. {
  3677. if (ctx && ctx->cm)
  3678. ctx->cm->caCacheCallback = cb;
  3679. }
  3680. #if defined(PERSIST_CERT_CACHE)
  3681. #if !defined(NO_FILESYSTEM)
  3682. /* Persist cert cache to file */
  3683. int wolfSSL_CTX_save_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
  3684. {
  3685. WOLFSSL_ENTER("wolfSSL_CTX_save_cert_cache");
  3686. if (ctx == NULL || fname == NULL)
  3687. return BAD_FUNC_ARG;
  3688. return CM_SaveCertCache(ctx->cm, fname);
  3689. }
  3690. /* Persist cert cache from file */
  3691. int wolfSSL_CTX_restore_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
  3692. {
  3693. WOLFSSL_ENTER("wolfSSL_CTX_restore_cert_cache");
  3694. if (ctx == NULL || fname == NULL)
  3695. return BAD_FUNC_ARG;
  3696. return CM_RestoreCertCache(ctx->cm, fname);
  3697. }
  3698. #endif /* NO_FILESYSTEM */
  3699. /* Persist cert cache to memory */
  3700. int wolfSSL_CTX_memsave_cert_cache(WOLFSSL_CTX* ctx, void* mem,
  3701. int sz, int* used)
  3702. {
  3703. WOLFSSL_ENTER("wolfSSL_CTX_memsave_cert_cache");
  3704. if (ctx == NULL || mem == NULL || used == NULL || sz <= 0)
  3705. return BAD_FUNC_ARG;
  3706. return CM_MemSaveCertCache(ctx->cm, mem, sz, used);
  3707. }
  3708. /* Restore cert cache from memory */
  3709. int wolfSSL_CTX_memrestore_cert_cache(WOLFSSL_CTX* ctx, const void* mem, int sz)
  3710. {
  3711. WOLFSSL_ENTER("wolfSSL_CTX_memrestore_cert_cache");
  3712. if (ctx == NULL || mem == NULL || sz <= 0)
  3713. return BAD_FUNC_ARG;
  3714. return CM_MemRestoreCertCache(ctx->cm, mem, sz);
  3715. }
  3716. /* get how big the the cert cache save buffer needs to be */
  3717. int wolfSSL_CTX_get_cert_cache_memsize(WOLFSSL_CTX* ctx)
  3718. {
  3719. WOLFSSL_ENTER("wolfSSL_CTX_get_cert_cache_memsize");
  3720. if (ctx == NULL)
  3721. return BAD_FUNC_ARG;
  3722. return CM_GetCertCacheMemSize(ctx->cm);
  3723. }
  3724. #endif /* PERSISTE_CERT_CACHE */
  3725. #endif /* !NO_CERTS */
  3726. #ifndef NO_SESSION_CACHE
  3727. WOLFSSL_SESSION* wolfSSL_get_session(WOLFSSL* ssl)
  3728. {
  3729. WOLFSSL_ENTER("SSL_get_session");
  3730. if (ssl)
  3731. return GetSession(ssl, 0);
  3732. return NULL;
  3733. }
  3734. int wolfSSL_set_session(WOLFSSL* ssl, WOLFSSL_SESSION* session)
  3735. {
  3736. WOLFSSL_ENTER("SSL_set_session");
  3737. if (session)
  3738. return SetSession(ssl, session);
  3739. return SSL_FAILURE;
  3740. }
  3741. #ifndef NO_CLIENT_CACHE
  3742. /* Associate client session with serverID, find existing or store for saving
  3743. if newSession flag on, don't reuse existing session
  3744. SSL_SUCCESS on ok */
  3745. int wolfSSL_SetServerID(WOLFSSL* ssl, const byte* id, int len, int newSession)
  3746. {
  3747. WOLFSSL_SESSION* session = NULL;
  3748. WOLFSSL_ENTER("wolfSSL_SetServerID");
  3749. if (ssl == NULL || id == NULL || len <= 0)
  3750. return BAD_FUNC_ARG;
  3751. if (newSession == 0) {
  3752. session = GetSessionClient(ssl, id, len);
  3753. if (session) {
  3754. if (SetSession(ssl, session) != SSL_SUCCESS) {
  3755. WOLFSSL_MSG("SetSession failed");
  3756. session = NULL;
  3757. }
  3758. }
  3759. }
  3760. if (session == NULL) {
  3761. WOLFSSL_MSG("Valid ServerID not cached already");
  3762. ssl->session.idLen = (word16)min(SERVER_ID_LEN, (word32)len);
  3763. XMEMCPY(ssl->session.serverID, id, ssl->session.idLen);
  3764. }
  3765. return SSL_SUCCESS;
  3766. }
  3767. #endif /* NO_CLIENT_CACHE */
  3768. #if defined(PERSIST_SESSION_CACHE)
  3769. /* for persistance, if changes to layout need to increment and modify
  3770. save_session_cache() and restore_session_cache and memory versions too */
  3771. #define WOLFSSL_CACHE_VERSION 2
  3772. /* Session Cache Header information */
  3773. typedef struct {
  3774. int version; /* cache layout version id */
  3775. int rows; /* session rows */
  3776. int columns; /* session columns */
  3777. int sessionSz; /* sizeof WOLFSSL_SESSION */
  3778. } cache_header_t;
  3779. /* current persistence layout is:
  3780. 1) cache_header_t
  3781. 2) SessionCache
  3782. 3) ClientCache
  3783. update WOLFSSL_CACHE_VERSION if change layout for the following
  3784. PERSISTENT_SESSION_CACHE functions
  3785. */
  3786. /* get how big the the session cache save buffer needs to be */
  3787. int wolfSSL_get_session_cache_memsize(void)
  3788. {
  3789. int sz = (int)(sizeof(SessionCache) + sizeof(cache_header_t));
  3790. #ifndef NO_CLIENT_CACHE
  3791. sz += (int)(sizeof(ClientCache));
  3792. #endif
  3793. return sz;
  3794. }
  3795. /* Persist session cache to memory */
  3796. int wolfSSL_memsave_session_cache(void* mem, int sz)
  3797. {
  3798. int i;
  3799. cache_header_t cache_header;
  3800. SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
  3801. #ifndef NO_CLIENT_CACHE
  3802. ClientRow* clRow;
  3803. #endif
  3804. WOLFSSL_ENTER("wolfSSL_memsave_session_cache");
  3805. if (sz < wolfSSL_get_session_cache_memsize()) {
  3806. WOLFSSL_MSG("Memory buffer too small");
  3807. return BUFFER_E;
  3808. }
  3809. cache_header.version = WOLFSSL_CACHE_VERSION;
  3810. cache_header.rows = SESSION_ROWS;
  3811. cache_header.columns = SESSIONS_PER_ROW;
  3812. cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
  3813. XMEMCPY(mem, &cache_header, sizeof(cache_header));
  3814. if (LockMutex(&session_mutex) != 0) {
  3815. WOLFSSL_MSG("Session cache mutex lock failed");
  3816. return BAD_MUTEX_E;
  3817. }
  3818. for (i = 0; i < cache_header.rows; ++i)
  3819. XMEMCPY(row++, SessionCache + i, sizeof(SessionRow));
  3820. #ifndef NO_CLIENT_CACHE
  3821. clRow = (ClientRow*)row;
  3822. for (i = 0; i < cache_header.rows; ++i)
  3823. XMEMCPY(clRow++, ClientCache + i, sizeof(ClientRow));
  3824. #endif
  3825. UnLockMutex(&session_mutex);
  3826. WOLFSSL_LEAVE("wolfSSL_memsave_session_cache", SSL_SUCCESS);
  3827. return SSL_SUCCESS;
  3828. }
  3829. /* Restore the persistant session cache from memory */
  3830. int wolfSSL_memrestore_session_cache(const void* mem, int sz)
  3831. {
  3832. int i;
  3833. cache_header_t cache_header;
  3834. SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
  3835. #ifndef NO_CLIENT_CACHE
  3836. ClientRow* clRow;
  3837. #endif
  3838. WOLFSSL_ENTER("wolfSSL_memrestore_session_cache");
  3839. if (sz < wolfSSL_get_session_cache_memsize()) {
  3840. WOLFSSL_MSG("Memory buffer too small");
  3841. return BUFFER_E;
  3842. }
  3843. XMEMCPY(&cache_header, mem, sizeof(cache_header));
  3844. if (cache_header.version != WOLFSSL_CACHE_VERSION ||
  3845. cache_header.rows != SESSION_ROWS ||
  3846. cache_header.columns != SESSIONS_PER_ROW ||
  3847. cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION)) {
  3848. WOLFSSL_MSG("Session cache header match failed");
  3849. return CACHE_MATCH_ERROR;
  3850. }
  3851. if (LockMutex(&session_mutex) != 0) {
  3852. WOLFSSL_MSG("Session cache mutex lock failed");
  3853. return BAD_MUTEX_E;
  3854. }
  3855. for (i = 0; i < cache_header.rows; ++i)
  3856. XMEMCPY(SessionCache + i, row++, sizeof(SessionRow));
  3857. #ifndef NO_CLIENT_CACHE
  3858. clRow = (ClientRow*)row;
  3859. for (i = 0; i < cache_header.rows; ++i)
  3860. XMEMCPY(ClientCache + i, clRow++, sizeof(ClientRow));
  3861. #endif
  3862. UnLockMutex(&session_mutex);
  3863. WOLFSSL_LEAVE("wolfSSL_memrestore_session_cache", SSL_SUCCESS);
  3864. return SSL_SUCCESS;
  3865. }
  3866. #if !defined(NO_FILESYSTEM)
  3867. /* Persist session cache to file */
  3868. /* doesn't use memsave because of additional memory use */
  3869. int wolfSSL_save_session_cache(const char *fname)
  3870. {
  3871. XFILE file;
  3872. int ret;
  3873. int rc = SSL_SUCCESS;
  3874. int i;
  3875. cache_header_t cache_header;
  3876. WOLFSSL_ENTER("wolfSSL_save_session_cache");
  3877. file = XFOPEN(fname, "w+b");
  3878. if (file == XBADFILE) {
  3879. WOLFSSL_MSG("Couldn't open session cache save file");
  3880. return SSL_BAD_FILE;
  3881. }
  3882. cache_header.version = WOLFSSL_CACHE_VERSION;
  3883. cache_header.rows = SESSION_ROWS;
  3884. cache_header.columns = SESSIONS_PER_ROW;
  3885. cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
  3886. /* cache header */
  3887. ret = (int)XFWRITE(&cache_header, sizeof cache_header, 1, file);
  3888. if (ret != 1) {
  3889. WOLFSSL_MSG("Session cache header file write failed");
  3890. XFCLOSE(file);
  3891. return FWRITE_ERROR;
  3892. }
  3893. if (LockMutex(&session_mutex) != 0) {
  3894. WOLFSSL_MSG("Session cache mutex lock failed");
  3895. XFCLOSE(file);
  3896. return BAD_MUTEX_E;
  3897. }
  3898. /* session cache */
  3899. for (i = 0; i < cache_header.rows; ++i) {
  3900. ret = (int)XFWRITE(SessionCache + i, sizeof(SessionRow), 1, file);
  3901. if (ret != 1) {
  3902. WOLFSSL_MSG("Session cache member file write failed");
  3903. rc = FWRITE_ERROR;
  3904. break;
  3905. }
  3906. }
  3907. #ifndef NO_CLIENT_CACHE
  3908. /* client cache */
  3909. for (i = 0; i < cache_header.rows; ++i) {
  3910. ret = (int)XFWRITE(ClientCache + i, sizeof(ClientRow), 1, file);
  3911. if (ret != 1) {
  3912. WOLFSSL_MSG("Client cache member file write failed");
  3913. rc = FWRITE_ERROR;
  3914. break;
  3915. }
  3916. }
  3917. #endif /* NO_CLIENT_CACHE */
  3918. UnLockMutex(&session_mutex);
  3919. XFCLOSE(file);
  3920. WOLFSSL_LEAVE("wolfSSL_save_session_cache", rc);
  3921. return rc;
  3922. }
  3923. /* Restore the persistant session cache from file */
  3924. /* doesn't use memstore because of additional memory use */
  3925. int wolfSSL_restore_session_cache(const char *fname)
  3926. {
  3927. XFILE file;
  3928. int rc = SSL_SUCCESS;
  3929. int ret;
  3930. int i;
  3931. cache_header_t cache_header;
  3932. WOLFSSL_ENTER("wolfSSL_restore_session_cache");
  3933. file = XFOPEN(fname, "rb");
  3934. if (file == XBADFILE) {
  3935. WOLFSSL_MSG("Couldn't open session cache save file");
  3936. return SSL_BAD_FILE;
  3937. }
  3938. /* cache header */
  3939. ret = (int)XFREAD(&cache_header, sizeof cache_header, 1, file);
  3940. if (ret != 1) {
  3941. WOLFSSL_MSG("Session cache header file read failed");
  3942. XFCLOSE(file);
  3943. return FREAD_ERROR;
  3944. }
  3945. if (cache_header.version != WOLFSSL_CACHE_VERSION ||
  3946. cache_header.rows != SESSION_ROWS ||
  3947. cache_header.columns != SESSIONS_PER_ROW ||
  3948. cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION)) {
  3949. WOLFSSL_MSG("Session cache header match failed");
  3950. XFCLOSE(file);
  3951. return CACHE_MATCH_ERROR;
  3952. }
  3953. if (LockMutex(&session_mutex) != 0) {
  3954. WOLFSSL_MSG("Session cache mutex lock failed");
  3955. XFCLOSE(file);
  3956. return BAD_MUTEX_E;
  3957. }
  3958. /* session cache */
  3959. for (i = 0; i < cache_header.rows; ++i) {
  3960. ret = (int)XFREAD(SessionCache + i, sizeof(SessionRow), 1, file);
  3961. if (ret != 1) {
  3962. WOLFSSL_MSG("Session cache member file read failed");
  3963. XMEMSET(SessionCache, 0, sizeof SessionCache);
  3964. rc = FREAD_ERROR;
  3965. break;
  3966. }
  3967. }
  3968. #ifndef NO_CLIENT_CACHE
  3969. /* client cache */
  3970. for (i = 0; i < cache_header.rows; ++i) {
  3971. ret = (int)XFREAD(ClientCache + i, sizeof(ClientRow), 1, file);
  3972. if (ret != 1) {
  3973. WOLFSSL_MSG("Client cache member file read failed");
  3974. XMEMSET(ClientCache, 0, sizeof ClientCache);
  3975. rc = FREAD_ERROR;
  3976. break;
  3977. }
  3978. }
  3979. #endif /* NO_CLIENT_CACHE */
  3980. UnLockMutex(&session_mutex);
  3981. XFCLOSE(file);
  3982. WOLFSSL_LEAVE("wolfSSL_restore_session_cache", rc);
  3983. return rc;
  3984. }
  3985. #endif /* !NO_FILESYSTEM */
  3986. #endif /* PERSIST_SESSION_CACHE */
  3987. #endif /* NO_SESSION_CACHE */
  3988. void wolfSSL_load_error_strings(void) /* compatibility only */
  3989. {}
  3990. int wolfSSL_library_init(void)
  3991. {
  3992. WOLFSSL_ENTER("SSL_library_init");
  3993. if (wolfSSL_Init() == SSL_SUCCESS)
  3994. return SSL_SUCCESS;
  3995. else
  3996. return SSL_FATAL_ERROR;
  3997. }
  3998. #ifdef HAVE_SECRET_CALLBACK
  3999. int wolfSSL_set_session_secret_cb(WOLFSSL* ssl, SessionSecretCb cb, void* ctx)
  4000. {
  4001. WOLFSSL_ENTER("wolfSSL_set_session_secret_cb");
  4002. if (ssl == NULL)
  4003. return SSL_FATAL_ERROR;
  4004. ssl->sessionSecretCb = cb;
  4005. ssl->sessionSecretCtx = ctx;
  4006. /* If using a pre-set key, assume session resumption. */
  4007. ssl->session.sessionIDSz = 0;
  4008. ssl->options.resuming = 1;
  4009. return SSL_SUCCESS;
  4010. }
  4011. #endif
  4012. #ifndef NO_SESSION_CACHE
  4013. /* on by default if built in but allow user to turn off */
  4014. long wolfSSL_CTX_set_session_cache_mode(WOLFSSL_CTX* ctx, long mode)
  4015. {
  4016. WOLFSSL_ENTER("SSL_CTX_set_session_cache_mode");
  4017. if (mode == SSL_SESS_CACHE_OFF)
  4018. ctx->sessionCacheOff = 1;
  4019. if (mode == SSL_SESS_CACHE_NO_AUTO_CLEAR)
  4020. ctx->sessionCacheFlushOff = 1;
  4021. return SSL_SUCCESS;
  4022. }
  4023. #endif /* NO_SESSION_CACHE */
  4024. #if !defined(NO_CERTS)
  4025. #if defined(PERSIST_CERT_CACHE)
  4026. #define WOLFSSL_CACHE_CERT_VERSION 1
  4027. typedef struct {
  4028. int version; /* cache cert layout version id */
  4029. int rows; /* hash table rows, CA_TABLE_SIZE */
  4030. int columns[CA_TABLE_SIZE]; /* columns per row on list */
  4031. int signerSz; /* sizeof Signer object */
  4032. } CertCacheHeader;
  4033. /* current cert persistance layout is:
  4034. 1) CertCacheHeader
  4035. 2) caTable
  4036. update WOLFSSL_CERT_CACHE_VERSION if change layout for the following
  4037. PERSIST_CERT_CACHE functions
  4038. */
  4039. /* Return memory needed to persist this signer, have lock */
  4040. static INLINE int GetSignerMemory(Signer* signer)
  4041. {
  4042. int sz = sizeof(signer->pubKeySize) + sizeof(signer->keyOID)
  4043. + sizeof(signer->nameLen) + sizeof(signer->subjectNameHash);
  4044. #if !defined(NO_SKID)
  4045. sz += (int)sizeof(signer->subjectKeyIdHash);
  4046. #endif
  4047. /* add dynamic bytes needed */
  4048. sz += signer->pubKeySize;
  4049. sz += signer->nameLen;
  4050. return sz;
  4051. }
  4052. /* Return memory needed to persist this row, have lock */
  4053. static INLINE int GetCertCacheRowMemory(Signer* row)
  4054. {
  4055. int sz = 0;
  4056. while (row) {
  4057. sz += GetSignerMemory(row);
  4058. row = row->next;
  4059. }
  4060. return sz;
  4061. }
  4062. /* get the size of persist cert cache, have lock */
  4063. static INLINE int GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm)
  4064. {
  4065. int sz;
  4066. int i;
  4067. sz = sizeof(CertCacheHeader);
  4068. for (i = 0; i < CA_TABLE_SIZE; i++)
  4069. sz += GetCertCacheRowMemory(cm->caTable[i]);
  4070. return sz;
  4071. }
  4072. /* Store cert cache header columns with number of items per list, have lock */
  4073. static INLINE void SetCertHeaderColumns(WOLFSSL_CERT_MANAGER* cm, int* columns)
  4074. {
  4075. int i;
  4076. Signer* row;
  4077. for (i = 0; i < CA_TABLE_SIZE; i++) {
  4078. int count = 0;
  4079. row = cm->caTable[i];
  4080. while (row) {
  4081. ++count;
  4082. row = row->next;
  4083. }
  4084. columns[i] = count;
  4085. }
  4086. }
  4087. /* Restore whole cert row from memory, have lock, return bytes consumed,
  4088. < 0 on error, have lock */
  4089. static INLINE int RestoreCertRow(WOLFSSL_CERT_MANAGER* cm, byte* current,
  4090. int row, int listSz, const byte* end)
  4091. {
  4092. int idx = 0;
  4093. if (listSz < 0) {
  4094. WOLFSSL_MSG("Row header corrupted, negative value");
  4095. return PARSE_ERROR;
  4096. }
  4097. while (listSz) {
  4098. Signer* signer;
  4099. byte* start = current + idx; /* for end checks on this signer */
  4100. int minSz = sizeof(signer->pubKeySize) + sizeof(signer->keyOID) +
  4101. sizeof(signer->nameLen) + sizeof(signer->subjectNameHash);
  4102. #ifndef NO_SKID
  4103. minSz += (int)sizeof(signer->subjectKeyIdHash);
  4104. #endif
  4105. if (start + minSz > end) {
  4106. WOLFSSL_MSG("Would overread restore buffer");
  4107. return BUFFER_E;
  4108. }
  4109. signer = MakeSigner(cm->heap);
  4110. if (signer == NULL)
  4111. return MEMORY_E;
  4112. /* pubKeySize */
  4113. XMEMCPY(&signer->pubKeySize, current + idx, sizeof(signer->pubKeySize));
  4114. idx += (int)sizeof(signer->pubKeySize);
  4115. /* keyOID */
  4116. XMEMCPY(&signer->keyOID, current + idx, sizeof(signer->keyOID));
  4117. idx += (int)sizeof(signer->keyOID);
  4118. /* pulicKey */
  4119. if (start + minSz + signer->pubKeySize > end) {
  4120. WOLFSSL_MSG("Would overread restore buffer");
  4121. FreeSigner(signer, cm->heap);
  4122. return BUFFER_E;
  4123. }
  4124. signer->publicKey = (byte*)XMALLOC(signer->pubKeySize, cm->heap,
  4125. DYNAMIC_TYPE_KEY);
  4126. if (signer->publicKey == NULL) {
  4127. FreeSigner(signer, cm->heap);
  4128. return MEMORY_E;
  4129. }
  4130. XMEMCPY(signer->publicKey, current + idx, signer->pubKeySize);
  4131. idx += signer->pubKeySize;
  4132. /* nameLen */
  4133. XMEMCPY(&signer->nameLen, current + idx, sizeof(signer->nameLen));
  4134. idx += (int)sizeof(signer->nameLen);
  4135. /* name */
  4136. if (start + minSz + signer->pubKeySize + signer->nameLen > end) {
  4137. WOLFSSL_MSG("Would overread restore buffer");
  4138. FreeSigner(signer, cm->heap);
  4139. return BUFFER_E;
  4140. }
  4141. signer->name = (char*)XMALLOC(signer->nameLen, cm->heap,
  4142. DYNAMIC_TYPE_SUBJECT_CN);
  4143. if (signer->name == NULL) {
  4144. FreeSigner(signer, cm->heap);
  4145. return MEMORY_E;
  4146. }
  4147. XMEMCPY(signer->name, current + idx, signer->nameLen);
  4148. idx += signer->nameLen;
  4149. /* subjectNameHash */
  4150. XMEMCPY(signer->subjectNameHash, current + idx, SIGNER_DIGEST_SIZE);
  4151. idx += SIGNER_DIGEST_SIZE;
  4152. #ifndef NO_SKID
  4153. /* subjectKeyIdHash */
  4154. XMEMCPY(signer->subjectKeyIdHash, current + idx,SIGNER_DIGEST_SIZE);
  4155. idx += SIGNER_DIGEST_SIZE;
  4156. #endif
  4157. signer->next = cm->caTable[row];
  4158. cm->caTable[row] = signer;
  4159. --listSz;
  4160. }
  4161. return idx;
  4162. }
  4163. /* Store whole cert row into memory, have lock, return bytes added */
  4164. static INLINE int StoreCertRow(WOLFSSL_CERT_MANAGER* cm, byte* current, int row)
  4165. {
  4166. int added = 0;
  4167. Signer* list = cm->caTable[row];
  4168. while (list) {
  4169. XMEMCPY(current + added, &list->pubKeySize, sizeof(list->pubKeySize));
  4170. added += (int)sizeof(list->pubKeySize);
  4171. XMEMCPY(current + added, &list->keyOID, sizeof(list->keyOID));
  4172. added += (int)sizeof(list->keyOID);
  4173. XMEMCPY(current + added, list->publicKey, list->pubKeySize);
  4174. added += list->pubKeySize;
  4175. XMEMCPY(current + added, &list->nameLen, sizeof(list->nameLen));
  4176. added += (int)sizeof(list->nameLen);
  4177. XMEMCPY(current + added, list->name, list->nameLen);
  4178. added += list->nameLen;
  4179. XMEMCPY(current + added, list->subjectNameHash, SIGNER_DIGEST_SIZE);
  4180. added += SIGNER_DIGEST_SIZE;
  4181. #ifndef NO_SKID
  4182. XMEMCPY(current + added, list->subjectKeyIdHash,SIGNER_DIGEST_SIZE);
  4183. added += SIGNER_DIGEST_SIZE;
  4184. #endif
  4185. list = list->next;
  4186. }
  4187. return added;
  4188. }
  4189. /* Persist cert cache to memory, have lock */
  4190. static INLINE int DoMemSaveCertCache(WOLFSSL_CERT_MANAGER* cm,
  4191. void* mem, int sz)
  4192. {
  4193. int realSz;
  4194. int ret = SSL_SUCCESS;
  4195. int i;
  4196. WOLFSSL_ENTER("DoMemSaveCertCache");
  4197. realSz = GetCertCacheMemSize(cm);
  4198. if (realSz > sz) {
  4199. WOLFSSL_MSG("Mem output buffer too small");
  4200. ret = BUFFER_E;
  4201. }
  4202. else {
  4203. byte* current;
  4204. CertCacheHeader hdr;
  4205. hdr.version = WOLFSSL_CACHE_CERT_VERSION;
  4206. hdr.rows = CA_TABLE_SIZE;
  4207. SetCertHeaderColumns(cm, hdr.columns);
  4208. hdr.signerSz = (int)sizeof(Signer);
  4209. XMEMCPY(mem, &hdr, sizeof(CertCacheHeader));
  4210. current = (byte*)mem + sizeof(CertCacheHeader);
  4211. for (i = 0; i < CA_TABLE_SIZE; ++i)
  4212. current += StoreCertRow(cm, current, i);
  4213. }
  4214. return ret;
  4215. }
  4216. #if !defined(NO_FILESYSTEM)
  4217. /* Persist cert cache to file */
  4218. int CM_SaveCertCache(WOLFSSL_CERT_MANAGER* cm, const char* fname)
  4219. {
  4220. XFILE file;
  4221. int rc = SSL_SUCCESS;
  4222. int memSz;
  4223. byte* mem;
  4224. WOLFSSL_ENTER("CM_SaveCertCache");
  4225. file = XFOPEN(fname, "w+b");
  4226. if (file == XBADFILE) {
  4227. WOLFSSL_MSG("Couldn't open cert cache save file");
  4228. return SSL_BAD_FILE;
  4229. }
  4230. if (LockMutex(&cm->caLock) != 0) {
  4231. WOLFSSL_MSG("LockMutex on caLock failed");
  4232. XFCLOSE(file);
  4233. return BAD_MUTEX_E;
  4234. }
  4235. memSz = GetCertCacheMemSize(cm);
  4236. mem = (byte*)XMALLOC(memSz, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
  4237. if (mem == NULL) {
  4238. WOLFSSL_MSG("Alloc for tmp buffer failed");
  4239. rc = MEMORY_E;
  4240. } else {
  4241. rc = DoMemSaveCertCache(cm, mem, memSz);
  4242. if (rc == SSL_SUCCESS) {
  4243. int ret = (int)XFWRITE(mem, memSz, 1, file);
  4244. if (ret != 1) {
  4245. WOLFSSL_MSG("Cert cache file write failed");
  4246. rc = FWRITE_ERROR;
  4247. }
  4248. }
  4249. XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
  4250. }
  4251. UnLockMutex(&cm->caLock);
  4252. XFCLOSE(file);
  4253. return rc;
  4254. }
  4255. /* Restore cert cache from file */
  4256. int CM_RestoreCertCache(WOLFSSL_CERT_MANAGER* cm, const char* fname)
  4257. {
  4258. XFILE file;
  4259. int rc = SSL_SUCCESS;
  4260. int ret;
  4261. int memSz;
  4262. byte* mem;
  4263. WOLFSSL_ENTER("CM_RestoreCertCache");
  4264. file = XFOPEN(fname, "rb");
  4265. if (file == XBADFILE) {
  4266. WOLFSSL_MSG("Couldn't open cert cache save file");
  4267. return SSL_BAD_FILE;
  4268. }
  4269. XFSEEK(file, 0, XSEEK_END);
  4270. memSz = (int)XFTELL(file);
  4271. XREWIND(file);
  4272. if (memSz <= 0) {
  4273. WOLFSSL_MSG("Bad file size");
  4274. XFCLOSE(file);
  4275. return SSL_BAD_FILE;
  4276. }
  4277. mem = (byte*)XMALLOC(memSz, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
  4278. if (mem == NULL) {
  4279. WOLFSSL_MSG("Alloc for tmp buffer failed");
  4280. XFCLOSE(file);
  4281. return MEMORY_E;
  4282. }
  4283. ret = (int)XFREAD(mem, memSz, 1, file);
  4284. if (ret != 1) {
  4285. WOLFSSL_MSG("Cert file read error");
  4286. rc = FREAD_ERROR;
  4287. } else {
  4288. rc = CM_MemRestoreCertCache(cm, mem, memSz);
  4289. if (rc != SSL_SUCCESS) {
  4290. WOLFSSL_MSG("Mem restore cert cache failed");
  4291. }
  4292. }
  4293. XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
  4294. XFCLOSE(file);
  4295. return rc;
  4296. }
  4297. #endif /* NO_FILESYSTEM */
  4298. /* Persist cert cache to memory */
  4299. int CM_MemSaveCertCache(WOLFSSL_CERT_MANAGER* cm, void* mem, int sz, int* used)
  4300. {
  4301. int ret = SSL_SUCCESS;
  4302. WOLFSSL_ENTER("CM_MemSaveCertCache");
  4303. if (LockMutex(&cm->caLock) != 0) {
  4304. WOLFSSL_MSG("LockMutex on caLock failed");
  4305. return BAD_MUTEX_E;
  4306. }
  4307. ret = DoMemSaveCertCache(cm, mem, sz);
  4308. if (ret == SSL_SUCCESS)
  4309. *used = GetCertCacheMemSize(cm);
  4310. UnLockMutex(&cm->caLock);
  4311. return ret;
  4312. }
  4313. /* Restore cert cache from memory */
  4314. int CM_MemRestoreCertCache(WOLFSSL_CERT_MANAGER* cm, const void* mem, int sz)
  4315. {
  4316. int ret = SSL_SUCCESS;
  4317. int i;
  4318. CertCacheHeader* hdr = (CertCacheHeader*)mem;
  4319. byte* current = (byte*)mem + sizeof(CertCacheHeader);
  4320. byte* end = (byte*)mem + sz; /* don't go over */
  4321. WOLFSSL_ENTER("CM_MemRestoreCertCache");
  4322. if (current > end) {
  4323. WOLFSSL_MSG("Cert Cache Memory buffer too small");
  4324. return BUFFER_E;
  4325. }
  4326. if (hdr->version != WOLFSSL_CACHE_CERT_VERSION ||
  4327. hdr->rows != CA_TABLE_SIZE ||
  4328. hdr->signerSz != (int)sizeof(Signer)) {
  4329. WOLFSSL_MSG("Cert Cache Memory header mismatch");
  4330. return CACHE_MATCH_ERROR;
  4331. }
  4332. if (LockMutex(&cm->caLock) != 0) {
  4333. WOLFSSL_MSG("LockMutex on caLock failed");
  4334. return BAD_MUTEX_E;
  4335. }
  4336. FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
  4337. for (i = 0; i < CA_TABLE_SIZE; ++i) {
  4338. int added = RestoreCertRow(cm, current, i, hdr->columns[i], end);
  4339. if (added < 0) {
  4340. WOLFSSL_MSG("RestoreCertRow error");
  4341. ret = added;
  4342. break;
  4343. }
  4344. current += added;
  4345. }
  4346. UnLockMutex(&cm->caLock);
  4347. return ret;
  4348. }
  4349. /* get how big the the cert cache save buffer needs to be */
  4350. int CM_GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm)
  4351. {
  4352. int sz;
  4353. WOLFSSL_ENTER("CM_GetCertCacheMemSize");
  4354. if (LockMutex(&cm->caLock) != 0) {
  4355. WOLFSSL_MSG("LockMutex on caLock failed");
  4356. return BAD_MUTEX_E;
  4357. }
  4358. sz = GetCertCacheMemSize(cm);
  4359. UnLockMutex(&cm->caLock);
  4360. return sz;
  4361. }
  4362. #endif /* PERSIST_CERT_CACHE */
  4363. #endif /* NO_CERTS */
  4364. int wolfSSL_CTX_set_cipher_list(WOLFSSL_CTX* ctx, const char* list)
  4365. {
  4366. WOLFSSL_ENTER("wolfSSL_CTX_set_cipher_list");
  4367. /* alloc/init on demand only */
  4368. if (ctx->suites == NULL) {
  4369. ctx->suites = (Suites*)XMALLOC(sizeof(Suites), ctx->heap,
  4370. DYNAMIC_TYPE_SUITES);
  4371. if (ctx->suites == NULL) {
  4372. WOLFSSL_MSG("Memory alloc for Suites failed");
  4373. return SSL_FAILURE;
  4374. }
  4375. XMEMSET(ctx->suites, 0, sizeof(Suites));
  4376. }
  4377. return (SetCipherList(ctx->suites, list)) ? SSL_SUCCESS : SSL_FAILURE;
  4378. }
  4379. int wolfSSL_set_cipher_list(WOLFSSL* ssl, const char* list)
  4380. {
  4381. WOLFSSL_ENTER("wolfSSL_set_cipher_list");
  4382. return (SetCipherList(ssl->suites, list)) ? SSL_SUCCESS : SSL_FAILURE;
  4383. }
  4384. #ifndef WOLFSSL_LEANPSK
  4385. #ifdef WOLFSSL_DTLS
  4386. int wolfSSL_dtls_get_current_timeout(WOLFSSL* ssl)
  4387. {
  4388. (void)ssl;
  4389. return ssl->dtls_timeout;
  4390. }
  4391. /* user may need to alter init dtls recv timeout, SSL_SUCCESS on ok */
  4392. int wolfSSL_dtls_set_timeout_init(WOLFSSL* ssl, int timeout)
  4393. {
  4394. if (ssl == NULL || timeout < 0)
  4395. return BAD_FUNC_ARG;
  4396. if (timeout > ssl->dtls_timeout_max) {
  4397. WOLFSSL_MSG("Can't set dtls timeout init greater than dtls timeout max");
  4398. return BAD_FUNC_ARG;
  4399. }
  4400. ssl->dtls_timeout_init = timeout;
  4401. ssl->dtls_timeout = timeout;
  4402. return SSL_SUCCESS;
  4403. }
  4404. /* user may need to alter max dtls recv timeout, SSL_SUCCESS on ok */
  4405. int wolfSSL_dtls_set_timeout_max(WOLFSSL* ssl, int timeout)
  4406. {
  4407. if (ssl == NULL || timeout < 0)
  4408. return BAD_FUNC_ARG;
  4409. if (timeout < ssl->dtls_timeout_init) {
  4410. WOLFSSL_MSG("Can't set dtls timeout max less than dtls timeout init");
  4411. return BAD_FUNC_ARG;
  4412. }
  4413. ssl->dtls_timeout_max = timeout;
  4414. return SSL_SUCCESS;
  4415. }
  4416. int wolfSSL_dtls_got_timeout(WOLFSSL* ssl)
  4417. {
  4418. int result = SSL_SUCCESS;
  4419. DtlsMsgListDelete(ssl->dtls_msg_list, ssl->heap);
  4420. ssl->dtls_msg_list = NULL;
  4421. if (DtlsPoolTimeout(ssl) < 0 || DtlsPoolSend(ssl) < 0) {
  4422. result = SSL_FATAL_ERROR;
  4423. }
  4424. return result;
  4425. }
  4426. #endif /* DTLS */
  4427. #endif /* LEANPSK */
  4428. #if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
  4429. /* Not an SSL function, return 0 for success, error code otherwise */
  4430. /* Prereq: ssl's RNG needs to be initialized. */
  4431. int wolfSSL_DTLS_SetCookieSecret(WOLFSSL* ssl,
  4432. const byte* secret, word32 secretSz)
  4433. {
  4434. WOLFSSL_ENTER("wolfSSL_DTLS_SetCookieSecret");
  4435. if (ssl == NULL) {
  4436. WOLFSSL_MSG("need a SSL object");
  4437. return BAD_FUNC_ARG;
  4438. }
  4439. if (secret != NULL && secretSz == 0) {
  4440. WOLFSSL_MSG("can't have a new secret without a size");
  4441. return BAD_FUNC_ARG;
  4442. }
  4443. /* If secretSz is 0, use the default size. */
  4444. if (secretSz == 0)
  4445. secretSz = COOKIE_SECRET_SZ;
  4446. if (secretSz != ssl->buffers.dtlsCookieSecret.length) {
  4447. byte* newSecret;
  4448. if (ssl->buffers.dtlsCookieSecret.buffer != NULL) {
  4449. ForceZero(ssl->buffers.dtlsCookieSecret.buffer,
  4450. ssl->buffers.dtlsCookieSecret.length);
  4451. XFREE(ssl->buffers.dtlsCookieSecret.buffer,
  4452. ssl->heap, DYNAMIC_TYPE_NONE);
  4453. }
  4454. newSecret = (byte*)XMALLOC(secretSz, ssl->heap,DYNAMIC_TYPE_COOKIE_PWD);
  4455. if (newSecret == NULL) {
  4456. ssl->buffers.dtlsCookieSecret.buffer = NULL;
  4457. ssl->buffers.dtlsCookieSecret.length = 0;
  4458. WOLFSSL_MSG("couldn't allocate new cookie secret");
  4459. return MEMORY_ERROR;
  4460. }
  4461. ssl->buffers.dtlsCookieSecret.buffer = newSecret;
  4462. ssl->buffers.dtlsCookieSecret.length = secretSz;
  4463. }
  4464. /* If the supplied secret is NULL, randomly generate a new secret. */
  4465. if (secret == NULL)
  4466. wc_RNG_GenerateBlock(ssl->rng,
  4467. ssl->buffers.dtlsCookieSecret.buffer, secretSz);
  4468. else
  4469. XMEMCPY(ssl->buffers.dtlsCookieSecret.buffer, secret, secretSz);
  4470. WOLFSSL_LEAVE("wolfSSL_DTLS_SetCookieSecret", 0);
  4471. return 0;
  4472. }
  4473. #endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
  4474. /* client only parts */
  4475. #ifndef NO_WOLFSSL_CLIENT
  4476. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  4477. WOLFSSL_METHOD* wolfSSLv3_client_method(void)
  4478. {
  4479. WOLFSSL_METHOD* method =
  4480. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  4481. 0, DYNAMIC_TYPE_METHOD);
  4482. WOLFSSL_ENTER("SSLv3_client_method");
  4483. if (method)
  4484. InitSSL_Method(method, MakeSSLv3());
  4485. return method;
  4486. }
  4487. #endif
  4488. #ifdef WOLFSSL_DTLS
  4489. #ifndef NO_OLD_TLS
  4490. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  4491. {
  4492. WOLFSSL_METHOD* method =
  4493. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  4494. 0, DYNAMIC_TYPE_METHOD);
  4495. WOLFSSL_ENTER("DTLSv1_client_method");
  4496. if (method)
  4497. InitSSL_Method(method, MakeDTLSv1());
  4498. return method;
  4499. }
  4500. #endif /* NO_OLD_TLS */
  4501. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  4502. {
  4503. WOLFSSL_METHOD* method =
  4504. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  4505. 0, DYNAMIC_TYPE_METHOD);
  4506. WOLFSSL_ENTER("DTLSv1_2_client_method");
  4507. if (method)
  4508. InitSSL_Method(method, MakeDTLSv1_2());
  4509. return method;
  4510. }
  4511. #endif
  4512. /* please see note at top of README if you get an error from connect */
  4513. int wolfSSL_connect(WOLFSSL* ssl)
  4514. {
  4515. int neededState;
  4516. WOLFSSL_ENTER("SSL_connect()");
  4517. #ifdef HAVE_ERRNO_H
  4518. errno = 0;
  4519. #endif
  4520. if (ssl->options.side != WOLFSSL_CLIENT_END) {
  4521. WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
  4522. return SSL_FATAL_ERROR;
  4523. }
  4524. #ifdef WOLFSSL_DTLS
  4525. if (ssl->version.major == DTLS_MAJOR) {
  4526. ssl->options.dtls = 1;
  4527. ssl->options.tls = 1;
  4528. ssl->options.tls1_1 = 1;
  4529. if (DtlsPoolInit(ssl) != 0) {
  4530. ssl->error = MEMORY_ERROR;
  4531. WOLFSSL_ERROR(ssl->error);
  4532. return SSL_FATAL_ERROR;
  4533. }
  4534. }
  4535. #endif
  4536. if (ssl->buffers.outputBuffer.length > 0) {
  4537. if ( (ssl->error = SendBuffered(ssl)) == 0) {
  4538. if (ssl->fragOffset == 0) {
  4539. ssl->options.connectState++;
  4540. WOLFSSL_MSG("connect state: Advanced from buffered send");
  4541. }
  4542. }
  4543. else {
  4544. WOLFSSL_ERROR(ssl->error);
  4545. return SSL_FATAL_ERROR;
  4546. }
  4547. }
  4548. switch (ssl->options.connectState) {
  4549. case CONNECT_BEGIN :
  4550. /* always send client hello first */
  4551. if ( (ssl->error = SendClientHello(ssl)) != 0) {
  4552. WOLFSSL_ERROR(ssl->error);
  4553. return SSL_FATAL_ERROR;
  4554. }
  4555. ssl->options.connectState = CLIENT_HELLO_SENT;
  4556. WOLFSSL_MSG("connect state: CLIENT_HELLO_SENT");
  4557. case CLIENT_HELLO_SENT :
  4558. neededState = ssl->options.resuming ? SERVER_FINISHED_COMPLETE :
  4559. SERVER_HELLODONE_COMPLETE;
  4560. #ifdef WOLFSSL_DTLS
  4561. /* In DTLS, when resuming, we can go straight to FINISHED,
  4562. * or do a cookie exchange and then skip to FINISHED, assume
  4563. * we need the cookie exchange first. */
  4564. if (ssl->options.dtls)
  4565. neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
  4566. #endif
  4567. /* get response */
  4568. while (ssl->options.serverState < neededState) {
  4569. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4570. WOLFSSL_ERROR(ssl->error);
  4571. return SSL_FATAL_ERROR;
  4572. }
  4573. /* if resumption failed, reset needed state */
  4574. else if (neededState == SERVER_FINISHED_COMPLETE)
  4575. if (!ssl->options.resuming) {
  4576. if (!ssl->options.dtls)
  4577. neededState = SERVER_HELLODONE_COMPLETE;
  4578. else
  4579. neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
  4580. }
  4581. }
  4582. ssl->options.connectState = HELLO_AGAIN;
  4583. WOLFSSL_MSG("connect state: HELLO_AGAIN");
  4584. case HELLO_AGAIN :
  4585. if (ssl->options.certOnly)
  4586. return SSL_SUCCESS;
  4587. #ifdef WOLFSSL_DTLS
  4588. if (ssl->options.dtls) {
  4589. /* re-init hashes, exclude first hello and verify request */
  4590. #ifndef NO_OLD_TLS
  4591. wc_InitMd5(&ssl->hsHashes->hashMd5);
  4592. if ( (ssl->error = wc_InitSha(&ssl->hsHashes->hashSha))
  4593. != 0) {
  4594. WOLFSSL_ERROR(ssl->error);
  4595. return SSL_FATAL_ERROR;
  4596. }
  4597. #endif
  4598. if (IsAtLeastTLSv1_2(ssl)) {
  4599. #ifndef NO_SHA256
  4600. if ( (ssl->error = wc_InitSha256(
  4601. &ssl->hsHashes->hashSha256)) != 0) {
  4602. WOLFSSL_ERROR(ssl->error);
  4603. return SSL_FATAL_ERROR;
  4604. }
  4605. #endif
  4606. #ifdef WOLFSSL_SHA384
  4607. if ( (ssl->error = wc_InitSha384(
  4608. &ssl->hsHashes->hashSha384)) != 0) {
  4609. WOLFSSL_ERROR(ssl->error);
  4610. return SSL_FATAL_ERROR;
  4611. }
  4612. #endif
  4613. #ifdef WOLFSSL_SHA512
  4614. if ( (ssl->error = wc_InitSha512(
  4615. &ssl->hsHashes->hashSha512)) != 0) {
  4616. WOLFSSL_ERROR(ssl->error);
  4617. return SSL_FATAL_ERROR;
  4618. }
  4619. #endif
  4620. }
  4621. if ( (ssl->error = SendClientHello(ssl)) != 0) {
  4622. WOLFSSL_ERROR(ssl->error);
  4623. return SSL_FATAL_ERROR;
  4624. }
  4625. }
  4626. #endif
  4627. ssl->options.connectState = HELLO_AGAIN_REPLY;
  4628. WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY");
  4629. case HELLO_AGAIN_REPLY :
  4630. #ifdef WOLFSSL_DTLS
  4631. if (ssl->options.dtls) {
  4632. neededState = ssl->options.resuming ?
  4633. SERVER_FINISHED_COMPLETE : SERVER_HELLODONE_COMPLETE;
  4634. /* get response */
  4635. while (ssl->options.serverState < neededState) {
  4636. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4637. WOLFSSL_ERROR(ssl->error);
  4638. return SSL_FATAL_ERROR;
  4639. }
  4640. /* if resumption failed, reset needed state */
  4641. else if (neededState == SERVER_FINISHED_COMPLETE)
  4642. if (!ssl->options.resuming)
  4643. neededState = SERVER_HELLODONE_COMPLETE;
  4644. }
  4645. }
  4646. #endif
  4647. ssl->options.connectState = FIRST_REPLY_DONE;
  4648. WOLFSSL_MSG("connect state: FIRST_REPLY_DONE");
  4649. case FIRST_REPLY_DONE :
  4650. #ifndef NO_CERTS
  4651. if (ssl->options.sendVerify) {
  4652. if ( (ssl->error = SendCertificate(ssl)) != 0) {
  4653. WOLFSSL_ERROR(ssl->error);
  4654. return SSL_FATAL_ERROR;
  4655. }
  4656. WOLFSSL_MSG("sent: certificate");
  4657. }
  4658. #endif
  4659. ssl->options.connectState = FIRST_REPLY_FIRST;
  4660. WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST");
  4661. case FIRST_REPLY_FIRST :
  4662. if (!ssl->options.resuming) {
  4663. if ( (ssl->error = SendClientKeyExchange(ssl)) != 0) {
  4664. WOLFSSL_ERROR(ssl->error);
  4665. return SSL_FATAL_ERROR;
  4666. }
  4667. WOLFSSL_MSG("sent: client key exchange");
  4668. }
  4669. ssl->options.connectState = FIRST_REPLY_SECOND;
  4670. WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND");
  4671. case FIRST_REPLY_SECOND :
  4672. #ifndef NO_CERTS
  4673. if (ssl->options.sendVerify) {
  4674. if ( (ssl->error = SendCertificateVerify(ssl)) != 0) {
  4675. WOLFSSL_ERROR(ssl->error);
  4676. return SSL_FATAL_ERROR;
  4677. }
  4678. WOLFSSL_MSG("sent: certificate verify");
  4679. }
  4680. #endif
  4681. ssl->options.connectState = FIRST_REPLY_THIRD;
  4682. WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD");
  4683. case FIRST_REPLY_THIRD :
  4684. if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
  4685. WOLFSSL_ERROR(ssl->error);
  4686. return SSL_FATAL_ERROR;
  4687. }
  4688. WOLFSSL_MSG("sent: change cipher spec");
  4689. ssl->options.connectState = FIRST_REPLY_FOURTH;
  4690. WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH");
  4691. case FIRST_REPLY_FOURTH :
  4692. if ( (ssl->error = SendFinished(ssl)) != 0) {
  4693. WOLFSSL_ERROR(ssl->error);
  4694. return SSL_FATAL_ERROR;
  4695. }
  4696. WOLFSSL_MSG("sent: finished");
  4697. ssl->options.connectState = FINISHED_DONE;
  4698. WOLFSSL_MSG("connect state: FINISHED_DONE");
  4699. case FINISHED_DONE :
  4700. /* get response */
  4701. while (ssl->options.serverState < SERVER_FINISHED_COMPLETE)
  4702. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4703. WOLFSSL_ERROR(ssl->error);
  4704. return SSL_FATAL_ERROR;
  4705. }
  4706. ssl->options.connectState = SECOND_REPLY_DONE;
  4707. WOLFSSL_MSG("connect state: SECOND_REPLY_DONE");
  4708. case SECOND_REPLY_DONE:
  4709. #ifndef NO_HANDSHAKE_DONE_CB
  4710. if (ssl->hsDoneCb) {
  4711. int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
  4712. if (cbret < 0) {
  4713. ssl->error = cbret;
  4714. WOLFSSL_MSG("HandShake Done Cb don't continue error");
  4715. return SSL_FATAL_ERROR;
  4716. }
  4717. }
  4718. #endif /* NO_HANDSHAKE_DONE_CB */
  4719. FreeHandshakeResources(ssl);
  4720. WOLFSSL_LEAVE("SSL_connect()", SSL_SUCCESS);
  4721. return SSL_SUCCESS;
  4722. default:
  4723. WOLFSSL_MSG("Unknown connect state ERROR");
  4724. return SSL_FATAL_ERROR; /* unknown connect state */
  4725. }
  4726. }
  4727. #endif /* NO_WOLFSSL_CLIENT */
  4728. /* server only parts */
  4729. #ifndef NO_WOLFSSL_SERVER
  4730. #if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
  4731. WOLFSSL_METHOD* wolfSSLv3_server_method(void)
  4732. {
  4733. WOLFSSL_METHOD* method =
  4734. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  4735. 0, DYNAMIC_TYPE_METHOD);
  4736. WOLFSSL_ENTER("SSLv3_server_method");
  4737. if (method) {
  4738. InitSSL_Method(method, MakeSSLv3());
  4739. method->side = WOLFSSL_SERVER_END;
  4740. }
  4741. return method;
  4742. }
  4743. #endif
  4744. #ifdef WOLFSSL_DTLS
  4745. #ifndef NO_OLD_TLS
  4746. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  4747. {
  4748. WOLFSSL_METHOD* method =
  4749. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  4750. 0, DYNAMIC_TYPE_METHOD);
  4751. WOLFSSL_ENTER("DTLSv1_server_method");
  4752. if (method) {
  4753. InitSSL_Method(method, MakeDTLSv1());
  4754. method->side = WOLFSSL_SERVER_END;
  4755. }
  4756. return method;
  4757. }
  4758. #endif /* NO_OLD_TLS */
  4759. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  4760. {
  4761. WOLFSSL_METHOD* method =
  4762. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  4763. 0, DYNAMIC_TYPE_METHOD);
  4764. WOLFSSL_ENTER("DTLSv1_2_server_method");
  4765. if (method) {
  4766. InitSSL_Method(method, MakeDTLSv1_2());
  4767. method->side = WOLFSSL_SERVER_END;
  4768. }
  4769. return method;
  4770. }
  4771. #endif
  4772. int wolfSSL_accept(WOLFSSL* ssl)
  4773. {
  4774. byte havePSK = 0;
  4775. byte haveAnon = 0;
  4776. WOLFSSL_ENTER("SSL_accept()");
  4777. #ifdef HAVE_ERRNO_H
  4778. errno = 0;
  4779. #endif
  4780. #ifndef NO_PSK
  4781. havePSK = ssl->options.havePSK;
  4782. #endif
  4783. (void)havePSK;
  4784. #ifdef HAVE_ANON
  4785. haveAnon = ssl->options.haveAnon;
  4786. #endif
  4787. (void)haveAnon;
  4788. if (ssl->options.side != WOLFSSL_SERVER_END) {
  4789. WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
  4790. return SSL_FATAL_ERROR;
  4791. }
  4792. #ifndef NO_CERTS
  4793. /* in case used set_accept_state after init */
  4794. if (!havePSK && !haveAnon &&
  4795. (ssl->buffers.certificate.buffer == NULL ||
  4796. ssl->buffers.key.buffer == NULL)) {
  4797. WOLFSSL_MSG("accept error: don't have server cert and key");
  4798. ssl->error = NO_PRIVATE_KEY;
  4799. WOLFSSL_ERROR(ssl->error);
  4800. return SSL_FATAL_ERROR;
  4801. }
  4802. #endif
  4803. #ifdef WOLFSSL_DTLS
  4804. if (ssl->version.major == DTLS_MAJOR) {
  4805. ssl->options.dtls = 1;
  4806. ssl->options.tls = 1;
  4807. ssl->options.tls1_1 = 1;
  4808. if (DtlsPoolInit(ssl) != 0) {
  4809. ssl->error = MEMORY_ERROR;
  4810. WOLFSSL_ERROR(ssl->error);
  4811. return SSL_FATAL_ERROR;
  4812. }
  4813. }
  4814. #endif
  4815. if (ssl->buffers.outputBuffer.length > 0) {
  4816. if ( (ssl->error = SendBuffered(ssl)) == 0) {
  4817. if (ssl->fragOffset == 0) {
  4818. ssl->options.acceptState++;
  4819. WOLFSSL_MSG("accept state: Advanced from buffered send");
  4820. }
  4821. }
  4822. else {
  4823. WOLFSSL_ERROR(ssl->error);
  4824. return SSL_FATAL_ERROR;
  4825. }
  4826. }
  4827. switch (ssl->options.acceptState) {
  4828. case ACCEPT_BEGIN :
  4829. /* get response */
  4830. while (ssl->options.clientState < CLIENT_HELLO_COMPLETE)
  4831. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4832. WOLFSSL_ERROR(ssl->error);
  4833. return SSL_FATAL_ERROR;
  4834. }
  4835. ssl->options.acceptState = ACCEPT_CLIENT_HELLO_DONE;
  4836. WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE");
  4837. case ACCEPT_CLIENT_HELLO_DONE :
  4838. ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE;
  4839. WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
  4840. case ACCEPT_FIRST_REPLY_DONE :
  4841. if ( (ssl->error = SendServerHello(ssl)) != 0) {
  4842. WOLFSSL_ERROR(ssl->error);
  4843. return SSL_FATAL_ERROR;
  4844. }
  4845. ssl->options.acceptState = SERVER_HELLO_SENT;
  4846. WOLFSSL_MSG("accept state SERVER_HELLO_SENT");
  4847. case SERVER_HELLO_SENT :
  4848. #ifndef NO_CERTS
  4849. if (!ssl->options.resuming)
  4850. if ( (ssl->error = SendCertificate(ssl)) != 0) {
  4851. WOLFSSL_ERROR(ssl->error);
  4852. return SSL_FATAL_ERROR;
  4853. }
  4854. #endif
  4855. ssl->options.acceptState = CERT_SENT;
  4856. WOLFSSL_MSG("accept state CERT_SENT");
  4857. case CERT_SENT :
  4858. if (!ssl->options.resuming)
  4859. if ( (ssl->error = SendServerKeyExchange(ssl)) != 0) {
  4860. WOLFSSL_ERROR(ssl->error);
  4861. return SSL_FATAL_ERROR;
  4862. }
  4863. ssl->options.acceptState = KEY_EXCHANGE_SENT;
  4864. WOLFSSL_MSG("accept state KEY_EXCHANGE_SENT");
  4865. case KEY_EXCHANGE_SENT :
  4866. #ifndef NO_CERTS
  4867. if (!ssl->options.resuming)
  4868. if (ssl->options.verifyPeer)
  4869. if ( (ssl->error = SendCertificateRequest(ssl)) != 0) {
  4870. WOLFSSL_ERROR(ssl->error);
  4871. return SSL_FATAL_ERROR;
  4872. }
  4873. #endif
  4874. ssl->options.acceptState = CERT_REQ_SENT;
  4875. WOLFSSL_MSG("accept state CERT_REQ_SENT");
  4876. case CERT_REQ_SENT :
  4877. if (!ssl->options.resuming)
  4878. if ( (ssl->error = SendServerHelloDone(ssl)) != 0) {
  4879. WOLFSSL_ERROR(ssl->error);
  4880. return SSL_FATAL_ERROR;
  4881. }
  4882. ssl->options.acceptState = SERVER_HELLO_DONE;
  4883. WOLFSSL_MSG("accept state SERVER_HELLO_DONE");
  4884. case SERVER_HELLO_DONE :
  4885. if (!ssl->options.resuming) {
  4886. while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
  4887. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4888. WOLFSSL_ERROR(ssl->error);
  4889. return SSL_FATAL_ERROR;
  4890. }
  4891. }
  4892. ssl->options.acceptState = ACCEPT_SECOND_REPLY_DONE;
  4893. WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
  4894. case ACCEPT_SECOND_REPLY_DONE :
  4895. #ifdef HAVE_SESSION_TICKET
  4896. if (ssl->options.createTicket) {
  4897. if ( (ssl->error = SendTicket(ssl)) != 0) {
  4898. WOLFSSL_ERROR(ssl->error);
  4899. return SSL_FATAL_ERROR;
  4900. }
  4901. }
  4902. #endif /* HAVE_SESSION_TICKET */
  4903. ssl->options.acceptState = TICKET_SENT;
  4904. WOLFSSL_MSG("accept state TICKET_SENT");
  4905. case TICKET_SENT:
  4906. if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
  4907. WOLFSSL_ERROR(ssl->error);
  4908. return SSL_FATAL_ERROR;
  4909. }
  4910. ssl->options.acceptState = CHANGE_CIPHER_SENT;
  4911. WOLFSSL_MSG("accept state CHANGE_CIPHER_SENT");
  4912. case CHANGE_CIPHER_SENT :
  4913. if ( (ssl->error = SendFinished(ssl)) != 0) {
  4914. WOLFSSL_ERROR(ssl->error);
  4915. return SSL_FATAL_ERROR;
  4916. }
  4917. ssl->options.acceptState = ACCEPT_FINISHED_DONE;
  4918. WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE");
  4919. case ACCEPT_FINISHED_DONE :
  4920. if (ssl->options.resuming)
  4921. while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
  4922. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4923. WOLFSSL_ERROR(ssl->error);
  4924. return SSL_FATAL_ERROR;
  4925. }
  4926. ssl->options.acceptState = ACCEPT_THIRD_REPLY_DONE;
  4927. WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE");
  4928. case ACCEPT_THIRD_REPLY_DONE :
  4929. #ifndef NO_HANDSHAKE_DONE_CB
  4930. if (ssl->hsDoneCb) {
  4931. int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
  4932. if (cbret < 0) {
  4933. ssl->error = cbret;
  4934. WOLFSSL_MSG("HandShake Done Cb don't continue error");
  4935. return SSL_FATAL_ERROR;
  4936. }
  4937. }
  4938. #endif /* NO_HANDSHAKE_DONE_CB */
  4939. FreeHandshakeResources(ssl);
  4940. WOLFSSL_LEAVE("SSL_accept()", SSL_SUCCESS);
  4941. return SSL_SUCCESS;
  4942. default :
  4943. WOLFSSL_MSG("Unknown accept state ERROR");
  4944. return SSL_FATAL_ERROR;
  4945. }
  4946. }
  4947. #endif /* NO_WOLFSSL_SERVER */
  4948. #ifndef NO_HANDSHAKE_DONE_CB
  4949. int wolfSSL_SetHsDoneCb(WOLFSSL* ssl, HandShakeDoneCb cb, void* user_ctx)
  4950. {
  4951. WOLFSSL_ENTER("wolfSSL_SetHsDoneCb");
  4952. if (ssl == NULL)
  4953. return BAD_FUNC_ARG;
  4954. ssl->hsDoneCb = cb;
  4955. ssl->hsDoneCtx = user_ctx;
  4956. return SSL_SUCCESS;
  4957. }
  4958. #endif /* NO_HANDSHAKE_DONE_CB */
  4959. int wolfSSL_Cleanup(void)
  4960. {
  4961. int ret = SSL_SUCCESS;
  4962. int release = 0;
  4963. WOLFSSL_ENTER("wolfSSL_Cleanup");
  4964. if (initRefCount == 0)
  4965. return ret; /* possibly no init yet, but not failure either way */
  4966. if (LockMutex(&count_mutex) != 0) {
  4967. WOLFSSL_MSG("Bad Lock Mutex count");
  4968. return BAD_MUTEX_E;
  4969. }
  4970. release = initRefCount-- == 1;
  4971. if (initRefCount < 0)
  4972. initRefCount = 0;
  4973. UnLockMutex(&count_mutex);
  4974. if (!release)
  4975. return ret;
  4976. #ifndef NO_SESSION_CACHE
  4977. if (FreeMutex(&session_mutex) != 0)
  4978. ret = BAD_MUTEX_E;
  4979. #endif
  4980. if (FreeMutex(&count_mutex) != 0)
  4981. ret = BAD_MUTEX_E;
  4982. #if defined(HAVE_ECC) && defined(FP_ECC)
  4983. wc_ecc_fp_free();
  4984. #endif
  4985. return ret;
  4986. }
  4987. #ifndef NO_SESSION_CACHE
  4988. /* some session IDs aren't random afterall, let's make them random */
  4989. static INLINE word32 HashSession(const byte* sessionID, word32 len, int* error)
  4990. {
  4991. byte digest[MAX_DIGEST_SIZE];
  4992. #ifndef NO_MD5
  4993. *error = wc_Md5Hash(sessionID, len, digest);
  4994. #elif !defined(NO_SHA)
  4995. *error = wc_ShaHash(sessionID, len, digest);
  4996. #elif !defined(NO_SHA256)
  4997. *error = wc_Sha256Hash(sessionID, len, digest);
  4998. #else
  4999. #error "We need a digest to hash the session IDs"
  5000. #endif
  5001. return *error == 0 ? MakeWordFromHash(digest) : 0; /* 0 on failure */
  5002. }
  5003. void wolfSSL_flush_sessions(WOLFSSL_CTX* ctx, long tm)
  5004. {
  5005. /* static table now, no flusing needed */
  5006. (void)ctx;
  5007. (void)tm;
  5008. }
  5009. /* set ssl session timeout in seconds */
  5010. int wolfSSL_set_timeout(WOLFSSL* ssl, unsigned int to)
  5011. {
  5012. if (ssl == NULL)
  5013. return BAD_FUNC_ARG;
  5014. ssl->timeout = to;
  5015. return SSL_SUCCESS;
  5016. }
  5017. /* set ctx session timeout in seconds */
  5018. int wolfSSL_CTX_set_timeout(WOLFSSL_CTX* ctx, unsigned int to)
  5019. {
  5020. if (ctx == NULL)
  5021. return BAD_FUNC_ARG;
  5022. ctx->timeout = to;
  5023. return SSL_SUCCESS;
  5024. }
  5025. #ifndef NO_CLIENT_CACHE
  5026. /* Get Session from Client cache based on id/len, return NULL on failure */
  5027. WOLFSSL_SESSION* GetSessionClient(WOLFSSL* ssl, const byte* id, int len)
  5028. {
  5029. WOLFSSL_SESSION* ret = NULL;
  5030. word32 row;
  5031. int idx;
  5032. int count;
  5033. int error = 0;
  5034. WOLFSSL_ENTER("GetSessionClient");
  5035. if (ssl->options.side == WOLFSSL_SERVER_END)
  5036. return NULL;
  5037. len = min(SERVER_ID_LEN, (word32)len);
  5038. row = HashSession(id, len, &error) % SESSION_ROWS;
  5039. if (error != 0) {
  5040. WOLFSSL_MSG("Hash session failed");
  5041. return NULL;
  5042. }
  5043. if (LockMutex(&session_mutex) != 0) {
  5044. WOLFSSL_MSG("Lock session mutex failed");
  5045. return NULL;
  5046. }
  5047. /* start from most recently used */
  5048. count = min((word32)ClientCache[row].totalCount, SESSIONS_PER_ROW);
  5049. idx = ClientCache[row].nextIdx - 1;
  5050. if (idx < 0)
  5051. idx = SESSIONS_PER_ROW - 1; /* if back to front, the previous was end */
  5052. for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
  5053. WOLFSSL_SESSION* current;
  5054. ClientSession clSess;
  5055. if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
  5056. WOLFSSL_MSG("Bad idx");
  5057. break;
  5058. }
  5059. clSess = ClientCache[row].Clients[idx];
  5060. current = &SessionCache[clSess.serverRow].Sessions[clSess.serverIdx];
  5061. if (XMEMCMP(current->serverID, id, len) == 0) {
  5062. WOLFSSL_MSG("Found a serverid match for client");
  5063. if (LowResTimer() < (current->bornOn + current->timeout)) {
  5064. WOLFSSL_MSG("Session valid");
  5065. ret = current;
  5066. break;
  5067. } else {
  5068. WOLFSSL_MSG("Session timed out"); /* could have more for id */
  5069. }
  5070. } else {
  5071. WOLFSSL_MSG("ServerID not a match from client table");
  5072. }
  5073. }
  5074. UnLockMutex(&session_mutex);
  5075. return ret;
  5076. }
  5077. #endif /* NO_CLIENT_CACHE */
  5078. WOLFSSL_SESSION* GetSession(WOLFSSL* ssl, byte* masterSecret)
  5079. {
  5080. WOLFSSL_SESSION* ret = 0;
  5081. const byte* id = NULL;
  5082. word32 row;
  5083. int idx;
  5084. int count;
  5085. int error = 0;
  5086. if (ssl->options.sessionCacheOff)
  5087. return NULL;
  5088. if (ssl->options.haveSessionId == 0)
  5089. return NULL;
  5090. #ifdef HAVE_SESSION_TICKET
  5091. if (ssl->options.side == WOLFSSL_SERVER_END && ssl->options.useTicket == 1)
  5092. return NULL;
  5093. #endif
  5094. if (ssl->arrays)
  5095. id = ssl->arrays->sessionID;
  5096. else
  5097. id = ssl->session.sessionID;
  5098. row = HashSession(id, ID_LEN, &error) % SESSION_ROWS;
  5099. if (error != 0) {
  5100. WOLFSSL_MSG("Hash session failed");
  5101. return NULL;
  5102. }
  5103. if (LockMutex(&session_mutex) != 0)
  5104. return 0;
  5105. /* start from most recently used */
  5106. count = min((word32)SessionCache[row].totalCount, SESSIONS_PER_ROW);
  5107. idx = SessionCache[row].nextIdx - 1;
  5108. if (idx < 0)
  5109. idx = SESSIONS_PER_ROW - 1; /* if back to front, the previous was end */
  5110. for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
  5111. WOLFSSL_SESSION* current;
  5112. if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
  5113. WOLFSSL_MSG("Bad idx");
  5114. break;
  5115. }
  5116. current = &SessionCache[row].Sessions[idx];
  5117. if (XMEMCMP(current->sessionID, id, ID_LEN) == 0) {
  5118. WOLFSSL_MSG("Found a session match");
  5119. if (LowResTimer() < (current->bornOn + current->timeout)) {
  5120. WOLFSSL_MSG("Session valid");
  5121. ret = current;
  5122. if (masterSecret)
  5123. XMEMCPY(masterSecret, current->masterSecret, SECRET_LEN);
  5124. } else {
  5125. WOLFSSL_MSG("Session timed out");
  5126. }
  5127. break; /* no more sessionIDs whether valid or not that match */
  5128. } else {
  5129. WOLFSSL_MSG("SessionID not a match at this idx");
  5130. }
  5131. }
  5132. UnLockMutex(&session_mutex);
  5133. return ret;
  5134. }
  5135. int SetSession(WOLFSSL* ssl, WOLFSSL_SESSION* session)
  5136. {
  5137. if (ssl->options.sessionCacheOff)
  5138. return SSL_FAILURE;
  5139. if (LowResTimer() < (session->bornOn + session->timeout)) {
  5140. ssl->session = *session;
  5141. ssl->options.resuming = 1;
  5142. #ifdef SESSION_CERTS
  5143. ssl->version = session->version;
  5144. ssl->options.cipherSuite0 = session->cipherSuite0;
  5145. ssl->options.cipherSuite = session->cipherSuite;
  5146. #endif
  5147. return SSL_SUCCESS;
  5148. }
  5149. return SSL_FAILURE; /* session timed out */
  5150. }
  5151. #ifdef WOLFSSL_SESSION_STATS
  5152. static int get_locked_session_stats(word32* active, word32* total,
  5153. word32* peak);
  5154. #endif
  5155. int AddSession(WOLFSSL* ssl)
  5156. {
  5157. word32 row, idx;
  5158. int error = 0;
  5159. if (ssl->options.sessionCacheOff)
  5160. return 0;
  5161. if (ssl->options.haveSessionId == 0)
  5162. return 0;
  5163. #ifdef HAVE_SESSION_TICKET
  5164. if (ssl->options.side == WOLFSSL_SERVER_END && ssl->options.useTicket == 1)
  5165. return 0;
  5166. #endif
  5167. row = HashSession(ssl->arrays->sessionID, ID_LEN, &error) % SESSION_ROWS;
  5168. if (error != 0) {
  5169. WOLFSSL_MSG("Hash session failed");
  5170. return error;
  5171. }
  5172. if (LockMutex(&session_mutex) != 0)
  5173. return BAD_MUTEX_E;
  5174. idx = SessionCache[row].nextIdx++;
  5175. #ifdef SESSION_INDEX
  5176. ssl->sessionIndex = (row << SESSIDX_ROW_SHIFT) | idx;
  5177. #endif
  5178. XMEMCPY(SessionCache[row].Sessions[idx].masterSecret,
  5179. ssl->arrays->masterSecret, SECRET_LEN);
  5180. XMEMCPY(SessionCache[row].Sessions[idx].sessionID, ssl->arrays->sessionID,
  5181. ID_LEN);
  5182. SessionCache[row].Sessions[idx].sessionIDSz = ssl->arrays->sessionIDSz;
  5183. SessionCache[row].Sessions[idx].timeout = ssl->timeout;
  5184. SessionCache[row].Sessions[idx].bornOn = LowResTimer();
  5185. #ifdef HAVE_SESSION_TICKET
  5186. SessionCache[row].Sessions[idx].ticketLen = ssl->session.ticketLen;
  5187. XMEMCPY(SessionCache[row].Sessions[idx].ticket,
  5188. ssl->session.ticket, ssl->session.ticketLen);
  5189. #endif
  5190. #ifdef SESSION_CERTS
  5191. SessionCache[row].Sessions[idx].chain.count = ssl->session.chain.count;
  5192. XMEMCPY(SessionCache[row].Sessions[idx].chain.certs,
  5193. ssl->session.chain.certs, sizeof(x509_buffer) * MAX_CHAIN_DEPTH);
  5194. SessionCache[row].Sessions[idx].version = ssl->version;
  5195. SessionCache[row].Sessions[idx].cipherSuite0 = ssl->options.cipherSuite0;
  5196. SessionCache[row].Sessions[idx].cipherSuite = ssl->options.cipherSuite;
  5197. #endif /* SESSION_CERTS */
  5198. SessionCache[row].totalCount++;
  5199. if (SessionCache[row].nextIdx == SESSIONS_PER_ROW)
  5200. SessionCache[row].nextIdx = 0;
  5201. #ifndef NO_CLIENT_CACHE
  5202. if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->session.idLen) {
  5203. word32 clientRow, clientIdx;
  5204. WOLFSSL_MSG("Adding client cache entry");
  5205. SessionCache[row].Sessions[idx].idLen = ssl->session.idLen;
  5206. XMEMCPY(SessionCache[row].Sessions[idx].serverID, ssl->session.serverID,
  5207. ssl->session.idLen);
  5208. clientRow = HashSession(ssl->session.serverID, ssl->session.idLen,
  5209. &error) % SESSION_ROWS;
  5210. if (error != 0) {
  5211. WOLFSSL_MSG("Hash session failed");
  5212. } else {
  5213. clientIdx = ClientCache[clientRow].nextIdx++;
  5214. ClientCache[clientRow].Clients[clientIdx].serverRow = (word16)row;
  5215. ClientCache[clientRow].Clients[clientIdx].serverIdx = (word16)idx;
  5216. ClientCache[clientRow].totalCount++;
  5217. if (ClientCache[clientRow].nextIdx == SESSIONS_PER_ROW)
  5218. ClientCache[clientRow].nextIdx = 0;
  5219. }
  5220. }
  5221. else
  5222. SessionCache[row].Sessions[idx].idLen = 0;
  5223. #endif /* NO_CLIENT_CACHE */
  5224. #if defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS)
  5225. if (error == 0) {
  5226. word32 active = 0;
  5227. error = get_locked_session_stats(&active, NULL, NULL);
  5228. if (error == SSL_SUCCESS) {
  5229. error = 0; /* back to this function ok */
  5230. if (active > PeakSessions)
  5231. PeakSessions = active;
  5232. }
  5233. }
  5234. #endif /* defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS) */
  5235. if (UnLockMutex(&session_mutex) != 0)
  5236. return BAD_MUTEX_E;
  5237. return error;
  5238. }
  5239. #ifdef SESSION_INDEX
  5240. int wolfSSL_GetSessionIndex(WOLFSSL* ssl)
  5241. {
  5242. WOLFSSL_ENTER("wolfSSL_GetSessionIndex");
  5243. WOLFSSL_LEAVE("wolfSSL_GetSessionIndex", ssl->sessionIndex);
  5244. return ssl->sessionIndex;
  5245. }
  5246. int wolfSSL_GetSessionAtIndex(int idx, WOLFSSL_SESSION* session)
  5247. {
  5248. int row, col, result = SSL_FAILURE;
  5249. WOLFSSL_ENTER("wolfSSL_GetSessionAtIndex");
  5250. row = idx >> SESSIDX_ROW_SHIFT;
  5251. col = idx & SESSIDX_IDX_MASK;
  5252. if (LockMutex(&session_mutex) != 0) {
  5253. return BAD_MUTEX_E;
  5254. }
  5255. if (row < SESSION_ROWS &&
  5256. col < (int)min(SessionCache[row].totalCount, SESSIONS_PER_ROW)) {
  5257. XMEMCPY(session,
  5258. &SessionCache[row].Sessions[col], sizeof(WOLFSSL_SESSION));
  5259. result = SSL_SUCCESS;
  5260. }
  5261. if (UnLockMutex(&session_mutex) != 0)
  5262. result = BAD_MUTEX_E;
  5263. WOLFSSL_LEAVE("wolfSSL_GetSessionAtIndex", result);
  5264. return result;
  5265. }
  5266. #endif /* SESSION_INDEX */
  5267. #if defined(SESSION_INDEX) && defined(SESSION_CERTS)
  5268. WOLFSSL_X509_CHAIN* wolfSSL_SESSION_get_peer_chain(WOLFSSL_SESSION* session)
  5269. {
  5270. WOLFSSL_X509_CHAIN* chain = NULL;
  5271. WOLFSSL_ENTER("wolfSSL_SESSION_get_peer_chain");
  5272. if (session)
  5273. chain = &session->chain;
  5274. WOLFSSL_LEAVE("wolfSSL_SESSION_get_peer_chain", chain ? 1 : 0);
  5275. return chain;
  5276. }
  5277. #endif /* SESSION_INDEX && SESSION_CERTS */
  5278. #ifdef WOLFSSL_SESSION_STATS
  5279. /* requires session_mutex lock held, SSL_SUCCESS on ok */
  5280. static int get_locked_session_stats(word32* active, word32* total, word32* peak)
  5281. {
  5282. int result = SSL_SUCCESS;
  5283. int i;
  5284. int count;
  5285. int idx;
  5286. word32 now = 0;
  5287. word32 seen = 0;
  5288. word32 ticks = LowResTimer();
  5289. (void)peak;
  5290. WOLFSSL_ENTER("get_locked_session_stats");
  5291. for (i = 0; i < SESSION_ROWS; i++) {
  5292. seen += SessionCache[i].totalCount;
  5293. if (active == NULL)
  5294. continue; /* no need to calculate what we can't set */
  5295. count = min((word32)SessionCache[i].totalCount, SESSIONS_PER_ROW);
  5296. idx = SessionCache[i].nextIdx - 1;
  5297. if (idx < 0)
  5298. idx = SESSIONS_PER_ROW - 1; /* if back to front previous was end */
  5299. for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
  5300. if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
  5301. WOLFSSL_MSG("Bad idx");
  5302. break;
  5303. }
  5304. /* if not expried then good */
  5305. if (ticks < (SessionCache[i].Sessions[idx].bornOn +
  5306. SessionCache[i].Sessions[idx].timeout) ) {
  5307. now++;
  5308. }
  5309. }
  5310. }
  5311. if (active)
  5312. *active = now;
  5313. if (total)
  5314. *total = seen;
  5315. #ifdef WOLFSSL_PEAK_SESSIONS
  5316. if (peak)
  5317. *peak = PeakSessions;
  5318. #endif
  5319. WOLFSSL_LEAVE("get_locked_session_stats", result);
  5320. return result;
  5321. }
  5322. /* return SSL_SUCCESS on ok */
  5323. int wolfSSL_get_session_stats(word32* active, word32* total, word32* peak,
  5324. word32* maxSessions)
  5325. {
  5326. int result = SSL_SUCCESS;
  5327. WOLFSSL_ENTER("wolfSSL_get_session_stats");
  5328. if (maxSessions) {
  5329. *maxSessions = SESSIONS_PER_ROW * SESSION_ROWS;
  5330. if (active == NULL && total == NULL && peak == NULL)
  5331. return result; /* we're done */
  5332. }
  5333. /* user must provide at least one query value */
  5334. if (active == NULL && total == NULL && peak == NULL)
  5335. return BAD_FUNC_ARG;
  5336. if (LockMutex(&session_mutex) != 0) {
  5337. return BAD_MUTEX_E;
  5338. }
  5339. result = get_locked_session_stats(active, total, peak);
  5340. if (UnLockMutex(&session_mutex) != 0)
  5341. result = BAD_MUTEX_E;
  5342. WOLFSSL_LEAVE("wolfSSL_get_session_stats", result);
  5343. return result;
  5344. }
  5345. #endif /* WOLFSSL_SESSION_STATS */
  5346. #ifdef PRINT_SESSION_STATS
  5347. /* SSL_SUCCESS on ok */
  5348. int wolfSSL_PrintSessionStats(void)
  5349. {
  5350. word32 totalSessionsSeen = 0;
  5351. word32 totalSessionsNow = 0;
  5352. word32 peak = 0;
  5353. word32 maxSessions = 0;
  5354. int i;
  5355. int ret;
  5356. double E; /* expected freq */
  5357. double chiSquare = 0;
  5358. ret = wolfSSL_get_session_stats(&totalSessionsNow, &totalSessionsSeen,
  5359. &peak, &maxSessions);
  5360. if (ret != SSL_SUCCESS)
  5361. return ret;
  5362. printf("Total Sessions Seen = %d\n", totalSessionsSeen);
  5363. printf("Total Sessions Now = %d\n", totalSessionsNow);
  5364. #ifdef WOLFSSL_PEAK_SESSIONS
  5365. printf("Peak Sessions = %d\n", peak);
  5366. #endif
  5367. printf("Max Sessions = %d\n", maxSessions);
  5368. E = (double)totalSessionsSeen / SESSION_ROWS;
  5369. for (i = 0; i < SESSION_ROWS; i++) {
  5370. double diff = SessionCache[i].totalCount - E;
  5371. diff *= diff; /* square */
  5372. diff /= E; /* normalize */
  5373. chiSquare += diff;
  5374. }
  5375. printf(" chi-square = %5.1f, d.f. = %d\n", chiSquare,
  5376. SESSION_ROWS - 1);
  5377. #if (SESSION_ROWS == 11)
  5378. printf(" .05 p value = 18.3, chi-square should be less\n");
  5379. #elif (SESSION_ROWS == 211)
  5380. printf(".05 p value = 244.8, chi-square should be less\n");
  5381. #elif (SESSION_ROWS == 5981)
  5382. printf(".05 p value = 6161.0, chi-square should be less\n");
  5383. #elif (SESSION_ROWS == 3)
  5384. printf(".05 p value = 6.0, chi-square should be less\n");
  5385. #elif (SESSION_ROWS == 2861)
  5386. printf(".05 p value = 2985.5, chi-square should be less\n");
  5387. #endif
  5388. printf("\n");
  5389. return ret;
  5390. }
  5391. #endif /* SESSION_STATS */
  5392. #else /* NO_SESSION_CACHE */
  5393. /* No session cache version */
  5394. WOLFSSL_SESSION* GetSession(WOLFSSL* ssl, byte* masterSecret)
  5395. {
  5396. (void)ssl;
  5397. (void)masterSecret;
  5398. return NULL;
  5399. }
  5400. #endif /* NO_SESSION_CACHE */
  5401. /* call before SSL_connect, if verifying will add name check to
  5402. date check and signature check */
  5403. int wolfSSL_check_domain_name(WOLFSSL* ssl, const char* dn)
  5404. {
  5405. WOLFSSL_ENTER("wolfSSL_check_domain_name");
  5406. if (ssl->buffers.domainName.buffer)
  5407. XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  5408. ssl->buffers.domainName.length = (word32)XSTRLEN(dn) + 1;
  5409. ssl->buffers.domainName.buffer = (byte*) XMALLOC(
  5410. ssl->buffers.domainName.length, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  5411. if (ssl->buffers.domainName.buffer) {
  5412. XSTRNCPY((char*)ssl->buffers.domainName.buffer, dn,
  5413. ssl->buffers.domainName.length);
  5414. return SSL_SUCCESS;
  5415. }
  5416. else {
  5417. ssl->error = MEMORY_ERROR;
  5418. return SSL_FAILURE;
  5419. }
  5420. }
  5421. /* turn on wolfSSL zlib compression
  5422. returns SSL_SUCCESS for success, else error (not built in)
  5423. */
  5424. int wolfSSL_set_compression(WOLFSSL* ssl)
  5425. {
  5426. WOLFSSL_ENTER("wolfSSL_set_compression");
  5427. (void)ssl;
  5428. #ifdef HAVE_LIBZ
  5429. ssl->options.usingCompression = 1;
  5430. return SSL_SUCCESS;
  5431. #else
  5432. return NOT_COMPILED_IN;
  5433. #endif
  5434. }
  5435. #ifndef USE_WINDOWS_API
  5436. #ifndef NO_WRITEV
  5437. /* simulate writev semantics, doesn't actually do block at a time though
  5438. because of SSL_write behavior and because front adds may be small */
  5439. int wolfSSL_writev(WOLFSSL* ssl, const struct iovec* iov, int iovcnt)
  5440. {
  5441. #ifdef WOLFSSL_SMALL_STACK
  5442. byte staticBuffer[1]; /* force heap usage */
  5443. #else
  5444. byte staticBuffer[FILE_BUFFER_SIZE];
  5445. #endif
  5446. byte* myBuffer = staticBuffer;
  5447. int dynamic = 0;
  5448. int sending = 0;
  5449. int idx = 0;
  5450. int i;
  5451. int ret;
  5452. WOLFSSL_ENTER("wolfSSL_writev");
  5453. for (i = 0; i < iovcnt; i++)
  5454. sending += (int)iov[i].iov_len;
  5455. if (sending > (int)sizeof(staticBuffer)) {
  5456. myBuffer = (byte*)XMALLOC(sending, ssl->heap,
  5457. DYNAMIC_TYPE_WRITEV);
  5458. if (!myBuffer)
  5459. return MEMORY_ERROR;
  5460. dynamic = 1;
  5461. }
  5462. for (i = 0; i < iovcnt; i++) {
  5463. XMEMCPY(&myBuffer[idx], iov[i].iov_base, iov[i].iov_len);
  5464. idx += (int)iov[i].iov_len;
  5465. }
  5466. ret = wolfSSL_write(ssl, myBuffer, sending);
  5467. if (dynamic)
  5468. XFREE(myBuffer, ssl->heap, DYNAMIC_TYPE_WRITEV);
  5469. return ret;
  5470. }
  5471. #endif
  5472. #endif
  5473. #ifdef WOLFSSL_CALLBACKS
  5474. typedef struct itimerval Itimerval;
  5475. /* don't keep calling simple functions while setting up timer and singals
  5476. if no inlining these are the next best */
  5477. #define AddTimes(a, b, c) \
  5478. do { \
  5479. c.tv_sec = a.tv_sec + b.tv_sec; \
  5480. c.tv_usec = a.tv_usec + b.tv_usec; \
  5481. if (c.tv_usec >= 1000000) { \
  5482. c.tv_sec++; \
  5483. c.tv_usec -= 1000000; \
  5484. } \
  5485. } while (0)
  5486. #define SubtractTimes(a, b, c) \
  5487. do { \
  5488. c.tv_sec = a.tv_sec - b.tv_sec; \
  5489. c.tv_usec = a.tv_usec - b.tv_usec; \
  5490. if (c.tv_usec < 0) { \
  5491. c.tv_sec--; \
  5492. c.tv_usec += 1000000; \
  5493. } \
  5494. } while (0)
  5495. #define CmpTimes(a, b, cmp) \
  5496. ((a.tv_sec == b.tv_sec) ? \
  5497. (a.tv_usec cmp b.tv_usec) : \
  5498. (a.tv_sec cmp b.tv_sec)) \
  5499. /* do nothing handler */
  5500. static void myHandler(int signo)
  5501. {
  5502. (void)signo;
  5503. return;
  5504. }
  5505. static int wolfSSL_ex_wrapper(WOLFSSL* ssl, HandShakeCallBack hsCb,
  5506. TimeoutCallBack toCb, Timeval timeout)
  5507. {
  5508. int ret = SSL_FATAL_ERROR;
  5509. int oldTimerOn = 0; /* was timer already on */
  5510. Timeval startTime;
  5511. Timeval endTime;
  5512. Timeval totalTime;
  5513. Itimerval myTimeout;
  5514. Itimerval oldTimeout; /* if old timer adjust from total time to reset */
  5515. struct sigaction act, oact;
  5516. #define ERR_OUT(x) { ssl->hsInfoOn = 0; ssl->toInfoOn = 0; return x; }
  5517. if (hsCb) {
  5518. ssl->hsInfoOn = 1;
  5519. InitHandShakeInfo(&ssl->handShakeInfo);
  5520. }
  5521. if (toCb) {
  5522. ssl->toInfoOn = 1;
  5523. InitTimeoutInfo(&ssl->timeoutInfo);
  5524. if (gettimeofday(&startTime, 0) < 0)
  5525. ERR_OUT(GETTIME_ERROR);
  5526. /* use setitimer to simulate getitimer, init 0 myTimeout */
  5527. myTimeout.it_interval.tv_sec = 0;
  5528. myTimeout.it_interval.tv_usec = 0;
  5529. myTimeout.it_value.tv_sec = 0;
  5530. myTimeout.it_value.tv_usec = 0;
  5531. if (setitimer(ITIMER_REAL, &myTimeout, &oldTimeout) < 0)
  5532. ERR_OUT(SETITIMER_ERROR);
  5533. if (oldTimeout.it_value.tv_sec || oldTimeout.it_value.tv_usec) {
  5534. oldTimerOn = 1;
  5535. /* is old timer going to expire before ours */
  5536. if (CmpTimes(oldTimeout.it_value, timeout, <)) {
  5537. timeout.tv_sec = oldTimeout.it_value.tv_sec;
  5538. timeout.tv_usec = oldTimeout.it_value.tv_usec;
  5539. }
  5540. }
  5541. myTimeout.it_value.tv_sec = timeout.tv_sec;
  5542. myTimeout.it_value.tv_usec = timeout.tv_usec;
  5543. /* set up signal handler, don't restart socket send/recv */
  5544. act.sa_handler = myHandler;
  5545. sigemptyset(&act.sa_mask);
  5546. act.sa_flags = 0;
  5547. #ifdef SA_INTERRUPT
  5548. act.sa_flags |= SA_INTERRUPT;
  5549. #endif
  5550. if (sigaction(SIGALRM, &act, &oact) < 0)
  5551. ERR_OUT(SIGACT_ERROR);
  5552. if (setitimer(ITIMER_REAL, &myTimeout, 0) < 0)
  5553. ERR_OUT(SETITIMER_ERROR);
  5554. }
  5555. /* do main work */
  5556. #ifndef NO_WOLFSSL_CLIENT
  5557. if (ssl->options.side == WOLFSSL_CLIENT_END)
  5558. ret = wolfSSL_connect(ssl);
  5559. #endif
  5560. #ifndef NO_WOLFSSL_SERVER
  5561. if (ssl->options.side == WOLFSSL_SERVER_END)
  5562. ret = wolfSSL_accept(ssl);
  5563. #endif
  5564. /* do callbacks */
  5565. if (toCb) {
  5566. if (oldTimerOn) {
  5567. gettimeofday(&endTime, 0);
  5568. SubtractTimes(endTime, startTime, totalTime);
  5569. /* adjust old timer for elapsed time */
  5570. if (CmpTimes(totalTime, oldTimeout.it_value, <))
  5571. SubtractTimes(oldTimeout.it_value, totalTime,
  5572. oldTimeout.it_value);
  5573. else {
  5574. /* reset value to interval, may be off */
  5575. oldTimeout.it_value.tv_sec = oldTimeout.it_interval.tv_sec;
  5576. oldTimeout.it_value.tv_usec =oldTimeout.it_interval.tv_usec;
  5577. }
  5578. /* keep iter the same whether there or not */
  5579. }
  5580. /* restore old handler */
  5581. if (sigaction(SIGALRM, &oact, 0) < 0)
  5582. ret = SIGACT_ERROR; /* more pressing error, stomp */
  5583. else
  5584. /* use old settings which may turn off (expired or not there) */
  5585. if (setitimer(ITIMER_REAL, &oldTimeout, 0) < 0)
  5586. ret = SETITIMER_ERROR;
  5587. /* if we had a timeout call callback */
  5588. if (ssl->timeoutInfo.timeoutName[0]) {
  5589. ssl->timeoutInfo.timeoutValue.tv_sec = timeout.tv_sec;
  5590. ssl->timeoutInfo.timeoutValue.tv_usec = timeout.tv_usec;
  5591. (toCb)(&ssl->timeoutInfo);
  5592. }
  5593. /* clean up */
  5594. FreeTimeoutInfo(&ssl->timeoutInfo, ssl->heap);
  5595. ssl->toInfoOn = 0;
  5596. }
  5597. if (hsCb) {
  5598. FinishHandShakeInfo(&ssl->handShakeInfo, ssl);
  5599. (hsCb)(&ssl->handShakeInfo);
  5600. ssl->hsInfoOn = 0;
  5601. }
  5602. return ret;
  5603. }
  5604. #ifndef NO_WOLFSSL_CLIENT
  5605. int wolfSSL_connect_ex(WOLFSSL* ssl, HandShakeCallBack hsCb,
  5606. TimeoutCallBack toCb, Timeval timeout)
  5607. {
  5608. WOLFSSL_ENTER("wolfSSL_connect_ex");
  5609. return wolfSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
  5610. }
  5611. #endif
  5612. #ifndef NO_WOLFSSL_SERVER
  5613. int wolfSSL_accept_ex(WOLFSSL* ssl, HandShakeCallBack hsCb,
  5614. TimeoutCallBack toCb,Timeval timeout)
  5615. {
  5616. WOLFSSL_ENTER("wolfSSL_accept_ex");
  5617. return wolfSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
  5618. }
  5619. #endif
  5620. #endif /* WOLFSSL_CALLBACKS */
  5621. #ifndef NO_PSK
  5622. void wolfSSL_CTX_set_psk_client_callback(WOLFSSL_CTX* ctx,
  5623. wc_psk_client_callback cb)
  5624. {
  5625. WOLFSSL_ENTER("SSL_CTX_set_psk_client_callback");
  5626. ctx->havePSK = 1;
  5627. ctx->client_psk_cb = cb;
  5628. }
  5629. void wolfSSL_set_psk_client_callback(WOLFSSL* ssl,wc_psk_client_callback cb)
  5630. {
  5631. byte haveRSA = 1;
  5632. WOLFSSL_ENTER("SSL_set_psk_client_callback");
  5633. ssl->options.havePSK = 1;
  5634. ssl->options.client_psk_cb = cb;
  5635. #ifdef NO_RSA
  5636. haveRSA = 0;
  5637. #endif
  5638. InitSuites(ssl->suites, ssl->version, haveRSA, TRUE,
  5639. ssl->options.haveDH, ssl->options.haveNTRU,
  5640. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  5641. ssl->options.side);
  5642. }
  5643. void wolfSSL_CTX_set_psk_server_callback(WOLFSSL_CTX* ctx,
  5644. wc_psk_server_callback cb)
  5645. {
  5646. WOLFSSL_ENTER("SSL_CTX_set_psk_server_callback");
  5647. ctx->havePSK = 1;
  5648. ctx->server_psk_cb = cb;
  5649. }
  5650. void wolfSSL_set_psk_server_callback(WOLFSSL* ssl,wc_psk_server_callback cb)
  5651. {
  5652. byte haveRSA = 1;
  5653. WOLFSSL_ENTER("SSL_set_psk_server_callback");
  5654. ssl->options.havePSK = 1;
  5655. ssl->options.server_psk_cb = cb;
  5656. #ifdef NO_RSA
  5657. haveRSA = 0;
  5658. #endif
  5659. InitSuites(ssl->suites, ssl->version, haveRSA, TRUE,
  5660. ssl->options.haveDH, ssl->options.haveNTRU,
  5661. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  5662. ssl->options.side);
  5663. }
  5664. const char* wolfSSL_get_psk_identity_hint(const WOLFSSL* ssl)
  5665. {
  5666. WOLFSSL_ENTER("SSL_get_psk_identity_hint");
  5667. if (ssl == NULL || ssl->arrays == NULL)
  5668. return NULL;
  5669. return ssl->arrays->server_hint;
  5670. }
  5671. const char* wolfSSL_get_psk_identity(const WOLFSSL* ssl)
  5672. {
  5673. WOLFSSL_ENTER("SSL_get_psk_identity");
  5674. if (ssl == NULL || ssl->arrays == NULL)
  5675. return NULL;
  5676. return ssl->arrays->client_identity;
  5677. }
  5678. int wolfSSL_CTX_use_psk_identity_hint(WOLFSSL_CTX* ctx, const char* hint)
  5679. {
  5680. WOLFSSL_ENTER("SSL_CTX_use_psk_identity_hint");
  5681. if (hint == 0)
  5682. ctx->server_hint[0] = 0;
  5683. else {
  5684. XSTRNCPY(ctx->server_hint, hint, MAX_PSK_ID_LEN);
  5685. ctx->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
  5686. }
  5687. return SSL_SUCCESS;
  5688. }
  5689. int wolfSSL_use_psk_identity_hint(WOLFSSL* ssl, const char* hint)
  5690. {
  5691. WOLFSSL_ENTER("SSL_use_psk_identity_hint");
  5692. if (ssl == NULL || ssl->arrays == NULL)
  5693. return SSL_FAILURE;
  5694. if (hint == 0)
  5695. ssl->arrays->server_hint[0] = 0;
  5696. else {
  5697. XSTRNCPY(ssl->arrays->server_hint, hint, MAX_PSK_ID_LEN);
  5698. ssl->arrays->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
  5699. }
  5700. return SSL_SUCCESS;
  5701. }
  5702. #endif /* NO_PSK */
  5703. #ifdef HAVE_ANON
  5704. int wolfSSL_CTX_allow_anon_cipher(WOLFSSL_CTX* ctx)
  5705. {
  5706. WOLFSSL_ENTER("wolfSSL_CTX_allow_anon_cipher");
  5707. if (ctx == NULL)
  5708. return SSL_FAILURE;
  5709. ctx->haveAnon = 1;
  5710. return SSL_SUCCESS;
  5711. }
  5712. #endif /* HAVE_ANON */
  5713. #ifndef NO_CERTS
  5714. /* used to be defined on NO_FILESYSTEM only, but are generally useful */
  5715. /* wolfSSL extension allows DER files to be loaded from buffers as well */
  5716. int wolfSSL_CTX_load_verify_buffer(WOLFSSL_CTX* ctx,
  5717. const unsigned char* in,
  5718. long sz, int format)
  5719. {
  5720. WOLFSSL_ENTER("wolfSSL_CTX_load_verify_buffer");
  5721. if (format == SSL_FILETYPE_PEM)
  5722. return ProcessChainBuffer(ctx, in, sz, format, CA_TYPE, NULL);
  5723. else
  5724. return ProcessBuffer(ctx, in, sz, format, CA_TYPE, NULL,NULL,0);
  5725. }
  5726. int wolfSSL_CTX_use_certificate_buffer(WOLFSSL_CTX* ctx,
  5727. const unsigned char* in, long sz, int format)
  5728. {
  5729. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_buffer");
  5730. return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 0);
  5731. }
  5732. int wolfSSL_CTX_use_PrivateKey_buffer(WOLFSSL_CTX* ctx,
  5733. const unsigned char* in, long sz, int format)
  5734. {
  5735. WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_buffer");
  5736. return ProcessBuffer(ctx, in, sz, format, PRIVATEKEY_TYPE, NULL,NULL,0);
  5737. }
  5738. int wolfSSL_CTX_use_certificate_chain_buffer(WOLFSSL_CTX* ctx,
  5739. const unsigned char* in, long sz)
  5740. {
  5741. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_buffer");
  5742. return ProcessBuffer(ctx, in, sz, SSL_FILETYPE_PEM, CERT_TYPE, NULL,
  5743. NULL, 1);
  5744. }
  5745. int wolfSSL_use_certificate_buffer(WOLFSSL* ssl,
  5746. const unsigned char* in, long sz, int format)
  5747. {
  5748. WOLFSSL_ENTER("wolfSSL_use_certificate_buffer");
  5749. return ProcessBuffer(ssl->ctx, in, sz, format,CERT_TYPE,ssl,NULL,0);
  5750. }
  5751. int wolfSSL_use_PrivateKey_buffer(WOLFSSL* ssl,
  5752. const unsigned char* in, long sz, int format)
  5753. {
  5754. WOLFSSL_ENTER("wolfSSL_use_PrivateKey_buffer");
  5755. return ProcessBuffer(ssl->ctx, in, sz, format, PRIVATEKEY_TYPE,
  5756. ssl, NULL, 0);
  5757. }
  5758. int wolfSSL_use_certificate_chain_buffer(WOLFSSL* ssl,
  5759. const unsigned char* in, long sz)
  5760. {
  5761. WOLFSSL_ENTER("wolfSSL_use_certificate_chain_buffer");
  5762. return ProcessBuffer(ssl->ctx, in, sz, SSL_FILETYPE_PEM, CERT_TYPE,
  5763. ssl, NULL, 1);
  5764. }
  5765. /* unload any certs or keys that SSL owns, leave CTX as is
  5766. SSL_SUCCESS on ok */
  5767. int wolfSSL_UnloadCertsKeys(WOLFSSL* ssl)
  5768. {
  5769. if (ssl == NULL) {
  5770. WOLFSSL_MSG("Null function arg");
  5771. return BAD_FUNC_ARG;
  5772. }
  5773. if (ssl->buffers.weOwnCert) {
  5774. WOLFSSL_MSG("Unloading cert");
  5775. XFREE(ssl->buffers.certificate.buffer, ssl->heap,DYNAMIC_TYPE_CERT);
  5776. ssl->buffers.weOwnCert = 0;
  5777. ssl->buffers.certificate.length = 0;
  5778. ssl->buffers.certificate.buffer = NULL;
  5779. }
  5780. if (ssl->buffers.weOwnCertChain) {
  5781. WOLFSSL_MSG("Unloading cert chain");
  5782. XFREE(ssl->buffers.certChain.buffer, ssl->heap,DYNAMIC_TYPE_CERT);
  5783. ssl->buffers.weOwnCertChain = 0;
  5784. ssl->buffers.certChain.length = 0;
  5785. ssl->buffers.certChain.buffer = NULL;
  5786. }
  5787. if (ssl->buffers.weOwnKey) {
  5788. WOLFSSL_MSG("Unloading key");
  5789. XFREE(ssl->buffers.key.buffer, ssl->heap, DYNAMIC_TYPE_KEY);
  5790. ssl->buffers.weOwnKey = 0;
  5791. ssl->buffers.key.length = 0;
  5792. ssl->buffers.key.buffer = NULL;
  5793. }
  5794. return SSL_SUCCESS;
  5795. }
  5796. int wolfSSL_CTX_UnloadCAs(WOLFSSL_CTX* ctx)
  5797. {
  5798. WOLFSSL_ENTER("wolfSSL_CTX_UnloadCAs");
  5799. if (ctx == NULL)
  5800. return BAD_FUNC_ARG;
  5801. return wolfSSL_CertManagerUnloadCAs(ctx->cm);
  5802. }
  5803. /* old NO_FILESYSTEM end */
  5804. #endif /* !NO_CERTS */
  5805. #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
  5806. int wolfSSL_add_all_algorithms(void)
  5807. {
  5808. WOLFSSL_ENTER("wolfSSL_add_all_algorithms");
  5809. wolfSSL_Init();
  5810. return SSL_SUCCESS;
  5811. }
  5812. long wolfSSL_CTX_sess_set_cache_size(WOLFSSL_CTX* ctx, long sz)
  5813. {
  5814. /* cache size fixed at compile time in wolfSSL */
  5815. (void)ctx;
  5816. (void)sz;
  5817. return 0;
  5818. }
  5819. void wolfSSL_CTX_set_quiet_shutdown(WOLFSSL_CTX* ctx, int mode)
  5820. {
  5821. WOLFSSL_ENTER("wolfSSL_CTX_set_quiet_shutdown");
  5822. if (mode)
  5823. ctx->quietShutdown = 1;
  5824. }
  5825. void wolfSSL_set_quiet_shutdown(WOLFSSL* ssl, int mode)
  5826. {
  5827. WOLFSSL_ENTER("wolfSSL_CTX_set_quiet_shutdown");
  5828. if (mode)
  5829. ssl->options.quietShutdown = 1;
  5830. }
  5831. void wolfSSL_set_bio(WOLFSSL* ssl, WOLFSSL_BIO* rd, WOLFSSL_BIO* wr)
  5832. {
  5833. WOLFSSL_ENTER("SSL_set_bio");
  5834. wolfSSL_set_rfd(ssl, rd->fd);
  5835. wolfSSL_set_wfd(ssl, wr->fd);
  5836. ssl->biord = rd;
  5837. ssl->biowr = wr;
  5838. }
  5839. void wolfSSL_CTX_set_client_CA_list(WOLFSSL_CTX* ctx,
  5840. STACK_OF(WOLFSSL_X509_NAME)* names)
  5841. {
  5842. (void)ctx;
  5843. (void)names;
  5844. }
  5845. STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_load_client_CA_file(const char* fname)
  5846. {
  5847. (void)fname;
  5848. return 0;
  5849. }
  5850. int wolfSSL_CTX_set_default_verify_paths(WOLFSSL_CTX* ctx)
  5851. {
  5852. /* TODO:, not needed in goahead */
  5853. (void)ctx;
  5854. return SSL_NOT_IMPLEMENTED;
  5855. }
  5856. /* keyblock size in bytes or -1 */
  5857. int wolfSSL_get_keyblock_size(WOLFSSL* ssl)
  5858. {
  5859. if (ssl == NULL)
  5860. return SSL_FATAL_ERROR;
  5861. return 2 * (ssl->specs.key_size + ssl->specs.iv_size +
  5862. ssl->specs.hash_size);
  5863. }
  5864. /* store keys returns SSL_SUCCESS or -1 on error */
  5865. int wolfSSL_get_keys(WOLFSSL* ssl, unsigned char** ms, unsigned int* msLen,
  5866. unsigned char** sr, unsigned int* srLen,
  5867. unsigned char** cr, unsigned int* crLen)
  5868. {
  5869. if (ssl == NULL || ssl->arrays == NULL)
  5870. return SSL_FATAL_ERROR;
  5871. *ms = ssl->arrays->masterSecret;
  5872. *sr = ssl->arrays->serverRandom;
  5873. *cr = ssl->arrays->clientRandom;
  5874. *msLen = SECRET_LEN;
  5875. *srLen = RAN_LEN;
  5876. *crLen = RAN_LEN;
  5877. return SSL_SUCCESS;
  5878. }
  5879. void wolfSSL_set_accept_state(WOLFSSL* ssl)
  5880. {
  5881. byte haveRSA = 1;
  5882. byte havePSK = 0;
  5883. WOLFSSL_ENTER("SSL_set_accept_state");
  5884. ssl->options.side = WOLFSSL_SERVER_END;
  5885. /* reset suites in case user switched */
  5886. #ifdef NO_RSA
  5887. haveRSA = 0;
  5888. #endif
  5889. #ifndef NO_PSK
  5890. havePSK = ssl->options.havePSK;
  5891. #endif
  5892. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK,
  5893. ssl->options.haveDH, ssl->options.haveNTRU,
  5894. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  5895. ssl->options.side);
  5896. }
  5897. #endif
  5898. /* return true if connection established */
  5899. int wolfSSL_is_init_finished(WOLFSSL* ssl)
  5900. {
  5901. if (ssl == NULL)
  5902. return 0;
  5903. if (ssl->options.handShakeState == HANDSHAKE_DONE)
  5904. return 1;
  5905. return 0;
  5906. }
  5907. #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
  5908. void wolfSSL_CTX_set_tmp_rsa_callback(WOLFSSL_CTX* ctx,
  5909. WOLFSSL_RSA*(*f)(WOLFSSL*, int, int))
  5910. {
  5911. /* wolfSSL verifies all these internally */
  5912. (void)ctx;
  5913. (void)f;
  5914. }
  5915. void wolfSSL_set_shutdown(WOLFSSL* ssl, int opt)
  5916. {
  5917. WOLFSSL_ENTER("wolfSSL_set_shutdown");
  5918. if(ssl==NULL) {
  5919. WOLFSSL_MSG("Shutdown not set. ssl is null");
  5920. return;
  5921. }
  5922. ssl->options.sentNotify = (opt&SSL_SENT_SHUTDOWN) > 0;
  5923. ssl->options.closeNotify = (opt&SSL_RECEIVED_SHUTDOWN) > 0;
  5924. }
  5925. long wolfSSL_CTX_set_options(WOLFSSL_CTX* ctx, long opt)
  5926. {
  5927. /* goahead calls with 0, do nothing */
  5928. WOLFSSL_ENTER("SSL_CTX_set_options");
  5929. (void)ctx;
  5930. return opt;
  5931. }
  5932. int wolfSSL_set_rfd(WOLFSSL* ssl, int rfd)
  5933. {
  5934. WOLFSSL_ENTER("SSL_set_rfd");
  5935. ssl->rfd = rfd; /* not used directly to allow IO callbacks */
  5936. ssl->IOCB_ReadCtx = &ssl->rfd;
  5937. return SSL_SUCCESS;
  5938. }
  5939. int wolfSSL_set_wfd(WOLFSSL* ssl, int wfd)
  5940. {
  5941. WOLFSSL_ENTER("SSL_set_wfd");
  5942. ssl->wfd = wfd; /* not used directly to allow IO callbacks */
  5943. ssl->IOCB_WriteCtx = &ssl->wfd;
  5944. return SSL_SUCCESS;
  5945. }
  5946. WOLFSSL_RSA* wolfSSL_RSA_generate_key(int len, unsigned long bits,
  5947. void(*f)(int, int, void*), void* data)
  5948. {
  5949. /* no tmp key needed, actual generation not supported */
  5950. WOLFSSL_ENTER("RSA_generate_key");
  5951. (void)len;
  5952. (void)bits;
  5953. (void)f;
  5954. (void)data;
  5955. return NULL;
  5956. }
  5957. WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get_current_cert(
  5958. WOLFSSL_X509_STORE_CTX* ctx)
  5959. {
  5960. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_current_cert");
  5961. if(ctx)
  5962. return ctx->current_cert;
  5963. return NULL;
  5964. }
  5965. int wolfSSL_X509_STORE_CTX_get_error(WOLFSSL_X509_STORE_CTX* ctx)
  5966. {
  5967. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error");
  5968. if (ctx != NULL)
  5969. return ctx->error;
  5970. return 0;
  5971. }
  5972. int wolfSSL_X509_STORE_CTX_get_error_depth(WOLFSSL_X509_STORE_CTX* ctx)
  5973. {
  5974. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error_depth");
  5975. if(ctx)
  5976. return ctx->error_depth;
  5977. return SSL_FATAL_ERROR;
  5978. }
  5979. WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_buffer(void)
  5980. {
  5981. static WOLFSSL_BIO_METHOD meth;
  5982. WOLFSSL_ENTER("BIO_f_buffer");
  5983. meth.type = BIO_BUFFER;
  5984. return &meth;
  5985. }
  5986. long wolfSSL_BIO_set_write_buffer_size(WOLFSSL_BIO* bio, long size)
  5987. {
  5988. /* wolfSSL has internal buffer, compatibility only */
  5989. WOLFSSL_ENTER("BIO_set_write_buffer_size");
  5990. (void)bio;
  5991. return size;
  5992. }
  5993. WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_ssl(void)
  5994. {
  5995. static WOLFSSL_BIO_METHOD meth;
  5996. WOLFSSL_ENTER("BIO_f_ssl");
  5997. meth.type = BIO_SSL;
  5998. return &meth;
  5999. }
  6000. WOLFSSL_BIO* wolfSSL_BIO_new_socket(int sfd, int closeF)
  6001. {
  6002. WOLFSSL_BIO* bio = (WOLFSSL_BIO*) XMALLOC(sizeof(WOLFSSL_BIO), 0,
  6003. DYNAMIC_TYPE_OPENSSL);
  6004. WOLFSSL_ENTER("BIO_new_socket");
  6005. if (bio) {
  6006. bio->type = BIO_SOCKET;
  6007. bio->close = (byte)closeF;
  6008. bio->eof = 0;
  6009. bio->ssl = 0;
  6010. bio->fd = sfd;
  6011. bio->prev = 0;
  6012. bio->next = 0;
  6013. bio->mem = NULL;
  6014. bio->memLen = 0;
  6015. }
  6016. return bio;
  6017. }
  6018. int wolfSSL_BIO_eof(WOLFSSL_BIO* b)
  6019. {
  6020. WOLFSSL_ENTER("BIO_eof");
  6021. if (b->eof)
  6022. return 1;
  6023. return 0;
  6024. }
  6025. long wolfSSL_BIO_set_ssl(WOLFSSL_BIO* b, WOLFSSL* ssl, int closeF)
  6026. {
  6027. WOLFSSL_ENTER("BIO_set_ssl");
  6028. b->ssl = ssl;
  6029. b->close = (byte)closeF;
  6030. /* add to ssl for bio free if SSL_free called before/instead of free_all? */
  6031. return 0;
  6032. }
  6033. WOLFSSL_BIO* wolfSSL_BIO_new(WOLFSSL_BIO_METHOD* method)
  6034. {
  6035. WOLFSSL_BIO* bio = (WOLFSSL_BIO*) XMALLOC(sizeof(WOLFSSL_BIO), 0,
  6036. DYNAMIC_TYPE_OPENSSL);
  6037. WOLFSSL_ENTER("BIO_new");
  6038. if (bio) {
  6039. bio->type = method->type;
  6040. bio->close = 0;
  6041. bio->eof = 0;
  6042. bio->ssl = NULL;
  6043. bio->mem = NULL;
  6044. bio->memLen = 0;
  6045. bio->fd = 0;
  6046. bio->prev = NULL;
  6047. bio->next = NULL;
  6048. }
  6049. return bio;
  6050. }
  6051. int wolfSSL_BIO_get_mem_data(WOLFSSL_BIO* bio, const byte** p)
  6052. {
  6053. if (bio == NULL || p == NULL)
  6054. return SSL_FATAL_ERROR;
  6055. *p = bio->mem;
  6056. return bio->memLen;
  6057. }
  6058. WOLFSSL_BIO* wolfSSL_BIO_new_mem_buf(void* buf, int len)
  6059. {
  6060. WOLFSSL_BIO* bio = NULL;
  6061. if (buf == NULL)
  6062. return bio;
  6063. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  6064. if (bio == NULL)
  6065. return bio;
  6066. bio->memLen = len;
  6067. bio->mem = (byte*)XMALLOC(len, 0, DYNAMIC_TYPE_OPENSSL);
  6068. if (bio->mem == NULL) {
  6069. XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
  6070. return NULL;
  6071. }
  6072. XMEMCPY(bio->mem, buf, len);
  6073. return bio;
  6074. }
  6075. #ifdef USE_WINDOWS_API
  6076. #define CloseSocket(s) closesocket(s)
  6077. #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  6078. #define CloseSocket(s) closesocket(s)
  6079. extern int closesocket(int) ;
  6080. #else
  6081. #define CloseSocket(s) close(s)
  6082. #endif
  6083. int wolfSSL_BIO_free(WOLFSSL_BIO* bio)
  6084. {
  6085. /* unchain?, doesn't matter in goahead since from free all */
  6086. WOLFSSL_ENTER("BIO_free");
  6087. if (bio) {
  6088. if (bio->close) {
  6089. if (bio->ssl)
  6090. wolfSSL_free(bio->ssl);
  6091. if (bio->fd)
  6092. CloseSocket(bio->fd);
  6093. }
  6094. if (bio->mem)
  6095. XFREE(bio->mem, 0, DYNAMIC_TYPE_OPENSSL);
  6096. XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
  6097. }
  6098. return 0;
  6099. }
  6100. int wolfSSL_BIO_free_all(WOLFSSL_BIO* bio)
  6101. {
  6102. WOLFSSL_ENTER("BIO_free_all");
  6103. while (bio) {
  6104. WOLFSSL_BIO* next = bio->next;
  6105. wolfSSL_BIO_free(bio);
  6106. bio = next;
  6107. }
  6108. return 0;
  6109. }
  6110. int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
  6111. {
  6112. int ret;
  6113. WOLFSSL* ssl = 0;
  6114. WOLFSSL_BIO* front = bio;
  6115. WOLFSSL_ENTER("BIO_read");
  6116. /* already got eof, again is error */
  6117. if (front->eof)
  6118. return SSL_FATAL_ERROR;
  6119. while(bio && ((ssl = bio->ssl) == 0) )
  6120. bio = bio->next;
  6121. if (ssl == 0) return BAD_FUNC_ARG;
  6122. ret = wolfSSL_read(ssl, buf, len);
  6123. if (ret == 0)
  6124. front->eof = 1;
  6125. else if (ret < 0) {
  6126. int err = wolfSSL_get_error(ssl, 0);
  6127. if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) )
  6128. front->eof = 1;
  6129. }
  6130. return ret;
  6131. }
  6132. int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
  6133. {
  6134. int ret;
  6135. WOLFSSL* ssl = 0;
  6136. WOLFSSL_BIO* front = bio;
  6137. WOLFSSL_ENTER("BIO_write");
  6138. /* already got eof, again is error */
  6139. if (front->eof)
  6140. return SSL_FATAL_ERROR;
  6141. while(bio && ((ssl = bio->ssl) == 0) )
  6142. bio = bio->next;
  6143. if (ssl == 0) return BAD_FUNC_ARG;
  6144. ret = wolfSSL_write(ssl, data, len);
  6145. if (ret == 0)
  6146. front->eof = 1;
  6147. else if (ret < 0) {
  6148. int err = wolfSSL_get_error(ssl, 0);
  6149. if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) )
  6150. front->eof = 1;
  6151. }
  6152. return ret;
  6153. }
  6154. WOLFSSL_BIO* wolfSSL_BIO_push(WOLFSSL_BIO* top, WOLFSSL_BIO* append)
  6155. {
  6156. WOLFSSL_ENTER("BIO_push");
  6157. top->next = append;
  6158. append->prev = top;
  6159. return top;
  6160. }
  6161. int wolfSSL_BIO_flush(WOLFSSL_BIO* bio)
  6162. {
  6163. /* for wolfSSL no flushing needed */
  6164. WOLFSSL_ENTER("BIO_flush");
  6165. (void)bio;
  6166. return 1;
  6167. }
  6168. #endif /* OPENSSL_EXTRA || GOAHEAD_WS */
  6169. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  6170. void wolfSSL_CTX_set_default_passwd_cb_userdata(WOLFSSL_CTX* ctx,
  6171. void* userdata)
  6172. {
  6173. WOLFSSL_ENTER("SSL_CTX_set_default_passwd_cb_userdata");
  6174. ctx->userdata = userdata;
  6175. }
  6176. void wolfSSL_CTX_set_default_passwd_cb(WOLFSSL_CTX* ctx, pem_password_cb cb)
  6177. {
  6178. WOLFSSL_ENTER("SSL_CTX_set_default_passwd_cb");
  6179. ctx->passwd_cb = cb;
  6180. }
  6181. int wolfSSL_num_locks(void)
  6182. {
  6183. return 0;
  6184. }
  6185. void wolfSSL_set_locking_callback(void (*f)(int, int, const char*, int))
  6186. {
  6187. (void)f;
  6188. }
  6189. void wolfSSL_set_id_callback(unsigned long (*f)(void))
  6190. {
  6191. (void)f;
  6192. }
  6193. unsigned long wolfSSL_ERR_get_error(void)
  6194. {
  6195. /* TODO: */
  6196. return 0;
  6197. }
  6198. #ifndef NO_MD5
  6199. int wolfSSL_EVP_BytesToKey(const WOLFSSL_EVP_CIPHER* type,
  6200. const WOLFSSL_EVP_MD* md, const byte* salt,
  6201. const byte* data, int sz, int count, byte* key, byte* iv)
  6202. {
  6203. int keyLen = 0;
  6204. int ivLen = 0;
  6205. int j;
  6206. int keyLeft;
  6207. int ivLeft;
  6208. int keyOutput = 0;
  6209. byte digest[MD5_DIGEST_SIZE];
  6210. #ifdef WOLFSSL_SMALL_STACK
  6211. Md5* md5 = NULL;
  6212. #else
  6213. Md5 md5[1];
  6214. #endif
  6215. #ifdef WOLFSSL_SMALL_STACK
  6216. md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6217. if (md5 == NULL)
  6218. return 0;
  6219. #endif
  6220. WOLFSSL_ENTER("wolfSSL_EVP_BytesToKey");
  6221. wc_InitMd5(md5);
  6222. /* only support MD5 for now */
  6223. if (XSTRNCMP(md, "MD5", 3) != 0) return 0;
  6224. /* only support CBC DES and AES for now */
  6225. if (XSTRNCMP(type, EVP_DES_CBC, EVP_DES_SIZE) == 0) {
  6226. keyLen = DES_KEY_SIZE;
  6227. ivLen = DES_IV_SIZE;
  6228. }
  6229. else if (XSTRNCMP(type, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0) {
  6230. keyLen = DES3_KEY_SIZE;
  6231. ivLen = DES_IV_SIZE;
  6232. }
  6233. else if (XSTRNCMP(type, EVP_AES_128_CBC, EVP_AES_SIZE) == 0) {
  6234. keyLen = AES_128_KEY_SIZE;
  6235. ivLen = AES_IV_SIZE;
  6236. }
  6237. else if (XSTRNCMP(type, EVP_AES_192_CBC, EVP_AES_SIZE) == 0) {
  6238. keyLen = AES_192_KEY_SIZE;
  6239. ivLen = AES_IV_SIZE;
  6240. }
  6241. else if (XSTRNCMP(type, EVP_AES_256_CBC, EVP_AES_SIZE) == 0) {
  6242. keyLen = AES_256_KEY_SIZE;
  6243. ivLen = AES_IV_SIZE;
  6244. }
  6245. else {
  6246. #ifdef WOLFSSL_SMALL_STACK
  6247. XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6248. #endif
  6249. return 0;
  6250. }
  6251. keyLeft = keyLen;
  6252. ivLeft = ivLen;
  6253. while (keyOutput < (keyLen + ivLen)) {
  6254. int digestLeft = MD5_DIGEST_SIZE;
  6255. /* D_(i - 1) */
  6256. if (keyOutput) /* first time D_0 is empty */
  6257. wc_Md5Update(md5, digest, MD5_DIGEST_SIZE);
  6258. /* data */
  6259. wc_Md5Update(md5, data, sz);
  6260. /* salt */
  6261. if (salt)
  6262. wc_Md5Update(md5, salt, EVP_SALT_SIZE);
  6263. wc_Md5Final(md5, digest);
  6264. /* count */
  6265. for (j = 1; j < count; j++) {
  6266. wc_Md5Update(md5, digest, MD5_DIGEST_SIZE);
  6267. wc_Md5Final(md5, digest);
  6268. }
  6269. if (keyLeft) {
  6270. int store = min(keyLeft, MD5_DIGEST_SIZE);
  6271. XMEMCPY(&key[keyLen - keyLeft], digest, store);
  6272. keyOutput += store;
  6273. keyLeft -= store;
  6274. digestLeft -= store;
  6275. }
  6276. if (ivLeft && digestLeft) {
  6277. int store = min(ivLeft, digestLeft);
  6278. if (iv != NULL)
  6279. XMEMCPY(&iv[ivLen - ivLeft],
  6280. &digest[MD5_DIGEST_SIZE - digestLeft], store);
  6281. keyOutput += store;
  6282. ivLeft -= store;
  6283. }
  6284. }
  6285. #ifdef WOLFSSL_SMALL_STACK
  6286. XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6287. #endif
  6288. return keyOutput == (keyLen + ivLen) ? keyOutput : 0;
  6289. }
  6290. #endif /* NO_MD5 */
  6291. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  6292. #ifdef OPENSSL_EXTRA
  6293. unsigned long wolfSSLeay(void)
  6294. {
  6295. return SSLEAY_VERSION_NUMBER;
  6296. }
  6297. const char* wolfSSLeay_version(int type)
  6298. {
  6299. static const char* version = "SSLeay wolfSSL compatibility";
  6300. (void)type;
  6301. return version;
  6302. }
  6303. #ifndef NO_MD5
  6304. void wolfSSL_MD5_Init(WOLFSSL_MD5_CTX* md5)
  6305. {
  6306. typedef char md5_test[sizeof(MD5_CTX) >= sizeof(Md5) ? 1 : -1];
  6307. (void)sizeof(md5_test);
  6308. WOLFSSL_ENTER("MD5_Init");
  6309. wc_InitMd5((Md5*)md5);
  6310. }
  6311. void wolfSSL_MD5_Update(WOLFSSL_MD5_CTX* md5, const void* input,
  6312. unsigned long sz)
  6313. {
  6314. WOLFSSL_ENTER("wolfSSL_MD5_Update");
  6315. wc_Md5Update((Md5*)md5, (const byte*)input, (word32)sz);
  6316. }
  6317. void wolfSSL_MD5_Final(byte* input, WOLFSSL_MD5_CTX* md5)
  6318. {
  6319. WOLFSSL_ENTER("MD5_Final");
  6320. wc_Md5Final((Md5*)md5, input);
  6321. }
  6322. #endif /* NO_MD5 */
  6323. #ifndef NO_SHA
  6324. void wolfSSL_SHA_Init(WOLFSSL_SHA_CTX* sha)
  6325. {
  6326. typedef char sha_test[sizeof(SHA_CTX) >= sizeof(Sha) ? 1 : -1];
  6327. (void)sizeof(sha_test);
  6328. WOLFSSL_ENTER("SHA_Init");
  6329. wc_InitSha((Sha*)sha); /* OpenSSL compat, no ret */
  6330. }
  6331. void wolfSSL_SHA_Update(WOLFSSL_SHA_CTX* sha, const void* input,
  6332. unsigned long sz)
  6333. {
  6334. WOLFSSL_ENTER("SHA_Update");
  6335. wc_ShaUpdate((Sha*)sha, (const byte*)input, (word32)sz);
  6336. }
  6337. void wolfSSL_SHA_Final(byte* input, WOLFSSL_SHA_CTX* sha)
  6338. {
  6339. WOLFSSL_ENTER("SHA_Final");
  6340. wc_ShaFinal((Sha*)sha, input);
  6341. }
  6342. void wolfSSL_SHA1_Init(WOLFSSL_SHA_CTX* sha)
  6343. {
  6344. WOLFSSL_ENTER("SHA1_Init");
  6345. SHA_Init(sha);
  6346. }
  6347. void wolfSSL_SHA1_Update(WOLFSSL_SHA_CTX* sha, const void* input,
  6348. unsigned long sz)
  6349. {
  6350. WOLFSSL_ENTER("SHA1_Update");
  6351. SHA_Update(sha, input, sz);
  6352. }
  6353. void wolfSSL_SHA1_Final(byte* input, WOLFSSL_SHA_CTX* sha)
  6354. {
  6355. WOLFSSL_ENTER("SHA1_Final");
  6356. SHA_Final(input, sha);
  6357. }
  6358. #endif /* NO_SHA */
  6359. void wolfSSL_SHA256_Init(WOLFSSL_SHA256_CTX* sha256)
  6360. {
  6361. typedef char sha_test[sizeof(SHA256_CTX) >= sizeof(Sha256) ? 1 : -1];
  6362. (void)sizeof(sha_test);
  6363. WOLFSSL_ENTER("SHA256_Init");
  6364. wc_InitSha256((Sha256*)sha256); /* OpenSSL compat, no error */
  6365. }
  6366. void wolfSSL_SHA256_Update(WOLFSSL_SHA256_CTX* sha, const void* input,
  6367. unsigned long sz)
  6368. {
  6369. WOLFSSL_ENTER("SHA256_Update");
  6370. wc_Sha256Update((Sha256*)sha, (const byte*)input, (word32)sz);
  6371. /* OpenSSL compat, no error */
  6372. }
  6373. void wolfSSL_SHA256_Final(byte* input, WOLFSSL_SHA256_CTX* sha)
  6374. {
  6375. WOLFSSL_ENTER("SHA256_Final");
  6376. wc_Sha256Final((Sha256*)sha, input);
  6377. /* OpenSSL compat, no error */
  6378. }
  6379. #ifdef WOLFSSL_SHA384
  6380. void wolfSSL_SHA384_Init(WOLFSSL_SHA384_CTX* sha)
  6381. {
  6382. typedef char sha_test[sizeof(SHA384_CTX) >= sizeof(Sha384) ? 1 : -1];
  6383. (void)sizeof(sha_test);
  6384. WOLFSSL_ENTER("SHA384_Init");
  6385. wc_InitSha384((Sha384*)sha); /* OpenSSL compat, no error */
  6386. }
  6387. void wolfSSL_SHA384_Update(WOLFSSL_SHA384_CTX* sha, const void* input,
  6388. unsigned long sz)
  6389. {
  6390. WOLFSSL_ENTER("SHA384_Update");
  6391. wc_Sha384Update((Sha384*)sha, (const byte*)input, (word32)sz);
  6392. /* OpenSSL compat, no error */
  6393. }
  6394. void wolfSSL_SHA384_Final(byte* input, WOLFSSL_SHA384_CTX* sha)
  6395. {
  6396. WOLFSSL_ENTER("SHA384_Final");
  6397. wc_Sha384Final((Sha384*)sha, input);
  6398. /* OpenSSL compat, no error */
  6399. }
  6400. #endif /* WOLFSSL_SHA384 */
  6401. #ifdef WOLFSSL_SHA512
  6402. void wolfSSL_SHA512_Init(WOLFSSL_SHA512_CTX* sha)
  6403. {
  6404. typedef char sha_test[sizeof(SHA512_CTX) >= sizeof(Sha512) ? 1 : -1];
  6405. (void)sizeof(sha_test);
  6406. WOLFSSL_ENTER("SHA512_Init");
  6407. wc_InitSha512((Sha512*)sha); /* OpenSSL compat, no error */
  6408. }
  6409. void wolfSSL_SHA512_Update(WOLFSSL_SHA512_CTX* sha, const void* input,
  6410. unsigned long sz)
  6411. {
  6412. WOLFSSL_ENTER("SHA512_Update");
  6413. wc_Sha512Update((Sha512*)sha, (const byte*)input, (word32)sz);
  6414. /* OpenSSL compat, no error */
  6415. }
  6416. void wolfSSL_SHA512_Final(byte* input, WOLFSSL_SHA512_CTX* sha)
  6417. {
  6418. WOLFSSL_ENTER("SHA512_Final");
  6419. wc_Sha512Final((Sha512*)sha, input);
  6420. /* OpenSSL compat, no error */
  6421. }
  6422. #endif /* WOLFSSL_SHA512 */
  6423. #ifndef NO_MD5
  6424. const WOLFSSL_EVP_MD* wolfSSL_EVP_md5(void)
  6425. {
  6426. static const char* type = "MD5";
  6427. WOLFSSL_ENTER("EVP_md5");
  6428. return type;
  6429. }
  6430. #endif /* NO_MD5 */
  6431. #ifndef NO_SHA
  6432. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha1(void)
  6433. {
  6434. static const char* type = "SHA";
  6435. WOLFSSL_ENTER("EVP_sha1");
  6436. return type;
  6437. }
  6438. #endif /* NO_SHA */
  6439. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha256(void)
  6440. {
  6441. static const char* type = "SHA256";
  6442. WOLFSSL_ENTER("EVP_sha256");
  6443. return type;
  6444. }
  6445. #ifdef WOLFSSL_SHA384
  6446. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha384(void)
  6447. {
  6448. static const char* type = "SHA384";
  6449. WOLFSSL_ENTER("EVP_sha384");
  6450. return type;
  6451. }
  6452. #endif /* WOLFSSL_SHA384 */
  6453. #ifdef WOLFSSL_SHA512
  6454. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha512(void)
  6455. {
  6456. static const char* type = "SHA512";
  6457. WOLFSSL_ENTER("EVP_sha512");
  6458. return type;
  6459. }
  6460. #endif /* WOLFSSL_SHA512 */
  6461. void wolfSSL_EVP_MD_CTX_init(WOLFSSL_EVP_MD_CTX* ctx)
  6462. {
  6463. WOLFSSL_ENTER("EVP_CIPHER_MD_CTX_init");
  6464. (void)ctx;
  6465. /* do nothing */
  6466. }
  6467. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cbc(void)
  6468. {
  6469. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cbc");
  6470. return EVP_AES_128_CBC;
  6471. }
  6472. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cbc(void)
  6473. {
  6474. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cbc");
  6475. return EVP_AES_192_CBC;
  6476. }
  6477. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cbc(void)
  6478. {
  6479. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cbc");
  6480. return EVP_AES_256_CBC;
  6481. }
  6482. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ctr(void)
  6483. {
  6484. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ctr");
  6485. return EVP_AES_128_CTR;
  6486. }
  6487. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ctr(void)
  6488. {
  6489. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ctr");
  6490. return EVP_AES_192_CTR;
  6491. }
  6492. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ctr(void)
  6493. {
  6494. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ctr");
  6495. return EVP_AES_256_CTR;
  6496. }
  6497. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_cbc(void)
  6498. {
  6499. WOLFSSL_ENTER("wolfSSL_EVP_des_cbc");
  6500. return EVP_DES_CBC;
  6501. }
  6502. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_cbc(void)
  6503. {
  6504. WOLFSSL_ENTER("wolfSSL_EVP_des_ede3_cbc");
  6505. return EVP_DES_EDE3_CBC;
  6506. }
  6507. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_rc4(void)
  6508. {
  6509. static const char* type = "ARC4";
  6510. WOLFSSL_ENTER("wolfSSL_EVP_rc4");
  6511. return type;
  6512. }
  6513. #ifdef HAVE_IDEA
  6514. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_idea_cbc(void)
  6515. {
  6516. WOLFSSL_ENTER("wolfSSL_EVP_idea_cbc");
  6517. return EVP_IDEA_CBC;
  6518. }
  6519. #endif
  6520. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_enc_null(void)
  6521. {
  6522. static const char* type = "NULL";
  6523. WOLFSSL_ENTER("wolfSSL_EVP_enc_null");
  6524. return type;
  6525. }
  6526. int wolfSSL_EVP_MD_CTX_cleanup(WOLFSSL_EVP_MD_CTX* ctx)
  6527. {
  6528. WOLFSSL_ENTER("EVP_MD_CTX_cleanup");
  6529. (void)ctx;
  6530. return 0;
  6531. }
  6532. void wolfSSL_EVP_CIPHER_CTX_init(WOLFSSL_EVP_CIPHER_CTX* ctx)
  6533. {
  6534. WOLFSSL_ENTER("EVP_CIPHER_CTX_init");
  6535. if (ctx) {
  6536. ctx->cipherType = 0xff; /* no init */
  6537. ctx->keyLen = 0;
  6538. ctx->enc = 1; /* start in encrypt mode */
  6539. }
  6540. }
  6541. /* SSL_SUCCESS on ok */
  6542. int wolfSSL_EVP_CIPHER_CTX_cleanup(WOLFSSL_EVP_CIPHER_CTX* ctx)
  6543. {
  6544. WOLFSSL_ENTER("EVP_CIPHER_CTX_cleanup");
  6545. if (ctx) {
  6546. ctx->cipherType = 0xff; /* no more init */
  6547. ctx->keyLen = 0;
  6548. }
  6549. return SSL_SUCCESS;
  6550. }
  6551. /* SSL_SUCCESS on ok */
  6552. int wolfSSL_EVP_CipherInit(WOLFSSL_EVP_CIPHER_CTX* ctx,
  6553. const WOLFSSL_EVP_CIPHER* type, byte* key,
  6554. byte* iv, int enc)
  6555. {
  6556. #if defined(NO_AES) && defined(NO_DES3) && !defined(HAVE_IDEA)
  6557. (void)iv;
  6558. (void)enc;
  6559. #else
  6560. int ret = 0;
  6561. #endif
  6562. WOLFSSL_ENTER("wolfSSL_EVP_CipherInit");
  6563. if (ctx == NULL) {
  6564. WOLFSSL_MSG("no ctx");
  6565. return 0; /* failure */
  6566. }
  6567. if (type == NULL && ctx->cipherType == 0xff) {
  6568. WOLFSSL_MSG("no type set");
  6569. return 0; /* failure */
  6570. }
  6571. #ifndef NO_AES
  6572. if (ctx->cipherType == AES_128_CBC_TYPE ||
  6573. (type && XSTRNCMP(type, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)) {
  6574. WOLFSSL_MSG(EVP_AES_128_CBC);
  6575. ctx->cipherType = AES_128_CBC_TYPE;
  6576. ctx->keyLen = 16;
  6577. if (enc == 0 || enc == 1)
  6578. ctx->enc = enc ? 1 : 0;
  6579. if (key) {
  6580. ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  6581. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  6582. if (ret != 0)
  6583. return ret;
  6584. }
  6585. if (iv && key == NULL) {
  6586. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  6587. if (ret != 0)
  6588. return ret;
  6589. }
  6590. }
  6591. else if (ctx->cipherType == AES_192_CBC_TYPE ||
  6592. (type && XSTRNCMP(type, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)) {
  6593. WOLFSSL_MSG(EVP_AES_192_CBC);
  6594. ctx->cipherType = AES_192_CBC_TYPE;
  6595. ctx->keyLen = 24;
  6596. if (enc == 0 || enc == 1)
  6597. ctx->enc = enc ? 1 : 0;
  6598. if (key) {
  6599. ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  6600. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  6601. if (ret != 0)
  6602. return ret;
  6603. }
  6604. if (iv && key == NULL) {
  6605. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  6606. if (ret != 0)
  6607. return ret;
  6608. }
  6609. }
  6610. else if (ctx->cipherType == AES_256_CBC_TYPE ||
  6611. (type && XSTRNCMP(type, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)) {
  6612. WOLFSSL_MSG(EVP_AES_256_CBC);
  6613. ctx->cipherType = AES_256_CBC_TYPE;
  6614. ctx->keyLen = 32;
  6615. if (enc == 0 || enc == 1)
  6616. ctx->enc = enc ? 1 : 0;
  6617. if (key) {
  6618. ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  6619. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  6620. if (ret != 0)
  6621. return ret;
  6622. }
  6623. if (iv && key == NULL) {
  6624. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  6625. if (ret != 0)
  6626. return ret;
  6627. }
  6628. }
  6629. #ifdef WOLFSSL_AES_COUNTER
  6630. else if (ctx->cipherType == AES_128_CTR_TYPE ||
  6631. (type && XSTRNCMP(type, EVP_AES_128_CTR, EVP_AES_SIZE) == 0)) {
  6632. WOLFSSL_MSG(EVP_AES_128_CTR);
  6633. ctx->cipherType = AES_128_CTR_TYPE;
  6634. ctx->keyLen = 16;
  6635. if (enc == 0 || enc == 1)
  6636. ctx->enc = enc ? 1 : 0;
  6637. if (key) {
  6638. ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  6639. AES_ENCRYPTION);
  6640. if (ret != 0)
  6641. return ret;
  6642. }
  6643. if (iv && key == NULL) {
  6644. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  6645. if (ret != 0)
  6646. return ret;
  6647. }
  6648. }
  6649. else if (ctx->cipherType == AES_192_CTR_TYPE ||
  6650. (type && XSTRNCMP(type, EVP_AES_192_CTR, EVP_AES_SIZE) == 0)) {
  6651. WOLFSSL_MSG(EVP_AES_192_CTR);
  6652. ctx->cipherType = AES_192_CTR_TYPE;
  6653. ctx->keyLen = 24;
  6654. if (enc == 0 || enc == 1)
  6655. ctx->enc = enc ? 1 : 0;
  6656. if (key) {
  6657. ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  6658. AES_ENCRYPTION);
  6659. if (ret != 0)
  6660. return ret;
  6661. }
  6662. if (iv && key == NULL) {
  6663. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  6664. if (ret != 0)
  6665. return ret;
  6666. }
  6667. }
  6668. else if (ctx->cipherType == AES_256_CTR_TYPE ||
  6669. (type && XSTRNCMP(type, EVP_AES_256_CTR, EVP_AES_SIZE) == 0)) {
  6670. WOLFSSL_MSG(EVP_AES_256_CTR);
  6671. ctx->cipherType = AES_256_CTR_TYPE;
  6672. ctx->keyLen = 32;
  6673. if (enc == 0 || enc == 1)
  6674. ctx->enc = enc ? 1 : 0;
  6675. if (key) {
  6676. ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  6677. AES_ENCRYPTION);
  6678. if (ret != 0)
  6679. return ret;
  6680. }
  6681. if (iv && key == NULL) {
  6682. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  6683. if (ret != 0)
  6684. return ret;
  6685. }
  6686. }
  6687. #endif /* WOLFSSL_AES_CTR */
  6688. #endif /* NO_AES */
  6689. #ifndef NO_DES3
  6690. else if (ctx->cipherType == DES_CBC_TYPE ||
  6691. (type && XSTRNCMP(type, EVP_DES_CBC, EVP_DES_SIZE) == 0)) {
  6692. WOLFSSL_MSG(EVP_DES_CBC);
  6693. ctx->cipherType = DES_CBC_TYPE;
  6694. ctx->keyLen = 8;
  6695. if (enc == 0 || enc == 1)
  6696. ctx->enc = enc ? 1 : 0;
  6697. if (key) {
  6698. ret = wc_Des_SetKey(&ctx->cipher.des, key, iv,
  6699. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  6700. if (ret != 0)
  6701. return ret;
  6702. }
  6703. if (iv && key == NULL)
  6704. wc_Des_SetIV(&ctx->cipher.des, iv);
  6705. }
  6706. else if (ctx->cipherType == DES_EDE3_CBC_TYPE ||
  6707. (type &&
  6708. XSTRNCMP(type, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)) {
  6709. WOLFSSL_MSG(EVP_DES_EDE3_CBC);
  6710. ctx->cipherType = DES_EDE3_CBC_TYPE;
  6711. ctx->keyLen = 24;
  6712. if (enc == 0 || enc == 1)
  6713. ctx->enc = enc ? 1 : 0;
  6714. if (key) {
  6715. ret = wc_Des3_SetKey(&ctx->cipher.des3, key, iv,
  6716. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  6717. if (ret != 0)
  6718. return ret;
  6719. }
  6720. if (iv && key == NULL) {
  6721. ret = wc_Des3_SetIV(&ctx->cipher.des3, iv);
  6722. if (ret != 0)
  6723. return ret;
  6724. }
  6725. }
  6726. #endif /* NO_DES3 */
  6727. #ifndef NO_RC4
  6728. else if (ctx->cipherType == ARC4_TYPE || (type &&
  6729. XSTRNCMP(type, "ARC4", 4) == 0)) {
  6730. WOLFSSL_MSG("ARC4");
  6731. ctx->cipherType = ARC4_TYPE;
  6732. if (ctx->keyLen == 0) /* user may have already set */
  6733. ctx->keyLen = 16; /* default to 128 */
  6734. if (key)
  6735. wc_Arc4SetKey(&ctx->cipher.arc4, key, ctx->keyLen);
  6736. }
  6737. #endif /* NO_RC4 */
  6738. #ifdef HAVE_IDEA
  6739. else if (ctx->cipherType == IDEA_CBC_TYPE ||
  6740. (type && XSTRNCMP(type, EVP_IDEA_CBC, EVP_IDEA_SIZE) == 0)) {
  6741. WOLFSSL_MSG(EVP_IDEA_CBC);
  6742. ctx->cipherType = IDEA_CBC_TYPE;
  6743. ctx->keyLen = IDEA_KEY_SIZE;
  6744. if (enc == 0 || enc == 1)
  6745. ctx->enc = enc ? 1 : 0;
  6746. if (key) {
  6747. ret = wc_IdeaSetKey(&ctx->cipher.idea, key, ctx->keyLen, iv,
  6748. ctx->enc ? IDEA_ENCRYPTION : IDEA_DECRYPTION);
  6749. if (ret != 0)
  6750. return ret;
  6751. }
  6752. if (iv && key == NULL)
  6753. wc_IdeaSetIV(&ctx->cipher.idea, iv);
  6754. }
  6755. #endif /* HAVE_IDEA */
  6756. else if (ctx->cipherType == NULL_CIPHER_TYPE || (type &&
  6757. XSTRNCMP(type, "NULL", 4) == 0)) {
  6758. WOLFSSL_MSG("NULL cipher");
  6759. ctx->cipherType = NULL_CIPHER_TYPE;
  6760. ctx->keyLen = 0;
  6761. }
  6762. else
  6763. return 0; /* failure */
  6764. return SSL_SUCCESS;
  6765. }
  6766. /* SSL_SUCCESS on ok */
  6767. int wolfSSL_EVP_CIPHER_CTX_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx)
  6768. {
  6769. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_key_length");
  6770. if (ctx)
  6771. return ctx->keyLen;
  6772. return 0; /* failure */
  6773. }
  6774. /* SSL_SUCCESS on ok */
  6775. int wolfSSL_EVP_CIPHER_CTX_set_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx,
  6776. int keylen)
  6777. {
  6778. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_set_key_length");
  6779. if (ctx)
  6780. ctx->keyLen = keylen;
  6781. else
  6782. return 0; /* failure */
  6783. return SSL_SUCCESS;
  6784. }
  6785. /* SSL_SUCCESS on ok */
  6786. int wolfSSL_EVP_Cipher(WOLFSSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src,
  6787. word32 len)
  6788. {
  6789. int ret = 0;
  6790. WOLFSSL_ENTER("wolfSSL_EVP_Cipher");
  6791. if (ctx == NULL || dst == NULL || src == NULL) {
  6792. WOLFSSL_MSG("Bad function argument");
  6793. return 0; /* failure */
  6794. }
  6795. if (ctx->cipherType == 0xff) {
  6796. WOLFSSL_MSG("no init");
  6797. return 0; /* failure */
  6798. }
  6799. switch (ctx->cipherType) {
  6800. #ifndef NO_AES
  6801. case AES_128_CBC_TYPE :
  6802. case AES_192_CBC_TYPE :
  6803. case AES_256_CBC_TYPE :
  6804. WOLFSSL_MSG("AES CBC");
  6805. if (ctx->enc)
  6806. ret = wc_AesCbcEncrypt(&ctx->cipher.aes, dst, src, len);
  6807. else
  6808. ret = wc_AesCbcDecrypt(&ctx->cipher.aes, dst, src, len);
  6809. break;
  6810. #ifdef WOLFSSL_AES_COUNTER
  6811. case AES_128_CTR_TYPE :
  6812. case AES_192_CTR_TYPE :
  6813. case AES_256_CTR_TYPE :
  6814. WOLFSSL_MSG("AES CTR");
  6815. wc_AesCtrEncrypt(&ctx->cipher.aes, dst, src, len);
  6816. break;
  6817. #endif
  6818. #endif /* NO_AES */
  6819. #ifndef NO_DES3
  6820. case DES_CBC_TYPE :
  6821. if (ctx->enc)
  6822. wc_Des_CbcEncrypt(&ctx->cipher.des, dst, src, len);
  6823. else
  6824. wc_Des_CbcDecrypt(&ctx->cipher.des, dst, src, len);
  6825. break;
  6826. case DES_EDE3_CBC_TYPE :
  6827. if (ctx->enc)
  6828. ret = wc_Des3_CbcEncrypt(&ctx->cipher.des3, dst, src, len);
  6829. else
  6830. ret = wc_Des3_CbcDecrypt(&ctx->cipher.des3, dst, src, len);
  6831. break;
  6832. #endif
  6833. #ifndef NO_RC4
  6834. case ARC4_TYPE :
  6835. wc_Arc4Process(&ctx->cipher.arc4, dst, src, len);
  6836. break;
  6837. #endif
  6838. #ifdef HAVE_IDEA
  6839. case IDEA_CBC_TYPE :
  6840. if (ctx->enc)
  6841. wc_IdeaCbcEncrypt(&ctx->cipher.idea, dst, src, len);
  6842. else
  6843. wc_IdeaCbcDecrypt(&ctx->cipher.idea, dst, src, len);
  6844. break;
  6845. #endif
  6846. case NULL_CIPHER_TYPE :
  6847. XMEMCPY(dst, src, len);
  6848. break;
  6849. default: {
  6850. WOLFSSL_MSG("bad type");
  6851. return 0; /* failure */
  6852. }
  6853. }
  6854. if (ret != 0) {
  6855. WOLFSSL_MSG("wolfSSL_EVP_Cipher failure");
  6856. return 0; /* failuer */
  6857. }
  6858. WOLFSSL_MSG("wolfSSL_EVP_Cipher success");
  6859. return SSL_SUCCESS; /* success */
  6860. }
  6861. /* store for external read of iv, SSL_SUCCESS on success */
  6862. int wolfSSL_StoreExternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx)
  6863. {
  6864. WOLFSSL_ENTER("wolfSSL_StoreExternalIV");
  6865. if (ctx == NULL) {
  6866. WOLFSSL_MSG("Bad function argument");
  6867. return SSL_FATAL_ERROR;
  6868. }
  6869. switch (ctx->cipherType) {
  6870. #ifndef NO_AES
  6871. case AES_128_CBC_TYPE :
  6872. case AES_192_CBC_TYPE :
  6873. case AES_256_CBC_TYPE :
  6874. WOLFSSL_MSG("AES CBC");
  6875. XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  6876. break;
  6877. #ifdef WOLFSSL_AES_COUNTER
  6878. case AES_128_CTR_TYPE :
  6879. case AES_192_CTR_TYPE :
  6880. case AES_256_CTR_TYPE :
  6881. WOLFSSL_MSG("AES CTR");
  6882. XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  6883. break;
  6884. #endif /* WOLFSSL_AES_COUNTER */
  6885. #endif /* NO_AES */
  6886. #ifndef NO_DES3
  6887. case DES_CBC_TYPE :
  6888. WOLFSSL_MSG("DES CBC");
  6889. XMEMCPY(ctx->iv, &ctx->cipher.des.reg, DES_BLOCK_SIZE);
  6890. break;
  6891. case DES_EDE3_CBC_TYPE :
  6892. WOLFSSL_MSG("DES EDE3 CBC");
  6893. XMEMCPY(ctx->iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
  6894. break;
  6895. #endif
  6896. #ifdef HAVE_IDEA
  6897. case IDEA_CBC_TYPE :
  6898. WOLFSSL_MSG("IDEA CBC");
  6899. XMEMCPY(ctx->iv, &ctx->cipher.idea.reg, IDEA_BLOCK_SIZE);
  6900. break;
  6901. #endif
  6902. case ARC4_TYPE :
  6903. WOLFSSL_MSG("ARC4");
  6904. break;
  6905. case NULL_CIPHER_TYPE :
  6906. WOLFSSL_MSG("NULL");
  6907. break;
  6908. default: {
  6909. WOLFSSL_MSG("bad type");
  6910. return SSL_FATAL_ERROR;
  6911. }
  6912. }
  6913. return SSL_SUCCESS;
  6914. }
  6915. /* set internal IV from external, SSL_SUCCESS on success */
  6916. int wolfSSL_SetInternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx)
  6917. {
  6918. WOLFSSL_ENTER("wolfSSL_SetInternalIV");
  6919. if (ctx == NULL) {
  6920. WOLFSSL_MSG("Bad function argument");
  6921. return SSL_FATAL_ERROR;
  6922. }
  6923. switch (ctx->cipherType) {
  6924. #ifndef NO_AES
  6925. case AES_128_CBC_TYPE :
  6926. case AES_192_CBC_TYPE :
  6927. case AES_256_CBC_TYPE :
  6928. WOLFSSL_MSG("AES CBC");
  6929. XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  6930. break;
  6931. #ifdef WOLFSSL_AES_COUNTER
  6932. case AES_128_CTR_TYPE :
  6933. case AES_192_CTR_TYPE :
  6934. case AES_256_CTR_TYPE :
  6935. WOLFSSL_MSG("AES CTR");
  6936. XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  6937. break;
  6938. #endif
  6939. #endif /* NO_AES */
  6940. #ifndef NO_DES3
  6941. case DES_CBC_TYPE :
  6942. WOLFSSL_MSG("DES CBC");
  6943. XMEMCPY(&ctx->cipher.des.reg, ctx->iv, DES_BLOCK_SIZE);
  6944. break;
  6945. case DES_EDE3_CBC_TYPE :
  6946. WOLFSSL_MSG("DES EDE3 CBC");
  6947. XMEMCPY(&ctx->cipher.des3.reg, ctx->iv, DES_BLOCK_SIZE);
  6948. break;
  6949. #endif
  6950. #ifdef HAVE_IDEA
  6951. case IDEA_CBC_TYPE :
  6952. WOLFSSL_MSG("IDEA CBC");
  6953. XMEMCPY(&ctx->cipher.idea.reg, ctx->iv, IDEA_BLOCK_SIZE);
  6954. break;
  6955. #endif
  6956. case ARC4_TYPE :
  6957. WOLFSSL_MSG("ARC4");
  6958. break;
  6959. case NULL_CIPHER_TYPE :
  6960. WOLFSSL_MSG("NULL");
  6961. break;
  6962. default: {
  6963. WOLFSSL_MSG("bad type");
  6964. return SSL_FATAL_ERROR;
  6965. }
  6966. }
  6967. return SSL_SUCCESS;
  6968. }
  6969. /* SSL_SUCCESS on ok */
  6970. int wolfSSL_EVP_DigestInit(WOLFSSL_EVP_MD_CTX* ctx,
  6971. const WOLFSSL_EVP_MD* type)
  6972. {
  6973. WOLFSSL_ENTER("EVP_DigestInit");
  6974. if (XSTRNCMP(type, "SHA256", 6) == 0) {
  6975. ctx->macType = SHA256;
  6976. wolfSSL_SHA256_Init((SHA256_CTX*)&ctx->hash);
  6977. }
  6978. #ifdef WOLFSSL_SHA384
  6979. else if (XSTRNCMP(type, "SHA384", 6) == 0) {
  6980. ctx->macType = SHA384;
  6981. wolfSSL_SHA384_Init((SHA384_CTX*)&ctx->hash);
  6982. }
  6983. #endif
  6984. #ifdef WOLFSSL_SHA512
  6985. else if (XSTRNCMP(type, "SHA512", 6) == 0) {
  6986. ctx->macType = SHA512;
  6987. wolfSSL_SHA512_Init((SHA512_CTX*)&ctx->hash);
  6988. }
  6989. #endif
  6990. #ifndef NO_MD5
  6991. else if (XSTRNCMP(type, "MD5", 3) == 0) {
  6992. ctx->macType = MD5;
  6993. wolfSSL_MD5_Init((MD5_CTX*)&ctx->hash);
  6994. }
  6995. #endif
  6996. #ifndef NO_SHA
  6997. /* has to be last since would pick or 256, 384, or 512 too */
  6998. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  6999. ctx->macType = SHA;
  7000. wolfSSL_SHA_Init((SHA_CTX*)&ctx->hash);
  7001. }
  7002. #endif /* NO_SHA */
  7003. else
  7004. return BAD_FUNC_ARG;
  7005. return SSL_SUCCESS;
  7006. }
  7007. /* SSL_SUCCESS on ok */
  7008. int wolfSSL_EVP_DigestUpdate(WOLFSSL_EVP_MD_CTX* ctx, const void* data,
  7009. unsigned long sz)
  7010. {
  7011. WOLFSSL_ENTER("EVP_DigestUpdate");
  7012. switch (ctx->macType) {
  7013. #ifndef NO_MD5
  7014. case MD5:
  7015. wolfSSL_MD5_Update((MD5_CTX*)&ctx->hash, data,
  7016. (unsigned long)sz);
  7017. break;
  7018. #endif
  7019. #ifndef NO_SHA
  7020. case SHA:
  7021. wolfSSL_SHA_Update((SHA_CTX*)&ctx->hash, data,
  7022. (unsigned long)sz);
  7023. break;
  7024. #endif
  7025. #ifndef NO_SHA256
  7026. case SHA256:
  7027. wolfSSL_SHA256_Update((SHA256_CTX*)&ctx->hash, data,
  7028. (unsigned long)sz);
  7029. break;
  7030. #endif
  7031. #ifdef WOLFSSL_SHA384
  7032. case SHA384:
  7033. wolfSSL_SHA384_Update((SHA384_CTX*)&ctx->hash, data,
  7034. (unsigned long)sz);
  7035. break;
  7036. #endif
  7037. #ifdef WOLFSSL_SHA512
  7038. case SHA512:
  7039. wolfSSL_SHA512_Update((SHA512_CTX*)&ctx->hash, data,
  7040. (unsigned long)sz);
  7041. break;
  7042. #endif
  7043. default:
  7044. return BAD_FUNC_ARG;
  7045. }
  7046. return SSL_SUCCESS;
  7047. }
  7048. /* SSL_SUCCESS on ok */
  7049. int wolfSSL_EVP_DigestFinal(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md,
  7050. unsigned int* s)
  7051. {
  7052. WOLFSSL_ENTER("EVP_DigestFinal");
  7053. switch (ctx->macType) {
  7054. #ifndef NO_MD5
  7055. case MD5:
  7056. wolfSSL_MD5_Final(md, (MD5_CTX*)&ctx->hash);
  7057. if (s) *s = MD5_DIGEST_SIZE;
  7058. break;
  7059. #endif
  7060. #ifndef NO_SHA
  7061. case SHA:
  7062. wolfSSL_SHA_Final(md, (SHA_CTX*)&ctx->hash);
  7063. if (s) *s = SHA_DIGEST_SIZE;
  7064. break;
  7065. #endif
  7066. #ifndef NO_SHA256
  7067. case SHA256:
  7068. wolfSSL_SHA256_Final(md, (SHA256_CTX*)&ctx->hash);
  7069. if (s) *s = SHA256_DIGEST_SIZE;
  7070. break;
  7071. #endif
  7072. #ifdef WOLFSSL_SHA384
  7073. case SHA384:
  7074. wolfSSL_SHA384_Final(md, (SHA384_CTX*)&ctx->hash);
  7075. if (s) *s = SHA384_DIGEST_SIZE;
  7076. break;
  7077. #endif
  7078. #ifdef WOLFSSL_SHA512
  7079. case SHA512:
  7080. wolfSSL_SHA512_Final(md, (SHA512_CTX*)&ctx->hash);
  7081. if (s) *s = SHA512_DIGEST_SIZE;
  7082. break;
  7083. #endif
  7084. default:
  7085. return BAD_FUNC_ARG;
  7086. }
  7087. return SSL_SUCCESS;
  7088. }
  7089. /* SSL_SUCCESS on ok */
  7090. int wolfSSL_EVP_DigestFinal_ex(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md,
  7091. unsigned int* s)
  7092. {
  7093. WOLFSSL_ENTER("EVP_DigestFinal_ex");
  7094. return EVP_DigestFinal(ctx, md, s);
  7095. }
  7096. unsigned char* wolfSSL_HMAC(const WOLFSSL_EVP_MD* evp_md, const void* key,
  7097. int key_len, const unsigned char* d, int n,
  7098. unsigned char* md, unsigned int* md_len)
  7099. {
  7100. int type;
  7101. unsigned char* ret = NULL;
  7102. #ifdef WOLFSSL_SMALL_STACK
  7103. Hmac* hmac = NULL;
  7104. #else
  7105. Hmac hmac[1];
  7106. #endif
  7107. WOLFSSL_ENTER("HMAC");
  7108. if (!md)
  7109. return NULL; /* no static buffer support */
  7110. if (XSTRNCMP(evp_md, "MD5", 3) == 0)
  7111. type = MD5;
  7112. else if (XSTRNCMP(evp_md, "SHA", 3) == 0)
  7113. type = SHA;
  7114. else
  7115. return NULL;
  7116. #ifdef WOLFSSL_SMALL_STACK
  7117. hmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7118. if (hmac == NULL)
  7119. return NULL;
  7120. #endif
  7121. if (wc_HmacSetKey(hmac, type, (const byte*)key, key_len) == 0)
  7122. if (wc_HmacUpdate(hmac, d, n) == 0)
  7123. if (wc_HmacFinal(hmac, md) == 0) {
  7124. if (md_len)
  7125. *md_len = (type == MD5) ? (int)MD5_DIGEST_SIZE
  7126. : (int)SHA_DIGEST_SIZE;
  7127. ret = md;
  7128. }
  7129. #ifdef WOLFSSL_SMALL_STACK
  7130. XFREE(hmac, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7131. #endif
  7132. return ret;
  7133. }
  7134. void wolfSSL_ERR_clear_error(void)
  7135. {
  7136. /* TODO: */
  7137. }
  7138. int wolfSSL_RAND_status(void)
  7139. {
  7140. return SSL_SUCCESS; /* wolfCrypt provides enough seed internally */
  7141. }
  7142. void wolfSSL_RAND_add(const void* add, int len, double entropy)
  7143. {
  7144. (void)add;
  7145. (void)len;
  7146. (void)entropy;
  7147. /* wolfSSL seeds/adds internally, use explicit RNG if you want
  7148. to take control */
  7149. }
  7150. #ifndef NO_DES3
  7151. /* SSL_SUCCESS on ok */
  7152. int wolfSSL_DES_key_sched(WOLFSSL_const_DES_cblock* key,
  7153. WOLFSSL_DES_key_schedule* schedule)
  7154. {
  7155. WOLFSSL_ENTER("DES_key_sched");
  7156. XMEMCPY(schedule, key, sizeof(const_DES_cblock));
  7157. return SSL_SUCCESS;
  7158. }
  7159. void wolfSSL_DES_cbc_encrypt(const unsigned char* input,
  7160. unsigned char* output, long length,
  7161. WOLFSSL_DES_key_schedule* schedule,
  7162. WOLFSSL_DES_cblock* ivec, int enc)
  7163. {
  7164. Des myDes;
  7165. WOLFSSL_ENTER("DES_cbc_encrypt");
  7166. /* OpenSSL compat, no ret */
  7167. wc_Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
  7168. if (enc)
  7169. wc_Des_CbcEncrypt(&myDes, output, input, (word32)length);
  7170. else
  7171. wc_Des_CbcDecrypt(&myDes, output, input, (word32)length);
  7172. }
  7173. /* correctly sets ivec for next call */
  7174. void wolfSSL_DES_ncbc_encrypt(const unsigned char* input,
  7175. unsigned char* output, long length,
  7176. WOLFSSL_DES_key_schedule* schedule, WOLFSSL_DES_cblock* ivec,
  7177. int enc)
  7178. {
  7179. Des myDes;
  7180. WOLFSSL_ENTER("DES_ncbc_encrypt");
  7181. /* OpenSSL compat, no ret */
  7182. wc_Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
  7183. if (enc)
  7184. wc_Des_CbcEncrypt(&myDes, output, input, (word32)length);
  7185. else
  7186. wc_Des_CbcDecrypt(&myDes, output, input, (word32)length);
  7187. XMEMCPY(ivec, output + length - sizeof(DES_cblock), sizeof(DES_cblock));
  7188. }
  7189. #endif /* NO_DES3 */
  7190. void wolfSSL_ERR_free_strings(void)
  7191. {
  7192. /* handled internally */
  7193. }
  7194. void wolfSSL_ERR_remove_state(unsigned long state)
  7195. {
  7196. /* TODO: GetErrors().Remove(); */
  7197. (void)state;
  7198. }
  7199. void wolfSSL_EVP_cleanup(void)
  7200. {
  7201. /* nothing to do here */
  7202. }
  7203. void wolfSSL_cleanup_all_ex_data(void)
  7204. {
  7205. /* nothing to do here */
  7206. }
  7207. int wolfSSL_clear(WOLFSSL* ssl)
  7208. {
  7209. (void)ssl;
  7210. /* TODO: GetErrors().Remove(); */
  7211. return SSL_SUCCESS;
  7212. }
  7213. long wolfSSL_SSL_SESSION_set_timeout(WOLFSSL_SESSION* ses, long t)
  7214. {
  7215. word32 tmptime;
  7216. if (!ses || t < 0)
  7217. return BAD_FUNC_ARG;
  7218. tmptime = t & 0xFFFFFFFF;
  7219. ses->timeout = tmptime;
  7220. return SSL_SUCCESS;
  7221. }
  7222. long wolfSSL_CTX_set_mode(WOLFSSL_CTX* ctx, long mode)
  7223. {
  7224. /* SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER is wolfSSL default mode */
  7225. WOLFSSL_ENTER("SSL_CTX_set_mode");
  7226. if (mode == SSL_MODE_ENABLE_PARTIAL_WRITE)
  7227. ctx->partialWrite = 1;
  7228. return mode;
  7229. }
  7230. long wolfSSL_SSL_get_mode(WOLFSSL* ssl)
  7231. {
  7232. /* TODO: */
  7233. (void)ssl;
  7234. return 0;
  7235. }
  7236. long wolfSSL_CTX_get_mode(WOLFSSL_CTX* ctx)
  7237. {
  7238. /* TODO: */
  7239. (void)ctx;
  7240. return 0;
  7241. }
  7242. void wolfSSL_CTX_set_default_read_ahead(WOLFSSL_CTX* ctx, int m)
  7243. {
  7244. /* TODO: maybe? */
  7245. (void)ctx;
  7246. (void)m;
  7247. }
  7248. int wolfSSL_CTX_set_session_id_context(WOLFSSL_CTX* ctx,
  7249. const unsigned char* sid_ctx,
  7250. unsigned int sid_ctx_len)
  7251. {
  7252. /* No application specific context needed for wolfSSL */
  7253. (void)ctx;
  7254. (void)sid_ctx;
  7255. (void)sid_ctx_len;
  7256. return SSL_SUCCESS;
  7257. }
  7258. long wolfSSL_CTX_sess_get_cache_size(WOLFSSL_CTX* ctx)
  7259. {
  7260. /* TODO: maybe? */
  7261. (void)ctx;
  7262. return (~0);
  7263. }
  7264. unsigned long wolfSSL_ERR_get_error_line_data(const char** file, int* line,
  7265. const char** data, int *flags)
  7266. {
  7267. /* Not implemented */
  7268. (void)file;
  7269. (void)line;
  7270. (void)data;
  7271. (void)flags;
  7272. return 0;
  7273. }
  7274. #endif /* OPENSSL_EXTRA */
  7275. #if defined(KEEP_PEER_CERT)
  7276. WOLFSSL_X509* wolfSSL_get_peer_certificate(WOLFSSL* ssl)
  7277. {
  7278. WOLFSSL_ENTER("SSL_get_peer_certificate");
  7279. if (ssl->peerCert.issuer.sz)
  7280. return &ssl->peerCert;
  7281. else
  7282. return 0;
  7283. }
  7284. #endif /* KEEP_PEER_CERT */
  7285. #if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || defined(OPENSSSL_EXTRA)
  7286. /* user externally called free X509, if dynamic go ahead with free, otherwise
  7287. * don't */
  7288. static void ExternalFreeX509(WOLFSSL_X509* x509)
  7289. {
  7290. WOLFSSL_ENTER("ExternalFreeX509");
  7291. if (x509) {
  7292. if (x509->dynamicMemory) {
  7293. FreeX509(x509);
  7294. } else {
  7295. WOLFSSL_MSG("free called on non dynamic object, not freeing");
  7296. }
  7297. }
  7298. }
  7299. #endif /* KEEP_PEER_CERT || SESSION_CERTS || OPENSSSL_EXTRA */
  7300. #if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)
  7301. void wolfSSL_FreeX509(WOLFSSL_X509* x509)
  7302. {
  7303. WOLFSSL_ENTER("wolfSSL_FreeX509");
  7304. ExternalFreeX509(x509);
  7305. }
  7306. /* return the next, if any, altname from the peer cert */
  7307. char* wolfSSL_X509_get_next_altname(WOLFSSL_X509* cert)
  7308. {
  7309. char* ret = NULL;
  7310. WOLFSSL_ENTER("wolfSSL_X509_get_next_altname");
  7311. /* don't have any to work with */
  7312. if (cert == NULL || cert->altNames == NULL)
  7313. return NULL;
  7314. /* already went through them */
  7315. if (cert->altNamesNext == NULL)
  7316. return NULL;
  7317. ret = cert->altNamesNext->name;
  7318. cert->altNamesNext = cert->altNamesNext->next;
  7319. return ret;
  7320. }
  7321. WOLFSSL_X509_NAME* wolfSSL_X509_get_issuer_name(WOLFSSL_X509* cert)
  7322. {
  7323. WOLFSSL_ENTER("X509_get_issuer_name");
  7324. if(cert)
  7325. return &cert->issuer;
  7326. return NULL;
  7327. }
  7328. WOLFSSL_X509_NAME* wolfSSL_X509_get_subject_name(WOLFSSL_X509* cert)
  7329. {
  7330. WOLFSSL_ENTER("wolfSSL_X509_get_subject_name");
  7331. if(cert)
  7332. return &cert->subject;
  7333. return NULL;
  7334. }
  7335. int wolfSSL_X509_get_isCA(WOLFSSL_X509* x509)
  7336. {
  7337. int isCA = 0;
  7338. WOLFSSL_ENTER("wolfSSL_X509_get_isCA");
  7339. if (x509 != NULL)
  7340. isCA = x509->isCa;
  7341. WOLFSSL_LEAVE("wolfSSL_X509_get_isCA", isCA);
  7342. return isCA;
  7343. }
  7344. #ifdef OPENSSL_EXTRA
  7345. int wolfSSL_X509_ext_isSet_by_NID(WOLFSSL_X509* x509, int nid)
  7346. {
  7347. int isSet = 0;
  7348. WOLFSSL_ENTER("wolfSSL_X509_ext_isSet_by_NID");
  7349. if (x509 != NULL) {
  7350. switch (nid) {
  7351. case BASIC_CA_OID: isSet = x509->basicConstSet; break;
  7352. case ALT_NAMES_OID: isSet = x509->subjAltNameSet; break;
  7353. case AUTH_KEY_OID: isSet = x509->authKeyIdSet; break;
  7354. case SUBJ_KEY_OID: isSet = x509->subjKeyIdSet; break;
  7355. case KEY_USAGE_OID: isSet = x509->keyUsageSet; break;
  7356. #ifdef WOLFSSL_SEP
  7357. case CERT_POLICY_OID: isSet = x509->certPolicySet; break;
  7358. #endif /* WOLFSSL_SEP */
  7359. }
  7360. }
  7361. WOLFSSL_LEAVE("wolfSSL_X509_ext_isSet_by_NID", isSet);
  7362. return isSet;
  7363. }
  7364. int wolfSSL_X509_ext_get_critical_by_NID(WOLFSSL_X509* x509, int nid)
  7365. {
  7366. int crit = 0;
  7367. WOLFSSL_ENTER("wolfSSL_X509_ext_get_critical_by_NID");
  7368. if (x509 != NULL) {
  7369. switch (nid) {
  7370. case BASIC_CA_OID: crit = x509->basicConstCrit; break;
  7371. case ALT_NAMES_OID: crit = x509->subjAltNameCrit; break;
  7372. case AUTH_KEY_OID: crit = x509->authKeyIdCrit; break;
  7373. case SUBJ_KEY_OID: crit = x509->subjKeyIdCrit; break;
  7374. case KEY_USAGE_OID: crit = x509->keyUsageCrit; break;
  7375. #ifdef WOLFSSL_SEP
  7376. case CERT_POLICY_OID: crit = x509->certPolicyCrit; break;
  7377. #endif /* WOLFSSL_SEP */
  7378. }
  7379. }
  7380. WOLFSSL_LEAVE("wolfSSL_X509_ext_get_critical_by_NID", crit);
  7381. return crit;
  7382. }
  7383. int wolfSSL_X509_get_isSet_pathLength(WOLFSSL_X509* x509)
  7384. {
  7385. int isSet = 0;
  7386. WOLFSSL_ENTER("wolfSSL_X509_get_isSet_pathLength");
  7387. if (x509 != NULL)
  7388. isSet = x509->basicConstPlSet;
  7389. WOLFSSL_LEAVE("wolfSSL_X509_get_isSet_pathLength", isSet);
  7390. return isSet;
  7391. }
  7392. word32 wolfSSL_X509_get_pathLength(WOLFSSL_X509* x509)
  7393. {
  7394. word32 pathLength = 0;
  7395. WOLFSSL_ENTER("wolfSSL_X509_get_pathLength");
  7396. if (x509 != NULL)
  7397. pathLength = x509->pathLength;
  7398. WOLFSSL_LEAVE("wolfSSL_X509_get_pathLength", pathLength);
  7399. return pathLength;
  7400. }
  7401. unsigned int wolfSSL_X509_get_keyUsage(WOLFSSL_X509* x509)
  7402. {
  7403. word16 usage = 0;
  7404. WOLFSSL_ENTER("wolfSSL_X509_get_keyUsage");
  7405. if (x509 != NULL)
  7406. usage = x509->keyUsage;
  7407. WOLFSSL_LEAVE("wolfSSL_X509_get_keyUsage", usage);
  7408. return usage;
  7409. }
  7410. byte* wolfSSL_X509_get_authorityKeyID(WOLFSSL_X509* x509,
  7411. byte* dst, int* dstLen)
  7412. {
  7413. byte *id = NULL;
  7414. int copySz = 0;
  7415. WOLFSSL_ENTER("wolfSSL_X509_get_authorityKeyID");
  7416. if (x509 != NULL) {
  7417. if (x509->authKeyIdSet) {
  7418. copySz = min(dstLen != NULL ? *dstLen : 0,
  7419. (int)x509->authKeyIdSz);
  7420. id = x509->authKeyId;
  7421. }
  7422. if (dst != NULL && dstLen != NULL && id != NULL && copySz > 0) {
  7423. XMEMCPY(dst, id, copySz);
  7424. id = dst;
  7425. *dstLen = copySz;
  7426. }
  7427. }
  7428. WOLFSSL_LEAVE("wolfSSL_X509_get_authorityKeyID", copySz);
  7429. return id;
  7430. }
  7431. byte* wolfSSL_X509_get_subjectKeyID(WOLFSSL_X509* x509,
  7432. byte* dst, int* dstLen)
  7433. {
  7434. byte *id = NULL;
  7435. int copySz = 0;
  7436. WOLFSSL_ENTER("wolfSSL_X509_get_subjectKeyID");
  7437. if (x509 != NULL) {
  7438. if (x509->subjKeyIdSet) {
  7439. copySz = min(dstLen != NULL ? *dstLen : 0,
  7440. (int)x509->subjKeyIdSz);
  7441. id = x509->subjKeyId;
  7442. }
  7443. if (dst != NULL && dstLen != NULL && id != NULL && copySz > 0) {
  7444. XMEMCPY(dst, id, copySz);
  7445. id = dst;
  7446. *dstLen = copySz;
  7447. }
  7448. }
  7449. WOLFSSL_LEAVE("wolfSSL_X509_get_subjectKeyID", copySz);
  7450. return id;
  7451. }
  7452. int wolfSSL_X509_NAME_entry_count(WOLFSSL_X509_NAME* name)
  7453. {
  7454. int count = 0;
  7455. WOLFSSL_ENTER("wolfSSL_X509_NAME_entry_count");
  7456. if (name != NULL)
  7457. count = name->fullName.entryCount;
  7458. WOLFSSL_LEAVE("wolfSSL_X509_NAME_entry_count", count);
  7459. return count;
  7460. }
  7461. int wolfSSL_X509_NAME_get_text_by_NID(WOLFSSL_X509_NAME* name,
  7462. int nid, char* buf, int len)
  7463. {
  7464. char *text = NULL;
  7465. int textSz = 0;
  7466. WOLFSSL_ENTER("wolfSSL_X509_NAME_get_text_by_NID");
  7467. switch (nid) {
  7468. case ASN_COMMON_NAME:
  7469. text = name->fullName.fullName + name->fullName.cnIdx;
  7470. textSz = name->fullName.cnLen;
  7471. break;
  7472. case ASN_SUR_NAME:
  7473. text = name->fullName.fullName + name->fullName.snIdx;
  7474. textSz = name->fullName.snLen;
  7475. break;
  7476. case ASN_SERIAL_NUMBER:
  7477. text = name->fullName.fullName + name->fullName.serialIdx;
  7478. textSz = name->fullName.serialLen;
  7479. break;
  7480. case ASN_COUNTRY_NAME:
  7481. text = name->fullName.fullName + name->fullName.cIdx;
  7482. textSz = name->fullName.cLen;
  7483. break;
  7484. case ASN_LOCALITY_NAME:
  7485. text = name->fullName.fullName + name->fullName.lIdx;
  7486. textSz = name->fullName.lLen;
  7487. break;
  7488. case ASN_STATE_NAME:
  7489. text = name->fullName.fullName + name->fullName.stIdx;
  7490. textSz = name->fullName.stLen;
  7491. break;
  7492. case ASN_ORG_NAME:
  7493. text = name->fullName.fullName + name->fullName.oIdx;
  7494. textSz = name->fullName.oLen;
  7495. break;
  7496. case ASN_ORGUNIT_NAME:
  7497. text = name->fullName.fullName + name->fullName.ouIdx;
  7498. textSz = name->fullName.ouLen;
  7499. break;
  7500. default:
  7501. break;
  7502. }
  7503. if (buf != NULL && text != NULL) {
  7504. textSz = min(textSz, len);
  7505. XMEMCPY(buf, text, textSz);
  7506. buf[textSz] = '\0';
  7507. }
  7508. WOLFSSL_LEAVE("wolfSSL_X509_NAME_get_text_by_NID", textSz);
  7509. return textSz;
  7510. }
  7511. #endif
  7512. /* copy name into in buffer, at most sz bytes, if buffer is null will
  7513. malloc buffer, call responsible for freeing */
  7514. char* wolfSSL_X509_NAME_oneline(WOLFSSL_X509_NAME* name, char* in, int sz)
  7515. {
  7516. int copySz = min(sz, name->sz);
  7517. WOLFSSL_ENTER("wolfSSL_X509_NAME_oneline");
  7518. if (!name->sz) return in;
  7519. if (!in) {
  7520. in = (char*)XMALLOC(name->sz, 0, DYNAMIC_TYPE_OPENSSL);
  7521. if (!in ) return in;
  7522. copySz = name->sz;
  7523. }
  7524. if (copySz == 0)
  7525. return in;
  7526. XMEMCPY(in, name->name, copySz - 1);
  7527. in[copySz - 1] = 0;
  7528. return in;
  7529. }
  7530. int wolfSSL_X509_get_signature_type(WOLFSSL_X509* x509)
  7531. {
  7532. int type = 0;
  7533. WOLFSSL_ENTER("wolfSSL_X509_get_signature_type");
  7534. if (x509 != NULL)
  7535. type = x509->sigOID;
  7536. return type;
  7537. }
  7538. int wolfSSL_X509_get_signature(WOLFSSL_X509* x509,
  7539. unsigned char* buf, int* bufSz)
  7540. {
  7541. WOLFSSL_ENTER("wolfSSL_X509_get_signature");
  7542. if (x509 == NULL || bufSz == NULL || *bufSz < (int)x509->sig.length)
  7543. return SSL_FATAL_ERROR;
  7544. if (buf != NULL)
  7545. XMEMCPY(buf, x509->sig.buffer, x509->sig.length);
  7546. *bufSz = x509->sig.length;
  7547. return SSL_SUCCESS;
  7548. }
  7549. /* write X509 serial number in unsigned binary to buffer
  7550. buffer needs to be at least EXTERNAL_SERIAL_SIZE (32) for all cases
  7551. return SSL_SUCCESS on success */
  7552. int wolfSSL_X509_get_serial_number(WOLFSSL_X509* x509,
  7553. byte* in, int* inOutSz)
  7554. {
  7555. WOLFSSL_ENTER("wolfSSL_X509_get_serial_number");
  7556. if (x509 == NULL || in == NULL ||
  7557. inOutSz == NULL || *inOutSz < x509->serialSz)
  7558. return BAD_FUNC_ARG;
  7559. XMEMCPY(in, x509->serial, x509->serialSz);
  7560. *inOutSz = x509->serialSz;
  7561. return SSL_SUCCESS;
  7562. }
  7563. const byte* wolfSSL_X509_get_der(WOLFSSL_X509* x509, int* outSz)
  7564. {
  7565. WOLFSSL_ENTER("wolfSSL_X509_get_der");
  7566. if (x509 == NULL || outSz == NULL)
  7567. return NULL;
  7568. *outSz = (int)x509->derCert.length;
  7569. return x509->derCert.buffer;
  7570. }
  7571. int wolfSSL_X509_version(WOLFSSL_X509* x509)
  7572. {
  7573. WOLFSSL_ENTER("wolfSSL_X509_version");
  7574. if (x509 == NULL)
  7575. return 0;
  7576. return x509->version;
  7577. }
  7578. const byte* wolfSSL_X509_notBefore(WOLFSSL_X509* x509)
  7579. {
  7580. WOLFSSL_ENTER("wolfSSL_X509_notBefore");
  7581. if (x509 == NULL)
  7582. return NULL;
  7583. return x509->notBefore;
  7584. }
  7585. const byte* wolfSSL_X509_notAfter(WOLFSSL_X509* x509)
  7586. {
  7587. WOLFSSL_ENTER("wolfSSL_X509_notAfter");
  7588. if (x509 == NULL)
  7589. return NULL;
  7590. return x509->notAfter;
  7591. }
  7592. #ifdef WOLFSSL_SEP
  7593. /* copy oid into in buffer, at most *inOutSz bytes, if buffer is null will
  7594. malloc buffer, call responsible for freeing. Actual size returned in
  7595. *inOutSz. Requires inOutSz be non-null */
  7596. byte* wolfSSL_X509_get_device_type(WOLFSSL_X509* x509, byte* in, int *inOutSz)
  7597. {
  7598. int copySz;
  7599. WOLFSSL_ENTER("wolfSSL_X509_get_dev_type");
  7600. if (inOutSz == NULL) return NULL;
  7601. if (!x509->deviceTypeSz) return in;
  7602. copySz = min(*inOutSz, x509->deviceTypeSz);
  7603. if (!in) {
  7604. in = (byte*)XMALLOC(x509->deviceTypeSz, 0, DYNAMIC_TYPE_OPENSSL);
  7605. if (!in) return in;
  7606. copySz = x509->deviceTypeSz;
  7607. }
  7608. XMEMCPY(in, x509->deviceType, copySz);
  7609. *inOutSz = copySz;
  7610. return in;
  7611. }
  7612. byte* wolfSSL_X509_get_hw_type(WOLFSSL_X509* x509, byte* in, int* inOutSz)
  7613. {
  7614. int copySz;
  7615. WOLFSSL_ENTER("wolfSSL_X509_get_hw_type");
  7616. if (inOutSz == NULL) return NULL;
  7617. if (!x509->hwTypeSz) return in;
  7618. copySz = min(*inOutSz, x509->hwTypeSz);
  7619. if (!in) {
  7620. in = (byte*)XMALLOC(x509->hwTypeSz, 0, DYNAMIC_TYPE_OPENSSL);
  7621. if (!in) return in;
  7622. copySz = x509->hwTypeSz;
  7623. }
  7624. XMEMCPY(in, x509->hwType, copySz);
  7625. *inOutSz = copySz;
  7626. return in;
  7627. }
  7628. byte* wolfSSL_X509_get_hw_serial_number(WOLFSSL_X509* x509,byte* in,
  7629. int* inOutSz)
  7630. {
  7631. int copySz;
  7632. WOLFSSL_ENTER("wolfSSL_X509_get_hw_serial_number");
  7633. if (inOutSz == NULL) return NULL;
  7634. if (!x509->hwTypeSz) return in;
  7635. copySz = min(*inOutSz, x509->hwSerialNumSz);
  7636. if (!in) {
  7637. in = (byte*)XMALLOC(x509->hwSerialNumSz, 0, DYNAMIC_TYPE_OPENSSL);
  7638. if (!in) return in;
  7639. copySz = x509->hwSerialNumSz;
  7640. }
  7641. XMEMCPY(in, x509->hwSerialNum, copySz);
  7642. *inOutSz = copySz;
  7643. return in;
  7644. }
  7645. #endif /* WOLFSSL_SEP */
  7646. WOLFSSL_X509* wolfSSL_X509_d2i(WOLFSSL_X509** x509, const byte* in, int len)
  7647. {
  7648. WOLFSSL_X509 *newX509 = NULL;
  7649. WOLFSSL_ENTER("wolfSSL_X509_d2i");
  7650. if (in != NULL && len != 0) {
  7651. #ifdef WOLFSSL_SMALL_STACK
  7652. DecodedCert* cert = NULL;
  7653. #else
  7654. DecodedCert cert[1];
  7655. #endif
  7656. #ifdef WOLFSSL_SMALL_STACK
  7657. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  7658. DYNAMIC_TYPE_TMP_BUFFER);
  7659. if (cert == NULL)
  7660. return NULL;
  7661. #endif
  7662. InitDecodedCert(cert, (byte*)in, len, NULL);
  7663. if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
  7664. newX509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
  7665. DYNAMIC_TYPE_X509);
  7666. if (newX509 != NULL) {
  7667. InitX509(newX509, 1);
  7668. if (CopyDecodedToX509(newX509, cert) != 0) {
  7669. XFREE(newX509, NULL, DYNAMIC_TYPE_X509);
  7670. newX509 = NULL;
  7671. }
  7672. }
  7673. }
  7674. FreeDecodedCert(cert);
  7675. #ifdef WOLFSSL_SMALL_STACK
  7676. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7677. #endif
  7678. }
  7679. if (x509 != NULL)
  7680. *x509 = newX509;
  7681. return newX509;
  7682. }
  7683. #ifndef NO_FILESYSTEM
  7684. #ifndef NO_STDIO_FILESYSTEM
  7685. WOLFSSL_X509* wolfSSL_X509_d2i_fp(WOLFSSL_X509** x509, XFILE file)
  7686. {
  7687. WOLFSSL_X509* newX509 = NULL;
  7688. WOLFSSL_ENTER("wolfSSL_X509_d2i_fp");
  7689. if (file != XBADFILE) {
  7690. byte* fileBuffer = NULL;
  7691. long sz = 0;
  7692. XFSEEK(file, 0, XSEEK_END);
  7693. sz = XFTELL(file);
  7694. XREWIND(file);
  7695. if (sz < 0) {
  7696. WOLFSSL_MSG("Bad tell on FILE");
  7697. return NULL;
  7698. }
  7699. fileBuffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
  7700. if (fileBuffer != NULL) {
  7701. int ret = (int)XFREAD(fileBuffer, sz, 1, file);
  7702. if (ret > 0) {
  7703. newX509 = wolfSSL_X509_d2i(NULL, fileBuffer, (int)sz);
  7704. }
  7705. XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
  7706. }
  7707. }
  7708. if (x509 != NULL)
  7709. *x509 = newX509;
  7710. return newX509;
  7711. }
  7712. #endif /* NO_STDIO_FILESYSTEM */
  7713. WOLFSSL_X509* wolfSSL_X509_load_certificate_file(const char* fname, int format)
  7714. {
  7715. #ifdef WOLFSSL_SMALL_STACK
  7716. byte staticBuffer[1]; /* force heap usage */
  7717. #else
  7718. byte staticBuffer[FILE_BUFFER_SIZE];
  7719. #endif
  7720. byte* fileBuffer = staticBuffer;
  7721. int dynamic = 0;
  7722. int ret;
  7723. long sz = 0;
  7724. XFILE file;
  7725. WOLFSSL_X509* x509 = NULL;
  7726. buffer der;
  7727. WOLFSSL_ENTER("wolfSSL_X509_load_certificate");
  7728. /* Check the inputs */
  7729. if ((fname == NULL) ||
  7730. (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM))
  7731. return NULL;
  7732. file = XFOPEN(fname, "rb");
  7733. if (file == XBADFILE)
  7734. return NULL;
  7735. XFSEEK(file, 0, XSEEK_END);
  7736. sz = XFTELL(file);
  7737. XREWIND(file);
  7738. if (sz > (long)sizeof(staticBuffer)) {
  7739. fileBuffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
  7740. if (fileBuffer == NULL) {
  7741. XFCLOSE(file);
  7742. return NULL;
  7743. }
  7744. dynamic = 1;
  7745. }
  7746. else if (sz < 0) {
  7747. XFCLOSE(file);
  7748. return NULL;
  7749. }
  7750. ret = (int)XFREAD(fileBuffer, sz, 1, file);
  7751. if (ret < 0) {
  7752. XFCLOSE(file);
  7753. if (dynamic)
  7754. XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
  7755. return NULL;
  7756. }
  7757. XFCLOSE(file);
  7758. der.buffer = NULL;
  7759. der.length = 0;
  7760. if (format == SSL_FILETYPE_PEM) {
  7761. int ecc = 0;
  7762. #ifdef WOLFSSL_SMALL_STACK
  7763. EncryptedInfo* info = NULL;
  7764. #else
  7765. EncryptedInfo info[1];
  7766. #endif
  7767. #ifdef WOLFSSL_SMALL_STACK
  7768. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  7769. DYNAMIC_TYPE_TMP_BUFFER);
  7770. if (info == NULL) {
  7771. if (dynamic)
  7772. XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
  7773. return NULL;
  7774. }
  7775. #endif
  7776. info->set = 0;
  7777. info->ctx = NULL;
  7778. info->consumed = 0;
  7779. if (PemToDer(fileBuffer, sz, CERT_TYPE, &der, NULL, info, &ecc) != 0)
  7780. {
  7781. /* Only time this should fail, and leave `der` with a buffer
  7782. is when the Base64 Decode fails. Release `der.buffer` in
  7783. that case. */
  7784. if (der.buffer != NULL) {
  7785. XFREE(der.buffer, NULL, DYNAMIC_TYPE_CERT);
  7786. der.buffer = NULL;
  7787. }
  7788. }
  7789. #ifdef WOLFSSL_SMALL_STACK
  7790. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7791. #endif
  7792. }
  7793. else {
  7794. der.buffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_CERT);
  7795. if (der.buffer != NULL) {
  7796. XMEMCPY(der.buffer, fileBuffer, sz);
  7797. der.length = (word32)sz;
  7798. }
  7799. }
  7800. if (dynamic)
  7801. XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
  7802. /* At this point we want `der` to have the certificate in DER format */
  7803. /* ready to be decoded. */
  7804. if (der.buffer != NULL) {
  7805. #ifdef WOLFSSL_SMALL_STACK
  7806. DecodedCert* cert = NULL;
  7807. #else
  7808. DecodedCert cert[1];
  7809. #endif
  7810. #ifdef WOLFSSL_SMALL_STACK
  7811. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  7812. DYNAMIC_TYPE_TMP_BUFFER);
  7813. if (cert != NULL)
  7814. #endif
  7815. {
  7816. InitDecodedCert(cert, der.buffer, der.length, NULL);
  7817. if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
  7818. x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
  7819. DYNAMIC_TYPE_X509);
  7820. if (x509 != NULL) {
  7821. InitX509(x509, 1);
  7822. if (CopyDecodedToX509(x509, cert) != 0) {
  7823. XFREE(x509, NULL, DYNAMIC_TYPE_X509);
  7824. x509 = NULL;
  7825. }
  7826. }
  7827. }
  7828. FreeDecodedCert(cert);
  7829. #ifdef WOLFSSL_SMALL_STACK
  7830. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7831. #endif
  7832. }
  7833. XFREE(der.buffer, NULL, DYNAMIC_TYPE_CERT);
  7834. }
  7835. return x509;
  7836. }
  7837. #endif /* NO_FILESYSTEM */
  7838. #endif /* KEEP_PEER_CERT || SESSION_CERTS */
  7839. #ifdef OPENSSL_EXTRA
  7840. int wolfSSL_set_session_id_context(WOLFSSL* ssl, const unsigned char* id,
  7841. unsigned int len)
  7842. {
  7843. (void)ssl;
  7844. (void)id;
  7845. (void)len;
  7846. return 0;
  7847. }
  7848. void wolfSSL_set_connect_state(WOLFSSL* ssl)
  7849. {
  7850. (void)ssl;
  7851. /* client by default */
  7852. }
  7853. #endif
  7854. int wolfSSL_get_shutdown(const WOLFSSL* ssl)
  7855. {
  7856. WOLFSSL_ENTER("wolfSSL_get_shutdown");
  7857. #ifdef HAVE_STUNNEL
  7858. return (ssl->options.sentNotify << 1) | (ssl->options.closeNotify);
  7859. #else
  7860. return (ssl->options.isClosed ||
  7861. ssl->options.connReset ||
  7862. ssl->options.sentNotify);
  7863. #endif
  7864. }
  7865. int wolfSSL_session_reused(WOLFSSL* ssl)
  7866. {
  7867. return ssl->options.resuming;
  7868. }
  7869. #ifdef OPENSSL_EXTRA
  7870. void wolfSSL_SESSION_free(WOLFSSL_SESSION* session)
  7871. {
  7872. (void)session;
  7873. }
  7874. #endif
  7875. const char* wolfSSL_get_version(WOLFSSL* ssl)
  7876. {
  7877. WOLFSSL_ENTER("SSL_get_version");
  7878. if (ssl->version.major == SSLv3_MAJOR) {
  7879. switch (ssl->version.minor) {
  7880. case SSLv3_MINOR :
  7881. return "SSLv3";
  7882. case TLSv1_MINOR :
  7883. return "TLSv1";
  7884. case TLSv1_1_MINOR :
  7885. return "TLSv1.1";
  7886. case TLSv1_2_MINOR :
  7887. return "TLSv1.2";
  7888. default:
  7889. return "unknown";
  7890. }
  7891. }
  7892. else if (ssl->version.major == DTLS_MAJOR) {
  7893. switch (ssl->version.minor) {
  7894. case DTLS_MINOR :
  7895. return "DTLS";
  7896. case DTLSv1_2_MINOR :
  7897. return "DTLSv1.2";
  7898. default:
  7899. return "unknown";
  7900. }
  7901. }
  7902. return "unknown";
  7903. }
  7904. /* current library version */
  7905. const char* wolfSSL_lib_version(void)
  7906. {
  7907. return LIBWOLFSSL_VERSION_STRING;
  7908. }
  7909. /* current library version in hex */
  7910. word32 wolfSSL_lib_version_hex(void)
  7911. {
  7912. return LIBWOLFSSL_VERSION_HEX;
  7913. }
  7914. int wolfSSL_get_current_cipher_suite(WOLFSSL* ssl)
  7915. {
  7916. WOLFSSL_ENTER("SSL_get_current_cipher_suite");
  7917. if (ssl)
  7918. return (ssl->options.cipherSuite0 << 8) | ssl->options.cipherSuite;
  7919. return 0;
  7920. }
  7921. WOLFSSL_CIPHER* wolfSSL_get_current_cipher(WOLFSSL* ssl)
  7922. {
  7923. WOLFSSL_ENTER("SSL_get_current_cipher");
  7924. if (ssl)
  7925. return &ssl->cipher;
  7926. else
  7927. return NULL;
  7928. }
  7929. const char* wolfSSL_CIPHER_get_name(const WOLFSSL_CIPHER* cipher)
  7930. {
  7931. (void)cipher;
  7932. WOLFSSL_ENTER("SSL_CIPHER_get_name");
  7933. #ifndef NO_ERROR_STRINGS
  7934. if (cipher) {
  7935. #if defined(HAVE_CHACHA)
  7936. if (cipher->ssl->options.cipherSuite0 == CHACHA_BYTE) {
  7937. /* ChaCha suites */
  7938. switch (cipher->ssl->options.cipherSuite) {
  7939. #ifdef HAVE_CHACHA
  7940. #ifndef NO_RSA
  7941. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  7942. return "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256";
  7943. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  7944. return "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256";
  7945. #endif
  7946. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  7947. return "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256";
  7948. #endif
  7949. }
  7950. }
  7951. #endif
  7952. #if defined(HAVE_ECC) || defined(HAVE_AESCCM)
  7953. /* Awkwardly, the ECC cipher suites use the ECC_BYTE as expected,
  7954. * but the AES-CCM cipher suites also use it, even the ones that
  7955. * aren't ECC. */
  7956. if (cipher->ssl->options.cipherSuite0 == ECC_BYTE) {
  7957. /* ECC suites */
  7958. switch (cipher->ssl->options.cipherSuite) {
  7959. #ifdef HAVE_ECC
  7960. #ifndef NO_RSA
  7961. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 :
  7962. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256";
  7963. #endif
  7964. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 :
  7965. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256";
  7966. #ifndef NO_RSA
  7967. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 :
  7968. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256";
  7969. #endif
  7970. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 :
  7971. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256";
  7972. #ifndef NO_RSA
  7973. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 :
  7974. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384";
  7975. #endif
  7976. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 :
  7977. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384";
  7978. #ifndef NO_RSA
  7979. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 :
  7980. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384";
  7981. #endif
  7982. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 :
  7983. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384";
  7984. #ifndef NO_SHA
  7985. #ifndef NO_RSA
  7986. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA :
  7987. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA";
  7988. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA :
  7989. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA";
  7990. #endif
  7991. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA :
  7992. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA";
  7993. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA :
  7994. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA";
  7995. #ifndef NO_RC4
  7996. #ifndef NO_RSA
  7997. case TLS_ECDHE_RSA_WITH_RC4_128_SHA :
  7998. return "TLS_ECDHE_RSA_WITH_RC4_128_SHA";
  7999. #endif
  8000. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA :
  8001. return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA";
  8002. #endif
  8003. #ifndef NO_DES3
  8004. #ifndef NO_RSA
  8005. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA :
  8006. return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA";
  8007. #endif
  8008. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA :
  8009. return "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA";
  8010. #endif
  8011. #ifndef NO_RSA
  8012. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA :
  8013. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA";
  8014. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA :
  8015. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA";
  8016. #endif
  8017. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA :
  8018. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA";
  8019. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA :
  8020. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA";
  8021. #ifndef NO_RC4
  8022. #ifndef NO_RSA
  8023. case TLS_ECDH_RSA_WITH_RC4_128_SHA :
  8024. return "TLS_ECDH_RSA_WITH_RC4_128_SHA";
  8025. #endif
  8026. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA :
  8027. return "TLS_ECDH_ECDSA_WITH_RC4_128_SHA";
  8028. #endif
  8029. #ifndef NO_DES3
  8030. #ifndef NO_RSA
  8031. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA :
  8032. return "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA";
  8033. #endif
  8034. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA :
  8035. return "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA";
  8036. #endif
  8037. #endif /* NO_SHA */
  8038. #ifdef HAVE_AESGCM
  8039. #ifndef NO_RSA
  8040. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 :
  8041. return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
  8042. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 :
  8043. return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
  8044. #endif
  8045. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 :
  8046. return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
  8047. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 :
  8048. return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384";
  8049. #ifndef NO_RSA
  8050. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 :
  8051. return "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256";
  8052. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 :
  8053. return "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384";
  8054. #endif
  8055. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 :
  8056. return "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256";
  8057. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 :
  8058. return "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384";
  8059. #endif
  8060. #endif /* HAVE_ECC */
  8061. #ifdef HAVE_AESCCM
  8062. #ifndef NO_RSA
  8063. case TLS_RSA_WITH_AES_128_CCM_8 :
  8064. return "TLS_RSA_WITH_AES_128_CCM_8";
  8065. case TLS_RSA_WITH_AES_256_CCM_8 :
  8066. return "TLS_RSA_WITH_AES_256_CCM_8";
  8067. #endif
  8068. #ifndef NO_PSK
  8069. case TLS_PSK_WITH_AES_128_CCM_8 :
  8070. return "TLS_PSK_WITH_AES_128_CCM_8";
  8071. case TLS_PSK_WITH_AES_256_CCM_8 :
  8072. return "TLS_PSK_WITH_AES_256_CCM_8";
  8073. case TLS_PSK_WITH_AES_128_CCM :
  8074. return "TLS_PSK_WITH_AES_128_CCM";
  8075. case TLS_PSK_WITH_AES_256_CCM :
  8076. return "TLS_PSK_WITH_AES_256_CCM";
  8077. case TLS_DHE_PSK_WITH_AES_128_CCM :
  8078. return "TLS_DHE_PSK_WITH_AES_128_CCM";
  8079. case TLS_DHE_PSK_WITH_AES_256_CCM :
  8080. return "TLS_DHE_PSK_WITH_AES_256_CCM";
  8081. #endif
  8082. #ifdef HAVE_ECC
  8083. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  8084. return "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8";
  8085. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 :
  8086. return "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8";
  8087. #endif
  8088. #endif
  8089. default:
  8090. return "NONE";
  8091. }
  8092. }
  8093. #endif /* ECC */
  8094. if (cipher->ssl->options.cipherSuite0 != ECC_BYTE &&
  8095. cipher->ssl->options.cipherSuite0 != CHACHA_BYTE) {
  8096. /* normal suites */
  8097. switch (cipher->ssl->options.cipherSuite) {
  8098. #ifndef NO_RSA
  8099. #ifndef NO_RC4
  8100. #ifndef NO_SHA
  8101. case SSL_RSA_WITH_RC4_128_SHA :
  8102. return "SSL_RSA_WITH_RC4_128_SHA";
  8103. #endif
  8104. #ifndef NO_MD5
  8105. case SSL_RSA_WITH_RC4_128_MD5 :
  8106. return "SSL_RSA_WITH_RC4_128_MD5";
  8107. #endif
  8108. #endif
  8109. #ifndef NO_SHA
  8110. #ifndef NO_DES3
  8111. case SSL_RSA_WITH_3DES_EDE_CBC_SHA :
  8112. return "SSL_RSA_WITH_3DES_EDE_CBC_SHA";
  8113. #endif
  8114. #ifdef HAVE_IDEA
  8115. case SSL_RSA_WITH_IDEA_CBC_SHA :
  8116. return "SSL_RSA_WITH_IDEA_CBC_SHA";
  8117. #endif
  8118. case TLS_RSA_WITH_AES_128_CBC_SHA :
  8119. return "TLS_RSA_WITH_AES_128_CBC_SHA";
  8120. case TLS_RSA_WITH_AES_256_CBC_SHA :
  8121. return "TLS_RSA_WITH_AES_256_CBC_SHA";
  8122. #endif
  8123. case TLS_RSA_WITH_AES_128_CBC_SHA256 :
  8124. return "TLS_RSA_WITH_AES_128_CBC_SHA256";
  8125. case TLS_RSA_WITH_AES_256_CBC_SHA256 :
  8126. return "TLS_RSA_WITH_AES_256_CBC_SHA256";
  8127. #ifdef HAVE_BLAKE2
  8128. case TLS_RSA_WITH_AES_128_CBC_B2B256:
  8129. return "TLS_RSA_WITH_AES_128_CBC_B2B256";
  8130. case TLS_RSA_WITH_AES_256_CBC_B2B256:
  8131. return "TLS_RSA_WITH_AES_256_CBC_B2B256";
  8132. #endif
  8133. #ifndef NO_SHA
  8134. case TLS_RSA_WITH_NULL_SHA :
  8135. return "TLS_RSA_WITH_NULL_SHA";
  8136. #endif
  8137. case TLS_RSA_WITH_NULL_SHA256 :
  8138. return "TLS_RSA_WITH_NULL_SHA256";
  8139. #endif /* NO_RSA */
  8140. #ifndef NO_PSK
  8141. #ifndef NO_SHA
  8142. case TLS_PSK_WITH_AES_128_CBC_SHA :
  8143. return "TLS_PSK_WITH_AES_128_CBC_SHA";
  8144. case TLS_PSK_WITH_AES_256_CBC_SHA :
  8145. return "TLS_PSK_WITH_AES_256_CBC_SHA";
  8146. #endif
  8147. #ifndef NO_SHA256
  8148. case TLS_PSK_WITH_AES_128_CBC_SHA256 :
  8149. return "TLS_PSK_WITH_AES_128_CBC_SHA256";
  8150. case TLS_PSK_WITH_NULL_SHA256 :
  8151. return "TLS_PSK_WITH_NULL_SHA256";
  8152. case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 :
  8153. return "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256";
  8154. case TLS_DHE_PSK_WITH_NULL_SHA256 :
  8155. return "TLS_DHE_PSK_WITH_NULL_SHA256";
  8156. #ifdef HAVE_AESGCM
  8157. case TLS_PSK_WITH_AES_128_GCM_SHA256 :
  8158. return "TLS_PSK_WITH_AES_128_GCM_SHA256";
  8159. case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 :
  8160. return "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256";
  8161. #endif
  8162. #endif
  8163. #ifdef WOLFSSL_SHA384
  8164. case TLS_PSK_WITH_AES_256_CBC_SHA384 :
  8165. return "TLS_PSK_WITH_AES_256_CBC_SHA384";
  8166. case TLS_PSK_WITH_NULL_SHA384 :
  8167. return "TLS_PSK_WITH_NULL_SHA384";
  8168. case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 :
  8169. return "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384";
  8170. case TLS_DHE_PSK_WITH_NULL_SHA384 :
  8171. return "TLS_DHE_PSK_WITH_NULL_SHA384";
  8172. #ifdef HAVE_AESGCM
  8173. case TLS_PSK_WITH_AES_256_GCM_SHA384 :
  8174. return "TLS_PSK_WITH_AES_256_GCM_SHA384";
  8175. case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 :
  8176. return "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384";
  8177. #endif
  8178. #endif
  8179. #ifndef NO_SHA
  8180. case TLS_PSK_WITH_NULL_SHA :
  8181. return "TLS_PSK_WITH_NULL_SHA";
  8182. #endif
  8183. #endif /* NO_PSK */
  8184. #ifndef NO_RSA
  8185. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 :
  8186. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256";
  8187. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 :
  8188. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256";
  8189. #ifndef NO_SHA
  8190. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA :
  8191. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
  8192. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA :
  8193. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
  8194. #endif
  8195. #ifndef NO_HC128
  8196. #ifndef NO_MD5
  8197. case TLS_RSA_WITH_HC_128_MD5 :
  8198. return "TLS_RSA_WITH_HC_128_MD5";
  8199. #endif
  8200. #ifndef NO_SHA
  8201. case TLS_RSA_WITH_HC_128_SHA :
  8202. return "TLS_RSA_WITH_HC_128_SHA";
  8203. #endif
  8204. #ifdef HAVE_BLAKE2
  8205. case TLS_RSA_WITH_HC_128_B2B256:
  8206. return "TLS_RSA_WITH_HC_128_B2B256";
  8207. #endif
  8208. #endif /* NO_HC128 */
  8209. #ifndef NO_SHA
  8210. #ifndef NO_RABBIT
  8211. case TLS_RSA_WITH_RABBIT_SHA :
  8212. return "TLS_RSA_WITH_RABBIT_SHA";
  8213. #endif
  8214. #ifdef HAVE_NTRU
  8215. #ifndef NO_RC4
  8216. case TLS_NTRU_RSA_WITH_RC4_128_SHA :
  8217. return "TLS_NTRU_RSA_WITH_RC4_128_SHA";
  8218. #endif
  8219. #ifndef NO_DES3
  8220. case TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA :
  8221. return "TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA";
  8222. #endif
  8223. case TLS_NTRU_RSA_WITH_AES_128_CBC_SHA :
  8224. return "TLS_NTRU_RSA_WITH_AES_128_CBC_SHA";
  8225. case TLS_NTRU_RSA_WITH_AES_256_CBC_SHA :
  8226. return "TLS_NTRU_RSA_WITH_AES_256_CBC_SHA";
  8227. #endif /* HAVE_NTRU */
  8228. #ifdef HAVE_QSH
  8229. case TLS_QSH :
  8230. return "TLS_QSH";
  8231. #endif /* HAVE_QSH*/
  8232. #endif /* NO_SHA */
  8233. case TLS_RSA_WITH_AES_128_GCM_SHA256 :
  8234. return "TLS_RSA_WITH_AES_128_GCM_SHA256";
  8235. case TLS_RSA_WITH_AES_256_GCM_SHA384 :
  8236. return "TLS_RSA_WITH_AES_256_GCM_SHA384";
  8237. case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
  8238. return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
  8239. case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
  8240. return "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384";
  8241. #ifndef NO_SHA
  8242. case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA :
  8243. return "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA";
  8244. case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA :
  8245. return "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA";
  8246. #endif
  8247. case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
  8248. return "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256";
  8249. case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
  8250. return "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256";
  8251. #ifndef NO_SHA
  8252. case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA :
  8253. return "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA";
  8254. case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA :
  8255. return "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA";
  8256. #endif
  8257. case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
  8258. return "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256";
  8259. case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
  8260. return "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256";
  8261. #endif /* NO_RSA */
  8262. #ifdef BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
  8263. case TLS_DH_anon_WITH_AES_128_CBC_SHA :
  8264. return "TLS_DH_anon_WITH_AES_128_CBC_SHA";
  8265. #endif
  8266. default:
  8267. return "NONE";
  8268. } /* switch */
  8269. } /* normal / ECC */
  8270. }
  8271. #endif /* NO_ERROR_STRINGS */
  8272. return "NONE";
  8273. }
  8274. const char* wolfSSL_get_cipher(WOLFSSL* ssl)
  8275. {
  8276. WOLFSSL_ENTER("wolfSSL_get_cipher");
  8277. return wolfSSL_CIPHER_get_name(wolfSSL_get_current_cipher(ssl));
  8278. }
  8279. #ifdef OPENSSL_EXTRA
  8280. char* wolfSSL_CIPHER_description(WOLFSSL_CIPHER* cipher, char* in, int len)
  8281. {
  8282. (void)cipher;
  8283. (void)in;
  8284. (void)len;
  8285. return 0;
  8286. }
  8287. WOLFSSL_SESSION* wolfSSL_get1_session(WOLFSSL* ssl) /* what's ref count */
  8288. {
  8289. (void)ssl;
  8290. return 0;
  8291. }
  8292. void wolfSSL_X509_free(WOLFSSL_X509* x509)
  8293. {
  8294. WOLFSSL_ENTER("wolfSSL_X509_free");
  8295. ExternalFreeX509(x509);
  8296. }
  8297. /* was do nothing */
  8298. /*
  8299. void OPENSSL_free(void* buf)
  8300. {
  8301. (void)buf;
  8302. }
  8303. */
  8304. int wolfSSL_OCSP_parse_url(char* url, char** host, char** port, char** path,
  8305. int* ssl)
  8306. {
  8307. (void)url;
  8308. (void)host;
  8309. (void)port;
  8310. (void)path;
  8311. (void)ssl;
  8312. return 0;
  8313. }
  8314. WOLFSSL_METHOD* wolfSSLv2_client_method(void)
  8315. {
  8316. return 0;
  8317. }
  8318. WOLFSSL_METHOD* wolfSSLv2_server_method(void)
  8319. {
  8320. return 0;
  8321. }
  8322. #ifndef NO_MD4
  8323. void wolfSSL_MD4_Init(WOLFSSL_MD4_CTX* md4)
  8324. {
  8325. /* make sure we have a big enough buffer */
  8326. typedef char ok[sizeof(md4->buffer) >= sizeof(Md4) ? 1 : -1];
  8327. (void) sizeof(ok);
  8328. WOLFSSL_ENTER("MD4_Init");
  8329. wc_InitMd4((Md4*)md4);
  8330. }
  8331. void wolfSSL_MD4_Update(WOLFSSL_MD4_CTX* md4, const void* data,
  8332. unsigned long len)
  8333. {
  8334. WOLFSSL_ENTER("MD4_Update");
  8335. wc_Md4Update((Md4*)md4, (const byte*)data, (word32)len);
  8336. }
  8337. void wolfSSL_MD4_Final(unsigned char* digest, WOLFSSL_MD4_CTX* md4)
  8338. {
  8339. WOLFSSL_ENTER("MD4_Final");
  8340. wc_Md4Final((Md4*)md4, digest);
  8341. }
  8342. #endif /* NO_MD4 */
  8343. WOLFSSL_BIO* wolfSSL_BIO_pop(WOLFSSL_BIO* top)
  8344. {
  8345. (void)top;
  8346. return 0;
  8347. }
  8348. int wolfSSL_BIO_pending(WOLFSSL_BIO* bio)
  8349. {
  8350. (void)bio;
  8351. return 0;
  8352. }
  8353. WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_mem(void)
  8354. {
  8355. static WOLFSSL_BIO_METHOD meth;
  8356. WOLFSSL_ENTER("BIO_s_mem");
  8357. meth.type = BIO_MEMORY;
  8358. return &meth;
  8359. }
  8360. WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_base64(void)
  8361. {
  8362. return 0;
  8363. }
  8364. void wolfSSL_BIO_set_flags(WOLFSSL_BIO* bio, int flags)
  8365. {
  8366. (void)bio;
  8367. (void)flags;
  8368. }
  8369. void wolfSSL_RAND_screen(void)
  8370. {
  8371. }
  8372. const char* wolfSSL_RAND_file_name(char* fname, unsigned long len)
  8373. {
  8374. (void)fname;
  8375. (void)len;
  8376. return 0;
  8377. }
  8378. int wolfSSL_RAND_write_file(const char* fname)
  8379. {
  8380. (void)fname;
  8381. return 0;
  8382. }
  8383. int wolfSSL_RAND_load_file(const char* fname, long len)
  8384. {
  8385. (void)fname;
  8386. /* wolfCrypt provides enough entropy internally or will report error */
  8387. if (len == -1)
  8388. return 1024;
  8389. else
  8390. return (int)len;
  8391. }
  8392. int wolfSSL_RAND_egd(const char* path)
  8393. {
  8394. (void)path;
  8395. return 0;
  8396. }
  8397. WOLFSSL_COMP_METHOD* wolfSSL_COMP_zlib(void)
  8398. {
  8399. return 0;
  8400. }
  8401. WOLFSSL_COMP_METHOD* wolfSSL_COMP_rle(void)
  8402. {
  8403. return 0;
  8404. }
  8405. int wolfSSL_COMP_add_compression_method(int method, void* data)
  8406. {
  8407. (void)method;
  8408. (void)data;
  8409. return 0;
  8410. }
  8411. void wolfSSL_set_dynlock_create_callback(WOLFSSL_dynlock_value* (*f)(
  8412. const char*, int))
  8413. {
  8414. (void)f;
  8415. }
  8416. void wolfSSL_set_dynlock_lock_callback(
  8417. void (*f)(int, WOLFSSL_dynlock_value*, const char*, int))
  8418. {
  8419. (void)f;
  8420. }
  8421. void wolfSSL_set_dynlock_destroy_callback(
  8422. void (*f)(WOLFSSL_dynlock_value*, const char*, int))
  8423. {
  8424. (void)f;
  8425. }
  8426. const char* wolfSSL_X509_verify_cert_error_string(long err)
  8427. {
  8428. return wolfSSL_ERR_reason_error_string(err);
  8429. }
  8430. int wolfSSL_X509_LOOKUP_add_dir(WOLFSSL_X509_LOOKUP* lookup, const char* dir,
  8431. long len)
  8432. {
  8433. (void)lookup;
  8434. (void)dir;
  8435. (void)len;
  8436. return 0;
  8437. }
  8438. int wolfSSL_X509_LOOKUP_load_file(WOLFSSL_X509_LOOKUP* lookup,
  8439. const char* file, long len)
  8440. {
  8441. (void)lookup;
  8442. (void)file;
  8443. (void)len;
  8444. return 0;
  8445. }
  8446. WOLFSSL_X509_LOOKUP_METHOD* wolfSSL_X509_LOOKUP_hash_dir(void)
  8447. {
  8448. return 0;
  8449. }
  8450. WOLFSSL_X509_LOOKUP_METHOD* wolfSSL_X509_LOOKUP_file(void)
  8451. {
  8452. return 0;
  8453. }
  8454. WOLFSSL_X509_LOOKUP* wolfSSL_X509_STORE_add_lookup(WOLFSSL_X509_STORE* store,
  8455. WOLFSSL_X509_LOOKUP_METHOD* m)
  8456. {
  8457. (void)store;
  8458. (void)m;
  8459. return 0;
  8460. }
  8461. int wolfSSL_X509_STORE_add_cert(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509)
  8462. {
  8463. int result = SSL_FATAL_ERROR;
  8464. WOLFSSL_ENTER("wolfSSL_X509_STORE_add_cert");
  8465. if (store != NULL && store->cm != NULL && x509 != NULL) {
  8466. buffer derCert;
  8467. derCert.buffer = (byte*)XMALLOC(x509->derCert.length,
  8468. NULL, DYNAMIC_TYPE_CERT);
  8469. if (derCert.buffer != NULL) {
  8470. derCert.length = x509->derCert.length;
  8471. /* AddCA() frees the buffer. */
  8472. XMEMCPY(derCert.buffer,
  8473. x509->derCert.buffer, x509->derCert.length);
  8474. result = AddCA(store->cm, derCert, WOLFSSL_USER_CA, 1);
  8475. if (result != SSL_SUCCESS) result = SSL_FATAL_ERROR;
  8476. }
  8477. }
  8478. WOLFSSL_LEAVE("wolfSSL_X509_STORE_add_cert", result);
  8479. return result;
  8480. }
  8481. WOLFSSL_X509_STORE* wolfSSL_X509_STORE_new(void)
  8482. {
  8483. WOLFSSL_X509_STORE* store = NULL;
  8484. store = (WOLFSSL_X509_STORE*)XMALLOC(sizeof(WOLFSSL_X509_STORE), NULL, 0);
  8485. if (store != NULL) {
  8486. store->cm = wolfSSL_CertManagerNew();
  8487. if (store->cm == NULL) {
  8488. XFREE(store, NULL, 0);
  8489. store = NULL;
  8490. }
  8491. }
  8492. return store;
  8493. }
  8494. void wolfSSL_X509_STORE_free(WOLFSSL_X509_STORE* store)
  8495. {
  8496. if (store != NULL) {
  8497. if (store->cm != NULL)
  8498. wolfSSL_CertManagerFree(store->cm);
  8499. XFREE(store, NULL, 0);
  8500. }
  8501. }
  8502. int wolfSSL_X509_STORE_set_default_paths(WOLFSSL_X509_STORE* store)
  8503. {
  8504. (void)store;
  8505. return SSL_SUCCESS;
  8506. }
  8507. int wolfSSL_X509_STORE_get_by_subject(WOLFSSL_X509_STORE_CTX* ctx, int idx,
  8508. WOLFSSL_X509_NAME* name, WOLFSSL_X509_OBJECT* obj)
  8509. {
  8510. (void)ctx;
  8511. (void)idx;
  8512. (void)name;
  8513. (void)obj;
  8514. return 0;
  8515. }
  8516. WOLFSSL_X509_STORE_CTX* wolfSSL_X509_STORE_CTX_new(void)
  8517. {
  8518. WOLFSSL_X509_STORE_CTX* ctx = (WOLFSSL_X509_STORE_CTX*)XMALLOC(
  8519. sizeof(WOLFSSL_X509_STORE_CTX), NULL, 0);
  8520. if (ctx != NULL)
  8521. wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL);
  8522. return ctx;
  8523. }
  8524. int wolfSSL_X509_STORE_CTX_init(WOLFSSL_X509_STORE_CTX* ctx,
  8525. WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509, STACK_OF(WOLFSSL_X509)* sk)
  8526. {
  8527. (void)sk;
  8528. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_init");
  8529. if (ctx != NULL) {
  8530. ctx->store = store;
  8531. ctx->current_cert = x509;
  8532. ctx->domain = NULL;
  8533. ctx->ex_data = NULL;
  8534. ctx->userCtx = NULL;
  8535. ctx->error = 0;
  8536. ctx->error_depth = 0;
  8537. ctx->discardSessionCerts = 0;
  8538. return SSL_SUCCESS;
  8539. }
  8540. return SSL_FATAL_ERROR;
  8541. }
  8542. void wolfSSL_X509_STORE_CTX_free(WOLFSSL_X509_STORE_CTX* ctx)
  8543. {
  8544. if (ctx != NULL) {
  8545. if (ctx->store != NULL)
  8546. wolfSSL_X509_STORE_free(ctx->store);
  8547. if (ctx->current_cert != NULL)
  8548. wolfSSL_FreeX509(ctx->current_cert);
  8549. XFREE(ctx, NULL, 0);
  8550. }
  8551. }
  8552. void wolfSSL_X509_STORE_CTX_cleanup(WOLFSSL_X509_STORE_CTX* ctx)
  8553. {
  8554. (void)ctx;
  8555. }
  8556. int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
  8557. {
  8558. if (ctx != NULL && ctx->store != NULL && ctx->store->cm != NULL
  8559. && ctx->current_cert != NULL) {
  8560. return wolfSSL_CertManagerVerifyBuffer(ctx->store->cm,
  8561. ctx->current_cert->derCert.buffer,
  8562. ctx->current_cert->derCert.length,
  8563. SSL_FILETYPE_ASN1);
  8564. }
  8565. return SSL_FATAL_ERROR;
  8566. }
  8567. WOLFSSL_ASN1_TIME* wolfSSL_X509_CRL_get_lastUpdate(WOLFSSL_X509_CRL* crl)
  8568. {
  8569. (void)crl;
  8570. return 0;
  8571. }
  8572. WOLFSSL_ASN1_TIME* wolfSSL_X509_CRL_get_nextUpdate(WOLFSSL_X509_CRL* crl)
  8573. {
  8574. (void)crl;
  8575. return 0;
  8576. }
  8577. WOLFSSL_EVP_PKEY* wolfSSL_X509_get_pubkey(WOLFSSL_X509* x509)
  8578. {
  8579. WOLFSSL_EVP_PKEY* key = NULL;
  8580. if (x509 != NULL) {
  8581. key = (WOLFSSL_EVP_PKEY*)XMALLOC(
  8582. sizeof(WOLFSSL_EVP_PKEY), NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  8583. if (key != NULL) {
  8584. key->type = x509->pubKeyOID;
  8585. key->save_type = 0;
  8586. key->pkey.ptr = (char*)XMALLOC(
  8587. x509->pubKey.length, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  8588. if (key->pkey.ptr == NULL) {
  8589. XFREE(key, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  8590. return NULL;
  8591. }
  8592. XMEMCPY(key->pkey.ptr,
  8593. x509->pubKey.buffer, x509->pubKey.length);
  8594. key->pkey_sz = x509->pubKey.length;
  8595. #ifdef HAVE_ECC
  8596. key->pkey_curve = (int)x509->pkCurveOID;
  8597. #endif /* HAVE_ECC */
  8598. }
  8599. }
  8600. return key;
  8601. }
  8602. int wolfSSL_X509_CRL_verify(WOLFSSL_X509_CRL* crl, WOLFSSL_EVP_PKEY* key)
  8603. {
  8604. (void)crl;
  8605. (void)key;
  8606. return 0;
  8607. }
  8608. void wolfSSL_X509_STORE_CTX_set_error(WOLFSSL_X509_STORE_CTX* ctx, int err)
  8609. {
  8610. (void)ctx;
  8611. (void)err;
  8612. }
  8613. void wolfSSL_X509_OBJECT_free_contents(WOLFSSL_X509_OBJECT* obj)
  8614. {
  8615. (void)obj;
  8616. }
  8617. void wolfSSL_EVP_PKEY_free(WOLFSSL_EVP_PKEY* key)
  8618. {
  8619. if (key != NULL) {
  8620. if (key->pkey.ptr != NULL)
  8621. XFREE(key->pkey.ptr, NULL, 0);
  8622. XFREE(key, NULL, 0);
  8623. }
  8624. }
  8625. int wolfSSL_X509_cmp_current_time(const WOLFSSL_ASN1_TIME* asnTime)
  8626. {
  8627. (void)asnTime;
  8628. return 0;
  8629. }
  8630. int wolfSSL_sk_X509_REVOKED_num(WOLFSSL_X509_REVOKED* revoked)
  8631. {
  8632. (void)revoked;
  8633. return 0;
  8634. }
  8635. WOLFSSL_X509_REVOKED* wolfSSL_X509_CRL_get_REVOKED(WOLFSSL_X509_CRL* crl)
  8636. {
  8637. (void)crl;
  8638. return 0;
  8639. }
  8640. WOLFSSL_X509_REVOKED* wolfSSL_sk_X509_REVOKED_value(
  8641. WOLFSSL_X509_REVOKED* revoked, int value)
  8642. {
  8643. (void)revoked;
  8644. (void)value;
  8645. return 0;
  8646. }
  8647. WOLFSSL_ASN1_INTEGER* wolfSSL_X509_get_serialNumber(WOLFSSL_X509* x509)
  8648. {
  8649. (void)x509;
  8650. return 0;
  8651. }
  8652. int wolfSSL_ASN1_TIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_TIME* asnTime)
  8653. {
  8654. (void)bio;
  8655. (void)asnTime;
  8656. return 0;
  8657. }
  8658. int wolfSSL_ASN1_INTEGER_cmp(const WOLFSSL_ASN1_INTEGER* a,
  8659. const WOLFSSL_ASN1_INTEGER* b)
  8660. {
  8661. (void)a;
  8662. (void)b;
  8663. return 0;
  8664. }
  8665. long wolfSSL_ASN1_INTEGER_get(const WOLFSSL_ASN1_INTEGER* i)
  8666. {
  8667. (void)i;
  8668. return 0;
  8669. }
  8670. void* wolfSSL_X509_STORE_CTX_get_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx)
  8671. {
  8672. WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_ex_data");
  8673. #if defined(FORTRESS) || defined(HAVE_STUNNEL)
  8674. if (ctx != NULL && idx == 0)
  8675. return ctx->ex_data;
  8676. #else
  8677. (void)ctx;
  8678. (void)idx;
  8679. #endif
  8680. return 0;
  8681. }
  8682. int wolfSSL_get_ex_data_X509_STORE_CTX_idx(void)
  8683. {
  8684. WOLFSSL_ENTER("wolfSSL_get_ex_data_X509_STORE_CTX_idx");
  8685. return 0;
  8686. }
  8687. void wolfSSL_CTX_set_info_callback(WOLFSSL_CTX* ctx,
  8688. void (*f)(const WOLFSSL* ssl, int type, int val))
  8689. {
  8690. (void)ctx;
  8691. (void)f;
  8692. }
  8693. unsigned long wolfSSL_ERR_peek_error(void)
  8694. {
  8695. return 0;
  8696. }
  8697. int wolfSSL_ERR_GET_REASON(unsigned long err)
  8698. {
  8699. (void)err;
  8700. return 0;
  8701. }
  8702. char* wolfSSL_alert_type_string_long(int alertID)
  8703. {
  8704. (void)alertID;
  8705. return 0;
  8706. }
  8707. char* wolfSSL_alert_desc_string_long(int alertID)
  8708. {
  8709. (void)alertID;
  8710. return 0;
  8711. }
  8712. char* wolfSSL_state_string_long(const WOLFSSL* ssl)
  8713. {
  8714. (void)ssl;
  8715. return 0;
  8716. }
  8717. int wolfSSL_PEM_def_callback(char* name, int num, int w, void* key)
  8718. {
  8719. (void)name;
  8720. (void)num;
  8721. (void)w;
  8722. (void)key;
  8723. return 0;
  8724. }
  8725. long wolfSSL_CTX_sess_accept(WOLFSSL_CTX* ctx)
  8726. {
  8727. (void)ctx;
  8728. return 0;
  8729. }
  8730. long wolfSSL_CTX_sess_connect(WOLFSSL_CTX* ctx)
  8731. {
  8732. (void)ctx;
  8733. return 0;
  8734. }
  8735. long wolfSSL_CTX_sess_accept_good(WOLFSSL_CTX* ctx)
  8736. {
  8737. (void)ctx;
  8738. return 0;
  8739. }
  8740. long wolfSSL_CTX_sess_connect_good(WOLFSSL_CTX* ctx)
  8741. {
  8742. (void)ctx;
  8743. return 0;
  8744. }
  8745. long wolfSSL_CTX_sess_accept_renegotiate(WOLFSSL_CTX* ctx)
  8746. {
  8747. (void)ctx;
  8748. return 0;
  8749. }
  8750. long wolfSSL_CTX_sess_connect_renegotiate(WOLFSSL_CTX* ctx)
  8751. {
  8752. (void)ctx;
  8753. return 0;
  8754. }
  8755. long wolfSSL_CTX_sess_hits(WOLFSSL_CTX* ctx)
  8756. {
  8757. (void)ctx;
  8758. return 0;
  8759. }
  8760. long wolfSSL_CTX_sess_cb_hits(WOLFSSL_CTX* ctx)
  8761. {
  8762. (void)ctx;
  8763. return 0;
  8764. }
  8765. long wolfSSL_CTX_sess_cache_full(WOLFSSL_CTX* ctx)
  8766. {
  8767. (void)ctx;
  8768. return 0;
  8769. }
  8770. long wolfSSL_CTX_sess_misses(WOLFSSL_CTX* ctx)
  8771. {
  8772. (void)ctx;
  8773. return 0;
  8774. }
  8775. long wolfSSL_CTX_sess_timeouts(WOLFSSL_CTX* ctx)
  8776. {
  8777. (void)ctx;
  8778. return 0;
  8779. }
  8780. long wolfSSL_CTX_sess_number(WOLFSSL_CTX* ctx)
  8781. {
  8782. (void)ctx;
  8783. return 0;
  8784. }
  8785. #ifndef NO_DES3
  8786. void wolfSSL_DES_set_key_unchecked(WOLFSSL_const_DES_cblock* myDes,
  8787. WOLFSSL_DES_key_schedule* key)
  8788. {
  8789. (void)myDes;
  8790. (void)key;
  8791. }
  8792. void wolfSSL_DES_set_odd_parity(WOLFSSL_DES_cblock* myDes)
  8793. {
  8794. (void)myDes;
  8795. }
  8796. void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* desa,
  8797. WOLFSSL_DES_cblock* desb, WOLFSSL_DES_key_schedule* key, int len)
  8798. {
  8799. (void)desa;
  8800. (void)desb;
  8801. (void)key;
  8802. (void)len;
  8803. }
  8804. #endif /* NO_DES3 */
  8805. int wolfSSL_BIO_printf(WOLFSSL_BIO* bio, const char* format, ...)
  8806. {
  8807. (void)bio;
  8808. (void)format;
  8809. return 0;
  8810. }
  8811. int wolfSSL_ASN1_UTCTIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_UTCTIME* a)
  8812. {
  8813. (void)bio;
  8814. (void)a;
  8815. return 0;
  8816. }
  8817. int wolfSSL_sk_num(WOLFSSL_X509_REVOKED* rev)
  8818. {
  8819. (void)rev;
  8820. return 0;
  8821. }
  8822. void* wolfSSL_sk_value(WOLFSSL_X509_REVOKED* rev, int i)
  8823. {
  8824. (void)rev;
  8825. (void)i;
  8826. return 0;
  8827. }
  8828. /* stunnel 4.28 needs */
  8829. void wolfSSL_CTX_sess_set_get_cb(WOLFSSL_CTX* ctx,
  8830. WOLFSSL_SESSION*(*f)(WOLFSSL*, unsigned char*, int, int*))
  8831. {
  8832. (void)ctx;
  8833. (void)f;
  8834. }
  8835. void wolfSSL_CTX_sess_set_new_cb(WOLFSSL_CTX* ctx,
  8836. int (*f)(WOLFSSL*, WOLFSSL_SESSION*))
  8837. {
  8838. (void)ctx;
  8839. (void)f;
  8840. }
  8841. void wolfSSL_CTX_sess_set_remove_cb(WOLFSSL_CTX* ctx, void (*f)(WOLFSSL_CTX*,
  8842. WOLFSSL_SESSION*))
  8843. {
  8844. (void)ctx;
  8845. (void)f;
  8846. }
  8847. int wolfSSL_i2d_SSL_SESSION(WOLFSSL_SESSION* sess, unsigned char** p)
  8848. {
  8849. (void)sess;
  8850. (void)p;
  8851. return sizeof(WOLFSSL_SESSION);
  8852. }
  8853. WOLFSSL_SESSION* wolfSSL_d2i_SSL_SESSION(WOLFSSL_SESSION** sess,
  8854. const unsigned char** p, long i)
  8855. {
  8856. (void)p;
  8857. (void)i;
  8858. if (sess)
  8859. return *sess;
  8860. return NULL;
  8861. }
  8862. long wolfSSL_SESSION_get_timeout(const WOLFSSL_SESSION* sess)
  8863. {
  8864. WOLFSSL_ENTER("wolfSSL_SESSION_get_timeout");
  8865. return sess->timeout;
  8866. }
  8867. long wolfSSL_SESSION_get_time(const WOLFSSL_SESSION* sess)
  8868. {
  8869. WOLFSSL_ENTER("wolfSSL_SESSION_get_time");
  8870. return sess->bornOn;
  8871. }
  8872. #endif /* OPENSSL_EXTRA */
  8873. #ifdef KEEP_PEER_CERT
  8874. char* wolfSSL_X509_get_subjectCN(WOLFSSL_X509* x509)
  8875. {
  8876. if (x509 == NULL)
  8877. return NULL;
  8878. return x509->subjectCN;
  8879. }
  8880. #endif /* KEEP_PEER_CERT */
  8881. #ifdef OPENSSL_EXTRA
  8882. #ifdef FORTRESS
  8883. int wolfSSL_cmp_peer_cert_to_file(WOLFSSL* ssl, const char *fname)
  8884. {
  8885. int ret = SSL_FATAL_ERROR;
  8886. WOLFSSL_ENTER("wolfSSL_cmp_peer_cert_to_file");
  8887. if (ssl != NULL && fname != NULL)
  8888. {
  8889. #ifdef WOLFSSL_SMALL_STACK
  8890. EncryptedInfo* info = NULL;
  8891. byte staticBuffer[1]; /* force heap usage */
  8892. #else
  8893. EncryptedInfo info[1];
  8894. byte staticBuffer[FILE_BUFFER_SIZE];
  8895. #endif
  8896. byte* myBuffer = staticBuffer;
  8897. int dynamic = 0;
  8898. XFILE file = XBADFILE;
  8899. long sz = 0;
  8900. int eccKey = 0;
  8901. WOLFSSL_CTX* ctx = ssl->ctx;
  8902. WOLFSSL_X509* peer_cert = &ssl->peerCert;
  8903. buffer fileDer;
  8904. fileDer.buffer = 0;
  8905. file = XFOPEN(fname, "rb");
  8906. if (file == XBADFILE)
  8907. return SSL_BAD_FILE;
  8908. XFSEEK(file, 0, XSEEK_END);
  8909. sz = XFTELL(file);
  8910. XREWIND(file);
  8911. if (sz > (long)sizeof(staticBuffer)) {
  8912. WOLFSSL_MSG("Getting dynamic buffer");
  8913. myBuffer = (byte*)XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
  8914. dynamic = 1;
  8915. }
  8916. #ifdef WOLFSSL_SMALL_STACK
  8917. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  8918. DYNAMIC_TYPE_TMP_BUFFER);
  8919. if (info == NULL)
  8920. ret = MEMORY_E;
  8921. else
  8922. #endif
  8923. {
  8924. info->set = 0;
  8925. info->ctx = ctx;
  8926. info->consumed = 0;
  8927. if ((myBuffer != NULL) &&
  8928. (sz > 0) &&
  8929. (XFREAD(myBuffer, sz, 1, file) > 0) &&
  8930. (PemToDer(myBuffer, sz, CERT_TYPE,
  8931. &fileDer, ctx->heap, info, &eccKey) == 0) &&
  8932. (fileDer.length != 0) &&
  8933. (fileDer.length == peer_cert->derCert.length) &&
  8934. (XMEMCMP(peer_cert->derCert.buffer, fileDer.buffer,
  8935. fileDer.length) == 0))
  8936. {
  8937. ret = 0;
  8938. }
  8939. #ifdef WOLFSSL_SMALL_STACK
  8940. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8941. #endif
  8942. }
  8943. XFREE(fileDer.buffer, ctx->heap, DYNAMIC_TYPE_CERT);
  8944. if (dynamic)
  8945. XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
  8946. XFCLOSE(file);
  8947. }
  8948. return ret;
  8949. }
  8950. #endif
  8951. static WC_RNG globalRNG;
  8952. static int initGlobalRNG = 0;
  8953. /* SSL_SUCCESS on ok */
  8954. int wolfSSL_RAND_seed(const void* seed, int len)
  8955. {
  8956. WOLFSSL_MSG("wolfSSL_RAND_seed");
  8957. (void)seed;
  8958. (void)len;
  8959. if (initGlobalRNG == 0) {
  8960. if (wc_InitRng(&globalRNG) < 0) {
  8961. WOLFSSL_MSG("wolfSSL Init Global RNG failed");
  8962. return 0;
  8963. }
  8964. initGlobalRNG = 1;
  8965. }
  8966. return SSL_SUCCESS;
  8967. }
  8968. /* SSL_SUCCESS on ok */
  8969. int wolfSSL_RAND_bytes(unsigned char* buf, int num)
  8970. {
  8971. int ret = 0;
  8972. int initTmpRng = 0;
  8973. WC_RNG* rng = NULL;
  8974. #ifdef WOLFSSL_SMALL_STACK
  8975. WC_RNG* tmpRNG = NULL;
  8976. #else
  8977. WC_RNG tmpRNG[1];
  8978. #endif
  8979. WOLFSSL_ENTER("wolfSSL_RAND_bytes");
  8980. #ifdef WOLFSSL_SMALL_STACK
  8981. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8982. if (tmpRNG == NULL)
  8983. return ret;
  8984. #endif
  8985. if (wc_InitRng(tmpRNG) == 0) {
  8986. rng = tmpRNG;
  8987. initTmpRng = 1;
  8988. }
  8989. else if (initGlobalRNG)
  8990. rng = &globalRNG;
  8991. if (rng) {
  8992. if (wc_RNG_GenerateBlock(rng, buf, num) != 0)
  8993. WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
  8994. else
  8995. ret = SSL_SUCCESS;
  8996. }
  8997. if (initTmpRng)
  8998. wc_FreeRng(tmpRNG);
  8999. #ifdef WOLFSSL_SMALL_STACK
  9000. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9001. #endif
  9002. return ret;
  9003. }
  9004. WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void)
  9005. {
  9006. static int ctx; /* wolfcrypt doesn't now need ctx */
  9007. WOLFSSL_MSG("wolfSSL_BN_CTX_new");
  9008. return (WOLFSSL_BN_CTX*)&ctx;
  9009. }
  9010. void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
  9011. {
  9012. (void)ctx;
  9013. WOLFSSL_MSG("wolfSSL_BN_CTX_init");
  9014. }
  9015. void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx)
  9016. {
  9017. (void)ctx;
  9018. WOLFSSL_MSG("wolfSSL_BN_CTX_free");
  9019. /* do free since static ctx that does nothing */
  9020. }
  9021. static void InitwolfSSL_BigNum(WOLFSSL_BIGNUM* bn)
  9022. {
  9023. WOLFSSL_MSG("InitwolfSSL_BigNum");
  9024. if (bn) {
  9025. bn->neg = 0;
  9026. bn->internal = NULL;
  9027. }
  9028. }
  9029. WOLFSSL_BIGNUM* wolfSSL_BN_new(void)
  9030. {
  9031. WOLFSSL_BIGNUM* external;
  9032. mp_int* mpi;
  9033. WOLFSSL_MSG("wolfSSL_BN_new");
  9034. mpi = (mp_int*) XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9035. if (mpi == NULL) {
  9036. WOLFSSL_MSG("wolfSSL_BN_new malloc mpi failure");
  9037. return NULL;
  9038. }
  9039. external = (WOLFSSL_BIGNUM*) XMALLOC(sizeof(WOLFSSL_BIGNUM), NULL,
  9040. DYNAMIC_TYPE_BIGINT);
  9041. if (external == NULL) {
  9042. WOLFSSL_MSG("wolfSSL_BN_new malloc WOLFSSL_BIGNUM failure");
  9043. XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
  9044. return NULL;
  9045. }
  9046. InitwolfSSL_BigNum(external);
  9047. external->internal = mpi;
  9048. if (mp_init(mpi) != MP_OKAY) {
  9049. wolfSSL_BN_free(external);
  9050. return NULL;
  9051. }
  9052. return external;
  9053. }
  9054. void wolfSSL_BN_free(WOLFSSL_BIGNUM* bn)
  9055. {
  9056. WOLFSSL_MSG("wolfSSL_BN_free");
  9057. if (bn) {
  9058. if (bn->internal) {
  9059. mp_clear((mp_int*)bn->internal);
  9060. XFREE(bn->internal, NULL, DYNAMIC_TYPE_BIGINT);
  9061. bn->internal = NULL;
  9062. }
  9063. XFREE(bn, NULL, DYNAMIC_TYPE_BIGINT);
  9064. bn = NULL;
  9065. }
  9066. }
  9067. void wolfSSL_BN_clear_free(WOLFSSL_BIGNUM* bn)
  9068. {
  9069. WOLFSSL_MSG("wolfSSL_BN_clear_free");
  9070. wolfSSL_BN_free(bn);
  9071. }
  9072. /* SSL_SUCCESS on ok */
  9073. int wolfSSL_BN_sub(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
  9074. const WOLFSSL_BIGNUM* b)
  9075. {
  9076. WOLFSSL_MSG("wolfSSL_BN_sub");
  9077. if (r == NULL || a == NULL || b == NULL)
  9078. return 0;
  9079. if (mp_sub((mp_int*)a->internal,(mp_int*)b->internal,
  9080. (mp_int*)r->internal) == MP_OKAY)
  9081. return SSL_SUCCESS;
  9082. WOLFSSL_MSG("wolfSSL_BN_sub mp_sub failed");
  9083. return 0;
  9084. }
  9085. /* SSL_SUCCESS on ok */
  9086. int wolfSSL_BN_mod(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
  9087. const WOLFSSL_BIGNUM* b, const WOLFSSL_BN_CTX* c)
  9088. {
  9089. (void)c;
  9090. WOLFSSL_MSG("wolfSSL_BN_mod");
  9091. if (r == NULL || a == NULL || b == NULL)
  9092. return 0;
  9093. if (mp_mod((mp_int*)a->internal,(mp_int*)b->internal,
  9094. (mp_int*)r->internal) == MP_OKAY)
  9095. return SSL_SUCCESS;
  9096. WOLFSSL_MSG("wolfSSL_BN_mod mp_mod failed");
  9097. return 0;
  9098. }
  9099. const WOLFSSL_BIGNUM* wolfSSL_BN_value_one(void)
  9100. {
  9101. static WOLFSSL_BIGNUM* bn_one = NULL;
  9102. WOLFSSL_MSG("wolfSSL_BN_value_one");
  9103. if (bn_one == NULL) {
  9104. bn_one = wolfSSL_BN_new();
  9105. if (bn_one)
  9106. mp_set_int((mp_int*)bn_one->internal, 1);
  9107. }
  9108. return bn_one;
  9109. }
  9110. /* return compliant with OpenSSL
  9111. * size of BIGNUM in bytes, 0 if error */
  9112. int wolfSSL_BN_num_bytes(const WOLFSSL_BIGNUM* bn)
  9113. {
  9114. WOLFSSL_MSG("wolfSSL_BN_num_bytes");
  9115. if (bn == NULL || bn->internal == NULL)
  9116. return SSL_FAILURE;
  9117. return mp_unsigned_bin_size((mp_int*)bn->internal);
  9118. }
  9119. /* return compliant with OpenSSL
  9120. * size of BIGNUM in bits, 0 if error */
  9121. int wolfSSL_BN_num_bits(const WOLFSSL_BIGNUM* bn)
  9122. {
  9123. WOLFSSL_MSG("wolfSSL_BN_num_bits");
  9124. if (bn == NULL || bn->internal == NULL)
  9125. return SSL_FAILURE;
  9126. return mp_count_bits((mp_int*)bn->internal);
  9127. }
  9128. /* return compliant with OpenSSL
  9129. * 1 if BIGNUM is zero, 0 else */
  9130. int wolfSSL_BN_is_zero(const WOLFSSL_BIGNUM* bn)
  9131. {
  9132. WOLFSSL_MSG("wolfSSL_BN_is_zero");
  9133. if (bn == NULL || bn->internal == NULL)
  9134. return SSL_FAILURE;
  9135. if (mp_iszero((mp_int*)bn->internal) == MP_YES)
  9136. return SSL_SUCCESS;
  9137. return SSL_FAILURE;
  9138. }
  9139. /* return compliant with OpenSSL
  9140. * 1 if BIGNUM is one, 0 else */
  9141. int wolfSSL_BN_is_one(const WOLFSSL_BIGNUM* bn)
  9142. {
  9143. WOLFSSL_MSG("wolfSSL_BN_is_one");
  9144. if (bn == NULL || bn->internal == NULL)
  9145. return SSL_FAILURE;
  9146. if (mp_cmp_d((mp_int*)bn->internal, 1) == MP_EQ)
  9147. return SSL_SUCCESS;
  9148. return SSL_FAILURE;
  9149. }
  9150. /* return compliant with OpenSSL
  9151. * 1 if BIGNUM is odd, 0 else */
  9152. int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn)
  9153. {
  9154. WOLFSSL_MSG("wolfSSL_BN_is_odd");
  9155. if (bn == NULL || bn->internal == NULL)
  9156. return SSL_FAILURE;
  9157. if (mp_isodd((mp_int*)bn->internal) == MP_YES)
  9158. return SSL_SUCCESS;
  9159. return SSL_FAILURE;
  9160. }
  9161. /* return compliant with OpenSSL
  9162. * -1 if a < b, 0 if a == b and 1 if a > b
  9163. */
  9164. int wolfSSL_BN_cmp(const WOLFSSL_BIGNUM* a, const WOLFSSL_BIGNUM* b)
  9165. {
  9166. int ret;
  9167. WOLFSSL_MSG("wolfSSL_BN_cmp");
  9168. if (a == NULL || a->internal == NULL || b == NULL || b->internal == NULL)
  9169. return SSL_FATAL_ERROR;
  9170. ret = mp_cmp((mp_int*)a->internal, (mp_int*)b->internal);
  9171. return (ret == MP_EQ ? 0 : (ret == MP_GT ? 1 : -1));
  9172. }
  9173. /* return compliant with OpenSSL
  9174. * length of BIGNUM in bytes, -1 if error */
  9175. int wolfSSL_BN_bn2bin(const WOLFSSL_BIGNUM* bn, unsigned char* r)
  9176. {
  9177. WOLFSSL_MSG("wolfSSL_BN_bn2bin");
  9178. if (bn == NULL || bn->internal == NULL) {
  9179. WOLFSSL_MSG("NULL bn error");
  9180. return SSL_FATAL_ERROR;
  9181. }
  9182. if (r == NULL)
  9183. return mp_unsigned_bin_size((mp_int*)bn->internal);
  9184. if (mp_to_unsigned_bin((mp_int*)bn->internal, r) != MP_OKAY) {
  9185. WOLFSSL_MSG("mp_to_unsigned_bin error");
  9186. return SSL_FATAL_ERROR;
  9187. }
  9188. return mp_unsigned_bin_size((mp_int*)bn->internal);
  9189. }
  9190. WOLFSSL_BIGNUM* wolfSSL_BN_bin2bn(const unsigned char* str, int len,
  9191. WOLFSSL_BIGNUM* ret)
  9192. {
  9193. int weOwn = 0;
  9194. WOLFSSL_MSG("wolfSSL_BN_bin2bn");
  9195. /* if ret is null create a BN */
  9196. if (ret == NULL) {
  9197. ret = wolfSSL_BN_new();
  9198. weOwn = 1;
  9199. if (ret == NULL)
  9200. return NULL;
  9201. }
  9202. /* check ret and ret->internal then read in value */
  9203. if (ret && ret->internal) {
  9204. if (mp_read_unsigned_bin((mp_int*)ret->internal, str, len) != 0) {
  9205. WOLFSSL_MSG("mp_read_unsigned_bin failure");
  9206. if (weOwn)
  9207. wolfSSL_BN_free(ret);
  9208. return NULL;
  9209. }
  9210. }
  9211. return ret;
  9212. }
  9213. /* return compliant with OpenSSL
  9214. * 1 if success, 0 if error */
  9215. int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n)
  9216. {
  9217. (void)bn;
  9218. (void)n;
  9219. WOLFSSL_MSG("wolfSSL_BN_mask_bits");
  9220. return SSL_FAILURE;
  9221. }
  9222. /* SSL_SUCCESS on ok */
  9223. int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
  9224. {
  9225. int ret = 0;
  9226. int len = bits / 8;
  9227. int initTmpRng = 0;
  9228. WC_RNG* rng = NULL;
  9229. #ifdef WOLFSSL_SMALL_STACK
  9230. WC_RNG* tmpRNG = NULL;
  9231. byte* buff = NULL;
  9232. #else
  9233. WC_RNG tmpRNG[1];
  9234. byte buff[1024];
  9235. #endif
  9236. (void)top;
  9237. (void)bottom;
  9238. WOLFSSL_MSG("wolfSSL_BN_rand");
  9239. if (bits % 8)
  9240. len++;
  9241. #ifdef WOLFSSL_SMALL_STACK
  9242. buff = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9243. tmpRNG = (WC_RNG*) XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9244. if (buff == NULL || tmpRNG == NULL) {
  9245. XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9246. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9247. return ret;
  9248. }
  9249. #endif
  9250. if (bn == NULL || bn->internal == NULL)
  9251. WOLFSSL_MSG("Bad function arguments");
  9252. else if (wc_InitRng(tmpRNG) == 0) {
  9253. rng = tmpRNG;
  9254. initTmpRng = 1;
  9255. }
  9256. else if (initGlobalRNG)
  9257. rng = &globalRNG;
  9258. if (rng) {
  9259. if (wc_RNG_GenerateBlock(rng, buff, len) != 0)
  9260. WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
  9261. else {
  9262. buff[0] |= 0x80 | 0x40;
  9263. buff[len-1] |= 0x01;
  9264. if (mp_read_unsigned_bin((mp_int*)bn->internal,buff,len) != MP_OKAY)
  9265. WOLFSSL_MSG("mp read bin failed");
  9266. else
  9267. ret = SSL_SUCCESS;
  9268. }
  9269. }
  9270. if (initTmpRng)
  9271. wc_FreeRng(tmpRNG);
  9272. #ifdef WOLFSSL_SMALL_STACK
  9273. XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9274. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9275. #endif
  9276. return ret;
  9277. }
  9278. /* return code compliant with OpenSSL :
  9279. * 1 if bit set, 0 else
  9280. */
  9281. int wolfSSL_BN_is_bit_set(const WOLFSSL_BIGNUM* bn, int n)
  9282. {
  9283. if (bn == NULL || bn->internal == NULL) {
  9284. WOLFSSL_MSG("bn NULL error");
  9285. return SSL_FAILURE;
  9286. }
  9287. return mp_is_bit_set((mp_int*)bn->internal, n);
  9288. }
  9289. /* return code compliant with OpenSSL :
  9290. * 1 if success, 0 else
  9291. */
  9292. int wolfSSL_BN_set_bit(WOLFSSL_BIGNUM* bn, int n)
  9293. {
  9294. if (bn == NULL || bn->internal == NULL) {
  9295. WOLFSSL_MSG("bn NULL error");
  9296. return SSL_FAILURE;
  9297. }
  9298. if (mp_set_bit((mp_int*)bn->internal, n) != MP_OKAY) {
  9299. WOLFSSL_MSG("mp_set_int error");
  9300. return SSL_FAILURE;
  9301. }
  9302. return SSL_SUCCESS;
  9303. }
  9304. /* SSL_SUCCESS on ok */
  9305. int wolfSSL_BN_hex2bn(WOLFSSL_BIGNUM** bn, const char* str)
  9306. {
  9307. int ret = 0;
  9308. word32 decSz = 1024;
  9309. #ifdef WOLFSSL_SMALL_STACK
  9310. byte* decoded = NULL;
  9311. #else
  9312. byte decoded[1024];
  9313. #endif
  9314. WOLFSSL_MSG("wolfSSL_BN_hex2bn");
  9315. #ifdef WOLFSSL_SMALL_STACK
  9316. decoded = (byte*)XMALLOC(decSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9317. if (decoded == NULL)
  9318. return ret;
  9319. #endif
  9320. if (str == NULL)
  9321. WOLFSSL_MSG("Bad function argument");
  9322. else if (Base16_Decode((byte*)str, (int)XSTRLEN(str), decoded, &decSz) < 0)
  9323. WOLFSSL_MSG("Bad Base16_Decode error");
  9324. else if (bn == NULL)
  9325. ret = decSz;
  9326. else {
  9327. if (*bn == NULL)
  9328. *bn = wolfSSL_BN_new();
  9329. if (*bn == NULL)
  9330. WOLFSSL_MSG("BN new failed");
  9331. else if (wolfSSL_BN_bin2bn(decoded, decSz, *bn) == NULL)
  9332. WOLFSSL_MSG("Bad bin2bn error");
  9333. else
  9334. ret = SSL_SUCCESS;
  9335. }
  9336. #ifdef WOLFSSL_SMALL_STACK
  9337. XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9338. #endif
  9339. return ret;
  9340. }
  9341. WOLFSSL_BIGNUM* wolfSSL_BN_dup(const WOLFSSL_BIGNUM* bn)
  9342. {
  9343. WOLFSSL_BIGNUM* ret;
  9344. WOLFSSL_MSG("wolfSSL_BN_dup");
  9345. if (bn == NULL || bn->internal == NULL) {
  9346. WOLFSSL_MSG("bn NULL error");
  9347. return NULL;
  9348. }
  9349. ret = wolfSSL_BN_new();
  9350. if (ret == NULL) {
  9351. WOLFSSL_MSG("bn new error");
  9352. return NULL;
  9353. }
  9354. if (mp_copy((mp_int*)bn->internal, (mp_int*)ret->internal) != MP_OKAY) {
  9355. WOLFSSL_MSG("mp_copy error");
  9356. wolfSSL_BN_free(ret);
  9357. return NULL;
  9358. }
  9359. ret->neg = bn->neg;
  9360. return ret;
  9361. }
  9362. WOLFSSL_BIGNUM* wolfSSL_BN_copy(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* bn)
  9363. {
  9364. WOLFSSL_MSG("wolfSSL_BN_copy");
  9365. if (mp_copy((mp_int*)bn->internal, (mp_int*)r->internal) != MP_OKAY) {
  9366. WOLFSSL_MSG("mp_copy error");
  9367. return NULL;
  9368. }
  9369. r->neg = bn->neg;
  9370. return r;
  9371. }
  9372. /* return code compliant with OpenSSL :
  9373. * 1 if success, 0 else
  9374. */
  9375. int wolfSSL_BN_set_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w)
  9376. {
  9377. WOLFSSL_MSG("wolfSSL_BN_set_word");
  9378. if (mp_set_int((mp_int*)bn->internal, w) != MP_OKAY) {
  9379. WOLFSSL_MSG("mp_init_set_int error");
  9380. return SSL_FAILURE;
  9381. }
  9382. return SSL_SUCCESS;
  9383. }
  9384. /* return code compliant with OpenSSL :
  9385. * number length in decimal if success, 0 if error
  9386. */
  9387. int wolfSSL_BN_dec2bn(WOLFSSL_BIGNUM** bn, const char* str)
  9388. {
  9389. (void)bn;
  9390. (void)str;
  9391. WOLFSSL_MSG("wolfSSL_BN_dec2bn");
  9392. return SSL_FAILURE;
  9393. }
  9394. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  9395. char *wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM *bn)
  9396. {
  9397. int len = 0;
  9398. char *buf;
  9399. WOLFSSL_MSG("wolfSSL_BN_bn2dec");
  9400. if (bn == NULL || bn->internal == NULL) {
  9401. WOLFSSL_MSG("bn NULL error");
  9402. return NULL;
  9403. }
  9404. if (mp_radix_size((mp_int*)bn->internal, 10, &len) != MP_OKAY) {
  9405. WOLFSSL_MSG("mp_radix_size failure");
  9406. return NULL;
  9407. }
  9408. buf = (char*) XMALLOC(len, NULL, DYNAMIC_TYPE_ECC);
  9409. if (buf == NULL) {
  9410. WOLFSSL_MSG("wolfSSL_BN_bn2hex malloc buffer failure");
  9411. return NULL;
  9412. }
  9413. if (mp_toradix((mp_int*)bn->internal, buf, 10) != MP_OKAY) {
  9414. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  9415. return NULL;
  9416. }
  9417. return buf;
  9418. }
  9419. #else
  9420. char* wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM* bn)
  9421. {
  9422. (void)bn;
  9423. WOLFSSL_MSG("wolfSSL_BN_bn2dec");
  9424. return NULL;
  9425. }
  9426. #endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
  9427. /* return code compliant with OpenSSL :
  9428. * 1 if success, 0 else
  9429. */
  9430. int wolfSSL_BN_lshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
  9431. {
  9432. WOLFSSL_MSG("wolfSSL_BN_lshift");
  9433. if (r == NULL || r->internal == NULL || bn == NULL || bn->internal == NULL){
  9434. WOLFSSL_MSG("bn NULL error");
  9435. return SSL_FAILURE;
  9436. }
  9437. if (mp_mul_2d((mp_int*)bn->internal, n, (mp_int*)r->internal) != MP_OKAY) {
  9438. WOLFSSL_MSG("mp_mul_2d error");
  9439. return SSL_FAILURE;
  9440. }
  9441. return SSL_SUCCESS;
  9442. }
  9443. /* return code compliant with OpenSSL :
  9444. * 1 if success, 0 else
  9445. */
  9446. int wolfSSL_BN_rshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
  9447. {
  9448. WOLFSSL_MSG("wolfSSL_BN_rshift");
  9449. if (r == NULL || r->internal == NULL || bn == NULL || bn->internal == NULL){
  9450. WOLFSSL_MSG("bn NULL error");
  9451. return SSL_FAILURE;
  9452. }
  9453. if (mp_div_2d((mp_int*)bn->internal, n,
  9454. (mp_int*)r->internal, NULL) != MP_OKAY) {
  9455. WOLFSSL_MSG("mp_mul_2d error");
  9456. return SSL_FAILURE;
  9457. }
  9458. return SSL_SUCCESS;
  9459. }
  9460. /* return code compliant with OpenSSL :
  9461. * 1 if success, 0 else
  9462. */
  9463. int wolfSSL_BN_add_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w)
  9464. {
  9465. WOLFSSL_MSG("wolfSSL_BN_add_word");
  9466. if (bn == NULL || bn->internal == NULL) {
  9467. WOLFSSL_MSG("bn NULL error");
  9468. return SSL_FAILURE;
  9469. }
  9470. if (mp_add_d((mp_int*)bn->internal, w, (mp_int*)bn->internal) != MP_OKAY) {
  9471. WOLFSSL_MSG("mp_add_d error");
  9472. return SSL_FAILURE;
  9473. }
  9474. return SSL_SUCCESS;
  9475. }
  9476. /* return code compliant with OpenSSL :
  9477. * 1 if success, 0 else
  9478. */
  9479. int wolfSSL_BN_add(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a, WOLFSSL_BIGNUM *b)
  9480. {
  9481. WOLFSSL_MSG("wolfSSL_BN_add");
  9482. if (r == NULL || r->internal == NULL || a == NULL || a->internal == NULL ||
  9483. b == NULL || b->internal == NULL) {
  9484. WOLFSSL_MSG("bn NULL error");
  9485. return SSL_FAILURE;
  9486. }
  9487. if (mp_add((mp_int*)a->internal, (mp_int*)b->internal,
  9488. (mp_int*)r->internal) != MP_OKAY) {
  9489. WOLFSSL_MSG("mp_add_d error");
  9490. return SSL_FAILURE;
  9491. }
  9492. return SSL_SUCCESS;
  9493. }
  9494. #ifdef WOLFSSL_KEY_GEN
  9495. /* return code compliant with OpenSSL :
  9496. * 1 if prime, 0 if not, -1 if error
  9497. */
  9498. int wolfSSL_BN_is_prime_ex(const WOLFSSL_BIGNUM *bn, int nbchecks,
  9499. WOLFSSL_BN_CTX *ctx, WOLFSSL_BN_GENCB *cb)
  9500. {
  9501. int res;
  9502. (void)ctx;
  9503. (void)cb;
  9504. WOLFSSL_MSG("wolfSSL_BN_is_prime_ex");
  9505. if (bn == NULL || bn->internal == NULL) {
  9506. WOLFSSL_MSG("bn NULL error");
  9507. return SSL_FATAL_ERROR;
  9508. }
  9509. if (mp_prime_is_prime((mp_int*)bn->internal, nbchecks, &res) != MP_OKAY) {
  9510. WOLFSSL_MSG("mp_prime_is_prime error");
  9511. return SSL_FATAL_ERROR;
  9512. }
  9513. if (res != MP_YES) {
  9514. WOLFSSL_MSG("mp_prime_is_prime not prime");
  9515. return SSL_FAILURE;
  9516. }
  9517. return SSL_SUCCESS;
  9518. }
  9519. /* return code compliant with OpenSSL :
  9520. * (bn mod w) if success, -1 if error
  9521. */
  9522. WOLFSSL_BN_ULONG wolfSSL_BN_mod_word(const WOLFSSL_BIGNUM *bn,
  9523. WOLFSSL_BN_ULONG w)
  9524. {
  9525. WOLFSSL_BN_ULONG ret = 0;
  9526. WOLFSSL_MSG("wolfSSL_BN_mod_word");
  9527. if (bn == NULL || bn->internal == NULL) {
  9528. WOLFSSL_MSG("bn NULL error");
  9529. return (WOLFSSL_BN_ULONG)SSL_FATAL_ERROR;
  9530. }
  9531. if (mp_mod_d((mp_int*)bn->internal, w, &ret) != MP_OKAY) {
  9532. WOLFSSL_MSG("mp_add_d error");
  9533. return (WOLFSSL_BN_ULONG)SSL_FATAL_ERROR;
  9534. }
  9535. return ret;
  9536. }
  9537. #endif /* #ifdef WOLFSSL_KEY_GEN */
  9538. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  9539. char *wolfSSL_BN_bn2hex(const WOLFSSL_BIGNUM *bn)
  9540. {
  9541. int len = 0;
  9542. char *buf;
  9543. WOLFSSL_MSG("wolfSSL_BN_bn2hex");
  9544. if (bn == NULL || bn->internal == NULL) {
  9545. WOLFSSL_MSG("bn NULL error");
  9546. return NULL;
  9547. }
  9548. if (mp_radix_size((mp_int*)bn->internal, 16, &len) != MP_OKAY) {
  9549. WOLFSSL_MSG("mp_radix_size failure");
  9550. return NULL;
  9551. }
  9552. buf = (char*) XMALLOC(len, NULL, DYNAMIC_TYPE_ECC);
  9553. if (buf == NULL) {
  9554. WOLFSSL_MSG("wolfSSL_BN_bn2hex malloc buffer failure");
  9555. return NULL;
  9556. }
  9557. if (mp_toradix((mp_int*)bn->internal, buf, 16) != MP_OKAY) {
  9558. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  9559. return NULL;
  9560. }
  9561. return buf;
  9562. }
  9563. #ifndef NO_FILESYSTEM
  9564. /* return code compliant with OpenSSL :
  9565. * 1 if success, 0 if error
  9566. */
  9567. int wolfSSL_BN_print_fp(FILE *fp, const WOLFSSL_BIGNUM *bn)
  9568. {
  9569. char *buf;
  9570. WOLFSSL_MSG("wolfSSL_BN_print_fp");
  9571. if (fp == NULL || bn == NULL || bn->internal == NULL) {
  9572. WOLFSSL_MSG("bn NULL error");
  9573. return SSL_FAILURE;
  9574. }
  9575. buf = wolfSSL_BN_bn2hex(bn);
  9576. if (buf == NULL) {
  9577. WOLFSSL_MSG("wolfSSL_BN_bn2hex failure");
  9578. return SSL_FAILURE;
  9579. }
  9580. fprintf(fp, "%s", buf);
  9581. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  9582. return SSL_SUCCESS;
  9583. }
  9584. #endif /* !defined(NO_FILESYSTEM) */
  9585. #else /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
  9586. char *wolfSSL_BN_bn2hex(const WOLFSSL_BIGNUM *bn)
  9587. {
  9588. (void)bn;
  9589. WOLFSSL_MSG("wolfSSL_BN_bn2hex not implemented");
  9590. return (char*)"";
  9591. }
  9592. #ifndef NO_FILESYSTEM
  9593. /* return code compliant with OpenSSL :
  9594. * 1 if success, 0 if error
  9595. */
  9596. int wolfSSL_BN_print_fp(FILE *fp, const WOLFSSL_BIGNUM *bn)
  9597. {
  9598. (void)fp;
  9599. (void)bn;
  9600. WOLFSSL_MSG("wolfSSL_BN_print_fp not implemented");
  9601. return SSL_SUCCESS;
  9602. }
  9603. #endif /* !defined(NO_FILESYSTEM) */
  9604. #endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
  9605. WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx)
  9606. {
  9607. /* ctx is not used, return new Bignum */
  9608. (void)ctx;
  9609. WOLFSSL_ENTER("wolfSSL_BN_CTX_get");
  9610. return wolfSSL_BN_new();
  9611. }
  9612. void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx)
  9613. {
  9614. (void)ctx;
  9615. WOLFSSL_ENTER("wolfSSL_BN_CTX_start");
  9616. WOLFSSL_MSG("wolfSSL_BN_CTX_start TBD");
  9617. }
  9618. #ifndef NO_DH
  9619. static void InitwolfSSL_DH(WOLFSSL_DH* dh)
  9620. {
  9621. if (dh) {
  9622. dh->p = NULL;
  9623. dh->g = NULL;
  9624. dh->pub_key = NULL;
  9625. dh->priv_key = NULL;
  9626. dh->internal = NULL;
  9627. dh->inSet = 0;
  9628. dh->exSet = 0;
  9629. }
  9630. }
  9631. WOLFSSL_DH* wolfSSL_DH_new(void)
  9632. {
  9633. WOLFSSL_DH* external;
  9634. DhKey* key;
  9635. WOLFSSL_MSG("wolfSSL_DH_new");
  9636. key = (DhKey*) XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
  9637. if (key == NULL) {
  9638. WOLFSSL_MSG("wolfSSL_DH_new malloc DhKey failure");
  9639. return NULL;
  9640. }
  9641. external = (WOLFSSL_DH*) XMALLOC(sizeof(WOLFSSL_DH), NULL,
  9642. DYNAMIC_TYPE_DH);
  9643. if (external == NULL) {
  9644. WOLFSSL_MSG("wolfSSL_DH_new malloc WOLFSSL_DH failure");
  9645. XFREE(key, NULL, DYNAMIC_TYPE_DH);
  9646. return NULL;
  9647. }
  9648. InitwolfSSL_DH(external);
  9649. wc_InitDhKey(key);
  9650. external->internal = key;
  9651. return external;
  9652. }
  9653. void wolfSSL_DH_free(WOLFSSL_DH* dh)
  9654. {
  9655. WOLFSSL_MSG("wolfSSL_DH_free");
  9656. if (dh) {
  9657. if (dh->internal) {
  9658. wc_FreeDhKey((DhKey*)dh->internal);
  9659. XFREE(dh->internal, NULL, DYNAMIC_TYPE_DH);
  9660. dh->internal = NULL;
  9661. }
  9662. wolfSSL_BN_free(dh->priv_key);
  9663. wolfSSL_BN_free(dh->pub_key);
  9664. wolfSSL_BN_free(dh->g);
  9665. wolfSSL_BN_free(dh->p);
  9666. InitwolfSSL_DH(dh); /* set back to NULLs for safety */
  9667. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  9668. }
  9669. }
  9670. static int SetDhInternal(WOLFSSL_DH* dh)
  9671. {
  9672. int ret = SSL_FATAL_ERROR;
  9673. int pSz = 1024;
  9674. int gSz = 1024;
  9675. #ifdef WOLFSSL_SMALL_STACK
  9676. unsigned char* p = NULL;
  9677. unsigned char* g = NULL;
  9678. #else
  9679. unsigned char p[1024];
  9680. unsigned char g[1024];
  9681. #endif
  9682. WOLFSSL_ENTER("SetDhInternal");
  9683. if (dh == NULL || dh->p == NULL || dh->g == NULL)
  9684. WOLFSSL_MSG("Bad function arguments");
  9685. else if (wolfSSL_BN_bn2bin(dh->p, NULL) > pSz)
  9686. WOLFSSL_MSG("Bad p internal size");
  9687. else if (wolfSSL_BN_bn2bin(dh->g, NULL) > gSz)
  9688. WOLFSSL_MSG("Bad g internal size");
  9689. else {
  9690. #ifdef WOLFSSL_SMALL_STACK
  9691. p = (unsigned char*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9692. g = (unsigned char*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9693. if (p == NULL || g == NULL) {
  9694. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9695. XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9696. return ret;
  9697. }
  9698. #endif
  9699. pSz = wolfSSL_BN_bn2bin(dh->p, p);
  9700. gSz = wolfSSL_BN_bn2bin(dh->g, g);
  9701. if (pSz <= 0 || gSz <= 0)
  9702. WOLFSSL_MSG("Bad BN2bin set");
  9703. else if (wc_DhSetKey((DhKey*)dh->internal, p, pSz, g, gSz) < 0)
  9704. WOLFSSL_MSG("Bad DH SetKey");
  9705. else {
  9706. dh->inSet = 1;
  9707. ret = SSL_SUCCESS;
  9708. }
  9709. #ifdef WOLFSSL_SMALL_STACK
  9710. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9711. XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9712. #endif
  9713. }
  9714. return ret;
  9715. }
  9716. /* return code compliant with OpenSSL :
  9717. * DH prime size in bytes if success, 0 if error
  9718. */
  9719. int wolfSSL_DH_size(WOLFSSL_DH* dh)
  9720. {
  9721. WOLFSSL_MSG("wolfSSL_DH_size");
  9722. if (dh == NULL)
  9723. return SSL_FATAL_ERROR;
  9724. return wolfSSL_BN_num_bytes(dh->p);
  9725. }
  9726. /* return code compliant with OpenSSL :
  9727. * 1 if success, 0 if error
  9728. */
  9729. int wolfSSL_DH_generate_key(WOLFSSL_DH* dh)
  9730. {
  9731. int ret = SSL_FAILURE;
  9732. word32 pubSz = 768;
  9733. word32 privSz = 768;
  9734. int initTmpRng = 0;
  9735. WC_RNG* rng = NULL;
  9736. #ifdef WOLFSSL_SMALL_STACK
  9737. unsigned char* pub = NULL;
  9738. unsigned char* priv = NULL;
  9739. WC_RNG* tmpRNG = NULL;
  9740. #else
  9741. unsigned char pub [768];
  9742. unsigned char priv[768];
  9743. WC_RNG tmpRNG[1];
  9744. #endif
  9745. WOLFSSL_MSG("wolfSSL_DH_generate_key");
  9746. #ifdef WOLFSSL_SMALL_STACK
  9747. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9748. pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9749. priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9750. if (tmpRNG == NULL || pub == NULL || priv == NULL) {
  9751. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9752. XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9753. XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9754. return ret;
  9755. }
  9756. #endif
  9757. if (dh == NULL || dh->p == NULL || dh->g == NULL)
  9758. WOLFSSL_MSG("Bad function arguments");
  9759. else if (dh->inSet == 0 && SetDhInternal(dh) != SSL_SUCCESS)
  9760. WOLFSSL_MSG("Bad DH set internal");
  9761. else if (wc_InitRng(tmpRNG) == 0) {
  9762. rng = tmpRNG;
  9763. initTmpRng = 1;
  9764. }
  9765. else {
  9766. WOLFSSL_MSG("Bad RNG Init, trying global");
  9767. if (initGlobalRNG == 0)
  9768. WOLFSSL_MSG("Global RNG no Init");
  9769. else
  9770. rng = &globalRNG;
  9771. }
  9772. if (rng) {
  9773. if (wc_DhGenerateKeyPair((DhKey*)dh->internal, rng, priv, &privSz,
  9774. pub, &pubSz) < 0)
  9775. WOLFSSL_MSG("Bad wc_DhGenerateKeyPair");
  9776. else {
  9777. if (dh->pub_key)
  9778. wolfSSL_BN_free(dh->pub_key);
  9779. dh->pub_key = wolfSSL_BN_new();
  9780. if (dh->pub_key == NULL) {
  9781. WOLFSSL_MSG("Bad DH new pub");
  9782. }
  9783. if (dh->priv_key)
  9784. wolfSSL_BN_free(dh->priv_key);
  9785. dh->priv_key = wolfSSL_BN_new();
  9786. if (dh->priv_key == NULL) {
  9787. WOLFSSL_MSG("Bad DH new priv");
  9788. }
  9789. if (dh->pub_key && dh->priv_key) {
  9790. if (wolfSSL_BN_bin2bn(pub, pubSz, dh->pub_key) == NULL)
  9791. WOLFSSL_MSG("Bad DH bn2bin error pub");
  9792. else if (wolfSSL_BN_bin2bn(priv, privSz, dh->priv_key) == NULL)
  9793. WOLFSSL_MSG("Bad DH bn2bin error priv");
  9794. else
  9795. ret = SSL_SUCCESS;
  9796. }
  9797. }
  9798. }
  9799. if (initTmpRng)
  9800. wc_FreeRng(tmpRNG);
  9801. #ifdef WOLFSSL_SMALL_STACK
  9802. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9803. XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9804. XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9805. #endif
  9806. return ret;
  9807. }
  9808. /* return code compliant with OpenSSL :
  9809. * size of shared secret if success, -1 if error
  9810. */
  9811. int wolfSSL_DH_compute_key(unsigned char* key, WOLFSSL_BIGNUM* otherPub,
  9812. WOLFSSL_DH* dh)
  9813. {
  9814. int ret = SSL_FATAL_ERROR;
  9815. word32 keySz = 0;
  9816. word32 pubSz = 1024;
  9817. word32 privSz = 1024;
  9818. #ifdef WOLFSSL_SMALL_STACK
  9819. unsigned char* pub = NULL;
  9820. unsigned char* priv = NULL;
  9821. #else
  9822. unsigned char pub [1024];
  9823. unsigned char priv[1024];
  9824. #endif
  9825. WOLFSSL_MSG("wolfSSL_DH_compute_key");
  9826. #ifdef WOLFSSL_SMALL_STACK
  9827. pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9828. if (pub == NULL)
  9829. return ret;
  9830. priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9831. if (priv == NULL) {
  9832. XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9833. return ret;
  9834. }
  9835. #endif
  9836. if (dh == NULL || dh->priv_key == NULL || otherPub == NULL)
  9837. WOLFSSL_MSG("Bad function arguments");
  9838. else if ((keySz = (word32)DH_size(dh)) == 0)
  9839. WOLFSSL_MSG("Bad DH_size");
  9840. else if (wolfSSL_BN_bn2bin(dh->priv_key, NULL) > (int)privSz)
  9841. WOLFSSL_MSG("Bad priv internal size");
  9842. else if (wolfSSL_BN_bn2bin(otherPub, NULL) > (int)pubSz)
  9843. WOLFSSL_MSG("Bad otherPub size");
  9844. else {
  9845. privSz = wolfSSL_BN_bn2bin(dh->priv_key, priv);
  9846. pubSz = wolfSSL_BN_bn2bin(otherPub, pub);
  9847. if (privSz <= 0 || pubSz <= 0)
  9848. WOLFSSL_MSG("Bad BN2bin set");
  9849. else if (wc_DhAgree((DhKey*)dh->internal, key, &keySz,
  9850. priv, privSz, pub, pubSz) < 0)
  9851. WOLFSSL_MSG("wc_DhAgree failed");
  9852. else
  9853. ret = (int)keySz;
  9854. }
  9855. #ifdef WOLFSSL_SMALL_STACK
  9856. XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9857. XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9858. #endif
  9859. return ret;
  9860. }
  9861. #endif /* NO_DH */
  9862. #ifndef NO_DSA
  9863. static void InitwolfSSL_DSA(WOLFSSL_DSA* dsa)
  9864. {
  9865. if (dsa) {
  9866. dsa->p = NULL;
  9867. dsa->q = NULL;
  9868. dsa->g = NULL;
  9869. dsa->pub_key = NULL;
  9870. dsa->priv_key = NULL;
  9871. dsa->internal = NULL;
  9872. dsa->inSet = 0;
  9873. dsa->exSet = 0;
  9874. }
  9875. }
  9876. WOLFSSL_DSA* wolfSSL_DSA_new(void)
  9877. {
  9878. WOLFSSL_DSA* external;
  9879. DsaKey* key;
  9880. WOLFSSL_MSG("wolfSSL_DSA_new");
  9881. key = (DsaKey*) XMALLOC(sizeof(DsaKey), NULL, DYNAMIC_TYPE_DSA);
  9882. if (key == NULL) {
  9883. WOLFSSL_MSG("wolfSSL_DSA_new malloc DsaKey failure");
  9884. return NULL;
  9885. }
  9886. external = (WOLFSSL_DSA*) XMALLOC(sizeof(WOLFSSL_DSA), NULL,
  9887. DYNAMIC_TYPE_DSA);
  9888. if (external == NULL) {
  9889. WOLFSSL_MSG("wolfSSL_DSA_new malloc WOLFSSL_DSA failure");
  9890. XFREE(key, NULL, DYNAMIC_TYPE_DSA);
  9891. return NULL;
  9892. }
  9893. InitwolfSSL_DSA(external);
  9894. InitDsaKey(key);
  9895. external->internal = key;
  9896. return external;
  9897. }
  9898. void wolfSSL_DSA_free(WOLFSSL_DSA* dsa)
  9899. {
  9900. WOLFSSL_MSG("wolfSSL_DSA_free");
  9901. if (dsa) {
  9902. if (dsa->internal) {
  9903. FreeDsaKey((DsaKey*)dsa->internal);
  9904. XFREE(dsa->internal, NULL, DYNAMIC_TYPE_DSA);
  9905. dsa->internal = NULL;
  9906. }
  9907. wolfSSL_BN_free(dsa->priv_key);
  9908. wolfSSL_BN_free(dsa->pub_key);
  9909. wolfSSL_BN_free(dsa->g);
  9910. wolfSSL_BN_free(dsa->q);
  9911. wolfSSL_BN_free(dsa->p);
  9912. InitwolfSSL_DSA(dsa); /* set back to NULLs for safety */
  9913. XFREE(dsa, NULL, DYNAMIC_TYPE_DSA);
  9914. dsa = NULL;
  9915. }
  9916. }
  9917. #endif /* NO_DSA */
  9918. #ifndef NO_RSA
  9919. static void InitwolfSSL_Rsa(WOLFSSL_RSA* rsa)
  9920. {
  9921. if (rsa) {
  9922. rsa->n = NULL;
  9923. rsa->e = NULL;
  9924. rsa->d = NULL;
  9925. rsa->p = NULL;
  9926. rsa->q = NULL;
  9927. rsa->dmp1 = NULL;
  9928. rsa->dmq1 = NULL;
  9929. rsa->iqmp = NULL;
  9930. rsa->internal = NULL;
  9931. rsa->inSet = 0;
  9932. rsa->exSet = 0;
  9933. }
  9934. }
  9935. WOLFSSL_RSA* wolfSSL_RSA_new(void)
  9936. {
  9937. WOLFSSL_RSA* external;
  9938. RsaKey* key;
  9939. WOLFSSL_MSG("wolfSSL_RSA_new");
  9940. key = (RsaKey*) XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
  9941. if (key == NULL) {
  9942. WOLFSSL_MSG("wolfSSL_RSA_new malloc RsaKey failure");
  9943. return NULL;
  9944. }
  9945. external = (WOLFSSL_RSA*) XMALLOC(sizeof(WOLFSSL_RSA), NULL,
  9946. DYNAMIC_TYPE_RSA);
  9947. if (external == NULL) {
  9948. WOLFSSL_MSG("wolfSSL_RSA_new malloc WOLFSSL_RSA failure");
  9949. XFREE(key, NULL, DYNAMIC_TYPE_RSA);
  9950. return NULL;
  9951. }
  9952. InitwolfSSL_Rsa(external);
  9953. if (wc_InitRsaKey(key, NULL) != 0) {
  9954. WOLFSSL_MSG("InitRsaKey WOLFSSL_RSA failure");
  9955. XFREE(external, NULL, DYNAMIC_TYPE_RSA);
  9956. XFREE(key, NULL, DYNAMIC_TYPE_RSA);
  9957. return NULL;
  9958. }
  9959. external->internal = key;
  9960. return external;
  9961. }
  9962. void wolfSSL_RSA_free(WOLFSSL_RSA* rsa)
  9963. {
  9964. WOLFSSL_MSG("wolfSSL_RSA_free");
  9965. if (rsa) {
  9966. if (rsa->internal) {
  9967. wc_FreeRsaKey((RsaKey*)rsa->internal);
  9968. XFREE(rsa->internal, NULL, DYNAMIC_TYPE_RSA);
  9969. rsa->internal = NULL;
  9970. }
  9971. wolfSSL_BN_free(rsa->iqmp);
  9972. wolfSSL_BN_free(rsa->dmq1);
  9973. wolfSSL_BN_free(rsa->dmp1);
  9974. wolfSSL_BN_free(rsa->q);
  9975. wolfSSL_BN_free(rsa->p);
  9976. wolfSSL_BN_free(rsa->d);
  9977. wolfSSL_BN_free(rsa->e);
  9978. wolfSSL_BN_free(rsa->n);
  9979. InitwolfSSL_Rsa(rsa); /* set back to NULLs for safety */
  9980. XFREE(rsa, NULL, DYNAMIC_TYPE_RSA);
  9981. rsa = NULL;
  9982. }
  9983. }
  9984. #endif /* NO_RSA */
  9985. #if (!defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)) \
  9986. || !defined(NO_DSA) || defined(HAVE_ECC)
  9987. static int SetIndividualExternal(WOLFSSL_BIGNUM** bn, mp_int* mpi)
  9988. {
  9989. WOLFSSL_MSG("Entering SetIndividualExternal");
  9990. if (mpi == NULL || bn == NULL) {
  9991. WOLFSSL_MSG("mpi NULL error");
  9992. return SSL_FATAL_ERROR;
  9993. }
  9994. if (*bn == NULL) {
  9995. *bn = wolfSSL_BN_new();
  9996. if (*bn == NULL) {
  9997. WOLFSSL_MSG("SetIndividualExternal alloc failed");
  9998. return SSL_FATAL_ERROR;
  9999. }
  10000. }
  10001. if (mp_copy(mpi, (mp_int*)((*bn)->internal)) != MP_OKAY) {
  10002. WOLFSSL_MSG("mp_copy error");
  10003. return SSL_FATAL_ERROR;
  10004. }
  10005. return SSL_SUCCESS;
  10006. }
  10007. static int SetIndividualInternal(WOLFSSL_BIGNUM* bn, mp_int* mpi)
  10008. {
  10009. WOLFSSL_MSG("Entering SetIndividualInternal");
  10010. if (bn == NULL || bn->internal == NULL) {
  10011. WOLFSSL_MSG("bn NULL error");
  10012. return SSL_FATAL_ERROR;
  10013. }
  10014. if (mpi == NULL || (mp_init(mpi) != MP_OKAY)) {
  10015. WOLFSSL_MSG("mpi NULL error");
  10016. return SSL_FATAL_ERROR;
  10017. }
  10018. if (mp_copy((mp_int*)bn->internal, mpi) != MP_OKAY) {
  10019. WOLFSSL_MSG("mp_copy error");
  10020. return SSL_FATAL_ERROR;
  10021. }
  10022. return SSL_SUCCESS;
  10023. }
  10024. #endif /* !NO_RSA && !NO_DSA */
  10025. #ifndef NO_DSA
  10026. /* wolfSSL -> OpenSSL */
  10027. static int SetDsaExternal(WOLFSSL_DSA* dsa)
  10028. {
  10029. DsaKey* key;
  10030. WOLFSSL_MSG("Entering SetDsaExternal");
  10031. if (dsa == NULL || dsa->internal == NULL) {
  10032. WOLFSSL_MSG("dsa key NULL error");
  10033. return SSL_FATAL_ERROR;
  10034. }
  10035. key = (DsaKey*)dsa->internal;
  10036. if (SetIndividualExternal(&dsa->p, &key->p) != SSL_SUCCESS) {
  10037. WOLFSSL_MSG("dsa p key error");
  10038. return SSL_FATAL_ERROR;
  10039. }
  10040. if (SetIndividualExternal(&dsa->q, &key->q) != SSL_SUCCESS) {
  10041. WOLFSSL_MSG("dsa q key error");
  10042. return SSL_FATAL_ERROR;
  10043. }
  10044. if (SetIndividualExternal(&dsa->g, &key->g) != SSL_SUCCESS) {
  10045. WOLFSSL_MSG("dsa g key error");
  10046. return SSL_FATAL_ERROR;
  10047. }
  10048. if (SetIndividualExternal(&dsa->pub_key, &key->y) != SSL_SUCCESS) {
  10049. WOLFSSL_MSG("dsa y key error");
  10050. return SSL_FATAL_ERROR;
  10051. }
  10052. if (SetIndividualExternal(&dsa->priv_key, &key->x) != SSL_SUCCESS) {
  10053. WOLFSSL_MSG("dsa x key error");
  10054. return SSL_FATAL_ERROR;
  10055. }
  10056. dsa->exSet = 1;
  10057. return SSL_SUCCESS;
  10058. }
  10059. /* Openssl -> WolfSSL */
  10060. static int SetDsaInternal(WOLFSSL_DSA* dsa)
  10061. {
  10062. DsaKey* key;
  10063. WOLFSSL_MSG("Entering SetDsaInternal");
  10064. if (dsa == NULL || dsa->internal == NULL) {
  10065. WOLFSSL_MSG("dsa key NULL error");
  10066. return SSL_FATAL_ERROR;
  10067. }
  10068. key = (DsaKey*)dsa->internal;
  10069. if (dsa->p != NULL &&
  10070. SetIndividualInternal(dsa->p, &key->p) != SSL_SUCCESS) {
  10071. WOLFSSL_MSG("rsa p key error");
  10072. return SSL_FATAL_ERROR;
  10073. }
  10074. if (dsa->q != NULL &&
  10075. SetIndividualInternal(dsa->q, &key->q) != SSL_SUCCESS) {
  10076. WOLFSSL_MSG("rsa q key error");
  10077. return SSL_FATAL_ERROR;
  10078. }
  10079. if (dsa->g != NULL &&
  10080. SetIndividualInternal(dsa->g, &key->g) != SSL_SUCCESS) {
  10081. WOLFSSL_MSG("rsa g key error");
  10082. return SSL_FATAL_ERROR;
  10083. }
  10084. if (dsa->pub_key != NULL) {
  10085. if (SetIndividualInternal(dsa->pub_key, &key->y) != SSL_SUCCESS) {
  10086. WOLFSSL_MSG("rsa pub_key error");
  10087. return SSL_FATAL_ERROR;
  10088. }
  10089. /* public key */
  10090. key->type = DSA_PUBLIC;
  10091. }
  10092. if (dsa->priv_key != NULL) {
  10093. if (SetIndividualInternal(dsa->priv_key, &key->x) != SSL_SUCCESS) {
  10094. WOLFSSL_MSG("rsa priv_key error");
  10095. return SSL_FATAL_ERROR;
  10096. }
  10097. /* private key */
  10098. key->type = DSA_PRIVATE;
  10099. }
  10100. dsa->inSet = 1;
  10101. return SSL_SUCCESS;
  10102. }
  10103. #endif /* NO_DSA */
  10104. #if !defined(NO_RSA)
  10105. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  10106. /* WolfSSL -> OpenSSL */
  10107. static int SetRsaExternal(WOLFSSL_RSA* rsa)
  10108. {
  10109. RsaKey* key;
  10110. WOLFSSL_MSG("Entering SetRsaExternal");
  10111. if (rsa == NULL || rsa->internal == NULL) {
  10112. WOLFSSL_MSG("rsa key NULL error");
  10113. return SSL_FATAL_ERROR;
  10114. }
  10115. key = (RsaKey*)rsa->internal;
  10116. if (SetIndividualExternal(&rsa->n, &key->n) != SSL_SUCCESS) {
  10117. WOLFSSL_MSG("rsa n key error");
  10118. return SSL_FATAL_ERROR;
  10119. }
  10120. if (SetIndividualExternal(&rsa->e, &key->e) != SSL_SUCCESS) {
  10121. WOLFSSL_MSG("rsa e key error");
  10122. return SSL_FATAL_ERROR;
  10123. }
  10124. if (SetIndividualExternal(&rsa->d, &key->d) != SSL_SUCCESS) {
  10125. WOLFSSL_MSG("rsa d key error");
  10126. return SSL_FATAL_ERROR;
  10127. }
  10128. if (SetIndividualExternal(&rsa->p, &key->p) != SSL_SUCCESS) {
  10129. WOLFSSL_MSG("rsa p key error");
  10130. return SSL_FATAL_ERROR;
  10131. }
  10132. if (SetIndividualExternal(&rsa->q, &key->q) != SSL_SUCCESS) {
  10133. WOLFSSL_MSG("rsa q key error");
  10134. return SSL_FATAL_ERROR;
  10135. }
  10136. if (SetIndividualExternal(&rsa->dmp1, &key->dP) != SSL_SUCCESS) {
  10137. WOLFSSL_MSG("rsa dP key error");
  10138. return SSL_FATAL_ERROR;
  10139. }
  10140. if (SetIndividualExternal(&rsa->dmq1, &key->dQ) != SSL_SUCCESS) {
  10141. WOLFSSL_MSG("rsa dQ key error");
  10142. return SSL_FATAL_ERROR;
  10143. }
  10144. if (SetIndividualExternal(&rsa->iqmp, &key->u) != SSL_SUCCESS) {
  10145. WOLFSSL_MSG("rsa u key error");
  10146. return SSL_FATAL_ERROR;
  10147. }
  10148. rsa->exSet = 1;
  10149. return SSL_SUCCESS;
  10150. }
  10151. /* Openssl -> WolfSSL */
  10152. static int SetRsaInternal(WOLFSSL_RSA* rsa)
  10153. {
  10154. RsaKey* key;
  10155. WOLFSSL_MSG("Entering SetRsaInternal");
  10156. if (rsa == NULL || rsa->internal == NULL) {
  10157. WOLFSSL_MSG("rsa key NULL error");
  10158. return SSL_FATAL_ERROR;
  10159. }
  10160. key = (RsaKey*)rsa->internal;
  10161. if (SetIndividualInternal(rsa->n, &key->n) != SSL_SUCCESS) {
  10162. WOLFSSL_MSG("rsa n key error");
  10163. return SSL_FATAL_ERROR;
  10164. }
  10165. if (SetIndividualInternal(rsa->e, &key->e) != SSL_SUCCESS) {
  10166. WOLFSSL_MSG("rsa e key error");
  10167. return SSL_FATAL_ERROR;
  10168. }
  10169. /* public key */
  10170. key->type = RSA_PUBLIC;
  10171. if (rsa->d != NULL) {
  10172. if (SetIndividualInternal(rsa->d, &key->d) != SSL_SUCCESS) {
  10173. WOLFSSL_MSG("rsa d key error");
  10174. return SSL_FATAL_ERROR;
  10175. }
  10176. /* private key */
  10177. key->type = RSA_PRIVATE;
  10178. }
  10179. if (rsa->p != NULL &&
  10180. SetIndividualInternal(rsa->p, &key->p) != SSL_SUCCESS) {
  10181. WOLFSSL_MSG("rsa p key error");
  10182. return SSL_FATAL_ERROR;
  10183. }
  10184. if (rsa->q != NULL &&
  10185. SetIndividualInternal(rsa->q, &key->q) != SSL_SUCCESS) {
  10186. WOLFSSL_MSG("rsa q key error");
  10187. return SSL_FATAL_ERROR;
  10188. }
  10189. if (rsa->dmp1 != NULL &&
  10190. SetIndividualInternal(rsa->dmp1, &key->dP) != SSL_SUCCESS) {
  10191. WOLFSSL_MSG("rsa dP key error");
  10192. return SSL_FATAL_ERROR;
  10193. }
  10194. if (rsa->dmq1 != NULL &&
  10195. SetIndividualInternal(rsa->dmq1, &key->dQ) != SSL_SUCCESS) {
  10196. WOLFSSL_MSG("rsa dQ key error");
  10197. return SSL_FATAL_ERROR;
  10198. }
  10199. if (rsa->iqmp != NULL &&
  10200. SetIndividualInternal(rsa->iqmp, &key->u) != SSL_SUCCESS) {
  10201. WOLFSSL_MSG("rsa u key error");
  10202. return SSL_FATAL_ERROR;
  10203. }
  10204. rsa->inSet = 1;
  10205. return SSL_SUCCESS;
  10206. }
  10207. #endif /* HAVE_USER_RSA */
  10208. /* return compliant with OpenSSL
  10209. * 1 if success, 0 if error
  10210. */
  10211. int wolfSSL_RSA_generate_key_ex(WOLFSSL_RSA* rsa, int bits, WOLFSSL_BIGNUM* bn,
  10212. void* cb)
  10213. {
  10214. int ret = SSL_FAILURE;
  10215. (void)cb;
  10216. (void)bn;
  10217. (void)bits;
  10218. WOLFSSL_ENTER("wolfSSL_RSA_generate_key_ex");
  10219. if (rsa == NULL || rsa->internal == NULL) {
  10220. /* bit size checked during make key call */
  10221. WOLFSSL_MSG("bad arguments");
  10222. return SSL_FAILURE;
  10223. }
  10224. #ifdef WOLFSSL_KEY_GEN
  10225. {
  10226. #ifdef WOLFSSL_SMALL_STACK
  10227. WC_RNG* rng = NULL;
  10228. #else
  10229. WC_RNG rng[1];
  10230. #endif
  10231. #ifdef WOLFSSL_SMALL_STACK
  10232. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10233. if (rng == NULL)
  10234. return SSL_FAILURE;
  10235. #endif
  10236. if (wc_InitRng(rng) < 0)
  10237. WOLFSSL_MSG("RNG init failed");
  10238. else if (wc_MakeRsaKey((RsaKey*)rsa->internal,
  10239. bits, 65537, rng) != MP_OKAY)
  10240. WOLFSSL_MSG("wc_MakeRsaKey failed");
  10241. else if (SetRsaExternal(rsa) != SSL_SUCCESS)
  10242. WOLFSSL_MSG("SetRsaExternal failed");
  10243. else {
  10244. rsa->inSet = 1;
  10245. ret = SSL_SUCCESS;
  10246. }
  10247. wc_FreeRng(rng);
  10248. #ifdef WOLFSSL_SMALL_STACK
  10249. XFREE(rng, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10250. #endif
  10251. }
  10252. #else
  10253. WOLFSSL_MSG("No Key Gen built in");
  10254. #endif
  10255. return ret;
  10256. }
  10257. /* SSL_SUCCESS on ok */
  10258. int wolfSSL_RSA_blinding_on(WOLFSSL_RSA* rsa, WOLFSSL_BN_CTX* bn)
  10259. {
  10260. (void)rsa;
  10261. (void)bn;
  10262. WOLFSSL_MSG("wolfSSL_RSA_blinding_on");
  10263. return SSL_SUCCESS; /* on by default */
  10264. }
  10265. /* return compliant with OpenSSL
  10266. * size of encrypted data if success , -1 if error
  10267. */
  10268. int wolfSSL_RSA_public_encrypt(int len, unsigned char* fr,
  10269. unsigned char* to, WOLFSSL_RSA* rsa, int padding)
  10270. {
  10271. (void)len;
  10272. (void)fr;
  10273. (void)to;
  10274. (void)rsa;
  10275. (void)padding;
  10276. WOLFSSL_MSG("wolfSSL_RSA_public_encrypt");
  10277. return SSL_FATAL_ERROR;
  10278. }
  10279. /* return compliant with OpenSSL
  10280. * size of plain recovered data if success , -1 if error
  10281. */
  10282. int wolfSSL_RSA_private_decrypt(int len, unsigned char* fr,
  10283. unsigned char* to, WOLFSSL_RSA* rsa, int padding)
  10284. {
  10285. (void)len;
  10286. (void)fr;
  10287. (void)to;
  10288. (void)rsa;
  10289. (void)padding;
  10290. WOLFSSL_MSG("wolfSSL_RSA_private_decrypt");
  10291. return SSL_FATAL_ERROR;
  10292. }
  10293. /* return compliant with OpenSSL
  10294. * RSA modulus size in bytes, -1 if error
  10295. */
  10296. int wolfSSL_RSA_size(const WOLFSSL_RSA* rsa)
  10297. {
  10298. WOLFSSL_MSG("wolfSSL_RSA_size");
  10299. if (rsa == NULL)
  10300. return SSL_FATAL_ERROR;
  10301. return wolfSSL_BN_num_bytes(rsa->n);
  10302. }
  10303. #endif /* NO_RSA */
  10304. #ifndef NO_DSA
  10305. /* return code compliant with OpenSSL :
  10306. * 1 if success, 0 if error
  10307. */
  10308. int wolfSSL_DSA_generate_key(WOLFSSL_DSA* dsa)
  10309. {
  10310. int ret = SSL_FAILURE;
  10311. WOLFSSL_ENTER("wolfSSL_DSA_generate_key");
  10312. if (dsa == NULL || dsa->internal == NULL) {
  10313. WOLFSSL_MSG("Bad arguments");
  10314. return SSL_FAILURE;
  10315. }
  10316. if (dsa->inSet == 0) {
  10317. WOLFSSL_MSG("No DSA internal set, do it");
  10318. if (SetDsaInternal(dsa) != SSL_SUCCESS) {
  10319. WOLFSSL_MSG("SetDsaInternal failed");
  10320. return ret;
  10321. }
  10322. }
  10323. #ifdef WOLFSSL_KEY_GEN
  10324. {
  10325. int initTmpRng = 0;
  10326. WC_RNG *rng = NULL;
  10327. #ifdef WOLFSSL_SMALL_STACK
  10328. WC_RNG *tmpRNG = NULL;
  10329. #else
  10330. WC_RNG tmpRNG[1];
  10331. #endif
  10332. #ifdef WOLFSSL_SMALL_STACK
  10333. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10334. if (tmpRNG == NULL)
  10335. return SSL_FATAL_ERROR;
  10336. #endif
  10337. if (wc_InitRng(tmpRNG) == 0) {
  10338. rng = tmpRNG;
  10339. initTmpRng = 1;
  10340. }
  10341. else {
  10342. WOLFSSL_MSG("Bad RNG Init, trying global");
  10343. if (initGlobalRNG == 0)
  10344. WOLFSSL_MSG("Global RNG no Init");
  10345. else
  10346. rng = &globalRNG;
  10347. }
  10348. if (rng) {
  10349. if (wc_MakeDsaKey(rng, (DsaKey*)dsa->internal) != MP_OKAY)
  10350. WOLFSSL_MSG("wc_MakeDsaKey failed");
  10351. else if (SetDsaExternal(dsa) != SSL_SUCCESS)
  10352. WOLFSSL_MSG("SetDsaExternal failed");
  10353. else
  10354. ret = SSL_SUCCESS;
  10355. }
  10356. if (initTmpRng)
  10357. wc_FreeRng(tmpRNG);
  10358. #ifdef WOLFSSL_SMALL_STACK
  10359. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10360. #endif
  10361. }
  10362. #else /* WOLFSSL_KEY_GEN */
  10363. WOLFSSL_MSG("No Key Gen built in");
  10364. #endif
  10365. return ret;
  10366. }
  10367. /* return code compliant with OpenSSL :
  10368. * 1 if success, 0 if error
  10369. */
  10370. int wolfSSL_DSA_generate_parameters_ex(WOLFSSL_DSA* dsa, int bits,
  10371. unsigned char* seed, int seedLen,
  10372. int* counterRet,
  10373. unsigned long* hRet, void* cb)
  10374. {
  10375. int ret = SSL_FAILURE;
  10376. (void)bits;
  10377. (void)seed;
  10378. (void)seedLen;
  10379. (void)counterRet;
  10380. (void)hRet;
  10381. (void)cb;
  10382. WOLFSSL_ENTER("wolfSSL_DSA_generate_parameters_ex");
  10383. if (dsa == NULL || dsa->internal == NULL) {
  10384. WOLFSSL_MSG("Bad arguments");
  10385. return SSL_FAILURE;
  10386. }
  10387. #ifdef WOLFSSL_KEY_GEN
  10388. {
  10389. int initTmpRng = 0;
  10390. WC_RNG *rng = NULL;
  10391. #ifdef WOLFSSL_SMALL_STACK
  10392. WC_RNG *tmpRNG = NULL;
  10393. #else
  10394. WC_RNG tmpRNG[1];
  10395. #endif
  10396. #ifdef WOLFSSL_SMALL_STACK
  10397. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10398. if (tmpRNG == NULL)
  10399. return SSL_FATAL_ERROR;
  10400. #endif
  10401. if (wc_InitRng(tmpRNG) == 0) {
  10402. rng = tmpRNG;
  10403. initTmpRng = 1;
  10404. }
  10405. else {
  10406. WOLFSSL_MSG("Bad RNG Init, trying global");
  10407. if (initGlobalRNG == 0)
  10408. WOLFSSL_MSG("Global RNG no Init");
  10409. else
  10410. rng = &globalRNG;
  10411. }
  10412. if (rng) {
  10413. if (wc_MakeDsaParameters(rng, bits,
  10414. (DsaKey*)dsa->internal) != MP_OKAY)
  10415. WOLFSSL_MSG("wc_MakeDsaParameters failed");
  10416. else if (SetDsaExternal(dsa) != SSL_SUCCESS)
  10417. WOLFSSL_MSG("SetDsaExternal failed");
  10418. else
  10419. ret = SSL_SUCCESS;
  10420. }
  10421. if (initTmpRng)
  10422. wc_FreeRng(tmpRNG);
  10423. #ifdef WOLFSSL_SMALL_STACK
  10424. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10425. #endif
  10426. }
  10427. #else /* WOLFSSL_KEY_GEN */
  10428. WOLFSSL_MSG("No Key Gen built in");
  10429. #endif
  10430. return ret;
  10431. }
  10432. /* return SSL_SUCCESS on success, < 0 otherwise */
  10433. int wolfSSL_DSA_do_sign(const unsigned char* d, unsigned char* sigRet,
  10434. WOLFSSL_DSA* dsa)
  10435. {
  10436. int ret = SSL_FATAL_ERROR;
  10437. int initTmpRng = 0;
  10438. WC_RNG* rng = NULL;
  10439. #ifdef WOLFSSL_SMALL_STACK
  10440. WC_RNG* tmpRNG = NULL;
  10441. #else
  10442. WC_RNG tmpRNG[1];
  10443. #endif
  10444. WOLFSSL_ENTER("wolfSSL_DSA_do_sign");
  10445. if (d == NULL || sigRet == NULL || dsa == NULL) {
  10446. WOLFSSL_MSG("Bad function arguments");
  10447. return ret;
  10448. }
  10449. if (dsa->inSet == 0)
  10450. {
  10451. WOLFSSL_MSG("No DSA internal set, do it");
  10452. if (SetDsaInternal(dsa) != SSL_SUCCESS) {
  10453. WOLFSSL_MSG("SetDsaInternal failed");
  10454. return ret;
  10455. }
  10456. }
  10457. #ifdef WOLFSSL_SMALL_STACK
  10458. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10459. if (tmpRNG == NULL)
  10460. return SSL_FATAL_ERROR;
  10461. #endif
  10462. if (wc_InitRng(tmpRNG) == 0) {
  10463. rng = tmpRNG;
  10464. initTmpRng = 1;
  10465. }
  10466. else {
  10467. WOLFSSL_MSG("Bad RNG Init, trying global");
  10468. if (initGlobalRNG == 0)
  10469. WOLFSSL_MSG("Global RNG no Init");
  10470. else
  10471. rng = &globalRNG;
  10472. }
  10473. if (rng) {
  10474. if (DsaSign(d, sigRet, (DsaKey*)dsa->internal, rng) < 0)
  10475. WOLFSSL_MSG("DsaSign failed");
  10476. else
  10477. ret = SSL_SUCCESS;
  10478. }
  10479. if (initTmpRng)
  10480. wc_FreeRng(tmpRNG);
  10481. #ifdef WOLFSSL_SMALL_STACK
  10482. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10483. #endif
  10484. return ret;
  10485. }
  10486. int wolfSSL_DSA_do_verify(const unsigned char* d, unsigned char* sig,
  10487. WOLFSSL_DSA* dsa, int *dsacheck)
  10488. {
  10489. int ret = SSL_FATAL_ERROR;
  10490. WOLFSSL_ENTER("wolfSSL_DSA_do_verify");
  10491. if (d == NULL || sig == NULL || dsa == NULL) {
  10492. WOLFSSL_MSG("Bad function arguments");
  10493. return SSL_FATAL_ERROR;
  10494. }
  10495. if (dsa->inSet == 0)
  10496. {
  10497. WOLFSSL_MSG("No DSA internal set, do it");
  10498. if (SetDsaInternal(dsa) != SSL_SUCCESS) {
  10499. WOLFSSL_MSG("SetDsaInternal failed");
  10500. return SSL_FATAL_ERROR;
  10501. }
  10502. }
  10503. ret = DsaVerify(d, sig, (DsaKey*)dsa->internal, dsacheck);
  10504. if (ret != 0 || *dsacheck != 1) {
  10505. WOLFSSL_MSG("DsaVerify failed");
  10506. return ret;
  10507. }
  10508. return SSL_SUCCESS;
  10509. }
  10510. #endif /* NO_DSA */
  10511. #ifndef NO_RSA
  10512. /* return SSL_SUCCES on ok, 0 otherwise */
  10513. int wolfSSL_RSA_sign(int type, const unsigned char* m,
  10514. unsigned int mLen, unsigned char* sigRet,
  10515. unsigned int* sigLen, WOLFSSL_RSA* rsa)
  10516. {
  10517. word32 outLen;
  10518. word32 signSz;
  10519. int initTmpRng = 0;
  10520. WC_RNG* rng = NULL;
  10521. int ret = 0;
  10522. #ifdef WOLFSSL_SMALL_STACK
  10523. WC_RNG* tmpRNG = NULL;
  10524. byte* encodedSig = NULL;
  10525. #else
  10526. WC_RNG tmpRNG[1];
  10527. byte encodedSig[MAX_ENCODED_SIG_SZ];
  10528. #endif
  10529. WOLFSSL_MSG("wolfSSL_RSA_sign");
  10530. if (m == NULL || sigRet == NULL || sigLen == NULL || rsa == NULL) {
  10531. WOLFSSL_MSG("Bad function arguments");
  10532. return 0;
  10533. }
  10534. if (type != NID_md5 && type != NID_sha1) {
  10535. WOLFSSL_MSG("Bad md type");
  10536. return 0;
  10537. }
  10538. if (rsa->inSet == 0)
  10539. {
  10540. WOLFSSL_MSG("No RSA internal set, do it");
  10541. if (SetRsaInternal(rsa) != SSL_SUCCESS) {
  10542. WOLFSSL_MSG("SetRsaInternal failed");
  10543. return 0;
  10544. }
  10545. }
  10546. outLen = (word32)wolfSSL_BN_num_bytes(rsa->n);
  10547. #ifdef WOLFSSL_SMALL_STACK
  10548. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10549. if (tmpRNG == NULL)
  10550. return 0;
  10551. encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL,
  10552. DYNAMIC_TYPE_TMP_BUFFER);
  10553. if (encodedSig == NULL) {
  10554. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10555. return 0;
  10556. }
  10557. #endif
  10558. if (outLen == 0)
  10559. WOLFSSL_MSG("Bad RSA size");
  10560. else if (wc_InitRng(tmpRNG) == 0) {
  10561. rng = tmpRNG;
  10562. initTmpRng = 1;
  10563. }
  10564. else {
  10565. WOLFSSL_MSG("Bad RNG Init, trying global");
  10566. if (initGlobalRNG == 0)
  10567. WOLFSSL_MSG("Global RNG no Init");
  10568. else
  10569. rng = &globalRNG;
  10570. }
  10571. if (rng) {
  10572. type = (type == NID_md5) ? MD5h : SHAh;
  10573. signSz = wc_EncodeSignature(encodedSig, m, mLen, type);
  10574. if (signSz == 0) {
  10575. WOLFSSL_MSG("Bad Encode Signature");
  10576. }
  10577. else {
  10578. *sigLen = wc_RsaSSL_Sign(encodedSig, signSz, sigRet, outLen,
  10579. (RsaKey*)rsa->internal, rng);
  10580. if (*sigLen <= 0)
  10581. WOLFSSL_MSG("Bad Rsa Sign");
  10582. else
  10583. ret = SSL_SUCCESS;
  10584. }
  10585. }
  10586. if (initTmpRng)
  10587. wc_FreeRng(tmpRNG);
  10588. #ifdef WOLFSSL_SMALL_STACK
  10589. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10590. XFREE(encodedSig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10591. #endif
  10592. if (ret == SSL_SUCCESS)
  10593. WOLFSSL_MSG("wolfSSL_RSA_sign success");
  10594. else {
  10595. WOLFSSL_MSG("wolfSSL_RSA_sign failed");
  10596. }
  10597. return ret;
  10598. }
  10599. int wolfSSL_RSA_public_decrypt(int flen, unsigned char* from,
  10600. unsigned char* to, WOLFSSL_RSA* rsa, int padding)
  10601. {
  10602. int tlen = 0;
  10603. WOLFSSL_MSG("wolfSSL_RSA_public_decrypt");
  10604. if (rsa == NULL || rsa->internal == NULL || from == NULL) {
  10605. WOLFSSL_MSG("Bad function arguments");
  10606. return 0;
  10607. }
  10608. if (padding != RSA_PKCS1_PADDING) {
  10609. WOLFSSL_MSG("wolfSSL_RSA_public_decrypt unsupported padding");
  10610. return 0;
  10611. }
  10612. if (rsa->inSet == 0)
  10613. {
  10614. WOLFSSL_MSG("No RSA internal set, do it");
  10615. if (SetRsaInternal(rsa) != SSL_SUCCESS) {
  10616. WOLFSSL_MSG("SetRsaInternal failed");
  10617. return 0;
  10618. }
  10619. }
  10620. /* size of 'to' buffer must be size of RSA key */
  10621. tlen = wc_RsaSSL_Verify(from, flen, to, wolfSSL_RSA_size(rsa),
  10622. (RsaKey*)rsa->internal);
  10623. if (tlen <= 0)
  10624. WOLFSSL_MSG("wolfSSL_RSA_public_decrypt failed");
  10625. else {
  10626. WOLFSSL_MSG("wolfSSL_RSA_public_decrypt success");
  10627. }
  10628. return tlen;
  10629. }
  10630. /* generate p-1 and q-1, SSL_SUCCESS on ok */
  10631. int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa)
  10632. {
  10633. int err;
  10634. mp_int tmp;
  10635. WOLFSSL_MSG("wolfSSL_RsaGenAdd");
  10636. if (rsa == NULL || rsa->p == NULL || rsa->q == NULL || rsa->d == NULL ||
  10637. rsa->dmp1 == NULL || rsa->dmq1 == NULL) {
  10638. WOLFSSL_MSG("rsa no init error");
  10639. return SSL_FATAL_ERROR;
  10640. }
  10641. if (mp_init(&tmp) != MP_OKAY) {
  10642. WOLFSSL_MSG("mp_init error");
  10643. return SSL_FATAL_ERROR;
  10644. }
  10645. err = mp_sub_d((mp_int*)rsa->p->internal, 1, &tmp);
  10646. if (err != MP_OKAY) {
  10647. WOLFSSL_MSG("mp_sub_d error");
  10648. }
  10649. else
  10650. err = mp_mod((mp_int*)rsa->d->internal, &tmp,
  10651. (mp_int*)rsa->dmp1->internal);
  10652. if (err != MP_OKAY) {
  10653. WOLFSSL_MSG("mp_mod error");
  10654. }
  10655. else
  10656. err = mp_sub_d((mp_int*)rsa->q->internal, 1, &tmp);
  10657. if (err != MP_OKAY) {
  10658. WOLFSSL_MSG("mp_sub_d error");
  10659. }
  10660. else
  10661. err = mp_mod((mp_int*)rsa->d->internal, &tmp,
  10662. (mp_int*)rsa->dmq1->internal);
  10663. mp_clear(&tmp);
  10664. if (err == MP_OKAY)
  10665. return SSL_SUCCESS;
  10666. else
  10667. return SSL_FATAL_ERROR;
  10668. }
  10669. #endif /* NO_RSA */
  10670. void wolfSSL_HMAC_Init(WOLFSSL_HMAC_CTX* ctx, const void* key, int keylen,
  10671. const EVP_MD* type)
  10672. {
  10673. WOLFSSL_MSG("wolfSSL_HMAC_Init");
  10674. if (ctx == NULL) {
  10675. WOLFSSL_MSG("no ctx on init");
  10676. return;
  10677. }
  10678. if (type) {
  10679. WOLFSSL_MSG("init has type");
  10680. if (XSTRNCMP(type, "MD5", 3) == 0) {
  10681. WOLFSSL_MSG("md5 hmac");
  10682. ctx->type = MD5;
  10683. }
  10684. else if (XSTRNCMP(type, "SHA256", 6) == 0) {
  10685. WOLFSSL_MSG("sha256 hmac");
  10686. ctx->type = SHA256;
  10687. }
  10688. /* has to be last since would pick or 256, 384, or 512 too */
  10689. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  10690. WOLFSSL_MSG("sha hmac");
  10691. ctx->type = SHA;
  10692. }
  10693. else {
  10694. WOLFSSL_MSG("bad init type");
  10695. }
  10696. }
  10697. if (key && keylen) {
  10698. WOLFSSL_MSG("keying hmac");
  10699. wc_HmacSetKey(&ctx->hmac, ctx->type, (const byte*)key, (word32)keylen);
  10700. /* OpenSSL compat, no error */
  10701. }
  10702. }
  10703. void wolfSSL_HMAC_Update(WOLFSSL_HMAC_CTX* ctx, const unsigned char* data,
  10704. int len)
  10705. {
  10706. WOLFSSL_MSG("wolfSSL_HMAC_Update");
  10707. if (ctx && data) {
  10708. WOLFSSL_MSG("updating hmac");
  10709. wc_HmacUpdate(&ctx->hmac, data, (word32)len);
  10710. /* OpenSSL compat, no error */
  10711. }
  10712. }
  10713. void wolfSSL_HMAC_Final(WOLFSSL_HMAC_CTX* ctx, unsigned char* hash,
  10714. unsigned int* len)
  10715. {
  10716. WOLFSSL_MSG("wolfSSL_HMAC_Final");
  10717. if (ctx && hash) {
  10718. WOLFSSL_MSG("final hmac");
  10719. wc_HmacFinal(&ctx->hmac, hash);
  10720. /* OpenSSL compat, no error */
  10721. if (len) {
  10722. WOLFSSL_MSG("setting output len");
  10723. switch (ctx->type) {
  10724. case MD5:
  10725. *len = MD5_DIGEST_SIZE;
  10726. break;
  10727. case SHA:
  10728. *len = SHA_DIGEST_SIZE;
  10729. break;
  10730. case SHA256:
  10731. *len = SHA256_DIGEST_SIZE;
  10732. break;
  10733. default:
  10734. WOLFSSL_MSG("bad hmac type");
  10735. }
  10736. }
  10737. }
  10738. }
  10739. void wolfSSL_HMAC_cleanup(WOLFSSL_HMAC_CTX* ctx)
  10740. {
  10741. (void)ctx;
  10742. WOLFSSL_MSG("wolfSSL_HMAC_cleanup");
  10743. }
  10744. const WOLFSSL_EVP_MD* wolfSSL_EVP_get_digestbynid(int id)
  10745. {
  10746. WOLFSSL_MSG("wolfSSL_get_digestbynid");
  10747. switch(id) {
  10748. #ifndef NO_MD5
  10749. case NID_md5:
  10750. return wolfSSL_EVP_md5();
  10751. #endif
  10752. #ifndef NO_SHA
  10753. case NID_sha1:
  10754. return wolfSSL_EVP_sha1();
  10755. #endif
  10756. default:
  10757. WOLFSSL_MSG("Bad digest id value");
  10758. }
  10759. return NULL;
  10760. }
  10761. WOLFSSL_RSA* wolfSSL_EVP_PKEY_get1_RSA(WOLFSSL_EVP_PKEY* key)
  10762. {
  10763. (void)key;
  10764. WOLFSSL_MSG("wolfSSL_EVP_PKEY_get1_RSA not implemented");
  10765. return NULL;
  10766. }
  10767. WOLFSSL_DSA* wolfSSL_EVP_PKEY_get1_DSA(WOLFSSL_EVP_PKEY* key)
  10768. {
  10769. (void)key;
  10770. WOLFSSL_MSG("wolfSSL_EVP_PKEY_get1_DSA not implemented");
  10771. return NULL;
  10772. }
  10773. WOLFSSL_EC_KEY* wolfSSL_EVP_PKEY_get1_EC_KEY(WOLFSSL_EVP_PKEY* key)
  10774. {
  10775. (void)key;
  10776. WOLFSSL_MSG("wolfSSL_EVP_PKEY_get1_EC_KEY not implemented");
  10777. return NULL;
  10778. }
  10779. void* wolfSSL_EVP_X_STATE(const WOLFSSL_EVP_CIPHER_CTX* ctx)
  10780. {
  10781. WOLFSSL_MSG("wolfSSL_EVP_X_STATE");
  10782. if (ctx) {
  10783. switch (ctx->cipherType) {
  10784. case ARC4_TYPE:
  10785. WOLFSSL_MSG("returning arc4 state");
  10786. return (void*)&ctx->cipher.arc4.x;
  10787. default:
  10788. WOLFSSL_MSG("bad x state type");
  10789. return 0;
  10790. }
  10791. }
  10792. return NULL;
  10793. }
  10794. int wolfSSL_EVP_X_STATE_LEN(const WOLFSSL_EVP_CIPHER_CTX* ctx)
  10795. {
  10796. WOLFSSL_MSG("wolfSSL_EVP_X_STATE_LEN");
  10797. if (ctx) {
  10798. switch (ctx->cipherType) {
  10799. case ARC4_TYPE:
  10800. WOLFSSL_MSG("returning arc4 state size");
  10801. return sizeof(Arc4);
  10802. default:
  10803. WOLFSSL_MSG("bad x state type");
  10804. return 0;
  10805. }
  10806. }
  10807. return 0;
  10808. }
  10809. #ifndef NO_DES3
  10810. void wolfSSL_3des_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
  10811. unsigned char* iv, int len)
  10812. {
  10813. (void)len;
  10814. WOLFSSL_MSG("wolfSSL_3des_iv");
  10815. if (ctx == NULL || iv == NULL) {
  10816. WOLFSSL_MSG("Bad function argument");
  10817. return;
  10818. }
  10819. if (doset)
  10820. wc_Des3_SetIV(&ctx->cipher.des3, iv); /* OpenSSL compat, no ret */
  10821. else
  10822. XMEMCPY(iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
  10823. }
  10824. #endif /* NO_DES3 */
  10825. #ifndef NO_AES
  10826. void wolfSSL_aes_ctr_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
  10827. unsigned char* iv, int len)
  10828. {
  10829. (void)len;
  10830. WOLFSSL_MSG("wolfSSL_aes_ctr_iv");
  10831. if (ctx == NULL || iv == NULL) {
  10832. WOLFSSL_MSG("Bad function argument");
  10833. return;
  10834. }
  10835. if (doset)
  10836. wc_AesSetIV(&ctx->cipher.aes, iv); /* OpenSSL compat, no ret */
  10837. else
  10838. XMEMCPY(iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  10839. }
  10840. #endif /* NO_AES */
  10841. const WOLFSSL_EVP_MD* wolfSSL_EVP_ripemd160(void)
  10842. {
  10843. WOLFSSL_MSG("wolfSSL_ripemd160");
  10844. return NULL;
  10845. }
  10846. int wolfSSL_EVP_MD_size(const WOLFSSL_EVP_MD* type)
  10847. {
  10848. WOLFSSL_MSG("wolfSSL_EVP_MD_size");
  10849. if (type == NULL) {
  10850. WOLFSSL_MSG("No md type arg");
  10851. return BAD_FUNC_ARG;
  10852. }
  10853. if (XSTRNCMP(type, "SHA256", 6) == 0) {
  10854. return SHA256_DIGEST_SIZE;
  10855. }
  10856. #ifndef NO_MD5
  10857. else if (XSTRNCMP(type, "MD5", 3) == 0) {
  10858. return MD5_DIGEST_SIZE;
  10859. }
  10860. #endif
  10861. #ifdef WOLFSSL_SHA384
  10862. else if (XSTRNCMP(type, "SHA384", 6) == 0) {
  10863. return SHA384_DIGEST_SIZE;
  10864. }
  10865. #endif
  10866. #ifdef WOLFSSL_SHA512
  10867. else if (XSTRNCMP(type, "SHA512", 6) == 0) {
  10868. return SHA512_DIGEST_SIZE;
  10869. }
  10870. #endif
  10871. #ifndef NO_SHA
  10872. /* has to be last since would pick or 256, 384, or 512 too */
  10873. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  10874. return SHA_DIGEST_SIZE;
  10875. }
  10876. #endif
  10877. return BAD_FUNC_ARG;
  10878. }
  10879. int wolfSSL_EVP_CIPHER_CTX_iv_length(const WOLFSSL_EVP_CIPHER_CTX* ctx)
  10880. {
  10881. WOLFSSL_MSG("wolfSSL_EVP_CIPHER_CTX_iv_length");
  10882. switch (ctx->cipherType) {
  10883. case AES_128_CBC_TYPE :
  10884. case AES_192_CBC_TYPE :
  10885. case AES_256_CBC_TYPE :
  10886. WOLFSSL_MSG("AES CBC");
  10887. return AES_BLOCK_SIZE;
  10888. #ifdef WOLFSSL_AES_COUNTER
  10889. case AES_128_CTR_TYPE :
  10890. case AES_192_CTR_TYPE :
  10891. case AES_256_CTR_TYPE :
  10892. WOLFSSL_MSG("AES CTR");
  10893. return AES_BLOCK_SIZE;
  10894. #endif
  10895. case DES_CBC_TYPE :
  10896. WOLFSSL_MSG("DES CBC");
  10897. return DES_BLOCK_SIZE;
  10898. case DES_EDE3_CBC_TYPE :
  10899. WOLFSSL_MSG("DES EDE3 CBC");
  10900. return DES_BLOCK_SIZE;
  10901. #ifdef HAVE_IDEA
  10902. case IDEA_CBC_TYPE :
  10903. WOLFSSL_MSG("IDEA CBC");
  10904. return IDEA_BLOCK_SIZE;
  10905. #endif
  10906. case ARC4_TYPE :
  10907. WOLFSSL_MSG("ARC4");
  10908. return 0;
  10909. case NULL_CIPHER_TYPE :
  10910. WOLFSSL_MSG("NULL");
  10911. return 0;
  10912. default: {
  10913. WOLFSSL_MSG("bad type");
  10914. }
  10915. }
  10916. return 0;
  10917. }
  10918. void wolfSSL_OPENSSL_free(void* p)
  10919. {
  10920. WOLFSSL_MSG("wolfSSL_OPENSSL_free");
  10921. XFREE(p, NULL, 0);
  10922. }
  10923. #if defined(WOLFSSL_KEY_GEN)
  10924. static int EncryptDerKey(byte *der, int *derSz, const EVP_CIPHER* cipher,
  10925. unsigned char* passwd, int passwdSz, byte **cipherInfo)
  10926. {
  10927. int ret, paddingSz;
  10928. word32 idx, cipherInfoSz;
  10929. #ifdef WOLFSSL_SMALL_STACK
  10930. EncryptedInfo* info = NULL;
  10931. #else
  10932. EncryptedInfo info[1];
  10933. #endif
  10934. WOLFSSL_ENTER("EncryptDerKey");
  10935. if (der == NULL || derSz == NULL || cipher == NULL ||
  10936. passwd == NULL || cipherInfo == NULL)
  10937. return BAD_FUNC_ARG;
  10938. #ifdef WOLFSSL_SMALL_STACK
  10939. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  10940. DYNAMIC_TYPE_TMP_BUFFER);
  10941. if (info == NULL) {
  10942. WOLFSSL_MSG("malloc failed");
  10943. return SSL_FAILURE;
  10944. }
  10945. #endif
  10946. info->set = 0;
  10947. info->ctx = NULL;
  10948. info->consumed = 0;
  10949. /* set iv size */
  10950. if (XSTRNCMP(cipher, "DES", 3) == 0)
  10951. info->ivSz = DES_IV_SIZE;
  10952. else if (XSTRNCMP(cipher, "AES", 3) == 0)
  10953. info->ivSz = AES_IV_SIZE;
  10954. else {
  10955. WOLFSSL_MSG("unsupported cipher");
  10956. #ifdef WOLFSSL_SMALL_STACK
  10957. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10958. #endif
  10959. return SSL_FAILURE;
  10960. }
  10961. /* set the cipher name on info */
  10962. XSTRNCPY(info->name, cipher, NAME_SZ);
  10963. /* Generate a random salt */
  10964. if (wolfSSL_RAND_bytes(info->iv, info->ivSz) != SSL_SUCCESS) {
  10965. WOLFSSL_MSG("generate iv failed");
  10966. #ifdef WOLFSSL_SMALL_STACK
  10967. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10968. #endif
  10969. return SSL_FAILURE;
  10970. }
  10971. /* add the padding before encryption */
  10972. paddingSz = ((*derSz)/info->ivSz + 1) * info->ivSz - (*derSz);
  10973. if (paddingSz == 0)
  10974. paddingSz = info->ivSz;
  10975. XMEMSET(der+(*derSz), (byte)paddingSz, paddingSz);
  10976. (*derSz) += paddingSz;
  10977. /* encrypt buffer */
  10978. if (wolfssl_encrypt_buffer_key(der, *derSz,
  10979. passwd, passwdSz, info) != SSL_SUCCESS) {
  10980. WOLFSSL_MSG("encrypt key failed");
  10981. #ifdef WOLFSSL_SMALL_STACK
  10982. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10983. #endif
  10984. return SSL_FAILURE;
  10985. }
  10986. /* create cipher info : 'cipher_name,Salt(hex)' */
  10987. cipherInfoSz = (word32)(2*info->ivSz + XSTRLEN(info->name) + 2);
  10988. *cipherInfo = (byte*)XMALLOC(cipherInfoSz, NULL,
  10989. DYNAMIC_TYPE_TMP_BUFFER);
  10990. if (*cipherInfo == NULL) {
  10991. WOLFSSL_MSG("malloc failed");
  10992. #ifdef WOLFSSL_SMALL_STACK
  10993. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10994. #endif
  10995. return SSL_FAILURE;
  10996. }
  10997. XSTRNCPY((char*)*cipherInfo, info->name, cipherInfoSz);
  10998. XSTRNCAT((char*)*cipherInfo, ",", 1);
  10999. idx = (word32)XSTRLEN((char*)*cipherInfo);
  11000. cipherInfoSz -= idx;
  11001. ret = Base16_Encode(info->iv, info->ivSz, *cipherInfo+idx, &cipherInfoSz);
  11002. #ifdef WOLFSSL_SMALL_STACK
  11003. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11004. #endif
  11005. if (ret != 0) {
  11006. WOLFSSL_MSG("Base16_Encode failed");
  11007. XFREE(*cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11008. return SSL_FAILURE;
  11009. }
  11010. return SSL_SUCCESS;
  11011. }
  11012. #endif /* defined(WOLFSSL_KEY_GEN) */
  11013. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  11014. /* return code compliant with OpenSSL :
  11015. * 1 if success, 0 if error
  11016. */
  11017. int wolfSSL_PEM_write_mem_RSAPrivateKey(RSA* rsa, const EVP_CIPHER* cipher,
  11018. unsigned char* passwd, int passwdSz,
  11019. unsigned char **pem, int *plen)
  11020. {
  11021. byte *der, *tmp, *cipherInfo = NULL;
  11022. int der_max_len = 0, derSz = 0;
  11023. WOLFSSL_ENTER("wolfSSL_PEM_write_mem_RSAPrivateKey");
  11024. if (pem == NULL || plen == NULL || rsa == NULL || rsa->internal == NULL) {
  11025. WOLFSSL_MSG("Bad function arguments");
  11026. return SSL_FAILURE;
  11027. }
  11028. if (rsa->inSet == 0) {
  11029. WOLFSSL_MSG("No RSA internal set, do it");
  11030. if (SetRsaInternal(rsa) != SSL_SUCCESS) {
  11031. WOLFSSL_MSG("SetRsaInternal failed");
  11032. return SSL_FAILURE;
  11033. }
  11034. }
  11035. /* 5 > size of n, d, p, q, d%(p-1), d(q-1), 1/q%p, e + ASN.1 additionnal
  11036. * informations
  11037. */
  11038. der_max_len = 5 * wolfSSL_RSA_size(rsa) + AES_BLOCK_SIZE;
  11039. der = (byte*)XMALLOC(der_max_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11040. if (der == NULL) {
  11041. WOLFSSL_MSG("malloc failed");
  11042. return SSL_FAILURE;
  11043. }
  11044. /* Key to DER */
  11045. derSz = wc_RsaKeyToDer((RsaKey*)rsa->internal, der, der_max_len);
  11046. if (derSz < 0) {
  11047. WOLFSSL_MSG("wc_RsaKeyToDer failed");
  11048. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11049. return SSL_FAILURE;
  11050. }
  11051. /* encrypt DER buffer if required */
  11052. if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
  11053. int ret;
  11054. ret = EncryptDerKey(der, &derSz, cipher,
  11055. passwd, passwdSz, &cipherInfo);
  11056. if (ret != SSL_SUCCESS) {
  11057. WOLFSSL_MSG("EncryptDerKey failed");
  11058. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11059. return ret;
  11060. }
  11061. /* tmp buffer with a max size */
  11062. *plen = (derSz * 2) + sizeof(BEGIN_RSA_PRIV) +
  11063. sizeof(END_RSA_PRIV) + HEADER_ENCRYPTED_KEY_SIZE;
  11064. }
  11065. else /* tmp buffer with a max size */
  11066. *plen = (derSz * 2) + sizeof(BEGIN_RSA_PRIV) + sizeof(END_RSA_PRIV);
  11067. tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11068. if (tmp == NULL) {
  11069. WOLFSSL_MSG("malloc failed");
  11070. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11071. if (cipherInfo != NULL)
  11072. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11073. return SSL_FAILURE;
  11074. }
  11075. /* DER to PEM */
  11076. *plen = wc_DerToPemEx(der, derSz, tmp, *plen, cipherInfo, PRIVATEKEY_TYPE);
  11077. if (*plen <= 0) {
  11078. WOLFSSL_MSG("wc_DerToPemEx failed");
  11079. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11080. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11081. if (cipherInfo != NULL)
  11082. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11083. return SSL_FAILURE;
  11084. }
  11085. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11086. if (cipherInfo != NULL)
  11087. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11088. *pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  11089. if (*pem == NULL) {
  11090. WOLFSSL_MSG("malloc failed");
  11091. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11092. return SSL_FAILURE;
  11093. }
  11094. XMEMSET(*pem, 0, (*plen)+1);
  11095. if (XMEMCPY(*pem, tmp, *plen) == NULL) {
  11096. WOLFSSL_MSG("XMEMCPY failed");
  11097. XFREE(pem, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  11098. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11099. return SSL_FAILURE;
  11100. }
  11101. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11102. return SSL_SUCCESS;
  11103. }
  11104. #ifndef NO_FILESYSTEM
  11105. /* return code compliant with OpenSSL :
  11106. * 1 if success, 0 if error
  11107. */
  11108. int wolfSSL_PEM_write_RSAPrivateKey(FILE *fp, WOLFSSL_RSA *rsa,
  11109. const EVP_CIPHER *enc,
  11110. unsigned char *kstr, int klen,
  11111. pem_password_cb *cb, void *u)
  11112. {
  11113. byte *pem;
  11114. int plen, ret;
  11115. (void)cb;
  11116. (void)u;
  11117. WOLFSSL_MSG("wolfSSL_PEM_write_RSAPrivateKey");
  11118. if (fp == NULL || rsa == NULL || rsa->internal == NULL) {
  11119. WOLFSSL_MSG("Bad function arguments");
  11120. return SSL_FAILURE;
  11121. }
  11122. ret = wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, enc, kstr, klen, &pem, &plen);
  11123. if (ret != SSL_SUCCESS) {
  11124. WOLFSSL_MSG("wolfSSL_PEM_write_mem_RSAPrivateKey failed");
  11125. return SSL_FAILURE;
  11126. }
  11127. ret = (int)XFWRITE(pem, plen, 1, fp);
  11128. if (ret != 1) {
  11129. WOLFSSL_MSG("RSA private key file write failed");
  11130. return SSL_FAILURE;
  11131. }
  11132. XFREE(pem, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  11133. return SSL_SUCCESS;
  11134. }
  11135. #endif /* NO_FILESYSTEM */
  11136. int wolfSSL_PEM_write_bio_RSAPrivateKey(WOLFSSL_BIO* bio, RSA* rsa,
  11137. const EVP_CIPHER* cipher,
  11138. unsigned char* passwd, int len,
  11139. pem_password_cb cb, void* arg)
  11140. {
  11141. (void)bio;
  11142. (void)rsa;
  11143. (void)cipher;
  11144. (void)passwd;
  11145. (void)len;
  11146. (void)cb;
  11147. (void)arg;
  11148. WOLFSSL_MSG("wolfSSL_PEM_write_bio_RSAPrivateKey not implemented");
  11149. return SSL_FAILURE;
  11150. }
  11151. #endif /* defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) */
  11152. #ifdef HAVE_ECC
  11153. /* EC_POINT Openssl -> WolfSSL */
  11154. static int SetECPointInternal(WOLFSSL_EC_POINT *p)
  11155. {
  11156. ecc_point* point;
  11157. WOLFSSL_ENTER("SetECPointInternal");
  11158. if (p == NULL || p->internal == NULL) {
  11159. WOLFSSL_MSG("ECPoint NULL error");
  11160. return SSL_FATAL_ERROR;
  11161. }
  11162. point = (ecc_point*)p->internal;
  11163. if (p->X != NULL && SetIndividualInternal(p->X, point->x) != SSL_SUCCESS) {
  11164. WOLFSSL_MSG("ecc point X error");
  11165. return SSL_FATAL_ERROR;
  11166. }
  11167. if (p->Y != NULL && SetIndividualInternal(p->Y, point->y) != SSL_SUCCESS) {
  11168. WOLFSSL_MSG("ecc point Y error");
  11169. return SSL_FATAL_ERROR;
  11170. }
  11171. if (p->Z != NULL && SetIndividualInternal(p->Z, point->z) != SSL_SUCCESS) {
  11172. WOLFSSL_MSG("ecc point Z error");
  11173. return SSL_FATAL_ERROR;
  11174. }
  11175. p->inSet = 1;
  11176. return SSL_SUCCESS;
  11177. }
  11178. /* EC_POINT WolfSSL -> OpenSSL */
  11179. static int SetECPointExternal(WOLFSSL_EC_POINT *p)
  11180. {
  11181. ecc_point* point;
  11182. WOLFSSL_ENTER("SetECPointExternal");
  11183. if (p == NULL || p->internal == NULL) {
  11184. WOLFSSL_MSG("ECPoint NULL error");
  11185. return SSL_FATAL_ERROR;
  11186. }
  11187. point = (ecc_point*)p->internal;
  11188. if (SetIndividualExternal(&p->X, point->x) != SSL_SUCCESS) {
  11189. WOLFSSL_MSG("ecc point X error");
  11190. return SSL_FATAL_ERROR;
  11191. }
  11192. if (SetIndividualExternal(&p->Y, point->y) != SSL_SUCCESS) {
  11193. WOLFSSL_MSG("ecc point Y error");
  11194. return SSL_FATAL_ERROR;
  11195. }
  11196. if (SetIndividualExternal(&p->Z, point->z) != SSL_SUCCESS) {
  11197. WOLFSSL_MSG("ecc point Z error");
  11198. return SSL_FATAL_ERROR;
  11199. }
  11200. p->exSet = 1;
  11201. return SSL_SUCCESS;
  11202. }
  11203. /* EC_KEY wolfSSL -> OpenSSL */
  11204. static int SetECKeyExternal(WOLFSSL_EC_KEY* eckey)
  11205. {
  11206. ecc_key* key;
  11207. WOLFSSL_ENTER("SetECKeyExternal");
  11208. if (eckey == NULL || eckey->internal == NULL) {
  11209. WOLFSSL_MSG("ec key NULL error");
  11210. return SSL_FATAL_ERROR;
  11211. }
  11212. key = (ecc_key*)eckey->internal;
  11213. /* set group (nid and idx) */
  11214. eckey->group->curve_nid = ecc_sets[key->idx].nid;
  11215. eckey->group->curve_idx = key->idx;
  11216. if (eckey->pub_key->internal != NULL) {
  11217. /* set the internal public key */
  11218. if (wc_ecc_copy_point(&key->pubkey,
  11219. (ecc_point*)eckey->pub_key->internal) != MP_OKAY) {
  11220. WOLFSSL_MSG("SetECKeyExternal ecc_copy_point failed");
  11221. return SSL_FATAL_ERROR;
  11222. }
  11223. /* set the external pubkey (point) */
  11224. if (SetECPointExternal(eckey->pub_key) != SSL_SUCCESS) {
  11225. WOLFSSL_MSG("SetECKeyExternal SetECPointExternal failed");
  11226. return SSL_FATAL_ERROR;
  11227. }
  11228. }
  11229. /* set the external privkey */
  11230. if (key->type == ECC_PRIVATEKEY) {
  11231. if (SetIndividualExternal(&eckey->priv_key, &key->k) != SSL_SUCCESS) {
  11232. WOLFSSL_MSG("ec priv key error");
  11233. return SSL_FATAL_ERROR;
  11234. }
  11235. }
  11236. eckey->exSet = 1;
  11237. return SSL_SUCCESS;
  11238. }
  11239. /* EC_KEY Openssl -> WolfSSL */
  11240. static int SetECKeyInternal(WOLFSSL_EC_KEY* eckey)
  11241. {
  11242. ecc_key* key;
  11243. WOLFSSL_ENTER("SetECKeyInternal");
  11244. if (eckey == NULL || eckey->internal == NULL) {
  11245. WOLFSSL_MSG("ec key NULL error");
  11246. return SSL_FATAL_ERROR;
  11247. }
  11248. key = (ecc_key*)eckey->internal;
  11249. /* validate group */
  11250. if ((eckey->group->curve_idx < 0) ||
  11251. (wc_ecc_is_valid_idx(eckey->group->curve_idx) == 0)) {
  11252. WOLFSSL_MSG("invalid curve idx");
  11253. return SSL_FATAL_ERROR;
  11254. }
  11255. /* set group (idx of curve and corresponding domain parameters) */
  11256. key->idx = eckey->group->curve_idx;
  11257. key->dp = &ecc_sets[key->idx];
  11258. /* set pubkey (point) */
  11259. if (eckey->pub_key != NULL) {
  11260. if (SetECPointInternal(eckey->pub_key) != SSL_SUCCESS) {
  11261. WOLFSSL_MSG("ec key pub error");
  11262. return SSL_FATAL_ERROR;
  11263. }
  11264. /* public key */
  11265. key->type = ECC_PUBLICKEY;
  11266. }
  11267. /* set privkey */
  11268. if (eckey->priv_key != NULL) {
  11269. if (SetIndividualInternal(eckey->priv_key, &key->k) != SSL_SUCCESS) {
  11270. WOLFSSL_MSG("ec key priv error");
  11271. return SSL_FATAL_ERROR;
  11272. }
  11273. /* private key */
  11274. key->type = ECC_PRIVATEKEY;
  11275. }
  11276. eckey->inSet = 1;
  11277. return SSL_SUCCESS;
  11278. }
  11279. WOLFSSL_EC_POINT *wolfSSL_EC_KEY_get0_public_key(const WOLFSSL_EC_KEY *key)
  11280. {
  11281. WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_public_key");
  11282. if (key == NULL) {
  11283. WOLFSSL_MSG("wolfSSL_EC_KEY_get0_group Bad arguments");
  11284. return NULL;
  11285. }
  11286. return key->pub_key;
  11287. }
  11288. const WOLFSSL_EC_GROUP *wolfSSL_EC_KEY_get0_group(const WOLFSSL_EC_KEY *key)
  11289. {
  11290. WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_group");
  11291. if (key == NULL) {
  11292. WOLFSSL_MSG("wolfSSL_EC_KEY_get0_group Bad arguments");
  11293. return NULL;
  11294. }
  11295. return key->group;
  11296. }
  11297. /* return code compliant with OpenSSL :
  11298. * 1 if success, 0 if error
  11299. */
  11300. int wolfSSL_EC_KEY_set_private_key(WOLFSSL_EC_KEY *key,
  11301. const WOLFSSL_BIGNUM *priv_key)
  11302. {
  11303. WOLFSSL_ENTER("wolfSSL_EC_KEY_set_private_key");
  11304. if (key == NULL || priv_key == NULL) {
  11305. WOLFSSL_MSG("Bad arguments");
  11306. return SSL_FAILURE;
  11307. }
  11308. /* free key if previously set */
  11309. if (key->priv_key != NULL)
  11310. wolfSSL_BN_free(key->priv_key);
  11311. key->priv_key = wolfSSL_BN_dup(priv_key);
  11312. if (key->priv_key == NULL) {
  11313. WOLFSSL_MSG("key ecc priv key NULL");
  11314. return SSL_FAILURE;
  11315. }
  11316. if (SetECKeyInternal(key) != SSL_SUCCESS) {
  11317. WOLFSSL_MSG("SetECKeyInternal failed");
  11318. wolfSSL_BN_free(key->priv_key);
  11319. return SSL_FAILURE;
  11320. }
  11321. return SSL_SUCCESS;
  11322. }
  11323. WOLFSSL_BIGNUM *wolfSSL_EC_KEY_get0_private_key(const WOLFSSL_EC_KEY *key)
  11324. {
  11325. WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_private_key");
  11326. if (key == NULL) {
  11327. WOLFSSL_MSG("wolfSSL_EC_KEY_get0_private_key Bad arguments");
  11328. return NULL;
  11329. }
  11330. return key->priv_key;
  11331. }
  11332. WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new_by_curve_name(int nid)
  11333. {
  11334. WOLFSSL_EC_KEY *key;
  11335. int x;
  11336. WOLFSSL_ENTER("wolfSSL_EC_KEY_new_by_curve_name");
  11337. key = wolfSSL_EC_KEY_new();
  11338. if (key == NULL) {
  11339. WOLFSSL_MSG("wolfSSL_EC_KEY_new failure");
  11340. return NULL;
  11341. }
  11342. /* set the nid of the curve */
  11343. key->group->curve_nid = nid;
  11344. /* search and set the corresponding internal curve idx */
  11345. for (x = 0; ecc_sets[x].size != 0; x++)
  11346. if (ecc_sets[x].nid == key->group->curve_nid) {
  11347. key->group->curve_idx = x;
  11348. break;
  11349. }
  11350. return key;
  11351. }
  11352. static void InitwolfSSL_ECKey(WOLFSSL_EC_KEY* key)
  11353. {
  11354. if (key) {
  11355. key->group = NULL;
  11356. key->pub_key = NULL;
  11357. key->priv_key = NULL;
  11358. key->internal = NULL;
  11359. key->inSet = 0;
  11360. key->exSet = 0;
  11361. }
  11362. }
  11363. WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new(void)
  11364. {
  11365. WOLFSSL_EC_KEY *external;
  11366. ecc_key* key;
  11367. WOLFSSL_ENTER("wolfSSL_EC_KEY_new");
  11368. external = (WOLFSSL_EC_KEY*)XMALLOC(sizeof(WOLFSSL_EC_KEY), NULL,
  11369. DYNAMIC_TYPE_ECC);
  11370. if (external == NULL) {
  11371. WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_KEY failure");
  11372. return NULL;
  11373. }
  11374. XMEMSET(external, 0, sizeof(WOLFSSL_EC_KEY));
  11375. InitwolfSSL_ECKey(external);
  11376. external->internal = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL,
  11377. DYNAMIC_TYPE_ECC);
  11378. if (external->internal == NULL) {
  11379. WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc ecc key failure");
  11380. wolfSSL_EC_KEY_free(external);
  11381. return NULL;
  11382. }
  11383. XMEMSET(external->internal, 0, sizeof(ecc_key));
  11384. wc_ecc_init((ecc_key*)external->internal);
  11385. /* public key */
  11386. external->pub_key = (WOLFSSL_EC_POINT*)XMALLOC(sizeof(WOLFSSL_EC_POINT),
  11387. NULL, DYNAMIC_TYPE_ECC);
  11388. if (external->pub_key == NULL) {
  11389. WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_POINT failure");
  11390. wolfSSL_EC_KEY_free(external);
  11391. return NULL;
  11392. }
  11393. XMEMSET(external->pub_key, 0, sizeof(WOLFSSL_EC_POINT));
  11394. key = (ecc_key*)external->internal;
  11395. external->pub_key->internal = (ecc_point*)&key->pubkey;
  11396. /* curve group */
  11397. external->group = (WOLFSSL_EC_GROUP*)XMALLOC(sizeof(WOLFSSL_EC_GROUP), NULL,
  11398. DYNAMIC_TYPE_ECC);
  11399. if (external->group == NULL) {
  11400. WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_GROUP failure");
  11401. wolfSSL_EC_KEY_free(external);
  11402. return NULL;
  11403. }
  11404. XMEMSET(external->group, 0, sizeof(WOLFSSL_EC_GROUP));
  11405. /* private key */
  11406. external->priv_key = wolfSSL_BN_new();
  11407. if (external->priv_key == NULL) {
  11408. WOLFSSL_MSG("wolfSSL_BN_new failure");
  11409. wolfSSL_EC_KEY_free(external);
  11410. return NULL;
  11411. }
  11412. return external;
  11413. }
  11414. void wolfSSL_EC_KEY_free(WOLFSSL_EC_KEY *key)
  11415. {
  11416. WOLFSSL_ENTER("wolfSSL_EC_KEY_free");
  11417. if (key != NULL) {
  11418. if (key->internal != NULL) {
  11419. wc_ecc_free((ecc_key*)key->internal);
  11420. XFREE(key->internal, NULL, DYNAMIC_TYPE_ECC);
  11421. }
  11422. wolfSSL_BN_free(key->priv_key);
  11423. wolfSSL_EC_POINT_free(key->pub_key);
  11424. wolfSSL_EC_GROUP_free(key->group);
  11425. InitwolfSSL_ECKey(key); /* set back to NULLs for safety */
  11426. XFREE(key, NULL, DYNAMIC_TYPE_ECC);
  11427. key = NULL;
  11428. }
  11429. }
  11430. int wolfSSL_EC_KEY_set_group(WOLFSSL_EC_KEY *key, WOLFSSL_EC_GROUP *group)
  11431. {
  11432. (void)key;
  11433. (void)group;
  11434. WOLFSSL_ENTER("wolfSSL_EC_KEY_set_group");
  11435. WOLFSSL_MSG("wolfSSL_EC_KEY_set_group TBD");
  11436. return -1;
  11437. }
  11438. int wolfSSL_EC_KEY_generate_key(WOLFSSL_EC_KEY *key)
  11439. {
  11440. int initTmpRng = 0;
  11441. WC_RNG* rng = NULL;
  11442. #ifdef WOLFSSL_SMALL_STACK
  11443. WC_RNG* tmpRNG = NULL;
  11444. #else
  11445. WC_RNG tmpRNG[1];
  11446. #endif
  11447. WOLFSSL_ENTER("wolfSSL_EC_KEY_generate_key");
  11448. if (key == NULL || key->internal == NULL ||
  11449. key->group == NULL || key->group->curve_idx < 0) {
  11450. WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key Bad arguments");
  11451. return 0;
  11452. }
  11453. #ifdef WOLFSSL_SMALL_STACK
  11454. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11455. if (tmpRNG == NULL)
  11456. return 0;
  11457. #endif
  11458. if (wc_InitRng(tmpRNG) == 0) {
  11459. rng = tmpRNG;
  11460. initTmpRng = 1;
  11461. }
  11462. else {
  11463. WOLFSSL_MSG("Bad RNG Init, trying global");
  11464. if (initGlobalRNG == 0)
  11465. WOLFSSL_MSG("Global RNG no Init");
  11466. else
  11467. rng = &globalRNG;
  11468. }
  11469. if (rng == NULL) {
  11470. WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key failed to set RNG");
  11471. #ifdef WOLFSSL_SMALL_STACK
  11472. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11473. #endif
  11474. return 0;
  11475. }
  11476. if (wc_ecc_make_key(rng, ecc_sets[key->group->curve_idx].size,
  11477. (ecc_key*)key->internal) != MP_OKAY) {
  11478. WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key wc_ecc_make_key failed");
  11479. #ifdef WOLFSSL_SMALL_STACK
  11480. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11481. #endif
  11482. return 0;
  11483. }
  11484. if (initTmpRng)
  11485. wc_FreeRng(tmpRNG);
  11486. #ifdef WOLFSSL_SMALL_STACK
  11487. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11488. #endif
  11489. if (SetECKeyExternal(key) != SSL_SUCCESS) {
  11490. WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key SetECKeyExternal failed");
  11491. return 0;
  11492. }
  11493. return 1;
  11494. }
  11495. void wolfSSL_EC_KEY_set_asn1_flag(WOLFSSL_EC_KEY *key, int asn1_flag)
  11496. {
  11497. (void)key;
  11498. (void)asn1_flag;
  11499. WOLFSSL_ENTER("wolfSSL_EC_KEY_set_asn1_flag");
  11500. WOLFSSL_MSG("wolfSSL_EC_KEY_set_asn1_flag TBD");
  11501. }
  11502. /* return code compliant with OpenSSL :
  11503. * 1 if success, 0 if error
  11504. */
  11505. int wolfSSL_EC_KEY_set_public_key(WOLFSSL_EC_KEY *key,
  11506. const WOLFSSL_EC_POINT *pub)
  11507. {
  11508. ecc_point *pub_p, *key_p;
  11509. WOLFSSL_ENTER("wolfSSL_EC_KEY_set_public_key");
  11510. if (key == NULL || key->internal == NULL ||
  11511. pub == NULL || pub->internal == NULL) {
  11512. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order Bad arguments");
  11513. return SSL_FAILURE;
  11514. }
  11515. if (key->inSet == 0) {
  11516. if (SetECKeyInternal(key) != SSL_SUCCESS) {
  11517. WOLFSSL_MSG("SetECKeyInternal failed");
  11518. return SSL_FAILURE;
  11519. }
  11520. }
  11521. if (pub->inSet == 0) {
  11522. if (SetECPointInternal((WOLFSSL_EC_POINT *)pub) != SSL_SUCCESS) {
  11523. WOLFSSL_MSG("SetECPointInternal failed");
  11524. return SSL_FAILURE;
  11525. }
  11526. }
  11527. pub_p = (ecc_point*)pub->internal;
  11528. key_p = (ecc_point*)key->pub_key->internal;
  11529. /* create new point if required */
  11530. if (key_p == NULL)
  11531. key_p = wc_ecc_new_point();
  11532. if (key_p == NULL) {
  11533. WOLFSSL_MSG("key ecc point NULL");
  11534. return SSL_FAILURE;
  11535. }
  11536. if (wc_ecc_copy_point(pub_p, key_p) != MP_OKAY) {
  11537. WOLFSSL_MSG("ecc_copy_point failure");
  11538. return SSL_FAILURE;
  11539. }
  11540. if (SetECKeyExternal(key) != SSL_SUCCESS) {
  11541. WOLFSSL_MSG("SetECKeyInternal failed");
  11542. return SSL_FAILURE;
  11543. }
  11544. #if defined(DEBUG_WOLFSSL) && !defined(NO_FILESYSTEM)
  11545. wolfssl_EC_POINT_dump("pub", pub);
  11546. wolfssl_EC_POINT_dump("key->pub_key", key->pub_key);
  11547. #endif
  11548. return SSL_SUCCESS;
  11549. }
  11550. /* End EC_KEY */
  11551. #if defined(DEBUG_WOLFSSL) && !defined(NO_FILESYSTEM)
  11552. void wolfssl_EC_POINT_dump(const char *msg, const WOLFSSL_EC_POINT *p)
  11553. {
  11554. char *num;
  11555. WOLFSSL_ENTER("wolfssl_EC_POINT_dump");
  11556. if (p == NULL) {
  11557. fprintf(stderr, "%s = NULL", msg);
  11558. return ;
  11559. }
  11560. fprintf(stderr, "%s:\n\tinSet=%d, exSet=%d\n", msg, p->inSet, p->exSet);
  11561. num = wolfSSL_BN_bn2hex(p->X);
  11562. fprintf(stderr, "\tX = %s\n", num);
  11563. XFREE(num, NULL, DYNAMIC_TYPE_ECC);
  11564. num = wolfSSL_BN_bn2hex(p->Y);
  11565. fprintf(stderr, "\tY = %s\n", num);
  11566. XFREE(num, NULL, DYNAMIC_TYPE_ECC);
  11567. }
  11568. #endif
  11569. /* Start EC_GROUP */
  11570. /* return code compliant with OpenSSL :
  11571. * 0 if equal, 1 if not and -1 in case of error
  11572. */
  11573. int wolfSSL_EC_GROUP_cmp(const WOLFSSL_EC_GROUP *a, const WOLFSSL_EC_GROUP *b,
  11574. WOLFSSL_BN_CTX *ctx)
  11575. {
  11576. (void)ctx;
  11577. WOLFSSL_ENTER("wolfSSL_EC_GROUP_cmp");
  11578. if (a == NULL || b == NULL) {
  11579. WOLFSSL_MSG("wolfSSL_EC_GROUP_cmp Bad arguments");
  11580. return SSL_FATAL_ERROR;
  11581. }
  11582. /* ok */
  11583. if ((a->curve_idx == b->curve_idx) && (a->curve_nid == b->curve_nid))
  11584. return 0;
  11585. /* ko */
  11586. return 1;
  11587. }
  11588. void wolfSSL_EC_GROUP_free(WOLFSSL_EC_GROUP *group)
  11589. {
  11590. WOLFSSL_ENTER("wolfSSL_EC_GROUP_free");
  11591. XFREE(group, NULL, DYNAMIC_TYPE_ECC);
  11592. group = NULL;
  11593. }
  11594. void wolfSSL_EC_GROUP_set_asn1_flag(WOLFSSL_EC_GROUP *group, int flag)
  11595. {
  11596. (void)group;
  11597. (void)flag;
  11598. WOLFSSL_ENTER("wolfSSL_EC_GROUP_set_asn1_flag");
  11599. WOLFSSL_MSG("wolfSSL_EC_GROUP_set_asn1_flag TBD");
  11600. }
  11601. WOLFSSL_EC_GROUP *wolfSSL_EC_GROUP_new_by_curve_name(int nid)
  11602. {
  11603. WOLFSSL_EC_GROUP *g;
  11604. int x;
  11605. WOLFSSL_ENTER("wolfSSL_EC_GROUP_new_by_curve_name");
  11606. /* curve group */
  11607. g = (WOLFSSL_EC_GROUP*) XMALLOC(sizeof(WOLFSSL_EC_GROUP), NULL,
  11608. DYNAMIC_TYPE_ECC);
  11609. if (g == NULL) {
  11610. WOLFSSL_MSG("wolfSSL_EC_GROUP_new_by_curve_name malloc failure");
  11611. return NULL;
  11612. }
  11613. XMEMSET(g, 0, sizeof(WOLFSSL_EC_GROUP));
  11614. /* set the nid of the curve */
  11615. g->curve_nid = nid;
  11616. /* search and set the corresponding internal curve idx */
  11617. for (x = 0; ecc_sets[x].size != 0; x++)
  11618. if (ecc_sets[x].nid == g->curve_nid) {
  11619. g->curve_idx = x;
  11620. break;
  11621. }
  11622. return g;
  11623. }
  11624. /* return code compliant with OpenSSL :
  11625. * the curve nid if success, 0 if error
  11626. */
  11627. int wolfSSL_EC_GROUP_get_curve_name(const WOLFSSL_EC_GROUP *group)
  11628. {
  11629. WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_curve_name");
  11630. if (group == NULL) {
  11631. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_curve_name Bad arguments");
  11632. return SSL_FAILURE;
  11633. }
  11634. return group->curve_nid;
  11635. }
  11636. /* return code compliant with OpenSSL :
  11637. * the degree of the curve if success, 0 if error
  11638. */
  11639. int wolfSSL_EC_GROUP_get_degree(const WOLFSSL_EC_GROUP *group)
  11640. {
  11641. WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_degree");
  11642. if (group == NULL || group->curve_idx < 0) {
  11643. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_degree Bad arguments");
  11644. return SSL_FAILURE;
  11645. }
  11646. switch(group->curve_nid) {
  11647. case NID_X9_62_prime256v1:
  11648. return 256;
  11649. break;
  11650. case NID_secp384r1:
  11651. return 384;
  11652. break;
  11653. case NID_secp521r1:
  11654. return 521;
  11655. break;
  11656. default :
  11657. return SSL_FAILURE;
  11658. break;
  11659. }
  11660. }
  11661. /* return code compliant with OpenSSL :
  11662. * 1 if success, 0 if error
  11663. */
  11664. int wolfSSL_EC_GROUP_get_order(const WOLFSSL_EC_GROUP *group,
  11665. WOLFSSL_BIGNUM *order, WOLFSSL_BN_CTX *ctx)
  11666. {
  11667. (void)ctx;
  11668. if (group == NULL || order == NULL || order->internal == NULL) {
  11669. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order NULL error");
  11670. return SSL_FAILURE;
  11671. }
  11672. if (mp_init((mp_int*)order->internal) != MP_OKAY) {
  11673. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_init failure");
  11674. return SSL_FAILURE;
  11675. }
  11676. if (mp_read_radix((mp_int*)order->internal,
  11677. ecc_sets[group->curve_idx].order, 16) != MP_OKAY) {
  11678. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_read order failure");
  11679. mp_clear((mp_int*)order->internal);
  11680. return SSL_FAILURE;
  11681. }
  11682. return SSL_SUCCESS;
  11683. }
  11684. /* End EC_GROUP */
  11685. /* Start EC_POINT */
  11686. /* return code compliant with OpenSSL :
  11687. * 1 if success, 0 if error
  11688. */
  11689. int wolfSSL_ECPoint_i2d(const WOLFSSL_EC_GROUP *group,
  11690. const WOLFSSL_EC_POINT *p,
  11691. unsigned char *out, unsigned int *len)
  11692. {
  11693. int err;
  11694. WOLFSSL_ENTER("wolfSSL_ECPoint_i2d");
  11695. if (group == NULL || p == NULL || len == NULL) {
  11696. WOLFSSL_MSG("wolfSSL_ECPoint_i2d NULL error");
  11697. return SSL_FAILURE;
  11698. }
  11699. if (p->inSet == 0) {
  11700. WOLFSSL_MSG("No ECPoint internal set, do it");
  11701. if (SetECPointInternal((WOLFSSL_EC_POINT *)p) != SSL_SUCCESS) {
  11702. WOLFSSL_MSG("SetECPointInternal SetECPointInternal failed");
  11703. return SSL_FAILURE;
  11704. }
  11705. }
  11706. #if defined(DEBUG_WOLFSSL) && !defined(NO_FILESYSTEM)
  11707. if (out != NULL) {
  11708. wolfssl_EC_POINT_dump("i2d p", p);
  11709. }
  11710. #endif
  11711. err = wc_ecc_export_point_der(group->curve_idx, (ecc_point*)p->internal,
  11712. out, len);
  11713. if (err != MP_OKAY && !(out == NULL && err == LENGTH_ONLY_E)) {
  11714. WOLFSSL_MSG("wolfSSL_ECPoint_i2d wc_ecc_export_point_der failed");
  11715. return SSL_FAILURE;
  11716. }
  11717. return SSL_SUCCESS;
  11718. }
  11719. /* return code compliant with OpenSSL :
  11720. * 1 if success, 0 if error
  11721. */
  11722. int wolfSSL_ECPoint_d2i(unsigned char *in, unsigned int len,
  11723. const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *p)
  11724. {
  11725. WOLFSSL_ENTER("wolfSSL_ECPoint_d2i");
  11726. if (group == NULL || p == NULL || p->internal == NULL || in == NULL) {
  11727. WOLFSSL_MSG("wolfSSL_ECPoint_d2i NULL error");
  11728. return SSL_FAILURE;
  11729. }
  11730. if (wc_ecc_import_point_der(in, len, group->curve_idx,
  11731. (ecc_point*)p->internal) != MP_OKAY) {
  11732. WOLFSSL_MSG("wc_ecc_import_point_der failed");
  11733. return SSL_FAILURE;
  11734. }
  11735. if (p->exSet == 0) {
  11736. WOLFSSL_MSG("No ECPoint external set, do it");
  11737. if (SetECPointExternal(p) != SSL_SUCCESS) {
  11738. WOLFSSL_MSG("SetECPointExternal failed");
  11739. return SSL_FAILURE;
  11740. }
  11741. }
  11742. #if defined(DEBUG_WOLFSSL) && !defined(NO_FILESYSTEM)
  11743. wolfssl_EC_POINT_dump("d2i p", p);
  11744. #endif
  11745. return SSL_SUCCESS;
  11746. }
  11747. WOLFSSL_EC_POINT *wolfSSL_EC_POINT_new(const WOLFSSL_EC_GROUP *group)
  11748. {
  11749. WOLFSSL_EC_POINT *p;
  11750. WOLFSSL_ENTER("wolfSSL_EC_POINT_new");
  11751. if (group == NULL) {
  11752. WOLFSSL_MSG("wolfSSL_EC_POINT_new NULL error");
  11753. return NULL;
  11754. }
  11755. p = (WOLFSSL_EC_POINT *)XMALLOC(sizeof(WOLFSSL_EC_POINT), NULL,
  11756. DYNAMIC_TYPE_ECC);
  11757. if (p == NULL) {
  11758. WOLFSSL_MSG("wolfSSL_EC_POINT_new malloc ecc point failure");
  11759. return NULL;
  11760. }
  11761. XMEMSET(p, 0, sizeof(WOLFSSL_EC_POINT));
  11762. p->internal = wc_ecc_new_point();
  11763. if (p->internal == NULL) {
  11764. WOLFSSL_MSG("ecc_new_point failure");
  11765. return NULL;
  11766. }
  11767. return p;
  11768. }
  11769. /* return code compliant with OpenSSL :
  11770. * 1 if success, 0 if error
  11771. */
  11772. int wolfSSL_EC_POINT_get_affine_coordinates_GFp(const WOLFSSL_EC_GROUP *group,
  11773. const WOLFSSL_EC_POINT *point,
  11774. WOLFSSL_BIGNUM *x,
  11775. WOLFSSL_BIGNUM *y,
  11776. WOLFSSL_BN_CTX *ctx)
  11777. {
  11778. (void)ctx;
  11779. WOLFSSL_ENTER("wolfSSL_EC_POINT_get_affine_coordinates_GFp");
  11780. if (group == NULL || point == NULL || point->internal == NULL ||
  11781. x == NULL || y == NULL) {
  11782. WOLFSSL_MSG("wolfSSL_EC_POINT_get_affine_coordinates_GFp NULL error");
  11783. return SSL_FAILURE;
  11784. }
  11785. if (point->inSet == 0) {
  11786. WOLFSSL_MSG("No ECPoint internal set, do it");
  11787. if (SetECPointInternal((WOLFSSL_EC_POINT *)point) != SSL_SUCCESS) {
  11788. WOLFSSL_MSG("SetECPointInternal failed");
  11789. return SSL_FAILURE;
  11790. }
  11791. }
  11792. BN_copy(x, point->X);
  11793. BN_copy(y, point->Y);
  11794. return SSL_SUCCESS;
  11795. }
  11796. /* return code compliant with OpenSSL :
  11797. * 1 if success, 0 if error
  11798. */
  11799. int wolfSSL_EC_POINT_mul(const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *r,
  11800. const WOLFSSL_BIGNUM *n, const WOLFSSL_EC_POINT *q,
  11801. const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
  11802. {
  11803. mp_int prime;
  11804. (void)ctx;
  11805. (void)n;
  11806. WOLFSSL_ENTER("wolfSSL_EC_POINT_mul");
  11807. if (group == NULL || r == NULL || r->internal == NULL ||
  11808. q == NULL || q->internal == NULL || m == NULL) {
  11809. WOLFSSL_MSG("wolfSSL_EC_POINT_mul NULL error");
  11810. return SSL_FAILURE;
  11811. }
  11812. if (q->inSet == 0) {
  11813. WOLFSSL_MSG("No ECPoint internal set, do it");
  11814. if (SetECPointInternal((WOLFSSL_EC_POINT *)q) != SSL_SUCCESS) {
  11815. WOLFSSL_MSG("SetECPointInternal failed");
  11816. return SSL_FAILURE;
  11817. }
  11818. }
  11819. /* compute the prime value of the curve */
  11820. if (mp_init(&prime) != MP_OKAY) {
  11821. WOLFSSL_MSG("wolfSSL_EC_POINT_mul init BN failed");
  11822. return SSL_FAILURE;
  11823. }
  11824. if (mp_read_radix(&prime, ecc_sets[group->curve_idx].prime, 16) != MP_OKAY){
  11825. WOLFSSL_MSG("wolfSSL_EC_POINT_mul read prime curve value failed");
  11826. return SSL_FAILURE;
  11827. }
  11828. /* r = q * m % prime */
  11829. if (wc_ecc_mulmod((mp_int*)m->internal, (ecc_point*)q->internal,
  11830. (ecc_point*)r->internal, &prime, 1) != MP_OKAY) {
  11831. WOLFSSL_MSG("ecc_mulmod failure");
  11832. mp_clear(&prime);
  11833. return SSL_FAILURE;
  11834. }
  11835. mp_clear(&prime);
  11836. /* set the external value for the computed point */
  11837. if (SetECPointInternal(r) != SSL_SUCCESS) {
  11838. WOLFSSL_MSG("SetECPointInternal failed");
  11839. return SSL_FAILURE;
  11840. }
  11841. return SSL_SUCCESS;
  11842. }
  11843. void wolfSSL_EC_POINT_clear_free(WOLFSSL_EC_POINT *p)
  11844. {
  11845. WOLFSSL_ENTER("wolfSSL_EC_POINT_clear_free");
  11846. wolfSSL_EC_POINT_free(p);
  11847. }
  11848. /* return code compliant with OpenSSL :
  11849. * 0 if equal, 1 if not and -1 in case of error
  11850. */
  11851. int wolfSSL_EC_POINT_cmp(const WOLFSSL_EC_GROUP *group,
  11852. const WOLFSSL_EC_POINT *a, const WOLFSSL_EC_POINT *b,
  11853. WOLFSSL_BN_CTX *ctx)
  11854. {
  11855. int ret;
  11856. (void)ctx;
  11857. WOLFSSL_ENTER("wolfSSL_EC_POINT_cmp");
  11858. if (group == NULL || a == NULL || a->internal == NULL || b == NULL ||
  11859. b->internal == NULL) {
  11860. WOLFSSL_MSG("wolfSSL_EC_POINT_cmp Bad arguments");
  11861. return SSL_FATAL_ERROR;
  11862. }
  11863. ret = wc_ecc_cmp_point((ecc_point*)a->internal, (ecc_point*)b->internal);
  11864. if (ret == MP_EQ)
  11865. return 0;
  11866. else if (ret == MP_LT || ret == MP_GT)
  11867. return 1;
  11868. return SSL_FATAL_ERROR;
  11869. }
  11870. void wolfSSL_EC_POINT_free(WOLFSSL_EC_POINT *p)
  11871. {
  11872. WOLFSSL_ENTER("wolfSSL_EC_POINT_free");
  11873. if (p != NULL) {
  11874. if (p->internal == NULL) {
  11875. wc_ecc_del_point((ecc_point*)p->internal);
  11876. XFREE(p->internal, NULL, DYNAMIC_TYPE_ECC);
  11877. p->internal = NULL;
  11878. }
  11879. wolfSSL_BN_free(p->X);
  11880. wolfSSL_BN_free(p->Y);
  11881. wolfSSL_BN_free(p->Z);
  11882. p->X = NULL;
  11883. p->Y = NULL;
  11884. p->Z = NULL;
  11885. p->inSet = p->exSet = 0;
  11886. XFREE(p, NULL, DYNAMIC_TYPE_ECC);
  11887. p = NULL;
  11888. }
  11889. }
  11890. /* return code compliant with OpenSSL :
  11891. * 1 if point at infinity, 0 else
  11892. */
  11893. int wolfSSL_EC_POINT_is_at_infinity(const WOLFSSL_EC_GROUP *group,
  11894. const WOLFSSL_EC_POINT *point)
  11895. {
  11896. int ret;
  11897. WOLFSSL_ENTER("wolfSSL_EC_POINT_is_at_infinity");
  11898. if (group == NULL || point == NULL || point->internal == NULL) {
  11899. WOLFSSL_MSG("wolfSSL_EC_POINT_is_at_infinity NULL error");
  11900. return SSL_FAILURE;
  11901. }
  11902. if (point->inSet == 0) {
  11903. WOLFSSL_MSG("No ECPoint internal set, do it");
  11904. if (SetECPointInternal((WOLFSSL_EC_POINT *)point) != SSL_SUCCESS) {
  11905. WOLFSSL_MSG("SetECPointInternal failed");
  11906. return SSL_FAILURE;
  11907. }
  11908. }
  11909. ret = wc_ecc_point_is_at_infinity((ecc_point*)point->internal);
  11910. if (ret <= 0) {
  11911. WOLFSSL_MSG("ecc_point_is_at_infinity failure");
  11912. return SSL_FAILURE;
  11913. }
  11914. return SSL_SUCCESS;
  11915. }
  11916. /* End EC_POINT */
  11917. /* Start ECDSA_SIG */
  11918. void wolfSSL_ECDSA_SIG_free(WOLFSSL_ECDSA_SIG *sig)
  11919. {
  11920. WOLFSSL_ENTER("wolfSSL_ECDSA_SIG_free");
  11921. if (sig) {
  11922. wolfSSL_BN_free(sig->r);
  11923. wolfSSL_BN_free(sig->s);
  11924. XFREE(sig, NULL, DYNAMIC_TYPE_ECC);
  11925. }
  11926. }
  11927. WOLFSSL_ECDSA_SIG *wolfSSL_ECDSA_SIG_new(void)
  11928. {
  11929. WOLFSSL_ECDSA_SIG *sig;
  11930. WOLFSSL_ENTER("wolfSSL_ECDSA_SIG_new");
  11931. sig = (WOLFSSL_ECDSA_SIG*) XMALLOC(sizeof(WOLFSSL_ECDSA_SIG), NULL,
  11932. DYNAMIC_TYPE_ECC);
  11933. if (sig == NULL) {
  11934. WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA signature failure");
  11935. return NULL;
  11936. }
  11937. sig->r = wolfSSL_BN_new();
  11938. if (sig->r == NULL) {
  11939. WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA r failure");
  11940. wolfSSL_ECDSA_SIG_free(sig);
  11941. return NULL;
  11942. }
  11943. sig->s = wolfSSL_BN_new();
  11944. if (sig->s == NULL) {
  11945. WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA s failure");
  11946. wolfSSL_ECDSA_SIG_free(sig);
  11947. return NULL;
  11948. }
  11949. return sig;
  11950. }
  11951. /* return signature structure on success, NULL otherwise */
  11952. WOLFSSL_ECDSA_SIG *wolfSSL_ECDSA_do_sign(const unsigned char *d, int dlen,
  11953. WOLFSSL_EC_KEY *key)
  11954. {
  11955. WOLFSSL_ECDSA_SIG *sig = NULL;
  11956. int initTmpRng = 0;
  11957. WC_RNG* rng = NULL;
  11958. #ifdef WOLFSSL_SMALL_STACK
  11959. WC_RNG* tmpRNG = NULL;
  11960. #else
  11961. WC_RNG tmpRNG[1];
  11962. #endif
  11963. WOLFSSL_ENTER("wolfSSL_ECDSA_do_sign");
  11964. if (d == NULL || key == NULL || key->internal == NULL) {
  11965. WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Bad arguments");
  11966. return NULL;
  11967. }
  11968. /* set internal key if not done */
  11969. if (key->inSet == 0)
  11970. {
  11971. WOLFSSL_MSG("wolfSSL_ECDSA_do_sign No EC key internal set, do it");
  11972. if (SetECKeyInternal(key) != SSL_SUCCESS) {
  11973. WOLFSSL_MSG("wolfSSL_ECDSA_do_sign SetECKeyInternal failed");
  11974. return NULL;
  11975. }
  11976. }
  11977. #ifdef WOLFSSL_SMALL_STACK
  11978. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11979. if (tmpRNG == NULL)
  11980. return NULL;
  11981. #endif
  11982. if (wc_InitRng(tmpRNG) == 0) {
  11983. rng = tmpRNG;
  11984. initTmpRng = 1;
  11985. }
  11986. else {
  11987. WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Bad RNG Init, trying global");
  11988. if (initGlobalRNG == 0)
  11989. WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Global RNG no Init");
  11990. else
  11991. rng = &globalRNG;
  11992. }
  11993. if (rng) {
  11994. mp_int sig_r, sig_s;
  11995. if (mp_init_multi(&sig_r, &sig_s, NULL, NULL, NULL, NULL) == MP_OKAY) {
  11996. if (wc_ecc_sign_hash_ex(d, dlen, rng, (ecc_key*)key->internal,
  11997. &sig_r, &sig_s) != MP_OKAY) {
  11998. WOLFSSL_MSG("wc_ecc_sign_hash_ex failed");
  11999. }
  12000. else {
  12001. /* put signature blob in ECDSA structure */
  12002. sig = wolfSSL_ECDSA_SIG_new();
  12003. if (sig == NULL)
  12004. WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new failed");
  12005. else if (SetIndividualExternal(&(sig->r), &sig_r)!=SSL_SUCCESS){
  12006. WOLFSSL_MSG("ecdsa r key error");
  12007. wolfSSL_ECDSA_SIG_free(sig);
  12008. }
  12009. else if (SetIndividualExternal(&(sig->s), &sig_s)!=SSL_SUCCESS){
  12010. WOLFSSL_MSG("ecdsa s key error");
  12011. wolfSSL_ECDSA_SIG_free(sig);
  12012. }
  12013. mp_clear(&sig_r);
  12014. mp_clear(&sig_s);
  12015. }
  12016. }
  12017. }
  12018. if (initTmpRng)
  12019. wc_FreeRng(tmpRNG);
  12020. #ifdef WOLFSSL_SMALL_STACK
  12021. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12022. #endif
  12023. return sig;
  12024. }
  12025. /* return code compliant with OpenSSL :
  12026. * 1 for a valid signature, 0 for an invalid signature and -1 on error
  12027. */
  12028. int wolfSSL_ECDSA_do_verify(const unsigned char *d, int dlen,
  12029. const WOLFSSL_ECDSA_SIG *sig, WOLFSSL_EC_KEY *key)
  12030. {
  12031. int check_sign = 0;
  12032. WOLFSSL_ENTER("wolfSSL_ECDSA_do_verify");
  12033. if (d == NULL || sig == NULL || key == NULL || key->internal == NULL) {
  12034. WOLFSSL_MSG("wolfSSL_ECDSA_do_verify Bad arguments");
  12035. return SSL_FATAL_ERROR;
  12036. }
  12037. /* set internal key if not done */
  12038. if (key->inSet == 0)
  12039. {
  12040. WOLFSSL_MSG("No EC key internal set, do it");
  12041. if (SetECKeyInternal(key) != SSL_SUCCESS) {
  12042. WOLFSSL_MSG("SetECKeyInternal failed");
  12043. return SSL_FATAL_ERROR;
  12044. }
  12045. }
  12046. if (wc_ecc_verify_hash_ex((mp_int*)sig->r->internal,
  12047. (mp_int*)sig->s->internal, d, dlen, &check_sign,
  12048. (ecc_key *)key->internal) != MP_OKAY) {
  12049. WOLFSSL_MSG("wc_ecc_verify_hash failed");
  12050. return SSL_FATAL_ERROR;
  12051. }
  12052. else if (check_sign == 0) {
  12053. WOLFSSL_MSG("wc_ecc_verify_hash incorrect signature detected");
  12054. return SSL_FAILURE;
  12055. }
  12056. return SSL_SUCCESS;
  12057. }
  12058. /* End ECDSA_SIG */
  12059. /* Start ECDH */
  12060. /* return code compliant with OpenSSL :
  12061. * length of computed key if success, -1 if error
  12062. */
  12063. int wolfSSL_ECDH_compute_key(void *out, size_t outlen,
  12064. const WOLFSSL_EC_POINT *pub_key,
  12065. WOLFSSL_EC_KEY *ecdh,
  12066. void *(*KDF) (const void *in, size_t inlen,
  12067. void *out, size_t *outlen))
  12068. {
  12069. word32 len;
  12070. (void)KDF;
  12071. (void)KDF;
  12072. WOLFSSL_ENTER("wolfSSL_ECDH_compute_key");
  12073. if (out == NULL || pub_key == NULL || pub_key->internal == NULL ||
  12074. ecdh == NULL || ecdh->internal == NULL) {
  12075. WOLFSSL_MSG("Bad function arguments");
  12076. return SSL_FATAL_ERROR;
  12077. }
  12078. /* set internal key if not done */
  12079. if (ecdh->inSet == 0)
  12080. {
  12081. WOLFSSL_MSG("No EC key internal set, do it");
  12082. if (SetECKeyInternal(ecdh) != SSL_SUCCESS) {
  12083. WOLFSSL_MSG("SetECKeyInternal failed");
  12084. return SSL_FATAL_ERROR;
  12085. }
  12086. }
  12087. len = (word32)outlen;
  12088. if (wc_ecc_shared_secret_ssh((ecc_key*)ecdh->internal,
  12089. (ecc_point*)pub_key->internal,
  12090. (byte *)out, &len) != MP_OKAY) {
  12091. WOLFSSL_MSG("wc_ecc_shared_secret failed");
  12092. return SSL_FATAL_ERROR;
  12093. }
  12094. return len;
  12095. }
  12096. /* End ECDH */
  12097. #if !defined(NO_FILESYSTEM)
  12098. /* return code compliant with OpenSSL :
  12099. * 1 if success, 0 if error
  12100. */
  12101. int wolfSSL_PEM_write_EC_PUBKEY(FILE *fp, WOLFSSL_EC_KEY *x)
  12102. {
  12103. (void)fp;
  12104. (void)x;
  12105. WOLFSSL_MSG("wolfSSL_PEM_write_EC_PUBKEY not implemented");
  12106. return SSL_FAILURE;
  12107. }
  12108. #endif /* NO_FILESYSTEM */
  12109. #if defined(WOLFSSL_KEY_GEN)
  12110. /* return code compliant with OpenSSL :
  12111. * 1 if success, 0 if error
  12112. */
  12113. int wolfSSL_PEM_write_bio_ECPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EC_KEY* ecc,
  12114. const EVP_CIPHER* cipher,
  12115. unsigned char* passwd, int len,
  12116. pem_password_cb cb, void* arg)
  12117. {
  12118. (void)bio;
  12119. (void)ecc;
  12120. (void)cipher;
  12121. (void)passwd;
  12122. (void)len;
  12123. (void)cb;
  12124. (void)arg;
  12125. WOLFSSL_MSG("wolfSSL_PEM_write_bio_ECPrivateKey not implemented");
  12126. return SSL_FAILURE;
  12127. }
  12128. /* return code compliant with OpenSSL :
  12129. * 1 if success, 0 if error
  12130. */
  12131. int wolfSSL_PEM_write_mem_ECPrivateKey(WOLFSSL_EC_KEY* ecc,
  12132. const EVP_CIPHER* cipher,
  12133. unsigned char* passwd, int passwdSz,
  12134. unsigned char **pem, int *plen)
  12135. {
  12136. byte *der, *tmp, *cipherInfo = NULL;
  12137. int der_max_len = 0, derSz = 0;
  12138. WOLFSSL_MSG("wolfSSL_PEM_write_mem_ECPrivateKey");
  12139. if (pem == NULL || plen == NULL || ecc == NULL || ecc->internal == NULL) {
  12140. WOLFSSL_MSG("Bad function arguments");
  12141. return SSL_FAILURE;
  12142. }
  12143. if (ecc->inSet == 0) {
  12144. WOLFSSL_MSG("No ECC internal set, do it");
  12145. if (SetECKeyInternal(ecc) != SSL_SUCCESS) {
  12146. WOLFSSL_MSG("SetDsaInternal failed");
  12147. return SSL_FAILURE;
  12148. }
  12149. }
  12150. /* 4 > size of pub, priv + ASN.1 additionnal informations
  12151. */
  12152. der_max_len = 4 * wc_ecc_size((ecc_key*)ecc->internal) + AES_BLOCK_SIZE;
  12153. der = (byte*)XMALLOC(der_max_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12154. if (der == NULL) {
  12155. WOLFSSL_MSG("malloc failed");
  12156. return SSL_FAILURE;
  12157. }
  12158. /* Key to DER */
  12159. derSz = wc_EccKeyToDer((ecc_key*)ecc->internal, der, der_max_len);
  12160. if (derSz < 0) {
  12161. WOLFSSL_MSG("wc_DsaKeyToDer failed");
  12162. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12163. return SSL_FAILURE;
  12164. }
  12165. /* encrypt DER buffer if required */
  12166. if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
  12167. int ret;
  12168. ret = EncryptDerKey(der, &derSz, cipher,
  12169. passwd, passwdSz, &cipherInfo);
  12170. if (ret != SSL_SUCCESS) {
  12171. WOLFSSL_MSG("EncryptDerKey failed");
  12172. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12173. return ret;
  12174. }
  12175. /* tmp buffer with a max size */
  12176. *plen = (derSz * 2) + sizeof(BEGIN_EC_PRIV) +
  12177. sizeof(END_EC_PRIV) + HEADER_ENCRYPTED_KEY_SIZE;
  12178. }
  12179. else /* tmp buffer with a max size */
  12180. *plen = (derSz * 2) + sizeof(BEGIN_EC_PRIV) + sizeof(END_EC_PRIV);
  12181. tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12182. if (tmp == NULL) {
  12183. WOLFSSL_MSG("malloc failed");
  12184. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12185. if (cipherInfo != NULL)
  12186. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12187. return SSL_FAILURE;
  12188. }
  12189. /* DER to PEM */
  12190. *plen = wc_DerToPemEx(der, derSz, tmp, *plen, cipherInfo, ECC_PRIVATEKEY_TYPE);
  12191. if (*plen <= 0) {
  12192. WOLFSSL_MSG("wc_DerToPemEx failed");
  12193. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12194. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12195. if (cipherInfo != NULL)
  12196. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12197. return SSL_FAILURE;
  12198. }
  12199. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12200. if (cipherInfo != NULL)
  12201. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12202. *pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  12203. if (*pem == NULL) {
  12204. WOLFSSL_MSG("malloc failed");
  12205. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12206. return SSL_FAILURE;
  12207. }
  12208. XMEMSET(*pem, 0, (*plen)+1);
  12209. if (XMEMCPY(*pem, tmp, *plen) == NULL) {
  12210. WOLFSSL_MSG("XMEMCPY failed");
  12211. XFREE(pem, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  12212. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12213. return SSL_FAILURE;
  12214. }
  12215. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12216. return SSL_SUCCESS;
  12217. }
  12218. #ifndef NO_FILESYSTEM
  12219. /* return code compliant with OpenSSL :
  12220. * 1 if success, 0 if error
  12221. */
  12222. int wolfSSL_PEM_write_ECPrivateKey(FILE *fp, WOLFSSL_EC_KEY *ecc,
  12223. const EVP_CIPHER *enc,
  12224. unsigned char *kstr, int klen,
  12225. pem_password_cb *cb, void *u)
  12226. {
  12227. byte *pem;
  12228. int plen, ret;
  12229. (void)cb;
  12230. (void)u;
  12231. WOLFSSL_MSG("wolfSSL_PEM_write_ECPrivateKey");
  12232. if (fp == NULL || ecc == NULL || ecc->internal == NULL) {
  12233. WOLFSSL_MSG("Bad function arguments");
  12234. return SSL_FAILURE;
  12235. }
  12236. ret = wolfSSL_PEM_write_mem_ECPrivateKey(ecc, enc, kstr, klen, &pem, &plen);
  12237. if (ret != SSL_SUCCESS) {
  12238. WOLFSSL_MSG("wolfSSL_PEM_write_mem_ECPrivateKey failed");
  12239. return SSL_FAILURE;
  12240. }
  12241. ret = (int)XFWRITE(pem, plen, 1, fp);
  12242. if (ret != 1) {
  12243. WOLFSSL_MSG("ECC private key file write failed");
  12244. return SSL_FAILURE;
  12245. }
  12246. XFREE(pem, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  12247. return SSL_SUCCESS;
  12248. }
  12249. #endif /* NO_FILESYSTEM */
  12250. #endif /* defined(WOLFSSL_KEY_GEN) */
  12251. #endif /* HAVE_ECC */
  12252. #ifndef NO_DSA
  12253. #if defined(WOLFSSL_KEY_GEN)
  12254. /* return code compliant with OpenSSL :
  12255. * 1 if success, 0 if error
  12256. */
  12257. int wolfSSL_PEM_write_bio_DSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_DSA* dsa,
  12258. const EVP_CIPHER* cipher,
  12259. unsigned char* passwd, int len,
  12260. pem_password_cb cb, void* arg)
  12261. {
  12262. (void)bio;
  12263. (void)dsa;
  12264. (void)cipher;
  12265. (void)passwd;
  12266. (void)len;
  12267. (void)cb;
  12268. (void)arg;
  12269. WOLFSSL_MSG("wolfSSL_PEM_write_bio_DSAPrivateKey not implemented");
  12270. return SSL_FAILURE;
  12271. }
  12272. /* return code compliant with OpenSSL :
  12273. * 1 if success, 0 if error
  12274. */
  12275. int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
  12276. const EVP_CIPHER* cipher,
  12277. unsigned char* passwd, int passwdSz,
  12278. unsigned char **pem, int *plen)
  12279. {
  12280. byte *der, *tmp, *cipherInfo = NULL;
  12281. int der_max_len = 0, derSz = 0;
  12282. WOLFSSL_MSG("wolfSSL_PEM_write_mem_DSAPrivateKey");
  12283. if (pem == NULL || plen == NULL || dsa == NULL || dsa->internal == NULL) {
  12284. WOLFSSL_MSG("Bad function arguments");
  12285. return SSL_FAILURE;
  12286. }
  12287. if (dsa->inSet == 0) {
  12288. WOLFSSL_MSG("No DSA internal set, do it");
  12289. if (SetDsaInternal(dsa) != SSL_SUCCESS) {
  12290. WOLFSSL_MSG("SetDsaInternal failed");
  12291. return SSL_FAILURE;
  12292. }
  12293. }
  12294. /* 4 > size of pub, priv, p, q, g + ASN.1 additionnal informations
  12295. */
  12296. der_max_len = 4 * wolfSSL_BN_num_bytes(dsa->g) + AES_BLOCK_SIZE;
  12297. der = (byte*)XMALLOC(der_max_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12298. if (der == NULL) {
  12299. WOLFSSL_MSG("malloc failed");
  12300. return SSL_FAILURE;
  12301. }
  12302. /* Key to DER */
  12303. derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, der, der_max_len);
  12304. if (derSz < 0) {
  12305. WOLFSSL_MSG("wc_DsaKeyToDer failed");
  12306. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12307. return SSL_FAILURE;
  12308. }
  12309. /* encrypt DER buffer if required */
  12310. if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
  12311. int ret;
  12312. ret = EncryptDerKey(der, &derSz, cipher,
  12313. passwd, passwdSz, &cipherInfo);
  12314. if (ret != SSL_SUCCESS) {
  12315. WOLFSSL_MSG("EncryptDerKey failed");
  12316. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12317. return ret;
  12318. }
  12319. /* tmp buffer with a max size */
  12320. *plen = (derSz * 2) + sizeof(BEGIN_DSA_PRIV) +
  12321. sizeof(END_DSA_PRIV) + HEADER_ENCRYPTED_KEY_SIZE;
  12322. }
  12323. else /* tmp buffer with a max size */
  12324. *plen = (derSz * 2) + sizeof(BEGIN_DSA_PRIV) + sizeof(END_DSA_PRIV);
  12325. tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12326. if (tmp == NULL) {
  12327. WOLFSSL_MSG("malloc failed");
  12328. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12329. if (cipherInfo != NULL)
  12330. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12331. return SSL_FAILURE;
  12332. }
  12333. /* DER to PEM */
  12334. *plen = wc_DerToPemEx(der, derSz, tmp, *plen, cipherInfo, DSA_PRIVATEKEY_TYPE);
  12335. if (*plen <= 0) {
  12336. WOLFSSL_MSG("wc_DerToPemEx failed");
  12337. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12338. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12339. if (cipherInfo != NULL)
  12340. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12341. return SSL_FAILURE;
  12342. }
  12343. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12344. if (cipherInfo != NULL)
  12345. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12346. *pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  12347. if (*pem == NULL) {
  12348. WOLFSSL_MSG("malloc failed");
  12349. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12350. return SSL_FAILURE;
  12351. }
  12352. XMEMSET(*pem, 0, (*plen)+1);
  12353. if (XMEMCPY(*pem, tmp, *plen) == NULL) {
  12354. WOLFSSL_MSG("XMEMCPY failed");
  12355. XFREE(pem, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  12356. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12357. return SSL_FAILURE;
  12358. }
  12359. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12360. return SSL_SUCCESS;
  12361. }
  12362. #ifndef NO_FILESYSTEM
  12363. /* return code compliant with OpenSSL :
  12364. * 1 if success, 0 if error
  12365. */
  12366. int wolfSSL_PEM_write_DSAPrivateKey(FILE *fp, WOLFSSL_DSA *dsa,
  12367. const EVP_CIPHER *enc,
  12368. unsigned char *kstr, int klen,
  12369. pem_password_cb *cb, void *u)
  12370. {
  12371. byte *pem;
  12372. int plen, ret;
  12373. (void)cb;
  12374. (void)u;
  12375. WOLFSSL_MSG("wolfSSL_PEM_write_DSAPrivateKey");
  12376. if (fp == NULL || dsa == NULL || dsa->internal == NULL) {
  12377. WOLFSSL_MSG("Bad function arguments");
  12378. return SSL_FAILURE;
  12379. }
  12380. ret = wolfSSL_PEM_write_mem_DSAPrivateKey(dsa, enc, kstr, klen, &pem, &plen);
  12381. if (ret != SSL_SUCCESS) {
  12382. WOLFSSL_MSG("wolfSSL_PEM_write_mem_DSAPrivateKey failed");
  12383. return SSL_FAILURE;
  12384. }
  12385. ret = (int)XFWRITE(pem, plen, 1, fp);
  12386. if (ret != 1) {
  12387. WOLFSSL_MSG("DSA private key file write failed");
  12388. return SSL_FAILURE;
  12389. }
  12390. XFREE(pem, NULL, DYNAMIC_TYPE_OUT_BUFFER);
  12391. return SSL_SUCCESS;
  12392. }
  12393. #endif /* NO_FILESYSTEM */
  12394. #endif /* defined(WOLFSSL_KEY_GEN) */
  12395. #ifndef NO_FILESYSTEM
  12396. /* return code compliant with OpenSSL :
  12397. * 1 if success, 0 if error
  12398. */
  12399. int wolfSSL_PEM_write_DSA_PUBKEY(FILE *fp, WOLFSSL_DSA *x)
  12400. {
  12401. (void)fp;
  12402. (void)x;
  12403. WOLFSSL_MSG("wolfSSL_PEM_write_DSA_PUBKEY not implemented");
  12404. return SSL_FAILURE;
  12405. }
  12406. #endif /* NO_FILESYSTEM */
  12407. #endif /* #ifndef NO_DSA */
  12408. WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_bio_PrivateKey(WOLFSSL_BIO* bio,
  12409. WOLFSSL_EVP_PKEY** key, pem_password_cb cb, void* arg)
  12410. {
  12411. (void)bio;
  12412. (void)key;
  12413. (void)cb;
  12414. (void)arg;
  12415. WOLFSSL_MSG("wolfSSL_PEM_read_bio_PrivateKey not implemented");
  12416. return NULL;
  12417. }
  12418. int wolfSSL_EVP_PKEY_type(int type)
  12419. {
  12420. (void)type;
  12421. WOLFSSL_MSG("wolfSSL_EVP_PKEY_type not implemented");
  12422. return SSL_FATAL_ERROR;
  12423. }
  12424. #if !defined(NO_FILESYSTEM)
  12425. WOLFSSL_EVP_PKEY *wolfSSL_PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
  12426. pem_password_cb *cb, void *u)
  12427. {
  12428. (void)fp;
  12429. (void)x;
  12430. (void)cb;
  12431. (void)u;
  12432. WOLFSSL_MSG("wolfSSL_PEM_read_PUBKEY not implemented");
  12433. return NULL;
  12434. }
  12435. #endif /* NO_FILESYSTEM */
  12436. #ifndef NO_RSA
  12437. #if !defined(NO_FILESYSTEM)
  12438. WOLFSSL_RSA *wolfSSL_PEM_read_RSAPublicKey(FILE *fp, WOLFSSL_RSA **x,
  12439. pem_password_cb *cb, void *u)
  12440. {
  12441. (void)fp;
  12442. (void)x;
  12443. (void)cb;
  12444. (void)u;
  12445. WOLFSSL_MSG("wolfSSL_PEM_read_RSAPublicKey not implemented");
  12446. return NULL;
  12447. }
  12448. /* return code compliant with OpenSSL :
  12449. * 1 if success, 0 if error
  12450. */
  12451. int wolfSSL_PEM_write_RSAPublicKey(FILE *fp, WOLFSSL_RSA *x)
  12452. {
  12453. (void)fp;
  12454. (void)x;
  12455. WOLFSSL_MSG("wolfSSL_PEM_write_RSAPublicKey not implemented");
  12456. return SSL_FAILURE;
  12457. }
  12458. /* return code compliant with OpenSSL :
  12459. * 1 if success, 0 if error
  12460. */
  12461. int wolfSSL_PEM_write_RSA_PUBKEY(FILE *fp, WOLFSSL_RSA *x)
  12462. {
  12463. (void)fp;
  12464. (void)x;
  12465. WOLFSSL_MSG("wolfSSL_PEM_write_RSA_PUBKEY not implemented");
  12466. return SSL_FAILURE;
  12467. }
  12468. #endif /* NO_FILESYSTEM */
  12469. /* return SSL_SUCCESS if success, SSL_FATAL_ERROR if error */
  12470. int wolfSSL_RSA_LoadDer(WOLFSSL_RSA* rsa, const unsigned char* der, int derSz)
  12471. {
  12472. word32 idx = 0;
  12473. int ret;
  12474. WOLFSSL_ENTER("wolfSSL_RSA_LoadDer");
  12475. if (rsa == NULL || rsa->internal == NULL || der == NULL || derSz <= 0) {
  12476. WOLFSSL_MSG("Bad function arguments");
  12477. return SSL_FATAL_ERROR;
  12478. }
  12479. ret = wc_RsaPrivateKeyDecode(der, &idx, (RsaKey*)rsa->internal, derSz);
  12480. if (ret < 0) {
  12481. WOLFSSL_MSG("RsaPrivateKeyDecode failed");
  12482. return SSL_FATAL_ERROR;
  12483. }
  12484. if (SetRsaExternal(rsa) != SSL_SUCCESS) {
  12485. WOLFSSL_MSG("SetRsaExternal failed");
  12486. return SSL_FATAL_ERROR;
  12487. }
  12488. rsa->inSet = 1;
  12489. return SSL_SUCCESS;
  12490. }
  12491. #endif /* NO_RSA */
  12492. #ifndef NO_DSA
  12493. /* return SSL_SUCCESS if success, SSL_FATAL_ERROR if error */
  12494. int wolfSSL_DSA_LoadDer(WOLFSSL_DSA* dsa, const unsigned char* der, int derSz)
  12495. {
  12496. word32 idx = 0;
  12497. int ret;
  12498. WOLFSSL_ENTER("wolfSSL_DSA_LoadDer");
  12499. if (dsa == NULL || dsa->internal == NULL || der == NULL || derSz <= 0) {
  12500. WOLFSSL_MSG("Bad function arguments");
  12501. return SSL_FATAL_ERROR;
  12502. }
  12503. ret = DsaPrivateKeyDecode(der, &idx, (DsaKey*)dsa->internal, derSz);
  12504. if (ret < 0) {
  12505. WOLFSSL_MSG("DsaPrivateKeyDecode failed");
  12506. return SSL_FATAL_ERROR;
  12507. }
  12508. if (SetDsaExternal(dsa) != SSL_SUCCESS) {
  12509. WOLFSSL_MSG("SetDsaExternal failed");
  12510. return SSL_FATAL_ERROR;
  12511. }
  12512. dsa->inSet = 1;
  12513. return SSL_SUCCESS;
  12514. }
  12515. #endif /* NO_DSA */
  12516. #ifdef HAVE_ECC
  12517. /* return SSL_SUCCESS if success, SSL_FATAL_ERROR if error */
  12518. int wolfSSL_EC_KEY_LoadDer(WOLFSSL_EC_KEY* key,
  12519. const unsigned char* der, int derSz)
  12520. {
  12521. word32 idx = 0;
  12522. int ret;
  12523. WOLFSSL_ENTER("wolfSSL_EC_KEY_LoadDer");
  12524. if (key == NULL || key->internal == NULL || der == NULL || derSz <= 0) {
  12525. WOLFSSL_MSG("Bad function arguments");
  12526. return SSL_FATAL_ERROR;
  12527. }
  12528. ret = wc_EccPrivateKeyDecode(der, &idx, (ecc_key*)key->internal, derSz);
  12529. if (ret < 0) {
  12530. WOLFSSL_MSG("wc_EccPrivateKeyDecode failed");
  12531. return SSL_FATAL_ERROR;
  12532. }
  12533. if (SetECKeyExternal(key) != SSL_SUCCESS) {
  12534. WOLFSSL_MSG("SetECKeyExternal failed");
  12535. return SSL_FATAL_ERROR;
  12536. }
  12537. key->inSet = 1;
  12538. return SSL_SUCCESS;
  12539. }
  12540. #endif /* HAVE_ECC */
  12541. #endif /* OPENSSL_EXTRA */
  12542. #ifdef SESSION_CERTS
  12543. /* Get peer's certificate chain */
  12544. WOLFSSL_X509_CHAIN* wolfSSL_get_peer_chain(WOLFSSL* ssl)
  12545. {
  12546. WOLFSSL_ENTER("wolfSSL_get_peer_chain");
  12547. if (ssl)
  12548. return &ssl->session.chain;
  12549. return 0;
  12550. }
  12551. /* Get peer's certificate chain total count */
  12552. int wolfSSL_get_chain_count(WOLFSSL_X509_CHAIN* chain)
  12553. {
  12554. WOLFSSL_ENTER("wolfSSL_get_chain_count");
  12555. if (chain)
  12556. return chain->count;
  12557. return 0;
  12558. }
  12559. /* Get peer's ASN.1 DER ceritifcate at index (idx) length in bytes */
  12560. int wolfSSL_get_chain_length(WOLFSSL_X509_CHAIN* chain, int idx)
  12561. {
  12562. WOLFSSL_ENTER("wolfSSL_get_chain_length");
  12563. if (chain)
  12564. return chain->certs[idx].length;
  12565. return 0;
  12566. }
  12567. /* Get peer's ASN.1 DER ceritifcate at index (idx) */
  12568. byte* wolfSSL_get_chain_cert(WOLFSSL_X509_CHAIN* chain, int idx)
  12569. {
  12570. WOLFSSL_ENTER("wolfSSL_get_chain_cert");
  12571. if (chain)
  12572. return chain->certs[idx].buffer;
  12573. return 0;
  12574. }
  12575. /* Get peer's wolfSSL X509 ceritifcate at index (idx) */
  12576. WOLFSSL_X509* wolfSSL_get_chain_X509(WOLFSSL_X509_CHAIN* chain, int idx)
  12577. {
  12578. int ret;
  12579. WOLFSSL_X509* x509 = NULL;
  12580. #ifdef WOLFSSL_SMALL_STACK
  12581. DecodedCert* cert = NULL;
  12582. #else
  12583. DecodedCert cert[1];
  12584. #endif
  12585. WOLFSSL_ENTER("wolfSSL_get_chain_X509");
  12586. if (chain != NULL) {
  12587. #ifdef WOLFSSL_SMALL_STACK
  12588. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  12589. DYNAMIC_TYPE_TMP_BUFFER);
  12590. if (cert != NULL)
  12591. #endif
  12592. {
  12593. InitDecodedCert(cert, chain->certs[idx].buffer,
  12594. chain->certs[idx].length, NULL);
  12595. if ((ret = ParseCertRelative(cert, CERT_TYPE, 0, NULL)) != 0)
  12596. WOLFSSL_MSG("Failed to parse cert");
  12597. else {
  12598. x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
  12599. DYNAMIC_TYPE_X509);
  12600. if (x509 == NULL) {
  12601. WOLFSSL_MSG("Failed alloc X509");
  12602. }
  12603. else {
  12604. InitX509(x509, 1);
  12605. if ((ret = CopyDecodedToX509(x509, cert)) != 0) {
  12606. WOLFSSL_MSG("Failed to copy decoded");
  12607. XFREE(x509, NULL, DYNAMIC_TYPE_X509);
  12608. x509 = NULL;
  12609. }
  12610. }
  12611. }
  12612. FreeDecodedCert(cert);
  12613. #ifdef WOLFSSL_SMALL_STACK
  12614. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  12615. #endif
  12616. }
  12617. }
  12618. return x509;
  12619. }
  12620. /* Get peer's PEM ceritifcate at index (idx), output to buffer if inLen big
  12621. enough else return error (-1). If buffer is NULL only calculate
  12622. outLen. Output length is in *outLen SSL_SUCCESS on ok */
  12623. int wolfSSL_get_chain_cert_pem(WOLFSSL_X509_CHAIN* chain, int idx,
  12624. unsigned char* buf, int inLen, int* outLen)
  12625. {
  12626. const char header[] = "-----BEGIN CERTIFICATE-----\n";
  12627. const char footer[] = "-----END CERTIFICATE-----\n";
  12628. int headerLen = sizeof(header) - 1;
  12629. int footerLen = sizeof(footer) - 1;
  12630. int i;
  12631. int err;
  12632. word32 szNeeded = 0;
  12633. WOLFSSL_ENTER("wolfSSL_get_chain_cert_pem");
  12634. if (!chain || !outLen || idx < 0 || idx >= wolfSSL_get_chain_count(chain))
  12635. return BAD_FUNC_ARG;
  12636. /* Null output buffer return size needed in outLen */
  12637. if(!buf) {
  12638. if(Base64_Encode(chain->certs[idx].buffer, chain->certs[idx].length,
  12639. NULL, &szNeeded) != LENGTH_ONLY_E)
  12640. return SSL_FAILURE;
  12641. *outLen = szNeeded + headerLen + footerLen;
  12642. return LENGTH_ONLY_E;
  12643. }
  12644. /* don't even try if inLen too short */
  12645. if (inLen < headerLen + footerLen + chain->certs[idx].length)
  12646. return BAD_FUNC_ARG;
  12647. /* header */
  12648. if (XMEMCPY(buf, header, headerLen) == NULL)
  12649. return SSL_FATAL_ERROR;
  12650. i = headerLen;
  12651. /* body */
  12652. *outLen = inLen; /* input to Base64_Encode */
  12653. if ( (err = Base64_Encode(chain->certs[idx].buffer,
  12654. chain->certs[idx].length, buf + i, (word32*)outLen)) < 0)
  12655. return err;
  12656. i += *outLen;
  12657. /* footer */
  12658. if ( (i + footerLen) > inLen)
  12659. return BAD_FUNC_ARG;
  12660. if (XMEMCPY(buf + i, footer, footerLen) == NULL)
  12661. return SSL_FATAL_ERROR;
  12662. *outLen += headerLen + footerLen;
  12663. return SSL_SUCCESS;
  12664. }
  12665. /* get session ID */
  12666. const byte* wolfSSL_get_sessionID(const WOLFSSL_SESSION* session)
  12667. {
  12668. WOLFSSL_ENTER("wolfSSL_get_sessionID");
  12669. if (session)
  12670. return session->sessionID;
  12671. return NULL;
  12672. }
  12673. #endif /* SESSION_CERTS */
  12674. #ifdef HAVE_FUZZER
  12675. void wolfSSL_SetFuzzerCb(WOLFSSL* ssl, CallbackFuzzer cbf, void* fCtx)
  12676. {
  12677. if (ssl) {
  12678. ssl->fuzzerCb = cbf;
  12679. ssl->fuzzerCtx = fCtx;
  12680. }
  12681. }
  12682. #endif
  12683. #ifndef NO_CERTS
  12684. #ifdef HAVE_PK_CALLBACKS
  12685. #ifdef HAVE_ECC
  12686. void wolfSSL_CTX_SetEccSignCb(WOLFSSL_CTX* ctx, CallbackEccSign cb)
  12687. {
  12688. if (ctx)
  12689. ctx->EccSignCb = cb;
  12690. }
  12691. void wolfSSL_SetEccSignCtx(WOLFSSL* ssl, void *ctx)
  12692. {
  12693. if (ssl)
  12694. ssl->EccSignCtx = ctx;
  12695. }
  12696. void* wolfSSL_GetEccSignCtx(WOLFSSL* ssl)
  12697. {
  12698. if (ssl)
  12699. return ssl->EccSignCtx;
  12700. return NULL;
  12701. }
  12702. void wolfSSL_CTX_SetEccVerifyCb(WOLFSSL_CTX* ctx, CallbackEccVerify cb)
  12703. {
  12704. if (ctx)
  12705. ctx->EccVerifyCb = cb;
  12706. }
  12707. void wolfSSL_SetEccVerifyCtx(WOLFSSL* ssl, void *ctx)
  12708. {
  12709. if (ssl)
  12710. ssl->EccVerifyCtx = ctx;
  12711. }
  12712. void* wolfSSL_GetEccVerifyCtx(WOLFSSL* ssl)
  12713. {
  12714. if (ssl)
  12715. return ssl->EccVerifyCtx;
  12716. return NULL;
  12717. }
  12718. #endif /* HAVE_ECC */
  12719. #ifndef NO_RSA
  12720. void wolfSSL_CTX_SetRsaSignCb(WOLFSSL_CTX* ctx, CallbackRsaSign cb)
  12721. {
  12722. if (ctx)
  12723. ctx->RsaSignCb = cb;
  12724. }
  12725. void wolfSSL_SetRsaSignCtx(WOLFSSL* ssl, void *ctx)
  12726. {
  12727. if (ssl)
  12728. ssl->RsaSignCtx = ctx;
  12729. }
  12730. void* wolfSSL_GetRsaSignCtx(WOLFSSL* ssl)
  12731. {
  12732. if (ssl)
  12733. return ssl->RsaSignCtx;
  12734. return NULL;
  12735. }
  12736. void wolfSSL_CTX_SetRsaVerifyCb(WOLFSSL_CTX* ctx, CallbackRsaVerify cb)
  12737. {
  12738. if (ctx)
  12739. ctx->RsaVerifyCb = cb;
  12740. }
  12741. void wolfSSL_SetRsaVerifyCtx(WOLFSSL* ssl, void *ctx)
  12742. {
  12743. if (ssl)
  12744. ssl->RsaVerifyCtx = ctx;
  12745. }
  12746. void* wolfSSL_GetRsaVerifyCtx(WOLFSSL* ssl)
  12747. {
  12748. if (ssl)
  12749. return ssl->RsaVerifyCtx;
  12750. return NULL;
  12751. }
  12752. void wolfSSL_CTX_SetRsaEncCb(WOLFSSL_CTX* ctx, CallbackRsaEnc cb)
  12753. {
  12754. if (ctx)
  12755. ctx->RsaEncCb = cb;
  12756. }
  12757. void wolfSSL_SetRsaEncCtx(WOLFSSL* ssl, void *ctx)
  12758. {
  12759. if (ssl)
  12760. ssl->RsaEncCtx = ctx;
  12761. }
  12762. void* wolfSSL_GetRsaEncCtx(WOLFSSL* ssl)
  12763. {
  12764. if (ssl)
  12765. return ssl->RsaEncCtx;
  12766. return NULL;
  12767. }
  12768. void wolfSSL_CTX_SetRsaDecCb(WOLFSSL_CTX* ctx, CallbackRsaDec cb)
  12769. {
  12770. if (ctx)
  12771. ctx->RsaDecCb = cb;
  12772. }
  12773. void wolfSSL_SetRsaDecCtx(WOLFSSL* ssl, void *ctx)
  12774. {
  12775. if (ssl)
  12776. ssl->RsaDecCtx = ctx;
  12777. }
  12778. void* wolfSSL_GetRsaDecCtx(WOLFSSL* ssl)
  12779. {
  12780. if (ssl)
  12781. return ssl->RsaDecCtx;
  12782. return NULL;
  12783. }
  12784. #endif /* NO_RSA */
  12785. #endif /* HAVE_PK_CALLBACKS */
  12786. #endif /* NO_CERTS */
  12787. #ifdef WOLFSSL_HAVE_WOLFSCEP
  12788. /* Used by autoconf to see if wolfSCEP is available */
  12789. void wolfSSL_wolfSCEP(void) {}
  12790. #endif
  12791. #ifdef WOLFSSL_HAVE_CERT_SERVICE
  12792. /* Used by autoconf to see if cert service is available */
  12793. void wolfSSL_cert_service(void) {}
  12794. #endif
  12795. #ifdef OPENSSL_EXTRA /*Lighttp compatibility*/
  12796. #ifdef HAVE_LIGHTY
  12797. unsigned char *wolfSSL_SHA1(const unsigned char *d, size_t n, unsigned char *md)
  12798. {
  12799. (void) *d; (void) n; (void) *md;
  12800. WOLFSSL_ENTER("wolfSSL_SHA1");
  12801. WOLFSSL_STUB("wolfssl_SHA1");
  12802. return NULL;
  12803. }
  12804. char wolfSSL_CTX_use_certificate(WOLFSSL_CTX *ctx, WOLFSSL_X509 *x) {
  12805. (void)ctx;
  12806. (void)x;
  12807. WOLFSSL_ENTER("wolfSSL_CTX_use_certificate");
  12808. WOLFSSL_STUB("wolfSSL_CTX_use_certificate");
  12809. return 0;
  12810. }
  12811. int wolfSSL_CTX_use_PrivateKey(WOLFSSL_CTX *ctx, WOLFSSL_EVP_PKEY *pkey) {
  12812. (void)ctx;
  12813. (void)pkey;
  12814. WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey");
  12815. WOLFSSL_STUB("wolfSSL_CTX_use_PrivateKey");
  12816. return 0;
  12817. }
  12818. int wolfSSL_BIO_read_filename(WOLFSSL_BIO *b, const char *name) {
  12819. (void)b;
  12820. (void)name;
  12821. WOLFSSL_ENTER("wolfSSL_BIO_read_filename");
  12822. WOLFSSL_STUB("wolfSSL_BIO_read_filename");
  12823. return 0;
  12824. }
  12825. WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_file(void) {
  12826. WOLFSSL_ENTER("wolfSSL_BIO_s_file");
  12827. WOLFSSL_STUB("wolfSSL_BIO_s_file");
  12828. return NULL;
  12829. }
  12830. const char * wolf_OBJ_nid2sn(int n) {
  12831. (void)n;
  12832. WOLFSSL_ENTER("wolf_OBJ_nid2sn");
  12833. WOLFSSL_STUB("wolf_OBJ_nid2sn");
  12834. return 0;
  12835. }
  12836. int wolf_OBJ_obj2nid(const WOLFSSL_ASN1_OBJECT *o) {
  12837. (void)o;
  12838. WOLFSSL_ENTER("wolf_OBJ_obj2nid");
  12839. WOLFSSL_STUB("wolf_OBJ_obj2nid");
  12840. return 0;
  12841. }
  12842. int wolf_OBJ_sn2nid(const char *sn) {
  12843. (void)sn;
  12844. WOLFSSL_ENTER("wolf_OBJ_osn2nid");
  12845. WOLFSSL_STUB("wolf_OBJ_osn2nid");
  12846. return 0;
  12847. }
  12848. WOLFSSL_X509 *PEM_read_bio_WOLFSSL_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 **x, pem_password_cb *cb, void *u) {
  12849. (void)bp;
  12850. (void)x;
  12851. (void)cb;
  12852. (void)u;
  12853. WOLFSSL_ENTER("PEM_read_bio_WOLFSSL_X509");
  12854. WOLFSSL_STUB("PEM_read_bio_WOLFSSL_X509");
  12855. return NULL;
  12856. }
  12857. void wolfSSL_CTX_set_verify_depth(WOLFSSL_CTX *ctx, int depth) {
  12858. (void)ctx;
  12859. (void)depth;
  12860. WOLFSSL_ENTER("wolfSSL_CTX_set_verify_depth");
  12861. WOLFSSL_STUB("wolfSSL_CTX_set_verify_depth");
  12862. }
  12863. void* wolfSSL_get_app_data( const WOLFSSL *ssl)
  12864. {
  12865. /* checkout exdata stuff... */
  12866. (void)ssl;
  12867. WOLFSSL_ENTER("wolfSSL_get_app_data");
  12868. WOLFSSL_STUB("wolfSSL_get_app_data");
  12869. return 0;
  12870. }
  12871. void wolfSSL_set_app_data(WOLFSSL *ssl, void *arg) {
  12872. (void)ssl;
  12873. (void)arg;
  12874. WOLFSSL_ENTER("wolfSSL_set_app_data");
  12875. WOLFSSL_STUB("wolfSSL_set_app_data");
  12876. }
  12877. WOLFSSL_ASN1_OBJECT * wolfSSL_X509_NAME_ENTRY_get_object(WOLFSSL_X509_NAME_ENTRY *ne) {
  12878. (void)ne;
  12879. WOLFSSL_ENTER("wolfSSL_X509_NAME_ENTRY_get_object");
  12880. WOLFSSL_STUB("wolfSSL_X509_NAME_ENTRY_get_object");
  12881. return NULL;
  12882. }
  12883. WOLFSSL_X509_NAME_ENTRY *wolfSSL_X509_NAME_get_entry(WOLFSSL_X509_NAME *name, int loc) {
  12884. (void)name;
  12885. (void)loc;
  12886. WOLFSSL_ENTER("wolfSSL_X509_NAME_get_entry");
  12887. WOLFSSL_STUB("wolfSSL_X509_NAME_get_entry");
  12888. return NULL;
  12889. }
  12890. void wolfSSL_X509_NAME_free(WOLFSSL_X509_NAME *name){
  12891. FreeX509Name(name);
  12892. WOLFSSL_ENTER("wolfSSL_X509_NAME_free");
  12893. WOLFSSL_STUB("wolfSSL_X509_NAME_free");
  12894. }
  12895. void wolfSSL_sk_X509_NAME_pop_free(STACK_OF(WOLFSSL_X509_NAME)* sk, void f (WOLFSSL_X509_NAME*)){
  12896. (void) sk;
  12897. (void) f;
  12898. WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_pop_free");
  12899. WOLFSSL_STUB("wolfSSL_sk_X509_NAME_pop_free");
  12900. }
  12901. int wolfSSL_X509_check_private_key(WOLFSSL_X509 *x509, WOLFSSL_EVP_PKEY *key){
  12902. (void) x509;
  12903. (void) key;
  12904. WOLFSSL_ENTER("wolfSSL_X509_check_private_key");
  12905. WOLFSSL_STUB("wolfSSL_X509_check_private_key");
  12906. return SSL_SUCCESS;
  12907. }
  12908. STACK_OF(WOLFSSL_X509_NAME) *wolfSSL_dup_CA_list( STACK_OF(WOLFSSL_X509_NAME) *sk ){
  12909. (void) sk;
  12910. WOLFSSL_ENTER("wolfSSL_dup_CA_list");
  12911. WOLFSSL_STUB("wolfSSL_dup_CA_list");
  12912. return NULL;
  12913. }
  12914. #endif
  12915. #endif
  12916. #ifdef OPENSSL_EXTRA
  12917. void* wolfSSL_CTX_get_ex_data(const WOLFSSL_CTX* ctx, int idx)
  12918. {
  12919. WOLFSSL_ENTER("wolfSSL_CTX_get_ex_data");
  12920. #ifdef HAVE_STUNNEL
  12921. if(ctx != NULL && idx < MAX_EX_DATA && idx >= 0) {
  12922. return ctx->ex_data[idx];
  12923. }
  12924. #else
  12925. (void)ctx;
  12926. (void)idx;
  12927. #endif
  12928. return NULL;
  12929. }
  12930. int wolfSSL_CTX_get_ex_new_index(long idx, void* arg, void* a, void* b,
  12931. void* c)
  12932. {
  12933. WOLFSSL_ENTER("wolfSSL_CTX_get_ex_new_index");
  12934. (void)idx;
  12935. (void)arg;
  12936. (void)a;
  12937. (void)b;
  12938. (void)c;
  12939. return 0;
  12940. }
  12941. int wolfSSL_CTX_set_ex_data(WOLFSSL_CTX* ctx, int idx, void* data)
  12942. {
  12943. WOLFSSL_ENTER("wolfSSL_CTX_set_ex_data");
  12944. #ifdef HAVE_STUNNEL
  12945. if (ctx != NULL && idx < MAX_EX_DATA)
  12946. {
  12947. ctx->ex_data[idx] = data;
  12948. return SSL_SUCCESS;
  12949. }
  12950. #else
  12951. (void)ctx;
  12952. (void)idx;
  12953. (void)data;
  12954. #endif
  12955. return SSL_FAILURE;
  12956. }
  12957. int wolfSSL_set_ex_data(WOLFSSL* ssl, int idx, void* data)
  12958. {
  12959. WOLFSSL_ENTER("wolfSSL_set_ex_data");
  12960. #if defined(FORTRESS) || defined(HAVE_STUNNEL)
  12961. if (ssl != NULL && idx < MAX_EX_DATA)
  12962. {
  12963. ssl->ex_data[idx] = data;
  12964. return SSL_SUCCESS;
  12965. }
  12966. #else
  12967. (void)ssl;
  12968. (void)idx;
  12969. (void)data;
  12970. #endif
  12971. return SSL_FAILURE;
  12972. }
  12973. int wolfSSL_get_ex_new_index(long idx, void* data, void* cb1, void* cb2,
  12974. void* cb3)
  12975. {
  12976. WOLFSSL_ENTER("wolfSSL_get_ex_new_index");
  12977. (void)idx;
  12978. (void)data;
  12979. (void)cb1;
  12980. (void)cb2;
  12981. (void)cb3;
  12982. return 0;
  12983. }
  12984. void* wolfSSL_get_ex_data(const WOLFSSL* ssl, int idx)
  12985. {
  12986. WOLFSSL_ENTER("wolfSSL_get_ex_data");
  12987. #if defined(FORTRESS) || defined(HAVE_STUNNEL)
  12988. if (ssl != NULL && idx < MAX_EX_DATA && idx >= 0)
  12989. return ssl->ex_data[idx];
  12990. #else
  12991. (void)ssl;
  12992. (void)idx;
  12993. #endif
  12994. return 0;
  12995. }
  12996. #endif /* OPENSSL_EXTRA */
  12997. #if defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  12998. WOLFSSL_BIO *wolfSSL_BIO_new_file(const char *filename, const char *mode) {
  12999. (void)filename;
  13000. (void)mode;
  13001. WOLFSSL_ENTER("wolfSSL_BIO_new_file");
  13002. WOLFSSL_STUB("wolfSSL_BIO_new_file");
  13003. return NULL;
  13004. }
  13005. WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFSSL_BIO *bp, WOLFSSL_DH **x, pem_password_cb *cb, void *u)
  13006. {
  13007. (void) bp;
  13008. (void) x;
  13009. (void) cb;
  13010. (void) u;
  13011. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DHparams");
  13012. WOLFSSL_STUB("wolfSSL_PEM_read_bio_DHparams");
  13013. return NULL;
  13014. }
  13015. int PEM_write_bio_WOLFSSL_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 *x) {
  13016. (void)bp;
  13017. (void)x;
  13018. WOLFSSL_ENTER("PEM_write_bio_WOLFSSL_X509");
  13019. WOLFSSL_STUB("PEM_write_bio_WOLFSSL_X509");
  13020. return 0;
  13021. }
  13022. #ifndef NO_DH
  13023. /* Intialize ctx->dh with dh's params. Return SSL_SUCCESS on ok */
  13024. long wolfSSL_CTX_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL_DH* dh)
  13025. {
  13026. int pSz, gSz;
  13027. byte *p, *g;
  13028. int ret=0;
  13029. WOLFSSL_ENTER("wolfSSL_CTX_set_tmp_dh");
  13030. if(!ctx || !dh)
  13031. return BAD_FUNC_ARG;
  13032. /* Get needed size for p and g */
  13033. pSz = wolfSSL_BN_bn2bin(dh->p, NULL);
  13034. gSz = wolfSSL_BN_bn2bin(dh->g, NULL);
  13035. if(pSz <= 0 || gSz <= 0)
  13036. return SSL_FATAL_ERROR;
  13037. p = (byte*)XMALLOC(pSz, ctx->heap, DYNAMIC_TYPE_DH);
  13038. if(!p)
  13039. return MEMORY_E;
  13040. g = (byte*)XMALLOC(gSz, ctx->heap, DYNAMIC_TYPE_DH);
  13041. if(!g) {
  13042. XFREE(p, ctx->heap, DYNAMIC_TYPE_DH);
  13043. return MEMORY_E;
  13044. }
  13045. pSz = wolfSSL_BN_bn2bin(dh->p, p);
  13046. gSz = wolfSSL_BN_bn2bin(dh->g, g);
  13047. if(pSz >= 0 && gSz >= 0) /* Conversion successful */
  13048. ret = wolfSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
  13049. XFREE(p, ctx->heap, DYNAMIC_TYPE_DH);
  13050. XFREE(g, ctx->heap, DYNAMIC_TYPE_DH);
  13051. return pSz > 0 && gSz > 0 ? ret : SSL_FATAL_ERROR;
  13052. }
  13053. #endif /* NO_DH */
  13054. #endif /* HAVE_LIGHTY || HAVE_STUNNEL */
  13055. /* stunnel compatability functions*/
  13056. #if defined(OPENSSL_EXTRA) && defined(HAVE_STUNNEL)
  13057. int wolfSSL_SESSION_set_ex_data(WOLFSSL_SESSION* session, int idx, void* data)
  13058. {
  13059. WOLFSSL_ENTER("wolfSSL_SESSION_set_ex_data");
  13060. if(session != NULL && idx < MAX_EX_DATA) {
  13061. session->ex_data[idx] = data;
  13062. return SSL_SUCCESS;
  13063. }
  13064. return SSL_FAILURE;
  13065. }
  13066. int wolfSSL_SESSION_get_ex_new_index(long idx, void* data, void* cb1,
  13067. void* cb2, CRYPTO_free_func* cb3)
  13068. {
  13069. WOLFSSL_ENTER("wolfSSL_SESSION_get_ex_new_index");
  13070. (void)idx;
  13071. (void)cb1;
  13072. (void)cb2;
  13073. (void)cb3;
  13074. if(XSTRNCMP((const char*)data, "redirect index", 14) == 0) {
  13075. return 0;
  13076. }
  13077. else if(XSTRNCMP((const char*)data, "addr index", 10) == 0) {
  13078. return 1;
  13079. }
  13080. return SSL_FAILURE;
  13081. }
  13082. void* wolfSSL_SESSION_get_ex_data(const WOLFSSL_SESSION* session, int idx)
  13083. {
  13084. WOLFSSL_ENTER("wolfSSL_SESSION_get_ex_data");
  13085. if (session != NULL && idx < MAX_EX_DATA && idx >= 0)
  13086. return session->ex_data[idx];
  13087. return NULL;
  13088. }
  13089. int wolfSSL_CRYPTO_set_mem_ex_functions(void *(*m) (size_t, const char *, int),
  13090. void *(*r) (void *, size_t, const char *,
  13091. int), void (*f) (void *))
  13092. {
  13093. (void) m;
  13094. (void) r;
  13095. (void) f;
  13096. WOLFSSL_ENTER("wolfSSL_CRYPTO_set_mem_ex_functions");
  13097. WOLFSSL_STUB("wolfSSL_CRYPTO_set_mem_ex_functions");
  13098. return SSL_FAILURE;
  13099. }
  13100. WOLFSSL_DH *wolfSSL_DH_generate_parameters(int prime_len, int generator,
  13101. void (*callback) (int, int, void *), void *cb_arg)
  13102. {
  13103. (void)prime_len;
  13104. (void)generator;
  13105. (void)callback;
  13106. (void)cb_arg;
  13107. WOLFSSL_ENTER("wolfSSL_DH_generate_parameters");
  13108. WOLFSSL_STUB("wolfSSL_DH_generate_parameters");
  13109. return NULL;
  13110. }
  13111. void wolfSSL_ERR_load_crypto_strings(void)
  13112. {
  13113. WOLFSSL_ENTER("wolfSSL_ERR_load_crypto_strings");
  13114. WOLFSSL_ENTER("wolfSSL_ERR_load_crypto_strings");
  13115. return;
  13116. }
  13117. unsigned long wolfSSL_ERR_peek_last_error(void)
  13118. {
  13119. unsigned long l = 0UL;
  13120. WOLFSSL_ENTER("wolfSSL_ERR_peek_last_error");
  13121. WOLFSSL_STUB("wolfSSL_ERR_peek_last_error");
  13122. return l;
  13123. }
  13124. int wolfSSL_FIPS_mode(void)
  13125. {
  13126. WOLFSSL_ENTER("wolfSSL_FIPS_mode");
  13127. WOLFSSL_STUB("wolfSSL_FIPS_mode");
  13128. return SSL_FAILURE;
  13129. }
  13130. int wolfSSL_FIPS_mode_set(int r)
  13131. {
  13132. (void)r;
  13133. WOLFSSL_ENTER("wolfSSL_FIPS_mode_set");
  13134. WOLFSSL_STUB("wolfSSL_FIPS_mode_set");
  13135. return SSL_FAILURE;
  13136. }
  13137. int wolfSSL_RAND_set_rand_method(const void *meth)
  13138. {
  13139. (void) meth;
  13140. WOLFSSL_ENTER("wolfSSL_RAND_set_rand_method");
  13141. WOLFSSL_STUB("wolfSSL_RAND_set_rand_method");
  13142. return SSL_FAILURE;
  13143. }
  13144. int wolfSSL_CIPHER_get_bits(const WOLFSSL_CIPHER *c, int *alg_bits)
  13145. {
  13146. int ret = SSL_FAILURE;
  13147. WOLFSSL_ENTER("wolfSSL_CIPHER_get_bits");
  13148. if(c != NULL && c->ssl != NULL) {
  13149. ret = 8 * c->ssl->specs.key_size;
  13150. if(alg_bits != NULL) {
  13151. *alg_bits = ret;
  13152. }
  13153. }
  13154. return ret;
  13155. }
  13156. int wolfSSL_sk_X509_NAME_num(const STACK_OF(WOLFSSL_X509_NAME) *s)
  13157. {
  13158. (void) s;
  13159. WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_num");
  13160. WOLFSSL_STUB("wolfSSL_sk_X509_NAME_num");
  13161. return SSL_FAILURE;
  13162. }
  13163. int wolfSSL_sk_X509_num(const STACK_OF(WOLFSSL_X509) *s)
  13164. {
  13165. (void) s;
  13166. WOLFSSL_ENTER("wolfSSL_sk_X509_num");
  13167. WOLFSSL_STUB("wolfSSL_sk_X509_num");
  13168. return SSL_FAILURE;
  13169. }
  13170. int wolfSSL_X509_NAME_print_ex(WOLFSSL_BIO* bio, WOLFSSL_X509_NAME* nm,
  13171. int indent, unsigned long flags)
  13172. {
  13173. (void)bio;
  13174. (void)nm;
  13175. (void)indent;
  13176. (void)flags;
  13177. WOLFSSL_ENTER("wolfSSL_X509_NAME_print_ex");
  13178. WOLFSSL_STUB("wolfSSL_X509_NAME_print_ex");
  13179. return SSL_FAILURE;
  13180. }
  13181. WOLFSSL_ASN1_BIT_STRING* wolfSSL_X509_get0_pubkey_bitstr(const WOLFSSL_X509* x)
  13182. {
  13183. (void)x;
  13184. WOLFSSL_ENTER("wolfSSL_X509_get0_pubkey_bitstr");
  13185. WOLFSSL_STUB("wolfSSL_X509_get0_pubkey_bitstr");
  13186. return NULL;
  13187. }
  13188. int wolfSSL_CTX_add_session(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session)
  13189. {
  13190. (void)ctx;
  13191. (void)session;
  13192. WOLFSSL_ENTER("wolfSSL_CTX_add_session");
  13193. WOLFSSL_STUB("wolfSSL_CTX_add_session");
  13194. return SSL_SUCCESS;
  13195. }
  13196. int wolfSSL_get_state(const WOLFSSL* ssl)
  13197. {
  13198. (void)ssl;
  13199. WOLFSSL_ENTER("wolfSSL_get_state");
  13200. WOLFSSL_STUB("wolfSSL_get_state");
  13201. return SSL_FAILURE;
  13202. }
  13203. void* wolfSSL_sk_X509_NAME_value(STACK_OF(WOLFSSL_X509_NAME)* sk, int i)
  13204. {
  13205. (void)sk;
  13206. (void)i;
  13207. WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_value");
  13208. WOLFSSL_STUB("wolfSSL_sk_X509_NAME_value");
  13209. return NULL;
  13210. }
  13211. void* wolfSSL_sk_X509_value(STACK_OF(WOLFSSL_X509)* sk, int i)
  13212. {
  13213. (void)sk;
  13214. (void)i;
  13215. WOLFSSL_ENTER("wolfSSL_sk_X509_value");
  13216. WOLFSSL_STUB("wolfSSL_sk_X509_value");
  13217. return NULL;
  13218. }
  13219. int wolfSSL_version(WOLFSSL* ssl)
  13220. {
  13221. WOLFSSL_ENTER("wolfSSL_version");
  13222. if (ssl->version.major == SSLv3_MAJOR) {
  13223. switch (ssl->version.minor) {
  13224. case SSLv3_MINOR :
  13225. return SSL3_VERSION;
  13226. case TLSv1_MINOR :
  13227. case TLSv1_1_MINOR :
  13228. case TLSv1_2_MINOR :
  13229. return TLS1_VERSION;
  13230. default:
  13231. return SSL_FAILURE;
  13232. }
  13233. }
  13234. else if (ssl->version.major == DTLS_MAJOR) {
  13235. switch (ssl->version.minor) {
  13236. case DTLS_MINOR :
  13237. case DTLSv1_2_MINOR :
  13238. return DTLS1_VERSION;
  13239. default:
  13240. return SSL_FAILURE;
  13241. }
  13242. }
  13243. return SSL_FAILURE;
  13244. }
  13245. STACK_OF(WOLFSSL_X509)* wolfSSL_get_peer_cert_chain(const WOLFSSL* ssl)
  13246. {
  13247. (void)ssl;
  13248. WOLFSSL_ENTER("wolfSSL_get_peer_cert_chain");
  13249. WOLFSSL_STUB("wolfSSL_get_peer_cert_chain");
  13250. return NULL;
  13251. }
  13252. long wolfSSL_CTX_get_options(WOLFSSL_CTX* ctx)
  13253. {
  13254. (void)ctx;
  13255. WOLFSSL_ENTER("wolfSSL_CTX_get_options");
  13256. WOLFSSL_STUB("wolfSSL_CTX_get_options");
  13257. return 0;
  13258. }
  13259. WOLFSSL_CTX* wolfSSL_get_SSL_CTX(WOLFSSL* ssl)
  13260. {
  13261. WOLFSSL_ENTER("wolfSSL_get_SSL_CTX");
  13262. return ssl->ctx;
  13263. }
  13264. int wolfSSL_X509_NAME_get_sz(WOLFSSL_X509_NAME* name)
  13265. {
  13266. WOLFSSL_ENTER("wolfSSL_X509_NAME_get_sz");
  13267. if(!name)
  13268. return -1;
  13269. return name->sz;
  13270. }
  13271. const byte* wolfSSL_SESSION_get_id(WOLFSSL_SESSION* sess, unsigned int* idLen)
  13272. {
  13273. WOLFSSL_ENTER("wolfSSL_SESSION_get_id");
  13274. WOLFSSL_STUB("wolfSSL_SESSION_get_id");
  13275. if(!sess || !idLen) {
  13276. WOLFSSL_MSG("Bad func args. Please provide idLen");
  13277. return NULL;
  13278. }
  13279. *idLen = sess->sessionIDSz;
  13280. return sess->sessionID;
  13281. }
  13282. int wolfSSL_set_tlsext_host_name(WOLFSSL* ssl, const char* host_name)
  13283. {
  13284. int ret;
  13285. WOLFSSL_ENTER("wolfSSL_set_tlsext_host_name");
  13286. ret = wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  13287. host_name, XSTRLEN(host_name));
  13288. WOLFSSL_LEAVE("wolfSSL_set_tlsext_host_name", ret);
  13289. return ret;
  13290. }
  13291. const char * wolfSSL_get_servername(WOLFSSL* ssl, byte type)
  13292. {
  13293. void * serverName = NULL;
  13294. if (ssl == NULL)
  13295. return NULL;
  13296. TLSX_SNI_GetRequest(ssl->extensions, type, &serverName);
  13297. return (const char *)serverName;
  13298. }
  13299. WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
  13300. {
  13301. if (ssl && ctx && SetSSL_CTX(ssl, ctx) == SSL_SUCCESS)
  13302. return ssl->ctx;
  13303. return NULL;
  13304. }
  13305. VerifyCallback wolfSSL_CTX_get_verify_callback(WOLFSSL_CTX* ctx)
  13306. {
  13307. WOLFSSL_ENTER("wolfSSL_CTX_get_verify_callback");
  13308. if(ctx)
  13309. return ctx->verifyCallback;
  13310. return NULL;
  13311. }
  13312. int wolfSSL_CTX_get_verify_mode(WOLFSSL_CTX* ctx)
  13313. {
  13314. WOLFSSL_ENTER("wolfSSL_CTX_get_verify_mode");
  13315. int mode = 0;
  13316. if(!ctx)
  13317. return SSL_FATAL_ERROR;
  13318. if (ctx->verifyPeer)
  13319. mode |= SSL_VERIFY_PEER;
  13320. else if (ctx->verifyNone)
  13321. mode |= SSL_VERIFY_NONE;
  13322. if (ctx->failNoCert)
  13323. mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
  13324. WOLFSSL_LEAVE("wolfSSL_CTX_get_verify_mode", mode);
  13325. return mode;
  13326. }
  13327. void wolfSSL_CTX_set_servername_callback(WOLFSSL_CTX* ctx, CallbackSniRecv cb)
  13328. {
  13329. WOLFSSL_ENTER("wolfSSL_CTX_set_servername_callback");
  13330. if (ctx)
  13331. ctx->sniRecvCb = cb;
  13332. }
  13333. void wolfSSL_CTX_set_servername_arg(WOLFSSL_CTX* ctx, void* arg)
  13334. {
  13335. WOLFSSL_ENTER("wolfSSL_CTX_set_servername_arg");
  13336. if (ctx)
  13337. ctx->sniRecvCbArg = arg;
  13338. }
  13339. #endif /* OPENSSL_EXTRA and HAVE_STUNNEL */
  13340. #if defined(OPENSSL_EXTRA) && defined(HAVE_CURVE25519)
  13341. /* return 1 if success, 0 if error
  13342. * output keys are little endian format
  13343. */
  13344. int wolfSSL_EC25519_generate_key(unsigned char *priv, unsigned int *privSz,
  13345. unsigned char *pub, unsigned int *pubSz)
  13346. {
  13347. #ifndef WOLFSSL_KEY_GEN
  13348. WOLFSSL_MSG("No Key Gen built in");
  13349. return SSL_FAILURE;
  13350. #else /* WOLFSSL_KEY_GEN */
  13351. int ret = SSL_FAILURE;
  13352. int initTmpRng = 0;
  13353. WC_RNG *rng = NULL;
  13354. #ifdef WOLFSSL_SMALL_STACK
  13355. WC_RNG *tmpRNG = NULL;
  13356. #else
  13357. WC_RNG tmpRNG[1];
  13358. #endif
  13359. WOLFSSL_ENTER("wolfSSL_EC25519_generate_key");
  13360. if (priv == NULL || privSz == NULL || *privSz < CURVE25519_KEYSIZE ||
  13361. pub == NULL || pubSz == NULL || *pubSz < CURVE25519_KEYSIZE) {
  13362. WOLFSSL_MSG("Bad arguments");
  13363. return SSL_FAILURE;
  13364. }
  13365. #ifdef WOLFSSL_SMALL_STACK
  13366. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13367. if (tmpRNG == NULL)
  13368. return SSL_FAILURE;
  13369. #endif
  13370. if (wc_InitRng(tmpRNG) == 0) {
  13371. rng = tmpRNG;
  13372. initTmpRng = 1;
  13373. }
  13374. else {
  13375. WOLFSSL_MSG("Bad RNG Init, trying global");
  13376. if (initGlobalRNG == 0)
  13377. WOLFSSL_MSG("Global RNG no Init");
  13378. else
  13379. rng = &globalRNG;
  13380. }
  13381. if (rng) {
  13382. curve25519_key key;
  13383. if (wc_curve25519_init(&key) != MP_OKAY)
  13384. WOLFSSL_MSG("wc_curve25519_init failed");
  13385. else if (wc_curve25519_make_key(rng, CURVE25519_KEYSIZE, &key)!=MP_OKAY)
  13386. WOLFSSL_MSG("wc_curve25519_make_key failed");
  13387. /* export key pair */
  13388. else if (wc_curve25519_export_key_raw_ex(&key, priv, privSz, pub,
  13389. pubSz, EC25519_LITTLE_ENDIAN)
  13390. != MP_OKAY)
  13391. WOLFSSL_MSG("wc_curve25519_export_key_raw_ex failed");
  13392. else
  13393. ret = SSL_SUCCESS;
  13394. wc_curve25519_free(&key);
  13395. }
  13396. if (initTmpRng)
  13397. wc_FreeRng(tmpRNG);
  13398. #ifdef WOLFSSL_SMALL_STACK
  13399. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13400. #endif
  13401. return ret;
  13402. #endif /* WOLFSSL_KEY_GEN */
  13403. }
  13404. /* return 1 if success, 0 if error
  13405. * input and output keys are little endian format
  13406. */
  13407. int wolfSSL_EC25519_shared_key(unsigned char *shared, unsigned int *sharedSz,
  13408. const unsigned char *priv, unsigned int privSz,
  13409. const unsigned char *pub, unsigned int pubSz)
  13410. {
  13411. #ifndef WOLFSSL_KEY_GEN
  13412. WOLFSSL_MSG("No Key Gen built in");
  13413. return SSL_FAILURE;
  13414. #else /* WOLFSSL_KEY_GEN */
  13415. int ret = SSL_FAILURE;
  13416. curve25519_key privkey, pubkey;
  13417. WOLFSSL_ENTER("wolfSSL_EC25519_shared_key");
  13418. if (shared == NULL || sharedSz == NULL || *sharedSz < CURVE25519_KEYSIZE ||
  13419. priv == NULL || privSz < CURVE25519_KEYSIZE ||
  13420. pub == NULL || pubSz < CURVE25519_KEYSIZE) {
  13421. WOLFSSL_MSG("Bad arguments");
  13422. return SSL_FAILURE;
  13423. }
  13424. /* import private key */
  13425. if (wc_curve25519_init(&privkey) != MP_OKAY) {
  13426. WOLFSSL_MSG("wc_curve25519_init privkey failed");
  13427. return ret;
  13428. }
  13429. if (wc_curve25519_import_private_ex(priv, privSz, &privkey,
  13430. EC25519_LITTLE_ENDIAN) != MP_OKAY) {
  13431. WOLFSSL_MSG("wc_curve25519_import_private_ex failed");
  13432. wc_curve25519_free(&privkey);
  13433. return ret;
  13434. }
  13435. /* import public key */
  13436. if (wc_curve25519_init(&pubkey) != MP_OKAY) {
  13437. WOLFSSL_MSG("wc_curve25519_init pubkey failed");
  13438. wc_curve25519_free(&privkey);
  13439. return ret;
  13440. }
  13441. if (wc_curve25519_import_public_ex(pub, pubSz, &pubkey,
  13442. EC25519_LITTLE_ENDIAN) != MP_OKAY) {
  13443. WOLFSSL_MSG("wc_curve25519_import_public_ex failed");
  13444. wc_curve25519_free(&privkey);
  13445. wc_curve25519_free(&pubkey);
  13446. return ret;
  13447. }
  13448. if (wc_curve25519_shared_secret_ex(&privkey, &pubkey,
  13449. shared, sharedSz,
  13450. EC25519_LITTLE_ENDIAN) != MP_OKAY)
  13451. WOLFSSL_MSG("wc_curve25519_shared_secret_ex failed");
  13452. else
  13453. ret = SSL_SUCCESS;
  13454. wc_curve25519_free(&privkey);
  13455. wc_curve25519_free(&pubkey);
  13456. return ret;
  13457. #endif /* WOLFSSL_KEY_GEN */
  13458. }
  13459. #endif /* OPENSSL_EXTRA && HAVE_CURVE25519 */
  13460. #if defined(OPENSSL_EXTRA) && defined(HAVE_ED25519)
  13461. /* return 1 if success, 0 if error
  13462. * output keys are little endian format
  13463. */
  13464. int wolfSSL_ED25519_generate_key(unsigned char *priv, unsigned int *privSz,
  13465. unsigned char *pub, unsigned int *pubSz)
  13466. {
  13467. #ifndef WOLFSSL_KEY_GEN
  13468. WOLFSSL_MSG("No Key Gen built in");
  13469. return SSL_FAILURE;
  13470. #else /* WOLFSSL_KEY_GEN */
  13471. int ret = SSL_FAILURE;
  13472. int initTmpRng = 0;
  13473. WC_RNG *rng = NULL;
  13474. #ifdef WOLFSSL_SMALL_STACK
  13475. WC_RNG *tmpRNG = NULL;
  13476. #else
  13477. WC_RNG tmpRNG[1];
  13478. #endif
  13479. WOLFSSL_ENTER("wolfSSL_ED25519_generate_key");
  13480. if (priv == NULL || privSz == NULL || *privSz < ED25519_PRV_KEY_SIZE ||
  13481. pub == NULL || pubSz == NULL || *pubSz < ED25519_PUB_KEY_SIZE) {
  13482. WOLFSSL_MSG("Bad arguments");
  13483. return SSL_FAILURE;
  13484. }
  13485. #ifdef WOLFSSL_SMALL_STACK
  13486. tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13487. if (tmpRNG == NULL)
  13488. return SSL_FATAL_ERROR;
  13489. #endif
  13490. if (wc_InitRng(tmpRNG) == 0) {
  13491. rng = tmpRNG;
  13492. initTmpRng = 1;
  13493. }
  13494. else {
  13495. WOLFSSL_MSG("Bad RNG Init, trying global");
  13496. if (initGlobalRNG == 0)
  13497. WOLFSSL_MSG("Global RNG no Init");
  13498. else
  13499. rng = &globalRNG;
  13500. }
  13501. if (rng) {
  13502. ed25519_key key;
  13503. if (wc_ed25519_init(&key) != MP_OKAY)
  13504. WOLFSSL_MSG("wc_ed25519_init failed");
  13505. else if (wc_ed25519_make_key(rng, ED25519_KEY_SIZE, &key)!=MP_OKAY)
  13506. WOLFSSL_MSG("wc_ed25519_make_key failed");
  13507. /* export private key */
  13508. else if (wc_ed25519_export_key(&key, priv, privSz, pub, pubSz)!=MP_OKAY)
  13509. WOLFSSL_MSG("wc_ed25519_export_key failed");
  13510. else
  13511. ret = SSL_SUCCESS;
  13512. wc_ed25519_free(&key);
  13513. }
  13514. if (initTmpRng)
  13515. wc_FreeRng(tmpRNG);
  13516. #ifdef WOLFSSL_SMALL_STACK
  13517. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13518. #endif
  13519. return ret;
  13520. #endif /* WOLFSSL_KEY_GEN */
  13521. }
  13522. /* return 1 if success, 0 if error
  13523. * input and output keys are little endian format
  13524. * priv is a buffer containing private and public part of key
  13525. */
  13526. int wolfSSL_ED25519_sign(const unsigned char *msg, unsigned int msgSz,
  13527. const unsigned char *priv, unsigned int privSz,
  13528. unsigned char *sig, unsigned int *sigSz)
  13529. {
  13530. #ifndef WOLFSSL_KEY_GEN
  13531. WOLFSSL_MSG("No Key Gen built in");
  13532. return SSL_FAILURE;
  13533. #else /* WOLFSSL_KEY_GEN */
  13534. ed25519_key key;
  13535. int ret = SSL_FAILURE;
  13536. WOLFSSL_ENTER("wolfSSL_ED25519_sign");
  13537. if (priv == NULL || privSz != ED25519_PRV_KEY_SIZE ||
  13538. msg == NULL || sig == NULL || *sigSz < ED25519_SIG_SIZE) {
  13539. WOLFSSL_MSG("Bad arguments");
  13540. return SSL_FAILURE;
  13541. }
  13542. /* import key */
  13543. if (wc_ed25519_init(&key) != MP_OKAY) {
  13544. WOLFSSL_MSG("wc_curve25519_init failed");
  13545. return ret;
  13546. }
  13547. if (wc_ed25519_import_private_key(priv, privSz/2,
  13548. priv+(privSz/2), ED25519_PUB_KEY_SIZE,
  13549. &key) != MP_OKAY){
  13550. WOLFSSL_MSG("wc_ed25519_import_private failed");
  13551. wc_ed25519_free(&key);
  13552. return ret;
  13553. }
  13554. if (wc_ed25519_sign_msg(msg, msgSz, sig, sigSz, &key) != MP_OKAY)
  13555. WOLFSSL_MSG("wc_curve25519_shared_secret_ex failed");
  13556. else
  13557. ret = SSL_SUCCESS;
  13558. wc_ed25519_free(&key);
  13559. return ret;
  13560. #endif /* WOLFSSL_KEY_GEN */
  13561. }
  13562. /* return 1 if success, 0 if error
  13563. * input and output keys are little endian format
  13564. * pub is a buffer containing public part of key
  13565. */
  13566. int wolfSSL_ED25519_verify(const unsigned char *msg, unsigned int msgSz,
  13567. const unsigned char *pub, unsigned int pubSz,
  13568. const unsigned char *sig, unsigned int sigSz)
  13569. {
  13570. #ifndef WOLFSSL_KEY_GEN
  13571. WOLFSSL_MSG("No Key Gen built in");
  13572. return SSL_FAILURE;
  13573. #else /* WOLFSSL_KEY_GEN */
  13574. ed25519_key key;
  13575. int ret = SSL_FAILURE, check = 0;
  13576. WOLFSSL_ENTER("wolfSSL_ED25519_verify");
  13577. if (pub == NULL || pubSz != ED25519_PUB_KEY_SIZE ||
  13578. msg == NULL || sig == NULL || sigSz != ED25519_SIG_SIZE) {
  13579. WOLFSSL_MSG("Bad arguments");
  13580. return SSL_FAILURE;
  13581. }
  13582. /* import key */
  13583. if (wc_ed25519_init(&key) != MP_OKAY) {
  13584. WOLFSSL_MSG("wc_curve25519_init failed");
  13585. return ret;
  13586. }
  13587. if (wc_ed25519_import_public(pub, pubSz, &key) != MP_OKAY){
  13588. WOLFSSL_MSG("wc_ed25519_import_public failed");
  13589. wc_ed25519_free(&key);
  13590. return ret;
  13591. }
  13592. if ((ret = wc_ed25519_verify_msg((byte*)sig, sigSz, msg, msgSz,
  13593. &check, &key)) != MP_OKAY) {
  13594. WOLFSSL_MSG("wc_ed25519_verify_msg failed");
  13595. fprintf(stderr, "err code = %d, sigSz=%d, msgSz=%d\n", ret, sigSz, msgSz);
  13596. }
  13597. else if (!check)
  13598. WOLFSSL_MSG("wc_ed25519_verify_msg failed (signature invalid)");
  13599. else
  13600. ret = SSL_SUCCESS;
  13601. wc_ed25519_free(&key);
  13602. return ret;
  13603. #endif /* WOLFSSL_KEY_GEN */
  13604. }
  13605. #endif /* OPENSSL_EXTRA && HAVE_ED25519 */
  13606. #ifdef WOLFSSL_JNI
  13607. int wolfSSL_set_jobject(WOLFSSL* ssl, void* objPtr)
  13608. {
  13609. WOLFSSL_ENTER("wolfSSL_set_jobject");
  13610. if (ssl != NULL)
  13611. {
  13612. ssl->jObjectRef = objPtr;
  13613. return SSL_SUCCESS;
  13614. }
  13615. return SSL_FAILURE;
  13616. }
  13617. void* wolfSSL_get_jobject(WOLFSSL* ssl)
  13618. {
  13619. WOLFSSL_ENTER("wolfSSL_get_jobject");
  13620. if (ssl != NULL)
  13621. return ssl->jObjectRef;
  13622. return NULL;
  13623. }
  13624. #endif /* WOLFSSL_JNI */
  13625. #endif /* WOLFCRYPT_ONLY */