1
0

api.c 2.4 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908689096891068911689126891368914689156891668917689186891968920689216892268923689246892568926689276892868929689306893168932689336893468935689366893768938689396894068941689426894368944689456894668947689486894968950689516895268953689546895568956689576895868959689606896168962689636896468965689666896768968689696897068971689726897368974689756897668977689786897968980689816898268983689846898568986689876898868989689906899168992689936899468995689966899768998689996900069001690026900369004690056900669007690086900969010690116901269013690146901569016690176901869019690206902169022690236902469025690266902769028690296903069031690326903369034690356903669037690386903969040690416904269043690446904569046690476904869049690506905169052690536905469055690566905769058690596906069061690626906369064690656906669067690686906969070690716907269073690746907569076690776907869079690806908169082690836908469085690866908769088690896909069091690926909369094690956909669097690986909969100691016910269103691046910569106691076910869109691106911169112691136911469115691166911769118691196912069121691226912369124691256912669127691286912969130691316913269133691346913569136691376913869139691406914169142691436914469145691466914769148691496915069151691526915369154691556915669157691586915969160691616916269163691646916569166691676916869169691706917169172691736917469175691766917769178691796918069181691826918369184691856918669187691886918969190691916919269193691946919569196691976919869199692006920169202692036920469205692066920769208692096921069211692126921369214692156921669217692186921969220692216922269223692246922569226692276922869229692306923169232692336923469235692366923769238692396924069241692426924369244692456924669247692486924969250692516925269253692546925569256692576925869259692606926169262692636926469265692666926769268692696927069271692726927369274692756927669277692786927969280692816928269283692846928569286692876928869289692906929169292692936929469295692966929769298692996930069301693026930369304693056930669307693086930969310693116931269313693146931569316693176931869319693206932169322693236932469325693266932769328693296933069331693326933369334693356933669337693386933969340693416934269343693446934569346693476934869349693506935169352693536935469355693566935769358693596936069361693626936369364693656936669367693686936969370693716937269373693746937569376693776937869379693806938169382693836938469385693866938769388693896939069391693926939369394693956939669397693986939969400694016940269403694046940569406694076940869409694106941169412694136941469415694166941769418694196942069421694226942369424694256942669427694286942969430694316943269433694346943569436694376943869439694406944169442694436944469445694466944769448694496945069451694526945369454694556945669457694586945969460694616946269463694646946569466694676946869469694706947169472694736947469475694766947769478694796948069481694826948369484694856948669487694886948969490694916949269493694946949569496694976949869499695006950169502695036950469505695066950769508695096951069511695126951369514695156951669517695186951969520695216952269523695246952569526695276952869529695306953169532695336953469535695366953769538695396954069541695426954369544695456954669547695486954969550695516955269553695546955569556695576955869559695606956169562695636956469565695666956769568695696957069571695726957369574695756957669577695786957969580695816958269583695846958569586695876958869589695906959169592695936959469595695966959769598695996960069601696026960369604696056960669607696086960969610696116961269613696146961569616696176961869619696206962169622696236962469625696266962769628696296963069631696326963369634696356963669637696386963969640696416964269643696446964569646696476964869649696506965169652696536965469655696566965769658696596966069661696626966369664696656966669667696686966969670696716967269673696746967569676696776967869679696806968169682696836968469685696866968769688696896969069691696926969369694696956969669697696986969969700697016970269703697046970569706697076970869709697106971169712697136971469715697166971769718697196972069721697226972369724697256972669727697286972969730697316973269733697346973569736697376973869739697406974169742697436974469745697466974769748697496975069751697526975369754697556975669757697586975969760697616976269763697646976569766697676976869769697706977169772697736977469775697766977769778697796978069781697826978369784697856978669787697886978969790697916979269793697946979569796697976979869799698006980169802698036980469805698066980769808698096981069811698126981369814698156981669817698186981969820698216982269823698246982569826698276982869829698306983169832698336983469835698366983769838698396984069841698426984369844698456984669847698486984969850698516985269853698546985569856698576985869859698606986169862698636986469865698666986769868698696987069871698726987369874698756987669877698786987969880698816988269883698846988569886698876988869889698906989169892698936989469895698966989769898698996990069901699026990369904699056990669907699086990969910699116991269913699146991569916699176991869919699206992169922699236992469925699266992769928699296993069931699326993369934699356993669937699386993969940699416994269943699446994569946699476994869949699506995169952699536995469955699566995769958699596996069961699626996369964699656996669967699686996969970699716997269973699746997569976699776997869979699806998169982699836998469985699866998769988699896999069991699926999369994699956999669997699986999970000700017000270003700047000570006700077000870009700107001170012700137001470015700167001770018700197002070021700227002370024700257002670027700287002970030700317003270033700347003570036700377003870039700407004170042700437004470045700467004770048700497005070051700527005370054700557005670057700587005970060700617006270063700647006570066700677006870069700707007170072700737007470075700767007770078700797008070081700827008370084700857008670087700887008970090700917009270093700947009570096700977009870099701007010170102701037010470105701067010770108701097011070111701127011370114701157011670117701187011970120701217012270123701247012570126701277012870129701307013170132701337013470135701367013770138701397014070141701427014370144701457014670147701487014970150701517015270153701547015570156701577015870159701607016170162701637016470165701667016770168701697017070171701727017370174701757017670177701787017970180701817018270183701847018570186701877018870189701907019170192701937019470195701967019770198701997020070201702027020370204702057020670207702087020970210702117021270213702147021570216702177021870219702207022170222702237022470225702267022770228702297023070231702327023370234702357023670237702387023970240702417024270243702447024570246702477024870249702507025170252702537025470255702567025770258702597026070261702627026370264702657026670267702687026970270702717027270273702747027570276702777027870279702807028170282702837028470285702867028770288702897029070291702927029370294702957029670297702987029970300703017030270303703047030570306703077030870309703107031170312703137031470315703167031770318703197032070321703227032370324703257032670327703287032970330703317033270333703347033570336703377033870339703407034170342703437034470345703467034770348703497035070351703527035370354703557035670357703587035970360703617036270363703647036570366703677036870369703707037170372703737037470375703767037770378703797038070381703827038370384703857038670387703887038970390703917039270393703947039570396703977039870399704007040170402704037040470405704067040770408704097041070411704127041370414704157041670417704187041970420704217042270423704247042570426704277042870429704307043170432704337043470435704367043770438704397044070441704427044370444704457044670447704487044970450704517045270453704547045570456704577045870459704607046170462704637046470465704667046770468704697047070471704727047370474704757047670477704787047970480704817048270483704847048570486704877048870489704907049170492704937049470495704967049770498704997050070501705027050370504705057050670507705087050970510705117051270513705147051570516705177051870519705207052170522705237052470525705267052770528705297053070531705327053370534705357053670537705387053970540705417054270543705447054570546705477054870549705507055170552705537055470555705567055770558705597056070561705627056370564705657056670567705687056970570705717057270573705747057570576705777057870579705807058170582705837058470585705867058770588705897059070591705927059370594705957059670597705987059970600706017060270603706047060570606706077060870609706107061170612706137061470615706167061770618706197062070621706227062370624706257062670627706287062970630706317063270633706347063570636706377063870639706407064170642706437064470645706467064770648706497065070651706527065370654706557065670657706587065970660706617066270663706647066570666706677066870669706707067170672706737067470675706767067770678706797068070681706827068370684706857068670687706887068970690706917069270693706947069570696706977069870699707007070170702707037070470705707067070770708707097071070711707127071370714707157071670717707187071970720707217072270723707247072570726707277072870729707307073170732707337073470735707367073770738707397074070741707427074370744707457074670747707487074970750707517075270753707547075570756707577075870759707607076170762707637076470765707667076770768707697077070771707727077370774707757077670777707787077970780707817078270783707847078570786707877078870789707907079170792707937079470795707967079770798707997080070801708027080370804708057080670807708087080970810708117081270813708147081570816708177081870819708207082170822708237082470825708267082770828708297083070831708327083370834708357083670837708387083970840708417084270843708447084570846708477084870849708507085170852708537085470855708567085770858708597086070861708627086370864708657086670867708687086970870708717087270873708747087570876708777087870879708807088170882708837088470885708867088770888708897089070891708927089370894708957089670897708987089970900709017090270903709047090570906709077090870909709107091170912709137091470915709167091770918709197092070921709227092370924709257092670927709287092970930709317093270933709347093570936709377093870939709407094170942709437094470945709467094770948709497095070951709527095370954709557095670957709587095970960709617096270963709647096570966709677096870969709707097170972709737097470975709767097770978709797098070981709827098370984709857098670987709887098970990709917099270993709947099570996709977099870999710007100171002710037100471005710067100771008710097101071011710127101371014710157101671017710187101971020710217102271023710247102571026710277102871029710307103171032710337103471035710367103771038710397104071041710427104371044710457104671047710487104971050710517105271053710547105571056710577105871059710607106171062710637106471065710667106771068710697107071071710727107371074710757107671077710787107971080710817108271083710847108571086710877108871089710907109171092710937109471095710967109771098710997110071101711027110371104711057110671107711087110971110711117111271113711147111571116711177111871119711207112171122711237112471125711267112771128711297113071131711327113371134711357113671137711387113971140711417114271143711447114571146711477114871149711507115171152711537115471155711567115771158711597116071161711627116371164711657116671167711687116971170711717117271173711747117571176711777117871179711807118171182711837118471185711867118771188711897119071191711927119371194711957119671197711987119971200712017120271203712047120571206712077120871209712107121171212712137121471215712167121771218712197122071221712227122371224712257122671227712287122971230712317123271233712347123571236712377123871239712407124171242712437124471245712467124771248712497125071251712527125371254712557125671257712587125971260712617126271263712647126571266712677126871269712707127171272712737127471275712767127771278712797128071281712827128371284712857128671287712887128971290712917129271293712947129571296712977129871299713007130171302713037130471305713067130771308713097131071311713127131371314713157131671317713187131971320713217132271323713247132571326713277132871329713307133171332713337133471335713367133771338713397134071341713427134371344713457134671347713487134971350713517135271353713547135571356713577135871359713607136171362713637136471365713667136771368713697137071371713727137371374713757137671377713787137971380713817138271383713847138571386713877138871389713907139171392713937139471395713967139771398713997140071401714027140371404714057140671407714087140971410714117141271413714147141571416714177141871419714207142171422714237142471425714267142771428714297143071431714327143371434714357143671437714387143971440714417144271443714447144571446714477144871449714507145171452714537145471455714567145771458714597146071461714627146371464714657146671467714687146971470714717147271473714747147571476714777147871479714807148171482714837148471485714867148771488714897149071491714927149371494714957149671497714987149971500715017150271503715047150571506715077150871509715107151171512715137151471515715167151771518715197152071521715227152371524715257152671527715287152971530715317153271533715347153571536715377153871539715407154171542715437154471545715467154771548715497155071551715527155371554715557155671557715587155971560715617156271563715647156571566715677156871569715707157171572715737157471575715767157771578715797158071581715827158371584715857158671587715887158971590715917159271593715947159571596715977159871599716007160171602716037160471605716067160771608716097161071611716127161371614716157161671617716187161971620716217162271623716247162571626716277162871629716307163171632716337163471635716367163771638716397164071641716427164371644716457164671647716487164971650716517165271653716547165571656716577165871659716607166171662716637166471665716667166771668716697167071671716727167371674716757167671677716787167971680716817168271683716847168571686716877168871689716907169171692716937169471695716967169771698716997170071701717027170371704717057170671707717087170971710717117171271713717147171571716717177171871719717207172171722717237172471725717267172771728717297173071731717327173371734717357173671737717387173971740717417174271743717447174571746717477174871749717507175171752717537175471755717567175771758717597176071761717627176371764717657176671767717687176971770717717177271773717747177571776717777177871779717807178171782717837178471785717867178771788717897179071791717927179371794717957179671797717987179971800718017180271803718047180571806718077180871809718107181171812718137181471815718167181771818718197182071821718227182371824718257182671827718287182971830718317183271833718347183571836718377183871839718407184171842718437184471845718467184771848718497185071851718527185371854718557185671857718587185971860718617186271863718647186571866718677186871869718707187171872718737187471875718767187771878718797188071881718827188371884718857188671887718887188971890718917189271893718947189571896718977189871899719007190171902719037190471905719067190771908719097191071911719127191371914719157191671917719187191971920719217192271923719247192571926719277192871929719307193171932719337193471935719367193771938719397194071941719427194371944719457194671947719487194971950719517195271953719547195571956719577195871959719607196171962719637196471965719667196771968719697197071971719727197371974719757197671977719787197971980719817198271983719847198571986719877198871989719907199171992719937199471995719967199771998719997200072001720027200372004720057200672007720087200972010720117201272013720147201572016720177201872019720207202172022720237202472025720267202772028720297203072031720327203372034720357203672037720387203972040720417204272043720447204572046720477204872049720507205172052720537205472055720567205772058720597206072061720627206372064720657206672067720687206972070720717207272073720747207572076720777207872079720807208172082720837208472085720867208772088720897209072091720927209372094720957209672097720987209972100721017210272103721047210572106721077210872109721107211172112721137211472115721167211772118721197212072121721227212372124721257212672127721287212972130721317213272133721347213572136721377213872139721407214172142721437214472145721467214772148721497215072151721527215372154721557215672157721587215972160721617216272163721647216572166721677216872169721707217172172721737217472175721767217772178721797218072181721827218372184721857218672187721887218972190721917219272193721947219572196721977219872199722007220172202722037220472205722067220772208722097221072211722127221372214722157221672217722187221972220722217222272223722247222572226722277222872229722307223172232722337223472235722367223772238722397224072241722427224372244722457224672247722487224972250722517225272253722547225572256722577225872259722607226172262722637226472265722667226772268722697227072271722727227372274722757227672277722787227972280722817228272283722847228572286722877228872289722907229172292722937229472295722967229772298722997230072301723027230372304723057230672307723087230972310723117231272313723147231572316723177231872319723207232172322723237232472325723267232772328723297233072331723327233372334723357233672337723387233972340723417234272343723447234572346723477234872349723507235172352723537235472355723567235772358723597236072361723627236372364723657236672367723687236972370723717237272373723747237572376
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #endif
  215. #ifndef NO_SIG_WRAPPER
  216. #include <wolfssl/wolfcrypt/signature.h>
  217. #endif
  218. #ifdef HAVE_AESCCM
  219. #include <wolfssl/wolfcrypt/aes.h>
  220. #endif
  221. #ifdef HAVE_PKCS7
  222. #include <wolfssl/wolfcrypt/pkcs7.h>
  223. #include <wolfssl/wolfcrypt/asn.h>
  224. #ifdef HAVE_LIBZ
  225. #include <wolfssl/wolfcrypt/compress.h>
  226. #endif
  227. #endif
  228. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  229. #include <wolfssl/wolfcrypt/asn.h>
  230. #endif
  231. #ifndef NO_DSA
  232. #include <wolfssl/wolfcrypt/dsa.h>
  233. #ifndef ONEK_BUF
  234. #define ONEK_BUF 1024
  235. #endif
  236. #ifndef TWOK_BUF
  237. #define TWOK_BUF 2048
  238. #endif
  239. #ifndef FOURK_BUF
  240. #define FOURK_BUF 4096
  241. #endif
  242. #ifndef DSA_SIG_SIZE
  243. #define DSA_SIG_SIZE 40
  244. #endif
  245. #ifndef MAX_DSA_PARAM_SIZE
  246. #define MAX_DSA_PARAM_SIZE 256
  247. #endif
  248. #endif
  249. #ifdef WOLFSSL_CMAC
  250. #include <wolfssl/wolfcrypt/cmac.h>
  251. #endif
  252. #ifdef HAVE_ED25519
  253. #include <wolfssl/wolfcrypt/ed25519.h>
  254. #endif
  255. #ifdef HAVE_CURVE25519
  256. #include <wolfssl/wolfcrypt/curve25519.h>
  257. #endif
  258. #ifdef HAVE_ED448
  259. #include <wolfssl/wolfcrypt/ed448.h>
  260. #endif
  261. #ifdef HAVE_CURVE448
  262. #include <wolfssl/wolfcrypt/curve448.h>
  263. #endif
  264. #ifdef HAVE_PKCS12
  265. #include <wolfssl/wolfcrypt/pkcs12.h>
  266. #endif
  267. #include <wolfssl/wolfcrypt/logging.h>
  268. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  269. #include <wolfssl/openssl/ssl.h>
  270. #ifndef NO_ASN
  271. /* for ASN_COMMON_NAME DN_tags enum */
  272. #include <wolfssl/wolfcrypt/asn.h>
  273. #endif
  274. #ifdef HAVE_OCSP
  275. #include <wolfssl/openssl/ocsp.h>
  276. #endif
  277. #endif
  278. #ifdef OPENSSL_EXTRA
  279. #include <wolfssl/openssl/cmac.h>
  280. #include <wolfssl/openssl/x509v3.h>
  281. #include <wolfssl/openssl/asn1.h>
  282. #include <wolfssl/openssl/crypto.h>
  283. #include <wolfssl/openssl/pkcs12.h>
  284. #include <wolfssl/openssl/evp.h>
  285. #include <wolfssl/openssl/dh.h>
  286. #include <wolfssl/openssl/bn.h>
  287. #include <wolfssl/openssl/buffer.h>
  288. #include <wolfssl/openssl/pem.h>
  289. #include <wolfssl/openssl/ec.h>
  290. #include <wolfssl/openssl/ecdh.h>
  291. #include <wolfssl/openssl/engine.h>
  292. #include <wolfssl/openssl/hmac.h>
  293. #include <wolfssl/openssl/objects.h>
  294. #include <wolfssl/openssl/rand.h>
  295. #include <wolfssl/openssl/modes.h>
  296. #include <wolfssl/openssl/fips_rand.h>
  297. #include <wolfssl/openssl/kdf.h>
  298. #ifdef OPENSSL_ALL
  299. #include <wolfssl/openssl/txt_db.h>
  300. #include <wolfssl/openssl/lhash.h>
  301. #endif
  302. #ifndef NO_AES
  303. #include <wolfssl/openssl/aes.h>
  304. #endif
  305. #ifndef NO_DES3
  306. #include <wolfssl/openssl/des.h>
  307. #endif
  308. #ifndef NO_RC4
  309. #include <wolfssl/openssl/rc4.h>
  310. #endif
  311. #ifdef HAVE_ECC
  312. #include <wolfssl/openssl/ecdsa.h>
  313. #endif
  314. #ifdef HAVE_PKCS7
  315. #include <wolfssl/openssl/pkcs7.h>
  316. #endif
  317. #ifdef HAVE_ED25519
  318. #include <wolfssl/openssl/ed25519.h>
  319. #endif
  320. #ifdef HAVE_ED448
  321. #include <wolfssl/openssl/ed448.h>
  322. #endif
  323. #endif /* OPENSSL_EXTRA */
  324. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  325. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  326. #include <wolfssl/wolfcrypt/srp.h>
  327. #endif
  328. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  329. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  330. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  331. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  332. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  333. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  334. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  335. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  336. * number tracking */
  337. #include "wolfssl/internal.h"
  338. #endif
  339. /* force enable test buffers */
  340. #ifndef USE_CERT_BUFFERS_2048
  341. #define USE_CERT_BUFFERS_2048
  342. #endif
  343. #ifndef USE_CERT_BUFFERS_256
  344. #define USE_CERT_BUFFERS_256
  345. #endif
  346. #include <wolfssl/certs_test.h>
  347. #include "tests/utils.h"
  348. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  349. * have default (hidden) visibility, and in the absence of visibility, it's
  350. * benign to mask out the library implementation.
  351. */
  352. #define WOLFSSL_MISC_INCLUDED
  353. #include <wolfcrypt/src/misc.c>
  354. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  355. /* FIPS build has replaced ecc.h. */
  356. #define wc_ecc_key_get_priv(key) (&((key)->k))
  357. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  358. #endif
  359. typedef struct testVector {
  360. const char* input;
  361. const char* output;
  362. size_t inLen;
  363. size_t outLen;
  364. } testVector;
  365. #if defined(HAVE_PKCS7)
  366. typedef struct {
  367. const byte* content;
  368. word32 contentSz;
  369. int contentOID;
  370. int encryptOID;
  371. int keyWrapOID;
  372. int keyAgreeOID;
  373. byte* cert;
  374. size_t certSz;
  375. byte* privateKey;
  376. word32 privateKeySz;
  377. } pkcs7EnvelopedVector;
  378. #ifndef NO_PKCS7_ENCRYPTED_DATA
  379. typedef struct {
  380. const byte* content;
  381. word32 contentSz;
  382. int contentOID;
  383. int encryptOID;
  384. byte* encryptionKey;
  385. word32 encryptionKeySz;
  386. } pkcs7EncryptedVector;
  387. #endif
  388. #endif /* HAVE_PKCS7 */
  389. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  390. typedef int (*ssl_cb)(WOLFSSL* ssl);
  391. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  392. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  393. typedef struct test_ssl_cbf {
  394. method_provider method;
  395. ctx_cb ctx_ready;
  396. ssl_cb ssl_ready;
  397. ssl_cb on_result;
  398. ctx_cb on_ctx_cleanup;
  399. ssl_cb on_cleanup;
  400. hs_cb on_handshake;
  401. WOLFSSL_CTX* ctx;
  402. const char* caPemFile;
  403. const char* certPemFile;
  404. const char* keyPemFile;
  405. const char* crlPemFile;
  406. #ifdef WOLFSSL_STATIC_MEMORY
  407. byte* mem;
  408. word32 memSz;
  409. wolfSSL_method_func method_ex;
  410. #endif
  411. int devId;
  412. int return_code;
  413. int last_err;
  414. unsigned char isSharedCtx:1;
  415. unsigned char loadToSSL:1;
  416. unsigned char ticNoInit:1;
  417. unsigned char doUdp:1;
  418. } test_ssl_cbf;
  419. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  420. typedef struct test_ssl_memio_ctx {
  421. WOLFSSL_CTX* s_ctx;
  422. WOLFSSL_CTX* c_ctx;
  423. WOLFSSL* s_ssl;
  424. WOLFSSL* c_ssl;
  425. const char* c_ciphers;
  426. const char* s_ciphers;
  427. char* c_msg;
  428. int c_msglen;
  429. char* s_msg;
  430. int s_msglen;
  431. test_ssl_cbf s_cb;
  432. test_ssl_cbf c_cb;
  433. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  434. int c_len;
  435. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  436. int s_len;
  437. } test_ssl_memio_ctx;
  438. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  439. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  440. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  441. const char* currentTestName;
  442. char tmpDirName[16];
  443. int tmpDirNameSet = 0;
  444. #endif
  445. /*----------------------------------------------------------------------------*
  446. | Constants
  447. *----------------------------------------------------------------------------*/
  448. /* Test result constants and macros. */
  449. /* Test succeeded. */
  450. #define TEST_SUCCESS (1)
  451. /* Test failed. */
  452. #define TEST_FAIL (0)
  453. /* Test skipped - not run. */
  454. #define TEST_SKIPPED (-7777)
  455. /* Returns the result based on whether check is true.
  456. *
  457. * @param [in] check Condition for success.
  458. * @return When condition is true: TEST_SUCCESS.
  459. * @return When condition is false: TEST_FAIL.
  460. */
  461. #ifdef DEBUG_WOLFSSL_VERBOSE
  462. #define XSTRINGIFY(s) STRINGIFY(s)
  463. #define STRINGIFY(s) #s
  464. #define TEST_RES_CHECK(check) ({ \
  465. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  466. if (_ret == TEST_FAIL) { \
  467. fprintf(stderr, " check \"%s\" at %d ", \
  468. XSTRINGIFY(check), __LINE__); \
  469. } \
  470. _ret; })
  471. #else
  472. #define TEST_RES_CHECK(check) \
  473. ((check) ? TEST_SUCCESS : TEST_FAIL)
  474. #endif /* DEBUG_WOLFSSL_VERBOSE */
  475. #define TEST_STRING "Everyone gets Friday off."
  476. #define TEST_STRING_SZ 25
  477. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  478. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  479. #define TEST_RSA_BITS 1024
  480. #else
  481. #define TEST_RSA_BITS 2048
  482. #endif
  483. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  484. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  485. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  486. static const char* bogusFile =
  487. #ifdef _WIN32
  488. "NUL"
  489. #else
  490. "/dev/null"
  491. #endif
  492. ;
  493. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  494. enum {
  495. TESTING_RSA = 1,
  496. TESTING_ECC = 2
  497. };
  498. #ifdef WOLFSSL_QNX_CAAM
  499. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  500. static int testDevId = WOLFSSL_CAAM_DEVID;
  501. #else
  502. static int testDevId = INVALID_DEVID;
  503. #endif
  504. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  505. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  506. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  507. #define HAVE_IO_TESTS_DEPENDENCIES
  508. #endif
  509. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  510. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  511. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  512. #endif
  513. /*----------------------------------------------------------------------------*
  514. | BIO with fixed read/write size
  515. *----------------------------------------------------------------------------*/
  516. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  517. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  518. int len)
  519. {
  520. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  521. len = 0;
  522. }
  523. else {
  524. if (bio->wrSz - bio->wrIdx < len) {
  525. len = bio->wrSz - bio->wrIdx;
  526. }
  527. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  528. bio->wrIdx += len;
  529. }
  530. return len;
  531. }
  532. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  533. {
  534. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  535. len = 0;
  536. }
  537. else {
  538. if (bio->wrSz - bio->rdIdx < len) {
  539. len = bio->wrSz - bio->rdIdx;
  540. }
  541. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  542. bio->rdIdx += len;
  543. }
  544. return len;
  545. }
  546. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  547. {
  548. static WOLFSSL_BIO_METHOD meth;
  549. meth.type = WOLFSSL_BIO_BIO;
  550. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  551. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  552. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  553. return &meth;
  554. }
  555. #endif
  556. /*----------------------------------------------------------------------------*
  557. | Setup
  558. *----------------------------------------------------------------------------*/
  559. static int test_wolfSSL_Init(void)
  560. {
  561. EXPECT_DECLS;
  562. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  563. return EXPECT_RESULT();
  564. }
  565. static int test_wolfSSL_Cleanup(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  569. return EXPECT_RESULT();
  570. }
  571. /* Initialize the wolfCrypt state.
  572. * POST: 0 success.
  573. */
  574. static int test_wolfCrypt_Init(void)
  575. {
  576. EXPECT_DECLS;
  577. ExpectIntEQ(wolfCrypt_Init(), 0);
  578. return EXPECT_RESULT();
  579. } /* END test_wolfCrypt_Init */
  580. static int test_wolfCrypt_Cleanup(void)
  581. {
  582. EXPECT_DECLS;
  583. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  584. return EXPECT_RESULT();
  585. }
  586. /*----------------------------------------------------------------------------*
  587. | Platform dependent function test
  588. *----------------------------------------------------------------------------*/
  589. static int test_fileAccess(void)
  590. {
  591. EXPECT_DECLS;
  592. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  593. const char *fname[] = {
  594. svrCertFile, svrKeyFile, caCertFile,
  595. eccCertFile, eccKeyFile, eccRsaCertFile,
  596. cliCertFile, cliCertDerFile, cliKeyFile,
  597. dhParamFile,
  598. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  599. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  600. NULL
  601. };
  602. const char derfile[] = "./certs/server-cert.der";
  603. XFILE f = XBADFILE;
  604. size_t sz;
  605. byte *buff = NULL;
  606. int i;
  607. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  608. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  609. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  610. XFCLOSE(f);
  611. }
  612. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  613. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  614. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  615. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  616. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  617. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  618. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  619. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  620. XFCLOSE(f);
  621. #endif
  622. return EXPECT_RESULT();
  623. }
  624. /*----------------------------------------------------------------------------*
  625. | Method Allocators
  626. *----------------------------------------------------------------------------*/
  627. static int test_wolfSSL_Method_Allocators(void)
  628. {
  629. EXPECT_DECLS;
  630. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  631. do { \
  632. WOLFSSL_METHOD *method = NULL; \
  633. condition(method = allocator()); \
  634. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  635. } while (0)
  636. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  637. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  638. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  639. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  640. #ifndef NO_OLD_TLS
  641. #ifdef WOLFSSL_ALLOW_SSLV3
  642. #ifndef NO_WOLFSSL_SERVER
  643. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  644. #endif
  645. #ifndef NO_WOLFSSL_CLIENT
  646. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  647. #endif
  648. #endif
  649. #ifdef WOLFSSL_ALLOW_TLSV10
  650. #ifndef NO_WOLFSSL_SERVER
  651. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  652. #endif
  653. #ifndef NO_WOLFSSL_CLIENT
  654. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  655. #endif
  656. #endif
  657. #ifndef NO_WOLFSSL_SERVER
  658. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  659. #endif
  660. #ifndef NO_WOLFSSL_CLIENT
  661. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  662. #endif
  663. #endif /* !NO_OLD_TLS */
  664. #ifndef WOLFSSL_NO_TLS12
  665. #ifndef NO_WOLFSSL_SERVER
  666. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  667. #endif
  668. #ifndef NO_WOLFSSL_CLIENT
  669. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  670. #endif
  671. #endif /* !WOLFSSL_NO_TLS12 */
  672. #ifdef WOLFSSL_TLS13
  673. #ifndef NO_WOLFSSL_SERVER
  674. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  675. #endif
  676. #ifndef NO_WOLFSSL_CLIENT
  677. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  678. #endif
  679. #endif /* WOLFSSL_TLS13 */
  680. #ifndef NO_WOLFSSL_SERVER
  681. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  682. #endif
  683. #ifndef NO_WOLFSSL_CLIENT
  684. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  685. #endif
  686. #ifdef WOLFSSL_DTLS
  687. #ifndef NO_OLD_TLS
  688. #ifndef NO_WOLFSSL_SERVER
  689. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  690. #endif
  691. #ifndef NO_WOLFSSL_CLIENT
  692. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  693. #endif
  694. #endif
  695. #ifndef WOLFSSL_NO_TLS12
  696. #ifndef NO_WOLFSSL_SERVER
  697. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  698. #endif
  699. #ifndef NO_WOLFSSL_CLIENT
  700. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  701. #endif
  702. #endif
  703. #endif /* WOLFSSL_DTLS */
  704. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  705. /* Stubs */
  706. #ifndef NO_WOLFSSL_SERVER
  707. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  708. #endif
  709. #ifndef NO_WOLFSSL_CLIENT
  710. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  711. #endif
  712. #endif
  713. /* Test Either Method (client or server) */
  714. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  715. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  716. #ifndef NO_OLD_TLS
  717. #ifdef WOLFSSL_ALLOW_TLSV10
  718. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  719. #endif
  720. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  721. #endif /* !NO_OLD_TLS */
  722. #ifndef WOLFSSL_NO_TLS12
  723. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  724. #endif /* !WOLFSSL_NO_TLS12 */
  725. #ifdef WOLFSSL_TLS13
  726. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  727. #endif /* WOLFSSL_TLS13 */
  728. #ifdef WOLFSSL_DTLS
  729. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  730. #ifndef NO_OLD_TLS
  731. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  732. #endif /* !NO_OLD_TLS */
  733. #ifndef WOLFSSL_NO_TLS12
  734. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  735. #endif /* !WOLFSSL_NO_TLS12 */
  736. #endif /* WOLFSSL_DTLS */
  737. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  738. return EXPECT_RESULT();
  739. }
  740. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  741. /*----------------------------------------------------------------------------*
  742. | Dual algorithm Certificate Tests
  743. *----------------------------------------------------------------------------*/
  744. #define LARGE_TEMP_SZ 4096
  745. /* To better understand this, please see the X9.146 example in wolfssl-examples
  746. * repo. */
  747. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  748. char *sapkiFile, char *altPrivFile)
  749. {
  750. EXPECT_DECLS;
  751. FILE* file = NULL;
  752. Cert newCert;
  753. DecodedCert preTBS;
  754. byte caKeyBuf[LARGE_TEMP_SZ];
  755. word32 caKeySz = LARGE_TEMP_SZ;
  756. byte sapkiBuf[LARGE_TEMP_SZ];
  757. word32 sapkiSz = LARGE_TEMP_SZ;
  758. byte altPrivBuf[LARGE_TEMP_SZ];
  759. word32 altPrivSz = LARGE_TEMP_SZ;
  760. byte altSigAlgBuf[LARGE_TEMP_SZ];
  761. word32 altSigAlgSz = LARGE_TEMP_SZ;
  762. byte scratchBuf[LARGE_TEMP_SZ];
  763. word32 scratchSz = LARGE_TEMP_SZ;
  764. byte preTbsBuf[LARGE_TEMP_SZ];
  765. word32 preTbsSz = LARGE_TEMP_SZ;
  766. byte altSigValBuf[LARGE_TEMP_SZ];
  767. word32 altSigValSz = LARGE_TEMP_SZ;
  768. byte *outBuf = NULL;
  769. word32 outSz = LARGE_TEMP_SZ;
  770. WC_RNG rng;
  771. RsaKey caKey;
  772. ecc_key altCaKey;
  773. word32 idx = 0;
  774. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  775. DYNAMIC_TYPE_TMP_BUFFER));
  776. ExpectIntEQ(wc_InitRng(&rng), 0);
  777. XMEMSET(caKeyBuf, 0, caKeySz);
  778. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  779. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  780. if (file) {
  781. fclose(file);
  782. file = NULL;
  783. }
  784. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  785. idx = 0;
  786. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  787. 0);
  788. XMEMSET(sapkiBuf, 0, sapkiSz);
  789. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  790. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  791. if (file) {
  792. fclose(file);
  793. file = NULL;
  794. }
  795. XMEMSET(altPrivBuf, 0, altPrivSz);
  796. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  797. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  798. if (file) {
  799. fclose(file);
  800. file = NULL;
  801. }
  802. wc_ecc_init(&altCaKey);
  803. idx = 0;
  804. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  805. (word32)altPrivSz), 0);
  806. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  807. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  808. oidSigType, 0), 0);
  809. wc_InitCert(&newCert);
  810. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  811. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  812. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  813. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  814. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  815. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  816. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  817. newCert.sigType = CTC_SHA256wRSA;
  818. newCert.isCA = 1;
  819. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  820. (const byte *)"This is NOT a critical extension", 32), 0);
  821. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  822. sapkiSz), 0);
  823. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  824. altSigAlgSz), 0);
  825. XMEMSET(scratchBuf, 0, scratchSz);
  826. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  827. &caKey, &rng), 0);
  828. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  829. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  830. XMEMSET(preTbsBuf, 0, preTbsSz);
  831. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  832. XMEMSET(altSigValBuf, 0, altSigValSz);
  833. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  834. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  835. &rng), 0);
  836. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  837. altSigValSz), 0);
  838. /* Finally, generate the new certificate. */
  839. XMEMSET(outBuf, 0, outSz);
  840. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  841. 0);
  842. *out = outBuf;
  843. wc_FreeRsaKey(&caKey);
  844. wc_FreeRng(&rng);
  845. wc_FreeDecodedCert(&preTBS);
  846. return outSz;
  847. }
  848. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  849. char *sapkiFile, char *altPrivFile,
  850. char *serverKeyFile,
  851. byte *caCertBuf, int caCertSz)
  852. {
  853. EXPECT_DECLS;
  854. FILE* file = NULL;
  855. Cert newCert;
  856. DecodedCert preTBS;
  857. byte serverKeyBuf[LARGE_TEMP_SZ];
  858. word32 serverKeySz = LARGE_TEMP_SZ;
  859. byte caKeyBuf[LARGE_TEMP_SZ];
  860. word32 caKeySz = LARGE_TEMP_SZ;
  861. byte sapkiBuf[LARGE_TEMP_SZ];
  862. word32 sapkiSz = LARGE_TEMP_SZ;
  863. byte altPrivBuf[LARGE_TEMP_SZ];
  864. word32 altPrivSz = LARGE_TEMP_SZ;
  865. byte altSigAlgBuf[LARGE_TEMP_SZ];
  866. word32 altSigAlgSz = LARGE_TEMP_SZ;
  867. byte scratchBuf[LARGE_TEMP_SZ];
  868. word32 scratchSz = LARGE_TEMP_SZ;
  869. byte preTbsBuf[LARGE_TEMP_SZ];
  870. word32 preTbsSz = LARGE_TEMP_SZ;
  871. byte altSigValBuf[LARGE_TEMP_SZ];
  872. word32 altSigValSz = LARGE_TEMP_SZ;
  873. byte *outBuf = NULL;
  874. word32 outSz = LARGE_TEMP_SZ;
  875. WC_RNG rng;
  876. RsaKey caKey;
  877. RsaKey serverKey;
  878. ecc_key altCaKey;
  879. word32 idx = 0;
  880. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  881. DYNAMIC_TYPE_TMP_BUFFER));
  882. ExpectIntEQ(wc_InitRng(&rng), 0);
  883. XMEMSET(serverKeyBuf, 0, serverKeySz);
  884. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  885. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  886. 0);
  887. if (file) {
  888. fclose(file);
  889. file = NULL;
  890. }
  891. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  892. idx = 0;
  893. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  894. (word32)serverKeySz), 0);
  895. XMEMSET(caKeyBuf, 0, caKeySz);
  896. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  897. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  898. if (file) {
  899. fclose(file);
  900. file = NULL;
  901. }
  902. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  903. idx = 0;
  904. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  905. (word32)caKeySz), 0);
  906. XMEMSET(sapkiBuf, 0, sapkiSz);
  907. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  908. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  909. if (file) {
  910. fclose(file);
  911. file = NULL;
  912. }
  913. XMEMSET(altPrivBuf, 0, altPrivSz);
  914. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  915. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  916. if (file) {
  917. fclose(file);
  918. file = NULL;
  919. }
  920. wc_ecc_init(&altCaKey);
  921. idx = 0;
  922. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  923. (word32)altPrivSz), 0);
  924. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  925. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  926. oidSigType, 0), 0);
  927. wc_InitCert(&newCert);
  928. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  929. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  930. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  931. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  932. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  933. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  934. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  935. newCert.sigType = CTC_SHA256wRSA;
  936. newCert.isCA = 0;
  937. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  938. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  939. (const byte *)"This is NOT a critical extension", 32), 0);
  940. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  941. sapkiSz), 0);
  942. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  943. altSigAlgSz), 0);
  944. XMEMSET(scratchBuf, 0, scratchSz);
  945. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  946. &rng), 0);
  947. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  948. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  949. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  950. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  951. XMEMSET(preTbsBuf, 0, preTbsSz);
  952. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  953. XMEMSET(altSigValBuf, 0, altSigValSz);
  954. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  955. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  956. &rng), 0);
  957. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  958. altSigValBuf, altSigValSz), 0);
  959. /* Finally, generate the new certificate. */
  960. XMEMSET(outBuf, 0, outSz);
  961. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  962. 0);
  963. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  964. outSz, &caKey, NULL, &rng), 0);
  965. *out = outBuf;
  966. wc_FreeRsaKey(&caKey);
  967. wc_FreeRsaKey(&serverKey);
  968. wc_FreeRng(&rng);
  969. wc_FreeDecodedCert(&preTBS);
  970. return outSz;
  971. }
  972. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  973. byte *serverCert, word32 serverCertSz,
  974. byte *serverKey, word32 serverKeySz,
  975. int negative_test)
  976. {
  977. EXPECT_DECLS;
  978. WOLFSSL_CTX *ctx_c = NULL;
  979. WOLFSSL_CTX *ctx_s = NULL;
  980. WOLFSSL *ssl_c = NULL;
  981. WOLFSSL *ssl_s = NULL;
  982. struct test_memio_ctx test_ctx;
  983. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  984. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  985. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  986. caCert, caCertSz, serverCert, serverCertSz,
  987. serverKey, serverKeySz), 0);
  988. if (negative_test) {
  989. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  990. }
  991. else {
  992. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  993. }
  994. wolfSSL_free(ssl_c);
  995. wolfSSL_free(ssl_s);
  996. wolfSSL_CTX_free(ctx_c);
  997. wolfSSL_CTX_free(ctx_s);
  998. return EXPECT_RESULT();
  999. }
  1000. static int extCount = 0;
  1001. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1002. const unsigned char* der, word32 derSz)
  1003. {
  1004. (void) oid;
  1005. (void) oidSz;
  1006. (void) crit;
  1007. (void) der;
  1008. (void) derSz;
  1009. extCount ++;
  1010. /* Accept all extensions. This is only a test. Normally we would be much more
  1011. * careful about critical extensions. */
  1012. return 1;
  1013. }
  1014. static int test_dual_alg_support(void)
  1015. {
  1016. EXPECT_DECLS;
  1017. /* Root CA and server keys will be the same. This is only appropriate for
  1018. * testing. */
  1019. char keyFile[] = "./certs/ca-key.der";
  1020. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1021. char altPrivFile[] = "./certs/ecc-key.der";
  1022. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1023. byte *serverKey = NULL;
  1024. size_t serverKeySz = 0;
  1025. byte *root = NULL;
  1026. int rootSz = 0;
  1027. byte *server = NULL;
  1028. int serverSz = 0;
  1029. WOLFSSL_CERT_MANAGER* cm = NULL;
  1030. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1031. /* Base normal case. */
  1032. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1033. ExpectNotNull(root);
  1034. ExpectIntGT(rootSz, 0);
  1035. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1036. altPrivFile, keyFile, root, rootSz);
  1037. ExpectNotNull(server);
  1038. ExpectIntGT(serverSz, 0);
  1039. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1040. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1041. TEST_SUCCESS);
  1042. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1043. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1044. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1045. * the alternative signature and then set negative_test to true for the
  1046. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1047. * because the signature won't verify. The exception is if
  1048. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1049. * and this is no longer a negative test. */
  1050. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1051. ExpectNotNull(root);
  1052. ExpectIntGT(rootSz, 0);
  1053. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1054. wrongPrivFile, keyFile, root, rootSz);
  1055. ExpectNotNull(server);
  1056. ExpectIntGT(serverSz, 0);
  1057. #ifdef WOLFSSL_TRUST_PEER_CERT
  1058. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1059. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1060. TEST_SUCCESS);
  1061. #else
  1062. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1063. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1064. TEST_SUCCESS);
  1065. #endif
  1066. /* Lets see if CertManager can find the new extensions */
  1067. extCount = 0;
  1068. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1069. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1070. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1071. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1072. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1073. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1074. /* There is only 1 unknown exension (1.2.3.4.5). The other ones are known
  1075. * because they are for the dual alg extensions. */
  1076. ExpectIntEQ(extCount, 1);
  1077. wolfSSL_CertManagerFree(cm);
  1078. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1079. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1080. free(serverKey);
  1081. return EXPECT_RESULT();
  1082. }
  1083. #else
  1084. static int test_dual_alg_support(void)
  1085. {
  1086. return TEST_SKIPPED;
  1087. }
  1088. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1089. /*----------------------------------------------------------------------------*
  1090. | Context
  1091. *----------------------------------------------------------------------------*/
  1092. #ifndef NO_WOLFSSL_SERVER
  1093. static int test_wolfSSL_CTX_new(void)
  1094. {
  1095. EXPECT_DECLS;
  1096. WOLFSSL_CTX *ctx;
  1097. WOLFSSL_METHOD* method;
  1098. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1099. ExpectNotNull(method = wolfSSLv23_server_method());
  1100. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1101. wolfSSL_CTX_free(ctx);
  1102. return EXPECT_RESULT();
  1103. }
  1104. #endif
  1105. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1106. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1107. static int test_for_double_Free(void)
  1108. {
  1109. EXPECT_DECLS;
  1110. WOLFSSL_CTX* ctx = NULL;
  1111. WOLFSSL* ssl = NULL;
  1112. int skipTest = 0;
  1113. const char* testCertFile;
  1114. const char* testKeyFile;
  1115. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1116. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1117. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1118. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1119. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1120. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1121. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1122. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1123. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1124. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1125. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1126. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1127. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1128. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1129. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1130. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1131. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1132. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1133. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1134. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1135. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1136. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1137. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1138. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1139. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1140. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1141. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1142. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1143. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1144. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1145. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1146. #ifdef OPENSSL_EXTRA
  1147. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1148. "!SRP:!kRSA:!aNULL:!eNULL";
  1149. #endif
  1150. #ifndef NO_RSA
  1151. testCertFile = svrCertFile;
  1152. testKeyFile = svrKeyFile;
  1153. #elif defined(HAVE_ECC)
  1154. testCertFile = eccCertFile;
  1155. testKeyFile = eccKeyFile;
  1156. #else
  1157. skipTest = 1;
  1158. #endif
  1159. if (skipTest != 1) {
  1160. #ifndef NO_WOLFSSL_SERVER
  1161. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1162. #else
  1163. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1164. #endif
  1165. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1166. WOLFSSL_FILETYPE_PEM));
  1167. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1168. WOLFSSL_FILETYPE_PEM));
  1169. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1170. /* First test freeing SSL, then CTX */
  1171. wolfSSL_free(ssl);
  1172. ssl = NULL;
  1173. wolfSSL_CTX_free(ctx);
  1174. ctx = NULL;
  1175. #ifndef NO_WOLFSSL_CLIENT
  1176. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1177. #else
  1178. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1179. #endif
  1180. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1181. WOLFSSL_FILETYPE_PEM));
  1182. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1183. WOLFSSL_FILETYPE_PEM));
  1184. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1185. /* Next test freeing CTX then SSL */
  1186. wolfSSL_CTX_free(ctx);
  1187. ctx = NULL;
  1188. wolfSSL_free(ssl);
  1189. ssl = NULL;
  1190. #ifndef NO_WOLFSSL_SERVER
  1191. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1192. #else
  1193. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1194. #endif
  1195. /* Test setting ciphers at ctx level */
  1196. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1197. WOLFSSL_FILETYPE_PEM));
  1198. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1199. WOLFSSL_FILETYPE_PEM));
  1200. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1201. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1202. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1203. /* only update TLSv13 suites */
  1204. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1205. #endif
  1206. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1207. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1208. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1209. /* only update pre-TLSv13 suites */
  1210. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1211. "ECDHE-RSA-AES128-GCM-SHA256"));
  1212. #endif
  1213. #ifdef OPENSSL_EXTRA
  1214. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1215. #endif
  1216. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1217. wolfSSL_CTX_free(ctx);
  1218. ctx = NULL;
  1219. wolfSSL_free(ssl);
  1220. ssl = NULL;
  1221. #ifndef NO_WOLFSSL_CLIENT
  1222. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1223. #else
  1224. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1225. #endif
  1226. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1227. WOLFSSL_FILETYPE_PEM));
  1228. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1229. WOLFSSL_FILETYPE_PEM));
  1230. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1231. /* test setting ciphers at SSL level */
  1232. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1233. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1234. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1235. /* only update TLSv13 suites */
  1236. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1237. #endif
  1238. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1239. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1240. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1241. /* only update pre-TLSv13 suites */
  1242. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1243. #endif
  1244. wolfSSL_CTX_free(ctx);
  1245. ctx = NULL;
  1246. wolfSSL_free(ssl);
  1247. ssl = NULL;
  1248. }
  1249. return EXPECT_RESULT();
  1250. }
  1251. #endif
  1252. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1253. {
  1254. EXPECT_DECLS;
  1255. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1256. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1257. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1258. const char* testCertFile;
  1259. const char* testKeyFile;
  1260. WOLFSSL_CTX* ctx = NULL;
  1261. WOLFSSL* ssl = NULL;
  1262. const byte cipherList[] =
  1263. {
  1264. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1265. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1266. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1267. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1268. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1269. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1270. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1271. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1272. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1273. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1274. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1275. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1276. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1277. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1278. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1279. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1280. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1281. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1282. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1283. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1284. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1285. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1286. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1287. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1288. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1289. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1290. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1291. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1292. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1293. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1294. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1295. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1296. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1297. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1298. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1299. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1300. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1301. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1302. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1303. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1304. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1305. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1306. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1307. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1308. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1309. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1310. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1311. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1312. /* SHA256 */
  1313. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1314. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1315. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1316. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1317. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1318. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1319. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1320. /* SHA384 */
  1321. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1322. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1323. /* AES-GCM */
  1324. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1325. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1326. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1327. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1328. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1329. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1330. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1331. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1332. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1333. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1334. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1335. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1336. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1337. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1338. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1339. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1340. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1341. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1342. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1343. * also, in some of the other AES-CCM suites
  1344. * there will be second byte number conflicts
  1345. * with non-ECC AES-GCM */
  1346. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1347. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1348. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1349. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1350. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1351. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1352. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1353. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1354. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1355. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1356. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1357. /* Camellia */
  1358. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1359. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1360. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1361. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1362. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1363. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1364. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1365. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1366. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1367. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1368. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1369. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1370. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1371. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1372. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1373. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1374. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1375. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1376. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1377. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1378. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1379. /* TLS v1.3 cipher suites */
  1380. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1381. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1382. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1383. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1384. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1385. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1386. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1387. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1388. };
  1389. #ifndef NO_RSA
  1390. testCertFile = svrCertFile;
  1391. testKeyFile = svrKeyFile;
  1392. #elif defined(HAVE_ECC)
  1393. testCertFile = eccCertFile;
  1394. testKeyFile = eccKeyFile;
  1395. #endif
  1396. #ifndef NO_WOLFSSL_SERVER
  1397. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1398. #else
  1399. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1400. #endif
  1401. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1402. sizeof(cipherList)));
  1403. wolfSSL_CTX_free(ctx);
  1404. ctx = NULL;
  1405. #ifndef NO_WOLFSSL_SERVER
  1406. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1407. #else
  1408. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1409. #endif
  1410. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1411. WOLFSSL_FILETYPE_PEM));
  1412. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1413. WOLFSSL_FILETYPE_PEM));
  1414. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1415. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1416. sizeof(cipherList)));
  1417. wolfSSL_free(ssl);
  1418. wolfSSL_CTX_free(ctx);
  1419. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1420. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1421. return EXPECT_RESULT();
  1422. }
  1423. static int test_wolfSSL_CTX_use_certificate_file(void)
  1424. {
  1425. EXPECT_DECLS;
  1426. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1427. WOLFSSL_CTX *ctx = NULL;
  1428. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1429. /* invalid context */
  1430. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1431. WOLFSSL_FILETYPE_PEM));
  1432. /* invalid cert file */
  1433. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1434. WOLFSSL_FILETYPE_PEM));
  1435. /* invalid cert type */
  1436. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1437. #ifdef NO_RSA
  1438. /* rsa needed */
  1439. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1440. WOLFSSL_FILETYPE_PEM));
  1441. #else
  1442. /* success */
  1443. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1444. WOLFSSL_FILETYPE_PEM));
  1445. #endif
  1446. wolfSSL_CTX_free(ctx);
  1447. #endif
  1448. return EXPECT_RESULT();
  1449. }
  1450. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1451. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1452. {
  1453. EXPECT_DECLS;
  1454. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1455. WOLFSSL_CTX* ctx = NULL;
  1456. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1457. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1458. server_cert_der_2048), WOLFSSL_SUCCESS);
  1459. wolfSSL_CTX_free(ctx);
  1460. #endif
  1461. return EXPECT_RESULT();
  1462. }
  1463. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1464. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1465. * context using buffer.
  1466. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1467. * --enable-testcert flag.
  1468. */
  1469. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1470. {
  1471. EXPECT_DECLS;
  1472. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1473. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1474. WOLFSSL_CTX* ctx = NULL;
  1475. int ret;
  1476. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1477. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1478. server_cert_der_2048, sizeof_server_cert_der_2048,
  1479. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1480. wolfSSL_CTX_free(ctx);
  1481. #endif
  1482. return EXPECT_RESULT();
  1483. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1484. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1485. {
  1486. EXPECT_DECLS;
  1487. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1488. WOLFSSL_CTX *ctx = NULL;
  1489. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1490. /* invalid context */
  1491. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1492. WOLFSSL_FILETYPE_PEM));
  1493. /* invalid key file */
  1494. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1495. WOLFSSL_FILETYPE_PEM));
  1496. /* invalid key type */
  1497. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1498. /* success */
  1499. #ifdef NO_RSA
  1500. /* rsa needed */
  1501. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1502. WOLFSSL_FILETYPE_PEM));
  1503. #else
  1504. /* success */
  1505. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1506. WOLFSSL_FILETYPE_PEM));
  1507. #endif
  1508. wolfSSL_CTX_free(ctx);
  1509. #endif
  1510. return EXPECT_RESULT();
  1511. }
  1512. /* test both file and buffer versions along with unloading trusted peer certs */
  1513. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1514. {
  1515. EXPECT_DECLS;
  1516. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1517. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1518. WOLFSSL_CTX *ctx = NULL;
  1519. WOLFSSL* ssl = NULL;
  1520. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1521. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1522. #if !defined(NO_FILESYSTEM)
  1523. /* invalid file */
  1524. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1525. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1526. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1527. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1528. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1529. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1530. /* success */
  1531. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1532. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1533. /* unload cert */
  1534. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1535. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1536. /* invalid file */
  1537. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1538. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1539. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1540. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1541. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1542. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1543. /* success */
  1544. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1545. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1546. #ifdef WOLFSSL_LOCAL_X509_STORE
  1547. /* unload cert */
  1548. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1549. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1550. #endif
  1551. #endif
  1552. /* Test of loading certs from buffers */
  1553. /* invalid buffer */
  1554. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1555. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1556. /* success */
  1557. #ifdef USE_CERT_BUFFERS_1024
  1558. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1559. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1560. #endif
  1561. #ifdef USE_CERT_BUFFERS_2048
  1562. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1563. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1564. #endif
  1565. /* unload cert */
  1566. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1567. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1568. wolfSSL_free(ssl);
  1569. wolfSSL_CTX_free(ctx);
  1570. #endif
  1571. return EXPECT_RESULT();
  1572. }
  1573. static int test_wolfSSL_CTX_load_verify_locations(void)
  1574. {
  1575. EXPECT_DECLS;
  1576. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1577. WOLFSSL_CTX *ctx = NULL;
  1578. #ifndef NO_RSA
  1579. WOLFSSL_CERT_MANAGER* cm = NULL;
  1580. #ifdef PERSIST_CERT_CACHE
  1581. int cacheSz = 0;
  1582. unsigned char* cache = NULL;
  1583. int used = 0;
  1584. #ifndef NO_FILESYSTEM
  1585. const char* cacheFile = "./tests/cert_cache.tmp";
  1586. #endif
  1587. int i;
  1588. int t;
  1589. int* p;
  1590. #endif
  1591. #endif
  1592. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1593. const char* load_certs_path = "./certs/external";
  1594. const char* load_no_certs_path = "./examples";
  1595. const char* load_expired_path = "./certs/test/expired";
  1596. #endif
  1597. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1598. /* invalid arguments */
  1599. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1600. WOLFSSL_FAILURE);
  1601. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1602. WOLFSSL_FAILURE);
  1603. /* invalid ca file */
  1604. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1605. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1606. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1607. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1608. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1609. /* invalid path */
  1610. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1611. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1612. #endif
  1613. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1614. /* test ignoring the invalid path */
  1615. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1616. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1617. #endif
  1618. /* load ca cert */
  1619. #ifdef NO_RSA
  1620. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1621. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1622. #else /* Skip the following test without RSA certs. */
  1623. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1624. WOLFSSL_SUCCESS);
  1625. #ifdef PERSIST_CERT_CACHE
  1626. /* Get cert cache size */
  1627. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1628. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1629. DYNAMIC_TYPE_TMP_BUFFER));
  1630. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1631. BAD_FUNC_ARG);
  1632. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1633. BAD_FUNC_ARG);
  1634. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1635. BAD_FUNC_ARG);
  1636. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1637. BAD_FUNC_ARG);
  1638. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1639. BAD_FUNC_ARG);
  1640. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1641. BAD_FUNC_ARG);
  1642. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1643. BAD_FUNC_ARG);
  1644. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1645. BAD_FUNC_ARG);
  1646. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1647. BAD_FUNC_ARG);
  1648. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1649. BUFFER_E);
  1650. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1651. ExpectIntEQ(cacheSz, used);
  1652. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1653. BAD_FUNC_ARG);
  1654. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1655. BAD_FUNC_ARG);
  1656. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1657. BAD_FUNC_ARG);
  1658. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1659. BAD_FUNC_ARG);
  1660. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1661. BAD_FUNC_ARG);
  1662. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1663. BAD_FUNC_ARG);
  1664. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1665. BAD_FUNC_ARG);
  1666. /* Smaller than header. */
  1667. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1668. for (i = 1; i < cacheSz; i++) {
  1669. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1670. BUFFER_E);
  1671. }
  1672. if (EXPECT_SUCCESS()) {
  1673. /* Modify header for bad results! */
  1674. p = (int*)cache;
  1675. /* version */
  1676. t = p[0]; p[0] = 0xff;
  1677. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1678. CACHE_MATCH_ERROR);
  1679. p[0] = t; p++;
  1680. /* rows */
  1681. t = p[0]; p[0] = 0xff;
  1682. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1683. CACHE_MATCH_ERROR);
  1684. p[0] = t; p++;
  1685. /* columns[0] */
  1686. t = p[0]; p[0] = -1;
  1687. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1688. PARSE_ERROR);
  1689. p[0] = t; p += CA_TABLE_SIZE;
  1690. /* signerSz*/
  1691. t = p[0]; p[0] = 0xff;
  1692. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1693. CACHE_MATCH_ERROR);
  1694. p[0] = t;
  1695. }
  1696. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1697. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1698. #ifndef NO_FILESYSTEM
  1699. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1700. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1701. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1702. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1703. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1704. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1705. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1706. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1707. WOLFSSL_BAD_FILE);
  1708. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1709. /* File contents is not a cache. */
  1710. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1711. CACHE_MATCH_ERROR);
  1712. #endif
  1713. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1714. #endif
  1715. /* Test unloading CA's */
  1716. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1717. #ifdef PERSIST_CERT_CACHE
  1718. /* Verify no certs (result is less than cacheSz) */
  1719. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1720. #endif
  1721. /* load ca cert again */
  1722. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1723. WOLFSSL_SUCCESS);
  1724. /* Test getting CERT_MANAGER */
  1725. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1726. /* Test unloading CA's using CM */
  1727. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1728. #ifdef PERSIST_CERT_CACHE
  1729. /* Verify no certs (result is less than cacheSz) */
  1730. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1731. #endif
  1732. #endif
  1733. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1734. /* Test loading CA certificates using a path */
  1735. #ifdef NO_RSA
  1736. /* failure here okay since certs in external directory are RSA */
  1737. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1738. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1739. #else
  1740. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1741. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1742. #endif
  1743. /* Test loading path with no files */
  1744. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1745. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1746. /* Test loading expired CA certificates */
  1747. #ifdef NO_RSA
  1748. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1749. load_expired_path,
  1750. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1751. WOLFSSL_SUCCESS);
  1752. #else
  1753. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1754. load_expired_path,
  1755. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1756. WOLFSSL_SUCCESS);
  1757. #endif
  1758. /* Test loading CA certificates and ignoring all errors */
  1759. #ifdef NO_RSA
  1760. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1761. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1762. #else
  1763. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1764. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1765. #endif
  1766. #endif
  1767. wolfSSL_CTX_free(ctx);
  1768. #endif
  1769. return EXPECT_RESULT();
  1770. }
  1771. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1772. {
  1773. int res = TEST_SKIPPED;
  1774. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1775. (!defined(NO_RSA) || defined(HAVE_ECC))
  1776. WOLFSSL_CTX* ctx;
  1777. byte dirValid = 0;
  1778. int ret = 0;
  1779. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1780. if (ctx == NULL) {
  1781. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1782. ret = -1;
  1783. }
  1784. if (ret == 0) {
  1785. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1786. dirValid = 1;
  1787. #else
  1788. word32 numDirs;
  1789. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1790. if (caDirs == NULL || numDirs == 0) {
  1791. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1792. ret = -1;
  1793. }
  1794. else {
  1795. ReadDirCtx dirCtx;
  1796. word32 i;
  1797. for (i = 0; i < numDirs; ++i) {
  1798. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1799. /* Directory isn't empty. */
  1800. dirValid = 1;
  1801. wc_ReadDirClose(&dirCtx);
  1802. break;
  1803. }
  1804. }
  1805. }
  1806. #endif
  1807. }
  1808. /*
  1809. * If the directory isn't empty, we should be able to load CA
  1810. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1811. * usable.
  1812. */
  1813. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1814. WOLFSSL_SUCCESS) {
  1815. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1816. ret = -1;
  1817. }
  1818. #ifdef OPENSSL_EXTRA
  1819. if (ret == 0 &&
  1820. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1821. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1822. ret = -1;
  1823. }
  1824. #endif /* OPENSSL_EXTRA */
  1825. wolfSSL_CTX_free(ctx);
  1826. res = TEST_RES_CHECK(ret == 0);
  1827. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1828. return res;
  1829. }
  1830. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1831. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1832. int file_type)
  1833. {
  1834. int ret;
  1835. WOLFSSL_CERT_MANAGER* cm;
  1836. cm = wolfSSL_CertManagerNew();
  1837. if (cm == NULL) {
  1838. fprintf(stderr, "test_cm_load_ca failed\n");
  1839. return -1;
  1840. }
  1841. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1842. wolfSSL_CertManagerFree(cm);
  1843. return ret;
  1844. }
  1845. static int test_cm_load_ca_file(const char* ca_cert_file)
  1846. {
  1847. int ret = 0;
  1848. byte* cert_buf = NULL;
  1849. size_t cert_sz = 0;
  1850. #if defined(WOLFSSL_PEM_TO_DER)
  1851. DerBuffer* pDer = NULL;
  1852. #endif
  1853. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1854. if (ret == 0) {
  1855. /* normal test */
  1856. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1857. if (ret == WOLFSSL_SUCCESS) {
  1858. /* test including null terminator in length */
  1859. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1860. if (tmp == NULL) {
  1861. ret = MEMORY_E;
  1862. }
  1863. else {
  1864. cert_buf = tmp;
  1865. cert_buf[cert_sz] = '\0';
  1866. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1867. WOLFSSL_FILETYPE_PEM);
  1868. }
  1869. }
  1870. #if defined(WOLFSSL_PEM_TO_DER)
  1871. if (ret == WOLFSSL_SUCCESS) {
  1872. /* test loading DER */
  1873. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1874. if (ret == 0 && pDer != NULL) {
  1875. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1876. WOLFSSL_FILETYPE_ASN1);
  1877. wc_FreeDer(&pDer);
  1878. }
  1879. }
  1880. #endif
  1881. }
  1882. free(cert_buf);
  1883. return ret;
  1884. }
  1885. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1886. int file_type, word32 flags)
  1887. {
  1888. int ret;
  1889. WOLFSSL_CERT_MANAGER* cm;
  1890. cm = wolfSSL_CertManagerNew();
  1891. if (cm == NULL) {
  1892. fprintf(stderr, "test_cm_load_ca failed\n");
  1893. return -1;
  1894. }
  1895. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1896. 0, flags);
  1897. wolfSSL_CertManagerFree(cm);
  1898. return ret;
  1899. }
  1900. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1901. {
  1902. int ret = 0;
  1903. byte* cert_buf = NULL;
  1904. size_t cert_sz = 0;
  1905. #if defined(WOLFSSL_PEM_TO_DER)
  1906. DerBuffer* pDer = NULL;
  1907. #endif
  1908. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1909. if (ret == 0) {
  1910. /* normal test */
  1911. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1912. WOLFSSL_FILETYPE_PEM, flags);
  1913. if (ret == WOLFSSL_SUCCESS) {
  1914. /* test including null terminator in length */
  1915. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1916. if (tmp == NULL) {
  1917. ret = MEMORY_E;
  1918. }
  1919. else {
  1920. cert_buf = tmp;
  1921. cert_buf[cert_sz] = '\0';
  1922. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1923. WOLFSSL_FILETYPE_PEM, flags);
  1924. }
  1925. }
  1926. #if defined(WOLFSSL_PEM_TO_DER)
  1927. if (ret == WOLFSSL_SUCCESS) {
  1928. /* test loading DER */
  1929. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1930. if (ret == 0 && pDer != NULL) {
  1931. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1932. WOLFSSL_FILETYPE_ASN1, flags);
  1933. wc_FreeDer(&pDer);
  1934. }
  1935. }
  1936. #endif
  1937. }
  1938. free(cert_buf);
  1939. return ret;
  1940. }
  1941. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1942. static int test_wolfSSL_CertManagerAPI(void)
  1943. {
  1944. EXPECT_DECLS;
  1945. #ifndef NO_CERTS
  1946. WOLFSSL_CERT_MANAGER* cm = NULL;
  1947. unsigned char c;
  1948. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1949. wolfSSL_CertManagerFree(NULL);
  1950. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1951. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1952. #ifdef WOLFSSL_TRUST_PEER_CERT
  1953. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1954. #endif
  1955. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1956. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1957. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1958. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1959. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1960. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1961. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1962. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1963. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1964. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1965. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1966. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1967. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1968. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1969. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1970. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1971. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1972. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1973. WOLFSSL_BAD_FILETYPE);
  1974. #endif
  1975. #if !defined(NO_FILESYSTEM)
  1976. {
  1977. const char* ca_cert = "./certs/ca-cert.pem";
  1978. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1979. const char* ca_cert_der = "./certs/ca-cert.der";
  1980. #endif
  1981. const char* ca_path = "./certs";
  1982. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1983. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1984. BAD_FUNC_ARG);
  1985. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1986. BAD_FUNC_ARG);
  1987. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1988. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1989. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1990. WOLFSSL_BAD_FILETYPE);
  1991. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1992. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1993. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  1994. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  1995. #endif
  1996. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  1997. WOLFSSL_FATAL_ERROR);
  1998. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  1999. WOLFSSL_FATAL_ERROR);
  2000. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2001. WOLFSSL_FATAL_ERROR);
  2002. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2003. WOLFSSL_FATAL_ERROR);
  2004. }
  2005. #endif
  2006. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2007. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2008. #elif !defined(HAVE_CRL)
  2009. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2010. #endif
  2011. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2012. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2013. #ifdef HAVE_CRL
  2014. /* Test APIs when CRL is disabled. */
  2015. #ifdef HAVE_CRL_IO
  2016. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2017. #endif
  2018. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2019. sizeof_server_cert_der_2048), 1);
  2020. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2021. #endif
  2022. /* OCSP */
  2023. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2024. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2025. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2026. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2027. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2028. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2029. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2030. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2031. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2032. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2033. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2034. #endif
  2035. #ifdef HAVE_OCSP
  2036. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2037. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2038. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2039. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2040. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2041. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2042. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2043. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2044. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2045. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2046. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2047. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2048. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2049. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2050. BAD_FUNC_ARG);
  2051. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2052. BAD_FUNC_ARG);
  2053. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2054. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2055. BAD_FUNC_ARG);
  2056. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2057. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2058. /* Test APIs when OCSP is disabled. */
  2059. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2060. NULL, NULL, NULL, NULL), 1);
  2061. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2062. #endif
  2063. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2064. wolfSSL_CertManagerFree(cm);
  2065. wolfSSL_CertManagerFree(cm);
  2066. cm = NULL;
  2067. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2068. #ifdef HAVE_OCSP
  2069. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2070. WOLFSSL_OCSP_CHECKALL), 1);
  2071. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2072. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2073. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2074. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2075. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2076. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2077. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2078. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2079. #endif
  2080. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2081. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2082. #endif
  2083. #ifdef WOLFSSL_TRUST_PEER_CERT
  2084. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2085. #endif
  2086. wolfSSL_CertManagerFree(cm);
  2087. #endif
  2088. return EXPECT_RESULT();
  2089. }
  2090. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2091. {
  2092. EXPECT_DECLS;
  2093. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2094. const char* ca_cert = "./certs/ca-cert.pem";
  2095. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2096. int ret;
  2097. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2098. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2099. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2100. #elif defined(NO_RSA)
  2101. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2102. #else
  2103. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2104. #endif
  2105. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2106. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2107. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2108. #elif defined(NO_RSA)
  2109. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2110. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2111. !defined(NO_ASN_TIME)
  2112. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2113. #else
  2114. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2115. #endif
  2116. #endif
  2117. return EXPECT_RESULT();
  2118. }
  2119. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2120. {
  2121. EXPECT_DECLS;
  2122. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2123. const char* ca_cert = "./certs/ca-cert.pem";
  2124. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2125. int ret;
  2126. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2127. 1);
  2128. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2129. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2130. #elif defined(NO_RSA)
  2131. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2132. #else
  2133. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2134. #endif
  2135. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2136. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2137. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2138. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2139. #elif defined(NO_RSA)
  2140. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2141. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2142. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2143. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2144. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2145. #else
  2146. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2147. #endif
  2148. #endif
  2149. return EXPECT_RESULT();
  2150. }
  2151. static int test_wolfSSL_CertManagerGetCerts(void)
  2152. {
  2153. EXPECT_DECLS;
  2154. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2155. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2156. defined(WOLFSSL_SIGNER_DER_CERT)
  2157. WOLFSSL_CERT_MANAGER* cm = NULL;
  2158. WOLFSSL_STACK* sk = NULL;
  2159. X509* x509 = NULL;
  2160. X509* cert1 = NULL;
  2161. FILE* file1 = NULL;
  2162. #ifdef DEBUG_WOLFSSL_VERBOSE
  2163. WOLFSSL_BIO* bio = NULL;
  2164. #endif
  2165. int i = 0;
  2166. int ret = 0;
  2167. const byte* der = NULL;
  2168. int derSz = 0;
  2169. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2170. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2171. if (file1 != NULL) {
  2172. fclose(file1);
  2173. }
  2174. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2175. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2176. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2177. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2178. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2179. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2180. * and full OpenSSL compatibility */
  2181. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2182. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2183. #else
  2184. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2185. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2186. #endif
  2187. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2188. "./certs/ca-cert.pem", NULL));
  2189. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2190. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2191. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2192. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2193. #ifdef DEBUG_WOLFSSL_VERBOSE
  2194. bio = BIO_new(wolfSSL_BIO_s_file());
  2195. if (bio != NULL) {
  2196. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2197. X509_print(bio, x509);
  2198. BIO_free(bio);
  2199. }
  2200. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2201. }
  2202. wolfSSL_X509_free(cert1);
  2203. sk_X509_pop_free(sk, NULL);
  2204. wolfSSL_CertManagerFree(cm);
  2205. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2206. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2207. defined(WOLFSSL_SIGNER_DER_CERT) */
  2208. return EXPECT_RESULT();
  2209. }
  2210. static int test_wolfSSL_CertManagerSetVerify(void)
  2211. {
  2212. EXPECT_DECLS;
  2213. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2214. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2215. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2216. WOLFSSL_CERT_MANAGER* cm = NULL;
  2217. int tmp = myVerifyAction;
  2218. const char* ca_cert = "./certs/ca-cert.pem";
  2219. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2220. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2221. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2222. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2223. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2224. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2225. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2226. #else
  2227. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2228. WOLFSSL_SUCCESS);
  2229. #endif
  2230. /* Use the test CB that always accepts certs */
  2231. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2232. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2233. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2234. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2235. {
  2236. const char* verifyCert = "./certs/server-cert.der";
  2237. /* Use the test CB that always fails certs */
  2238. myVerifyAction = VERIFY_FORCE_FAIL;
  2239. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2240. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2241. }
  2242. #endif
  2243. wolfSSL_CertManagerFree(cm);
  2244. myVerifyAction = tmp;
  2245. #endif
  2246. return EXPECT_RESULT();
  2247. }
  2248. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2249. defined(DEBUG_UNIT_TEST_CERTS)
  2250. /* Used when debugging name constraint tests. Not static to allow use in
  2251. * multiple locations with complex define guards. */
  2252. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2253. {
  2254. BIO* out = BIO_new_file(fileName, "wb");
  2255. if (out != NULL) {
  2256. PEM_write_bio_X509(out, x509);
  2257. BIO_free(out);
  2258. }
  2259. }
  2260. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2261. {
  2262. BIO* out = BIO_new_file(fileName, "wb");
  2263. if (out != NULL) {
  2264. BIO_write(out, der, derSz);
  2265. BIO_free(out);
  2266. }
  2267. }
  2268. #else
  2269. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2270. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2271. #endif
  2272. static int test_wolfSSL_CertManagerNameConstraint(void)
  2273. {
  2274. EXPECT_DECLS;
  2275. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2276. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2277. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2278. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2279. !defined(NO_SHA256)
  2280. WOLFSSL_CERT_MANAGER* cm = NULL;
  2281. WOLFSSL_EVP_PKEY *priv = NULL;
  2282. WOLFSSL_X509_NAME* name = NULL;
  2283. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2284. const char* server_cert = "./certs/test/server-goodcn.pem";
  2285. int i = 0;
  2286. static const byte extNameConsOid[] = {85, 29, 30};
  2287. RsaKey key;
  2288. WC_RNG rng;
  2289. byte *der = NULL;
  2290. int derSz = 0;
  2291. word32 idx = 0;
  2292. byte *pt;
  2293. WOLFSSL_X509 *x509 = NULL;
  2294. WOLFSSL_X509 *ca = NULL;
  2295. wc_InitRng(&rng);
  2296. /* load in CA private key for signing */
  2297. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2298. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2299. sizeof_server_key_der_2048), 0);
  2300. /* get ca certificate then alter it */
  2301. ExpectNotNull(der =
  2302. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2303. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2304. WOLFSSL_FILETYPE_ASN1));
  2305. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2306. if (EXPECT_SUCCESS() && (der != NULL)) {
  2307. XMEMCPY(der, pt, derSz);
  2308. /* find the name constraint extension and alter it */
  2309. pt = der;
  2310. for (i = 0; i < derSz - 3; i++) {
  2311. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2312. pt += 3;
  2313. break;
  2314. }
  2315. pt++;
  2316. }
  2317. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2318. /* go to the length value and set it to 0 */
  2319. while (i < derSz && *pt != 0x81) {
  2320. pt++;
  2321. i++;
  2322. }
  2323. ExpectIntNE(i, derSz); /* did not place to alter */
  2324. pt++;
  2325. *pt = 0x00;
  2326. }
  2327. /* resign the altered certificate */
  2328. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2329. FOURK_BUF, &key, NULL, &rng)), 0);
  2330. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2331. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2332. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2333. wolfSSL_CertManagerFree(cm);
  2334. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2335. wolfSSL_X509_free(x509);
  2336. wc_FreeRsaKey(&key);
  2337. wc_FreeRng(&rng);
  2338. /* add email alt name to satisfy constraint */
  2339. pt = (byte*)server_key_der_2048;
  2340. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2341. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2342. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2343. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2344. WOLFSSL_FILETYPE_ASN1));
  2345. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2346. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2347. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2348. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2349. /* Good cert test with proper alt email name */
  2350. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2351. WOLFSSL_FILETYPE_PEM));
  2352. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2353. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2354. name = NULL;
  2355. ExpectNotNull(name = X509_NAME_new());
  2356. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2357. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2358. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2359. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2360. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2361. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2362. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2363. X509_NAME_free(name);
  2364. name = NULL;
  2365. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2366. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2367. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2368. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2369. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2370. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2371. wolfSSL_X509_free(x509);
  2372. x509 = NULL;
  2373. /* Cert with bad alt name list */
  2374. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2375. WOLFSSL_FILETYPE_PEM));
  2376. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2377. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2378. name = NULL;
  2379. ExpectNotNull(name = X509_NAME_new());
  2380. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2381. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2382. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2383. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2384. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2385. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2386. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2387. X509_NAME_free(name);
  2388. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2389. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2390. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2391. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2392. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2393. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2394. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2395. wolfSSL_CertManagerFree(cm);
  2396. wolfSSL_X509_free(x509);
  2397. wolfSSL_X509_free(ca);
  2398. wolfSSL_EVP_PKEY_free(priv);
  2399. #endif
  2400. return EXPECT_RESULT();
  2401. }
  2402. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2403. {
  2404. EXPECT_DECLS;
  2405. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2406. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2407. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2408. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2409. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2410. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2411. const char* server_cert = "./certs/server-cert.pem";
  2412. WOLFSSL_CERT_MANAGER* cm = NULL;
  2413. WOLFSSL_X509 *x509 = NULL;
  2414. WOLFSSL_X509 *ca = NULL;
  2415. const unsigned char *der = NULL;
  2416. const unsigned char *pt;
  2417. WOLFSSL_EVP_PKEY *priv = NULL;
  2418. WOLFSSL_X509_NAME* name = NULL;
  2419. int derSz = 0;
  2420. /* C=US*/
  2421. char altName[] = {
  2422. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2423. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2424. };
  2425. /* C=ID */
  2426. char altNameFail[] = {
  2427. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2428. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2429. };
  2430. /* C=US ST=California*/
  2431. char altNameExc[] = {
  2432. 0x30, 0x22,
  2433. 0x31, 0x0B,
  2434. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2435. 0x31, 0x13,
  2436. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2437. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2438. };
  2439. /* load in CA private key for signing */
  2440. pt = ca_key_der_2048;
  2441. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2442. sizeof_ca_key_der_2048));
  2443. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2444. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2445. WOLFSSL_FILETYPE_ASN1));
  2446. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2447. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2448. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2449. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2450. WOLFSSL_FILETYPE_PEM));
  2451. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2452. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2453. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2454. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2455. #else
  2456. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2457. #endif
  2458. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2459. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2460. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2461. /* add in matching DIR alt name and resign */
  2462. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2463. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2464. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2465. #else
  2466. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2467. #endif
  2468. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2469. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2470. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2471. wolfSSL_X509_free(x509);
  2472. x509 = NULL;
  2473. /* check verify fail */
  2474. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2475. WOLFSSL_FILETYPE_PEM));
  2476. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2477. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2478. /* add in miss matching DIR alt name and resign */
  2479. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2480. ASN_DIR_TYPE);
  2481. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2482. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2483. #else
  2484. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2485. #endif
  2486. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2487. #ifndef WOLFSSL_NO_ASN_STRICT
  2488. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2489. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2490. #else
  2491. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2492. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2493. #endif
  2494. /* check that it still fails if one bad altname and one good altname is in
  2495. * the certificate */
  2496. wolfSSL_X509_free(x509);
  2497. x509 = NULL;
  2498. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2499. WOLFSSL_FILETYPE_PEM));
  2500. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2501. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2502. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2503. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2504. ASN_DIR_TYPE);
  2505. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2506. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2507. #else
  2508. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2509. #endif
  2510. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2511. #ifndef WOLFSSL_NO_ASN_STRICT
  2512. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2513. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2514. #else
  2515. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2516. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2517. #endif
  2518. /* check it fails with switching position of bad altname */
  2519. wolfSSL_X509_free(x509);
  2520. x509 = NULL;
  2521. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2522. WOLFSSL_FILETYPE_PEM));
  2523. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2524. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2525. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2526. ASN_DIR_TYPE);
  2527. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2528. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2529. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2530. #else
  2531. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2532. #endif
  2533. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2534. #ifndef WOLFSSL_NO_ASN_STRICT
  2535. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2536. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2537. #else
  2538. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2539. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2540. #endif
  2541. wolfSSL_CertManagerFree(cm);
  2542. wolfSSL_X509_free(x509);
  2543. x509 = NULL;
  2544. wolfSSL_X509_free(ca);
  2545. ca = NULL;
  2546. /* now test with excluded name constraint */
  2547. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2548. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2549. WOLFSSL_FILETYPE_ASN1));
  2550. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2551. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2552. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2553. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2554. WOLFSSL_FILETYPE_PEM));
  2555. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2556. ASN_DIR_TYPE);
  2557. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2558. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2559. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2560. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2561. #else
  2562. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2563. #endif
  2564. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2565. #ifndef WOLFSSL_NO_ASN_STRICT
  2566. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2567. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2568. #else
  2569. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2570. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2571. #endif
  2572. wolfSSL_CertManagerFree(cm);
  2573. wolfSSL_X509_free(x509);
  2574. wolfSSL_X509_free(ca);
  2575. wolfSSL_EVP_PKEY_free(priv);
  2576. #endif
  2577. return EXPECT_RESULT();
  2578. }
  2579. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2580. {
  2581. EXPECT_DECLS;
  2582. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2583. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2584. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2585. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2586. !defined(NO_SHA256)
  2587. WOLFSSL_CERT_MANAGER* cm = NULL;
  2588. WOLFSSL_EVP_PKEY *priv = NULL;
  2589. WOLFSSL_X509_NAME* name = NULL;
  2590. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2591. const char* server_cert = "./certs/test/server-goodcn.pem";
  2592. byte *der = NULL;
  2593. int derSz = 0;
  2594. byte *pt;
  2595. WOLFSSL_X509 *x509 = NULL;
  2596. WOLFSSL_X509 *ca = NULL;
  2597. pt = (byte*)server_key_der_2048;
  2598. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2599. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2600. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2601. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2602. WOLFSSL_FILETYPE_ASN1));
  2603. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2604. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2605. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2606. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2607. /* check satisfying .wolfssl.com constraint passes */
  2608. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2609. WOLFSSL_FILETYPE_PEM));
  2610. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2611. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2612. name = NULL;
  2613. ExpectNotNull(name = X509_NAME_new());
  2614. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2615. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2616. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2617. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2618. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2619. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2620. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2621. X509_NAME_free(name);
  2622. name = NULL;
  2623. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2624. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2625. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2626. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2627. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2628. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2629. wolfSSL_X509_free(x509);
  2630. x509 = NULL;
  2631. /* check satisfying .random.com constraint passes */
  2632. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2633. WOLFSSL_FILETYPE_PEM));
  2634. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2635. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2636. name = NULL;
  2637. ExpectNotNull(name = X509_NAME_new());
  2638. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2639. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2640. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2641. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2642. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2643. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2644. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2645. X509_NAME_free(name);
  2646. name = NULL;
  2647. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2648. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2649. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2650. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2651. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2652. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2653. wolfSSL_X509_free(x509);
  2654. x509 = NULL;
  2655. /* check fail case when neither constraint is matched */
  2656. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2657. WOLFSSL_FILETYPE_PEM));
  2658. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2659. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2660. name = NULL;
  2661. ExpectNotNull(name = X509_NAME_new());
  2662. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2663. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2664. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2665. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2666. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2667. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2668. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2669. X509_NAME_free(name);
  2670. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2671. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2672. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2673. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2674. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2675. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2676. wolfSSL_CertManagerFree(cm);
  2677. wolfSSL_X509_free(x509);
  2678. wolfSSL_X509_free(ca);
  2679. wolfSSL_EVP_PKEY_free(priv);
  2680. #endif
  2681. return EXPECT_RESULT();
  2682. }
  2683. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2684. {
  2685. EXPECT_DECLS;
  2686. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2687. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2688. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2689. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2690. !defined(NO_SHA256)
  2691. WOLFSSL_CERT_MANAGER* cm = NULL;
  2692. WOLFSSL_EVP_PKEY *priv = NULL;
  2693. WOLFSSL_X509_NAME* name = NULL;
  2694. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2695. const char* server_cert = "./certs/test/server-goodcn.pem";
  2696. byte *der = NULL;
  2697. int derSz;
  2698. byte *pt;
  2699. WOLFSSL_X509 *x509 = NULL;
  2700. WOLFSSL_X509 *ca = NULL;
  2701. pt = (byte*)server_key_der_2048;
  2702. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2703. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2704. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2705. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2706. WOLFSSL_FILETYPE_ASN1));
  2707. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2708. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2709. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2710. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2711. /* check satisfying wolfssl.com constraint passes */
  2712. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2713. WOLFSSL_FILETYPE_PEM));
  2714. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2715. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2716. name = NULL;
  2717. ExpectNotNull(name = X509_NAME_new());
  2718. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2719. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2720. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2721. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2722. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2723. X509_NAME_free(name);
  2724. name = NULL;
  2725. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2726. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2727. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2728. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2729. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2730. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2731. wolfSSL_X509_free(x509);
  2732. x509 = NULL;
  2733. /* check satisfying example.com constraint passes */
  2734. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2735. WOLFSSL_FILETYPE_PEM));
  2736. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2737. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2738. name = NULL;
  2739. ExpectNotNull(name = X509_NAME_new());
  2740. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2741. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2742. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2743. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2744. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2745. X509_NAME_free(name);
  2746. name = NULL;
  2747. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2748. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2749. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2750. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2751. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2752. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2753. wolfSSL_X509_free(x509);
  2754. x509 = NULL;
  2755. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2756. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2757. WOLFSSL_FILETYPE_PEM));
  2758. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2759. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2760. name = NULL;
  2761. ExpectNotNull(name = X509_NAME_new());
  2762. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2763. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2764. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2765. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2766. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2767. X509_NAME_free(name);
  2768. name = NULL;
  2769. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2770. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2771. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2772. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2773. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2774. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2775. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2776. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2777. wolfSSL_X509_free(x509);
  2778. x509 = NULL;
  2779. /* check fail when one DNS in the list is bad */
  2780. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2781. WOLFSSL_FILETYPE_PEM));
  2782. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2783. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2784. name = NULL;
  2785. ExpectNotNull(name = X509_NAME_new());
  2786. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2787. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2788. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2789. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2790. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2791. X509_NAME_free(name);
  2792. name = NULL;
  2793. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2794. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2795. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2796. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2797. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2798. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2799. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2800. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2801. wolfSSL_X509_free(x509);
  2802. x509 = NULL;
  2803. /* check fail case when neither constraint is matched */
  2804. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2805. WOLFSSL_FILETYPE_PEM));
  2806. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2807. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2808. name = NULL;
  2809. ExpectNotNull(name = X509_NAME_new());
  2810. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2811. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2812. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2813. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2814. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2815. X509_NAME_free(name);
  2816. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2817. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2818. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2819. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2820. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2821. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2822. wolfSSL_CertManagerFree(cm);
  2823. wolfSSL_X509_free(x509);
  2824. wolfSSL_X509_free(ca);
  2825. wolfSSL_EVP_PKEY_free(priv);
  2826. #endif
  2827. return EXPECT_RESULT();
  2828. }
  2829. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2830. {
  2831. EXPECT_DECLS;
  2832. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2833. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2834. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2835. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2836. !defined(NO_SHA256)
  2837. WOLFSSL_CERT_MANAGER* cm = NULL;
  2838. WOLFSSL_EVP_PKEY *priv = NULL;
  2839. WOLFSSL_X509_NAME* name = NULL;
  2840. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2841. const char* server_cert = "./certs/test/server-goodcn.pem";
  2842. byte *der = NULL;
  2843. int derSz;
  2844. byte *pt;
  2845. WOLFSSL_X509 *x509 = NULL;
  2846. WOLFSSL_X509 *ca = NULL;
  2847. pt = (byte*)server_key_der_2048;
  2848. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2849. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2850. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2851. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2852. WOLFSSL_FILETYPE_ASN1));
  2853. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2854. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2855. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2856. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2857. /* check satisfying wolfssl.com constraint passes */
  2858. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2859. WOLFSSL_FILETYPE_PEM));
  2860. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2861. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2862. name = NULL;
  2863. ExpectNotNull(name = X509_NAME_new());
  2864. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2865. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2866. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2867. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2868. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2869. X509_NAME_free(name);
  2870. name = NULL;
  2871. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2872. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2873. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2874. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2875. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2876. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2877. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2878. wolfSSL_X509_free(x509);
  2879. x509 = NULL;
  2880. /* fail with DNS check because of common name */
  2881. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2882. WOLFSSL_FILETYPE_PEM));
  2883. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2884. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2885. name = NULL;
  2886. ExpectNotNull(name = X509_NAME_new());
  2887. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2888. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2889. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2890. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2891. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2892. X509_NAME_free(name);
  2893. name = NULL;
  2894. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2895. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2896. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2897. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2898. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2899. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2900. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2901. wolfSSL_X509_free(x509);
  2902. x509 = NULL;
  2903. /* fail on permitted DNS name constraint */
  2904. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2905. WOLFSSL_FILETYPE_PEM));
  2906. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2907. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2908. name = NULL;
  2909. ExpectNotNull(name = X509_NAME_new());
  2910. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2911. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2912. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2913. X509_NAME_free(name);
  2914. name = NULL;
  2915. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2916. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2917. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2918. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2919. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2920. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2921. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2922. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2923. wolfSSL_X509_free(x509);
  2924. x509 = NULL;
  2925. /* fail on permitted email name constraint */
  2926. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2927. WOLFSSL_FILETYPE_PEM));
  2928. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2929. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2930. name = NULL;
  2931. ExpectNotNull(name = X509_NAME_new());
  2932. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2933. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2934. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2935. X509_NAME_free(name);
  2936. name = NULL;
  2937. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2938. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2939. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2940. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2941. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2942. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2943. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2944. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2945. wolfSSL_X509_free(x509);
  2946. x509 = NULL;
  2947. /* success with empty email name */
  2948. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2949. WOLFSSL_FILETYPE_PEM));
  2950. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2951. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2952. name = NULL;
  2953. ExpectNotNull(name = X509_NAME_new());
  2954. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2955. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2956. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2957. X509_NAME_free(name);
  2958. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2959. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2960. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2961. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2962. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2963. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2964. wolfSSL_X509_free(x509);
  2965. wolfSSL_CertManagerFree(cm);
  2966. wolfSSL_X509_free(ca);
  2967. wolfSSL_EVP_PKEY_free(priv);
  2968. #endif
  2969. return EXPECT_RESULT();
  2970. }
  2971. static int test_wolfSSL_CertManagerCRL(void)
  2972. {
  2973. EXPECT_DECLS;
  2974. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2975. !defined(NO_RSA)
  2976. const char* ca_cert = "./certs/ca-cert.pem";
  2977. const char* crl1 = "./certs/crl/crl.pem";
  2978. const char* crl2 = "./certs/crl/crl2.pem";
  2979. #ifdef WC_RSA_PSS
  2980. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  2981. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  2982. #endif
  2983. const unsigned char crl_buff[] = {
  2984. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2985. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2986. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2987. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2988. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2989. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2990. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2991. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  2992. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  2993. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  2994. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  2995. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  2996. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  2997. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  2998. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  2999. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3000. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3001. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3002. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3003. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3004. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3005. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3006. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3007. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3008. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3009. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3010. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3011. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3012. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3013. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3014. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3015. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3016. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3017. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3018. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3019. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3020. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3021. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3022. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3023. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3024. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3025. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3026. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3027. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3028. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3029. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3030. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3031. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3032. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3033. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3034. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3035. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3036. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3037. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3038. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3039. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3040. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3041. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3042. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3043. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3044. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3045. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3046. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3047. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3048. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3049. };
  3050. WOLFSSL_CERT_MANAGER* cm = NULL;
  3051. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3052. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3053. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3054. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3055. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3056. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3057. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3058. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3059. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3060. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3061. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3062. BAD_FUNC_ARG);
  3063. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3064. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3065. BAD_FUNC_ARG);
  3066. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3067. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3068. BAD_FUNC_ARG);
  3069. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3070. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3071. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3072. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3073. #ifdef HAVE_CRL_IO
  3074. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3075. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3076. #endif
  3077. #ifndef NO_FILESYSTEM
  3078. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3079. 0), BAD_FUNC_ARG);
  3080. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3081. 0), BAD_FUNC_ARG);
  3082. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3083. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3084. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3085. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3086. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3087. BAD_FUNC_ARG);
  3088. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3089. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3090. ASN_PARSE_E);
  3091. #endif
  3092. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3093. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3094. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3095. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3096. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3097. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3098. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3099. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3100. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3101. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3102. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3103. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3104. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3105. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3106. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3107. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3108. ExpectIntEQ(WOLFSSL_SUCCESS,
  3109. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3110. ExpectIntEQ(WOLFSSL_SUCCESS,
  3111. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3112. ExpectIntEQ(WOLFSSL_SUCCESS,
  3113. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3114. wolfSSL_CertManagerFreeCRL(cm);
  3115. ExpectIntEQ(WOLFSSL_SUCCESS,
  3116. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3117. ExpectIntEQ(WOLFSSL_SUCCESS,
  3118. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3119. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3120. sizeof_server_cert_der_2048), CRL_MISSING);
  3121. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3122. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3123. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3124. WOLFSSL_FILETYPE_ASN1), 1);
  3125. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3126. /* loading should fail without the CA set */
  3127. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3128. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3129. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3130. ExpectIntEQ(WOLFSSL_SUCCESS,
  3131. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3132. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3133. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3134. #endif
  3135. wolfSSL_CertManagerFree(cm);
  3136. #endif
  3137. return EXPECT_RESULT();
  3138. }
  3139. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3140. {
  3141. EXPECT_DECLS;
  3142. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3143. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3144. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3145. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3146. defined(HAVE_LIGHTY)
  3147. WOLFSSL_CERT_MANAGER* cm = NULL;
  3148. /* Raw OCSP response bytes captured using the following setup:
  3149. * - Run responder with
  3150. * openssl ocsp -port 9999 -ndays 9999
  3151. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3152. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3153. * -rkey certs/ocsp/ocsp-responder-key.pem
  3154. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3155. * - Run client with
  3156. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3157. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3158. * -cert certs/ocsp/server1-cert.pem
  3159. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3160. * - Select the response packet in Wireshark, and export it using
  3161. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3162. * packets only". After importing into the editor, remove the initial
  3163. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3164. */
  3165. static const byte response[] = {
  3166. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3167. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3168. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3169. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3170. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3171. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3172. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3173. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3174. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3175. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3176. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3177. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3178. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3179. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3180. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3181. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3182. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3183. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3184. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3185. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3186. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3187. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3188. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3189. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3190. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3191. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3192. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3193. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3194. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3195. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3196. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3197. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3198. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3199. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3200. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3201. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3202. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3203. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3204. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3205. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3206. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3207. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3208. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3209. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3210. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3211. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3212. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3213. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3214. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3215. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3216. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3217. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3218. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3219. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3220. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3221. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3222. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3223. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3224. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3225. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3226. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3227. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3228. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3229. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3230. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3231. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3232. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3233. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3234. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3235. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3236. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3237. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3238. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3239. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3240. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3241. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3242. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3243. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3244. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3245. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3246. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3247. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3248. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3249. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3250. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3251. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3252. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3253. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3254. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3255. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3256. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3257. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3258. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3259. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3260. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3261. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3262. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3263. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3264. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3265. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3266. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3267. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3268. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3269. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3270. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3271. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3272. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3273. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3274. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3275. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3276. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3277. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3278. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3279. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3280. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3281. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3282. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3283. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3284. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3285. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3286. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3287. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3288. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3289. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3290. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3291. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3292. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3293. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3294. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3295. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3296. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3297. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3298. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3299. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3300. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3301. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3302. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3303. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3304. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3305. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3306. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3307. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3308. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3309. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3310. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3311. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3312. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3313. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3314. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3315. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3316. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3317. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3318. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3319. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3320. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3321. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3322. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3323. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3324. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3325. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3326. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3327. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3328. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3329. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3330. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3331. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3332. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3333. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3334. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3335. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3336. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3337. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3338. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3339. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3340. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3341. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3342. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3343. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3344. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3345. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3346. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3347. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3348. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3349. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3350. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3351. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3352. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3353. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3354. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3355. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3356. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3357. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3358. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3359. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3360. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3361. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3362. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3363. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3364. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3365. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3366. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3367. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3368. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3369. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3370. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3371. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3372. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3373. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3374. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3375. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3376. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3377. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3378. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3379. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3380. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3381. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3382. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3383. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3384. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3385. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3386. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3387. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3388. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3389. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3390. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3391. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3392. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3393. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3394. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3395. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3396. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3397. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3398. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3399. };
  3400. OcspEntry entry[1];
  3401. CertStatus status[1];
  3402. OcspRequest* request = NULL;
  3403. #ifndef NO_FILESYSTEM
  3404. const char* ca_cert = "./certs/ca-cert.pem";
  3405. #endif
  3406. byte serial[] = {0x05};
  3407. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3408. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3409. XMEMSET(entry, 0, sizeof(OcspEntry));
  3410. XMEMSET(status, 0, sizeof(CertStatus));
  3411. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3412. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3413. DYNAMIC_TYPE_OCSP_REQUEST));
  3414. if ((request != NULL) && (request->serial != NULL)) {
  3415. request->serialSz = sizeof(serial);
  3416. XMEMCPY(request->serial, serial, sizeof(serial));
  3417. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3418. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3419. }
  3420. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3421. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3422. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3423. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3424. /* Response should be valid. */
  3425. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3426. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3427. /* Flip a byte in the request serial number, response should be invalid
  3428. * now. */
  3429. if ((request != NULL) && (request->serial != NULL))
  3430. request->serial[0] ^= request->serial[0];
  3431. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3432. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3433. #ifndef NO_FILESYSTEM
  3434. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3435. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3436. ExpectIntEQ(WOLFSSL_SUCCESS,
  3437. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3438. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3439. sizeof(server_cert_der_2048)), 1);
  3440. #endif
  3441. wolfSSL_OCSP_REQUEST_free(request);
  3442. wolfSSL_CertManagerFree(cm);
  3443. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3444. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3445. #endif /* HAVE_OCSP */
  3446. return EXPECT_RESULT();
  3447. }
  3448. static int test_wolfSSL_CheckOCSPResponse(void)
  3449. {
  3450. EXPECT_DECLS;
  3451. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3452. defined(OPENSSL_ALL)
  3453. const char* responseFile = "./certs/ocsp/test-response.der";
  3454. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3455. const char* responseNoInternFile =
  3456. "./certs/ocsp/test-response-nointern.der";
  3457. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3458. OcspResponse* res = NULL;
  3459. byte data[4096];
  3460. const unsigned char* pt;
  3461. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3462. * gcc sanitizer with --enable-heapmath.
  3463. */
  3464. XFILE f = XBADFILE;
  3465. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3466. WOLFSSL_X509_STORE* st = NULL;
  3467. WOLFSSL_X509* issuer = NULL;
  3468. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3469. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3470. if (f != XBADFILE) {
  3471. XFCLOSE(f);
  3472. f = XBADFILE;
  3473. }
  3474. pt = data;
  3475. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3476. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3477. SSL_FILETYPE_PEM));
  3478. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3479. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3480. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3481. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3482. wolfSSL_OCSP_BASICRESP_free(bs);
  3483. bs = NULL;
  3484. wolfSSL_OCSP_RESPONSE_free(res);
  3485. res = NULL;
  3486. wolfSSL_X509_STORE_free(st);
  3487. st = NULL;
  3488. wolfSSL_X509_free(issuer);
  3489. issuer = NULL;
  3490. /* check loading a response with optional certs */
  3491. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3492. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3493. if (f != XBADFILE)
  3494. XFCLOSE(f);
  3495. f = XBADFILE;
  3496. pt = data;
  3497. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3498. wolfSSL_OCSP_RESPONSE_free(res);
  3499. res = NULL;
  3500. /* check loading a response with multiple certs */
  3501. {
  3502. WOLFSSL_CERT_MANAGER* cm = NULL;
  3503. OcspEntry *entry = NULL;
  3504. CertStatus* status = NULL;
  3505. OcspRequest* request = NULL;
  3506. byte serial1[] = {0x01};
  3507. byte serial[] = {0x02};
  3508. byte issuerHash[] = {
  3509. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3510. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3511. 0x37, 0x15, 0x8A, 0x88
  3512. };
  3513. byte issuerKeyHash[] = {
  3514. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3515. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3516. 0x7E, 0x72, 0x15, 0x21
  3517. };
  3518. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3519. DYNAMIC_TYPE_OPENSSL));
  3520. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3521. DYNAMIC_TYPE_OPENSSL));
  3522. if (entry != NULL)
  3523. XMEMSET(entry, 0, sizeof(OcspEntry));
  3524. if (status != NULL)
  3525. XMEMSET(status, 0, sizeof(CertStatus));
  3526. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3527. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3528. DYNAMIC_TYPE_OCSP_REQUEST));
  3529. if (request != NULL && request->serial != NULL) {
  3530. request->serialSz = sizeof(serial);
  3531. XMEMCPY(request->serial, serial, sizeof(serial));
  3532. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3533. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3534. sizeof(issuerKeyHash));
  3535. }
  3536. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3537. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3538. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3539. WOLFSSL_SUCCESS);
  3540. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3541. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3542. if (f != XBADFILE)
  3543. XFCLOSE(f);
  3544. f = XBADFILE;
  3545. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3546. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3547. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3548. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3549. ExpectNotNull(entry->status);
  3550. if (request != NULL && request->serial != NULL)
  3551. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3552. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3553. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3554. /* store both status's in the entry to check that "next" is not
  3555. * overwritten */
  3556. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3557. status->next = entry->status;
  3558. entry->status = status;
  3559. }
  3560. if (request != NULL && request->serial != NULL)
  3561. XMEMCPY(request->serial, serial, sizeof(serial));
  3562. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3563. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3564. ExpectNotNull(entry->status->next);
  3565. /* compare the status found */
  3566. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3567. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3568. status->serialSz), 0);
  3569. if (status != NULL && entry != NULL && entry->status != status) {
  3570. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3571. }
  3572. wolfSSL_OCSP_CERTID_free(entry);
  3573. wolfSSL_OCSP_REQUEST_free(request);
  3574. wolfSSL_CertManagerFree(cm);
  3575. }
  3576. #if defined(WC_RSA_PSS)
  3577. {
  3578. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3579. /* check loading a response with RSA-PSS signature */
  3580. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3581. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3582. if (f != XBADFILE)
  3583. XFCLOSE(f);
  3584. pt = data;
  3585. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3586. /* try to verify the response */
  3587. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3588. SSL_FILETYPE_PEM));
  3589. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3590. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3591. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3592. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3593. WOLFSSL_SUCCESS);
  3594. wolfSSL_OCSP_BASICRESP_free(bs);
  3595. wolfSSL_OCSP_RESPONSE_free(res);
  3596. wolfSSL_X509_STORE_free(st);
  3597. wolfSSL_X509_free(issuer);
  3598. }
  3599. #endif
  3600. #endif /* HAVE_OCSP */
  3601. return EXPECT_RESULT();
  3602. }
  3603. static int test_wolfSSL_FPKI(void)
  3604. {
  3605. EXPECT_DECLS;
  3606. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3607. XFILE f = XBADFILE;
  3608. const char* fpkiCert = "./certs/fpki-cert.der";
  3609. DecodedCert cert;
  3610. byte buf[4096];
  3611. byte* uuid = NULL;
  3612. byte* fascn = NULL;
  3613. word32 fascnSz;
  3614. word32 uuidSz;
  3615. int bytes = 0;
  3616. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3617. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3618. if (f != XBADFILE)
  3619. XFCLOSE(f);
  3620. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3621. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3622. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3623. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3624. DYNAMIC_TYPE_TMP_BUFFER));
  3625. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3626. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3627. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3628. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3629. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3630. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3631. wc_FreeDecodedCert(&cert);
  3632. #endif
  3633. return EXPECT_RESULT();
  3634. }
  3635. /* use RID in confuncture with other names to test parsing of unknown other
  3636. * names */
  3637. static int test_wolfSSL_OtherName(void)
  3638. {
  3639. EXPECT_DECLS;
  3640. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3641. XFILE f = XBADFILE;
  3642. const char* ridCert = "./certs/rid-cert.der";
  3643. DecodedCert cert;
  3644. byte buf[4096];
  3645. int bytes = 0;
  3646. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3647. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3648. if (f != XBADFILE)
  3649. XFCLOSE(f);
  3650. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3651. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3652. wc_FreeDecodedCert(&cert);
  3653. #endif
  3654. return EXPECT_RESULT();
  3655. }
  3656. static int test_wolfSSL_CertRsaPss(void)
  3657. {
  3658. EXPECT_DECLS;
  3659. /* FIPS v2 and below don't support long salts. */
  3660. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3661. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3662. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3663. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3664. XFILE f = XBADFILE;
  3665. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3666. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3667. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3668. RSA_MAX_SIZE >= 3072
  3669. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3670. #endif
  3671. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3672. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3673. #endif
  3674. DecodedCert cert;
  3675. byte buf[4096];
  3676. int bytes = 0;
  3677. WOLFSSL_CERT_MANAGER* cm = NULL;
  3678. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3679. ExpectIntEQ(WOLFSSL_SUCCESS,
  3680. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3681. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3682. ExpectIntEQ(WOLFSSL_SUCCESS,
  3683. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3684. #endif
  3685. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3686. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3687. if (f != XBADFILE) {
  3688. XFCLOSE(f);
  3689. f = XBADFILE;
  3690. }
  3691. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3692. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3693. wc_FreeDecodedCert(&cert);
  3694. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3695. RSA_MAX_SIZE >= 3072
  3696. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3697. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3698. if (f != XBADFILE)
  3699. XFCLOSE(f);
  3700. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3701. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3702. wc_FreeDecodedCert(&cert);
  3703. #endif
  3704. wolfSSL_CertManagerFree(cm);
  3705. #endif
  3706. return EXPECT_RESULT();
  3707. }
  3708. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3709. {
  3710. EXPECT_DECLS;
  3711. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3712. !defined(NO_WOLFSSL_CLIENT)
  3713. WOLFSSL_CTX* ctx = NULL;
  3714. const char* ca_cert = "./certs/ca-cert.pem";
  3715. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3716. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3717. /* test good CA */
  3718. ExpectTrue(WOLFSSL_SUCCESS ==
  3719. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3720. WOLFSSL_LOAD_FLAG_NONE));
  3721. /* test expired CA */
  3722. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3723. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3724. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3725. #else
  3726. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3727. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3728. #endif
  3729. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3730. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3731. wolfSSL_CTX_free(ctx);
  3732. #endif
  3733. return EXPECT_RESULT();
  3734. }
  3735. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3736. {
  3737. EXPECT_DECLS;
  3738. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3739. defined(USE_CERT_BUFFERS_2048)
  3740. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3741. WOLFSSL_CTX* ctx;
  3742. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3743. byte ca_expired_cert[TWOK_BUF];
  3744. word32 sizeof_ca_expired_cert = 0;
  3745. XFILE fp = XBADFILE;
  3746. #ifndef NO_WOLFSSL_CLIENT
  3747. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3748. #else
  3749. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3750. #endif
  3751. ExpectNotNull(ctx);
  3752. /* test good CA */
  3753. ExpectTrue(WOLFSSL_SUCCESS ==
  3754. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3755. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3756. WOLFSSL_LOAD_FLAG_NONE));
  3757. /* load expired CA */
  3758. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3759. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3760. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3761. sizeof(ca_expired_cert), fp), 0);
  3762. if (fp != XBADFILE)
  3763. XFCLOSE(fp);
  3764. /* test expired CA failure */
  3765. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3766. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3767. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3768. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3769. #else
  3770. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3771. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3772. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3773. #endif
  3774. /* test expired CA success */
  3775. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3776. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3777. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3778. wolfSSL_CTX_free(ctx);
  3779. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3780. #endif
  3781. return EXPECT_RESULT();
  3782. }
  3783. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3784. {
  3785. EXPECT_DECLS;
  3786. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3787. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3788. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3789. WOLFSSL_CTX* ctx = NULL;
  3790. #ifndef NO_WOLFSSL_CLIENT
  3791. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3792. #else
  3793. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3794. #endif
  3795. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3796. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3797. WOLFSSL_FILETYPE_ASN1));
  3798. wolfSSL_CTX_free(ctx);
  3799. #endif
  3800. return EXPECT_RESULT();
  3801. }
  3802. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3803. {
  3804. EXPECT_DECLS;
  3805. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3806. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3807. WOLFSSL_CTX* ctx;
  3808. WOLFSSL* ssl = NULL;
  3809. const char *certChain[] = {
  3810. "./certs/intermediate/client-int-cert.pem",
  3811. "./certs/intermediate/ca-int2-cert.pem",
  3812. "./certs/intermediate/ca-int-cert.pem",
  3813. "./certs/ca-cert.pem",
  3814. NULL
  3815. };
  3816. const char** cert;
  3817. WOLFSSL_X509* x509 = NULL;
  3818. WOLF_STACK_OF(X509)* chain = NULL;
  3819. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3820. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3821. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3822. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3823. WOLFSSL_FILETYPE_PEM));
  3824. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3825. X509_free(x509);
  3826. x509 = NULL;
  3827. }
  3828. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3829. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3830. WOLFSSL_FILETYPE_PEM));
  3831. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3832. X509_free(x509);
  3833. x509 = NULL;
  3834. }
  3835. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3836. ExpectIntEQ(sk_X509_num(chain), 3);
  3837. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3838. ExpectIntEQ(sk_X509_num(chain), 3);
  3839. SSL_free(ssl);
  3840. SSL_CTX_free(ctx);
  3841. #endif
  3842. return EXPECT_RESULT();
  3843. }
  3844. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3845. {
  3846. EXPECT_DECLS;
  3847. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3848. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3849. const char* server_chain_der = "./certs/server-cert-chain.der";
  3850. const char* client_single_pem = "./certs/client-cert.pem";
  3851. WOLFSSL_CTX* ctx;
  3852. (void)server_chain_der;
  3853. (void)client_single_pem;
  3854. (void)ctx;
  3855. #ifndef NO_WOLFSSL_CLIENT
  3856. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3857. #else
  3858. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3859. #endif
  3860. ExpectNotNull(ctx);
  3861. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3862. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3863. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3864. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3865. wolfSSL_CTX_free(ctx);
  3866. #endif
  3867. return EXPECT_RESULT();
  3868. }
  3869. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3870. {
  3871. EXPECT_DECLS;
  3872. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3873. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3874. WOLFSSL_CTX *ctx = NULL;
  3875. (void)ctx;
  3876. #ifndef NO_WOLFSSL_CLIENT
  3877. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3878. #else
  3879. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3880. #endif
  3881. /* invalid context */
  3882. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3883. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3884. /* invalid dhParamFile file */
  3885. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3886. NULL, WOLFSSL_FILETYPE_PEM));
  3887. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3888. bogusFile, WOLFSSL_FILETYPE_PEM));
  3889. /* success */
  3890. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3891. WOLFSSL_FILETYPE_PEM));
  3892. wolfSSL_CTX_free(ctx);
  3893. #endif
  3894. return EXPECT_RESULT();
  3895. }
  3896. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3897. {
  3898. EXPECT_DECLS;
  3899. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3900. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3901. WOLFSSL_CTX *ctx = NULL;
  3902. #ifndef NO_WOLFSSL_CLIENT
  3903. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3904. #else
  3905. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3906. #endif
  3907. /* invalid context */
  3908. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3909. dh_key_der_2048, sizeof_dh_key_der_2048,
  3910. WOLFSSL_FILETYPE_ASN1));
  3911. /* invalid dhParamFile file */
  3912. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3913. 0, WOLFSSL_FILETYPE_ASN1));
  3914. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3915. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3916. WOLFSSL_FILETYPE_ASN1));
  3917. /* success */
  3918. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3919. dh_key_der_2048, sizeof_dh_key_der_2048,
  3920. WOLFSSL_FILETYPE_ASN1));
  3921. wolfSSL_CTX_free(ctx);
  3922. #endif
  3923. return EXPECT_RESULT();
  3924. }
  3925. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3926. {
  3927. EXPECT_DECLS;
  3928. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3929. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3930. WOLFSSL_CTX *ctx;
  3931. (void)ctx;
  3932. #ifndef NO_WOLFSSL_CLIENT
  3933. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3934. #else
  3935. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3936. #endif
  3937. ExpectNotNull(ctx);
  3938. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3939. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3940. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3941. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3942. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3943. dh_key_der_2048, sizeof_dh_key_der_2048,
  3944. WOLFSSL_FILETYPE_ASN1));
  3945. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3946. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3947. dh_key_der_2048, sizeof_dh_key_der_2048,
  3948. WOLFSSL_FILETYPE_ASN1));
  3949. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3950. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3951. dh_key_der_2048, sizeof_dh_key_der_2048,
  3952. WOLFSSL_FILETYPE_ASN1));
  3953. wolfSSL_CTX_free(ctx);
  3954. #endif
  3955. return EXPECT_RESULT();
  3956. }
  3957. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3958. {
  3959. EXPECT_DECLS;
  3960. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3961. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3962. WOLFSSL_CTX* ctx = NULL;
  3963. const char* derCert = "./certs/server-cert.der";
  3964. const char* nullPath = NULL;
  3965. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3966. const char* emptyPath = "";
  3967. /* der load Case 1 ctx NULL */
  3968. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3969. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3970. #ifndef NO_WOLFSSL_CLIENT
  3971. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3972. #else
  3973. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3974. #endif
  3975. /* Case 2 filePath NULL */
  3976. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3977. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3978. /* Case 3 invalid format */
  3979. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3980. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3981. /* Case 4 filePath not valid */
  3982. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3983. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3984. /* Case 5 filePath empty */
  3985. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3986. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3987. #ifndef NO_RSA
  3988. /* Case 6 success case */
  3989. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3990. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3991. #endif
  3992. wolfSSL_CTX_free(ctx);
  3993. #endif
  3994. return EXPECT_RESULT();
  3995. }
  3996. static int test_wolfSSL_CTX_enable_disable(void)
  3997. {
  3998. EXPECT_DECLS;
  3999. #ifndef NO_CERTS
  4000. WOLFSSL_CTX* ctx = NULL;
  4001. #ifdef HAVE_CRL
  4002. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4003. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4004. #endif
  4005. #ifdef HAVE_OCSP
  4006. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4007. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4008. #endif
  4009. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4010. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4011. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4012. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4013. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4014. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4015. #endif
  4016. #ifndef NO_WOLFSSL_CLIENT
  4017. #ifdef HAVE_EXTENDED_MASTER
  4018. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4019. #endif
  4020. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4021. #ifdef HAVE_EXTENDED_MASTER
  4022. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4023. #endif
  4024. #elif !defined(NO_WOLFSSL_SERVER)
  4025. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4026. #endif
  4027. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4028. #ifdef HAVE_CRL
  4029. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4030. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4031. #endif
  4032. #ifdef HAVE_OCSP
  4033. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4034. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4035. WOLFSSL_SUCCESS);
  4036. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4037. WOLFSSL_SUCCESS);
  4038. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4039. WOLFSSL_SUCCESS);
  4040. #endif
  4041. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4042. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4043. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4044. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4045. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4046. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4047. #endif
  4048. wolfSSL_CTX_free(ctx);
  4049. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4050. #endif /* NO_CERTS */
  4051. return EXPECT_RESULT();
  4052. }
  4053. static int test_wolfSSL_CTX_ticket_API(void)
  4054. {
  4055. EXPECT_DECLS;
  4056. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4057. WOLFSSL_CTX* ctx = NULL;
  4058. void *userCtx = (void*)"this is my ctx";
  4059. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4060. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4061. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4062. wolfSSL_CTX_free(ctx);
  4063. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4064. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4065. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4066. return EXPECT_RESULT();
  4067. }
  4068. static int test_wolfSSL_set_minmax_proto_version(void)
  4069. {
  4070. EXPECT_DECLS;
  4071. #ifdef OPENSSL_EXTRA
  4072. WOLFSSL_CTX *ctx = NULL;
  4073. WOLFSSL *ssl = NULL;
  4074. (void)ssl;
  4075. #ifndef NO_WOLFSSL_CLIENT
  4076. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4077. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4078. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4079. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4080. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4081. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4082. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4083. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4084. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4085. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4086. wolfSSL_free(ssl);
  4087. wolfSSL_CTX_free(ctx);
  4088. ctx = NULL;
  4089. #endif
  4090. #ifndef NO_WOLFSSL_SERVER
  4091. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4092. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4093. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4094. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4095. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4096. wolfSSL_CTX_free(ctx);
  4097. #endif
  4098. #endif
  4099. return EXPECT_RESULT();
  4100. }
  4101. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4102. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4103. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4104. {
  4105. EXPECT_DECLS;
  4106. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4107. return EXPECT_RESULT();
  4108. }
  4109. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4110. {
  4111. EXPECT_DECLS;
  4112. /* Set TLS 1.2 */
  4113. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4114. WOLFSSL_SUCCESS);
  4115. return EXPECT_RESULT();
  4116. }
  4117. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4118. * what was set at ctx creation. */
  4119. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4120. {
  4121. EXPECT_DECLS;
  4122. test_ssl_cbf client_cbs;
  4123. test_ssl_cbf server_cbs;
  4124. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4125. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4126. client_cbs.method = wolfTLS_client_method;
  4127. server_cbs.method = wolfTLS_server_method;
  4128. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4129. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4130. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4131. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4132. &server_cbs, NULL), TEST_SUCCESS);
  4133. return EXPECT_RESULT();
  4134. }
  4135. #else
  4136. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4137. {
  4138. return TEST_SKIPPED;
  4139. }
  4140. #endif
  4141. /*----------------------------------------------------------------------------*
  4142. | SSL
  4143. *----------------------------------------------------------------------------*/
  4144. static int test_server_wolfSSL_new(void)
  4145. {
  4146. EXPECT_DECLS;
  4147. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4148. !defined(NO_WOLFSSL_SERVER)
  4149. WOLFSSL_CTX *ctx = NULL;
  4150. WOLFSSL_CTX *ctx_nocert = NULL;
  4151. WOLFSSL *ssl = NULL;
  4152. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4153. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4154. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4155. WOLFSSL_FILETYPE_PEM));
  4156. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4157. WOLFSSL_FILETYPE_PEM));
  4158. /* invalid context */
  4159. ExpectNull(ssl = wolfSSL_new(NULL));
  4160. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4161. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4162. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4163. #endif
  4164. /* success */
  4165. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4166. wolfSSL_free(ssl);
  4167. wolfSSL_CTX_free(ctx);
  4168. wolfSSL_CTX_free(ctx_nocert);
  4169. #endif
  4170. return EXPECT_RESULT();
  4171. }
  4172. static int test_client_wolfSSL_new(void)
  4173. {
  4174. EXPECT_DECLS;
  4175. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4176. !defined(NO_WOLFSSL_CLIENT)
  4177. WOLFSSL_CTX *ctx = NULL;
  4178. WOLFSSL_CTX *ctx_nocert = NULL;
  4179. WOLFSSL *ssl = NULL;
  4180. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4181. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4182. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4183. /* invalid context */
  4184. ExpectNull(ssl = wolfSSL_new(NULL));
  4185. /* success */
  4186. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4187. wolfSSL_free(ssl);
  4188. ssl = NULL;
  4189. /* success */
  4190. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4191. wolfSSL_free(ssl);
  4192. wolfSSL_CTX_free(ctx);
  4193. wolfSSL_CTX_free(ctx_nocert);
  4194. #endif
  4195. return EXPECT_RESULT();
  4196. }
  4197. static int test_wolfSSL_SetTmpDH_file(void)
  4198. {
  4199. EXPECT_DECLS;
  4200. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4201. !defined(NO_WOLFSSL_SERVER)
  4202. WOLFSSL_CTX *ctx = NULL;
  4203. WOLFSSL *ssl = NULL;
  4204. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4205. #ifndef NO_RSA
  4206. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4207. WOLFSSL_FILETYPE_PEM));
  4208. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4209. WOLFSSL_FILETYPE_PEM));
  4210. #elif defined(HAVE_ECC)
  4211. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4212. WOLFSSL_FILETYPE_PEM));
  4213. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4214. WOLFSSL_FILETYPE_PEM));
  4215. #elif defined(HAVE_ED25519)
  4216. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4217. WOLFSSL_FILETYPE_PEM));
  4218. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4219. WOLFSSL_FILETYPE_PEM));
  4220. #elif defined(HAVE_ED448)
  4221. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4222. WOLFSSL_FILETYPE_PEM));
  4223. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4224. WOLFSSL_FILETYPE_PEM));
  4225. #endif
  4226. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4227. /* invalid ssl */
  4228. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4229. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4230. /* invalid dhParamFile file */
  4231. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4232. NULL, WOLFSSL_FILETYPE_PEM));
  4233. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4234. bogusFile, WOLFSSL_FILETYPE_PEM));
  4235. /* success */
  4236. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4237. WOLFSSL_FILETYPE_PEM));
  4238. wolfSSL_free(ssl);
  4239. wolfSSL_CTX_free(ctx);
  4240. #endif
  4241. return EXPECT_RESULT();
  4242. }
  4243. static int test_wolfSSL_SetTmpDH_buffer(void)
  4244. {
  4245. EXPECT_DECLS;
  4246. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4247. WOLFSSL_CTX *ctx = NULL;
  4248. WOLFSSL *ssl = NULL;
  4249. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4250. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4251. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4252. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4253. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4254. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4255. /* invalid ssl */
  4256. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4257. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4258. /* invalid dhParamFile file */
  4259. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4260. 0, WOLFSSL_FILETYPE_ASN1));
  4261. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4262. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4263. /* success */
  4264. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4265. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4266. wolfSSL_free(ssl);
  4267. wolfSSL_CTX_free(ctx);
  4268. #endif
  4269. return EXPECT_RESULT();
  4270. }
  4271. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4272. {
  4273. EXPECT_DECLS;
  4274. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4275. WOLFSSL_CTX *ctx = NULL;
  4276. WOLFSSL_CTX *ctx2 = NULL;
  4277. WOLFSSL *ssl = NULL;
  4278. WOLFSSL *ssl2 = NULL;
  4279. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4280. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4281. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4282. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4283. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4284. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4285. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4286. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4287. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4288. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4289. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4290. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4291. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4292. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4293. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4294. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4295. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4296. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4297. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4298. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4299. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4300. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4301. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4302. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4303. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4304. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4305. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4306. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4307. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4308. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4309. wolfSSL_free(ssl2);
  4310. wolfSSL_CTX_free(ctx2);
  4311. wolfSSL_free(ssl);
  4312. wolfSSL_CTX_free(ctx);
  4313. #endif
  4314. return EXPECT_RESULT();
  4315. }
  4316. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4317. * allowed.
  4318. * POST: return 1 on success.
  4319. */
  4320. static int test_wolfSSL_SetMinVersion(void)
  4321. {
  4322. int res = TEST_SKIPPED;
  4323. #ifndef NO_WOLFSSL_CLIENT
  4324. int failFlag = WOLFSSL_SUCCESS;
  4325. WOLFSSL_CTX* ctx = NULL;
  4326. WOLFSSL* ssl = NULL;
  4327. int itr;
  4328. #ifndef NO_OLD_TLS
  4329. const int versions[] = {
  4330. #ifdef WOLFSSL_ALLOW_TLSV10
  4331. WOLFSSL_TLSV1,
  4332. #endif
  4333. WOLFSSL_TLSV1_1,
  4334. WOLFSSL_TLSV1_2};
  4335. #elif !defined(WOLFSSL_NO_TLS12)
  4336. const int versions[] = { WOLFSSL_TLSV1_2 };
  4337. #else
  4338. const int versions[] = { WOLFSSL_TLSV1_3 };
  4339. #endif
  4340. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4341. ssl = wolfSSL_new(ctx);
  4342. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4343. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4344. failFlag = WOLFSSL_FAILURE;
  4345. }
  4346. }
  4347. wolfSSL_free(ssl);
  4348. wolfSSL_CTX_free(ctx);
  4349. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4350. #endif
  4351. return res;
  4352. } /* END test_wolfSSL_SetMinVersion */
  4353. #ifdef OPENSSL_EXTRA
  4354. static int test_ED25519(void)
  4355. {
  4356. EXPECT_DECLS;
  4357. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4358. defined(WOLFSSL_KEY_GEN)
  4359. byte priv[ED25519_PRV_KEY_SIZE];
  4360. unsigned int privSz = (unsigned int)sizeof(priv);
  4361. byte pub[ED25519_PUB_KEY_SIZE];
  4362. unsigned int pubSz = (unsigned int)sizeof(pub);
  4363. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4364. const char* msg = TEST_STRING;
  4365. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4366. byte sig[ED25519_SIG_SIZE];
  4367. unsigned int sigSz = (unsigned int)sizeof(sig);
  4368. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4369. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4370. WOLFSSL_SUCCESS);
  4371. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4372. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4373. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4374. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4375. &sigSz), WOLFSSL_SUCCESS);
  4376. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4377. #ifdef HAVE_ED25519_VERIFY
  4378. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4379. sigSz), WOLFSSL_SUCCESS);
  4380. #endif /* HAVE_ED25519_VERIFY */
  4381. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4382. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4383. return EXPECT_RESULT();
  4384. }
  4385. static int test_ED448(void)
  4386. {
  4387. EXPECT_DECLS;
  4388. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4389. defined(WOLFSSL_KEY_GEN)
  4390. byte priv[ED448_PRV_KEY_SIZE];
  4391. unsigned int privSz = (unsigned int)sizeof(priv);
  4392. byte pub[ED448_PUB_KEY_SIZE];
  4393. unsigned int pubSz = (unsigned int)sizeof(pub);
  4394. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4395. const char* msg = TEST_STRING;
  4396. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4397. byte sig[ED448_SIG_SIZE];
  4398. unsigned int sigSz = (unsigned int)sizeof(sig);
  4399. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4400. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  4401. WOLFSSL_SUCCESS);
  4402. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4403. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4404. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4405. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4406. &sigSz), WOLFSSL_SUCCESS);
  4407. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4408. #ifdef HAVE_ED448_VERIFY
  4409. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4410. sigSz), WOLFSSL_SUCCESS);
  4411. #endif /* HAVE_ED448_VERIFY */
  4412. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4413. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4414. return EXPECT_RESULT();
  4415. }
  4416. #endif /* OPENSSL_EXTRA */
  4417. #include <wolfssl/openssl/pem.h>
  4418. /*----------------------------------------------------------------------------*
  4419. | EVP
  4420. *----------------------------------------------------------------------------*/
  4421. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4422. {
  4423. EXPECT_DECLS;
  4424. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4425. WOLFSSL_BIO* rbio = NULL;
  4426. WOLFSSL_BIO* wbio = NULL;
  4427. WOLFSSL_EVP_PKEY* pkey = NULL;
  4428. char line[256] = { 0 };
  4429. char line1[256] = { 0 };
  4430. int i = 0;
  4431. /* test error cases */
  4432. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4433. /*
  4434. * test RSA public key print
  4435. * in this test, pass '3' for indent
  4436. */
  4437. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4438. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4439. sizeof_client_keypub_der_1024));
  4440. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4441. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4442. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4443. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4444. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4445. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4446. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4447. strcpy(line1, " Modulus:\n");
  4448. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4449. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4450. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4451. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4452. /* skip to the end of modulus element*/
  4453. for (i = 0; i < 8 ;i++) {
  4454. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4455. }
  4456. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4457. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4458. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4459. /* should reach EOF */
  4460. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4461. EVP_PKEY_free(pkey);
  4462. pkey = NULL;
  4463. BIO_free(rbio);
  4464. BIO_free(wbio);
  4465. rbio = NULL;
  4466. wbio = NULL;
  4467. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4468. /*
  4469. * test DSA public key print
  4470. */
  4471. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4472. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4473. sizeof_dsa_pub_key_der_2048));
  4474. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4475. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4476. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4477. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4478. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4479. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4480. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4481. strcpy(line1, "pub:\n");
  4482. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4483. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4484. strcpy(line1,
  4485. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4486. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4487. /* skip to the end of pub element*/
  4488. for (i = 0; i < 17 ;i++) {
  4489. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4490. }
  4491. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4492. strcpy(line1, "P:\n");
  4493. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4494. /* skip to the end of P element*/
  4495. for (i = 0; i < 18 ;i++) {
  4496. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4497. }
  4498. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4499. strcpy(line1, "Q:\n");
  4500. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4501. /* skip to the end of Q element*/
  4502. for (i = 0; i < 3 ;i++) {
  4503. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4504. }
  4505. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4506. strcpy(line1, "G:\n");
  4507. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4508. /* skip to the end of G element*/
  4509. for (i = 0; i < 18 ;i++) {
  4510. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4511. }
  4512. /* should reach EOF */
  4513. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4514. EVP_PKEY_free(pkey);
  4515. pkey = NULL;
  4516. BIO_free(rbio);
  4517. BIO_free(wbio);
  4518. rbio = NULL;
  4519. wbio = NULL;
  4520. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4521. /*
  4522. * test ECC public key print
  4523. */
  4524. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4525. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4526. sizeof_ecc_clikeypub_der_256));
  4527. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4528. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4529. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4530. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4531. strcpy(line1, "Public-Key: (256 bit)\n");
  4532. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4533. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4534. strcpy(line1, "pub:\n");
  4535. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4536. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4537. strcpy(line1,
  4538. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4539. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4540. /* skip to the end of pub element*/
  4541. for (i = 0; i < 4 ;i++) {
  4542. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4543. }
  4544. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4545. strcpy(line1, "ASN1 OID: prime256v1\n");
  4546. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4547. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4548. strcpy(line1, "NIST CURVE: P-256\n");
  4549. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4550. /* should reach EOF */
  4551. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4552. EVP_PKEY_free(pkey);
  4553. pkey = NULL;
  4554. BIO_free(rbio);
  4555. BIO_free(wbio);
  4556. rbio = NULL;
  4557. wbio = NULL;
  4558. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4559. /*
  4560. * test DH public key print
  4561. */
  4562. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4563. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4564. sizeof_dh_pub_key_der_2048));
  4565. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4566. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4567. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4568. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4569. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4570. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4571. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4572. strcpy(line1, "public-key:\n");
  4573. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4574. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4575. strcpy(line1,
  4576. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4577. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4578. /* skip to the end of public-key element*/
  4579. for (i = 0; i < 17 ;i++) {
  4580. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4581. }
  4582. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4583. strcpy(line1, "prime:\n");
  4584. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4585. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4586. strcpy(line1,
  4587. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4588. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4589. /* skip to the end of prime element*/
  4590. for (i = 0; i < 17 ;i++) {
  4591. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4592. }
  4593. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4594. strcpy(line1, "generator: 2 (0x02)\n");
  4595. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4596. /* should reach EOF */
  4597. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4598. EVP_PKEY_free(pkey);
  4599. pkey = NULL;
  4600. BIO_free(rbio);
  4601. BIO_free(wbio);
  4602. rbio = NULL;
  4603. wbio = NULL;
  4604. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4605. /* to prevent "unused variable" warning */
  4606. (void)pkey;
  4607. (void)wbio;
  4608. (void)rbio;
  4609. (void)line;
  4610. (void)line1;
  4611. (void)i;
  4612. #endif /* OPENSSL_EXTRA */
  4613. return EXPECT_RESULT();
  4614. }
  4615. /* Test functions for base64 encode/decode */
  4616. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4617. {
  4618. EXPECT_DECLS;
  4619. #if defined(OPENSSL_EXTRA) && \
  4620. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4621. EVP_ENCODE_CTX* ctx = NULL;
  4622. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4623. ExpectIntEQ(ctx->remaining,0);
  4624. ExpectIntEQ(ctx->data[0],0);
  4625. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4626. EVP_ENCODE_CTX_free(ctx);
  4627. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4628. return EXPECT_RESULT();
  4629. }
  4630. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4631. {
  4632. EXPECT_DECLS;
  4633. #if defined(OPENSSL_EXTRA) && \
  4634. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4635. EVP_ENCODE_CTX* ctx = NULL;
  4636. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4637. EVP_ENCODE_CTX_free(ctx);
  4638. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4639. return EXPECT_RESULT();
  4640. }
  4641. static int test_wolfSSL_EVP_EncodeInit(void)
  4642. {
  4643. EXPECT_DECLS;
  4644. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4645. EVP_ENCODE_CTX* ctx = NULL;
  4646. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4647. ExpectIntEQ(ctx->remaining, 0);
  4648. ExpectIntEQ(ctx->data[0], 0);
  4649. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4650. if (ctx != NULL) {
  4651. /* make ctx dirty */
  4652. ctx->remaining = 10;
  4653. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4654. }
  4655. EVP_EncodeInit(ctx);
  4656. ExpectIntEQ(ctx->remaining, 0);
  4657. ExpectIntEQ(ctx->data[0], 0);
  4658. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4659. EVP_ENCODE_CTX_free(ctx);
  4660. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4661. return EXPECT_RESULT();
  4662. }
  4663. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4664. {
  4665. EXPECT_DECLS;
  4666. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4667. int outl;
  4668. int total;
  4669. const unsigned char plain0[] = {"Th"};
  4670. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4671. const unsigned char plain2[] = {"This is additional data."};
  4672. const unsigned char encBlock0[] = {"VGg="};
  4673. const unsigned char enc0[] = {"VGg=\n"};
  4674. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4675. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4676. const unsigned char enc2[] =
  4677. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4678. unsigned char encOutBuff[300];
  4679. EVP_ENCODE_CTX* ctx = NULL;
  4680. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4681. EVP_EncodeInit(ctx);
  4682. /* illegal parameter test */
  4683. ExpectIntEQ(
  4684. EVP_EncodeUpdate(
  4685. NULL, /* pass NULL as ctx */
  4686. encOutBuff,
  4687. &outl,
  4688. plain1,
  4689. sizeof(plain1)-1),
  4690. 0 /* expected result code 0: fail */
  4691. );
  4692. ExpectIntEQ(
  4693. EVP_EncodeUpdate(
  4694. ctx,
  4695. NULL, /* pass NULL as out buff */
  4696. &outl,
  4697. plain1,
  4698. sizeof(plain1)-1),
  4699. 0 /* expected result code 0: fail */
  4700. );
  4701. ExpectIntEQ(
  4702. EVP_EncodeUpdate(
  4703. ctx,
  4704. encOutBuff,
  4705. NULL, /* pass NULL as outl */
  4706. plain1,
  4707. sizeof(plain1)-1),
  4708. 0 /* expected result code 0: fail */
  4709. );
  4710. ExpectIntEQ(
  4711. EVP_EncodeUpdate(
  4712. ctx,
  4713. encOutBuff,
  4714. &outl,
  4715. NULL, /* pass NULL as in */
  4716. sizeof(plain1)-1),
  4717. 0 /* expected result code 0: fail */
  4718. );
  4719. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  4720. /* meaningless parameter test */
  4721. ExpectIntEQ(
  4722. EVP_EncodeUpdate(
  4723. ctx,
  4724. encOutBuff,
  4725. &outl,
  4726. plain1,
  4727. 0), /* pass zero input */
  4728. 1 /* expected result code 1: success */
  4729. );
  4730. /* very small data encoding test */
  4731. EVP_EncodeInit(ctx);
  4732. ExpectIntEQ(
  4733. EVP_EncodeUpdate(
  4734. ctx,
  4735. encOutBuff,
  4736. &outl,
  4737. plain0,
  4738. sizeof(plain0)-1),
  4739. 1 /* expected result code 1: success */
  4740. );
  4741. ExpectIntEQ(outl,0);
  4742. if (EXPECT_SUCCESS()) {
  4743. EVP_EncodeFinal(
  4744. ctx,
  4745. encOutBuff + outl,
  4746. &outl);
  4747. }
  4748. ExpectIntEQ( outl, sizeof(enc0)-1);
  4749. ExpectIntEQ(
  4750. XSTRNCMP(
  4751. (const char*)encOutBuff,
  4752. (const char*)enc0,sizeof(enc0) ),
  4753. 0);
  4754. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4755. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  4756. sizeof(encBlock0)-1);
  4757. ExpectStrEQ(encOutBuff, encBlock0);
  4758. /* pass small size( < 48bytes ) input, then make sure they are not
  4759. * encoded and just stored in ctx
  4760. */
  4761. EVP_EncodeInit(ctx);
  4762. total = 0;
  4763. outl = 0;
  4764. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4765. ExpectIntEQ(
  4766. EVP_EncodeUpdate(
  4767. ctx,
  4768. encOutBuff, /* buffer for output */
  4769. &outl, /* size of output */
  4770. plain1, /* input */
  4771. sizeof(plain1)-1), /* size of input */
  4772. 1); /* expected result code 1:success */
  4773. total += outl;
  4774. ExpectIntEQ(outl, 0); /* no output expected */
  4775. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  4776. ExpectTrue(
  4777. XSTRNCMP((const char*)(ctx->data),
  4778. (const char*)plain1,
  4779. ctx->remaining) ==0 );
  4780. ExpectTrue(encOutBuff[0] == 0);
  4781. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  4782. * the stored data and the new input together
  4783. */
  4784. ExpectIntEQ(
  4785. EVP_EncodeUpdate(
  4786. ctx,
  4787. encOutBuff + outl, /* buffer for output */
  4788. &outl, /* size of output */
  4789. plain2, /* additional input */
  4790. sizeof(plain2) -1), /* size of additional input */
  4791. 1); /* expected result code 1:success */
  4792. total += outl;
  4793. ExpectIntNE(outl, 0); /* some output is expected this time*/
  4794. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  4795. ExpectIntEQ(
  4796. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  4797. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  4798. EVP_EncodeFinal(
  4799. ctx,
  4800. encOutBuff + outl,
  4801. &outl);
  4802. total += outl;
  4803. ExpectIntNE(total,0);
  4804. ExpectIntNE(outl,0);
  4805. ExpectIntEQ(XSTRNCMP(
  4806. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  4807. /* test with illeagal parameters */
  4808. outl = 1;
  4809. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  4810. ExpectIntEQ(outl, 0);
  4811. outl = 1;
  4812. EVP_EncodeFinal(ctx, NULL, &outl);
  4813. ExpectIntEQ(outl, 0);
  4814. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  4815. EVP_EncodeFinal(NULL, NULL, NULL);
  4816. EVP_ENCODE_CTX_free(ctx);
  4817. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4818. return EXPECT_RESULT();
  4819. }
  4820. static int test_wolfSSL_EVP_EncodeFinal(void)
  4821. {
  4822. int res = TEST_SKIPPED;
  4823. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4824. /* tests for wolfSSL_EVP_EncodeFinal are included in
  4825. * test_wolfSSL_EVP_EncodeUpdate
  4826. */
  4827. res = TEST_SUCCESS;
  4828. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4829. return res;
  4830. }
  4831. static int test_wolfSSL_EVP_DecodeInit(void)
  4832. {
  4833. EXPECT_DECLS;
  4834. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4835. EVP_ENCODE_CTX* ctx = NULL;
  4836. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  4837. ExpectIntEQ( ctx->remaining,0);
  4838. ExpectIntEQ( ctx->data[0],0);
  4839. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4840. if (ctx != NULL) {
  4841. /* make ctx dirty */
  4842. ctx->remaining = 10;
  4843. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  4844. }
  4845. EVP_DecodeInit(ctx);
  4846. ExpectIntEQ( ctx->remaining,0);
  4847. ExpectIntEQ( ctx->data[0],0);
  4848. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4849. EVP_ENCODE_CTX_free(ctx);
  4850. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4851. return EXPECT_RESULT();
  4852. }
  4853. static int test_wolfSSL_EVP_DecodeUpdate(void)
  4854. {
  4855. EXPECT_DECLS;
  4856. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4857. int outl;
  4858. unsigned char decOutBuff[300];
  4859. EVP_ENCODE_CTX* ctx = NULL;
  4860. static const unsigned char enc1[] =
  4861. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4862. /* const unsigned char plain1[] =
  4863. {"This is a base64 decoding test."} */
  4864. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4865. EVP_DecodeInit(ctx);
  4866. /* illegal parameter tests */
  4867. /* pass NULL as ctx */
  4868. ExpectIntEQ(
  4869. EVP_DecodeUpdate(
  4870. NULL, /* pass NULL as ctx */
  4871. decOutBuff,
  4872. &outl,
  4873. enc1,
  4874. sizeof(enc1)-1),
  4875. -1 /* expected result code -1: fail */
  4876. );
  4877. ExpectIntEQ( outl, 0);
  4878. /* pass NULL as output */
  4879. ExpectIntEQ(
  4880. EVP_DecodeUpdate(
  4881. ctx,
  4882. NULL, /* pass NULL as out buff */
  4883. &outl,
  4884. enc1,
  4885. sizeof(enc1)-1),
  4886. -1 /* expected result code -1: fail */
  4887. );
  4888. ExpectIntEQ( outl, 0);
  4889. /* pass NULL as outl */
  4890. ExpectIntEQ(
  4891. EVP_DecodeUpdate(
  4892. ctx,
  4893. decOutBuff,
  4894. NULL, /* pass NULL as outl */
  4895. enc1,
  4896. sizeof(enc1)-1),
  4897. -1 /* expected result code -1: fail */
  4898. );
  4899. /* pass NULL as input */
  4900. ExpectIntEQ(
  4901. EVP_DecodeUpdate(
  4902. ctx,
  4903. decOutBuff,
  4904. &outl,
  4905. NULL, /* pass NULL as in */
  4906. sizeof(enc1)-1),
  4907. -1 /* expected result code -1: fail */
  4908. );
  4909. ExpectIntEQ( outl, 0);
  4910. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4911. /* pass zero length input */
  4912. ExpectIntEQ(
  4913. EVP_DecodeUpdate(
  4914. ctx,
  4915. decOutBuff,
  4916. &outl,
  4917. enc1,
  4918. 0), /* pass zero as input len */
  4919. 1 /* expected result code 1: success */
  4920. );
  4921. /* decode correct base64 string */
  4922. {
  4923. static const unsigned char enc2[] =
  4924. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4925. static const unsigned char plain2[] =
  4926. {"This is a base64 decoding test."};
  4927. EVP_EncodeInit(ctx);
  4928. ExpectIntEQ(
  4929. EVP_DecodeUpdate(
  4930. ctx,
  4931. decOutBuff,
  4932. &outl,
  4933. enc2,
  4934. sizeof(enc2)-1),
  4935. 0 /* expected result code 0: success */
  4936. );
  4937. ExpectIntEQ(outl,sizeof(plain2) -1);
  4938. ExpectIntEQ(
  4939. EVP_DecodeFinal(
  4940. ctx,
  4941. decOutBuff + outl,
  4942. &outl),
  4943. 1 /* expected result code 1: success */
  4944. );
  4945. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  4946. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4947. sizeof(plain2) -1 ),0);
  4948. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4949. sizeof(plain2)-1);
  4950. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4951. sizeof(plain2) -1 ),0);
  4952. }
  4953. /* decode correct base64 string which does not have '\n' in its last*/
  4954. {
  4955. static const unsigned char enc3[] =
  4956. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4957. static const unsigned char plain3[] =
  4958. {"This is a base64 decoding test."}; /* 31 chars */
  4959. EVP_EncodeInit(ctx);
  4960. ExpectIntEQ(
  4961. EVP_DecodeUpdate(
  4962. ctx,
  4963. decOutBuff,
  4964. &outl,
  4965. enc3,
  4966. sizeof(enc3)-1),
  4967. 0 /* expected result code 0: success */
  4968. );
  4969. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4970. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4971. sizeof(plain3) -1 ),0);
  4972. ExpectIntEQ(
  4973. EVP_DecodeFinal(
  4974. ctx,
  4975. decOutBuff + outl,
  4976. &outl),
  4977. 1 /* expected result code 1: success */
  4978. );
  4979. ExpectIntEQ(outl,0 );
  4980. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4981. sizeof(plain3)-1);
  4982. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4983. sizeof(plain3) -1 ),0);
  4984. }
  4985. /* decode string which has a padding char ('=') in the illegal position*/
  4986. {
  4987. static const unsigned char enc4[] =
  4988. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4989. EVP_EncodeInit(ctx);
  4990. ExpectIntEQ(
  4991. EVP_DecodeUpdate(
  4992. ctx,
  4993. decOutBuff,
  4994. &outl,
  4995. enc4,
  4996. sizeof(enc4)-1),
  4997. -1 /* expected result code -1: error */
  4998. );
  4999. ExpectIntEQ(outl,0);
  5000. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5001. }
  5002. /* small data decode test */
  5003. {
  5004. static const unsigned char enc00[] = {"VG"};
  5005. static const unsigned char enc01[] = {"g=\n"};
  5006. static const unsigned char plain4[] = {"Th"};
  5007. EVP_EncodeInit(ctx);
  5008. ExpectIntEQ(
  5009. EVP_DecodeUpdate(
  5010. ctx,
  5011. decOutBuff,
  5012. &outl,
  5013. enc00,
  5014. sizeof(enc00)-1),
  5015. 1 /* expected result code 1: success */
  5016. );
  5017. ExpectIntEQ(outl,0);
  5018. ExpectIntEQ(
  5019. EVP_DecodeUpdate(
  5020. ctx,
  5021. decOutBuff + outl,
  5022. &outl,
  5023. enc01,
  5024. sizeof(enc01)-1),
  5025. 0 /* expected result code 0: success */
  5026. );
  5027. ExpectIntEQ(outl,sizeof(plain4)-1);
  5028. /* test with illegal parameters */
  5029. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5030. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5031. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5032. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5033. if (EXPECT_SUCCESS()) {
  5034. EVP_DecodeFinal(
  5035. ctx,
  5036. decOutBuff + outl,
  5037. &outl);
  5038. }
  5039. ExpectIntEQ( outl, 0);
  5040. ExpectIntEQ(
  5041. XSTRNCMP(
  5042. (const char*)decOutBuff,
  5043. (const char*)plain4,sizeof(plain4)-1 ),
  5044. 0);
  5045. }
  5046. EVP_ENCODE_CTX_free(ctx);
  5047. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5048. return EXPECT_RESULT();
  5049. }
  5050. static int test_wolfSSL_EVP_DecodeFinal(void)
  5051. {
  5052. int res = TEST_SKIPPED;
  5053. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5054. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5055. * test_wolfSSL_EVP_DecodeUpdate
  5056. */
  5057. res = TEST_SUCCESS;
  5058. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5059. return res;
  5060. }
  5061. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5062. */
  5063. #ifdef OPENSSL_EXTRA
  5064. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5065. {
  5066. EXPECT_DECLS;
  5067. #ifndef NO_AES
  5068. const WOLFSSL_EVP_CIPHER* c;
  5069. c = wolfSSL_EVP_get_cipherbynid(419);
  5070. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5071. defined(WOLFSSL_AES_128)
  5072. ExpectNotNull(c);
  5073. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5074. #else
  5075. ExpectNull(c);
  5076. #endif
  5077. c = wolfSSL_EVP_get_cipherbynid(423);
  5078. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5079. defined(WOLFSSL_AES_192)
  5080. ExpectNotNull(c);
  5081. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5082. #else
  5083. ExpectNull(c);
  5084. #endif
  5085. c = wolfSSL_EVP_get_cipherbynid(427);
  5086. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5087. defined(WOLFSSL_AES_256)
  5088. ExpectNotNull(c);
  5089. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5090. #else
  5091. ExpectNull(c);
  5092. #endif
  5093. c = wolfSSL_EVP_get_cipherbynid(904);
  5094. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5095. ExpectNotNull(c);
  5096. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5097. #else
  5098. ExpectNull(c);
  5099. #endif
  5100. c = wolfSSL_EVP_get_cipherbynid(905);
  5101. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5102. ExpectNotNull(c);
  5103. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5104. #else
  5105. ExpectNull(c);
  5106. #endif
  5107. c = wolfSSL_EVP_get_cipherbynid(906);
  5108. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5109. ExpectNotNull(c);
  5110. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5111. #else
  5112. ExpectNull(c);
  5113. #endif
  5114. c = wolfSSL_EVP_get_cipherbynid(418);
  5115. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5116. ExpectNotNull(c);
  5117. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5118. #else
  5119. ExpectNull(c);
  5120. #endif
  5121. c = wolfSSL_EVP_get_cipherbynid(422);
  5122. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5123. ExpectNotNull(c);
  5124. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5125. #else
  5126. ExpectNull(c);
  5127. #endif
  5128. c = wolfSSL_EVP_get_cipherbynid(426);
  5129. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5130. ExpectNotNull(c);
  5131. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5132. #else
  5133. ExpectNull(c);
  5134. #endif
  5135. #endif /* !NO_AES */
  5136. #ifndef NO_DES3
  5137. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5138. #ifdef WOLFSSL_DES_ECB
  5139. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5140. #endif
  5141. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5142. #ifdef WOLFSSL_DES_ECB
  5143. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5144. #endif
  5145. #endif /* !NO_DES3 */
  5146. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5147. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5148. #endif
  5149. /* test for nid is out of range */
  5150. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5151. return EXPECT_RESULT();
  5152. }
  5153. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5154. {
  5155. EXPECT_DECLS;
  5156. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5157. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5158. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5159. const EVP_CIPHER *test;
  5160. byte key[AES_BLOCK_SIZE] = {0};
  5161. byte iv[AES_BLOCK_SIZE] = {0};
  5162. ExpectNotNull(ctx);
  5163. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5164. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5165. test = EVP_CIPHER_CTX_cipher(ctx);
  5166. ExpectTrue(init == test);
  5167. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5168. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5169. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5170. EVP_CIPHER_CTX_free(ctx);
  5171. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5172. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5173. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5174. return EXPECT_RESULT();
  5175. }
  5176. #endif /* OPENSSL_EXTRA */
  5177. /*----------------------------------------------------------------------------*
  5178. | IO
  5179. *----------------------------------------------------------------------------*/
  5180. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5181. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5182. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5183. #ifdef WC_SHA512_DIGEST_SIZE
  5184. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5185. #else
  5186. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5187. #endif
  5188. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5189. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5190. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5191. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5192. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5193. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5194. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5195. defined(HAVE_AES_CBC)
  5196. typedef struct openssl_key_ctx {
  5197. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5198. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5199. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5200. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5201. } openssl_key_ctx;
  5202. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5203. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5204. static WC_INLINE int OpenSSLTicketInit(void)
  5205. {
  5206. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5207. if (ret != 0) return ret;
  5208. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5209. sizeof(myOpenSSLKey_ctx.name));
  5210. if (ret != 0) return ret;
  5211. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5212. sizeof(myOpenSSLKey_ctx.key));
  5213. if (ret != 0) return ret;
  5214. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5215. sizeof(myOpenSSLKey_ctx.hmacKey));
  5216. if (ret != 0) return ret;
  5217. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5218. sizeof(myOpenSSLKey_ctx.iv));
  5219. if (ret != 0) return ret;
  5220. return 0;
  5221. }
  5222. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5223. byte name[WOLFSSL_TICKET_NAME_SZ],
  5224. byte iv[WOLFSSL_TICKET_IV_SZ],
  5225. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5226. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5227. (void)ssl;
  5228. if (enc) {
  5229. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5230. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5231. }
  5232. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5233. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5234. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5235. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5236. return 0;
  5237. }
  5238. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5239. if (enc)
  5240. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5241. else
  5242. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5243. return 1;
  5244. }
  5245. static WC_INLINE void OpenSSLTicketCleanup(void)
  5246. {
  5247. wc_FreeRng(&myOpenSSLKey_rng);
  5248. }
  5249. #endif
  5250. #endif
  5251. /* helper functions */
  5252. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5253. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5254. void *ctx)
  5255. {
  5256. struct test_ssl_memio_ctx *test_ctx;
  5257. byte *buf;
  5258. int *len;
  5259. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5260. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5261. buf = test_ctx->c_buff;
  5262. len = &test_ctx->c_len;
  5263. }
  5264. else {
  5265. buf = test_ctx->s_buff;
  5266. len = &test_ctx->s_len;
  5267. }
  5268. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5269. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5270. XMEMCPY(buf + *len, data, sz);
  5271. *len += sz;
  5272. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5273. {
  5274. /* This can be imported into Wireshark by transforming the file with
  5275. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5276. * And then loading test_output.dump.hex into Wireshark using the
  5277. * "Import from Hex Dump..." option ion and selecting the TCP
  5278. * encapsulation option. */
  5279. char dump_file_name[64];
  5280. WOLFSSL_BIO *dump_file;
  5281. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5282. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5283. if (dump_file != NULL) {
  5284. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5285. wolfSSL_BIO_free(dump_file);
  5286. }
  5287. }
  5288. #endif
  5289. return sz;
  5290. }
  5291. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5292. void *ctx)
  5293. {
  5294. struct test_ssl_memio_ctx *test_ctx;
  5295. int read_sz;
  5296. byte *buf;
  5297. int *len;
  5298. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5299. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5300. buf = test_ctx->s_buff;
  5301. len = &test_ctx->s_len;
  5302. }
  5303. else {
  5304. buf = test_ctx->c_buff;
  5305. len = &test_ctx->c_len;
  5306. }
  5307. if (*len == 0)
  5308. return WOLFSSL_CBIO_ERR_WANT_READ;
  5309. read_sz = sz < *len ? sz : *len;
  5310. XMEMCPY(data, buf, read_sz);
  5311. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5312. *len -= read_sz;
  5313. return read_sz;
  5314. }
  5315. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5316. {
  5317. EXPECT_DECLS;
  5318. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5319. int c_sharedCtx = 0;
  5320. int s_sharedCtx = 0;
  5321. #endif
  5322. const char* clientCertFile = cliCertFile;
  5323. const char* clientKeyFile = cliKeyFile;
  5324. const char* serverCertFile = svrCertFile;
  5325. const char* serverKeyFile = svrKeyFile;
  5326. /********************************
  5327. * Create WOLFSSL_CTX for client.
  5328. ********************************/
  5329. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5330. if (ctx->c_ctx != NULL) {
  5331. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5332. }
  5333. else
  5334. #endif
  5335. {
  5336. WOLFSSL_METHOD* method = NULL;
  5337. if (ctx->c_cb.method != NULL) {
  5338. method = ctx->c_cb.method();
  5339. }
  5340. else {
  5341. method = wolfSSLv23_client_method();
  5342. }
  5343. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5344. }
  5345. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5346. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5347. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5348. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5349. #endif
  5350. if (ctx->c_cb.caPemFile != NULL)
  5351. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5352. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5353. else
  5354. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5355. caCertFile, 0), WOLFSSL_SUCCESS);
  5356. if (ctx->c_cb.certPemFile != NULL) {
  5357. clientCertFile = ctx->c_cb.certPemFile;
  5358. }
  5359. if (ctx->c_cb.keyPemFile != NULL) {
  5360. clientKeyFile = ctx->c_cb.keyPemFile;
  5361. }
  5362. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5363. if (!c_sharedCtx)
  5364. #endif
  5365. {
  5366. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5367. clientCertFile), WOLFSSL_SUCCESS);
  5368. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5369. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5370. }
  5371. #ifdef HAVE_CRL
  5372. if (ctx->c_cb.crlPemFile != NULL) {
  5373. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5374. WOLFSSL_SUCCESS);
  5375. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5376. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5377. }
  5378. #endif
  5379. if (ctx->c_ciphers != NULL) {
  5380. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5381. WOLFSSL_SUCCESS);
  5382. }
  5383. if (ctx->c_cb.ctx_ready != NULL) {
  5384. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5385. }
  5386. /********************************
  5387. * Create WOLFSSL_CTX for server.
  5388. ********************************/
  5389. if (ctx->s_ctx != NULL) {
  5390. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5391. s_sharedCtx = 1;
  5392. #endif
  5393. ctx->s_cb.isSharedCtx = 1;
  5394. }
  5395. else
  5396. {
  5397. WOLFSSL_METHOD* method = NULL;
  5398. if (ctx->s_cb.method != NULL) {
  5399. method = ctx->s_cb.method();
  5400. }
  5401. else {
  5402. method = wolfSSLv23_server_method();
  5403. }
  5404. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5405. ctx->s_cb.isSharedCtx = 0;
  5406. }
  5407. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5408. #if defined(HAVE_SESSION_TICKET) && \
  5409. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5410. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5411. OpenSSLTicketInit();
  5412. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5413. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5414. TicketInit();
  5415. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5416. #endif
  5417. #endif
  5418. }
  5419. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5420. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5421. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5422. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5423. if (ctx->s_cb.caPemFile != NULL)
  5424. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5425. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5426. else
  5427. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5428. cliCertFile, 0), WOLFSSL_SUCCESS);
  5429. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5430. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5431. #endif
  5432. if (ctx->s_cb.certPemFile != NULL) {
  5433. serverCertFile = ctx->s_cb.certPemFile;
  5434. }
  5435. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5436. if (!s_sharedCtx)
  5437. #endif
  5438. {
  5439. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5440. serverCertFile), WOLFSSL_SUCCESS);
  5441. }
  5442. if (ctx->s_cb.keyPemFile != NULL) {
  5443. serverKeyFile = ctx->s_cb.keyPemFile;
  5444. }
  5445. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5446. if (!s_sharedCtx)
  5447. #endif
  5448. {
  5449. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5450. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5451. }
  5452. if (ctx->s_ciphers != NULL) {
  5453. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5454. WOLFSSL_SUCCESS);
  5455. }
  5456. if (ctx->s_cb.ctx_ready != NULL) {
  5457. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5458. }
  5459. /****************************
  5460. * Create WOLFSSL for client.
  5461. ****************************/
  5462. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5463. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5464. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5465. if (0
  5466. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5467. || c_sharedCtx
  5468. #endif
  5469. )
  5470. {
  5471. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5472. clientCertFile), WOLFSSL_SUCCESS);
  5473. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5474. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5475. }
  5476. if (ctx->c_cb.ssl_ready != NULL) {
  5477. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5478. }
  5479. /****************************
  5480. * Create WOLFSSL for server.
  5481. ****************************/
  5482. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5483. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5484. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5485. if (0
  5486. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5487. || s_sharedCtx
  5488. #endif
  5489. )
  5490. {
  5491. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5492. serverCertFile), WOLFSSL_SUCCESS);
  5493. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5494. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5495. }
  5496. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5497. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5498. #elif !defined(NO_DH)
  5499. /* will repick suites with DHE, higher priority than PSK */
  5500. SetDH(ctx->s_ssl);
  5501. #endif
  5502. if (ctx->s_cb.ssl_ready != NULL) {
  5503. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5504. }
  5505. return EXPECT_RESULT();
  5506. }
  5507. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5508. int* rounds)
  5509. {
  5510. int handshake_complete = 0;
  5511. int hs_c = 0;
  5512. int hs_s = 0;
  5513. int failing_s = 0;
  5514. int failing_c = 0;
  5515. int ret;
  5516. int err;
  5517. if (rounds != NULL) {
  5518. *rounds = 0;
  5519. }
  5520. while ((!handshake_complete) && (max_rounds > 0)) {
  5521. if (!hs_c) {
  5522. wolfSSL_SetLoggingPrefix("client");
  5523. ret = wolfSSL_connect(ctx->c_ssl);
  5524. wolfSSL_SetLoggingPrefix(NULL);
  5525. if (ret == WOLFSSL_SUCCESS) {
  5526. hs_c = 1;
  5527. }
  5528. else {
  5529. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5530. if (err != WOLFSSL_ERROR_WANT_READ &&
  5531. err != WOLFSSL_ERROR_WANT_WRITE) {
  5532. char buff[WOLFSSL_MAX_ERROR_SZ];
  5533. fprintf(stderr, "error = %d, %s\n", err,
  5534. wolfSSL_ERR_error_string(err, buff));
  5535. failing_c = 1;
  5536. hs_c = 1;
  5537. if (failing_c && failing_s) {
  5538. break;
  5539. }
  5540. }
  5541. }
  5542. }
  5543. if (!hs_s) {
  5544. wolfSSL_SetLoggingPrefix("server");
  5545. ret = wolfSSL_accept(ctx->s_ssl);
  5546. wolfSSL_SetLoggingPrefix(NULL);
  5547. if (ret == WOLFSSL_SUCCESS) {
  5548. hs_s = 1;
  5549. }
  5550. else {
  5551. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5552. if (err != WOLFSSL_ERROR_WANT_READ &&
  5553. err != WOLFSSL_ERROR_WANT_WRITE) {
  5554. char buff[WOLFSSL_MAX_ERROR_SZ];
  5555. fprintf(stderr, "error = %d, %s\n", err,
  5556. wolfSSL_ERR_error_string(err, buff));
  5557. failing_s = 1;
  5558. hs_s = 1;
  5559. if (failing_c && failing_s) {
  5560. break;
  5561. }
  5562. }
  5563. }
  5564. }
  5565. handshake_complete = hs_c && hs_s;
  5566. max_rounds--;
  5567. if (rounds != NULL) {
  5568. *rounds += 1;
  5569. }
  5570. }
  5571. if (!handshake_complete || failing_c || failing_s) {
  5572. return TEST_FAIL;
  5573. }
  5574. return TEST_SUCCESS;
  5575. }
  5576. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5577. {
  5578. EXPECT_DECLS;
  5579. char input[1024];
  5580. int idx = 0;
  5581. const char* msg_c = "hello wolfssl!";
  5582. int msglen_c = (int)XSTRLEN(msg_c);
  5583. const char* msg_s = "I hear you fa shizzle!";
  5584. int msglen_s = (int)XSTRLEN(msg_s);
  5585. if (ctx->c_msg != NULL) {
  5586. msg_c = ctx->c_msg;
  5587. msglen_c = ctx->c_msglen;
  5588. }
  5589. if (ctx->s_msg != NULL) {
  5590. msg_s = ctx->s_msg;
  5591. msglen_s = ctx->s_msglen;
  5592. }
  5593. wolfSSL_SetLoggingPrefix("client");
  5594. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5595. wolfSSL_SetLoggingPrefix("server");
  5596. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5597. if (idx >= 0) {
  5598. input[idx] = '\0';
  5599. }
  5600. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5601. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5602. ctx->s_cb.return_code = EXPECT_RESULT();
  5603. wolfSSL_SetLoggingPrefix("client");
  5604. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5605. wolfSSL_SetLoggingPrefix(NULL);
  5606. if (idx >= 0) {
  5607. input[idx] = '\0';
  5608. }
  5609. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5610. ctx->c_cb.return_code = EXPECT_RESULT();
  5611. if (ctx->c_cb.on_result != NULL) {
  5612. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5613. }
  5614. if (ctx->s_cb.on_result != NULL) {
  5615. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5616. }
  5617. return EXPECT_RESULT();
  5618. }
  5619. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5620. {
  5621. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5622. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5623. if (ctx->c_cb.on_cleanup != NULL) {
  5624. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5625. }
  5626. if (ctx->s_cb.on_cleanup != NULL) {
  5627. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5628. }
  5629. wolfSSL_shutdown(ctx->s_ssl);
  5630. wolfSSL_shutdown(ctx->c_ssl);
  5631. wolfSSL_free(ctx->s_ssl);
  5632. wolfSSL_free(ctx->c_ssl);
  5633. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  5634. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  5635. }
  5636. if (!ctx->c_cb.isSharedCtx) {
  5637. wolfSSL_CTX_free(ctx->c_ctx);
  5638. ctx->c_ctx = NULL;
  5639. }
  5640. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  5641. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  5642. }
  5643. if (!ctx->s_cb.isSharedCtx) {
  5644. wolfSSL_CTX_free(ctx->s_ctx);
  5645. ctx->s_ctx = NULL;
  5646. }
  5647. if (!ctx->s_cb.ticNoInit) {
  5648. #if defined(HAVE_SESSION_TICKET) && \
  5649. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5650. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5651. OpenSSLTicketCleanup();
  5652. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5653. TicketCleanup();
  5654. #endif
  5655. #endif
  5656. }
  5657. }
  5658. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5659. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5660. {
  5661. EXPECT_DECLS;
  5662. struct test_ssl_memio_ctx test_ctx;
  5663. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5664. size_t msg_len;
  5665. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5666. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5667. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5668. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5669. test_ctx.c_ctx = client_cb->ctx;
  5670. test_ctx.s_ctx = server_cb->ctx;
  5671. test_ctx.c_cb.return_code = TEST_FAIL;
  5672. test_ctx.s_cb.return_code = TEST_FAIL;
  5673. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5674. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5675. if (client_on_handshake != NULL) {
  5676. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5677. TEST_SUCCESS);
  5678. }
  5679. if (client_cb->on_handshake != NULL) {
  5680. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  5681. TEST_SUCCESS);
  5682. }
  5683. if (server_cb->on_handshake != NULL) {
  5684. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  5685. TEST_SUCCESS);
  5686. }
  5687. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5688. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5689. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  5690. MD_MAX_SIZE);
  5691. ExpectIntGE(msg_len, 0);
  5692. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5693. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  5694. MD_MAX_SIZE);
  5695. ExpectIntGE(msg_len, 0);
  5696. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5697. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  5698. test_ssl_memio_cleanup(&test_ctx);
  5699. client_cb->return_code = test_ctx.c_cb.return_code;
  5700. client_cb->last_err = test_ctx.c_cb.last_err;
  5701. server_cb->return_code = test_ctx.s_cb.return_code;
  5702. server_cb->last_err = test_ctx.s_cb.last_err;
  5703. return EXPECT_RESULT();
  5704. }
  5705. #endif
  5706. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  5707. #ifdef WOLFSSL_SESSION_EXPORT
  5708. #ifdef WOLFSSL_DTLS
  5709. /* set up function for sending session information */
  5710. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  5711. {
  5712. WOLFSSL_CTX* ctx = NULL;
  5713. WOLFSSL* ssl = NULL;
  5714. AssertNotNull(inSsl);
  5715. AssertNotNull(buf);
  5716. AssertIntNE(0, sz);
  5717. /* Set ctx to DTLS 1.2 */
  5718. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  5719. AssertNotNull(ctx);
  5720. ssl = wolfSSL_new(ctx);
  5721. AssertNotNull(ssl);
  5722. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  5723. wolfSSL_free(ssl);
  5724. wolfSSL_CTX_free(ctx);
  5725. (void)userCtx;
  5726. return 0;
  5727. }
  5728. #endif
  5729. /* returns negative value on fail and positive (including 0) on success */
  5730. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  5731. {
  5732. int ret, err, loop_count, count, timeout = 10;
  5733. char msg[] = "I hear you fa shizzle!";
  5734. char input[1024];
  5735. loop_count = ((func_args*)args)->argc;
  5736. #ifdef WOLFSSL_ASYNC_CRYPT
  5737. err = 0; /* Reset error */
  5738. #endif
  5739. do {
  5740. #ifdef WOLFSSL_ASYNC_CRYPT
  5741. if (err == WC_PENDING_E) {
  5742. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5743. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5744. }
  5745. #endif
  5746. ret = wolfSSL_accept(ssl);
  5747. err = wolfSSL_get_error(ssl, 0);
  5748. if (err == WOLFSSL_ERROR_WANT_READ ||
  5749. err == WOLFSSL_ERROR_WANT_WRITE) {
  5750. int select_ret;
  5751. err = WC_PENDING_E;
  5752. select_ret = tcp_select(*sockfd, timeout);
  5753. if (select_ret == TEST_TIMEOUT) {
  5754. return WOLFSSL_FATAL_ERROR;
  5755. }
  5756. }
  5757. } while (err == WC_PENDING_E);
  5758. if (ret != WOLFSSL_SUCCESS) {
  5759. char buff[WOLFSSL_MAX_ERROR_SZ];
  5760. fprintf(stderr, "error = %d, %s\n", err,
  5761. wolfSSL_ERR_error_string(err, buff));
  5762. return ret;
  5763. }
  5764. for (count = 0; count < loop_count; count++) {
  5765. int select_ret;
  5766. select_ret = tcp_select(*sockfd, timeout);
  5767. if (select_ret == TEST_TIMEOUT) {
  5768. ret = WOLFSSL_FATAL_ERROR;
  5769. break;
  5770. }
  5771. do {
  5772. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5773. if (ret > 0) {
  5774. input[ret] = '\0';
  5775. fprintf(stderr, "Client message: %s\n", input);
  5776. }
  5777. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5778. do {
  5779. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  5780. return WOLFSSL_FATAL_ERROR;
  5781. }
  5782. err = wolfSSL_get_error(ssl, ret);
  5783. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5784. }
  5785. return ret;
  5786. }
  5787. #endif /* WOLFSSL_SESSION_EXPORT */
  5788. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  5789. {
  5790. SOCKET_T sockfd = 0;
  5791. SOCKET_T clientfd = 0;
  5792. word16 port;
  5793. callback_functions* cbf;
  5794. WOLFSSL_CTX* ctx = NULL;
  5795. WOLFSSL* ssl = NULL;
  5796. func_args* opts = (func_args*)args;
  5797. char msg[] = "I hear you fa shizzle!";
  5798. char input[1024];
  5799. int idx;
  5800. int ret, err = 0;
  5801. int sharedCtx = 0;
  5802. int doUdp = 0;
  5803. SOCKADDR_IN_T cliAddr;
  5804. socklen_t cliLen;
  5805. const char* certFile = svrCertFile;
  5806. const char* keyFile = svrKeyFile;
  5807. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5808. size_t msg_len = 0;
  5809. #endif
  5810. wolfSSL_SetLoggingPrefix("server");
  5811. #ifdef WOLFSSL_TIRTOS
  5812. fdOpenSession(Task_self());
  5813. #endif
  5814. opts->return_code = TEST_FAIL;
  5815. cbf = opts->callbacks;
  5816. if (cbf != NULL && cbf->ctx) {
  5817. ctx = cbf->ctx;
  5818. sharedCtx = 1;
  5819. }
  5820. else
  5821. {
  5822. WOLFSSL_METHOD* method = NULL;
  5823. if (cbf != NULL && cbf->method != NULL) {
  5824. method = cbf->method();
  5825. }
  5826. else {
  5827. method = wolfSSLv23_server_method();
  5828. }
  5829. ctx = wolfSSL_CTX_new(method);
  5830. }
  5831. if (ctx == NULL) {
  5832. /* Release the wait for TCP ready. */
  5833. signal_ready(opts->signal);
  5834. goto done;
  5835. }
  5836. if (cbf == NULL || !cbf->ticNoInit) {
  5837. #if defined(HAVE_SESSION_TICKET) && \
  5838. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5839. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5840. OpenSSLTicketInit();
  5841. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  5842. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5843. TicketInit();
  5844. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  5845. #endif
  5846. #endif
  5847. }
  5848. #if defined(USE_WINDOWS_API)
  5849. port = opts->signal->port;
  5850. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5851. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5852. /* Let tcp_listen assign port */
  5853. port = 0;
  5854. #else
  5855. /* Use default port */
  5856. port = wolfSSLPort;
  5857. #endif
  5858. if (cbf != NULL)
  5859. doUdp = cbf->doUdp;
  5860. /* do it here to detect failure */
  5861. tcp_accept(
  5862. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  5863. if (doUdp) {
  5864. cliLen = sizeof(cliAddr);
  5865. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  5866. (struct sockaddr*)&cliAddr, &cliLen);
  5867. AssertIntGT(idx, 0);
  5868. }
  5869. else {
  5870. CloseSocket(sockfd);
  5871. }
  5872. wolfSSL_CTX_set_verify(ctx,
  5873. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5874. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5875. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5876. #endif
  5877. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5878. != WOLFSSL_SUCCESS) {
  5879. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5880. goto done;
  5881. }
  5882. if (cbf != NULL && cbf->certPemFile != NULL)
  5883. certFile = cbf->certPemFile;
  5884. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5885. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5886. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5887. #else
  5888. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5889. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5890. #endif
  5891. /*err_sys("can't load server cert chain file, "
  5892. "Please run from wolfSSL home dir");*/
  5893. goto done;
  5894. }
  5895. if (cbf != NULL && cbf->keyPemFile != NULL)
  5896. keyFile = cbf->keyPemFile;
  5897. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5898. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5899. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5900. #else
  5901. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5902. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5903. #endif
  5904. /*err_sys("can't load server key file, "
  5905. "Please run from wolfSSL home dir");*/
  5906. goto done;
  5907. }
  5908. #ifdef HAVE_CRL
  5909. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5910. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5911. goto done;
  5912. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5913. != WOLFSSL_SUCCESS)
  5914. goto done;
  5915. }
  5916. #endif
  5917. /* call ctx setup callback */
  5918. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5919. cbf->ctx_ready(ctx);
  5920. }
  5921. ssl = wolfSSL_new(ctx);
  5922. if (ssl == NULL) {
  5923. goto done;
  5924. }
  5925. if (doUdp) {
  5926. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5927. if (err != WOLFSSL_SUCCESS)
  5928. goto done;
  5929. }
  5930. #ifdef WOLFSSL_SESSION_EXPORT
  5931. /* only add in more complex nonblocking case with session export tests */
  5932. if (args && opts->argc > 0) {
  5933. /* set as nonblock and time out for waiting on read/write */
  5934. tcp_set_nonblocking(&clientfd);
  5935. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5936. }
  5937. #endif
  5938. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5939. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5940. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5941. #else
  5942. if (wolfSSL_use_certificate_file(ssl, certFile,
  5943. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5944. #endif
  5945. /*err_sys("can't load server cert chain file, "
  5946. "Please run from wolfSSL home dir");*/
  5947. goto done;
  5948. }
  5949. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5950. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5951. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5952. #else
  5953. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5954. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5955. #endif
  5956. /*err_sys("can't load server key file, "
  5957. "Please run from wolfSSL home dir");*/
  5958. goto done;
  5959. }
  5960. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5961. /*err_sys("SSL_set_fd failed");*/
  5962. goto done;
  5963. }
  5964. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5965. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5966. #elif !defined(NO_DH)
  5967. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5968. #endif
  5969. /* call ssl setup callback */
  5970. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5971. cbf->ssl_ready(ssl);
  5972. }
  5973. #ifdef WOLFSSL_SESSION_EXPORT
  5974. /* only add in more complex nonblocking case with session export tests */
  5975. if (opts->argc > 0) {
  5976. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5977. if (ret >= 0) {
  5978. opts->return_code = TEST_SUCCESS;
  5979. }
  5980. #ifdef WOLFSSL_TIRTOS
  5981. Task_yield();
  5982. #endif
  5983. goto done;
  5984. }
  5985. #endif
  5986. #ifdef WOLFSSL_ASYNC_CRYPT
  5987. err = 0; /* Reset error */
  5988. #endif
  5989. do {
  5990. #ifdef WOLFSSL_ASYNC_CRYPT
  5991. if (err == WC_PENDING_E) {
  5992. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5993. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5994. }
  5995. #endif
  5996. ret = wolfSSL_negotiate(ssl);
  5997. err = wolfSSL_get_error(ssl, 0);
  5998. } while (err == WC_PENDING_E);
  5999. if (ret != WOLFSSL_SUCCESS) {
  6000. char buff[WOLFSSL_MAX_ERROR_SZ];
  6001. fprintf(stderr, "error = %d, %s\n", err,
  6002. wolfSSL_ERR_error_string(err, buff));
  6003. /*err_sys("SSL_accept failed");*/
  6004. goto done;
  6005. }
  6006. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6007. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6008. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6009. AssertIntGE(msg_len, 0);
  6010. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6011. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6012. AssertIntGE(msg_len, 0);
  6013. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6014. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6015. if (idx > 0) {
  6016. input[idx] = '\0';
  6017. fprintf(stderr, "Client message: %s\n", input);
  6018. }
  6019. else if (idx < 0) {
  6020. goto done;
  6021. }
  6022. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6023. /*err_sys("SSL_write failed");*/
  6024. goto done;
  6025. }
  6026. if (cbf != NULL && cbf->on_result != NULL)
  6027. cbf->on_result(ssl);
  6028. #ifdef WOLFSSL_TIRTOS
  6029. Task_yield();
  6030. #endif
  6031. opts->return_code = TEST_SUCCESS;
  6032. done:
  6033. if (cbf != NULL)
  6034. cbf->last_err = err;
  6035. if (cbf != NULL && cbf->on_cleanup != NULL)
  6036. cbf->on_cleanup(ssl);
  6037. wolfSSL_shutdown(ssl);
  6038. wolfSSL_free(ssl);
  6039. if (!sharedCtx)
  6040. wolfSSL_CTX_free(ctx);
  6041. CloseSocket(clientfd);
  6042. #ifdef WOLFSSL_TIRTOS
  6043. fdCloseSession(Task_self());
  6044. #endif
  6045. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6046. && defined(HAVE_THREAD_LS)
  6047. wc_ecc_fp_free(); /* free per thread cache */
  6048. #endif
  6049. if (cbf == NULL || !cbf->ticNoInit) {
  6050. #if defined(HAVE_SESSION_TICKET) && \
  6051. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6052. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6053. OpenSSLTicketCleanup();
  6054. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6055. TicketCleanup();
  6056. #endif
  6057. #endif
  6058. }
  6059. wolfSSL_SetLoggingPrefix(NULL);
  6060. WOLFSSL_RETURN_FROM_THREAD(0);
  6061. }
  6062. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6063. !defined(WOLFSSL_NO_TLS12)
  6064. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6065. {
  6066. SOCKET_T sockfd = 0;
  6067. SOCKET_T clientfd = 0;
  6068. word16 port;
  6069. callback_functions* cbf;
  6070. WOLFSSL_CTX* ctx = 0;
  6071. WOLFSSL* ssl = 0;
  6072. char msg[] = "I hear you fa shizzle!";
  6073. char input[1024];
  6074. int idx;
  6075. int ret, err = 0;
  6076. int sharedCtx = 0;
  6077. func_args* opts = (func_args*)args;
  6078. int loop_count = opts->argc;
  6079. int count = 0;
  6080. #ifdef WOLFSSL_TIRTOS
  6081. fdOpenSession(Task_self());
  6082. #endif
  6083. opts->return_code = TEST_FAIL;
  6084. cbf = opts->callbacks;
  6085. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6086. if (cbf != NULL && cbf->ctx) {
  6087. ctx = cbf->ctx;
  6088. sharedCtx = 1;
  6089. }
  6090. else
  6091. #endif
  6092. {
  6093. WOLFSSL_METHOD* method = NULL;
  6094. if (cbf != NULL && cbf->method != NULL) {
  6095. method = cbf->method();
  6096. }
  6097. else {
  6098. method = wolfSSLv23_server_method();
  6099. }
  6100. ctx = wolfSSL_CTX_new(method);
  6101. }
  6102. #if defined(USE_WINDOWS_API)
  6103. port = opts->signal->port;
  6104. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6105. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6106. /* Let tcp_listen assign port */
  6107. port = 0;
  6108. #else
  6109. /* Use default port */
  6110. port = wolfSSLPort;
  6111. #endif
  6112. wolfSSL_CTX_set_verify(ctx,
  6113. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6114. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6115. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6116. #endif
  6117. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6118. != WOLFSSL_SUCCESS) {
  6119. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6120. /* Release the wait for TCP ready. */
  6121. signal_ready(opts->signal);
  6122. goto done;
  6123. }
  6124. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6125. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6126. /*err_sys("can't load server cert chain file, "
  6127. "Please run from wolfSSL home dir");*/
  6128. /* Release the wait for TCP ready. */
  6129. signal_ready(opts->signal);
  6130. goto done;
  6131. }
  6132. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6133. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6134. /*err_sys("can't load server key file, "
  6135. "Please run from wolfSSL home dir");*/
  6136. /* Release the wait for TCP ready. */
  6137. signal_ready(opts->signal);
  6138. goto done;
  6139. }
  6140. /* call ctx setup callback */
  6141. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6142. cbf->ctx_ready(ctx);
  6143. }
  6144. while (count != loop_count) {
  6145. ssl = wolfSSL_new(ctx);
  6146. if (ssl == NULL) {
  6147. signal_ready(opts->signal);
  6148. goto done;
  6149. }
  6150. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6151. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6152. /*err_sys("can't load server cert chain file, "
  6153. "Please run from wolfSSL home dir");*/
  6154. /* Release the wait for TCP ready. */
  6155. signal_ready(opts->signal);
  6156. goto done;
  6157. }
  6158. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6159. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6160. /*err_sys("can't load server key file, "
  6161. "Please run from wolfSSL home dir");*/
  6162. /* Release the wait for TCP ready. */
  6163. signal_ready(opts->signal);
  6164. goto done;
  6165. }
  6166. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6167. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6168. #elif !defined(NO_DH)
  6169. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6170. #endif
  6171. /* call ssl setup callback */
  6172. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6173. cbf->ssl_ready(ssl);
  6174. }
  6175. /* do it here to detect failure */
  6176. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6177. 0);
  6178. CloseSocket(sockfd);
  6179. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6180. /*err_sys("SSL_set_fd failed");*/
  6181. goto done;
  6182. }
  6183. #ifdef WOLFSSL_ASYNC_CRYPT
  6184. err = 0; /* Reset error */
  6185. #endif
  6186. do {
  6187. #ifdef WOLFSSL_ASYNC_CRYPT
  6188. if (err == WC_PENDING_E) {
  6189. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6190. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6191. }
  6192. #endif
  6193. ret = wolfSSL_accept(ssl);
  6194. err = wolfSSL_get_error(ssl, 0);
  6195. } while (err == WC_PENDING_E);
  6196. if (ret != WOLFSSL_SUCCESS) {
  6197. char buff[WOLFSSL_MAX_ERROR_SZ];
  6198. fprintf(stderr, "error = %d, %s\n", err,
  6199. wolfSSL_ERR_error_string(err, buff));
  6200. /*err_sys("SSL_accept failed");*/
  6201. goto done;
  6202. }
  6203. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6204. if (idx > 0) {
  6205. input[idx] = '\0';
  6206. fprintf(stderr, "Client message: %s\n", input);
  6207. }
  6208. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6209. /*err_sys("SSL_write failed");*/
  6210. goto done;
  6211. }
  6212. /* free ssl for this connection */
  6213. wolfSSL_shutdown(ssl);
  6214. wolfSSL_free(ssl); ssl = NULL;
  6215. CloseSocket(clientfd);
  6216. count++;
  6217. }
  6218. #ifdef WOLFSSL_TIRTOS
  6219. Task_yield();
  6220. #endif
  6221. opts->return_code = TEST_SUCCESS;
  6222. done:
  6223. if (ssl != NULL) {
  6224. wolfSSL_shutdown(ssl);
  6225. wolfSSL_free(ssl);
  6226. }
  6227. if (!sharedCtx)
  6228. wolfSSL_CTX_free(ctx);
  6229. CloseSocket(clientfd);
  6230. #ifdef WOLFSSL_TIRTOS
  6231. fdCloseSession(Task_self());
  6232. #endif
  6233. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6234. && defined(HAVE_THREAD_LS)
  6235. wc_ecc_fp_free(); /* free per thread cache */
  6236. #endif
  6237. WOLFSSL_RETURN_FROM_THREAD(0);
  6238. }
  6239. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6240. static int test_client_nofail(void* args, cbType cb)
  6241. {
  6242. #if !defined(NO_WOLFSSL_CLIENT)
  6243. SOCKET_T sockfd = 0;
  6244. callback_functions* cbf;
  6245. WOLFSSL_CTX* ctx = 0;
  6246. WOLFSSL* ssl = 0;
  6247. WOLFSSL_CIPHER* cipher;
  6248. char msg[64] = "hello wolfssl!";
  6249. char reply[1024];
  6250. int input;
  6251. int msgSz = (int)XSTRLEN(msg);
  6252. int ret, err = 0;
  6253. int cipherSuite;
  6254. int sharedCtx = 0;
  6255. int doUdp = 0;
  6256. const char* cipherName1, *cipherName2;
  6257. wolfSSL_SetLoggingPrefix("client");
  6258. #ifdef WOLFSSL_TIRTOS
  6259. fdOpenSession(Task_self());
  6260. #endif
  6261. ((func_args*)args)->return_code = TEST_FAIL;
  6262. cbf = ((func_args*)args)->callbacks;
  6263. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6264. if (cbf != NULL && cbf->ctx) {
  6265. ctx = cbf->ctx;
  6266. sharedCtx = cbf->isSharedCtx;
  6267. }
  6268. else
  6269. #endif
  6270. {
  6271. WOLFSSL_METHOD* method = NULL;
  6272. if (cbf != NULL && cbf->method != NULL) {
  6273. method = cbf->method();
  6274. }
  6275. else {
  6276. method = wolfSSLv23_client_method();
  6277. }
  6278. ctx = wolfSSL_CTX_new(method);
  6279. }
  6280. if (cbf != NULL)
  6281. doUdp = cbf->doUdp;
  6282. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6283. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6284. #endif
  6285. /* Do connect here so server detects failures */
  6286. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6287. doUdp, 0, NULL);
  6288. /* Connect the socket so that we don't have to set the peer later on */
  6289. if (doUdp)
  6290. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6291. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6292. {
  6293. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6294. goto done;
  6295. }
  6296. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6297. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6298. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6299. #else
  6300. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6301. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6302. #endif
  6303. /*err_sys("can't load client cert file, "
  6304. "Please run from wolfSSL home dir");*/
  6305. goto done;
  6306. }
  6307. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6308. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6309. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6310. #else
  6311. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6312. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6313. #endif
  6314. /*err_sys("can't load client key file, "
  6315. "Please run from wolfSSL home dir");*/
  6316. goto done;
  6317. }
  6318. #ifdef HAVE_CRL
  6319. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6320. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6321. goto done;
  6322. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6323. != WOLFSSL_SUCCESS)
  6324. goto done;
  6325. }
  6326. #endif
  6327. /* call ctx setup callback */
  6328. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6329. cbf->ctx_ready(ctx);
  6330. }
  6331. ssl = wolfSSL_new(ctx);
  6332. if (ssl == NULL) {
  6333. goto done;
  6334. }
  6335. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6336. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6337. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6338. #else
  6339. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6340. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6341. #endif
  6342. /*err_sys("can't load client cert file, "
  6343. "Please run from wolfSSL home dir");*/
  6344. goto done;
  6345. }
  6346. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6347. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6348. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6349. #else
  6350. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6351. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6352. #endif
  6353. /*err_sys("can't load client key file, "
  6354. "Please run from wolfSSL home dir");*/
  6355. goto done;
  6356. }
  6357. if (!doUdp) {
  6358. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6359. /*err_sys("SSL_set_fd failed");*/
  6360. goto done;
  6361. }
  6362. }
  6363. else {
  6364. #ifdef WOLFSSL_DTLS
  6365. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6366. /*err_sys("SSL_set_fd failed");*/
  6367. goto done;
  6368. }
  6369. #else
  6370. goto done;
  6371. #endif
  6372. }
  6373. /* call ssl setup callback */
  6374. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6375. cbf->ssl_ready(ssl);
  6376. }
  6377. #ifdef WOLFSSL_ASYNC_CRYPT
  6378. err = 0; /* Reset error */
  6379. #endif
  6380. do {
  6381. #ifdef WOLFSSL_ASYNC_CRYPT
  6382. if (err == WC_PENDING_E) {
  6383. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6384. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6385. }
  6386. #endif
  6387. ret = wolfSSL_negotiate(ssl);
  6388. err = wolfSSL_get_error(ssl, 0);
  6389. } while (err == WC_PENDING_E);
  6390. if (ret != WOLFSSL_SUCCESS) {
  6391. char buff[WOLFSSL_MAX_ERROR_SZ];
  6392. fprintf(stderr, "error = %d, %s\n", err,
  6393. wolfSSL_ERR_error_string(err, buff));
  6394. /*err_sys("SSL_connect failed");*/
  6395. goto done;
  6396. }
  6397. /* test the various get cipher methods */
  6398. /* Internal cipher suite names */
  6399. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6400. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6401. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6402. (cipherSuite >> 8), cipherSuite & 0xFF);
  6403. AssertStrEQ(cipherName1, cipherName2);
  6404. /* IANA Cipher Suites Names */
  6405. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6406. then it's the internal cipher suite name */
  6407. cipher = wolfSSL_get_current_cipher(ssl);
  6408. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6409. cipherName2 = wolfSSL_get_cipher(ssl);
  6410. AssertStrEQ(cipherName1, cipherName2);
  6411. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6412. !defined(WOLFSSL_QT)
  6413. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6414. (cipherSuite >> 8), cipherSuite & 0xFF);
  6415. AssertStrEQ(cipherName1, cipherName2);
  6416. #endif
  6417. if (cb != NULL)
  6418. (cb)(ctx, ssl);
  6419. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6420. /*err_sys("SSL_write failed");*/
  6421. goto done;
  6422. }
  6423. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6424. if (input > 0) {
  6425. reply[input] = '\0';
  6426. fprintf(stderr, "Server response: %s\n", reply);
  6427. }
  6428. if (cbf != NULL && cbf->on_result != NULL)
  6429. cbf->on_result(ssl);
  6430. ((func_args*)args)->return_code = TEST_SUCCESS;
  6431. done:
  6432. if (cbf != NULL)
  6433. cbf->last_err = err;
  6434. if (cbf != NULL && cbf->on_cleanup != NULL)
  6435. cbf->on_cleanup(ssl);
  6436. wolfSSL_free(ssl);
  6437. if (!sharedCtx)
  6438. wolfSSL_CTX_free(ctx);
  6439. CloseSocket(sockfd);
  6440. #ifdef WOLFSSL_TIRTOS
  6441. fdCloseSession(Task_self());
  6442. #endif
  6443. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6444. && defined(HAVE_THREAD_LS)
  6445. wc_ecc_fp_free(); /* free per thread cache */
  6446. #endif
  6447. #else
  6448. (void)args;
  6449. (void)cb;
  6450. #endif /* !NO_WOLFSSL_CLIENT */
  6451. wolfSSL_SetLoggingPrefix(NULL);
  6452. return 0;
  6453. }
  6454. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6455. callback_functions* server_cb, cbType client_on_handshake)
  6456. {
  6457. func_args client_args;
  6458. func_args server_args;
  6459. tcp_ready ready;
  6460. THREAD_TYPE serverThread;
  6461. XMEMSET(&client_args, 0, sizeof(func_args));
  6462. XMEMSET(&server_args, 0, sizeof(func_args));
  6463. #ifdef WOLFSSL_TIRTOS
  6464. fdOpenSession(Task_self());
  6465. #endif
  6466. StartTCP();
  6467. InitTcpReady(&ready);
  6468. #if defined(USE_WINDOWS_API)
  6469. /* use RNG to get random port if using windows */
  6470. ready.port = GetRandomPort();
  6471. #endif
  6472. server_args.signal = &ready;
  6473. server_args.callbacks = server_cb;
  6474. client_args.signal = &ready;
  6475. client_args.callbacks = client_cb;
  6476. start_thread(test_server_nofail, &server_args, &serverThread);
  6477. wait_tcp_ready(&server_args);
  6478. test_client_nofail(&client_args, client_on_handshake);
  6479. join_thread(serverThread);
  6480. client_cb->return_code = client_args.return_code;
  6481. server_cb->return_code = server_args.return_code;
  6482. FreeTcpReady(&ready);
  6483. #ifdef WOLFSSL_TIRTOS
  6484. fdOpenSession(Task_self());
  6485. #endif
  6486. }
  6487. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6488. callback_functions* server_cb)
  6489. {
  6490. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6491. }
  6492. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6493. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6494. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6495. void* server_args)
  6496. {
  6497. SOCKET_T sockfd = 0;
  6498. callback_functions* cbf;
  6499. WOLFSSL_CTX* ctx = 0;
  6500. WOLFSSL* ssl = 0;
  6501. WOLFSSL_SESSION* session = NULL;
  6502. char msg[64] = "hello wolfssl!";
  6503. char reply[1024];
  6504. int input;
  6505. int msgSz = (int)XSTRLEN(msg);
  6506. int ret, err = 0;
  6507. int sharedCtx = 0;
  6508. #ifdef WOLFSSL_TIRTOS
  6509. fdOpenSession(Task_self());
  6510. #endif
  6511. ((func_args*)args)->return_code = TEST_FAIL;
  6512. cbf = ((func_args*)args)->callbacks;
  6513. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6514. if (cbf != NULL && cbf->ctx) {
  6515. ctx = cbf->ctx;
  6516. sharedCtx = 1;
  6517. }
  6518. else
  6519. #endif
  6520. {
  6521. WOLFSSL_METHOD* method = NULL;
  6522. if (cbf != NULL && cbf->method != NULL) {
  6523. method = cbf->method();
  6524. }
  6525. else {
  6526. method = wolfSSLv23_client_method();
  6527. }
  6528. ctx = wolfSSL_CTX_new(method);
  6529. }
  6530. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6531. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6532. #endif
  6533. /* Do connect here so server detects failures */
  6534. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6535. 0, 0, NULL);
  6536. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6537. WOLFSSL_SUCCESS) {
  6538. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6539. goto done;
  6540. }
  6541. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6542. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6543. /*err_sys("can't load client cert file, "
  6544. "Please run from wolfSSL home dir");*/
  6545. goto done;
  6546. }
  6547. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6548. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6549. /*err_sys("can't load client key file, "
  6550. "Please run from wolfSSL home dir");*/
  6551. goto done;
  6552. }
  6553. /* call ctx setup callback */
  6554. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6555. cbf->ctx_ready(ctx);
  6556. }
  6557. ssl = wolfSSL_new(ctx);
  6558. if (ssl == NULL) {
  6559. goto done;
  6560. }
  6561. /* keep handshake resources for re-using WOLFSSL obj */
  6562. wolfSSL_KeepArrays(ssl);
  6563. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6564. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6565. goto done;
  6566. }
  6567. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6568. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6569. /*err_sys("can't load client cert file, "
  6570. "Please run from wolfSSL home dir");*/
  6571. goto done;
  6572. }
  6573. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6574. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6575. /*err_sys("can't load client key file, "
  6576. "Please run from wolfSSL home dir");*/
  6577. goto done;
  6578. }
  6579. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6580. /*err_sys("SSL_set_fd failed");*/
  6581. goto done;
  6582. }
  6583. /* call ssl setup callback */
  6584. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6585. cbf->ssl_ready(ssl);
  6586. }
  6587. #ifdef WOLFSSL_ASYNC_CRYPT
  6588. err = 0; /* Reset error */
  6589. #endif
  6590. do {
  6591. #ifdef WOLFSSL_ASYNC_CRYPT
  6592. if (err == WC_PENDING_E) {
  6593. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6594. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6595. }
  6596. #endif
  6597. ret = wolfSSL_connect(ssl);
  6598. err = wolfSSL_get_error(ssl, 0);
  6599. } while (err == WC_PENDING_E);
  6600. if (ret != WOLFSSL_SUCCESS) {
  6601. char buff[WOLFSSL_MAX_ERROR_SZ];
  6602. fprintf(stderr, "error = %d, %s\n", err,
  6603. wolfSSL_ERR_error_string(err, buff));
  6604. /*err_sys("SSL_connect failed");*/
  6605. goto done;
  6606. }
  6607. /* Build first session */
  6608. if (cb != NULL)
  6609. cb(ctx, ssl);
  6610. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6611. /*err_sys("SSL_write failed");*/
  6612. goto done;
  6613. }
  6614. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6615. if (input > 0) {
  6616. reply[input] = '\0';
  6617. fprintf(stderr, "Server response: %s\n", reply);
  6618. }
  6619. /* Session Resumption by re-using WOLFSSL object */
  6620. wolfSSL_set_quiet_shutdown(ssl, 1);
  6621. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6622. /* err_sys ("SSL shutdown failed"); */
  6623. goto done;
  6624. }
  6625. session = wolfSSL_get1_session(ssl);
  6626. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6627. wolfSSL_SESSION_free(session);
  6628. /* err_sys ("SSL_clear failed"); */
  6629. goto done;
  6630. }
  6631. wolfSSL_set_session(ssl, session);
  6632. wolfSSL_SESSION_free(session);
  6633. session = NULL;
  6634. /* close socket once */
  6635. CloseSocket(sockfd);
  6636. sockfd = 0;
  6637. /* wait until server ready */
  6638. wait_tcp_ready((func_args*)server_args);
  6639. fprintf(stderr, "session resumption\n");
  6640. /* Do re-connect */
  6641. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6642. 0, 0, NULL);
  6643. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6644. /*err_sys("SSL_set_fd failed");*/
  6645. goto done;
  6646. }
  6647. #ifdef WOLFSSL_ASYNC_CRYPT
  6648. err = 0; /* Reset error */
  6649. #endif
  6650. do {
  6651. #ifdef WOLFSSL_ASYNC_CRYPT
  6652. if (err == WC_PENDING_E) {
  6653. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6654. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6655. }
  6656. #endif
  6657. ret = wolfSSL_connect(ssl);
  6658. err = wolfSSL_get_error(ssl, 0);
  6659. } while (err == WC_PENDING_E);
  6660. if (ret != WOLFSSL_SUCCESS) {
  6661. char buff[WOLFSSL_MAX_ERROR_SZ];
  6662. fprintf(stderr, "error = %d, %s\n", err,
  6663. wolfSSL_ERR_error_string(err, buff));
  6664. /*err_sys("SSL_connect failed");*/
  6665. goto done;
  6666. }
  6667. /* Build first session */
  6668. if (cb != NULL)
  6669. cb(ctx, ssl);
  6670. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6671. /*err_sys("SSL_write failed");*/
  6672. goto done;
  6673. }
  6674. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6675. if (input > 0) {
  6676. reply[input] = '\0';
  6677. fprintf(stderr, "Server response: %s\n", reply);
  6678. }
  6679. ((func_args*)args)->return_code = TEST_SUCCESS;
  6680. done:
  6681. wolfSSL_free(ssl);
  6682. if (!sharedCtx)
  6683. wolfSSL_CTX_free(ctx);
  6684. CloseSocket(sockfd);
  6685. #ifdef WOLFSSL_TIRTOS
  6686. fdCloseSession(Task_self());
  6687. #endif
  6688. return;
  6689. }
  6690. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6691. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  6692. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  6693. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  6694. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  6695. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  6696. #define HAVE_ALPN_PROTOS_SUPPORT
  6697. #endif
  6698. /* Generic TLS client / server with callbacks for API unit tests
  6699. * Used by SNI / ALPN / crypto callback helper functions */
  6700. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  6701. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  6702. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  6703. #define ENABLE_TLS_CALLBACK_TEST
  6704. #endif
  6705. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  6706. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  6707. /* TLS server for API unit testing - generic */
  6708. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  6709. {
  6710. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6711. WOLFSSL_CTX* ctx = NULL;
  6712. WOLFSSL* ssl = NULL;
  6713. SOCKET_T sfd = 0;
  6714. SOCKET_T cfd = 0;
  6715. word16 port;
  6716. char msg[] = "I hear you fa shizzle!";
  6717. int len = (int) XSTRLEN(msg);
  6718. char input[1024];
  6719. int idx;
  6720. int ret, err = 0;
  6721. ((func_args*)args)->return_code = TEST_FAIL;
  6722. #if defined(USE_WINDOWS_API)
  6723. port = ((func_args*)args)->signal->port;
  6724. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6725. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6726. /* Let tcp_listen assign port */
  6727. port = 0;
  6728. #else
  6729. /* Use default port */
  6730. port = wolfSSLPort;
  6731. #endif
  6732. #ifdef WOLFSSL_DTLS
  6733. if (callbacks->method == wolfDTLS_server_method
  6734. #ifdef WOLFSSL_STATIC_MEMORY
  6735. || callbacks->method_ex == wolfDTLS_server_method_ex
  6736. #endif
  6737. #ifndef NO_OLD_TLS
  6738. || callbacks->method == wolfDTLSv1_server_method
  6739. #ifdef WOLFSSL_STATIC_MEMORY
  6740. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  6741. #endif
  6742. #endif
  6743. #ifndef WOLFSSL_NO_TLS12
  6744. || callbacks->method == wolfDTLSv1_2_server_method
  6745. #ifdef WOLFSSL_STATIC_MEMORY
  6746. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  6747. #endif
  6748. #endif
  6749. #ifdef WOLFSSL_DTLS13
  6750. || callbacks->method == wolfDTLSv1_3_server_method
  6751. #ifdef WOLFSSL_STATIC_MEMORY
  6752. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  6753. #endif
  6754. #endif
  6755. ) {
  6756. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  6757. }
  6758. else
  6759. #endif
  6760. {
  6761. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6762. }
  6763. #ifdef WOLFSSL_STATIC_MEMORY
  6764. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6765. callbacks->memSz > 0) {
  6766. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6767. callbacks->mem, callbacks->memSz, 0, 1);
  6768. if (ret != WOLFSSL_SUCCESS) {
  6769. fprintf(stderr, "CTX static new failed %d\n", ret);
  6770. goto cleanup;
  6771. }
  6772. }
  6773. #else
  6774. ctx = wolfSSL_CTX_new(callbacks->method());
  6775. #endif
  6776. if (ctx == NULL) {
  6777. fprintf(stderr, "CTX new failed\n");
  6778. goto cleanup;
  6779. }
  6780. /* set defaults */
  6781. if (callbacks->caPemFile == NULL)
  6782. callbacks->caPemFile = cliCertFile;
  6783. if (callbacks->certPemFile == NULL)
  6784. callbacks->certPemFile = svrCertFile;
  6785. if (callbacks->keyPemFile == NULL)
  6786. callbacks->keyPemFile = svrKeyFile;
  6787. #ifdef WOLFSSL_TIRTOS
  6788. fdOpenSession(Task_self());
  6789. #endif
  6790. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6791. wolfSSL_CTX_set_verify(ctx,
  6792. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6793. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6794. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6795. #endif
  6796. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  6797. if (callbacks->method == wolfDTLSv1_2_server_method) {
  6798. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  6799. goto cleanup;
  6800. }
  6801. #endif
  6802. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6803. WOLFSSL_SUCCESS) {
  6804. goto cleanup;
  6805. }
  6806. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6807. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6808. goto cleanup;
  6809. }
  6810. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6811. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6812. goto cleanup;
  6813. }
  6814. #ifdef HAVE_CRL
  6815. if (callbacks->crlPemFile != NULL) {
  6816. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6817. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6818. goto cleanup;
  6819. }
  6820. }
  6821. #endif
  6822. if (callbacks->ctx_ready)
  6823. callbacks->ctx_ready(ctx);
  6824. ssl = wolfSSL_new(ctx);
  6825. if (ssl == NULL) {
  6826. fprintf(stderr, "SSL new failed\n");
  6827. goto cleanup;
  6828. }
  6829. if (wolfSSL_dtls(ssl)) {
  6830. SOCKADDR_IN_T cliAddr;
  6831. socklen_t cliLen;
  6832. cliLen = sizeof(cliAddr);
  6833. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  6834. (struct sockaddr*)&cliAddr, &cliLen);
  6835. if (idx <= 0) {
  6836. goto cleanup;
  6837. }
  6838. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6839. }
  6840. else {
  6841. CloseSocket(sfd);
  6842. }
  6843. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  6844. goto cleanup;
  6845. }
  6846. if (callbacks->loadToSSL) {
  6847. wolfSSL_SetDevId(ssl, callbacks->devId);
  6848. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6849. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6850. goto cleanup;
  6851. }
  6852. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6853. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6854. goto cleanup;
  6855. }
  6856. }
  6857. #ifdef NO_PSK
  6858. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6859. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6860. #elif !defined(NO_DH)
  6861. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6862. #endif
  6863. #endif
  6864. if (callbacks->ssl_ready)
  6865. callbacks->ssl_ready(ssl);
  6866. #ifdef WOLFSSL_ASYNC_CRYPT
  6867. err = 0; /* Reset error */
  6868. #endif
  6869. do {
  6870. #ifdef WOLFSSL_ASYNC_CRYPT
  6871. if (err == WC_PENDING_E) {
  6872. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6873. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6874. }
  6875. #endif
  6876. ret = wolfSSL_accept(ssl);
  6877. err = wolfSSL_get_error(ssl, ret);
  6878. } while (err == WC_PENDING_E);
  6879. if (ret != WOLFSSL_SUCCESS) {
  6880. char buff[WOLFSSL_MAX_ERROR_SZ];
  6881. fprintf(stderr, "accept error = %d, %s\n", err,
  6882. wolfSSL_ERR_error_string(err, buff));
  6883. /*err_sys("SSL_accept failed");*/
  6884. }
  6885. else {
  6886. #ifdef WOLFSSL_ASYNC_CRYPT
  6887. err = 0; /* Reset error */
  6888. #endif
  6889. do {
  6890. #ifdef WOLFSSL_ASYNC_CRYPT
  6891. if (err == WC_PENDING_E) {
  6892. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6893. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6894. }
  6895. #endif
  6896. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6897. err = wolfSSL_get_error(ssl, idx);
  6898. } while (err == WC_PENDING_E);
  6899. if (idx > 0) {
  6900. input[idx] = 0;
  6901. fprintf(stderr, "Client message: %s\n", input);
  6902. }
  6903. #ifdef WOLFSSL_ASYNC_CRYPT
  6904. err = 0; /* Reset error */
  6905. #endif
  6906. do {
  6907. #ifdef WOLFSSL_ASYNC_CRYPT
  6908. if (err == WC_PENDING_E) {
  6909. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6910. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6911. }
  6912. #endif
  6913. ret = wolfSSL_write(ssl, msg, len);
  6914. err = wolfSSL_get_error(ssl, ret);
  6915. } while (err == WC_PENDING_E);
  6916. if (len != ret) {
  6917. goto cleanup;
  6918. }
  6919. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6920. defined(WOLFSSL_DTLS)
  6921. if (wolfSSL_dtls(ssl)) {
  6922. byte* import;
  6923. word32 sz;
  6924. wolfSSL_dtls_export(ssl, NULL, &sz);
  6925. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6926. if (import == NULL) {
  6927. goto cleanup;
  6928. }
  6929. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6930. if (idx < 0) {
  6931. goto cleanup;
  6932. }
  6933. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6934. goto cleanup;
  6935. }
  6936. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6937. }
  6938. #endif
  6939. #ifdef WOLFSSL_TIRTOS
  6940. Task_yield();
  6941. #endif
  6942. ((func_args*)args)->return_code = TEST_SUCCESS;
  6943. }
  6944. if (callbacks->on_result)
  6945. callbacks->on_result(ssl);
  6946. wolfSSL_shutdown(ssl);
  6947. cleanup:
  6948. wolfSSL_free(ssl);
  6949. wolfSSL_CTX_free(ctx);
  6950. CloseSocket(cfd);
  6951. #ifdef WOLFSSL_TIRTOS
  6952. fdCloseSession(Task_self());
  6953. #endif
  6954. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6955. && defined(HAVE_THREAD_LS)
  6956. wc_ecc_fp_free(); /* free per thread cache */
  6957. #endif
  6958. WOLFSSL_RETURN_FROM_THREAD(0);
  6959. }
  6960. /* TLS Client for API unit testing - generic */
  6961. static void run_wolfssl_client(void* args)
  6962. {
  6963. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6964. WOLFSSL_CTX* ctx = NULL;
  6965. WOLFSSL* ssl = NULL;
  6966. SOCKET_T sfd = 0;
  6967. char msg[] = "hello wolfssl server!";
  6968. int len = (int) XSTRLEN(msg);
  6969. char input[1024];
  6970. int ret, err = 0;
  6971. ((func_args*)args)->return_code = TEST_FAIL;
  6972. /* set defaults */
  6973. if (callbacks->caPemFile == NULL)
  6974. callbacks->caPemFile = caCertFile;
  6975. if (callbacks->certPemFile == NULL)
  6976. callbacks->certPemFile = cliCertFile;
  6977. if (callbacks->keyPemFile == NULL)
  6978. callbacks->keyPemFile = cliKeyFile;
  6979. #ifdef WOLFSSL_STATIC_MEMORY
  6980. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6981. callbacks->memSz > 0) {
  6982. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6983. callbacks->mem, callbacks->memSz, 0, 1);
  6984. if (ret != WOLFSSL_SUCCESS) {
  6985. fprintf(stderr, "CTX static new failed %d\n", ret);
  6986. goto cleanup;
  6987. }
  6988. }
  6989. #else
  6990. ctx = wolfSSL_CTX_new(callbacks->method());
  6991. #endif
  6992. if (ctx == NULL) {
  6993. fprintf(stderr, "CTX new failed\n");
  6994. goto cleanup;
  6995. }
  6996. #ifdef WOLFSSL_TIRTOS
  6997. fdOpenSession(Task_self());
  6998. #endif
  6999. if (!callbacks->loadToSSL) {
  7000. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7001. }
  7002. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7003. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7004. #endif
  7005. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7006. WOLFSSL_SUCCESS) {
  7007. goto cleanup;
  7008. }
  7009. if (!callbacks->loadToSSL) {
  7010. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7011. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7012. goto cleanup;
  7013. }
  7014. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7015. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7016. goto cleanup;
  7017. }
  7018. }
  7019. #ifdef HAVE_CRL
  7020. if (callbacks->crlPemFile != NULL) {
  7021. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7022. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7023. goto cleanup;
  7024. }
  7025. }
  7026. #endif
  7027. if (callbacks->ctx_ready)
  7028. callbacks->ctx_ready(ctx);
  7029. ssl = wolfSSL_new(ctx);
  7030. if (wolfSSL_dtls(ssl)) {
  7031. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7032. 1, 0, ssl);
  7033. }
  7034. else {
  7035. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7036. 0, 0, ssl);
  7037. }
  7038. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7039. goto cleanup;
  7040. }
  7041. if (callbacks->loadToSSL) {
  7042. wolfSSL_SetDevId(ssl, callbacks->devId);
  7043. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7044. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7045. goto cleanup;
  7046. }
  7047. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7048. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7049. goto cleanup;
  7050. }
  7051. }
  7052. if (callbacks->ssl_ready)
  7053. callbacks->ssl_ready(ssl);
  7054. #ifdef WOLFSSL_ASYNC_CRYPT
  7055. err = 0; /* Reset error */
  7056. #endif
  7057. do {
  7058. #ifdef WOLFSSL_ASYNC_CRYPT
  7059. if (err == WC_PENDING_E) {
  7060. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7061. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7062. }
  7063. #endif
  7064. ret = wolfSSL_connect(ssl);
  7065. err = wolfSSL_get_error(ssl, ret);
  7066. } while (err == WC_PENDING_E);
  7067. if (ret != WOLFSSL_SUCCESS) {
  7068. char buff[WOLFSSL_MAX_ERROR_SZ];
  7069. fprintf(stderr, "error = %d, %s\n", err,
  7070. wolfSSL_ERR_error_string(err, buff));
  7071. /*err_sys("SSL_connect failed");*/
  7072. }
  7073. else {
  7074. #ifdef WOLFSSL_ASYNC_CRYPT
  7075. err = 0; /* Reset error */
  7076. #endif
  7077. do {
  7078. #ifdef WOLFSSL_ASYNC_CRYPT
  7079. if (err == WC_PENDING_E) {
  7080. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7081. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7082. }
  7083. #endif
  7084. ret = wolfSSL_write(ssl, msg, len);
  7085. err = wolfSSL_get_error(ssl, ret);
  7086. } while (err == WC_PENDING_E);
  7087. if (len != ret)
  7088. goto cleanup;
  7089. #ifdef WOLFSSL_ASYNC_CRYPT
  7090. err = 0; /* Reset error */
  7091. #endif
  7092. do {
  7093. #ifdef WOLFSSL_ASYNC_CRYPT
  7094. if (err == WC_PENDING_E) {
  7095. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7096. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7097. }
  7098. #endif
  7099. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7100. err = wolfSSL_get_error(ssl, ret);
  7101. } while (err == WC_PENDING_E);
  7102. if (ret > 0) {
  7103. input[ret] = '\0'; /* null term */
  7104. fprintf(stderr, "Server response: %s\n", input);
  7105. }
  7106. ((func_args*)args)->return_code = TEST_SUCCESS;
  7107. }
  7108. if (callbacks->on_result)
  7109. callbacks->on_result(ssl);
  7110. cleanup:
  7111. wolfSSL_free(ssl);
  7112. wolfSSL_CTX_free(ctx);
  7113. CloseSocket(sfd);
  7114. #ifdef WOLFSSL_TIRTOS
  7115. fdCloseSession(Task_self());
  7116. #endif
  7117. }
  7118. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7119. static int test_wolfSSL_read_write(void)
  7120. {
  7121. /* The unit testing for read and write shall happen simultaneously, since
  7122. * one can't do anything with one without the other. (Except for a failure
  7123. * test case.) This function will call all the others that will set up,
  7124. * execute, and report their test findings.
  7125. *
  7126. * Set up the success case first. This function will become the template
  7127. * for the other tests. This should eventually be renamed
  7128. *
  7129. * The success case isn't interesting, how can this fail?
  7130. * - Do not give the client context a CA certificate. The connect should
  7131. * fail. Do not need server for this?
  7132. * - Using NULL for the ssl object on server. Do not need client for this.
  7133. * - Using NULL for the ssl object on client. Do not need server for this.
  7134. * - Good ssl objects for client and server. Client write() without server
  7135. * read().
  7136. * - Good ssl objects for client and server. Server write() without client
  7137. * read().
  7138. * - Forgetting the password callback?
  7139. */
  7140. tcp_ready ready;
  7141. func_args client_args;
  7142. func_args server_args;
  7143. THREAD_TYPE serverThread;
  7144. EXPECT_DECLS;
  7145. XMEMSET(&client_args, 0, sizeof(func_args));
  7146. XMEMSET(&server_args, 0, sizeof(func_args));
  7147. #ifdef WOLFSSL_TIRTOS
  7148. fdOpenSession(Task_self());
  7149. #endif
  7150. StartTCP();
  7151. InitTcpReady(&ready);
  7152. #if defined(USE_WINDOWS_API)
  7153. /* use RNG to get random port if using windows */
  7154. ready.port = GetRandomPort();
  7155. #endif
  7156. server_args.signal = &ready;
  7157. client_args.signal = &ready;
  7158. start_thread(test_server_nofail, &server_args, &serverThread);
  7159. wait_tcp_ready(&server_args);
  7160. test_client_nofail(&client_args, NULL);
  7161. join_thread(serverThread);
  7162. ExpectTrue(client_args.return_code);
  7163. ExpectTrue(server_args.return_code);
  7164. FreeTcpReady(&ready);
  7165. #ifdef WOLFSSL_TIRTOS
  7166. fdOpenSession(Task_self());
  7167. #endif
  7168. return EXPECT_RESULT();
  7169. }
  7170. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7171. {
  7172. EXPECT_DECLS;
  7173. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7174. !defined(WOLFSSL_NO_TLS12)
  7175. /* The unit test for session resumption by re-using WOLFSSL object.
  7176. * WOLFSSL object is not cleared after first session. It reuse the object
  7177. * for second connection.
  7178. */
  7179. tcp_ready ready;
  7180. func_args client_args;
  7181. func_args server_args;
  7182. THREAD_TYPE serverThread;
  7183. callback_functions client_cbf;
  7184. callback_functions server_cbf;
  7185. XMEMSET(&client_args, 0, sizeof(func_args));
  7186. XMEMSET(&server_args, 0, sizeof(func_args));
  7187. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7188. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7189. #ifdef WOLFSSL_TIRTOS
  7190. fdOpenSession(Task_self());
  7191. #endif
  7192. StartTCP();
  7193. InitTcpReady(&ready);
  7194. #if defined(USE_WINDOWS_API)
  7195. /* use RNG to get random port if using windows */
  7196. ready.port = GetRandomPort();
  7197. #endif
  7198. client_cbf.method = wolfTLSv1_2_client_method;
  7199. server_cbf.method = wolfTLSv1_2_server_method;
  7200. client_args.callbacks = &client_cbf;
  7201. server_args.callbacks = &server_cbf;
  7202. server_args.signal = &ready;
  7203. client_args.signal = &ready;
  7204. /* the var is used for loop number */
  7205. server_args.argc = 2;
  7206. start_thread(test_server_loop, &server_args, &serverThread);
  7207. wait_tcp_ready(&server_args);
  7208. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7209. join_thread(serverThread);
  7210. ExpectTrue(client_args.return_code);
  7211. ExpectTrue(server_args.return_code);
  7212. FreeTcpReady(&ready);
  7213. #ifdef WOLFSSL_TIRTOS
  7214. fdOpenSession(Task_self());
  7215. #endif
  7216. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7217. * !defined(WOLFSSL_TLS13) */
  7218. return EXPECT_RESULT();
  7219. }
  7220. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7221. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7222. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7223. WOLFSSL_CTX* ctx)
  7224. {
  7225. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7226. myVerifyAction = VERIFY_USE_PREVERFIY;
  7227. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7228. return TEST_SUCCESS;
  7229. }
  7230. #endif
  7231. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7232. {
  7233. EXPECT_DECLS;
  7234. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7235. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7236. test_ssl_cbf client_cbf;
  7237. test_ssl_cbf server_cbf;
  7238. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7239. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7240. #ifdef WOLFSSL_TLS13
  7241. client_cbf.method = wolfTLSv1_3_client_method;
  7242. #endif /* WOLFSSL_TLS13 */
  7243. client_cbf.ctx_ready =
  7244. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7245. /* test case 1 verify depth is equal to peer chain */
  7246. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7247. &server_cbf, NULL), TEST_SUCCESS);
  7248. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7249. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7250. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7251. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7252. return EXPECT_RESULT();
  7253. }
  7254. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7255. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7256. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7257. WOLFSSL_CTX* ctx)
  7258. {
  7259. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7260. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7261. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7262. return TEST_SUCCESS;
  7263. }
  7264. #endif
  7265. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7266. {
  7267. EXPECT_DECLS;
  7268. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7269. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7270. test_ssl_cbf client_cbf;
  7271. test_ssl_cbf server_cbf;
  7272. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7273. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7274. #ifdef WOLFSSL_TLS13
  7275. client_cbf.method = wolfTLSv1_3_client_method;
  7276. #endif /* WOLFSSL_TLS13 */
  7277. client_cbf.ctx_ready =
  7278. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7279. /* test case 2
  7280. * verify depth is zero, number of peer's chain is 2.
  7281. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7282. * callback.
  7283. */
  7284. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7285. &server_cbf, NULL), TEST_SUCCESS);
  7286. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7287. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7288. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7289. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7290. return EXPECT_RESULT();
  7291. }
  7292. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7293. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7294. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7295. WOLFSSL_CTX* ctx)
  7296. {
  7297. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7298. myVerifyAction = VERIFY_USE_PREVERFIY;
  7299. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7300. return TEST_SUCCESS;
  7301. }
  7302. #endif
  7303. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7304. {
  7305. EXPECT_DECLS;
  7306. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7307. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7308. test_ssl_cbf client_cbf;
  7309. test_ssl_cbf server_cbf;
  7310. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7311. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7312. #ifdef WOLFSSL_TLS13
  7313. client_cbf.method = wolfTLSv1_3_client_method;
  7314. #endif /* WOLFSSL_TLS13 */
  7315. client_cbf.ctx_ready =
  7316. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7317. /* test case 3
  7318. * verify depth is zero, number of peer's chain is 2
  7319. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7320. * therefore, handshake becomes failure.
  7321. */
  7322. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7323. &server_cbf, NULL), TEST_FAIL);
  7324. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7325. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7326. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7327. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7328. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7329. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7330. return EXPECT_RESULT();
  7331. }
  7332. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7333. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7334. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7335. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7336. {
  7337. EXPECT_DECLS;
  7338. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7339. return EXPECT_RESULT();
  7340. }
  7341. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7342. {
  7343. EXPECT_DECLS;
  7344. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7345. return EXPECT_RESULT();
  7346. }
  7347. #endif
  7348. static int test_wolfSSL_CTX_set_cipher_list(void)
  7349. {
  7350. EXPECT_DECLS;
  7351. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7352. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7353. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7354. WOLFSSL_CTX* ctxClient = NULL;
  7355. WOLFSSL* sslClient = NULL;
  7356. test_ssl_cbf client_cbf;
  7357. test_ssl_cbf server_cbf;
  7358. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7359. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7360. server_cbf.method = wolfTLSv1_2_server_method;
  7361. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7362. client_cbf.method = wolfTLSv1_2_client_method;
  7363. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7364. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7365. &server_cbf, NULL), TEST_SUCCESS);
  7366. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7367. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7368. /* check with cipher string that has '+' */
  7369. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7370. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7371. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7372. /* check for the existence of an ECDHE ECDSA cipher suite */
  7373. if (EXPECT_SUCCESS()) {
  7374. int i = 0;
  7375. int found = 0;
  7376. const char* suite;
  7377. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7378. WOLFSSL_CIPHER* current;
  7379. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7380. do {
  7381. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7382. if (current) {
  7383. suite = wolfSSL_CIPHER_get_name(current);
  7384. if (suite && XSTRSTR(suite, "ECDSA")) {
  7385. found = 1;
  7386. break;
  7387. }
  7388. }
  7389. } while (current);
  7390. ExpectIntEQ(found, 1);
  7391. }
  7392. wolfSSL_free(sslClient);
  7393. wolfSSL_CTX_free(ctxClient);
  7394. #endif
  7395. return EXPECT_RESULT();
  7396. }
  7397. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7398. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7399. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7400. WOLFSSL* ssl)
  7401. {
  7402. EXPECT_DECLS;
  7403. size_t msg_len;
  7404. (void)ctx;
  7405. /* get_finished test */
  7406. /* 1. get own sent message */
  7407. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7408. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7409. ExpectIntGE(msg_len, 0);
  7410. /* 2. get peer message */
  7411. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7412. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7413. ExpectIntGE(msg_len, 0);
  7414. return EXPECT_RESULT();
  7415. }
  7416. #endif
  7417. static int test_wolfSSL_get_finished(void)
  7418. {
  7419. EXPECT_DECLS;
  7420. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7421. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7422. test_ssl_cbf client_cbf;
  7423. test_ssl_cbf server_cbf;
  7424. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7425. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7426. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7427. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7428. TEST_SUCCESS);
  7429. /* test received msg vs sent msg */
  7430. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7431. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7432. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7433. return EXPECT_RESULT();
  7434. }
  7435. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7436. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7437. !defined(NO_SESSION_CACHE)
  7438. /* Sessions to restore/store */
  7439. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7440. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7441. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7442. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7443. {
  7444. /* Don't store sessions. Lookup is still enabled. */
  7445. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7446. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7447. #ifdef OPENSSL_EXTRA
  7448. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7449. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7450. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7451. #endif
  7452. /* Require both peers to provide certs */
  7453. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7454. }
  7455. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7456. {
  7457. WOLFSSL_SESSION** sess;
  7458. if (wolfSSL_is_server(ssl))
  7459. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7460. else
  7461. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7462. if (*sess == NULL) {
  7463. #ifdef NO_SESSION_CACHE_REF
  7464. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7465. #else
  7466. /* Test for backwards compatibility */
  7467. if (wolfSSL_is_server(ssl)) {
  7468. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7469. }
  7470. else {
  7471. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7472. }
  7473. #endif
  7474. /* Now save the session in the internal store to make it available
  7475. * for lookup. For TLS 1.3, we can't save the session without
  7476. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7477. * session from cache. */
  7478. if (wolfSSL_is_server(ssl)
  7479. #ifndef WOLFSSL_TICKET_HAVE_ID
  7480. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7481. #endif
  7482. )
  7483. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7484. *sess), WOLFSSL_SUCCESS);
  7485. }
  7486. else {
  7487. /* If we have a session retrieved then remaining connections should be
  7488. * resuming on that session */
  7489. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7490. }
  7491. /* Save CTX to be able to decrypt tickets */
  7492. if (wolfSSL_is_server(ssl) &&
  7493. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7494. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7495. = wolfSSL_get_SSL_CTX(ssl));
  7496. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7497. WOLFSSL_SUCCESS);
  7498. }
  7499. #ifdef SESSION_CERTS
  7500. #ifndef WOLFSSL_TICKET_HAVE_ID
  7501. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7502. wolfSSL_session_reused(ssl))
  7503. #endif
  7504. {
  7505. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7506. * for all connections. TLS 1.3 only has tickets so if we don't
  7507. * include the session id in the ticket then the certificates
  7508. * will not be available on resumption. */
  7509. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7510. AssertNotNull(peer);
  7511. wolfSSL_X509_free(peer);
  7512. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7513. #ifdef OPENSSL_EXTRA
  7514. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7515. #endif
  7516. }
  7517. #endif /* SESSION_CERTS */
  7518. }
  7519. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7520. {
  7521. /* Set the session to reuse for the client */
  7522. AssertIntEQ(wolfSSL_set_session(ssl,
  7523. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7524. }
  7525. #endif
  7526. static int test_wolfSSL_CTX_add_session(void)
  7527. {
  7528. EXPECT_DECLS;
  7529. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7530. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7531. !defined(NO_SESSION_CACHE)
  7532. tcp_ready ready;
  7533. func_args client_args;
  7534. func_args server_args;
  7535. THREAD_TYPE serverThread;
  7536. callback_functions client_cb;
  7537. callback_functions server_cb;
  7538. method_provider methods[][2] = {
  7539. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7540. !defined(NO_DES3))
  7541. /* Without AES there are almost no ciphersuites available. This leads
  7542. * to no ciphersuites being available and an error. */
  7543. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7544. #endif
  7545. #ifndef WOLFSSL_NO_TLS12
  7546. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7547. #endif
  7548. /* Needs the default ticket callback since it is tied to the
  7549. * connection context and this makes it easy to carry over the ticket
  7550. * crypto context between connections */
  7551. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7552. defined(HAVE_SESSION_TICKET)
  7553. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7554. #endif
  7555. };
  7556. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7557. size_t i, j;
  7558. for (i = 0; i < methodsLen; i++) {
  7559. /* First run creates a connection while the second+ run will attempt
  7560. * to resume the connection. The trick is that the internal cache
  7561. * is turned off. wolfSSL_CTX_add_session should put the session in
  7562. * the cache anyway. */
  7563. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7564. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7565. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7566. #ifdef NO_SESSION_CACHE_REF
  7567. for (j = 0; j < 4; j++) {
  7568. #else
  7569. /* The session may be overwritten in this case. Do only one resumption
  7570. * to stop this test from failing intermittently. */
  7571. for (j = 0; j < 2; j++) {
  7572. #endif
  7573. #ifdef WOLFSSL_TIRTOS
  7574. fdOpenSession(Task_self());
  7575. #endif
  7576. StartTCP();
  7577. InitTcpReady(&ready);
  7578. XMEMSET(&client_args, 0, sizeof(func_args));
  7579. XMEMSET(&server_args, 0, sizeof(func_args));
  7580. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7581. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7582. client_cb.method = methods[i][0];
  7583. server_cb.method = methods[i][1];
  7584. server_args.signal = &ready;
  7585. server_args.callbacks = &server_cb;
  7586. client_args.signal = &ready;
  7587. client_args.callbacks = &client_cb;
  7588. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7589. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7590. server_cb.isSharedCtx = 1;
  7591. }
  7592. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7593. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7594. if (j != 0)
  7595. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7596. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7597. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7598. server_cb.ticNoInit = 1; /* Use default builtin */
  7599. start_thread(test_server_nofail, &server_args, &serverThread);
  7600. wait_tcp_ready(&server_args);
  7601. test_client_nofail(&client_args, NULL);
  7602. join_thread(serverThread);
  7603. ExpectTrue(client_args.return_code);
  7604. ExpectTrue(server_args.return_code);
  7605. FreeTcpReady(&ready);
  7606. if (EXPECT_FAIL())
  7607. break;
  7608. }
  7609. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7610. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7611. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7612. if (EXPECT_FAIL())
  7613. break;
  7614. }
  7615. #endif
  7616. return EXPECT_RESULT();
  7617. }
  7618. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7619. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7620. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7621. defined(HAVE_SESSION_TICKET) && \
  7622. !defined(TITAN_SESSION_CACHE) && \
  7623. !defined(HUGE_SESSION_CACHE) && \
  7624. !defined(BIG_SESSION_CACHE) && \
  7625. !defined(MEDIUM_SESSION_CACHE)
  7626. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7627. /* Sessions to restore/store */
  7628. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7629. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7630. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7631. static int twcase_new_session_called = 0;
  7632. static int twcase_remove_session_called = 0;
  7633. static int twcase_get_session_called = 0;
  7634. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7635. #define SESSION_CACHE_SIZE 33
  7636. typedef struct {
  7637. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7638. WOLFSSL_SESSION* value;
  7639. } hashTable_entry;
  7640. typedef struct {
  7641. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7642. size_t capacity; /* size of entries */
  7643. size_t length; /* number of items in the hash table */
  7644. wolfSSL_Mutex htLock; /* lock */
  7645. }hashTable;
  7646. static hashTable server_sessionCache;
  7647. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7648. {
  7649. int i;
  7650. unsigned int len;
  7651. (void)ssl;
  7652. /*
  7653. * This example uses a hash table.
  7654. * Steps you should take for a non-demo code:
  7655. * - acquire a lock for the file named according to the session id
  7656. * - open the file
  7657. * - encrypt and write the SSL_SESSION object to the file
  7658. * - release the lock
  7659. *
  7660. * Return:
  7661. * 0: The callback does not wish to hold a reference of the sess
  7662. * 1: The callback wants to hold a reference of the sess. The callback is
  7663. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7664. */
  7665. if (sess == NULL)
  7666. return 0;
  7667. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7668. return 0;
  7669. }
  7670. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7671. if (server_sessionCache.entries[i].value == NULL) {
  7672. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7673. server_sessionCache.entries[i].value = sess;
  7674. server_sessionCache.length++;
  7675. break;
  7676. }
  7677. }
  7678. ++twcase_new_session_called;
  7679. wc_UnLockMutex(&server_sessionCache.htLock);
  7680. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  7681. twcase_new_session_called);
  7682. return 1;
  7683. }
  7684. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  7685. {
  7686. int i;
  7687. (void)ctx;
  7688. (void)sess;
  7689. if (sess == NULL)
  7690. return;
  7691. /*
  7692. * This example uses a hash table.
  7693. * Steps you should take for a non-demo code:
  7694. * - acquire a lock for the file named according to the session id
  7695. * - remove the file
  7696. * - release the lock
  7697. */
  7698. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7699. return;
  7700. }
  7701. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7702. if (server_sessionCache.entries[i].key != NULL &&
  7703. XMEMCMP(server_sessionCache.entries[i].key,
  7704. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7705. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7706. server_sessionCache.entries[i].value = NULL;
  7707. server_sessionCache.entries[i].key = NULL;
  7708. server_sessionCache.length--;
  7709. break;
  7710. }
  7711. }
  7712. ++twcase_remove_session_called;
  7713. wc_UnLockMutex(&server_sessionCache.htLock);
  7714. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  7715. twcase_remove_session_called);
  7716. }
  7717. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  7718. const unsigned char *id, int len, int *ref)
  7719. {
  7720. int i;
  7721. (void)ssl;
  7722. (void)id;
  7723. (void)len;
  7724. /*
  7725. * This example uses a hash table.
  7726. * Steps you should take for a non-demo code:
  7727. * - acquire a lock for the file named according to the session id in the
  7728. * 2nd arg
  7729. * - read and decrypt contents of file and create a new SSL_SESSION
  7730. * - object release the lock
  7731. * - return the new session object
  7732. */
  7733. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  7734. ++twcase_get_session_called);
  7735. /* This callback want to retain a copy of the object. If we want wolfSSL to
  7736. * be responsible for the pointer then set to 0. */
  7737. *ref = 1;
  7738. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7739. if (server_sessionCache.entries[i].key != NULL &&
  7740. XMEMCMP(server_sessionCache.entries[i].key, id,
  7741. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7742. return server_sessionCache.entries[i].value;
  7743. }
  7744. }
  7745. return NULL;
  7746. }
  7747. static int twcase_get_sessionCb_cleanup(void)
  7748. {
  7749. int i;
  7750. int cnt = 0;
  7751. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  7752. * for freeing sessions */
  7753. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7754. if (server_sessionCache.entries[i].value != NULL) {
  7755. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7756. cnt++;
  7757. }
  7758. }
  7759. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  7760. cnt);
  7761. return TEST_SUCCESS;
  7762. }
  7763. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  7764. {
  7765. EXPECT_DECLS;
  7766. /* off - Disable internal cache */
  7767. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7768. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7769. #ifdef OPENSSL_EXTRA
  7770. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7771. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7772. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7773. #endif
  7774. /* off - Do not setup external cache */
  7775. /* Require both peers to provide certs */
  7776. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7777. return EXPECT_RESULT();
  7778. }
  7779. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  7780. {
  7781. /* on - internal cache is on by default */
  7782. /* off - Do not setup external cache */
  7783. /* Require both peers to provide certs */
  7784. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7785. return TEST_SUCCESS;
  7786. }
  7787. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  7788. {
  7789. EXPECT_DECLS;
  7790. /* off - Disable internal cache */
  7791. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7792. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7793. #ifdef OPENSSL_EXTRA
  7794. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7795. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7796. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7797. #endif
  7798. /* on - Enable external cache */
  7799. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7800. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7801. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7802. /* Require both peers to provide certs */
  7803. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7804. return EXPECT_RESULT();
  7805. }
  7806. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  7807. {
  7808. /* on - internal cache is on by default */
  7809. /* on - Enable external cache */
  7810. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7811. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7812. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7813. /* Require both peers to provide certs */
  7814. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7815. return TEST_SUCCESS;
  7816. }
  7817. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  7818. {
  7819. /* on - internal cache is on by default */
  7820. /* on - Enable external cache */
  7821. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7822. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7823. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7824. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  7825. /* Require both peers to provide certs */
  7826. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7827. return TEST_SUCCESS;
  7828. }
  7829. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7830. {
  7831. EXPECT_DECLS;
  7832. WOLFSSL_SESSION** sess;
  7833. if (wolfSSL_is_server(ssl))
  7834. sess = &twcase_server_first_session_ptr;
  7835. else
  7836. return TEST_SUCCESS;
  7837. if (*sess == NULL) {
  7838. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7839. /* Now save the session in the internal store to make it available
  7840. * for lookup. For TLS 1.3, we can't save the session without
  7841. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7842. * session from cache. */
  7843. if (wolfSSL_is_server(ssl)
  7844. #ifndef WOLFSSL_TICKET_HAVE_ID
  7845. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7846. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7847. #endif
  7848. ) {
  7849. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7850. *sess), WOLFSSL_SUCCESS);
  7851. }
  7852. }
  7853. /* Save CTX to be able to decrypt tickets */
  7854. if (twcase_server_current_ctx_ptr == NULL) {
  7855. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7856. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7857. WOLFSSL_SUCCESS);
  7858. }
  7859. #ifdef SESSION_CERTS
  7860. #ifndef WOLFSSL_TICKET_HAVE_ID
  7861. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7862. wolfSSL_session_reused(ssl))
  7863. #endif
  7864. {
  7865. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7866. * for all connections. TLS 1.3 only has tickets so if we don't
  7867. * include the session id in the ticket then the certificates
  7868. * will not be available on resumption. */
  7869. WOLFSSL_X509* peer = NULL;
  7870. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7871. wolfSSL_X509_free(peer);
  7872. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7873. }
  7874. #endif
  7875. return EXPECT_RESULT();
  7876. }
  7877. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7878. {
  7879. EXPECT_DECLS;
  7880. WOLFSSL_SESSION** sess;
  7881. sess = &twcase_client_first_session_ptr;
  7882. if (*sess == NULL) {
  7883. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7884. }
  7885. else {
  7886. /* If we have a session retrieved then remaining connections should be
  7887. * resuming on that session */
  7888. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7889. }
  7890. #ifdef SESSION_CERTS
  7891. #ifndef WOLFSSL_TICKET_HAVE_ID
  7892. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7893. wolfSSL_session_reused(ssl))
  7894. #endif
  7895. {
  7896. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7897. ExpectNotNull(peer);
  7898. wolfSSL_X509_free(peer);
  7899. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7900. #ifdef OPENSSL_EXTRA
  7901. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7902. #endif
  7903. }
  7904. #endif
  7905. return EXPECT_RESULT();
  7906. }
  7907. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7908. {
  7909. EXPECT_DECLS;
  7910. /* Set the session to reuse for the client */
  7911. ExpectNotNull(ssl);
  7912. ExpectNotNull(twcase_client_first_session_ptr);
  7913. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7914. WOLFSSL_SUCCESS);
  7915. return EXPECT_RESULT();
  7916. }
  7917. struct test_add_session_ext_params {
  7918. method_provider client_meth;
  7919. method_provider server_meth;
  7920. const char* tls_version;
  7921. };
  7922. static int test_wolfSSL_CTX_add_session_ext(
  7923. struct test_add_session_ext_params* param)
  7924. {
  7925. EXPECT_DECLS;
  7926. /* Test the default 33 sessions */
  7927. int j;
  7928. /* Clear cache before starting */
  7929. wolfSSL_CTX_flush_sessions(NULL, -1);
  7930. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7931. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7932. return BAD_MUTEX_E;
  7933. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7934. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  7935. for (j = 0; j < 5; j++) {
  7936. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  7937. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  7938. test_ssl_cbf client_cb;
  7939. test_ssl_cbf server_cb;
  7940. (void)dtls;
  7941. /* Test five cache configurations */
  7942. twcase_client_first_session_ptr = NULL;
  7943. twcase_server_first_session_ptr = NULL;
  7944. twcase_server_current_ctx_ptr = NULL;
  7945. twcase_new_session_called = 0;
  7946. twcase_remove_session_called = 0;
  7947. twcase_get_session_called = 0;
  7948. /* connection 1 - first connection */
  7949. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  7950. XMEMSET(&client_cb, 0, sizeof(client_cb));
  7951. XMEMSET(&server_cb, 0, sizeof(server_cb));
  7952. client_cb.method = param->client_meth;
  7953. server_cb.method = param->server_meth;
  7954. if (dtls)
  7955. client_cb.doUdp = server_cb.doUdp = 1;
  7956. /* Setup internal and external cache */
  7957. switch (j) {
  7958. case 0:
  7959. /* SSL_OP_NO_TICKET stateful ticket case */
  7960. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7961. break;
  7962. case 1:
  7963. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7964. break;
  7965. case 2:
  7966. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7967. break;
  7968. case 3:
  7969. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7970. break;
  7971. case 4:
  7972. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7973. break;
  7974. }
  7975. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7976. /* Add session to internal cache and save SSL session for testing */
  7977. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7978. /* Save client SSL session for testing */
  7979. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7980. server_cb.ticNoInit = 1; /* Use default builtin */
  7981. /* Don't free/release ctx */
  7982. server_cb.ctx = twcase_server_current_ctx_ptr;
  7983. server_cb.isSharedCtx = 1;
  7984. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7985. &server_cb, NULL), TEST_SUCCESS);
  7986. ExpectIntEQ(twcase_get_session_called, 0);
  7987. if (EXPECT_FAIL()) {
  7988. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7989. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7990. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7991. break;
  7992. }
  7993. switch (j) {
  7994. case 0:
  7995. case 1:
  7996. case 2:
  7997. /* cache cannot be searched with out a connection */
  7998. /* Add a new session */
  7999. ExpectIntEQ(twcase_new_session_called, 1);
  8000. /* In twcase_server_sess_ctx_pre_shutdown
  8001. * wolfSSL_CTX_add_session which evicts the existing session
  8002. * in cache and adds it back in */
  8003. ExpectIntLE(twcase_remove_session_called, 1);
  8004. break;
  8005. case 3:
  8006. case 4:
  8007. /* no external cache */
  8008. ExpectIntEQ(twcase_new_session_called, 0);
  8009. ExpectIntEQ(twcase_remove_session_called, 0);
  8010. break;
  8011. }
  8012. /* connection 2 - session resume */
  8013. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8014. twcase_new_session_called = 0;
  8015. twcase_remove_session_called = 0;
  8016. twcase_get_session_called = 0;
  8017. server_cb.on_result = 0;
  8018. client_cb.on_result = 0;
  8019. server_cb.ticNoInit = 1; /* Use default builtin */
  8020. server_cb.ctx = twcase_server_current_ctx_ptr;
  8021. /* try session resumption */
  8022. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8023. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8024. &server_cb, NULL), TEST_SUCCESS);
  8025. /* Clear cache before checking */
  8026. wolfSSL_CTX_flush_sessions(NULL, -1);
  8027. switch (j) {
  8028. case 0:
  8029. if (tls13) {
  8030. /* (D)TLSv1.3 stateful case */
  8031. /* cache hit */
  8032. /* DTLS accesses cache once for stateless parsing and
  8033. * once for stateful parsing */
  8034. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8035. /* (D)TLSv1.3 creates a new ticket,
  8036. * updates both internal and external cache */
  8037. ExpectIntEQ(twcase_new_session_called, 1);
  8038. /* A new session ID is created for a new ticket */
  8039. ExpectIntEQ(twcase_remove_session_called, 2);
  8040. }
  8041. else {
  8042. /* non (D)TLSv1.3 case, no update */
  8043. /* DTLS accesses cache once for stateless parsing and
  8044. * once for stateful parsing */
  8045. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8046. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8047. #else
  8048. ExpectIntEQ(twcase_get_session_called, 1);
  8049. #endif
  8050. ExpectIntEQ(twcase_new_session_called, 0);
  8051. /* Called on session added in
  8052. * twcase_server_sess_ctx_pre_shutdown */
  8053. ExpectIntEQ(twcase_remove_session_called, 1);
  8054. }
  8055. break;
  8056. case 1:
  8057. if (tls13) {
  8058. /* (D)TLSv1.3 case */
  8059. /* cache hit */
  8060. ExpectIntEQ(twcase_get_session_called, 1);
  8061. /* (D)TLSv1.3 creates a new ticket,
  8062. * updates both internal and external cache */
  8063. ExpectIntEQ(twcase_new_session_called, 1);
  8064. /* Called on session added in
  8065. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8066. ExpectIntEQ(twcase_remove_session_called, 1);
  8067. }
  8068. else {
  8069. /* non (D)TLSv1.3 case */
  8070. /* cache hit */
  8071. /* DTLS accesses cache once for stateless parsing and
  8072. * once for stateful parsing */
  8073. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8074. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8075. #else
  8076. ExpectIntEQ(twcase_get_session_called, 1);
  8077. #endif
  8078. ExpectIntEQ(twcase_new_session_called, 0);
  8079. /* Called on session added in
  8080. * twcase_server_sess_ctx_pre_shutdown */
  8081. ExpectIntEQ(twcase_remove_session_called, 1);
  8082. }
  8083. break;
  8084. case 2:
  8085. if (tls13) {
  8086. /* (D)TLSv1.3 case */
  8087. /* cache hit */
  8088. ExpectIntEQ(twcase_get_session_called, 1);
  8089. /* (D)TLSv1.3 creates a new ticket,
  8090. * updates both internal and external cache */
  8091. ExpectIntEQ(twcase_new_session_called, 1);
  8092. /* Called on session added in
  8093. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8094. ExpectIntEQ(twcase_remove_session_called, 1);
  8095. }
  8096. else {
  8097. /* non (D)TLSv1.3 case */
  8098. /* cache hit */
  8099. /* DTLS accesses cache once for stateless parsing and
  8100. * once for stateful parsing */
  8101. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8102. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8103. #else
  8104. ExpectIntEQ(twcase_get_session_called, 1);
  8105. #endif
  8106. ExpectIntEQ(twcase_new_session_called, 0);
  8107. /* Called on session added in
  8108. * twcase_server_sess_ctx_pre_shutdown */
  8109. ExpectIntEQ(twcase_remove_session_called, 1);
  8110. }
  8111. break;
  8112. case 3:
  8113. case 4:
  8114. /* no external cache */
  8115. ExpectIntEQ(twcase_get_session_called, 0);
  8116. ExpectIntEQ(twcase_new_session_called, 0);
  8117. ExpectIntEQ(twcase_remove_session_called, 0);
  8118. break;
  8119. }
  8120. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8121. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8122. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8123. if (EXPECT_FAIL())
  8124. break;
  8125. }
  8126. twcase_get_sessionCb_cleanup();
  8127. XMEMSET(&server_sessionCache.entries, 0,
  8128. sizeof(server_sessionCache.entries));
  8129. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8130. wc_FreeMutex(&server_sessionCache.htLock);
  8131. return EXPECT_RESULT();
  8132. }
  8133. #endif
  8134. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8135. {
  8136. EXPECT_DECLS;
  8137. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8138. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8139. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8140. defined(HAVE_SESSION_TICKET) && \
  8141. !defined(TITAN_SESSION_CACHE) && \
  8142. !defined(HUGE_SESSION_CACHE) && \
  8143. !defined(BIG_SESSION_CACHE) && \
  8144. !defined(MEDIUM_SESSION_CACHE)
  8145. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8146. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8147. struct test_add_session_ext_params param[1] = {
  8148. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8149. };
  8150. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8151. #endif
  8152. #endif
  8153. return EXPECT_RESULT();
  8154. }
  8155. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8156. {
  8157. EXPECT_DECLS;
  8158. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8159. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8160. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8161. defined(HAVE_SESSION_TICKET) && \
  8162. !defined(TITAN_SESSION_CACHE) && \
  8163. !defined(HUGE_SESSION_CACHE) && \
  8164. !defined(BIG_SESSION_CACHE) && \
  8165. !defined(MEDIUM_SESSION_CACHE)
  8166. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8167. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8168. #ifdef WOLFSSL_DTLS13
  8169. struct test_add_session_ext_params param[1] = {
  8170. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8171. };
  8172. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8173. #endif
  8174. #endif
  8175. #endif
  8176. return EXPECT_RESULT();
  8177. }
  8178. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8179. {
  8180. EXPECT_DECLS;
  8181. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8182. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8183. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8184. defined(HAVE_SESSION_TICKET) && \
  8185. !defined(TITAN_SESSION_CACHE) && \
  8186. !defined(HUGE_SESSION_CACHE) && \
  8187. !defined(BIG_SESSION_CACHE) && \
  8188. !defined(MEDIUM_SESSION_CACHE)
  8189. #ifndef WOLFSSL_NO_TLS12
  8190. struct test_add_session_ext_params param[1] = {
  8191. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8192. };
  8193. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8194. #endif
  8195. #endif
  8196. return EXPECT_RESULT();
  8197. }
  8198. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8199. {
  8200. EXPECT_DECLS;
  8201. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8202. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8203. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8204. defined(HAVE_SESSION_TICKET) && \
  8205. !defined(TITAN_SESSION_CACHE) && \
  8206. !defined(HUGE_SESSION_CACHE) && \
  8207. !defined(BIG_SESSION_CACHE) && \
  8208. !defined(MEDIUM_SESSION_CACHE)
  8209. #ifndef WOLFSSL_NO_TLS12
  8210. #ifdef WOLFSSL_DTLS
  8211. struct test_add_session_ext_params param[1] = {
  8212. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8213. };
  8214. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8215. #endif
  8216. #endif
  8217. #endif
  8218. return EXPECT_RESULT();
  8219. }
  8220. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8221. {
  8222. EXPECT_DECLS;
  8223. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8224. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8225. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8226. defined(HAVE_SESSION_TICKET) && \
  8227. !defined(TITAN_SESSION_CACHE) && \
  8228. !defined(HUGE_SESSION_CACHE) && \
  8229. !defined(BIG_SESSION_CACHE) && \
  8230. !defined(MEDIUM_SESSION_CACHE)
  8231. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8232. !defined(NO_DES3))
  8233. struct test_add_session_ext_params param[1] = {
  8234. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8235. };
  8236. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8237. #endif
  8238. #endif
  8239. return EXPECT_RESULT();
  8240. }
  8241. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8242. {
  8243. EXPECT_DECLS;
  8244. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8245. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8246. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8247. defined(HAVE_SESSION_TICKET) && \
  8248. !defined(TITAN_SESSION_CACHE) && \
  8249. !defined(HUGE_SESSION_CACHE) && \
  8250. !defined(BIG_SESSION_CACHE) && \
  8251. !defined(MEDIUM_SESSION_CACHE)
  8252. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8253. !defined(NO_DES3))
  8254. #ifdef WOLFSSL_DTLS
  8255. struct test_add_session_ext_params param[1] = {
  8256. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8257. };
  8258. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8259. #endif
  8260. #endif
  8261. #endif
  8262. return EXPECT_RESULT();
  8263. }
  8264. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8265. /* canned export of a session using older version 3 */
  8266. static unsigned char version_3[] = {
  8267. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8268. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8269. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8270. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8271. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8272. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8273. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8274. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8275. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8276. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8277. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8278. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8279. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8280. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8281. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8282. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8283. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8284. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8285. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8286. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8287. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8289. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8290. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8291. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8292. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8293. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8294. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8295. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8296. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8297. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8298. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8299. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8300. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8301. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8302. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8303. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8304. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8305. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8306. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8307. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8308. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8309. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8310. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8311. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8312. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8313. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8314. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8315. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8316. 0x2E, 0x31, 0xED, 0x4F
  8317. };
  8318. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8319. static int test_wolfSSL_dtls_export(void)
  8320. {
  8321. EXPECT_DECLS;
  8322. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8323. tcp_ready ready;
  8324. func_args client_args;
  8325. func_args server_args;
  8326. THREAD_TYPE serverThread;
  8327. callback_functions server_cbf;
  8328. callback_functions client_cbf;
  8329. #ifdef WOLFSSL_TIRTOS
  8330. fdOpenSession(Task_self());
  8331. #endif
  8332. InitTcpReady(&ready);
  8333. #if defined(USE_WINDOWS_API)
  8334. /* use RNG to get random port if using windows */
  8335. ready.port = GetRandomPort();
  8336. #endif
  8337. /* set using dtls */
  8338. XMEMSET(&client_args, 0, sizeof(func_args));
  8339. XMEMSET(&server_args, 0, sizeof(func_args));
  8340. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8341. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8342. server_cbf.method = wolfDTLSv1_2_server_method;
  8343. client_cbf.method = wolfDTLSv1_2_client_method;
  8344. server_args.callbacks = &server_cbf;
  8345. client_args.callbacks = &client_cbf;
  8346. server_args.signal = &ready;
  8347. client_args.signal = &ready;
  8348. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8349. wait_tcp_ready(&server_args);
  8350. run_wolfssl_client(&client_args);
  8351. join_thread(serverThread);
  8352. ExpectTrue(client_args.return_code);
  8353. ExpectTrue(server_args.return_code);
  8354. FreeTcpReady(&ready);
  8355. #ifdef WOLFSSL_TIRTOS
  8356. fdOpenSession(Task_self());
  8357. #endif
  8358. if (EXPECT_SUCCESS()) {
  8359. SOCKET_T sockfd = 0;
  8360. WOLFSSL_CTX* ctx = NULL;
  8361. WOLFSSL* ssl = NULL;
  8362. char msg[64] = "hello wolfssl!";
  8363. char reply[1024];
  8364. int msgSz = (int)XSTRLEN(msg);
  8365. byte *session, *window;
  8366. unsigned int sessionSz = 0;
  8367. unsigned int windowSz = 0;
  8368. #ifndef TEST_IPV6
  8369. struct sockaddr_in peerAddr;
  8370. #else
  8371. struct sockaddr_in6 peerAddr;
  8372. #endif /* TEST_IPV6 */
  8373. int i;
  8374. /* Set ctx to DTLS 1.2 */
  8375. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8376. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8377. /* test importing version 3 */
  8378. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8379. /* test importing bad length and bad version */
  8380. version_3[2] += 1;
  8381. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8382. version_3[2] -= 1; version_3[1] = 0XA0;
  8383. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8384. wolfSSL_free(ssl);
  8385. wolfSSL_CTX_free(ctx);
  8386. /* check storing client state after connection and storing window only */
  8387. #ifdef WOLFSSL_TIRTOS
  8388. fdOpenSession(Task_self());
  8389. #endif
  8390. InitTcpReady(&ready);
  8391. #if defined(USE_WINDOWS_API)
  8392. /* use RNG to get random port if using windows */
  8393. ready.port = GetRandomPort();
  8394. #endif
  8395. /* set using dtls */
  8396. XMEMSET(&server_args, 0, sizeof(func_args));
  8397. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8398. server_cbf.method = wolfDTLSv1_2_server_method;
  8399. server_cbf.doUdp = 1;
  8400. server_args.callbacks = &server_cbf;
  8401. server_args.argc = 3; /* set loop_count to 3 */
  8402. server_args.signal = &ready;
  8403. start_thread(test_server_nofail, &server_args, &serverThread);
  8404. wait_tcp_ready(&server_args);
  8405. /* create and connect with client */
  8406. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8407. ExpectIntEQ(WOLFSSL_SUCCESS,
  8408. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8409. ExpectIntEQ(WOLFSSL_SUCCESS,
  8410. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8411. ExpectIntEQ(WOLFSSL_SUCCESS,
  8412. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8413. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8414. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8415. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8416. /* store server information connected too */
  8417. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8418. #ifndef TEST_IPV6
  8419. peerAddr.sin_family = AF_INET;
  8420. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8421. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8422. #else
  8423. peerAddr.sin6_family = AF_INET6;
  8424. ExpectIntEQ(
  8425. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8426. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8427. #endif
  8428. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8429. WOLFSSL_SUCCESS);
  8430. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8431. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8432. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8433. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8434. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8435. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8436. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8437. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8438. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8439. wolfSSL_free(ssl);
  8440. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8441. /* restore state */
  8442. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8443. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8444. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8445. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8446. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8447. WOLFSSL_SUCCESS);
  8448. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8449. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8450. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8451. wolfSSL_free(ssl);
  8452. }
  8453. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8454. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8455. wolfSSL_CTX_free(ctx);
  8456. fprintf(stderr, "done and waiting for server\n");
  8457. join_thread(serverThread);
  8458. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8459. FreeTcpReady(&ready);
  8460. #ifdef WOLFSSL_TIRTOS
  8461. fdOpenSession(Task_self());
  8462. #endif
  8463. }
  8464. #endif
  8465. return EXPECT_RESULT();
  8466. }
  8467. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8468. #ifdef WOLFSSL_TLS13
  8469. static const byte canned_client_tls13_session[] = {
  8470. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8471. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8472. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8473. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8474. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8475. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8476. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8477. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8478. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8479. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8480. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8481. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8482. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8483. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8484. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8485. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8486. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8487. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8488. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8489. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8490. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8491. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8492. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8493. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8494. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8495. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8496. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8497. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8498. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8499. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8500. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8501. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8502. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8503. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8504. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8505. 0x00, 0x03
  8506. };
  8507. static const byte canned_server_tls13_session[] = {
  8508. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8509. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8510. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8511. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8512. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8513. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8514. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8515. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8516. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8517. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8518. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8519. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8520. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8521. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8522. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8523. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8524. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8525. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8526. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8527. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8528. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8529. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8530. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8531. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8532. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8533. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8534. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8535. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8536. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8537. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8538. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8539. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8540. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8541. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8542. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8543. 0x00, 0x04
  8544. };
  8545. #endif /* WOLFSSL_TLS13 */
  8546. static const byte canned_client_session[] = {
  8547. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8548. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8549. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8550. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8551. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8552. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8553. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8554. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8555. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8556. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8557. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8558. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8559. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8560. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8561. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8562. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8563. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8564. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8565. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8566. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8567. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8568. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8569. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8570. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8571. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8572. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8573. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8574. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8575. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8576. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8577. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8578. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8579. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8580. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8581. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8582. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8583. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8584. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8585. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8586. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8587. 0x00, 0x03
  8588. };
  8589. static const byte canned_server_session[] = {
  8590. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8591. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8592. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8593. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8594. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8595. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8596. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8597. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8598. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8599. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8600. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8601. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8602. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8603. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8604. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8605. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8606. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8607. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8608. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8609. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8610. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8611. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8612. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8613. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8614. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8615. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8616. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8617. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8618. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8619. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8620. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8621. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8622. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8623. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8624. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8625. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8626. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8627. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8628. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8629. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8630. 0x00, 0x04
  8631. };
  8632. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8633. {
  8634. SOCKET_T sockfd = 0;
  8635. SOCKET_T clientfd = 0;
  8636. word16 port;
  8637. callback_functions* cbf;
  8638. WOLFSSL_CTX* ctx = 0;
  8639. WOLFSSL* ssl = 0;
  8640. char msg[] = "I hear you fa shizzle!";
  8641. char input[1024];
  8642. int idx;
  8643. #ifdef WOLFSSL_TIRTOS
  8644. fdOpenSession(Task_self());
  8645. #endif
  8646. ((func_args*)args)->return_code = TEST_FAIL;
  8647. cbf = ((func_args*)args)->callbacks;
  8648. #if defined(USE_WINDOWS_API)
  8649. port = ((func_args*)args)->signal->port;
  8650. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8651. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8652. /* Let tcp_listen assign port */
  8653. port = 0;
  8654. #else
  8655. /* Use default port */
  8656. port = wolfSSLPort;
  8657. #endif
  8658. /* do it here to detect failure */
  8659. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8660. CloseSocket(sockfd);
  8661. {
  8662. WOLFSSL_METHOD* method = NULL;
  8663. if (cbf != NULL && cbf->method != NULL) {
  8664. method = cbf->method();
  8665. }
  8666. else {
  8667. method = wolfTLSv1_2_server_method();
  8668. }
  8669. ctx = wolfSSL_CTX_new(method);
  8670. }
  8671. if (ctx == NULL) {
  8672. goto done;
  8673. }
  8674. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8675. /* call ctx setup callback */
  8676. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8677. cbf->ctx_ready(ctx);
  8678. }
  8679. ssl = wolfSSL_new(ctx);
  8680. if (ssl == NULL) {
  8681. goto done;
  8682. }
  8683. wolfSSL_set_fd(ssl, clientfd);
  8684. /* call ssl setup callback */
  8685. if (cbf != NULL && cbf->ssl_ready != NULL) {
  8686. cbf->ssl_ready(ssl);
  8687. }
  8688. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  8689. if (idx > 0) {
  8690. input[idx] = '\0';
  8691. fprintf(stderr, "Client message export/import: %s\n", input);
  8692. }
  8693. else {
  8694. fprintf(stderr, "ret = %d error = %d\n", idx,
  8695. wolfSSL_get_error(ssl, idx));
  8696. goto done;
  8697. }
  8698. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  8699. /*err_sys("SSL_write failed");*/
  8700. WOLFSSL_RETURN_FROM_THREAD(0);
  8701. }
  8702. #ifdef WOLFSSL_TIRTOS
  8703. Task_yield();
  8704. #endif
  8705. ((func_args*)args)->return_code = TEST_SUCCESS;
  8706. done:
  8707. wolfSSL_shutdown(ssl);
  8708. wolfSSL_free(ssl);
  8709. wolfSSL_CTX_free(ctx);
  8710. CloseSocket(clientfd);
  8711. #ifdef WOLFSSL_TIRTOS
  8712. fdCloseSession(Task_self());
  8713. #endif
  8714. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8715. && defined(HAVE_THREAD_LS)
  8716. wc_ecc_fp_free(); /* free per thread cache */
  8717. #endif
  8718. #if defined(HAVE_SESSION_TICKET) && \
  8719. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  8720. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  8721. OpenSSLTicketCleanup();
  8722. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  8723. TicketCleanup();
  8724. #endif
  8725. #endif
  8726. WOLFSSL_RETURN_FROM_THREAD(0);
  8727. }
  8728. static void load_tls12_canned_server(WOLFSSL* ssl)
  8729. {
  8730. int clientfd = wolfSSL_get_fd(ssl);
  8731. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  8732. sizeof(canned_server_session)), sizeof(canned_server_session));
  8733. wolfSSL_set_fd(ssl, clientfd);
  8734. }
  8735. #ifdef WOLFSSL_TLS13
  8736. static void load_tls13_canned_server(WOLFSSL* ssl)
  8737. {
  8738. int clientfd = wolfSSL_get_fd(ssl);
  8739. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  8740. sizeof(canned_server_tls13_session)),
  8741. sizeof(canned_server_tls13_session));
  8742. wolfSSL_set_fd(ssl, clientfd);
  8743. }
  8744. #endif
  8745. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  8746. static int test_wolfSSL_tls_export_run(int v)
  8747. {
  8748. EXPECT_DECLS;
  8749. SOCKET_T sockfd = 0;
  8750. WOLFSSL_CTX* ctx = 0;
  8751. WOLFSSL* ssl = 0;
  8752. char msg[64] = "hello wolfssl!";
  8753. char reply[1024];
  8754. word32 replySz;
  8755. int msgSz = (int)XSTRLEN(msg);
  8756. const byte* clientSession = NULL;
  8757. int clientSessionSz = 0;
  8758. tcp_ready ready;
  8759. func_args server_args;
  8760. THREAD_TYPE serverThread;
  8761. callback_functions server_cbf;
  8762. #ifdef WOLFSSL_TIRTOS
  8763. fdOpenSession(Task_self());
  8764. #endif
  8765. InitTcpReady(&ready);
  8766. #if defined(USE_WINDOWS_API)
  8767. /* use RNG to get random port if using windows */
  8768. ready.port = GetRandomPort();
  8769. #endif
  8770. XMEMSET(&server_args, 0, sizeof(func_args));
  8771. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8772. switch (v) {
  8773. case WOLFSSL_TLSV1_2:
  8774. server_cbf.method = wolfTLSv1_2_server_method;
  8775. server_cbf.ssl_ready = load_tls12_canned_server;
  8776. /* setup the client side */
  8777. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8778. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8779. clientSession = canned_client_session;
  8780. clientSessionSz = sizeof(canned_client_session);
  8781. break;
  8782. #ifdef WOLFSSL_TLS13
  8783. case WOLFSSL_TLSV1_3:
  8784. server_cbf.method = wolfTLSv1_3_server_method;
  8785. server_cbf.ssl_ready = load_tls13_canned_server;
  8786. /* setup the client side */
  8787. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8788. clientSession = canned_client_tls13_session;
  8789. clientSessionSz = sizeof(canned_client_tls13_session);
  8790. break;
  8791. #endif
  8792. }
  8793. server_args.callbacks = &server_cbf;
  8794. server_args.signal = &ready;
  8795. start_thread(tls_export_server, &server_args, &serverThread);
  8796. wait_tcp_ready(&server_args);
  8797. #ifdef WOLFSSL_TIRTOS
  8798. fdOpenSession(Task_self());
  8799. #endif
  8800. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8801. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  8802. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  8803. clientSessionSz);
  8804. replySz = sizeof(reply);
  8805. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  8806. #if !defined(NO_PSK) && defined(HAVE_ANON)
  8807. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  8808. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  8809. #endif
  8810. wolfSSL_set_fd(ssl, sockfd);
  8811. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8812. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  8813. wolfSSL_free(ssl);
  8814. wolfSSL_CTX_free(ctx);
  8815. CloseSocket(sockfd);
  8816. #ifdef WOLFSSL_TIRTOS
  8817. fdCloseSession(Task_self());
  8818. #endif
  8819. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8820. && defined(HAVE_THREAD_LS)
  8821. wc_ecc_fp_free(); /* free per thread cache */
  8822. #endif
  8823. join_thread(serverThread);
  8824. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8825. FreeTcpReady(&ready);
  8826. #ifdef WOLFSSL_TIRTOS
  8827. fdOpenSession(Task_self());
  8828. #endif
  8829. return EXPECT_RESULT();
  8830. }
  8831. #endif
  8832. static int test_wolfSSL_tls_export(void)
  8833. {
  8834. int res = TEST_SKIPPED;
  8835. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8836. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  8837. #ifdef WOLFSSL_TLS13
  8838. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  8839. #endif
  8840. res = TEST_RES_CHECK(1);
  8841. #endif
  8842. return res;
  8843. }
  8844. /*----------------------------------------------------------------------------*
  8845. | TLS extensions tests
  8846. *----------------------------------------------------------------------------*/
  8847. #ifdef ENABLE_TLS_CALLBACK_TEST
  8848. /* Connection test runner - generic */
  8849. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  8850. callback_functions* server_callbacks)
  8851. {
  8852. tcp_ready ready;
  8853. func_args client_args;
  8854. func_args server_args;
  8855. THREAD_TYPE serverThread;
  8856. XMEMSET(&client_args, 0, sizeof(func_args));
  8857. XMEMSET(&server_args, 0, sizeof(func_args));
  8858. StartTCP();
  8859. client_args.callbacks = client_callbacks;
  8860. server_args.callbacks = server_callbacks;
  8861. #ifdef WOLFSSL_TIRTOS
  8862. fdOpenSession(Task_self());
  8863. #endif
  8864. /* RUN Server side */
  8865. InitTcpReady(&ready);
  8866. #if defined(USE_WINDOWS_API)
  8867. /* use RNG to get random port if using windows */
  8868. ready.port = GetRandomPort();
  8869. #endif
  8870. server_args.signal = &ready;
  8871. client_args.signal = &ready;
  8872. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8873. wait_tcp_ready(&server_args);
  8874. /* RUN Client side */
  8875. run_wolfssl_client(&client_args);
  8876. join_thread(serverThread);
  8877. FreeTcpReady(&ready);
  8878. #ifdef WOLFSSL_TIRTOS
  8879. fdCloseSession(Task_self());
  8880. #endif
  8881. client_callbacks->return_code = client_args.return_code;
  8882. server_callbacks->return_code = server_args.return_code;
  8883. }
  8884. #endif /* ENABLE_TLS_CALLBACK_TEST */
  8885. #ifdef HAVE_SNI
  8886. static int test_wolfSSL_UseSNI_params(void)
  8887. {
  8888. EXPECT_DECLS;
  8889. #if !defined(NO_WOLFSSL_CLIENT)
  8890. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8891. WOLFSSL *ssl = wolfSSL_new(ctx);
  8892. ExpectNotNull(ctx);
  8893. ExpectNotNull(ssl);
  8894. /* invalid [ctx|ssl] */
  8895. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  8896. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  8897. /* invalid type */
  8898. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  8899. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  8900. /* invalid data */
  8901. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  8902. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  8903. /* success case */
  8904. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  8905. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  8906. wolfSSL_free(ssl);
  8907. wolfSSL_CTX_free(ctx);
  8908. #endif /* !NO_WOLFSSL_CLIENT */
  8909. return EXPECT_RESULT();
  8910. }
  8911. /* BEGIN of connection tests callbacks */
  8912. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8913. {
  8914. AssertIntEQ(WOLFSSL_SUCCESS,
  8915. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8916. }
  8917. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8918. {
  8919. AssertIntEQ(WOLFSSL_SUCCESS,
  8920. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8921. }
  8922. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8923. {
  8924. AssertIntEQ(WOLFSSL_SUCCESS,
  8925. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8926. }
  8927. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8928. {
  8929. use_SNI_at_ssl(ssl);
  8930. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8931. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8932. }
  8933. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8934. {
  8935. use_SNI_at_ssl(ssl);
  8936. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8937. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8938. }
  8939. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8940. {
  8941. use_SNI_at_ctx(ctx);
  8942. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8943. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8944. }
  8945. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8946. {
  8947. use_SNI_at_ssl(ssl);
  8948. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8949. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8950. }
  8951. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8952. {
  8953. use_SNI_at_ctx(ctx);
  8954. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8955. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8956. }
  8957. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8958. {
  8959. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8960. }
  8961. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8962. {
  8963. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8964. }
  8965. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8966. {
  8967. byte type = WOLFSSL_SNI_HOST_NAME;
  8968. void* request = (void*) &type; /* to be overwritten */
  8969. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8970. AssertNotNull(request);
  8971. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8972. AssertNull(request);
  8973. }
  8974. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8975. {
  8976. byte type = WOLFSSL_SNI_HOST_NAME;
  8977. void* request = NULL;
  8978. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8979. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8980. AssertNotNull(request);
  8981. AssertStrEQ("www.wolfssl.com", (char*)request);
  8982. }
  8983. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8984. {
  8985. byte type = WOLFSSL_SNI_HOST_NAME;
  8986. void* request = NULL;
  8987. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8988. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8989. AssertNotNull(request);
  8990. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8991. }
  8992. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8993. {
  8994. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8995. }
  8996. /* END of connection tests callbacks */
  8997. static int test_wolfSSL_UseSNI_connection(void)
  8998. {
  8999. int res = TEST_SKIPPED;
  9000. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9001. callback_functions client_cb;
  9002. callback_functions server_cb;
  9003. size_t i;
  9004. #ifdef WOLFSSL_STATIC_MEMORY
  9005. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9006. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9007. #endif
  9008. struct {
  9009. method_provider client_meth;
  9010. method_provider server_meth;
  9011. #ifdef WOLFSSL_STATIC_MEMORY
  9012. wolfSSL_method_func client_meth_ex;
  9013. wolfSSL_method_func server_meth_ex;
  9014. #endif
  9015. } methods[] = {
  9016. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9017. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9018. #ifdef WOLFSSL_STATIC_MEMORY
  9019. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9020. #endif
  9021. },
  9022. #endif
  9023. #ifndef WOLFSSL_NO_TLS12
  9024. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9025. #ifdef WOLFSSL_STATIC_MEMORY
  9026. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9027. #endif
  9028. },
  9029. #endif
  9030. #ifdef WOLFSSL_TLS13
  9031. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9032. #ifdef WOLFSSL_STATIC_MEMORY
  9033. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9034. #endif
  9035. },
  9036. #endif
  9037. };
  9038. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9039. for (i = 0; i < methodsSz; i++) {
  9040. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9041. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9042. client_cb.method = methods[i].client_meth;
  9043. server_cb.method = methods[i].server_meth;
  9044. client_cb.devId = testDevId;
  9045. server_cb.devId = testDevId;
  9046. #ifdef WOLFSSL_STATIC_MEMORY
  9047. client_cb.method_ex = methods[i].client_meth_ex;
  9048. server_cb.method_ex = methods[i].server_meth_ex;
  9049. client_cb.mem = cliMem;
  9050. client_cb.memSz = (word32)sizeof(cliMem);
  9051. server_cb.mem = svrMem;
  9052. server_cb.memSz = (word32)sizeof(svrMem);;
  9053. #endif
  9054. /* success case at ctx */
  9055. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9056. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9057. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9058. test_wolfSSL_client_server(&client_cb, &server_cb);
  9059. /* success case at ssl */
  9060. fprintf(stderr, "\tsuccess case at ssl\n");
  9061. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9062. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9063. test_wolfSSL_client_server(&client_cb, &server_cb);
  9064. /* default mismatch behavior */
  9065. fprintf(stderr, "\tdefault mismatch behavior\n");
  9066. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9067. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9068. test_wolfSSL_client_server(&client_cb, &server_cb);
  9069. /* continue on mismatch */
  9070. fprintf(stderr, "\tcontinue on mismatch\n");
  9071. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9072. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9073. test_wolfSSL_client_server(&client_cb, &server_cb);
  9074. /* fake answer on mismatch */
  9075. fprintf(stderr, "\tfake answer on mismatch\n");
  9076. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9077. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9078. test_wolfSSL_client_server(&client_cb, &server_cb);
  9079. /* sni abort - success */
  9080. fprintf(stderr, "\tsni abort - success\n");
  9081. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9082. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9083. test_wolfSSL_client_server(&client_cb, &server_cb);
  9084. /* sni abort - abort when absent (ctx) */
  9085. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9086. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9087. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9088. test_wolfSSL_client_server(&client_cb, &server_cb);
  9089. /* sni abort - abort when absent (ssl) */
  9090. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9091. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9092. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9093. test_wolfSSL_client_server(&client_cb, &server_cb);
  9094. /* sni abort - success when overwritten */
  9095. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9096. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9097. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9098. test_wolfSSL_client_server(&client_cb, &server_cb);
  9099. /* sni abort - success when allowing mismatches */
  9100. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9101. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9102. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9103. test_wolfSSL_client_server(&client_cb, &server_cb);
  9104. }
  9105. res = TEST_RES_CHECK(1);
  9106. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9107. return res;
  9108. }
  9109. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9110. {
  9111. EXPECT_DECLS;
  9112. byte buff[] = { /* www.paypal.com */
  9113. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9114. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9115. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9116. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9117. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9118. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9119. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9120. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9121. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9122. };
  9123. byte buff2[] = { /* api.textmate.org */
  9124. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9125. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9126. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9127. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9128. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9129. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9130. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9131. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9132. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9133. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9134. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9135. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9136. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9137. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9138. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9139. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9140. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9141. };
  9142. byte buff3[] = { /* no sni extension */
  9143. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9144. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9145. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9146. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9147. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9148. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9149. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9150. };
  9151. byte buff4[] = { /* last extension has zero size */
  9152. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9153. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9154. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9155. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9156. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9157. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9158. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9159. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9160. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9161. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9162. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9163. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9164. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9165. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9166. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9167. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9168. 0x12, 0x00, 0x00
  9169. };
  9170. byte buff5[] = { /* SSL v2.0 client hello */
  9171. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9172. /* dummy bytes below, just to pass size check */
  9173. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9174. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9175. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9176. };
  9177. byte result[32] = {0};
  9178. word32 length = 32;
  9179. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9180. 0, result, &length));
  9181. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9182. 0, result, &length));
  9183. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9184. 1, result, &length));
  9185. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9186. 0, result, &length));
  9187. buff[0] = 0x16;
  9188. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9189. 0, result, &length));
  9190. buff[1] = 0x03;
  9191. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9192. sizeof(buff), 0, result, &length));
  9193. buff[2] = 0x03;
  9194. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9195. sizeof(buff), 0, result, &length));
  9196. buff[4] = 0x64;
  9197. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9198. 0, result, &length));
  9199. if (EXPECT_SUCCESS())
  9200. result[length] = 0;
  9201. ExpectStrEQ("www.paypal.com", (const char*) result);
  9202. length = 32;
  9203. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9204. 0, result, &length));
  9205. if (EXPECT_SUCCESS())
  9206. result[length] = 0;
  9207. ExpectStrEQ("api.textmate.org", (const char*) result);
  9208. /* SSL v2.0 tests */
  9209. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9210. sizeof(buff5), 0, result, &length));
  9211. buff5[2] = 0x02;
  9212. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9213. sizeof(buff5), 0, result, &length));
  9214. buff5[2] = 0x01; buff5[6] = 0x08;
  9215. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9216. sizeof(buff5), 0, result, &length));
  9217. buff5[6] = 0x09; buff5[8] = 0x01;
  9218. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9219. sizeof(buff5), 0, result, &length));
  9220. return EXPECT_RESULT();
  9221. }
  9222. #endif /* HAVE_SNI */
  9223. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9224. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9225. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9226. /* Dummy peer functions to satisfy the exporter/importer */
  9227. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9228. int* ipSz, unsigned short* port, int* fam)
  9229. {
  9230. (void)ssl;
  9231. ip[0] = -1;
  9232. *ipSz = 1;
  9233. *port = 1;
  9234. *fam = 2;
  9235. return 1;
  9236. }
  9237. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9238. int ipSz, unsigned short port, int fam)
  9239. {
  9240. (void)ssl;
  9241. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9242. return 0;
  9243. return 1;
  9244. }
  9245. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9246. WOLFSSL **ssl)
  9247. {
  9248. EXPECT_DECLS;
  9249. unsigned char* sessionBuf = NULL;
  9250. unsigned int sessionSz = 0;
  9251. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9252. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9253. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9254. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9255. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9256. ExpectNotNull(sessionBuf =
  9257. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9258. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9259. wolfSSL_free(*ssl);
  9260. *ssl = NULL;
  9261. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9262. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9263. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9264. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9265. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9266. return EXPECT_RESULT();
  9267. }
  9268. #endif
  9269. static int test_wolfSSL_dtls_export_peers(void)
  9270. {
  9271. EXPECT_DECLS;
  9272. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9273. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9274. test_ssl_cbf client_cbf;
  9275. test_ssl_cbf server_cbf;
  9276. size_t i, j;
  9277. struct test_params {
  9278. method_provider client_meth;
  9279. method_provider server_meth;
  9280. const char* dtls_version;
  9281. } params[] = {
  9282. #ifndef NO_OLD_TLS
  9283. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9284. #endif
  9285. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9286. /* TODO DTLS 1.3 exporting not supported
  9287. #ifdef WOLFSSL_DTLS13
  9288. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9289. #endif
  9290. */
  9291. };
  9292. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9293. for (j = 0; j <= 0b11; j++) {
  9294. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9295. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9296. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9297. client_cbf.method = params[i].client_meth;
  9298. server_cbf.method = params[i].server_meth;
  9299. if (j & 0b01) {
  9300. client_cbf.on_handshake =
  9301. test_wolfSSL_dtls_export_peers_on_handshake;
  9302. printf(" With client export;");
  9303. }
  9304. if (j & 0b10) {
  9305. server_cbf.on_handshake =
  9306. test_wolfSSL_dtls_export_peers_on_handshake;
  9307. printf(" With server export;");
  9308. }
  9309. printf("\n");
  9310. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9311. &server_cbf, NULL), TEST_SUCCESS);
  9312. if (!EXPECT_SUCCESS())
  9313. break;
  9314. }
  9315. }
  9316. #endif
  9317. return EXPECT_RESULT();
  9318. }
  9319. static int test_wolfSSL_UseTrustedCA(void)
  9320. {
  9321. EXPECT_DECLS;
  9322. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9323. && !defined(NO_RSA)
  9324. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9325. WOLFSSL_CTX *ctx = NULL;
  9326. WOLFSSL *ssl = NULL;
  9327. byte id[20];
  9328. #ifndef NO_WOLFSSL_SERVER
  9329. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9330. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9331. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9332. #else
  9333. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9334. #endif
  9335. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9336. XMEMSET(id, 0, sizeof(id));
  9337. /* error cases */
  9338. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9339. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9340. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9341. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9342. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9343. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9344. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9345. #ifdef NO_SHA
  9346. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9347. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9348. #endif
  9349. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9350. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9351. /* success cases */
  9352. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9353. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9354. #ifndef NO_SHA
  9355. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9356. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9357. #endif
  9358. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9359. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9360. wolfSSL_free(ssl);
  9361. wolfSSL_CTX_free(ctx);
  9362. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9363. #endif /* HAVE_TRUSTED_CA */
  9364. return EXPECT_RESULT();
  9365. }
  9366. static int test_wolfSSL_UseMaxFragment(void)
  9367. {
  9368. EXPECT_DECLS;
  9369. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9370. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9371. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9372. #ifndef NO_WOLFSSL_SERVER
  9373. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9374. #else
  9375. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9376. #endif
  9377. WOLFSSL *ssl = NULL;
  9378. #ifdef OPENSSL_EXTRA
  9379. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  9380. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  9381. #else
  9382. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9383. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9384. #endif
  9385. #ifndef NO_WOLFSSL_SERVER
  9386. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9387. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9388. #endif
  9389. ExpectNotNull(ctx);
  9390. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9391. #ifdef OPENSSL_EXTRA
  9392. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9393. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9394. #else
  9395. UseMaxFragment = wolfSSL_UseMaxFragment;
  9396. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9397. #endif
  9398. /* error cases */
  9399. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9400. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9401. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9402. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9403. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9404. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9405. /* success case */
  9406. #ifdef OPENSSL_EXTRA
  9407. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9408. #else
  9409. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9410. #endif
  9411. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9412. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9413. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9414. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9415. #ifdef OPENSSL_EXTRA
  9416. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9417. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9418. #else
  9419. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9420. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9421. #endif
  9422. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9423. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9424. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9425. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9426. #ifdef OPENSSL_EXTRA
  9427. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9428. #else
  9429. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9430. #endif
  9431. wolfSSL_free(ssl);
  9432. wolfSSL_CTX_free(ctx);
  9433. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9434. #endif
  9435. return EXPECT_RESULT();
  9436. }
  9437. static int test_wolfSSL_UseTruncatedHMAC(void)
  9438. {
  9439. EXPECT_DECLS;
  9440. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9441. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9442. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9443. #ifndef NO_WOLFSSL_SERVER
  9444. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9445. #else
  9446. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9447. #endif
  9448. WOLFSSL *ssl = NULL;
  9449. ExpectNotNull(ctx);
  9450. #ifndef NO_WOLFSSL_SERVER
  9451. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9452. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9453. #endif
  9454. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9455. /* error cases */
  9456. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9457. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9458. /* success case */
  9459. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9460. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9461. wolfSSL_free(ssl);
  9462. wolfSSL_CTX_free(ctx);
  9463. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9464. #endif
  9465. return EXPECT_RESULT();
  9466. }
  9467. static int test_wolfSSL_UseSupportedCurve(void)
  9468. {
  9469. EXPECT_DECLS;
  9470. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9471. !defined(NO_TLS)
  9472. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9473. WOLFSSL *ssl = wolfSSL_new(ctx);
  9474. ExpectNotNull(ctx);
  9475. ExpectNotNull(ssl);
  9476. /* error cases */
  9477. ExpectIntNE(WOLFSSL_SUCCESS,
  9478. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9479. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9480. ExpectIntNE(WOLFSSL_SUCCESS,
  9481. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9482. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9483. /* success case */
  9484. ExpectIntEQ(WOLFSSL_SUCCESS,
  9485. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9486. ExpectIntEQ(WOLFSSL_SUCCESS,
  9487. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9488. wolfSSL_free(ssl);
  9489. wolfSSL_CTX_free(ctx);
  9490. #endif
  9491. return EXPECT_RESULT();
  9492. }
  9493. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9494. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9495. {
  9496. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9497. }
  9498. static void use_ALPN_all(WOLFSSL* ssl)
  9499. {
  9500. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9501. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9502. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9503. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9504. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9505. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9506. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9507. }
  9508. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9509. {
  9510. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9511. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9512. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9513. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9514. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9515. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9516. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9517. }
  9518. static void use_ALPN_one(WOLFSSL* ssl)
  9519. {
  9520. /* spdy/2 */
  9521. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9522. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9523. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9524. }
  9525. static void use_ALPN_unknown(WOLFSSL* ssl)
  9526. {
  9527. /* http/2.0 */
  9528. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9529. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9530. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9531. }
  9532. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9533. {
  9534. /* http/2.0 */
  9535. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9536. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9537. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9538. }
  9539. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9540. {
  9541. /* spdy/3 */
  9542. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9543. char *proto = NULL;
  9544. word16 protoSz = 0;
  9545. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9546. /* check value */
  9547. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9548. if (proto) {
  9549. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9550. }
  9551. }
  9552. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9553. {
  9554. char *proto = NULL;
  9555. word16 protoSz = 0;
  9556. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9557. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9558. /* check value */
  9559. AssertIntEQ(1, (0 == protoSz));
  9560. AssertIntEQ(1, (NULL == proto));
  9561. }
  9562. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9563. {
  9564. /* http/1.1 */
  9565. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9566. char *proto;
  9567. word16 protoSz = 0;
  9568. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9569. /* check value */
  9570. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9571. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9572. }
  9573. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9574. {
  9575. /* spdy/2 */
  9576. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9577. char *proto;
  9578. word16 protoSz = 0;
  9579. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9580. /* check value */
  9581. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9582. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9583. }
  9584. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9585. {
  9586. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9587. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9588. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9589. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9590. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9591. char *clist = NULL;
  9592. word16 clistSz = 0;
  9593. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9594. &clistSz));
  9595. /* check value */
  9596. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9597. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9598. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9599. }
  9600. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  9601. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  9602. /* ALPN select callback, success with spdy/2 */
  9603. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  9604. unsigned char *outlen, const unsigned char *in,
  9605. unsigned int inlen, void *arg)
  9606. {
  9607. /* spdy/2 */
  9608. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9609. (void)ssl;
  9610. (void)arg;
  9611. /* adding +1 since LEN byte comes first */
  9612. if (inlen < sizeof(proto) + 1) {
  9613. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9614. }
  9615. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  9616. *out = in + 1;
  9617. *outlen = (unsigned char)sizeof(proto);
  9618. return SSL_TLSEXT_ERR_OK;
  9619. }
  9620. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9621. }
  9622. /* ALPN select callback, force failure */
  9623. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  9624. unsigned char *outlen, const unsigned char *in,
  9625. unsigned int inlen, void *arg)
  9626. {
  9627. (void)ssl;
  9628. (void)out;
  9629. (void)outlen;
  9630. (void)in;
  9631. (void)inlen;
  9632. (void)arg;
  9633. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9634. }
  9635. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  9636. {
  9637. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  9638. }
  9639. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  9640. {
  9641. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  9642. }
  9643. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  9644. static int test_wolfSSL_UseALPN_connection(void)
  9645. {
  9646. int res = TEST_SKIPPED;
  9647. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9648. callback_functions client_cb;
  9649. callback_functions server_cb;
  9650. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9651. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9652. client_cb.method = wolfSSLv23_client_method;
  9653. server_cb.method = wolfSSLv23_server_method;
  9654. client_cb.devId = testDevId;
  9655. server_cb.devId = testDevId;
  9656. /* success case same list */
  9657. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9658. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9659. test_wolfSSL_client_server(&client_cb, &server_cb);
  9660. /* success case only one for server */
  9661. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9662. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9663. test_wolfSSL_client_server(&client_cb, &server_cb);
  9664. /* success case only one for client */
  9665. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9666. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9667. test_wolfSSL_client_server(&client_cb, &server_cb);
  9668. /* success case none for client */
  9669. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9670. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9671. test_wolfSSL_client_server(&client_cb, &server_cb);
  9672. /* success case mismatch behavior but option 'continue' set */
  9673. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9674. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9675. test_wolfSSL_client_server(&client_cb, &server_cb);
  9676. /* success case read protocol send by client */
  9677. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9678. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9679. test_wolfSSL_client_server(&client_cb, &server_cb);
  9680. /* mismatch behavior with same list
  9681. * the first and only this one must be taken */
  9682. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9683. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  9684. test_wolfSSL_client_server(&client_cb, &server_cb);
  9685. /* default mismatch behavior */
  9686. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9687. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9688. test_wolfSSL_client_server(&client_cb, &server_cb);
  9689. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  9690. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  9691. /* WOLFSSL-level ALPN select callback tests */
  9692. /* Callback: success (one protocol, spdy/2) */
  9693. client_cb.ctx_ready = NULL;
  9694. client_cb.ssl_ready = use_ALPN_one;
  9695. client_cb.on_result = verify_ALPN_matching_spdy2;
  9696. server_cb.ctx_ready = NULL;
  9697. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  9698. server_cb.on_result = verify_ALPN_matching_spdy2;
  9699. test_wolfSSL_client_server(&client_cb, &server_cb);
  9700. /* Callback: failure (one client protocol, spdy/2) */
  9701. client_cb.ctx_ready = NULL;
  9702. client_cb.ssl_ready = use_ALPN_one;
  9703. client_cb.on_result = NULL;
  9704. server_cb.ctx_ready = NULL;
  9705. server_cb.ssl_ready = use_ALPN_failure_callback;
  9706. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9707. test_wolfSSL_client_server(&client_cb, &server_cb);
  9708. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  9709. res = TEST_RES_CHECK(1);
  9710. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9711. return res;
  9712. }
  9713. static int test_wolfSSL_UseALPN_params(void)
  9714. {
  9715. EXPECT_DECLS;
  9716. #ifndef NO_WOLFSSL_CLIENT
  9717. /* "http/1.1" */
  9718. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9719. /* "spdy/1" */
  9720. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  9721. /* "spdy/2" */
  9722. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9723. /* "spdy/3" */
  9724. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9725. char buff[256];
  9726. word32 idx;
  9727. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9728. WOLFSSL *ssl = wolfSSL_new(ctx);
  9729. ExpectNotNull(ctx);
  9730. ExpectNotNull(ssl);
  9731. /* error cases */
  9732. ExpectIntNE(WOLFSSL_SUCCESS,
  9733. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  9734. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9735. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  9736. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9737. /* success case */
  9738. /* http1 only */
  9739. ExpectIntEQ(WOLFSSL_SUCCESS,
  9740. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  9741. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9742. /* http1, spdy1 */
  9743. XMEMCPY(buff, http1, sizeof(http1));
  9744. idx = sizeof(http1);
  9745. buff[idx++] = ',';
  9746. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9747. idx += sizeof(spdy1);
  9748. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9749. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9750. /* http1, spdy2, spdy1 */
  9751. XMEMCPY(buff, http1, sizeof(http1));
  9752. idx = sizeof(http1);
  9753. buff[idx++] = ',';
  9754. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9755. idx += sizeof(spdy2);
  9756. buff[idx++] = ',';
  9757. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9758. idx += sizeof(spdy1);
  9759. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9760. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9761. /* spdy3, http1, spdy2, spdy1 */
  9762. XMEMCPY(buff, spdy3, sizeof(spdy3));
  9763. idx = sizeof(spdy3);
  9764. buff[idx++] = ',';
  9765. XMEMCPY(buff+idx, http1, sizeof(http1));
  9766. idx += sizeof(http1);
  9767. buff[idx++] = ',';
  9768. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9769. idx += sizeof(spdy2);
  9770. buff[idx++] = ',';
  9771. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9772. idx += sizeof(spdy1);
  9773. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9774. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9775. wolfSSL_free(ssl);
  9776. wolfSSL_CTX_free(ctx);
  9777. #endif
  9778. return EXPECT_RESULT();
  9779. }
  9780. #endif /* HAVE_ALPN */
  9781. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  9782. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  9783. {
  9784. unsigned char p[] = {
  9785. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9786. 6, 's', 'p', 'd', 'y', '/', '2',
  9787. 6, 's', 'p', 'd', 'y', '/', '1',
  9788. };
  9789. unsigned char p_len = sizeof(p);
  9790. int ret;
  9791. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  9792. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9793. AssertIntEQ(ret, 0);
  9794. #else
  9795. AssertIntEQ(ret, SSL_SUCCESS);
  9796. #endif
  9797. }
  9798. static void set_alpn_protos(SSL* ssl)
  9799. {
  9800. unsigned char p[] = {
  9801. 6, 's', 'p', 'd', 'y', '/', '3',
  9802. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9803. 6, 's', 'p', 'd', 'y', '/', '2',
  9804. 6, 's', 'p', 'd', 'y', '/', '1',
  9805. };
  9806. unsigned char p_len = sizeof(p);
  9807. int ret;
  9808. ret = SSL_set_alpn_protos(ssl, p, p_len);
  9809. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9810. AssertIntEQ(ret, 0);
  9811. #else
  9812. AssertIntEQ(ret, SSL_SUCCESS);
  9813. #endif
  9814. }
  9815. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  9816. {
  9817. /* "spdy/3" */
  9818. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9819. const unsigned char *proto;
  9820. unsigned int protoSz = 0;
  9821. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9822. /* check value */
  9823. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9824. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9825. }
  9826. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  9827. {
  9828. /* "http/1.1" */
  9829. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9830. const unsigned char *proto;
  9831. unsigned int protoSz = 0;
  9832. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9833. /* check value */
  9834. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9835. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9836. }
  9837. static int test_wolfSSL_set_alpn_protos(void)
  9838. {
  9839. int res = TEST_SKIPPED;
  9840. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9841. callback_functions client_cb;
  9842. callback_functions server_cb;
  9843. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9844. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9845. client_cb.method = wolfSSLv23_client_method;
  9846. server_cb.method = wolfSSLv23_server_method;
  9847. client_cb.devId = testDevId;
  9848. server_cb.devId = testDevId;
  9849. /* use CTX_alpn_protos */
  9850. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9851. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  9852. test_wolfSSL_client_server(&client_cb, &server_cb);
  9853. /* use set_alpn_protos */
  9854. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  9855. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  9856. test_wolfSSL_client_server(&client_cb, &server_cb);
  9857. res = TEST_SUCCESS;
  9858. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9859. return res;
  9860. }
  9861. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  9862. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  9863. {
  9864. EXPECT_DECLS;
  9865. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  9866. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9867. WOLFSSL *ssl = wolfSSL_new(ctx);
  9868. ExpectNotNull(ctx);
  9869. ExpectNotNull(ssl);
  9870. /* error cases */
  9871. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  9872. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  9873. /* success cases */
  9874. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  9875. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  9876. wolfSSL_free(ssl);
  9877. wolfSSL_CTX_free(ctx);
  9878. #endif
  9879. return EXPECT_RESULT();
  9880. }
  9881. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  9882. {
  9883. EXPECT_DECLS;
  9884. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  9885. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9886. WOLFSSL *ssl = wolfSSL_new(ctx);
  9887. ExpectNotNull(ctx);
  9888. ExpectNotNull(ssl);
  9889. /* error cases */
  9890. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  9891. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  9892. /* success cases */
  9893. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  9894. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  9895. wolfSSL_free(ssl);
  9896. wolfSSL_CTX_free(ctx);
  9897. #endif
  9898. return EXPECT_RESULT();
  9899. }
  9900. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  9901. static int test_wolfSSL_SCR_Reconnect(void)
  9902. {
  9903. EXPECT_DECLS;
  9904. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  9905. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  9906. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  9907. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9908. struct test_memio_ctx test_ctx;
  9909. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  9910. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  9911. byte data;
  9912. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9913. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  9914. test_ctx.s_ciphers =
  9915. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  9916. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9917. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9918. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  9919. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  9920. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  9921. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  9922. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9923. /* WOLFSSL_FATAL_ERROR since it will block */
  9924. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  9925. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9926. WOLFSSL_ERROR_WANT_READ);
  9927. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  9928. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9929. WOLFSSL_ERROR_WANT_READ);
  9930. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9931. wolfSSL_free(ssl_c);
  9932. ssl_c = NULL;
  9933. wolfSSL_free(ssl_s);
  9934. ssl_s = NULL;
  9935. wolfSSL_CTX_free(ctx_c);
  9936. ctx_c = NULL;
  9937. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  9938. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9939. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9940. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9941. wolfSSL_free(ssl_s);
  9942. wolfSSL_free(ssl_c);
  9943. wolfSSL_CTX_free(ctx_s);
  9944. wolfSSL_CTX_free(ctx_c);
  9945. #endif
  9946. return EXPECT_RESULT();
  9947. }
  9948. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  9949. (!defined(NO_RSA) || defined(HAVE_ECC))
  9950. /* Called when writing. */
  9951. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9952. {
  9953. (void)ssl;
  9954. (void)buf;
  9955. (void)sz;
  9956. (void)ctx;
  9957. /* Force error return from wolfSSL_accept_TLSv13(). */
  9958. return WANT_WRITE;
  9959. }
  9960. /* Called when reading. */
  9961. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9962. {
  9963. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  9964. int len = (int)msg->length;
  9965. (void)ssl;
  9966. (void)sz;
  9967. /* Pass back as much of message as will fit in buffer. */
  9968. if (len > sz)
  9969. len = sz;
  9970. XMEMCPY(buf, msg->buffer, len);
  9971. /* Move over returned data. */
  9972. msg->buffer += len;
  9973. msg->length -= len;
  9974. /* Amount actually copied. */
  9975. return len;
  9976. }
  9977. #endif
  9978. /* Test the detection of duplicate known TLS extensions.
  9979. * Specifically in a ClientHello.
  9980. */
  9981. static int test_tls_ext_duplicate(void)
  9982. {
  9983. EXPECT_DECLS;
  9984. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  9985. !defined(NO_FILESYSTEM)
  9986. const unsigned char clientHelloDupTlsExt[] = {
  9987. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  9988. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  9989. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  9990. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  9991. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  9992. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  9993. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  9994. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  9995. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9996. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  9997. 0x00, 0x9e, 0x01, 0x00,
  9998. /* Extensions - duplicate signature algorithms. */
  9999. 0x00, 0x19, 0x00, 0x0d,
  10000. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10001. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10002. /* Supported Versions extension for TLS 1.3. */
  10003. 0x00, 0x2b,
  10004. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10005. };
  10006. WOLFSSL_BUFFER_INFO msg;
  10007. const char* testCertFile;
  10008. const char* testKeyFile;
  10009. WOLFSSL_CTX *ctx = NULL;
  10010. WOLFSSL *ssl = NULL;
  10011. #ifndef NO_RSA
  10012. testCertFile = svrCertFile;
  10013. testKeyFile = svrKeyFile;
  10014. #elif defined(HAVE_ECC)
  10015. testCertFile = eccCertFile;
  10016. testKeyFile = eccKeyFile;
  10017. #endif
  10018. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10019. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10020. WOLFSSL_FILETYPE_PEM));
  10021. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10022. WOLFSSL_FILETYPE_PEM));
  10023. /* Read from 'msg'. */
  10024. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10025. /* No where to send to - dummy sender. */
  10026. wolfSSL_SetIOSend(ctx, DummySend);
  10027. ssl = wolfSSL_new(ctx);
  10028. ExpectNotNull(ssl);
  10029. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10030. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10031. wolfSSL_SetIOReadCtx(ssl, &msg);
  10032. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10033. /* can return duplicate ext error or socket error if the peer closed down
  10034. * while sending alert */
  10035. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10036. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10037. }
  10038. wolfSSL_free(ssl);
  10039. wolfSSL_CTX_free(ctx);
  10040. #endif
  10041. return EXPECT_RESULT();
  10042. }
  10043. /*----------------------------------------------------------------------------*
  10044. | X509 Tests
  10045. *----------------------------------------------------------------------------*/
  10046. static int test_wolfSSL_X509_NAME_get_entry(void)
  10047. {
  10048. EXPECT_DECLS;
  10049. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10050. #if defined(OPENSSL_ALL) || \
  10051. (defined(OPENSSL_EXTRA) && \
  10052. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10053. /* use openssl like name to test mapping */
  10054. X509_NAME_ENTRY* ne = NULL;
  10055. X509_NAME* name = NULL;
  10056. X509* x509 = NULL;
  10057. #ifndef NO_FILESYSTEM
  10058. ASN1_STRING* asn = NULL;
  10059. char* subCN = NULL;
  10060. #endif
  10061. int idx = 0;
  10062. ASN1_OBJECT *object = NULL;
  10063. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10064. defined(WOLFSSL_NGINX)
  10065. #ifndef NO_BIO
  10066. BIO* bio = NULL;
  10067. #endif
  10068. #endif
  10069. #ifndef NO_FILESYSTEM
  10070. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10071. WOLFSSL_FILETYPE_PEM));
  10072. ExpectNotNull(name = X509_get_subject_name(x509));
  10073. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10074. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10075. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10076. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10077. wolfSSL_FreeX509(x509);
  10078. x509 = NULL;
  10079. #endif
  10080. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10081. WOLFSSL_FILETYPE_PEM));
  10082. ExpectNotNull(name = X509_get_subject_name(x509));
  10083. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10084. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10085. defined(WOLFSSL_NGINX)
  10086. #ifndef NO_BIO
  10087. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10088. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10089. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10090. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10091. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10092. BIO_free(bio);
  10093. #endif
  10094. #endif
  10095. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10096. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10097. wolfSSL_FreeX509(x509);
  10098. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10099. #endif /* !NO_CERTS && !NO_RSA */
  10100. return EXPECT_RESULT();
  10101. }
  10102. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10103. static int test_wolfSSL_PKCS12(void)
  10104. {
  10105. EXPECT_DECLS;
  10106. /* .p12 file is encrypted with DES3 */
  10107. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10108. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10109. * Password Key
  10110. */
  10111. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10112. !defined(NO_STDIO_FILESYSTEM) && \
  10113. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10114. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10115. byte buf[6000];
  10116. char file[] = "./certs/test-servercert.p12";
  10117. char order[] = "./certs/ecc-rsa-server.p12";
  10118. #ifdef WC_RC2
  10119. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10120. #endif
  10121. char pass[] = "a password";
  10122. const char goodPsw[] = "wolfSSL test";
  10123. const char badPsw[] = "bad";
  10124. #ifdef HAVE_ECC
  10125. WOLFSSL_X509_NAME *subject = NULL;
  10126. WOLFSSL_X509 *x509 = NULL;
  10127. #endif
  10128. XFILE f = XBADFILE;
  10129. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10130. WOLFSSL_BIO *bio = NULL;
  10131. WOLFSSL_EVP_PKEY *pkey = NULL;
  10132. WC_PKCS12 *pkcs12 = NULL;
  10133. WC_PKCS12 *pkcs12_2 = NULL;
  10134. WOLFSSL_X509 *cert = NULL;
  10135. WOLFSSL_X509 *tmp = NULL;
  10136. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10137. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10138. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10139. WOLFSSL_CTX *ctx = NULL;
  10140. WOLFSSL *ssl = NULL;
  10141. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10142. #endif
  10143. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10144. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10145. if (f != XBADFILE) {
  10146. XFCLOSE(f);
  10147. f = XBADFILE;
  10148. }
  10149. goodPswLen = (int)XSTRLEN(goodPsw);
  10150. badPswLen = (int)XSTRLEN(badPsw);
  10151. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10152. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10153. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10154. ExpectNotNull(pkcs12);
  10155. BIO_free(bio);
  10156. bio = NULL;
  10157. /* check verify MAC directly */
  10158. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10159. /* check verify MAC fail case directly */
  10160. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10161. /* check verify MAC fail case */
  10162. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10163. ExpectNull(pkey);
  10164. ExpectNull(cert);
  10165. /* check parse with no extra certs kept */
  10166. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10167. 1);
  10168. ExpectNotNull(pkey);
  10169. ExpectNotNull(cert);
  10170. wolfSSL_EVP_PKEY_free(pkey);
  10171. pkey = NULL;
  10172. wolfSSL_X509_free(cert);
  10173. cert = NULL;
  10174. /* check parse with extra certs kept */
  10175. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10176. 1);
  10177. ExpectNotNull(pkey);
  10178. ExpectNotNull(cert);
  10179. ExpectNotNull(ca);
  10180. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10181. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10182. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10183. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10184. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10185. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10186. #else
  10187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10188. #endif
  10189. /* Copy stack structure */
  10190. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10191. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10192. /* CTX now owns the tmp_ca stack structure */
  10193. tmp_ca = NULL;
  10194. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10195. ExpectNotNull(tmp_ca);
  10196. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10197. /* Check that the main cert is also set */
  10198. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10199. ExpectNotNull(ssl = SSL_new(ctx));
  10200. ExpectNotNull(SSL_get_certificate(ssl));
  10201. SSL_free(ssl);
  10202. SSL_CTX_free(ctx);
  10203. ctx = NULL;
  10204. #endif
  10205. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10206. /* should be 2 other certs on stack */
  10207. ExpectNotNull(tmp = sk_X509_pop(ca));
  10208. X509_free(tmp);
  10209. ExpectNotNull(tmp = sk_X509_pop(ca));
  10210. X509_free(tmp);
  10211. ExpectNull(sk_X509_pop(ca));
  10212. EVP_PKEY_free(pkey);
  10213. pkey = NULL;
  10214. X509_free(cert);
  10215. cert = NULL;
  10216. sk_X509_pop_free(ca, X509_free);
  10217. ca = NULL;
  10218. /* check PKCS12_create */
  10219. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10220. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10221. SSL_SUCCESS);
  10222. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10223. -1, -1, 100, -1, 0)));
  10224. EVP_PKEY_free(pkey);
  10225. pkey = NULL;
  10226. X509_free(cert);
  10227. cert = NULL;
  10228. sk_X509_pop_free(ca, NULL);
  10229. ca = NULL;
  10230. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10231. SSL_SUCCESS);
  10232. PKCS12_free(pkcs12_2);
  10233. pkcs12_2 = NULL;
  10234. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10235. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10236. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10237. 2000, 1, 0)));
  10238. EVP_PKEY_free(pkey);
  10239. pkey = NULL;
  10240. X509_free(cert);
  10241. cert = NULL;
  10242. sk_X509_pop_free(ca, NULL);
  10243. ca = NULL;
  10244. /* convert to DER then back and parse */
  10245. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10246. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10247. PKCS12_free(pkcs12_2);
  10248. pkcs12_2 = NULL;
  10249. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10250. BIO_free(bio);
  10251. bio = NULL;
  10252. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10253. SSL_SUCCESS);
  10254. /* should be 2 other certs on stack */
  10255. ExpectNotNull(tmp = sk_X509_pop(ca));
  10256. X509_free(tmp);
  10257. ExpectNotNull(tmp = sk_X509_pop(ca));
  10258. X509_free(tmp);
  10259. ExpectNull(sk_X509_pop(ca));
  10260. #ifndef NO_RC4
  10261. PKCS12_free(pkcs12_2);
  10262. pkcs12_2 = NULL;
  10263. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10264. NID_pbe_WithSHA1And128BitRC4,
  10265. NID_pbe_WithSHA1And128BitRC4,
  10266. 2000, 1, 0)));
  10267. EVP_PKEY_free(pkey);
  10268. pkey = NULL;
  10269. X509_free(cert);
  10270. cert = NULL;
  10271. sk_X509_pop_free(ca, NULL);
  10272. ca = NULL;
  10273. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10274. SSL_SUCCESS);
  10275. #endif /* NO_RC4 */
  10276. EVP_PKEY_free(pkey);
  10277. pkey = NULL;
  10278. X509_free(cert);
  10279. cert = NULL;
  10280. PKCS12_free(pkcs12);
  10281. pkcs12 = NULL;
  10282. PKCS12_free(pkcs12_2);
  10283. pkcs12_2 = NULL;
  10284. sk_X509_pop_free(ca, NULL);
  10285. ca = NULL;
  10286. #ifdef HAVE_ECC
  10287. /* test order of parsing */
  10288. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10289. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10290. if (f != XBADFILE) {
  10291. XFCLOSE(f);
  10292. f = XBADFILE;
  10293. }
  10294. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10295. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10296. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10297. WOLFSSL_SUCCESS);
  10298. /* check use of pkey after parse */
  10299. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10300. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10301. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10302. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10303. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10304. #else
  10305. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10306. #endif
  10307. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10308. SSL_CTX_free(ctx);
  10309. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10310. #endif
  10311. ExpectNotNull(pkey);
  10312. ExpectNotNull(cert);
  10313. ExpectNotNull(ca);
  10314. /* compare subject lines of certificates */
  10315. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10316. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10317. SSL_FILETYPE_PEM));
  10318. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10319. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10320. X509_free(x509);
  10321. x509 = NULL;
  10322. /* test expected fail case */
  10323. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10324. SSL_FILETYPE_PEM));
  10325. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10326. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10327. X509_free(x509);
  10328. x509 = NULL;
  10329. X509_free(cert);
  10330. cert = NULL;
  10331. /* get subject line from ca stack */
  10332. ExpectNotNull(cert = sk_X509_pop(ca));
  10333. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10334. /* compare subject from certificate in ca to expected */
  10335. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10336. SSL_FILETYPE_PEM));
  10337. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10338. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10339. EVP_PKEY_free(pkey);
  10340. pkey = NULL;
  10341. X509_free(x509);
  10342. x509 = NULL;
  10343. X509_free(cert);
  10344. cert = NULL;
  10345. BIO_free(bio);
  10346. bio = NULL;
  10347. PKCS12_free(pkcs12);
  10348. pkcs12 = NULL;
  10349. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10350. ca = NULL;
  10351. /* test order of parsing */
  10352. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10353. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10354. if (f != XBADFILE) {
  10355. XFCLOSE(f);
  10356. f = XBADFILE;
  10357. }
  10358. /* check verify MAC fail case */
  10359. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10360. ExpectNull(pkey);
  10361. ExpectNull(cert);
  10362. /* check parse with no extra certs kept */
  10363. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10364. 1);
  10365. ExpectNotNull(pkey);
  10366. ExpectNotNull(cert);
  10367. wolfSSL_EVP_PKEY_free(pkey);
  10368. pkey = NULL;
  10369. wolfSSL_X509_free(cert);
  10370. cert = NULL;
  10371. /* check parse with extra certs kept */
  10372. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10373. 1);
  10374. ExpectNotNull(pkey);
  10375. ExpectNotNull(cert);
  10376. ExpectNotNull(ca);
  10377. wolfSSL_EVP_PKEY_free(pkey);
  10378. pkey = NULL;
  10379. wolfSSL_X509_free(cert);
  10380. cert = NULL;
  10381. sk_X509_pop_free(ca, NULL);
  10382. ca = NULL;
  10383. PKCS12_free(pkcs12);
  10384. pkcs12 = NULL;
  10385. #endif /* HAVE_ECC */
  10386. #ifdef WC_RC2
  10387. /* test PKCS#12 with RC2 encryption */
  10388. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10389. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10390. if (f != XBADFILE) {
  10391. XFCLOSE(f);
  10392. f = XBADFILE;
  10393. }
  10394. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10395. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10396. /* check verify MAC fail case */
  10397. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10398. ExpectNull(pkey);
  10399. ExpectNull(cert);
  10400. /* check parse with not extra certs kept */
  10401. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10402. WOLFSSL_SUCCESS);
  10403. ExpectNotNull(pkey);
  10404. ExpectNotNull(cert);
  10405. wolfSSL_EVP_PKEY_free(pkey);
  10406. pkey = NULL;
  10407. wolfSSL_X509_free(cert);
  10408. cert = NULL;
  10409. /* check parse with extra certs kept */
  10410. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10411. WOLFSSL_SUCCESS);
  10412. ExpectNotNull(pkey);
  10413. ExpectNotNull(cert);
  10414. ExpectNotNull(ca);
  10415. wolfSSL_EVP_PKEY_free(pkey);
  10416. wolfSSL_X509_free(cert);
  10417. sk_X509_pop_free(ca, NULL);
  10418. BIO_free(bio);
  10419. bio = NULL;
  10420. PKCS12_free(pkcs12);
  10421. pkcs12 = NULL;
  10422. #endif /* WC_RC2 */
  10423. /* Test i2d_PKCS12_bio */
  10424. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10425. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10426. if (f != XBADFILE)
  10427. XFCLOSE(f);
  10428. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10429. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10430. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10431. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10432. PKCS12_free(pkcs12);
  10433. BIO_free(bio);
  10434. (void)order;
  10435. #endif /* OPENSSL_EXTRA */
  10436. #endif /* HAVE_FIPS */
  10437. return EXPECT_RESULT();
  10438. }
  10439. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10440. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10441. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10442. #define TEST_PKCS8_ENC
  10443. #endif
  10444. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10445. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10446. /* used to keep track if FailTestCallback was called */
  10447. static int failTestCallbackCalled = 0;
  10448. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10449. {
  10450. (void)passwd;
  10451. (void)sz;
  10452. (void)rw;
  10453. (void)userdata;
  10454. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10455. failTestCallbackCalled = 1;
  10456. return -1;
  10457. }
  10458. #endif
  10459. static int test_wolfSSL_no_password_cb(void)
  10460. {
  10461. EXPECT_DECLS;
  10462. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10463. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10464. WOLFSSL_CTX* ctx = NULL;
  10465. byte buff[FOURK_BUF];
  10466. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10467. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10468. XFILE f = XBADFILE;
  10469. int bytes = 0;
  10470. #ifndef NO_WOLFSSL_CLIENT
  10471. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10472. #else
  10473. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10474. #endif
  10475. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10476. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10477. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10478. if (f != XBADFILE) {
  10479. XFCLOSE(f);
  10480. f = XBADFILE;
  10481. }
  10482. ExpectIntLE(bytes, sizeof(buff));
  10483. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10484. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10485. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10486. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10487. if (f != XBADFILE)
  10488. XFCLOSE(f);
  10489. ExpectIntLE(bytes, sizeof(buff));
  10490. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10491. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10492. wolfSSL_CTX_free(ctx);
  10493. /* Password callback should not be called by default */
  10494. ExpectIntEQ(failTestCallbackCalled, 0);
  10495. #endif
  10496. return EXPECT_RESULT();
  10497. }
  10498. #ifdef TEST_PKCS8_ENC
  10499. /* for PKCS8 test case */
  10500. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10501. {
  10502. int flag = 0;
  10503. (void)rw;
  10504. if (userdata != NULL) {
  10505. flag = *((int*)userdata); /* user set data */
  10506. }
  10507. switch (flag) {
  10508. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10509. * can be anything the user wishes to be passed to the callback
  10510. * associated with the WOLFSSL_CTX */
  10511. XSTRNCPY(passwd, "yassl123", sz);
  10512. return 8;
  10513. default:
  10514. return BAD_FUNC_ARG;
  10515. }
  10516. }
  10517. #endif /* TEST_PKCS8_ENC */
  10518. /* Testing functions dealing with PKCS8 */
  10519. static int test_wolfSSL_PKCS8(void)
  10520. {
  10521. EXPECT_DECLS;
  10522. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10523. !defined(WOLFCRYPT_ONLY)
  10524. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10525. byte buff[FOURK_BUF];
  10526. byte der[FOURK_BUF];
  10527. #ifndef NO_RSA
  10528. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10529. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10530. #endif
  10531. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10532. #ifdef HAVE_ECC
  10533. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10534. #endif
  10535. XFILE f = XBADFILE;
  10536. int bytes = 0;
  10537. WOLFSSL_CTX* ctx = NULL;
  10538. #if defined(HAVE_ECC) && !defined(NO_CODING)
  10539. int ret;
  10540. ecc_key key;
  10541. word32 x = 0;
  10542. #endif
  10543. #ifdef TEST_PKCS8_ENC
  10544. #if !defined(NO_RSA) && !defined(NO_SHA)
  10545. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10546. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10547. #endif
  10548. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10549. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10550. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10551. #endif
  10552. int flag;
  10553. #endif
  10554. (void)der;
  10555. #ifndef NO_WOLFSSL_CLIENT
  10556. #ifndef WOLFSSL_NO_TLS12
  10557. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10558. #else
  10559. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10560. #endif
  10561. #else
  10562. #ifndef WOLFSSL_NO_TLS12
  10563. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10564. #else
  10565. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10566. #endif
  10567. #endif
  10568. #ifdef TEST_PKCS8_ENC
  10569. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10570. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10571. flag = 1; /* used by password callback as return code */
  10572. #if !defined(NO_RSA) && !defined(NO_SHA)
  10573. /* test loading PEM PKCS8 encrypted file */
  10574. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10575. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10576. if (f != XBADFILE) {
  10577. XFCLOSE(f);
  10578. f = XBADFILE;
  10579. }
  10580. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10581. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10582. /* this next case should fail because of password callback return code */
  10583. flag = 0; /* used by password callback as return code */
  10584. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10585. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10586. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10587. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10588. "yassl123"), 0);
  10589. /* test that error value is returned with a bad password */
  10590. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10591. "bad"), 0);
  10592. /* test loading PEM PKCS8 encrypted file */
  10593. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10594. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10595. if (f != XBADFILE) {
  10596. XFCLOSE(f);
  10597. f = XBADFILE;
  10598. }
  10599. flag = 1; /* used by password callback as return code */
  10600. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10601. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10602. /* this next case should fail because of password callback return code */
  10603. flag = 0; /* used by password callback as return code */
  10604. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10605. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10606. #endif /* !NO_RSA && !NO_SHA */
  10607. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10608. /* test loading PEM PKCS8 encrypted ECC Key file */
  10609. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  10610. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10611. if (f != XBADFILE) {
  10612. XFCLOSE(f);
  10613. f = XBADFILE;
  10614. }
  10615. flag = 1; /* used by password callback as return code */
  10616. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10617. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10618. /* this next case should fail because of password callback return code */
  10619. flag = 0; /* used by password callback as return code */
  10620. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10621. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10622. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10623. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10624. "yassl123"), 0);
  10625. /* test that error value is returned with a bad password */
  10626. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10627. "bad"), 0);
  10628. /* test loading DER PKCS8 encrypted ECC Key file */
  10629. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  10630. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10631. if (f != XBADFILE) {
  10632. XFCLOSE(f);
  10633. f = XBADFILE;
  10634. }
  10635. flag = 1; /* used by password callback as return code */
  10636. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10637. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10638. /* this next case should fail because of password callback return code */
  10639. flag = 0; /* used by password callback as return code */
  10640. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10641. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10642. /* leave flag as "okay" */
  10643. flag = 1;
  10644. #endif /* HAVE_ECC && !NO_SHA */
  10645. #endif /* TEST_PKCS8_ENC */
  10646. #ifndef NO_RSA
  10647. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  10648. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  10649. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10650. if (f != XBADFILE) {
  10651. XFCLOSE(f);
  10652. f = XBADFILE;
  10653. }
  10654. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10655. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10656. /* test loading PEM PKCS8 private key file (not encrypted) */
  10657. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  10658. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10659. if (f != XBADFILE) {
  10660. XFCLOSE(f);
  10661. f = XBADFILE;
  10662. }
  10663. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10664. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10665. #endif /* !NO_RSA */
  10666. /* Test PKCS8 PEM ECC key no crypt */
  10667. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10668. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10669. if (f != XBADFILE) {
  10670. XFCLOSE(f);
  10671. f = XBADFILE;
  10672. }
  10673. #ifdef HAVE_ECC
  10674. /* Test PKCS8 PEM ECC key no crypt */
  10675. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10676. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10677. #ifndef NO_CODING
  10678. /* decrypt PKCS8 PEM to key in DER format */
  10679. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  10680. (word32)sizeof(der), NULL)), 0);
  10681. ret = wc_ecc_init(&key);
  10682. if (ret == 0) {
  10683. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  10684. wc_ecc_free(&key);
  10685. }
  10686. ExpectIntEQ(ret, 0);
  10687. #endif
  10688. /* Test PKCS8 DER ECC key no crypt */
  10689. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10690. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10691. if (f != XBADFILE)
  10692. XFCLOSE(f);
  10693. /* Test using a PKCS8 ECC PEM */
  10694. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10695. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10696. #else
  10697. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  10698. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  10699. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  10700. #endif /* HAVE_ECC */
  10701. wolfSSL_CTX_free(ctx);
  10702. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10703. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  10704. return EXPECT_RESULT();
  10705. }
  10706. static int test_wolfSSL_PKCS8_ED25519(void)
  10707. {
  10708. EXPECT_DECLS;
  10709. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10710. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  10711. defined(HAVE_ED25519_KEY_IMPORT)
  10712. const byte encPrivKey[] = \
  10713. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10714. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  10715. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  10716. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  10717. "2L9W4v7swXkV+X+a1ww=\n"
  10718. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10719. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10720. byte der[FOURK_BUF];
  10721. WOLFSSL_CTX* ctx = NULL;
  10722. int bytes;
  10723. XMEMSET(der, 0, sizeof(der));
  10724. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10725. (word32)sizeof(der), password)), 0);
  10726. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10727. #ifndef NO_WOLFSSL_SERVER
  10728. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10729. #else
  10730. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10731. #endif
  10732. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10733. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10734. wolfSSL_CTX_free(ctx);
  10735. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10736. #endif
  10737. return EXPECT_RESULT();
  10738. }
  10739. static int test_wolfSSL_PKCS8_ED448(void)
  10740. {
  10741. EXPECT_DECLS;
  10742. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10743. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  10744. defined(HAVE_ED448_KEY_IMPORT)
  10745. const byte encPrivKey[] = \
  10746. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10747. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  10748. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  10749. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  10750. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  10751. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10752. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10753. byte der[FOURK_BUF];
  10754. WOLFSSL_CTX* ctx = NULL;
  10755. int bytes;
  10756. XMEMSET(der, 0, sizeof(der));
  10757. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10758. (word32)sizeof(der), password)), 0);
  10759. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10760. #ifndef NO_WOLFSSL_SERVER
  10761. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10762. #else
  10763. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10764. #endif
  10765. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10766. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10767. wolfSSL_CTX_free(ctx);
  10768. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10769. #endif
  10770. return EXPECT_RESULT();
  10771. }
  10772. /* Testing functions dealing with PKCS5 */
  10773. static int test_wolfSSL_PKCS5(void)
  10774. {
  10775. EXPECT_DECLS;
  10776. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  10777. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  10778. const char* passwd = "myfipsPa$$W0rd";
  10779. #else
  10780. const char *passwd = "pass1234";
  10781. #endif
  10782. const unsigned char *salt = (unsigned char *)"salt1234";
  10783. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  10784. DYNAMIC_TYPE_TMP_BUFFER);
  10785. int ret = 0;
  10786. ExpectNotNull(out);
  10787. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  10788. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  10789. WOLFSSL_SUCCESS);
  10790. #ifdef WOLFSSL_SHA512
  10791. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  10792. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  10793. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  10794. #endif
  10795. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10796. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  10797. return EXPECT_RESULT();
  10798. }
  10799. /* test parsing URI from certificate */
  10800. static int test_wolfSSL_URI(void)
  10801. {
  10802. EXPECT_DECLS;
  10803. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10804. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  10805. defined(OPENSSL_EXTRA))
  10806. WOLFSSL_X509* x509 = NULL;
  10807. const char uri[] = "./certs/client-uri-cert.pem";
  10808. const char urn[] = "./certs/client-absolute-urn.pem";
  10809. const char badUri[] = "./certs/client-relative-uri.pem";
  10810. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  10811. WOLFSSL_FILETYPE_PEM));
  10812. wolfSSL_FreeX509(x509);
  10813. x509 = NULL;
  10814. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  10815. WOLFSSL_FILETYPE_PEM));
  10816. wolfSSL_FreeX509(x509);
  10817. x509 = NULL;
  10818. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  10819. && !defined(WOLFSSL_FPKI)
  10820. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10821. WOLFSSL_FILETYPE_PEM));
  10822. #else
  10823. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10824. WOLFSSL_FILETYPE_PEM));
  10825. #endif
  10826. wolfSSL_FreeX509(x509);
  10827. #endif
  10828. return EXPECT_RESULT();
  10829. }
  10830. static int test_wolfSSL_TBS(void)
  10831. {
  10832. EXPECT_DECLS;
  10833. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10834. && defined(OPENSSL_EXTRA)
  10835. WOLFSSL_X509* x509 = NULL;
  10836. const unsigned char* tbs;
  10837. int tbsSz;
  10838. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  10839. WOLFSSL_FILETYPE_PEM));
  10840. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  10841. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  10842. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  10843. ExpectIntEQ(tbsSz, 1003);
  10844. wolfSSL_FreeX509(x509);
  10845. #endif
  10846. return EXPECT_RESULT();
  10847. }
  10848. static int test_wolfSSL_X509_verify(void)
  10849. {
  10850. EXPECT_DECLS;
  10851. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  10852. defined(OPENSSL_EXTRA)
  10853. WOLFSSL_X509* ca = NULL;
  10854. WOLFSSL_X509* serv = NULL;
  10855. WOLFSSL_EVP_PKEY* pkey = NULL;
  10856. unsigned char buf[2048];
  10857. const unsigned char* pt = NULL;
  10858. int bufSz;
  10859. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  10860. WOLFSSL_FILETYPE_PEM));
  10861. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  10862. WOLFSSL_SUCCESS);
  10863. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  10864. WOLFSSL_SUCCESS);
  10865. ExpectIntEQ(bufSz, 294);
  10866. bufSz = 2048;
  10867. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  10868. WOLFSSL_SUCCESS);
  10869. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  10870. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  10871. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  10872. WOLFSSL_FILETYPE_PEM));
  10873. /* success case */
  10874. pt = buf;
  10875. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10876. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  10877. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  10878. wolfSSL_EVP_PKEY_free(pkey);
  10879. pkey = NULL;
  10880. /* fail case */
  10881. bufSz = 2048;
  10882. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  10883. WOLFSSL_SUCCESS);
  10884. pt = buf;
  10885. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10886. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  10887. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  10888. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  10889. wolfSSL_EVP_PKEY_free(pkey);
  10890. wolfSSL_FreeX509(ca);
  10891. wolfSSL_FreeX509(serv);
  10892. #endif
  10893. return EXPECT_RESULT();
  10894. }
  10895. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10896. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10897. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10898. /* create certificate with version 2 */
  10899. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  10900. {
  10901. EXPECT_DECLS;
  10902. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  10903. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  10904. unsigned char *der = NULL, *key = NULL, *pt;
  10905. char *header = NULL, *name = NULL;
  10906. int derSz;
  10907. long keySz;
  10908. XFILE fp = XBADFILE;
  10909. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  10910. time_t t;
  10911. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10912. WOLFSSL_FILETYPE_PEM));
  10913. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  10914. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  10915. WOLFSSL_SUCCESS);
  10916. if (fp != XBADFILE)
  10917. XFCLOSE(fp);
  10918. pt = key;
  10919. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  10920. (const unsigned char**)&pt, keySz));
  10921. /* create the version 2 certificate */
  10922. ExpectNotNull(x509v2 = X509_new());
  10923. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  10924. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  10925. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  10926. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  10927. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  10928. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  10929. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  10930. t = time(NULL);
  10931. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  10932. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  10933. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  10934. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  10935. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  10936. derSz = wolfSSL_i2d_X509(x509v2, &der);
  10937. ExpectIntGT(derSz, 0);
  10938. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  10939. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10940. /* TODO: Replace with API call */
  10941. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  10942. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10943. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10944. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10945. wolfSSL_X509_free(x509);
  10946. wolfSSL_X509_free(x509v2);
  10947. wolfSSL_EVP_PKEY_free(priv);
  10948. wolfSSL_EVP_PKEY_free(pub);
  10949. wolfSSL_ASN1_TIME_free(notBefore);
  10950. wolfSSL_ASN1_TIME_free(notAfter);
  10951. return EXPECT_RESULT();
  10952. }
  10953. /* override certificate version error */
  10954. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  10955. {
  10956. EXPECT_DECLS;
  10957. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10958. ExpectIntEQ(store->error, ASN_VERSION_E);
  10959. #else
  10960. ExpectIntEQ(store->error, 0);
  10961. #endif
  10962. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  10963. (void)preverify;
  10964. return EXPECT_RESULT() == TEST_SUCCESS;
  10965. }
  10966. /* set verify callback that will override bad certificate version */
  10967. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  10968. {
  10969. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  10970. return TEST_SUCCESS;
  10971. }
  10972. #endif
  10973. static int test_wolfSSL_X509_TLS_version_test_1(void)
  10974. {
  10975. EXPECT_DECLS;
  10976. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10977. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10978. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10979. test_ssl_cbf func_cb_client;
  10980. test_ssl_cbf func_cb_server;
  10981. /* test server rejects a client certificate that is not version 3 */
  10982. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10983. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10984. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10985. #ifndef WOLFSSL_NO_TLS12
  10986. func_cb_client.method = wolfTLSv1_2_client_method;
  10987. #else
  10988. func_cb_client.method = wolfTLSv1_3_client_method;
  10989. #endif
  10990. #ifndef WOLFSSL_NO_TLS12
  10991. func_cb_server.method = wolfTLSv1_2_server_method;
  10992. #else
  10993. func_cb_server.method = wolfTLSv1_3_server_method;
  10994. #endif
  10995. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10996. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10997. &func_cb_server, NULL), TEST_FAIL);
  10998. #else
  10999. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11000. &func_cb_server, NULL), TEST_SUCCESS);
  11001. #endif
  11002. #endif
  11003. return EXPECT_RESULT();
  11004. }
  11005. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11006. {
  11007. EXPECT_DECLS;
  11008. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11009. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11010. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11011. test_ssl_cbf func_cb_client;
  11012. test_ssl_cbf func_cb_server;
  11013. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11014. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11015. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11016. func_cb_server.ctx_ready = &test_set_override_x509;
  11017. #ifndef WOLFSSL_NO_TLS12
  11018. func_cb_client.method = wolfTLSv1_2_client_method;
  11019. #else
  11020. func_cb_client.method = wolfTLSv1_3_client_method;
  11021. #endif
  11022. #ifndef WOLFSSL_NO_TLS12
  11023. func_cb_server.method = wolfTLSv1_2_server_method;
  11024. #else
  11025. func_cb_server.method = wolfTLSv1_3_server_method;
  11026. #endif
  11027. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11028. &func_cb_server, NULL), TEST_SUCCESS);
  11029. #endif
  11030. return EXPECT_RESULT();
  11031. }
  11032. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11033. * version allowed.
  11034. * POST: 1 on success.
  11035. */
  11036. static int test_wolfSSL_CTX_SetMinVersion(void)
  11037. {
  11038. int res = TEST_SKIPPED;
  11039. #ifndef NO_WOLFSSL_CLIENT
  11040. int failFlag = WOLFSSL_SUCCESS;
  11041. WOLFSSL_CTX* ctx;
  11042. int itr;
  11043. #ifndef NO_OLD_TLS
  11044. const int versions[] = {
  11045. #ifdef WOLFSSL_ALLOW_TLSV10
  11046. WOLFSSL_TLSV1,
  11047. #endif
  11048. WOLFSSL_TLSV1_1,
  11049. WOLFSSL_TLSV1_2 };
  11050. #elif !defined(WOLFSSL_NO_TLS12)
  11051. const int versions[] = { WOLFSSL_TLSV1_2 };
  11052. #elif defined(WOLFSSL_TLS13)
  11053. const int versions[] = { WOLFSSL_TLSV1_3 };
  11054. #else
  11055. const int versions[0];
  11056. #endif
  11057. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11058. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11059. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11060. != WOLFSSL_SUCCESS) {
  11061. failFlag = WOLFSSL_FAILURE;
  11062. }
  11063. }
  11064. wolfSSL_CTX_free(ctx);
  11065. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11066. #endif
  11067. return res;
  11068. } /* END test_wolfSSL_CTX_SetMinVersion */
  11069. /*----------------------------------------------------------------------------*
  11070. | OCSP Stapling
  11071. *----------------------------------------------------------------------------*/
  11072. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11073. * need to contact the CA, lowering the cost of cert revocation checking.
  11074. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11075. * POST: 1 returned for success.
  11076. */
  11077. static int test_wolfSSL_UseOCSPStapling(void)
  11078. {
  11079. EXPECT_DECLS;
  11080. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11081. !defined(NO_WOLFSSL_CLIENT)
  11082. WOLFSSL_CTX* ctx = NULL;
  11083. WOLFSSL* ssl = NULL;
  11084. #ifndef NO_WOLFSSL_CLIENT
  11085. #ifndef WOLFSSL_NO_TLS12
  11086. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11087. #else
  11088. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11089. #endif
  11090. #else
  11091. #ifndef WOLFSSL_NO_TLS12
  11092. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11093. #else
  11094. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11095. #endif
  11096. #endif
  11097. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11098. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11099. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11100. #ifndef NO_WOLFSSL_CLIENT
  11101. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11102. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11103. #else
  11104. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11105. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11106. #endif
  11107. wolfSSL_free(ssl);
  11108. wolfSSL_CTX_free(ctx);
  11109. #endif
  11110. return EXPECT_RESULT();
  11111. } /* END test_wolfSSL_UseOCSPStapling */
  11112. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11113. * stapling eliminates the need to contact the CA and lowers cert revocation
  11114. * check.
  11115. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11116. */
  11117. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11118. {
  11119. EXPECT_DECLS;
  11120. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11121. !defined(NO_WOLFSSL_CLIENT)
  11122. WOLFSSL_CTX* ctx = NULL;
  11123. WOLFSSL* ssl = NULL;
  11124. #ifndef NO_WOLFSSL_CLIENT
  11125. #ifndef WOLFSSL_NO_TLS12
  11126. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11127. #else
  11128. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11129. #endif
  11130. #else
  11131. #ifndef WOLFSSL_NO_TLS12
  11132. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11133. #else
  11134. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11135. #endif
  11136. #endif
  11137. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11138. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11139. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11140. #ifndef NO_WOLFSSL_CLIENT
  11141. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11142. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11143. #else
  11144. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11145. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11146. #endif
  11147. wolfSSL_free(ssl);
  11148. wolfSSL_CTX_free(ctx);
  11149. #endif
  11150. return EXPECT_RESULT();
  11151. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11152. /*----------------------------------------------------------------------------*
  11153. | Multicast Tests
  11154. *----------------------------------------------------------------------------*/
  11155. static int test_wolfSSL_mcast(void)
  11156. {
  11157. EXPECT_DECLS;
  11158. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11159. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11160. WOLFSSL_CTX* ctx = NULL;
  11161. WOLFSSL* ssl = NULL;
  11162. byte preMasterSecret[512];
  11163. byte clientRandom[32];
  11164. byte serverRandom[32];
  11165. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11166. byte buf[256];
  11167. word16 newId;
  11168. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11169. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11170. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11171. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11172. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11173. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11174. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11175. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11176. WOLFSSL_SUCCESS);
  11177. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11178. ExpectIntLE(newId, 100);
  11179. wolfSSL_free(ssl);
  11180. wolfSSL_CTX_free(ctx);
  11181. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11182. * WOLFSSL_SNIFFER) */
  11183. return EXPECT_RESULT();
  11184. }
  11185. /*----------------------------------------------------------------------------*
  11186. | Wolfcrypt
  11187. *----------------------------------------------------------------------------*/
  11188. /*
  11189. * Unit test for the wc_InitBlake2b()
  11190. */
  11191. static int test_wc_InitBlake2b(void)
  11192. {
  11193. EXPECT_DECLS;
  11194. #ifdef HAVE_BLAKE2
  11195. Blake2b blake;
  11196. /* Test good arg. */
  11197. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11198. /* Test bad arg. */
  11199. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11200. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11201. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11202. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11203. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11204. #endif
  11205. return EXPECT_RESULT();
  11206. } /* END test_wc_InitBlake2b*/
  11207. /*
  11208. * Unit test for the wc_InitBlake2b_WithKey()
  11209. */
  11210. static int test_wc_InitBlake2b_WithKey(void)
  11211. {
  11212. EXPECT_DECLS;
  11213. #ifdef HAVE_BLAKE2
  11214. Blake2b blake;
  11215. word32 digestSz = BLAKE2B_KEYBYTES;
  11216. byte key[BLAKE2B_KEYBYTES];
  11217. word32 keylen = BLAKE2B_KEYBYTES;
  11218. XMEMSET(key, 0, sizeof(key));
  11219. /* Test good arg. */
  11220. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11221. /* Test bad args. */
  11222. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11223. BAD_FUNC_ARG);
  11224. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11225. BAD_FUNC_ARG);
  11226. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11227. #endif
  11228. return EXPECT_RESULT();
  11229. } /* END wc_InitBlake2b_WithKey*/
  11230. /*
  11231. * Unit test for the wc_InitBlake2s_WithKey()
  11232. */
  11233. static int test_wc_InitBlake2s_WithKey(void)
  11234. {
  11235. EXPECT_DECLS;
  11236. #ifdef HAVE_BLAKE2S
  11237. Blake2s blake;
  11238. word32 digestSz = BLAKE2S_KEYBYTES;
  11239. byte *key = (byte*)"01234567890123456789012345678901";
  11240. word32 keylen = BLAKE2S_KEYBYTES;
  11241. /* Test good arg. */
  11242. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11243. /* Test bad args. */
  11244. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11245. BAD_FUNC_ARG);
  11246. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11247. BAD_FUNC_ARG);
  11248. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11249. #endif
  11250. return EXPECT_RESULT();
  11251. } /* END wc_InitBlake2s_WithKey*/
  11252. /*
  11253. * Unit test for the wc_InitMd5()
  11254. */
  11255. static int test_wc_InitMd5(void)
  11256. {
  11257. EXPECT_DECLS;
  11258. #ifndef NO_MD5
  11259. wc_Md5 md5;
  11260. /* Test good arg. */
  11261. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11262. /* Test bad arg. */
  11263. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11264. wc_Md5Free(&md5);
  11265. #endif
  11266. return EXPECT_RESULT();
  11267. } /* END test_wc_InitMd5 */
  11268. /*
  11269. * Testing wc_UpdateMd5()
  11270. */
  11271. static int test_wc_Md5Update(void)
  11272. {
  11273. EXPECT_DECLS;
  11274. #ifndef NO_MD5
  11275. wc_Md5 md5;
  11276. byte hash[WC_MD5_DIGEST_SIZE];
  11277. testVector a, b, c;
  11278. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11279. /* Input */
  11280. a.input = "a";
  11281. a.inLen = XSTRLEN(a.input);
  11282. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11283. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11284. /* Update input. */
  11285. a.input = "abc";
  11286. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11287. "\x72";
  11288. a.inLen = XSTRLEN(a.input);
  11289. a.outLen = XSTRLEN(a.output);
  11290. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11291. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11292. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11293. /* Pass in bad values. */
  11294. b.input = NULL;
  11295. b.inLen = 0;
  11296. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11297. c.input = NULL;
  11298. c.inLen = WC_MD5_DIGEST_SIZE;
  11299. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11300. BAD_FUNC_ARG);
  11301. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11302. BAD_FUNC_ARG);
  11303. wc_Md5Free(&md5);
  11304. #endif
  11305. return EXPECT_RESULT();
  11306. } /* END test_wc_Md5Update() */
  11307. /*
  11308. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11309. */
  11310. static int test_wc_Md5Final(void)
  11311. {
  11312. EXPECT_DECLS;
  11313. #ifndef NO_MD5
  11314. /* Instantiate */
  11315. wc_Md5 md5;
  11316. byte* hash_test[3];
  11317. byte hash1[WC_MD5_DIGEST_SIZE];
  11318. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11319. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11320. int times, i;
  11321. /* Initialize */
  11322. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11323. hash_test[0] = hash1;
  11324. hash_test[1] = hash2;
  11325. hash_test[2] = hash3;
  11326. times = sizeof(hash_test)/sizeof(byte*);
  11327. for (i = 0; i < times; i++) {
  11328. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11329. }
  11330. /* Test bad args. */
  11331. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11332. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11333. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11334. wc_Md5Free(&md5);
  11335. #endif
  11336. return EXPECT_RESULT();
  11337. }
  11338. /*
  11339. * Unit test for the wc_InitSha()
  11340. */
  11341. static int test_wc_InitSha(void)
  11342. {
  11343. EXPECT_DECLS;
  11344. #ifndef NO_SHA
  11345. wc_Sha sha;
  11346. /* Test good arg. */
  11347. ExpectIntEQ(wc_InitSha(&sha), 0);
  11348. /* Test bad arg. */
  11349. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11350. wc_ShaFree(&sha);
  11351. #endif
  11352. return EXPECT_RESULT();
  11353. } /* END test_wc_InitSha */
  11354. /*
  11355. * Tesing wc_ShaUpdate()
  11356. */
  11357. static int test_wc_ShaUpdate(void)
  11358. {
  11359. EXPECT_DECLS;
  11360. #ifndef NO_SHA
  11361. wc_Sha sha;
  11362. byte hash[WC_SHA_DIGEST_SIZE];
  11363. testVector a, b, c;
  11364. ExpectIntEQ(wc_InitSha(&sha), 0);
  11365. /* Input. */
  11366. a.input = "a";
  11367. a.inLen = XSTRLEN(a.input);
  11368. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11369. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11370. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11371. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11372. /* Update input. */
  11373. a.input = "abc";
  11374. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11375. "\x6C\x9C\xD0\xD8\x9D";
  11376. a.inLen = XSTRLEN(a.input);
  11377. a.outLen = XSTRLEN(a.output);
  11378. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11379. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11380. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11381. /* Try passing in bad values. */
  11382. b.input = NULL;
  11383. b.inLen = 0;
  11384. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11385. c.input = NULL;
  11386. c.inLen = WC_SHA_DIGEST_SIZE;
  11387. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11388. BAD_FUNC_ARG);
  11389. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11390. BAD_FUNC_ARG);
  11391. wc_ShaFree(&sha);
  11392. #endif
  11393. return EXPECT_RESULT();
  11394. } /* END test_wc_ShaUpdate() */
  11395. /*
  11396. * Unit test on wc_ShaFinal
  11397. */
  11398. static int test_wc_ShaFinal(void)
  11399. {
  11400. EXPECT_DECLS;
  11401. #ifndef NO_SHA
  11402. wc_Sha sha;
  11403. byte* hash_test[3];
  11404. byte hash1[WC_SHA_DIGEST_SIZE];
  11405. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11406. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11407. int times, i;
  11408. /* Initialize*/
  11409. ExpectIntEQ(wc_InitSha(&sha), 0);
  11410. hash_test[0] = hash1;
  11411. hash_test[1] = hash2;
  11412. hash_test[2] = hash3;
  11413. times = sizeof(hash_test)/sizeof(byte*);
  11414. for (i = 0; i < times; i++) {
  11415. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11416. }
  11417. /* Test bad args. */
  11418. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11419. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11420. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11421. wc_ShaFree(&sha);
  11422. #endif
  11423. return EXPECT_RESULT();
  11424. } /* END test_wc_ShaFinal */
  11425. /*
  11426. * Unit test for wc_InitSha256()
  11427. */
  11428. static int test_wc_InitSha256(void)
  11429. {
  11430. EXPECT_DECLS;
  11431. #ifndef NO_SHA256
  11432. wc_Sha256 sha256;
  11433. /* Test good arg. */
  11434. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11435. /* Test bad arg. */
  11436. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11437. wc_Sha256Free(&sha256);
  11438. #endif
  11439. return EXPECT_RESULT();
  11440. } /* END test_wc_InitSha256 */
  11441. /*
  11442. * Unit test for wc_Sha256Update()
  11443. */
  11444. static int test_wc_Sha256Update(void)
  11445. {
  11446. EXPECT_DECLS;
  11447. #ifndef NO_SHA256
  11448. wc_Sha256 sha256;
  11449. byte hash[WC_SHA256_DIGEST_SIZE];
  11450. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11451. testVector a, b, c;
  11452. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11453. /* Input. */
  11454. a.input = "a";
  11455. a.inLen = XSTRLEN(a.input);
  11456. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11457. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11458. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11459. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11460. /* Update input. */
  11461. a.input = "abc";
  11462. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11463. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11464. "\x15\xAD";
  11465. a.inLen = XSTRLEN(a.input);
  11466. a.outLen = XSTRLEN(a.output);
  11467. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11468. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11469. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11470. /* Unaligned check. */
  11471. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11472. 0);
  11473. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11474. /* Try passing in bad values */
  11475. b.input = NULL;
  11476. b.inLen = 0;
  11477. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11478. c.input = NULL;
  11479. c.inLen = WC_SHA256_DIGEST_SIZE;
  11480. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11481. BAD_FUNC_ARG);
  11482. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11483. BAD_FUNC_ARG);
  11484. wc_Sha256Free(&sha256);
  11485. #endif
  11486. return EXPECT_RESULT();
  11487. } /* END test_wc_Sha256Update */
  11488. /*
  11489. * Unit test function for wc_Sha256Final()
  11490. */
  11491. static int test_wc_Sha256Final(void)
  11492. {
  11493. EXPECT_DECLS;
  11494. #ifndef NO_SHA256
  11495. wc_Sha256 sha256;
  11496. byte* hash_test[3];
  11497. byte hash1[WC_SHA256_DIGEST_SIZE];
  11498. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11499. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11500. int times, i;
  11501. /* Initialize */
  11502. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11503. hash_test[0] = hash1;
  11504. hash_test[1] = hash2;
  11505. hash_test[2] = hash3;
  11506. times = sizeof(hash_test) / sizeof(byte*);
  11507. for (i = 0; i < times; i++) {
  11508. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11509. }
  11510. /* Test bad args. */
  11511. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11512. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11513. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11514. wc_Sha256Free(&sha256);
  11515. #endif
  11516. return EXPECT_RESULT();
  11517. } /* END test_wc_Sha256Final */
  11518. /*
  11519. * Unit test function for wc_Sha256FinalRaw()
  11520. */
  11521. static int test_wc_Sha256FinalRaw(void)
  11522. {
  11523. EXPECT_DECLS;
  11524. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11525. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11526. !defined(WOLFSSL_NO_HASH_RAW)
  11527. wc_Sha256 sha256;
  11528. byte* hash_test[3];
  11529. byte hash1[WC_SHA256_DIGEST_SIZE];
  11530. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11531. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11532. int times, i;
  11533. /* Initialize */
  11534. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11535. hash_test[0] = hash1;
  11536. hash_test[1] = hash2;
  11537. hash_test[2] = hash3;
  11538. times = sizeof(hash_test) / sizeof(byte*);
  11539. for (i = 0; i < times; i++) {
  11540. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11541. }
  11542. /* Test bad args. */
  11543. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11544. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11545. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11546. wc_Sha256Free(&sha256);
  11547. #endif
  11548. return EXPECT_RESULT();
  11549. } /* END test_wc_Sha256FinalRaw */
  11550. /*
  11551. * Unit test function for wc_Sha256GetFlags()
  11552. */
  11553. static int test_wc_Sha256GetFlags(void)
  11554. {
  11555. EXPECT_DECLS;
  11556. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11557. wc_Sha256 sha256;
  11558. word32 flags = 0;
  11559. /* Initialize */
  11560. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11561. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11562. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11563. wc_Sha256Free(&sha256);
  11564. #endif
  11565. return EXPECT_RESULT();
  11566. } /* END test_wc_Sha256GetFlags */
  11567. /*
  11568. * Unit test function for wc_Sha256Free()
  11569. */
  11570. static int test_wc_Sha256Free(void)
  11571. {
  11572. EXPECT_DECLS;
  11573. #ifndef NO_SHA256
  11574. wc_Sha256Free(NULL);
  11575. /* Set result to SUCCESS. */
  11576. ExpectTrue(1);
  11577. #endif
  11578. return EXPECT_RESULT();
  11579. } /* END test_wc_Sha256Free */
  11580. /*
  11581. * Unit test function for wc_Sha256GetHash()
  11582. */
  11583. static int test_wc_Sha256GetHash(void)
  11584. {
  11585. EXPECT_DECLS;
  11586. #ifndef NO_SHA256
  11587. wc_Sha256 sha256;
  11588. byte hash1[WC_SHA256_DIGEST_SIZE];
  11589. /* Initialize */
  11590. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11591. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11592. /* test bad arguments*/
  11593. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11594. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11595. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11596. wc_Sha256Free(&sha256);
  11597. #endif
  11598. return EXPECT_RESULT();
  11599. } /* END test_wc_Sha256GetHash */
  11600. /*
  11601. * Unit test function for wc_Sha256Copy()
  11602. */
  11603. static int test_wc_Sha256Copy(void)
  11604. {
  11605. EXPECT_DECLS;
  11606. #ifndef NO_SHA256
  11607. wc_Sha256 sha256;
  11608. wc_Sha256 temp;
  11609. XMEMSET(&sha256, 0, sizeof(sha256));
  11610. XMEMSET(&temp, 0, sizeof(temp));
  11611. /* Initialize */
  11612. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11613. ExpectIntEQ(wc_InitSha256(&temp), 0);
  11614. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  11615. /* test bad arguments*/
  11616. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  11617. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  11618. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  11619. wc_Sha256Free(&sha256);
  11620. wc_Sha256Free(&temp);
  11621. #endif
  11622. return EXPECT_RESULT();
  11623. } /* END test_wc_Sha256Copy */
  11624. /*
  11625. * Testing wc_InitSha512()
  11626. */
  11627. static int test_wc_InitSha512(void)
  11628. {
  11629. EXPECT_DECLS;
  11630. #ifdef WOLFSSL_SHA512
  11631. wc_Sha512 sha512;
  11632. /* Test good arg. */
  11633. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11634. /* Test bad arg. */
  11635. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  11636. wc_Sha512Free(&sha512);
  11637. #endif
  11638. return EXPECT_RESULT();
  11639. } /* END test_wc_InitSha512 */
  11640. /*
  11641. * wc_Sha512Update() test.
  11642. */
  11643. static int test_wc_Sha512Update(void)
  11644. {
  11645. EXPECT_DECLS;
  11646. #ifdef WOLFSSL_SHA512
  11647. wc_Sha512 sha512;
  11648. byte hash[WC_SHA512_DIGEST_SIZE];
  11649. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  11650. testVector a, b, c;
  11651. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11652. /* Input. */
  11653. a.input = "a";
  11654. a.inLen = XSTRLEN(a.input);
  11655. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  11656. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  11657. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  11658. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11659. /* Update input. */
  11660. a.input = "abc";
  11661. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11662. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11663. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11664. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11665. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11666. a.inLen = XSTRLEN(a.input);
  11667. a.outLen = XSTRLEN(a.output);
  11668. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  11669. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11670. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  11671. /* Unaligned check. */
  11672. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  11673. 0);
  11674. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  11675. /* Try passing in bad values */
  11676. b.input = NULL;
  11677. b.inLen = 0;
  11678. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  11679. c.input = NULL;
  11680. c.inLen = WC_SHA512_DIGEST_SIZE;
  11681. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11682. BAD_FUNC_ARG);
  11683. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  11684. BAD_FUNC_ARG);
  11685. wc_Sha512Free(&sha512);
  11686. #endif
  11687. return EXPECT_RESULT();
  11688. } /* END test_wc_Sha512Update */
  11689. #ifdef WOLFSSL_SHA512
  11690. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11691. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11692. /* Performs test for
  11693. * - wc_Sha512Final/wc_Sha512FinalRaw
  11694. * - wc_Sha512_224Final/wc_Sha512_224Final
  11695. * - wc_Sha512_256Final/wc_Sha512_256Final
  11696. * parameter:
  11697. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11698. * WC_HASH_TYPE_SHA512_256
  11699. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11700. *return 0 on success
  11701. */
  11702. static int test_Sha512_Family_Final(int type, int isRaw)
  11703. {
  11704. EXPECT_DECLS;
  11705. wc_Sha512 sha512;
  11706. byte* hash_test[3];
  11707. byte hash1[WC_SHA512_DIGEST_SIZE];
  11708. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11709. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11710. int times, i;
  11711. int(*initFp)(wc_Sha512*);
  11712. int(*finalFp)(wc_Sha512*, byte*);
  11713. void(*freeFp)(wc_Sha512*);
  11714. if (type == WC_HASH_TYPE_SHA512) {
  11715. initFp = wc_InitSha512;
  11716. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11717. !defined(WOLFSSL_NO_HASH_RAW)
  11718. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11719. #else
  11720. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11721. #endif
  11722. freeFp = wc_Sha512Free;
  11723. }
  11724. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11725. #if !defined(WOLFSSL_NOSHA512_224)
  11726. else if (type == WC_HASH_TYPE_SHA512_224) {
  11727. initFp = wc_InitSha512_224;
  11728. #if !defined(WOLFSSL_NO_HASH_RAW)
  11729. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11730. #else
  11731. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11732. #endif
  11733. freeFp = wc_Sha512_224Free;
  11734. }
  11735. #endif
  11736. #if !defined(WOLFSSL_NOSHA512_256)
  11737. else if (type == WC_HASH_TYPE_SHA512_256) {
  11738. initFp = wc_InitSha512_256;
  11739. #if !defined(WOLFSSL_NO_HASH_RAW)
  11740. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11741. #else
  11742. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11743. #endif
  11744. freeFp = wc_Sha512_256Free;
  11745. }
  11746. #endif
  11747. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11748. else
  11749. return TEST_FAIL;
  11750. /* Initialize */
  11751. ExpectIntEQ(initFp(&sha512), 0);
  11752. hash_test[0] = hash1;
  11753. hash_test[1] = hash2;
  11754. hash_test[2] = hash3;
  11755. times = sizeof(hash_test) / sizeof(byte *);
  11756. /* Good test args. */
  11757. for (i = 0; i < times; i++) {
  11758. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  11759. }
  11760. /* Test bad args. */
  11761. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  11762. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  11763. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  11764. freeFp(&sha512);
  11765. return EXPECT_RESULT();
  11766. }
  11767. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11768. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11769. #endif /* WOLFSSL_SHA512 */
  11770. /*
  11771. * Unit test function for wc_Sha512Final()
  11772. */
  11773. static int test_wc_Sha512Final(void)
  11774. {
  11775. EXPECT_DECLS;
  11776. #ifdef WOLFSSL_SHA512
  11777. wc_Sha512 sha512;
  11778. byte* hash_test[3];
  11779. byte hash1[WC_SHA512_DIGEST_SIZE];
  11780. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11781. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11782. int times, i;
  11783. /* Initialize */
  11784. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11785. hash_test[0] = hash1;
  11786. hash_test[1] = hash2;
  11787. hash_test[2] = hash3;
  11788. times = sizeof(hash_test) / sizeof(byte *);
  11789. for (i = 0; i < times; i++) {
  11790. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  11791. }
  11792. /* Test bad args. */
  11793. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  11794. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  11795. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  11796. wc_Sha512Free(&sha512);
  11797. #endif
  11798. return EXPECT_RESULT();
  11799. } /* END test_wc_Sha512Final */
  11800. /*
  11801. * Unit test function for wc_Sha512GetFlags()
  11802. */
  11803. static int test_wc_Sha512GetFlags(void)
  11804. {
  11805. EXPECT_DECLS;
  11806. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11807. wc_Sha512 sha512;
  11808. word32 flags = 0;
  11809. /* Initialize */
  11810. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11811. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  11812. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  11813. wc_Sha512Free(&sha512);
  11814. #endif
  11815. return EXPECT_RESULT();
  11816. } /* END test_wc_Sha512GetFlags */
  11817. /*
  11818. * Unit test function for wc_Sha512FinalRaw()
  11819. */
  11820. static int test_wc_Sha512FinalRaw(void)
  11821. {
  11822. EXPECT_DECLS;
  11823. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11824. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11825. !defined(WOLFSSL_NO_HASH_RAW)
  11826. wc_Sha512 sha512;
  11827. byte* hash_test[3];
  11828. byte hash1[WC_SHA512_DIGEST_SIZE];
  11829. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11830. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11831. int times, i;
  11832. /* Initialize */
  11833. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11834. hash_test[0] = hash1;
  11835. hash_test[1] = hash2;
  11836. hash_test[2] = hash3;
  11837. times = sizeof(hash_test) / sizeof(byte*);
  11838. /* Good test args. */
  11839. for (i = 0; i < times; i++) {
  11840. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  11841. }
  11842. /* Test bad args. */
  11843. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11844. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11845. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  11846. wc_Sha512Free(&sha512);
  11847. #endif
  11848. return EXPECT_RESULT();
  11849. } /* END test_wc_Sha512FinalRaw */
  11850. /*
  11851. * Unit test function for wc_Sha512Free()
  11852. */
  11853. static int test_wc_Sha512Free(void)
  11854. {
  11855. EXPECT_DECLS;
  11856. #ifdef WOLFSSL_SHA512
  11857. wc_Sha512Free(NULL);
  11858. /* Set result to SUCCESS. */
  11859. ExpectTrue(1);
  11860. #endif
  11861. return EXPECT_RESULT();
  11862. } /* END test_wc_Sha512Free */
  11863. #ifdef WOLFSSL_SHA512
  11864. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11865. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11866. static int test_Sha512_Family_GetHash(int type )
  11867. {
  11868. EXPECT_DECLS;
  11869. int(*initFp)(wc_Sha512*);
  11870. int(*ghashFp)(wc_Sha512*, byte*);
  11871. wc_Sha512 sha512;
  11872. byte hash1[WC_SHA512_DIGEST_SIZE];
  11873. if (type == WC_HASH_TYPE_SHA512) {
  11874. initFp = wc_InitSha512;
  11875. ghashFp = wc_Sha512GetHash;
  11876. }
  11877. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11878. #if !defined(WOLFSSL_NOSHA512_224)
  11879. else if (type == WC_HASH_TYPE_SHA512_224) {
  11880. initFp = wc_InitSha512_224;
  11881. ghashFp = wc_Sha512_224GetHash;
  11882. }
  11883. #endif
  11884. #if !defined(WOLFSSL_NOSHA512_256)
  11885. else if (type == WC_HASH_TYPE_SHA512_256) {
  11886. initFp = wc_InitSha512_256;
  11887. ghashFp = wc_Sha512_256GetHash;
  11888. }
  11889. #endif
  11890. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11891. else {
  11892. initFp = NULL;
  11893. ghashFp = NULL;
  11894. }
  11895. if (initFp == NULL || ghashFp == NULL)
  11896. return TEST_FAIL;
  11897. ExpectIntEQ(initFp(&sha512), 0);
  11898. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  11899. /* test bad arguments*/
  11900. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  11901. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  11902. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  11903. wc_Sha512Free(&sha512);
  11904. return EXPECT_RESULT();
  11905. }
  11906. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11907. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11908. #endif /* WOLFSSL_SHA512 */
  11909. /*
  11910. * Unit test function for wc_Sha512GetHash()
  11911. */
  11912. static int test_wc_Sha512GetHash(void)
  11913. {
  11914. EXPECT_DECLS;
  11915. #ifdef WOLFSSL_SHA512
  11916. wc_Sha512 sha512;
  11917. byte hash1[WC_SHA512_DIGEST_SIZE];
  11918. /* Initialize */
  11919. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11920. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  11921. /* test bad arguments*/
  11922. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  11923. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  11924. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  11925. wc_Sha512Free(&sha512);
  11926. #endif
  11927. return EXPECT_RESULT();
  11928. } /* END test_wc_Sha512GetHash */
  11929. /*
  11930. * Unit test function for wc_Sha512Copy()
  11931. */
  11932. static int test_wc_Sha512Copy(void)
  11933. {
  11934. EXPECT_DECLS;
  11935. #ifdef WOLFSSL_SHA512
  11936. wc_Sha512 sha512;
  11937. wc_Sha512 temp;
  11938. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11939. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11940. /* Initialize */
  11941. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11942. ExpectIntEQ(wc_InitSha512(&temp), 0);
  11943. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  11944. /* test bad arguments*/
  11945. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  11946. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  11947. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  11948. wc_Sha512Free(&sha512);
  11949. wc_Sha512Free(&temp);
  11950. #endif
  11951. return EXPECT_RESULT();
  11952. } /* END test_wc_Sha512Copy */
  11953. static int test_wc_InitSha512_224(void)
  11954. {
  11955. EXPECT_DECLS;
  11956. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11957. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11958. wc_Sha512 sha512;
  11959. /* Test good arg. */
  11960. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11961. /* Test bad arg. */
  11962. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  11963. wc_Sha512_224Free(&sha512);
  11964. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11965. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11966. return EXPECT_RESULT();
  11967. }
  11968. static int test_wc_Sha512_224Update(void)
  11969. {
  11970. EXPECT_DECLS;
  11971. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11972. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11973. wc_Sha512 sha512;
  11974. byte hash[WC_SHA512_DIGEST_SIZE];
  11975. testVector a, c;
  11976. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11977. /* Input. */
  11978. a.input = "a";
  11979. a.inLen = XSTRLEN(a.input);
  11980. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  11981. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  11982. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11983. 0);
  11984. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11985. /* Update input. */
  11986. a.input = "abc";
  11987. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11988. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11989. a.inLen = XSTRLEN(a.input);
  11990. a.outLen = XSTRLEN(a.output);
  11991. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11992. 0);
  11993. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11994. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  11995. c.input = NULL;
  11996. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11997. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11998. BAD_FUNC_ARG);
  11999. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12000. BAD_FUNC_ARG);
  12001. wc_Sha512_224Free(&sha512);
  12002. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12003. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12004. return EXPECT_RESULT();
  12005. }
  12006. static int test_wc_Sha512_224Final(void)
  12007. {
  12008. EXPECT_DECLS;
  12009. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12010. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12011. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12012. TEST_SUCCESS);
  12013. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12014. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12015. return EXPECT_RESULT();
  12016. }
  12017. static int test_wc_Sha512_224GetFlags(void)
  12018. {
  12019. EXPECT_DECLS;
  12020. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12021. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12022. wc_Sha512 sha512;
  12023. wc_Sha512 copy;
  12024. word32 flags = 0;
  12025. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12026. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12027. /* Initialize */
  12028. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12029. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12030. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12031. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12032. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12033. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12034. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12035. wc_Sha512_224Free(&copy);
  12036. wc_Sha512_224Free(&sha512);
  12037. #endif
  12038. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12039. return EXPECT_RESULT();
  12040. }
  12041. static int test_wc_Sha512_224FinalRaw(void)
  12042. {
  12043. EXPECT_DECLS;
  12044. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12045. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12046. !defined(WOLFSSL_NO_HASH_RAW)
  12047. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12048. TEST_SUCCESS);
  12049. #endif
  12050. return EXPECT_RESULT();
  12051. }
  12052. static int test_wc_Sha512_224Free(void)
  12053. {
  12054. EXPECT_DECLS;
  12055. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12056. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12057. wc_Sha512_224Free(NULL);
  12058. /* Set result to SUCCESS. */
  12059. ExpectTrue(1);
  12060. #endif
  12061. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12062. return EXPECT_RESULT();
  12063. }
  12064. static int test_wc_Sha512_224GetHash(void)
  12065. {
  12066. EXPECT_DECLS;
  12067. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12068. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12069. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12070. TEST_SUCCESS);
  12071. #endif
  12072. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12073. return EXPECT_RESULT();
  12074. }
  12075. static int test_wc_Sha512_224Copy(void)
  12076. {
  12077. EXPECT_DECLS;
  12078. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12079. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12080. wc_Sha512 sha512;
  12081. wc_Sha512 temp;
  12082. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12083. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12084. /* Initialize */
  12085. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12086. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12087. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12088. /* test bad arguments*/
  12089. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12090. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12091. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12092. wc_Sha512_224Free(&sha512);
  12093. wc_Sha512_224Free(&temp);
  12094. #endif
  12095. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12096. return EXPECT_RESULT();
  12097. }
  12098. static int test_wc_InitSha512_256(void)
  12099. {
  12100. EXPECT_DECLS;
  12101. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12102. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12103. wc_Sha512 sha512;
  12104. /* Test good arg. */
  12105. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12106. /* Test bad arg. */
  12107. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12108. wc_Sha512_256Free(&sha512);
  12109. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12110. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12111. return EXPECT_RESULT();
  12112. }
  12113. static int test_wc_Sha512_256Update(void)
  12114. {
  12115. EXPECT_DECLS;
  12116. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12117. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12118. wc_Sha512 sha512;
  12119. byte hash[WC_SHA512_DIGEST_SIZE];
  12120. testVector a, c;
  12121. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12122. /* Input. */
  12123. a.input = "a";
  12124. a.inLen = XSTRLEN(a.input);
  12125. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12126. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12127. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12128. 0);
  12129. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12130. /* Update input. */
  12131. a.input = "abc";
  12132. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12133. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12134. "\x07\xe7\xaf\x23";
  12135. a.inLen = XSTRLEN(a.input);
  12136. a.outLen = XSTRLEN(a.output);
  12137. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12138. 0);
  12139. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12140. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12141. c.input = NULL;
  12142. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12143. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12144. BAD_FUNC_ARG);
  12145. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12146. BAD_FUNC_ARG);
  12147. wc_Sha512_256Free(&sha512);
  12148. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12149. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12150. return EXPECT_RESULT();
  12151. }
  12152. static int test_wc_Sha512_256Final(void)
  12153. {
  12154. EXPECT_DECLS;
  12155. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12156. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12157. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12158. TEST_SUCCESS);
  12159. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12160. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12161. return EXPECT_RESULT();
  12162. }
  12163. static int test_wc_Sha512_256GetFlags(void)
  12164. {
  12165. EXPECT_DECLS;
  12166. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12167. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12168. wc_Sha512 sha512, copy;
  12169. word32 flags = 0;
  12170. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12171. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12172. /* Initialize */
  12173. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12174. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12175. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12176. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12177. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12178. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12179. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12180. wc_Sha512_256Free(&sha512);
  12181. #endif
  12182. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12183. return EXPECT_RESULT();
  12184. }
  12185. static int test_wc_Sha512_256FinalRaw(void)
  12186. {
  12187. EXPECT_DECLS;
  12188. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12189. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12190. !defined(WOLFSSL_NO_HASH_RAW)
  12191. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12192. TEST_SUCCESS);
  12193. #endif
  12194. return EXPECT_RESULT();
  12195. }
  12196. static int test_wc_Sha512_256Free(void)
  12197. {
  12198. EXPECT_DECLS;
  12199. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12200. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12201. wc_Sha512_256Free(NULL);
  12202. /* Set result to SUCCESS. */
  12203. ExpectTrue(1);
  12204. #endif
  12205. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12206. return EXPECT_RESULT();
  12207. }
  12208. static int test_wc_Sha512_256GetHash(void)
  12209. {
  12210. EXPECT_DECLS;
  12211. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12212. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12213. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12214. TEST_SUCCESS);
  12215. #endif
  12216. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12217. return EXPECT_RESULT();
  12218. }
  12219. static int test_wc_Sha512_256Copy(void)
  12220. {
  12221. EXPECT_DECLS;
  12222. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12223. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12224. wc_Sha512 sha512;
  12225. wc_Sha512 temp;
  12226. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12227. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12228. /* Initialize */
  12229. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12230. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12231. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12232. /* test bad arguments*/
  12233. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12234. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12235. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12236. wc_Sha512_256Free(&sha512);
  12237. wc_Sha512_256Free(&temp);
  12238. #endif
  12239. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12240. return EXPECT_RESULT();
  12241. }
  12242. /*
  12243. * Testing wc_InitSha384()
  12244. */
  12245. static int test_wc_InitSha384(void)
  12246. {
  12247. EXPECT_DECLS;
  12248. #ifdef WOLFSSL_SHA384
  12249. wc_Sha384 sha384;
  12250. /* Test good arg. */
  12251. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12252. /* Test bad arg. */
  12253. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12254. wc_Sha384Free(&sha384);
  12255. #endif
  12256. return EXPECT_RESULT();
  12257. } /* END test_wc_InitSha384 */
  12258. /*
  12259. * test wc_Sha384Update()
  12260. */
  12261. static int test_wc_Sha384Update(void)
  12262. {
  12263. EXPECT_DECLS;
  12264. #ifdef WOLFSSL_SHA384
  12265. wc_Sha384 sha384;
  12266. byte hash[WC_SHA384_DIGEST_SIZE];
  12267. testVector a, b, c;
  12268. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12269. /* Input */
  12270. a.input = "a";
  12271. a.inLen = XSTRLEN(a.input);
  12272. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12273. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12274. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12275. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12276. /* Update input. */
  12277. a.input = "abc";
  12278. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12279. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12280. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12281. "\xc8\x25\xa7";
  12282. a.inLen = XSTRLEN(a.input);
  12283. a.outLen = XSTRLEN(a.output);
  12284. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12285. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12286. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12287. /* Pass in bad values. */
  12288. b.input = NULL;
  12289. b.inLen = 0;
  12290. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12291. c.input = NULL;
  12292. c.inLen = WC_SHA384_DIGEST_SIZE;
  12293. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12294. BAD_FUNC_ARG);
  12295. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12296. BAD_FUNC_ARG);
  12297. wc_Sha384Free(&sha384);
  12298. #endif
  12299. return EXPECT_RESULT();
  12300. } /* END test_wc_Sha384Update */
  12301. /*
  12302. * Unit test function for wc_Sha384Final();
  12303. */
  12304. static int test_wc_Sha384Final(void)
  12305. {
  12306. EXPECT_DECLS;
  12307. #ifdef WOLFSSL_SHA384
  12308. wc_Sha384 sha384;
  12309. byte* hash_test[3];
  12310. byte hash1[WC_SHA384_DIGEST_SIZE];
  12311. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12312. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12313. int times, i;
  12314. /* Initialize */
  12315. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12316. hash_test[0] = hash1;
  12317. hash_test[1] = hash2;
  12318. hash_test[2] = hash3;
  12319. times = sizeof(hash_test) / sizeof(byte*);
  12320. /* Good test args. */
  12321. for (i = 0; i < times; i++) {
  12322. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12323. }
  12324. /* Test bad args. */
  12325. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12326. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12327. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12328. wc_Sha384Free(&sha384);
  12329. #endif
  12330. return EXPECT_RESULT();
  12331. } /* END test_wc_Sha384Final */
  12332. /*
  12333. * Unit test function for wc_Sha384GetFlags()
  12334. */
  12335. static int test_wc_Sha384GetFlags(void)
  12336. {
  12337. EXPECT_DECLS;
  12338. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12339. wc_Sha384 sha384;
  12340. word32 flags = 0;
  12341. /* Initialize */
  12342. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12343. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12344. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12345. wc_Sha384Free(&sha384);
  12346. #endif
  12347. return EXPECT_RESULT();
  12348. } /* END test_wc_Sha384GetFlags */
  12349. /*
  12350. * Unit test function for wc_Sha384FinalRaw()
  12351. */
  12352. static int test_wc_Sha384FinalRaw(void)
  12353. {
  12354. EXPECT_DECLS;
  12355. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12356. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12357. !defined(WOLFSSL_NO_HASH_RAW)
  12358. wc_Sha384 sha384;
  12359. byte* hash_test[3];
  12360. byte hash1[WC_SHA384_DIGEST_SIZE];
  12361. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12362. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12363. int times, i;
  12364. /* Initialize */
  12365. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12366. hash_test[0] = hash1;
  12367. hash_test[1] = hash2;
  12368. hash_test[2] = hash3;
  12369. times = sizeof(hash_test) / sizeof(byte*);
  12370. /* Good test args. */
  12371. for (i = 0; i < times; i++) {
  12372. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12373. }
  12374. /* Test bad args. */
  12375. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12376. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12377. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12378. wc_Sha384Free(&sha384);
  12379. #endif
  12380. return EXPECT_RESULT();
  12381. } /* END test_wc_Sha384FinalRaw */
  12382. /*
  12383. * Unit test function for wc_Sha384Free()
  12384. */
  12385. static int test_wc_Sha384Free(void)
  12386. {
  12387. EXPECT_DECLS;
  12388. #ifdef WOLFSSL_SHA384
  12389. wc_Sha384Free(NULL);
  12390. /* Set result to SUCCESS. */
  12391. ExpectTrue(1);
  12392. #endif
  12393. return EXPECT_RESULT();
  12394. } /* END test_wc_Sha384Free */
  12395. /*
  12396. * Unit test function for wc_Sha384GetHash()
  12397. */
  12398. static int test_wc_Sha384GetHash(void)
  12399. {
  12400. EXPECT_DECLS;
  12401. #ifdef WOLFSSL_SHA384
  12402. wc_Sha384 sha384;
  12403. byte hash1[WC_SHA384_DIGEST_SIZE];
  12404. /* Initialize */
  12405. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12406. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12407. /* test bad arguments*/
  12408. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12409. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12410. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12411. wc_Sha384Free(&sha384);
  12412. #endif
  12413. return EXPECT_RESULT();
  12414. } /* END test_wc_Sha384GetHash */
  12415. /*
  12416. * Unit test function for wc_Sha384Copy()
  12417. */
  12418. static int test_wc_Sha384Copy(void)
  12419. {
  12420. EXPECT_DECLS;
  12421. #ifdef WOLFSSL_SHA384
  12422. wc_Sha384 sha384;
  12423. wc_Sha384 temp;
  12424. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12425. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12426. /* Initialize */
  12427. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12428. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12429. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12430. /* test bad arguments*/
  12431. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12432. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12433. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12434. wc_Sha384Free(&sha384);
  12435. wc_Sha384Free(&temp);
  12436. #endif
  12437. return EXPECT_RESULT();
  12438. } /* END test_wc_Sha384Copy */
  12439. /*
  12440. * Testing wc_InitSha224();
  12441. */
  12442. static int test_wc_InitSha224(void)
  12443. {
  12444. EXPECT_DECLS;
  12445. #ifdef WOLFSSL_SHA224
  12446. wc_Sha224 sha224;
  12447. /* Test good arg. */
  12448. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12449. /* Test bad arg. */
  12450. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12451. wc_Sha224Free(&sha224);
  12452. #endif
  12453. return EXPECT_RESULT();
  12454. } /* END test_wc_InitSha224 */
  12455. /*
  12456. * Unit test on wc_Sha224Update
  12457. */
  12458. static int test_wc_Sha224Update(void)
  12459. {
  12460. EXPECT_DECLS;
  12461. #ifdef WOLFSSL_SHA224
  12462. wc_Sha224 sha224;
  12463. byte hash[WC_SHA224_DIGEST_SIZE];
  12464. testVector a, b, c;
  12465. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12466. /* Input. */
  12467. a.input = "a";
  12468. a.inLen = XSTRLEN(a.input);
  12469. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12470. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12471. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12472. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12473. /* Update input. */
  12474. a.input = "abc";
  12475. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12476. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12477. a.inLen = XSTRLEN(a.input);
  12478. a.outLen = XSTRLEN(a.output);
  12479. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12480. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12481. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12482. /* Pass in bad values. */
  12483. b.input = NULL;
  12484. b.inLen = 0;
  12485. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12486. c.input = NULL;
  12487. c.inLen = WC_SHA224_DIGEST_SIZE;
  12488. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12489. BAD_FUNC_ARG);
  12490. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12491. BAD_FUNC_ARG);
  12492. wc_Sha224Free(&sha224);
  12493. #endif
  12494. return EXPECT_RESULT();
  12495. } /* END test_wc_Sha224Update */
  12496. /*
  12497. * Unit test for wc_Sha224Final();
  12498. */
  12499. static int test_wc_Sha224Final(void)
  12500. {
  12501. EXPECT_DECLS;
  12502. #ifdef WOLFSSL_SHA224
  12503. wc_Sha224 sha224;
  12504. byte* hash_test[3];
  12505. byte hash1[WC_SHA224_DIGEST_SIZE];
  12506. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12507. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12508. int times, i;
  12509. /* Initialize */
  12510. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12511. hash_test[0] = hash1;
  12512. hash_test[1] = hash2;
  12513. hash_test[2] = hash3;
  12514. times = sizeof(hash_test) / sizeof(byte*);
  12515. /* Good test args. */
  12516. /* Testing oversized buffers. */
  12517. for (i = 0; i < times; i++) {
  12518. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12519. }
  12520. /* Test bad args. */
  12521. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12522. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12523. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12524. wc_Sha224Free(&sha224);
  12525. #endif
  12526. return EXPECT_RESULT();
  12527. } /* END test_wc_Sha224Final */
  12528. /*
  12529. * Unit test function for wc_Sha224SetFlags()
  12530. */
  12531. static int test_wc_Sha224SetFlags(void)
  12532. {
  12533. EXPECT_DECLS;
  12534. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12535. wc_Sha224 sha224;
  12536. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12537. /* Initialize */
  12538. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12539. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12540. flags = 0;
  12541. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12542. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12543. wc_Sha224Free(&sha224);
  12544. #endif
  12545. return EXPECT_RESULT();
  12546. } /* END test_wc_Sha224SetFlags */
  12547. /*
  12548. * Unit test function for wc_Sha224GetFlags()
  12549. */
  12550. static int test_wc_Sha224GetFlags(void)
  12551. {
  12552. EXPECT_DECLS;
  12553. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12554. wc_Sha224 sha224;
  12555. word32 flags = 0;
  12556. /* Initialize */
  12557. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12558. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12559. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12560. wc_Sha224Free(&sha224);
  12561. #endif
  12562. return EXPECT_RESULT();
  12563. } /* END test_wc_Sha224GetFlags */
  12564. /*
  12565. * Unit test function for wc_Sha224Free()
  12566. */
  12567. static int test_wc_Sha224Free(void)
  12568. {
  12569. EXPECT_DECLS;
  12570. #ifdef WOLFSSL_SHA224
  12571. wc_Sha224Free(NULL);
  12572. /* Set result to SUCCESS. */
  12573. ExpectTrue(1);
  12574. #endif
  12575. return EXPECT_RESULT();
  12576. } /* END test_wc_Sha224Free */
  12577. /*
  12578. * Unit test function for wc_Sha224GetHash()
  12579. */
  12580. static int test_wc_Sha224GetHash(void)
  12581. {
  12582. EXPECT_DECLS;
  12583. #ifdef WOLFSSL_SHA224
  12584. wc_Sha224 sha224;
  12585. byte hash1[WC_SHA224_DIGEST_SIZE];
  12586. /* Initialize */
  12587. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12588. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12589. /* test bad arguments*/
  12590. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12591. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12592. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12593. wc_Sha224Free(&sha224);
  12594. #endif
  12595. return EXPECT_RESULT();
  12596. } /* END test_wc_Sha224GetHash */
  12597. /*
  12598. * Unit test function for wc_Sha224Copy()
  12599. */
  12600. static int test_wc_Sha224Copy(void)
  12601. {
  12602. EXPECT_DECLS;
  12603. #ifdef WOLFSSL_SHA224
  12604. wc_Sha224 sha224;
  12605. wc_Sha224 temp;
  12606. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  12607. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  12608. /* Initialize */
  12609. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12610. ExpectIntEQ(wc_InitSha224(&temp), 0);
  12611. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  12612. /* test bad arguments*/
  12613. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  12614. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  12615. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  12616. wc_Sha224Free(&sha224);
  12617. wc_Sha224Free(&temp);
  12618. #endif
  12619. return EXPECT_RESULT();
  12620. } /* END test_wc_Sha224Copy */
  12621. /*
  12622. * Testing wc_InitRipeMd()
  12623. */
  12624. static int test_wc_InitRipeMd(void)
  12625. {
  12626. EXPECT_DECLS;
  12627. #ifdef WOLFSSL_RIPEMD
  12628. RipeMd ripemd;
  12629. /* Test good arg. */
  12630. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12631. /* Test bad arg. */
  12632. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  12633. #endif
  12634. return EXPECT_RESULT();
  12635. } /* END test_wc_InitRipeMd */
  12636. /*
  12637. * Testing wc_RipeMdUpdate()
  12638. */
  12639. static int test_wc_RipeMdUpdate(void)
  12640. {
  12641. EXPECT_DECLS;
  12642. #ifdef WOLFSSL_RIPEMD
  12643. RipeMd ripemd;
  12644. byte hash[RIPEMD_DIGEST_SIZE];
  12645. testVector a, b, c;
  12646. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12647. /* Input */
  12648. a.input = "a";
  12649. a.inLen = XSTRLEN(a.input);
  12650. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12651. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12652. /* Update input. */
  12653. a.input = "abc";
  12654. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12655. "\xb0\x87\xf1\x5a\x0b\xfc";
  12656. a.inLen = XSTRLEN(a.input);
  12657. a.outLen = XSTRLEN(a.output);
  12658. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12659. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12660. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  12661. /* Pass in bad values. */
  12662. b.input = NULL;
  12663. b.inLen = 0;
  12664. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  12665. c.input = NULL;
  12666. c.inLen = RIPEMD_DIGEST_SIZE;
  12667. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  12668. BAD_FUNC_ARG);
  12669. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12670. BAD_FUNC_ARG);
  12671. #endif
  12672. return EXPECT_RESULT();
  12673. } /* END test_wc_RipeMdUdpate */
  12674. /*
  12675. * Unit test function for wc_RipeMdFinal()
  12676. */
  12677. static int test_wc_RipeMdFinal(void)
  12678. {
  12679. EXPECT_DECLS;
  12680. #ifdef WOLFSSL_RIPEMD
  12681. RipeMd ripemd;
  12682. byte* hash_test[3];
  12683. byte hash1[RIPEMD_DIGEST_SIZE];
  12684. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12685. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12686. int times, i;
  12687. /* Initialize */
  12688. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12689. hash_test[0] = hash1;
  12690. hash_test[1] = hash2;
  12691. hash_test[2] = hash3;
  12692. times = sizeof(hash_test) / sizeof(byte*);
  12693. /* Testing oversized buffers. */
  12694. for (i = 0; i < times; i++) {
  12695. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  12696. }
  12697. /* Test bad args. */
  12698. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  12699. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  12700. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  12701. #endif
  12702. return EXPECT_RESULT();
  12703. } /* END test_wc_RipeMdFinal */
  12704. /*
  12705. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12706. * wc_InitSha3_512
  12707. */
  12708. static int test_wc_InitSha3(void)
  12709. {
  12710. EXPECT_DECLS;
  12711. #if defined(WOLFSSL_SHA3)
  12712. wc_Sha3 sha3;
  12713. (void)sha3;
  12714. #if !defined(WOLFSSL_NOSHA3_224)
  12715. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12716. /* Test bad args. */
  12717. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12718. wc_Sha3_224_Free(&sha3);
  12719. #endif /* NOSHA3_224 */
  12720. #if !defined(WOLFSSL_NOSHA3_256)
  12721. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12722. /* Test bad args. */
  12723. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12724. wc_Sha3_256_Free(&sha3);
  12725. #endif /* NOSHA3_256 */
  12726. #if !defined(WOLFSSL_NOSHA3_384)
  12727. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12728. /* Test bad args. */
  12729. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12730. wc_Sha3_384_Free(&sha3);
  12731. #endif /* NOSHA3_384 */
  12732. #if !defined(WOLFSSL_NOSHA3_512)
  12733. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12734. /* Test bad args. */
  12735. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12736. wc_Sha3_512_Free(&sha3);
  12737. #endif /* NOSHA3_512 */
  12738. #endif
  12739. return EXPECT_RESULT();
  12740. } /* END test_wc_InitSha3 */
  12741. /*
  12742. * Testing wc_Sha3_Update()
  12743. */
  12744. static int testing_wc_Sha3_Update(void)
  12745. {
  12746. EXPECT_DECLS;
  12747. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12748. !defined(WOLFSSL_AFALG_XILINX)
  12749. wc_Sha3 sha3;
  12750. byte msg[] = "Everybody's working for the weekend.";
  12751. byte msg2[] = "Everybody gets Friday off.";
  12752. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12753. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12754. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12755. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12756. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12757. word32 msglen = sizeof(msg) - 1;
  12758. word32 msg2len = sizeof(msg2);
  12759. word32 msgCmplen = sizeof(msgCmp);
  12760. #if !defined(WOLFSSL_NOSHA3_224)
  12761. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12762. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  12763. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12764. ExpectTrue(sha3.i == msglen);
  12765. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12766. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12767. /* Pass bad args. */
  12768. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12769. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12770. wc_Sha3_224_Free(&sha3);
  12771. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12772. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  12773. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12774. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12775. wc_Sha3_224_Free(&sha3);
  12776. #endif /* SHA3_224 */
  12777. #if !defined(WOLFSSL_NOSHA3_256)
  12778. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12779. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  12780. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12781. ExpectTrue(sha3.i == msglen);
  12782. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12783. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12784. /* Pass bad args. */
  12785. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12786. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12787. wc_Sha3_256_Free(&sha3);
  12788. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12789. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  12790. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12791. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12792. wc_Sha3_256_Free(&sha3);
  12793. #endif /* SHA3_256 */
  12794. #if !defined(WOLFSSL_NOSHA3_384)
  12795. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12796. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  12797. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12798. ExpectTrue(sha3.i == msglen);
  12799. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12800. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12801. /* Pass bad args. */
  12802. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12803. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12804. wc_Sha3_384_Free(&sha3);
  12805. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12806. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  12807. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12808. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12809. wc_Sha3_384_Free(&sha3);
  12810. #endif /* SHA3_384 */
  12811. #if !defined(WOLFSSL_NOSHA3_512)
  12812. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12813. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  12814. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12815. ExpectTrue(sha3.i == msglen);
  12816. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12817. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12818. /* Pass bad args. */
  12819. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12820. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12821. wc_Sha3_512_Free(&sha3);
  12822. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12823. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  12824. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12825. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12826. wc_Sha3_512_Free(&sha3);
  12827. #endif /* SHA3_512 */
  12828. #endif /* WOLFSSL_SHA3 */
  12829. return EXPECT_RESULT();
  12830. } /* END testing_wc_Sha3_Update */
  12831. /*
  12832. * Testing wc_Sha3_224_Final()
  12833. */
  12834. static int test_wc_Sha3_224_Final(void)
  12835. {
  12836. EXPECT_DECLS;
  12837. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12838. wc_Sha3 sha3;
  12839. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12840. "nopnopq";
  12841. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12842. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12843. "\x64\xea\xd0\xfc\xce\x33";
  12844. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12845. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12846. /* Init stack variables. */
  12847. XMEMSET(hash, 0, sizeof(hash));
  12848. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12849. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12850. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12851. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  12852. /* Test bad args. */
  12853. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  12854. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  12855. wc_Sha3_224_Free(&sha3);
  12856. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12857. /* Init stack variables. */
  12858. XMEMSET(hash, 0, sizeof(hash));
  12859. XMEMSET(hashRet, 0, sizeof(hashRet));
  12860. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12861. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  12862. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12863. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  12864. /* Test bad args. */
  12865. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12866. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12867. wc_Sha3_224_Free(&sha3);
  12868. #endif
  12869. return EXPECT_RESULT();
  12870. } /* END test_wc_Sha3_224_Final */
  12871. /*
  12872. * Testing wc_Sha3_256_Final()
  12873. */
  12874. static int test_wc_Sha3_256_Final(void)
  12875. {
  12876. EXPECT_DECLS;
  12877. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12878. wc_Sha3 sha3;
  12879. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12880. "nopnopq";
  12881. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12882. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12883. "\xdd\x97\x49\x6d\x33\x76";
  12884. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12885. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12886. /* Init stack variables. */
  12887. XMEMSET(hash, 0, sizeof(hash));
  12888. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12889. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12890. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12891. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  12892. /* Test bad args. */
  12893. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  12894. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  12895. wc_Sha3_256_Free(&sha3);
  12896. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12897. /* Init stack variables. */
  12898. XMEMSET(hash, 0, sizeof(hash));
  12899. XMEMSET(hashRet, 0, sizeof(hashRet));
  12900. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12901. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  12902. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12903. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  12904. /* Test bad args. */
  12905. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12906. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12907. wc_Sha3_256_Free(&sha3);
  12908. #endif
  12909. return EXPECT_RESULT();
  12910. } /* END test_wc_Sha3_256_Final */
  12911. /*
  12912. * Testing wc_Sha3_384_Final()
  12913. */
  12914. static int test_wc_Sha3_384_Final(void)
  12915. {
  12916. EXPECT_DECLS;
  12917. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12918. wc_Sha3 sha3;
  12919. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12920. "nopnopq";
  12921. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12922. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12923. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12924. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12925. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12926. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12927. /* Init stack variables. */
  12928. XMEMSET(hash, 0, sizeof(hash));
  12929. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12930. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12931. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12932. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  12933. /* Test bad args. */
  12934. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  12935. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  12936. wc_Sha3_384_Free(&sha3);
  12937. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12938. /* Init stack variables. */
  12939. XMEMSET(hash, 0, sizeof(hash));
  12940. XMEMSET(hashRet, 0, sizeof(hashRet));
  12941. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12942. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  12943. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12944. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  12945. /* Test bad args. */
  12946. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12947. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12948. wc_Sha3_384_Free(&sha3);
  12949. #endif
  12950. return EXPECT_RESULT();
  12951. } /* END test_wc_Sha3_384_Final */
  12952. /*
  12953. * Testing wc_Sha3_512_Final()
  12954. */
  12955. static int test_wc_Sha3_512_Final(void)
  12956. {
  12957. EXPECT_DECLS;
  12958. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12959. !defined(WOLFSSL_NOSHA3_384)
  12960. wc_Sha3 sha3;
  12961. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12962. "nopnopq";
  12963. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12964. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12965. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12966. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12967. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12968. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12969. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12970. /* Init stack variables. */
  12971. XMEMSET(hash, 0, sizeof(hash));
  12972. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12973. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12974. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12975. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  12976. /* Test bad args. */
  12977. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  12978. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  12979. wc_Sha3_512_Free(&sha3);
  12980. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12981. /* Init stack variables. */
  12982. XMEMSET(hash, 0, sizeof(hash));
  12983. XMEMSET(hashRet, 0, sizeof(hashRet));
  12984. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12985. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  12986. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12987. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  12988. /* Test bad args. */
  12989. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12990. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12991. wc_Sha3_512_Free(&sha3);
  12992. #endif
  12993. return EXPECT_RESULT();
  12994. } /* END test_wc_Sha3_512_Final */
  12995. /*
  12996. * Testing wc_Sha3_224_Copy()
  12997. */
  12998. static int test_wc_Sha3_224_Copy(void)
  12999. {
  13000. EXPECT_DECLS;
  13001. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13002. wc_Sha3 sha3, sha3Cpy;
  13003. const char* msg = TEST_STRING;
  13004. word32 msglen = (word32)TEST_STRING_SZ;
  13005. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13006. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13007. XMEMSET(hash, 0, sizeof(hash));
  13008. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13009. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13010. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13011. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13012. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13013. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13014. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13015. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13016. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13017. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13018. /* Test bad args. */
  13019. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13020. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13021. wc_Sha3_224_Free(&sha3);
  13022. wc_Sha3_224_Free(&sha3Cpy);
  13023. #endif
  13024. return EXPECT_RESULT();
  13025. } /* END test_wc_Sha3_224_Copy */
  13026. /*
  13027. * Testing wc_Sha3_256_Copy()
  13028. */
  13029. static int test_wc_Sha3_256_Copy(void)
  13030. {
  13031. EXPECT_DECLS;
  13032. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13033. wc_Sha3 sha3, sha3Cpy;
  13034. const char* msg = TEST_STRING;
  13035. word32 msglen = (word32)TEST_STRING_SZ;
  13036. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13037. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13038. XMEMSET(hash, 0, sizeof(hash));
  13039. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13040. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13041. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13042. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13043. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13044. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13045. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13046. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13047. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13048. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13049. /* Test bad args. */
  13050. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13051. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13052. wc_Sha3_256_Free(&sha3);
  13053. wc_Sha3_256_Free(&sha3Cpy);
  13054. #endif
  13055. return EXPECT_RESULT();
  13056. } /* END test_wc_Sha3_256_Copy */
  13057. /*
  13058. * Testing wc_Sha3_384_Copy()
  13059. */
  13060. static int test_wc_Sha3_384_Copy(void)
  13061. {
  13062. EXPECT_DECLS;
  13063. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13064. wc_Sha3 sha3, sha3Cpy;
  13065. const char* msg = TEST_STRING;
  13066. word32 msglen = (word32)TEST_STRING_SZ;
  13067. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13068. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13069. XMEMSET(hash, 0, sizeof(hash));
  13070. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13071. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13072. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13073. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13074. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13075. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13076. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13077. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13078. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13079. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13080. /* Test bad args. */
  13081. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13082. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13083. wc_Sha3_384_Free(&sha3);
  13084. wc_Sha3_384_Free(&sha3Cpy);
  13085. #endif
  13086. return EXPECT_RESULT();
  13087. } /* END test_wc_Sha3_384_Copy */
  13088. /*
  13089. * Testing wc_Sha3_512_Copy()
  13090. */
  13091. static int test_wc_Sha3_512_Copy(void)
  13092. {
  13093. EXPECT_DECLS;
  13094. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13095. wc_Sha3 sha3, sha3Cpy;
  13096. const char* msg = TEST_STRING;
  13097. word32 msglen = (word32)TEST_STRING_SZ;
  13098. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13099. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13100. XMEMSET(hash, 0, sizeof(hash));
  13101. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13102. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13103. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13104. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13105. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13106. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13107. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13108. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13109. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13110. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13111. /* Test bad args. */
  13112. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13113. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13114. wc_Sha3_512_Free(&sha3);
  13115. wc_Sha3_512_Free(&sha3Cpy);
  13116. #endif
  13117. return EXPECT_RESULT();
  13118. } /* END test_wc_Sha3_512_Copy */
  13119. /*
  13120. * Unit test function for wc_Sha3_GetFlags()
  13121. */
  13122. static int test_wc_Sha3_GetFlags(void)
  13123. {
  13124. EXPECT_DECLS;
  13125. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13126. wc_Sha3 sha3;
  13127. word32 flags = 0;
  13128. /* Initialize */
  13129. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13130. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13131. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13132. wc_Sha3_224_Free(&sha3);
  13133. #endif
  13134. return EXPECT_RESULT();
  13135. } /* END test_wc_Sha3_GetFlags */
  13136. static int test_wc_InitShake256(void)
  13137. {
  13138. EXPECT_DECLS;
  13139. #ifdef WOLFSSL_SHAKE256
  13140. wc_Shake shake;
  13141. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13142. /* Test bad args. */
  13143. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13144. wc_Shake256_Free(&shake);
  13145. #endif
  13146. return EXPECT_RESULT();
  13147. }
  13148. static int testing_wc_Shake256_Update(void)
  13149. {
  13150. EXPECT_DECLS;
  13151. #ifdef WOLFSSL_SHAKE256
  13152. wc_Shake shake;
  13153. byte msg[] = "Everybody's working for the weekend.";
  13154. byte msg2[] = "Everybody gets Friday off.";
  13155. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13156. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13157. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13158. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13159. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13160. word32 msglen = sizeof(msg) - 1;
  13161. word32 msg2len = sizeof(msg2);
  13162. word32 msgCmplen = sizeof(msgCmp);
  13163. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13164. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13165. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13166. ExpectTrue(shake.i == msglen);
  13167. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13168. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13169. /* Pass bad args. */
  13170. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13171. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13172. wc_Shake256_Free(&shake);
  13173. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13174. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13175. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13176. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13177. wc_Shake256_Free(&shake);
  13178. #endif /* WOLFSSL_SHAKE256 */
  13179. return EXPECT_RESULT();
  13180. }
  13181. static int test_wc_Shake256_Final(void)
  13182. {
  13183. EXPECT_DECLS;
  13184. #ifdef WOLFSSL_SHAKE256
  13185. wc_Shake shake;
  13186. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13187. "nopnopq";
  13188. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13189. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13190. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13191. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13192. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13193. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13194. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13195. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13196. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13197. byte hash[114];
  13198. /* Init stack variables. */
  13199. XMEMSET(hash, 0, sizeof(hash));
  13200. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13201. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13202. 0);
  13203. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13204. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13205. /* Test bad args. */
  13206. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13207. BAD_FUNC_ARG);
  13208. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13209. BAD_FUNC_ARG);
  13210. wc_Shake256_Free(&shake);
  13211. #endif
  13212. return EXPECT_RESULT();
  13213. }
  13214. /*
  13215. * Testing wc_Shake256_Copy()
  13216. */
  13217. static int test_wc_Shake256_Copy(void)
  13218. {
  13219. EXPECT_DECLS;
  13220. #ifdef WOLFSSL_SHAKE256
  13221. wc_Shake shake, shakeCpy;
  13222. const char* msg = TEST_STRING;
  13223. word32 msglen = (word32)TEST_STRING_SZ;
  13224. byte hash[144];
  13225. byte hashCpy[144];
  13226. word32 hashLen = sizeof(hash);
  13227. word32 hashLenCpy = sizeof(hashCpy);
  13228. XMEMSET(hash, 0, sizeof(hash));
  13229. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13230. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13231. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13232. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13233. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13234. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13235. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13236. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13237. /* Test bad args. */
  13238. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13239. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13240. wc_Shake256_Free(&shake);
  13241. wc_Shake256_Free(&shakeCpy);
  13242. #endif
  13243. return EXPECT_RESULT();
  13244. } /* END test_wc_Shake256_Copy */
  13245. /*
  13246. * Unit test function for wc_Shake256Hash()
  13247. */
  13248. static int test_wc_Shake256Hash(void)
  13249. {
  13250. EXPECT_DECLS;
  13251. #ifdef WOLFSSL_SHAKE256
  13252. const byte data[] = { /* Hello World */
  13253. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13254. 0x72,0x6c,0x64
  13255. };
  13256. word32 len = sizeof(data);
  13257. byte hash[144];
  13258. word32 hashLen = sizeof(hash);
  13259. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13260. #endif
  13261. return EXPECT_RESULT();
  13262. } /* END test_wc_Shake256Hash */
  13263. /*
  13264. * Testing wc_InitSm3(), wc_Sm3Free()
  13265. */
  13266. static int test_wc_InitSm3Free(void)
  13267. {
  13268. EXPECT_DECLS;
  13269. #ifdef WOLFSSL_SM3
  13270. wc_Sm3 sm3;
  13271. /* Invalid Parameters */
  13272. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13273. /* Valid Parameters */
  13274. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13275. wc_Sm3Free(NULL);
  13276. wc_Sm3Free(&sm3);
  13277. #endif
  13278. return EXPECT_RESULT();
  13279. } /* END test_wc_InitSm3 */
  13280. /*
  13281. * Testing wc_Sm3Update(), wc_Sm3Final()
  13282. */
  13283. static int test_wc_Sm3UpdateFinal(void)
  13284. {
  13285. EXPECT_DECLS;
  13286. #ifdef WOLFSSL_SM3
  13287. wc_Sm3 sm3;
  13288. byte data[WC_SM3_BLOCK_SIZE * 4];
  13289. byte hash[WC_SM3_DIGEST_SIZE];
  13290. byte calcHash[WC_SM3_DIGEST_SIZE];
  13291. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13292. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13293. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13294. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13295. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13296. };
  13297. word32 chunk;
  13298. word32 i;
  13299. XMEMSET(data, 0, sizeof(data));
  13300. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13301. /* Invalid Parameters */
  13302. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13303. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13304. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13305. /* Valid Parameters */
  13306. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13307. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13308. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13309. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13310. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13311. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13312. /* Ensure too many bytes for lengths. */
  13313. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13314. /* Invalid Parameters */
  13315. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13316. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13317. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13318. /* Valid Parameters */
  13319. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13320. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13321. /* Chunk tests. */
  13322. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13323. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13324. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13325. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13326. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13327. }
  13328. if (i < (word32)sizeof(data)) {
  13329. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13330. 0);
  13331. }
  13332. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13333. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13334. }
  13335. /* Not testing when the low 32-bit length overflows. */
  13336. wc_Sm3Free(&sm3);
  13337. #endif
  13338. return EXPECT_RESULT();
  13339. } /* END test_wc_Sm3Update */
  13340. /*
  13341. * Testing wc_Sm3GetHash()
  13342. */
  13343. static int test_wc_Sm3GetHash(void)
  13344. {
  13345. EXPECT_DECLS;
  13346. #ifdef WOLFSSL_SM3
  13347. wc_Sm3 sm3;
  13348. byte hash[WC_SM3_DIGEST_SIZE];
  13349. byte calcHash[WC_SM3_DIGEST_SIZE];
  13350. byte data[WC_SM3_BLOCK_SIZE];
  13351. XMEMSET(data, 0, sizeof(data));
  13352. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13353. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13354. /* Invalid Parameters */
  13355. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13356. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13357. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13358. /* Valid Parameters */
  13359. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13360. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13361. /* With update. */
  13362. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13363. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13364. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13365. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13366. wc_Sm3Free(&sm3);
  13367. #endif
  13368. return EXPECT_RESULT();
  13369. } /* END test_wc_Sm3Update */
  13370. /*
  13371. * Testing wc_Sm3Copy()
  13372. */
  13373. static int test_wc_Sm3Copy(void)
  13374. {
  13375. EXPECT_DECLS;
  13376. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13377. wc_Sm3 sm3;
  13378. wc_Sm3 sm3Copy;
  13379. byte hash[WC_SM3_DIGEST_SIZE];
  13380. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13381. byte data[WC_SM3_BLOCK_SIZE + 1];
  13382. int i;
  13383. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13384. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13385. /* Invalid Parameters */
  13386. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13387. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13388. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13389. /* Valid Parameters */
  13390. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13391. /* Ensure all parts of data updated during hashing are copied. */
  13392. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13393. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13394. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13395. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13396. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13397. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13398. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13399. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13400. }
  13401. wc_Sm3Free(&sm3Copy);
  13402. wc_Sm3Free(&sm3);
  13403. #endif
  13404. return EXPECT_RESULT();
  13405. } /* END test_wc_Sm3Copy */
  13406. /*
  13407. * Testing wc_Sm3FinalRaw()
  13408. */
  13409. static int test_wc_Sm3FinalRaw(void)
  13410. {
  13411. EXPECT_DECLS;
  13412. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13413. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13414. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13415. !defined(WOLFSSL_NO_HASH_RAW)
  13416. wc_Sm3 sm3;
  13417. byte hash1[WC_SM3_DIGEST_SIZE];
  13418. byte hash2[WC_SM3_DIGEST_SIZE];
  13419. byte hash3[WC_SM3_DIGEST_SIZE];
  13420. byte* hash_test[3] = { hash1, hash2, hash3 };
  13421. int times;
  13422. int i;
  13423. XMEMSET(&sm3, 0, sizeof(sm3));
  13424. /* Initialize */
  13425. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13426. /* Invalid Parameters */
  13427. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13428. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13429. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13430. times = sizeof(hash_test) / sizeof(byte*);
  13431. for (i = 0; i < times; i++) {
  13432. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13433. }
  13434. wc_Sm3Free(&sm3);
  13435. #endif
  13436. return EXPECT_RESULT();
  13437. } /* END test_wc_Sm3FinalRaw */
  13438. /*
  13439. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13440. */
  13441. static int test_wc_Sm3GetSetFlags(void)
  13442. {
  13443. EXPECT_DECLS;
  13444. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13445. wc_Sm3 sm3;
  13446. wc_Sm3 sm3Copy;
  13447. word32 flags = 0;
  13448. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13449. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13450. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13451. ExpectIntEQ(flags, 0);
  13452. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13453. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13454. ExpectIntEQ(flags, 0);
  13455. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13456. ExpectIntEQ(flags, 0);
  13457. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13458. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13459. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13460. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13461. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13462. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13463. wc_Sm3Free(&sm3Copy);
  13464. wc_Sm3Free(&sm3);
  13465. #endif
  13466. return EXPECT_RESULT();
  13467. } /* END test_wc_Sm3Update */
  13468. /*
  13469. * Testing wc_Sm3Hash()
  13470. */
  13471. static int test_wc_Sm3Hash(void)
  13472. {
  13473. EXPECT_DECLS;
  13474. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13475. byte data[WC_SM3_BLOCK_SIZE];
  13476. byte hash[WC_SM3_DIGEST_SIZE];
  13477. /* Invalid parameters. */
  13478. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13479. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13480. /* Valid parameters. */
  13481. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13482. #endif
  13483. return EXPECT_RESULT();
  13484. } /* END test_wc_Sm3Hash */
  13485. /*
  13486. * Test function for wc_HmacSetKey
  13487. */
  13488. static int test_wc_Md5HmacSetKey(void)
  13489. {
  13490. EXPECT_DECLS;
  13491. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13492. Hmac hmac;
  13493. int ret, times, itr;
  13494. const char* keys[]=
  13495. {
  13496. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13497. #ifndef HAVE_FIPS
  13498. "Jefe", /* smaller than minimum FIPS key size */
  13499. #endif
  13500. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13501. };
  13502. times = sizeof(keys) / sizeof(char*);
  13503. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13504. for (itr = 0; itr < times; itr++) {
  13505. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13506. (word32)XSTRLEN(keys[itr]));
  13507. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13508. wc_HmacFree(&hmac);
  13509. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13510. #else
  13511. ExpectIntEQ(ret, 0);
  13512. #endif
  13513. }
  13514. /* Bad args. */
  13515. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13516. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13517. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13518. BAD_FUNC_ARG);
  13519. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13520. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13521. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13522. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13523. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13524. #elif defined(HAVE_FIPS)
  13525. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13526. #else
  13527. ExpectIntEQ(ret, 0);
  13528. #endif
  13529. wc_HmacFree(&hmac);
  13530. #endif
  13531. return EXPECT_RESULT();
  13532. } /* END test_wc_Md5HmacSetKey */
  13533. /*
  13534. * testing wc_HmacSetKey() on wc_Sha hash.
  13535. */
  13536. static int test_wc_ShaHmacSetKey(void)
  13537. {
  13538. EXPECT_DECLS;
  13539. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13540. Hmac hmac;
  13541. int ret, times, itr;
  13542. const char* keys[]=
  13543. {
  13544. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13545. "\x0b\x0b\x0b",
  13546. #ifndef HAVE_FIPS
  13547. "Jefe", /* smaller than minimum FIPS key size */
  13548. #endif
  13549. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13550. "\xAA\xAA\xAA"
  13551. };
  13552. times = sizeof(keys) / sizeof(char*);
  13553. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13554. for (itr = 0; itr < times; itr++) {
  13555. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13556. (word32)XSTRLEN(keys[itr])), 0);
  13557. }
  13558. /* Bad args. */
  13559. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13560. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13561. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13562. BAD_FUNC_ARG);
  13563. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13564. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13565. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13566. #ifdef HAVE_FIPS
  13567. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13568. #else
  13569. ExpectIntEQ(ret, 0);
  13570. #endif
  13571. wc_HmacFree(&hmac);
  13572. #endif
  13573. return EXPECT_RESULT();
  13574. } /* END test_wc_ShaHmacSetKey() */
  13575. /*
  13576. * testing wc_HmacSetKey() on Sha224 hash.
  13577. */
  13578. static int test_wc_Sha224HmacSetKey(void)
  13579. {
  13580. EXPECT_DECLS;
  13581. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13582. Hmac hmac;
  13583. int ret, times, itr;
  13584. const char* keys[]=
  13585. {
  13586. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13587. "\x0b\x0b\x0b",
  13588. #ifndef HAVE_FIPS
  13589. "Jefe", /* smaller than minimum FIPS key size */
  13590. #endif
  13591. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13592. "\xAA\xAA\xAA"
  13593. };
  13594. times = sizeof(keys) / sizeof(char*);
  13595. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13596. for (itr = 0; itr < times; itr++) {
  13597. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13598. (word32)XSTRLEN(keys[itr])), 0);
  13599. }
  13600. /* Bad args. */
  13601. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13602. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13603. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  13604. BAD_FUNC_ARG);
  13605. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13606. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13607. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13608. #ifdef HAVE_FIPS
  13609. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13610. #else
  13611. ExpectIntEQ(ret, 0);
  13612. #endif
  13613. wc_HmacFree(&hmac);
  13614. #endif
  13615. return EXPECT_RESULT();
  13616. } /* END test_wc_Sha224HmacSetKey() */
  13617. /*
  13618. * testing wc_HmacSetKey() on Sha256 hash
  13619. */
  13620. static int test_wc_Sha256HmacSetKey(void)
  13621. {
  13622. EXPECT_DECLS;
  13623. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13624. Hmac hmac;
  13625. int ret, times, itr;
  13626. const char* keys[]=
  13627. {
  13628. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13629. "\x0b\x0b\x0b",
  13630. #ifndef HAVE_FIPS
  13631. "Jefe", /* smaller than minimum FIPS key size */
  13632. #endif
  13633. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13634. "\xAA\xAA\xAA"
  13635. };
  13636. times = sizeof(keys) / sizeof(char*);
  13637. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13638. for (itr = 0; itr < times; itr++) {
  13639. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13640. (word32)XSTRLEN(keys[itr])), 0);
  13641. }
  13642. /* Bad args. */
  13643. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13644. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13645. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  13646. BAD_FUNC_ARG);
  13647. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13648. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13649. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13650. #ifdef HAVE_FIPS
  13651. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13652. #else
  13653. ExpectIntEQ(ret, 0);
  13654. #endif
  13655. wc_HmacFree(&hmac);
  13656. #endif
  13657. return EXPECT_RESULT();
  13658. } /* END test_wc_Sha256HmacSetKey() */
  13659. /*
  13660. * testing wc_HmacSetKey on Sha384 hash.
  13661. */
  13662. static int test_wc_Sha384HmacSetKey(void)
  13663. {
  13664. EXPECT_DECLS;
  13665. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13666. Hmac hmac;
  13667. int ret, times, itr;
  13668. const char* keys[]=
  13669. {
  13670. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13671. "\x0b\x0b\x0b",
  13672. #ifndef HAVE_FIPS
  13673. "Jefe", /* smaller than minimum FIPS key size */
  13674. #endif
  13675. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13676. "\xAA\xAA\xAA"
  13677. };
  13678. times = sizeof(keys) / sizeof(char*);
  13679. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13680. for (itr = 0; itr < times; itr++) {
  13681. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13682. (word32)XSTRLEN(keys[itr])), 0);
  13683. }
  13684. /* Bad args. */
  13685. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13686. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13687. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  13688. BAD_FUNC_ARG);
  13689. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13690. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13691. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13692. #ifdef HAVE_FIPS
  13693. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13694. #else
  13695. ExpectIntEQ(ret, 0);
  13696. #endif
  13697. wc_HmacFree(&hmac);
  13698. #endif
  13699. return EXPECT_RESULT();
  13700. } /* END test_wc_Sha384HmacSetKey() */
  13701. /*
  13702. * testing wc_HmacUpdate on wc_Md5 hash.
  13703. */
  13704. static int test_wc_Md5HmacUpdate(void)
  13705. {
  13706. EXPECT_DECLS;
  13707. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13708. Hmac hmac;
  13709. testVector a, b;
  13710. #ifdef HAVE_FIPS
  13711. const char* keys =
  13712. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13713. #else
  13714. const char* keys = "Jefe";
  13715. #endif
  13716. a.input = "what do ya want for nothing?";
  13717. a.inLen = XSTRLEN(a.input);
  13718. b.input = "Hi There";
  13719. b.inLen = XSTRLEN(b.input);
  13720. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13721. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  13722. (word32)XSTRLEN(keys)), 0);
  13723. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13724. /* Update Hmac. */
  13725. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13726. /* Test bad args. */
  13727. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13728. BAD_FUNC_ARG);
  13729. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13730. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13731. wc_HmacFree(&hmac);
  13732. #endif
  13733. return EXPECT_RESULT();
  13734. } /* END test_wc_Md5HmacUpdate */
  13735. /*
  13736. * testing wc_HmacUpdate on SHA hash.
  13737. */
  13738. static int test_wc_ShaHmacUpdate(void)
  13739. {
  13740. EXPECT_DECLS;
  13741. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13742. Hmac hmac;
  13743. testVector a, b;
  13744. #ifdef HAVE_FIPS
  13745. const char* keys =
  13746. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13747. #else
  13748. const char* keys = "Jefe";
  13749. #endif
  13750. a.input = "what do ya want for nothing?";
  13751. a.inLen = XSTRLEN(a.input);
  13752. b.input = "Hi There";
  13753. b.inLen = XSTRLEN(b.input);
  13754. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13755. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  13756. (word32)XSTRLEN(keys)), 0);
  13757. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13758. /* Update Hmac. */
  13759. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13760. /* Test bad args. */
  13761. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13762. BAD_FUNC_ARG);
  13763. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13764. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13765. wc_HmacFree(&hmac);
  13766. #endif
  13767. return EXPECT_RESULT();
  13768. } /* END test_wc_ShaHmacUpdate */
  13769. /*
  13770. * testing wc_HmacUpdate on SHA224 hash.
  13771. */
  13772. static int test_wc_Sha224HmacUpdate(void)
  13773. {
  13774. EXPECT_DECLS;
  13775. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13776. Hmac hmac;
  13777. testVector a, b;
  13778. #ifdef HAVE_FIPS
  13779. const char* keys =
  13780. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13781. #else
  13782. const char* keys = "Jefe";
  13783. #endif
  13784. a.input = "what do ya want for nothing?";
  13785. a.inLen = XSTRLEN(a.input);
  13786. b.input = "Hi There";
  13787. b.inLen = XSTRLEN(b.input);
  13788. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13789. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  13790. (word32)XSTRLEN(keys)), 0);
  13791. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13792. /* Update Hmac. */
  13793. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13794. /* Test bad args. */
  13795. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13796. BAD_FUNC_ARG);
  13797. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13798. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13799. wc_HmacFree(&hmac);
  13800. #endif
  13801. return EXPECT_RESULT();
  13802. } /* END test_wc_Sha224HmacUpdate */
  13803. /*
  13804. * testing wc_HmacUpdate on SHA256 hash.
  13805. */
  13806. static int test_wc_Sha256HmacUpdate(void)
  13807. {
  13808. EXPECT_DECLS;
  13809. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13810. Hmac hmac;
  13811. testVector a, b;
  13812. #ifdef HAVE_FIPS
  13813. const char* keys =
  13814. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13815. #else
  13816. const char* keys = "Jefe";
  13817. #endif
  13818. a.input = "what do ya want for nothing?";
  13819. a.inLen = XSTRLEN(a.input);
  13820. b.input = "Hi There";
  13821. b.inLen = XSTRLEN(b.input);
  13822. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13823. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  13824. (word32)XSTRLEN(keys)), 0);
  13825. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13826. /* Update Hmac. */
  13827. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13828. /* Test bad args. */
  13829. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13830. BAD_FUNC_ARG);
  13831. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13832. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13833. wc_HmacFree(&hmac);
  13834. #endif
  13835. return EXPECT_RESULT();
  13836. } /* END test_wc_Sha256HmacUpdate */
  13837. /*
  13838. * testing wc_HmacUpdate on SHA384 hash.
  13839. */
  13840. static int test_wc_Sha384HmacUpdate(void)
  13841. {
  13842. EXPECT_DECLS;
  13843. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13844. Hmac hmac;
  13845. testVector a, b;
  13846. #ifdef HAVE_FIPS
  13847. const char* keys =
  13848. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13849. #else
  13850. const char* keys = "Jefe";
  13851. #endif
  13852. a.input = "what do ya want for nothing?";
  13853. a.inLen = XSTRLEN(a.input);
  13854. b.input = "Hi There";
  13855. b.inLen = XSTRLEN(b.input);
  13856. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13857. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  13858. (word32)XSTRLEN(keys)), 0);
  13859. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13860. /* Update Hmac. */
  13861. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13862. /* Test bad args. */
  13863. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13864. BAD_FUNC_ARG);
  13865. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13866. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13867. wc_HmacFree(&hmac);
  13868. #endif
  13869. return EXPECT_RESULT();
  13870. } /* END test_wc_Sha384HmacUpdate */
  13871. /*
  13872. * Testing wc_HmacFinal() with MD5
  13873. */
  13874. static int test_wc_Md5HmacFinal(void)
  13875. {
  13876. EXPECT_DECLS;
  13877. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13878. Hmac hmac;
  13879. byte hash[WC_MD5_DIGEST_SIZE];
  13880. testVector a;
  13881. const char* key;
  13882. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13883. a.input = "Hi There";
  13884. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13885. "\x9d";
  13886. a.inLen = XSTRLEN(a.input);
  13887. a.outLen = XSTRLEN(a.output);
  13888. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13889. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  13890. 0);
  13891. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13892. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13893. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  13894. /* Try bad parameters. */
  13895. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13896. #ifndef HAVE_FIPS
  13897. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13898. #endif
  13899. wc_HmacFree(&hmac);
  13900. #endif
  13901. return EXPECT_RESULT();
  13902. } /* END test_wc_Md5HmacFinal */
  13903. /*
  13904. * Testing wc_HmacFinal() with SHA
  13905. */
  13906. static int test_wc_ShaHmacFinal(void)
  13907. {
  13908. EXPECT_DECLS;
  13909. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13910. Hmac hmac;
  13911. byte hash[WC_SHA_DIGEST_SIZE];
  13912. testVector a;
  13913. const char* key;
  13914. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13915. "\x0b\x0b\x0b";
  13916. a.input = "Hi There";
  13917. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13918. "\x8e\xf1\x46\xbe\x00";
  13919. a.inLen = XSTRLEN(a.input);
  13920. a.outLen = XSTRLEN(a.output);
  13921. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13922. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  13923. 0);
  13924. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13925. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13926. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  13927. /* Try bad parameters. */
  13928. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13929. #ifndef HAVE_FIPS
  13930. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13931. #endif
  13932. wc_HmacFree(&hmac);
  13933. #endif
  13934. return EXPECT_RESULT();
  13935. } /* END test_wc_ShaHmacFinal */
  13936. /*
  13937. * Testing wc_HmacFinal() with SHA224
  13938. */
  13939. static int test_wc_Sha224HmacFinal(void)
  13940. {
  13941. EXPECT_DECLS;
  13942. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13943. Hmac hmac;
  13944. byte hash[WC_SHA224_DIGEST_SIZE];
  13945. testVector a;
  13946. const char* key;
  13947. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13948. "\x0b\x0b\x0b";
  13949. a.input = "Hi There";
  13950. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13951. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13952. a.inLen = XSTRLEN(a.input);
  13953. a.outLen = XSTRLEN(a.output);
  13954. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13955. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  13956. (word32)XSTRLEN(key)), 0);
  13957. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13958. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13959. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13960. /* Try bad parameters. */
  13961. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13962. #ifndef HAVE_FIPS
  13963. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13964. #endif
  13965. wc_HmacFree(&hmac);
  13966. #endif
  13967. return EXPECT_RESULT();
  13968. } /* END test_wc_Sha224HmacFinal */
  13969. /*
  13970. * Testing wc_HmacFinal() with SHA256
  13971. */
  13972. static int test_wc_Sha256HmacFinal(void)
  13973. {
  13974. EXPECT_DECLS;
  13975. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13976. Hmac hmac;
  13977. byte hash[WC_SHA256_DIGEST_SIZE];
  13978. testVector a;
  13979. const char* key;
  13980. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13981. "\x0b\x0b\x0b";
  13982. a.input = "Hi There";
  13983. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13984. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13985. "\xcf\xf7";
  13986. a.inLen = XSTRLEN(a.input);
  13987. a.outLen = XSTRLEN(a.output);
  13988. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13989. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  13990. (word32)XSTRLEN(key)), 0);
  13991. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13992. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13993. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  13994. /* Try bad parameters. */
  13995. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13996. #ifndef HAVE_FIPS
  13997. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13998. #endif
  13999. wc_HmacFree(&hmac);
  14000. #endif
  14001. return EXPECT_RESULT();
  14002. } /* END test_wc_Sha256HmacFinal */
  14003. /*
  14004. * Testing wc_HmacFinal() with SHA384
  14005. */
  14006. static int test_wc_Sha384HmacFinal(void)
  14007. {
  14008. EXPECT_DECLS;
  14009. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14010. Hmac hmac;
  14011. byte hash[WC_SHA384_DIGEST_SIZE];
  14012. testVector a;
  14013. const char* key;
  14014. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14015. "\x0b\x0b\x0b";
  14016. a.input = "Hi There";
  14017. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14018. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14019. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14020. "\xfa\x9c\xb6";
  14021. a.inLen = XSTRLEN(a.input);
  14022. a.outLen = XSTRLEN(a.output);
  14023. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14024. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14025. (word32)XSTRLEN(key)), 0);
  14026. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14027. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14028. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14029. /* Try bad parameters. */
  14030. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14031. #ifndef HAVE_FIPS
  14032. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14033. #endif
  14034. wc_HmacFree(&hmac);
  14035. #endif
  14036. return EXPECT_RESULT();
  14037. } /* END test_wc_Sha384HmacFinal */
  14038. /*
  14039. * Testing wc_InitCmac()
  14040. */
  14041. static int test_wc_InitCmac(void)
  14042. {
  14043. EXPECT_DECLS;
  14044. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14045. Cmac cmac1;
  14046. Cmac cmac2;
  14047. Cmac cmac3;
  14048. /* AES 128 key. */
  14049. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14050. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14051. /* AES 192 key. */
  14052. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14053. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14054. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14055. /* AES 256 key. */
  14056. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14057. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14058. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14059. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14060. word32 key1Sz = (word32)sizeof(key1) - 1;
  14061. word32 key2Sz = (word32)sizeof(key2) - 1;
  14062. word32 key3Sz = (word32)sizeof(key3) - 1;
  14063. int type = WC_CMAC_AES;
  14064. (void)key1;
  14065. (void)key1Sz;
  14066. (void)key2;
  14067. (void)key2Sz;
  14068. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14069. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14070. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14071. #ifdef WOLFSSL_AES_128
  14072. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14073. #endif
  14074. #ifdef WOLFSSL_AES_192
  14075. wc_AesFree(&cmac1.aes);
  14076. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14077. #endif
  14078. #ifdef WOLFSSL_AES_256
  14079. wc_AesFree(&cmac2.aes);
  14080. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14081. #endif
  14082. wc_AesFree(&cmac3.aes);
  14083. /* Test bad args. */
  14084. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14085. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14086. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14087. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14088. #endif
  14089. return EXPECT_RESULT();
  14090. } /* END test_wc_InitCmac */
  14091. /*
  14092. * Testing wc_CmacUpdate()
  14093. */
  14094. static int test_wc_CmacUpdate(void)
  14095. {
  14096. EXPECT_DECLS;
  14097. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14098. Cmac cmac;
  14099. byte key[] = {
  14100. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14101. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14102. };
  14103. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14104. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14105. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14106. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14107. "\xf4";
  14108. word32 inSz = (word32)sizeof(in) - 1;
  14109. word32 keySz = (word32)sizeof(key);
  14110. int type = WC_CMAC_AES;
  14111. XMEMSET(&cmac, 0, sizeof(Cmac));
  14112. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14113. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14114. /* Test bad args. */
  14115. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14116. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14117. wc_AesFree(&cmac.aes);
  14118. #endif
  14119. return EXPECT_RESULT();
  14120. } /* END test_wc_CmacUpdate */
  14121. /*
  14122. * Testing wc_CmacFinal()
  14123. */
  14124. static int test_wc_CmacFinal(void)
  14125. {
  14126. EXPECT_DECLS;
  14127. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14128. Cmac cmac;
  14129. byte key[] = {
  14130. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14131. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14132. };
  14133. byte msg[] = {
  14134. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14135. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14136. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14137. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14138. 0xf4
  14139. };
  14140. /* Test vectors from CMACGenAES128.rsp from
  14141. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14142. * Per RFC4493 truncation of lsb is possible.
  14143. */
  14144. byte expMac[] = {
  14145. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14146. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14147. };
  14148. byte mac[AES_BLOCK_SIZE];
  14149. word32 msgSz = (word32)sizeof(msg);
  14150. word32 keySz = (word32)sizeof(key);
  14151. word32 macSz = sizeof(mac);
  14152. word32 badMacSz = 17;
  14153. int expMacSz = sizeof(expMac);
  14154. int type = WC_CMAC_AES;
  14155. XMEMSET(&cmac, 0, sizeof(Cmac));
  14156. XMEMSET(mac, 0, macSz);
  14157. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14158. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14159. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14160. /* Pass in bad args. */
  14161. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14162. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14163. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14164. /* For the last call, use the API with implicit wc_CmacFree(). */
  14165. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14166. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14167. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14168. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14169. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14170. /* Pass in bad args. */
  14171. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14172. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14173. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14174. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14175. #endif
  14176. return EXPECT_RESULT();
  14177. } /* END test_wc_CmacFinal */
  14178. /*
  14179. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14180. */
  14181. static int test_wc_AesCmacGenerate(void)
  14182. {
  14183. EXPECT_DECLS;
  14184. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14185. byte key[] = {
  14186. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14187. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14188. };
  14189. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14190. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14191. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14192. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14193. byte mac[AES_BLOCK_SIZE];
  14194. word32 keySz = sizeof(key);
  14195. word32 macSz = sizeof(mac);
  14196. word32 msgSz = sizeof(msg) - 1;
  14197. word32 expMacSz = sizeof(expMac) - 1;
  14198. XMEMSET(mac, 0, macSz);
  14199. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14200. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14201. /* Pass in bad args. */
  14202. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14203. BAD_FUNC_ARG);
  14204. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14205. BAD_FUNC_ARG);
  14206. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14207. BAD_FUNC_ARG);
  14208. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14209. BAD_FUNC_ARG);
  14210. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14211. /* Test bad args. */
  14212. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14213. BAD_FUNC_ARG);
  14214. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14215. BAD_FUNC_ARG);
  14216. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14217. BAD_FUNC_ARG);
  14218. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14219. BAD_FUNC_ARG);
  14220. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14221. BAD_FUNC_ARG);
  14222. #endif
  14223. return EXPECT_RESULT();
  14224. } /* END test_wc_AesCmacGenerate */
  14225. /*
  14226. * Testing streaming AES-GCM API.
  14227. */
  14228. static int test_wc_AesGcmStream(void)
  14229. {
  14230. EXPECT_DECLS;
  14231. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14232. defined(WOLFSSL_AESGCM_STREAM)
  14233. int i;
  14234. WC_RNG rng[1];
  14235. Aes aesEnc[1];
  14236. Aes aesDec[1];
  14237. byte tag[AES_BLOCK_SIZE];
  14238. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14239. byte out[AES_BLOCK_SIZE * 3 + 2];
  14240. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14241. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14242. byte key[AES_128_KEY_SIZE] = { 0, };
  14243. byte iv[AES_IV_SIZE] = { 1, };
  14244. byte ivOut[AES_IV_SIZE];
  14245. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14246. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14247. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14248. };
  14249. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14250. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14251. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14252. };
  14253. static const byte expTag[AES_BLOCK_SIZE] = {
  14254. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14255. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14256. };
  14257. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14258. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14259. XMEMSET(&aesDec, 0, sizeof(Aes));
  14260. /* Create a random for generating IV/nonce. */
  14261. ExpectIntEQ(wc_InitRng(rng), 0);
  14262. /* Initialize data structures. */
  14263. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14264. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14265. /* BadParameters to streaming init. */
  14266. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14267. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14268. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14269. BAD_FUNC_ARG);
  14270. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14271. BAD_FUNC_ARG);
  14272. /* Bad parameters to encrypt update. */
  14273. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14274. BAD_FUNC_ARG);
  14275. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14276. BAD_FUNC_ARG);
  14277. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14278. BAD_FUNC_ARG);
  14279. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14280. BAD_FUNC_ARG);
  14281. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14282. BAD_FUNC_ARG);
  14283. /* Bad parameters to decrypt update. */
  14284. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14285. BAD_FUNC_ARG);
  14286. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14287. BAD_FUNC_ARG);
  14288. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14289. BAD_FUNC_ARG);
  14290. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14291. BAD_FUNC_ARG);
  14292. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14293. BAD_FUNC_ARG);
  14294. /* Bad parameters to encrypt final. */
  14295. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14296. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14297. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14298. BAD_FUNC_ARG);
  14299. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14300. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14301. BAD_FUNC_ARG);
  14302. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14303. BAD_FUNC_ARG);
  14304. /* Bad parameters to decrypt final. */
  14305. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14306. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14307. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14308. BAD_FUNC_ARG);
  14309. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14310. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14311. BAD_FUNC_ARG);
  14312. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14313. BAD_FUNC_ARG);
  14314. /* Check calling final before setting key fails. */
  14315. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14316. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14317. /* Check calling update before setting key else fails. */
  14318. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14319. MISSING_KEY);
  14320. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14321. MISSING_KEY);
  14322. /* Set key but not IV. */
  14323. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14324. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14325. /* Check calling final before setting IV fails. */
  14326. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14327. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14328. /* Check calling update before setting IV else fails. */
  14329. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14330. MISSING_IV);
  14331. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14332. MISSING_IV);
  14333. /* Set IV using fixed part IV and external IV APIs. */
  14334. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14335. rng), 0);
  14336. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14337. GCM_NONCE_MID_SZ), 0);
  14338. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14339. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14340. /* Encrypt and decrypt data. */
  14341. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14342. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14343. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14344. /* Finalize and check tag matches. */
  14345. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14346. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14347. /* Set key and IV through streaming init API. */
  14348. wc_AesFree(aesEnc);
  14349. wc_AesFree(aesDec);
  14350. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14351. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14352. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14353. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14354. /* Encrypt/decrypt one block and AAD of one block. */
  14355. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14356. AES_BLOCK_SIZE), 0);
  14357. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14358. AES_BLOCK_SIZE), 0);
  14359. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14360. /* Finalize and check tag matches. */
  14361. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14362. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14363. /* Set key and IV through streaming init API. */
  14364. wc_AesFree(aesEnc);
  14365. wc_AesFree(aesDec);
  14366. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14367. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14368. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14369. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14370. /* No data to encrypt/decrypt one byte of AAD. */
  14371. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14372. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14373. /* Finalize and check tag matches. */
  14374. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14375. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14376. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14377. /* Set key and IV through streaming init API. */
  14378. wc_AesFree(aesEnc);
  14379. wc_AesFree(aesDec);
  14380. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14381. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14382. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14383. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14384. /* Encrypt/decrypt one byte and no AAD. */
  14385. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14386. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14387. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14388. /* Finalize and check tag matches. */
  14389. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14390. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14391. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14392. /* Set key and IV through streaming init API. */
  14393. wc_AesFree(aesEnc);
  14394. wc_AesFree(aesDec);
  14395. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14396. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14397. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14398. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14399. /* Encryption AES is one byte at a time */
  14400. for (i = 0; i < (int)sizeof(aad); i++) {
  14401. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14402. 0);
  14403. }
  14404. for (i = 0; i < (int)sizeof(in); i++) {
  14405. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14406. 0);
  14407. }
  14408. /* Decryption AES is two bytes at a time */
  14409. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14410. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14411. 0);
  14412. }
  14413. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14414. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14415. 0), 0);
  14416. }
  14417. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14418. /* Finalize and check tag matches. */
  14419. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14420. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14421. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14422. /* Check streaming encryption can be decrypted with one shot. */
  14423. wc_AesFree(aesDec);
  14424. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14425. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14426. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14427. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14428. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14429. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14430. wc_AesFree(aesEnc);
  14431. wc_AesFree(aesDec);
  14432. wc_FreeRng(rng);
  14433. #endif
  14434. return EXPECT_RESULT();
  14435. } /* END test_wc_AesGcmStream */
  14436. /*
  14437. * Testing streaming SM4 API.
  14438. */
  14439. static int test_wc_Sm4(void)
  14440. {
  14441. int res = TEST_SKIPPED;
  14442. #ifdef WOLFSSL_SM4
  14443. EXPECT_DECLS;
  14444. wc_Sm4 sm4;
  14445. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14446. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14447. unsigned char key[SM4_KEY_SIZE];
  14448. #endif
  14449. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14450. unsigned char iv[SM4_IV_SIZE];
  14451. #endif
  14452. /* Invalid parameters - wc_Sm4Init */
  14453. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14454. /* Valid cases - wc_Sm4Init */
  14455. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14456. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14457. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14458. XMEMSET(key, 0, sizeof(key));
  14459. /* Invalid parameters - wc_Sm4SetKey. */
  14460. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14461. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14462. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14463. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14464. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14465. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14466. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14467. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14468. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14469. /* Valid cases - wc_Sm4SetKey. */
  14470. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14471. #endif
  14472. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14473. XMEMSET(iv, 0, sizeof(iv));
  14474. /* Invalid parameters - wc_Sm4SetIV. */
  14475. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14476. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14477. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14478. /* Valid cases - wc_Sm4SetIV. */
  14479. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14480. #endif
  14481. /* Valid cases - wc_Sm4Free */
  14482. wc_Sm4Free(NULL);
  14483. wc_Sm4Free(&sm4);
  14484. res = EXPECT_RESULT();
  14485. #endif
  14486. return res;
  14487. } /* END test_wc_Sm4 */
  14488. /*
  14489. * Testing block based SM4-ECB API.
  14490. */
  14491. static int test_wc_Sm4Ecb(void)
  14492. {
  14493. int res = TEST_SKIPPED;
  14494. #ifdef WOLFSSL_SM4_ECB
  14495. EXPECT_DECLS;
  14496. wc_Sm4 sm4;
  14497. unsigned char key[SM4_KEY_SIZE];
  14498. unsigned char in[SM4_BLOCK_SIZE * 2];
  14499. unsigned char out[SM4_BLOCK_SIZE * 2];
  14500. unsigned char out2[SM4_BLOCK_SIZE];
  14501. XMEMSET(key, 0, sizeof(key));
  14502. XMEMSET(in, 0, sizeof(in));
  14503. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14504. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14505. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14506. /* Tested in test_wc_Sm4. */
  14507. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14508. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14509. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14510. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14511. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14512. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14513. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14514. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14515. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14516. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14517. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14518. /* Valid cases - wc_Sm4EcbEncrypt. */
  14519. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14520. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14521. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14522. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14523. /* In and out are same pointer. */
  14524. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14525. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14526. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14527. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14528. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14529. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14530. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14531. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14532. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14533. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14534. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14535. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14536. /* Valid cases - wc_Sm4EcbDecrypt. */
  14537. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14538. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14539. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14540. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14541. /* In and out are same pointer. */
  14542. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14543. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14544. wc_Sm4Free(&sm4);
  14545. res = EXPECT_RESULT();
  14546. #endif
  14547. return res;
  14548. } /* END test_wc_Sm4Ecb */
  14549. /*
  14550. * Testing block based SM4-CBC API.
  14551. */
  14552. static int test_wc_Sm4Cbc(void)
  14553. {
  14554. int res = TEST_SKIPPED;
  14555. #ifdef WOLFSSL_SM4_CBC
  14556. EXPECT_DECLS;
  14557. wc_Sm4 sm4;
  14558. unsigned char key[SM4_KEY_SIZE];
  14559. unsigned char iv[SM4_IV_SIZE];
  14560. unsigned char in[SM4_BLOCK_SIZE * 2];
  14561. unsigned char out[SM4_BLOCK_SIZE * 2];
  14562. unsigned char out2[SM4_BLOCK_SIZE];
  14563. XMEMSET(key, 0, sizeof(key));
  14564. XMEMSET(iv, 0, sizeof(iv));
  14565. XMEMSET(in, 0, sizeof(in));
  14566. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14567. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14568. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14569. /* Tested in test_wc_Sm4. */
  14570. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14571. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14572. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14573. /* Tested in test_wc_Sm4. */
  14574. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14575. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  14576. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14577. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14578. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14579. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14580. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14581. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14582. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14583. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14584. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14585. /* Valid cases - wc_Sm4CbcEncrypt. */
  14586. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  14587. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14588. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14589. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14590. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14591. /* In and out are same pointer. */
  14592. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14593. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14594. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14595. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  14596. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14597. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14598. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14599. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14600. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14601. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14602. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14603. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14604. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14605. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14606. /* Valid cases - wc_Sm4CbcDecrypt. */
  14607. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  14608. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14609. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14610. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14611. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14612. /* In and out are same pointer. */
  14613. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14614. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14615. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14616. wc_Sm4Free(&sm4);
  14617. res = EXPECT_RESULT();
  14618. #endif
  14619. return res;
  14620. } /* END test_wc_Sm4Cbc */
  14621. /*
  14622. * Testing streaming SM4-CTR API.
  14623. */
  14624. static int test_wc_Sm4Ctr(void)
  14625. {
  14626. int res = TEST_SKIPPED;
  14627. #ifdef WOLFSSL_SM4_CTR
  14628. EXPECT_DECLS;
  14629. wc_Sm4 sm4;
  14630. unsigned char key[SM4_KEY_SIZE];
  14631. unsigned char iv[SM4_IV_SIZE];
  14632. unsigned char in[SM4_BLOCK_SIZE * 4];
  14633. unsigned char out[SM4_BLOCK_SIZE * 4];
  14634. unsigned char out2[SM4_BLOCK_SIZE * 4];
  14635. word32 chunk;
  14636. word32 i;
  14637. XMEMSET(key, 0, sizeof(key));
  14638. XMEMSET(iv, 0, sizeof(iv));
  14639. XMEMSET(in, 0, sizeof(in));
  14640. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14641. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14642. /* Tested in test_wc_Sm4. */
  14643. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14644. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  14645. /* Tested in test_wc_Sm4. */
  14646. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14647. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  14648. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14649. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  14650. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  14651. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  14652. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14653. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14654. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14655. /* Valid cases - wc_Sm4CtrEncrypt. */
  14656. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  14657. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  14658. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14659. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  14660. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  14661. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14662. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14663. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  14664. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14665. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14666. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14667. /* In and out are same pointer. Also check encrypt of cipher text produces
  14668. * plaintext.
  14669. */
  14670. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14671. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  14672. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14673. /* Chunking tests. */
  14674. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14675. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  14676. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14677. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14678. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  14679. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  14680. }
  14681. if (i < (word32)sizeof(in)) {
  14682. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  14683. (word32)sizeof(in) - i), 0);
  14684. }
  14685. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  14686. }
  14687. for (i = 0; i < (word32)sizeof(iv); i++) {
  14688. iv[i] = 0xff;
  14689. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14690. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14691. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14692. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  14693. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  14694. }
  14695. wc_Sm4Free(&sm4);
  14696. res = EXPECT_RESULT();
  14697. #endif
  14698. return res;
  14699. } /* END test_wc_Sm4Ctr */
  14700. /*
  14701. * Testing stream SM4-GCM API.
  14702. */
  14703. static int test_wc_Sm4Gcm(void)
  14704. {
  14705. int res = TEST_SKIPPED;
  14706. #ifdef WOLFSSL_SM4_GCM
  14707. EXPECT_DECLS;
  14708. wc_Sm4 sm4;
  14709. unsigned char key[SM4_KEY_SIZE];
  14710. unsigned char nonce[GCM_NONCE_MAX_SZ];
  14711. unsigned char in[SM4_BLOCK_SIZE * 2];
  14712. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14713. unsigned char out[SM4_BLOCK_SIZE * 2];
  14714. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14715. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14716. unsigned char tag[SM4_BLOCK_SIZE];
  14717. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14718. word32 i;
  14719. XMEMSET(key, 0, sizeof(key));
  14720. XMEMSET(nonce, 0, sizeof(nonce));
  14721. XMEMSET(in, 0, sizeof(in));
  14722. XMEMSET(in2, 0, sizeof(in2));
  14723. XMEMSET(aad, 0, sizeof(aad));
  14724. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14725. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14726. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14727. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14728. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14729. /* Invalid parameters - wc_Sm4GcmSetKey. */
  14730. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14731. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14732. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  14733. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14734. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14735. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14736. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14737. /* Valid parameters - wc_Sm4GcmSetKey. */
  14738. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14739. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  14740. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14741. 0), BAD_FUNC_ARG);
  14742. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14743. 0), BAD_FUNC_ARG);
  14744. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14745. 0), BAD_FUNC_ARG);
  14746. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14747. 0), BAD_FUNC_ARG);
  14748. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14749. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14750. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14751. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14752. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14753. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14754. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14755. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14756. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14757. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14758. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14759. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14760. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14761. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14762. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14763. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14764. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14765. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14766. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14767. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14768. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  14769. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14770. 0), BAD_FUNC_ARG);
  14771. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14772. 0), BAD_FUNC_ARG);
  14773. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14774. 0), BAD_FUNC_ARG);
  14775. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14776. 0), BAD_FUNC_ARG);
  14777. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14778. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14779. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14780. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14781. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14782. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14783. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14784. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14785. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14786. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14787. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14788. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14789. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14790. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14791. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14792. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14793. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14794. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14795. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14796. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14797. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14798. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14799. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14800. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14801. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14802. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14803. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14804. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14805. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14806. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14807. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14808. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14809. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14810. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14811. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14812. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14813. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14814. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14815. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14816. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14817. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14818. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14819. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14820. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14821. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14822. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14823. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14824. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14825. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14826. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14827. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14828. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14829. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14830. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14831. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14832. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14833. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14834. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14835. SM4_GCM_AUTH_E);
  14836. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14837. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  14838. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14839. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14840. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14841. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14842. }
  14843. /* Check different in/out sizes. */
  14844. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  14845. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14846. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  14847. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14848. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  14849. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14850. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14851. XMEMCPY(out2, out, i - 1);
  14852. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  14853. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14854. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14855. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  14856. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14857. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14858. }
  14859. /* Force the counter to roll over in first byte. */
  14860. {
  14861. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14862. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14863. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14864. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14865. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14866. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14867. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14868. }
  14869. wc_Sm4Free(&sm4);
  14870. res = EXPECT_RESULT();
  14871. #endif
  14872. return res;
  14873. } /* END test_wc_Sm4Gcm */
  14874. /*
  14875. * Testing stream SM4-CCM API.
  14876. */
  14877. static int test_wc_Sm4Ccm(void)
  14878. {
  14879. int res = TEST_SKIPPED;
  14880. #ifdef WOLFSSL_SM4_CCM
  14881. EXPECT_DECLS;
  14882. wc_Sm4 sm4;
  14883. unsigned char key[SM4_KEY_SIZE];
  14884. unsigned char nonce[CCM_NONCE_MAX_SZ];
  14885. unsigned char in[SM4_BLOCK_SIZE * 2];
  14886. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14887. unsigned char out[SM4_BLOCK_SIZE * 2];
  14888. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14889. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14890. unsigned char tag[SM4_BLOCK_SIZE];
  14891. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14892. word32 i;
  14893. XMEMSET(key, 0, sizeof(key));
  14894. XMEMSET(nonce, 0, sizeof(nonce));
  14895. XMEMSET(in, 0, sizeof(in));
  14896. XMEMSET(in2, 0, sizeof(in2));
  14897. XMEMSET(aad, 0, sizeof(aad));
  14898. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14899. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14900. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14901. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14902. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14903. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14904. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  14905. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14906. 0), BAD_FUNC_ARG);
  14907. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14908. 0), BAD_FUNC_ARG);
  14909. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14910. 0), BAD_FUNC_ARG);
  14911. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14912. 0), BAD_FUNC_ARG);
  14913. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14914. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14915. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14916. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14917. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14918. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14919. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14920. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14921. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14922. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14923. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14924. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14925. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14926. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14927. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14928. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14929. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14930. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14931. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14932. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14933. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  14934. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14935. 0), BAD_FUNC_ARG);
  14936. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14937. 0), BAD_FUNC_ARG);
  14938. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14939. 0), BAD_FUNC_ARG);
  14940. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14941. 0), BAD_FUNC_ARG);
  14942. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14943. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14944. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14945. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14946. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14947. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14948. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14949. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14950. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14951. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14952. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14953. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14954. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14955. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14956. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14957. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14958. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14959. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14960. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14961. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14962. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14963. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14964. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14965. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14966. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14967. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14968. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14969. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14970. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14971. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14972. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14973. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14974. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14975. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14976. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14977. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14978. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14979. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14980. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14981. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14982. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14983. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14984. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14985. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14986. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14987. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14988. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14989. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14990. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  14991. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14992. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14993. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14994. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14995. }
  14996. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14997. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14998. SM4_CCM_AUTH_E);
  14999. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15000. for (i = 0; i < 4; i++) {
  15001. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15002. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15003. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15004. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15005. }
  15006. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15007. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15008. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15009. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15010. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15011. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15012. }
  15013. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15014. * Even values in range 4..SM4_BLOCK_SIZE.
  15015. */
  15016. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15017. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15018. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15019. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15020. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15021. }
  15022. /* Check different in/out sizes. */
  15023. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15024. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15025. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15026. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15027. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15028. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15029. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15030. XMEMCPY(out2, out, i - 1);
  15031. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15032. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15033. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15034. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15035. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15036. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15037. }
  15038. /* Force the counter to roll over in first byte. */
  15039. {
  15040. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15041. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15042. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15043. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15044. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15045. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15046. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15047. }
  15048. wc_Sm4Free(&sm4);
  15049. res = EXPECT_RESULT();
  15050. #endif
  15051. return res;
  15052. } /* END test_wc_Sm4Ccm */
  15053. /*
  15054. * unit test for wc_Des3_SetIV()
  15055. */
  15056. static int test_wc_Des3_SetIV(void)
  15057. {
  15058. EXPECT_DECLS;
  15059. #ifndef NO_DES3
  15060. Des3 des;
  15061. const byte key[] = {
  15062. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15063. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15064. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15065. };
  15066. const byte iv[] = {
  15067. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15068. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15069. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15070. };
  15071. XMEMSET(&des, 0, sizeof(Des3));
  15072. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15073. /* DES_ENCRYPTION or DES_DECRYPTION */
  15074. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15075. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15076. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15077. /* Test explicitly wc_Des3_SetIV() */
  15078. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15079. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15080. #endif
  15081. wc_Des3Free(&des);
  15082. #endif
  15083. return EXPECT_RESULT();
  15084. } /* END test_wc_Des3_SetIV */
  15085. /*
  15086. * unit test for wc_Des3_SetKey()
  15087. */
  15088. static int test_wc_Des3_SetKey(void)
  15089. {
  15090. EXPECT_DECLS;
  15091. #ifndef NO_DES3
  15092. Des3 des;
  15093. const byte key[] = {
  15094. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15095. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15096. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15097. };
  15098. const byte iv[] = {
  15099. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15100. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15101. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15102. };
  15103. XMEMSET(&des, 0, sizeof(Des3));
  15104. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15105. /* DES_ENCRYPTION or DES_DECRYPTION */
  15106. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15107. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15108. /* Test bad args. */
  15109. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15110. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15111. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15112. /* Default case. Should return 0. */
  15113. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15114. wc_Des3Free(&des);
  15115. #endif
  15116. return EXPECT_RESULT();
  15117. } /* END test_wc_Des3_SetKey */
  15118. /*
  15119. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15120. */
  15121. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15122. {
  15123. EXPECT_DECLS;
  15124. #ifndef NO_DES3
  15125. Des3 des;
  15126. byte cipher[24];
  15127. byte plain[24];
  15128. const byte key[] = {
  15129. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15130. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15131. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15132. };
  15133. const byte iv[] = {
  15134. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15135. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15136. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15137. };
  15138. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15139. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15140. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15141. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15142. };
  15143. XMEMSET(&des, 0, sizeof(Des3));
  15144. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15145. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15146. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15147. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15148. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15149. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15150. /* Pass in bad args. */
  15151. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15152. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15153. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15154. BAD_FUNC_ARG);
  15155. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15156. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15157. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15158. wc_Des3Free(&des);
  15159. #endif
  15160. return EXPECT_RESULT();
  15161. } /* END wc_Des3_CbcEncrypt */
  15162. /*
  15163. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15164. */
  15165. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15166. {
  15167. EXPECT_DECLS;
  15168. #ifndef NO_DES3
  15169. word32 vectorSz, cipherSz;
  15170. byte cipher[24];
  15171. byte plain[24];
  15172. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15173. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15174. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15175. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15176. };
  15177. byte key[] = {
  15178. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15179. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15180. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15181. };
  15182. byte iv[] = {
  15183. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15184. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15185. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15186. };
  15187. vectorSz = sizeof(byte) * 24;
  15188. cipherSz = sizeof(byte) * 24;
  15189. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15190. 0);
  15191. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15192. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15193. /* pass in bad args. */
  15194. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15195. BAD_FUNC_ARG);
  15196. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15197. BAD_FUNC_ARG);
  15198. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15199. BAD_FUNC_ARG);
  15200. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15201. 0);
  15202. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15203. BAD_FUNC_ARG);
  15204. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15205. BAD_FUNC_ARG);
  15206. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15207. BAD_FUNC_ARG);
  15208. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15209. 0);
  15210. #endif
  15211. return EXPECT_RESULT();
  15212. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15213. /*
  15214. * Unit test for wc_Des3_EcbEncrypt
  15215. */
  15216. static int test_wc_Des3_EcbEncrypt(void)
  15217. {
  15218. EXPECT_DECLS;
  15219. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15220. Des3 des;
  15221. byte cipher[24];
  15222. word32 cipherSz = sizeof(cipher);
  15223. const byte key[] = {
  15224. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15225. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15226. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15227. };
  15228. const byte iv[] = {
  15229. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15230. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15231. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15232. };
  15233. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15234. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15235. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15236. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15237. };
  15238. XMEMSET(&des, 0, sizeof(Des3));
  15239. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15240. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15241. /* Bad Cases */
  15242. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15243. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15244. BAD_FUNC_ARG);
  15245. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15246. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15247. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15248. /* Good Cases */
  15249. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15250. wc_Des3Free(&des);
  15251. #endif
  15252. return EXPECT_RESULT();
  15253. } /* END test_wc_Des3_EcbEncrypt */
  15254. /*
  15255. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15256. */
  15257. static int test_wc_Chacha_SetKey(void)
  15258. {
  15259. EXPECT_DECLS;
  15260. #ifdef HAVE_CHACHA
  15261. ChaCha ctx;
  15262. const byte key[] = {
  15263. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15264. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15265. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15266. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15267. };
  15268. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15269. byte cipher[128];
  15270. XMEMSET(cipher, 0, sizeof(cipher));
  15271. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15272. /* Test bad args. */
  15273. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15274. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15275. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15276. /* Test bad args. */
  15277. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15278. #endif
  15279. return EXPECT_RESULT();
  15280. } /* END test_wc_Chacha_SetKey */
  15281. /*
  15282. * unit test for wc_Poly1305SetKey()
  15283. */
  15284. static int test_wc_Poly1305SetKey(void)
  15285. {
  15286. EXPECT_DECLS;
  15287. #ifdef HAVE_POLY1305
  15288. Poly1305 ctx;
  15289. const byte key[] =
  15290. {
  15291. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15292. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15293. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15294. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15295. };
  15296. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15297. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15298. /* Test bad args. */
  15299. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15300. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15301. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15302. #endif
  15303. return EXPECT_RESULT();
  15304. } /* END test_wc_Poly1305_SetKey() */
  15305. /*
  15306. * Testing wc_Chacha_Process()
  15307. */
  15308. static int test_wc_Chacha_Process(void)
  15309. {
  15310. EXPECT_DECLS;
  15311. #ifdef HAVE_CHACHA
  15312. ChaCha enc, dec;
  15313. byte cipher[128];
  15314. byte plain[128];
  15315. const byte key[] =
  15316. {
  15317. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15319. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15320. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15321. };
  15322. const char* input = "Everybody gets Friday off.";
  15323. word32 keySz = sizeof(key)/sizeof(byte);
  15324. unsigned long int inlen = XSTRLEN(input);
  15325. /* Initialize stack variables. */
  15326. XMEMSET(cipher, 0, 128);
  15327. XMEMSET(plain, 0, 128);
  15328. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15329. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15330. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15331. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15332. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15333. 0);
  15334. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15335. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15336. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15337. /* test checking and using leftovers, currently just in C code */
  15338. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15339. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15340. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15341. (word32)inlen - 2), 0);
  15342. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15343. (byte*)input + (inlen - 2), 2), 0);
  15344. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15345. (word32)inlen - 2), 0);
  15346. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15347. (byte*)input + (inlen - 2), 2), 0);
  15348. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15349. /* check edge cases with counter increment */
  15350. {
  15351. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15352. const byte expected[] = {
  15353. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15354. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15355. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15356. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15357. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15358. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15359. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15360. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15361. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15362. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15363. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15364. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15365. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15366. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15367. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15368. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15369. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15370. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15371. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15372. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15373. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15374. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15375. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15376. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15377. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15378. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15379. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15380. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15381. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15382. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15383. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15384. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15385. };
  15386. const byte iv2[] = {
  15387. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15388. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15389. };
  15390. byte input2[256];
  15391. int i;
  15392. for (i = 0; i < 256; i++)
  15393. input2[i] = i;
  15394. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15395. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15396. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15397. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15398. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15399. /* partial */
  15400. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15401. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15402. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15403. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15404. }
  15405. #endif
  15406. /* Test bad args. */
  15407. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15408. BAD_FUNC_ARG);
  15409. #endif
  15410. return EXPECT_RESULT();
  15411. } /* END test_wc_Chacha_Process */
  15412. /*
  15413. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15414. */
  15415. static int test_wc_ChaCha20Poly1305_aead(void)
  15416. {
  15417. EXPECT_DECLS;
  15418. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15419. const byte key[] = {
  15420. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15421. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15422. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15423. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15424. };
  15425. const byte plaintext[] = {
  15426. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15427. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15428. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15429. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15430. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15431. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15432. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15433. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15434. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15435. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15436. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15437. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15438. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15439. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15440. 0x74, 0x2e
  15441. };
  15442. const byte iv[] = {
  15443. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15444. 0x44, 0x45, 0x46, 0x47
  15445. };
  15446. const byte aad[] = { /* additional data */
  15447. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15448. 0xc4, 0xc5, 0xc6, 0xc7
  15449. };
  15450. const byte cipher[] = { /* expected output from operation */
  15451. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15452. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15453. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15454. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15455. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15456. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15457. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15458. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15459. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15460. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15461. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15462. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15463. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15464. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15465. 0x61, 0x16
  15466. };
  15467. const byte authTag[] = { /* expected output from operation */
  15468. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15469. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15470. };
  15471. byte generatedCiphertext[272];
  15472. byte generatedPlaintext[272];
  15473. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15474. /* Initialize stack variables. */
  15475. XMEMSET(generatedCiphertext, 0, 272);
  15476. XMEMSET(generatedPlaintext, 0, 272);
  15477. /* Test Encrypt */
  15478. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15479. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15480. 0);
  15481. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15482. sizeof(cipher)/sizeof(byte)), 0);
  15483. /* Test bad args. */
  15484. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15485. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15486. BAD_FUNC_ARG);
  15487. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15488. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15489. BAD_FUNC_ARG);
  15490. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15491. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15492. BAD_FUNC_ARG);
  15493. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15494. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15495. BAD_FUNC_ARG);
  15496. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15497. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15498. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15499. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15500. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15501. sizeof(cipher), authTag, generatedPlaintext), 0);
  15502. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15503. sizeof(plaintext)/sizeof(byte)), 0);
  15504. /* Test bad args. */
  15505. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15506. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15507. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15508. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15509. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15510. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15511. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15512. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15513. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15514. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15515. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15516. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15517. #endif
  15518. return EXPECT_RESULT();
  15519. } /* END test_wc_ChaCha20Poly1305_aead */
  15520. /*
  15521. * Testing function for wc_Rc2SetKey().
  15522. */
  15523. static int test_wc_Rc2SetKey(void)
  15524. {
  15525. EXPECT_DECLS;
  15526. #ifdef WC_RC2
  15527. Rc2 rc2;
  15528. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15529. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15530. /* valid key and IV */
  15531. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15532. iv, 40), 0);
  15533. /* valid key, no IV */
  15534. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15535. NULL, 40), 0);
  15536. /* bad arguments */
  15537. /* null Rc2 struct */
  15538. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15539. iv, 40), BAD_FUNC_ARG);
  15540. /* null key */
  15541. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15542. iv, 40), BAD_FUNC_ARG);
  15543. /* key size == 0 */
  15544. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15545. /* key size > 128 */
  15546. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15547. /* effective bits == 0 */
  15548. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15549. iv, 0), WC_KEY_SIZE_E);
  15550. /* effective bits > 1024 */
  15551. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15552. iv, 1025), WC_KEY_SIZE_E);
  15553. #endif
  15554. return EXPECT_RESULT();
  15555. } /* END test_wc_Rc2SetKey */
  15556. /*
  15557. * Testing function for wc_Rc2SetIV().
  15558. */
  15559. static int test_wc_Rc2SetIV(void)
  15560. {
  15561. EXPECT_DECLS;
  15562. #ifdef WC_RC2
  15563. Rc2 rc2;
  15564. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15565. /* valid IV */
  15566. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15567. /* valid NULL IV */
  15568. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15569. /* bad arguments */
  15570. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15571. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15572. #endif
  15573. return EXPECT_RESULT();
  15574. } /* END test_wc_Rc2SetIV */
  15575. /*
  15576. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  15577. */
  15578. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15579. {
  15580. EXPECT_DECLS;
  15581. #ifdef WC_RC2
  15582. Rc2 rc2;
  15583. int effectiveKeyBits = 63;
  15584. byte cipher[RC2_BLOCK_SIZE];
  15585. byte plain[RC2_BLOCK_SIZE];
  15586. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15587. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15588. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15589. XMEMSET(cipher, 0, sizeof(cipher));
  15590. XMEMSET(plain, 0, sizeof(plain));
  15591. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15592. NULL, effectiveKeyBits), 0);
  15593. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  15594. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  15595. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  15596. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  15597. /* Rc2EcbEncrypt bad arguments */
  15598. /* null Rc2 struct */
  15599. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  15600. BAD_FUNC_ARG);
  15601. /* null out buffer */
  15602. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  15603. BAD_FUNC_ARG);
  15604. /* null input buffer */
  15605. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  15606. BAD_FUNC_ARG);
  15607. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15608. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  15609. /* Rc2EcbDecrypt bad arguments */
  15610. /* null Rc2 struct */
  15611. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  15612. BAD_FUNC_ARG);
  15613. /* null out buffer */
  15614. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  15615. BAD_FUNC_ARG);
  15616. /* null input buffer */
  15617. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  15618. BAD_FUNC_ARG);
  15619. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15620. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  15621. #endif
  15622. return EXPECT_RESULT();
  15623. } /* END test_wc_Rc2EcbEncryptDecrypt */
  15624. /*
  15625. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  15626. */
  15627. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15628. {
  15629. EXPECT_DECLS;
  15630. #ifdef WC_RC2
  15631. Rc2 rc2;
  15632. int effectiveKeyBits = 63;
  15633. byte cipher[RC2_BLOCK_SIZE*2];
  15634. byte plain[RC2_BLOCK_SIZE*2];
  15635. /* vector taken from test.c */
  15636. byte key[] = {
  15637. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15638. };
  15639. byte iv[] = {
  15640. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15641. };
  15642. byte input[] = {
  15643. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15644. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15645. };
  15646. byte output[] = {
  15647. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15648. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15649. };
  15650. XMEMSET(cipher, 0, sizeof(cipher));
  15651. XMEMSET(plain, 0, sizeof(plain));
  15652. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15653. iv, effectiveKeyBits), 0);
  15654. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  15655. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  15656. /* reset IV for decrypt */
  15657. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15658. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  15659. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  15660. /* Rc2CbcEncrypt bad arguments */
  15661. /* null Rc2 struct */
  15662. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  15663. BAD_FUNC_ARG);
  15664. /* null out buffer */
  15665. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  15666. BAD_FUNC_ARG);
  15667. /* null input buffer */
  15668. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  15669. BAD_FUNC_ARG);
  15670. /* Rc2CbcDecrypt bad arguments */
  15671. /* in size is 0 */
  15672. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  15673. /* null Rc2 struct */
  15674. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  15675. BAD_FUNC_ARG);
  15676. /* null out buffer */
  15677. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  15678. BAD_FUNC_ARG);
  15679. /* null input buffer */
  15680. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  15681. BAD_FUNC_ARG);
  15682. #endif
  15683. return EXPECT_RESULT();
  15684. } /* END test_wc_Rc2CbcEncryptDecrypt */
  15685. /*
  15686. * Testing function for wc_AesSetIV
  15687. */
  15688. static int test_wc_AesSetIV(void)
  15689. {
  15690. int res = TEST_SKIPPED;
  15691. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15692. Aes aes;
  15693. int ret = 0;
  15694. byte key16[] =
  15695. {
  15696. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15697. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15698. };
  15699. byte iv1[] = "1234567890abcdef";
  15700. byte iv2[] = "0987654321fedcba";
  15701. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15702. if (ret != 0)
  15703. return ret;
  15704. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15705. iv1, AES_ENCRYPTION);
  15706. if (ret == 0) {
  15707. ret = wc_AesSetIV(&aes, iv2);
  15708. }
  15709. /* Test bad args. */
  15710. if (ret == 0) {
  15711. ret = wc_AesSetIV(NULL, iv1);
  15712. if (ret == BAD_FUNC_ARG) {
  15713. /* NULL iv should return 0. */
  15714. ret = wc_AesSetIV(&aes, NULL);
  15715. }
  15716. else {
  15717. ret = WOLFSSL_FATAL_ERROR;
  15718. }
  15719. }
  15720. wc_AesFree(&aes);
  15721. res = TEST_RES_CHECK(ret == 0);
  15722. #endif
  15723. return res;
  15724. } /* test_wc_AesSetIV */
  15725. /*
  15726. * Testing function for wc_AesSetKey().
  15727. */
  15728. static int test_wc_AesSetKey(void)
  15729. {
  15730. EXPECT_DECLS;
  15731. #ifndef NO_AES
  15732. Aes aes;
  15733. byte key16[] = {
  15734. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15735. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15736. };
  15737. #ifdef WOLFSSL_AES_192
  15738. byte key24[] = {
  15739. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15740. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15741. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15742. };
  15743. #endif
  15744. #ifdef WOLFSSL_AES_256
  15745. byte key32[] = {
  15746. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15747. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15748. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15749. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15750. };
  15751. #endif
  15752. byte badKey16[] = {
  15753. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15754. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15755. };
  15756. byte iv[] = "1234567890abcdef";
  15757. XMEMSET(&aes, 0, sizeof(Aes));
  15758. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15759. #ifdef WOLFSSL_AES_128
  15760. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  15761. iv, AES_ENCRYPTION), 0);
  15762. #endif
  15763. #ifdef WOLFSSL_AES_192
  15764. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  15765. iv, AES_ENCRYPTION), 0);
  15766. #endif
  15767. #ifdef WOLFSSL_AES_256
  15768. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  15769. iv, AES_ENCRYPTION), 0);
  15770. #endif
  15771. /* Pass in bad args. */
  15772. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  15773. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  15774. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  15775. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  15776. BAD_FUNC_ARG);
  15777. wc_AesFree(&aes);
  15778. #endif
  15779. return EXPECT_RESULT();
  15780. } /* END test_wc_AesSetKey */
  15781. /*
  15782. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15783. * and wc_AesCbcDecryptWithKey()
  15784. */
  15785. static int test_wc_AesCbcEncryptDecrypt(void)
  15786. {
  15787. EXPECT_DECLS;
  15788. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15789. defined(WOLFSSL_AES_256)
  15790. Aes aes;
  15791. byte key32[] = {
  15792. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15793. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15794. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15795. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15796. };
  15797. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  15798. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  15799. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  15800. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  15801. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  15802. };
  15803. byte iv[] = "1234567890abcdef";
  15804. byte enc[sizeof(vector)];
  15805. byte dec[sizeof(vector)];
  15806. byte dec2[sizeof(vector)];
  15807. /* Init stack variables. */
  15808. XMEMSET(&aes, 0, sizeof(Aes));
  15809. XMEMSET(enc, 0, sizeof(enc));
  15810. XMEMSET(dec, 0, sizeof(vector));
  15811. XMEMSET(dec2, 0, sizeof(vector));
  15812. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15813. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15814. AES_ENCRYPTION), 0);
  15815. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  15816. /* Re init for decrypt and set flag. */
  15817. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15818. AES_DECRYPTION), 0);
  15819. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  15820. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15821. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  15822. sizeof(key32)/sizeof(byte), iv), 0);
  15823. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  15824. /* Pass in bad args */
  15825. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  15826. BAD_FUNC_ARG);
  15827. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  15828. BAD_FUNC_ARG);
  15829. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  15830. BAD_FUNC_ARG);
  15831. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15832. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  15833. BAD_LENGTH_E);
  15834. #endif
  15835. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15836. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15837. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15838. "mode (v2), skip test");
  15839. #else
  15840. /* Test passing in size of 0 */
  15841. XMEMSET(enc, 0, sizeof(enc));
  15842. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  15843. /* Check enc was not modified */
  15844. {
  15845. int i;
  15846. for (i = 0; i < (int)sizeof(enc); i++)
  15847. ExpectIntEQ(enc[i], 0);
  15848. }
  15849. #endif
  15850. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  15851. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  15852. BAD_FUNC_ARG);
  15853. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  15854. BAD_FUNC_ARG);
  15855. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15856. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15857. BAD_LENGTH_E);
  15858. #else
  15859. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15860. BAD_FUNC_ARG);
  15861. #endif
  15862. /* Test passing in size of 0 */
  15863. XMEMSET(dec, 0, sizeof(dec));
  15864. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  15865. /* Check dec was not modified */
  15866. {
  15867. int i;
  15868. for (i = 0; i < (int)sizeof(dec); i++)
  15869. ExpectIntEQ(dec[i], 0);
  15870. }
  15871. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15872. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15873. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15874. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15875. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15876. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15877. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15878. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  15879. wc_AesFree(&aes);
  15880. #endif
  15881. return EXPECT_RESULT();
  15882. } /* END test_wc_AesCbcEncryptDecrypt */
  15883. /*
  15884. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15885. */
  15886. static int test_wc_AesCtrEncryptDecrypt(void)
  15887. {
  15888. EXPECT_DECLS;
  15889. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15890. Aes aesEnc;
  15891. Aes aesDec;
  15892. byte key32[] = {
  15893. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15894. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15895. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15896. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15897. };
  15898. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15899. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15900. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15901. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15902. };
  15903. byte iv[] = "1234567890abcdef";
  15904. byte enc[AES_BLOCK_SIZE * 2];
  15905. byte dec[AES_BLOCK_SIZE * 2];
  15906. /* Init stack variables. */
  15907. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15908. XMEMSET(&aesDec, 0, sizeof(Aes));
  15909. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15910. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15911. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15912. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15913. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  15914. AES_ENCRYPTION), 0);
  15915. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15916. sizeof(vector)/sizeof(byte)), 0);
  15917. /* Decrypt with wc_AesCtrEncrypt() */
  15918. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  15919. AES_ENCRYPTION), 0);
  15920. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  15921. 0);
  15922. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15923. /* Test bad args. */
  15924. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  15925. BAD_FUNC_ARG);
  15926. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  15927. BAD_FUNC_ARG);
  15928. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  15929. BAD_FUNC_ARG);
  15930. wc_AesFree(&aesEnc);
  15931. wc_AesFree(&aesDec);
  15932. #endif
  15933. return EXPECT_RESULT();
  15934. } /* END test_wc_AesCtrEncryptDecrypt */
  15935. /*
  15936. * test function for wc_AesGcmSetKey()
  15937. */
  15938. static int test_wc_AesGcmSetKey(void)
  15939. {
  15940. EXPECT_DECLS;
  15941. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15942. Aes aes;
  15943. #ifdef WOLFSSL_AES_128
  15944. byte key16[] = {
  15945. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15946. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15947. };
  15948. #endif
  15949. #ifdef WOLFSSL_AES_192
  15950. byte key24[] = {
  15951. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15952. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15953. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15954. };
  15955. #endif
  15956. #ifdef WOLFSSL_AES_256
  15957. byte key32[] = {
  15958. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15959. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15960. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15961. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15962. };
  15963. #endif
  15964. byte badKey16[] = {
  15965. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15966. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15967. };
  15968. byte badKey24[] = {
  15969. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15970. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15971. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15972. };
  15973. byte badKey32[] = {
  15974. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15975. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15976. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15977. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15978. };
  15979. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15980. #ifdef WOLFSSL_AES_128
  15981. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  15982. #endif
  15983. #ifdef WOLFSSL_AES_192
  15984. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  15985. #endif
  15986. #ifdef WOLFSSL_AES_256
  15987. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15988. #endif
  15989. /* Pass in bad args. */
  15990. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  15991. BAD_FUNC_ARG);
  15992. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  15993. BAD_FUNC_ARG);
  15994. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  15995. BAD_FUNC_ARG);
  15996. wc_AesFree(&aes);
  15997. #endif
  15998. return EXPECT_RESULT();
  15999. } /* END test_wc_AesGcmSetKey */
  16000. /*
  16001. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16002. */
  16003. static int test_wc_AesGcmEncryptDecrypt(void)
  16004. {
  16005. EXPECT_DECLS;
  16006. /* WOLFSSL_AFALG requires 12 byte IV */
  16007. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16008. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16009. Aes aes;
  16010. byte key32[] = {
  16011. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16012. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16013. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16014. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16015. };
  16016. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16017. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16018. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16019. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16020. };
  16021. const byte a[] = {
  16022. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16023. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16024. 0xab, 0xad, 0xda, 0xd2
  16025. };
  16026. byte iv[] = "1234567890a";
  16027. byte longIV[] = "1234567890abcdefghij";
  16028. byte enc[sizeof(vector)];
  16029. byte resultT[AES_BLOCK_SIZE];
  16030. byte dec[sizeof(vector)];
  16031. /* Init stack variables. */
  16032. XMEMSET(&aes, 0, sizeof(Aes));
  16033. XMEMSET(enc, 0, sizeof(vector));
  16034. XMEMSET(dec, 0, sizeof(vector));
  16035. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16036. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16037. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16038. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16039. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16040. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16041. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16042. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16043. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16044. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16045. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16046. BAD_FUNC_ARG);
  16047. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16048. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16049. BAD_FUNC_ARG);
  16050. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16051. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16052. BAD_FUNC_ARG);
  16053. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16054. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16055. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16056. /* FIPS does not check the lower bound of ivSz */
  16057. #else
  16058. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16059. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16060. #endif
  16061. /* This case is now considered good. Long IVs are now allowed.
  16062. * Except for the original FIPS release, it still has an upper
  16063. * bound on the IV length. */
  16064. #if (!defined(HAVE_FIPS) || \
  16065. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16066. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16067. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16068. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16069. 0);
  16070. #else
  16071. (void)longIV;
  16072. #endif /* Old FIPS */
  16073. /* END wc_AesGcmEncrypt */
  16074. #ifdef HAVE_AES_DECRYPT
  16075. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16076. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16077. BAD_FUNC_ARG);
  16078. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16079. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16080. BAD_FUNC_ARG);
  16081. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16082. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16083. BAD_FUNC_ARG);
  16084. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16085. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16086. BAD_FUNC_ARG);
  16087. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16088. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16089. BAD_FUNC_ARG);
  16090. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16091. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16092. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16093. AES_GCM_AUTH_E);
  16094. #else
  16095. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16096. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16097. BAD_FUNC_ARG);
  16098. #endif
  16099. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16100. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16101. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16102. /* FIPS does not check the lower bound of ivSz */
  16103. #else
  16104. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16105. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16106. #endif
  16107. #endif /* HAVE_AES_DECRYPT */
  16108. wc_AesFree(&aes);
  16109. #endif
  16110. return EXPECT_RESULT();
  16111. } /* END test_wc_AesGcmEncryptDecrypt */
  16112. /*
  16113. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16114. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16115. */
  16116. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16117. {
  16118. EXPECT_DECLS;
  16119. #if (!defined(HAVE_FIPS) || \
  16120. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16121. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16122. const byte key[] = {
  16123. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16124. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16125. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16126. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16127. };
  16128. const byte in[] = {
  16129. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16130. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16131. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16132. };
  16133. const byte aad[] = {
  16134. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16135. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16136. 0xab, 0xad, 0xda, 0xd2
  16137. };
  16138. Aes aesEnc;
  16139. Aes aesDec;
  16140. byte iv[] = "1234567890abcdefghij";
  16141. byte out[sizeof(in)];
  16142. byte plain[sizeof(in)];
  16143. byte tag[AES_BLOCK_SIZE];
  16144. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16145. XMEMSET(&aesDec, 0, sizeof(Aes));
  16146. XMEMSET(out, 0, sizeof(out));
  16147. XMEMSET(plain, 0, sizeof(plain));
  16148. XMEMSET(tag, 0, sizeof(tag));
  16149. /* Perform one-shot encryption using long IV */
  16150. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16151. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16152. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16153. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16154. /* Perform streaming decryption using long IV */
  16155. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16156. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16157. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16158. sizeof(aad)), 0);
  16159. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16160. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16161. /* Free resources */
  16162. wc_AesFree(&aesEnc);
  16163. wc_AesFree(&aesDec);
  16164. #endif
  16165. return EXPECT_RESULT();
  16166. } /* END wc_AesGcmMixedEncDecLongIV */
  16167. /*
  16168. * unit test for wc_GmacSetKey()
  16169. */
  16170. static int test_wc_GmacSetKey(void)
  16171. {
  16172. EXPECT_DECLS;
  16173. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16174. Gmac gmac;
  16175. byte key16[] = {
  16176. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16177. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16178. };
  16179. #ifdef WOLFSSL_AES_192
  16180. byte key24[] = {
  16181. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16182. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16183. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16184. };
  16185. #endif
  16186. #ifdef WOLFSSL_AES_256
  16187. byte key32[] = {
  16188. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16189. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16190. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16191. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16192. };
  16193. #endif
  16194. byte badKey16[] = {
  16195. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16196. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16197. };
  16198. byte badKey24[] = {
  16199. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16200. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16201. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16202. };
  16203. byte badKey32[] = {
  16204. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16205. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16206. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16207. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16208. };
  16209. XMEMSET(&gmac, 0, sizeof(Gmac));
  16210. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16211. #ifdef WOLFSSL_AES_128
  16212. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16213. #endif
  16214. #ifdef WOLFSSL_AES_192
  16215. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16216. #endif
  16217. #ifdef WOLFSSL_AES_256
  16218. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16219. #endif
  16220. /* Pass in bad args. */
  16221. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16222. BAD_FUNC_ARG);
  16223. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16224. BAD_FUNC_ARG);
  16225. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16226. BAD_FUNC_ARG);
  16227. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16228. BAD_FUNC_ARG);
  16229. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16230. BAD_FUNC_ARG);
  16231. wc_AesFree(&gmac.aes);
  16232. #endif
  16233. return EXPECT_RESULT();
  16234. } /* END test_wc_GmacSetKey */
  16235. /*
  16236. * unit test for wc_GmacUpdate
  16237. */
  16238. static int test_wc_GmacUpdate(void)
  16239. {
  16240. EXPECT_DECLS;
  16241. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16242. Gmac gmac;
  16243. #ifdef WOLFSSL_AES_128
  16244. const byte key16[] = {
  16245. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16246. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16247. };
  16248. #endif
  16249. #ifdef WOLFSSL_AES_192
  16250. byte key24[] = {
  16251. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16252. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16253. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16254. };
  16255. #endif
  16256. #ifdef WOLFSSL_AES_256
  16257. byte key32[] = {
  16258. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16259. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16260. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16261. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16262. };
  16263. #endif
  16264. #ifdef WOLFSSL_AES_128
  16265. const byte authIn[] = {
  16266. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16267. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16268. };
  16269. #endif
  16270. #ifdef WOLFSSL_AES_192
  16271. const byte authIn2[] = {
  16272. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16273. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16274. };
  16275. #endif
  16276. const byte authIn3[] = {
  16277. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16278. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16279. };
  16280. #ifdef WOLFSSL_AES_128
  16281. const byte tag1[] = { /* Known. */
  16282. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16283. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16284. };
  16285. #endif
  16286. #ifdef WOLFSSL_AES_192
  16287. const byte tag2[] = { /* Known */
  16288. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16289. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16290. };
  16291. #endif
  16292. const byte tag3[] = { /* Known */
  16293. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16294. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16295. };
  16296. #ifdef WOLFSSL_AES_128
  16297. const byte iv[] = {
  16298. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16299. 0xe2, 0x8c, 0x8f, 0x16
  16300. };
  16301. #endif
  16302. #ifdef WOLFSSL_AES_192
  16303. const byte iv2[] = {
  16304. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16305. 0x7e, 0x1a, 0x6f, 0xbc
  16306. };
  16307. #endif
  16308. const byte iv3[] = {
  16309. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16310. 0xc3, 0xfb, 0x6c, 0x8a
  16311. };
  16312. byte tagOut[16];
  16313. byte tagOut2[24];
  16314. byte tagOut3[32];
  16315. /* Init stack variables. */
  16316. XMEMSET(&gmac, 0, sizeof(Gmac));
  16317. XMEMSET(tagOut, 0, sizeof(tagOut));
  16318. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16319. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16320. #ifdef WOLFSSL_AES_128
  16321. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16322. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16323. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16324. tagOut, sizeof(tag1)), 0);
  16325. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16326. wc_AesFree(&gmac.aes);
  16327. #endif
  16328. #ifdef WOLFSSL_AES_192
  16329. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16330. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16331. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16332. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16333. tagOut2, sizeof(tag2)), 0);
  16334. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16335. wc_AesFree(&gmac.aes);
  16336. #endif
  16337. #ifdef WOLFSSL_AES_256
  16338. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16339. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16340. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16341. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16342. tagOut3, sizeof(tag3)), 0);
  16343. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16344. wc_AesFree(&gmac.aes);
  16345. #endif
  16346. /* Pass bad args. */
  16347. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16348. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16349. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16350. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16351. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16352. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16353. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16354. wc_AesFree(&gmac.aes);
  16355. #endif
  16356. return EXPECT_RESULT();
  16357. } /* END test_wc_GmacUpdate */
  16358. /*
  16359. * testing wc_CamelliaSetKey
  16360. */
  16361. static int test_wc_CamelliaSetKey(void)
  16362. {
  16363. EXPECT_DECLS;
  16364. #ifdef HAVE_CAMELLIA
  16365. Camellia camellia;
  16366. /*128-bit key*/
  16367. static const byte key16[] = {
  16368. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16369. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16370. };
  16371. /* 192-bit key */
  16372. static const byte key24[] = {
  16373. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16374. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16375. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16376. };
  16377. /* 256-bit key */
  16378. static const byte key32[] = {
  16379. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16380. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16381. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16382. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16383. };
  16384. static const byte iv[] = {
  16385. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16386. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16387. };
  16388. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16389. 0);
  16390. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16391. NULL), 0);
  16392. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16393. 0);
  16394. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16395. NULL), 0);
  16396. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16397. 0);
  16398. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16399. NULL), 0);
  16400. /* Bad args. */
  16401. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16402. BAD_FUNC_ARG);
  16403. #endif
  16404. return EXPECT_RESULT();
  16405. } /* END test_wc_CammeliaSetKey */
  16406. /*
  16407. * Testing wc_CamelliaSetIV()
  16408. */
  16409. static int test_wc_CamelliaSetIV(void)
  16410. {
  16411. EXPECT_DECLS;
  16412. #ifdef HAVE_CAMELLIA
  16413. Camellia camellia;
  16414. static const byte iv[] = {
  16415. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16416. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16417. };
  16418. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16419. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16420. /* Bad args. */
  16421. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16422. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16423. #endif
  16424. return EXPECT_RESULT();
  16425. } /* END test_wc_CamelliaSetIV*/
  16426. /*
  16427. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16428. */
  16429. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16430. {
  16431. EXPECT_DECLS;
  16432. #ifdef HAVE_CAMELLIA
  16433. Camellia camellia;
  16434. static const byte key24[] = {
  16435. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16436. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16437. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16438. };
  16439. static const byte iv[] = {
  16440. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16441. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16442. };
  16443. static const byte plainT[] = {
  16444. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16445. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16446. };
  16447. byte enc[sizeof(plainT)];
  16448. byte dec[sizeof(enc)];
  16449. /* Init stack variables.*/
  16450. XMEMSET(enc, 0, 16);
  16451. XMEMSET(enc, 0, 16);
  16452. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16453. 0);
  16454. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16455. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16456. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16457. /* Pass bad args. */
  16458. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16459. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16460. BAD_FUNC_ARG);
  16461. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16462. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16463. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16464. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16465. #endif
  16466. return EXPECT_RESULT();
  16467. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16468. /*
  16469. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16470. */
  16471. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16472. {
  16473. EXPECT_DECLS;
  16474. #ifdef HAVE_CAMELLIA
  16475. Camellia camellia;
  16476. static const byte key24[] = {
  16477. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16478. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16479. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16480. };
  16481. static const byte plainT[] = {
  16482. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16483. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16484. };
  16485. byte enc[CAMELLIA_BLOCK_SIZE];
  16486. byte dec[CAMELLIA_BLOCK_SIZE];
  16487. /* Init stack variables. */
  16488. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16489. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16490. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16491. NULL), 0);
  16492. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16493. CAMELLIA_BLOCK_SIZE), 0);
  16494. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16495. NULL), 0);
  16496. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16497. 0);
  16498. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16499. /* Pass in bad args. */
  16500. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16501. BAD_FUNC_ARG);
  16502. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16503. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16504. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16505. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16506. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16507. BAD_FUNC_ARG);
  16508. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16509. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16510. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16511. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16512. #endif
  16513. return EXPECT_RESULT();
  16514. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16515. /*
  16516. * Testing wc_Arc4SetKey()
  16517. */
  16518. static int test_wc_Arc4SetKey(void)
  16519. {
  16520. EXPECT_DECLS;
  16521. #ifndef NO_RC4
  16522. Arc4 arc;
  16523. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16524. int keyLen = 8;
  16525. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  16526. /* Test bad args. */
  16527. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  16528. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  16529. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16530. #endif
  16531. return EXPECT_RESULT();
  16532. } /* END test_wc_Arc4SetKey */
  16533. /*
  16534. * Testing wc_Arc4Process for ENC/DEC.
  16535. */
  16536. static int test_wc_Arc4Process(void)
  16537. {
  16538. EXPECT_DECLS;
  16539. #ifndef NO_RC4
  16540. Arc4 enc;
  16541. Arc4 dec;
  16542. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16543. int keyLen = 8;
  16544. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16545. byte cipher[8];
  16546. byte plain[8];
  16547. /* Init stack variables */
  16548. XMEMSET(&enc, 0, sizeof(Arc4));
  16549. XMEMSET(&dec, 0, sizeof(Arc4));
  16550. XMEMSET(cipher, 0, sizeof(cipher));
  16551. XMEMSET(plain, 0, sizeof(plain));
  16552. /* Use for async. */
  16553. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16554. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16555. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  16556. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  16557. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  16558. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  16559. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16560. /* Bad args. */
  16561. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  16562. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  16563. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  16564. wc_Arc4Free(&enc);
  16565. wc_Arc4Free(&dec);
  16566. #endif
  16567. return EXPECT_RESULT();
  16568. } /* END test_wc_Arc4Process */
  16569. /*
  16570. * Testing wc_Init RsaKey()
  16571. */
  16572. static int test_wc_InitRsaKey(void)
  16573. {
  16574. EXPECT_DECLS;
  16575. #ifndef NO_RSA
  16576. RsaKey key;
  16577. XMEMSET(&key, 0, sizeof(RsaKey));
  16578. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16579. /* Test bad args. */
  16580. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  16581. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16582. #endif
  16583. return EXPECT_RESULT();
  16584. } /* END test_wc_InitRsaKey */
  16585. /*
  16586. * Testing wc_RsaPrivateKeyDecode()
  16587. */
  16588. static int test_wc_RsaPrivateKeyDecode(void)
  16589. {
  16590. EXPECT_DECLS;
  16591. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16592. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16593. RsaKey key;
  16594. byte* tmp = NULL;
  16595. word32 idx = 0;
  16596. int bytes = 0;
  16597. XMEMSET(&key, 0, sizeof(RsaKey));
  16598. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  16599. DYNAMIC_TYPE_TMP_BUFFER));
  16600. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16601. if (tmp != NULL) {
  16602. #ifdef USE_CERT_BUFFERS_1024
  16603. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16604. bytes = sizeof_client_key_der_1024;
  16605. #else
  16606. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16607. bytes = sizeof_client_key_der_2048;
  16608. #endif /* Use cert buffers. */
  16609. }
  16610. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  16611. /* Test bad args. */
  16612. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16613. BAD_FUNC_ARG);
  16614. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16615. BAD_FUNC_ARG);
  16616. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16617. BAD_FUNC_ARG);
  16618. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16619. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16620. #endif
  16621. return EXPECT_RESULT();
  16622. } /* END test_wc_RsaPrivateKeyDecode */
  16623. /*
  16624. * Testing wc_RsaPublicKeyDecode()
  16625. */
  16626. static int test_wc_RsaPublicKeyDecode(void)
  16627. {
  16628. EXPECT_DECLS;
  16629. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16630. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16631. RsaKey keyPub;
  16632. byte* tmp = NULL;
  16633. word32 idx = 0;
  16634. int bytes = 0;
  16635. word32 keySz = 0;
  16636. word32 tstKeySz = 0;
  16637. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16638. XFILE f = XBADFILE;
  16639. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16640. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16641. byte buf[4096];
  16642. #endif
  16643. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  16644. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16645. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  16646. if (tmp != NULL) {
  16647. #ifdef USE_CERT_BUFFERS_1024
  16648. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16649. bytes = sizeof_client_keypub_der_1024;
  16650. keySz = 1024;
  16651. #else
  16652. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16653. bytes = sizeof_client_keypub_der_2048;
  16654. keySz = 2048;
  16655. #endif
  16656. }
  16657. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  16658. /* Pass in bad args. */
  16659. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16660. BAD_FUNC_ARG);
  16661. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16662. BAD_FUNC_ARG);
  16663. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16664. BAD_FUNC_ARG);
  16665. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  16666. /* Test for getting modulus key size */
  16667. idx = 0;
  16668. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16669. &tstKeySz, NULL, NULL), 0);
  16670. ExpectIntEQ(tstKeySz, keySz/8);
  16671. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16672. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  16673. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16674. if (f != XBADFILE) {
  16675. XFCLOSE(f);
  16676. f = XBADFILE;
  16677. }
  16678. idx = 0;
  16679. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16680. NULL), 0);
  16681. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  16682. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16683. if (f != XBADFILE)
  16684. XFCLOSE(f);
  16685. idx = 0;
  16686. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16687. NULL), 0);
  16688. #endif
  16689. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16690. #endif
  16691. return EXPECT_RESULT();
  16692. } /* END test_wc_RsaPublicKeyDecode */
  16693. /*
  16694. * Testing wc_RsaPublicKeyDecodeRaw()
  16695. */
  16696. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16697. {
  16698. EXPECT_DECLS;
  16699. #if !defined(NO_RSA)
  16700. RsaKey key;
  16701. const byte n = 0x23;
  16702. const byte e = 0x03;
  16703. int nSz = sizeof(n);
  16704. int eSz = sizeof(e);
  16705. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16706. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  16707. /* Pass in bad args. */
  16708. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16709. BAD_FUNC_ARG);
  16710. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16711. BAD_FUNC_ARG);
  16712. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16713. BAD_FUNC_ARG);
  16714. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16715. #endif
  16716. return EXPECT_RESULT();
  16717. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16718. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16719. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16720. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16721. * trying until it gets a probable prime. */
  16722. #ifdef HAVE_FIPS
  16723. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16724. {
  16725. int ret;
  16726. for (;;) {
  16727. ret = wc_MakeRsaKey(key, size, e, rng);
  16728. if (ret != PRIME_GEN_E) break;
  16729. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16730. "trying again.\n");
  16731. }
  16732. return ret;
  16733. }
  16734. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16735. #else
  16736. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16737. #endif
  16738. #endif
  16739. /*
  16740. * Testing wc_MakeRsaKey()
  16741. */
  16742. static int test_wc_MakeRsaKey(void)
  16743. {
  16744. EXPECT_DECLS;
  16745. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16746. RsaKey genKey;
  16747. WC_RNG rng;
  16748. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16749. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16750. int bits = 1024;
  16751. #else
  16752. int bits = 2048;
  16753. #endif
  16754. XMEMSET(&genKey, 0, sizeof(RsaKey));
  16755. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16756. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16757. ExpectIntEQ(wc_InitRng(&rng), 0);
  16758. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16759. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16760. /* Test bad args. */
  16761. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  16762. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16763. BAD_FUNC_ARG);
  16764. /* e < 3 */
  16765. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  16766. /* e & 1 == 0 */
  16767. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  16768. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16769. #endif
  16770. return EXPECT_RESULT();
  16771. } /* END test_wc_MakeRsaKey */
  16772. /*
  16773. * Test the bounds checking on the cipher text versus the key modulus.
  16774. * 1. Make a new RSA key.
  16775. * 2. Set c to 1.
  16776. * 3. Decrypt c into k. (error)
  16777. * 4. Copy the key modulus to c and sub 1 from the copy.
  16778. * 5. Decrypt c into k. (error)
  16779. * Valid bounds test cases are covered by all the other RSA tests.
  16780. */
  16781. static int test_RsaDecryptBoundsCheck(void)
  16782. {
  16783. EXPECT_DECLS;
  16784. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16785. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16786. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16787. WC_RNG rng;
  16788. RsaKey key;
  16789. byte flatC[256];
  16790. word32 flatCSz;
  16791. byte out[256];
  16792. word32 outSz = sizeof(out);
  16793. XMEMSET(&key, 0, sizeof(RsaKey));
  16794. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16795. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16796. ExpectIntEQ(wc_InitRng(&rng), 0);
  16797. if (EXPECT_SUCCESS()) {
  16798. const byte* derKey;
  16799. word32 derKeySz;
  16800. word32 idx = 0;
  16801. #ifdef USE_CERT_BUFFERS_1024
  16802. derKey = server_key_der_1024;
  16803. derKeySz = (word32)sizeof_server_key_der_1024;
  16804. flatCSz = 128;
  16805. #else
  16806. derKey = server_key_der_2048;
  16807. derKeySz = (word32)sizeof_server_key_der_2048;
  16808. flatCSz = 256;
  16809. #endif
  16810. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  16811. }
  16812. if (EXPECT_SUCCESS()) {
  16813. XMEMSET(flatC, 0, flatCSz);
  16814. flatC[flatCSz-1] = 1;
  16815. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16816. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  16817. if (EXPECT_SUCCESS()) {
  16818. mp_int c;
  16819. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  16820. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  16821. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  16822. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16823. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  16824. mp_clear(&c);
  16825. }
  16826. }
  16827. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16828. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16829. #endif
  16830. return EXPECT_RESULT();
  16831. } /* END test_wc_RsaDecryptBoundsCheck */
  16832. /*
  16833. * Testing wc_SetKeyUsage()
  16834. */
  16835. static int test_wc_SetKeyUsage(void)
  16836. {
  16837. EXPECT_DECLS;
  16838. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16839. Cert myCert;
  16840. ExpectIntEQ(wc_InitCert(&myCert), 0);
  16841. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  16842. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  16843. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  16844. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  16845. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  16846. /* Test bad args. */
  16847. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  16848. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  16849. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  16850. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  16851. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  16852. KEYUSAGE_E);
  16853. #endif
  16854. return EXPECT_RESULT();
  16855. } /* END test_wc_SetKeyUsage */
  16856. /*
  16857. * Testing wc_CheckProbablePrime()
  16858. */
  16859. static int test_wc_CheckProbablePrime(void)
  16860. {
  16861. EXPECT_DECLS;
  16862. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16863. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16864. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16865. RsaKey key;
  16866. WC_RNG rng;
  16867. byte e[3];
  16868. word32 eSz = (word32)sizeof(e);
  16869. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16870. word32 nSz = (word32)sizeof(n);
  16871. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16872. word32 dSz = (word32)sizeof(d);
  16873. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16874. word32 pSz = (word32)sizeof(p);
  16875. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16876. word32 qSz = (word32)sizeof(q);
  16877. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16878. int* isPrime;
  16879. int test[5];
  16880. isPrime = test;
  16881. XMEMSET(&key, 0, sizeof(RsaKey));
  16882. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16883. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16884. ExpectIntEQ(wc_InitRng(&rng), 0);
  16885. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16886. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  16887. WC_RSA_EXPONENT, &rng), 0);
  16888. PRIVATE_KEY_UNLOCK();
  16889. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  16890. &qSz), 0);
  16891. PRIVATE_KEY_LOCK();
  16892. /* Bad cases */
  16893. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  16894. BAD_FUNC_ARG);
  16895. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  16896. BAD_FUNC_ARG);
  16897. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  16898. BAD_FUNC_ARG);
  16899. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  16900. BAD_FUNC_ARG);
  16901. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  16902. BAD_FUNC_ARG);
  16903. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  16904. BAD_FUNC_ARG);
  16905. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  16906. BAD_FUNC_ARG);
  16907. /* Good case */
  16908. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  16909. 0);
  16910. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16911. wc_FreeRng(&rng);
  16912. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16913. #endif
  16914. return EXPECT_RESULT();
  16915. } /* END test_wc_CheckProbablePrime */
  16916. /*
  16917. * Testing wc_RsaPSS_Verify()
  16918. */
  16919. static int test_wc_RsaPSS_Verify(void)
  16920. {
  16921. EXPECT_DECLS;
  16922. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16923. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16924. RsaKey key;
  16925. WC_RNG rng;
  16926. int sz = 256;
  16927. const char* szMessage = "This is the string to be signed";
  16928. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16929. unsigned char pDecrypted[2048/8];
  16930. byte* pt = pDecrypted;
  16931. word32 outLen = sizeof(pDecrypted);
  16932. XMEMSET(&key, 0, sizeof(RsaKey));
  16933. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16934. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16935. ExpectIntEQ(wc_InitRng(&rng), 0);
  16936. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16937. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16938. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  16939. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  16940. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16941. /* Bad cases */
  16942. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16943. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16944. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16945. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16946. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16947. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16948. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16949. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16950. /* Good case */
  16951. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16952. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16953. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16954. wc_FreeRng(&rng);
  16955. #endif
  16956. return EXPECT_RESULT();
  16957. } /* END test_wc_RsaPSS_Verify */
  16958. /*
  16959. * Testing wc_RsaPSS_VerifyCheck()
  16960. */
  16961. static int test_wc_RsaPSS_VerifyCheck(void)
  16962. {
  16963. EXPECT_DECLS;
  16964. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16965. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16966. RsaKey key;
  16967. WC_RNG rng;
  16968. int sz = 256; /* 2048/8 */
  16969. byte digest[32];
  16970. word32 digestSz = sizeof(digest);
  16971. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16972. word32 pSignatureSz = sizeof(pSignature);
  16973. unsigned char pDecrypted[2048/8];
  16974. byte* pt = pDecrypted;
  16975. word32 outLen = sizeof(pDecrypted);
  16976. XMEMSET(&key, 0, sizeof(RsaKey));
  16977. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16978. XMEMSET(digest, 0, sizeof(digest));
  16979. XMEMSET(pSignature, 0, sizeof(pSignature));
  16980. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16981. ExpectIntEQ(wc_InitRng(&rng), 0);
  16982. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16983. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16984. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16985. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16986. 0);
  16987. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16988. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16989. /* Bad cases */
  16990. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  16991. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16992. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  16993. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16994. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  16995. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16996. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  16997. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16998. /* Good case */
  16999. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  17000. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17001. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17002. wc_FreeRng(&rng);
  17003. #endif
  17004. return EXPECT_RESULT();
  17005. } /* END test_wc_RsaPSS_VerifyCheck */
  17006. /*
  17007. * Testing wc_RsaPSS_VerifyCheckInline()
  17008. */
  17009. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17010. {
  17011. EXPECT_DECLS;
  17012. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17013. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17014. RsaKey key;
  17015. WC_RNG rng;
  17016. int sz = 256;
  17017. byte digest[32];
  17018. word32 digestSz = sizeof(digest);
  17019. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17020. unsigned char pDecrypted[2048/8];
  17021. byte* pt = pDecrypted;
  17022. XMEMSET(&key, 0, sizeof(RsaKey));
  17023. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17024. XMEMSET(digest, 0, sizeof(digest));
  17025. XMEMSET(pSignature, 0, sizeof(pSignature));
  17026. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17027. ExpectIntEQ(wc_InitRng(&rng), 0);
  17028. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17029. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17030. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17031. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17032. 0);
  17033. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17034. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17035. /* Bad Cases */
  17036. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  17037. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17038. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17039. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17040. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17041. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17042. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17043. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17044. /* Good case */
  17045. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17046. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17047. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17048. wc_FreeRng(&rng);
  17049. #endif
  17050. return EXPECT_RESULT();
  17051. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17052. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17053. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17054. {
  17055. (void)flag;
  17056. (void)type;
  17057. (void)file;
  17058. (void)line;
  17059. }
  17060. #endif
  17061. /*
  17062. * Testing wc_LockMutex_ex
  17063. */
  17064. static int test_wc_LockMutex_ex(void)
  17065. {
  17066. EXPECT_DECLS;
  17067. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17068. int flag = CRYPTO_LOCK;
  17069. int type = 0;
  17070. const char* file = "./test-LockMutex_ex.txt";
  17071. int line = 0;
  17072. /* without SetMutexCb */
  17073. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17074. /* with SetMutexCb */
  17075. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17076. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17077. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17078. #endif
  17079. return EXPECT_RESULT();
  17080. } /* End test_wc_LockMutex_ex*/
  17081. /*
  17082. * Testing wc_SetMutexCb
  17083. */
  17084. static int test_wc_SetMutexCb(void)
  17085. {
  17086. EXPECT_DECLS;
  17087. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17088. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17089. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17090. #endif
  17091. return EXPECT_RESULT();
  17092. } /* End test_wc_SetMutexCb*/
  17093. /*
  17094. * Testing wc_RsaKeyToDer()
  17095. */
  17096. static int test_wc_RsaKeyToDer(void)
  17097. {
  17098. EXPECT_DECLS;
  17099. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17100. RsaKey genKey;
  17101. WC_RNG rng;
  17102. byte* der = NULL;
  17103. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17104. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17105. int bits = 1024;
  17106. word32 derSz = 611;
  17107. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17108. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17109. #else
  17110. int bits = 2048;
  17111. word32 derSz = 1196;
  17112. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17113. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17114. #endif
  17115. XMEMSET(&rng, 0, sizeof(rng));
  17116. XMEMSET(&genKey, 0, sizeof(genKey));
  17117. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17118. /* Init structures. */
  17119. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17120. ExpectIntEQ(wc_InitRng(&rng), 0);
  17121. /* Make key. */
  17122. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17123. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17124. /* Pass good/bad args. */
  17125. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17126. /* Get just the output length */
  17127. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17128. /* Try Public Key. */
  17129. genKey.type = 0;
  17130. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17131. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17132. /* Put back to Private Key */
  17133. genKey.type = 1;
  17134. #endif
  17135. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17136. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17137. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17138. #endif
  17139. return EXPECT_RESULT();
  17140. } /* END test_wc_RsaKeyToDer */
  17141. /*
  17142. * Testing wc_RsaKeyToPublicDer()
  17143. */
  17144. static int test_wc_RsaKeyToPublicDer(void)
  17145. {
  17146. EXPECT_DECLS;
  17147. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17148. RsaKey key;
  17149. WC_RNG rng;
  17150. byte* der = NULL;
  17151. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17152. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17153. int bits = 1024;
  17154. word32 derLen = 162;
  17155. #else
  17156. int bits = 2048;
  17157. word32 derLen = 294;
  17158. #endif
  17159. int ret;
  17160. XMEMSET(&rng, 0, sizeof(rng));
  17161. XMEMSET(&key, 0, sizeof(key));
  17162. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17163. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17164. ExpectIntEQ(wc_InitRng(&rng), 0);
  17165. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17166. /* test getting size only */
  17167. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17168. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17169. /* test getting size only */
  17170. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17171. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17172. /* Pass in bad args. */
  17173. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17174. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17175. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17176. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17177. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17178. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17179. #endif
  17180. return EXPECT_RESULT();
  17181. } /* END test_wc_RsaKeyToPublicDer */
  17182. /*
  17183. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17184. */
  17185. static int test_wc_RsaPublicEncryptDecrypt(void)
  17186. {
  17187. EXPECT_DECLS;
  17188. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17189. RsaKey key;
  17190. WC_RNG rng;
  17191. const char inStr[] = TEST_STRING;
  17192. const word32 plainLen = (word32)TEST_STRING_SZ;
  17193. const word32 inLen = (word32)TEST_STRING_SZ;
  17194. int bits = TEST_RSA_BITS;
  17195. const word32 cipherLen = TEST_RSA_BYTES;
  17196. word32 cipherLenResult = cipherLen;
  17197. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17198. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17199. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17200. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17201. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17202. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17203. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17204. ExpectNotNull(in);
  17205. ExpectNotNull(plain);
  17206. ExpectNotNull(cipher);
  17207. #endif
  17208. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17209. /* Initialize stack structures. */
  17210. XMEMSET(&key, 0, sizeof(RsaKey));
  17211. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17212. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17213. ExpectIntEQ(wc_InitRng(&rng), 0);
  17214. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17215. /* Encrypt. */
  17216. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  17217. cipherLen, &key, &rng), 0);
  17218. /* Pass bad args - tested in another testing function.*/
  17219. /* Decrypt */
  17220. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17221. /* Bind rng */
  17222. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17223. #endif
  17224. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17225. &key), 0);
  17226. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17227. /* Pass bad args - tested in another testing function.*/
  17228. WC_FREE_VAR(in, NULL);
  17229. WC_FREE_VAR(plain, NULL);
  17230. WC_FREE_VAR(cipher, NULL);
  17231. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17232. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17233. #endif
  17234. return EXPECT_RESULT();
  17235. } /* END test_wc_RsaPublicEncryptDecrypt */
  17236. /*
  17237. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17238. */
  17239. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17240. {
  17241. EXPECT_DECLS;
  17242. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17243. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17244. RsaKey key;
  17245. WC_RNG rng;
  17246. const char inStr[] = TEST_STRING;
  17247. const word32 inLen = (word32)TEST_STRING_SZ;
  17248. const word32 plainSz = (word32)TEST_STRING_SZ;
  17249. byte* res = NULL;
  17250. int idx = 0;
  17251. int bits = TEST_RSA_BITS;
  17252. const word32 cipherSz = TEST_RSA_BYTES;
  17253. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17254. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17255. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17256. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17257. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17258. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17259. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17260. ExpectNotNull(in);
  17261. ExpectNotNull(plain);
  17262. ExpectNotNull(cipher);
  17263. #endif
  17264. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17265. /* Initialize stack structures. */
  17266. XMEMSET(&key, 0, sizeof(RsaKey));
  17267. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17268. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17269. ExpectIntEQ(wc_InitRng(&rng), 0);
  17270. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17271. /* Encrypt */
  17272. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17273. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17274. /* Pass bad args - tested in another testing function.*/
  17275. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17276. /* Decrypt */
  17277. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17278. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17279. #endif
  17280. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17281. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17282. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17283. /* Pass bad args - tested in another testing function.*/
  17284. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17285. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17286. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17287. #endif
  17288. WC_FREE_VAR(in, NULL);
  17289. WC_FREE_VAR(plain, NULL);
  17290. WC_FREE_VAR(cipher, NULL);
  17291. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17292. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17293. #endif
  17294. return EXPECT_RESULT();
  17295. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17296. /*
  17297. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17298. */
  17299. static int test_wc_RsaSSL_SignVerify(void)
  17300. {
  17301. EXPECT_DECLS;
  17302. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17303. RsaKey key;
  17304. WC_RNG rng;
  17305. const char inStr[] = TEST_STRING;
  17306. const word32 plainSz = (word32)TEST_STRING_SZ;
  17307. const word32 inLen = (word32)TEST_STRING_SZ;
  17308. word32 idx = 0;
  17309. int bits = TEST_RSA_BITS;
  17310. const word32 outSz = TEST_RSA_BYTES;
  17311. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17312. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17313. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17314. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17315. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17316. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17317. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17318. ExpectNotNull(in);
  17319. ExpectNotNull(out);
  17320. ExpectNotNull(plain);
  17321. #endif
  17322. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17323. XMEMSET(&key, 0, sizeof(RsaKey));
  17324. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17325. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17326. ExpectIntEQ(wc_InitRng(&rng), 0);
  17327. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17328. /* Sign. */
  17329. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17330. idx = (int)outSz;
  17331. /* Test bad args. */
  17332. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17333. BAD_FUNC_ARG);
  17334. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17335. BAD_FUNC_ARG);
  17336. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17337. BAD_FUNC_ARG);
  17338. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17339. BAD_FUNC_ARG);
  17340. /* Verify. */
  17341. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17342. /* Pass bad args. */
  17343. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17344. BAD_FUNC_ARG);
  17345. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17346. BAD_FUNC_ARG);
  17347. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17348. BAD_FUNC_ARG);
  17349. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17350. BAD_FUNC_ARG);
  17351. WC_FREE_VAR(in, NULL);
  17352. WC_FREE_VAR(out, NULL);
  17353. WC_FREE_VAR(plain, NULL);
  17354. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17355. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17356. #endif
  17357. return EXPECT_RESULT();
  17358. } /* END test_wc_RsaSSL_SignVerify */
  17359. /*
  17360. * Testing wc_RsaEncryptSize()
  17361. */
  17362. static int test_wc_RsaEncryptSize(void)
  17363. {
  17364. EXPECT_DECLS;
  17365. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17366. RsaKey key;
  17367. WC_RNG rng;
  17368. XMEMSET(&key, 0, sizeof(RsaKey));
  17369. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17370. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17371. ExpectIntEQ(wc_InitRng(&rng), 0);
  17372. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17373. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17374. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17375. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17376. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17377. #endif
  17378. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17379. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17380. /* Pass in bad arg. */
  17381. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17382. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17383. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17384. #endif
  17385. return EXPECT_RESULT();
  17386. } /* END test_wc_RsaEncryptSize*/
  17387. /*
  17388. * Testing wc_RsaFlattenPublicKey()
  17389. */
  17390. static int test_wc_RsaFlattenPublicKey(void)
  17391. {
  17392. EXPECT_DECLS;
  17393. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17394. RsaKey key;
  17395. WC_RNG rng;
  17396. byte e[256];
  17397. byte n[256];
  17398. word32 eSz = sizeof(e);
  17399. word32 nSz = sizeof(n);
  17400. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17401. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17402. int bits = 1024;
  17403. #else
  17404. int bits = 2048;
  17405. #endif
  17406. XMEMSET(&key, 0, sizeof(RsaKey));
  17407. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17408. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17409. ExpectIntEQ(wc_InitRng(&rng), 0);
  17410. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17411. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17412. /* Pass bad args. */
  17413. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17414. BAD_FUNC_ARG);
  17415. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17416. BAD_FUNC_ARG);
  17417. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17418. BAD_FUNC_ARG);
  17419. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17420. BAD_FUNC_ARG);
  17421. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17422. BAD_FUNC_ARG);
  17423. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17424. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17425. #endif
  17426. return EXPECT_RESULT();
  17427. } /* END test_wc_RsaFlattenPublicKey */
  17428. /*
  17429. * unit test for wc_AesCcmSetKey
  17430. */
  17431. static int test_wc_AesCcmSetKey(void)
  17432. {
  17433. EXPECT_DECLS;
  17434. #ifdef HAVE_AESCCM
  17435. Aes aes;
  17436. const byte key16[] = {
  17437. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17438. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17439. };
  17440. const byte key24[] = {
  17441. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17442. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17443. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17444. };
  17445. const byte key32[] = {
  17446. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17447. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17448. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17449. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17450. };
  17451. XMEMSET(&aes, 0, sizeof(Aes));
  17452. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17453. #ifdef WOLFSSL_AES_128
  17454. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17455. #endif
  17456. #ifdef WOLFSSL_AES_192
  17457. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17458. #endif
  17459. #ifdef WOLFSSL_AES_256
  17460. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17461. #endif
  17462. /* Test bad args. */
  17463. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17464. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17465. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17466. wc_AesFree(&aes);
  17467. #endif
  17468. return EXPECT_RESULT();
  17469. } /* END test_wc_AesCcmSetKey */
  17470. /*
  17471. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17472. */
  17473. static int test_wc_AesCcmEncryptDecrypt(void)
  17474. {
  17475. EXPECT_DECLS;
  17476. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17477. Aes aes;
  17478. const byte key16[] = {
  17479. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17480. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17481. };
  17482. /* plaintext */
  17483. const byte plainT[] = {
  17484. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17485. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17486. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17487. };
  17488. /* nonce */
  17489. const byte iv[] = {
  17490. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17491. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17492. };
  17493. const byte c[] = { /* cipher text. */
  17494. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17495. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17496. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17497. };
  17498. const byte t[] = { /* Auth tag */
  17499. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17500. };
  17501. const byte authIn[] = {
  17502. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17503. };
  17504. byte cipherOut[sizeof(plainT)];
  17505. byte authTag[sizeof(t)];
  17506. #ifdef HAVE_AES_DECRYPT
  17507. byte plainOut[sizeof(cipherOut)];
  17508. #endif
  17509. XMEMSET(&aes, 0, sizeof(Aes));
  17510. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17511. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17512. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17513. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17514. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17515. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17516. #ifdef HAVE_AES_DECRYPT
  17517. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17518. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17519. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17520. #endif
  17521. /* Pass in bad args. Encrypt*/
  17522. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17523. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17524. BAD_FUNC_ARG);
  17525. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17526. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17527. BAD_FUNC_ARG);
  17528. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17529. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17530. BAD_FUNC_ARG);
  17531. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17532. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17533. BAD_FUNC_ARG);
  17534. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17535. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17536. BAD_FUNC_ARG);
  17537. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17538. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17539. BAD_FUNC_ARG);
  17540. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17541. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17542. BAD_FUNC_ARG);
  17543. #ifdef HAVE_AES_DECRYPT
  17544. /* Pass in bad args. Decrypt*/
  17545. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17546. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17547. BAD_FUNC_ARG);
  17548. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17549. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17550. BAD_FUNC_ARG);
  17551. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17552. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17553. BAD_FUNC_ARG);
  17554. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17555. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17556. BAD_FUNC_ARG);
  17557. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17558. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17559. BAD_FUNC_ARG);
  17560. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17561. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17562. BAD_FUNC_ARG);
  17563. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17564. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17565. BAD_FUNC_ARG);
  17566. #endif
  17567. wc_AesFree(&aes);
  17568. #endif /* HAVE_AESCCM */
  17569. return EXPECT_RESULT();
  17570. } /* END test_wc_AesCcmEncryptDecrypt */
  17571. #if defined(WOLFSSL_AES_EAX) && \
  17572. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17573. /*
  17574. * Testing test_wc_AesEaxVectors()
  17575. */
  17576. static int test_wc_AesEaxVectors(void)
  17577. {
  17578. EXPECT_DECLS;
  17579. typedef struct {
  17580. byte key[AES_256_KEY_SIZE];
  17581. int key_length;
  17582. byte iv[AES_BLOCK_SIZE];
  17583. int iv_length;
  17584. byte aad[AES_BLOCK_SIZE * 2];
  17585. int aad_length;
  17586. byte msg[AES_BLOCK_SIZE * 5];
  17587. int msg_length;
  17588. byte ct[AES_BLOCK_SIZE * 5];
  17589. int ct_length;
  17590. byte tag[AES_BLOCK_SIZE];
  17591. int tag_length;
  17592. int valid;
  17593. } AadVector;
  17594. /* Test vectors obtained from Google wycheproof project
  17595. * https://github.com/google/wycheproof
  17596. * from testvectors/aes_eax_test.json
  17597. */
  17598. const AadVector vectors[] = {
  17599. {
  17600. /* key, key length */
  17601. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  17602. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  17603. /* iv, iv length */
  17604. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  17605. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  17606. /* aad, aad length */
  17607. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  17608. /* msg, msg length */
  17609. {0x00}, 0,
  17610. /* ct, ct length */
  17611. {0x00}, 0,
  17612. /* tag, tag length */
  17613. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  17614. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  17615. /* valid */
  17616. 1,
  17617. },
  17618. {
  17619. /* key, key length */
  17620. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  17621. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  17622. /* iv, iv length */
  17623. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  17624. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  17625. /* aad, aad length */
  17626. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  17627. /* msg, msg length */
  17628. {0xf7, 0xfb}, 2,
  17629. /* ct, ct length */
  17630. {0x19, 0xdd}, 2,
  17631. /* tag, tag length */
  17632. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  17633. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  17634. /* valid */
  17635. 1,
  17636. },
  17637. {
  17638. /* key, key length */
  17639. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  17640. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  17641. /* iv, iv length */
  17642. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  17643. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  17644. /* aad, aad length */
  17645. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  17646. /* msg, msg length */
  17647. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  17648. /* ct, ct length */
  17649. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  17650. /* tag, tag length */
  17651. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  17652. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  17653. /* valid */
  17654. 1,
  17655. },
  17656. {
  17657. /* key, key length */
  17658. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  17659. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  17660. /* iv, iv length */
  17661. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  17662. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  17663. /* aad, aad length */
  17664. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  17665. /* msg, msg length */
  17666. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  17667. /* ct, ct length */
  17668. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  17669. /* tag, tag length */
  17670. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  17671. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  17672. /* valid */
  17673. 1,
  17674. },
  17675. {
  17676. /* key, key length */
  17677. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  17678. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  17679. /* iv, iv length */
  17680. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  17681. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  17682. /* aad, aad length */
  17683. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  17684. /* msg, msg length */
  17685. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  17686. /* ct, ct length */
  17687. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  17688. /* tag, tag length */
  17689. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  17690. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  17691. /* valid */
  17692. 1,
  17693. },
  17694. {
  17695. /* key, key length */
  17696. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  17697. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  17698. /* iv, iv length */
  17699. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  17700. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  17701. /* aad, aad length */
  17702. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  17703. /* msg, msg length */
  17704. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  17705. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  17706. /* ct, ct length */
  17707. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  17708. 0x0e, 0x72, 0x84, 0x14}, 12,
  17709. /* tag, tag length */
  17710. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  17711. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  17712. /* valid */
  17713. 1,
  17714. },
  17715. {
  17716. /* key, key length */
  17717. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  17718. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  17719. /* iv, iv length */
  17720. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  17721. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  17722. /* aad, aad length */
  17723. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  17724. /* msg, msg length */
  17725. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  17726. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  17727. 0x36}, 17,
  17728. /* ct, ct length */
  17729. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  17730. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  17731. 0x30}, 17,
  17732. /* tag, tag length */
  17733. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  17734. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  17735. /* valid */
  17736. 1,
  17737. },
  17738. {
  17739. /* key, key length */
  17740. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  17741. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  17742. /* iv, iv length */
  17743. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  17744. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  17745. /* aad, aad length */
  17746. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  17747. /* msg, msg length */
  17748. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  17749. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  17750. 0x2d, 0x56}, 18,
  17751. /* ct, ct length */
  17752. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  17753. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  17754. 0x8c, 0xc3}, 18,
  17755. /* tag, tag length */
  17756. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  17757. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  17758. /* valid */
  17759. 1,
  17760. },
  17761. {
  17762. /* key, key length */
  17763. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  17764. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  17765. /* iv, iv length */
  17766. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  17767. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  17768. /* aad, aad length */
  17769. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  17770. /* msg, msg length */
  17771. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  17772. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  17773. 0xef, 0x11}, 18,
  17774. /* ct, ct length */
  17775. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  17776. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  17777. 0xb1, 0xe8}, 18,
  17778. /* tag, tag length */
  17779. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  17780. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  17781. /* valid */
  17782. 1,
  17783. },
  17784. {
  17785. /* key, key length */
  17786. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  17787. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  17788. /* iv, iv length */
  17789. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  17790. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  17791. /* aad, aad length */
  17792. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  17793. /* msg, msg length */
  17794. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  17795. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  17796. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  17797. /* ct, ct length */
  17798. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  17799. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  17800. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  17801. /* tag, tag length */
  17802. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  17803. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  17804. /* valid */
  17805. 1,
  17806. },
  17807. {
  17808. /* key, key length */
  17809. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17810. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17811. /* iv, iv length */
  17812. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  17813. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  17814. /* aad, aad length */
  17815. {0x00}, 0,
  17816. /* msg, msg length */
  17817. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17818. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17819. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17820. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17821. /* ct, ct length */
  17822. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  17823. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  17824. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  17825. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  17826. /* tag, tag length */
  17827. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  17828. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  17829. /* valid */
  17830. 1,
  17831. },
  17832. {
  17833. /* key, key length */
  17834. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17835. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17836. /* iv, iv length */
  17837. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  17838. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  17839. /* aad, aad length */
  17840. {0x00}, 0,
  17841. /* msg, msg length */
  17842. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17843. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17844. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17845. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17846. /* ct, ct length */
  17847. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  17848. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  17849. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  17850. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  17851. /* tag, tag length */
  17852. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  17853. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  17854. /* valid */
  17855. 1,
  17856. },
  17857. {
  17858. /* key, key length */
  17859. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17860. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17861. /* iv, iv length */
  17862. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  17863. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  17864. /* aad, aad length */
  17865. {0x00}, 0,
  17866. /* msg, msg length */
  17867. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17868. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17869. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17870. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17871. /* ct, ct length */
  17872. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  17873. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  17874. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  17875. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  17876. /* tag, tag length */
  17877. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  17878. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  17879. /* valid */
  17880. 1,
  17881. },
  17882. {
  17883. /* key, key length */
  17884. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17885. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17886. /* iv, iv length */
  17887. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  17888. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  17889. /* aad, aad length */
  17890. {0x00}, 0,
  17891. /* msg, msg length */
  17892. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17893. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17894. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17895. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17896. /* ct, ct length */
  17897. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  17898. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  17899. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  17900. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  17901. /* tag, tag length */
  17902. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  17903. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  17904. /* valid */
  17905. 1,
  17906. },
  17907. {
  17908. /* key, key length */
  17909. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17910. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17911. /* iv, iv length */
  17912. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  17913. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  17914. /* aad, aad length */
  17915. {0x00}, 0,
  17916. /* msg, msg length */
  17917. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17918. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17919. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17920. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17921. /* ct, ct length */
  17922. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  17923. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  17924. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  17925. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  17926. /* tag, tag length */
  17927. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  17928. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  17929. /* valid */
  17930. 1,
  17931. },
  17932. {
  17933. /* key, key length */
  17934. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17935. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17936. /* iv, iv length */
  17937. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  17938. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  17939. /* aad, aad length */
  17940. {0x00}, 0,
  17941. /* msg, msg length */
  17942. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17943. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17944. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17945. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17946. /* ct, ct length */
  17947. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  17948. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  17949. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  17950. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  17951. /* tag, tag length */
  17952. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  17953. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  17954. /* valid */
  17955. 1,
  17956. },
  17957. {
  17958. /* key, key length */
  17959. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17960. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17961. /* iv, iv length */
  17962. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  17963. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  17964. /* aad, aad length */
  17965. {0x00}, 0,
  17966. /* msg, msg length */
  17967. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17968. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17969. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17970. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17971. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17972. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17973. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17974. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  17975. /* ct, ct length */
  17976. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  17977. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  17978. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  17979. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  17980. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  17981. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  17982. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  17983. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  17984. /* tag, tag length */
  17985. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  17986. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  17987. /* valid */
  17988. 1,
  17989. },
  17990. {
  17991. /* key, key length */
  17992. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17993. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17994. /* iv, iv length */
  17995. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  17996. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  17997. /* aad, aad length */
  17998. {0x00}, 0,
  17999. /* msg, msg length */
  18000. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18001. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18002. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18003. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18004. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18005. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18006. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18007. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18008. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18009. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18010. /* ct, ct length */
  18011. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18012. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18013. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18014. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18015. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18016. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18017. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18018. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18019. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18020. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18021. /* tag, tag length */
  18022. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18023. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18024. /* valid */
  18025. 1,
  18026. },
  18027. {
  18028. /* key, key length */
  18029. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18030. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18031. /* iv, iv length */
  18032. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18033. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18034. /* aad, aad length */
  18035. {0x00}, 0,
  18036. /* msg, msg length */
  18037. {0x00}, 0,
  18038. /* ct, ct length */
  18039. {0x00}, 0,
  18040. /* tag, tag length */
  18041. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18042. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18043. /* valid */
  18044. 1,
  18045. },
  18046. {
  18047. /* key, key length */
  18048. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18049. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18050. /* iv, iv length */
  18051. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18052. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18053. /* aad, aad length */
  18054. {0x00}, 0,
  18055. /* msg, msg length */
  18056. {0x01}, 1,
  18057. /* ct, ct length */
  18058. {0x1d}, 1,
  18059. /* tag, tag length */
  18060. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18061. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18062. /* valid */
  18063. 1,
  18064. },
  18065. {
  18066. /* key, key length */
  18067. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18068. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18069. /* iv, iv length */
  18070. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18071. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18072. /* aad, aad length */
  18073. {0x00}, 0,
  18074. /* msg, msg length */
  18075. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18076. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18077. /* ct, ct length */
  18078. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18079. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18080. /* tag, tag length */
  18081. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18082. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18083. /* valid */
  18084. 1,
  18085. },
  18086. {
  18087. /* key, key length */
  18088. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18089. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18090. /* iv, iv length */
  18091. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18092. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18093. /* aad, aad length */
  18094. {0x00}, 0,
  18095. /* msg, msg length */
  18096. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18097. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18098. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18099. /* ct, ct length */
  18100. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18101. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18102. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18103. /* tag, tag length */
  18104. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18105. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18106. /* valid */
  18107. 1,
  18108. },
  18109. {
  18110. /* key, key length */
  18111. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18112. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18113. /* iv, iv length */
  18114. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18115. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18116. /* aad, aad length */
  18117. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18118. /* msg, msg length */
  18119. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18120. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18121. /* ct, ct length */
  18122. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18123. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18124. /* tag, tag length */
  18125. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18126. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18127. /* valid */
  18128. 1,
  18129. },
  18130. {
  18131. /* key, key length */
  18132. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18133. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18134. /* iv, iv length */
  18135. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18136. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18137. /* aad, aad length */
  18138. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18139. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18140. /* msg, msg length */
  18141. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18142. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18143. /* ct, ct length */
  18144. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18145. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18146. /* tag, tag length */
  18147. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18148. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18149. /* valid */
  18150. 1,
  18151. },
  18152. {
  18153. /* key, key length */
  18154. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18155. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18156. /* iv, iv length */
  18157. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18158. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18159. /* aad, aad length */
  18160. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18161. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18162. /* msg, msg length */
  18163. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18164. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18165. /* ct, ct length */
  18166. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18167. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18168. /* tag, tag length */
  18169. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18170. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18171. /* valid */
  18172. 1,
  18173. },
  18174. {
  18175. /* key, key length */
  18176. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18177. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18178. /* iv, iv length */
  18179. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18180. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18181. /* aad, aad length */
  18182. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18183. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18184. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18185. /* msg, msg length */
  18186. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18187. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18188. /* ct, ct length */
  18189. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18190. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18191. /* tag, tag length */
  18192. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18193. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18194. /* valid */
  18195. 1,
  18196. },
  18197. {
  18198. /* key, key length */
  18199. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18200. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18201. /* iv, iv length */
  18202. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18203. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18204. /* aad, aad length */
  18205. {0x00}, 0,
  18206. /* msg, msg length */
  18207. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18208. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18209. /* ct, ct length */
  18210. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18211. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18212. /* tag, tag length */
  18213. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18214. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18215. /* valid */
  18216. 0,
  18217. },
  18218. {
  18219. /* key, key length */
  18220. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18221. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18222. /* iv, iv length */
  18223. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18224. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18225. /* aad, aad length */
  18226. {0x00}, 0,
  18227. /* msg, msg length */
  18228. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18229. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18230. /* ct, ct length */
  18231. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18232. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18233. /* tag, tag length */
  18234. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18235. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18236. /* valid */
  18237. 0,
  18238. },
  18239. {
  18240. /* key, key length */
  18241. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18242. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18243. /* iv, iv length */
  18244. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18245. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18246. /* aad, aad length */
  18247. {0x00}, 0,
  18248. /* msg, msg length */
  18249. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18250. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18251. /* ct, ct length */
  18252. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18253. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18254. /* tag, tag length */
  18255. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18256. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18257. /* valid */
  18258. 0,
  18259. },
  18260. {
  18261. /* key, key length */
  18262. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18263. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18264. /* iv, iv length */
  18265. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18266. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18267. /* aad, aad length */
  18268. {0x00}, 0,
  18269. /* msg, msg length */
  18270. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18271. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18272. /* ct, ct length */
  18273. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18274. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18275. /* tag, tag length */
  18276. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18277. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18278. /* valid */
  18279. 0,
  18280. },
  18281. {
  18282. /* key, key length */
  18283. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18284. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18285. /* iv, iv length */
  18286. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18287. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18288. /* aad, aad length */
  18289. {0x00}, 0,
  18290. /* msg, msg length */
  18291. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18292. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18293. /* ct, ct length */
  18294. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18295. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18296. /* tag, tag length */
  18297. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18298. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18299. /* valid */
  18300. 0,
  18301. },
  18302. {
  18303. /* key, key length */
  18304. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18305. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18306. /* iv, iv length */
  18307. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18308. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18309. /* aad, aad length */
  18310. {0x00}, 0,
  18311. /* msg, msg length */
  18312. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18313. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18314. /* ct, ct length */
  18315. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18316. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18317. /* tag, tag length */
  18318. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18319. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18320. /* valid */
  18321. 0,
  18322. },
  18323. {
  18324. /* key, key length */
  18325. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18326. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18327. /* iv, iv length */
  18328. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18329. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18330. /* aad, aad length */
  18331. {0x00}, 0,
  18332. /* msg, msg length */
  18333. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18334. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18335. /* ct, ct length */
  18336. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18337. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18338. /* tag, tag length */
  18339. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18340. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18341. /* valid */
  18342. 0,
  18343. },
  18344. {
  18345. /* key, key length */
  18346. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18347. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18348. /* iv, iv length */
  18349. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18350. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18351. /* aad, aad length */
  18352. {0x00}, 0,
  18353. /* msg, msg length */
  18354. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18355. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18356. /* ct, ct length */
  18357. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18358. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18359. /* tag, tag length */
  18360. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18361. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18362. /* valid */
  18363. 0,
  18364. },
  18365. {
  18366. /* key, key length */
  18367. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18368. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18369. /* iv, iv length */
  18370. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18371. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18372. /* aad, aad length */
  18373. {0x00}, 0,
  18374. /* msg, msg length */
  18375. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18376. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18377. /* ct, ct length */
  18378. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18379. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18380. /* tag, tag length */
  18381. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18382. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18383. /* valid */
  18384. 0,
  18385. },
  18386. {
  18387. /* key, key length */
  18388. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18389. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18390. /* iv, iv length */
  18391. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18392. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18393. /* aad, aad length */
  18394. {0x00}, 0,
  18395. /* msg, msg length */
  18396. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18397. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18398. /* ct, ct length */
  18399. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18400. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18401. /* tag, tag length */
  18402. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18403. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18404. /* valid */
  18405. 0,
  18406. },
  18407. {
  18408. /* key, key length */
  18409. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18410. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18411. /* iv, iv length */
  18412. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18413. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18414. /* aad, aad length */
  18415. {0x00}, 0,
  18416. /* msg, msg length */
  18417. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18418. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18419. /* ct, ct length */
  18420. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18421. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18422. /* tag, tag length */
  18423. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18424. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18425. /* valid */
  18426. 0,
  18427. },
  18428. {
  18429. /* key, key length */
  18430. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18431. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18432. /* iv, iv length */
  18433. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18434. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18435. /* aad, aad length */
  18436. {0x00}, 0,
  18437. /* msg, msg length */
  18438. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18439. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18440. /* ct, ct length */
  18441. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18442. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18443. /* tag, tag length */
  18444. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18445. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18446. /* valid */
  18447. 0,
  18448. },
  18449. {
  18450. /* key, key length */
  18451. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18452. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18453. /* iv, iv length */
  18454. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18455. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18456. /* aad, aad length */
  18457. {0x00}, 0,
  18458. /* msg, msg length */
  18459. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18460. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18461. /* ct, ct length */
  18462. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18463. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18464. /* tag, tag length */
  18465. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18466. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18467. /* valid */
  18468. 0,
  18469. },
  18470. {
  18471. /* key, key length */
  18472. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18473. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18474. /* iv, iv length */
  18475. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18476. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18477. /* aad, aad length */
  18478. {0x00}, 0,
  18479. /* msg, msg length */
  18480. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18481. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18482. /* ct, ct length */
  18483. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18484. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18485. /* tag, tag length */
  18486. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18487. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18488. /* valid */
  18489. 0,
  18490. },
  18491. {
  18492. /* key, key length */
  18493. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18494. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18495. /* iv, iv length */
  18496. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18497. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18498. /* aad, aad length */
  18499. {0x00}, 0,
  18500. /* msg, msg length */
  18501. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18502. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18503. /* ct, ct length */
  18504. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18505. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18506. /* tag, tag length */
  18507. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18508. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18509. /* valid */
  18510. 0,
  18511. },
  18512. {
  18513. /* key, key length */
  18514. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18515. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18516. /* iv, iv length */
  18517. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18518. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18519. /* aad, aad length */
  18520. {0x00}, 0,
  18521. /* msg, msg length */
  18522. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18523. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18524. /* ct, ct length */
  18525. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18526. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18527. /* tag, tag length */
  18528. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18529. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18530. /* valid */
  18531. 0,
  18532. },
  18533. {
  18534. /* key, key length */
  18535. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18536. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18537. /* iv, iv length */
  18538. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18539. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18540. /* aad, aad length */
  18541. {0x00}, 0,
  18542. /* msg, msg length */
  18543. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18544. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18545. /* ct, ct length */
  18546. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18547. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18548. /* tag, tag length */
  18549. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18550. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18551. /* valid */
  18552. 0,
  18553. },
  18554. {
  18555. /* key, key length */
  18556. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18557. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18558. /* iv, iv length */
  18559. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18560. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18561. /* aad, aad length */
  18562. {0x00}, 0,
  18563. /* msg, msg length */
  18564. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18565. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18566. /* ct, ct length */
  18567. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18568. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18569. /* tag, tag length */
  18570. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18571. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18572. /* valid */
  18573. 0,
  18574. },
  18575. {
  18576. /* key, key length */
  18577. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18578. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18579. /* iv, iv length */
  18580. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18581. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18582. /* aad, aad length */
  18583. {0x00}, 0,
  18584. /* msg, msg length */
  18585. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18586. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18587. /* ct, ct length */
  18588. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18589. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18590. /* tag, tag length */
  18591. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18592. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18593. /* valid */
  18594. 0,
  18595. },
  18596. {
  18597. /* key, key length */
  18598. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18599. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18600. /* iv, iv length */
  18601. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18602. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18603. /* aad, aad length */
  18604. {0x00}, 0,
  18605. /* msg, msg length */
  18606. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18607. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18608. /* ct, ct length */
  18609. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18610. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18611. /* tag, tag length */
  18612. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18613. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18614. /* valid */
  18615. 0,
  18616. },
  18617. {
  18618. /* key, key length */
  18619. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18620. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18621. /* iv, iv length */
  18622. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18623. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18624. /* aad, aad length */
  18625. {0x00}, 0,
  18626. /* msg, msg length */
  18627. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18628. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18629. /* ct, ct length */
  18630. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18631. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18632. /* tag, tag length */
  18633. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  18634. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18635. /* valid */
  18636. 0,
  18637. },
  18638. {
  18639. /* key, key length */
  18640. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18641. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18642. /* iv, iv length */
  18643. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18644. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18645. /* aad, aad length */
  18646. {0x00}, 0,
  18647. /* msg, msg length */
  18648. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18649. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18650. /* ct, ct length */
  18651. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18652. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18653. /* tag, tag length */
  18654. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18655. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18656. /* valid */
  18657. 0,
  18658. },
  18659. {
  18660. /* key, key length */
  18661. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18662. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18663. /* iv, iv length */
  18664. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18665. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18666. /* aad, aad length */
  18667. {0x00}, 0,
  18668. /* msg, msg length */
  18669. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18670. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18671. /* ct, ct length */
  18672. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18673. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18674. /* tag, tag length */
  18675. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  18676. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  18677. /* valid */
  18678. 0,
  18679. },
  18680. {
  18681. /* key, key length */
  18682. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18683. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18684. /* iv, iv length */
  18685. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18686. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18687. /* aad, aad length */
  18688. {0x00}, 0,
  18689. /* msg, msg length */
  18690. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18691. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18692. /* ct, ct length */
  18693. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18694. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18695. /* tag, tag length */
  18696. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18697. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  18698. /* valid */
  18699. 0,
  18700. },
  18701. {
  18702. /* key, key length */
  18703. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18704. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18705. /* iv, iv length */
  18706. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18707. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18708. /* aad, aad length */
  18709. {0x00}, 0,
  18710. /* msg, msg length */
  18711. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18712. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18713. /* ct, ct length */
  18714. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18715. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18716. /* tag, tag length */
  18717. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  18718. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  18719. /* valid */
  18720. 0,
  18721. },
  18722. {
  18723. /* key, key length */
  18724. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18725. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18726. /* iv, iv length */
  18727. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18728. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18729. /* aad, aad length */
  18730. {0x00}, 0,
  18731. /* msg, msg length */
  18732. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18733. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18734. /* ct, ct length */
  18735. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18736. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18737. /* tag, tag length */
  18738. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  18739. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  18740. /* valid */
  18741. 0,
  18742. },
  18743. {
  18744. /* key, key length */
  18745. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18746. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18747. /* iv, iv length */
  18748. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18749. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18750. /* aad, aad length */
  18751. {0x00}, 0,
  18752. /* msg, msg length */
  18753. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18754. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18755. /* ct, ct length */
  18756. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18757. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18758. /* tag, tag length */
  18759. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  18760. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  18761. /* valid */
  18762. 0,
  18763. },
  18764. };
  18765. byte ciphertext[sizeof(vectors[0].ct)];
  18766. byte authtag[sizeof(vectors[0].tag)];
  18767. int i;
  18768. int len;
  18769. int ret;
  18770. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  18771. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18772. len = sizeof(authtag);
  18773. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  18774. ciphertext,
  18775. vectors[i].msg, vectors[i].msg_length,
  18776. vectors[i].iv, vectors[i].iv_length,
  18777. authtag, len,
  18778. vectors[i].aad, vectors[i].aad_length),
  18779. 0);
  18780. /* check ciphertext matches vector */
  18781. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  18782. 0);
  18783. /* check that computed tag matches vector only for vectors marked as valid */
  18784. ret = XMEMCMP(authtag, vectors[i].tag, len);
  18785. if (vectors[i].valid) {
  18786. ExpectIntEQ(ret, 0);
  18787. }
  18788. else {
  18789. ExpectIntNE(ret, 0);
  18790. }
  18791. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18792. /* Decrypt, checking that the computed auth tags match */
  18793. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  18794. ciphertext,
  18795. vectors[i].ct, vectors[i].ct_length,
  18796. vectors[i].iv, vectors[i].iv_length,
  18797. authtag, len,
  18798. vectors[i].aad, vectors[i].aad_length),
  18799. 0);
  18800. /* check decrypted ciphertext matches vector plaintext */
  18801. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  18802. 0);
  18803. }
  18804. return EXPECT_RESULT();
  18805. } /* END test_wc_AesEaxVectors */
  18806. /*
  18807. * Testing test_wc_AesEaxEncryptAuth()
  18808. */
  18809. static int test_wc_AesEaxEncryptAuth(void)
  18810. {
  18811. EXPECT_DECLS;
  18812. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18813. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18814. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18815. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18816. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18817. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18818. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18819. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18820. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18821. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18822. byte ciphertext[sizeof(msg)];
  18823. byte authtag[AES_BLOCK_SIZE];
  18824. int i;
  18825. int len;
  18826. len = sizeof(authtag);
  18827. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18828. ciphertext,
  18829. msg, sizeof(msg),
  18830. iv, sizeof(iv),
  18831. authtag, len,
  18832. aad, sizeof(aad)),
  18833. 0);
  18834. /* Test null checking */
  18835. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  18836. ciphertext,
  18837. msg, sizeof(msg),
  18838. iv, sizeof(iv),
  18839. authtag, len,
  18840. aad, sizeof(aad)),
  18841. BAD_FUNC_ARG);
  18842. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18843. NULL,
  18844. msg, sizeof(msg),
  18845. iv, sizeof(iv),
  18846. authtag, len,
  18847. aad, sizeof(aad)),
  18848. BAD_FUNC_ARG);
  18849. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18850. ciphertext,
  18851. NULL, sizeof(msg),
  18852. iv, sizeof(iv),
  18853. authtag, len,
  18854. aad, sizeof(aad)),
  18855. BAD_FUNC_ARG);
  18856. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18857. ciphertext,
  18858. msg, sizeof(msg),
  18859. NULL, sizeof(iv),
  18860. authtag, len,
  18861. aad, sizeof(aad)),
  18862. BAD_FUNC_ARG);
  18863. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18864. ciphertext,
  18865. msg, sizeof(msg),
  18866. iv, sizeof(iv),
  18867. NULL, len,
  18868. aad, sizeof(aad)),
  18869. BAD_FUNC_ARG);
  18870. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18871. ciphertext,
  18872. msg, sizeof(msg),
  18873. iv, sizeof(iv),
  18874. authtag, len,
  18875. NULL, sizeof(aad)),
  18876. BAD_FUNC_ARG);
  18877. /* Test bad key lengths */
  18878. for (i = 0; i <= 32; i++) {
  18879. int exp_ret;
  18880. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18881. || i == AES_256_KEY_SIZE) {
  18882. exp_ret = 0;
  18883. }
  18884. else {
  18885. exp_ret = BAD_FUNC_ARG;
  18886. }
  18887. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  18888. ciphertext,
  18889. msg, sizeof(msg),
  18890. iv, sizeof(iv),
  18891. authtag, len,
  18892. aad, sizeof(aad)),
  18893. exp_ret);
  18894. }
  18895. /* Test auth tag size out of range */
  18896. len = AES_BLOCK_SIZE + 1;
  18897. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18898. ciphertext,
  18899. msg, sizeof(msg),
  18900. iv, sizeof(iv),
  18901. authtag, len,
  18902. aad, sizeof(aad)),
  18903. BAD_FUNC_ARG);
  18904. return EXPECT_RESULT();
  18905. } /* END test_wc_AesEaxEncryptAuth() */
  18906. /*
  18907. * Testing test_wc_AesEaxDecryptAuth()
  18908. */
  18909. static int test_wc_AesEaxDecryptAuth(void)
  18910. {
  18911. EXPECT_DECLS;
  18912. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18913. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18914. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18915. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18916. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18917. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18918. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18919. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18920. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18921. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18922. /* Garbage tag that should always fail for above aad */
  18923. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  18924. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  18925. byte plaintext[sizeof(ct)];
  18926. int i;
  18927. int len;
  18928. len = sizeof(tag);
  18929. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18930. plaintext,
  18931. ct, sizeof(ct),
  18932. iv, sizeof(iv),
  18933. tag, len,
  18934. aad, sizeof(aad)),
  18935. AES_EAX_AUTH_E);
  18936. /* Test null checking */
  18937. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  18938. plaintext,
  18939. ct, sizeof(ct),
  18940. iv, sizeof(iv),
  18941. tag, len,
  18942. aad, sizeof(aad)),
  18943. BAD_FUNC_ARG);
  18944. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18945. NULL,
  18946. ct, sizeof(ct),
  18947. iv, sizeof(iv),
  18948. tag, len,
  18949. aad, sizeof(aad)),
  18950. BAD_FUNC_ARG);
  18951. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18952. plaintext,
  18953. NULL, sizeof(ct),
  18954. iv, sizeof(iv),
  18955. tag, len,
  18956. aad, sizeof(aad)),
  18957. BAD_FUNC_ARG);
  18958. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18959. plaintext,
  18960. ct, sizeof(ct),
  18961. NULL, sizeof(iv),
  18962. tag, len,
  18963. aad, sizeof(aad)),
  18964. BAD_FUNC_ARG);
  18965. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18966. plaintext,
  18967. ct, sizeof(ct),
  18968. iv, sizeof(iv),
  18969. NULL, len,
  18970. aad, sizeof(aad)),
  18971. BAD_FUNC_ARG);
  18972. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18973. plaintext,
  18974. ct, sizeof(ct),
  18975. iv, sizeof(iv),
  18976. tag, len,
  18977. NULL, sizeof(aad)),
  18978. BAD_FUNC_ARG);
  18979. /* Test bad key lengths */
  18980. for (i = 0; i <= 32; i++) {
  18981. int exp_ret;
  18982. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18983. || i == AES_256_KEY_SIZE) {
  18984. exp_ret = AES_EAX_AUTH_E;
  18985. }
  18986. else {
  18987. exp_ret = BAD_FUNC_ARG;
  18988. }
  18989. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  18990. plaintext,
  18991. ct, sizeof(ct),
  18992. iv, sizeof(iv),
  18993. tag, len,
  18994. aad, sizeof(aad)),
  18995. exp_ret);
  18996. }
  18997. /* Test auth tag size out of range */
  18998. len = AES_BLOCK_SIZE + 1;
  18999. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19000. plaintext,
  19001. ct, sizeof(ct),
  19002. iv, sizeof(iv),
  19003. tag, len,
  19004. aad, sizeof(aad)),
  19005. BAD_FUNC_ARG);
  19006. return EXPECT_RESULT();
  19007. } /* END test_wc_AesEaxDecryptAuth() */
  19008. #endif /* WOLFSSL_AES_EAX &&
  19009. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19010. */
  19011. /*
  19012. * Testing wc_InitDsaKey()
  19013. */
  19014. static int test_wc_InitDsaKey(void)
  19015. {
  19016. EXPECT_DECLS;
  19017. #ifndef NO_DSA
  19018. DsaKey key;
  19019. XMEMSET(&key, 0, sizeof(DsaKey));
  19020. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19021. /* Pass in bad args. */
  19022. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19023. wc_FreeDsaKey(&key);
  19024. #endif
  19025. return EXPECT_RESULT();
  19026. } /* END test_wc_InitDsaKey */
  19027. /*
  19028. * Testing wc_DsaSign() and wc_DsaVerify()
  19029. */
  19030. static int test_wc_DsaSignVerify(void)
  19031. {
  19032. EXPECT_DECLS;
  19033. #if !defined(NO_DSA)
  19034. DsaKey key;
  19035. WC_RNG rng;
  19036. wc_Sha sha;
  19037. byte signature[DSA_SIG_SIZE];
  19038. byte hash[WC_SHA_DIGEST_SIZE];
  19039. word32 idx = 0;
  19040. word32 bytes;
  19041. int answer;
  19042. #ifdef USE_CERT_BUFFERS_1024
  19043. byte tmp[ONEK_BUF];
  19044. XMEMSET(tmp, 0, sizeof(tmp));
  19045. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19046. bytes = sizeof_dsa_key_der_1024;
  19047. #elif defined(USE_CERT_BUFFERS_2048)
  19048. byte tmp[TWOK_BUF];
  19049. XMEMSET(tmp, 0, sizeof(tmp));
  19050. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19051. bytes = sizeof_dsa_key_der_2048;
  19052. #else
  19053. byte tmp[TWOK_BUF];
  19054. XFILE fp = XBADFILE;
  19055. XMEMSET(tmp, 0, sizeof(tmp));
  19056. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19057. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19058. if (fp != XBADFILE)
  19059. XFCLOSE(fp);
  19060. #endif /* END USE_CERT_BUFFERS_1024 */
  19061. ExpectIntEQ(wc_InitSha(&sha), 0);
  19062. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19063. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19064. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19065. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19066. ExpectIntEQ(wc_InitRng(&rng), 0);
  19067. /* Sign. */
  19068. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19069. /* Test bad args. */
  19070. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19071. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19072. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19073. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19074. /* Verify. */
  19075. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19076. ExpectIntEQ(answer, 1);
  19077. /* Pass in bad args. */
  19078. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19079. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19080. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19081. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19082. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19083. /* hard set q to 0 and test fail case */
  19084. mp_free(&key.q);
  19085. mp_init(&key.q);
  19086. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19087. mp_set(&key.q, 1);
  19088. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19089. #endif
  19090. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19091. wc_FreeDsaKey(&key);
  19092. wc_ShaFree(&sha);
  19093. #endif
  19094. return EXPECT_RESULT();
  19095. } /* END test_wc_DsaSign */
  19096. /*
  19097. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19098. */
  19099. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19100. {
  19101. EXPECT_DECLS;
  19102. #if !defined(NO_DSA)
  19103. DsaKey key;
  19104. word32 bytes = 0;
  19105. word32 idx = 0;
  19106. int ret = 0;
  19107. #ifdef USE_CERT_BUFFERS_1024
  19108. byte tmp[ONEK_BUF];
  19109. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19110. bytes = sizeof_dsa_key_der_1024;
  19111. #elif defined(USE_CERT_BUFFERS_2048)
  19112. byte tmp[TWOK_BUF];
  19113. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19114. bytes = sizeof_dsa_key_der_2048;
  19115. #else
  19116. byte tmp[TWOK_BUF];
  19117. XFILE fp = XBADFILE;
  19118. XMEMSET(tmp, 0, sizeof(tmp));
  19119. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19120. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19121. if (fp != XBADFILE)
  19122. XFCLOSE(fp);
  19123. #endif /* END USE_CERT_BUFFERS_1024 */
  19124. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19125. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19126. /* Test bad args. */
  19127. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19128. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19129. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19130. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19131. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19132. wc_FreeDsaKey(&key);
  19133. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19134. idx = 0; /* Reset */
  19135. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19136. /* Test bad args. */
  19137. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19138. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19139. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19140. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19141. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19142. wc_FreeDsaKey(&key);
  19143. #endif /* !NO_DSA */
  19144. return EXPECT_RESULT();
  19145. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19146. /*
  19147. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19148. */
  19149. static int test_wc_MakeDsaKey(void)
  19150. {
  19151. EXPECT_DECLS;
  19152. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19153. DsaKey genKey;
  19154. WC_RNG rng;
  19155. XMEMSET(&genKey, 0, sizeof(genKey));
  19156. XMEMSET(&rng, 0, sizeof(rng));
  19157. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19158. ExpectIntEQ(wc_InitRng(&rng), 0);
  19159. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19160. /* Test bad args. */
  19161. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19162. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19163. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19164. BAD_FUNC_ARG);
  19165. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19166. /* Test bad args. */
  19167. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19168. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19169. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19170. wc_FreeDsaKey(&genKey);
  19171. #endif
  19172. return EXPECT_RESULT();
  19173. } /* END test_wc_MakeDsaKey */
  19174. /*
  19175. * Testing wc_DsaKeyToDer()
  19176. */
  19177. static int test_wc_DsaKeyToDer(void)
  19178. {
  19179. EXPECT_DECLS;
  19180. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19181. DsaKey key;
  19182. word32 bytes;
  19183. word32 idx = 0;
  19184. #ifdef USE_CERT_BUFFERS_1024
  19185. byte tmp[ONEK_BUF];
  19186. byte der[ONEK_BUF];
  19187. XMEMSET(tmp, 0, sizeof(tmp));
  19188. XMEMSET(der, 0, sizeof(der));
  19189. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19190. bytes = sizeof_dsa_key_der_1024;
  19191. #elif defined(USE_CERT_BUFFERS_2048)
  19192. byte tmp[TWOK_BUF];
  19193. byte der[TWOK_BUF];
  19194. XMEMSET(tmp, 0, sizeof(tmp));
  19195. XMEMSET(der, 0, sizeof(der));
  19196. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19197. bytes = sizeof_dsa_key_der_2048;
  19198. #else
  19199. byte tmp[TWOK_BUF];
  19200. byte der[TWOK_BUF];
  19201. XFILE fp = XBADFILE;
  19202. XMEMSET(tmp, 0, sizeof(tmp));
  19203. XMEMSET(der, 0, sizeof(der));
  19204. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19205. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19206. if (fp != XBADFILE)
  19207. XFCLOSE(fp);
  19208. #endif /* END USE_CERT_BUFFERS_1024 */
  19209. XMEMSET(&key, 0, sizeof(DsaKey));
  19210. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19211. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19212. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19213. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19214. /* Test bad args. */
  19215. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19216. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19217. wc_FreeDsaKey(&key);
  19218. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19219. return EXPECT_RESULT();
  19220. } /* END test_wc_DsaKeyToDer */
  19221. /*
  19222. * Testing wc_DsaKeyToPublicDer()
  19223. * (indirectly testing setDsaPublicKey())
  19224. */
  19225. static int test_wc_DsaKeyToPublicDer(void)
  19226. {
  19227. EXPECT_DECLS;
  19228. #ifndef HAVE_SELFTEST
  19229. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19230. DsaKey key;
  19231. WC_RNG rng;
  19232. byte* der = NULL;
  19233. word32 sz = 0;
  19234. word32 idx = 0;
  19235. XMEMSET(&key, 0, sizeof(DsaKey));
  19236. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19237. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19238. DYNAMIC_TYPE_TMP_BUFFER));
  19239. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19240. ExpectIntEQ(wc_InitRng(&rng), 0);
  19241. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19242. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19243. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19244. wc_FreeDsaKey(&key);
  19245. idx = 0;
  19246. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19247. /* Test without the SubjectPublicKeyInfo header */
  19248. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19249. wc_FreeDsaKey(&key);
  19250. idx = 0;
  19251. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19252. /* Test bad args. */
  19253. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19254. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19255. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19256. wc_FreeDsaKey(&key);
  19257. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19258. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19259. #endif /* !HAVE_SELFTEST */
  19260. return EXPECT_RESULT();
  19261. } /* END test_wc_DsaKeyToPublicDer */
  19262. /*
  19263. * Testing wc_DsaImportParamsRaw()
  19264. */
  19265. static int test_wc_DsaImportParamsRaw(void)
  19266. {
  19267. EXPECT_DECLS;
  19268. #if !defined(NO_DSA)
  19269. DsaKey key;
  19270. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19271. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19272. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19273. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19274. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19275. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19276. "80a0093113a8bd73";
  19277. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19278. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19279. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19280. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19281. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19282. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19283. "76341a7e7d9";
  19284. /* invalid p and q parameters */
  19285. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19286. const char* invalidQ = "96c5390a";
  19287. XMEMSET(&key, 0, sizeof(DsaKey));
  19288. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19289. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19290. /* test bad args */
  19291. /* null key struct */
  19292. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19293. /* null param pointers */
  19294. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19295. /* illegal p length */
  19296. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19297. /* illegal q length */
  19298. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19299. wc_FreeDsaKey(&key);
  19300. #endif
  19301. return EXPECT_RESULT();
  19302. } /* END test_wc_DsaImportParamsRaw */
  19303. /*
  19304. * Testing wc_DsaImportParamsRawCheck()
  19305. */
  19306. static int test_wc_DsaImportParamsRawCheck(void)
  19307. {
  19308. EXPECT_DECLS;
  19309. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19310. DsaKey key;
  19311. int trusted = 0;
  19312. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19313. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19314. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19315. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19316. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19317. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19318. "80a0093113a8bd73";
  19319. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19320. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19321. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19322. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19323. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19324. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19325. "76341a7e7d9";
  19326. /* invalid p and q parameters */
  19327. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19328. const char* invalidQ = "96c5390a";
  19329. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19330. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19331. /* test bad args */
  19332. /* null key struct */
  19333. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19334. BAD_FUNC_ARG);
  19335. /* null param pointers */
  19336. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19337. NULL), BAD_FUNC_ARG);
  19338. /* illegal p length */
  19339. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19340. BAD_FUNC_ARG);
  19341. /* illegal q length */
  19342. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19343. BAD_FUNC_ARG);
  19344. wc_FreeDsaKey(&key);
  19345. #endif
  19346. return EXPECT_RESULT();
  19347. } /* END test_wc_DsaImportParamsRawCheck */
  19348. /*
  19349. * Testing wc_DsaExportParamsRaw()
  19350. */
  19351. static int test_wc_DsaExportParamsRaw(void)
  19352. {
  19353. EXPECT_DECLS;
  19354. #if !defined(NO_DSA)
  19355. DsaKey key;
  19356. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19357. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19358. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19359. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19360. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19361. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19362. "80a0093113a8bd73";
  19363. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19364. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19365. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19366. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19367. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19368. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19369. "76341a7e7d9";
  19370. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19371. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19372. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19373. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19374. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19375. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19376. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19377. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19378. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19379. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19380. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19381. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19382. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19383. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19384. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19385. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19386. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19387. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19388. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19389. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19390. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19391. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19392. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19393. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19394. byte pOut[MAX_DSA_PARAM_SIZE];
  19395. byte qOut[MAX_DSA_PARAM_SIZE];
  19396. byte gOut[MAX_DSA_PARAM_SIZE];
  19397. word32 pOutSz;
  19398. word32 qOutSz;
  19399. word32 gOutSz;
  19400. XMEMSET(&key, 0, sizeof(DsaKey));
  19401. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19402. /* first test using imported raw parameters, for expected */
  19403. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19404. pOutSz = sizeof(pOut);
  19405. qOutSz = sizeof(qOut);
  19406. gOutSz = sizeof(gOut);
  19407. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19408. &gOutSz), 0);
  19409. /* validate exported parameters are correct */
  19410. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19411. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19412. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19413. /* test bad args */
  19414. /* null key struct */
  19415. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19416. &gOutSz), BAD_FUNC_ARG);
  19417. /* null output pointers */
  19418. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19419. &gOutSz), LENGTH_ONLY_E);
  19420. /* null output size pointers */
  19421. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19422. NULL), BAD_FUNC_ARG);
  19423. /* p output buffer size too small */
  19424. pOutSz = 1;
  19425. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19426. &gOutSz), BUFFER_E);
  19427. pOutSz = sizeof(pOut);
  19428. /* q output buffer size too small */
  19429. qOutSz = 1;
  19430. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19431. &gOutSz), BUFFER_E);
  19432. qOutSz = sizeof(qOut);
  19433. /* g output buffer size too small */
  19434. gOutSz = 1;
  19435. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19436. &gOutSz), BUFFER_E);
  19437. wc_FreeDsaKey(&key);
  19438. #endif
  19439. return EXPECT_RESULT();
  19440. } /* END test_wc_DsaExportParamsRaw */
  19441. /*
  19442. * Testing wc_DsaExportKeyRaw()
  19443. */
  19444. static int test_wc_DsaExportKeyRaw(void)
  19445. {
  19446. EXPECT_DECLS;
  19447. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19448. DsaKey key;
  19449. WC_RNG rng;
  19450. byte xOut[MAX_DSA_PARAM_SIZE];
  19451. byte yOut[MAX_DSA_PARAM_SIZE];
  19452. word32 xOutSz, yOutSz;
  19453. XMEMSET(&key, 0, sizeof(key));
  19454. XMEMSET(&rng, 0, sizeof(rng));
  19455. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19456. ExpectIntEQ(wc_InitRng(&rng), 0);
  19457. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19458. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19459. /* try successful export */
  19460. xOutSz = sizeof(xOut);
  19461. yOutSz = sizeof(yOut);
  19462. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19463. /* test bad args */
  19464. /* null key struct */
  19465. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19466. BAD_FUNC_ARG);
  19467. /* null output pointers */
  19468. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19469. LENGTH_ONLY_E);
  19470. /* null output size pointers */
  19471. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19472. BAD_FUNC_ARG);
  19473. /* x output buffer size too small */
  19474. xOutSz = 1;
  19475. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19476. BUFFER_E);
  19477. xOutSz = sizeof(xOut);
  19478. /* y output buffer size too small */
  19479. yOutSz = 1;
  19480. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19481. BUFFER_E);
  19482. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19483. wc_FreeDsaKey(&key);
  19484. #endif
  19485. return EXPECT_RESULT();
  19486. } /* END test_wc_DsaExportParamsRaw */
  19487. /*
  19488. * Testing wc_ed25519_make_key().
  19489. */
  19490. static int test_wc_ed25519_make_key(void)
  19491. {
  19492. EXPECT_DECLS;
  19493. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19494. ed25519_key key;
  19495. WC_RNG rng;
  19496. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19497. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19498. XMEMSET(&key, 0, sizeof(ed25519_key));
  19499. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19500. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19501. ExpectIntEQ(wc_InitRng(&rng), 0);
  19502. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19503. ECC_PRIV_KEY_E);
  19504. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19505. ECC_PRIV_KEY_E);
  19506. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19507. /* Test bad args. */
  19508. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19509. BAD_FUNC_ARG);
  19510. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19511. BAD_FUNC_ARG);
  19512. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19513. BAD_FUNC_ARG);
  19514. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19515. BAD_FUNC_ARG);
  19516. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19517. wc_ed25519_free(&key);
  19518. #endif
  19519. return EXPECT_RESULT();
  19520. } /* END test_wc_ed25519_make_key */
  19521. /*
  19522. * Testing wc_ed25519_init()
  19523. */
  19524. static int test_wc_ed25519_init(void)
  19525. {
  19526. EXPECT_DECLS;
  19527. #if defined(HAVE_ED25519)
  19528. ed25519_key key;
  19529. XMEMSET(&key, 0, sizeof(ed25519_key));
  19530. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19531. /* Test bad args. */
  19532. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19533. wc_ed25519_free(&key);
  19534. #endif
  19535. return EXPECT_RESULT();
  19536. } /* END test_wc_ed25519_init */
  19537. /*
  19538. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19539. */
  19540. static int test_wc_ed25519_sign_msg(void)
  19541. {
  19542. EXPECT_DECLS;
  19543. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19544. WC_RNG rng;
  19545. ed25519_key key;
  19546. byte msg[] = "Everybody gets Friday off.\n";
  19547. byte sig[ED25519_SIG_SIZE+1];
  19548. word32 msglen = sizeof(msg);
  19549. word32 siglen = ED25519_SIG_SIZE;
  19550. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19551. #ifdef HAVE_ED25519_VERIFY
  19552. int verify_ok = 0; /*1 = Verify success.*/
  19553. #endif
  19554. /* Initialize stack variables. */
  19555. XMEMSET(&key, 0, sizeof(ed25519_key));
  19556. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19557. XMEMSET(sig, 0, sizeof(sig));
  19558. /* Initialize key. */
  19559. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19560. ExpectIntEQ(wc_InitRng(&rng), 0);
  19561. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19562. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19563. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19564. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19565. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19566. /* Test bad args. */
  19567. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19568. BAD_FUNC_ARG);
  19569. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19570. BAD_FUNC_ARG);
  19571. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19572. BAD_FUNC_ARG);
  19573. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19574. BAD_FUNC_ARG);
  19575. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19576. BUFFER_E);
  19577. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19578. badSigLen -= 1;
  19579. #ifdef HAVE_ED25519_VERIFY
  19580. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19581. &key), 0);
  19582. ExpectIntEQ(verify_ok, 1);
  19583. /* Test bad args. */
  19584. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19585. &verify_ok, &key), BAD_FUNC_ARG);
  19586. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19587. &verify_ok, &key), BAD_FUNC_ARG);
  19588. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19589. &key), BAD_FUNC_ARG);
  19590. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19591. &key), BAD_FUNC_ARG);
  19592. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19593. BAD_FUNC_ARG);
  19594. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19595. NULL), BAD_FUNC_ARG);
  19596. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19597. &key), BAD_FUNC_ARG);
  19598. #endif /* Verify. */
  19599. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19600. wc_ed25519_free(&key);
  19601. #endif
  19602. return EXPECT_RESULT();
  19603. } /* END test_wc_ed25519_sign_msg */
  19604. /*
  19605. * Testing wc_ed25519_import_public()
  19606. */
  19607. static int test_wc_ed25519_import_public(void)
  19608. {
  19609. EXPECT_DECLS;
  19610. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19611. ed25519_key pubKey;
  19612. WC_RNG rng;
  19613. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19614. word32 inlen = sizeof(in);
  19615. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  19616. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19617. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  19618. ExpectIntEQ(wc_InitRng(&rng), 0);
  19619. #ifdef HAVE_ED25519_MAKE_KEY
  19620. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  19621. #endif
  19622. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  19623. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19624. /* Test bad args. */
  19625. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19626. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19627. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19628. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19629. wc_ed25519_free(&pubKey);
  19630. #endif
  19631. return EXPECT_RESULT();
  19632. } /* END wc_ed25519_import_public */
  19633. /*
  19634. * Testing wc_ed25519_import_private_key()
  19635. */
  19636. static int test_wc_ed25519_import_private_key(void)
  19637. {
  19638. EXPECT_DECLS;
  19639. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19640. ed25519_key key;
  19641. WC_RNG rng;
  19642. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19643. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19644. word32 privKeySz = sizeof(privKey);
  19645. word32 pubKeySz = sizeof(pubKey);
  19646. #ifdef HAVE_ED25519_KEY_EXPORT
  19647. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19648. word32 bothKeysSz = sizeof(bothKeys);
  19649. #endif
  19650. XMEMSET(&key, 0, sizeof(ed25519_key));
  19651. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19652. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19653. ExpectIntEQ(wc_InitRng(&rng), 0);
  19654. #ifdef HAVE_ED25519_MAKE_KEY
  19655. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19656. #endif
  19657. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19658. pubKeySz, &key, 1), 0);
  19659. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19660. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19661. #ifdef HAVE_ED25519_KEY_EXPORT
  19662. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  19663. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19664. &key, 1), 0);
  19665. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19666. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19667. #endif
  19668. /* Test bad args. */
  19669. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19670. &key), BAD_FUNC_ARG);
  19671. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19672. pubKeySz, &key), BAD_FUNC_ARG);
  19673. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19674. pubKeySz, NULL), BAD_FUNC_ARG);
  19675. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19676. pubKeySz, &key), BAD_FUNC_ARG);
  19677. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19678. pubKeySz - 1, &key), BAD_FUNC_ARG);
  19679. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  19680. &key), BAD_FUNC_ARG);
  19681. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19682. wc_ed25519_free(&key);
  19683. #endif
  19684. return EXPECT_RESULT();
  19685. } /* END test_wc_ed25519_import_private_key */
  19686. /*
  19687. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19688. */
  19689. static int test_wc_ed25519_export(void)
  19690. {
  19691. EXPECT_DECLS;
  19692. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19693. ed25519_key key;
  19694. WC_RNG rng;
  19695. byte priv[ED25519_PRV_KEY_SIZE];
  19696. byte pub[ED25519_PUB_KEY_SIZE];
  19697. word32 privSz = sizeof(priv);
  19698. word32 pubSz = sizeof(pub);
  19699. #ifndef HAVE_ED25519_MAKE_KEY
  19700. const byte privKey[] = {
  19701. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19702. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19703. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19704. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19705. };
  19706. const byte pubKey[] = {
  19707. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19708. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19709. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19710. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19711. };
  19712. #endif
  19713. XMEMSET(&key, 0, sizeof(ed25519_key));
  19714. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19715. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19716. ExpectIntEQ(wc_InitRng(&rng), 0);
  19717. #ifdef HAVE_ED25519_MAKE_KEY
  19718. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19719. #else
  19720. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19721. pubKey, sizeof(pubKey), &key, 1), 0);
  19722. #endif
  19723. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  19724. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  19725. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  19726. /* Test bad args. */
  19727. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  19728. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  19729. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  19730. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  19731. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  19732. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  19733. /* Test bad args. */
  19734. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  19735. BAD_FUNC_ARG);
  19736. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  19737. BAD_FUNC_ARG);
  19738. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  19739. BAD_FUNC_ARG);
  19740. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19741. wc_ed25519_free(&key);
  19742. #endif
  19743. return EXPECT_RESULT();
  19744. } /* END test_wc_ed25519_export */
  19745. /*
  19746. * Testing wc_ed25519_size()
  19747. */
  19748. static int test_wc_ed25519_size(void)
  19749. {
  19750. EXPECT_DECLS;
  19751. #if defined(HAVE_ED25519)
  19752. ed25519_key key;
  19753. WC_RNG rng;
  19754. #ifndef HAVE_ED25519_MAKE_KEY
  19755. const byte privKey[] = {
  19756. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19757. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19758. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19759. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19760. };
  19761. const byte pubKey[] = {
  19762. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19763. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19764. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19765. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19766. };
  19767. #endif
  19768. XMEMSET(&key, 0, sizeof(ed25519_key));
  19769. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19770. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19771. ExpectIntEQ(wc_InitRng(&rng), 0);
  19772. #ifdef HAVE_ED25519_MAKE_KEY
  19773. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19774. #else
  19775. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19776. pubKey, sizeof(pubKey), &key, 1), 0);
  19777. #endif
  19778. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  19779. /* Test bad args. */
  19780. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  19781. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  19782. /* Test bad args. */
  19783. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  19784. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  19785. /* Test bad args. */
  19786. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  19787. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  19788. /* Test bad args. */
  19789. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  19790. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19791. wc_ed25519_free(&key);
  19792. #endif
  19793. return EXPECT_RESULT();
  19794. } /* END test_wc_ed25519_size */
  19795. /*
  19796. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19797. */
  19798. static int test_wc_ed25519_exportKey(void)
  19799. {
  19800. EXPECT_DECLS;
  19801. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19802. WC_RNG rng;
  19803. ed25519_key key;
  19804. byte priv[ED25519_PRV_KEY_SIZE];
  19805. byte pub[ED25519_PUB_KEY_SIZE];
  19806. byte privOnly[ED25519_PRV_KEY_SIZE];
  19807. word32 privSz = sizeof(priv);
  19808. word32 pubSz = sizeof(pub);
  19809. word32 privOnlySz = sizeof(privOnly);
  19810. #ifndef HAVE_ED25519_MAKE_KEY
  19811. const byte privKey[] = {
  19812. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19813. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19814. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19815. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19816. };
  19817. const byte pubKey[] = {
  19818. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19819. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19820. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19821. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19822. };
  19823. #endif
  19824. XMEMSET(&key, 0, sizeof(ed25519_key));
  19825. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19826. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19827. ExpectIntEQ(wc_InitRng(&rng), 0);
  19828. #ifdef HAVE_ED25519_MAKE_KEY
  19829. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19830. #else
  19831. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19832. pubKey, sizeof(pubKey), &key, 1), 0);
  19833. #endif
  19834. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  19835. /* Test bad args. */
  19836. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  19837. BAD_FUNC_ARG);
  19838. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  19839. BAD_FUNC_ARG);
  19840. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  19841. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  19842. /* Test bad args. */
  19843. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  19844. BAD_FUNC_ARG);
  19845. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  19846. BAD_FUNC_ARG);
  19847. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  19848. BAD_FUNC_ARG);
  19849. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  19850. BAD_FUNC_ARG);
  19851. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  19852. BAD_FUNC_ARG);
  19853. /* Cross check output. */
  19854. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  19855. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19856. wc_ed25519_free(&key);
  19857. #endif
  19858. return EXPECT_RESULT();
  19859. } /* END test_wc_ed25519_exportKey */
  19860. /*
  19861. * Testing wc_Ed25519PublicKeyToDer
  19862. */
  19863. static int test_wc_Ed25519PublicKeyToDer(void)
  19864. {
  19865. EXPECT_DECLS;
  19866. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19867. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19868. ed25519_key key;
  19869. byte derBuf[1024];
  19870. XMEMSET(&key, 0, sizeof(ed25519_key));
  19871. /* Test bad args */
  19872. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  19873. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19874. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  19875. wc_ed25519_free(&key);
  19876. /* Test good args */
  19877. if (EXPECT_SUCCESS()) {
  19878. WC_RNG rng;
  19879. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19880. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19881. ExpectIntEQ(wc_InitRng(&rng), 0);
  19882. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19883. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  19884. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19885. wc_ed25519_free(&key);
  19886. }
  19887. #endif
  19888. return EXPECT_RESULT();
  19889. } /* END testing wc_Ed25519PublicKeyToDer */
  19890. /*
  19891. * Testing wc_curve25519_init and wc_curve25519_free.
  19892. */
  19893. static int test_wc_curve25519_init(void)
  19894. {
  19895. EXPECT_DECLS;
  19896. #if defined(HAVE_CURVE25519)
  19897. curve25519_key key;
  19898. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19899. /* Test bad args for wc_curve25519_init */
  19900. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  19901. /* Test good args for wc_curve_25519_free */
  19902. wc_curve25519_free(&key);
  19903. /* Test bad args for wc_curve25519 free. */
  19904. wc_curve25519_free(NULL);
  19905. #endif
  19906. return EXPECT_RESULT();
  19907. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19908. /*
  19909. * Testing test_wc_curve25519_size.
  19910. */
  19911. static int test_wc_curve25519_size(void)
  19912. {
  19913. EXPECT_DECLS;
  19914. #if defined(HAVE_CURVE25519)
  19915. curve25519_key key;
  19916. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19917. /* Test good args for wc_curve25519_size */
  19918. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19919. /* Test bad args for wc_curve25519_size */
  19920. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  19921. wc_curve25519_free(&key);
  19922. #endif
  19923. return EXPECT_RESULT();
  19924. } /* END test_wc_curve25519_size*/
  19925. /*
  19926. * Testing test_wc_curve25519_export_key_raw().
  19927. */
  19928. static int test_wc_curve25519_export_key_raw(void)
  19929. {
  19930. EXPECT_DECLS;
  19931. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19932. curve25519_key key;
  19933. WC_RNG rng;
  19934. byte privateKey[CURVE25519_KEYSIZE];
  19935. byte publicKey[CURVE25519_KEYSIZE];
  19936. word32 prvkSz;
  19937. word32 pubkSz;
  19938. byte prik[CURVE25519_KEYSIZE];
  19939. byte pubk[CURVE25519_KEYSIZE];
  19940. word32 prksz;
  19941. word32 pbksz;
  19942. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19943. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19944. ExpectIntEQ(wc_InitRng(&rng), 0);
  19945. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19946. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19947. prvkSz = CURVE25519_KEYSIZE;
  19948. pubkSz = CURVE25519_KEYSIZE;
  19949. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  19950. publicKey, &pubkSz), BAD_FUNC_ARG);
  19951. prvkSz = CURVE25519_KEYSIZE;
  19952. pubkSz = CURVE25519_KEYSIZE;
  19953. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  19954. &pubkSz), BAD_FUNC_ARG);
  19955. prvkSz = CURVE25519_KEYSIZE;
  19956. pubkSz = CURVE25519_KEYSIZE;
  19957. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  19958. publicKey, &pubkSz), BAD_FUNC_ARG);
  19959. /* prvkSz = CURVE25519_KEYSIZE; */
  19960. pubkSz = CURVE25519_KEYSIZE;
  19961. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19962. NULL, &pubkSz), BAD_FUNC_ARG);
  19963. prvkSz = CURVE25519_KEYSIZE;
  19964. pubkSz = CURVE25519_KEYSIZE;
  19965. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19966. publicKey, NULL), BAD_FUNC_ARG);
  19967. /* cross-testing */
  19968. prksz = CURVE25519_KEYSIZE;
  19969. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  19970. pbksz = CURVE25519_KEYSIZE;
  19971. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  19972. prvkSz = CURVE25519_KEYSIZE;
  19973. /* pubkSz = CURVE25519_KEYSIZE; */
  19974. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19975. publicKey, &pubkSz), 0);
  19976. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19977. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19978. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19979. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19980. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19981. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19982. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19983. wc_curve25519_free(&key);
  19984. #endif
  19985. return EXPECT_RESULT();
  19986. } /* end of test_wc_curve25519_export_key_raw */
  19987. /*
  19988. * Testing test_wc_curve25519_export_key_raw_ex().
  19989. */
  19990. static int test_wc_curve25519_export_key_raw_ex(void)
  19991. {
  19992. EXPECT_DECLS;
  19993. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19994. curve25519_key key;
  19995. WC_RNG rng;
  19996. byte privateKey[CURVE25519_KEYSIZE];
  19997. byte publicKey[CURVE25519_KEYSIZE];
  19998. word32 prvkSz;
  19999. word32 pubkSz;
  20000. byte prik[CURVE25519_KEYSIZE];
  20001. byte pubk[CURVE25519_KEYSIZE];
  20002. word32 prksz;
  20003. word32 pbksz;
  20004. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20005. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20006. ExpectIntEQ(wc_InitRng(&rng), 0);
  20007. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20008. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20009. prvkSz = CURVE25519_KEYSIZE;
  20010. pubkSz = CURVE25519_KEYSIZE;
  20011. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20012. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20013. prvkSz = CURVE25519_KEYSIZE;
  20014. pubkSz = CURVE25519_KEYSIZE;
  20015. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20016. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20017. prvkSz = CURVE25519_KEYSIZE;
  20018. pubkSz = CURVE25519_KEYSIZE;
  20019. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20020. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20021. /* prvkSz = CURVE25519_KEYSIZE; */
  20022. pubkSz = CURVE25519_KEYSIZE;
  20023. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20024. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20025. prvkSz = CURVE25519_KEYSIZE;
  20026. pubkSz = CURVE25519_KEYSIZE;
  20027. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20028. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20029. prvkSz = CURVE25519_KEYSIZE;
  20030. /* pubkSz = CURVE25519_KEYSIZE; */
  20031. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20032. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20033. prvkSz = CURVE25519_KEYSIZE;
  20034. pubkSz = CURVE25519_KEYSIZE;
  20035. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20036. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20037. prvkSz = CURVE25519_KEYSIZE;
  20038. pubkSz = CURVE25519_KEYSIZE;
  20039. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20040. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20041. /* prvkSz = CURVE25519_KEYSIZE; */
  20042. pubkSz = CURVE25519_KEYSIZE;
  20043. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20044. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20045. prvkSz = CURVE25519_KEYSIZE;
  20046. pubkSz = CURVE25519_KEYSIZE;
  20047. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20048. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20049. /* illegal value for endian */
  20050. prvkSz = CURVE25519_KEYSIZE;
  20051. /* pubkSz = CURVE25519_KEYSIZE; */
  20052. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20053. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20054. /* cross-testing */
  20055. prksz = CURVE25519_KEYSIZE;
  20056. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20057. pbksz = CURVE25519_KEYSIZE;
  20058. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20059. prvkSz = CURVE25519_KEYSIZE;
  20060. /* pubkSz = CURVE25519_KEYSIZE; */
  20061. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20062. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20063. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20064. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20065. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20066. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20067. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20068. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20069. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20070. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20071. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20072. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20073. /* try once with another endian */
  20074. prvkSz = CURVE25519_KEYSIZE;
  20075. pubkSz = CURVE25519_KEYSIZE;
  20076. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20077. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20078. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20079. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20080. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20081. wc_curve25519_free(&key);
  20082. #endif
  20083. return EXPECT_RESULT();
  20084. } /* end of test_wc_curve25519_export_key_raw_ex */
  20085. /*
  20086. * Testing wc_curve25519_make_key
  20087. */
  20088. static int test_wc_curve25519_make_key(void)
  20089. {
  20090. EXPECT_DECLS;
  20091. #if defined(HAVE_CURVE25519)
  20092. curve25519_key key;
  20093. WC_RNG rng;
  20094. int keysize;
  20095. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20096. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20097. ExpectIntEQ(wc_InitRng(&rng), 0);
  20098. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20099. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20100. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20101. /* test bad cases*/
  20102. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20103. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20104. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20105. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20106. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20107. wc_curve25519_free(&key);
  20108. #endif
  20109. return EXPECT_RESULT();
  20110. } /* END test_wc_curve25519_make_key*/
  20111. /*
  20112. * Testing wc_curve25519_shared_secret_ex
  20113. */
  20114. static int test_wc_curve25519_shared_secret_ex(void)
  20115. {
  20116. EXPECT_DECLS;
  20117. #if defined(HAVE_CURVE25519)
  20118. curve25519_key private_key;
  20119. curve25519_key public_key;
  20120. WC_RNG rng;
  20121. byte out[CURVE25519_KEYSIZE];
  20122. word32 outLen = sizeof(out);
  20123. int endian = EC25519_BIG_ENDIAN;
  20124. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20125. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20126. ExpectIntEQ(wc_InitRng(&rng), 0);
  20127. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20128. 0);
  20129. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20130. 0);
  20131. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20132. &outLen, endian), 0);
  20133. /* test bad cases*/
  20134. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20135. BAD_FUNC_ARG);
  20136. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20137. endian), BAD_FUNC_ARG);
  20138. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20139. endian), BAD_FUNC_ARG);
  20140. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20141. &outLen, endian), BAD_FUNC_ARG);
  20142. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20143. NULL, endian), BAD_FUNC_ARG);
  20144. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20145. * increasing to 0x8f checks for error being returned*/
  20146. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20147. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20148. &outLen, endian), ECC_BAD_ARG_E);
  20149. outLen = outLen - 2;
  20150. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20151. &outLen, endian), BAD_FUNC_ARG);
  20152. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20153. wc_curve25519_free(&private_key);
  20154. wc_curve25519_free(&public_key);
  20155. #endif
  20156. return EXPECT_RESULT();
  20157. } /* END test_wc_curve25519_shared_secret_ex*/
  20158. /*
  20159. * Testing wc_curve25519_make_pub
  20160. */
  20161. static int test_wc_curve25519_make_pub(void)
  20162. {
  20163. EXPECT_DECLS;
  20164. #ifdef HAVE_CURVE25519
  20165. curve25519_key key;
  20166. WC_RNG rng;
  20167. byte out[CURVE25519_KEYSIZE];
  20168. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20169. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20170. ExpectIntEQ(wc_InitRng(&rng), 0);
  20171. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20172. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20173. (int)sizeof(key.k), key.k), 0);
  20174. /* test bad cases*/
  20175. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20176. (int)sizeof out, out), ECC_BAD_ARG_E);
  20177. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20178. NULL), ECC_BAD_ARG_E);
  20179. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20180. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20181. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20182. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20183. /* verify clamping test */
  20184. key.k[0] |= ~248;
  20185. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20186. key.k), ECC_BAD_ARG_E);
  20187. key.k[0] &= 248;
  20188. /* repeat the expected-to-succeed test. */
  20189. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20190. key.k), 0);
  20191. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20192. wc_curve25519_free(&key);
  20193. #endif
  20194. return EXPECT_RESULT();
  20195. } /* END test_wc_curve25519_make_pub */
  20196. /*
  20197. * Testing test_wc_curve25519_export_public_ex
  20198. */
  20199. static int test_wc_curve25519_export_public_ex(void)
  20200. {
  20201. EXPECT_DECLS;
  20202. #if defined(HAVE_CURVE25519)
  20203. curve25519_key key;
  20204. WC_RNG rng;
  20205. byte out[CURVE25519_KEYSIZE];
  20206. word32 outLen = sizeof(out);
  20207. int endian = EC25519_BIG_ENDIAN;
  20208. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20209. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20210. ExpectIntEQ(wc_InitRng(&rng), 0);
  20211. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20212. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20213. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20214. /* test bad cases*/
  20215. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20216. BAD_FUNC_ARG);
  20217. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20218. BAD_FUNC_ARG);
  20219. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20220. BAD_FUNC_ARG);
  20221. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20222. BAD_FUNC_ARG);
  20223. outLen = outLen - 2;
  20224. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20225. ECC_BAD_ARG_E);
  20226. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20227. wc_curve25519_free(&key);
  20228. #endif
  20229. return EXPECT_RESULT();
  20230. } /* END test_wc_curve25519_export_public_ex*/
  20231. /*
  20232. * Testing test_wc_curve25519_import_private_raw_ex
  20233. */
  20234. static int test_wc_curve25519_import_private_raw_ex(void)
  20235. {
  20236. EXPECT_DECLS;
  20237. #if defined(HAVE_CURVE25519)
  20238. curve25519_key key;
  20239. WC_RNG rng;
  20240. byte priv[CURVE25519_KEYSIZE];
  20241. byte pub[CURVE25519_KEYSIZE];
  20242. word32 privSz = sizeof(priv);
  20243. word32 pubSz = sizeof(pub);
  20244. int endian = EC25519_BIG_ENDIAN;
  20245. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20246. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20247. ExpectIntEQ(wc_InitRng(&rng), 0);
  20248. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20249. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20250. endian), 0);
  20251. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20252. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20253. &key, endian), 0);
  20254. /* test bad cases*/
  20255. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20256. endian), BAD_FUNC_ARG);
  20257. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20258. &key, endian), BAD_FUNC_ARG);
  20259. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20260. &key, endian), BAD_FUNC_ARG);
  20261. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20262. NULL, endian), BAD_FUNC_ARG);
  20263. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20264. &key, endian), ECC_BAD_ARG_E);
  20265. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20266. &key, endian), ECC_BAD_ARG_E);
  20267. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20268. &key, EC25519_LITTLE_ENDIAN), 0);
  20269. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20270. wc_curve25519_free(&key);
  20271. #endif
  20272. return EXPECT_RESULT();
  20273. } /* END test_wc_curve25519_import_private_raw_ex*/
  20274. /*
  20275. * Testing test_wc_curve25519_import_private
  20276. */
  20277. static int test_wc_curve25519_import_private(void)
  20278. {
  20279. EXPECT_DECLS;
  20280. #if defined(HAVE_CURVE25519)
  20281. curve25519_key key;
  20282. WC_RNG rng;
  20283. byte priv[CURVE25519_KEYSIZE];
  20284. word32 privSz = sizeof(priv);
  20285. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20286. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20287. ExpectIntEQ(wc_InitRng(&rng), 0);
  20288. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20289. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20290. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20291. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20292. wc_curve25519_free(&key);
  20293. #endif
  20294. return EXPECT_RESULT();
  20295. } /* END test_wc_curve25519_import*/
  20296. /*
  20297. * Testing test_wc_curve25519_export_private_raw_ex
  20298. */
  20299. static int test_wc_curve25519_export_private_raw_ex(void)
  20300. {
  20301. EXPECT_DECLS;
  20302. #if defined(HAVE_CURVE25519)
  20303. curve25519_key key;
  20304. byte out[CURVE25519_KEYSIZE];
  20305. word32 outLen = sizeof(out);
  20306. int endian = EC25519_BIG_ENDIAN;
  20307. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20308. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20309. 0);
  20310. /* test bad cases*/
  20311. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20312. BAD_FUNC_ARG);
  20313. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20314. BAD_FUNC_ARG);
  20315. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20316. endian), BAD_FUNC_ARG);
  20317. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20318. BAD_FUNC_ARG);
  20319. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20320. EC25519_LITTLE_ENDIAN), 0);
  20321. outLen = outLen - 2;
  20322. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20323. ECC_BAD_ARG_E);
  20324. wc_curve25519_free(&key);
  20325. #endif
  20326. return EXPECT_RESULT();
  20327. } /* END test_wc_curve25519_export_private_raw_ex*/
  20328. /*
  20329. * Testing wc_ed448_make_key().
  20330. */
  20331. static int test_wc_ed448_make_key(void)
  20332. {
  20333. EXPECT_DECLS;
  20334. #if defined(HAVE_ED448)
  20335. ed448_key key;
  20336. WC_RNG rng;
  20337. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20338. XMEMSET(&key, 0, sizeof(ed448_key));
  20339. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20340. ExpectIntEQ(wc_ed448_init(&key), 0);
  20341. ExpectIntEQ(wc_InitRng(&rng), 0);
  20342. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20343. ECC_PRIV_KEY_E);
  20344. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20345. /* Test bad args. */
  20346. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20347. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20348. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20349. BAD_FUNC_ARG);
  20350. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20351. BAD_FUNC_ARG);
  20352. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20353. wc_ed448_free(&key);
  20354. #endif
  20355. return EXPECT_RESULT();
  20356. } /* END test_wc_ed448_make_key */
  20357. /*
  20358. * Testing wc_ed448_init()
  20359. */
  20360. static int test_wc_ed448_init(void)
  20361. {
  20362. EXPECT_DECLS;
  20363. #if defined(HAVE_ED448)
  20364. ed448_key key;
  20365. XMEMSET(&key, 0, sizeof(ed448_key));
  20366. ExpectIntEQ(wc_ed448_init(&key), 0);
  20367. /* Test bad args. */
  20368. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20369. wc_ed448_free(&key);
  20370. #endif
  20371. return EXPECT_RESULT();
  20372. } /* END test_wc_ed448_init */
  20373. /*
  20374. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20375. */
  20376. static int test_wc_ed448_sign_msg(void)
  20377. {
  20378. EXPECT_DECLS;
  20379. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20380. ed448_key key;
  20381. WC_RNG rng;
  20382. byte msg[] = "Everybody gets Friday off.\n";
  20383. byte sig[ED448_SIG_SIZE];
  20384. word32 msglen = sizeof(msg);
  20385. word32 siglen = sizeof(sig);
  20386. word32 badSigLen = sizeof(sig) - 1;
  20387. #ifdef HAVE_ED448_VERIFY
  20388. int verify_ok = 0; /*1 = Verify success.*/
  20389. #endif
  20390. /* Initialize stack variables. */
  20391. XMEMSET(&key, 0, sizeof(ed448_key));
  20392. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20393. XMEMSET(sig, 0, siglen);
  20394. /* Initialize key. */
  20395. ExpectIntEQ(wc_ed448_init(&key), 0);
  20396. ExpectIntEQ(wc_InitRng(&rng), 0);
  20397. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20398. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20399. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20400. /* Test bad args. */
  20401. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20402. BAD_FUNC_ARG);
  20403. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20404. BAD_FUNC_ARG);
  20405. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20406. BAD_FUNC_ARG);
  20407. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20408. BAD_FUNC_ARG);
  20409. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20410. BUFFER_E);
  20411. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20412. badSigLen -= 1;
  20413. #ifdef HAVE_ED448_VERIFY
  20414. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20415. NULL, 0), 0);
  20416. ExpectIntEQ(verify_ok, 1);
  20417. /* Test bad args. */
  20418. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20419. &key, NULL, 0), BAD_FUNC_ARG);
  20420. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20421. &key, NULL, 0), BAD_FUNC_ARG);
  20422. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20423. &key, NULL, 0), BAD_FUNC_ARG);
  20424. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20425. &key, NULL, 0), BAD_FUNC_ARG);
  20426. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20427. &key, NULL, 0), BAD_FUNC_ARG);
  20428. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20429. NULL, NULL, 0), BAD_FUNC_ARG);
  20430. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20431. &key, NULL, 0), BAD_FUNC_ARG);
  20432. #endif /* Verify. */
  20433. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20434. wc_ed448_free(&key);
  20435. #endif
  20436. return EXPECT_RESULT();
  20437. } /* END test_wc_ed448_sign_msg */
  20438. /*
  20439. * Testing wc_ed448_import_public()
  20440. */
  20441. static int test_wc_ed448_import_public(void)
  20442. {
  20443. EXPECT_DECLS;
  20444. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20445. ed448_key pubKey;
  20446. WC_RNG rng;
  20447. const byte in[] =
  20448. "Ed448PublicKeyUnitTest.................................\n";
  20449. word32 inlen = sizeof(in);
  20450. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20451. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20452. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20453. ExpectIntEQ(wc_InitRng(&rng), 0);
  20454. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20455. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20456. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20457. /* Test bad args. */
  20458. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20459. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20460. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20461. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20462. wc_ed448_free(&pubKey);
  20463. #endif
  20464. return EXPECT_RESULT();
  20465. } /* END wc_ed448_import_public */
  20466. /*
  20467. * Testing wc_ed448_import_private_key()
  20468. */
  20469. static int test_wc_ed448_import_private_key(void)
  20470. {
  20471. EXPECT_DECLS;
  20472. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20473. ed448_key key;
  20474. WC_RNG rng;
  20475. const byte privKey[] =
  20476. "Ed448PrivateKeyUnitTest................................\n";
  20477. const byte pubKey[] =
  20478. "Ed448PublicKeyUnitTest.................................\n";
  20479. word32 privKeySz = sizeof(privKey);
  20480. word32 pubKeySz = sizeof(pubKey);
  20481. #ifdef HAVE_ED448_KEY_EXPORT
  20482. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20483. word32 bothKeysSz = sizeof(bothKeys);
  20484. #endif
  20485. XMEMSET(&key, 0, sizeof(ed448_key));
  20486. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20487. ExpectIntEQ(wc_ed448_init(&key), 0);
  20488. ExpectIntEQ(wc_InitRng(&rng), 0);
  20489. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20490. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20491. pubKeySz, &key, 1), 0);
  20492. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20493. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20494. #ifdef HAVE_ED448_KEY_EXPORT
  20495. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20496. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20497. &key, 1), 0);
  20498. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20499. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20500. #endif
  20501. /* Test bad args. */
  20502. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20503. &key), BAD_FUNC_ARG);
  20504. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20505. &key), BAD_FUNC_ARG);
  20506. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20507. pubKeySz, NULL), BAD_FUNC_ARG);
  20508. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20509. pubKeySz, &key), BAD_FUNC_ARG);
  20510. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20511. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20512. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20513. BAD_FUNC_ARG);
  20514. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20515. wc_ed448_free(&key);
  20516. #endif
  20517. return EXPECT_RESULT();
  20518. } /* END test_wc_ed448_import_private_key */
  20519. /*
  20520. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20521. */
  20522. static int test_wc_ed448_export(void)
  20523. {
  20524. EXPECT_DECLS;
  20525. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20526. ed448_key key;
  20527. WC_RNG rng;
  20528. byte priv[ED448_PRV_KEY_SIZE];
  20529. byte pub[ED448_PUB_KEY_SIZE];
  20530. word32 privSz = sizeof(priv);
  20531. word32 pubSz = sizeof(pub);
  20532. XMEMSET(&key, 0, sizeof(ed448_key));
  20533. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20534. ExpectIntEQ(wc_ed448_init(&key), 0);
  20535. ExpectIntEQ(wc_InitRng(&rng), 0);
  20536. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20537. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20538. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20539. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20540. /* Test bad args. */
  20541. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20542. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20543. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20544. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20545. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20546. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20547. /* Test bad args. */
  20548. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20549. BAD_FUNC_ARG);
  20550. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20551. BAD_FUNC_ARG);
  20552. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20553. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20554. wc_ed448_free(&key);
  20555. #endif
  20556. return EXPECT_RESULT();
  20557. } /* END test_wc_ed448_export */
  20558. /*
  20559. * Testing wc_ed448_size()
  20560. */
  20561. static int test_wc_ed448_size(void)
  20562. {
  20563. EXPECT_DECLS;
  20564. #if defined(HAVE_ED448)
  20565. ed448_key key;
  20566. WC_RNG rng;
  20567. XMEMSET(&key, 0, sizeof(ed448_key));
  20568. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20569. ExpectIntEQ(wc_ed448_init(&key), 0);
  20570. ExpectIntEQ(wc_InitRng(&rng), 0);
  20571. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20572. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20573. /* Test bad args. */
  20574. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20575. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20576. /* Test bad args. */
  20577. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20578. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20579. /* Test bad args. */
  20580. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20581. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20582. /* Test bad args. */
  20583. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20584. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20585. wc_ed448_free(&key);
  20586. #endif
  20587. return EXPECT_RESULT();
  20588. } /* END test_wc_ed448_size */
  20589. /*
  20590. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20591. */
  20592. static int test_wc_ed448_exportKey(void)
  20593. {
  20594. EXPECT_DECLS;
  20595. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20596. ed448_key key;
  20597. WC_RNG rng;
  20598. byte priv[ED448_PRV_KEY_SIZE];
  20599. byte pub[ED448_PUB_KEY_SIZE];
  20600. byte privOnly[ED448_PRV_KEY_SIZE];
  20601. word32 privSz = sizeof(priv);
  20602. word32 pubSz = sizeof(pub);
  20603. word32 privOnlySz = sizeof(privOnly);
  20604. XMEMSET(&key, 0, sizeof(ed448_key));
  20605. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20606. ExpectIntEQ(wc_ed448_init(&key), 0);
  20607. ExpectIntEQ(wc_InitRng(&rng), 0);
  20608. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20609. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  20610. /* Test bad args. */
  20611. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  20612. BAD_FUNC_ARG);
  20613. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  20614. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20615. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20616. /* Test bad args. */
  20617. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  20618. BAD_FUNC_ARG);
  20619. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  20620. BAD_FUNC_ARG);
  20621. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  20622. BAD_FUNC_ARG);
  20623. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  20624. BAD_FUNC_ARG);
  20625. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  20626. BAD_FUNC_ARG);
  20627. /* Cross check output. */
  20628. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20629. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20630. wc_ed448_free(&key);
  20631. #endif
  20632. return EXPECT_RESULT();
  20633. } /* END test_wc_ed448_exportKey */
  20634. /*
  20635. * Testing wc_Ed448PublicKeyToDer
  20636. */
  20637. static int test_wc_Ed448PublicKeyToDer(void)
  20638. {
  20639. EXPECT_DECLS;
  20640. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20641. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20642. ed448_key key;
  20643. byte derBuf[1024];
  20644. XMEMSET(&key, 0, sizeof(ed448_key));
  20645. /* Test bad args */
  20646. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20647. ExpectIntEQ(wc_ed448_init(&key), 0);
  20648. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20649. wc_ed448_free(&key);
  20650. /* Test good args */
  20651. if (EXPECT_SUCCESS()) {
  20652. WC_RNG rng;
  20653. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20654. ExpectIntEQ(wc_ed448_init(&key), 0);
  20655. ExpectIntEQ(wc_InitRng(&rng), 0);
  20656. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20657. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20658. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20659. wc_ed448_free(&key);
  20660. }
  20661. #endif
  20662. return EXPECT_RESULT();
  20663. } /* END testing wc_Ed448PublicKeyToDer */
  20664. /*
  20665. * Testing wc_curve448_init and wc_curve448_free.
  20666. */
  20667. static int test_wc_curve448_init(void)
  20668. {
  20669. EXPECT_DECLS;
  20670. #if defined(HAVE_CURVE448)
  20671. curve448_key key;
  20672. /* Test bad args for wc_curve448_init */
  20673. ExpectIntEQ(wc_curve448_init(&key), 0);
  20674. /* Test bad args for wc_curve448_init */
  20675. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  20676. /* Test good args for wc_curve_448_free */
  20677. wc_curve448_free(&key);
  20678. /* Test bad args for wc_curve448_free */
  20679. wc_curve448_free(NULL);
  20680. #endif
  20681. return EXPECT_RESULT();
  20682. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20683. /*
  20684. * Testing wc_curve448_make_key
  20685. */
  20686. static int test_wc_curve448_make_key(void)
  20687. {
  20688. EXPECT_DECLS;
  20689. #if defined(HAVE_CURVE448)
  20690. curve448_key key;
  20691. WC_RNG rng;
  20692. int keysize;
  20693. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20694. ExpectIntEQ(wc_curve448_init(&key), 0);
  20695. ExpectIntEQ(wc_InitRng(&rng), 0);
  20696. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20697. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20698. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  20699. /* test bad cases */
  20700. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20701. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20702. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20703. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20704. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20705. wc_curve448_free(&key);
  20706. #endif
  20707. return EXPECT_RESULT();
  20708. } /* END test_wc_curve448_make_key*/
  20709. /*
  20710. * Testing test_wc_curve448_shared_secret_ex
  20711. */
  20712. static int test_wc_curve448_shared_secret_ex(void)
  20713. {
  20714. EXPECT_DECLS;
  20715. #if defined(HAVE_CURVE448)
  20716. curve448_key private_key;
  20717. curve448_key public_key;
  20718. WC_RNG rng;
  20719. byte out[CURVE448_KEY_SIZE];
  20720. word32 outLen = sizeof(out);
  20721. int endian = EC448_BIG_ENDIAN;
  20722. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20723. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  20724. ExpectIntEQ(wc_InitRng(&rng), 0);
  20725. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  20726. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  20727. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  20728. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20729. &outLen, endian), 0);
  20730. /* test bad cases */
  20731. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20732. BAD_FUNC_ARG);
  20733. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  20734. endian), BAD_FUNC_ARG);
  20735. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  20736. endian), BAD_FUNC_ARG);
  20737. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20738. &outLen, endian), BAD_FUNC_ARG);
  20739. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20740. NULL, endian), BAD_FUNC_ARG);
  20741. outLen = outLen - 2;
  20742. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20743. &outLen, endian), BAD_FUNC_ARG);
  20744. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20745. wc_curve448_free(&private_key);
  20746. wc_curve448_free(&public_key);
  20747. #endif
  20748. return EXPECT_RESULT();
  20749. } /* END test_wc_curve448_shared_secret_ex*/
  20750. /*
  20751. * Testing test_wc_curve448_export_public_ex
  20752. */
  20753. static int test_wc_curve448_export_public_ex(void)
  20754. {
  20755. EXPECT_DECLS;
  20756. #if defined(HAVE_CURVE448)
  20757. WC_RNG rng;
  20758. curve448_key key;
  20759. byte out[CURVE448_KEY_SIZE];
  20760. word32 outLen = sizeof(out);
  20761. int endian = EC448_BIG_ENDIAN;
  20762. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20763. ExpectIntEQ(wc_curve448_init(&key), 0);
  20764. ExpectIntEQ(wc_InitRng(&rng), 0);
  20765. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20766. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  20767. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  20768. /* test bad cases*/
  20769. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  20770. BAD_FUNC_ARG);
  20771. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  20772. BAD_FUNC_ARG);
  20773. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  20774. BAD_FUNC_ARG);
  20775. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  20776. BAD_FUNC_ARG);
  20777. outLen = outLen - 2;
  20778. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  20779. ECC_BAD_ARG_E);
  20780. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20781. wc_curve448_free(&key);
  20782. #endif
  20783. return EXPECT_RESULT();
  20784. } /* END test_wc_curve448_export_public_ex*/
  20785. /*
  20786. * Testing test_wc_curve448_export_private_raw_ex
  20787. */
  20788. static int test_wc_curve448_export_private_raw_ex(void)
  20789. {
  20790. EXPECT_DECLS;
  20791. #if defined(HAVE_CURVE448)
  20792. curve448_key key;
  20793. byte out[CURVE448_KEY_SIZE];
  20794. word32 outLen = sizeof(out);
  20795. int endian = EC448_BIG_ENDIAN;
  20796. ExpectIntEQ(wc_curve448_init(&key), 0);
  20797. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20798. 0);
  20799. /* test bad cases*/
  20800. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  20801. BAD_FUNC_ARG);
  20802. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  20803. BAD_FUNC_ARG);
  20804. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  20805. BAD_FUNC_ARG);
  20806. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  20807. BAD_FUNC_ARG);
  20808. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20809. EC448_LITTLE_ENDIAN), 0);
  20810. outLen = outLen - 2;
  20811. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20812. ECC_BAD_ARG_E);
  20813. wc_curve448_free(&key);
  20814. #endif
  20815. return EXPECT_RESULT();
  20816. } /* END test_wc_curve448_export_private_raw_ex*/
  20817. /*
  20818. * Testing test_wc_curve448_import_private_raw_ex
  20819. */
  20820. static int test_wc_curve448_import_private_raw_ex(void)
  20821. {
  20822. EXPECT_DECLS;
  20823. #if defined(HAVE_CURVE448)
  20824. curve448_key key;
  20825. WC_RNG rng;
  20826. byte priv[CURVE448_KEY_SIZE];
  20827. byte pub[CURVE448_KEY_SIZE];
  20828. word32 privSz = sizeof(priv);
  20829. word32 pubSz = sizeof(pub);
  20830. int endian = EC448_BIG_ENDIAN;
  20831. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20832. ExpectIntEQ(wc_curve448_init(&key), 0);
  20833. ExpectIntEQ(wc_InitRng(&rng), 0);
  20834. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20835. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20836. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20837. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20838. &key, endian), 0);
  20839. /* test bad cases */
  20840. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  20841. BAD_FUNC_ARG);
  20842. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20843. &key, endian), BAD_FUNC_ARG);
  20844. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20845. &key, endian), BAD_FUNC_ARG);
  20846. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20847. NULL, endian), BAD_FUNC_ARG);
  20848. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20849. &key, endian), ECC_BAD_ARG_E);
  20850. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20851. &key, endian), ECC_BAD_ARG_E);
  20852. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20853. &key, EC448_LITTLE_ENDIAN), 0);
  20854. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20855. wc_curve448_free(&key);
  20856. #endif
  20857. return EXPECT_RESULT();
  20858. } /* END test_wc_curve448_import_private_raw_ex*/
  20859. /*
  20860. * Testing test_curve448_export_key_raw
  20861. */
  20862. static int test_wc_curve448_export_key_raw(void)
  20863. {
  20864. EXPECT_DECLS;
  20865. #if defined(HAVE_CURVE448)
  20866. curve448_key key;
  20867. WC_RNG rng;
  20868. byte priv[CURVE448_KEY_SIZE];
  20869. byte pub[CURVE448_KEY_SIZE];
  20870. word32 privSz = sizeof(priv);
  20871. word32 pubSz = sizeof(pub);
  20872. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20873. ExpectIntEQ(wc_curve448_init(&key), 0);
  20874. ExpectIntEQ(wc_InitRng(&rng), 0);
  20875. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20876. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20877. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20878. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  20879. 0);
  20880. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20881. wc_curve448_free(&key);
  20882. #endif
  20883. return EXPECT_RESULT();
  20884. } /* END test_wc_curve448_import_private_raw_ex*/
  20885. /*
  20886. * Testing test_wc_curve448_import_private
  20887. */
  20888. static int test_wc_curve448_import_private(void)
  20889. {
  20890. EXPECT_DECLS;
  20891. #if defined(HAVE_CURVE448)
  20892. curve448_key key;
  20893. WC_RNG rng;
  20894. byte priv[CURVE448_KEY_SIZE];
  20895. word32 privSz = sizeof(priv);
  20896. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20897. ExpectIntEQ(wc_curve448_init(&key), 0);
  20898. ExpectIntEQ(wc_InitRng(&rng), 0);
  20899. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20900. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20901. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  20902. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20903. wc_curve448_free(&key);
  20904. #endif
  20905. return EXPECT_RESULT();
  20906. } /* END test_wc_curve448_import*/
  20907. /*
  20908. * Testing test_wc_curve448_size.
  20909. */
  20910. static int test_wc_curve448_size(void)
  20911. {
  20912. EXPECT_DECLS;
  20913. #if defined(HAVE_CURVE448)
  20914. curve448_key key;
  20915. ExpectIntEQ(wc_curve448_init(&key), 0);
  20916. /* Test good args for wc_curve448_size */
  20917. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20918. /* Test bad args for wc_curve448_size */
  20919. ExpectIntEQ(wc_curve448_size(NULL), 0);
  20920. wc_curve448_free(&key);
  20921. #endif
  20922. return EXPECT_RESULT();
  20923. } /* END test_wc_curve448_size*/
  20924. /*
  20925. * Testing wc_ecc_make_key.
  20926. */
  20927. static int test_wc_ecc_make_key(void)
  20928. {
  20929. EXPECT_DECLS;
  20930. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20931. ecc_key key;
  20932. WC_RNG rng;
  20933. int ret;
  20934. XMEMSET(&key, 0, sizeof(ecc_key));
  20935. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20936. ExpectIntEQ(wc_ecc_init(&key), 0);
  20937. ExpectIntEQ(wc_InitRng(&rng), 0);
  20938. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20939. #if defined(WOLFSSL_ASYNC_CRYPT)
  20940. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20941. #endif
  20942. ExpectIntEQ(ret, 0);
  20943. /* Pass in bad args. */
  20944. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  20945. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  20946. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20947. wc_ecc_free(&key);
  20948. #ifdef FP_ECC
  20949. wc_ecc_fp_free();
  20950. #endif
  20951. #endif
  20952. return EXPECT_RESULT();
  20953. } /* END test_wc_ecc_make_key */
  20954. /*
  20955. * Testing wc_ecc_init()
  20956. */
  20957. static int test_wc_ecc_init(void)
  20958. {
  20959. EXPECT_DECLS;
  20960. #ifdef HAVE_ECC
  20961. ecc_key key;
  20962. XMEMSET(&key, 0, sizeof(ecc_key));
  20963. ExpectIntEQ(wc_ecc_init(&key), 0);
  20964. /* Pass in bad args. */
  20965. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  20966. wc_ecc_free(&key);
  20967. #endif
  20968. return EXPECT_RESULT();
  20969. } /* END test_wc_ecc_init */
  20970. /*
  20971. * Testing wc_ecc_check_key()
  20972. */
  20973. static int test_wc_ecc_check_key(void)
  20974. {
  20975. EXPECT_DECLS;
  20976. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20977. ecc_key key;
  20978. WC_RNG rng;
  20979. int ret;
  20980. XMEMSET(&rng, 0, sizeof(rng));
  20981. XMEMSET(&key, 0, sizeof(key));
  20982. ExpectIntEQ(wc_ecc_init(&key), 0);
  20983. ExpectIntEQ(wc_InitRng(&rng), 0);
  20984. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20985. #if defined(WOLFSSL_ASYNC_CRYPT)
  20986. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20987. #endif
  20988. ExpectIntEQ(ret, 0);
  20989. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  20990. /* Pass in bad args. */
  20991. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  20992. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20993. wc_ecc_free(&key);
  20994. #ifdef FP_ECC
  20995. wc_ecc_fp_free();
  20996. #endif
  20997. #endif
  20998. return EXPECT_RESULT();
  20999. } /* END test_wc_ecc_check_key */
  21000. /*
  21001. * Testing wc_ecc_get_generator()
  21002. */
  21003. static int test_wc_ecc_get_generator(void)
  21004. {
  21005. EXPECT_DECLS;
  21006. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21007. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21008. ecc_point* pt = NULL;
  21009. ExpectNotNull(pt = wc_ecc_new_point());
  21010. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21011. MP_OKAY);
  21012. /* Test bad args. */
  21013. /* Returns Zero for bad arg. */
  21014. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21015. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21016. MP_OKAY);
  21017. /* If we ever get to 1000 curves increase this number */
  21018. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21019. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21020. wc_ecc_del_point(pt);
  21021. #endif
  21022. return EXPECT_RESULT();
  21023. } /* END test_wc_ecc_get_generator */
  21024. /*
  21025. * Testing wc_ecc_size()
  21026. */
  21027. static int test_wc_ecc_size(void)
  21028. {
  21029. EXPECT_DECLS;
  21030. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21031. WC_RNG rng;
  21032. ecc_key key;
  21033. int ret;
  21034. XMEMSET(&key, 0, sizeof(ecc_key));
  21035. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21036. ExpectIntEQ(wc_ecc_init(&key), 0);
  21037. ExpectIntEQ(wc_InitRng(&rng), 0);
  21038. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21039. #if defined(WOLFSSL_ASYNC_CRYPT)
  21040. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21041. #endif
  21042. ExpectIntEQ(ret, 0);
  21043. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21044. /* Test bad args. */
  21045. /* Returns Zero for bad arg. */
  21046. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21047. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21048. wc_ecc_free(&key);
  21049. #endif
  21050. return EXPECT_RESULT();
  21051. } /* END test_wc_ecc_size */
  21052. static int test_wc_ecc_params(void)
  21053. {
  21054. EXPECT_DECLS;
  21055. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21056. It was added after certifications */
  21057. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21058. const ecc_set_type* ecc_set;
  21059. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21060. /* Test for SECP256R1 curve */
  21061. int curve_id = ECC_SECP256R1;
  21062. int curve_idx;
  21063. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21064. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21065. ExpectIntEQ(ecc_set->id, curve_id);
  21066. #endif
  21067. /* Test case when SECP256R1 is not enabled */
  21068. /* Test that we get curve params for index 0 */
  21069. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21070. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21071. return EXPECT_RESULT();
  21072. }
  21073. /*
  21074. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21075. */
  21076. static int test_wc_ecc_signVerify_hash(void)
  21077. {
  21078. EXPECT_DECLS;
  21079. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21080. ecc_key key;
  21081. WC_RNG rng;
  21082. int ret;
  21083. #ifdef HAVE_ECC_VERIFY
  21084. int verify = 0;
  21085. #endif
  21086. word32 siglen = ECC_BUFSIZE;
  21087. byte sig[ECC_BUFSIZE];
  21088. byte adjustedSig[ECC_BUFSIZE+1];
  21089. byte digest[] = TEST_STRING;
  21090. word32 digestlen = (word32)TEST_STRING_SZ;
  21091. /* Init stack var */
  21092. XMEMSET(&key, 0, sizeof(ecc_key));
  21093. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21094. XMEMSET(sig, 0, siglen);
  21095. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21096. /* Init structs. */
  21097. ExpectIntEQ(wc_ecc_init(&key), 0);
  21098. ExpectIntEQ(wc_InitRng(&rng), 0);
  21099. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21100. #if defined(WOLFSSL_ASYNC_CRYPT)
  21101. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21102. #endif
  21103. ExpectIntEQ(ret, 0);
  21104. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21105. 0);
  21106. /* Check bad args. */
  21107. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21108. ECC_BAD_ARG_E);
  21109. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21110. ECC_BAD_ARG_E);
  21111. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21112. ECC_BAD_ARG_E);
  21113. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21114. ECC_BAD_ARG_E);
  21115. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21116. ECC_BAD_ARG_E);
  21117. #ifdef HAVE_ECC_VERIFY
  21118. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21119. &key), 0);
  21120. ExpectIntEQ(verify, 1);
  21121. /* test check on length of signature passed in */
  21122. XMEMCPY(adjustedSig, sig, siglen);
  21123. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21124. #ifndef NO_STRICT_ECDSA_LEN
  21125. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21126. &verify, &key), 0);
  21127. #else
  21128. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21129. * is allowed */
  21130. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21131. &verify, &key), 0);
  21132. #endif
  21133. /* Test bad args. */
  21134. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21135. &key), ECC_BAD_ARG_E);
  21136. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21137. ECC_BAD_ARG_E);
  21138. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21139. ECC_BAD_ARG_E);
  21140. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21141. NULL), ECC_BAD_ARG_E);
  21142. #endif /* HAVE_ECC_VERIFY */
  21143. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21144. wc_ecc_free(&key);
  21145. #ifdef FP_ECC
  21146. wc_ecc_fp_free();
  21147. #endif
  21148. #endif
  21149. return EXPECT_RESULT();
  21150. } /* END test_wc_ecc_sign_hash */
  21151. /*
  21152. * Testing wc_ecc_shared_secret()
  21153. */
  21154. static int test_wc_ecc_shared_secret(void)
  21155. {
  21156. EXPECT_DECLS;
  21157. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21158. ecc_key key;
  21159. ecc_key pubKey;
  21160. WC_RNG rng;
  21161. #if defined(NO_ECC256)
  21162. int ret;
  21163. #endif
  21164. byte out[KEY32];
  21165. int keySz = sizeof(out);
  21166. word32 outlen = (word32)sizeof(out);
  21167. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21168. const char* qx =
  21169. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21170. const char* qy =
  21171. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21172. const char* d =
  21173. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21174. const char* curveName = "SECP256R1";
  21175. const byte expected_shared_secret[] =
  21176. {
  21177. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21178. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21179. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21180. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21181. };
  21182. #endif
  21183. PRIVATE_KEY_UNLOCK();
  21184. /* Initialize variables. */
  21185. XMEMSET(&key, 0, sizeof(ecc_key));
  21186. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21187. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21188. XMEMSET(out, 0, keySz);
  21189. ExpectIntEQ(wc_ecc_init(&key), 0);
  21190. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21191. ExpectIntEQ(wc_InitRng(&rng), 0);
  21192. #if !defined(NO_ECC256)
  21193. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21194. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21195. #else
  21196. ret = wc_ecc_make_key(&rng, keySz, &key);
  21197. #if defined(WOLFSSL_ASYNC_CRYPT)
  21198. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21199. #endif
  21200. ExpectIntEQ(ret, 0);
  21201. ret = wc_ecc_make_key(&rng, keySz, &key);
  21202. #if defined(WOLFSSL_ASYNC_CRYPT)
  21203. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21204. #endif
  21205. ExpectIntEQ(ret, 0);
  21206. #endif
  21207. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21208. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21209. !defined(HAVE_SELFTEST)
  21210. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21211. #endif
  21212. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21213. #if !defined(NO_ECC256)
  21214. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21215. #endif
  21216. /* Test bad args. */
  21217. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21218. BAD_FUNC_ARG);
  21219. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21220. BAD_FUNC_ARG);
  21221. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21222. BAD_FUNC_ARG);
  21223. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21224. BAD_FUNC_ARG);
  21225. /* Invalid length */
  21226. outlen = 1;
  21227. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21228. BUFFER_E);
  21229. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21230. wc_ecc_free(&pubKey);
  21231. wc_ecc_free(&key);
  21232. #ifdef FP_ECC
  21233. wc_ecc_fp_free();
  21234. #endif
  21235. PRIVATE_KEY_LOCK();
  21236. #endif
  21237. return EXPECT_RESULT();
  21238. } /* END tests_wc_ecc_shared_secret */
  21239. /*
  21240. * testint wc_ecc_export_x963()
  21241. */
  21242. static int test_wc_ecc_export_x963(void)
  21243. {
  21244. EXPECT_DECLS;
  21245. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21246. ecc_key key;
  21247. WC_RNG rng;
  21248. byte out[ECC_ASN963_MAX_BUF_SZ];
  21249. word32 outlen = sizeof(out);
  21250. int ret;
  21251. PRIVATE_KEY_UNLOCK();
  21252. /* Initialize variables. */
  21253. XMEMSET(&key, 0, sizeof(ecc_key));
  21254. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21255. XMEMSET(out, 0, outlen);
  21256. ExpectIntEQ(wc_ecc_init(&key), 0);
  21257. ExpectIntEQ(wc_InitRng(&rng), 0);
  21258. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21259. #if defined(WOLFSSL_ASYNC_CRYPT)
  21260. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21261. #endif
  21262. ExpectIntEQ(ret, 0);
  21263. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21264. /* Test bad args. */
  21265. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21266. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21267. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21268. key.idx = -4;
  21269. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21270. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21271. wc_ecc_free(&key);
  21272. #ifdef FP_ECC
  21273. wc_ecc_fp_free();
  21274. #endif
  21275. PRIVATE_KEY_LOCK();
  21276. #endif
  21277. return EXPECT_RESULT();
  21278. } /* END test_wc_ecc_export_x963 */
  21279. /*
  21280. * Testing wc_ecc_export_x963_ex()
  21281. * compile with --enable-compkey will use compression.
  21282. */
  21283. static int test_wc_ecc_export_x963_ex(void)
  21284. {
  21285. EXPECT_DECLS;
  21286. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21287. ecc_key key;
  21288. WC_RNG rng;
  21289. int ret;
  21290. byte out[ECC_ASN963_MAX_BUF_SZ];
  21291. word32 outlen = sizeof(out);
  21292. #ifdef HAVE_COMP_KEY
  21293. word32 badOutLen = 5;
  21294. #endif
  21295. /* Init stack variables. */
  21296. XMEMSET(&key, 0, sizeof(ecc_key));
  21297. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21298. XMEMSET(out, 0, outlen);
  21299. ExpectIntEQ(wc_ecc_init(&key), 0);
  21300. ExpectIntEQ(wc_InitRng(&rng), 0);
  21301. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21302. #if defined(WOLFSSL_ASYNC_CRYPT)
  21303. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21304. #endif
  21305. ExpectIntEQ(ret, 0);
  21306. #ifdef HAVE_COMP_KEY
  21307. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21308. #else
  21309. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21310. #endif
  21311. /* Test bad args. */
  21312. #ifdef HAVE_COMP_KEY
  21313. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21314. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21315. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21316. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21317. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21318. #else
  21319. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21320. LENGTH_ONLY_E);
  21321. #endif
  21322. key.idx = -4;
  21323. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21324. #else
  21325. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21326. ECC_BAD_ARG_E);
  21327. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21328. LENGTH_ONLY_E);
  21329. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21330. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21331. ECC_BAD_ARG_E);
  21332. key.idx = -4;
  21333. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21334. ECC_BAD_ARG_E);
  21335. #endif
  21336. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21337. wc_ecc_free(&key);
  21338. #ifdef FP_ECC
  21339. wc_ecc_fp_free();
  21340. #endif
  21341. #endif
  21342. return EXPECT_RESULT();
  21343. } /* END test_wc_ecc_export_x963_ex */
  21344. /*
  21345. * testing wc_ecc_import_x963()
  21346. */
  21347. static int test_wc_ecc_import_x963(void)
  21348. {
  21349. EXPECT_DECLS;
  21350. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21351. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21352. ecc_key pubKey;
  21353. ecc_key key;
  21354. WC_RNG rng;
  21355. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21356. word32 x963Len = (word32)sizeof(x963);
  21357. int ret;
  21358. /* Init stack variables. */
  21359. XMEMSET(&key, 0, sizeof(ecc_key));
  21360. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21361. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21362. XMEMSET(x963, 0, x963Len);
  21363. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21364. ExpectIntEQ(wc_ecc_init(&key), 0);
  21365. ExpectIntEQ(wc_InitRng(&rng), 0);
  21366. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21367. #if defined(WOLFSSL_ASYNC_CRYPT)
  21368. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21369. #endif
  21370. ExpectIntEQ(ret, 0);
  21371. PRIVATE_KEY_UNLOCK();
  21372. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21373. PRIVATE_KEY_LOCK();
  21374. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21375. /* Test bad args. */
  21376. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21377. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21378. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21379. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21380. wc_ecc_free(&key);
  21381. wc_ecc_free(&pubKey);
  21382. #ifdef FP_ECC
  21383. wc_ecc_fp_free();
  21384. #endif
  21385. #endif
  21386. return EXPECT_RESULT();
  21387. } /* END wc_ecc_import_x963 */
  21388. /*
  21389. * testing wc_ecc_import_private_key()
  21390. */
  21391. static int test_wc_ecc_import_private_key(void)
  21392. {
  21393. EXPECT_DECLS;
  21394. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21395. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21396. ecc_key key;
  21397. ecc_key keyImp;
  21398. WC_RNG rng;
  21399. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21400. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21401. word32 privKeySz = (word32)sizeof(privKey);
  21402. word32 x963KeySz = (word32)sizeof(x963Key);
  21403. int ret;
  21404. /* Init stack variables. */
  21405. XMEMSET(&key, 0, sizeof(ecc_key));
  21406. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21407. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21408. XMEMSET(privKey, 0, privKeySz);
  21409. XMEMSET(x963Key, 0, x963KeySz);
  21410. ExpectIntEQ(wc_ecc_init(&key), 0);
  21411. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21412. ExpectIntEQ(wc_InitRng(&rng), 0);
  21413. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21414. #if defined(WOLFSSL_ASYNC_CRYPT)
  21415. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21416. #endif
  21417. ExpectIntEQ(ret, 0);
  21418. PRIVATE_KEY_UNLOCK();
  21419. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21420. PRIVATE_KEY_LOCK();
  21421. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21422. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21423. x963KeySz, &keyImp), 0);
  21424. /* Pass in bad args. */
  21425. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21426. x963KeySz, NULL), BAD_FUNC_ARG);
  21427. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21428. &keyImp), BAD_FUNC_ARG);
  21429. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21430. wc_ecc_free(&keyImp);
  21431. wc_ecc_free(&key);
  21432. #ifdef FP_ECC
  21433. wc_ecc_fp_free();
  21434. #endif
  21435. #endif
  21436. return EXPECT_RESULT();
  21437. } /* END test_wc_ecc_import_private_key */
  21438. /*
  21439. * Testing wc_ecc_export_private_only()
  21440. */
  21441. static int test_wc_ecc_export_private_only(void)
  21442. {
  21443. EXPECT_DECLS;
  21444. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21445. ecc_key key;
  21446. WC_RNG rng;
  21447. byte out[ECC_PRIV_KEY_BUF];
  21448. word32 outlen = sizeof(out);
  21449. int ret;
  21450. /* Init stack variables. */
  21451. XMEMSET(&key, 0, sizeof(ecc_key));
  21452. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21453. XMEMSET(out, 0, outlen);
  21454. ExpectIntEQ(wc_ecc_init(&key), 0);
  21455. ExpectIntEQ(wc_InitRng(&rng), 0);
  21456. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21457. #if defined(WOLFSSL_ASYNC_CRYPT)
  21458. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21459. #endif
  21460. ExpectIntEQ(ret, 0);
  21461. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  21462. /* Pass in bad args. */
  21463. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21464. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21465. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21466. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21467. wc_ecc_free(&key);
  21468. #ifdef FP_ECC
  21469. wc_ecc_fp_free();
  21470. #endif
  21471. #endif
  21472. return EXPECT_RESULT();
  21473. } /* END test_wc_ecc_export_private_only */
  21474. /*
  21475. * Testing wc_ecc_rs_to_sig()
  21476. */
  21477. static int test_wc_ecc_rs_to_sig(void)
  21478. {
  21479. EXPECT_DECLS;
  21480. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21481. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21482. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21483. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21484. const char* zeroStr = "0";
  21485. byte sig[ECC_MAX_SIG_SIZE];
  21486. word32 siglen = (word32)sizeof(sig);
  21487. /* R and S max size is the order of curve. 2^192.*/
  21488. int keySz = KEY24;
  21489. byte r[KEY24];
  21490. byte s[KEY24];
  21491. word32 rlen = (word32)sizeof(r);
  21492. word32 slen = (word32)sizeof(s);
  21493. /* Init stack variables. */
  21494. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21495. XMEMSET(r, 0, keySz);
  21496. XMEMSET(s, 0, keySz);
  21497. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21498. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21499. /* Test bad args. */
  21500. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21501. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21502. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21503. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21504. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21505. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21506. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21507. ECC_BAD_ARG_E);
  21508. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21509. ECC_BAD_ARG_E);
  21510. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21511. ECC_BAD_ARG_E);
  21512. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21513. ECC_BAD_ARG_E);
  21514. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21515. ECC_BAD_ARG_E);
  21516. #endif
  21517. return EXPECT_RESULT();
  21518. } /* END test_wc_ecc_rs_to_sig */
  21519. static int test_wc_ecc_import_raw(void)
  21520. {
  21521. EXPECT_DECLS;
  21522. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21523. ecc_key key;
  21524. const char* qx =
  21525. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21526. const char* qy =
  21527. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21528. const char* d =
  21529. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21530. const char* curveName = "SECP256R1";
  21531. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21532. const char* kNullStr = "";
  21533. int ret;
  21534. #endif
  21535. XMEMSET(&key, 0, sizeof(ecc_key));
  21536. ExpectIntEQ(wc_ecc_init(&key), 0);
  21537. /* Test good import */
  21538. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21539. /* Test bad args. */
  21540. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21541. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21542. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21543. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21544. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21545. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21546. wc_ecc_free(&key);
  21547. #endif
  21548. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21549. curveName), 0);
  21550. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21551. #endif
  21552. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21553. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21554. wc_ecc_free(&key);
  21555. #endif
  21556. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21557. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21558. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21559. #else
  21560. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21561. #endif
  21562. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21563. wc_ecc_free(&key);
  21564. #endif
  21565. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21566. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21567. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21568. #else
  21569. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21570. #endif
  21571. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21572. wc_ecc_free(&key);
  21573. #endif
  21574. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21575. #endif
  21576. wc_ecc_free(&key);
  21577. #endif
  21578. return EXPECT_RESULT();
  21579. } /* END test_wc_ecc_import_raw */
  21580. static int test_wc_ecc_import_unsigned(void)
  21581. {
  21582. EXPECT_DECLS;
  21583. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21584. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  21585. HAVE_FIPS_VERSION >= 2))
  21586. ecc_key key;
  21587. const byte qx[] = {
  21588. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21589. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21590. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21591. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21592. };
  21593. const byte qy[] = {
  21594. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21595. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21596. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21597. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21598. };
  21599. const byte d[] = {
  21600. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21601. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21602. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21603. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21604. };
  21605. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21606. const byte nullBytes[32] = {0};
  21607. int ret;
  21608. #endif
  21609. int curveId = ECC_SECP256R1;
  21610. XMEMSET(&key, 0, sizeof(ecc_key));
  21611. ExpectIntEQ(wc_ecc_init(&key), 0);
  21612. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21613. curveId), 0);
  21614. /* Test bad args. */
  21615. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21616. curveId), BAD_FUNC_ARG);
  21617. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21618. curveId), BAD_FUNC_ARG);
  21619. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21620. curveId), BAD_FUNC_ARG);
  21621. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21622. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  21623. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21624. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21625. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  21626. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21627. #endif
  21628. wc_ecc_free(&key);
  21629. #endif
  21630. return EXPECT_RESULT();
  21631. } /* END test_wc_ecc_import_unsigned */
  21632. /*
  21633. * Testing wc_ecc_sig_size()
  21634. */
  21635. static int test_wc_ecc_sig_size(void)
  21636. {
  21637. EXPECT_DECLS;
  21638. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21639. ecc_key key;
  21640. WC_RNG rng;
  21641. int keySz = KEY16;
  21642. int ret;
  21643. XMEMSET(&rng, 0, sizeof(rng));
  21644. XMEMSET(&key, 0, sizeof(key));
  21645. ExpectIntEQ(wc_ecc_init(&key), 0);
  21646. ExpectIntEQ(wc_InitRng(&rng), 0);
  21647. ret = wc_ecc_make_key(&rng, keySz, &key);
  21648. #if defined(WOLFSSL_ASYNC_CRYPT)
  21649. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21650. #endif
  21651. ExpectIntEQ(ret, 0);
  21652. ExpectIntLE(wc_ecc_sig_size(&key),
  21653. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  21654. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21655. wc_ecc_free(&key);
  21656. #endif
  21657. return EXPECT_RESULT();
  21658. } /* END test_wc_ecc_sig_size */
  21659. /*
  21660. * Testing wc_ecc_ctx_new()
  21661. */
  21662. static int test_wc_ecc_ctx_new(void)
  21663. {
  21664. EXPECT_DECLS;
  21665. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21666. WC_RNG rng;
  21667. ecEncCtx* cli = NULL;
  21668. ecEncCtx* srv = NULL;
  21669. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21670. ExpectIntEQ(wc_InitRng(&rng), 0);
  21671. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21672. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21673. wc_ecc_ctx_free(cli);
  21674. cli = NULL;
  21675. wc_ecc_ctx_free(srv);
  21676. /* Test bad args. */
  21677. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21678. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  21679. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  21680. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21681. wc_ecc_ctx_free(cli);
  21682. #endif
  21683. return EXPECT_RESULT();
  21684. } /* END test_wc_ecc_ctx_new */
  21685. /*
  21686. * Tesing wc_ecc_reset()
  21687. */
  21688. static int test_wc_ecc_ctx_reset(void)
  21689. {
  21690. EXPECT_DECLS;
  21691. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21692. ecEncCtx* ctx = NULL;
  21693. WC_RNG rng;
  21694. XMEMSET(&rng, 0, sizeof(rng));
  21695. ExpectIntEQ(wc_InitRng(&rng), 0);
  21696. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21697. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  21698. /* Pass in bad args. */
  21699. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  21700. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  21701. wc_ecc_ctx_free(ctx);
  21702. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21703. #endif
  21704. return EXPECT_RESULT();
  21705. } /* END test_wc_ecc_ctx_reset */
  21706. /*
  21707. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21708. */
  21709. static int test_wc_ecc_ctx_set_peer_salt(void)
  21710. {
  21711. EXPECT_DECLS;
  21712. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21713. WC_RNG rng;
  21714. ecEncCtx* cliCtx = NULL;
  21715. ecEncCtx* servCtx = NULL;
  21716. const byte* cliSalt = NULL;
  21717. const byte* servSalt = NULL;
  21718. XMEMSET(&rng, 0, sizeof(rng));
  21719. ExpectIntEQ(wc_InitRng(&rng), 0);
  21720. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21721. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21722. /* Test bad args. */
  21723. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  21724. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  21725. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  21726. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  21727. /* Test bad args. */
  21728. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  21729. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  21730. wc_ecc_ctx_free(cliCtx);
  21731. wc_ecc_ctx_free(servCtx);
  21732. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21733. #endif
  21734. return EXPECT_RESULT();
  21735. } /* END test_wc_ecc_ctx_set_peer_salt */
  21736. /*
  21737. * Testing wc_ecc_ctx_set_info()
  21738. */
  21739. static int test_wc_ecc_ctx_set_info(void)
  21740. {
  21741. EXPECT_DECLS;
  21742. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21743. ecEncCtx* ctx = NULL;
  21744. WC_RNG rng;
  21745. const char* optInfo = "Optional Test Info.";
  21746. int optInfoSz = (int)XSTRLEN(optInfo);
  21747. const char* badOptInfo = NULL;
  21748. XMEMSET(&rng, 0, sizeof(rng));
  21749. ExpectIntEQ(wc_InitRng(&rng), 0);
  21750. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21751. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  21752. /* Test bad args. */
  21753. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  21754. BAD_FUNC_ARG);
  21755. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  21756. BAD_FUNC_ARG);
  21757. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  21758. wc_ecc_ctx_free(ctx);
  21759. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21760. #endif
  21761. return EXPECT_RESULT();
  21762. } /* END test_wc_ecc_ctx_set_info */
  21763. /*
  21764. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  21765. */
  21766. static int test_wc_ecc_encryptDecrypt(void)
  21767. {
  21768. EXPECT_DECLS;
  21769. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  21770. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21771. ecc_key srvKey;
  21772. ecc_key cliKey;
  21773. ecc_key tmpKey;
  21774. WC_RNG rng;
  21775. int ret;
  21776. const char* msg = "EccBlock Size 16";
  21777. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  21778. #ifdef WOLFSSL_ECIES_OLD
  21779. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21780. #elif defined(WOLFSSL_ECIES_GEN_IV)
  21781. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  21782. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21783. #else
  21784. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  21785. WC_SHA256_DIGEST_SIZE];
  21786. #endif
  21787. word32 outSz = (word32)sizeof(out);
  21788. byte plain[sizeof("EccBlock Size 16")];
  21789. word32 plainSz = (word32)sizeof(plain);
  21790. int keySz = KEY20;
  21791. /* Init stack variables. */
  21792. XMEMSET(out, 0, outSz);
  21793. XMEMSET(plain, 0, plainSz);
  21794. XMEMSET(&rng, 0, sizeof(rng));
  21795. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  21796. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  21797. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  21798. ExpectIntEQ(wc_InitRng(&rng), 0);
  21799. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  21800. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  21801. #if defined(WOLFSSL_ASYNC_CRYPT)
  21802. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21803. #endif
  21804. ExpectIntEQ(ret, 0);
  21805. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  21806. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  21807. #if defined(WOLFSSL_ASYNC_CRYPT)
  21808. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21809. #endif
  21810. ExpectIntEQ(ret, 0);
  21811. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  21812. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21813. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21814. !defined(HAVE_SELFTEST)
  21815. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  21816. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  21817. #endif
  21818. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21819. &outSz, NULL), 0);
  21820. /* Test bad args. */
  21821. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  21822. NULL), BAD_FUNC_ARG);
  21823. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  21824. NULL), BAD_FUNC_ARG);
  21825. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  21826. NULL), BAD_FUNC_ARG);
  21827. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  21828. &outSz, NULL), BAD_FUNC_ARG);
  21829. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  21830. NULL), BAD_FUNC_ARG);
  21831. #ifdef WOLFSSL_ECIES_OLD
  21832. tmpKey.dp = cliKey.dp;
  21833. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  21834. #endif
  21835. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  21836. NULL), 0);
  21837. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  21838. NULL), BAD_FUNC_ARG);
  21839. #ifdef WOLFSSL_ECIES_OLD
  21840. /* NULL parameter allowed in new implementations - public key comes from
  21841. * the message. */
  21842. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  21843. NULL), BAD_FUNC_ARG);
  21844. #endif
  21845. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  21846. NULL), BAD_FUNC_ARG);
  21847. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  21848. NULL), BAD_FUNC_ARG);
  21849. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  21850. BAD_FUNC_ARG);
  21851. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  21852. wc_ecc_free(&tmpKey);
  21853. wc_ecc_free(&srvKey);
  21854. wc_ecc_free(&cliKey);
  21855. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21856. #endif
  21857. return EXPECT_RESULT();
  21858. } /* END test_wc_ecc_encryptDecrypt */
  21859. /*
  21860. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  21861. */
  21862. static int test_wc_ecc_del_point(void)
  21863. {
  21864. EXPECT_DECLS;
  21865. #if defined(HAVE_ECC)
  21866. ecc_point* pt = NULL;
  21867. ExpectNotNull(pt = wc_ecc_new_point());
  21868. wc_ecc_del_point(pt);
  21869. #endif
  21870. return EXPECT_RESULT();
  21871. } /* END test_wc_ecc_del_point */
  21872. /*
  21873. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  21874. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  21875. * and wc_ecc_cmp_point()
  21876. */
  21877. static int test_wc_ecc_pointFns(void)
  21878. {
  21879. EXPECT_DECLS;
  21880. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  21881. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21882. !defined(WOLFSSL_ATECC608A)
  21883. ecc_key key;
  21884. WC_RNG rng;
  21885. int ret;
  21886. ecc_point* point = NULL;
  21887. ecc_point* cpypt = NULL;
  21888. int idx = 0;
  21889. int keySz = KEY32;
  21890. byte der[DER_SZ(KEY32)];
  21891. word32 derlenChk = 0;
  21892. word32 derSz = DER_SZ(KEY32);
  21893. /* Init stack variables. */
  21894. XMEMSET(der, 0, derSz);
  21895. XMEMSET(&key, 0, sizeof(ecc_key));
  21896. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21897. ExpectIntEQ(wc_InitRng(&rng), 0);
  21898. ExpectIntEQ(wc_ecc_init(&key), 0);
  21899. ret = wc_ecc_make_key(&rng, keySz, &key);
  21900. #if defined(WOLFSSL_ASYNC_CRYPT)
  21901. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21902. #endif
  21903. ExpectIntEQ(ret, 0);
  21904. ExpectNotNull(point = wc_ecc_new_point());
  21905. ExpectNotNull(cpypt = wc_ecc_new_point());
  21906. /* Export */
  21907. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  21908. &derlenChk), LENGTH_ONLY_E);
  21909. /* Check length value. */
  21910. ExpectIntEQ(derSz, derlenChk);
  21911. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21912. &derSz), 0);
  21913. /* Test bad args. */
  21914. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  21915. ECC_BAD_ARG_E);
  21916. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  21917. ECC_BAD_ARG_E);
  21918. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21919. NULL), ECC_BAD_ARG_E);
  21920. /* Import */
  21921. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  21922. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  21923. /* Test bad args. */
  21924. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  21925. ECC_BAD_ARG_E);
  21926. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  21927. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  21928. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  21929. ECC_BAD_ARG_E);
  21930. /* Copy */
  21931. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  21932. /* Test bad args. */
  21933. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  21934. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  21935. /* Compare point */
  21936. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  21937. /* Test bad args. */
  21938. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  21939. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  21940. /* At infinity if return == 1, otherwise return == 0. */
  21941. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  21942. /* Test bad args. */
  21943. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  21944. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21945. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  21946. #ifdef USE_ECC_B_PARAM
  21947. /* On curve if ret == 0 */
  21948. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  21949. /* Test bad args. */
  21950. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  21951. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  21952. #endif /* USE_ECC_B_PARAM */
  21953. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  21954. /* Free */
  21955. wc_ecc_del_point(point);
  21956. wc_ecc_del_point(cpypt);
  21957. wc_ecc_free(&key);
  21958. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21959. #endif
  21960. return EXPECT_RESULT();
  21961. } /* END test_wc_ecc_pointFns */
  21962. /*
  21963. * Testing wc_ecc_shared_secret_ssh()
  21964. */
  21965. static int test_wc_ecc_shared_secret_ssh(void)
  21966. {
  21967. EXPECT_DECLS;
  21968. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  21969. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21970. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  21971. !defined(WOLFSSL_CRYPTOCELL)
  21972. ecc_key key;
  21973. ecc_key key2;
  21974. WC_RNG rng;
  21975. int ret;
  21976. int keySz = KEY32;
  21977. int key2Sz = KEY24;
  21978. byte secret[KEY32];
  21979. word32 secretLen = keySz;
  21980. /* Init stack variables. */
  21981. XMEMSET(&key, 0, sizeof(ecc_key));
  21982. XMEMSET(&key2, 0, sizeof(ecc_key));
  21983. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21984. XMEMSET(secret, 0, secretLen);
  21985. /* Make keys */
  21986. ExpectIntEQ(wc_ecc_init(&key), 0);
  21987. ExpectIntEQ(wc_InitRng(&rng), 0);
  21988. ret = wc_ecc_make_key(&rng, keySz, &key);
  21989. #if defined(WOLFSSL_ASYNC_CRYPT)
  21990. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21991. #endif
  21992. ExpectIntEQ(ret, 0);
  21993. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21994. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21995. ExpectIntEQ(wc_InitRng(&rng), 0);
  21996. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  21997. #if defined(WOLFSSL_ASYNC_CRYPT)
  21998. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  21999. #endif
  22000. ExpectIntEQ(ret, 0);
  22001. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22002. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22003. !defined(HAVE_SELFTEST)
  22004. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22005. #endif
  22006. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22007. &secretLen), 0);
  22008. /* Pass in bad args. */
  22009. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22010. &secretLen), BAD_FUNC_ARG);
  22011. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22012. BAD_FUNC_ARG);
  22013. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22014. BAD_FUNC_ARG);
  22015. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22016. BAD_FUNC_ARG);
  22017. key.type = ECC_PUBLICKEY;
  22018. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22019. &secretLen), ECC_BAD_ARG_E);
  22020. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22021. wc_ecc_free(&key);
  22022. wc_ecc_free(&key2);
  22023. #ifdef FP_ECC
  22024. wc_ecc_fp_free();
  22025. #endif
  22026. #endif
  22027. return EXPECT_RESULT();
  22028. } /* END test_wc_ecc_shared_secret_ssh */
  22029. /*
  22030. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22031. */
  22032. static int test_wc_ecc_verify_hash_ex(void)
  22033. {
  22034. EXPECT_DECLS;
  22035. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22036. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22037. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22038. ecc_key key;
  22039. WC_RNG rng;
  22040. int ret;
  22041. mp_int r;
  22042. mp_int s;
  22043. mp_int z;
  22044. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22045. unsigned char iHash[] = "Everyone gets Friday off.......";
  22046. unsigned char shortHash[] = TEST_STRING;
  22047. word32 hashlen = sizeof(hash);
  22048. word32 iHashLen = sizeof(iHash);
  22049. word32 shortHashLen = sizeof(shortHash);
  22050. int keySz = KEY32;
  22051. int verify_ok = 0;
  22052. XMEMSET(&key, 0, sizeof(ecc_key));
  22053. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22054. XMEMSET(&r, 0, sizeof(mp_int));
  22055. XMEMSET(&s, 0, sizeof(mp_int));
  22056. XMEMSET(&z, 0, sizeof(mp_int));
  22057. /* Initialize r, s and z. */
  22058. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22059. ExpectIntEQ(wc_ecc_init(&key), 0);
  22060. ExpectIntEQ(wc_InitRng(&rng), 0);
  22061. ret = wc_ecc_make_key(&rng, keySz, &key);
  22062. #if defined(WOLFSSL_ASYNC_CRYPT)
  22063. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22064. #endif
  22065. ExpectIntEQ(ret, 0);
  22066. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22067. /* verify_ok should be 1. */
  22068. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22069. 0);
  22070. ExpectIntEQ(verify_ok, 1);
  22071. /* verify_ok should be 0 */
  22072. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22073. &key), 0);
  22074. ExpectIntEQ(verify_ok, 0);
  22075. /* verify_ok should be 0. */
  22076. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22077. &verify_ok, &key), 0);
  22078. ExpectIntEQ(verify_ok, 0);
  22079. /* Test bad args. */
  22080. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22081. ECC_BAD_ARG_E);
  22082. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22083. ECC_BAD_ARG_E);
  22084. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22085. ECC_BAD_ARG_E);
  22086. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22087. ECC_BAD_ARG_E);
  22088. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22089. ECC_BAD_ARG_E);
  22090. /* Test bad args. */
  22091. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22092. &verify_ok, &key), ECC_BAD_ARG_E);
  22093. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22094. &verify_ok, &key), ECC_BAD_ARG_E);
  22095. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22096. &verify_ok, &key), MP_ZERO_E);
  22097. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22098. &verify_ok, &key), MP_ZERO_E);
  22099. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22100. &verify_ok, &key), MP_ZERO_E);
  22101. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22102. &key), ECC_BAD_ARG_E);
  22103. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22104. &key), ECC_BAD_ARG_E);
  22105. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22106. &verify_ok, NULL), ECC_BAD_ARG_E);
  22107. wc_ecc_free(&key);
  22108. mp_free(&r);
  22109. mp_free(&s);
  22110. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22111. #endif
  22112. return EXPECT_RESULT();
  22113. } /* END test_wc_ecc_verify_hash_ex */
  22114. /*
  22115. * Testing wc_ecc_mulmod()
  22116. */
  22117. static int test_wc_ecc_mulmod(void)
  22118. {
  22119. EXPECT_DECLS;
  22120. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22121. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22122. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22123. ecc_key key1;
  22124. ecc_key key2;
  22125. ecc_key key3;
  22126. WC_RNG rng;
  22127. int ret;
  22128. XMEMSET(&key1, 0, sizeof(ecc_key));
  22129. XMEMSET(&key2, 0, sizeof(ecc_key));
  22130. XMEMSET(&key3, 0, sizeof(ecc_key));
  22131. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22132. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22133. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22134. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22135. ExpectIntEQ(wc_InitRng(&rng), 0);
  22136. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22137. #if defined(WOLFSSL_ASYNC_CRYPT)
  22138. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22139. #endif
  22140. ExpectIntEQ(ret, 0);
  22141. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22142. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22143. key1.dp->Af, ECC_SECP256R1), 0);
  22144. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22145. key1.dp->prime, ECC_SECP256R1), 0);
  22146. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22147. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22148. 1), 0);
  22149. /* Test bad args. */
  22150. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22151. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22152. ECC_BAD_ARG_E);
  22153. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22154. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22155. ECC_BAD_ARG_E);
  22156. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22157. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22158. ECC_BAD_ARG_E);
  22159. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22160. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22161. wc_ecc_free(&key1);
  22162. wc_ecc_free(&key2);
  22163. wc_ecc_free(&key3);
  22164. #ifdef FP_ECC
  22165. wc_ecc_fp_free();
  22166. #endif
  22167. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22168. return EXPECT_RESULT();
  22169. } /* END test_wc_ecc_mulmod */
  22170. /*
  22171. * Testing wc_ecc_is_valid_idx()
  22172. */
  22173. static int test_wc_ecc_is_valid_idx(void)
  22174. {
  22175. EXPECT_DECLS;
  22176. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22177. ecc_key key;
  22178. WC_RNG rng;
  22179. int ret;
  22180. int iVal = -2;
  22181. int iVal2 = 3000;
  22182. XMEMSET(&key, 0, sizeof(ecc_key));
  22183. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22184. ExpectIntEQ(wc_ecc_init(&key), 0);
  22185. ExpectIntEQ(wc_InitRng(&rng), 0);
  22186. ret = wc_ecc_make_key(&rng, 32, &key);
  22187. #if defined(WOLFSSL_ASYNC_CRYPT)
  22188. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22189. #endif
  22190. ExpectIntEQ(ret, 0);
  22191. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22192. /* Test bad args. */
  22193. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22194. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22195. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22196. wc_ecc_free(&key);
  22197. #ifdef FP_ECC
  22198. wc_ecc_fp_free();
  22199. #endif
  22200. #endif
  22201. return EXPECT_RESULT();
  22202. } /* END test_wc_ecc_is_valid_idx */
  22203. /*
  22204. * Testing wc_ecc_get_curve_id_from_oid()
  22205. */
  22206. static int test_wc_ecc_get_curve_id_from_oid(void)
  22207. {
  22208. EXPECT_DECLS;
  22209. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22210. !defined(HAVE_FIPS)
  22211. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22212. word32 len = sizeof(oid);
  22213. /* Bad Cases */
  22214. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22215. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22216. /* Good Case */
  22217. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22218. #endif
  22219. return EXPECT_RESULT();
  22220. } /* END test_wc_ecc_get_curve_id_from_oid */
  22221. /*
  22222. * Testing wc_ecc_sig_size_calc()
  22223. */
  22224. static int test_wc_ecc_sig_size_calc(void)
  22225. {
  22226. EXPECT_DECLS;
  22227. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22228. ecc_key key;
  22229. WC_RNG rng;
  22230. int sz = 0;
  22231. int ret;
  22232. XMEMSET(&key, 0, sizeof(ecc_key));
  22233. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22234. ExpectIntEQ(wc_ecc_init(&key), 0);
  22235. ExpectIntEQ(wc_InitRng(&rng), 0);
  22236. ret = wc_ecc_make_key(&rng, 16, &key);
  22237. #if defined(WOLFSSL_ASYNC_CRYPT)
  22238. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22239. #endif
  22240. ExpectIntEQ(ret, 0);
  22241. sz = key.dp->size;
  22242. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22243. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22244. wc_ecc_free(&key);
  22245. #endif
  22246. return EXPECT_RESULT();
  22247. } /* END test_wc_ecc_sig_size_calc */
  22248. /*
  22249. * Testing wc_ecc_sm2_make_key()
  22250. */
  22251. static int test_wc_ecc_sm2_make_key(void)
  22252. {
  22253. int res = TEST_SKIPPED;
  22254. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22255. EXPECT_DECLS;
  22256. WC_RNG rng[1];
  22257. ecc_key key[1];
  22258. XMEMSET(rng, 0, sizeof(*rng));
  22259. XMEMSET(key, 0, sizeof(*key));
  22260. ExpectIntEQ(wc_InitRng(rng), 0);
  22261. ExpectIntEQ(wc_ecc_init(key), 0);
  22262. /* Test invalid parameters. */
  22263. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22264. BAD_FUNC_ARG);
  22265. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22266. BAD_FUNC_ARG);
  22267. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22268. BAD_FUNC_ARG);
  22269. /* Test valid parameters. */
  22270. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22271. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22272. wc_ecc_free(key);
  22273. wc_FreeRng(rng);
  22274. #ifdef FP_ECC
  22275. wc_ecc_fp_free();
  22276. #endif
  22277. res = EXPECT_RESULT();
  22278. #endif
  22279. return res;
  22280. }
  22281. /*
  22282. * Testing wc_ecc_sm2_shared_secret()
  22283. */
  22284. static int test_wc_ecc_sm2_shared_secret(void)
  22285. {
  22286. int res = TEST_SKIPPED;
  22287. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22288. EXPECT_DECLS;
  22289. WC_RNG rng[1];
  22290. ecc_key keyA[1];
  22291. ecc_key keyB[1];
  22292. byte outA[32];
  22293. byte outB[32];
  22294. word32 outALen = 32;
  22295. word32 outBLen = 32;
  22296. XMEMSET(rng, 0, sizeof(*rng));
  22297. XMEMSET(keyA, 0, sizeof(*keyA));
  22298. XMEMSET(keyB, 0, sizeof(*keyB));
  22299. ExpectIntEQ(wc_InitRng(rng), 0);
  22300. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22301. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22302. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22303. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22304. #ifdef ECC_TIMING_RESISTANT
  22305. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22306. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22307. #endif
  22308. /* Test invalid parameters. */
  22309. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22310. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22311. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22312. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22313. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22314. BAD_FUNC_ARG);
  22315. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22316. BAD_FUNC_ARG);
  22317. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22318. BAD_FUNC_ARG);
  22319. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22320. BAD_FUNC_ARG);
  22321. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22322. /* Test valid parameters. */
  22323. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22324. ExpectIntLE(outALen, 32);
  22325. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22326. ExpectIntLE(outBLen, 32);
  22327. ExpectIntEQ(outALen, outBLen);
  22328. ExpectBufEQ(outA, outB, outALen);
  22329. wc_ecc_free(keyB);
  22330. wc_ecc_free(keyA);
  22331. wc_FreeRng(rng);
  22332. #ifdef FP_ECC
  22333. wc_ecc_fp_free();
  22334. #endif
  22335. res = EXPECT_RESULT();
  22336. #endif
  22337. return res;
  22338. }
  22339. /*
  22340. * Testing wc_ecc_sm2_create_digest()
  22341. */
  22342. static int test_wc_ecc_sm2_create_digest(void)
  22343. {
  22344. int res = TEST_SKIPPED;
  22345. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22346. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22347. EXPECT_DECLS;
  22348. ecc_key key[1];
  22349. enum wc_HashType hashType;
  22350. unsigned char pub[] = {
  22351. 0x04,
  22352. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22353. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22354. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22355. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22356. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22357. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22358. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22359. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22360. };
  22361. unsigned char id[] = {
  22362. 0x01, 0x02, 0x03,
  22363. };
  22364. unsigned char msg[] = {
  22365. 0x01, 0x02, 0x03,
  22366. };
  22367. unsigned char hash[32];
  22368. #ifdef WOLFSSL_SM3
  22369. unsigned char expHash[32] = {
  22370. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22371. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22372. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22373. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22374. };
  22375. #else
  22376. unsigned char expHash[32] = {
  22377. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22378. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22379. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22380. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22381. };
  22382. #endif
  22383. #ifdef WOLFSSL_SM3
  22384. hashType = WC_HASH_TYPE_SM3;
  22385. #else
  22386. hashType = WC_HASH_TYPE_SHA256;
  22387. #endif
  22388. XMEMSET(key, 0, sizeof(*key));
  22389. ExpectIntEQ(wc_ecc_init(key), 0);
  22390. /* Test with no curve set. */
  22391. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22392. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22393. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22394. /* Test invalid parameters. */
  22395. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22396. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22397. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22398. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22399. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22400. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22401. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22402. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22403. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22404. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22405. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22406. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22407. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22408. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22409. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22410. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22411. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22412. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22413. /* Bad hash type. */
  22414. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22415. -1, hash, 0, key), BAD_FUNC_ARG);
  22416. /* Bad hash size. */
  22417. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22418. hashType, hash, 0, key), BUFFER_E);
  22419. /* Test valid parameters. */
  22420. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22421. hashType, hash, sizeof(hash), key), 0);
  22422. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22423. wc_ecc_free(key);
  22424. res = EXPECT_RESULT();
  22425. #endif
  22426. return res;
  22427. }
  22428. /*
  22429. * Testing wc_ecc_sm2_verify_hash_ex()
  22430. */
  22431. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22432. {
  22433. int res = TEST_SKIPPED;
  22434. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22435. defined(WOLFSSL_PUBLIC_MP)
  22436. EXPECT_DECLS;
  22437. ecc_key key[1];
  22438. mp_int r[1];
  22439. mp_int s[1];
  22440. int verified;
  22441. unsigned char pub[] = {
  22442. 0x04,
  22443. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22444. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22445. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22446. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22447. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22448. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22449. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22450. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22451. };
  22452. unsigned char hash[] = {
  22453. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22454. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22455. 0x50, 0x69, 0x5B, 0x20
  22456. };
  22457. unsigned char rData[] = {
  22458. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22459. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22460. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22461. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  22462. };
  22463. unsigned char sData[] = {
  22464. 0x1D,
  22465. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22466. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22467. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22468. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22469. };
  22470. unsigned char rBadData[] = {
  22471. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22472. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22473. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22474. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22475. };
  22476. XMEMSET(key, 0, sizeof(*key));
  22477. XMEMSET(r, 0, sizeof(*r));
  22478. XMEMSET(s, 0, sizeof(*s));
  22479. ExpectIntEQ(mp_init(r), 0);
  22480. ExpectIntEQ(mp_init(s), 0);
  22481. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22482. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22483. ExpectIntEQ(wc_ecc_init(key), 0);
  22484. /* Test with no curve set. */
  22485. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22486. &verified, key), BAD_FUNC_ARG);
  22487. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22488. /* Test invalid parameters. */
  22489. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22490. NULL, NULL), BAD_FUNC_ARG);
  22491. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22492. NULL, NULL), BAD_FUNC_ARG);
  22493. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22494. NULL, NULL), BAD_FUNC_ARG);
  22495. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22496. NULL, NULL), BAD_FUNC_ARG);
  22497. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22498. &verified, NULL), BAD_FUNC_ARG);
  22499. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22500. NULL, key), BAD_FUNC_ARG);
  22501. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22502. &verified, key), BAD_FUNC_ARG);
  22503. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22504. &verified, key), BAD_FUNC_ARG);
  22505. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22506. &verified, key), BAD_FUNC_ARG);
  22507. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22508. NULL, key), BAD_FUNC_ARG);
  22509. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22510. &verified, NULL), BAD_FUNC_ARG);
  22511. /* Make key not on the SM2 curve. */
  22512. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22513. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22514. &verified, key), BAD_FUNC_ARG);
  22515. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22516. /* Test valid parameters. */
  22517. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22518. &verified, key), 0);
  22519. ExpectIntEQ(verified, 1);
  22520. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22521. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22522. &verified, key), 0);
  22523. ExpectIntEQ(verified, 0);
  22524. mp_free(s);
  22525. mp_free(r);
  22526. wc_ecc_free(key);
  22527. #ifdef FP_ECC
  22528. wc_ecc_fp_free();
  22529. #endif
  22530. res = EXPECT_RESULT();
  22531. #endif
  22532. return res;
  22533. }
  22534. /*
  22535. * Testing wc_ecc_sm2_verify_hash()
  22536. */
  22537. static int test_wc_ecc_sm2_verify_hash(void)
  22538. {
  22539. int res = TEST_SKIPPED;
  22540. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22541. EXPECT_DECLS;
  22542. ecc_key key[1];
  22543. int verified;
  22544. unsigned char pub[] = {
  22545. 0x04,
  22546. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22547. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22548. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22549. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22550. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22551. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22552. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22553. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22554. };
  22555. unsigned char hash[] = {
  22556. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22557. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22558. 0x50, 0x69, 0x5B, 0x20
  22559. };
  22560. unsigned char sig[] = {
  22561. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22562. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22563. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22564. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22565. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22566. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22567. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22568. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22569. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22570. };
  22571. unsigned char sigBad[] = {
  22572. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22573. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22574. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22575. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22576. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22577. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22578. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22579. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22580. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  22581. };
  22582. XMEMSET(key, 0, sizeof(*key));
  22583. ExpectIntEQ(wc_ecc_init(key), 0);
  22584. /* Test with no curve set. */
  22585. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22586. &verified, key), BAD_FUNC_ARG);
  22587. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22588. /* Test invalid parameters. */
  22589. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22590. NULL, NULL), BAD_FUNC_ARG);
  22591. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22592. NULL, NULL), BAD_FUNC_ARG);
  22593. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22594. NULL, NULL), BAD_FUNC_ARG);
  22595. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22596. &verified, NULL), BAD_FUNC_ARG);
  22597. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22598. NULL, key), BAD_FUNC_ARG);
  22599. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22600. &verified, key), BAD_FUNC_ARG);
  22601. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22602. &verified, key), BAD_FUNC_ARG);
  22603. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22604. NULL, key), BAD_FUNC_ARG);
  22605. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22606. &verified, NULL), BAD_FUNC_ARG);
  22607. /* Make key not on the SM2 curve. */
  22608. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22609. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22610. &verified, key), BAD_FUNC_ARG);
  22611. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22612. /* Test valid parameters. */
  22613. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22614. &verified, key), 0);
  22615. ExpectIntEQ(verified, 1);
  22616. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  22617. sizeof(hash), &verified, key), 0);
  22618. ExpectIntEQ(verified, 0);
  22619. wc_ecc_free(key);
  22620. #ifdef FP_ECC
  22621. wc_ecc_fp_free();
  22622. #endif
  22623. res = EXPECT_RESULT();
  22624. #endif
  22625. return res;
  22626. }
  22627. /*
  22628. * Testing wc_ecc_sm2_verify_hash_ex()
  22629. */
  22630. static int test_wc_ecc_sm2_sign_hash_ex(void)
  22631. {
  22632. int res = TEST_SKIPPED;
  22633. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  22634. defined(WOLFSSL_PUBLIC_MP)
  22635. EXPECT_DECLS;
  22636. WC_RNG rng[1];
  22637. ecc_key key[1];
  22638. mp_int r[1];
  22639. mp_int s[1];
  22640. unsigned char hash[32];
  22641. #ifdef HAVE_ECC_VERIFY
  22642. int verified;
  22643. #endif
  22644. XMEMSET(rng, 0, sizeof(*rng));
  22645. XMEMSET(key, 0, sizeof(*key));
  22646. XMEMSET(r, 0, sizeof(*r));
  22647. XMEMSET(s, 0, sizeof(*s));
  22648. ExpectIntEQ(wc_InitRng(rng), 0);
  22649. ExpectIntEQ(mp_init(r), 0);
  22650. ExpectIntEQ(mp_init(s), 0);
  22651. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22652. ExpectIntEQ(wc_ecc_init(key), 0);
  22653. /* Test with no curve set. */
  22654. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22655. BAD_FUNC_ARG);
  22656. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22657. /* Test invalid parameters. */
  22658. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22659. NULL), BAD_FUNC_ARG);
  22660. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  22661. NULL), BAD_FUNC_ARG);
  22662. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  22663. NULL), BAD_FUNC_ARG);
  22664. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  22665. NULL), BAD_FUNC_ARG);
  22666. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  22667. NULL), BAD_FUNC_ARG);
  22668. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22669. s), BAD_FUNC_ARG);
  22670. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  22671. BAD_FUNC_ARG);
  22672. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  22673. BAD_FUNC_ARG);
  22674. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  22675. BAD_FUNC_ARG);
  22676. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  22677. BAD_FUNC_ARG);
  22678. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  22679. BAD_FUNC_ARG);
  22680. /* Make key not on the SM2 curve. */
  22681. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22682. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22683. BAD_FUNC_ARG);
  22684. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22685. #ifdef WOLFSSL_SP_MATH_ALL
  22686. {
  22687. mp_int smallR[1];
  22688. sp_init_size(smallR, 1);
  22689. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  22690. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  22691. smallR, s), 0);
  22692. }
  22693. #endif
  22694. /* Test valid parameters. */
  22695. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22696. 0);
  22697. #ifdef HAVE_ECC_VERIFY
  22698. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  22699. key), 0);
  22700. ExpectIntEQ(verified, 1);
  22701. #endif
  22702. mp_free(s);
  22703. mp_free(r);
  22704. wc_ecc_free(key);
  22705. wc_FreeRng(rng);
  22706. #ifdef FP_ECC
  22707. wc_ecc_fp_free();
  22708. #endif
  22709. res = EXPECT_RESULT();
  22710. #endif
  22711. return res;
  22712. }
  22713. /*
  22714. * Testing wc_ecc_sm2_verify_hash()
  22715. */
  22716. static int test_wc_ecc_sm2_sign_hash(void)
  22717. {
  22718. int res = TEST_SKIPPED;
  22719. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  22720. EXPECT_DECLS;
  22721. WC_RNG rng[1];
  22722. ecc_key key[1];
  22723. unsigned char hash[32];
  22724. unsigned char sig[72];
  22725. word32 sigSz = sizeof(sig);
  22726. #ifdef HAVE_ECC_VERIFY
  22727. int verified;
  22728. #endif
  22729. XMEMSET(rng, 0, sizeof(*rng));
  22730. XMEMSET(key, 0, sizeof(*key));
  22731. ExpectIntEQ(wc_InitRng(rng), 0);
  22732. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22733. ExpectIntEQ(wc_ecc_init(key), 0);
  22734. /* Test with no curve set. */
  22735. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22736. BAD_FUNC_ARG);
  22737. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22738. /* Test invalid parameters. */
  22739. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22740. NULL), BAD_FUNC_ARG);
  22741. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  22742. NULL), BAD_FUNC_ARG);
  22743. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  22744. NULL), BAD_FUNC_ARG);
  22745. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  22746. NULL), BAD_FUNC_ARG);
  22747. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  22748. NULL), BAD_FUNC_ARG);
  22749. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22750. key), BAD_FUNC_ARG);
  22751. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  22752. key), BAD_FUNC_ARG);
  22753. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  22754. key), BAD_FUNC_ARG);
  22755. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  22756. key), BAD_FUNC_ARG);
  22757. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  22758. key), BAD_FUNC_ARG);
  22759. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  22760. NULL), BAD_FUNC_ARG);
  22761. /* Make key not on the SM2 curve. */
  22762. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22763. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22764. BAD_FUNC_ARG);
  22765. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22766. /* Test valid parameters. */
  22767. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22768. 0);
  22769. #ifdef HAVE_ECC_VERIFY
  22770. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  22771. &verified, key), 0);
  22772. ExpectIntEQ(verified, 1);
  22773. #endif
  22774. wc_ecc_free(key);
  22775. wc_FreeRng(rng);
  22776. #ifdef FP_ECC
  22777. wc_ecc_fp_free();
  22778. #endif
  22779. res = EXPECT_RESULT();
  22780. #endif
  22781. return res;
  22782. }
  22783. /*
  22784. * Testing ToTraditional
  22785. */
  22786. static int test_ToTraditional(void)
  22787. {
  22788. EXPECT_DECLS;
  22789. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22790. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22791. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  22792. XFILE f = XBADFILE;
  22793. byte input[TWOK_BUF];
  22794. word32 sz = 0;
  22795. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  22796. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  22797. if (f != XBADFILE)
  22798. XFCLOSE(f);
  22799. /* Good case */
  22800. ExpectIntGT(ToTraditional(input, sz), 0);
  22801. /* Bad cases */
  22802. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  22803. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  22804. #ifdef WOLFSSL_ASN_TEMPLATE
  22805. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  22806. #else
  22807. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  22808. #endif
  22809. #endif
  22810. return EXPECT_RESULT();
  22811. } /* End test_ToTraditional*/
  22812. /*
  22813. * Testing wc_EccPrivateKeyToDer
  22814. */
  22815. static int test_wc_EccPrivateKeyToDer(void)
  22816. {
  22817. EXPECT_DECLS;
  22818. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22819. byte output[ONEK_BUF];
  22820. ecc_key eccKey;
  22821. WC_RNG rng;
  22822. word32 inLen;
  22823. word32 outLen = 0;
  22824. int ret;
  22825. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22826. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22827. ExpectIntEQ(wc_InitRng(&rng), 0);
  22828. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22829. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22830. #if defined(WOLFSSL_ASYNC_CRYPT)
  22831. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22832. #endif
  22833. ExpectIntEQ(ret, 0);
  22834. inLen = (word32)sizeof(output);
  22835. /* Bad Cases */
  22836. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22837. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22838. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  22839. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  22840. /* Good Case */
  22841. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  22842. wc_ecc_free(&eccKey);
  22843. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22844. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  22845. {
  22846. /* test importing private only into a PKEY struct */
  22847. EC_KEY* ec = NULL;
  22848. EVP_PKEY* pkey = NULL;
  22849. const unsigned char* der;
  22850. der = output;
  22851. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  22852. der = output;
  22853. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  22854. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  22855. if (EXPECT_FAIL()) {
  22856. EC_KEY_free(ec);
  22857. }
  22858. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  22859. }
  22860. #endif
  22861. #endif
  22862. return EXPECT_RESULT();
  22863. } /* End test_wc_EccPrivateKeyToDer*/
  22864. /*
  22865. * Testing wc_DhPublicKeyDecode
  22866. */
  22867. static int test_wc_DhPublicKeyDecode(void)
  22868. {
  22869. EXPECT_DECLS;
  22870. #ifndef NO_DH
  22871. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22872. DhKey key;
  22873. word32 inOutIdx;
  22874. XMEMSET(&key, 0, sizeof(DhKey));
  22875. ExpectIntEQ(wc_InitDhKey(&key), 0);
  22876. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  22877. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22878. BAD_FUNC_ARG);
  22879. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22880. BAD_FUNC_ARG);
  22881. inOutIdx = 0;
  22882. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22883. BAD_FUNC_ARG);
  22884. inOutIdx = 0;
  22885. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22886. BAD_FUNC_ARG);
  22887. inOutIdx = 0;
  22888. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22889. sizeof_dh_pub_key_der_2048), 0);
  22890. ExpectIntNE(key.p.used, 0);
  22891. ExpectIntNE(key.g.used, 0);
  22892. ExpectIntEQ(key.q.used, 0);
  22893. ExpectIntNE(key.pub.used, 0);
  22894. ExpectIntEQ(key.priv.used, 0);
  22895. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  22896. #endif
  22897. #endif /* !NO_DH */
  22898. return EXPECT_RESULT();
  22899. }
  22900. /*
  22901. * Testing wc_Ed25519KeyToDer
  22902. */
  22903. static int test_wc_Ed25519KeyToDer(void)
  22904. {
  22905. EXPECT_DECLS;
  22906. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22907. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22908. byte output[ONEK_BUF];
  22909. ed25519_key ed25519Key;
  22910. WC_RNG rng;
  22911. word32 inLen;
  22912. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22913. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22914. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22915. ExpectIntEQ(wc_InitRng(&rng), 0);
  22916. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22917. inLen = (word32)sizeof(output);
  22918. /* Bad Cases */
  22919. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22920. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22921. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  22922. /* Good Cases */
  22923. /* length only */
  22924. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  22925. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  22926. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22927. wc_ed25519_free(&ed25519Key);
  22928. #endif
  22929. return EXPECT_RESULT();
  22930. } /* End test_wc_Ed25519KeyToDer*/
  22931. /*
  22932. * Testing wc_Ed25519PrivateKeyToDer
  22933. */
  22934. static int test_wc_Ed25519PrivateKeyToDer(void)
  22935. {
  22936. EXPECT_DECLS;
  22937. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22938. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22939. byte output[ONEK_BUF];
  22940. ed25519_key ed25519PrivKey;
  22941. WC_RNG rng;
  22942. word32 inLen;
  22943. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  22944. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22945. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  22946. ExpectIntEQ(wc_InitRng(&rng), 0);
  22947. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  22948. 0);
  22949. inLen = (word32)sizeof(output);
  22950. /* Bad Cases */
  22951. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22952. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22953. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  22954. BAD_FUNC_ARG);
  22955. /* Good Cases */
  22956. /* length only */
  22957. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  22958. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  22959. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22960. wc_ed25519_free(&ed25519PrivKey);
  22961. #endif
  22962. return EXPECT_RESULT();
  22963. } /* End test_wc_Ed25519PrivateKeyToDer*/
  22964. /*
  22965. * Testing wc_Ed448KeyToDer
  22966. */
  22967. static int test_wc_Ed448KeyToDer(void)
  22968. {
  22969. EXPECT_DECLS;
  22970. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22971. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22972. byte output[ONEK_BUF];
  22973. ed448_key ed448Key;
  22974. WC_RNG rng;
  22975. word32 inLen;
  22976. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22977. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22978. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22979. ExpectIntEQ(wc_InitRng(&rng), 0);
  22980. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22981. inLen = (word32)sizeof(output);
  22982. /* Bad Cases */
  22983. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22984. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22985. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  22986. /* Good Cases */
  22987. /* length only */
  22988. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  22989. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  22990. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22991. wc_ed448_free(&ed448Key);
  22992. #endif
  22993. return EXPECT_RESULT();
  22994. } /* End test_wc_Ed448KeyToDer*/
  22995. /*
  22996. * Testing wc_Ed448PrivateKeyToDer
  22997. */
  22998. static int test_wc_Ed448PrivateKeyToDer(void)
  22999. {
  23000. EXPECT_DECLS;
  23001. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23002. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23003. byte output[ONEK_BUF];
  23004. ed448_key ed448PrivKey;
  23005. WC_RNG rng;
  23006. word32 inLen;
  23007. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23008. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23009. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23010. ExpectIntEQ(wc_InitRng(&rng), 0);
  23011. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23012. 0);
  23013. inLen = (word32)sizeof(output);
  23014. /* Bad Cases */
  23015. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23016. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23017. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23018. BAD_FUNC_ARG);
  23019. /* Good cases */
  23020. /* length only */
  23021. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23022. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23023. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23024. wc_ed448_free(&ed448PrivKey);
  23025. #endif
  23026. return EXPECT_RESULT();
  23027. } /* End test_wc_Ed448PrivateKeyToDer*/
  23028. /*
  23029. * Testing wc_SetSubjectBuffer
  23030. */
  23031. static int test_wc_SetSubjectBuffer(void)
  23032. {
  23033. EXPECT_DECLS;
  23034. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23035. Cert cert;
  23036. XFILE file = XBADFILE;
  23037. byte* der = NULL;
  23038. word32 derSz;
  23039. derSz = FOURK_BUF;
  23040. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23041. DYNAMIC_TYPE_TMP_BUFFER));
  23042. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23043. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23044. if (file != XBADFILE)
  23045. XFCLOSE(file);
  23046. ExpectIntEQ(wc_InitCert(&cert), 0);
  23047. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  23048. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  23049. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23050. #endif
  23051. return EXPECT_RESULT();
  23052. } /* End test_wc_SetSubjectBuffer*/
  23053. /*
  23054. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23055. */
  23056. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23057. {
  23058. EXPECT_DECLS;
  23059. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23060. WC_RNG rng;
  23061. Cert cert;
  23062. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23063. RsaKey rsaKey;
  23064. int bits = 2048;
  23065. #endif
  23066. #if defined(HAVE_ECC)
  23067. ecc_key eccKey;
  23068. int ret;
  23069. #endif
  23070. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23071. ed25519_key ed25519Key;
  23072. #endif
  23073. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23074. ed448_key ed448Key;
  23075. #endif
  23076. #ifndef HAVE_FIPS
  23077. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23078. #else
  23079. ExpectIntEQ(wc_InitRng(&rng), 0);
  23080. #endif
  23081. ExpectIntEQ(wc_InitCert(&cert), 0);
  23082. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23083. /* RSA */
  23084. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23085. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23086. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23087. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23088. 0);
  23089. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23090. #endif
  23091. #if defined(HAVE_ECC)
  23092. /* ECC */
  23093. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23094. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23095. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23096. #if defined(WOLFSSL_ASYNC_CRYPT)
  23097. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23098. #endif
  23099. ExpectIntEQ(ret, 0);
  23100. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23101. 0);
  23102. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23103. #endif
  23104. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23105. /* ED25519 */
  23106. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23107. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23108. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23109. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23110. &ed25519Key), 0);
  23111. wc_ed25519_free(&ed25519Key);
  23112. #endif
  23113. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23114. /* ED448 */
  23115. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23116. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23117. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23118. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23119. &ed448Key), 0);
  23120. wc_ed448_free(&ed448Key);
  23121. #endif
  23122. wc_FreeRng(&rng);
  23123. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23124. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23125. return EXPECT_RESULT();
  23126. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23127. /*
  23128. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23129. */
  23130. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23131. {
  23132. EXPECT_DECLS;
  23133. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23134. WC_RNG rng;
  23135. Cert cert;
  23136. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23137. RsaKey rsaKey;
  23138. int bits = 2048;
  23139. #endif
  23140. #if defined(HAVE_ECC)
  23141. ecc_key eccKey;
  23142. int ret;
  23143. #endif
  23144. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23145. ed25519_key ed25519Key;
  23146. #endif
  23147. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23148. ed448_key ed448Key;
  23149. #endif
  23150. #ifndef HAVE_FIPS
  23151. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23152. #else
  23153. ExpectIntEQ(wc_InitRng(&rng), 0);
  23154. #endif
  23155. ExpectIntEQ(wc_InitCert(&cert), 0);
  23156. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23157. /* RSA */
  23158. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23159. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23160. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23161. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23162. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23163. #endif
  23164. #if defined(HAVE_ECC)
  23165. /* ECC */
  23166. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23167. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23168. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23169. #if defined(WOLFSSL_ASYNC_CRYPT)
  23170. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23171. #endif
  23172. ExpectIntEQ(ret, 0);
  23173. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23174. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23175. #endif
  23176. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23177. /* ED25519 */
  23178. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23179. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23180. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23181. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23182. &ed25519Key), 0);
  23183. wc_ed25519_free(&ed25519Key);
  23184. #endif
  23185. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23186. /* ED448 */
  23187. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23188. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23189. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23190. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23191. 0);
  23192. wc_ed448_free(&ed448Key);
  23193. #endif
  23194. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23195. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23196. return EXPECT_RESULT();
  23197. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23198. /*
  23199. * Testing wc_PKCS7_New()
  23200. */
  23201. static int test_wc_PKCS7_New(void)
  23202. {
  23203. EXPECT_DECLS;
  23204. #if defined(HAVE_PKCS7)
  23205. PKCS7* pkcs7 = NULL;
  23206. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23207. wc_PKCS7_Free(pkcs7);
  23208. #endif
  23209. return EXPECT_RESULT();
  23210. } /* END test-wc_PKCS7_New */
  23211. /*
  23212. * Testing wc_PKCS7_Init()
  23213. */
  23214. static int test_wc_PKCS7_Init(void)
  23215. {
  23216. EXPECT_DECLS;
  23217. #if defined(HAVE_PKCS7)
  23218. PKCS7* pkcs7 = NULL;
  23219. void* heap = NULL;
  23220. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23221. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23222. /* Pass in bad args. */
  23223. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23224. wc_PKCS7_Free(pkcs7);
  23225. #endif
  23226. return EXPECT_RESULT();
  23227. } /* END test-wc_PKCS7_Init */
  23228. /*
  23229. * Testing wc_PKCS7_InitWithCert()
  23230. */
  23231. static int test_wc_PKCS7_InitWithCert(void)
  23232. {
  23233. EXPECT_DECLS;
  23234. #if defined(HAVE_PKCS7)
  23235. PKCS7* pkcs7 = NULL;
  23236. #ifndef NO_RSA
  23237. #if defined(USE_CERT_BUFFERS_2048)
  23238. unsigned char cert[sizeof(client_cert_der_2048)];
  23239. int certSz = (int)sizeof(cert);
  23240. XMEMSET(cert, 0, certSz);
  23241. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23242. #elif defined(USE_CERT_BUFFERS_1024)
  23243. unsigned char cert[sizeof(client_cert_der_1024)];
  23244. int certSz = (int)sizeof(cert);
  23245. XMEMSET(cert, 0, certSz);
  23246. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23247. #else
  23248. unsigned char cert[ONEK_BUF];
  23249. XFILE fp = XBADFILE;
  23250. int certSz;
  23251. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23252. XBADFILE);
  23253. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23254. fp), 0);
  23255. if (fp != XBADFILE)
  23256. XFCLOSE(fp);
  23257. #endif
  23258. #elif defined(HAVE_ECC)
  23259. #if defined(USE_CERT_BUFFERS_256)
  23260. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23261. int certSz = (int)sizeof(cert);
  23262. XMEMSET(cert, 0, certSz);
  23263. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23264. #else
  23265. unsigned char cert[ONEK_BUF];
  23266. XFILE fp = XBADFILE;
  23267. int certSz;
  23268. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23269. XBADFILE);
  23270. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23271. fp), 0);
  23272. if (fp != XBADFILE)
  23273. XFCLOSE(fp);
  23274. #endif
  23275. #else
  23276. #error PKCS7 requires ECC or RSA
  23277. #endif
  23278. #ifdef HAVE_ECC
  23279. {
  23280. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23281. static unsigned char certWithInvalidEccKey[] = {
  23282. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23283. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23284. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23285. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23286. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23287. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23288. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23289. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23290. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23291. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23292. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23293. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23294. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23295. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23296. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23297. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23298. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23299. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23300. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23301. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23302. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23303. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23304. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23305. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23306. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23307. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23308. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23309. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23310. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23311. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23312. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23313. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23314. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23315. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23316. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23317. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23318. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23319. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23320. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23321. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23322. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23323. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23324. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23325. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23326. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23327. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23328. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23329. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23330. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23331. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23332. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23333. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23334. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23335. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23336. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23337. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23338. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23339. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23340. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23341. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23342. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23343. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23344. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23345. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23346. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23347. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23348. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23349. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23350. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23351. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23352. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23353. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23354. 0x08, 0xA8, 0xB4
  23355. };
  23356. #endif
  23357. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23358. /* If initialization is not successful, it's free'd in init func. */
  23359. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23360. 0);
  23361. wc_PKCS7_Free(pkcs7);
  23362. pkcs7 = NULL;
  23363. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23364. /* Valid initialization usage. */
  23365. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23366. /* Pass in bad args. No need free for null checks, free at end.*/
  23367. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23368. BAD_FUNC_ARG);
  23369. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23370. BAD_FUNC_ARG);
  23371. #ifdef HAVE_ECC
  23372. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23373. sizeof(certWithInvalidEccKey)), 0);
  23374. }
  23375. #endif
  23376. wc_PKCS7_Free(pkcs7);
  23377. #endif
  23378. return EXPECT_RESULT();
  23379. } /* END test_wc_PKCS7_InitWithCert */
  23380. /*
  23381. * Testing wc_PKCS7_EncodeData()
  23382. */
  23383. static int test_wc_PKCS7_EncodeData(void)
  23384. {
  23385. EXPECT_DECLS;
  23386. #if defined(HAVE_PKCS7)
  23387. PKCS7* pkcs7 = NULL;
  23388. byte output[FOURK_BUF];
  23389. byte data[] = "My encoded DER cert.";
  23390. #ifndef NO_RSA
  23391. #if defined(USE_CERT_BUFFERS_2048)
  23392. unsigned char cert[sizeof(client_cert_der_2048)];
  23393. unsigned char key[sizeof(client_key_der_2048)];
  23394. int certSz = (int)sizeof(cert);
  23395. int keySz = (int)sizeof(key);
  23396. XMEMSET(cert, 0, certSz);
  23397. XMEMSET(key, 0, keySz);
  23398. XMEMCPY(cert, client_cert_der_2048, certSz);
  23399. XMEMCPY(key, client_key_der_2048, keySz);
  23400. #elif defined(USE_CERT_BUFFERS_1024)
  23401. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23402. unsigned char key[sizeof_client_key_der_1024];
  23403. int certSz = (int)sizeof(cert);
  23404. int keySz = (int)sizeof(key);
  23405. XMEMSET(cert, 0, certSz);
  23406. XMEMSET(key, 0, keySz);
  23407. XMEMCPY(cert, client_cert_der_1024, certSz);
  23408. XMEMCPY(key, client_key_der_1024, keySz);
  23409. #else
  23410. unsigned char cert[ONEK_BUF];
  23411. unsigned char key[ONEK_BUF];
  23412. XFILE fp = XBADFILE;
  23413. int certSz;
  23414. int keySz;
  23415. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23416. XBADFILE);
  23417. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23418. fp), 0);
  23419. if (fp != XBADFILE) {
  23420. XFCLOSE(fp);
  23421. fp = XBADFILE;
  23422. }
  23423. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23424. XBADFILE);
  23425. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23426. 0);
  23427. if (fp != XBADFILE)
  23428. XFCLOSE(fp);
  23429. #endif
  23430. #elif defined(HAVE_ECC)
  23431. #if defined(USE_CERT_BUFFERS_256)
  23432. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23433. unsigned char key[sizeof(ecc_clikey_der_256)];
  23434. int certSz = (int)sizeof(cert);
  23435. int keySz = (int)sizeof(key);
  23436. XMEMSET(cert, 0, certSz);
  23437. XMEMSET(key, 0, keySz);
  23438. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23439. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23440. #else
  23441. unsigned char cert[ONEK_BUF];
  23442. unsigned char key[ONEK_BUF];
  23443. XFILE fp = XBADFILE;
  23444. int certSz, keySz;
  23445. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23446. XBADFILE);
  23447. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23448. fp), 0);
  23449. if (fp != XBADFILE) {
  23450. XFCLOSE(fp);
  23451. fp = XBADFILE;
  23452. }
  23453. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23454. XBADFILE);
  23455. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23456. 0);
  23457. if (fp != XBADFILE)
  23458. XFCLOSE(fp);
  23459. #endif
  23460. #endif
  23461. XMEMSET(output, 0, sizeof(output));
  23462. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23463. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23464. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23465. if (pkcs7 != NULL) {
  23466. pkcs7->content = data;
  23467. pkcs7->contentSz = sizeof(data);
  23468. pkcs7->privateKey = key;
  23469. pkcs7->privateKeySz = keySz;
  23470. }
  23471. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23472. /* Test bad args. */
  23473. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23474. BAD_FUNC_ARG);
  23475. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23476. BAD_FUNC_ARG);
  23477. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23478. wc_PKCS7_Free(pkcs7);
  23479. #endif
  23480. return EXPECT_RESULT();
  23481. } /* END test_wc_PKCS7_EncodeData */
  23482. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23483. !defined(NO_RSA) && !defined(NO_SHA256)
  23484. /* RSA sign raw digest callback */
  23485. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23486. byte* out, word32 outSz, byte* privateKey,
  23487. word32 privateKeySz, int devid, int hashOID)
  23488. {
  23489. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23490. byte digInfoEncoding[] = {
  23491. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23492. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23493. 0x00, 0x04, 0x20
  23494. };
  23495. int ret;
  23496. byte digestInfo[ONEK_BUF];
  23497. byte sig[FOURK_BUF];
  23498. word32 digestInfoSz = 0;
  23499. word32 idx = 0;
  23500. RsaKey rsa;
  23501. /* SHA-256 required only for this example callback due to above
  23502. * digInfoEncoding[] */
  23503. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23504. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23505. (hashOID != SHA256h)) {
  23506. return -1;
  23507. }
  23508. /* build DigestInfo */
  23509. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23510. digestInfoSz += sizeof(digInfoEncoding);
  23511. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23512. digestInfoSz += digestSz;
  23513. /* set up RSA key */
  23514. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23515. if (ret != 0) {
  23516. return ret;
  23517. }
  23518. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23519. /* sign DigestInfo */
  23520. if (ret == 0) {
  23521. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23522. &rsa, pkcs7->rng);
  23523. if (ret > 0) {
  23524. if (ret > (int)outSz) {
  23525. /* output buffer too small */
  23526. ret = -1;
  23527. }
  23528. else {
  23529. /* success, ret holds sig size */
  23530. XMEMCPY(out, sig, ret);
  23531. }
  23532. }
  23533. }
  23534. wc_FreeRsaKey(&rsa);
  23535. return ret;
  23536. }
  23537. #endif
  23538. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  23539. typedef struct encodeSignedDataStream {
  23540. byte out[FOURK_BUF*3];
  23541. int idx;
  23542. word32 outIdx;
  23543. } encodeSignedDataStream;
  23544. /* content is 8k of partially created bundle */
  23545. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  23546. {
  23547. int ret = 0;
  23548. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23549. if (strm->outIdx < pkcs7->contentSz) {
  23550. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  23551. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  23552. *content = strm->out + strm->outIdx;
  23553. strm->outIdx += ret;
  23554. }
  23555. (void)pkcs7;
  23556. return ret;
  23557. }
  23558. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  23559. void* ctx)
  23560. {
  23561. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23562. XMEMCPY(strm->out + strm->idx, output, outputSz);
  23563. strm->idx += outputSz;
  23564. (void)pkcs7;
  23565. return 0;
  23566. }
  23567. #endif
  23568. /*
  23569. * Testing wc_PKCS7_EncodeSignedData()
  23570. */
  23571. static int test_wc_PKCS7_EncodeSignedData(void)
  23572. {
  23573. EXPECT_DECLS;
  23574. #if defined(HAVE_PKCS7)
  23575. PKCS7* pkcs7 = NULL;
  23576. WC_RNG rng;
  23577. byte output[FOURK_BUF];
  23578. byte badOut[1];
  23579. word32 outputSz = (word32)sizeof(output);
  23580. word32 badOutSz = 0;
  23581. byte data[] = "Test data to encode.";
  23582. #ifndef NO_RSA
  23583. #if defined(USE_CERT_BUFFERS_2048)
  23584. byte key[sizeof(client_key_der_2048)];
  23585. byte cert[sizeof(client_cert_der_2048)];
  23586. word32 keySz = (word32)sizeof(key);
  23587. word32 certSz = (word32)sizeof(cert);
  23588. XMEMSET(key, 0, keySz);
  23589. XMEMSET(cert, 0, certSz);
  23590. XMEMCPY(key, client_key_der_2048, keySz);
  23591. XMEMCPY(cert, client_cert_der_2048, certSz);
  23592. #elif defined(USE_CERT_BUFFERS_1024)
  23593. byte key[sizeof_client_key_der_1024];
  23594. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23595. word32 keySz = (word32)sizeof(key);
  23596. word32 certSz = (word32)sizeof(cert);
  23597. XMEMSET(key, 0, keySz);
  23598. XMEMSET(cert, 0, certSz);
  23599. XMEMCPY(key, client_key_der_1024, keySz);
  23600. XMEMCPY(cert, client_cert_der_1024, certSz);
  23601. #else
  23602. unsigned char cert[ONEK_BUF];
  23603. unsigned char key[ONEK_BUF];
  23604. XFILE fp = XBADFILE;
  23605. int certSz;
  23606. int keySz;
  23607. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23608. XBADFILE);
  23609. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23610. fp), 0);
  23611. if (fp != XBADFILE) {
  23612. XFCLOSE(fp);
  23613. fp = XBADFILE;
  23614. }
  23615. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23616. XBADFILE);
  23617. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23618. 0);
  23619. if (fp != XBADFILE)
  23620. XFCLOSE(fp);
  23621. #endif
  23622. #elif defined(HAVE_ECC)
  23623. #if defined(USE_CERT_BUFFERS_256)
  23624. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23625. unsigned char key[sizeof(ecc_clikey_der_256)];
  23626. int certSz = (int)sizeof(cert);
  23627. int keySz = (int)sizeof(key);
  23628. XMEMSET(cert, 0, certSz);
  23629. XMEMSET(key, 0, keySz);
  23630. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23631. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23632. #else
  23633. unsigned char cert[ONEK_BUF];
  23634. unsigned char key[ONEK_BUF];
  23635. XFILE fp = XBADFILE;
  23636. int certSz;
  23637. int keySz;
  23638. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23639. XBADFILE);
  23640. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23641. if (fp != XBADFILE) {
  23642. XFCLOSE(fp);
  23643. fp = XBADFILE;
  23644. }
  23645. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23646. XBADFILE);
  23647. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23648. if (fp != XBADFILE)
  23649. XFCLOSE(fp);
  23650. #endif
  23651. #endif
  23652. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23653. XMEMSET(output, 0, outputSz);
  23654. ExpectIntEQ(wc_InitRng(&rng), 0);
  23655. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23656. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23657. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23658. if (pkcs7 != NULL) {
  23659. pkcs7->content = data;
  23660. pkcs7->contentSz = (word32)sizeof(data);
  23661. pkcs7->privateKey = key;
  23662. pkcs7->privateKeySz = (word32)sizeof(key);
  23663. pkcs7->encryptOID = RSAk;
  23664. #ifdef NO_SHA
  23665. pkcs7->hashOID = SHA256h;
  23666. #else
  23667. pkcs7->hashOID = SHAh;
  23668. #endif
  23669. pkcs7->rng = &rng;
  23670. }
  23671. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23672. wc_PKCS7_Free(pkcs7);
  23673. pkcs7 = NULL;
  23674. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23675. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23676. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23677. #ifdef ASN_BER_TO_DER
  23678. wc_PKCS7_Free(pkcs7);
  23679. /* reinitialize and test setting stream mode */
  23680. {
  23681. int signedSz;
  23682. encodeSignedDataStream strm;
  23683. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23684. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23685. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23686. if (pkcs7 != NULL) {
  23687. pkcs7->content = data;
  23688. pkcs7->contentSz = (word32)sizeof(data);
  23689. pkcs7->privateKey = key;
  23690. pkcs7->privateKeySz = (word32)sizeof(key);
  23691. pkcs7->encryptOID = RSAk;
  23692. #ifdef NO_SHA
  23693. pkcs7->hashOID = SHA256h;
  23694. #else
  23695. pkcs7->hashOID = SHAh;
  23696. #endif
  23697. pkcs7->rng = &rng;
  23698. }
  23699. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  23700. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  23701. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  23702. BAD_FUNC_ARG);
  23703. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  23704. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  23705. outputSz), 0);
  23706. wc_PKCS7_Free(pkcs7);
  23707. pkcs7 = NULL;
  23708. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23709. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23710. /* use exact signed buffer size since BER encoded */
  23711. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
  23712. wc_PKCS7_Free(pkcs7);
  23713. /* now try with using callbacks for IO */
  23714. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23715. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23716. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23717. if (pkcs7 != NULL) {
  23718. pkcs7->contentSz = FOURK_BUF*2;
  23719. pkcs7->privateKey = key;
  23720. pkcs7->privateKeySz = (word32)sizeof(key);
  23721. pkcs7->encryptOID = RSAk;
  23722. #ifdef NO_SHA
  23723. pkcs7->hashOID = SHA256h;
  23724. #else
  23725. pkcs7->hashOID = SHAh;
  23726. #endif
  23727. pkcs7->rng = &rng;
  23728. }
  23729. XMEMSET(&strm, 0, sizeof(strm));
  23730. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  23731. StreamOutputCB, (void*)&strm), 0);
  23732. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  23733. wc_PKCS7_Free(pkcs7);
  23734. pkcs7 = NULL;
  23735. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23736. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23737. /* use exact signed buffer size since BER encoded */
  23738. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, signedSz), 0);
  23739. }
  23740. #endif
  23741. #ifndef NO_PKCS7_STREAM
  23742. wc_PKCS7_Free(pkcs7);
  23743. pkcs7 = NULL;
  23744. {
  23745. word32 z;
  23746. int ret;
  23747. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23748. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23749. /* test for streaming mode */
  23750. ret = -1;
  23751. for (z = 0; z < outputSz && ret != 0; z++) {
  23752. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  23753. if (ret < 0){
  23754. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  23755. }
  23756. }
  23757. ExpectIntEQ(ret, 0);
  23758. ExpectIntNE(pkcs7->contentSz, 0);
  23759. ExpectNotNull(pkcs7->contentDynamic);
  23760. }
  23761. #endif /* !NO_PKCS7_STREAM */
  23762. /* Pass in bad args. */
  23763. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  23764. BAD_FUNC_ARG);
  23765. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23766. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23767. badOutSz), BAD_FUNC_ARG);
  23768. if (pkcs7 != NULL) {
  23769. pkcs7->hashOID = 0; /* bad hashOID */
  23770. }
  23771. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  23772. BAD_FUNC_ARG);
  23773. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23774. !defined(NO_RSA) && !defined(NO_SHA256)
  23775. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23776. * Example callback assumes SHA-256, so only run test if compiled in. */
  23777. wc_PKCS7_Free(pkcs7);
  23778. pkcs7 = NULL;
  23779. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23780. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23781. if (pkcs7 != NULL) {
  23782. pkcs7->content = data;
  23783. pkcs7->contentSz = (word32)sizeof(data);
  23784. pkcs7->privateKey = key;
  23785. pkcs7->privateKeySz = (word32)sizeof(key);
  23786. pkcs7->encryptOID = RSAk;
  23787. pkcs7->hashOID = SHA256h;
  23788. pkcs7->rng = &rng;
  23789. }
  23790. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23791. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23792. #endif
  23793. wc_PKCS7_Free(pkcs7);
  23794. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23795. #endif
  23796. return EXPECT_RESULT();
  23797. } /* END test_wc_PKCS7_EncodeSignedData */
  23798. /*
  23799. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23800. */
  23801. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23802. {
  23803. EXPECT_DECLS;
  23804. #if defined(HAVE_PKCS7)
  23805. int i;
  23806. PKCS7* pkcs7 = NULL;
  23807. WC_RNG rng;
  23808. byte outputHead[FOURK_BUF/2];
  23809. byte outputFoot[FOURK_BUF/2];
  23810. word32 outputHeadSz = (word32)sizeof(outputHead);
  23811. word32 outputFootSz = (word32)sizeof(outputFoot);
  23812. byte data[FOURK_BUF];
  23813. wc_HashAlg hash;
  23814. #ifdef NO_SHA
  23815. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23816. #else
  23817. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23818. #endif
  23819. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23820. word32 hashSz = wc_HashGetDigestSize(hashType);
  23821. #ifndef NO_RSA
  23822. #if defined(USE_CERT_BUFFERS_2048)
  23823. byte key[sizeof(client_key_der_2048)];
  23824. byte cert[sizeof(client_cert_der_2048)];
  23825. word32 keySz = (word32)sizeof(key);
  23826. word32 certSz = (word32)sizeof(cert);
  23827. XMEMSET(key, 0, keySz);
  23828. XMEMSET(cert, 0, certSz);
  23829. XMEMCPY(key, client_key_der_2048, keySz);
  23830. XMEMCPY(cert, client_cert_der_2048, certSz);
  23831. #elif defined(USE_CERT_BUFFERS_1024)
  23832. byte key[sizeof_client_key_der_1024];
  23833. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23834. word32 keySz = (word32)sizeof(key);
  23835. word32 certSz = (word32)sizeof(cert);
  23836. XMEMSET(key, 0, keySz);
  23837. XMEMSET(cert, 0, certSz);
  23838. XMEMCPY(key, client_key_der_1024, keySz);
  23839. XMEMCPY(cert, client_cert_der_1024, certSz);
  23840. #else
  23841. unsigned char cert[ONEK_BUF];
  23842. unsigned char key[ONEK_BUF];
  23843. XFILE fp = XBADFILE;
  23844. int certSz;
  23845. int keySz;
  23846. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23847. XBADFILE);
  23848. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23849. fp), 0);
  23850. if (fp != XBADFILE) {
  23851. XFCLOSE(fp);
  23852. fp = XBADFILE;
  23853. }
  23854. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23855. XBADFILE);
  23856. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23857. 0);
  23858. if (fp != XBADFILE)
  23859. XFCLOSE(fp);
  23860. #endif
  23861. #elif defined(HAVE_ECC)
  23862. #if defined(USE_CERT_BUFFERS_256)
  23863. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23864. unsigned char key[sizeof(ecc_clikey_der_256)];
  23865. int certSz = (int)sizeof(cert);
  23866. int keySz = (int)sizeof(key);
  23867. XMEMSET(cert, 0, certSz);
  23868. XMEMSET(key, 0, keySz);
  23869. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23870. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23871. #else
  23872. unsigned char cert[ONEK_BUF];
  23873. unsigned char key[ONEK_BUF];
  23874. XFILE fp = XBADFILE;
  23875. int certSz;
  23876. int keySz;
  23877. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23878. XBADFILE);
  23879. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23880. fp), 0);
  23881. if (fp != XBADFILE) {
  23882. XFCLOSE(fp);
  23883. fp = XBADFILE;
  23884. }
  23885. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23886. XBADFILE);
  23887. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23888. 0);
  23889. if (fp != XBADFILE)
  23890. XFCLOSE(fp);
  23891. #endif
  23892. #endif
  23893. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23894. /* initialize large data with sequence */
  23895. for (i=0; i<(int)sizeof(data); i++)
  23896. data[i] = i & 0xff;
  23897. XMEMSET(outputHead, 0, outputHeadSz);
  23898. XMEMSET(outputFoot, 0, outputFootSz);
  23899. ExpectIntEQ(wc_InitRng(&rng), 0);
  23900. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23901. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23902. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23903. if (pkcs7 != NULL) {
  23904. pkcs7->content = NULL; /* not used for ex */
  23905. pkcs7->contentSz = (word32)sizeof(data);
  23906. pkcs7->privateKey = key;
  23907. pkcs7->privateKeySz = (word32)sizeof(key);
  23908. pkcs7->encryptOID = RSAk;
  23909. #ifdef NO_SHA
  23910. pkcs7->hashOID = SHA256h;
  23911. #else
  23912. pkcs7->hashOID = SHAh;
  23913. #endif
  23914. pkcs7->rng = &rng;
  23915. }
  23916. /* calculate hash for content */
  23917. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23918. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23919. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23920. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23921. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23922. /* Perform PKCS7 sign using hash directly */
  23923. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23924. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23925. ExpectIntGT(outputHeadSz, 0);
  23926. ExpectIntGT(outputFootSz, 0);
  23927. wc_PKCS7_Free(pkcs7);
  23928. pkcs7 = NULL;
  23929. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23930. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23931. /* required parameter even on verify when using _ex, if using outputHead
  23932. * and outputFoot */
  23933. if (pkcs7 != NULL) {
  23934. pkcs7->contentSz = (word32)sizeof(data);
  23935. }
  23936. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23937. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23938. wc_PKCS7_Free(pkcs7);
  23939. pkcs7 = NULL;
  23940. /* assembly complete PKCS7 sign and use normal verify */
  23941. {
  23942. byte* output = NULL;
  23943. word32 outputSz = 0;
  23944. #ifndef NO_PKCS7_STREAM
  23945. word32 z;
  23946. int ret;
  23947. #endif /* !NO_PKCS7_STREAM */
  23948. ExpectNotNull(output = (byte*)XMALLOC(
  23949. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  23950. DYNAMIC_TYPE_TMP_BUFFER));
  23951. if (output != NULL) {
  23952. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23953. outputSz += outputHeadSz;
  23954. XMEMCPY(&output[outputSz], data, sizeof(data));
  23955. outputSz += sizeof(data);
  23956. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23957. outputSz += outputFootSz;
  23958. }
  23959. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23960. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23961. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23962. #ifndef NO_PKCS7_STREAM
  23963. wc_PKCS7_Free(pkcs7);
  23964. pkcs7 = NULL;
  23965. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23966. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23967. /* test for streaming mode */
  23968. ret = -1;
  23969. for (z = 0; z < outputSz && ret != 0; z++) {
  23970. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  23971. if (ret < 0){
  23972. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  23973. }
  23974. }
  23975. ExpectIntEQ(ret, 0);
  23976. ExpectIntNE(pkcs7->contentSz, 0);
  23977. ExpectNotNull(pkcs7->contentDynamic);
  23978. wc_PKCS7_Free(pkcs7);
  23979. pkcs7 = NULL;
  23980. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23981. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23982. #endif /* !NO_PKCS7_STREAM */
  23983. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23984. }
  23985. /* Pass in bad args. */
  23986. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23987. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23988. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23989. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23990. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23991. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23992. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23993. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23994. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23995. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23996. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23997. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23998. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23999. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24000. if (pkcs7 != NULL) {
  24001. pkcs7->hashOID = 0; /* bad hashOID */
  24002. }
  24003. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24004. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24005. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24006. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24007. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24008. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24009. #ifndef NO_PKCS7_STREAM
  24010. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24011. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24012. #else
  24013. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24014. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24015. #endif
  24016. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24017. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24018. #ifndef NO_PKCS7_STREAM
  24019. /* can pass in 0 buffer length with streaming API */
  24020. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24021. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24022. #else
  24023. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24024. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24025. #endif
  24026. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24027. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24028. #ifndef NO_PKCS7_STREAM
  24029. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24030. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24031. #else
  24032. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24033. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24034. #endif
  24035. wc_PKCS7_Free(pkcs7);
  24036. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24037. #endif
  24038. return EXPECT_RESULT();
  24039. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24040. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24041. /**
  24042. * Loads certs/keys from files or buffers into the argument buffers,
  24043. * helper function called by CreatePKCS7SignedData().
  24044. *
  24045. * Returns 0 on success, negative on error.
  24046. */
  24047. static int LoadPKCS7SignedDataCerts(
  24048. int useIntermediateCertChain, int pkAlgoType,
  24049. byte* intCARoot, word32* intCARootSz,
  24050. byte* intCA1, word32* intCA1Sz,
  24051. byte* intCA2, word32* intCA2Sz,
  24052. byte* cert, word32* certSz,
  24053. byte* key, word32* keySz)
  24054. {
  24055. EXPECT_DECLS;
  24056. int ret = 0;
  24057. XFILE fp = XBADFILE;
  24058. #ifndef NO_RSA
  24059. const char* intCARootRSA = "./certs/ca-cert.der";
  24060. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24061. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24062. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24063. const char* intServKeyRSA = "./certs/server-key.der";
  24064. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24065. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24066. const char* cli1024Key = "./certs/1024/client-key.der";
  24067. #endif
  24068. #endif
  24069. #ifdef HAVE_ECC
  24070. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24071. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24072. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24073. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24074. const char* intServKeyECC = "./certs/ecc-key.der";
  24075. #ifndef USE_CERT_BUFFERS_256
  24076. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24077. const char* cliEccKey = "./certs/client-ecc-key.der";
  24078. #endif
  24079. #endif
  24080. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24081. ((useIntermediateCertChain == 1) &&
  24082. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24083. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24084. return BAD_FUNC_ARG;
  24085. }
  24086. /* Read/load certs and keys to use for signing based on PK type and chain */
  24087. switch (pkAlgoType) {
  24088. #ifndef NO_RSA
  24089. case RSA_TYPE:
  24090. if (useIntermediateCertChain == 1) {
  24091. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24092. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24093. if (fp != XBADFILE) {
  24094. XFCLOSE(fp);
  24095. fp = XBADFILE;
  24096. }
  24097. ExpectIntGT(*intCARootSz, 0);
  24098. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24099. if (fp != XBADFILE) {
  24100. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24101. XFCLOSE(fp);
  24102. fp = XBADFILE;
  24103. }
  24104. ExpectIntGT(*intCA1Sz, 0);
  24105. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24106. if (fp != XBADFILE) {
  24107. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24108. XFCLOSE(fp);
  24109. fp = XBADFILE;
  24110. }
  24111. ExpectIntGT(*intCA2Sz, 0);
  24112. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24113. if (fp != XBADFILE) {
  24114. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24115. XFCLOSE(fp);
  24116. fp = XBADFILE;
  24117. }
  24118. ExpectIntGT(*certSz, 0);
  24119. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24120. if (fp != XBADFILE) {
  24121. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24122. XFCLOSE(fp);
  24123. fp = XBADFILE;
  24124. }
  24125. ExpectIntGT(*keySz, 0);
  24126. }
  24127. else {
  24128. #if defined(USE_CERT_BUFFERS_2048)
  24129. *keySz = sizeof_client_key_der_2048;
  24130. *certSz = sizeof_client_cert_der_2048;
  24131. XMEMCPY(key, client_key_der_2048, *keySz);
  24132. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24133. #elif defined(USE_CERT_BUFFERS_1024)
  24134. *keySz = sizeof_client_key_der_1024;
  24135. *certSz = sizeof_client_cert_der_1024;
  24136. XMEMCPY(key, client_key_der_1024, *keySz);
  24137. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24138. #else
  24139. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24140. if (fp != XBADFILE) {
  24141. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24142. XFCLOSE(fp);
  24143. fp = XBADFILE;
  24144. }
  24145. ExpectIntGT(*keySz, 0);
  24146. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24147. if (fp != XBADFILE) {
  24148. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24149. XFCLOSE(fp);
  24150. fp = XBADFILE;
  24151. }
  24152. ExpectIntGT(*certSz, 0);
  24153. #endif /* USE_CERT_BUFFERS_2048 */
  24154. }
  24155. break;
  24156. #endif /* !NO_RSA */
  24157. #ifdef HAVE_ECC
  24158. case ECC_TYPE:
  24159. if (useIntermediateCertChain == 1) {
  24160. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24161. if (fp != XBADFILE) {
  24162. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24163. fp);
  24164. XFCLOSE(fp);
  24165. fp = XBADFILE;
  24166. }
  24167. ExpectIntGT(*intCARootSz, 0);
  24168. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24169. if (fp != XBADFILE) {
  24170. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24171. XFCLOSE(fp);
  24172. fp = XBADFILE;
  24173. }
  24174. ExpectIntGT(*intCA1Sz, 0);
  24175. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24176. if (fp != XBADFILE) {
  24177. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24178. XFCLOSE(fp);
  24179. fp = XBADFILE;
  24180. }
  24181. ExpectIntGT(*intCA2Sz, 0);
  24182. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24183. if (fp != XBADFILE) {
  24184. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24185. XFCLOSE(fp);
  24186. fp = XBADFILE;
  24187. }
  24188. ExpectIntGT(*certSz, 0);
  24189. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24190. if (fp != XBADFILE) {
  24191. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24192. XFCLOSE(fp);
  24193. fp = XBADFILE;
  24194. }
  24195. ExpectIntGT(*keySz, 0);
  24196. }
  24197. else {
  24198. #if defined(USE_CERT_BUFFERS_256)
  24199. *keySz = sizeof_ecc_clikey_der_256;
  24200. *certSz = sizeof_cliecc_cert_der_256;
  24201. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24202. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24203. #else
  24204. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24205. if (fp != XBADFILE) {
  24206. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24207. XFCLOSE(fp);
  24208. fp = XBADFILE;
  24209. }
  24210. ExpectIntGT(*keySz, 0);
  24211. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24212. if (fp != XBADFILE) {
  24213. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24214. XFCLOSE(fp);
  24215. fp = XBADFILE;
  24216. }
  24217. ExpectIntGT(*certSz, 0);
  24218. #endif /* USE_CERT_BUFFERS_256 */
  24219. }
  24220. break;
  24221. #endif /* HAVE_ECC */
  24222. default:
  24223. WOLFSSL_MSG("Unsupported SignedData PK type");
  24224. ret = BAD_FUNC_ARG;
  24225. break;
  24226. }
  24227. if (EXPECT_FAIL() && (ret == 0)) {
  24228. ret = BAD_FUNC_ARG;
  24229. }
  24230. return ret;
  24231. }
  24232. /**
  24233. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24234. *
  24235. * output output buffer to place SignedData
  24236. * outputSz size of output buffer
  24237. * data data buffer to be signed
  24238. * dataSz size of data buffer
  24239. * withAttribs [1/0] include attributes in SignedData message
  24240. * detachedSig [1/0] create detached signature, no content
  24241. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24242. * root CAs in bundle
  24243. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24244. *
  24245. * Return size of bundle created on success, negative on error */
  24246. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24247. byte* data, word32 dataSz,
  24248. int withAttribs, int detachedSig,
  24249. int useIntermediateCertChain,
  24250. int pkAlgoType)
  24251. {
  24252. EXPECT_DECLS;
  24253. int ret = 0;
  24254. WC_RNG rng;
  24255. PKCS7* pkcs7 = NULL;
  24256. static byte messageTypeOid[] =
  24257. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24258. 0x09, 0x02 };
  24259. static byte messageType[] = { 0x13, 2, '1', '9' };
  24260. PKCS7Attrib attribs[] =
  24261. {
  24262. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24263. sizeof(messageType) }
  24264. };
  24265. byte intCARoot[TWOK_BUF];
  24266. byte intCA1[TWOK_BUF];
  24267. byte intCA2[TWOK_BUF];
  24268. byte cert[TWOK_BUF];
  24269. byte key[TWOK_BUF];
  24270. word32 intCARootSz = sizeof(intCARoot);
  24271. word32 intCA1Sz = sizeof(intCA1);
  24272. word32 intCA2Sz = sizeof(intCA2);
  24273. word32 certSz = sizeof(cert);
  24274. word32 keySz = sizeof(key);
  24275. XMEMSET(intCARoot, 0, intCARootSz);
  24276. XMEMSET(intCA1, 0, intCA1Sz);
  24277. XMEMSET(intCA2, 0, intCA2Sz);
  24278. XMEMSET(cert, 0, certSz);
  24279. XMEMSET(key, 0, keySz);
  24280. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24281. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24282. cert, &certSz, key, &keySz);
  24283. ExpectIntEQ(ret, 0);
  24284. XMEMSET(output, 0, outputSz);
  24285. ExpectIntEQ(wc_InitRng(&rng), 0);
  24286. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24287. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24288. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24289. if (useIntermediateCertChain == 1) {
  24290. /* Add intermediate and root CA certs into SignedData Certs SET */
  24291. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24292. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24293. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24294. }
  24295. if (pkcs7 != NULL) {
  24296. pkcs7->content = data;
  24297. pkcs7->contentSz = dataSz;
  24298. pkcs7->privateKey = key;
  24299. pkcs7->privateKeySz = (word32)sizeof(key);
  24300. if (pkAlgoType == RSA_TYPE) {
  24301. pkcs7->encryptOID = RSAk;
  24302. }
  24303. else {
  24304. pkcs7->encryptOID = ECDSAk;
  24305. }
  24306. #ifdef NO_SHA
  24307. pkcs7->hashOID = SHA256h;
  24308. #else
  24309. pkcs7->hashOID = SHAh;
  24310. #endif
  24311. pkcs7->rng = &rng;
  24312. if (withAttribs) {
  24313. /* include a signed attribute */
  24314. pkcs7->signedAttribs = attribs;
  24315. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24316. }
  24317. }
  24318. if (detachedSig) {
  24319. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24320. }
  24321. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24322. ExpectIntGT(outputSz, 0);
  24323. wc_PKCS7_Free(pkcs7);
  24324. pkcs7 = NULL;
  24325. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24326. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24327. if (detachedSig && (pkcs7 != NULL)) {
  24328. pkcs7->content = data;
  24329. pkcs7->contentSz = dataSz;
  24330. }
  24331. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24332. wc_PKCS7_Free(pkcs7);
  24333. wc_FreeRng(&rng);
  24334. if (EXPECT_FAIL()) {
  24335. outputSz = 0;
  24336. }
  24337. return outputSz;
  24338. }
  24339. #endif
  24340. /*
  24341. * Testing wc_PKCS_VerifySignedData()
  24342. */
  24343. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24344. {
  24345. EXPECT_DECLS;
  24346. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24347. PKCS7* pkcs7 = NULL;
  24348. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24349. word32 outputSz = sizeof(output);
  24350. byte data[] = "Test data to encode.";
  24351. byte badOut[1];
  24352. word32 badOutSz = 0;
  24353. byte badContent[] = "This is different content than was signed";
  24354. wc_HashAlg hash;
  24355. #ifdef NO_SHA
  24356. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24357. #else
  24358. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24359. #endif
  24360. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24361. word32 hashSz = wc_HashGetDigestSize(hashType);
  24362. #ifndef NO_RSA
  24363. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24364. /* contentType OID (1.2.840.113549.1.9.3) */
  24365. static const byte contentTypeOid[] =
  24366. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24367. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24368. static const byte dataType[] =
  24369. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24370. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24371. static const byte messageDigestOid[] =
  24372. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24373. #ifndef NO_ASN_TIME
  24374. /* signingTime OID () */
  24375. static const byte signingTimeOid[] =
  24376. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24377. #endif
  24378. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24379. int dateLength = 0;
  24380. byte dateFormat;
  24381. const byte* datePart = NULL;
  24382. struct tm timearg;
  24383. time_t now;
  24384. struct tm* nowTm = NULL;
  24385. #ifdef NEED_TMP_TIME
  24386. struct tm tmpTimeStorage;
  24387. struct tm* tmpTime = &tmpTimeStorage;
  24388. #endif
  24389. #endif /* !NO_ASN && !NO_ASN_TIME */
  24390. #ifndef NO_PKCS7_STREAM
  24391. word32 z;
  24392. int ret;
  24393. #endif /* !NO_PKCS7_STREAM */
  24394. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24395. /* Success test with RSA certs/key */
  24396. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24397. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  24398. /* calculate hash for content, used later */
  24399. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24400. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24401. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24402. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24403. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24404. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24405. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24406. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24407. #ifndef NO_PKCS7_STREAM
  24408. wc_PKCS7_Free(pkcs7);
  24409. pkcs7 = NULL;
  24410. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24411. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24412. /* test for streaming */
  24413. ret = -1;
  24414. for (z = 0; z < outputSz && ret != 0; z++) {
  24415. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24416. if (ret < 0){
  24417. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24418. }
  24419. }
  24420. ExpectIntEQ(ret, 0);
  24421. ExpectIntNE(pkcs7->contentSz, 0);
  24422. ExpectNotNull(pkcs7->contentDynamic);
  24423. #endif /* !NO_PKCS7_STREAM */
  24424. /* Check that decoded signed attributes are correct */
  24425. /* messageDigest should be first */
  24426. if (pkcs7 != NULL) {
  24427. decodedAttrib = pkcs7->decodedAttrib;
  24428. }
  24429. ExpectNotNull(decodedAttrib);
  24430. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  24431. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  24432. decodedAttrib->oidSz), 0);
  24433. /* + 2 for OCTET STRING and length bytes */
  24434. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  24435. ExpectNotNull(decodedAttrib->value);
  24436. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  24437. #ifndef NO_ASN_TIME
  24438. /* signingTime should be second */
  24439. if (decodedAttrib != NULL) {
  24440. decodedAttrib = decodedAttrib->next;
  24441. }
  24442. ExpectNotNull(decodedAttrib);
  24443. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  24444. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  24445. decodedAttrib->oidSz), 0);
  24446. ExpectIntGT(decodedAttrib->valueSz, 0);
  24447. ExpectNotNull(decodedAttrib->value);
  24448. #endif
  24449. /* Verify signingTime if ASN and time are available */
  24450. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24451. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24452. &datePart, &dateFormat, &dateLength), 0);
  24453. ExpectNotNull(datePart);
  24454. ExpectIntGT(dateLength, 0);
  24455. XMEMSET(&timearg, 0, sizeof(timearg));
  24456. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24457. &timearg), 0);
  24458. /* Get current time and compare year/month/day against attribute value */
  24459. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24460. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24461. ExpectNotNull(nowTm);
  24462. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24463. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24464. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24465. #endif /* !NO_ASN && !NO_ASN_TIME */
  24466. /* contentType should be third */
  24467. if (decodedAttrib != NULL) {
  24468. decodedAttrib = decodedAttrib->next;
  24469. }
  24470. ExpectNotNull(decodedAttrib);
  24471. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24472. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24473. decodedAttrib->oidSz), 0);
  24474. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24475. ExpectNotNull(decodedAttrib->value);
  24476. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24477. 0);
  24478. #endif /* !NO_RSA */
  24479. /* Test bad args. */
  24480. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24481. BAD_FUNC_ARG);
  24482. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24483. BAD_FUNC_ARG);
  24484. #ifndef NO_PKCS7_STREAM
  24485. /* can pass in 0 buffer length with streaming API */
  24486. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24487. badOutSz), WC_PKCS7_WANT_READ_E);
  24488. #else
  24489. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24490. badOutSz), BAD_FUNC_ARG);
  24491. #endif
  24492. wc_PKCS7_Free(pkcs7);
  24493. pkcs7 = NULL;
  24494. #ifndef NO_RSA
  24495. /* Try RSA certs/key/sig first */
  24496. outputSz = sizeof(output);
  24497. XMEMSET(output, 0, outputSz);
  24498. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24499. (word32)sizeof(data),
  24500. 1, 1, 0, RSA_TYPE)), 0);
  24501. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24502. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24503. if (pkcs7 != NULL) {
  24504. pkcs7->content = badContent;
  24505. pkcs7->contentSz = sizeof(badContent);
  24506. }
  24507. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24508. SIG_VERIFY_E);
  24509. wc_PKCS7_Free(pkcs7);
  24510. pkcs7 = NULL;
  24511. #ifndef NO_PKCS7_STREAM
  24512. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24513. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24514. if (pkcs7 != NULL) {
  24515. pkcs7->content = badContent;
  24516. pkcs7->contentSz = sizeof(badContent);
  24517. }
  24518. /* test for streaming */
  24519. ret = -1;
  24520. for (z = 0; z < outputSz && ret != 0; z++) {
  24521. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24522. if (ret == WC_PKCS7_WANT_READ_E){
  24523. continue;
  24524. }
  24525. else if (ret < 0) {
  24526. break;
  24527. }
  24528. }
  24529. ExpectIntEQ(ret, SIG_VERIFY_E);
  24530. ExpectIntNE(pkcs7->contentSz, 0);
  24531. ExpectNotNull(pkcs7->contentDynamic);
  24532. wc_PKCS7_Free(pkcs7);
  24533. pkcs7 = NULL;
  24534. #endif /* !NO_PKCS7_STREAM */
  24535. /* Test success case with detached signature and valid content */
  24536. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24537. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24538. if (pkcs7 != NULL) {
  24539. pkcs7->content = data;
  24540. pkcs7->contentSz = sizeof(data);
  24541. }
  24542. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24543. wc_PKCS7_Free(pkcs7);
  24544. pkcs7 = NULL;
  24545. #ifndef NO_PKCS7_STREAM
  24546. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24547. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24548. if (pkcs7 != NULL) {
  24549. pkcs7->content = data;
  24550. pkcs7->contentSz = sizeof(data);
  24551. }
  24552. /* test for streaming */
  24553. ret = -1;
  24554. for (z = 0; z < outputSz && ret != 0; z++) {
  24555. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24556. if (ret < 0){
  24557. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24558. }
  24559. }
  24560. ExpectIntEQ(ret, 0);
  24561. ExpectIntNE(pkcs7->contentSz, 0);
  24562. ExpectNotNull(pkcs7->contentDynamic);
  24563. wc_PKCS7_Free(pkcs7);
  24564. pkcs7 = NULL;
  24565. #endif /* !NO_PKCS7_STREAM */
  24566. /* verify using pre-computed content digest only (no content) */
  24567. {
  24568. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24569. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24570. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24571. output, outputSz, NULL, 0), 0);
  24572. wc_PKCS7_Free(pkcs7);
  24573. pkcs7 = NULL;
  24574. }
  24575. #endif /* !NO_RSA */
  24576. /* Test verify on signedData containing intermediate/root CA certs */
  24577. #ifndef NO_RSA
  24578. outputSz = sizeof(output);
  24579. XMEMSET(output, 0, outputSz);
  24580. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24581. (word32)sizeof(data),
  24582. 0, 0, 1, RSA_TYPE)), 0);
  24583. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24584. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24585. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24586. wc_PKCS7_Free(pkcs7);
  24587. pkcs7 = NULL;
  24588. #ifndef NO_PKCS7_STREAM
  24589. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24590. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24591. /* test for streaming */
  24592. ret = -1;
  24593. for (z = 0; z < outputSz && ret != 0; z++) {
  24594. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24595. if (ret < 0){
  24596. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24597. }
  24598. }
  24599. ExpectIntEQ(ret, 0);
  24600. ExpectIntNE(pkcs7->contentSz, 0);
  24601. ExpectNotNull(pkcs7->contentDynamic);
  24602. wc_PKCS7_Free(pkcs7);
  24603. pkcs7 = NULL;
  24604. #endif /* !NO_PKCS7_STREAM */
  24605. #endif /* !NO_RSA */
  24606. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  24607. !defined(NO_FILESYSTEM)
  24608. {
  24609. XFILE signedBundle = XBADFILE;
  24610. int signedBundleSz = 0;
  24611. int chunkSz = 1;
  24612. int i, rc = 0;
  24613. byte* buf = NULL;
  24614. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  24615. "rb")) != XBADFILE);
  24616. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  24617. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  24618. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  24619. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  24620. DYNAMIC_TYPE_FILE));
  24621. if (buf != NULL) {
  24622. ExpectIntEQ(XFREAD(buf, 1, signedBundleSz, signedBundle),
  24623. signedBundleSz);
  24624. }
  24625. if (signedBundle != XBADFILE) {
  24626. XFCLOSE(signedBundle);
  24627. signedBundle = XBADFILE;
  24628. }
  24629. if (buf != NULL) {
  24630. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24631. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24632. for (i = 0; i < signedBundleSz;) {
  24633. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  24634. chunkSz;
  24635. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  24636. if (rc < 0 ) {
  24637. if (rc == WC_PKCS7_WANT_READ_E) {
  24638. i += sz;
  24639. continue;
  24640. }
  24641. break;
  24642. }
  24643. else {
  24644. break;
  24645. }
  24646. }
  24647. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  24648. wc_PKCS7_Free(pkcs7);
  24649. pkcs7 = NULL;
  24650. }
  24651. /* now try with malformed bundle */
  24652. if (buf != NULL) {
  24653. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24654. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24655. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  24656. for (i = 0; i < signedBundleSz;) {
  24657. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  24658. chunkSz;
  24659. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  24660. if (rc < 0 ) {
  24661. if (rc == WC_PKCS7_WANT_READ_E) {
  24662. i += sz;
  24663. continue;
  24664. }
  24665. break;
  24666. }
  24667. else {
  24668. break;
  24669. }
  24670. }
  24671. ExpectIntEQ(rc, ASN_PARSE_E);
  24672. wc_PKCS7_Free(pkcs7);
  24673. pkcs7 = NULL;
  24674. }
  24675. if (buf != NULL)
  24676. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  24677. }
  24678. #endif /* BER and stream */
  24679. #endif
  24680. return EXPECT_RESULT();
  24681. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  24682. /*
  24683. * Testing wc_PKCS_VerifySignedData()
  24684. */
  24685. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  24686. {
  24687. EXPECT_DECLS;
  24688. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  24689. PKCS7* pkcs7 = NULL;
  24690. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24691. word32 outputSz = sizeof(output);
  24692. byte data[] = "Test data to encode.";
  24693. byte badContent[] = "This is different content than was signed";
  24694. wc_HashAlg hash;
  24695. #ifndef NO_PKCS7_STREAM
  24696. word32 z;
  24697. int ret;
  24698. #endif /* !NO_PKCS7_STREAM */
  24699. #ifdef NO_SHA
  24700. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24701. #else
  24702. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24703. #endif
  24704. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24705. word32 hashSz = wc_HashGetDigestSize(hashType);
  24706. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24707. /* Success test with ECC certs/key */
  24708. outputSz = sizeof(output);
  24709. XMEMSET(output, 0, outputSz);
  24710. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24711. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  24712. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24713. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24714. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24715. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24716. wc_PKCS7_Free(pkcs7);
  24717. pkcs7 = NULL;
  24718. #ifndef NO_PKCS7_STREAM
  24719. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24720. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24721. /* test for streaming */
  24722. ret = -1;
  24723. for (z = 0; z < outputSz && ret != 0; z++) {
  24724. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24725. if (ret < 0){
  24726. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24727. }
  24728. }
  24729. ExpectIntEQ(ret, 0);
  24730. ExpectIntNE(pkcs7->contentSz, 0);
  24731. ExpectNotNull(pkcs7->contentDynamic);
  24732. wc_PKCS7_Free(pkcs7);
  24733. pkcs7 = NULL;
  24734. #endif /* !NO_PKCS7_STREAM */
  24735. /* Invalid content should error, use detached signature so we can
  24736. * easily change content */
  24737. outputSz = sizeof(output);
  24738. XMEMSET(output, 0, outputSz);
  24739. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24740. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  24741. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24742. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24743. if (pkcs7 != NULL) {
  24744. pkcs7->content = badContent;
  24745. pkcs7->contentSz = sizeof(badContent);
  24746. }
  24747. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24748. SIG_VERIFY_E);
  24749. wc_PKCS7_Free(pkcs7);
  24750. pkcs7 = NULL;
  24751. #ifndef NO_PKCS7_STREAM
  24752. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24753. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24754. if (pkcs7 != NULL) {
  24755. pkcs7->content = badContent;
  24756. pkcs7->contentSz = sizeof(badContent);
  24757. }
  24758. /* test for streaming */
  24759. ret = -1;
  24760. for (z = 0; z < outputSz && ret != 0; z++) {
  24761. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24762. if (ret == WC_PKCS7_WANT_READ_E){
  24763. continue;
  24764. }
  24765. else if (ret < 0) {
  24766. break;
  24767. }
  24768. }
  24769. ExpectIntEQ(ret, SIG_VERIFY_E);
  24770. ExpectIntNE(pkcs7->contentSz, 0);
  24771. ExpectNotNull(pkcs7->contentDynamic);
  24772. wc_PKCS7_Free(pkcs7);
  24773. pkcs7 = NULL;
  24774. #endif /* !NO_PKCS7_STREAM */
  24775. /* Test success case with detached signature and valid content */
  24776. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24777. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24778. if (pkcs7 != NULL) {
  24779. pkcs7->content = data;
  24780. pkcs7->contentSz = sizeof(data);
  24781. }
  24782. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24783. wc_PKCS7_Free(pkcs7);
  24784. pkcs7 = NULL;
  24785. #ifndef NO_PKCS7_STREAM
  24786. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24787. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24788. if (pkcs7 != NULL) {
  24789. pkcs7->content = data;
  24790. pkcs7->contentSz = sizeof(data);
  24791. }
  24792. /* test for streaming */
  24793. ret = -1;
  24794. for (z = 0; z < outputSz && ret != 0; z++) {
  24795. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24796. if (ret < 0){
  24797. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24798. }
  24799. }
  24800. ExpectIntEQ(ret, 0);
  24801. ExpectIntNE(pkcs7->contentSz, 0);
  24802. ExpectNotNull(pkcs7->contentDynamic);
  24803. wc_PKCS7_Free(pkcs7);
  24804. pkcs7 = NULL;
  24805. #endif /* !NO_PKCS7_STREAM */
  24806. /* verify using pre-computed content digest only (no content) */
  24807. {
  24808. /* calculate hash for content */
  24809. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24810. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24811. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24812. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24813. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24814. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24815. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24816. output, outputSz, NULL, 0), 0);
  24817. wc_PKCS7_Free(pkcs7);
  24818. pkcs7 = NULL;
  24819. }
  24820. /* Test verify on signedData containing intermediate/root CA certs */
  24821. outputSz = sizeof(output);
  24822. XMEMSET(output, 0, outputSz);
  24823. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24824. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  24825. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24826. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24827. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24828. wc_PKCS7_Free(pkcs7);
  24829. pkcs7 = NULL;
  24830. #ifndef NO_PKCS7_STREAM
  24831. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24832. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24833. /* test for streaming */
  24834. ret = -1;
  24835. for (z = 0; z < outputSz && ret != 0; z++) {
  24836. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24837. if (ret < 0){
  24838. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24839. }
  24840. }
  24841. ExpectIntEQ(ret, 0);
  24842. ExpectIntNE(pkcs7->contentSz, 0);
  24843. ExpectNotNull(pkcs7->contentDynamic);
  24844. wc_PKCS7_Free(pkcs7);
  24845. pkcs7 = NULL;
  24846. #endif /* !NO_PKCS7_STREAM */
  24847. #endif
  24848. return EXPECT_RESULT();
  24849. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  24850. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  24851. !defined(NO_AES_256)
  24852. static const byte defKey[] = {
  24853. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24854. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24855. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24856. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24857. };
  24858. static byte aesHandle[32]; /* simulated hardware key handle */
  24859. /* return 0 on success */
  24860. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  24861. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  24862. byte* in, int inSz, byte* out, void* usrCtx)
  24863. {
  24864. int ret;
  24865. Aes aes;
  24866. if (usrCtx == NULL) {
  24867. /* no simulated handle passed in */
  24868. return -1;
  24869. }
  24870. switch (encryptOID) {
  24871. case AES256CBCb:
  24872. if (ivSz != AES_BLOCK_SIZE)
  24873. return BAD_FUNC_ARG;
  24874. break;
  24875. default:
  24876. WOLFSSL_MSG("Unsupported content cipher type for test");
  24877. return ALGO_ID_E;
  24878. };
  24879. /* simulate using handle to get key */
  24880. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  24881. if (ret == 0) {
  24882. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  24883. if (ret == 0)
  24884. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  24885. wc_AesFree(&aes);
  24886. }
  24887. (void)aad;
  24888. (void)aadSz;
  24889. (void)authTag;
  24890. (void)authTagSz;
  24891. (void)pkcs7;
  24892. return ret;
  24893. }
  24894. /* returns key size on success */
  24895. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24896. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24897. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24898. {
  24899. int ret = -1;
  24900. (void)cekSz;
  24901. (void)cek;
  24902. (void)outSz;
  24903. (void)keyIdSz;
  24904. (void)direction;
  24905. (void)orginKey; /* used with KAKRI */
  24906. (void)orginKeySz;
  24907. if (out == NULL)
  24908. return BAD_FUNC_ARG;
  24909. if (keyId[0] != 0x00) {
  24910. return -1;
  24911. }
  24912. if (type != (int)PKCS7_KEKRI) {
  24913. return -1;
  24914. }
  24915. switch (keyWrapAlgo) {
  24916. case AES256_WRAP:
  24917. /* simulate setting a handle for later decryption but use key
  24918. * as handle in the test case here */
  24919. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24920. aesHandle, sizeof(aesHandle), NULL);
  24921. if (ret < 0)
  24922. return ret;
  24923. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24924. if (ret < 0)
  24925. return ret;
  24926. /* return key size on success */
  24927. return sizeof(defKey);
  24928. default:
  24929. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24930. return BAD_KEYWRAP_ALG_E;
  24931. };
  24932. }
  24933. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24934. /*
  24935. * Testing wc_PKCS7_EncodeEnvelopedData()
  24936. */
  24937. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  24938. {
  24939. EXPECT_DECLS;
  24940. #if defined(HAVE_PKCS7)
  24941. PKCS7* pkcs7 = NULL;
  24942. #ifdef ASN_BER_TO_DER
  24943. int encodedSz = 0;
  24944. #endif
  24945. #ifdef ECC_TIMING_RESISTANT
  24946. WC_RNG rng;
  24947. #endif
  24948. word32 tempWrd32 = 0;
  24949. byte* tmpBytePtr = NULL;
  24950. const char input[] = "Test data to encode.";
  24951. int i;
  24952. int testSz = 0;
  24953. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24954. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24955. byte* rsaCert = NULL;
  24956. byte* rsaPrivKey = NULL;
  24957. word32 rsaCertSz;
  24958. word32 rsaPrivKeySz;
  24959. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24960. !defined(USE_CERT_BUFFERS_2048) )
  24961. static const char* rsaClientCert = "./certs/client-cert.der";
  24962. static const char* rsaClientKey = "./certs/client-key.der";
  24963. rsaCertSz = (word32)sizeof(rsaClientCert);
  24964. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24965. #endif
  24966. #endif
  24967. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24968. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24969. byte* eccCert = NULL;
  24970. byte* eccPrivKey = NULL;
  24971. word32 eccCertSz;
  24972. word32 eccPrivKeySz;
  24973. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24974. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24975. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24976. #endif
  24977. #endif
  24978. /* Generic buffer size. */
  24979. byte output[ONEK_BUF];
  24980. byte decoded[sizeof(input)/sizeof(char)];
  24981. int decodedSz = 0;
  24982. #ifndef NO_FILESYSTEM
  24983. XFILE certFile = XBADFILE;
  24984. XFILE keyFile = XBADFILE;
  24985. #endif
  24986. #ifdef ECC_TIMING_RESISTANT
  24987. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24988. #endif
  24989. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24990. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24991. /* RSA certs and keys. */
  24992. #if defined(USE_CERT_BUFFERS_1024)
  24993. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24994. /* Allocate buffer space. */
  24995. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24996. DYNAMIC_TYPE_TMP_BUFFER));
  24997. /* Init buffer. */
  24998. if (rsaCert != NULL) {
  24999. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25000. }
  25001. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25002. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25003. DYNAMIC_TYPE_TMP_BUFFER));
  25004. if (rsaPrivKey != NULL) {
  25005. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25006. }
  25007. #elif defined(USE_CERT_BUFFERS_2048)
  25008. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25009. /* Allocate buffer */
  25010. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25011. DYNAMIC_TYPE_TMP_BUFFER));
  25012. /* Init buffer. */
  25013. if (rsaCert != NULL) {
  25014. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25015. }
  25016. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25017. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25018. DYNAMIC_TYPE_TMP_BUFFER));
  25019. if (rsaPrivKey != NULL) {
  25020. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25021. }
  25022. #else
  25023. /* File system. */
  25024. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  25025. rsaCertSz = (word32)FOURK_BUF;
  25026. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25027. DYNAMIC_TYPE_TMP_BUFFER));
  25028. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  25029. certFile)) > 0);
  25030. if (certFile != XBADFILE)
  25031. XFCLOSE(certFile);
  25032. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25033. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25034. DYNAMIC_TYPE_TMP_BUFFER));
  25035. rsaPrivKeySz = (word32)FOURK_BUF;
  25036. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25037. keyFile)) > 0);
  25038. if (keyFile != XBADFILE)
  25039. XFCLOSE(keyFile);
  25040. #endif /* USE_CERT_BUFFERS */
  25041. #endif /* NO_RSA */
  25042. /* ECC */
  25043. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25044. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25045. #ifdef USE_CERT_BUFFERS_256
  25046. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25047. DYNAMIC_TYPE_TMP_BUFFER));
  25048. /* Init buffer. */
  25049. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25050. if (eccCert != NULL) {
  25051. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25052. }
  25053. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25054. DYNAMIC_TYPE_TMP_BUFFER));
  25055. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25056. if (eccPrivKey != NULL) {
  25057. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25058. }
  25059. #else /* File system. */
  25060. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25061. eccCertSz = (word32)FOURK_BUF;
  25062. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25063. DYNAMIC_TYPE_TMP_BUFFER));
  25064. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25065. certFile)) > 0);
  25066. if (certFile != XBADFILE) {
  25067. XFCLOSE(certFile);
  25068. }
  25069. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25070. eccPrivKeySz = (word32)FOURK_BUF;
  25071. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25072. DYNAMIC_TYPE_TMP_BUFFER));
  25073. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25074. keyFile)) > 0);
  25075. if (keyFile != XBADFILE) {
  25076. XFCLOSE(keyFile);
  25077. }
  25078. #endif /* USE_CERT_BUFFERS_256 */
  25079. #endif /* END HAVE_ECC */
  25080. #ifndef NO_FILESYSTEM
  25081. /* Silence. */
  25082. (void)keyFile;
  25083. (void)certFile;
  25084. #endif
  25085. {
  25086. const pkcs7EnvelopedVector testVectors[] = {
  25087. /* DATA is a global variable defined in the makefile. */
  25088. #if !defined(NO_RSA)
  25089. #ifndef NO_DES3
  25090. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25091. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25092. #endif /* NO_DES3 */
  25093. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25094. #ifndef NO_AES_128
  25095. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25096. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25097. #endif
  25098. #ifndef NO_AES_192
  25099. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25100. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25101. #endif
  25102. #ifndef NO_AES_256
  25103. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25104. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25105. #endif
  25106. #endif /* NO_AES && HAVE_AES_CBC */
  25107. #endif /* NO_RSA */
  25108. #if defined(HAVE_ECC)
  25109. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25110. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25111. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25112. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25113. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25114. #endif
  25115. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25116. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25117. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25118. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25119. #endif
  25120. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25121. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25122. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25123. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25124. #endif
  25125. #endif /* NO_AES && HAVE_AES_CBC*/
  25126. #endif /* END HAVE_ECC */
  25127. }; /* END pkcs7EnvelopedVector */
  25128. #ifdef ECC_TIMING_RESISTANT
  25129. ExpectIntEQ(wc_InitRng(&rng), 0);
  25130. #endif
  25131. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25132. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25133. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25134. for (i = 0; i < testSz; i++) {
  25135. #ifdef ASN_BER_TO_DER
  25136. encodeSignedDataStream strm;
  25137. /* test setting stream mode, the first one using IO callbacks */
  25138. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25139. (word32)(testVectors + i)->certSz), 0);
  25140. if (pkcs7 != NULL) {
  25141. #ifdef ECC_TIMING_RESISTANT
  25142. pkcs7->rng = &rng;
  25143. #endif
  25144. if (i != 0)
  25145. pkcs7->content = (byte*)(testVectors + i)->content;
  25146. pkcs7->contentSz = (testVectors + i)->contentSz;
  25147. pkcs7->contentOID = (testVectors + i)->contentOID;
  25148. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25149. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25150. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25151. pkcs7->privateKey = (testVectors + i)->privateKey;
  25152. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25153. }
  25154. if (i == 0) {
  25155. XMEMSET(&strm, 0, sizeof(strm));
  25156. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25157. StreamOutputCB, (void*)&strm), 0);
  25158. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25159. }
  25160. else {
  25161. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25162. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25163. (word32)sizeof(output));
  25164. }
  25165. switch ((testVectors + i)->encryptOID) {
  25166. #ifndef NO_DES3
  25167. case DES3b:
  25168. case DESb:
  25169. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25170. break;
  25171. #endif
  25172. #ifdef HAVE_AESCCM
  25173. #ifdef WOLFSSL_AES_128
  25174. case AES128CCMb:
  25175. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25176. break;
  25177. #endif
  25178. #ifdef WOLFSSL_AES_192
  25179. case AES192CCMb:
  25180. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25181. break;
  25182. #endif
  25183. #ifdef WOLFSSL_AES_256
  25184. case AES256CCMb:
  25185. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25186. break;
  25187. #endif
  25188. #endif
  25189. default:
  25190. ExpectIntGE(encodedSz, 0);
  25191. }
  25192. if (encodedSz > 0) {
  25193. if (i == 0) {
  25194. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25195. strm.out, (word32)encodedSz, decoded,
  25196. (word32)sizeof(decoded));
  25197. }
  25198. else {
  25199. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25200. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25201. }
  25202. ExpectIntGE(decodedSz, 0);
  25203. /* Verify the size of each buffer. */
  25204. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25205. }
  25206. wc_PKCS7_Free(pkcs7);
  25207. pkcs7 = NULL;
  25208. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25209. #endif
  25210. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25211. (word32)(testVectors + i)->certSz), 0);
  25212. if (pkcs7 != NULL) {
  25213. #ifdef ECC_TIMING_RESISTANT
  25214. pkcs7->rng = &rng;
  25215. #endif
  25216. pkcs7->content = (byte*)(testVectors + i)->content;
  25217. pkcs7->contentSz = (testVectors + i)->contentSz;
  25218. pkcs7->contentOID = (testVectors + i)->contentOID;
  25219. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25220. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25221. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25222. pkcs7->privateKey = (testVectors + i)->privateKey;
  25223. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25224. }
  25225. #ifdef ASN_BER_TO_DER
  25226. /* test without setting stream mode */
  25227. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25228. #endif
  25229. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25230. (word32)sizeof(output)), 0);
  25231. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25232. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25233. ExpectIntGE(decodedSz, 0);
  25234. /* Verify the size of each buffer. */
  25235. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25236. /* Don't free the last time through the loop. */
  25237. if (i < testSz - 1) {
  25238. wc_PKCS7_Free(pkcs7);
  25239. pkcs7 = NULL;
  25240. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25241. }
  25242. } /* END test loop. */
  25243. }
  25244. /* Test bad args. */
  25245. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25246. (word32)sizeof(output)), BAD_FUNC_ARG);
  25247. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25248. (word32)sizeof(output)), BAD_FUNC_ARG);
  25249. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25250. /* Decode. */
  25251. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25252. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25253. BAD_FUNC_ARG);
  25254. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25255. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25256. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25257. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25258. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25259. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25260. BAD_FUNC_ARG);
  25261. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25262. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25263. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25264. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25265. /* only a failure for KARI test cases */
  25266. if (pkcs7 != NULL) {
  25267. tempWrd32 = pkcs7->singleCertSz;
  25268. pkcs7->singleCertSz = 0;
  25269. }
  25270. #if defined(WOLFSSL_ASN_TEMPLATE)
  25271. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25272. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25273. BUFFER_E);
  25274. #else
  25275. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25276. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25277. ASN_PARSE_E);
  25278. #endif
  25279. if (pkcs7 != NULL) {
  25280. pkcs7->singleCertSz = tempWrd32;
  25281. tmpBytePtr = pkcs7->singleCert;
  25282. pkcs7->singleCert = NULL;
  25283. }
  25284. #ifndef NO_RSA
  25285. #if defined(NO_PKCS7_STREAM)
  25286. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25287. * is returned from kari parse which gets set to bad func arg */
  25288. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25289. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25290. BAD_FUNC_ARG);
  25291. #else
  25292. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25293. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25294. ASN_PARSE_E);
  25295. #endif
  25296. #endif /* !NO_RSA */
  25297. if (pkcs7 != NULL) {
  25298. pkcs7->singleCert = tmpBytePtr;
  25299. }
  25300. #endif
  25301. if (pkcs7 != NULL) {
  25302. tempWrd32 = pkcs7->privateKeySz;
  25303. pkcs7->privateKeySz = 0;
  25304. }
  25305. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25306. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25307. BAD_FUNC_ARG);
  25308. if (pkcs7 != NULL) {
  25309. pkcs7->privateKeySz = tempWrd32;
  25310. tmpBytePtr = pkcs7->privateKey;
  25311. pkcs7->privateKey = NULL;
  25312. }
  25313. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25314. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25315. BAD_FUNC_ARG);
  25316. if (pkcs7 != NULL) {
  25317. pkcs7->privateKey = tmpBytePtr;
  25318. }
  25319. wc_PKCS7_Free(pkcs7);
  25320. pkcs7 = NULL;
  25321. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25322. /* test of decrypt callback with KEKRI enveloped data */
  25323. {
  25324. int envelopedSz = 0;
  25325. const byte keyId[] = { 0x00 };
  25326. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25327. if (pkcs7 != NULL) {
  25328. pkcs7->content = (byte*)input;
  25329. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25330. pkcs7->contentOID = DATA;
  25331. pkcs7->encryptOID = AES256CBCb;
  25332. }
  25333. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25334. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25335. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25336. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25337. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25338. (word32)sizeof(output))), 0);
  25339. wc_PKCS7_Free(pkcs7);
  25340. pkcs7 = NULL;
  25341. /* decode envelopedData */
  25342. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25343. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25344. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25345. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25346. envelopedSz, decoded, sizeof(decoded))), 0);
  25347. wc_PKCS7_Free(pkcs7);
  25348. pkcs7 = NULL;
  25349. }
  25350. #endif /* !NO_AES && !NO_AES_256 */
  25351. #ifndef NO_RSA
  25352. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25353. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25354. #endif /* NO_RSA */
  25355. #ifdef HAVE_ECC
  25356. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25357. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25358. #endif /* HAVE_ECC */
  25359. #ifdef ECC_TIMING_RESISTANT
  25360. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25361. #endif
  25362. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25363. !defined(NO_RSA) && !defined(NO_SHA)
  25364. {
  25365. byte out[7];
  25366. byte *cms = NULL;
  25367. word32 cmsSz;
  25368. XFILE cmsFile = XBADFILE;
  25369. XMEMSET(out, 0, sizeof(out));
  25370. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25371. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25372. != XBADFILE);
  25373. cmsSz = (word32)FOURK_BUF;
  25374. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25375. DYNAMIC_TYPE_TMP_BUFFER));
  25376. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25377. if (cmsFile != XBADFILE)
  25378. XFCLOSE(cmsFile);
  25379. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25380. sizeof_client_cert_der_2048), 0);
  25381. if (pkcs7 != NULL) {
  25382. pkcs7->privateKey = (byte*)client_key_der_2048;
  25383. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25384. }
  25385. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25386. 2), 0);
  25387. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25388. sizeof(out)), 0);
  25389. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25390. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25391. wc_PKCS7_Free(pkcs7);
  25392. pkcs7 = NULL;
  25393. }
  25394. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25395. #endif /* HAVE_PKCS7 */
  25396. return EXPECT_RESULT();
  25397. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  25398. /*
  25399. * Testing wc_PKCS7_EncodeEncryptedData()
  25400. */
  25401. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25402. {
  25403. EXPECT_DECLS;
  25404. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25405. PKCS7* pkcs7 = NULL;
  25406. byte* tmpBytePtr = NULL;
  25407. byte encrypted[TWOK_BUF];
  25408. byte decoded[TWOK_BUF];
  25409. word32 tmpWrd32 = 0;
  25410. int tmpInt = 0;
  25411. int decodedSz = 0;
  25412. int encryptedSz = 0;
  25413. int testSz = 0;
  25414. int i = 0;
  25415. const byte data[] = { /* Hello World */
  25416. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25417. 0x72,0x6c,0x64
  25418. };
  25419. #ifndef NO_DES3
  25420. byte desKey[] = {
  25421. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25422. };
  25423. byte des3Key[] = {
  25424. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25425. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25426. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25427. };
  25428. #endif
  25429. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25430. #ifndef NO_AES_128
  25431. byte aes128Key[] = {
  25432. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25433. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25434. };
  25435. #endif
  25436. #ifndef NO_AES_192
  25437. byte aes192Key[] = {
  25438. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25439. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25440. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25441. };
  25442. #endif
  25443. #ifndef NO_AES_256
  25444. byte aes256Key[] = {
  25445. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25446. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25447. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25448. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25449. };
  25450. #endif
  25451. #endif /* !NO_AES && HAVE_AES_CBC */
  25452. const pkcs7EncryptedVector testVectors[] =
  25453. {
  25454. #ifndef NO_DES3
  25455. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25456. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25457. #endif /* !NO_DES3 */
  25458. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25459. #ifndef NO_AES_128
  25460. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25461. sizeof(aes128Key)},
  25462. #endif
  25463. #ifndef NO_AES_192
  25464. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25465. sizeof(aes192Key)},
  25466. #endif
  25467. #ifndef NO_AES_256
  25468. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25469. sizeof(aes256Key)},
  25470. #endif
  25471. #endif /* !NO_AES && HAVE_AES_CBC */
  25472. };
  25473. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25474. for (i = 0; i < testSz; i++) {
  25475. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25476. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25477. if (pkcs7 != NULL) {
  25478. pkcs7->content = (byte*)testVectors[i].content;
  25479. pkcs7->contentSz = testVectors[i].contentSz;
  25480. pkcs7->contentOID = testVectors[i].contentOID;
  25481. pkcs7->encryptOID = testVectors[i].encryptOID;
  25482. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25483. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25484. pkcs7->heap = HEAP_HINT;
  25485. }
  25486. /* encode encryptedData */
  25487. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25488. sizeof(encrypted)), 0);
  25489. /* Decode encryptedData */
  25490. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  25491. encryptedSz, decoded, sizeof(decoded)), 0);
  25492. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25493. /* Keep values for last itr. */
  25494. if (i < testSz - 1) {
  25495. wc_PKCS7_Free(pkcs7);
  25496. pkcs7 = NULL;
  25497. }
  25498. }
  25499. if (pkcs7 == NULL || testSz == 0) {
  25500. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25501. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25502. }
  25503. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25504. sizeof(encrypted)),BAD_FUNC_ARG);
  25505. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25506. sizeof(encrypted)), BAD_FUNC_ARG);
  25507. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25508. 0), BAD_FUNC_ARG);
  25509. /* Testing the struct. */
  25510. if (pkcs7 != NULL) {
  25511. tmpBytePtr = pkcs7->content;
  25512. pkcs7->content = NULL;
  25513. }
  25514. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25515. sizeof(encrypted)), BAD_FUNC_ARG);
  25516. if (pkcs7 != NULL) {
  25517. pkcs7->content = tmpBytePtr;
  25518. tmpWrd32 = pkcs7->contentSz;
  25519. pkcs7->contentSz = 0;
  25520. }
  25521. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25522. sizeof(encrypted)), BAD_FUNC_ARG);
  25523. if (pkcs7 != NULL) {
  25524. pkcs7->contentSz = tmpWrd32;
  25525. tmpInt = pkcs7->encryptOID;
  25526. pkcs7->encryptOID = 0;
  25527. }
  25528. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25529. sizeof(encrypted)), BAD_FUNC_ARG);
  25530. if (pkcs7 != NULL) {
  25531. pkcs7->encryptOID = tmpInt;
  25532. tmpBytePtr = pkcs7->encryptionKey;
  25533. pkcs7->encryptionKey = NULL;
  25534. }
  25535. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25536. sizeof(encrypted)), BAD_FUNC_ARG);
  25537. if (pkcs7 != NULL) {
  25538. pkcs7->encryptionKey = tmpBytePtr;
  25539. tmpWrd32 = pkcs7->encryptionKeySz;
  25540. pkcs7->encryptionKeySz = 0;
  25541. }
  25542. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25543. sizeof(encrypted)), BAD_FUNC_ARG);
  25544. if (pkcs7 != NULL) {
  25545. pkcs7->encryptionKeySz = tmpWrd32;
  25546. }
  25547. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25548. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25549. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25550. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25551. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25552. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25553. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25554. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25555. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25556. decoded, 0), BAD_FUNC_ARG);
  25557. /* Test struct fields */
  25558. if (pkcs7 != NULL) {
  25559. tmpBytePtr = pkcs7->encryptionKey;
  25560. pkcs7->encryptionKey = NULL;
  25561. }
  25562. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25563. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25564. if (pkcs7 != NULL) {
  25565. pkcs7->encryptionKey = tmpBytePtr;
  25566. pkcs7->encryptionKeySz = 0;
  25567. }
  25568. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25569. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25570. wc_PKCS7_Free(pkcs7);
  25571. #endif
  25572. return EXPECT_RESULT();
  25573. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25574. /*
  25575. * Testing wc_PKCS7_Degenerate()
  25576. */
  25577. static int test_wc_PKCS7_Degenerate(void)
  25578. {
  25579. EXPECT_DECLS;
  25580. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25581. PKCS7* pkcs7 = NULL;
  25582. char fName[] = "./certs/test-degenerate.p7b";
  25583. XFILE f = XBADFILE;
  25584. byte der[4096];
  25585. word32 derSz = 0;
  25586. #ifndef NO_PKCS7_STREAM
  25587. word32 z;
  25588. int ret;
  25589. #endif /* !NO_PKCS7_STREAM */
  25590. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25591. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25592. if (f != XBADFILE)
  25593. XFCLOSE(f);
  25594. /* test degenerate success */
  25595. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25596. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25597. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25598. #ifndef NO_RSA
  25599. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25600. #ifndef NO_PKCS7_STREAM
  25601. wc_PKCS7_Free(pkcs7);
  25602. pkcs7 = NULL;
  25603. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25604. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25605. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25606. /* test for streaming */
  25607. ret = -1;
  25608. for (z = 0; z < derSz && ret != 0; z++) {
  25609. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25610. if (ret < 0){
  25611. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25612. }
  25613. }
  25614. ExpectIntEQ(ret, 0);
  25615. #endif /* !NO_PKCS7_STREAM */
  25616. #else
  25617. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25618. #endif /* NO_RSA */
  25619. wc_PKCS7_Free(pkcs7);
  25620. pkcs7 = NULL;
  25621. /* test with turning off degenerate cases */
  25622. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25623. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25624. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25625. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25626. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  25627. PKCS7_NO_SIGNER_E);
  25628. #ifndef NO_PKCS7_STREAM
  25629. wc_PKCS7_Free(pkcs7);
  25630. pkcs7 = NULL;
  25631. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25632. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25633. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25634. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25635. /* test for streaming */
  25636. ret = -1;
  25637. for (z = 0; z < derSz && ret != 0; z++) {
  25638. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25639. if (ret == WC_PKCS7_WANT_READ_E){
  25640. continue;
  25641. }
  25642. else
  25643. break;
  25644. }
  25645. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  25646. #endif /* !NO_PKCS7_STREAM */
  25647. wc_PKCS7_Free(pkcs7);
  25648. #endif
  25649. return EXPECT_RESULT();
  25650. } /* END test_wc_PKCS7_Degenerate() */
  25651. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25652. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  25653. static byte berContent[] = {
  25654. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25655. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  25656. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  25657. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  25658. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  25659. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  25660. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  25661. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  25662. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  25663. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  25664. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  25665. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  25666. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  25667. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  25668. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  25669. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  25670. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  25671. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  25672. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25673. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25674. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  25675. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25676. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25677. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  25678. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  25679. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  25680. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25681. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  25682. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  25683. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  25684. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  25685. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  25686. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  25687. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  25688. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  25689. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  25690. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  25691. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  25692. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  25693. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  25694. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  25695. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  25696. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  25697. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  25698. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25699. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  25700. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  25701. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  25702. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  25703. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  25704. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  25705. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  25706. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  25707. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  25708. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  25709. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  25710. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  25711. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  25712. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  25713. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  25714. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  25715. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  25716. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  25717. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  25718. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  25719. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  25720. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  25721. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  25722. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  25723. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  25724. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  25725. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  25726. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  25727. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  25728. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  25729. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  25730. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  25731. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  25732. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  25733. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  25734. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  25735. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  25736. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  25737. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  25738. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  25739. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  25740. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  25741. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  25742. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  25743. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  25744. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  25745. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  25746. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  25747. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  25748. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  25749. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  25750. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25751. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25752. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25753. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25754. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25755. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25756. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25757. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25758. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25759. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25760. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25761. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25762. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25763. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25764. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25765. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25766. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25767. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25768. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25769. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25770. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25771. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25772. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25773. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25774. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25775. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25776. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25777. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25778. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25779. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25780. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25781. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25782. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25783. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25784. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25785. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25786. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25787. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25788. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25789. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25790. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25791. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25792. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25793. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25794. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25795. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25796. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25797. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25798. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25799. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25800. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25801. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25802. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25803. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25804. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25805. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25806. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25807. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25808. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25809. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25810. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25811. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25812. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25813. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25814. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25815. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25816. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25817. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25818. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25819. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25820. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25821. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25822. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25823. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25824. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25825. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25826. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25827. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25828. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25829. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25830. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25831. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25832. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25833. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25834. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25835. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25836. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25837. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25838. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25839. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25840. 0x00, 0x00, 0x00, 0x00, 0x00
  25841. };
  25842. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  25843. * !NO_DES3 && !NO_SHA
  25844. */
  25845. /*
  25846. * Testing wc_PKCS7_BER()
  25847. */
  25848. static int test_wc_PKCS7_BER(void)
  25849. {
  25850. EXPECT_DECLS;
  25851. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25852. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  25853. PKCS7* pkcs7 = NULL;
  25854. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25855. XFILE f = XBADFILE;
  25856. byte der[4096];
  25857. #ifndef NO_DES3
  25858. byte decoded[2048];
  25859. #endif
  25860. word32 derSz = 0;
  25861. #ifndef NO_PKCS7_STREAM
  25862. word32 z;
  25863. int ret;
  25864. #endif /* !NO_PKCS7_STREAM */
  25865. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25866. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25867. if (f != XBADFILE) {
  25868. XFCLOSE(f);
  25869. f = XBADFILE;
  25870. }
  25871. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25872. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25873. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25874. #ifndef NO_RSA
  25875. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25876. #ifndef NO_PKCS7_STREAM
  25877. wc_PKCS7_Free(pkcs7);
  25878. pkcs7 = NULL;
  25879. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25880. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25881. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25882. /* test for streaming */
  25883. ret = -1;
  25884. for (z = 0; z < derSz && ret != 0; z++) {
  25885. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25886. if (ret < 0){
  25887. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25888. }
  25889. }
  25890. ExpectIntEQ(ret, 0);
  25891. #endif /* !NO_PKCS7_STREAM */
  25892. #else
  25893. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25894. #endif
  25895. wc_PKCS7_Free(pkcs7);
  25896. pkcs7 = NULL;
  25897. #ifndef NO_DES3
  25898. /* decode BER content */
  25899. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  25900. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25901. if (f != XBADFILE) {
  25902. XFCLOSE(f);
  25903. f = XBADFILE;
  25904. }
  25905. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25906. #ifndef NO_RSA
  25907. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25908. #else
  25909. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25910. #endif
  25911. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  25912. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25913. if (f != XBADFILE) {
  25914. XFCLOSE(f);
  25915. f = XBADFILE;
  25916. }
  25917. if (pkcs7 != NULL) {
  25918. pkcs7->privateKey = der;
  25919. pkcs7->privateKeySz = derSz;
  25920. }
  25921. #ifndef NO_RSA
  25922. #ifdef WOLFSSL_SP_MATH
  25923. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25924. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25925. #else
  25926. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25927. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25928. #endif
  25929. #else
  25930. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25931. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  25932. #endif
  25933. wc_PKCS7_Free(pkcs7);
  25934. #endif /* !NO_DES3 */
  25935. #endif
  25936. return EXPECT_RESULT();
  25937. } /* END test_wc_PKCS7_BER() */
  25938. static int test_wc_PKCS7_signed_enveloped(void)
  25939. {
  25940. EXPECT_DECLS;
  25941. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  25942. !defined(NO_FILESYSTEM)
  25943. XFILE f = XBADFILE;
  25944. PKCS7* pkcs7 = NULL;
  25945. #ifdef HAVE_AES_CBC
  25946. PKCS7* inner = NULL;
  25947. #endif
  25948. WC_RNG rng;
  25949. unsigned char key[FOURK_BUF/2];
  25950. unsigned char cert[FOURK_BUF/2];
  25951. unsigned char env[FOURK_BUF];
  25952. int envSz = FOURK_BUF;
  25953. int keySz = 0;
  25954. int certSz = 0;
  25955. unsigned char sig[FOURK_BUF * 2];
  25956. int sigSz = FOURK_BUF * 2;
  25957. #ifdef HAVE_AES_CBC
  25958. unsigned char decoded[FOURK_BUF];
  25959. int decodedSz = FOURK_BUF;
  25960. #endif
  25961. #ifndef NO_PKCS7_STREAM
  25962. int z;
  25963. int ret;
  25964. #endif /* !NO_PKCS7_STREAM */
  25965. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25966. /* load cert */
  25967. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  25968. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  25969. if (f != XBADFILE) {
  25970. XFCLOSE(f);
  25971. f = XBADFILE;
  25972. }
  25973. /* load key */
  25974. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  25975. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  25976. if (f != XBADFILE) {
  25977. XFCLOSE(f);
  25978. f = XBADFILE;
  25979. }
  25980. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  25981. /* sign cert for envelope */
  25982. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25983. ExpectIntEQ(wc_InitRng(&rng), 0);
  25984. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25985. if (pkcs7 != NULL) {
  25986. pkcs7->content = cert;
  25987. pkcs7->contentSz = certSz;
  25988. pkcs7->contentOID = DATA;
  25989. pkcs7->privateKey = key;
  25990. pkcs7->privateKeySz = keySz;
  25991. pkcs7->encryptOID = RSAk;
  25992. pkcs7->hashOID = SHA256h;
  25993. pkcs7->rng = &rng;
  25994. }
  25995. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25996. wc_PKCS7_Free(pkcs7);
  25997. pkcs7 = NULL;
  25998. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25999. #ifdef HAVE_AES_CBC
  26000. /* create envelope */
  26001. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26002. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26003. if (pkcs7 != NULL) {
  26004. pkcs7->content = sig;
  26005. pkcs7->contentSz = sigSz;
  26006. pkcs7->contentOID = DATA;
  26007. pkcs7->encryptOID = AES256CBCb;
  26008. pkcs7->privateKey = key;
  26009. pkcs7->privateKeySz = keySz;
  26010. }
  26011. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26012. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26013. wc_PKCS7_Free(pkcs7);
  26014. pkcs7 = NULL;
  26015. #endif
  26016. /* create bad signed enveloped data */
  26017. sigSz = FOURK_BUF * 2;
  26018. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26019. ExpectIntEQ(wc_InitRng(&rng), 0);
  26020. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26021. if (pkcs7 != NULL) {
  26022. pkcs7->content = env;
  26023. pkcs7->contentSz = envSz;
  26024. pkcs7->contentOID = DATA;
  26025. pkcs7->privateKey = key;
  26026. pkcs7->privateKeySz = keySz;
  26027. pkcs7->encryptOID = RSAk;
  26028. pkcs7->hashOID = SHA256h;
  26029. pkcs7->rng = &rng;
  26030. }
  26031. /* Set no certs in bundle for this test. */
  26032. if (pkcs7 != NULL) {
  26033. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26034. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26035. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26036. }
  26037. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26038. wc_PKCS7_Free(pkcs7);
  26039. pkcs7 = NULL;
  26040. /* check verify fails */
  26041. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26042. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26043. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26044. PKCS7_SIGNEEDS_CHECK);
  26045. /* try verifying the signature manually */
  26046. {
  26047. RsaKey rKey;
  26048. word32 idx = 0;
  26049. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26050. WC_MAX_DIGEST_SIZE];
  26051. int digestSz = 0;
  26052. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26053. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26054. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26055. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26056. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26057. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26058. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26059. /* verify was success */
  26060. }
  26061. wc_PKCS7_Free(pkcs7);
  26062. pkcs7 = NULL;
  26063. /* initializing the PKCS7 struct with the signing certificate should pass */
  26064. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26065. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26066. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26067. #ifndef NO_PKCS7_STREAM
  26068. wc_PKCS7_Free(pkcs7);
  26069. pkcs7 = NULL;
  26070. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26071. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26072. /* test for streaming */
  26073. ret = -1;
  26074. for (z = 0; z < sigSz && ret != 0; z++) {
  26075. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26076. if (ret < 0){
  26077. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26078. }
  26079. }
  26080. ExpectIntEQ(ret, 0);
  26081. #endif /* !NO_PKCS7_STREAM */
  26082. wc_PKCS7_Free(pkcs7);
  26083. pkcs7 = NULL;
  26084. /* create valid degenerate bundle */
  26085. sigSz = FOURK_BUF * 2;
  26086. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26087. if (pkcs7 != NULL) {
  26088. pkcs7->content = env;
  26089. pkcs7->contentSz = envSz;
  26090. pkcs7->contentOID = DATA;
  26091. pkcs7->privateKey = key;
  26092. pkcs7->privateKeySz = keySz;
  26093. pkcs7->encryptOID = RSAk;
  26094. pkcs7->hashOID = SHA256h;
  26095. pkcs7->rng = &rng;
  26096. }
  26097. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26098. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26099. wc_PKCS7_Free(pkcs7);
  26100. pkcs7 = NULL;
  26101. wc_FreeRng(&rng);
  26102. /* check verify */
  26103. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26104. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26105. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26106. ExpectNotNull(pkcs7->content);
  26107. #ifndef NO_PKCS7_STREAM
  26108. wc_PKCS7_Free(pkcs7);
  26109. pkcs7 = NULL;
  26110. /* create valid degenerate bundle */
  26111. sigSz = FOURK_BUF * 2;
  26112. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26113. if (pkcs7 != NULL) {
  26114. pkcs7->content = env;
  26115. pkcs7->contentSz = envSz;
  26116. pkcs7->contentOID = DATA;
  26117. pkcs7->privateKey = key;
  26118. pkcs7->privateKeySz = keySz;
  26119. pkcs7->encryptOID = RSAk;
  26120. pkcs7->hashOID = SHA256h;
  26121. pkcs7->rng = &rng;
  26122. }
  26123. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26124. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26125. wc_PKCS7_Free(pkcs7);
  26126. pkcs7 = NULL;
  26127. wc_FreeRng(&rng);
  26128. /* check verify */
  26129. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26130. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26131. /* test for streaming */
  26132. ret = -1;
  26133. for (z = 0; z < sigSz && ret != 0; z++) {
  26134. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26135. if (ret < 0){
  26136. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26137. }
  26138. }
  26139. ExpectIntEQ(ret, 0);
  26140. #endif /* !NO_PKCS7_STREAM */
  26141. #ifdef HAVE_AES_CBC
  26142. /* check decode */
  26143. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26144. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26145. if (inner != NULL) {
  26146. inner->privateKey = key;
  26147. inner->privateKeySz = keySz;
  26148. }
  26149. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26150. pkcs7->contentSz, decoded, decodedSz)), 0);
  26151. wc_PKCS7_Free(inner);
  26152. inner = NULL;
  26153. #endif
  26154. wc_PKCS7_Free(pkcs7);
  26155. pkcs7 = NULL;
  26156. #ifdef HAVE_AES_CBC
  26157. /* check cert set */
  26158. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26159. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26160. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26161. ExpectNotNull(pkcs7->singleCert);
  26162. ExpectIntNE(pkcs7->singleCertSz, 0);
  26163. wc_PKCS7_Free(pkcs7);
  26164. pkcs7 = NULL;
  26165. #ifndef NO_PKCS7_STREAM
  26166. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26167. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26168. /* test for streaming */
  26169. ret = -1;
  26170. for (z = 0; z < decodedSz && ret != 0; z++) {
  26171. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26172. if (ret < 0){
  26173. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26174. }
  26175. }
  26176. ExpectIntEQ(ret, 0);
  26177. ExpectNotNull(pkcs7->singleCert);
  26178. ExpectIntNE(pkcs7->singleCertSz, 0);
  26179. wc_PKCS7_Free(pkcs7);
  26180. pkcs7 = NULL;
  26181. #endif /* !NO_PKCS7_STREAM */
  26182. #endif
  26183. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26184. return EXPECT_RESULT();
  26185. }
  26186. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26187. {
  26188. EXPECT_DECLS;
  26189. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26190. && !defined(NO_AES)
  26191. PKCS7* pkcs7 = NULL;
  26192. void* heap = NULL;
  26193. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26194. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26195. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26196. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26197. wc_PKCS7_Free(pkcs7);
  26198. #endif
  26199. return EXPECT_RESULT();
  26200. }
  26201. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26202. {
  26203. EXPECT_DECLS;
  26204. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26205. && !defined(NO_AES)
  26206. PKCS7* pkcs7 = NULL;
  26207. void* heap = NULL;
  26208. WOLFSSL_CTX* ctx = NULL;
  26209. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26210. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26211. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26212. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26213. wc_PKCS7_Free(pkcs7);
  26214. #endif
  26215. return EXPECT_RESULT();
  26216. }
  26217. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26218. {
  26219. EXPECT_DECLS;
  26220. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26221. && !defined(NO_AES)
  26222. PKCS7* pkcs7 = NULL;
  26223. void* heap = NULL;
  26224. WOLFSSL_CTX* ctx = NULL;
  26225. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26226. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26227. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26228. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26229. wc_PKCS7_Free(pkcs7);
  26230. #endif
  26231. return EXPECT_RESULT();
  26232. }
  26233. static int test_wc_PKCS7_DecodeCompressedData(void)
  26234. {
  26235. EXPECT_DECLS;
  26236. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26237. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26238. PKCS7* pkcs7 = NULL;
  26239. void* heap = NULL;
  26240. byte out[4096];
  26241. byte* decompressed = NULL;
  26242. int outSz;
  26243. int decompressedSz;
  26244. const char* cert = "./certs/client-cert.pem";
  26245. byte* cert_buf = NULL;
  26246. size_t cert_sz = 0;
  26247. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26248. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26249. DYNAMIC_TYPE_TMP_BUFFER)));
  26250. decompressedSz = (int)cert_sz;
  26251. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26252. if (pkcs7 != NULL) {
  26253. pkcs7->content = (byte*)cert_buf;
  26254. pkcs7->contentSz = (word32)cert_sz;
  26255. pkcs7->contentOID = DATA;
  26256. }
  26257. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26258. sizeof(out))), 0);
  26259. wc_PKCS7_Free(pkcs7);
  26260. pkcs7 = NULL;
  26261. /* compressed key should be smaller than when started */
  26262. ExpectIntLT(outSz, cert_sz);
  26263. /* test decompression */
  26264. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26265. ExpectIntEQ(pkcs7->contentOID, 0);
  26266. /* fail case with out buffer too small */
  26267. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26268. decompressed, outSz), 0);
  26269. /* success case */
  26270. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26271. decompressed, decompressedSz), cert_sz);
  26272. ExpectIntEQ(pkcs7->contentOID, DATA);
  26273. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26274. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26275. decompressed = NULL;
  26276. /* test decompression function with different 'max' inputs */
  26277. outSz = sizeof(out);
  26278. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26279. 0);
  26280. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26281. out, outSz, 0, heap), 0);
  26282. ExpectNull(decompressed);
  26283. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26284. out, outSz, 0, heap), 0);
  26285. ExpectNotNull(decompressed);
  26286. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26287. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26288. decompressed = NULL;
  26289. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26290. out, outSz, 0, heap), 0);
  26291. ExpectNotNull(decompressed);
  26292. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26293. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26294. if (cert_buf != NULL)
  26295. free(cert_buf);
  26296. wc_PKCS7_Free(pkcs7);
  26297. #endif
  26298. return EXPECT_RESULT();
  26299. }
  26300. static int test_wc_i2d_PKCS12(void)
  26301. {
  26302. EXPECT_DECLS;
  26303. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26304. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26305. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26306. WC_PKCS12* pkcs12 = NULL;
  26307. unsigned char der[FOURK_BUF * 2];
  26308. unsigned char* pt;
  26309. int derSz = 0;
  26310. unsigned char out[FOURK_BUF * 2];
  26311. int outSz = FOURK_BUF * 2;
  26312. const char p12_f[] = "./certs/test-servercert.p12";
  26313. XFILE f = XBADFILE;
  26314. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26315. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26316. if (f != XBADFILE)
  26317. XFCLOSE(f);
  26318. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26319. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26320. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26321. ExpectIntEQ(outSz, derSz);
  26322. outSz = derSz - 1;
  26323. pt = out;
  26324. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26325. outSz = derSz;
  26326. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26327. ExpectIntEQ((pt == out), 0);
  26328. pt = NULL;
  26329. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26330. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26331. wc_PKCS12_free(pkcs12);
  26332. pkcs12 = NULL;
  26333. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26334. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26335. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26336. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26337. ExpectIntEQ(outSz, derSz);
  26338. wc_PKCS12_free(pkcs12);
  26339. pkcs12 = NULL;
  26340. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26341. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26342. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26343. ExpectIntEQ(outSz, derSz);
  26344. wc_PKCS12_free(pkcs12);
  26345. pkcs12 = NULL;
  26346. #endif
  26347. return EXPECT_RESULT();
  26348. }
  26349. /* Testing wc_SignatureGetSize() for signature type ECC */
  26350. static int test_wc_SignatureGetSize_ecc(void)
  26351. {
  26352. EXPECT_DECLS;
  26353. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26354. enum wc_SignatureType sig_type;
  26355. word32 key_len;
  26356. ecc_key ecc;
  26357. const char* qx =
  26358. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26359. const char* qy =
  26360. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26361. const char* d =
  26362. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26363. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26364. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26365. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26366. /* Input for signature type ECC */
  26367. sig_type = WC_SIGNATURE_TYPE_ECC;
  26368. key_len = sizeof(ecc_key);
  26369. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26370. /* Test bad args */
  26371. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26372. sig_type = (enum wc_SignatureType) 100;
  26373. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26374. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26375. sig_type = WC_SIGNATURE_TYPE_ECC;
  26376. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26377. key_len = (word32)0;
  26378. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26379. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26380. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26381. return EXPECT_RESULT();
  26382. } /* END test_wc_SignatureGetSize_ecc() */
  26383. /* Testing wc_SignatureGetSize() for signature type rsa */
  26384. static int test_wc_SignatureGetSize_rsa(void)
  26385. {
  26386. EXPECT_DECLS;
  26387. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26388. enum wc_SignatureType sig_type;
  26389. word32 key_len;
  26390. word32 idx = 0;
  26391. RsaKey rsa_key;
  26392. byte* tmp = NULL;
  26393. size_t bytes;
  26394. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26395. #ifdef USE_CERT_BUFFERS_1024
  26396. bytes = (size_t)sizeof_client_key_der_1024;
  26397. if (bytes < (size_t)sizeof_client_key_der_1024)
  26398. bytes = (size_t)sizeof_client_cert_der_1024;
  26399. #elif defined(USE_CERT_BUFFERS_2048)
  26400. bytes = (size_t)sizeof_client_key_der_2048;
  26401. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26402. bytes = (size_t)sizeof_client_cert_der_2048;
  26403. #else
  26404. bytes = FOURK_BUF;
  26405. #endif
  26406. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  26407. DYNAMIC_TYPE_TMP_BUFFER));
  26408. if (tmp != NULL) {
  26409. #ifdef USE_CERT_BUFFERS_1024
  26410. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  26411. #elif defined(USE_CERT_BUFFERS_2048)
  26412. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  26413. #elif !defined(NO_FILESYSTEM)
  26414. XFILE file = XBADFILE;
  26415. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  26416. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  26417. if (file != XBADFILE)
  26418. XFCLOSE(file);
  26419. }
  26420. #else
  26421. ExpectFail();
  26422. #endif
  26423. }
  26424. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  26425. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  26426. /* Input for signature type RSA */
  26427. sig_type = WC_SIGNATURE_TYPE_RSA;
  26428. key_len = sizeof(RsaKey);
  26429. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  26430. /* Test bad args */
  26431. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26432. sig_type = (enum wc_SignatureType)100;
  26433. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26434. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26435. sig_type = WC_SIGNATURE_TYPE_RSA;
  26436. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  26437. key_len = (word32)0;
  26438. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26439. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  26440. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26441. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  26442. return EXPECT_RESULT();
  26443. } /* END test_wc_SignatureGetSize_rsa(void) */
  26444. /*----------------------------------------------------------------------------*
  26445. | hash.h Tests
  26446. *----------------------------------------------------------------------------*/
  26447. static int test_wc_HashInit(void)
  26448. {
  26449. EXPECT_DECLS;
  26450. int i; /* 0 indicates tests passed, 1 indicates failure */
  26451. wc_HashAlg hash;
  26452. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26453. enum wc_HashType enumArray[] = {
  26454. #ifndef NO_MD5
  26455. WC_HASH_TYPE_MD5,
  26456. #endif
  26457. #ifndef NO_SHA
  26458. WC_HASH_TYPE_SHA,
  26459. #endif
  26460. #ifdef WOLFSSL_SHA224
  26461. WC_HASH_TYPE_SHA224,
  26462. #endif
  26463. #ifndef NO_SHA256
  26464. WC_HASH_TYPE_SHA256,
  26465. #endif
  26466. #ifdef WOLFSSL_SHA384
  26467. WC_HASH_TYPE_SHA384,
  26468. #endif
  26469. #ifdef WOLFSSL_SHA512
  26470. WC_HASH_TYPE_SHA512,
  26471. #endif
  26472. };
  26473. /* dynamically finds the length */
  26474. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26475. /* For loop to test various arguments... */
  26476. for (i = 0; i < enumlen; i++) {
  26477. /* check for bad args */
  26478. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26479. wc_HashFree(&hash, enumArray[i]);
  26480. /* check for null ptr */
  26481. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  26482. } /* end of for loop */
  26483. return EXPECT_RESULT();
  26484. } /* end of test_wc_HashInit */
  26485. /*
  26486. * Unit test function for wc_HashSetFlags()
  26487. */
  26488. static int test_wc_HashSetFlags(void)
  26489. {
  26490. EXPECT_DECLS;
  26491. #ifdef WOLFSSL_HASH_FLAGS
  26492. wc_HashAlg hash;
  26493. word32 flags = 0;
  26494. int i, j;
  26495. int notSupportedLen;
  26496. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26497. enum wc_HashType enumArray[] = {
  26498. #ifndef NO_MD5
  26499. WC_HASH_TYPE_MD5,
  26500. #endif
  26501. #ifndef NO_SHA
  26502. WC_HASH_TYPE_SHA,
  26503. #endif
  26504. #ifdef WOLFSSL_SHA224
  26505. WC_HASH_TYPE_SHA224,
  26506. #endif
  26507. #ifndef NO_SHA256
  26508. WC_HASH_TYPE_SHA256,
  26509. #endif
  26510. #ifdef WOLFSSL_SHA384
  26511. WC_HASH_TYPE_SHA384,
  26512. #endif
  26513. #ifdef WOLFSSL_SHA512
  26514. WC_HASH_TYPE_SHA512,
  26515. #endif
  26516. #ifdef WOLFSSL_SHA3
  26517. WC_HASH_TYPE_SHA3_224,
  26518. #endif
  26519. };
  26520. enum wc_HashType notSupported[] = {
  26521. WC_HASH_TYPE_MD5_SHA,
  26522. WC_HASH_TYPE_MD2,
  26523. WC_HASH_TYPE_MD4,
  26524. WC_HASH_TYPE_BLAKE2B,
  26525. WC_HASH_TYPE_BLAKE2S,
  26526. WC_HASH_TYPE_NONE,
  26527. };
  26528. /* dynamically finds the length */
  26529. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26530. /* For loop to test various arguments... */
  26531. for (i = 0; i < enumlen; i++) {
  26532. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26533. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  26534. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26535. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  26536. wc_HashFree(&hash, enumArray[i]);
  26537. }
  26538. /* For loop to test not supported cases */
  26539. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26540. for (j = 0; j < notSupportedLen; j++) {
  26541. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26542. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  26543. BAD_FUNC_ARG);
  26544. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26545. }
  26546. #endif
  26547. return EXPECT_RESULT();
  26548. } /* END test_wc_HashSetFlags */
  26549. /*
  26550. * Unit test function for wc_HashGetFlags()
  26551. */
  26552. static int test_wc_HashGetFlags(void)
  26553. {
  26554. EXPECT_DECLS;
  26555. #ifdef WOLFSSL_HASH_FLAGS
  26556. wc_HashAlg hash;
  26557. word32 flags = 0;
  26558. int i, j;
  26559. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26560. enum wc_HashType enumArray[] = {
  26561. #ifndef NO_MD5
  26562. WC_HASH_TYPE_MD5,
  26563. #endif
  26564. #ifndef NO_SHA
  26565. WC_HASH_TYPE_SHA,
  26566. #endif
  26567. #ifdef WOLFSSL_SHA224
  26568. WC_HASH_TYPE_SHA224,
  26569. #endif
  26570. #ifndef NO_SHA256
  26571. WC_HASH_TYPE_SHA256,
  26572. #endif
  26573. #ifdef WOLFSSL_SHA384
  26574. WC_HASH_TYPE_SHA384,
  26575. #endif
  26576. #ifdef WOLFSSL_SHA512
  26577. WC_HASH_TYPE_SHA512,
  26578. #endif
  26579. #ifdef WOLFSSL_SHA3
  26580. WC_HASH_TYPE_SHA3_224,
  26581. #endif
  26582. };
  26583. enum wc_HashType notSupported[] = {
  26584. WC_HASH_TYPE_MD5_SHA,
  26585. WC_HASH_TYPE_MD2,
  26586. WC_HASH_TYPE_MD4,
  26587. WC_HASH_TYPE_BLAKE2B,
  26588. WC_HASH_TYPE_BLAKE2S,
  26589. WC_HASH_TYPE_NONE,
  26590. };
  26591. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26592. int notSupportedLen;
  26593. /* For loop to test various arguments... */
  26594. for (i = 0; i < enumlen; i++) {
  26595. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26596. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  26597. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26598. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  26599. wc_HashFree(&hash, enumArray[i]);
  26600. }
  26601. /* For loop to test not supported cases */
  26602. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26603. for (j = 0; j < notSupportedLen; j++) {
  26604. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26605. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  26606. BAD_FUNC_ARG);
  26607. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26608. }
  26609. #endif
  26610. return EXPECT_RESULT();
  26611. } /* END test_wc_HashGetFlags */
  26612. /*----------------------------------------------------------------------------*
  26613. | Compatibility Tests
  26614. *----------------------------------------------------------------------------*/
  26615. /*----------------------------------------------------------------------------*
  26616. | ASN.1 Tests
  26617. *----------------------------------------------------------------------------*/
  26618. static int test_wolfSSL_ASN1_BIT_STRING(void)
  26619. {
  26620. EXPECT_DECLS;
  26621. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  26622. ASN1_BIT_STRING* str = NULL;
  26623. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26624. /* Empty data testing. */
  26625. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  26626. ASN1_BIT_STRING_free(str);
  26627. str = NULL;
  26628. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26629. /* Invalid parameter testing. */
  26630. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  26631. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  26632. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  26633. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  26634. /* No bit string - bit is always 0. */
  26635. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  26636. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  26637. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26638. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  26639. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  26640. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  26641. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  26642. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26643. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  26644. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  26645. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  26646. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  26647. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  26648. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  26649. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  26650. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  26651. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  26652. ASN1_BIT_STRING_free(str);
  26653. ASN1_BIT_STRING_free(NULL);
  26654. #endif
  26655. return EXPECT_RESULT();
  26656. }
  26657. static int test_wolfSSL_ASN1_INTEGER(void)
  26658. {
  26659. EXPECT_DECLS;
  26660. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26661. ASN1_INTEGER* a = NULL;
  26662. ASN1_INTEGER* dup = NULL;
  26663. const unsigned char invalidLenDer[] = {
  26664. 0x02, 0x20, 0x00
  26665. };
  26666. const unsigned char longDer[] = {
  26667. 0x02, 0x20,
  26668. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26669. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26670. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  26671. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  26672. };
  26673. const unsigned char* p;
  26674. /* Invalid parameter testing. */
  26675. ASN1_INTEGER_free(NULL);
  26676. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  26677. ExpectNotNull(a = ASN1_INTEGER_new());
  26678. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  26679. ASN1_INTEGER_free(dup);
  26680. dup = NULL;
  26681. ASN1_INTEGER_free(a);
  26682. a = NULL;
  26683. p = longDer;
  26684. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  26685. p = longDer;
  26686. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  26687. ExpectPtrNE(p, longDer);
  26688. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  26689. ASN1_INTEGER_free(dup);
  26690. ASN1_INTEGER_free(a);
  26691. #endif
  26692. return EXPECT_RESULT();
  26693. }
  26694. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  26695. {
  26696. EXPECT_DECLS;
  26697. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26698. ASN1_INTEGER* a = NULL;
  26699. ASN1_INTEGER* b = NULL;
  26700. ExpectNotNull(a = ASN1_INTEGER_new());
  26701. ExpectNotNull(b = ASN1_INTEGER_new());
  26702. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  26703. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  26704. /* Invalid parameter testing. */
  26705. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  26706. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  26707. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  26708. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26709. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  26710. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26711. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  26712. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26713. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  26714. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26715. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  26716. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  26717. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  26718. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  26719. ASN1_INTEGER_free(b);
  26720. ASN1_INTEGER_free(a);
  26721. #endif
  26722. return EXPECT_RESULT();
  26723. }
  26724. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  26725. {
  26726. EXPECT_DECLS;
  26727. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26728. ASN1_INTEGER* ai = NULL;
  26729. ASN1_INTEGER* ai2 = NULL;
  26730. BIGNUM* bn = NULL;
  26731. BIGNUM* bn2 = NULL;
  26732. ExpectNotNull(ai = ASN1_INTEGER_new());
  26733. ExpectNotNull(bn2 = BN_new());
  26734. /* Invalid parameter testing. */
  26735. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  26736. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  26737. /* at the moment hard setting since no set function */
  26738. if (ai != NULL) {
  26739. ai->data[0] = 0xff; /* No DER encoding. */
  26740. ai->length = 1;
  26741. }
  26742. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26743. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26744. BN_free(bn);
  26745. bn = NULL;
  26746. #else
  26747. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  26748. #endif
  26749. if (ai != NULL) {
  26750. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26751. ai->data[1] = 0x04; /* bad length of integer */
  26752. ai->data[2] = 0x03;
  26753. ai->length = 3;
  26754. }
  26755. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26756. /* Interpreted as a number 0x020403. */
  26757. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26758. BN_free(bn);
  26759. bn = NULL;
  26760. #else
  26761. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  26762. #endif
  26763. if (ai != NULL) {
  26764. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26765. ai->data[1] = 0x01; /* length of integer */
  26766. ai->data[2] = 0x03;
  26767. ai->length = 3;
  26768. }
  26769. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  26770. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  26771. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26772. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26773. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26774. if (ai != NULL) {
  26775. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26776. ai->data[1] = 0x02; /* length of integer */
  26777. ai->data[2] = 0x00; /* padding byte to ensure positive */
  26778. ai->data[3] = 0xff;
  26779. ai->length = 4;
  26780. }
  26781. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26782. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26783. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26784. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26785. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26786. if (ai != NULL) {
  26787. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26788. ai->data[1] = 0x01; /* length of integer */
  26789. ai->data[2] = 0x00;
  26790. ai->length = 3;
  26791. }
  26792. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26793. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26794. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26795. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26796. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26797. if (ai != NULL) {
  26798. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  26799. ai->data[1] = 0x01; /* length of integer */
  26800. ai->data[2] = 0x01;
  26801. ai->length = 3;
  26802. ai->negative = 1;
  26803. }
  26804. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  26805. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  26806. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  26807. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  26808. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  26809. BN_free(bn2);
  26810. BN_free(bn);
  26811. ASN1_INTEGER_free(ai2);
  26812. ASN1_INTEGER_free(ai);
  26813. #endif
  26814. return EXPECT_RESULT();
  26815. }
  26816. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  26817. {
  26818. EXPECT_DECLS;
  26819. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26820. ASN1_INTEGER *a = NULL;
  26821. long val;
  26822. ExpectNotNull(a = ASN1_INTEGER_new());
  26823. /* Invalid parameter testing. */
  26824. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  26825. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  26826. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  26827. #else
  26828. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  26829. #endif
  26830. ASN1_INTEGER_free(a);
  26831. a = NULL;
  26832. ExpectNotNull(a = ASN1_INTEGER_new());
  26833. val = 0;
  26834. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  26835. ASN1_INTEGER_free(a);
  26836. a = NULL;
  26837. /* 0 */
  26838. ExpectNotNull(a = ASN1_INTEGER_new());
  26839. val = 0;
  26840. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26841. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26842. ASN1_INTEGER_free(a);
  26843. a = NULL;
  26844. /* 40 */
  26845. ExpectNotNull(a = ASN1_INTEGER_new());
  26846. val = 40;
  26847. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26848. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26849. ASN1_INTEGER_free(a);
  26850. a = NULL;
  26851. /* -40 */
  26852. ExpectNotNull(a = ASN1_INTEGER_new());
  26853. val = -40;
  26854. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26855. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26856. ASN1_INTEGER_free(a);
  26857. a = NULL;
  26858. /* 128 */
  26859. ExpectNotNull(a = ASN1_INTEGER_new());
  26860. val = 128;
  26861. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26862. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26863. ASN1_INTEGER_free(a);
  26864. a = NULL;
  26865. /* -128 */
  26866. ExpectNotNull(a = ASN1_INTEGER_new());
  26867. val = -128;
  26868. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26869. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26870. ASN1_INTEGER_free(a);
  26871. a = NULL;
  26872. /* 200 */
  26873. ExpectNotNull(a = ASN1_INTEGER_new());
  26874. val = 200;
  26875. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26876. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26877. ASN1_INTEGER_free(a);
  26878. a = NULL;
  26879. /* int max (2147483647) */
  26880. ExpectNotNull(a = ASN1_INTEGER_new());
  26881. val = 2147483647;
  26882. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26883. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26884. ASN1_INTEGER_free(a);
  26885. a = NULL;
  26886. /* int min (-2147483648) */
  26887. ExpectNotNull(a = ASN1_INTEGER_new());
  26888. val = -2147483647 - 1;
  26889. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26890. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26891. ASN1_INTEGER_free(a);
  26892. a = NULL;
  26893. /* long max positive */
  26894. ExpectNotNull(a = ASN1_INTEGER_new());
  26895. val = (long)(((unsigned long)-1) >> 1);
  26896. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  26897. ExpectTrue(ASN1_INTEGER_get(a) == val);
  26898. ASN1_INTEGER_free(a);
  26899. #endif
  26900. return EXPECT_RESULT();
  26901. }
  26902. #if defined(OPENSSL_EXTRA)
  26903. typedef struct ASN1IntTestVector {
  26904. const byte* der;
  26905. const size_t derSz;
  26906. const long value;
  26907. } ASN1IntTestVector;
  26908. #endif
  26909. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  26910. {
  26911. EXPECT_DECLS;
  26912. #if defined(OPENSSL_EXTRA)
  26913. size_t i;
  26914. WOLFSSL_ASN1_INTEGER* a = NULL;
  26915. WOLFSSL_ASN1_INTEGER* b = NULL;
  26916. WOLFSSL_ASN1_INTEGER* c = NULL;
  26917. const byte* p = NULL;
  26918. byte* p2 = NULL;
  26919. byte* reEncoded = NULL;
  26920. int reEncodedSz = 0;
  26921. static const byte zeroDer[] = {
  26922. 0x02, 0x01, 0x00
  26923. };
  26924. static const byte oneDer[] = {
  26925. 0x02, 0x01, 0x01
  26926. };
  26927. static const byte negativeDer[] = {
  26928. 0x02, 0x03, 0xC1, 0x16, 0x0D
  26929. };
  26930. static const byte positiveDer[] = {
  26931. 0x02, 0x03, 0x01, 0x00, 0x01
  26932. };
  26933. static const byte primeDer[] = {
  26934. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  26935. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  26936. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  26937. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  26938. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  26939. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  26940. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  26941. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  26942. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  26943. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  26944. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  26945. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  26946. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  26947. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  26948. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  26949. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  26950. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  26951. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  26952. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  26953. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  26954. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  26955. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  26956. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  26957. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  26958. };
  26959. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  26960. static const ASN1IntTestVector testVectors[] = {
  26961. {zeroDer, sizeof(zeroDer), 0},
  26962. {oneDer, sizeof(oneDer), 1},
  26963. {negativeDer, sizeof(negativeDer), -4123123},
  26964. {positiveDer, sizeof(positiveDer), 65537},
  26965. {primeDer, sizeof(primeDer), 0}
  26966. };
  26967. static const size_t NUM_TEST_VECTORS =
  26968. sizeof(testVectors)/sizeof(testVectors[0]);
  26969. /* Check d2i error conditions */
  26970. /* NULL pointer to input. */
  26971. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  26972. ExpectNull(b);
  26973. /* NULL input. */
  26974. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  26975. ExpectNull(b);
  26976. /* 0 length. */
  26977. p = testVectors[0].der;
  26978. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  26979. ExpectNull(b);
  26980. /* Negative length. */
  26981. p = testVectors[0].der;
  26982. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  26983. ExpectNull(b);
  26984. /* Garbage DER input. */
  26985. p = garbageDer;
  26986. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  26987. ExpectNull(b);
  26988. /* Check i2d error conditions */
  26989. /* NULL input. */
  26990. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  26991. /* 0 length input data buffer (a->length == 0). */
  26992. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  26993. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  26994. if (a != NULL)
  26995. a->data = NULL;
  26996. /* NULL input data buffer. */
  26997. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  26998. if (a != NULL) {
  26999. /* Reset a->data. */
  27000. a->data = a->intData;
  27001. }
  27002. /* Set a to valid value. */
  27003. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27004. /* NULL output buffer. */
  27005. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27006. wolfSSL_ASN1_INTEGER_free(a);
  27007. a = NULL;
  27008. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27009. p = testVectors[i].der;
  27010. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  27011. testVectors[i].derSz));
  27012. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27013. if (testVectors[i].derSz <= sizeof(long)) {
  27014. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  27015. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  27016. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27017. wolfSSL_ASN1_INTEGER_free(c);
  27018. c = NULL;
  27019. }
  27020. /* Convert to DER without a pre-allocated output buffer. */
  27021. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27022. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27023. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27024. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27025. * output buffer pointer should be incremented just past the end of the
  27026. * encoded data. */
  27027. p2 = reEncoded;
  27028. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  27029. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27030. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27031. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27032. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27033. reEncoded = NULL;
  27034. wolfSSL_ASN1_INTEGER_free(a);
  27035. a = NULL;
  27036. }
  27037. #endif /* OPENSSL_EXTRA */
  27038. return EXPECT_RESULT();
  27039. }
  27040. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27041. {
  27042. EXPECT_DECLS;
  27043. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27044. BIO* bio = NULL;
  27045. BIO* out = NULL;
  27046. BIO* fixed = NULL;
  27047. ASN1_INTEGER* ai = NULL;
  27048. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27049. char tmp[1024];
  27050. int tmpSz;
  27051. const char expected1[] = "123456";
  27052. const char expected2[] = "112345678912345678901234567890";
  27053. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27054. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27055. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27056. ExpectNotNull(ai = ASN1_INTEGER_new());
  27057. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27058. /* Invalid parameter testing. */
  27059. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27060. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27061. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27062. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27063. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27064. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27065. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27066. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27067. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27068. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27069. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27070. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27071. /* No data to read from BIO. */
  27072. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27073. /* read first line */
  27074. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27075. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27076. XMEMSET(tmp, 0, 1024);
  27077. tmpSz = BIO_read(out, tmp, 1024);
  27078. ExpectIntEQ(tmpSz, 6);
  27079. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27080. /* fail on second line (not % 2) */
  27081. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27082. /* read 3rd long line */
  27083. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27084. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27085. XMEMSET(tmp, 0, 1024);
  27086. tmpSz = BIO_read(out, tmp, 1024);
  27087. ExpectIntEQ(tmpSz, 30);
  27088. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27089. /* fail on empty line */
  27090. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27091. BIO_free(bio);
  27092. bio = NULL;
  27093. /* Make long integer, requiring dynamic memory, even longer. */
  27094. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27095. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27096. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27097. XMEMSET(tmp, 0, 1024);
  27098. tmpSz = BIO_read(out, tmp, 1024);
  27099. ExpectIntEQ(tmpSz, 48);
  27100. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27101. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27102. XMEMSET(tmp, 0, 1024);
  27103. tmpSz = BIO_read(out, tmp, 1024);
  27104. ExpectIntEQ(tmpSz, 56);
  27105. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27106. BIO_free(bio);
  27107. BIO_free(out);
  27108. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27109. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27110. /* Ensure there is 0 bytes available to write into. */
  27111. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27112. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27113. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27114. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27115. BIO_free(fixed);
  27116. ASN1_INTEGER_free(ai);
  27117. #endif
  27118. return EXPECT_RESULT();
  27119. }
  27120. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27121. {
  27122. EXPECT_DECLS;
  27123. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27124. ASN1_INTEGER *a = NULL;
  27125. unsigned char *pp = NULL,*tpp = NULL;
  27126. int ret = 0;
  27127. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27128. /* Invalid parameter testing. */
  27129. /* Set pp to an invalid value. */
  27130. pp = NULL;
  27131. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27132. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27133. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27134. /* 40 */
  27135. if (a != NULL) {
  27136. a->intData[0] = ASN_INTEGER;
  27137. a->intData[1] = 1;
  27138. a->intData[2] = 40;
  27139. }
  27140. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27141. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27142. DYNAMIC_TYPE_TMP_BUFFER));
  27143. tpp = pp;
  27144. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27145. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27146. tpp--;
  27147. ExpectIntEQ(*tpp, 40);
  27148. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27149. pp = NULL;
  27150. /* 128 */
  27151. if (a != NULL) {
  27152. a->intData[0] = ASN_INTEGER;
  27153. a->intData[1] = 1;
  27154. a->intData[2] = 128;
  27155. }
  27156. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27157. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27158. DYNAMIC_TYPE_TMP_BUFFER));
  27159. tpp = pp;
  27160. if (tpp != NULL) {
  27161. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27162. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27163. tpp--;
  27164. ExpectIntEQ(*(tpp--), 128);
  27165. ExpectIntEQ(*tpp, 0);
  27166. }
  27167. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27168. pp = NULL;
  27169. /* -40 */
  27170. if (a != NULL) {
  27171. a->intData[0] = ASN_INTEGER;
  27172. a->intData[1] = 1;
  27173. a->intData[2] = 40;
  27174. a->negative = 1;
  27175. }
  27176. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27177. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27178. DYNAMIC_TYPE_TMP_BUFFER));
  27179. tpp = pp;
  27180. if (tpp != NULL) {
  27181. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27182. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27183. tpp--;
  27184. ExpectIntEQ(*tpp, 216);
  27185. }
  27186. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27187. pp = NULL;
  27188. /* -128 */
  27189. if (a != NULL) {
  27190. a->intData[0] = ASN_INTEGER;
  27191. a->intData[1] = 1;
  27192. a->intData[2] = 128;
  27193. a->negative = 1;
  27194. }
  27195. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27196. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27197. DYNAMIC_TYPE_TMP_BUFFER));
  27198. tpp = pp;
  27199. if (tpp != NULL) {
  27200. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27201. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27202. tpp--;
  27203. ExpectIntEQ(*tpp, 128);
  27204. }
  27205. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27206. pp = NULL;
  27207. /* -200 */
  27208. if (a != NULL) {
  27209. a->intData[0] = ASN_INTEGER;
  27210. a->intData[1] = 1;
  27211. a->intData[2] = 200;
  27212. a->negative = 1;
  27213. }
  27214. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27215. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27216. DYNAMIC_TYPE_TMP_BUFFER));
  27217. tpp = pp;
  27218. if (tpp != NULL) {
  27219. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27220. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27221. tpp--;
  27222. ExpectIntEQ(*(tpp--), 56);
  27223. ExpectIntEQ(*tpp, 255);
  27224. }
  27225. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27226. pp = NULL;
  27227. /* Empty */
  27228. if (a != NULL) {
  27229. a->intData[0] = ASN_INTEGER;
  27230. a->intData[1] = 0;
  27231. a->negative = 0;
  27232. }
  27233. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27234. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27235. DYNAMIC_TYPE_TMP_BUFFER));
  27236. tpp = pp;
  27237. if (tpp != NULL) {
  27238. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27239. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27240. tpp--;
  27241. ExpectIntEQ(*tpp, 0);
  27242. }
  27243. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27244. pp = NULL;
  27245. /* 0 */
  27246. if (a != NULL) {
  27247. a->intData[0] = ASN_INTEGER;
  27248. a->intData[1] = 1;
  27249. a->intData[2] = 0;
  27250. a->negative = 1;
  27251. }
  27252. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27253. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27254. DYNAMIC_TYPE_TMP_BUFFER));
  27255. if (tpp != NULL) {
  27256. tpp = pp;
  27257. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27258. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27259. tpp--;
  27260. ExpectIntEQ(*tpp, 0);
  27261. }
  27262. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27263. pp = NULL;
  27264. /* 0x100 */
  27265. if (a != NULL) {
  27266. a->intData[0] = ASN_INTEGER;
  27267. a->intData[1] = 2;
  27268. a->intData[2] = 0x01;
  27269. a->intData[3] = 0x00;
  27270. a->negative = 0;
  27271. }
  27272. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27273. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27274. DYNAMIC_TYPE_TMP_BUFFER));
  27275. if (tpp != NULL) {
  27276. tpp = pp;
  27277. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27278. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27279. tpp -= 2;
  27280. ExpectIntEQ(tpp[0], 0x01);
  27281. ExpectIntEQ(tpp[1], 0x00);
  27282. }
  27283. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27284. pp = NULL;
  27285. /* -0x8000 => 0x8000 */
  27286. if (a != NULL) {
  27287. a->intData[0] = ASN_INTEGER;
  27288. a->intData[1] = 2;
  27289. a->intData[2] = 0x80;
  27290. a->intData[3] = 0x00;
  27291. a->negative = 1;
  27292. }
  27293. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27294. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27295. DYNAMIC_TYPE_TMP_BUFFER));
  27296. tpp = pp;
  27297. if (tpp != NULL) {
  27298. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27299. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27300. tpp -= 2;
  27301. ExpectIntEQ(tpp[0], 0x80);
  27302. ExpectIntEQ(tpp[1], 0x00);
  27303. }
  27304. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27305. pp = NULL;
  27306. /* -0x8001 => 0xFF7FFF */
  27307. if (a != NULL) {
  27308. a->intData[0] = ASN_INTEGER;
  27309. a->intData[1] = 2;
  27310. a->intData[2] = 0x80;
  27311. a->intData[3] = 0x01;
  27312. a->negative = 1;
  27313. }
  27314. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27315. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27316. DYNAMIC_TYPE_TMP_BUFFER));
  27317. tpp = pp;
  27318. if (tpp != NULL) {
  27319. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27320. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27321. tpp -= 3;
  27322. ExpectIntEQ(tpp[0], 0xFF);
  27323. ExpectIntEQ(tpp[1], 0x7F);
  27324. ExpectIntEQ(tpp[2], 0xFF);
  27325. }
  27326. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27327. wolfSSL_ASN1_INTEGER_free(a);
  27328. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27329. return EXPECT_RESULT();
  27330. }
  27331. static int test_wolfSSL_ASN1_OBJECT(void)
  27332. {
  27333. EXPECT_DECLS;
  27334. #if defined(OPENSSL_EXTRA)
  27335. ASN1_OBJECT* a = NULL;
  27336. ASN1_OBJECT s;
  27337. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27338. /* Invalid parameter testing. */
  27339. ASN1_OBJECT_free(NULL);
  27340. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27341. /* Test that a static ASN1_OBJECT can be freed. */
  27342. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27343. ASN1_OBJECT_free(&s);
  27344. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27345. ASN1_OBJECT_free(a);
  27346. a = NULL;
  27347. s.obj = der;
  27348. s.objSz = sizeof(der);
  27349. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27350. ASN1_OBJECT_free(a);
  27351. ASN1_OBJECT_free(&s);
  27352. #endif /* OPENSSL_EXTRA */
  27353. return EXPECT_RESULT();
  27354. }
  27355. static int test_wolfSSL_ASN1_get_object(void)
  27356. {
  27357. EXPECT_DECLS;
  27358. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27359. const unsigned char* derBuf = cliecc_cert_der_256;
  27360. const unsigned char* nullPtr = NULL;
  27361. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27362. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27363. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27364. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27365. const unsigned char* p;
  27366. unsigned char objDer[8];
  27367. unsigned char* der;
  27368. unsigned char* derPtr;
  27369. int len = sizeof_cliecc_cert_der_256;
  27370. long asnLen = 0;
  27371. int tag = 0;
  27372. int cls = 0;
  27373. ASN1_OBJECT* a = NULL;
  27374. ASN1_OBJECT s;
  27375. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27376. /* Invalid encoding at length. */
  27377. p = objDerInvalidLen;
  27378. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27379. 0x80);
  27380. p = objDerBadLen;
  27381. /* Error = 0x80, Constructed = 0x20 */
  27382. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27383. 0x80 | 0x20);
  27384. /* Read a couple TLV triplets and make sure they match the expected values
  27385. */
  27386. /* SEQUENCE */
  27387. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27388. ExpectIntEQ(asnLen, 861);
  27389. ExpectIntEQ(tag, 0x10);
  27390. ExpectIntEQ(cls, 0);
  27391. /* SEQUENCE */
  27392. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27393. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27394. ExpectIntEQ(asnLen, 772);
  27395. ExpectIntEQ(tag, 0x10);
  27396. ExpectIntEQ(cls, 0);
  27397. /* [0] */
  27398. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27399. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27400. ExpectIntEQ(asnLen, 3);
  27401. ExpectIntEQ(tag, 0);
  27402. ExpectIntEQ(cls, 0x80);
  27403. /* INTEGER */
  27404. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27405. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27406. ExpectIntEQ(asnLen, 1);
  27407. ExpectIntEQ(tag, 0x2);
  27408. ExpectIntEQ(cls, 0);
  27409. derBuf += asnLen;
  27410. /* INTEGER */
  27411. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27412. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27413. ExpectIntEQ(asnLen, 20);
  27414. ExpectIntEQ(tag, 0x2);
  27415. ExpectIntEQ(cls, 0);
  27416. derBuf += asnLen;
  27417. /* SEQUENCE */
  27418. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27419. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27420. ExpectIntEQ(asnLen, 10);
  27421. ExpectIntEQ(tag, 0x10);
  27422. ExpectIntEQ(cls, 0);
  27423. /* Found OBJECT_ID. */
  27424. /* Invalid parameter testing. */
  27425. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  27426. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  27427. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  27428. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  27429. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  27430. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  27431. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  27432. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  27433. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  27434. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  27435. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27436. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27437. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  27438. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  27439. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  27440. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  27441. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  27442. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27443. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27444. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  27445. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  27446. /* Invalid encoding at length. */
  27447. p = objDerInvalidLen;
  27448. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  27449. p = objDerBadLen;
  27450. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  27451. p = objDerNotObj;
  27452. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  27453. p = objDerNoData;
  27454. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  27455. /* Create an ASN OBJECT from content */
  27456. p = derBuf + 2;
  27457. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  27458. ASN1_OBJECT_free(a);
  27459. a = NULL;
  27460. /* Create an ASN OBJECT from DER */
  27461. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  27462. /* Invalid parameter testing. */
  27463. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  27464. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  27465. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  27466. der = NULL;
  27467. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  27468. derPtr = objDer;
  27469. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  27470. ExpectPtrNE(derPtr, objDer);
  27471. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  27472. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  27473. ASN1_OBJECT_free(a);
  27474. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27475. return EXPECT_RESULT();
  27476. }
  27477. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  27478. {
  27479. EXPECT_DECLS;
  27480. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27481. ASN1_OBJECT* obj = NULL;
  27482. ASN1_OBJECT* a = NULL;
  27483. BIO *bio = NULL;
  27484. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  27485. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  27486. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  27487. const unsigned char* p;
  27488. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  27489. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  27490. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  27491. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  27492. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  27493. /* No DER encoding in ASN1_OBJECT. */
  27494. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  27495. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27496. ASN1_OBJECT_free(a);
  27497. a = NULL;
  27498. /* DER encoding - not OBJECT_ID */
  27499. p = notObjDer;
  27500. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27501. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27502. ASN1_OBJECT_free(a);
  27503. a = NULL;
  27504. /* Bad length encoding. */
  27505. p = badLenDer;
  27506. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27507. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27508. ASN1_OBJECT_free(a);
  27509. a = NULL;
  27510. /* Good encoding - but unknown. */
  27511. p = goodDer;
  27512. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27513. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27514. ASN1_OBJECT_free(a);
  27515. BIO_free(bio);
  27516. ASN1_OBJECT_free(obj);
  27517. #endif
  27518. return EXPECT_RESULT();
  27519. }
  27520. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  27521. {
  27522. EXPECT_DECLS;
  27523. #if defined(OPENSSL_EXTRA) && \
  27524. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  27525. char buf[50] = {0};
  27526. ASN1_OBJECT* obj;
  27527. const char* oid = "2.5.29.19";
  27528. const char* ln = "X509v3 Basic Constraints";
  27529. obj = NULL;
  27530. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  27531. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  27532. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  27533. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  27534. XMEMSET(buf, 0, sizeof(buf));
  27535. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  27536. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  27537. ASN1_OBJECT_free(obj);
  27538. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  27539. return EXPECT_RESULT();
  27540. }
  27541. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  27542. {
  27543. EXPECT_DECLS;
  27544. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  27545. WOLFSSL_STACK* sk = NULL;
  27546. WOLFSSL_ASN1_OBJECT* obj;
  27547. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27548. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27549. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27550. sk = NULL;
  27551. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27552. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  27553. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  27554. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  27555. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27556. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  27557. sk = NULL;
  27558. /* obj freed in pop_free call. */
  27559. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27560. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27561. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27562. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  27563. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27564. wolfSSL_ASN1_OBJECT_free(obj);
  27565. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  27566. return EXPECT_RESULT();
  27567. }
  27568. static int test_wolfSSL_ASN1_STRING(void)
  27569. {
  27570. EXPECT_DECLS;
  27571. #if defined(OPENSSL_EXTRA)
  27572. ASN1_STRING* str = NULL;
  27573. ASN1_STRING* c = NULL;
  27574. const char data[] = "hello wolfSSL";
  27575. const char data2[] = "Same len data";
  27576. const char longData[] =
  27577. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  27578. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27579. ASN1_STRING_free(str);
  27580. str = NULL;
  27581. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27582. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  27583. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  27584. /* Check setting to NULL works. */
  27585. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  27586. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  27587. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27588. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  27589. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  27590. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  27591. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  27592. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  27593. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  27594. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  27595. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  27596. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  27597. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  27598. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27599. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  27600. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  27601. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  27602. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  27603. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27604. /* Check setting back to smaller size frees dynamic data. */
  27605. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27606. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  27607. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  27608. ExpectNull(ASN1_STRING_get0_data(NULL));
  27609. ExpectNotNull(ASN1_STRING_get0_data(str));
  27610. ExpectNull(ASN1_STRING_data(NULL));
  27611. ExpectNotNull(ASN1_STRING_data(str));
  27612. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  27613. ExpectIntGT(ASN1_STRING_length(str), 0);
  27614. ASN1_STRING_free(c);
  27615. ASN1_STRING_free(str);
  27616. ASN1_STRING_free(NULL);
  27617. #ifndef NO_WOLFSSL_STUB
  27618. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  27619. #endif
  27620. #endif
  27621. return EXPECT_RESULT();
  27622. }
  27623. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  27624. {
  27625. EXPECT_DECLS;
  27626. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  27627. !defined(NO_FILESYSTEM)
  27628. WOLFSSL_X509* x509 = NULL;
  27629. WOLFSSL_X509_NAME* subject = NULL;
  27630. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  27631. WOLFSSL_ASN1_STRING* a = NULL;
  27632. FILE* file = XBADFILE;
  27633. int idx = 0;
  27634. char targetOutput[16] = "www.wolfssl.com";
  27635. unsigned char* actual_output = NULL;
  27636. int len = 0;
  27637. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27638. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27639. if (file != NULL)
  27640. fclose(file);
  27641. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  27642. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  27643. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  27644. NID_commonName, -1)), 5);
  27645. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  27646. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  27647. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  27648. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  27649. a = NULL;
  27650. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  27651. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  27652. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  27653. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  27654. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  27655. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  27656. wolfSSL_X509_free(x509);
  27657. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27658. ExpectNotNull(a = ASN1_STRING_new());
  27659. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  27660. ASN1_STRING_free(a);
  27661. #endif
  27662. return EXPECT_RESULT();
  27663. }
  27664. static int test_wolfSSL_i2s_ASN1_STRING(void)
  27665. {
  27666. EXPECT_DECLS;
  27667. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27668. WOLFSSL_ASN1_STRING* str = NULL;
  27669. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  27670. char* ret = NULL;
  27671. ExpectNotNull(str = ASN1_STRING_new());
  27672. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  27673. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27674. ret = NULL;
  27675. /* No data. */
  27676. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27677. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27678. ret = NULL;
  27679. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  27680. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27681. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27682. ret = NULL;
  27683. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  27684. /* No type. */
  27685. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  27686. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27687. ASN1_STRING_free(str);
  27688. #endif
  27689. return EXPECT_RESULT();
  27690. }
  27691. static int test_wolfSSL_ASN1_STRING_canon(void)
  27692. {
  27693. EXPECT_DECLS;
  27694. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  27695. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  27696. defined(OPENSSL_EXTRA_X509_SMALL))
  27697. WOLFSSL_ASN1_STRING* orig = NULL;
  27698. WOLFSSL_ASN1_STRING* canon = NULL;
  27699. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  27700. const char* whitespaceOnly = "\t\r\n";
  27701. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  27702. const char* canonData = "\x01 \x02 \xff tt";
  27703. const char longData[] =
  27704. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  27705. ExpectNotNull(orig = ASN1_STRING_new());
  27706. ExpectNotNull(canon = ASN1_STRING_new());
  27707. /* Invalid parameter testing. */
  27708. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  27709. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  27710. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  27711. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27712. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27713. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  27714. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27715. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27716. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  27717. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27718. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27719. ASN1_STRING_free(orig);
  27720. orig = NULL;
  27721. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  27722. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  27723. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27724. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  27725. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27726. ASN1_STRING_free(orig);
  27727. orig = NULL;
  27728. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  27729. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  27730. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  27731. ASN1_STRING_free(orig);
  27732. orig = NULL;
  27733. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  27734. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  27735. ASN1_STRING_free(orig);
  27736. ASN1_STRING_free(canon);
  27737. #endif
  27738. #endif
  27739. return EXPECT_RESULT();
  27740. }
  27741. static int test_wolfSSL_ASN1_STRING_print(void)
  27742. {
  27743. EXPECT_DECLS;
  27744. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  27745. !defined(NO_BIO)
  27746. ASN1_STRING* asnStr = NULL;
  27747. const char HELLO_DATA[]= \
  27748. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  27749. #define MAX_UNPRINTABLE_CHAR 32
  27750. #define MAX_BUF 255
  27751. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  27752. unsigned char expected[sizeof(unprintableData)+1];
  27753. unsigned char rbuf[MAX_BUF];
  27754. BIO *bio = NULL;
  27755. int p_len;
  27756. int i;
  27757. /* setup */
  27758. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  27759. unprintableData[i] = HELLO_DATA[i];
  27760. expected[i] = HELLO_DATA[i];
  27761. }
  27762. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  27763. unprintableData[sizeof(HELLO_DATA)+i] = i;
  27764. if (i == (int)'\n' || i == (int)'\r')
  27765. expected[sizeof(HELLO_DATA)+i] = i;
  27766. else
  27767. expected[sizeof(HELLO_DATA)+i] = '.';
  27768. }
  27769. unprintableData[sizeof(unprintableData)-1] = '\0';
  27770. expected[sizeof(expected)-1] = '\0';
  27771. XMEMSET(rbuf, 0, MAX_BUF);
  27772. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27773. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  27774. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27775. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  27776. (int)sizeof(unprintableData)), 1);
  27777. /* test */
  27778. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  27779. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  27780. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  27781. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  27782. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  27783. ExpectStrEQ((char*)rbuf, (const char*)expected);
  27784. BIO_free(bio);
  27785. bio = NULL;
  27786. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27787. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  27788. /* Ensure there is 0 bytes available to write into. */
  27789. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  27790. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27791. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  27792. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27793. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  27794. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  27795. BIO_free(bio);
  27796. ASN1_STRING_free(asnStr);
  27797. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  27798. return EXPECT_RESULT();
  27799. }
  27800. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  27801. {
  27802. EXPECT_DECLS;
  27803. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27804. ASN1_STRING* asn_str = NULL;
  27805. const char data[] = "Hello wolfSSL!";
  27806. ASN1_STRING* esc_str = NULL;
  27807. const char esc_data[] = "a+;<>";
  27808. ASN1_STRING* neg_int = NULL;
  27809. const char neg_int_data[] = "\xff";
  27810. ASN1_STRING* neg_enum = NULL;
  27811. const char neg_enum_data[] = "\xff";
  27812. BIO *bio = NULL;
  27813. BIO *fixed = NULL;
  27814. unsigned long flags;
  27815. int p_len;
  27816. unsigned char rbuf[255];
  27817. /* setup */
  27818. XMEMSET(rbuf, 0, 255);
  27819. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27820. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  27821. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27822. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27823. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  27824. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27825. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  27826. sizeof(esc_data)), 1);
  27827. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  27828. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  27829. sizeof(neg_int_data) - 1), 1);
  27830. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  27831. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  27832. sizeof(neg_enum_data) - 1), 1);
  27833. /* Invalid parameter testing. */
  27834. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  27835. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  27836. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  27837. /* no flags */
  27838. XMEMSET(rbuf, 0, 255);
  27839. flags = 0;
  27840. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  27841. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  27842. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  27843. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27844. /* Ensure there is 0 bytes available to write into. */
  27845. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27846. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27847. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27848. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27849. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  27850. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27851. /* RFC2253 Escape */
  27852. XMEMSET(rbuf, 0, 255);
  27853. flags = ASN1_STRFLGS_ESC_2253;
  27854. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  27855. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  27856. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  27857. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27858. /* Ensure there is 0 bytes available to write into. */
  27859. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27860. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27861. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27862. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27863. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  27864. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  27865. /* Show type */
  27866. XMEMSET(rbuf, 0, 255);
  27867. flags = ASN1_STRFLGS_SHOW_TYPE;
  27868. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  27869. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  27870. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  27871. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27872. /* Ensure there is 0 bytes available to write into. */
  27873. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27874. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27875. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27876. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27877. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  27878. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27879. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  27880. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27881. /* Dump All */
  27882. XMEMSET(rbuf, 0, 255);
  27883. flags = ASN1_STRFLGS_DUMP_ALL;
  27884. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  27885. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  27886. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  27887. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27888. /* Ensure there is 0 bytes available to write into. */
  27889. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27890. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27891. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27892. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27893. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  27894. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27895. /* Dump Der */
  27896. XMEMSET(rbuf, 0, 255);
  27897. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  27898. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  27899. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  27900. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  27901. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27902. /* Ensure there is 0 bytes available to write into. */
  27903. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  27904. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27905. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27906. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27907. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  27908. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27909. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  27910. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  27911. /* Dump All + Show type */
  27912. XMEMSET(rbuf, 0, 255);
  27913. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27914. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  27915. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  27916. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  27917. /* Dump All + Show type - Negative Integer. */
  27918. XMEMSET(rbuf, 0, 255);
  27919. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27920. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  27921. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  27922. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  27923. /* Dump All + Show type - Negative Enumerated. */
  27924. XMEMSET(rbuf, 0, 255);
  27925. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  27926. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  27927. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  27928. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  27929. BIO_free(fixed);
  27930. BIO_free(bio);
  27931. ASN1_STRING_free(asn_str);
  27932. ASN1_STRING_free(esc_str);
  27933. ASN1_STRING_free(neg_int);
  27934. ASN1_STRING_free(neg_enum);
  27935. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  27936. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  27937. #endif
  27938. return EXPECT_RESULT();
  27939. }
  27940. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  27941. {
  27942. EXPECT_DECLS;
  27943. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  27944. ASN1_STRING* asn1str_test = NULL;
  27945. ASN1_STRING* asn1str_answer = NULL;
  27946. /* Each character is encoded using 4 bytes */
  27947. char input[] = {
  27948. 0, 0, 0, 'T',
  27949. 0, 0, 0, 'e',
  27950. 0, 0, 0, 's',
  27951. 0, 0, 0, 't',
  27952. };
  27953. char output[] = "Test";
  27954. char badInput[] = {
  27955. 1, 0, 0, 'T',
  27956. 0, 1, 0, 'e',
  27957. 0, 0, 1, 's',
  27958. };
  27959. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  27960. /* Test wrong type. */
  27961. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27962. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27963. ASN1_STRING_free(asn1str_test);
  27964. asn1str_test = NULL;
  27965. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  27966. /* Test bad length. */
  27967. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  27968. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27969. /* Test bad input. */
  27970. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  27971. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27972. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  27973. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27974. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  27975. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  27976. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  27977. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  27978. ExpectNotNull(
  27979. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  27980. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  27981. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  27982. ASN1_STRING_free(asn1str_test);
  27983. ASN1_STRING_free(asn1str_answer);
  27984. #endif /* OPENSSL_ALL && !NO_ASN */
  27985. return EXPECT_RESULT();
  27986. }
  27987. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27988. {
  27989. EXPECT_DECLS;
  27990. #if defined(OPENSSL_EXTRA)
  27991. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  27992. unsigned char nullstr[32];
  27993. XMEMSET(nullstr, 0, 32);
  27994. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27995. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  27996. if (asn1_gtime != NULL) {
  27997. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27998. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27999. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  28000. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28001. }
  28002. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  28003. #endif /* OPENSSL_EXTRA */
  28004. return EXPECT_RESULT();
  28005. }
  28006. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28007. {
  28008. EXPECT_DECLS;
  28009. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28010. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  28011. BIO* bio = NULL;
  28012. unsigned char buf[24];
  28013. int i;
  28014. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28015. BIO_set_write_buf_size(bio, 24);
  28016. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  28017. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28018. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  28019. /* Type not set. */
  28020. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28021. gtime.type = V_ASN1_GENERALIZEDTIME;
  28022. /* Invalid parameters testing. */
  28023. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28024. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28025. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  28026. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  28027. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28028. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28029. BIO_free(bio);
  28030. bio = NULL;
  28031. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28032. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28033. /* Ensure there is 0 bytes available to write into. */
  28034. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28035. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28036. for (i = 1; i < 20; i++) {
  28037. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28038. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28039. }
  28040. BIO_free(bio);
  28041. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  28042. #endif /* OPENSSL_EXTRA */
  28043. return EXPECT_RESULT();
  28044. }
  28045. static int test_wolfSSL_ASN1_TIME(void)
  28046. {
  28047. EXPECT_DECLS;
  28048. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28049. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28050. unsigned char *data;
  28051. ExpectNotNull(asn_time = ASN1_TIME_new());
  28052. #ifndef NO_WOLFSSL_STUB
  28053. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28054. #endif
  28055. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28056. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28057. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28058. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28059. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28060. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28061. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28062. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28063. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28064. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28065. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28066. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28067. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28068. ASN1_TIME_free(asn_time);
  28069. ASN1_TIME_free(NULL);
  28070. #endif
  28071. return EXPECT_RESULT();
  28072. }
  28073. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28074. {
  28075. EXPECT_DECLS;
  28076. #ifndef NO_ASN_TIME
  28077. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28078. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28079. WOLFSSL_ASN1_TIME* t = NULL;
  28080. char buf[ASN_GENERALIZED_TIME_SIZE];
  28081. ExpectNotNull((t = ASN1_TIME_new()));
  28082. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28083. /* Invalid parameter testing. */
  28084. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28085. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28086. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28087. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28088. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28089. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28090. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28091. /* Buffer needs to be longer than minimum of 5 characters. */
  28092. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28093. ASN1_TIME_free(t);
  28094. #endif
  28095. #endif /* NO_ASN_TIME */
  28096. return EXPECT_RESULT();
  28097. }
  28098. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28099. {
  28100. EXPECT_DECLS;
  28101. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28102. ASN1_TIME* fromTime = NULL;
  28103. ASN1_TIME* closeToTime = NULL;
  28104. ASN1_TIME* toTime = NULL;
  28105. ASN1_TIME* invalidTime = NULL;
  28106. int daysDiff = 0;
  28107. int secsDiff = 0;
  28108. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28109. /* Feb 22, 2003, 21:15:15 */
  28110. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28111. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28112. /* Feb 22, 2003, 21:16:15 */
  28113. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28114. ExpectNotNull((toTime = ASN1_TIME_new()));
  28115. /* Dec 19, 2010, 18:10:11 */
  28116. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28117. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28118. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28119. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28120. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28121. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28122. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28123. /* Error conditions. */
  28124. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28125. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28126. /* If both times are NULL, difference is 0. */
  28127. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28128. ExpectIntEQ(daysDiff, 0);
  28129. ExpectIntEQ(secsDiff, 0);
  28130. /* If one time is NULL, it defaults to the current time. */
  28131. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28132. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28133. /* Normal operation. Both times non-NULL. */
  28134. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28135. ExpectIntEQ(daysDiff, 2856);
  28136. ExpectIntEQ(secsDiff, 75296);
  28137. /* Swapping the times should return negative values. */
  28138. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28139. ExpectIntEQ(daysDiff, -2856);
  28140. ExpectIntEQ(secsDiff, -75296);
  28141. /* Compare with invalid time string. */
  28142. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28143. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28144. /* Compare with days difference of 0. */
  28145. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28146. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28147. /* Days and seconds differences not 0. */
  28148. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28149. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28150. /* Same time. */
  28151. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28152. /* Compare regression test: No seconds difference, just difference in days.
  28153. */
  28154. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28155. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28156. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28157. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28158. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28159. /* Edge case with Unix epoch. */
  28160. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28161. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28162. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28163. ExpectIntEQ(daysDiff, 3652);
  28164. ExpectIntEQ(secsDiff, 0);
  28165. /* Edge case with year > 2038 (year 2038 problem). */
  28166. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28167. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28168. ExpectIntEQ(daysDiff, 2932896);
  28169. ExpectIntEQ(secsDiff, 86399);
  28170. ASN1_TIME_free(fromTime);
  28171. ASN1_TIME_free(closeToTime);
  28172. ASN1_TIME_free(toTime);
  28173. ASN1_TIME_free(invalidTime);
  28174. #endif
  28175. return EXPECT_RESULT();
  28176. }
  28177. static int test_wolfSSL_ASN1_TIME_adj(void)
  28178. {
  28179. EXPECT_DECLS;
  28180. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28181. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28182. const int year = 365*24*60*60;
  28183. const int day = 24*60*60;
  28184. const int hour = 60*60;
  28185. const int mini = 60;
  28186. const byte asn_utc_time = ASN_UTC_TIME;
  28187. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28188. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28189. #endif
  28190. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28191. WOLFSSL_ASN1_TIME* s = NULL;
  28192. int offset_day;
  28193. long offset_sec;
  28194. char date_str[CTC_DATE_SIZE + 1];
  28195. time_t t;
  28196. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28197. /* UTC notation test */
  28198. /* 2000/2/15 20:30:00 */
  28199. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28200. offset_day = 7;
  28201. offset_sec = 45 * mini;
  28202. /* offset_sec = -45 * min;*/
  28203. ExpectNotNull(asn_time =
  28204. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28205. ExpectTrue(asn_time->type == asn_utc_time);
  28206. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28207. CTC_DATE_SIZE));
  28208. date_str[CTC_DATE_SIZE] = '\0';
  28209. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28210. /* negative offset */
  28211. offset_sec = -45 * mini;
  28212. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28213. ExpectNotNull(asn_time);
  28214. ExpectTrue(asn_time->type == asn_utc_time);
  28215. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28216. CTC_DATE_SIZE));
  28217. date_str[CTC_DATE_SIZE] = '\0';
  28218. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28219. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28220. s = NULL;
  28221. XMEMSET(date_str, 0, sizeof(date_str));
  28222. /* Generalized time will overflow time_t if not long */
  28223. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28224. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28225. DYNAMIC_TYPE_OPENSSL);
  28226. /* GeneralizedTime notation test */
  28227. /* 2055/03/01 09:00:00 */
  28228. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28229. offset_day = 12;
  28230. offset_sec = 10 * mini;
  28231. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28232. offset_sec));
  28233. ExpectTrue(asn_time->type == asn_gen_time);
  28234. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28235. CTC_DATE_SIZE));
  28236. date_str[CTC_DATE_SIZE] = '\0';
  28237. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28238. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28239. s = NULL;
  28240. XMEMSET(date_str, 0, sizeof(date_str));
  28241. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28242. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28243. s = NULL;
  28244. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28245. offset_day = 7;
  28246. offset_sec = 45 * mini;
  28247. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28248. offset_sec));
  28249. ExpectTrue(asn_time->type == asn_utc_time);
  28250. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28251. CTC_DATE_SIZE));
  28252. date_str[CTC_DATE_SIZE] = '\0';
  28253. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28254. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28255. asn_time = NULL;
  28256. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28257. offset_sec));
  28258. ExpectTrue(asn_time->type == asn_utc_time);
  28259. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28260. CTC_DATE_SIZE));
  28261. date_str[CTC_DATE_SIZE] = '\0';
  28262. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28263. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28264. #endif
  28265. return EXPECT_RESULT();
  28266. }
  28267. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28268. {
  28269. EXPECT_DECLS;
  28270. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28271. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28272. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28273. ASN1_TIME asnTime;
  28274. struct tm tm;
  28275. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28276. XMEMSET(&tm, 0, sizeof(struct tm));
  28277. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28278. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28279. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28280. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28281. ExpectIntEQ(tm.tm_sec, 15);
  28282. ExpectIntEQ(tm.tm_min, 15);
  28283. ExpectIntEQ(tm.tm_hour, 21);
  28284. ExpectIntEQ(tm.tm_mday, 22);
  28285. ExpectIntEQ(tm.tm_mon, 1);
  28286. ExpectIntEQ(tm.tm_year, 100);
  28287. ExpectIntEQ(tm.tm_isdst, 0);
  28288. #ifdef XMKTIME
  28289. ExpectIntEQ(tm.tm_wday, 2);
  28290. ExpectIntEQ(tm.tm_yday, 52);
  28291. #endif
  28292. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28293. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28294. ExpectIntEQ(tm.tm_year, 50);
  28295. /* Get current time. */
  28296. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28297. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28298. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28299. /* 0 length. */
  28300. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28301. /* No type. */
  28302. asnTime.length = 1;
  28303. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28304. /* Not UTCTIME length. */
  28305. asnTime.type = V_ASN1_UTCTIME;
  28306. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28307. /* Not GENERALIZEDTIME length. */
  28308. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28309. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28310. /* Not Zulu timezone. */
  28311. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28312. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28313. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28314. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28315. #ifdef XMKTIME
  28316. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28317. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28318. ExpectIntEQ(tm.tm_sec, 47);
  28319. ExpectIntEQ(tm.tm_min, 22);
  28320. ExpectIntEQ(tm.tm_hour, 21);
  28321. ExpectIntEQ(tm.tm_mday, 12);
  28322. ExpectIntEQ(tm.tm_mon, 4);
  28323. ExpectIntEQ(tm.tm_year, 123);
  28324. ExpectIntEQ(tm.tm_wday, 5);
  28325. ExpectIntEQ(tm.tm_yday, 131);
  28326. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28327. fields are zeroed out as expected. */
  28328. ExpectIntEQ(tm.tm_isdst, 0);
  28329. #endif
  28330. #endif
  28331. return EXPECT_RESULT();
  28332. }
  28333. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28334. {
  28335. EXPECT_DECLS;
  28336. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28337. WOLFSSL_ASN1_TIME *t = NULL;
  28338. WOLFSSL_ASN1_TIME *out = NULL;
  28339. WOLFSSL_ASN1_TIME *gtime = NULL;
  28340. int tlen = 0;
  28341. unsigned char *data = NULL;
  28342. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28343. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28344. /* type not set. */
  28345. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28346. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28347. t = NULL;
  28348. /* UTC Time test */
  28349. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28350. if (t != NULL) {
  28351. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28352. t->type = ASN_UTC_TIME;
  28353. t->length = ASN_UTC_TIME_SIZE;
  28354. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28355. }
  28356. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28357. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28358. out = NULL;
  28359. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28360. wolfSSL_ASN1_TIME_free(gtime);
  28361. gtime = NULL;
  28362. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28363. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28364. ExpectPtrEq(gtime, out);
  28365. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28366. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28367. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28368. /* Generalized Time test */
  28369. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28370. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28371. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28372. if (t != NULL) {
  28373. t->type = ASN_GENERALIZED_TIME;
  28374. t->length = ASN_GENERALIZED_TIME_SIZE;
  28375. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28376. }
  28377. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28378. ASN_GENERALIZED_TIME_SIZE);
  28379. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28380. "20050727123456Z");
  28381. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28382. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28383. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28384. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28385. /* UTC Time to Generalized Time 1900's test */
  28386. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28387. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28388. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28389. if (t != NULL) {
  28390. t->type = ASN_UTC_TIME;
  28391. t->length = ASN_UTC_TIME_SIZE;
  28392. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28393. }
  28394. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28395. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28396. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28397. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28398. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28399. /* Null parameter test */
  28400. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28401. gtime = NULL;
  28402. out = NULL;
  28403. if (t != NULL) {
  28404. t->type = ASN_UTC_TIME;
  28405. t->length = ASN_UTC_TIME_SIZE;
  28406. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28407. }
  28408. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  28409. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28410. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28411. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28412. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28413. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28414. #endif
  28415. return EXPECT_RESULT();
  28416. }
  28417. static int test_wolfSSL_ASN1_TIME_print(void)
  28418. {
  28419. EXPECT_DECLS;
  28420. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  28421. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28422. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28423. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  28424. !defined(NO_ASN_TIME)
  28425. BIO* bio = NULL;
  28426. BIO* fixed = NULL;
  28427. X509* x509 = NULL;
  28428. const unsigned char* der = client_cert_der_2048;
  28429. ASN1_TIME* notAfter = NULL;
  28430. ASN1_TIME* notBefore = NULL;
  28431. unsigned char buf[25];
  28432. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28433. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28434. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  28435. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  28436. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  28437. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  28438. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  28439. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  28440. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  28441. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28442. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  28443. /* Test BIO_write fails. */
  28444. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28445. /* Ensure there is 0 bytes available to write into. */
  28446. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  28447. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28448. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28449. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28450. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  28451. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28452. /* create a bad time and test results */
  28453. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  28454. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  28455. if (EXPECT_SUCCESS()) {
  28456. notAfter->data[8] = 0;
  28457. notAfter->data[3] = 0;
  28458. }
  28459. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  28460. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28461. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28462. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  28463. BIO_free(bio);
  28464. BIO_free(fixed);
  28465. X509_free(x509);
  28466. #endif
  28467. return EXPECT_RESULT();
  28468. }
  28469. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  28470. {
  28471. EXPECT_DECLS;
  28472. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  28473. BIO* bio = NULL;
  28474. ASN1_UTCTIME* utc = NULL;
  28475. unsigned char buf[25];
  28476. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  28477. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  28478. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  28479. /* Valid date */
  28480. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28481. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  28482. DYNAMIC_TYPE_ASN1));
  28483. if (utc != NULL) {
  28484. utc->type = ASN_UTC_TIME;
  28485. utc->length = ASN_UTC_TIME_SIZE;
  28486. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  28487. }
  28488. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  28489. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  28490. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  28491. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  28492. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28493. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  28494. XMEMSET(buf, 0, sizeof(buf));
  28495. BIO_free(bio);
  28496. bio = NULL;
  28497. /* Invalid format */
  28498. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28499. if (utc != NULL) {
  28500. utc->type = ASN_UTC_TIME;
  28501. utc->length = ASN_UTC_TIME_SIZE;
  28502. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  28503. }
  28504. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28505. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28506. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28507. /* Invalid type */
  28508. if (utc != NULL) {
  28509. utc->type = ASN_GENERALIZED_TIME;
  28510. utc->length = ASN_GENERALIZED_TIME_SIZE;
  28511. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  28512. }
  28513. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28514. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  28515. BIO_free(bio);
  28516. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  28517. return EXPECT_RESULT();
  28518. }
  28519. static int test_wolfSSL_ASN1_TYPE(void)
  28520. {
  28521. EXPECT_DECLS;
  28522. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  28523. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  28524. WOLFSSL_ASN1_TYPE* t = NULL;
  28525. WOLFSSL_ASN1_OBJECT* obj = NULL;
  28526. #ifndef NO_ASN_TIME
  28527. WOLFSSL_ASN1_TIME* time = NULL;
  28528. #endif
  28529. WOLFSSL_ASN1_STRING* str = NULL;
  28530. unsigned char data[] = { 0x00 };
  28531. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  28532. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28533. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  28534. wolfSSL_ASN1_TYPE_free(t);
  28535. t = NULL;
  28536. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28537. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  28538. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  28539. wolfSSL_ASN1_TYPE_free(t);
  28540. t = NULL;
  28541. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28542. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28543. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  28544. wolfSSL_ASN1_TYPE_free(t);
  28545. t = NULL;
  28546. #ifndef NO_ASN_TIME
  28547. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28548. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28549. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  28550. wolfSSL_ASN1_TYPE_free(t);
  28551. t = NULL;
  28552. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28553. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28554. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  28555. wolfSSL_ASN1_TYPE_free(t);
  28556. t = NULL;
  28557. #endif
  28558. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28559. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28560. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  28561. wolfSSL_ASN1_TYPE_free(t);
  28562. t = NULL;
  28563. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28564. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28565. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  28566. wolfSSL_ASN1_TYPE_free(t);
  28567. t = NULL;
  28568. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28569. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28570. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  28571. wolfSSL_ASN1_TYPE_free(t);
  28572. t = NULL;
  28573. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28574. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28575. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  28576. wolfSSL_ASN1_TYPE_free(t);
  28577. t = NULL;
  28578. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28579. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28580. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  28581. wolfSSL_ASN1_TYPE_free(t);
  28582. t = NULL;
  28583. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28584. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28585. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  28586. wolfSSL_ASN1_TYPE_free(t);
  28587. t = NULL;
  28588. #endif
  28589. return EXPECT_RESULT();
  28590. }
  28591. /* Testing code used in dpp.c in hostap */
  28592. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28593. typedef struct {
  28594. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  28595. * as an OID identifying the curve */
  28596. X509_ALGOR *alg;
  28597. /* Compressed format public key per ANSI X9.63 */
  28598. ASN1_BIT_STRING *pub_key;
  28599. } DPP_BOOTSTRAPPING_KEY;
  28600. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  28601. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  28602. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  28603. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  28604. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  28605. typedef struct {
  28606. ASN1_INTEGER *integer;
  28607. } TEST_ASN1;
  28608. ASN1_SEQUENCE(TEST_ASN1) = {
  28609. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  28610. } ASN1_SEQUENCE_END(TEST_ASN1)
  28611. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  28612. typedef struct {
  28613. ASN1_OCTET_STRING *octet_string;
  28614. } TEST_FAIL_ASN1;
  28615. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  28616. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  28617. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  28618. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  28619. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  28620. #endif
  28621. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  28622. {
  28623. EXPECT_DECLS;
  28624. /* Testing code used in dpp.c in hostap */
  28625. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28626. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28627. EC_KEY *eckey = NULL;
  28628. EVP_PKEY *key = NULL;
  28629. size_t len = 0;
  28630. unsigned char *der = NULL;
  28631. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  28632. const unsigned char *in = ecc_clikey_der_256;
  28633. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  28634. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  28635. const EC_GROUP *group = NULL;
  28636. const EC_POINT *point = NULL;
  28637. int nid;
  28638. TEST_ASN1 *test_asn1 = NULL;
  28639. TEST_FAIL_ASN1 test_fail_asn1;
  28640. const unsigned char badObjDer[] = { 0x06, 0x00 };
  28641. const unsigned char goodObjDer[] = {
  28642. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  28643. };
  28644. WOLFSSL_ASN1_ITEM emptyTemplate;
  28645. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  28646. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  28647. der = NULL;
  28648. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  28649. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  28650. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28651. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  28652. (long)sizeof_ecc_clikey_der_256));
  28653. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  28654. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  28655. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  28656. nid = EC_GROUP_get_curve_name(group);
  28657. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  28658. group_obj = OBJ_nid2obj(nid);
  28659. if ((ec_obj != NULL) && (group_obj != NULL)) {
  28660. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  28661. group_obj), 1);
  28662. if (EXPECT_SUCCESS()) {
  28663. ec_obj = NULL;
  28664. group_obj = NULL;
  28665. }
  28666. }
  28667. wolfSSL_ASN1_OBJECT_free(group_obj);
  28668. wolfSSL_ASN1_OBJECT_free(ec_obj);
  28669. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  28670. #ifdef HAVE_COMP_KEY
  28671. ExpectIntGT((len = EC_POINT_point2oct(
  28672. group, point, POINT_CONVERSION_COMPRESSED,
  28673. NULL, 0, NULL)), 0);
  28674. #else
  28675. ExpectIntGT((len = EC_POINT_point2oct(
  28676. group, point, POINT_CONVERSION_UNCOMPRESSED,
  28677. NULL, 0, NULL)), 0);
  28678. #endif
  28679. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  28680. #ifdef HAVE_COMP_KEY
  28681. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  28682. der, len-1, NULL), 0);
  28683. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  28684. der, len, NULL), len);
  28685. #else
  28686. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  28687. der, len-1, NULL), 0);
  28688. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  28689. der, len, NULL), len);
  28690. #endif
  28691. if (EXPECT_SUCCESS()) {
  28692. bootstrap->pub_key->data = der;
  28693. bootstrap->pub_key->length = (int)len;
  28694. /* Not actually used */
  28695. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  28696. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  28697. }
  28698. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  28699. der = NULL;
  28700. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28701. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28702. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28703. EVP_PKEY_free(key);
  28704. EC_KEY_free(eckey);
  28705. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  28706. bootstrap = NULL;
  28707. DPP_BOOTSTRAPPING_KEY_free(NULL);
  28708. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  28709. * a NULL and an empty BIT_STRING. */
  28710. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  28711. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  28712. if (EXPECT_SUCCESS()) {
  28713. bootstrap->alg->algorithm->obj = badObjDer;
  28714. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  28715. }
  28716. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  28717. if (EXPECT_SUCCESS()) {
  28718. bootstrap->alg->parameter->type = V_ASN1_NULL;
  28719. bootstrap->alg->parameter->value.ptr = NULL;
  28720. bootstrap->pub_key->data = NULL;
  28721. bootstrap->pub_key->length = 0;
  28722. /* Not actually used */
  28723. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  28724. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  28725. }
  28726. /* Encode with bad OBJECT_ID. */
  28727. der = NULL;
  28728. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28729. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  28730. if (EXPECT_SUCCESS()) {
  28731. bootstrap->alg->algorithm->obj = goodObjDer;
  28732. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  28733. bootstrap->alg->algorithm->grp = 2;
  28734. }
  28735. der = NULL;
  28736. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  28737. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  28738. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28739. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  28740. /* Test integer */
  28741. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  28742. der = NULL;
  28743. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  28744. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  28745. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  28746. TEST_ASN1_free(test_asn1);
  28747. /* Test integer cases. */
  28748. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  28749. TEST_ASN1_free(NULL);
  28750. /* Test error cases. */
  28751. ExpectNull(TEST_FAIL_ASN1_new());
  28752. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  28753. TEST_FAIL_ASN1_free(NULL);
  28754. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  28755. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  28756. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  28757. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  28758. return EXPECT_RESULT();
  28759. }
  28760. static int test_wolfSSL_lhash(void)
  28761. {
  28762. EXPECT_DECLS;
  28763. #ifdef OPENSSL_ALL
  28764. const char testStr[] = "Like a true nature's child\n"
  28765. "We were born\n"
  28766. "Born to be wild";
  28767. #ifdef NO_SHA
  28768. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  28769. #else
  28770. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  28771. #endif
  28772. #endif
  28773. return EXPECT_RESULT();
  28774. }
  28775. static int test_wolfSSL_X509_NAME(void)
  28776. {
  28777. EXPECT_DECLS;
  28778. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  28779. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28780. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  28781. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  28782. defined(OPENSSL_EXTRA))
  28783. X509* x509 = NULL;
  28784. const unsigned char* c = NULL;
  28785. unsigned char buf[4096];
  28786. int bytes = 0;
  28787. XFILE f = XBADFILE;
  28788. const X509_NAME* a = NULL;
  28789. const X509_NAME* b = NULL;
  28790. X509_NAME* d2i_name = NULL;
  28791. int sz = 0;
  28792. unsigned char* tmp = NULL;
  28793. char file[] = "./certs/ca-cert.der";
  28794. #ifndef OPENSSL_EXTRA_X509_SMALL
  28795. byte empty[] = { /* CN=empty emailAddress= */
  28796. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  28797. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  28798. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  28799. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  28800. 0x01, 0x16, 0x00
  28801. };
  28802. #endif
  28803. #ifndef OPENSSL_EXTRA_X509_SMALL
  28804. /* test compile of deprecated function, returns 0 */
  28805. ExpectIntEQ(CRYPTO_thread_id(), 0);
  28806. #endif
  28807. ExpectNotNull(a = X509_NAME_new());
  28808. X509_NAME_free((X509_NAME*)a);
  28809. a = NULL;
  28810. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  28811. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  28812. if (f != XBADFILE)
  28813. XFCLOSE(f);
  28814. c = buf;
  28815. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  28816. /* test cmp function */
  28817. ExpectNotNull(a = X509_get_issuer_name(x509));
  28818. ExpectNotNull(b = X509_get_subject_name(x509));
  28819. #ifndef OPENSSL_EXTRA_X509_SMALL
  28820. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  28821. #endif
  28822. tmp = buf;
  28823. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  28824. if (sz > 0 && tmp == buf) {
  28825. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  28826. __LINE__);
  28827. fprintf(stderr, " Expected pointer to be incremented\n");
  28828. abort();
  28829. }
  28830. #ifndef OPENSSL_EXTRA_X509_SMALL
  28831. tmp = buf;
  28832. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  28833. #endif
  28834. /* if output parameter is NULL, should still return required size. */
  28835. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  28836. /* retry but with the function creating a buffer */
  28837. tmp = NULL;
  28838. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  28839. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  28840. tmp = NULL;
  28841. #ifdef WOLFSSL_CERT_NAME_ALL
  28842. /* test for givenName and name */
  28843. {
  28844. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  28845. const byte gName[] = "test-given-name";
  28846. const byte name[] = "test-name";
  28847. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  28848. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  28849. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  28850. 1);
  28851. wolfSSL_X509_NAME_ENTRY_free(entry);
  28852. entry = NULL;
  28853. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  28854. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  28855. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  28856. 1);
  28857. wolfSSL_X509_NAME_ENTRY_free(entry);
  28858. tmp = NULL;
  28859. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  28860. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  28861. }
  28862. #endif
  28863. b = NULL;
  28864. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  28865. #ifndef OPENSSL_EXTRA_X509_SMALL
  28866. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  28867. #endif
  28868. X509_NAME_free((X509_NAME*)b);
  28869. X509_NAME_free(d2i_name);
  28870. d2i_name = NULL;
  28871. X509_free(x509);
  28872. #ifndef OPENSSL_EXTRA_X509_SMALL
  28873. /* test with an empty domain component */
  28874. tmp = empty;
  28875. sz = sizeof(empty);
  28876. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  28877. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  28878. /* size of empty emailAddress will be 0 */
  28879. tmp = buf;
  28880. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  28881. (char*)tmp, sizeof(buf)), 0);
  28882. /* should contain no organization name */
  28883. tmp = buf;
  28884. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  28885. (char*)tmp, sizeof(buf)), -1);
  28886. X509_NAME_free(d2i_name);
  28887. #endif
  28888. #endif
  28889. return EXPECT_RESULT();
  28890. }
  28891. static int test_wolfSSL_X509_NAME_hash(void)
  28892. {
  28893. EXPECT_DECLS;
  28894. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28895. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  28896. BIO* bio = NULL;
  28897. X509* x509 = NULL;
  28898. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  28899. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28900. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  28901. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  28902. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  28903. X509_free(x509);
  28904. BIO_free(bio);
  28905. #endif
  28906. return EXPECT_RESULT();
  28907. }
  28908. static int test_wolfSSL_X509_NAME_print_ex(void)
  28909. {
  28910. EXPECT_DECLS;
  28911. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  28912. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  28913. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  28914. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  28915. !defined(NO_BIO) && !defined(NO_RSA)
  28916. int memSz = 0;
  28917. byte* mem = NULL;
  28918. BIO* bio = NULL;
  28919. BIO* membio = NULL;
  28920. X509* x509 = NULL;
  28921. X509_NAME* name = NULL;
  28922. const char* expNormal = "C=US, CN=wolfssl.com";
  28923. const char* expReverse = "CN=wolfssl.com, C=US";
  28924. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  28925. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  28926. const char* expRFC5523 =
  28927. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  28928. /* Test with real cert (svrCertFile) first */
  28929. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  28930. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  28931. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  28932. ExpectNotNull(name = X509_get_subject_name(x509));
  28933. /* Test without flags */
  28934. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28935. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28936. BIO_free(membio);
  28937. membio = NULL;
  28938. /* Test flag: XN_FLAG_RFC2253 */
  28939. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28940. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28941. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28942. BIO_free(membio);
  28943. membio = NULL;
  28944. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  28945. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28946. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28947. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28948. BIO_free(membio);
  28949. membio = NULL;
  28950. X509_free(x509);
  28951. BIO_free(bio);
  28952. name = NULL;
  28953. /* Test normal case without escaped characters */
  28954. {
  28955. /* Create name: "/C=US/CN=wolfssl.com" */
  28956. ExpectNotNull(name = X509_NAME_new());
  28957. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  28958. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  28959. WOLFSSL_SUCCESS);
  28960. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  28961. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  28962. WOLFSSL_SUCCESS);
  28963. /* Test without flags */
  28964. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28965. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  28966. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28967. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  28968. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  28969. BIO_free(membio);
  28970. membio = NULL;
  28971. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  28972. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28973. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28974. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  28975. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28976. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  28977. BIO_free(membio);
  28978. membio = NULL;
  28979. /* Test flags: XN_FLAG_DN_REV - reversed */
  28980. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  28981. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  28982. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  28983. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  28984. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  28985. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  28986. BIO_free(membio);
  28987. membio = NULL;
  28988. X509_NAME_free(name);
  28989. name = NULL;
  28990. }
  28991. /* Test RFC2253 characters are escaped with backslashes */
  28992. {
  28993. ExpectNotNull(name = X509_NAME_new());
  28994. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  28995. /* space at beginning and end, and: ,+"\ */
  28996. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  28997. WOLFSSL_SUCCESS);
  28998. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  28999. /* # at beginning, and: <>;*/
  29000. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29001. WOLFSSL_SUCCESS);
  29002. /* Test without flags */
  29003. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29004. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29005. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29006. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  29007. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29008. XSTRLEN(expNotEscaped)), 0);
  29009. BIO_free(membio);
  29010. membio = NULL;
  29011. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29012. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29013. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29014. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29015. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29016. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  29017. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29018. BIO_free(membio);
  29019. membio = NULL;
  29020. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29021. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29022. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29023. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29024. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29025. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29026. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29027. XSTRLEN(expNotEscapedRev)), 0);
  29028. BIO_free(membio);
  29029. X509_NAME_free(name);
  29030. }
  29031. #endif
  29032. return EXPECT_RESULT();
  29033. }
  29034. #ifndef NO_BIO
  29035. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29036. {
  29037. EXPECT_DECLS;
  29038. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29039. STACK_OF(X509_INFO) *info_stack = NULL;
  29040. X509_INFO *info = NULL;
  29041. int len;
  29042. int i;
  29043. const char* files[] = {
  29044. cliCertFile,
  29045. cliKeyFile,
  29046. /* This needs to be the order as svrCertFile contains the
  29047. * intermediate cert as well. */
  29048. svrKeyFile,
  29049. svrCertFile,
  29050. NULL,
  29051. };
  29052. const char** curFile;
  29053. BIO *fileBIO = NULL;
  29054. BIO *concatBIO = NULL;
  29055. byte tmp[FOURK_BUF];
  29056. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29057. * to group objects together. */
  29058. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29059. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29060. int fileLen = 0;
  29061. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29062. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29063. if (EXPECT_SUCCESS()) {
  29064. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29065. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29066. fileLen -= len;
  29067. if (EXPECT_FAIL())
  29068. break;
  29069. }
  29070. /* Make sure we read the entire file */
  29071. ExpectIntEQ(fileLen, 0);
  29072. }
  29073. BIO_free(fileBIO);
  29074. fileBIO = NULL;
  29075. }
  29076. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29077. NULL));
  29078. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  29079. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29080. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29081. ExpectNotNull(info->x509);
  29082. ExpectNull(info->crl);
  29083. if (i != 0) {
  29084. ExpectNotNull(info->x_pkey);
  29085. ExpectIntEQ(X509_check_private_key(info->x509,
  29086. info->x_pkey->dec_pkey), 1);
  29087. }
  29088. else {
  29089. ExpectNull(info->x_pkey);
  29090. }
  29091. }
  29092. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29093. BIO_free(concatBIO);
  29094. #endif
  29095. return EXPECT_RESULT();
  29096. }
  29097. #endif
  29098. #ifndef NO_BIO
  29099. static int test_wolfSSL_X509_INFO(void)
  29100. {
  29101. EXPECT_DECLS;
  29102. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29103. STACK_OF(X509_INFO) *info_stack = NULL;
  29104. X509_INFO *info = NULL;
  29105. BIO *cert = NULL;
  29106. int i;
  29107. /* PEM in hex format to avoid null terminator */
  29108. byte data[] = {
  29109. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29110. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29111. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29112. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29113. 0x2d, 0x2d, 0x2d
  29114. };
  29115. /* PEM in hex format to avoid null terminator */
  29116. byte data2[] = {
  29117. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29118. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29119. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29120. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29121. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29122. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29123. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29124. };
  29125. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29126. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29127. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29128. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29129. ExpectNotNull(info->x509);
  29130. ExpectNull(info->crl);
  29131. ExpectNull(info->x_pkey);
  29132. }
  29133. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29134. info_stack = NULL;
  29135. BIO_free(cert);
  29136. cert = NULL;
  29137. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29138. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29139. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29140. info_stack = NULL;
  29141. BIO_free(cert);
  29142. cert = NULL;
  29143. /* This case should fail due to invalid input. */
  29144. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29145. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29146. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29147. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29148. info_stack = NULL;
  29149. BIO_free(cert);
  29150. cert = NULL;
  29151. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29152. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29153. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29154. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29155. BIO_free(cert);
  29156. #endif
  29157. return EXPECT_RESULT();
  29158. }
  29159. #endif
  29160. static int test_wolfSSL_X509_subject_name_hash(void)
  29161. {
  29162. EXPECT_DECLS;
  29163. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29164. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29165. X509* x509 = NULL;
  29166. X509_NAME* subjectName = NULL;
  29167. unsigned long ret1 = 0;
  29168. unsigned long ret2 = 0;
  29169. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29170. SSL_FILETYPE_PEM));
  29171. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29172. /* These two
  29173. * - X509_subject_name_hash(x509)
  29174. * - X509_NAME_hash(X509_get_subject_name(x509))
  29175. * should give the same hash, if !defined(NO_SHA) is true. */
  29176. ret1 = X509_subject_name_hash(x509);
  29177. ExpectIntNE(ret1, 0);
  29178. #if !defined(NO_SHA)
  29179. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29180. ExpectIntNE(ret2, 0);
  29181. ExpectIntEQ(ret1, ret2);
  29182. #else
  29183. (void) ret2;
  29184. #endif
  29185. X509_free(x509);
  29186. #endif
  29187. return EXPECT_RESULT();
  29188. }
  29189. static int test_wolfSSL_X509_issuer_name_hash(void)
  29190. {
  29191. EXPECT_DECLS;
  29192. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29193. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29194. X509* x509 = NULL;
  29195. X509_NAME* issuertName = NULL;
  29196. unsigned long ret1 = 0;
  29197. unsigned long ret2 = 0;
  29198. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29199. SSL_FILETYPE_PEM));
  29200. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29201. /* These two
  29202. * - X509_issuer_name_hash(x509)
  29203. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29204. * should give the same hash, if !defined(NO_SHA) is true. */
  29205. ret1 = X509_issuer_name_hash(x509);
  29206. ExpectIntNE(ret1, 0);
  29207. #if !defined(NO_SHA)
  29208. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29209. ExpectIntNE(ret2, 0);
  29210. ExpectIntEQ(ret1, ret2);
  29211. #else
  29212. (void) ret2;
  29213. #endif
  29214. X509_free(x509);
  29215. #endif
  29216. return EXPECT_RESULT();
  29217. }
  29218. static int test_wolfSSL_X509_check_host(void)
  29219. {
  29220. EXPECT_DECLS;
  29221. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29222. && !defined(NO_SHA) && !defined(NO_RSA)
  29223. X509* x509 = NULL;
  29224. const char altName[] = "example.com";
  29225. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29226. SSL_FILETYPE_PEM));
  29227. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29228. WOLFSSL_SUCCESS);
  29229. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29230. WOLFSSL_FAILURE);
  29231. X509_free(x509);
  29232. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29233. WOLFSSL_FAILURE);
  29234. #endif
  29235. return EXPECT_RESULT();
  29236. }
  29237. static int test_wolfSSL_X509_check_email(void)
  29238. {
  29239. EXPECT_DECLS;
  29240. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29241. X509* x509 = NULL;
  29242. const char goodEmail[] = "info@wolfssl.com";
  29243. const char badEmail[] = "disinfo@wolfssl.com";
  29244. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29245. SSL_FILETYPE_PEM));
  29246. /* Should fail on non-matching email address */
  29247. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29248. WOLFSSL_FAILURE);
  29249. /* Should succeed on matching email address */
  29250. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29251. WOLFSSL_SUCCESS);
  29252. /* Should compute length internally when not provided */
  29253. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29254. WOLFSSL_SUCCESS);
  29255. /* Should fail when email address is NULL */
  29256. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29257. WOLFSSL_FAILURE);
  29258. X509_free(x509);
  29259. /* Should fail when x509 is NULL */
  29260. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29261. WOLFSSL_FAILURE);
  29262. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29263. return EXPECT_RESULT();
  29264. }
  29265. static int test_wc_PemToDer(void)
  29266. {
  29267. EXPECT_DECLS;
  29268. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29269. int ret;
  29270. DerBuffer* pDer = NULL;
  29271. const char* ca_cert = "./certs/server-cert.pem";
  29272. byte* cert_buf = NULL;
  29273. size_t cert_sz = 0;
  29274. int eccKey = 0;
  29275. EncryptedInfo info;
  29276. XMEMSET(&info, 0, sizeof(info));
  29277. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29278. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  29279. &info, &eccKey), 0);
  29280. wc_FreeDer(&pDer);
  29281. pDer = NULL;
  29282. if (cert_buf != NULL) {
  29283. free(cert_buf);
  29284. cert_buf = NULL;
  29285. }
  29286. #ifdef HAVE_ECC
  29287. {
  29288. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29289. byte key_buf[256] = {0};
  29290. /* Test fail of loading a key with cert type */
  29291. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29292. key_buf[0] = '\n';
  29293. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29294. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29295. &pDer, NULL, &info, &eccKey)), 0);
  29296. #ifdef OPENSSL_EXTRA
  29297. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29298. &pDer, NULL, &info, &eccKey)), 0);
  29299. #endif
  29300. wc_FreeDer(&pDer);
  29301. if (cert_buf != NULL)
  29302. free(cert_buf);
  29303. }
  29304. #endif
  29305. #endif
  29306. return EXPECT_RESULT();
  29307. }
  29308. static int test_wc_AllocDer(void)
  29309. {
  29310. EXPECT_DECLS;
  29311. #if !defined(NO_CERTS)
  29312. DerBuffer* pDer = NULL;
  29313. word32 testSize = 1024;
  29314. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29315. BAD_FUNC_ARG);
  29316. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29317. ExpectNotNull(pDer);
  29318. wc_FreeDer(&pDer);
  29319. #endif
  29320. return EXPECT_RESULT();
  29321. }
  29322. static int test_wc_CertPemToDer(void)
  29323. {
  29324. EXPECT_DECLS;
  29325. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29326. const char* ca_cert = "./certs/ca-cert.pem";
  29327. byte* cert_buf = NULL;
  29328. size_t cert_sz = 0;
  29329. size_t cert_dersz = 0;
  29330. byte* cert_der = NULL;
  29331. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29332. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29333. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29334. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29335. (int)cert_dersz, CERT_TYPE), 0);
  29336. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29337. BAD_FUNC_ARG);
  29338. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29339. BAD_FUNC_ARG);
  29340. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29341. BAD_FUNC_ARG);
  29342. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29343. CERT_TYPE), BAD_FUNC_ARG);
  29344. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29345. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29346. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29347. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29348. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29349. CERT_TYPE), BAD_FUNC_ARG);
  29350. if (cert_der != NULL)
  29351. free(cert_der);
  29352. if (cert_buf != NULL)
  29353. free(cert_buf);
  29354. #endif
  29355. return EXPECT_RESULT();
  29356. }
  29357. static int test_wc_KeyPemToDer(void)
  29358. {
  29359. EXPECT_DECLS;
  29360. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29361. int ret = 0;
  29362. const byte cert_buf[] = \
  29363. "-----BEGIN PRIVATE KEY-----\n"
  29364. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29365. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29366. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29367. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29368. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29369. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29370. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29371. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29372. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29373. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29374. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29375. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29376. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29377. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29378. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29379. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29380. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29381. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29382. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29383. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29384. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29385. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29386. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29387. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29388. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29389. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29390. "-----END PRIVATE KEY-----\n";
  29391. const int cert_sz = sizeof(cert_buf);
  29392. const char cert_pw[] = "password";
  29393. int cert_dersz = 0;
  29394. byte* cert_der = NULL;
  29395. /* Bad arg: Cert buffer is NULL */
  29396. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29397. BAD_FUNC_ARG);
  29398. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29399. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29400. BAD_FUNC_ARG);
  29401. /* Test normal operation */
  29402. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29403. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29404. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29405. cert_pw), 0);
  29406. ExpectIntLE(ret, cert_sz);
  29407. if (cert_der != NULL) {
  29408. free(cert_der);
  29409. cert_der = NULL;
  29410. }
  29411. /* Test NULL for DER buffer to return needed DER buffer size */
  29412. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  29413. ExpectIntLE(ret, cert_sz);
  29414. if (EXPECT_SUCCESS())
  29415. cert_dersz = ret;
  29416. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29417. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29418. cert_pw), 0);
  29419. ExpectIntLE(ret, cert_sz);
  29420. if (cert_der != NULL)
  29421. free(cert_der);
  29422. #endif
  29423. return EXPECT_RESULT();
  29424. }
  29425. static int test_wc_PubKeyPemToDer(void)
  29426. {
  29427. EXPECT_DECLS;
  29428. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  29429. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29430. int ret = 0;
  29431. const char* key = "./certs/ecc-client-keyPub.pem";
  29432. byte* cert_buf = NULL;
  29433. size_t cert_sz = 0, cert_dersz = 0;
  29434. byte* cert_der = NULL;
  29435. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  29436. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  29437. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  29438. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29439. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29440. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29441. (int)cert_dersz), 0);
  29442. if (cert_der != NULL) {
  29443. free(cert_der);
  29444. cert_der = NULL;
  29445. }
  29446. /* Test NULL for DER buffer to return needed DER buffer size */
  29447. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  29448. ExpectIntLE(ret, cert_sz);
  29449. cert_dersz = ret;
  29450. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29451. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29452. (int)cert_dersz), 0);
  29453. if (cert_der != NULL) {
  29454. free(cert_der);
  29455. }
  29456. if (cert_buf != NULL) {
  29457. free(cert_buf);
  29458. }
  29459. #endif
  29460. return EXPECT_RESULT();
  29461. }
  29462. static int test_wc_PemPubKeyToDer(void)
  29463. {
  29464. EXPECT_DECLS;
  29465. #if !defined(NO_FILESYSTEM) && \
  29466. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29467. const char* key = "./certs/ecc-client-keyPub.pem";
  29468. size_t cert_dersz = 1024;
  29469. byte* cert_der = NULL;
  29470. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  29471. BAD_FUNC_ARG);
  29472. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29473. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  29474. if (cert_der != NULL) {
  29475. free(cert_der);
  29476. }
  29477. #endif
  29478. return EXPECT_RESULT();
  29479. }
  29480. static int test_wc_GetPubKeyDerFromCert(void)
  29481. {
  29482. EXPECT_DECLS;
  29483. #if !defined(NO_RSA) || defined(HAVE_ECC)
  29484. int ret;
  29485. word32 idx = 0;
  29486. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29487. word32 keyDerSz = (word32)sizeof(keyDer);
  29488. DecodedCert decoded;
  29489. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29490. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  29491. word32 certBufSz = sizeof(certBuf);
  29492. #endif
  29493. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  29494. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29495. XFILE fp = XBADFILE;
  29496. #endif
  29497. #ifndef NO_RSA
  29498. RsaKey rsaKey;
  29499. #if defined(USE_CERT_BUFFERS_2048)
  29500. byte* rsaCertDer = (byte*)client_cert_der_2048;
  29501. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  29502. #elif defined(USE_CERT_BUFFERS_1024)
  29503. byte* rsaCertDer = (byte*)client_cert_der_1024;
  29504. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  29505. #else
  29506. unsigned char rsaCertDer[TWOK_BUF];
  29507. word32 rsaCertDerSz;
  29508. #endif
  29509. #endif
  29510. #ifdef HAVE_ECC
  29511. ecc_key eccKey;
  29512. #if defined(USE_CERT_BUFFERS_256)
  29513. byte* eccCert = (byte*)cliecc_cert_der_256;
  29514. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  29515. #else
  29516. unsigned char eccCert[ONEK_BUF];
  29517. word32 eccCertSz;
  29518. XFILE fp2 = XBADFILE;
  29519. #endif
  29520. #endif
  29521. #ifndef NO_RSA
  29522. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  29523. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  29524. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  29525. fp), 0);
  29526. if (fp != XBADFILE) {
  29527. XFCLOSE(fp);
  29528. fp = XBADFILE;
  29529. }
  29530. #endif
  29531. /* good test case - RSA DER cert */
  29532. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  29533. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29534. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29535. ExpectIntGT(keyDerSz, 0);
  29536. /* sanity check, verify we can import DER public key */
  29537. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29538. ExpectIntEQ(ret, 0);
  29539. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29540. if (ret == 0) {
  29541. wc_FreeRsaKey(&rsaKey);
  29542. }
  29543. /* test LENGTH_ONLY_E case */
  29544. keyDerSz = 0;
  29545. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29546. LENGTH_ONLY_E);
  29547. ExpectIntGT(keyDerSz, 0);
  29548. /* bad args: DecodedCert NULL */
  29549. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  29550. /* bad args: output key buff size */
  29551. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  29552. /* bad args: zero size output key buffer */
  29553. keyDerSz = 0;
  29554. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29555. BAD_FUNC_ARG);
  29556. wc_FreeDecodedCert(&decoded);
  29557. /* Certificate Request Tests */
  29558. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29559. {
  29560. XMEMSET(certBuf, 0, sizeof(certBuf));
  29561. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  29562. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  29563. if (fp != XBADFILE) {
  29564. XFCLOSE(fp);
  29565. }
  29566. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  29567. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  29568. /* good test case - RSA DER certificate request */
  29569. keyDerSz = sizeof(keyDer);
  29570. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29571. 0);
  29572. ExpectIntGT(keyDerSz, 0);
  29573. /* sanity check, verify we can import DER public key */
  29574. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29575. ExpectIntEQ(ret, 0);
  29576. idx = 0;
  29577. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29578. if (ret == 0) {
  29579. wc_FreeRsaKey(&rsaKey);
  29580. }
  29581. wc_FreeDecodedCert(&decoded);
  29582. }
  29583. #endif /* WOLFSSL_CERT_REQ */
  29584. #endif /* NO_RSA */
  29585. #ifdef HAVE_ECC
  29586. #ifndef USE_CERT_BUFFERS_256
  29587. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  29588. XBADFILE);
  29589. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  29590. if (fp2 != XBADFILE) {
  29591. XFCLOSE(fp2);
  29592. }
  29593. #endif
  29594. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  29595. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29596. /* good test case - ECC */
  29597. XMEMSET(keyDer, 0, sizeof(keyDer));
  29598. keyDerSz = sizeof(keyDer);
  29599. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29600. ExpectIntGT(keyDerSz, 0);
  29601. /* sanity check, verify we can import DER public key */
  29602. ret = wc_ecc_init(&eccKey);
  29603. ExpectIntEQ(ret, 0);
  29604. idx = 0; /* reset idx to 0, used above in RSA case */
  29605. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  29606. if (ret == 0) {
  29607. wc_ecc_free(&eccKey);
  29608. }
  29609. /* test LENGTH_ONLY_E case */
  29610. keyDerSz = 0;
  29611. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29612. LENGTH_ONLY_E);
  29613. ExpectIntGT(keyDerSz, 0);
  29614. wc_FreeDecodedCert(&decoded);
  29615. #endif
  29616. #endif /* !NO_RSA || HAVE_ECC */
  29617. return EXPECT_RESULT();
  29618. }
  29619. static int test_wc_CheckCertSigPubKey(void)
  29620. {
  29621. EXPECT_DECLS;
  29622. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29623. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  29624. int ret = 0;
  29625. const char* ca_cert = "./certs/ca-cert.pem";
  29626. byte* cert_buf = NULL;
  29627. size_t cert_sz = 0;
  29628. byte* cert_der = NULL;
  29629. word32 cert_dersz = 0;
  29630. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29631. word32 keyDerSz = (word32)sizeof(keyDer);
  29632. DecodedCert decoded;
  29633. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29634. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  29635. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29636. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29637. (int)cert_dersz, CERT_TYPE), 0);
  29638. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  29639. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29640. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29641. ExpectIntGT(keyDerSz, 0);
  29642. /* Good test case. */
  29643. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  29644. keyDerSz, RSAk), 0);
  29645. /* No certificate. */
  29646. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  29647. ECDSAk), BAD_FUNC_ARG);
  29648. /* Bad cert size. */
  29649. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  29650. RSAk), 0);
  29651. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  29652. /* No public key. */
  29653. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  29654. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  29655. /* Bad public key size. */
  29656. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  29657. RSAk), BAD_FUNC_ARG);
  29658. /* Wrong aglo. */
  29659. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  29660. keyDerSz, ECDSAk), ASN_PARSE_E);
  29661. wc_FreeDecodedCert(&decoded);
  29662. if (cert_der != NULL)
  29663. free(cert_der);
  29664. if (cert_buf != NULL)
  29665. free(cert_buf);
  29666. #endif
  29667. return EXPECT_RESULT();
  29668. }
  29669. static int test_wolfSSL_certs(void)
  29670. {
  29671. EXPECT_DECLS;
  29672. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29673. !defined(NO_RSA)
  29674. X509* x509ext = NULL;
  29675. #ifdef OPENSSL_ALL
  29676. X509* x509 = NULL;
  29677. WOLFSSL_X509_EXTENSION* ext = NULL;
  29678. ASN1_OBJECT* obj = NULL;
  29679. #endif
  29680. WOLFSSL* ssl = NULL;
  29681. WOLFSSL_CTX* ctx = NULL;
  29682. STACK_OF(ASN1_OBJECT)* sk = NULL;
  29683. ASN1_STRING* asn1_str = NULL;
  29684. AUTHORITY_KEYID* akey = NULL;
  29685. BASIC_CONSTRAINTS* bc = NULL;
  29686. int crit = 0;
  29687. #ifndef NO_WOLFSSL_SERVER
  29688. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  29689. #else
  29690. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  29691. #endif
  29692. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29693. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29694. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29695. #if !defined(NO_CHECK_PRIVATE_KEY)
  29696. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  29697. #endif
  29698. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29699. #if !defined(NO_CHECK_PRIVATE_KEY)
  29700. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  29701. #endif
  29702. ExpectNotNull(ssl = SSL_new(ctx));
  29703. #if !defined(NO_CHECK_PRIVATE_KEY)
  29704. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29705. #endif
  29706. #ifdef HAVE_PK_CALLBACKS
  29707. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  29708. #endif /* HAVE_PK_CALLBACKS */
  29709. /* create and use x509 */
  29710. #ifdef OPENSSL_ALL
  29711. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29712. WOLFSSL_FILETYPE_PEM));
  29713. #endif
  29714. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  29715. WOLFSSL_FILETYPE_PEM));
  29716. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  29717. #if !defined(NO_CHECK_PRIVATE_KEY)
  29718. /* with loading in a new cert the check on private key should now fail */
  29719. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29720. #endif
  29721. #if defined(USE_CERT_BUFFERS_2048)
  29722. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  29723. (unsigned char*)server_cert_der_2048,
  29724. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  29725. #endif
  29726. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  29727. /************* Get Digest of Certificate ******************/
  29728. {
  29729. byte digest[64]; /* max digest size */
  29730. word32 digestSz;
  29731. XMEMSET(digest, 0, sizeof(digest));
  29732. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  29733. WOLFSSL_SUCCESS);
  29734. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  29735. WOLFSSL_SUCCESS);
  29736. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  29737. WOLFSSL_FAILURE);
  29738. }
  29739. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  29740. /* test and checkout X509 extensions */
  29741. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  29742. NID_basic_constraints, &crit, NULL));
  29743. ExpectIntEQ(crit, 0);
  29744. #ifdef OPENSSL_ALL
  29745. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  29746. X509_EXTENSION_free(ext);
  29747. ext = NULL;
  29748. ExpectNotNull(ext = X509_EXTENSION_new());
  29749. X509_EXTENSION_set_critical(ext, 1);
  29750. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  29751. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  29752. ASN1_OBJECT_free(obj);
  29753. obj = NULL;
  29754. X509_EXTENSION_free(ext);
  29755. ext = NULL;
  29756. ExpectNotNull(ext = X509_EXTENSION_new());
  29757. X509_EXTENSION_set_critical(ext, 0);
  29758. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  29759. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  29760. NULL);
  29761. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  29762. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  29763. * and X509_get_ext_d2i has created new */
  29764. asn1_str = NULL;
  29765. X509_EXTENSION_free(ext);
  29766. ext = NULL;
  29767. #endif
  29768. BASIC_CONSTRAINTS_free(bc);
  29769. bc = NULL;
  29770. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  29771. NID_key_usage, &crit, NULL));
  29772. ExpectIntEQ(crit, 1);
  29773. ExpectIntEQ(asn1_str->type, NID_key_usage);
  29774. #ifdef OPENSSL_ALL
  29775. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  29776. X509_EXTENSION_free(ext);
  29777. ext = NULL;
  29778. #endif
  29779. ASN1_STRING_free(asn1_str);
  29780. asn1_str = NULL;
  29781. #ifdef OPENSSL_ALL
  29782. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  29783. NID_ext_key_usage, &crit, NULL));
  29784. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  29785. X509_EXTENSION_free(ext);
  29786. ext = NULL;
  29787. EXTENDED_KEY_USAGE_free(sk);
  29788. sk = NULL;
  29789. #else
  29790. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  29791. &crit, NULL);
  29792. ExpectNull(sk);
  29793. #endif
  29794. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  29795. NID_authority_key_identifier, &crit, NULL));
  29796. #ifdef OPENSSL_ALL
  29797. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  29798. akey));
  29799. X509_EXTENSION_free(ext);
  29800. ext = NULL;
  29801. #endif
  29802. wolfSSL_AUTHORITY_KEYID_free(akey);
  29803. akey = NULL;
  29804. /* NID not yet supported */
  29805. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29806. NID_private_key_usage_period, &crit, NULL));
  29807. ExpectIntEQ(crit, -1);
  29808. sk_ASN1_OBJECT_free(sk);
  29809. sk = NULL;
  29810. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  29811. NID_subject_alt_name, &crit, NULL));
  29812. {
  29813. int i;
  29814. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  29815. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  29816. ExpectIntEQ(gen->type, GEN_DNS);
  29817. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  29818. }
  29819. }
  29820. sk_GENERAL_NAME_free(sk);
  29821. sk = NULL;
  29822. /* NID not yet supported */
  29823. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29824. NID_issuer_alt_name, &crit, NULL));
  29825. ExpectIntEQ(crit, -1);
  29826. sk_ASN1_OBJECT_free(sk);
  29827. sk = NULL;
  29828. /* NID not yet supported */
  29829. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29830. NID_info_access, &crit, NULL));
  29831. sk_ASN1_OBJECT_free(sk);
  29832. sk = NULL;
  29833. /* NID not yet supported */
  29834. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29835. NID_sinfo_access, &crit, NULL));
  29836. ExpectIntEQ(crit, -1);
  29837. sk_ASN1_OBJECT_free(sk);
  29838. sk = NULL;
  29839. /* NID not yet supported */
  29840. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29841. NID_name_constraints, &crit, NULL));
  29842. ExpectIntEQ(crit, -1);
  29843. sk_ASN1_OBJECT_free(sk);
  29844. sk = NULL;
  29845. /* no cert policy set */
  29846. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29847. NID_certificate_policies, &crit, NULL));
  29848. sk_ASN1_OBJECT_free(sk);
  29849. sk = NULL;
  29850. /* NID not yet supported */
  29851. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29852. NID_policy_mappings, &crit, NULL));
  29853. ExpectIntEQ(crit, -1);
  29854. sk_ASN1_OBJECT_free(sk);
  29855. sk = NULL;
  29856. /* NID not yet supported */
  29857. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29858. NID_policy_constraints, &crit, NULL));
  29859. ExpectIntEQ(crit, -1);
  29860. sk_ASN1_OBJECT_free(sk);
  29861. sk = NULL;
  29862. /* NID not yet supported */
  29863. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29864. NID_inhibit_any_policy, &crit, NULL));
  29865. ExpectIntEQ(crit, -1);
  29866. sk_ASN1_OBJECT_free(sk);
  29867. sk = NULL;
  29868. /* NID not yet supported */
  29869. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  29870. NID_tlsfeature, &crit, NULL));
  29871. ExpectIntEQ(crit, -1);
  29872. sk_ASN1_OBJECT_free(sk);
  29873. sk = NULL;
  29874. /* test invalid cases */
  29875. crit = 0;
  29876. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  29877. NULL));
  29878. ExpectIntEQ(crit, -1);
  29879. /* NULL passed for criticality. */
  29880. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  29881. NID_tlsfeature, NULL, NULL));
  29882. ExpectIntEQ(SSL_get_hit(ssl), 0);
  29883. #ifdef OPENSSL_ALL
  29884. X509_free(x509);
  29885. #endif
  29886. X509_free(x509ext);
  29887. SSL_free(ssl);
  29888. SSL_CTX_free(ctx);
  29889. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  29890. return EXPECT_RESULT();
  29891. }
  29892. static int test_wolfSSL_X509_check_private_key(void)
  29893. {
  29894. EXPECT_DECLS;
  29895. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  29896. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  29897. X509* x509 = NULL;
  29898. EVP_PKEY* pkey = NULL;
  29899. const byte* key;
  29900. /* Check with correct key */
  29901. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  29902. SSL_FILETYPE_PEM)));
  29903. key = client_key_der_2048;
  29904. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  29905. (long)sizeof_client_key_der_2048));
  29906. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  29907. EVP_PKEY_free(pkey);
  29908. pkey = NULL;
  29909. /* Check with wrong key */
  29910. key = server_key_der_2048;
  29911. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  29912. (long)sizeof_server_key_der_2048));
  29913. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  29914. /* test for incorrect parameter */
  29915. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  29916. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  29917. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  29918. EVP_PKEY_free(pkey);
  29919. X509_free(x509);
  29920. #endif
  29921. return EXPECT_RESULT();
  29922. }
  29923. static int test_wolfSSL_private_keys(void)
  29924. {
  29925. EXPECT_DECLS;
  29926. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29927. !defined(NO_FILESYSTEM)
  29928. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29929. WOLFSSL* ssl = NULL;
  29930. WOLFSSL_CTX* ctx = NULL;
  29931. EVP_PKEY* pkey = NULL;
  29932. OpenSSL_add_all_digests();
  29933. OpenSSL_add_all_algorithms();
  29934. #ifndef NO_RSA
  29935. #ifndef NO_WOLFSSL_SERVER
  29936. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29937. #else
  29938. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29939. #endif
  29940. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  29941. /* Have to load a cert before you can check the private key against that
  29942. * certificates public key! */
  29943. #if !defined(NO_CHECK_PRIVATE_KEY)
  29944. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  29945. #endif
  29946. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  29947. #if !defined(NO_CHECK_PRIVATE_KEY)
  29948. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29949. #endif
  29950. ExpectNotNull(ssl = SSL_new(ctx));
  29951. #if !defined(NO_CHECK_PRIVATE_KEY)
  29952. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29953. #endif
  29954. #ifdef USE_CERT_BUFFERS_2048
  29955. {
  29956. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29957. unsigned char buf[FOURK_BUF];
  29958. word32 bufSz;
  29959. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  29960. (unsigned char*)client_key_der_2048,
  29961. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  29962. #if !defined(NO_CHECK_PRIVATE_KEY)
  29963. /* Should mismatch now that a different private key loaded */
  29964. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29965. #endif
  29966. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  29967. (unsigned char*)server_key,
  29968. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  29969. #if !defined(NO_CHECK_PRIVATE_KEY)
  29970. /* After loading back in DER format of original key, should match */
  29971. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29972. #endif
  29973. /* test loading private key to the WOLFSSL_CTX */
  29974. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  29975. (unsigned char*)client_key_der_2048,
  29976. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  29977. #if !defined(NO_CHECK_PRIVATE_KEY)
  29978. /* Should mismatch now that a different private key loaded */
  29979. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29980. #endif
  29981. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  29982. (unsigned char*)server_key,
  29983. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  29984. #if !defined(NO_CHECK_PRIVATE_KEY)
  29985. /* After loading back in DER format of original key, should match */
  29986. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  29987. #endif
  29988. /* pkey not set yet, expecting to fail */
  29989. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  29990. /* set PKEY and test again */
  29991. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29992. &server_key, (long)sizeof_server_key_der_2048));
  29993. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  29994. /* reuse PKEY structure and test
  29995. * this should be checked with a memory management sanity checker */
  29996. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  29997. server_key = (const unsigned char*)server_key_der_2048;
  29998. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29999. &server_key, (long)sizeof_server_key_der_2048));
  30000. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30001. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30002. bufSz = FOURK_BUF;
  30003. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  30004. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30005. RSAk, NULL, 0)), 0);
  30006. server_key = (const unsigned char*)buf;
  30007. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30008. (long)bufSz));
  30009. }
  30010. #endif
  30011. EVP_PKEY_free(pkey);
  30012. pkey = NULL;
  30013. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30014. ssl = NULL;
  30015. SSL_CTX_free(ctx);
  30016. ctx = NULL;
  30017. #endif /* end of RSA private key match tests */
  30018. #ifdef HAVE_ECC
  30019. #ifndef NO_WOLFSSL_SERVER
  30020. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30021. #else
  30022. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30023. #endif
  30024. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30025. WOLFSSL_FILETYPE_PEM));
  30026. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30027. WOLFSSL_FILETYPE_PEM));
  30028. ExpectNotNull(ssl = SSL_new(ctx));
  30029. #if !defined(NO_CHECK_PRIVATE_KEY)
  30030. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30031. #endif
  30032. SSL_free(ssl);
  30033. ssl = NULL;
  30034. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30035. WOLFSSL_FILETYPE_PEM));
  30036. ExpectNotNull(ssl = SSL_new(ctx));
  30037. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30038. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30039. #endif
  30040. SSL_free(ssl);
  30041. ssl = NULL;
  30042. SSL_CTX_free(ctx);
  30043. ctx = NULL;
  30044. #endif /* end of ECC private key match tests */
  30045. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30046. #ifndef NO_WOLFSSL_SERVER
  30047. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30048. #else
  30049. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30050. #endif
  30051. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30052. WOLFSSL_FILETYPE_PEM));
  30053. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30054. WOLFSSL_FILETYPE_PEM));
  30055. ExpectNotNull(ssl = SSL_new(ctx));
  30056. #if !defined(NO_CHECK_PRIVATE_KEY)
  30057. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30058. #endif
  30059. SSL_free(ssl);
  30060. ssl = NULL;
  30061. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30062. WOLFSSL_FILETYPE_PEM));
  30063. ExpectNotNull(ssl = SSL_new(ctx));
  30064. #if !defined(NO_CHECK_PRIVATE_KEY)
  30065. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30066. #endif
  30067. SSL_free(ssl);
  30068. ssl = NULL;
  30069. SSL_CTX_free(ctx);
  30070. ctx = NULL;
  30071. #endif /* end of Ed25519 private key match tests */
  30072. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30073. #ifndef NO_WOLFSSL_SERVER
  30074. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30075. #else
  30076. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30077. #endif
  30078. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30079. WOLFSSL_FILETYPE_PEM));
  30080. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30081. WOLFSSL_FILETYPE_PEM));
  30082. ExpectNotNull(ssl = SSL_new(ctx));
  30083. #if !defined(NO_CHECK_PRIVATE_KEY)
  30084. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30085. #endif
  30086. SSL_free(ssl);
  30087. ssl = NULL;
  30088. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30089. WOLFSSL_FILETYPE_PEM));
  30090. ExpectNotNull(ssl = SSL_new(ctx));
  30091. #if !defined(NO_CHECK_PRIVATE_KEY)
  30092. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30093. #endif
  30094. SSL_free(ssl);
  30095. ssl = NULL;
  30096. SSL_CTX_free(ctx);
  30097. ctx = NULL;
  30098. #endif /* end of Ed448 private key match tests */
  30099. EVP_cleanup();
  30100. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30101. CONF_modules_free();
  30102. ENGINE_cleanup();
  30103. CONF_modules_unload();
  30104. (void)ssl;
  30105. (void)ctx;
  30106. (void)pkey;
  30107. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30108. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30109. return EXPECT_RESULT();
  30110. }
  30111. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30112. {
  30113. EXPECT_DECLS;
  30114. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30115. && !defined(NO_FILESYSTEM)
  30116. XFILE file = XBADFILE;
  30117. const char* fname = "./certs/server-key.pem";
  30118. EVP_PKEY* pkey = NULL;
  30119. RSA* rsa = NULL;
  30120. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30121. unsigned char* sig = NULL;
  30122. size_t sigLen = 0;
  30123. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30124. size_t tbsLen = sizeof(tbs);
  30125. /* Check error case. */
  30126. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30127. /* Read in an RSA key. */
  30128. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30129. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30130. if (file != XBADFILE)
  30131. XFCLOSE(file);
  30132. /* Make sure the key is usable by signing some data with it. */
  30133. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30134. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30135. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30136. DYNAMIC_TYPE_TMP_BUFFER));
  30137. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30138. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30139. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30140. WOLFSSL_SUCCESS);
  30141. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30142. EVP_PKEY_CTX_free(ctx);
  30143. EVP_PKEY_free(pkey);
  30144. #endif
  30145. return EXPECT_RESULT();
  30146. }
  30147. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30148. {
  30149. EXPECT_DECLS;
  30150. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30151. && !defined(NO_FILESYSTEM)
  30152. XFILE file = XBADFILE;
  30153. const char* fname = "./certs/client-keyPub.pem";
  30154. EVP_PKEY* pkey = NULL;
  30155. /* Check error case. */
  30156. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30157. /* Read in an RSA key. */
  30158. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30159. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30160. EVP_PKEY_free(pkey);
  30161. if (file != XBADFILE)
  30162. XFCLOSE(file);
  30163. #endif
  30164. return EXPECT_RESULT();
  30165. }
  30166. static int test_wolfSSL_PEM_PrivateKey(void)
  30167. {
  30168. EXPECT_DECLS;
  30169. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30170. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  30171. #ifndef NO_BIO
  30172. BIO* bio = NULL;
  30173. #endif
  30174. EVP_PKEY* pkey = NULL;
  30175. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30176. #ifndef NO_BIO
  30177. /* test creating new EVP_PKEY with bad arg */
  30178. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  30179. /* test loading RSA key using BIO */
  30180. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30181. {
  30182. XFILE file = XBADFILE;
  30183. const char* fname = "./certs/server-key.pem";
  30184. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30185. size_t sz = 0;
  30186. byte* buf = NULL;
  30187. EVP_PKEY* pkey2 = NULL;
  30188. EVP_PKEY* pkey3 = NULL;
  30189. RSA* rsa_key = NULL;
  30190. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30191. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30192. ExpectIntGT(sz = XFTELL(file), 0);
  30193. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30194. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30195. if (buf != NULL) {
  30196. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30197. }
  30198. if (file != XBADFILE) {
  30199. XFCLOSE(file);
  30200. file = XBADFILE;
  30201. }
  30202. /* Test using BIO new mem and loading PEM private key */
  30203. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30204. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30205. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30206. buf = NULL;
  30207. BIO_free(bio);
  30208. bio = NULL;
  30209. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30210. if (pkey2 != NULL) {
  30211. pkey2->type = EVP_PKEY_RSA;
  30212. }
  30213. /* Test parameter copy */
  30214. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30215. EVP_PKEY_free(pkey2);
  30216. EVP_PKEY_free(pkey);
  30217. pkey = NULL;
  30218. /* Qt unit test case : rsa pkcs8 key */
  30219. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30220. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30221. ExpectIntGT(sz = XFTELL(file), 0);
  30222. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30223. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30224. if (buf) {
  30225. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30226. }
  30227. if (file != XBADFILE) {
  30228. XFCLOSE(file);
  30229. file = XBADFILE;
  30230. }
  30231. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30232. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30233. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30234. buf = NULL;
  30235. BIO_free(bio);
  30236. bio = NULL;
  30237. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30238. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30239. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30240. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30241. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30242. #else
  30243. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30244. #endif
  30245. RSA_free(rsa_key);
  30246. EVP_PKEY_free(pkey3);
  30247. EVP_PKEY_free(pkey);
  30248. pkey = NULL;
  30249. }
  30250. #endif
  30251. /* test loading ECC key using BIO */
  30252. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30253. {
  30254. XFILE file = XBADFILE;
  30255. const char* fname = "./certs/ecc-key.pem";
  30256. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30257. size_t sz = 0;
  30258. byte* buf = NULL;
  30259. EVP_PKEY* pkey2 = NULL;
  30260. EVP_PKEY* pkey3 = NULL;
  30261. EC_KEY* ec_key = NULL;
  30262. int nid = 0;
  30263. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30264. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30265. ExpectIntGT(sz = XFTELL(file), 0);
  30266. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30267. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30268. if (buf) {
  30269. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30270. }
  30271. if (file != XBADFILE) {
  30272. XFCLOSE(file);
  30273. file = XBADFILE;
  30274. }
  30275. /* Test using BIO new mem and loading PEM private key */
  30276. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30277. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30278. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30279. buf = NULL;
  30280. BIO_free(bio);
  30281. bio = NULL;
  30282. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30283. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30284. if (pkey2 != NULL) {
  30285. pkey2->type = EVP_PKEY_EC;
  30286. }
  30287. /* Test parameter copy */
  30288. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30289. /* Qt unit test case 1*/
  30290. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30291. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30292. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30293. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30294. #else
  30295. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30296. #endif
  30297. /* Test default digest */
  30298. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30299. ExpectIntEQ(nid, NID_sha256);
  30300. EC_KEY_free(ec_key);
  30301. ec_key = NULL;
  30302. EVP_PKEY_free(pkey3);
  30303. pkey3 = NULL;
  30304. EVP_PKEY_free(pkey2);
  30305. pkey2 = NULL;
  30306. EVP_PKEY_free(pkey);
  30307. pkey = NULL;
  30308. /* Qt unit test case ec pkcs8 key */
  30309. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  30310. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30311. ExpectIntGT(sz = XFTELL(file), 0);
  30312. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30313. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30314. if (buf) {
  30315. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30316. }
  30317. if (file != XBADFILE) {
  30318. XFCLOSE(file);
  30319. file = XBADFILE;
  30320. }
  30321. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30322. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30323. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30324. buf = NULL;
  30325. BIO_free(bio);
  30326. bio = NULL;
  30327. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30328. /* Qt unit test case */
  30329. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30330. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30331. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30332. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30333. #else
  30334. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30335. #endif
  30336. EC_KEY_free(ec_key);
  30337. EVP_PKEY_free(pkey3);
  30338. EVP_PKEY_free(pkey);
  30339. pkey = NULL;
  30340. }
  30341. #endif
  30342. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  30343. defined(WOLFSSL_CERT_GEN))
  30344. {
  30345. #define BIO_PEM_TEST_CHAR 'a'
  30346. EVP_PKEY* pkey2 = NULL;
  30347. unsigned char extra[10];
  30348. int i;
  30349. BIO* pub_bio = NULL;
  30350. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30351. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30352. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30353. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30354. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30355. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  30356. &server_key, (long)sizeof_server_key_der_2048));
  30357. ExpectNull(pkey);
  30358. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30359. &server_key, (long)sizeof_server_key_der_2048));
  30360. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  30361. NULL), WOLFSSL_FAILURE);
  30362. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  30363. NULL), WOLFSSL_FAILURE);
  30364. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30365. NULL), WOLFSSL_SUCCESS);
  30366. ExpectIntGT(BIO_pending(bio), 0);
  30367. ExpectIntEQ(BIO_pending(bio), 1679);
  30368. /* Check if the pubkey API writes only the public key */
  30369. #ifdef WOLFSSL_KEY_GEN
  30370. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30371. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30372. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30373. ExpectIntGT(BIO_pending(pub_bio), 0);
  30374. /* Previously both the private key and the pubkey calls would write
  30375. * out the private key and the PEM header was the only difference.
  30376. * The public PEM should be significantly shorter than the
  30377. * private key versison. */
  30378. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30379. #endif
  30380. /* test creating new EVP_PKEY with good args */
  30381. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30382. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  30383. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  30384. /* test of reuse of EVP_PKEY */
  30385. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30386. ExpectIntEQ(BIO_pending(bio), 0);
  30387. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30388. SSL_SUCCESS);
  30389. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  30390. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30391. ExpectNotNull(pkey);
  30392. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30393. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  30394. }
  30395. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  30396. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30397. for (i = 0; i < 10; i++) {
  30398. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  30399. }
  30400. BIO_free(pub_bio);
  30401. BIO_free(bio);
  30402. bio = NULL;
  30403. EVP_PKEY_free(pkey);
  30404. pkey = NULL;
  30405. EVP_PKEY_free(pkey2);
  30406. }
  30407. #endif
  30408. /* key is DES encrypted */
  30409. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  30410. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  30411. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  30412. {
  30413. XFILE f = XBADFILE;
  30414. wc_pem_password_cb* passwd_cb = NULL;
  30415. void* passwd_cb_userdata;
  30416. SSL_CTX* ctx = NULL;
  30417. char passwd[] = "bad password";
  30418. #ifndef WOLFSSL_NO_TLS12
  30419. #ifndef NO_WOLFSSL_SERVER
  30420. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  30421. #else
  30422. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  30423. #endif
  30424. #else
  30425. #ifndef NO_WOLFSSL_SERVER
  30426. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  30427. #else
  30428. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  30429. #endif
  30430. #endif
  30431. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  30432. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  30433. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  30434. ExpectNull(passwd_cb_userdata =
  30435. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  30436. /* fail case with password call back */
  30437. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  30438. (void*)passwd));
  30439. BIO_free(bio);
  30440. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  30441. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  30442. (void*)passwd));
  30443. BIO_free(bio);
  30444. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  30445. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  30446. if ((bio == NULL) && (f != XBADFILE)) {
  30447. XFCLOSE(f);
  30448. }
  30449. /* use callback that works */
  30450. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  30451. (void*)"yassl123"));
  30452. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  30453. EVP_PKEY_free(pkey);
  30454. pkey = NULL;
  30455. BIO_free(bio);
  30456. bio = NULL;
  30457. SSL_CTX_free(ctx);
  30458. }
  30459. #endif /* !defined(NO_DES3) */
  30460. #endif /* !NO_BIO */
  30461. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30462. {
  30463. unsigned char buf[2048];
  30464. size_t bytes = 0;
  30465. XFILE f = XBADFILE;
  30466. SSL_CTX* ctx = NULL;
  30467. #ifndef WOLFSSL_NO_TLS12
  30468. #ifndef NO_WOLFSSL_SERVER
  30469. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  30470. #else
  30471. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  30472. #endif
  30473. #else
  30474. #ifndef NO_WOLFSSL_SERVER
  30475. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  30476. #else
  30477. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  30478. #endif
  30479. #endif
  30480. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30481. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  30482. if (f != XBADFILE)
  30483. XFCLOSE(f);
  30484. server_key = buf;
  30485. pkey = NULL;
  30486. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  30487. ExpectNull(pkey);
  30488. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  30489. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  30490. EVP_PKEY_free(pkey);
  30491. pkey = NULL;
  30492. SSL_CTX_free(ctx);
  30493. server_key = NULL;
  30494. }
  30495. #endif
  30496. #ifndef NO_BIO
  30497. (void)bio;
  30498. #endif
  30499. (void)pkey;
  30500. (void)server_key;
  30501. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  30502. return EXPECT_RESULT();
  30503. }
  30504. static int test_wolfSSL_PEM_file_RSAKey(void)
  30505. {
  30506. EXPECT_DECLS;
  30507. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30508. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  30509. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  30510. RSA* rsa = NULL;
  30511. XFILE fp = XBADFILE;
  30512. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  30513. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  30514. if (fp != XBADFILE)
  30515. XFCLOSE(fp);
  30516. ExpectIntEQ(RSA_size(rsa), 256);
  30517. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  30518. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  30519. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  30520. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  30521. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  30522. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  30523. RSA_free(rsa);
  30524. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30525. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  30526. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  30527. return EXPECT_RESULT();
  30528. }
  30529. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  30530. {
  30531. EXPECT_DECLS;
  30532. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  30533. !defined(NO_FILESYSTEM) && \
  30534. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  30535. RSA* rsa = NULL;
  30536. XFILE f = NULL;
  30537. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  30538. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  30539. ExpectIntEQ(RSA_size(rsa), 256);
  30540. if (f != XBADFILE) {
  30541. XFCLOSE(f);
  30542. f = XBADFILE;
  30543. }
  30544. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  30545. NULL), WOLFSSL_FAILURE);
  30546. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  30547. NULL), WOLFSSL_FAILURE);
  30548. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  30549. WOLFSSL_SUCCESS);
  30550. RSA_free(rsa);
  30551. #ifdef HAVE_ECC
  30552. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  30553. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  30554. if (f != XBADFILE)
  30555. XFCLOSE(f);
  30556. #endif /* HAVE_ECC */
  30557. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30558. return EXPECT_RESULT();
  30559. }
  30560. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  30561. {
  30562. EXPECT_DECLS;
  30563. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30564. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30565. XFILE file = XBADFILE;
  30566. const char* fname = "./certs/client-keyPub.pem";
  30567. RSA *rsa = NULL;
  30568. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  30569. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30570. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  30571. ExpectIntEQ(RSA_size(rsa), 256);
  30572. RSA_free(rsa);
  30573. if (file != XBADFILE)
  30574. XFCLOSE(file);
  30575. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30576. return EXPECT_RESULT();
  30577. }
  30578. #ifndef NO_BIO
  30579. static int test_wolfSSL_PEM_bio_RSAKey(void)
  30580. {
  30581. EXPECT_DECLS;
  30582. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30583. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  30584. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  30585. RSA* rsa = NULL;
  30586. BIO* bio = NULL;
  30587. /* PrivateKey */
  30588. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  30589. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  30590. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  30591. ExpectNotNull(rsa);
  30592. ExpectIntEQ(RSA_size(rsa), 256);
  30593. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  30594. NULL), WOLFSSL_FAILURE);
  30595. BIO_free(bio);
  30596. bio = NULL;
  30597. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30598. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  30599. NULL), WOLFSSL_SUCCESS);
  30600. BIO_free(bio);
  30601. bio = NULL;
  30602. RSA_free(rsa);
  30603. rsa = NULL;
  30604. /* PUBKEY */
  30605. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  30606. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  30607. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30608. ExpectIntEQ(RSA_size(rsa), 256);
  30609. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30610. BIO_free(bio);
  30611. bio = NULL;
  30612. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30613. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  30614. BIO_free(bio);
  30615. bio = NULL;
  30616. RSA_free(rsa);
  30617. rsa = NULL;
  30618. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  30619. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  30620. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30621. BIO_free(bio);
  30622. bio = NULL;
  30623. RSA_free(rsa);
  30624. rsa = NULL;
  30625. #ifdef HAVE_ECC
  30626. /* ensure that non-rsa keys do not work */
  30627. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  30628. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30629. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  30630. BIO_free(bio);
  30631. bio = NULL;
  30632. RSA_free(rsa);
  30633. rsa = NULL;
  30634. #endif /* HAVE_ECC */
  30635. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30636. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  30637. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  30638. return EXPECT_RESULT();
  30639. }
  30640. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  30641. {
  30642. EXPECT_DECLS;
  30643. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30644. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30645. RSA* rsa = NULL;
  30646. RSA* rsa_dup = NULL;
  30647. BIO* bio = NULL;
  30648. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  30649. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30650. ExpectIntEQ(RSA_size(rsa), 256);
  30651. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  30652. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  30653. /* Test duplicating empty key. */
  30654. ExpectNotNull(rsa_dup = RSA_new());
  30655. ExpectNull(RSAPublicKey_dup(rsa_dup));
  30656. RSA_free(rsa_dup);
  30657. rsa_dup = NULL;
  30658. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  30659. ExpectPtrNE(rsa_dup, rsa);
  30660. #endif
  30661. /* test if valgrind complains about unreleased memory */
  30662. RSA_up_ref(rsa);
  30663. RSA_free(rsa);
  30664. BIO_free(bio);
  30665. bio = NULL;
  30666. RSA_free(rsa);
  30667. rsa = NULL;
  30668. RSA_free(rsa_dup);
  30669. rsa_dup = NULL;
  30670. #ifdef HAVE_ECC
  30671. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  30672. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  30673. BIO_free(bio);
  30674. #endif /* HAVE_ECC */
  30675. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30676. return EXPECT_RESULT();
  30677. }
  30678. static int test_wolfSSL_PEM_bio_DSAKey(void)
  30679. {
  30680. EXPECT_DECLS;
  30681. #ifndef HAVE_SELFTEST
  30682. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  30683. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  30684. DSA* dsa = NULL;
  30685. BIO* bio = NULL;
  30686. /* PrivateKey */
  30687. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  30688. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  30689. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  30690. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  30691. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  30692. NULL), WOLFSSL_FAILURE);
  30693. BIO_free(bio);
  30694. bio = NULL;
  30695. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30696. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  30697. NULL), WOLFSSL_SUCCESS);
  30698. BIO_free(bio);
  30699. bio = NULL;
  30700. DSA_free(dsa);
  30701. dsa = NULL;
  30702. /* PUBKEY */
  30703. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  30704. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  30705. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  30706. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  30707. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30708. BIO_free(bio);
  30709. bio = NULL;
  30710. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30711. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  30712. BIO_free(bio);
  30713. bio = NULL;
  30714. DSA_free(dsa);
  30715. dsa = NULL;
  30716. #ifdef HAVE_ECC
  30717. /* ensure that non-dsa keys do not work */
  30718. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  30719. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  30720. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  30721. BIO_free(bio);
  30722. bio = NULL;
  30723. DSA_free(dsa);
  30724. dsa = NULL;
  30725. #endif /* HAVE_ECC */
  30726. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  30727. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  30728. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  30729. #endif /* HAVE_SELFTEST */
  30730. return EXPECT_RESULT();
  30731. }
  30732. static int test_wolfSSL_PEM_bio_ECKey(void)
  30733. {
  30734. EXPECT_DECLS;
  30735. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  30736. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  30737. EC_KEY* ec = NULL;
  30738. EC_KEY* ec2;
  30739. BIO* bio = NULL;
  30740. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  30741. unsigned char* pem = NULL;
  30742. int pLen;
  30743. #endif
  30744. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  30745. "MAA=\n"
  30746. "-----END PUBLIC KEY-----";
  30747. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  30748. "MAA=\n"
  30749. "-----END EC PRIVATE KEY-----";
  30750. /* PrivateKey */
  30751. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  30752. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  30753. ec2 = NULL;
  30754. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  30755. ExpectIntEQ(ec == ec2, 1);
  30756. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  30757. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  30758. NULL), WOLFSSL_FAILURE);
  30759. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  30760. NULL), WOLFSSL_FAILURE);
  30761. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  30762. WOLFSSL_FAILURE);
  30763. BIO_free(bio);
  30764. bio = NULL;
  30765. /* Public key data - fail. */
  30766. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  30767. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  30768. BIO_free(bio);
  30769. bio = NULL;
  30770. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30771. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  30772. NULL), WOLFSSL_SUCCESS);
  30773. BIO_free(bio);
  30774. bio = NULL;
  30775. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  30776. NULL),WOLFSSL_FAILURE);
  30777. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  30778. WOLFSSL_FAILURE);
  30779. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  30780. WOLFSSL_FAILURE);
  30781. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  30782. WOLFSSL_SUCCESS);
  30783. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  30784. NULL), 0);
  30785. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  30786. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  30787. NULL), 0);
  30788. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  30789. NULL), 0);
  30790. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  30791. &pLen), 0);
  30792. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  30793. &pLen), 0);
  30794. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  30795. &pLen), 0);
  30796. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  30797. NULL), 0);
  30798. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  30799. &pLen), 1);
  30800. ExpectIntGT(pLen, 0);
  30801. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30802. #endif
  30803. EC_KEY_free(ec);
  30804. ec = NULL;
  30805. /* PUBKEY */
  30806. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  30807. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  30808. ec2 = NULL;
  30809. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  30810. ExpectIntEQ(ec == ec2, 1);
  30811. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  30812. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30813. BIO_free(bio);
  30814. bio = NULL;
  30815. /* Test 0x30, 0x00 fails. */
  30816. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  30817. sizeof(ec_key_bad_1)));
  30818. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  30819. BIO_free(bio);
  30820. bio = NULL;
  30821. /* Private key data - fail. */
  30822. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  30823. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  30824. BIO_free(bio);
  30825. bio = NULL;
  30826. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30827. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  30828. BIO_free(bio);
  30829. bio = NULL;
  30830. /* Same test as above, but with a file pointer rather than a BIO. */
  30831. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  30832. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  30833. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  30834. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  30835. EC_KEY_free(ec);
  30836. ec = NULL;
  30837. #ifndef NO_RSA
  30838. /* ensure that non-ec keys do not work */
  30839. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  30840. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  30841. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  30842. BIO_free(bio);
  30843. bio = NULL;
  30844. EC_KEY_free(ec);
  30845. ec = NULL;
  30846. #endif /* !NO_RSA */
  30847. /* Test 0x30, 0x00 fails. */
  30848. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  30849. sizeof(ec_priv_key_bad_1)));
  30850. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  30851. BIO_free(bio);
  30852. bio = NULL;
  30853. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30854. return EXPECT_RESULT();
  30855. }
  30856. static int test_wolfSSL_PEM_PUBKEY(void)
  30857. {
  30858. EXPECT_DECLS;
  30859. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  30860. BIO* bio = NULL;
  30861. EVP_PKEY* pkey = NULL;
  30862. /* test creating new EVP_PKEY with bad arg */
  30863. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  30864. /* test loading ECC key using BIO */
  30865. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  30866. {
  30867. XFILE file = XBADFILE;
  30868. const char* fname = "./certs/ecc-client-keyPub.pem";
  30869. size_t sz;
  30870. byte* buf = NULL;
  30871. EVP_PKEY* pkey2 = NULL;
  30872. EC_KEY* ec_key = NULL;
  30873. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30874. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  30875. ExpectIntGT(sz = XFTELL(file), 0);
  30876. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  30877. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30878. if (buf != NULL) {
  30879. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30880. }
  30881. if (file != XBADFILE) {
  30882. XFCLOSE(file);
  30883. }
  30884. /* Test using BIO new mem and loading PEM private key */
  30885. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30886. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  30887. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30888. BIO_free(bio);
  30889. bio = NULL;
  30890. /* Qt unit test case*/
  30891. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30892. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30893. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  30894. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30895. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  30896. #else
  30897. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  30898. #endif
  30899. EC_KEY_free(ec_key);
  30900. EVP_PKEY_free(pkey2);
  30901. EVP_PKEY_free(pkey);
  30902. pkey = NULL;
  30903. }
  30904. #endif
  30905. (void)bio;
  30906. (void)pkey;
  30907. #endif
  30908. return EXPECT_RESULT();
  30909. }
  30910. #endif /* !NO_BIO */
  30911. static int test_DSA_do_sign_verify(void)
  30912. {
  30913. EXPECT_DECLS;
  30914. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  30915. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30916. !defined(NO_DSA)
  30917. unsigned char digest[WC_SHA_DIGEST_SIZE];
  30918. DSA_SIG* sig = NULL;
  30919. DSA* dsa = NULL;
  30920. word32 bytes;
  30921. byte sigBin[DSA_SIG_SIZE];
  30922. int dsacheck;
  30923. #ifdef USE_CERT_BUFFERS_1024
  30924. byte tmp[ONEK_BUF];
  30925. XMEMSET(tmp, 0, sizeof(tmp));
  30926. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  30927. bytes = sizeof_dsa_key_der_1024;
  30928. #elif defined(USE_CERT_BUFFERS_2048)
  30929. byte tmp[TWOK_BUF];
  30930. XMEMSET(tmp, 0, sizeof(tmp));
  30931. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  30932. bytes = sizeof_dsa_key_der_2048;
  30933. #else
  30934. byte tmp[TWOK_BUF];
  30935. XFILE fp = XBADFILE;
  30936. XMEMSET(tmp, 0, sizeof(tmp));
  30937. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  30938. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  30939. if (fp != XBADFILE)
  30940. XFCLOSE(fp);
  30941. #endif /* END USE_CERT_BUFFERS_1024 */
  30942. XMEMSET(digest, 202, sizeof(digest));
  30943. ExpectNotNull(dsa = DSA_new());
  30944. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  30945. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  30946. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  30947. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  30948. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  30949. DSA_SIG_free(sig);
  30950. DSA_free(dsa);
  30951. #endif
  30952. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  30953. return EXPECT_RESULT();
  30954. }
  30955. static int test_wolfSSL_tmp_dh(void)
  30956. {
  30957. EXPECT_DECLS;
  30958. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30959. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  30960. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30961. byte buff[6000];
  30962. char file[] = "./certs/dsaparams.pem";
  30963. XFILE f = XBADFILE;
  30964. int bytes = 0;
  30965. DSA* dsa = NULL;
  30966. DH* dh = NULL;
  30967. #if defined(WOLFSSL_DH_EXTRA) && \
  30968. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30969. DH* dh2 = NULL;
  30970. #endif
  30971. BIO* bio = NULL;
  30972. SSL* ssl = NULL;
  30973. SSL_CTX* ctx = NULL;
  30974. #ifndef NO_WOLFSSL_SERVER
  30975. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30976. #else
  30977. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30978. #endif
  30979. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30980. WOLFSSL_FILETYPE_PEM));
  30981. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30982. WOLFSSL_FILETYPE_PEM));
  30983. ExpectNotNull(ssl = SSL_new(ctx));
  30984. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  30985. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  30986. if (f != XBADFILE)
  30987. XFCLOSE(f);
  30988. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  30989. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  30990. ExpectNotNull(dsa);
  30991. dh = wolfSSL_DSA_dup_DH(dsa);
  30992. ExpectNotNull(dh);
  30993. #if defined(WOLFSSL_DH_EXTRA) && \
  30994. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  30995. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  30996. #endif
  30997. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  30998. #ifndef NO_WOLFSSL_SERVER
  30999. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31000. #else
  31001. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31002. #endif
  31003. BIO_free(bio);
  31004. DSA_free(dsa);
  31005. DH_free(dh);
  31006. dh = NULL;
  31007. #if defined(WOLFSSL_DH_EXTRA) && \
  31008. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31009. DH_free(dh2);
  31010. dh2 = NULL;
  31011. #endif
  31012. SSL_free(ssl);
  31013. SSL_CTX_free(ctx);
  31014. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31015. #endif
  31016. return EXPECT_RESULT();
  31017. }
  31018. static int test_wolfSSL_ctrl(void)
  31019. {
  31020. EXPECT_DECLS;
  31021. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31022. byte buff[6000];
  31023. BIO* bio = NULL;
  31024. int bytes;
  31025. BUF_MEM* ptr = NULL;
  31026. XMEMSET(buff, 0, sizeof(buff));
  31027. bytes = sizeof(buff);
  31028. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31029. ExpectNotNull(BIO_s_socket());
  31030. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31031. /* needs tested after stubs filled out @TODO
  31032. SSL_ctrl
  31033. SSL_CTX_ctrl
  31034. */
  31035. BIO_free(bio);
  31036. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31037. return EXPECT_RESULT();
  31038. }
  31039. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31040. {
  31041. EXPECT_DECLS;
  31042. #ifdef OPENSSL_EXTRA
  31043. static const unsigned char pw[] = "password";
  31044. static const int pwSz = sizeof(pw) - 1;
  31045. size_t checkPwSz = 0;
  31046. const unsigned char* checkPw = NULL;
  31047. WOLFSSL_EVP_PKEY* key = NULL;
  31048. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31049. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31050. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31051. pwSz));
  31052. if (key != NULL) {
  31053. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31054. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31055. ExpectIntEQ(key->pkey_sz, pwSz);
  31056. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31057. }
  31058. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31059. ExpectIntEQ((int)checkPwSz, pwSz);
  31060. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31061. wolfSSL_EVP_PKEY_free(key);
  31062. key = NULL;
  31063. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31064. 0));
  31065. ExpectIntEQ(key->pkey_sz, 0);
  31066. if (EXPECT_SUCCESS()) {
  31067. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31068. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31069. }
  31070. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31071. ExpectIntEQ((int)checkPwSz, 0);
  31072. wolfSSL_EVP_PKEY_free(key);
  31073. key = NULL;
  31074. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31075. 0));
  31076. ExpectIntEQ(key->pkey_sz, 0);
  31077. if (EXPECT_SUCCESS()) {
  31078. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31079. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31080. }
  31081. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31082. ExpectIntEQ((int)checkPwSz, 0);
  31083. wolfSSL_EVP_PKEY_free(key);
  31084. key = NULL;
  31085. #endif /* OPENSSL_EXTRA */
  31086. return EXPECT_RESULT();
  31087. }
  31088. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31089. {
  31090. EXPECT_DECLS;
  31091. #ifdef OPENSSL_EXTRA
  31092. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31093. const char *priv = "ABCDEFGHIJKLMNOP";
  31094. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31095. WOLFSSL_EVP_PKEY* key = NULL;
  31096. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31097. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31098. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31099. NULL, (const unsigned char *)priv, 0, cipher));
  31100. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31101. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31102. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31103. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31104. wolfSSL_EVP_PKEY_free(key);
  31105. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31106. #endif /* OPENSSL_EXTRA */
  31107. return EXPECT_RESULT();
  31108. }
  31109. static int test_wolfSSL_EVP_Digest(void)
  31110. {
  31111. EXPECT_DECLS;
  31112. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31113. const char* in = "abc";
  31114. int inLen = (int)XSTRLEN(in);
  31115. byte out[WC_SHA256_DIGEST_SIZE];
  31116. unsigned int outLen;
  31117. const char* expOut =
  31118. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31119. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31120. "\x15\xAD";
  31121. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31122. "SHA256", NULL), 1);
  31123. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31124. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31125. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31126. return EXPECT_RESULT();
  31127. }
  31128. static int test_wolfSSL_EVP_Digest_all(void)
  31129. {
  31130. EXPECT_DECLS;
  31131. #ifdef OPENSSL_EXTRA
  31132. const char* digests[] = {
  31133. #ifndef NO_MD5
  31134. "MD5",
  31135. #endif
  31136. #ifndef NO_SHA
  31137. "SHA",
  31138. #endif
  31139. #ifdef WOLFSSL_SHA224
  31140. "SHA224",
  31141. #endif
  31142. #ifndef NO_SHA256
  31143. "SHA256",
  31144. #endif
  31145. #ifdef WOLFSSL_SHA384
  31146. "SHA384",
  31147. #endif
  31148. #ifdef WOLFSSL_SHA512
  31149. "SHA512",
  31150. #endif
  31151. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31152. "SHA512_224",
  31153. #endif
  31154. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31155. "SHA512_256",
  31156. #endif
  31157. #ifdef WOLFSSL_SHA3
  31158. #ifndef WOLFSSL_NOSHA3_224
  31159. "SHA3_224",
  31160. #endif
  31161. #ifndef WOLFSSL_NOSHA3_256
  31162. "SHA3_256",
  31163. #endif
  31164. "SHA3_384",
  31165. #ifndef WOLFSSL_NOSHA3_512
  31166. "SHA3_512",
  31167. #endif
  31168. #endif /* WOLFSSL_SHA3 */
  31169. NULL
  31170. };
  31171. const char** d;
  31172. const unsigned char in[] = "abc";
  31173. int inLen = XSTR_SIZEOF(in);
  31174. byte out[WC_MAX_DIGEST_SIZE];
  31175. unsigned int outLen;
  31176. for (d = digests; *d != NULL; d++) {
  31177. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31178. ExpectIntGT(outLen, 0);
  31179. ExpectIntEQ(EVP_MD_size(*d), outLen);
  31180. }
  31181. #endif
  31182. return EXPECT_RESULT();
  31183. }
  31184. static int test_wolfSSL_EVP_MD_size(void)
  31185. {
  31186. EXPECT_DECLS;
  31187. #ifdef OPENSSL_EXTRA
  31188. WOLFSSL_EVP_MD_CTX mdCtx;
  31189. #ifdef WOLFSSL_SHA3
  31190. #ifndef WOLFSSL_NOSHA3_224
  31191. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31192. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31193. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31194. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31195. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31196. #endif
  31197. #ifndef WOLFSSL_NOSHA3_256
  31198. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31199. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31200. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31201. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31202. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31203. #endif
  31204. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31205. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31206. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31207. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31208. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31209. #ifndef WOLFSSL_NOSHA3_512
  31210. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31211. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31212. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31213. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31214. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31215. #endif
  31216. #endif /* WOLFSSL_SHA3 */
  31217. #ifndef NO_SHA256
  31218. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31219. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31220. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31221. WC_SHA256_DIGEST_SIZE);
  31222. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31223. WC_SHA256_BLOCK_SIZE);
  31224. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31225. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31226. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31227. #endif
  31228. #ifndef NO_MD5
  31229. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31230. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31231. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31232. WC_MD5_DIGEST_SIZE);
  31233. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31234. WC_MD5_BLOCK_SIZE);
  31235. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31236. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31237. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31238. #endif
  31239. #ifdef WOLFSSL_SHA224
  31240. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31241. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31242. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31243. WC_SHA224_DIGEST_SIZE);
  31244. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31245. WC_SHA224_BLOCK_SIZE);
  31246. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31247. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31248. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31249. #endif
  31250. #ifdef WOLFSSL_SHA384
  31251. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31252. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31253. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31254. WC_SHA384_DIGEST_SIZE);
  31255. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31256. WC_SHA384_BLOCK_SIZE);
  31257. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31258. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31259. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31260. #endif
  31261. #ifdef WOLFSSL_SHA512
  31262. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31263. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31264. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31265. WC_SHA512_DIGEST_SIZE);
  31266. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31267. WC_SHA512_BLOCK_SIZE);
  31268. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  31269. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  31270. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31271. #endif
  31272. #ifndef NO_SHA
  31273. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31274. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  31275. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31276. WC_SHA_DIGEST_SIZE);
  31277. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31278. WC_SHA_BLOCK_SIZE);
  31279. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31280. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31281. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31282. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31283. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  31284. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31285. WC_SHA_DIGEST_SIZE);
  31286. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31287. WC_SHA_BLOCK_SIZE);
  31288. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31289. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31290. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31291. #endif
  31292. /* error case */
  31293. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31294. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  31295. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31296. BAD_FUNC_ARG);
  31297. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  31298. /* Cleanup is valid on uninit'ed struct */
  31299. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31300. #endif /* OPENSSL_EXTRA */
  31301. return EXPECT_RESULT();
  31302. }
  31303. static int test_wolfSSL_EVP_MD_pkey_type(void)
  31304. {
  31305. EXPECT_DECLS;
  31306. #ifdef OPENSSL_EXTRA
  31307. const WOLFSSL_EVP_MD* md;
  31308. #ifndef NO_MD5
  31309. ExpectNotNull(md = EVP_md5());
  31310. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  31311. #endif
  31312. #ifndef NO_SHA
  31313. ExpectNotNull(md = EVP_sha1());
  31314. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  31315. #endif
  31316. #ifdef WOLFSSL_SHA224
  31317. ExpectNotNull(md = EVP_sha224());
  31318. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  31319. #endif
  31320. ExpectNotNull(md = EVP_sha256());
  31321. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  31322. #ifdef WOLFSSL_SHA384
  31323. ExpectNotNull(md = EVP_sha384());
  31324. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  31325. #endif
  31326. #ifdef WOLFSSL_SHA512
  31327. ExpectNotNull(md = EVP_sha512());
  31328. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  31329. #endif
  31330. #endif
  31331. return EXPECT_RESULT();
  31332. }
  31333. #ifdef OPENSSL_EXTRA
  31334. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  31335. size_t testKeySz, const char* testData, size_t testDataSz,
  31336. const byte* testResult, size_t testResultSz)
  31337. {
  31338. EXPECT_DECLS;
  31339. unsigned char check[WC_MAX_DIGEST_SIZE];
  31340. size_t checkSz = -1;
  31341. WOLFSSL_EVP_PKEY* key = NULL;
  31342. WOLFSSL_EVP_MD_CTX mdCtx;
  31343. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  31344. testKey, (int)testKeySz));
  31345. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31346. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31347. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31348. (unsigned int)testDataSz), 1);
  31349. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31350. ExpectIntEQ((int)checkSz, (int)testResultSz);
  31351. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31352. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31353. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31354. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31355. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31356. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31357. (unsigned int)testDataSz), 1);
  31358. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31359. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31360. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31361. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31362. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31363. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31364. ExpectIntEQ((int)checkSz, (int)testResultSz);
  31365. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31366. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31367. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31368. (unsigned int)testDataSz - 4), 1);
  31369. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31370. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31371. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31372. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31373. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31374. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31375. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31376. (unsigned int)testDataSz - 4), 1);
  31377. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31378. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31379. wolfSSL_EVP_PKEY_free(key);
  31380. return EXPECT_RESULT();
  31381. }
  31382. #endif
  31383. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  31384. {
  31385. EXPECT_DECLS;
  31386. #ifdef OPENSSL_EXTRA
  31387. static const unsigned char testKey[] =
  31388. {
  31389. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31390. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31391. 0x0b, 0x0b, 0x0b, 0x0b
  31392. };
  31393. static const char testData[] = "Hi There";
  31394. #ifdef WOLFSSL_SHA224
  31395. static const unsigned char testResultSha224[] =
  31396. {
  31397. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  31398. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  31399. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  31400. 0x53, 0x68, 0x4b, 0x22
  31401. };
  31402. #endif
  31403. #ifndef NO_SHA256
  31404. static const unsigned char testResultSha256[] =
  31405. {
  31406. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  31407. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  31408. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  31409. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  31410. };
  31411. #endif
  31412. #ifdef WOLFSSL_SHA384
  31413. static const unsigned char testResultSha384[] =
  31414. {
  31415. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  31416. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  31417. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  31418. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  31419. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  31420. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  31421. };
  31422. #endif
  31423. #ifdef WOLFSSL_SHA512
  31424. static const unsigned char testResultSha512[] =
  31425. {
  31426. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  31427. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  31428. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  31429. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  31430. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  31431. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  31432. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  31433. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  31434. };
  31435. #endif
  31436. #ifdef WOLFSSL_SHA3
  31437. #ifndef WOLFSSL_NOSHA3_224
  31438. static const unsigned char testResultSha3_224[] =
  31439. {
  31440. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  31441. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  31442. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  31443. 0xf3, 0xc8, 0x60, 0xf7
  31444. };
  31445. #endif
  31446. #ifndef WOLFSSL_NOSHA3_256
  31447. static const unsigned char testResultSha3_256[] =
  31448. {
  31449. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  31450. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  31451. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  31452. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  31453. };
  31454. #endif
  31455. #ifndef WOLFSSL_NOSHA3_384
  31456. static const unsigned char testResultSha3_384[] =
  31457. {
  31458. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  31459. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  31460. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  31461. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  31462. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  31463. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  31464. };
  31465. #endif
  31466. #ifndef WOLFSSL_NOSHA3_512
  31467. static const unsigned char testResultSha3_512[] =
  31468. {
  31469. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  31470. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  31471. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  31472. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  31473. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  31474. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  31475. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  31476. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  31477. };
  31478. #endif
  31479. #endif
  31480. #ifndef NO_SHA256
  31481. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  31482. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  31483. sizeof(testResultSha256)), TEST_SUCCESS);
  31484. #endif
  31485. #ifdef WOLFSSL_SHA224
  31486. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  31487. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  31488. sizeof(testResultSha224)), TEST_SUCCESS);
  31489. #endif
  31490. #ifdef WOLFSSL_SHA384
  31491. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  31492. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  31493. sizeof(testResultSha384)), TEST_SUCCESS);
  31494. #endif
  31495. #ifdef WOLFSSL_SHA512
  31496. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  31497. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  31498. sizeof(testResultSha512)), TEST_SUCCESS);
  31499. #endif
  31500. #ifdef WOLFSSL_SHA3
  31501. #ifndef WOLFSSL_NOSHA3_224
  31502. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  31503. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  31504. sizeof(testResultSha3_224)), TEST_SUCCESS);
  31505. #endif
  31506. #ifndef WOLFSSL_NOSHA3_256
  31507. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  31508. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  31509. sizeof(testResultSha3_256)), TEST_SUCCESS);
  31510. #endif
  31511. #ifndef WOLFSSL_NOSHA3_384
  31512. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  31513. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  31514. sizeof(testResultSha3_384)), TEST_SUCCESS);
  31515. #endif
  31516. #ifndef WOLFSSL_NOSHA3_512
  31517. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  31518. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  31519. sizeof(testResultSha3_512)), TEST_SUCCESS);
  31520. #endif
  31521. #endif
  31522. #endif /* OPENSSL_EXTRA */
  31523. return EXPECT_RESULT();
  31524. }
  31525. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  31526. {
  31527. EXPECT_DECLS;
  31528. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  31529. WOLFSSL_EVP_PKEY* privKey = NULL;
  31530. WOLFSSL_EVP_PKEY* pubKey = NULL;
  31531. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  31532. const char testData[] = "Hi There";
  31533. WOLFSSL_EVP_MD_CTX mdCtx;
  31534. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  31535. int ret;
  31536. size_t checkSz = -1;
  31537. int sz = 2048 / 8;
  31538. const unsigned char* cp;
  31539. const unsigned char* p;
  31540. unsigned char check[2048/8];
  31541. size_t i;
  31542. int paddings[] = {
  31543. RSA_PKCS1_PADDING,
  31544. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  31545. RSA_PKCS1_PSS_PADDING,
  31546. #endif
  31547. };
  31548. cp = client_key_der_2048;
  31549. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  31550. sizeof_client_key_der_2048)));
  31551. p = client_keypub_der_2048;
  31552. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  31553. sizeof_client_keypub_der_2048)));
  31554. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31555. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  31556. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31557. NULL, privKey), 1);
  31558. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31559. (unsigned int)XSTRLEN(testData)), 1);
  31560. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31561. ExpectIntEQ((int)checkSz, sz);
  31562. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31563. ExpectIntEQ((int)checkSz,sz);
  31564. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  31565. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  31566. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  31567. ExpectIntEQ(ret, 1);
  31568. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31569. ExpectIntEQ(ret, 1);
  31570. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31571. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31572. NULL, pubKey), 1);
  31573. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31574. (unsigned int)XSTRLEN(testData)),
  31575. 1);
  31576. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31577. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31578. ExpectIntEQ(ret, 1);
  31579. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31580. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31581. NULL, privKey), 1);
  31582. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31583. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31584. ExpectIntEQ((int)checkSz, sz);
  31585. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31586. ExpectIntEQ((int)checkSz, sz);
  31587. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31588. (unsigned int)XSTRLEN(testData) - 4), 1);
  31589. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31590. ExpectIntEQ((int)checkSz, sz);
  31591. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31592. ExpectIntEQ(ret, 1);
  31593. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31594. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31595. NULL, pubKey), 1);
  31596. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31597. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31598. (unsigned int)XSTRLEN(testData) - 4),
  31599. 1);
  31600. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31601. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31602. ExpectIntEQ(ret, 1);
  31603. /* Check all signing padding types */
  31604. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  31605. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31606. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  31607. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  31608. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  31609. paddings[i]), 1);
  31610. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31611. (unsigned int)XSTRLEN(testData)), 1);
  31612. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31613. ExpectIntEQ((int)checkSz, sz);
  31614. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31615. ExpectIntEQ((int)checkSz,sz);
  31616. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31617. ExpectIntEQ(ret, 1);
  31618. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31619. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  31620. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  31621. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  31622. paddings[i]), 1);
  31623. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31624. (unsigned int)XSTRLEN(testData)), 1);
  31625. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31626. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31627. ExpectIntEQ(ret, 1);
  31628. }
  31629. wolfSSL_EVP_PKEY_free(pubKey);
  31630. wolfSSL_EVP_PKEY_free(privKey);
  31631. #endif
  31632. return EXPECT_RESULT();
  31633. }
  31634. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  31635. {
  31636. EXPECT_DECLS;
  31637. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  31638. WOLFSSL_EVP_PKEY* privKey = NULL;
  31639. WOLFSSL_EVP_PKEY* pubKey = NULL;
  31640. const char testData[] = "Hi There";
  31641. WOLFSSL_EVP_MD_CTX mdCtx;
  31642. int ret;
  31643. size_t checkSz = -1;
  31644. const unsigned char* cp;
  31645. const unsigned char* p;
  31646. unsigned char check[2048/8];
  31647. cp = ecc_clikey_der_256;
  31648. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  31649. sizeof_ecc_clikey_der_256));
  31650. p = ecc_clikeypub_der_256;
  31651. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  31652. sizeof_ecc_clikeypub_der_256)));
  31653. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31654. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31655. NULL, privKey), 1);
  31656. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31657. (unsigned int)XSTRLEN(testData)), 1);
  31658. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31659. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31660. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31661. ExpectIntEQ(ret, 1);
  31662. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31663. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31664. NULL, pubKey), 1);
  31665. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31666. (unsigned int)XSTRLEN(testData)),
  31667. 1);
  31668. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31669. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31670. ExpectIntEQ(ret, 1);
  31671. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31672. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31673. NULL, privKey), 1);
  31674. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31675. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31676. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31677. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31678. (unsigned int)XSTRLEN(testData) - 4), 1);
  31679. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31680. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31681. ExpectIntEQ(ret, 1);
  31682. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31683. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  31684. NULL, pubKey), 1);
  31685. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31686. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31687. (unsigned int)XSTRLEN(testData) - 4),
  31688. 1);
  31689. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  31690. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  31691. ExpectIntEQ(ret, 1);
  31692. wolfSSL_EVP_PKEY_free(pubKey);
  31693. wolfSSL_EVP_PKEY_free(privKey);
  31694. #endif
  31695. return EXPECT_RESULT();
  31696. }
  31697. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  31698. {
  31699. EXPECT_DECLS;
  31700. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31701. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  31702. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31703. char caFile[] = "./certs/client-ca.pem";
  31704. char clientFile[] = "./certs/client-cert.pem";
  31705. SSL_CTX* ctx = NULL;
  31706. X509* x509 = NULL;
  31707. BIO *bio = NULL;
  31708. X509 *cert = NULL;
  31709. X509 *ca = NULL;
  31710. STACK_OF(X509) *chain = NULL;
  31711. STACK_OF(X509) *chain2 = NULL;
  31712. #ifndef NO_WOLFSSL_SERVER
  31713. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31714. #else
  31715. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31716. #endif
  31717. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  31718. WOLFSSL_FILETYPE_PEM));
  31719. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  31720. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  31721. WOLFSSL_FILETYPE_PEM));
  31722. /* additional test of getting EVP_PKEY key size from X509
  31723. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  31724. * allowed with user RSA */
  31725. {
  31726. EVP_PKEY* pkey = NULL;
  31727. #if defined(HAVE_ECC)
  31728. X509* ecX509 = NULL;
  31729. #endif /* HAVE_ECC */
  31730. ExpectNotNull(pkey = X509_get_pubkey(x509));
  31731. /* current RSA key is 2048 bit (256 bytes) */
  31732. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  31733. EVP_PKEY_free(pkey);
  31734. pkey = NULL;
  31735. #if defined(HAVE_ECC)
  31736. #if defined(USE_CERT_BUFFERS_256)
  31737. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  31738. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  31739. SSL_FILETYPE_ASN1));
  31740. #else
  31741. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  31742. cliEccCertFile, SSL_FILETYPE_PEM));
  31743. #endif
  31744. pkey = X509_get_pubkey(ecX509);
  31745. ExpectNotNull(pkey);
  31746. /* current ECC key is 256 bit (32 bytes) */
  31747. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  31748. X509_free(ecX509);
  31749. ecX509 = NULL;
  31750. EVP_PKEY_free(pkey);
  31751. pkey = NULL;
  31752. #endif /* HAVE_ECC */
  31753. }
  31754. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  31755. if (EXPECT_SUCCESS()) {
  31756. x509 = NULL;
  31757. }
  31758. #ifdef WOLFSSL_ENCRYPTED_KEYS
  31759. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  31760. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31761. #endif
  31762. SSL_CTX_free(ctx);
  31763. ctx = NULL;
  31764. #ifndef NO_WOLFSSL_SERVER
  31765. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31766. #else
  31767. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31768. #endif
  31769. /* Test haproxy use case */
  31770. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  31771. /* Read Certificate */
  31772. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  31773. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  31774. ExpectNotNull(chain = sk_X509_new_null());
  31775. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  31776. if (EXPECT_SUCCESS()) {
  31777. ca = NULL;
  31778. }
  31779. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  31780. ExpectNotNull(ca = sk_X509_shift(chain2));
  31781. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  31782. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  31783. if (EXPECT_SUCCESS()) {
  31784. ca = NULL;
  31785. }
  31786. BIO_free(bio);
  31787. X509_free(cert);
  31788. X509_free(ca);
  31789. X509_free(x509);
  31790. sk_X509_pop_free(chain, X509_free);
  31791. sk_X509_pop_free(chain2, X509_free);
  31792. SSL_CTX_free(ctx);
  31793. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31794. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31795. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  31796. return EXPECT_RESULT();
  31797. }
  31798. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  31799. static int test_wolfSSL_ERR_peek_last_error_line(void)
  31800. {
  31801. EXPECT_DECLS;
  31802. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31803. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31804. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  31805. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  31806. callback_functions client_cb;
  31807. callback_functions server_cb;
  31808. int line = 0;
  31809. int flag = ERR_TXT_STRING;
  31810. const char* file = NULL;
  31811. const char* data = NULL;
  31812. /* create a failed connection and inspect the error */
  31813. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31814. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31815. client_cb.method = wolfTLSv1_1_client_method;
  31816. server_cb.method = wolfTLSv1_2_server_method;
  31817. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  31818. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  31819. ExpectNotNull(data);
  31820. /* check clearing error state */
  31821. ERR_remove_state(0);
  31822. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  31823. ERR_peek_last_error_line(NULL, &line);
  31824. ExpectIntEQ(line, 0);
  31825. ERR_peek_last_error_line(&file, NULL);
  31826. ExpectNull(file);
  31827. /* retry connection to fill error queue */
  31828. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31829. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31830. client_cb.method = wolfTLSv1_1_client_method;
  31831. server_cb.method = wolfTLSv1_2_server_method;
  31832. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  31833. /* check that error code was stored */
  31834. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  31835. ERR_peek_last_error_line(NULL, &line);
  31836. ExpectIntNE(line, 0);
  31837. ERR_peek_last_error_line(&file, NULL);
  31838. ExpectNotNull(file);
  31839. fprintf(stderr, "\nTesting error print out\n");
  31840. ERR_print_errors_fp(stderr);
  31841. fprintf(stderr, "Done testing print out\n\n");
  31842. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  31843. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  31844. return EXPECT_RESULT();
  31845. }
  31846. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  31847. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31848. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31849. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  31850. {
  31851. (void) ok;
  31852. (void) ctx;
  31853. fprintf(stderr, "ENTER verify_cb\n");
  31854. return SSL_SUCCESS;
  31855. }
  31856. #endif
  31857. static int test_wolfSSL_X509_Name_canon(void)
  31858. {
  31859. EXPECT_DECLS;
  31860. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31861. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  31862. defined(WOLFSSL_CERT_GEN) && \
  31863. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  31864. const long ex_hash1 = 0x0fdb2da4;
  31865. const long ex_hash2 = 0x9f3e8c9e;
  31866. X509_NAME *name = NULL;
  31867. X509 *x509 = NULL;
  31868. XFILE file = XBADFILE;
  31869. unsigned long hash = 0;
  31870. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  31871. byte *pbuf = NULL;
  31872. word32 len = 0;
  31873. (void) ex_hash2;
  31874. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  31875. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  31876. ExpectNotNull(name = X509_get_issuer_name(x509));
  31877. /* When output buffer is NULL, should return necessary output buffer
  31878. * length.*/
  31879. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  31880. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  31881. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  31882. hash = (((unsigned long)digest[3] << 24) |
  31883. ((unsigned long)digest[2] << 16) |
  31884. ((unsigned long)digest[1] << 8) |
  31885. ((unsigned long)digest[0]));
  31886. ExpectIntEQ(hash, ex_hash1);
  31887. if (file != XBADFILE) {
  31888. XFCLOSE(file);
  31889. file = XBADFILE;
  31890. }
  31891. X509_free(x509);
  31892. x509 = NULL;
  31893. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  31894. pbuf = NULL;
  31895. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  31896. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  31897. ExpectNotNull(name = X509_get_issuer_name(x509));
  31898. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  31899. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  31900. hash = (((unsigned long)digest[3] << 24) |
  31901. ((unsigned long)digest[2] << 16) |
  31902. ((unsigned long)digest[1] << 8) |
  31903. ((unsigned long)digest[0]));
  31904. ExpectIntEQ(hash, ex_hash2);
  31905. if (file != XBADFILE)
  31906. XFCLOSE(file);
  31907. X509_free(x509);
  31908. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  31909. #endif
  31910. return EXPECT_RESULT();
  31911. }
  31912. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  31913. {
  31914. EXPECT_DECLS;
  31915. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  31916. const int MAX_DIR = 4;
  31917. const char paths[][32] = {
  31918. "./certs/ed25519",
  31919. "./certs/ecc",
  31920. "./certs/crl",
  31921. "./certs/",
  31922. };
  31923. char CertCrl_path[MAX_FILENAME_SZ];
  31924. char *p;
  31925. X509_STORE* str = NULL;
  31926. X509_LOOKUP* lookup = NULL;
  31927. WOLFSSL_STACK* sk = NULL;
  31928. int len, total_len, i;
  31929. (void)sk;
  31930. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  31931. /* illegal string */
  31932. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31933. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31934. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  31935. SSL_FILETYPE_PEM,NULL), 0);
  31936. /* free store */
  31937. X509_STORE_free(str);
  31938. str = NULL;
  31939. /* short folder string */
  31940. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31941. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31942. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  31943. SSL_FILETYPE_PEM,NULL), 1);
  31944. #if defined(WOLFSSL_INT_H)
  31945. /* only available when including internal.h */
  31946. ExpectNotNull(sk = lookup->dirs->dir_entry);
  31947. #endif
  31948. /* free store */
  31949. X509_STORE_free(str);
  31950. str = NULL;
  31951. /* typical function check */
  31952. p = &CertCrl_path[0];
  31953. total_len = 0;
  31954. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  31955. len = (int)XSTRLEN((const char*)&paths[i]);
  31956. total_len += len;
  31957. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  31958. p += len;
  31959. if (i != 0) *(p++) = SEPARATOR_CHAR;
  31960. }
  31961. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31962. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31963. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  31964. SSL_FILETYPE_PEM,NULL), 1);
  31965. #if defined(WOLFSSL_INT_H)
  31966. /* only available when including internal.h */
  31967. ExpectNotNull(sk = lookup->dirs->dir_entry);
  31968. #endif
  31969. X509_STORE_free(str);
  31970. #endif
  31971. return EXPECT_RESULT();
  31972. }
  31973. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  31974. {
  31975. EXPECT_DECLS;
  31976. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31977. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31978. defined(WOLFSSL_SIGNER_DER_CERT)
  31979. X509_STORE_CTX* ctx = NULL;
  31980. X509_STORE* str = NULL;
  31981. X509_LOOKUP* lookup = NULL;
  31982. X509* cert1 = NULL;
  31983. X509* x509Ca = NULL;
  31984. X509* x509Svr = NULL;
  31985. X509* issuer = NULL;
  31986. WOLFSSL_STACK* sk = NULL;
  31987. X509_NAME* caName = NULL;
  31988. X509_NAME* issuerName = NULL;
  31989. XFILE file1 = XBADFILE;
  31990. int i;
  31991. int cert_count = 0;
  31992. int cmp;
  31993. char der[] = "certs/ca-cert.der";
  31994. #ifdef HAVE_CRL
  31995. char pem[][100] = {
  31996. "./certs/crl/crl.pem",
  31997. "./certs/crl/crl2.pem",
  31998. "./certs/crl/caEccCrl.pem",
  31999. "./certs/crl/eccCliCRL.pem",
  32000. "./certs/crl/eccSrvCRL.pem",
  32001. ""
  32002. };
  32003. #endif
  32004. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  32005. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32006. if (file1 != XBADFILE)
  32007. XFCLOSE(file1);
  32008. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32009. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32010. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32011. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32012. SSL_FILETYPE_PEM,NULL), 1);
  32013. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32014. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32015. /* check if CA cert is loaded into the store */
  32016. for (i = 0; i < cert_count; i++) {
  32017. x509Ca = sk_X509_value(sk, i);
  32018. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32019. }
  32020. ExpectNotNull((x509Svr =
  32021. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32022. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32023. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32024. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32025. ExpectNotNull(issuer);
  32026. caName = X509_get_subject_name(x509Ca);
  32027. ExpectNotNull(caName);
  32028. issuerName = X509_get_subject_name(issuer);
  32029. ExpectNotNull(issuerName);
  32030. cmp = X509_NAME_cmp(caName, issuerName);
  32031. ExpectIntEQ(cmp, 0);
  32032. /* load der format */
  32033. X509_free(issuer);
  32034. issuer = NULL;
  32035. X509_STORE_CTX_free(ctx);
  32036. ctx = NULL;
  32037. X509_STORE_free(str);
  32038. str = NULL;
  32039. sk_X509_pop_free(sk, NULL);
  32040. sk = NULL;
  32041. X509_free(x509Svr);
  32042. x509Svr = NULL;
  32043. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32044. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32045. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32046. SSL_FILETYPE_ASN1,NULL), 1);
  32047. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32048. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32049. /* check if CA cert is loaded into the store */
  32050. for (i = 0; i < cert_count; i++) {
  32051. x509Ca = sk_X509_value(sk, i);
  32052. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32053. }
  32054. X509_STORE_free(str);
  32055. str = NULL;
  32056. sk_X509_pop_free(sk, NULL);
  32057. sk = NULL;
  32058. X509_free(cert1);
  32059. cert1 = NULL;
  32060. #ifdef HAVE_CRL
  32061. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32062. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32063. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32064. SSL_FILETYPE_PEM,NULL), 1);
  32065. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32066. "certs/server-revoked-cert.pem",
  32067. SSL_FILETYPE_PEM,NULL), 1);
  32068. if (str) {
  32069. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32070. WOLFSSL_FILETYPE_PEM), 1);
  32071. /* since store hasn't yet known the revoked cert*/
  32072. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32073. "certs/server-revoked-cert.pem",
  32074. WOLFSSL_FILETYPE_PEM), 1);
  32075. }
  32076. for (i = 0; pem[i][0] != '\0'; i++)
  32077. {
  32078. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32079. SSL_FILETYPE_PEM, NULL), 1);
  32080. }
  32081. if (str) {
  32082. /* since store knows crl list */
  32083. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32084. "certs/server-revoked-cert.pem",
  32085. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32086. }
  32087. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32088. X509_STORE_free(str);
  32089. #endif
  32090. #endif
  32091. return EXPECT_RESULT();
  32092. }
  32093. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32094. {
  32095. int res = TEST_SKIPPED;
  32096. #if defined(OPENSSL_EXTRA)
  32097. X509_STORE_CTX_cleanup(NULL);
  32098. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32099. res = TEST_SUCCESS;
  32100. #endif
  32101. return res;
  32102. }
  32103. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32104. {
  32105. EXPECT_DECLS;
  32106. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32107. X509_STORE_CTX* ctx = NULL;
  32108. X509_STORE* str = NULL;
  32109. X509* x509Ca = NULL;
  32110. X509* x509Svr = NULL;
  32111. X509* issuer = NULL;
  32112. X509_NAME* caName = NULL;
  32113. X509_NAME* issuerName = NULL;
  32114. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32115. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32116. ExpectNotNull((x509Ca =
  32117. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32118. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32119. ExpectNotNull((x509Svr =
  32120. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32121. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32122. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32123. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32124. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32125. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32126. #ifdef WOLFSSL_SIGNER_DER_CERT
  32127. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32128. #endif
  32129. X509_free(issuer);
  32130. X509_STORE_CTX_free(ctx);
  32131. X509_free(x509Svr);
  32132. X509_STORE_free(str);
  32133. X509_free(x509Ca);
  32134. #endif
  32135. return EXPECT_RESULT();
  32136. }
  32137. static int test_wolfSSL_PKCS7_certs(void)
  32138. {
  32139. EXPECT_DECLS;
  32140. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32141. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32142. STACK_OF(X509)* sk = NULL;
  32143. STACK_OF(X509_INFO)* info_sk = NULL;
  32144. PKCS7 *p7 = NULL;
  32145. BIO* bio = NULL;
  32146. const byte* p = NULL;
  32147. int buflen = 0;
  32148. int i;
  32149. /* Test twice. Once with d2i and once without to test
  32150. * that everything is free'd correctly. */
  32151. for (i = 0; i < 2; i++) {
  32152. ExpectNotNull(p7 = PKCS7_new());
  32153. if (p7 != NULL) {
  32154. p7->version = 1;
  32155. #ifdef NO_SHA
  32156. p7->hashOID = SHA256h;
  32157. #else
  32158. p7->hashOID = SHAh;
  32159. #endif
  32160. }
  32161. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32162. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32163. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32164. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32165. ExpectNotNull(sk = sk_X509_new_null());
  32166. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  32167. X509_INFO* info = NULL;
  32168. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  32169. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  32170. if (EXPECT_SUCCESS() && (info != NULL)) {
  32171. info->x509 = NULL;
  32172. }
  32173. X509_INFO_free(info);
  32174. }
  32175. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  32176. info_sk = NULL;
  32177. BIO_free(bio);
  32178. bio = NULL;
  32179. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32180. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32181. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  32182. sk_X509_pop_free(sk, X509_free);
  32183. }
  32184. sk = NULL;
  32185. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32186. if (i == 0) {
  32187. PKCS7_free(p7);
  32188. p7 = NULL;
  32189. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  32190. if (p7 != NULL) {
  32191. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  32192. * them */
  32193. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32194. }
  32195. /* PKCS7_free free's the certs */
  32196. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  32197. }
  32198. BIO_free(bio);
  32199. bio = NULL;
  32200. PKCS7_free(p7);
  32201. p7 = NULL;
  32202. }
  32203. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32204. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32205. return EXPECT_RESULT();
  32206. }
  32207. static int test_wolfSSL_X509_STORE_CTX(void)
  32208. {
  32209. EXPECT_DECLS;
  32210. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32211. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32212. X509_STORE_CTX* ctx = NULL;
  32213. X509_STORE* str = NULL;
  32214. X509* x509 = NULL;
  32215. #ifdef OPENSSL_ALL
  32216. X509* x5092 = NULL;
  32217. STACK_OF(X509) *sk = NULL;
  32218. STACK_OF(X509) *sk2 = NULL;
  32219. STACK_OF(X509) *sk3 = NULL;
  32220. #endif
  32221. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32222. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32223. ExpectNotNull((x509 =
  32224. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32225. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32226. #ifdef OPENSSL_ALL
  32227. /* sk_X509_new only in OPENSSL_ALL */
  32228. sk = sk_X509_new_null();
  32229. ExpectNotNull(sk);
  32230. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32231. #else
  32232. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32233. #endif
  32234. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32235. X509_STORE_CTX_set_error(ctx, -5);
  32236. X509_STORE_CTX_set_error(NULL, -5);
  32237. X509_STORE_CTX_free(ctx);
  32238. ctx = NULL;
  32239. #ifdef OPENSSL_ALL
  32240. sk_X509_pop_free(sk, NULL);
  32241. sk = NULL;
  32242. #endif
  32243. X509_STORE_free(str);
  32244. str = NULL;
  32245. X509_free(x509);
  32246. x509 = NULL;
  32247. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32248. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32249. X509_STORE_CTX_free(ctx);
  32250. ctx = NULL;
  32251. #ifdef OPENSSL_ALL
  32252. /* test X509_STORE_CTX_get(1)_chain */
  32253. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32254. SSL_FILETYPE_PEM)));
  32255. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32256. SSL_FILETYPE_PEM)));
  32257. ExpectNotNull((sk = sk_X509_new_null()));
  32258. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  32259. if (EXPECT_FAIL()) {
  32260. X509_free(x509);
  32261. x509 = NULL;
  32262. }
  32263. ExpectNotNull((str = X509_STORE_new()));
  32264. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  32265. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32266. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32267. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32268. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  32269. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  32270. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  32271. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  32272. X509_STORE_CTX_free(ctx);
  32273. ctx = NULL;
  32274. X509_STORE_free(str);
  32275. str = NULL;
  32276. /* CTX certs not freed yet */
  32277. X509_free(x5092);
  32278. x5092 = NULL;
  32279. sk_X509_pop_free(sk, NULL);
  32280. sk = NULL;
  32281. /* sk3 is dup so free here */
  32282. sk_X509_pop_free(sk3, NULL);
  32283. sk3 = NULL;
  32284. #endif
  32285. /* test X509_STORE_CTX_get/set_ex_data */
  32286. {
  32287. int i = 0, tmpData = 5;
  32288. void* tmpDataRet;
  32289. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32290. #ifdef HAVE_EX_DATA
  32291. for (i = 0; i < MAX_EX_DATA; i++) {
  32292. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32293. WOLFSSL_SUCCESS);
  32294. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32295. ExpectNotNull(tmpDataRet);
  32296. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32297. }
  32298. #else
  32299. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32300. WOLFSSL_FAILURE);
  32301. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32302. ExpectNull(tmpDataRet);
  32303. #endif
  32304. X509_STORE_CTX_free(ctx);
  32305. ctx = NULL;
  32306. }
  32307. /* test X509_STORE_get/set_ex_data */
  32308. {
  32309. int i = 0, tmpData = 99;
  32310. void* tmpDataRet;
  32311. ExpectNotNull(str = X509_STORE_new());
  32312. #ifdef HAVE_EX_DATA
  32313. for (i = 0; i < MAX_EX_DATA; i++) {
  32314. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32315. WOLFSSL_SUCCESS);
  32316. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32317. ExpectNotNull(tmpDataRet);
  32318. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32319. }
  32320. #else
  32321. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32322. WOLFSSL_FAILURE);
  32323. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32324. ExpectNull(tmpDataRet);
  32325. #endif
  32326. X509_STORE_free(str);
  32327. str = NULL;
  32328. }
  32329. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32330. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32331. return EXPECT_RESULT();
  32332. }
  32333. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32334. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  32335. STACK_OF(X509)* chain)
  32336. {
  32337. EXPECT_DECLS;
  32338. XFILE fp = XBADFILE;
  32339. X509* cert = NULL;
  32340. ExpectTrue((fp = XFOPEN(filename, "rb"))
  32341. != XBADFILE);
  32342. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  32343. if (fp != XBADFILE) {
  32344. XFCLOSE(fp);
  32345. fp = XBADFILE;
  32346. }
  32347. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  32348. if (EXPECT_FAIL())
  32349. X509_free(cert);
  32350. return EXPECT_RESULT();
  32351. }
  32352. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  32353. int err, int loadCA)
  32354. {
  32355. EXPECT_DECLS;
  32356. X509_STORE_CTX* ctx = NULL;
  32357. X509_STORE* str = NULL;
  32358. XFILE fp = XBADFILE;
  32359. X509* cert = NULL;
  32360. STACK_OF(X509)* untrusted = NULL;
  32361. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  32362. != XBADFILE);
  32363. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  32364. if (fp != XBADFILE) {
  32365. XFCLOSE(fp);
  32366. fp = XBADFILE;
  32367. }
  32368. ExpectNotNull(str = X509_STORE_new());
  32369. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32370. ExpectNotNull(untrusted = sk_X509_new_null());
  32371. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  32372. if (loadCA) {
  32373. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  32374. 1);
  32375. }
  32376. for (; *filenames; filenames++) {
  32377. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  32378. untrusted), TEST_SUCCESS);
  32379. }
  32380. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  32381. ExpectIntEQ(X509_verify_cert(ctx), ret);
  32382. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  32383. X509_free(cert);
  32384. X509_STORE_free(str);
  32385. X509_STORE_CTX_free(ctx);
  32386. sk_X509_pop_free(untrusted, NULL);
  32387. return EXPECT_RESULT();
  32388. }
  32389. #endif
  32390. static int test_X509_STORE_untrusted(void)
  32391. {
  32392. EXPECT_DECLS;
  32393. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32394. const char* untrusted1[] = {
  32395. "./certs/intermediate/ca-int2-cert.pem",
  32396. NULL
  32397. };
  32398. const char* untrusted2[] = {
  32399. "./certs/intermediate/ca-int-cert.pem",
  32400. "./certs/intermediate/ca-int2-cert.pem",
  32401. NULL
  32402. };
  32403. const char* untrusted3[] = {
  32404. "./certs/intermediate/ca-int-cert.pem",
  32405. "./certs/intermediate/ca-int2-cert.pem",
  32406. "./certs/ca-cert.pem",
  32407. NULL
  32408. };
  32409. /* Adding unrelated certs that should be ignored */
  32410. const char* untrusted4[] = {
  32411. "./certs/client-ca.pem",
  32412. "./certs/intermediate/ca-int-cert.pem",
  32413. "./certs/server-cert.pem",
  32414. "./certs/intermediate/ca-int2-cert.pem",
  32415. NULL
  32416. };
  32417. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  32418. * to loaded CA. */
  32419. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  32420. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  32421. /* Succeeds because path to loaded CA is available. */
  32422. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  32423. TEST_SUCCESS);
  32424. /* Fails because root CA is in the untrusted stack */
  32425. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  32426. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  32427. /* Succeeds because path to loaded CA is available. */
  32428. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  32429. TEST_SUCCESS);
  32430. #endif
  32431. return EXPECT_RESULT();
  32432. }
  32433. static int test_wolfSSL_X509_STORE_set_flags(void)
  32434. {
  32435. EXPECT_DECLS;
  32436. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32437. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32438. X509_STORE* store = NULL;
  32439. X509* x509 = NULL;
  32440. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  32441. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  32442. WOLFSSL_FILETYPE_PEM)));
  32443. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  32444. #ifdef HAVE_CRL
  32445. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  32446. WOLFSSL_SUCCESS);
  32447. #else
  32448. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  32449. NOT_COMPILED_IN);
  32450. #endif
  32451. wolfSSL_X509_free(x509);
  32452. wolfSSL_X509_STORE_free(store);
  32453. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32454. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32455. return EXPECT_RESULT();
  32456. }
  32457. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  32458. {
  32459. EXPECT_DECLS;
  32460. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  32461. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32462. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  32463. WOLFSSL_X509_STORE* store = NULL;
  32464. WOLFSSL_X509_LOOKUP* lookup = NULL;
  32465. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  32466. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  32467. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  32468. X509_FILETYPE_PEM), 1);
  32469. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  32470. X509_FILETYPE_PEM), 1);
  32471. if (store != NULL) {
  32472. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  32473. WOLFSSL_FILETYPE_PEM), 1);
  32474. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  32475. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  32476. }
  32477. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  32478. X509_FILETYPE_PEM), 1);
  32479. if (store != NULL) {
  32480. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  32481. WOLFSSL_FILETYPE_PEM), 1);
  32482. }
  32483. wolfSSL_X509_STORE_free(store);
  32484. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  32485. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32486. return EXPECT_RESULT();
  32487. }
  32488. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  32489. {
  32490. EXPECT_DECLS;
  32491. #if defined(OPENSSL_EXTRA)
  32492. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  32493. time_t c_time;
  32494. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  32495. c_time = 365*24*60*60;
  32496. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  32497. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  32498. WOLFSSL_USE_CHECK_TIME);
  32499. ExpectTrue(ctx->param->check_time == c_time);
  32500. wolfSSL_X509_STORE_CTX_free(ctx);
  32501. #endif /* OPENSSL_EXTRA */
  32502. return EXPECT_RESULT();
  32503. }
  32504. static int test_wolfSSL_CTX_get0_set1_param(void)
  32505. {
  32506. EXPECT_DECLS;
  32507. #if defined(OPENSSL_EXTRA)
  32508. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32509. SSL_CTX* ctx = NULL;
  32510. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  32511. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  32512. char testIPv4[] = "127.0.0.1";
  32513. char testhostName[] = "foo.hoge.com";
  32514. #ifndef NO_WOLFSSL_SERVER
  32515. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32516. #else
  32517. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32518. #endif
  32519. ExpectNull(SSL_CTX_get0_param(NULL));
  32520. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  32521. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  32522. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  32523. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  32524. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  32525. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  32526. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  32527. WOLFSSL_SUCCESS);
  32528. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  32529. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  32530. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  32531. (int)XSTRLEN(testhostName)));
  32532. ExpectIntEQ(0x01, pParam->hostFlags);
  32533. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  32534. /* test for incorrect parameter */
  32535. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  32536. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  32537. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  32538. SSL_CTX_free(ctx);
  32539. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  32540. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32541. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  32542. return EXPECT_RESULT();
  32543. }
  32544. static int test_wolfSSL_get0_param(void)
  32545. {
  32546. EXPECT_DECLS;
  32547. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32548. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32549. SSL_CTX* ctx = NULL;
  32550. SSL* ssl = NULL;
  32551. #ifndef NO_WOLFSSL_SERVER
  32552. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32553. #else
  32554. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32555. #endif
  32556. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32557. SSL_FILETYPE_PEM));
  32558. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32559. ExpectNotNull(ssl = SSL_new(ctx));
  32560. ExpectNotNull(SSL_get0_param(ssl));
  32561. SSL_free(ssl);
  32562. SSL_CTX_free(ctx);
  32563. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32564. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  32565. return EXPECT_RESULT();
  32566. }
  32567. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  32568. {
  32569. EXPECT_DECLS;
  32570. #if defined(OPENSSL_EXTRA)
  32571. const char host[] = "www.example.com";
  32572. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  32573. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  32574. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32575. if (pParam != NULL) {
  32576. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  32577. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  32578. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32579. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  32580. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32581. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32582. }
  32583. #endif /* OPENSSL_EXTRA */
  32584. return EXPECT_RESULT();
  32585. }
  32586. static int test_wolfSSL_set1_host(void)
  32587. {
  32588. EXPECT_DECLS;
  32589. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32590. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32591. const char host[] = "www.test_wolfSSL_set1_host.com";
  32592. const char emptyStr[] = "";
  32593. SSL_CTX* ctx = NULL;
  32594. SSL* ssl = NULL;
  32595. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  32596. #ifndef NO_WOLFSSL_SERVER
  32597. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32598. #else
  32599. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32600. #endif
  32601. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32602. SSL_FILETYPE_PEM));
  32603. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32604. ExpectNotNull(ssl = SSL_new(ctx));
  32605. pParam = SSL_get0_param(ssl);
  32606. /* we should get back host string */
  32607. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  32608. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32609. /* we should get back empty string */
  32610. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  32611. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  32612. /* we should get back host string */
  32613. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  32614. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  32615. /* we should get back empty string */
  32616. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  32617. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  32618. SSL_free(ssl);
  32619. SSL_CTX_free(ctx);
  32620. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32621. #endif /* OPENSSL_EXTRA */
  32622. return EXPECT_RESULT();
  32623. }
  32624. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  32625. {
  32626. EXPECT_DECLS;
  32627. #if defined(OPENSSL_EXTRA)
  32628. unsigned char buf[16] = {0};
  32629. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  32630. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  32631. /* test 127.0.0.1 */
  32632. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  32633. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  32634. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  32635. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  32636. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32637. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  32638. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  32639. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  32640. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32641. ExpectIntEQ(XSTRNCMP(param->ipasc,
  32642. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  32643. /* test 2001:db8:: */
  32644. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32645. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32646. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32647. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  32648. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32649. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  32650. /* test ::1234:5678 */
  32651. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  32652. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32653. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32654. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  32655. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32656. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  32657. /* test 2001:db8::1234:5678 */
  32658. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32659. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  32660. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  32661. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  32662. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32663. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  32664. sizeof(param->ipasc)), 0);
  32665. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  32666. /* 2001:db8:1::ab9:c0a8:102 */
  32667. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  32668. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  32669. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  32670. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  32671. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  32672. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  32673. sizeof(param->ipasc)), 0);
  32674. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32675. #endif /* OPENSSL_EXTRA */
  32676. return EXPECT_RESULT();
  32677. }
  32678. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  32679. {
  32680. EXPECT_DECLS;
  32681. #if defined(OPENSSL_EXTRA)
  32682. X509_STORE* store = NULL;
  32683. X509_STORE_CTX* ctx = NULL;
  32684. X509_STORE_CTX* ctx_no_init = NULL;
  32685. ExpectNotNull((store = X509_STORE_new()));
  32686. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32687. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  32688. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  32689. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  32690. /* should return NULL if ctx has not bee initialized */
  32691. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  32692. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  32693. wolfSSL_X509_STORE_CTX_free(ctx);
  32694. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  32695. X509_STORE_free(store);
  32696. #endif /* OPENSSL_EXTRA */
  32697. return EXPECT_RESULT();
  32698. }
  32699. static int test_wolfSSL_CTX_set_client_CA_list(void)
  32700. {
  32701. EXPECT_DECLS;
  32702. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32703. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  32704. WOLFSSL_CTX* ctx = NULL;
  32705. WOLFSSL* ssl = NULL;
  32706. X509_NAME* name = NULL;
  32707. STACK_OF(X509_NAME)* names = NULL;
  32708. STACK_OF(X509_NAME)* ca_list = NULL;
  32709. int names_len = 0;
  32710. int i;
  32711. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  32712. /* Send two X501 names in cert request */
  32713. names = SSL_load_client_CA_file(cliCertFile);
  32714. ExpectNotNull(names);
  32715. ca_list = SSL_load_client_CA_file(caCertFile);
  32716. ExpectNotNull(ca_list);
  32717. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  32718. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  32719. if (EXPECT_FAIL()) {
  32720. wolfSSL_X509_NAME_free(name);
  32721. name = NULL;
  32722. }
  32723. SSL_CTX_set_client_CA_list(ctx, names);
  32724. /* This should only free the stack structure */
  32725. sk_X509_NAME_free(ca_list);
  32726. ca_list = NULL;
  32727. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  32728. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  32729. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  32730. for (i = 0; i < names_len; i++) {
  32731. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  32732. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  32733. }
  32734. /* Needed to be able to create ssl object */
  32735. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32736. SSL_FILETYPE_PEM));
  32737. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32738. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32739. /* load again as old names are responsibility of ctx to free*/
  32740. names = SSL_load_client_CA_file(cliCertFile);
  32741. ExpectNotNull(names);
  32742. SSL_set_client_CA_list(ssl, names);
  32743. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  32744. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  32745. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  32746. for (i = 0; i < names_len; i++) {
  32747. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  32748. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  32749. }
  32750. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  32751. {
  32752. tcp_ready ready;
  32753. func_args server_args;
  32754. callback_functions server_cb;
  32755. THREAD_TYPE serverThread;
  32756. WOLFSSL* ssl_client = NULL;
  32757. WOLFSSL_CTX* ctx_client = NULL;
  32758. SOCKET_T sockfd = 0;
  32759. /* wolfSSL_get_client_CA_list() with handshake */
  32760. StartTCP();
  32761. InitTcpReady(&ready);
  32762. XMEMSET(&server_args, 0, sizeof(func_args));
  32763. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32764. server_args.signal = &ready;
  32765. server_args.callbacks = &server_cb;
  32766. /* we are responsible for free'ing WOLFSSL_CTX */
  32767. server_cb.ctx = ctx;
  32768. server_cb.isSharedCtx = 1;
  32769. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  32770. cliCertFile, 0));
  32771. start_thread(test_server_nofail, &server_args, &serverThread);
  32772. wait_tcp_ready(&server_args);
  32773. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32774. ExpectNotNull(ctx_client =
  32775. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  32776. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  32777. ctx_client, caCertFile, 0));
  32778. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  32779. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  32780. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  32781. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  32782. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  32783. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  32784. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  32785. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  32786. /* We are expecting two cert names to be sent */
  32787. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  32788. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  32789. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  32790. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  32791. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  32792. }
  32793. wolfSSL_shutdown(ssl_client);
  32794. wolfSSL_free(ssl_client);
  32795. wolfSSL_CTX_free(ctx_client);
  32796. CloseSocket(sockfd);
  32797. join_thread(serverThread);
  32798. FreeTcpReady(&ready);
  32799. }
  32800. #endif
  32801. wolfSSL_free(ssl);
  32802. wolfSSL_CTX_free(ctx);
  32803. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  32804. * !NO_BIO */
  32805. return EXPECT_RESULT();
  32806. }
  32807. static int test_wolfSSL_CTX_add_client_CA(void)
  32808. {
  32809. EXPECT_DECLS;
  32810. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32811. !defined(NO_WOLFSSL_CLIENT)
  32812. WOLFSSL_CTX* ctx = NULL;
  32813. WOLFSSL_X509* x509 = NULL;
  32814. WOLFSSL_X509* x509_a = NULL;
  32815. STACK_OF(X509_NAME)* ca_list = NULL;
  32816. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32817. /* Add client cert */
  32818. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  32819. SSL_FILETYPE_PEM));
  32820. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  32821. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  32822. /* Add another client cert */
  32823. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  32824. SSL_FILETYPE_PEM));
  32825. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  32826. /* test for incorrect parameter */
  32827. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  32828. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  32829. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  32830. X509_free(x509);
  32831. X509_free(x509_a);
  32832. SSL_CTX_free(ctx);
  32833. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  32834. return EXPECT_RESULT();
  32835. }
  32836. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  32837. defined(HAVE_IO_TESTS_DEPENDENCIES)
  32838. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  32839. {
  32840. callback_functions* callbacks = ((func_args*)args)->callbacks;
  32841. WOLFSSL_CTX* ctx = callbacks->ctx;
  32842. WOLFSSL* ssl = NULL;
  32843. SOCKET_T sfd = 0;
  32844. SOCKET_T cfd = 0;
  32845. word16 port;
  32846. char input[1024];
  32847. int idx;
  32848. int ret, err = 0;
  32849. const char* privateName = "ech-private-name.com";
  32850. int privateNameLen = (int)XSTRLEN(privateName);
  32851. ((func_args*)args)->return_code = TEST_FAIL;
  32852. port = ((func_args*)args)->signal->port;
  32853. AssertIntEQ(WOLFSSL_SUCCESS,
  32854. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  32855. AssertIntEQ(WOLFSSL_SUCCESS,
  32856. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  32857. WOLFSSL_FILETYPE_PEM));
  32858. AssertIntEQ(WOLFSSL_SUCCESS,
  32859. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32860. WOLFSSL_FILETYPE_PEM));
  32861. if (callbacks->ctx_ready)
  32862. callbacks->ctx_ready(ctx);
  32863. ssl = wolfSSL_new(ctx);
  32864. /* set the sni for the server */
  32865. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  32866. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  32867. CloseSocket(sfd);
  32868. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  32869. if (callbacks->ssl_ready)
  32870. callbacks->ssl_ready(ssl);
  32871. do {
  32872. err = 0; /* Reset error */
  32873. ret = wolfSSL_accept(ssl);
  32874. if (ret != WOLFSSL_SUCCESS) {
  32875. err = wolfSSL_get_error(ssl, 0);
  32876. }
  32877. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  32878. if (ret != WOLFSSL_SUCCESS) {
  32879. char buff[WOLFSSL_MAX_ERROR_SZ];
  32880. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  32881. }
  32882. else {
  32883. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  32884. input[idx] = 0;
  32885. fprintf(stderr, "Client message: %s\n", input);
  32886. }
  32887. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  32888. privateNameLen));
  32889. ((func_args*)args)->return_code = TEST_SUCCESS;
  32890. }
  32891. if (callbacks->on_result)
  32892. callbacks->on_result(ssl);
  32893. wolfSSL_shutdown(ssl);
  32894. wolfSSL_free(ssl);
  32895. wolfSSL_CTX_free(ctx);
  32896. CloseSocket(cfd);
  32897. #ifdef FP_ECC
  32898. wc_ecc_fp_free();
  32899. #endif
  32900. WOLFSSL_RETURN_FROM_THREAD(0);
  32901. }
  32902. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32903. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32904. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  32905. {
  32906. AssertNotNull(ssl);
  32907. AssertNotNull(line);
  32908. XFILE fp;
  32909. const byte lf = '\n';
  32910. fp = XFOPEN("./MyKeyLog.txt", "a");
  32911. XFWRITE( line, 1, strlen(line),fp);
  32912. XFWRITE( (void*)&lf,1,1,fp);
  32913. XFFLUSH(fp);
  32914. XFCLOSE(fp);
  32915. }
  32916. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  32917. static int test_wolfSSL_CTX_set_keylog_callback(void)
  32918. {
  32919. EXPECT_DECLS;
  32920. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  32921. !defined(NO_WOLFSSL_CLIENT)
  32922. SSL_CTX* ctx = NULL;
  32923. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32924. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  32925. SSL_CTX_free(ctx);
  32926. SSL_CTX_set_keylog_callback(NULL, NULL);
  32927. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  32928. return EXPECT_RESULT();
  32929. }
  32930. static int test_wolfSSL_CTX_get_keylog_callback(void)
  32931. {
  32932. EXPECT_DECLS;
  32933. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  32934. !defined(NO_WOLFSSL_CLIENT)
  32935. SSL_CTX* ctx = NULL;
  32936. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32937. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  32938. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  32939. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  32940. SSL_CTX_set_keylog_callback(ctx, NULL );
  32941. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  32942. SSL_CTX_free(ctx);
  32943. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  32944. return EXPECT_RESULT();
  32945. }
  32946. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32947. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  32948. {
  32949. /* set keylog callback */
  32950. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  32951. return TEST_SUCCESS;
  32952. }
  32953. #endif
  32954. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  32955. {
  32956. EXPECT_DECLS;
  32957. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  32958. /* This test is intended for checking whether keylog callback is called
  32959. * in client during TLS handshake between the client and a server.
  32960. */
  32961. test_ssl_cbf server_cbf;
  32962. test_ssl_cbf client_cbf;
  32963. XFILE fp = XBADFILE;
  32964. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  32965. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  32966. server_cbf.method = wolfTLSv1_2_server_method;
  32967. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  32968. /* clean up keylog file */
  32969. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  32970. if (fp != XBADFILE) {
  32971. XFFLUSH(fp);
  32972. XFCLOSE(fp);
  32973. fp = XBADFILE;
  32974. }
  32975. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32976. &server_cbf, NULL), TEST_SUCCESS);
  32977. XSLEEP_MS(100);
  32978. /* check if the keylog file exists */
  32979. char buff[300] = {0};
  32980. int found = 0;
  32981. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  32982. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  32983. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  32984. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  32985. found = 1;
  32986. break;
  32987. }
  32988. }
  32989. if (fp != XBADFILE) {
  32990. XFCLOSE(fp);
  32991. }
  32992. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  32993. ExpectIntEQ(found, 1);
  32994. /* clean up */
  32995. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  32996. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  32997. return EXPECT_RESULT();
  32998. }
  32999. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33000. defined(HAVE_SECRET_CALLBACK)
  33001. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33002. {
  33003. /* set keylog callback */
  33004. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33005. return TEST_SUCCESS;
  33006. }
  33007. #endif
  33008. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33009. {
  33010. EXPECT_DECLS;
  33011. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33012. defined(HAVE_SECRET_CALLBACK)
  33013. /* This test is intended for checking whether keylog callback is called
  33014. * in client during TLS handshake between the client and a server.
  33015. */
  33016. test_ssl_cbf server_cbf;
  33017. test_ssl_cbf client_cbf;
  33018. XFILE fp = XBADFILE;
  33019. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33020. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33021. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33022. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  33023. /* clean up keylog file */
  33024. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33025. if (fp != XBADFILE) {
  33026. XFCLOSE(fp);
  33027. fp = XBADFILE;
  33028. }
  33029. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33030. &server_cbf, NULL), TEST_SUCCESS);
  33031. /* check if the keylog file exists */
  33032. {
  33033. char buff[300] = {0};
  33034. int found[4] = {0};
  33035. int numfnd = 0;
  33036. int i;
  33037. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33038. while (EXPECT_SUCCESS() &&
  33039. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33040. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33041. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33042. found[0] = 1;
  33043. continue;
  33044. }
  33045. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33046. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33047. found[1] = 1;
  33048. continue;
  33049. }
  33050. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33051. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33052. found[2] = 1;
  33053. continue;
  33054. }
  33055. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33056. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33057. found[3] = 1;
  33058. continue;
  33059. }
  33060. }
  33061. if (fp != XBADFILE)
  33062. XFCLOSE(fp);
  33063. for (i = 0; i < 4; i++) {
  33064. if (found[i] != 0)
  33065. numfnd++;
  33066. }
  33067. ExpectIntEQ(numfnd, 4);
  33068. }
  33069. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33070. return EXPECT_RESULT();
  33071. }
  33072. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33073. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33074. static int test_wolfSSL_Tls13_ECH_params(void)
  33075. {
  33076. EXPECT_DECLS;
  33077. #if !defined(NO_WOLFSSL_CLIENT)
  33078. word32 outputLen = 0;
  33079. byte testBuf[72];
  33080. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33081. WOLFSSL *ssl = wolfSSL_new(ctx);
  33082. ExpectNotNull(ctx);
  33083. ExpectNotNull(ssl);
  33084. /* invalid ctx */
  33085. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33086. "ech-public-name.com", 0, 0, 0));
  33087. /* invalid public name */
  33088. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33089. 0, 0));
  33090. /* invalid algorithms */
  33091. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33092. "ech-public-name.com", 1000, 1000, 1000));
  33093. /* invalid ctx */
  33094. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33095. &outputLen));
  33096. /* invalid output len */
  33097. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33098. /* invalid ssl */
  33099. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33100. (char*)testBuf, sizeof(testBuf)));
  33101. /* invalid configs64 */
  33102. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33103. sizeof(testBuf)));
  33104. /* invalid size */
  33105. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33106. (char*)testBuf, 0));
  33107. /* invalid ssl */
  33108. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33109. sizeof(testBuf)));
  33110. /* invalid configs */
  33111. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33112. sizeof(testBuf)));
  33113. /* invalid size */
  33114. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33115. /* invalid ssl */
  33116. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33117. /* invalid size */
  33118. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33119. wolfSSL_free(ssl);
  33120. wolfSSL_CTX_free(ctx);
  33121. #endif /* !NO_WOLFSSL_CLIENT */
  33122. return EXPECT_RESULT();
  33123. }
  33124. static int test_wolfSSL_Tls13_ECH(void)
  33125. {
  33126. EXPECT_DECLS;
  33127. tcp_ready ready;
  33128. func_args client_args;
  33129. func_args server_args;
  33130. THREAD_TYPE serverThread;
  33131. callback_functions server_cbf;
  33132. callback_functions client_cbf;
  33133. SOCKET_T sockfd = 0;
  33134. WOLFSSL_CTX* ctx = NULL;
  33135. WOLFSSL* ssl = NULL;
  33136. const char* publicName = "ech-public-name.com";
  33137. const char* privateName = "ech-private-name.com";
  33138. int privateNameLen = 20;
  33139. char reply[1024];
  33140. int replyLen = 0;
  33141. byte rawEchConfig[128];
  33142. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33143. InitTcpReady(&ready);
  33144. ready.port = 22222;
  33145. XMEMSET(&client_args, 0, sizeof(func_args));
  33146. XMEMSET(&server_args, 0, sizeof(func_args));
  33147. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33148. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33149. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33150. /* create the server context here so we can get the ech config */
  33151. ExpectNotNull(server_cbf.ctx =
  33152. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33153. /* generate ech config */
  33154. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33155. publicName, 0, 0, 0));
  33156. /* get the config for the client to use */
  33157. ExpectIntEQ(WOLFSSL_SUCCESS,
  33158. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33159. &rawEchConfigLen));
  33160. server_args.callbacks = &server_cbf;
  33161. server_args.signal = &ready;
  33162. /* start server task */
  33163. start_thread(server_task_ech, &server_args, &serverThread);
  33164. wait_tcp_ready(&server_args);
  33165. /* run as a TLS1.3 client */
  33166. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33167. ExpectIntEQ(WOLFSSL_SUCCESS,
  33168. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33169. ExpectIntEQ(WOLFSSL_SUCCESS,
  33170. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33171. ExpectIntEQ(WOLFSSL_SUCCESS,
  33172. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33173. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33174. /* get connected the server task */
  33175. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33176. /* set the ech configs for the client */
  33177. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33178. rawEchConfigLen));
  33179. /* set the sni for the client */
  33180. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33181. privateName, privateNameLen));
  33182. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33183. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33184. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33185. privateNameLen);
  33186. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33187. /* add th null terminator for string compare */
  33188. reply[replyLen] = 0;
  33189. /* check that the server replied with the private name */
  33190. ExpectStrEQ(privateName, reply);
  33191. wolfSSL_free(ssl);
  33192. wolfSSL_CTX_free(ctx);
  33193. CloseSocket(sockfd);
  33194. join_thread(serverThread);
  33195. FreeTcpReady(&ready);
  33196. return EXPECT_RESULT();
  33197. }
  33198. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33199. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33200. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33201. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33202. static int post_auth_version_cb(WOLFSSL* ssl)
  33203. {
  33204. EXPECT_DECLS;
  33205. /* do handshake and then test version error */
  33206. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33207. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33208. return EXPECT_RESULT();
  33209. }
  33210. static int post_auth_version_client_cb(WOLFSSL* ssl)
  33211. {
  33212. EXPECT_DECLS;
  33213. /* do handshake and then test version error */
  33214. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33215. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33216. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33217. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33218. /* check was added to error queue */
  33219. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33220. /* check the string matches expected string */
  33221. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33222. "WRONG_SSL_VERSION");
  33223. #endif
  33224. return EXPECT_RESULT();
  33225. }
  33226. static int post_auth_cb(WOLFSSL* ssl)
  33227. {
  33228. EXPECT_DECLS;
  33229. WOLFSSL_X509* x509 = NULL;
  33230. /* do handshake and then test version error */
  33231. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33232. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33233. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33234. wolfSSL_X509_free(x509);
  33235. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33236. return EXPECT_RESULT();
  33237. }
  33238. static int set_post_auth_cb(WOLFSSL* ssl)
  33239. {
  33240. if (!wolfSSL_is_server(ssl)) {
  33241. EXPECT_DECLS;
  33242. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33243. return EXPECT_RESULT();
  33244. }
  33245. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33246. return TEST_SUCCESS;
  33247. }
  33248. #endif
  33249. static int test_wolfSSL_Tls13_postauth(void)
  33250. {
  33251. EXPECT_DECLS;
  33252. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33253. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33254. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33255. test_ssl_cbf server_cbf;
  33256. test_ssl_cbf client_cbf;
  33257. /* test version failure doing post auth with TLS 1.2 connection */
  33258. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33259. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33260. server_cbf.method = wolfTLSv1_2_server_method;
  33261. server_cbf.ssl_ready = set_post_auth_cb;
  33262. server_cbf.on_result = post_auth_version_cb;
  33263. client_cbf.ssl_ready = set_post_auth_cb;
  33264. client_cbf.on_result = post_auth_version_client_cb;
  33265. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33266. &server_cbf, NULL), TEST_SUCCESS);
  33267. /* tests on post auth with TLS 1.3 */
  33268. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33269. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33270. server_cbf.method = wolfTLSv1_3_server_method;
  33271. server_cbf.ssl_ready = set_post_auth_cb;
  33272. client_cbf.ssl_ready = set_post_auth_cb;
  33273. server_cbf.on_result = post_auth_cb;
  33274. client_cbf.on_result = NULL;
  33275. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33276. &server_cbf, NULL), TEST_SUCCESS);
  33277. #endif
  33278. return EXPECT_RESULT();
  33279. }
  33280. static int test_wolfSSL_X509_NID(void)
  33281. {
  33282. EXPECT_DECLS;
  33283. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  33284. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  33285. int sigType;
  33286. int nameSz;
  33287. X509* cert = NULL;
  33288. EVP_PKEY* pubKeyTmp = NULL;
  33289. X509_NAME* name = NULL;
  33290. char commonName[80];
  33291. char countryName[80];
  33292. char localityName[80];
  33293. char stateName[80];
  33294. char orgName[80];
  33295. char orgUnit[80];
  33296. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  33297. /* convert cert from DER to internal WOLFSSL_X509 struct */
  33298. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  33299. sizeof_client_cert_der_2048, HEAP_HINT));
  33300. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  33301. /* extract PUBLIC KEY from cert */
  33302. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  33303. /* extract signatureType */
  33304. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  33305. /* extract subjectName info */
  33306. ExpectNotNull(name = X509_get_subject_name(cert));
  33307. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  33308. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33309. NULL, 0)), 0);
  33310. ExpectIntEQ(nameSz, 15);
  33311. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33312. commonName, sizeof(commonName))), 0);
  33313. ExpectIntEQ(nameSz, 15);
  33314. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  33315. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33316. commonName, 9)), 0);
  33317. ExpectIntEQ(nameSz, 8);
  33318. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  33319. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  33320. countryName, sizeof(countryName))), 0);
  33321. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  33322. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  33323. localityName, sizeof(localityName))), 0);
  33324. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  33325. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33326. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  33327. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  33328. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  33329. orgName, sizeof(orgName))), 0);
  33330. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  33331. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33332. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  33333. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  33334. EVP_PKEY_free(pubKeyTmp);
  33335. X509_free(cert);
  33336. #endif
  33337. return EXPECT_RESULT();
  33338. }
  33339. static int test_wolfSSL_CTX_set_srp_username(void)
  33340. {
  33341. EXPECT_DECLS;
  33342. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  33343. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33344. WOLFSSL_CTX* ctx = NULL;
  33345. WOLFSSL* ssl = NULL;
  33346. const char *username = "TESTUSER";
  33347. const char *password = "TESTPASSWORD";
  33348. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33349. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33350. SSL_SUCCESS);
  33351. wolfSSL_CTX_free(ctx);
  33352. ctx = NULL;
  33353. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33354. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33355. SSL_SUCCESS);
  33356. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33357. SSL_SUCCESS);
  33358. ExpectNotNull(ssl = SSL_new(ctx));
  33359. ExpectNotNull(SSL_get_srp_username(ssl));
  33360. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  33361. wolfSSL_free(ssl);
  33362. wolfSSL_CTX_free(ctx);
  33363. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  33364. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  33365. return EXPECT_RESULT();
  33366. }
  33367. static int test_wolfSSL_CTX_set_srp_password(void)
  33368. {
  33369. EXPECT_DECLS;
  33370. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  33371. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33372. WOLFSSL_CTX* ctx = NULL;
  33373. const char *username = "TESTUSER";
  33374. const char *password = "TESTPASSWORD";
  33375. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33376. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33377. SSL_SUCCESS);
  33378. wolfSSL_CTX_free(ctx);
  33379. ctx = NULL;
  33380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33381. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33382. SSL_SUCCESS);
  33383. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33384. SSL_SUCCESS);
  33385. wolfSSL_CTX_free(ctx);
  33386. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  33387. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  33388. return EXPECT_RESULT();
  33389. }
  33390. static int test_wolfSSL_X509_STORE(void)
  33391. {
  33392. EXPECT_DECLS;
  33393. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33394. X509_STORE *store = NULL;
  33395. #ifdef HAVE_CRL
  33396. X509_STORE_CTX *storeCtx = NULL;
  33397. X509_CRL *crl = NULL;
  33398. X509 *ca = NULL;
  33399. X509 *cert = NULL;
  33400. const char crlPem[] = "./certs/crl/crl.revoked";
  33401. const char srvCert[] = "./certs/server-revoked-cert.pem";
  33402. const char caCert[] = "./certs/ca-cert.pem";
  33403. XFILE fp = XBADFILE;
  33404. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33405. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  33406. SSL_FILETYPE_PEM)));
  33407. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  33408. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  33409. SSL_FILETYPE_PEM)));
  33410. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  33411. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  33412. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  33413. X509_STORE_free(store);
  33414. store = NULL;
  33415. X509_STORE_CTX_free(storeCtx);
  33416. storeCtx = NULL;
  33417. X509_free(cert);
  33418. cert = NULL;
  33419. X509_free(ca);
  33420. ca = NULL;
  33421. /* should fail to verify now after adding in CRL */
  33422. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33423. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  33424. SSL_FILETYPE_PEM)));
  33425. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  33426. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  33427. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  33428. NULL, NULL));
  33429. if (fp != XBADFILE)
  33430. XFCLOSE(fp);
  33431. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  33432. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  33433. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  33434. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  33435. SSL_FILETYPE_PEM)));
  33436. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  33437. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  33438. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  33439. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  33440. X509_CRL_free(crl);
  33441. crl = NULL;
  33442. X509_STORE_free(store);
  33443. store = NULL;
  33444. X509_STORE_CTX_free(storeCtx);
  33445. storeCtx = NULL;
  33446. X509_free(cert);
  33447. cert = NULL;
  33448. X509_free(ca);
  33449. ca = NULL;
  33450. #endif /* HAVE_CRL */
  33451. #ifndef WOLFCRYPT_ONLY
  33452. {
  33453. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33454. SSL_CTX* ctx = NULL;
  33455. SSL* ssl = NULL;
  33456. int i;
  33457. for (i = 0; i < 2; i++) {
  33458. #ifndef NO_WOLFSSL_SERVER
  33459. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33460. #else
  33461. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33462. #endif
  33463. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33464. SSL_CTX_set_cert_store(ctx, store);
  33465. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33466. SSL_CTX_set_cert_store(ctx, store);
  33467. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33468. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33469. SSL_FILETYPE_PEM), SSL_SUCCESS);
  33470. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33471. SSL_FILETYPE_PEM), SSL_SUCCESS);
  33472. ExpectNotNull(ssl = SSL_new(ctx));
  33473. if (i == 0) {
  33474. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  33475. SSL_SUCCESS);
  33476. }
  33477. else {
  33478. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  33479. #ifdef OPENSSL_ALL
  33480. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  33481. #endif
  33482. }
  33483. if (EXPECT_FAIL() || (i == 1)) {
  33484. X509_STORE_free(store);
  33485. store = NULL;
  33486. }
  33487. SSL_free(ssl);
  33488. ssl = NULL;
  33489. SSL_CTX_free(ctx);
  33490. ctx = NULL;
  33491. }
  33492. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33493. }
  33494. #endif
  33495. #endif
  33496. return EXPECT_RESULT();
  33497. }
  33498. static int test_wolfSSL_X509_STORE_load_locations(void)
  33499. {
  33500. EXPECT_DECLS;
  33501. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33502. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  33503. SSL_CTX *ctx = NULL;
  33504. X509_STORE *store = NULL;
  33505. const char ca_file[] = "./certs/ca-cert.pem";
  33506. const char client_pem_file[] = "./certs/client-cert.pem";
  33507. const char client_der_file[] = "./certs/client-cert.der";
  33508. const char ecc_file[] = "./certs/ecc-key.pem";
  33509. const char certs_path[] = "./certs/";
  33510. const char bad_path[] = "./bad-path/";
  33511. #ifdef HAVE_CRL
  33512. const char crl_path[] = "./certs/crl/";
  33513. const char crl_file[] = "./certs/crl/crl.pem";
  33514. #endif
  33515. #ifndef NO_WOLFSSL_SERVER
  33516. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  33517. #else
  33518. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  33519. #endif
  33520. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  33521. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  33522. WOLFSSL_SUCCESS);
  33523. /* Test bad arguments */
  33524. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  33525. WOLFSSL_FAILURE);
  33526. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  33527. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  33528. WOLFSSL_FAILURE);
  33529. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  33530. WOLFSSL_FAILURE);
  33531. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  33532. WOLFSSL_FAILURE);
  33533. #ifdef HAVE_CRL
  33534. /* Test with CRL */
  33535. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  33536. WOLFSSL_SUCCESS);
  33537. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  33538. WOLFSSL_SUCCESS);
  33539. #endif
  33540. /* Test with CA */
  33541. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  33542. WOLFSSL_SUCCESS);
  33543. /* Test with client_cert and certs path */
  33544. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  33545. WOLFSSL_SUCCESS);
  33546. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  33547. WOLFSSL_SUCCESS);
  33548. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  33549. /* Clear nodes */
  33550. ERR_clear_error();
  33551. #endif
  33552. SSL_CTX_free(ctx);
  33553. #endif
  33554. return EXPECT_RESULT();
  33555. }
  33556. static int test_X509_STORE_get0_objects(void)
  33557. {
  33558. EXPECT_DECLS;
  33559. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  33560. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  33561. X509_STORE *store = NULL;
  33562. X509_STORE *store_cpy = NULL;
  33563. SSL_CTX *ctx = NULL;
  33564. X509_OBJECT *obj = NULL;
  33565. STACK_OF(X509_OBJECT) *objs = NULL;
  33566. int i;
  33567. /* Setup store */
  33568. #ifndef NO_WOLFSSL_SERVER
  33569. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  33570. #else
  33571. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  33572. #endif
  33573. ExpectNotNull(store_cpy = X509_STORE_new());
  33574. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  33575. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  33576. WOLFSSL_SUCCESS);
  33577. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  33578. WOLFSSL_SUCCESS);
  33579. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  33580. WOLFSSL_SUCCESS);
  33581. #ifdef HAVE_CRL
  33582. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  33583. WOLFSSL_SUCCESS);
  33584. #endif
  33585. /* Store ready */
  33586. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  33587. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  33588. #ifdef HAVE_CRL
  33589. #ifdef WOLFSSL_SIGNER_DER_CERT
  33590. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  33591. #else
  33592. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  33593. #endif
  33594. #else
  33595. #ifdef WOLFSSL_SIGNER_DER_CERT
  33596. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  33597. #else
  33598. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  33599. #endif
  33600. #endif
  33601. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  33602. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  33603. switch (X509_OBJECT_get_type(obj)) {
  33604. case X509_LU_X509:
  33605. {
  33606. WOLFSSL_X509* x509;
  33607. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  33608. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  33609. break;
  33610. }
  33611. case X509_LU_CRL:
  33612. #ifdef HAVE_CRL
  33613. {
  33614. WOLFSSL_CRL* crl = NULL;
  33615. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  33616. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  33617. break;
  33618. }
  33619. #endif
  33620. case X509_LU_NONE:
  33621. default:
  33622. Fail(("X509_OBJECT_get_type should return x509 or crl "
  33623. "(when built with crl support)"),
  33624. ("Unrecognized X509_OBJECT type or none"));
  33625. }
  33626. }
  33627. X509_STORE_free(store_cpy);
  33628. SSL_CTX_free(ctx);
  33629. #endif
  33630. return EXPECT_RESULT();
  33631. }
  33632. static int test_wolfSSL_BN_CTX(void)
  33633. {
  33634. EXPECT_DECLS;
  33635. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33636. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33637. WOLFSSL_BN_CTX* bn_ctx = NULL;
  33638. WOLFSSL_BIGNUM* t = NULL;
  33639. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  33640. /* No implementation. */
  33641. BN_CTX_init(NULL);
  33642. ExpectNotNull(t = BN_CTX_get(NULL));
  33643. BN_free(t);
  33644. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  33645. BN_free(t);
  33646. #ifndef NO_WOLFSSL_STUB
  33647. /* No implementation. */
  33648. BN_CTX_start(NULL);
  33649. BN_CTX_start(bn_ctx);
  33650. #endif
  33651. BN_CTX_free(NULL);
  33652. BN_CTX_free(bn_ctx);
  33653. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33654. return EXPECT_RESULT();
  33655. }
  33656. static int test_wolfSSL_BN(void)
  33657. {
  33658. EXPECT_DECLS;
  33659. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33660. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33661. BIGNUM* a = NULL;
  33662. BIGNUM* b = NULL;
  33663. BIGNUM* c = NULL;
  33664. BIGNUM* d = NULL;
  33665. BIGNUM emptyBN;
  33666. /* Setup */
  33667. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33668. /* internal not set emptyBN. */
  33669. ExpectNotNull(a = BN_new());
  33670. ExpectNotNull(b = BN_new());
  33671. ExpectNotNull(c = BN_dup(b));
  33672. ExpectNotNull(d = BN_new());
  33673. /* Invalid parameter testing. */
  33674. BN_free(NULL);
  33675. ExpectNull(BN_dup(NULL));
  33676. ExpectNull(BN_dup(&emptyBN));
  33677. ExpectNull(BN_copy(NULL, NULL));
  33678. ExpectNull(BN_copy(b, NULL));
  33679. ExpectNull(BN_copy(NULL, c));
  33680. ExpectNull(BN_copy(b, &emptyBN));
  33681. ExpectNull(BN_copy(&emptyBN, c));
  33682. BN_clear(NULL);
  33683. BN_clear(&emptyBN);
  33684. ExpectIntEQ(BN_num_bytes(NULL), 0);
  33685. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  33686. ExpectIntEQ(BN_num_bits(NULL), 0);
  33687. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  33688. ExpectIntEQ(BN_is_negative(NULL), 0);
  33689. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  33690. /* END Invalid Parameters */
  33691. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  33692. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  33693. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  33694. ExpectIntEQ(BN_num_bits(a), 2);
  33695. ExpectIntEQ(BN_num_bytes(a), 1);
  33696. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  33697. defined(WOLFSSL_SP_INT_NEGATIVE))
  33698. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  33699. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  33700. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  33701. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  33702. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  33703. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33704. {
  33705. /* Do additional tests on negative BN conversions. */
  33706. char* ret = NULL;
  33707. ASN1_INTEGER* asn1 = NULL;
  33708. BIGNUM* tmp = NULL;
  33709. /* Sanity check we have a negative BN. */
  33710. ExpectIntEQ(BN_is_negative(c), 1);
  33711. ExpectNotNull(ret = BN_bn2dec(c));
  33712. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  33713. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  33714. ret = NULL;
  33715. /* Convert to ASN1_INTEGER and back to BN. */
  33716. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  33717. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  33718. /* After converting back BN should be negative and correct. */
  33719. ExpectIntEQ(BN_is_negative(tmp), 1);
  33720. ExpectNotNull(ret = BN_bn2dec(tmp));
  33721. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  33722. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  33723. ASN1_INTEGER_free(asn1);
  33724. BN_free(tmp);
  33725. }
  33726. #endif
  33727. ExpectIntEQ(BN_get_word(c), 4);
  33728. #endif
  33729. ExpectIntEQ(BN_set_word(a, 3), 1);
  33730. ExpectIntEQ(BN_set_word(b, 3), 1);
  33731. ExpectIntEQ(BN_set_word(c, 4), 1);
  33732. /* NULL == NULL, NULL < num, num > NULL */
  33733. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  33734. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  33735. ExpectIntLT(BN_cmp(NULL, b), 0);
  33736. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  33737. ExpectIntGT(BN_cmp(a, NULL), 0);
  33738. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  33739. ExpectIntEQ(BN_cmp(a, b), 0);
  33740. ExpectIntLT(BN_cmp(a, c), 0);
  33741. ExpectIntGT(BN_cmp(c, b), 0);
  33742. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  33743. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  33744. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  33745. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  33746. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  33747. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  33748. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  33749. #endif
  33750. BN_clear(a);
  33751. BN_free(a);
  33752. BN_free(b);
  33753. BN_free(c);
  33754. BN_clear_free(d);
  33755. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33756. return EXPECT_RESULT();
  33757. }
  33758. static int test_wolfSSL_BN_init(void)
  33759. {
  33760. EXPECT_DECLS;
  33761. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33762. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33763. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  33764. BIGNUM* ap = NULL;
  33765. BIGNUM bv;
  33766. BIGNUM cv;
  33767. BIGNUM dv;
  33768. ExpectNotNull(ap = BN_new());
  33769. BN_init(NULL);
  33770. XMEMSET(&bv, 0, sizeof(bv));
  33771. ExpectNull(BN_dup(&bv));
  33772. BN_init(&bv);
  33773. BN_init(&cv);
  33774. BN_init(&dv);
  33775. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  33776. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  33777. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  33778. /* a^b mod c = */
  33779. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  33780. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  33781. /* check result 3^2 mod 5 */
  33782. ExpectIntEQ(BN_get_word(&dv), 4);
  33783. /* a*b mod c = */
  33784. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  33785. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  33786. /* check result 3*2 mod 5 */
  33787. ExpectIntEQ(BN_get_word(&dv), 1);
  33788. BN_free(ap);
  33789. #endif
  33790. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33791. return EXPECT_RESULT();
  33792. }
  33793. static int test_wolfSSL_BN_enc_dec(void)
  33794. {
  33795. EXPECT_DECLS;
  33796. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  33797. BIGNUM* a = NULL;
  33798. BIGNUM* b = NULL;
  33799. BIGNUM* c = NULL;
  33800. BIGNUM emptyBN;
  33801. char* str = NULL;
  33802. const char* emptyStr = "";
  33803. const char* numberStr = "12345";
  33804. const char* badStr = "g12345";
  33805. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33806. const char* twoStr = "2";
  33807. #endif
  33808. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  33809. unsigned char outNum[5];
  33810. /* Setup */
  33811. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33812. ExpectNotNull(a = BN_new());
  33813. ExpectNotNull(b = BN_new());
  33814. ExpectIntEQ(BN_set_word(a, 2), 1);
  33815. /* Invalid parameters */
  33816. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  33817. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  33818. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  33819. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  33820. ExpectNull(BN_bn2hex(NULL));
  33821. ExpectNull(BN_bn2hex(&emptyBN));
  33822. ExpectNull(BN_bn2dec(NULL));
  33823. ExpectNull(BN_bn2dec(&emptyBN));
  33824. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  33825. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  33826. ExpectNull(BN_bin2bn(binNum, -1, a));
  33827. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  33828. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  33829. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  33830. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  33831. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  33832. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  33833. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  33834. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  33835. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  33836. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  33837. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  33838. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  33839. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  33840. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  33841. ExpectIntEQ(BN_set_word(a, 2), 1);
  33842. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  33843. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  33844. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  33845. ExpectIntEQ(BN_cmp(a, b), 0);
  33846. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  33847. ExpectIntEQ(BN_cmp(a, b), -1);
  33848. ExpectNotNull(str = BN_bn2hex(a));
  33849. ExpectNotNull(BN_hex2bn(&b, str));
  33850. ExpectIntEQ(BN_cmp(a, b), 0);
  33851. ExpectNotNull(BN_hex2bn(&b, numberStr));
  33852. ExpectIntEQ(BN_cmp(a, b), -1);
  33853. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33854. str = NULL;
  33855. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33856. ExpectNotNull(str = BN_bn2dec(a));
  33857. ExpectStrEQ(str, twoStr);
  33858. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33859. str = NULL;
  33860. #ifndef NO_RSA
  33861. ExpectNotNull(str = BN_bn2dec(a));
  33862. ExpectNotNull(BN_dec2bn(&b, str));
  33863. ExpectIntEQ(BN_cmp(a, b), 0);
  33864. ExpectNotNull(BN_dec2bn(&b, numberStr));
  33865. ExpectIntEQ(BN_cmp(a, b), -1);
  33866. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  33867. str = NULL;
  33868. #else
  33869. /* No implementation - fail with good parameters. */
  33870. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  33871. #endif
  33872. #endif
  33873. BN_free(b);
  33874. BN_free(a);
  33875. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33876. return EXPECT_RESULT();
  33877. }
  33878. static int test_wolfSSL_BN_word(void)
  33879. {
  33880. EXPECT_DECLS;
  33881. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  33882. BIGNUM* a = NULL;
  33883. BIGNUM* b = NULL;
  33884. BIGNUM* c = NULL;
  33885. BIGNUM av;
  33886. ExpectNotNull(a = BN_new());
  33887. ExpectNotNull(b = BN_new());
  33888. ExpectNotNull(c = BN_new());
  33889. XMEMSET(&av, 0, sizeof(av));
  33890. /* Invalid parameter. */
  33891. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  33892. ExpectIntEQ(BN_add_word(&av, 3), 0);
  33893. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  33894. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  33895. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  33896. ExpectIntEQ(BN_set_word(&av, 3), 0);
  33897. ExpectIntEQ(BN_get_word(NULL), 0);
  33898. ExpectIntEQ(BN_get_word(&av), 0);
  33899. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  33900. ExpectIntEQ(BN_is_word(&av, 3), 0);
  33901. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  33902. !defined(NO_DSA))
  33903. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  33904. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  33905. #endif
  33906. ExpectIntEQ(BN_one(NULL), 0);
  33907. ExpectIntEQ(BN_one(&av), 0);
  33908. BN_zero(NULL);
  33909. BN_zero(&av);
  33910. ExpectIntEQ(BN_is_one(NULL), 0);
  33911. ExpectIntEQ(BN_is_one(&av), 0);
  33912. ExpectIntEQ(BN_is_zero(NULL), 0);
  33913. ExpectIntEQ(BN_is_zero(&av), 0);
  33914. ExpectIntEQ(BN_set_word(a, 3), 1);
  33915. ExpectIntEQ(BN_set_word(b, 2), 1);
  33916. ExpectIntEQ(BN_set_word(c, 5), 1);
  33917. /* a + 3 = */
  33918. ExpectIntEQ(BN_add_word(a, 3), 1);
  33919. /* check result 3 + 3*/
  33920. ExpectIntEQ(BN_get_word(a), 6);
  33921. ExpectIntEQ(BN_is_word(a, 6), 1);
  33922. ExpectIntEQ(BN_is_word(a, 5), 0);
  33923. /* set a back to 3 */
  33924. ExpectIntEQ(BN_set_word(a, 3), 1);
  33925. /* a - 3 = */
  33926. ExpectIntEQ(BN_sub_word(a, 3), 1);
  33927. /* check result 3 - 3*/
  33928. ExpectIntEQ(BN_get_word(a), 0);
  33929. ExpectIntEQ(BN_one(a), 1);
  33930. ExpectIntEQ(BN_is_word(a, 1), 1);
  33931. ExpectIntEQ(BN_is_word(a, 0), 0);
  33932. ExpectIntEQ(BN_is_one(a), 1);
  33933. ExpectIntEQ(BN_is_zero(a), 0);
  33934. BN_zero(a);
  33935. ExpectIntEQ(BN_is_word(a, 0), 1);
  33936. ExpectIntEQ(BN_is_word(a, 1), 0);
  33937. ExpectIntEQ(BN_is_zero(a), 1);
  33938. ExpectIntEQ(BN_is_one(a), 0);
  33939. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  33940. !defined(NO_DSA))
  33941. ExpectIntEQ(BN_set_word(a, 5), 1);
  33942. ExpectIntEQ(BN_mod_word(a, 3), 2);
  33943. ExpectIntEQ(BN_mod_word(a, 0), -1);
  33944. #endif
  33945. BN_free(c);
  33946. BN_free(b);
  33947. BN_free(a);
  33948. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33949. return EXPECT_RESULT();
  33950. }
  33951. static int test_wolfSSL_BN_bits(void)
  33952. {
  33953. EXPECT_DECLS;
  33954. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33955. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33956. BIGNUM* a = NULL;
  33957. BIGNUM emptyBN;
  33958. /* Setup */
  33959. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33960. ExpectNotNull(a = BN_new());
  33961. /* Invalid parameters. */
  33962. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  33963. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  33964. ExpectIntEQ(BN_set_bit(a, -1), 0);
  33965. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  33966. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  33967. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  33968. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  33969. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  33970. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  33971. ExpectIntEQ(BN_is_odd(NULL), 0);
  33972. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  33973. ExpectIntEQ(BN_set_word(a, 0), 1);
  33974. ExpectIntEQ(BN_is_zero(a), 1);
  33975. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  33976. ExpectIntEQ(BN_is_zero(a), 0);
  33977. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  33978. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  33979. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  33980. ExpectIntEQ(BN_is_zero(a), 1);
  33981. ExpectIntEQ(BN_set_bit(a, 0), 1);
  33982. ExpectIntEQ(BN_is_odd(a), 1);
  33983. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33984. ExpectIntEQ(BN_is_odd(a), 0);
  33985. ExpectIntEQ(BN_set_bit(a, 1), 1);
  33986. ExpectIntEQ(BN_is_odd(a), 0);
  33987. ExpectIntEQ(BN_set_bit(a, 129), 1);
  33988. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  33989. #ifndef NO_WOLFSSL_STUB
  33990. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  33991. #endif
  33992. BN_free(a);
  33993. #endif
  33994. return EXPECT_RESULT();
  33995. }
  33996. static int test_wolfSSL_BN_shift(void)
  33997. {
  33998. EXPECT_DECLS;
  33999. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34000. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34001. BIGNUM* a = NULL;
  34002. BIGNUM* b = NULL;
  34003. BIGNUM emptyBN;
  34004. /* Setup */
  34005. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34006. ExpectNotNull(a = BN_new());
  34007. ExpectNotNull(b = BN_new());
  34008. /* Invalid parameters. */
  34009. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34010. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34011. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34012. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  34013. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34014. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  34015. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34016. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  34017. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34018. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34019. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34020. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  34021. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34022. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  34023. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34024. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  34025. ExpectIntEQ(BN_set_word(a, 1), 1);
  34026. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  34027. ExpectIntEQ(BN_is_word(b, 2), 1);
  34028. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  34029. ExpectIntEQ(BN_is_word(a, 2), 1);
  34030. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34031. ExpectIntEQ(BN_is_word(b, 1), 1);
  34032. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34033. ExpectIntEQ(BN_is_word(a, 1), 1);
  34034. BN_free(b);
  34035. BN_free(a);
  34036. #endif
  34037. return EXPECT_RESULT();
  34038. }
  34039. static int test_wolfSSL_BN_math(void)
  34040. {
  34041. EXPECT_DECLS;
  34042. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34043. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34044. BIGNUM* a = NULL;
  34045. BIGNUM* b = NULL;
  34046. BIGNUM* r = NULL;
  34047. BIGNUM* rem = NULL;
  34048. BIGNUM emptyBN;
  34049. BN_ULONG val1;
  34050. BN_ULONG val2;
  34051. /* Setup */
  34052. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34053. ExpectNotNull(a = BN_new());
  34054. ExpectNotNull(b = BN_new());
  34055. ExpectNotNull(r = BN_new());
  34056. ExpectNotNull(rem = BN_new());
  34057. /* Invalid parameters. */
  34058. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34059. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34060. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34061. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34062. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34063. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34064. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34065. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34066. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34067. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34068. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34069. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34070. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34071. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34072. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34073. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34074. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34075. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34076. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34077. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34078. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34079. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34080. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34081. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34082. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34083. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34084. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34085. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34086. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34087. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34088. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34089. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34090. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34091. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34092. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34093. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34094. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34095. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34096. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34097. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34098. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34099. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34100. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34101. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34102. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34103. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34104. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34105. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34106. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34107. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34108. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34109. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34110. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34111. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34112. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34113. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34114. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34115. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34116. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34117. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34118. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34119. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34120. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34121. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34122. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34123. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34124. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34125. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34126. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34127. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34128. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34129. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34130. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34131. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34132. /* END Invalid parameters. */
  34133. val1 = 8;
  34134. val2 = 3;
  34135. ExpectIntEQ(BN_set_word(a, val1), 1);
  34136. ExpectIntEQ(BN_set_word(b, val2), 1);
  34137. ExpectIntEQ(BN_add(r, a, b), 1);
  34138. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34139. ExpectIntEQ(BN_sub(r, a, b), 1);
  34140. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34141. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34142. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34143. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34144. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34145. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34146. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34147. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34148. BN_free(rem);
  34149. BN_free(r);
  34150. BN_free(b);
  34151. BN_free(a);
  34152. #endif
  34153. return EXPECT_RESULT();
  34154. }
  34155. static int test_wolfSSL_BN_math_mod(void)
  34156. {
  34157. EXPECT_DECLS;
  34158. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34159. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34160. BIGNUM* a = NULL;
  34161. BIGNUM* b = NULL;
  34162. BIGNUM* m = NULL;
  34163. BIGNUM* r = NULL;
  34164. BIGNUM* t = NULL;
  34165. BIGNUM emptyBN;
  34166. BN_ULONG val1;
  34167. BN_ULONG val2;
  34168. BN_ULONG val3;
  34169. /* Setup */
  34170. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34171. ExpectNotNull(a = BN_new());
  34172. ExpectNotNull(b = BN_new());
  34173. ExpectNotNull(m = BN_new());
  34174. ExpectNotNull(r = BN_new());
  34175. /* Invalid parameters. */
  34176. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34177. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34178. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34179. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34180. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34181. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34182. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34183. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34184. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34185. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34186. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34187. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34188. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34189. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34190. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34191. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34192. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34193. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34194. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34195. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34196. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34197. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34198. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34199. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34200. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34201. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34202. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34203. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34204. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34205. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34206. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34207. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34208. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34209. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34210. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34211. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34212. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34213. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34214. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34215. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34216. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34217. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34218. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34219. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34220. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34221. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34222. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34223. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34224. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34225. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34226. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34227. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34228. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34229. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34230. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34231. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34232. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34233. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34234. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34235. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34236. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34237. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34238. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34239. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34240. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34241. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34242. /* END Invalid parameters. */
  34243. val1 = 9;
  34244. val2 = 13;
  34245. val3 = 5;
  34246. ExpectIntEQ(BN_set_word(a, val1), 1);
  34247. ExpectIntEQ(BN_set_word(b, val2), 1);
  34248. ExpectIntEQ(BN_set_word(m, val3), 1);
  34249. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34250. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34251. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34252. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34253. ExpectIntEQ(BN_set_word(a, 2), 1);
  34254. ExpectIntEQ(BN_set_word(b, 3), 1);
  34255. ExpectIntEQ(BN_set_word(m, 5), 1);
  34256. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34257. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34258. ExpectIntEQ(BN_is_word(r, 3), 1);
  34259. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34260. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  34261. ExpectIntEQ(BN_is_word(r, 3), 1);
  34262. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34263. ExpectIntEQ(BN_is_word(t, 3), 1);
  34264. BN_free(t);
  34265. /* No inverse case. No inverse when a divides b. */
  34266. ExpectIntEQ(BN_set_word(a, 3), 1);
  34267. ExpectIntEQ(BN_set_word(m, 9), 1);
  34268. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  34269. BN_free(r);
  34270. BN_free(m);
  34271. BN_free(b);
  34272. BN_free(a);
  34273. #endif
  34274. return EXPECT_RESULT();
  34275. }
  34276. static int test_wolfSSL_BN_math_other(void)
  34277. {
  34278. EXPECT_DECLS;
  34279. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34280. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34281. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  34282. BIGNUM* a = NULL;
  34283. BIGNUM* b = NULL;
  34284. BIGNUM* r = NULL;
  34285. BIGNUM emptyBN;
  34286. /* Setup */
  34287. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34288. ExpectNotNull(a = BN_new());
  34289. ExpectNotNull(b = BN_new());
  34290. ExpectNotNull(r = BN_new());
  34291. /* Invalid parameters. */
  34292. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  34293. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  34294. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  34295. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  34296. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  34297. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  34298. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  34299. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34300. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  34301. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  34302. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  34303. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  34304. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  34305. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  34306. /* END Invalid parameters. */
  34307. /* No common factors between 2 and 3. */
  34308. ExpectIntEQ(BN_set_word(a, 2), 1);
  34309. ExpectIntEQ(BN_set_word(b, 3), 1);
  34310. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34311. ExpectIntEQ(BN_is_word(r, 1), 1);
  34312. /* 3 is largest value that divides both 6 and 9. */
  34313. ExpectIntEQ(BN_set_word(a, 6), 1);
  34314. ExpectIntEQ(BN_set_word(b, 9), 1);
  34315. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34316. ExpectIntEQ(BN_is_word(r, 3), 1);
  34317. /* GCD of 0 and 0 is undefined. */
  34318. ExpectIntEQ(BN_set_word(a, 0), 1);
  34319. ExpectIntEQ(BN_set_word(b, 0), 1);
  34320. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  34321. /* Teardown */
  34322. BN_free(r);
  34323. BN_free(b);
  34324. BN_free(a);
  34325. #endif
  34326. #endif
  34327. return EXPECT_RESULT();
  34328. }
  34329. static int test_wolfSSL_BN_rand(void)
  34330. {
  34331. EXPECT_DECLS;
  34332. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  34333. BIGNUM* bn = NULL;
  34334. BIGNUM* range = NULL;
  34335. BIGNUM emptyBN;
  34336. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34337. ExpectNotNull(bn = BN_new());
  34338. ExpectNotNull(range = BN_new());
  34339. /* Invalid parameters. */
  34340. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  34341. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34342. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  34343. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  34344. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34345. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  34346. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  34347. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34348. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  34349. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  34350. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34351. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  34352. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  34353. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  34354. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  34355. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  34356. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  34357. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  34358. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  34359. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34360. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34361. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34362. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34363. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34364. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34365. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34366. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34367. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34368. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34369. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34370. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34371. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34372. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34373. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34374. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34375. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34376. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34377. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34378. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34379. /* 1 bit random value must have no more than one top bit set. */
  34380. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34381. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34382. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34383. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34384. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34385. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34386. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34387. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34388. /* END Invalid parameters. */
  34389. /* 0 bit random: 0. */
  34390. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34391. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34392. ExpectIntEQ(BN_is_zero(bn), 1);
  34393. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  34394. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34395. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34396. ExpectIntEQ(BN_is_zero(bn), 1);
  34397. /* 1 bit random: 0 or 1. */
  34398. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  34399. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34400. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  34401. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34402. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34403. ExpectIntEQ(BN_get_word(bn), 1);
  34404. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34405. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34406. ExpectIntEQ(BN_get_word(bn), 1);
  34407. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  34408. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34409. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  34410. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34411. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34412. ExpectIntEQ(BN_get_word(bn), 1);
  34413. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34414. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34415. ExpectIntEQ(BN_get_word(bn), 1);
  34416. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  34417. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34418. ExpectIntEQ(BN_num_bits(bn), 8);
  34419. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  34420. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  34421. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34422. ExpectIntEQ(BN_num_bits(bn), 8);
  34423. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  34424. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  34425. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34426. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  34427. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  34428. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  34429. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34430. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  34431. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  34432. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  34433. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34434. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  34435. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  34436. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34437. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  34438. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  34439. * requested number of bits up to the nearest multiple of 8. E.g. in this
  34440. * case, requesting a 13-bit random number would actually return a 16-bit
  34441. * random number. */
  34442. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  34443. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34444. ExpectIntEQ(BN_num_bits(bn), 13);
  34445. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  34446. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34447. ExpectIntEQ(BN_rand_range(bn, range), 1);
  34448. ExpectIntEQ(BN_set_word(range, 0), 1);
  34449. ExpectIntEQ(BN_rand_range(bn, range), 1);
  34450. ExpectIntEQ(BN_set_word(range, 1), 1);
  34451. ExpectIntEQ(BN_rand_range(bn, range), 1);
  34452. BN_free(bn);
  34453. BN_free(range);
  34454. #endif
  34455. return EXPECT_RESULT();
  34456. }
  34457. static int test_wolfSSL_BN_prime(void)
  34458. {
  34459. EXPECT_DECLS;
  34460. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34461. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34462. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  34463. BIGNUM* a = NULL;
  34464. BIGNUM* add = NULL;
  34465. BIGNUM* rem = NULL;
  34466. BIGNUM emptyBN;
  34467. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34468. ExpectNotNull(a = BN_new());
  34469. ExpectNotNull(add = BN_new());
  34470. ExpectNotNull(rem = BN_new());
  34471. /* Invalid parameters. */
  34472. /* BN_generate_prime_ex()
  34473. * prime - must have valid BIGNUM
  34474. * bits - Greater then 0
  34475. * safe - not supported, must be 0
  34476. * add - not supported, must be NULL
  34477. * rem - not supported, must be NULL
  34478. * cb - anything
  34479. */
  34480. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  34481. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  34482. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  34483. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  34484. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  34485. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  34486. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  34487. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  34488. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  34489. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  34490. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  34491. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  34492. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  34493. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  34494. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  34495. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  34496. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  34497. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  34498. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  34499. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  34500. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  34501. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  34502. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  34503. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  34504. /* END Invalid parameters. */
  34505. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  34506. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  34507. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34508. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  34509. BN_free(rem);
  34510. BN_free(add);
  34511. BN_free(a);
  34512. #endif
  34513. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34514. return EXPECT_RESULT();
  34515. }
  34516. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34517. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34518. #define TEST_ARG 0x1234
  34519. static void msg_cb(int write_p, int version, int content_type,
  34520. const void *buf, size_t len, SSL *ssl, void *arg)
  34521. {
  34522. (void)write_p;
  34523. (void)version;
  34524. (void)content_type;
  34525. (void)buf;
  34526. (void)len;
  34527. (void)ssl;
  34528. AssertTrue(arg == (void*)TEST_ARG);
  34529. }
  34530. #endif
  34531. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  34532. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34533. #if defined(SESSION_CERTS)
  34534. #include "wolfssl/internal.h"
  34535. #endif
  34536. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  34537. {
  34538. EXPECT_DECLS;
  34539. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  34540. STACK_OF(X509)* sk = NULL;
  34541. X509* x509 = NULL;
  34542. int i, num;
  34543. BIO* bio = NULL;
  34544. #endif
  34545. ExpectNotNull(ctx);
  34546. ExpectNotNull(ssl);
  34547. fprintf(stderr, "\n===== msgcb called ====\n");
  34548. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  34549. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  34550. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  34551. ExpectNotNull(SSL_get0_verified_chain(ssl));
  34552. #endif
  34553. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  34554. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  34555. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  34556. if (sk == NULL) {
  34557. BIO_free(bio);
  34558. return TEST_FAIL;
  34559. }
  34560. num = sk_X509_num(sk);
  34561. ExpectTrue(num > 0);
  34562. for (i = 0; i < num; i++) {
  34563. ExpectNotNull(x509 = sk_X509_value(sk,i));
  34564. if (x509 == NULL)
  34565. break;
  34566. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  34567. X509_print(bio,x509);
  34568. fprintf(stderr, "\n\n");
  34569. }
  34570. BIO_free(bio);
  34571. #endif
  34572. return EXPECT_RESULT();
  34573. }
  34574. #endif
  34575. static int test_wolfSSL_msgCb(void)
  34576. {
  34577. EXPECT_DECLS;
  34578. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  34579. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34580. test_ssl_cbf client_cb;
  34581. test_ssl_cbf server_cb;
  34582. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34583. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34584. #ifndef WOLFSSL_NO_TLS12
  34585. client_cb.method = wolfTLSv1_2_client_method;
  34586. server_cb.method = wolfTLSv1_2_server_method;
  34587. #else
  34588. client_cb.method = wolfTLSv1_3_client_method;
  34589. server_cb.method = wolfTLSv1_3_server_method;
  34590. #endif
  34591. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34592. &server_cb, msgCb), TEST_SUCCESS);
  34593. #endif
  34594. return EXPECT_RESULT();
  34595. }
  34596. static int test_wolfSSL_either_side(void)
  34597. {
  34598. EXPECT_DECLS;
  34599. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  34600. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34601. test_ssl_cbf client_cb;
  34602. test_ssl_cbf server_cb;
  34603. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34604. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34605. /* Use different CTX for client and server */
  34606. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  34607. ExpectNotNull(client_cb.ctx);
  34608. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  34609. ExpectNotNull(server_cb.ctx);
  34610. /* we are responsible for free'ing WOLFSSL_CTX */
  34611. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  34612. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34613. &server_cb, NULL), TEST_SUCCESS);
  34614. wolfSSL_CTX_free(client_cb.ctx);
  34615. wolfSSL_CTX_free(server_cb.ctx);
  34616. #endif
  34617. return EXPECT_RESULT();
  34618. }
  34619. static int test_wolfSSL_DTLS_either_side(void)
  34620. {
  34621. EXPECT_DECLS;
  34622. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  34623. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  34624. test_ssl_cbf client_cb;
  34625. test_ssl_cbf server_cb;
  34626. XMEMSET(&client_cb, 0, sizeof(client_cb));
  34627. XMEMSET(&server_cb, 0, sizeof(server_cb));
  34628. /* Use different CTX for client and server */
  34629. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  34630. ExpectNotNull(client_cb.ctx);
  34631. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  34632. ExpectNotNull(server_cb.ctx);
  34633. /* we are responsible for free'ing WOLFSSL_CTX */
  34634. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  34635. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  34636. &server_cb, NULL), TEST_SUCCESS);
  34637. wolfSSL_CTX_free(client_cb.ctx);
  34638. wolfSSL_CTX_free(server_cb.ctx);
  34639. #endif
  34640. return EXPECT_RESULT();
  34641. }
  34642. static int test_generate_cookie(void)
  34643. {
  34644. EXPECT_DECLS;
  34645. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  34646. SSL_CTX* ctx = NULL;
  34647. SSL* ssl = NULL;
  34648. byte buf[FOURK_BUF] = {0};
  34649. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  34650. ExpectNotNull(ssl = SSL_new(ctx));
  34651. /* Test unconnected */
  34652. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  34653. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  34654. wolfSSL_SetCookieCtx(ssl, ctx);
  34655. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  34656. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  34657. SSL_free(ssl);
  34658. SSL_CTX_free(ctx);
  34659. #endif
  34660. return EXPECT_RESULT();
  34661. }
  34662. static int test_wolfSSL_set_options(void)
  34663. {
  34664. EXPECT_DECLS;
  34665. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34666. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34667. WOLFSSL* ssl = NULL;
  34668. WOLFSSL_CTX* ctx = NULL;
  34669. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  34670. char appData[] = "extra msg";
  34671. #endif
  34672. #ifdef OPENSSL_EXTRA
  34673. unsigned char protos[] = {
  34674. 7, 't', 'l', 's', '/', '1', '.', '2',
  34675. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  34676. };
  34677. unsigned int len = sizeof(protos);
  34678. void *arg = (void *)TEST_ARG;
  34679. #endif
  34680. #ifndef NO_WOLFSSL_SERVER
  34681. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34682. #else
  34683. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34684. #endif
  34685. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  34686. WOLFSSL_FILETYPE_PEM));
  34687. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34688. WOLFSSL_FILETYPE_PEM));
  34689. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  34690. == WOLFSSL_OP_NO_TLSv1);
  34691. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  34692. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  34693. WOLFSSL_OP_NO_SSLv2)), 0);
  34694. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  34695. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  34696. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  34697. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  34698. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  34699. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  34700. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  34701. WOLFSSL_OP_NO_COMPRESSION));
  34702. wolfSSL_CTX_free(ctx);
  34703. ctx = NULL;
  34704. #ifndef NO_WOLFSSL_SERVER
  34705. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34706. #else
  34707. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34708. #endif
  34709. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  34710. WOLFSSL_FILETYPE_PEM));
  34711. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34712. WOLFSSL_FILETYPE_PEM));
  34713. #ifdef OPENSSL_EXTRA
  34714. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  34715. #endif
  34716. ExpectNotNull(ssl = wolfSSL_new(ctx));
  34717. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  34718. #ifdef HAVE_EX_DATA
  34719. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  34720. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  34721. if (ssl != NULL) {
  34722. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  34723. appData, sizeof(appData)), 0);
  34724. }
  34725. #else
  34726. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  34727. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  34728. #endif
  34729. #endif
  34730. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  34731. WOLFSSL_OP_NO_TLSv1);
  34732. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  34733. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  34734. WOLFSSL_OP_NO_SSLv2)), 0);
  34735. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  34736. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  34737. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  34738. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  34739. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  34740. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  34741. #ifdef OPENSSL_EXTRA
  34742. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  34743. WOLFSSL_OP_NO_COMPRESSION));
  34744. #endif
  34745. #ifdef OPENSSL_EXTRA
  34746. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  34747. wolfSSL_set_msg_callback_arg(ssl, arg);
  34748. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  34749. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  34750. #else
  34751. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  34752. #endif
  34753. #endif
  34754. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  34755. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  34756. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  34757. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  34758. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  34759. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  34760. #else
  34761. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  34762. #endif
  34763. #endif /* HAVE_ALPN && !NO_BIO */
  34764. #endif
  34765. wolfSSL_free(ssl);
  34766. wolfSSL_CTX_free(ctx);
  34767. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34768. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34769. return EXPECT_RESULT();
  34770. }
  34771. static int test_wolfSSL_sk_SSL_CIPHER(void)
  34772. {
  34773. EXPECT_DECLS;
  34774. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34775. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34776. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34777. SSL* ssl = NULL;
  34778. SSL_CTX* ctx = NULL;
  34779. STACK_OF(SSL_CIPHER) *sk = NULL;
  34780. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  34781. #ifndef NO_WOLFSSL_SERVER
  34782. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34783. #else
  34784. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34785. #endif
  34786. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  34787. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34788. ExpectNotNull(ssl = SSL_new(ctx));
  34789. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  34790. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  34791. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  34792. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  34793. /* error case because connection has not been established yet */
  34794. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  34795. sk_SSL_CIPHER_free(dupSk);
  34796. /* sk is pointer to internal struct that should be free'd in SSL_free */
  34797. SSL_free(ssl);
  34798. SSL_CTX_free(ctx);
  34799. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34800. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34801. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34802. return EXPECT_RESULT();
  34803. }
  34804. static int test_wolfSSL_set1_curves_list(void)
  34805. {
  34806. EXPECT_DECLS;
  34807. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  34808. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34809. SSL* ssl = NULL;
  34810. SSL_CTX* ctx = NULL;
  34811. #ifndef NO_WOLFSSL_SERVER
  34812. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34813. #else
  34814. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34815. #endif
  34816. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  34817. SSL_FILETYPE_PEM));
  34818. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  34819. ExpectNotNull(ssl = SSL_new(ctx));
  34820. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  34821. #ifdef HAVE_ECC
  34822. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  34823. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  34824. #endif
  34825. #ifdef HAVE_CURVE25519
  34826. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  34827. #else
  34828. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  34829. #endif
  34830. #ifdef HAVE_CURVE448
  34831. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  34832. #else
  34833. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  34834. #endif
  34835. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  34836. #ifdef HAVE_ECC
  34837. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  34838. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  34839. #endif
  34840. #ifdef HAVE_CURVE25519
  34841. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  34842. #else
  34843. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  34844. #endif
  34845. #ifdef HAVE_CURVE448
  34846. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  34847. #else
  34848. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  34849. #endif
  34850. SSL_free(ssl);
  34851. SSL_CTX_free(ctx);
  34852. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34853. #endif
  34854. return EXPECT_RESULT();
  34855. }
  34856. static int test_wolfSSL_set1_sigalgs_list(void)
  34857. {
  34858. EXPECT_DECLS;
  34859. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  34860. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34861. SSL* ssl = NULL;
  34862. SSL_CTX* ctx = NULL;
  34863. #ifndef NO_WOLFSSL_SERVER
  34864. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34865. #else
  34866. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34867. #endif
  34868. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34869. SSL_FILETYPE_PEM));
  34870. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34871. ExpectNotNull(ssl = SSL_new(ctx));
  34872. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  34873. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  34874. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  34875. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  34876. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  34877. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  34878. #ifndef NO_RSA
  34879. #ifndef NO_SHA256
  34880. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  34881. WOLFSSL_FAILURE);
  34882. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  34883. WOLFSSL_FAILURE);
  34884. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  34885. WOLFSSL_SUCCESS);
  34886. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  34887. WOLFSSL_SUCCESS);
  34888. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  34889. WOLFSSL_FAILURE);
  34890. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  34891. WOLFSSL_FAILURE);
  34892. #ifdef WC_RSA_PSS
  34893. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  34894. WOLFSSL_SUCCESS);
  34895. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  34896. WOLFSSL_SUCCESS);
  34897. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  34898. WOLFSSL_SUCCESS);
  34899. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  34900. WOLFSSL_SUCCESS);
  34901. #endif
  34902. #ifdef WOLFSSL_SHA512
  34903. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34904. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  34905. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34906. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  34907. #elif defined(WOLFSSL_SHA384)
  34908. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34909. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  34910. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34911. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  34912. #endif
  34913. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  34914. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  34915. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  34916. WOLFSSL_FAILURE);
  34917. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  34918. WOLFSSL_FAILURE);
  34919. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  34920. WOLFSSL_FAILURE);
  34921. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  34922. WOLFSSL_FAILURE);
  34923. #endif
  34924. #endif
  34925. #ifdef HAVE_ECC
  34926. #ifndef NO_SHA256
  34927. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  34928. WOLFSSL_SUCCESS);
  34929. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  34930. WOLFSSL_SUCCESS);
  34931. #ifdef WOLFSSL_SHA512
  34932. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34933. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  34934. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34935. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  34936. #elif defined(WOLFSSL_SHA384)
  34937. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  34938. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  34939. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  34940. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  34941. #endif
  34942. #endif
  34943. #endif
  34944. #ifdef HAVE_ED25519
  34945. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  34946. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  34947. #endif
  34948. #ifdef HAVE_ED448
  34949. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  34950. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  34951. #endif
  34952. #ifndef NO_DSA
  34953. #ifndef NO_SHA256
  34954. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  34955. WOLFSSL_SUCCESS);
  34956. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  34957. WOLFSSL_SUCCESS);
  34958. #endif
  34959. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  34960. defined(WOLFSSL_ALLOW_TLS_SHA1))
  34961. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  34962. WOLFSSL_SUCCESS);
  34963. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  34964. WOLFSSL_SUCCESS);
  34965. #endif
  34966. #endif
  34967. SSL_free(ssl);
  34968. SSL_CTX_free(ctx);
  34969. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34970. #endif
  34971. return EXPECT_RESULT();
  34972. }
  34973. /* Testing wolfSSL_set_tlsext_status_type function.
  34974. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  34975. */
  34976. static int test_wolfSSL_set_tlsext_status_type(void)
  34977. {
  34978. EXPECT_DECLS;
  34979. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  34980. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  34981. SSL* ssl = NULL;
  34982. SSL_CTX* ctx = NULL;
  34983. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34984. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34985. SSL_FILETYPE_PEM));
  34986. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34987. ExpectNotNull(ssl = SSL_new(ctx));
  34988. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  34989. SSL_SUCCESS);
  34990. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  34991. SSL_free(ssl);
  34992. SSL_CTX_free(ctx);
  34993. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  34994. return EXPECT_RESULT();
  34995. }
  34996. #ifndef NO_BIO
  34997. static int test_wolfSSL_PEM_read_bio(void)
  34998. {
  34999. EXPECT_DECLS;
  35000. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35001. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35002. byte buff[6000];
  35003. XFILE f = XBADFILE;
  35004. int bytes;
  35005. X509* x509 = NULL;
  35006. BIO* bio = NULL;
  35007. BUF_MEM* buf = NULL;
  35008. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  35009. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  35010. if (f != XBADFILE)
  35011. XFCLOSE(f);
  35012. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35013. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35014. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35015. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35016. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35017. /* BIO should return the set EOF value */
  35018. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35019. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35020. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35021. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35022. BIO_free(bio);
  35023. BUF_MEM_free(buf);
  35024. X509_free(x509);
  35025. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  35026. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35027. return EXPECT_RESULT();
  35028. }
  35029. #if defined(OPENSSL_EXTRA)
  35030. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35031. long argl, long ret)
  35032. {
  35033. (void)bio;
  35034. (void)cmd;
  35035. (void)argp;
  35036. (void)argi;
  35037. (void)argl;
  35038. return ret;
  35039. }
  35040. #endif
  35041. static int test_wolfSSL_BIO(void)
  35042. {
  35043. EXPECT_DECLS;
  35044. #if defined(OPENSSL_EXTRA)
  35045. const unsigned char* p = NULL;
  35046. byte buff[20];
  35047. BIO* bio1 = NULL;
  35048. BIO* bio2 = NULL;
  35049. BIO* bio3 = NULL;
  35050. char* bufPt = NULL;
  35051. int i;
  35052. for (i = 0; i < 20; i++) {
  35053. buff[i] = i;
  35054. }
  35055. /* test BIO_free with NULL */
  35056. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35057. /* Creating and testing type BIO_s_bio */
  35058. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35059. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35060. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35061. /* read/write before set up */
  35062. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35063. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35064. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35065. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35066. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35067. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35068. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35069. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35070. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35071. /* write buffer full */
  35072. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35073. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35074. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35075. /* write the other direction with pair */
  35076. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35077. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35078. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35079. /* try read */
  35080. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35081. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35082. /* try read using ctrl function */
  35083. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35084. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35085. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35086. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35087. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35088. for (i = 0; i < 20; i++) {
  35089. ExpectIntEQ((int)bufPt[i], i);
  35090. }
  35091. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35092. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35093. for (i = 0; i < 8; i++) {
  35094. ExpectIntEQ((int)bufPt[i], i);
  35095. }
  35096. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  35097. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35098. /* new pair */
  35099. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35100. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35101. bio2 = NULL;
  35102. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35103. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35104. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  35105. /* test wrap around... */
  35106. ExpectIntEQ(BIO_reset(bio1), 0);
  35107. ExpectIntEQ(BIO_reset(bio3), 0);
  35108. /* fill write buffer, read only small amount then write again */
  35109. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35110. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35111. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35112. for (i = 0; i < 4; i++) {
  35113. ExpectIntEQ(bufPt[i], i);
  35114. }
  35115. /* try writing over read index */
  35116. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35117. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  35118. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35119. /* read and write 0 bytes */
  35120. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35121. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35122. /* should read only to end of write buffer then need to read again */
  35123. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35124. for (i = 0; i < 16; i++) {
  35125. ExpectIntEQ(bufPt[i], buff[4 + i]);
  35126. }
  35127. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35128. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35129. for (i = 0; i < 4; i++) {
  35130. ExpectIntEQ(bufPt[i], 0);
  35131. }
  35132. /* read index should not have advanced with nread0 */
  35133. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35134. for (i = 0; i < 4; i++) {
  35135. ExpectIntEQ(bufPt[i], 0);
  35136. }
  35137. /* write and fill up buffer checking reset of index state */
  35138. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35139. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35140. /* test reset on data in bio1 write buffer */
  35141. ExpectIntEQ(BIO_reset(bio1), 0);
  35142. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35143. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  35144. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35145. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35146. ExpectNotNull(p);
  35147. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35148. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35149. for (i = 0; i < 6; i++) {
  35150. ExpectIntEQ(bufPt[i], i);
  35151. }
  35152. /* test case of writing twice with offset read index */
  35153. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35154. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35155. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35156. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35157. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35158. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35159. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35160. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35161. BIO_free(bio1);
  35162. bio1 = NULL;
  35163. BIO_free(bio3);
  35164. bio3 = NULL;
  35165. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35166. {
  35167. BIO* bioA = NULL;
  35168. BIO* bioB = NULL;
  35169. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35170. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35171. BIO_free(bioA);
  35172. bioA = NULL;
  35173. BIO_free(bioB);
  35174. bioB = NULL;
  35175. }
  35176. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35177. /* BIOs with file pointers */
  35178. #if !defined(NO_FILESYSTEM)
  35179. {
  35180. XFILE f1 = XBADFILE;
  35181. XFILE f2 = XBADFILE;
  35182. BIO* f_bio1 = NULL;
  35183. BIO* f_bio2 = NULL;
  35184. unsigned char cert[300];
  35185. char testFile[] = "tests/bio_write_test.txt";
  35186. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35187. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35188. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35189. /* Failure due to wrong BIO type */
  35190. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35191. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35192. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  35193. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35194. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  35195. WOLFSSL_SUCCESS);
  35196. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35197. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  35198. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  35199. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  35200. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  35201. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  35202. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  35203. ExpectIntEQ(BIO_reset(f_bio2), 0);
  35204. ExpectIntEQ(BIO_tell(NULL),-1);
  35205. ExpectIntEQ(BIO_tell(f_bio2),0);
  35206. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  35207. ExpectIntEQ(BIO_tell(f_bio2),4);
  35208. BIO_free(f_bio1);
  35209. f_bio1 = NULL;
  35210. BIO_free(f_bio2);
  35211. f_bio2 = NULL;
  35212. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  35213. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35214. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35215. BIO_free(f_bio1);
  35216. f_bio1 = NULL;
  35217. }
  35218. #endif /* !defined(NO_FILESYSTEM) */
  35219. /* BIO info callback */
  35220. {
  35221. const char* testArg = "test";
  35222. BIO* cb_bio = NULL;
  35223. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  35224. BIO_set_callback(cb_bio, bioCallback);
  35225. ExpectNotNull(BIO_get_callback(cb_bio));
  35226. BIO_set_callback(cb_bio, NULL);
  35227. ExpectNull(BIO_get_callback(cb_bio));
  35228. BIO_set_callback_arg(cb_bio, (char*)testArg);
  35229. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  35230. ExpectNull(BIO_get_callback_arg(NULL));
  35231. BIO_free(cb_bio);
  35232. cb_bio = NULL;
  35233. }
  35234. /* BIO_vfree */
  35235. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35236. BIO_vfree(NULL);
  35237. BIO_vfree(bio1);
  35238. #endif
  35239. return EXPECT_RESULT();
  35240. }
  35241. static int test_wolfSSL_BIO_BIO_ring_read(void)
  35242. {
  35243. EXPECT_DECLS;
  35244. #if defined(OPENSSL_ALL)
  35245. BIO* bio1 = NULL;
  35246. BIO* bio2 = NULL;
  35247. byte data[50];
  35248. byte tmp[50];
  35249. XMEMSET(data, 42, sizeof(data));
  35250. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  35251. SSL_SUCCESS);
  35252. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  35253. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  35254. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  35255. ExpectBufEQ(tmp, data, 20);
  35256. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  35257. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  35258. ExpectBufEQ(tmp, data, 40);
  35259. BIO_free(bio1);
  35260. BIO_free(bio2);
  35261. #endif
  35262. return EXPECT_RESULT();
  35263. }
  35264. #endif /* !NO_BIO */
  35265. static int test_wolfSSL_a2i_IPADDRESS(void)
  35266. {
  35267. EXPECT_DECLS;
  35268. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  35269. const unsigned char* data = NULL;
  35270. int dataSz = 0;
  35271. ASN1_OCTET_STRING *st = NULL;
  35272. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  35273. const unsigned char ipv6_exp[] = {
  35274. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35275. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  35276. };
  35277. const unsigned char ipv6_home[] = {
  35278. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35279. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  35280. };
  35281. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  35282. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  35283. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35284. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  35285. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  35286. ASN1_STRING_free(st);
  35287. st = NULL;
  35288. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  35289. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35290. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35291. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  35292. ASN1_STRING_free(st);
  35293. st = NULL;
  35294. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  35295. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35296. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35297. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  35298. ASN1_STRING_free(st);
  35299. #endif
  35300. return EXPECT_RESULT();
  35301. }
  35302. static int test_wolfSSL_X509_cmp_time(void)
  35303. {
  35304. EXPECT_DECLS;
  35305. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  35306. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  35307. WOLFSSL_ASN1_TIME asn_time;
  35308. time_t t;
  35309. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  35310. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  35311. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  35312. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  35313. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  35314. #endif
  35315. return EXPECT_RESULT();
  35316. }
  35317. static int test_wolfSSL_X509_time_adj(void)
  35318. {
  35319. EXPECT_DECLS;
  35320. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  35321. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  35322. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  35323. !defined(NO_ASN_TIME)
  35324. X509* x509 = NULL;
  35325. time_t t;
  35326. time_t not_before;
  35327. time_t not_after;
  35328. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  35329. client_cert_der_2048, sizeof_client_cert_der_2048,
  35330. WOLFSSL_FILETYPE_ASN1));
  35331. t = 0;
  35332. not_before = wc_Time(0);
  35333. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  35334. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  35335. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  35336. /* Check X509_gmtime_adj, too. */
  35337. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  35338. X509_free(x509);
  35339. #endif
  35340. return EXPECT_RESULT();
  35341. }
  35342. static int test_wolfSSL_X509(void)
  35343. {
  35344. EXPECT_DECLS;
  35345. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35346. !defined(NO_RSA)
  35347. X509* x509 = NULL;
  35348. #ifndef NO_BIO
  35349. BIO* bio = NULL;
  35350. X509_STORE_CTX* ctx = NULL;
  35351. X509_STORE* store = NULL;
  35352. #endif
  35353. char der[] = "certs/ca-cert.der";
  35354. XFILE fp = XBADFILE;
  35355. ExpectNotNull(x509 = X509_new());
  35356. X509_free(x509);
  35357. x509 = NULL;
  35358. #ifndef NO_BIO
  35359. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  35360. SSL_FILETYPE_PEM));
  35361. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35362. #ifdef WOLFSSL_CERT_GEN
  35363. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  35364. #endif
  35365. ExpectNotNull(ctx = X509_STORE_CTX_new());
  35366. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  35367. ExpectNotNull(store = X509_STORE_new());
  35368. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  35369. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  35370. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  35371. X509_STORE_CTX_free(ctx);
  35372. X509_STORE_free(store);
  35373. X509_free(x509);
  35374. x509 = NULL;
  35375. BIO_free(bio);
  35376. #endif
  35377. /** d2i_X509_fp test **/
  35378. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  35379. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  35380. ExpectNotNull(x509);
  35381. X509_free(x509);
  35382. x509 = NULL;
  35383. if (fp != XBADFILE) {
  35384. XFCLOSE(fp);
  35385. fp = XBADFILE;
  35386. }
  35387. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  35388. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  35389. ExpectNotNull(x509);
  35390. X509_free(x509);
  35391. if (fp != XBADFILE)
  35392. XFCLOSE(fp);
  35393. /* X509_up_ref test */
  35394. ExpectIntEQ(X509_up_ref(NULL), 0);
  35395. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  35396. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  35397. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  35398. X509_free(x509); /* refCount = 2 */
  35399. X509_free(x509); /* refCount = 1 */
  35400. X509_free(x509); /* refCount = 0, free */
  35401. #endif
  35402. return EXPECT_RESULT();
  35403. }
  35404. static int test_wolfSSL_X509_get_ext_count(void)
  35405. {
  35406. EXPECT_DECLS;
  35407. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35408. !defined(NO_RSA)
  35409. int ret = 0;
  35410. WOLFSSL_X509* x509 = NULL;
  35411. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  35412. XFILE f = XBADFILE;
  35413. /* NULL parameter check */
  35414. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  35415. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  35416. SSL_FILETYPE_PEM));
  35417. ExpectIntEQ(X509_get_ext_count(x509), 5);
  35418. wolfSSL_X509_free(x509);
  35419. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  35420. SSL_FILETYPE_PEM));
  35421. ExpectIntEQ(X509_get_ext_count(x509), 5);
  35422. wolfSSL_X509_free(x509);
  35423. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  35424. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35425. if (f != XBADFILE)
  35426. XFCLOSE(f);
  35427. /* wolfSSL_X509_get_ext_count() valid input */
  35428. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  35429. /* wolfSSL_X509_get_ext_count() NULL argument */
  35430. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  35431. wolfSSL_X509_free(x509);
  35432. #endif
  35433. return EXPECT_RESULT();
  35434. }
  35435. static int test_wolfSSL_X509_sign2(void)
  35436. {
  35437. EXPECT_DECLS;
  35438. /* test requires WOLFSSL_AKID_NAME to match expected output */
  35439. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  35440. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  35441. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  35442. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  35443. defined(WOLFSSL_IP_ALT_NAME))
  35444. WOLFSSL_X509 *x509 = NULL;
  35445. WOLFSSL_X509 *ca = NULL;
  35446. const unsigned char *der = NULL;
  35447. const unsigned char *pt = NULL;
  35448. WOLFSSL_EVP_PKEY *priv = NULL;
  35449. WOLFSSL_X509_NAME *name = NULL;
  35450. int derSz;
  35451. #ifndef NO_ASN_TIME
  35452. WOLFSSL_ASN1_TIME *notBefore = NULL;
  35453. WOLFSSL_ASN1_TIME *notAfter = NULL;
  35454. const int year = 365*24*60*60;
  35455. const int day = 24*60*60;
  35456. const int hour = 60*60;
  35457. const int mini = 60;
  35458. time_t t;
  35459. #endif
  35460. const unsigned char expected[] = {
  35461. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  35462. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  35463. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  35464. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  35465. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  35466. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  35467. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  35468. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  35469. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  35470. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  35471. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  35472. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  35473. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  35474. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  35475. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35476. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  35477. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  35478. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  35479. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  35480. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  35481. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  35482. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  35483. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  35484. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  35485. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  35486. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  35487. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  35488. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  35489. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  35490. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  35491. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  35492. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  35493. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  35494. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35495. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  35496. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  35497. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  35498. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  35499. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  35500. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  35501. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  35502. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  35503. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  35504. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  35505. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  35506. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  35507. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  35508. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  35509. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  35510. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  35511. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  35512. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  35513. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  35514. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  35515. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  35516. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  35517. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  35518. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  35519. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  35520. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  35521. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  35522. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  35523. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  35524. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  35525. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  35526. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  35527. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  35528. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  35529. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  35530. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  35531. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  35532. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  35533. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  35534. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  35535. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  35536. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  35537. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  35538. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  35539. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  35540. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  35541. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  35542. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  35543. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  35544. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  35545. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  35546. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  35547. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  35548. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  35549. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  35550. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  35551. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  35552. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  35553. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  35554. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  35555. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  35556. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  35557. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  35558. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  35559. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  35560. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  35561. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  35562. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  35563. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  35564. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  35565. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  35566. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  35567. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  35568. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  35569. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  35570. };
  35571. pt = ca_key_der_2048;
  35572. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  35573. sizeof_ca_key_der_2048));
  35574. pt = client_cert_der_2048;
  35575. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  35576. sizeof_client_cert_der_2048));
  35577. pt = ca_cert_der_2048;
  35578. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  35579. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  35580. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  35581. #ifndef NO_ASN_TIME
  35582. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  35583. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  35584. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  35585. ExpectIntEQ(notAfter->length, 13);
  35586. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  35587. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  35588. #endif
  35589. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  35590. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  35591. ExpectIntEQ(derSz, sizeof(expected));
  35592. #ifndef NO_ASN_TIME
  35593. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  35594. #endif
  35595. wolfSSL_X509_free(ca);
  35596. wolfSSL_X509_free(x509);
  35597. wolfSSL_EVP_PKEY_free(priv);
  35598. #ifndef NO_ASN_TIME
  35599. wolfSSL_ASN1_TIME_free(notBefore);
  35600. wolfSSL_ASN1_TIME_free(notAfter);
  35601. #endif
  35602. #endif
  35603. return EXPECT_RESULT();
  35604. }
  35605. static int test_wolfSSL_X509_sign(void)
  35606. {
  35607. EXPECT_DECLS;
  35608. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  35609. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  35610. int ret = 0;
  35611. char *cn = NULL;
  35612. word32 cnSz = 0;
  35613. X509_NAME *name = NULL;
  35614. X509 *x509 = NULL;
  35615. X509 *ca = NULL;
  35616. DecodedCert dCert;
  35617. EVP_PKEY *pub = NULL;
  35618. EVP_PKEY *priv = NULL;
  35619. EVP_MD_CTX *mctx = NULL;
  35620. #if defined(USE_CERT_BUFFERS_1024)
  35621. const unsigned char* rsaPriv = client_key_der_1024;
  35622. const unsigned char* rsaPub = client_keypub_der_1024;
  35623. const unsigned char* certIssuer = client_cert_der_1024;
  35624. long clientKeySz = (long)sizeof_client_key_der_1024;
  35625. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  35626. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  35627. #elif defined(USE_CERT_BUFFERS_2048)
  35628. const unsigned char* rsaPriv = client_key_der_2048;
  35629. const unsigned char* rsaPub = client_keypub_der_2048;
  35630. const unsigned char* certIssuer = client_cert_der_2048;
  35631. long clientKeySz = (long)sizeof_client_key_der_2048;
  35632. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  35633. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  35634. #endif
  35635. byte sn[16];
  35636. int snSz = sizeof(sn);
  35637. /* Set X509_NAME fields */
  35638. ExpectNotNull(name = X509_NAME_new());
  35639. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  35640. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  35641. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  35642. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  35643. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  35644. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  35645. /* Get private and public keys */
  35646. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  35647. clientKeySz));
  35648. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  35649. ExpectNotNull(x509 = X509_new());
  35650. /* Set version 3 */
  35651. ExpectIntNE(X509_set_version(x509, 2L), 0);
  35652. /* Set subject name, add pubkey, and sign certificate */
  35653. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  35654. X509_NAME_free(name);
  35655. name = NULL;
  35656. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  35657. #ifdef WOLFSSL_ALT_NAMES
  35658. /* Add some subject alt names */
  35659. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  35660. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  35661. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35662. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  35663. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35664. "sphygmomanometer",
  35665. ASN_DNS_TYPE), SSL_SUCCESS);
  35666. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35667. "supercalifragilisticexpialidocious",
  35668. ASN_DNS_TYPE), SSL_SUCCESS);
  35669. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  35670. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  35671. ASN_DNS_TYPE), SSL_SUCCESS);
  35672. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35673. {
  35674. unsigned char ip4_type[] = {127,128,0,255};
  35675. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  35676. 0xff, 0xee, 0x99, 0x88,
  35677. 0x77, 0x66, 0x55, 0x44,
  35678. 0x00, 0x33, 0x22, 0x11};
  35679. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  35680. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  35681. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  35682. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  35683. }
  35684. #endif
  35685. #endif /* WOLFSSL_ALT_NAMES */
  35686. /* test valid sign case */
  35687. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  35688. /* test valid X509_sign_ctx case */
  35689. ExpectNotNull(mctx = EVP_MD_CTX_new());
  35690. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  35691. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  35692. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  35693. ExpectIntEQ(X509_get_ext_count(x509), 1);
  35694. #endif
  35695. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  35696. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  35697. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  35698. #endif
  35699. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  35700. WOLFSSL_SUCCESS);
  35701. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  35702. /* Variation in size depends on ASN.1 encoding when MSB is set.
  35703. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  35704. * with the MSB set. See GenerateInteger in asn.c */
  35705. #ifndef USE_CERT_BUFFERS_1024
  35706. #ifndef WOLFSSL_ALT_NAMES
  35707. /* Valid case - size should be 781-786 with 16 byte serial number */
  35708. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  35709. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35710. /* Valid case - size should be 955-960 with 16 byte serial number */
  35711. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  35712. #else
  35713. /* Valid case - size should be 926-931 with 16 byte serial number */
  35714. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  35715. #endif
  35716. #else
  35717. #ifndef WOLFSSL_ALT_NAMES
  35718. /* Valid case - size should be 537-542 with 16 byte serial number */
  35719. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  35720. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  35721. /* Valid case - size should be 695-670 with 16 byte serial number */
  35722. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  35723. #else
  35724. /* Valid case - size should be 666-671 with 16 byte serial number */
  35725. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  35726. #endif
  35727. #endif
  35728. /* check that issuer name is as expected after signature */
  35729. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  35730. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  35731. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  35732. ExpectNotNull(name = X509_get_subject_name(ca));
  35733. cnSz = X509_NAME_get_sz(name);
  35734. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  35735. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  35736. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  35737. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  35738. cn = NULL;
  35739. #ifdef WOLFSSL_MULTI_ATTRIB
  35740. /* test adding multiple OU's to the signer */
  35741. ExpectNotNull(name = X509_get_subject_name(ca));
  35742. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  35743. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  35744. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  35745. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  35746. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  35747. #endif
  35748. ExpectNotNull(name = X509_get_subject_name(ca));
  35749. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  35750. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  35751. ExpectNotNull(name = X509_get_issuer_name(x509));
  35752. cnSz = X509_NAME_get_sz(name);
  35753. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  35754. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  35755. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  35756. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  35757. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  35758. cn = NULL;
  35759. FreeDecodedCert(&dCert);
  35760. /* Test invalid parameters */
  35761. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  35762. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  35763. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  35764. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  35765. EVP_MD_CTX_free(mctx);
  35766. mctx = NULL;
  35767. ExpectNotNull(mctx = EVP_MD_CTX_new());
  35768. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  35769. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  35770. /* test invalid version number */
  35771. #if defined(OPENSSL_ALL)
  35772. ExpectIntNE(X509_set_version(x509, 6L), 0);
  35773. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  35774. /* uses ParseCert which fails on bad version number */
  35775. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  35776. #endif
  35777. EVP_MD_CTX_free(mctx);
  35778. EVP_PKEY_free(priv);
  35779. EVP_PKEY_free(pub);
  35780. X509_free(x509);
  35781. X509_free(ca);
  35782. #endif
  35783. return EXPECT_RESULT();
  35784. }
  35785. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  35786. {
  35787. EXPECT_DECLS;
  35788. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  35789. X509* x509 = NULL;
  35790. const X509_ALGOR* alg;
  35791. ExpectNotNull(x509 = X509_new());
  35792. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  35793. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  35794. X509_free(x509);
  35795. #endif
  35796. return EXPECT_RESULT();
  35797. }
  35798. static int test_wolfSSL_X509_ALGOR_get0(void)
  35799. {
  35800. EXPECT_DECLS;
  35801. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35802. !defined(NO_SHA256) && !defined(NO_RSA)
  35803. X509* x509 = NULL;
  35804. const ASN1_OBJECT* obj = NULL;
  35805. const X509_ALGOR* alg = NULL;
  35806. int pptype = 0;
  35807. const void *ppval = NULL;
  35808. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  35809. SSL_FILETYPE_PEM));
  35810. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  35811. /* Invalid case */
  35812. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  35813. ExpectNull(obj);
  35814. /* Valid case */
  35815. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  35816. ExpectNotNull(obj);
  35817. ExpectNull(ppval);
  35818. ExpectIntNE(pptype, 0);
  35819. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  35820. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  35821. X509_free(x509);
  35822. #endif
  35823. return EXPECT_RESULT();
  35824. }
  35825. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  35826. {
  35827. EXPECT_DECLS;
  35828. #if defined(OPENSSL_EXTRA)
  35829. X509_VERIFY_PARAM *paramTo = NULL;
  35830. X509_VERIFY_PARAM *paramFrom = NULL;
  35831. char testIPv4[] = "127.0.0.1";
  35832. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  35833. char testhostName1[] = "foo.hoge.com";
  35834. char testhostName2[] = "foobar.hoge.com";
  35835. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  35836. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  35837. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  35838. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  35839. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  35840. (int)XSTRLEN(testhostName1)), 1);
  35841. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  35842. (int)XSTRLEN(testhostName1)));
  35843. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  35844. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  35845. ExpectIntEQ(0x01, paramFrom->hostFlags);
  35846. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  35847. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  35848. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  35849. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  35850. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  35851. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35852. /* null pointer */
  35853. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  35854. /* in the case of "from" null, returns success */
  35855. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  35856. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  35857. /* inherit flags test : VPARAM_DEFAULT */
  35858. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35859. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35860. (int)XSTRLEN(testhostName1)));
  35861. ExpectIntEQ(0x01, paramTo->hostFlags);
  35862. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35863. /* inherit flags test : VPARAM OVERWRITE */
  35864. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35865. (int)XSTRLEN(testhostName2)), 1);
  35866. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35867. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  35868. if (paramTo != NULL) {
  35869. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  35870. }
  35871. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35872. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35873. (int)XSTRLEN(testhostName1)));
  35874. ExpectIntEQ(0x01, paramTo->hostFlags);
  35875. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35876. /* inherit flags test : VPARAM_RESET_FLAGS */
  35877. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35878. (int)XSTRLEN(testhostName2)), 1);
  35879. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35880. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  35881. if (paramTo != NULL) {
  35882. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  35883. }
  35884. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35885. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  35886. (int)XSTRLEN(testhostName1)));
  35887. ExpectIntEQ(0x01, paramTo->hostFlags);
  35888. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  35889. /* inherit flags test : VPARAM_LOCKED */
  35890. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  35891. (int)XSTRLEN(testhostName2)), 1);
  35892. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  35893. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  35894. if (paramTo != NULL) {
  35895. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  35896. }
  35897. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  35898. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  35899. (int)XSTRLEN(testhostName2)));
  35900. ExpectIntEQ(0x00, paramTo->hostFlags);
  35901. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  35902. /* test for incorrect parameters */
  35903. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  35904. 0);
  35905. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  35906. /* inherit flags test : VPARAM_ONCE, not testable yet */
  35907. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  35908. 1);
  35909. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  35910. X509_V_FLAG_CRL_CHECK_ALL);
  35911. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  35912. X509_V_FLAG_CRL_CHECK_ALL), 1);
  35913. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  35914. X509_VERIFY_PARAM_free(paramTo);
  35915. X509_VERIFY_PARAM_free(paramFrom);
  35916. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  35917. #endif
  35918. return EXPECT_RESULT();
  35919. }
  35920. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35921. static int test_wolfSSL_check_domain_verify_count = 0;
  35922. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  35923. WOLFSSL_X509_STORE_CTX* store)
  35924. {
  35925. EXPECT_DECLS;
  35926. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  35927. ExpectIntEQ(preverify, 1);
  35928. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  35929. return EXPECT_SUCCESS();
  35930. }
  35931. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  35932. {
  35933. EXPECT_DECLS;
  35934. X509_VERIFY_PARAM *param = NULL;
  35935. ExpectNotNull(param = SSL_get0_param(ssl));
  35936. /* Domain check should only be done on the leaf cert */
  35937. X509_VERIFY_PARAM_set_hostflags(param,
  35938. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  35939. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  35940. "wolfSSL Server Chain", 0), 1);
  35941. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  35942. test_wolfSSL_check_domain_verify_cb);
  35943. return EXPECT_RESULT();
  35944. }
  35945. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  35946. {
  35947. EXPECT_DECLS;
  35948. /* Use a cert with different domains in chain */
  35949. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  35950. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  35951. return EXPECT_RESULT();
  35952. }
  35953. static int test_wolfSSL_check_domain(void)
  35954. {
  35955. EXPECT_DECLS;
  35956. test_ssl_cbf func_cb_client;
  35957. test_ssl_cbf func_cb_server;
  35958. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35959. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35960. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  35961. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  35962. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35963. &func_cb_server, NULL), TEST_SUCCESS);
  35964. /* Should have been called once for each cert in sent chain */
  35965. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  35966. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  35967. #else
  35968. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  35969. #endif
  35970. return EXPECT_RESULT();
  35971. }
  35972. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  35973. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  35974. {
  35975. EXPECT_DECLS;
  35976. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  35977. X509* x509 = NULL;
  35978. X509_PUBKEY* pubKey;
  35979. ExpectNotNull(x509 = X509_new());
  35980. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  35981. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  35982. X509_free(x509);
  35983. #endif
  35984. return EXPECT_RESULT();
  35985. }
  35986. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  35987. {
  35988. EXPECT_DECLS;
  35989. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35990. !defined(NO_SHA256) && !defined(NO_RSA)
  35991. X509* x509 = NULL;
  35992. ASN1_OBJECT* obj = NULL;
  35993. const ASN1_OBJECT* pa_oid = NULL;
  35994. X509_PUBKEY* pubKey = NULL;
  35995. X509_PUBKEY* pubKey2 = NULL;
  35996. EVP_PKEY* evpKey = NULL;
  35997. const unsigned char *pk = NULL;
  35998. int ppklen;
  35999. int pptype;
  36000. X509_ALGOR *pa = NULL;
  36001. const void *pval;
  36002. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  36003. SSL_FILETYPE_PEM));
  36004. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36005. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36006. ExpectNotNull(pk);
  36007. ExpectNotNull(pa);
  36008. ExpectNotNull(pubKey);
  36009. ExpectIntGT(ppklen, 0);
  36010. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  36011. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36012. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36013. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36014. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36015. ExpectNotNull(pk);
  36016. ExpectNotNull(pa);
  36017. ExpectIntGT(ppklen, 0);
  36018. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36019. ExpectNotNull(pa_oid);
  36020. ExpectNull(pval);
  36021. ExpectIntEQ(pptype, V_ASN1_NULL);
  36022. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  36023. X509_PUBKEY_free(pubKey2);
  36024. X509_free(x509);
  36025. EVP_PKEY_free(evpKey);
  36026. #endif
  36027. return EXPECT_RESULT();
  36028. }
  36029. static int test_wolfSSL_X509_PUBKEY_EC(void)
  36030. {
  36031. EXPECT_DECLS;
  36032. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36033. X509* x509 = NULL;
  36034. ASN1_OBJECT* obj = NULL;
  36035. ASN1_OBJECT* poid = NULL;
  36036. const ASN1_OBJECT* pa_oid = NULL;
  36037. X509_PUBKEY* pubKey = NULL;
  36038. X509_PUBKEY* pubKey2 = NULL;
  36039. EVP_PKEY* evpKey = NULL;
  36040. const unsigned char *pk = NULL;
  36041. int ppklen;
  36042. int pptype;
  36043. X509_ALGOR *pa = NULL;
  36044. const void *pval;
  36045. char buf[50];
  36046. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36047. SSL_FILETYPE_PEM));
  36048. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36049. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36050. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36051. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36052. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36053. ExpectNotNull(pk);
  36054. ExpectNotNull(pa);
  36055. ExpectIntGT(ppklen, 0);
  36056. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36057. ExpectNotNull(pa_oid);
  36058. ExpectNotNull(pval);
  36059. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  36060. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36061. poid = (ASN1_OBJECT *)pval;
  36062. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36063. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36064. X509_PUBKEY_free(pubKey2);
  36065. X509_free(x509);
  36066. EVP_PKEY_free(evpKey);
  36067. #endif
  36068. return EXPECT_RESULT();
  36069. }
  36070. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36071. {
  36072. EXPECT_DECLS;
  36073. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36074. word32 bytes;
  36075. #ifdef USE_CERT_BUFFERS_1024
  36076. byte tmp[ONEK_BUF];
  36077. #elif defined(USE_CERT_BUFFERS_2048)
  36078. byte tmp[TWOK_BUF];
  36079. #else
  36080. byte tmp[TWOK_BUF];
  36081. #endif /* END USE_CERT_BUFFERS_1024 */
  36082. const unsigned char* dsaKeyDer = tmp;
  36083. ASN1_OBJECT* obj = NULL;
  36084. ASN1_STRING* str;
  36085. const ASN1_OBJECT* pa_oid = NULL;
  36086. X509_PUBKEY* pubKey = NULL;
  36087. EVP_PKEY* evpKey = NULL;
  36088. const unsigned char *pk = NULL;
  36089. int ppklen, pptype;
  36090. X509_ALGOR *pa = NULL;
  36091. const void *pval;
  36092. #ifdef USE_CERT_BUFFERS_1024
  36093. XMEMSET(tmp, 0, sizeof(tmp));
  36094. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36095. bytes = sizeof_dsa_key_der_1024;
  36096. #elif defined(USE_CERT_BUFFERS_2048)
  36097. XMEMSET(tmp, 0, sizeof(tmp));
  36098. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36099. bytes = sizeof_dsa_key_der_2048;
  36100. #else
  36101. {
  36102. XFILE fp = XBADFILE;
  36103. XMEMSET(tmp, 0, sizeof(tmp));
  36104. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  36105. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  36106. if (fp != XBADFILE)
  36107. XFCLOSE(fp);
  36108. }
  36109. #endif
  36110. /* Initialize pkey with der format dsa key */
  36111. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  36112. ExpectNotNull(pubKey = X509_PUBKEY_new());
  36113. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  36114. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36115. ExpectNotNull(pk);
  36116. ExpectNotNull(pa);
  36117. ExpectIntGT(ppklen, 0);
  36118. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36119. ExpectNotNull(pa_oid);
  36120. ExpectNotNull(pval);
  36121. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  36122. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  36123. str = (ASN1_STRING *)pval;
  36124. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  36125. #ifdef USE_CERT_BUFFERS_1024
  36126. ExpectIntEQ(ASN1_STRING_length(str), 291);
  36127. #else
  36128. ExpectIntEQ(ASN1_STRING_length(str), 549);
  36129. #endif /* END USE_CERT_BUFFERS_1024 */
  36130. X509_PUBKEY_free(pubKey);
  36131. EVP_PKEY_free(evpKey);
  36132. #endif
  36133. return EXPECT_RESULT();
  36134. }
  36135. static int test_wolfSSL_BUF(void)
  36136. {
  36137. EXPECT_DECLS;
  36138. #if defined(OPENSSL_EXTRA)
  36139. BUF_MEM* buf = NULL;
  36140. ExpectNotNull(buf = BUF_MEM_new());
  36141. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  36142. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  36143. BUF_MEM_free(buf);
  36144. #endif
  36145. return EXPECT_RESULT();
  36146. }
  36147. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36148. static int stub_rand_seed(const void *buf, int num)
  36149. {
  36150. (void)buf;
  36151. (void)num;
  36152. return 123;
  36153. }
  36154. static int stub_rand_bytes(unsigned char *buf, int num)
  36155. {
  36156. (void)buf;
  36157. (void)num;
  36158. return 456;
  36159. }
  36160. static byte* was_stub_rand_cleanup_called(void)
  36161. {
  36162. static byte was_called = 0;
  36163. return &was_called;
  36164. }
  36165. static void stub_rand_cleanup(void)
  36166. {
  36167. byte* was_called = was_stub_rand_cleanup_called();
  36168. *was_called = 1;
  36169. return;
  36170. }
  36171. static byte* was_stub_rand_add_called(void)
  36172. {
  36173. static byte was_called = 0;
  36174. return &was_called;
  36175. }
  36176. static int stub_rand_add(const void *buf, int num, double entropy)
  36177. {
  36178. byte* was_called = was_stub_rand_add_called();
  36179. (void)buf;
  36180. (void)num;
  36181. (void)entropy;
  36182. *was_called = 1;
  36183. return 0;
  36184. }
  36185. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  36186. {
  36187. (void)buf;
  36188. (void)num;
  36189. return 9876;
  36190. }
  36191. static int stub_rand_status(void)
  36192. {
  36193. return 5432;
  36194. }
  36195. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36196. static int test_wolfSSL_RAND_set_rand_method(void)
  36197. {
  36198. EXPECT_DECLS;
  36199. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36200. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  36201. unsigned char* buf = NULL;
  36202. int num = 0;
  36203. double entropy = 0;
  36204. int ret;
  36205. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  36206. byte* was_add_called = was_stub_rand_add_called();
  36207. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  36208. DYNAMIC_TYPE_TMP_BUFFER));
  36209. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  36210. ExpectIntEQ(*was_cleanup_called, 0);
  36211. RAND_cleanup();
  36212. ExpectIntEQ(*was_cleanup_called, 0);
  36213. rand_methods.seed = &stub_rand_seed;
  36214. rand_methods.bytes = &stub_rand_bytes;
  36215. rand_methods.cleanup = &stub_rand_cleanup;
  36216. rand_methods.add = &stub_rand_add;
  36217. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  36218. rand_methods.status = &stub_rand_status;
  36219. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  36220. ExpectIntEQ(RAND_seed(buf, num), 123);
  36221. ExpectIntEQ(RAND_bytes(buf, num), 456);
  36222. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  36223. ExpectIntEQ(RAND_status(), 5432);
  36224. ExpectIntEQ(*was_add_called, 0);
  36225. /* The function pointer for RAND_add returns int, but RAND_add itself
  36226. * returns void. */
  36227. RAND_add(buf, num, entropy);
  36228. ExpectIntEQ(*was_add_called, 1);
  36229. was_add_called = 0;
  36230. ExpectIntEQ(*was_cleanup_called, 0);
  36231. RAND_cleanup();
  36232. ExpectIntEQ(*was_cleanup_called, 1);
  36233. *was_cleanup_called = 0;
  36234. ret = RAND_set_rand_method(NULL);
  36235. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  36236. ExpectIntNE(RAND_status(), 5432);
  36237. ExpectIntEQ(*was_cleanup_called, 0);
  36238. RAND_cleanup();
  36239. ExpectIntEQ(*was_cleanup_called, 0);
  36240. RAND_set_rand_method(NULL);
  36241. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36242. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36243. return EXPECT_RESULT();
  36244. }
  36245. static int test_wolfSSL_RAND_bytes(void)
  36246. {
  36247. EXPECT_DECLS;
  36248. #if defined(OPENSSL_EXTRA)
  36249. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  36250. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  36251. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  36252. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  36253. int max_bufsize;
  36254. byte *my_buf = NULL;
  36255. /* sanity check */
  36256. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  36257. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  36258. max_bufsize = size4;
  36259. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  36260. DYNAMIC_TYPE_TMP_BUFFER));
  36261. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  36262. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  36263. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  36264. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  36265. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  36266. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  36267. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  36268. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36269. #endif
  36270. return EXPECT_RESULT();
  36271. }
  36272. static int test_wolfSSL_RAND(void)
  36273. {
  36274. EXPECT_DECLS;
  36275. #if defined(OPENSSL_EXTRA)
  36276. byte seed[16];
  36277. XMEMSET(seed, 0, sizeof(seed));
  36278. /* No global methods set. */
  36279. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  36280. ExpectIntEQ(RAND_poll(), 1);
  36281. RAND_cleanup();
  36282. ExpectIntEQ(RAND_egd(NULL), -1);
  36283. #ifndef NO_FILESYSTEM
  36284. {
  36285. char fname[100];
  36286. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  36287. ExpectIntEQ(RAND_write_file(NULL), 0);
  36288. }
  36289. #endif
  36290. #endif
  36291. return EXPECT_RESULT();
  36292. }
  36293. static int test_wolfSSL_PKCS8_Compat(void)
  36294. {
  36295. EXPECT_DECLS;
  36296. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  36297. !defined(NO_BIO)
  36298. PKCS8_PRIV_KEY_INFO* pt = NULL;
  36299. BIO* bio = NULL;
  36300. XFILE f = XBADFILE;
  36301. int bytes;
  36302. char pkcs8_buffer[512];
  36303. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36304. EVP_PKEY *pkey = NULL;
  36305. #endif
  36306. /* file from wolfssl/certs/ directory */
  36307. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  36308. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  36309. 0);
  36310. if (f != XBADFILE)
  36311. XFCLOSE(f);
  36312. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36313. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  36314. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36315. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  36316. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  36317. /* gets PKCS8 pointer to pkey */
  36318. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  36319. EVP_PKEY_free(pkey);
  36320. #endif
  36321. BIO_free(bio);
  36322. PKCS8_PRIV_KEY_INFO_free(pt);
  36323. #endif
  36324. return EXPECT_RESULT();
  36325. }
  36326. static int test_wolfSSL_PKCS8_d2i(void)
  36327. {
  36328. EXPECT_DECLS;
  36329. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  36330. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  36331. * requires a 12 byte password in FIPS mode. This test ends up
  36332. * trying to use an 8 byte password. */
  36333. #ifndef NO_FILESYSTEM
  36334. unsigned char pkcs8_buffer[2048];
  36335. const unsigned char* p = NULL;
  36336. int bytes = 0;
  36337. XFILE file = XBADFILE;
  36338. WOLFSSL_EVP_PKEY* pkey = NULL;
  36339. #ifndef NO_BIO
  36340. BIO* bio = NULL;
  36341. #if defined(OPENSSL_ALL) && \
  36342. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  36343. defined(HAVE_ECC)) && \
  36344. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36345. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  36346. #endif
  36347. #endif
  36348. #ifndef NO_RSA
  36349. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  36350. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  36351. #ifndef NO_DES3
  36352. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  36353. #endif
  36354. #endif /* NO_RSA */
  36355. #ifdef HAVE_ECC
  36356. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  36357. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  36358. #ifndef NO_DES3
  36359. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  36360. #endif
  36361. #endif /* HAVE_ECC */
  36362. #endif /* !NO_FILESYSTEM */
  36363. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  36364. #ifndef NO_RSA
  36365. #ifdef USE_CERT_BUFFERS_1024
  36366. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  36367. int rsaSz = sizeof_server_key_der_1024;
  36368. #else
  36369. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  36370. int rsaSz = sizeof_server_key_der_2048;
  36371. #endif
  36372. #endif
  36373. #ifdef HAVE_ECC
  36374. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  36375. int ecSz = sizeof_ecc_key_der_256;
  36376. #endif
  36377. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  36378. #ifndef NO_FILESYSTEM
  36379. (void)pkcs8_buffer;
  36380. (void)p;
  36381. (void)bytes;
  36382. (void)file;
  36383. #ifndef NO_BIO
  36384. (void)bio;
  36385. #endif
  36386. #endif
  36387. #ifdef OPENSSL_ALL
  36388. #ifndef NO_RSA
  36389. /* Try to auto-detect normal RSA private key */
  36390. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  36391. EVP_PKEY_free(pkey);
  36392. pkey = NULL;
  36393. #endif
  36394. #ifdef HAVE_ECC
  36395. /* Try to auto-detect normal EC private key */
  36396. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  36397. EVP_PKEY_free(pkey);
  36398. pkey = NULL;
  36399. #endif
  36400. #endif /* OPENSSL_ALL */
  36401. #ifndef NO_FILESYSTEM
  36402. #ifndef NO_RSA
  36403. /* Get DER encoded RSA PKCS#8 data. */
  36404. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  36405. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36406. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36407. file)), 0);
  36408. if (file != XBADFILE) {
  36409. XFCLOSE(file);
  36410. file = XBADFILE;
  36411. }
  36412. p = pkcs8_buffer;
  36413. #ifdef OPENSSL_ALL
  36414. /* Try to decode - auto-detect key type. */
  36415. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  36416. #else
  36417. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  36418. #endif
  36419. /* Get PEM encoded RSA PKCS#8 data. */
  36420. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  36421. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36422. file)), 0);
  36423. if (file != XBADFILE) {
  36424. XFCLOSE(file);
  36425. file = XBADFILE;
  36426. }
  36427. #if defined(OPENSSL_ALL) && \
  36428. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36429. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36430. /* Write PKCS#8 PEM to BIO. */
  36431. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  36432. NULL), bytes);
  36433. /* Compare file and written data */
  36434. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  36435. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  36436. BIO_free(bio);
  36437. bio = NULL;
  36438. #if !defined(NO_DES3) && !defined(NO_SHA)
  36439. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36440. /* Write Encrypted PKCS#8 PEM to BIO. */
  36441. bytes = 1834;
  36442. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  36443. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  36444. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  36445. (void*)"yassl123"));
  36446. EVP_PKEY_free(evpPkey);
  36447. evpPkey = NULL;
  36448. BIO_free(bio);
  36449. bio = NULL;
  36450. #endif /* !NO_DES3 && !NO_SHA */
  36451. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  36452. EVP_PKEY_free(pkey);
  36453. pkey = NULL;
  36454. /* PKCS#8 encrypted RSA key */
  36455. #ifndef NO_DES3
  36456. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  36457. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36458. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36459. file)), 0);
  36460. if (file != XBADFILE) {
  36461. XFCLOSE(file);
  36462. file = XBADFILE;
  36463. }
  36464. #if defined(OPENSSL_ALL) && \
  36465. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36466. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36467. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  36468. (void*)"yassl123"));
  36469. EVP_PKEY_free(pkey);
  36470. pkey = NULL;
  36471. BIO_free(bio);
  36472. bio = NULL;
  36473. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  36474. #endif /* !NO_DES3 */
  36475. #endif /* NO_RSA */
  36476. #ifdef HAVE_ECC
  36477. /* PKCS#8 encode EC key */
  36478. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  36479. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36480. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36481. file)), 0);
  36482. if (file != XBADFILE) {
  36483. XFCLOSE(file);
  36484. file = XBADFILE;
  36485. }
  36486. p = pkcs8_buffer;
  36487. #ifdef OPENSSL_ALL
  36488. /* Try to decode - auto-detect key type. */
  36489. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  36490. #else
  36491. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  36492. #endif
  36493. /* Get PEM encoded RSA PKCS#8 data. */
  36494. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  36495. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36496. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36497. file)), 0);
  36498. if (file != XBADFILE) {
  36499. XFCLOSE(file);
  36500. file = XBADFILE;
  36501. }
  36502. #if defined(OPENSSL_ALL) && \
  36503. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  36504. defined(HAVE_AES_CBC)
  36505. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36506. /* Write PKCS#8 PEM to BIO. */
  36507. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  36508. NULL), bytes);
  36509. /* Compare file and written data */
  36510. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  36511. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  36512. BIO_free(bio);
  36513. bio = NULL;
  36514. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36515. /* Write Encrypted PKCS#8 PEM to BIO. */
  36516. bytes = 379;
  36517. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  36518. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  36519. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  36520. (void*)"yassl123"));
  36521. EVP_PKEY_free(evpPkey);
  36522. evpPkey = NULL;
  36523. BIO_free(bio);
  36524. bio = NULL;
  36525. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  36526. EVP_PKEY_free(pkey);
  36527. pkey = NULL;
  36528. /* PKCS#8 encrypted EC key */
  36529. #ifndef NO_DES3
  36530. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  36531. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  36532. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  36533. file)), 0);
  36534. if (file != XBADFILE) {
  36535. XFCLOSE(file);
  36536. file = XBADFILE;
  36537. }
  36538. #if defined(OPENSSL_ALL) && \
  36539. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36540. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36541. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  36542. (void*)"yassl123"));
  36543. EVP_PKEY_free(pkey);
  36544. pkey = NULL;
  36545. BIO_free(bio);
  36546. bio = NULL;
  36547. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  36548. #endif /* !NO_DES3 */
  36549. #endif /* HAVE_ECC */
  36550. #endif /* !NO_FILESYSTEM */
  36551. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  36552. return EXPECT_RESULT();
  36553. }
  36554. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  36555. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  36556. #define LOGGING_THREADS 5
  36557. #define ERROR_COUNT 10
  36558. /* copied from logging.c since this is not exposed otherwise */
  36559. #ifndef ERROR_QUEUE_MAX
  36560. #ifdef ERROR_QUEUE_PER_THREAD
  36561. #define ERROR_QUEUE_MAX 16
  36562. #else
  36563. /* this breaks from compat of unlimited error queue size */
  36564. #define ERROR_QUEUE_MAX 100
  36565. #endif
  36566. #endif
  36567. static volatile int loggingThreadsReady;
  36568. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  36569. {
  36570. const char* file;
  36571. int line;
  36572. unsigned long err;
  36573. int errorCount = 0;
  36574. int i;
  36575. (void)args;
  36576. while (!loggingThreadsReady);
  36577. for (i = 0; i < ERROR_COUNT; i++)
  36578. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  36579. while ((err = ERR_get_error_line(&file, &line))) {
  36580. AssertIntEQ(err, 990 + errorCount);
  36581. errorCount++;
  36582. }
  36583. AssertIntEQ(errorCount, ERROR_COUNT);
  36584. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  36585. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  36586. errorCount = 0;
  36587. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  36588. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  36589. while ((err = ERR_get_error_line(&file, &line))) {
  36590. AssertIntEQ(err, 990 + errorCount);
  36591. errorCount++;
  36592. }
  36593. /* test that the 3 errors over the max were dropped */
  36594. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  36595. WOLFSSL_RETURN_FROM_THREAD(0);
  36596. }
  36597. #endif
  36598. static int test_error_queue_per_thread(void)
  36599. {
  36600. int res = TEST_SKIPPED;
  36601. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  36602. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  36603. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  36604. int i;
  36605. ERR_clear_error(); /* clear out any error nodes */
  36606. loggingThreadsReady = 0;
  36607. for (i = 0; i < LOGGING_THREADS; i++)
  36608. start_thread(test_logging, NULL, &loggingThreads[i]);
  36609. loggingThreadsReady = 1;
  36610. for (i = 0; i < LOGGING_THREADS; i++)
  36611. join_thread(loggingThreads[i]);
  36612. res = TEST_SUCCESS;
  36613. #endif
  36614. return res;
  36615. }
  36616. static int test_wolfSSL_ERR_put_error(void)
  36617. {
  36618. EXPECT_DECLS;
  36619. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36620. defined(DEBUG_WOLFSSL)
  36621. const char* file;
  36622. int line;
  36623. ERR_clear_error(); /* clear out any error nodes */
  36624. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  36625. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36626. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  36627. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  36628. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  36629. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  36630. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  36631. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  36632. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  36633. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  36634. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  36635. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  36636. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  36637. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  36638. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  36639. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  36640. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  36641. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  36642. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  36643. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  36644. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  36645. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  36646. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  36647. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  36648. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  36649. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  36650. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  36651. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  36652. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  36653. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  36654. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  36655. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  36656. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  36657. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  36658. "this file", 100);
  36659. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  36660. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  36661. ExpectIntEQ(line, 100);
  36662. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  36663. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  36664. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  36665. #endif
  36666. /* try reading past end of error queue */
  36667. file = NULL;
  36668. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36669. ExpectNull(file);
  36670. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  36671. PEMerr(4,4);
  36672. ExpectIntEQ(ERR_get_error(), 4);
  36673. /* Empty and free up all error nodes */
  36674. ERR_clear_error();
  36675. /* Verify all nodes are cleared */
  36676. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  36677. ERR_clear_error();
  36678. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  36679. #endif
  36680. return EXPECT_RESULT();
  36681. }
  36682. /*
  36683. * This is a regression test for a bug where the peek/get error functions were
  36684. * drawing from the end of the queue rather than the front.
  36685. */
  36686. static int test_wolfSSL_ERR_get_error_order(void)
  36687. {
  36688. EXPECT_DECLS;
  36689. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  36690. /* Empty the queue. */
  36691. wolfSSL_ERR_clear_error();
  36692. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  36693. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  36694. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  36695. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  36696. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  36697. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  36698. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  36699. return EXPECT_RESULT();
  36700. }
  36701. #ifndef NO_BIO
  36702. static int test_wolfSSL_ERR_print_errors(void)
  36703. {
  36704. EXPECT_DECLS;
  36705. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36706. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  36707. BIO* bio = NULL;
  36708. char buf[1024];
  36709. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36710. ERR_clear_error(); /* clear out any error nodes */
  36711. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  36712. /* Choosing -600 as an unused errno. */
  36713. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  36714. ERR_print_errors(bio);
  36715. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  36716. ExpectIntEQ(XSTRNCMP(
  36717. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  36718. buf, 55), 0);
  36719. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  36720. ExpectIntEQ(XSTRNCMP(
  36721. "error:600:wolfSSL library:unknown error number:asn.c:100",
  36722. buf, 56), 0);
  36723. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  36724. ExpectIntEQ(buf[0], '\0');
  36725. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  36726. BIO_free(bio);
  36727. #endif
  36728. return EXPECT_RESULT();
  36729. }
  36730. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36731. defined(DEBUG_WOLFSSL)
  36732. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  36733. {
  36734. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  36735. return 0;
  36736. }
  36737. #endif
  36738. static int test_wolfSSL_ERR_print_errors_cb(void)
  36739. {
  36740. EXPECT_DECLS;
  36741. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  36742. defined(DEBUG_WOLFSSL)
  36743. BIO* bio = NULL;
  36744. char buf[1024];
  36745. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36746. ERR_clear_error(); /* clear out any error nodes */
  36747. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  36748. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  36749. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  36750. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  36751. ExpectIntEQ(XSTRNCMP(
  36752. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  36753. buf, 53), 0);
  36754. ExpectIntEQ(XSTRNCMP(
  36755. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  36756. buf + 53, 55), 0);
  36757. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  36758. BIO_free(bio);
  36759. #endif
  36760. return EXPECT_RESULT();
  36761. }
  36762. /*
  36763. * Testing WOLFSSL_ERROR_MSG
  36764. */
  36765. static int test_WOLFSSL_ERROR_MSG(void)
  36766. {
  36767. int res = TEST_SKIPPED;
  36768. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  36769. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  36770. const char* msg = TEST_STRING;
  36771. WOLFSSL_ERROR_MSG(msg);
  36772. res = TEST_SUCCESS;
  36773. #endif
  36774. return res;
  36775. } /* End test_WOLFSSL_ERROR_MSG */
  36776. /*
  36777. * Testing wc_ERR_remove_state
  36778. */
  36779. static int test_wc_ERR_remove_state(void)
  36780. {
  36781. int res = TEST_SKIPPED;
  36782. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  36783. wc_ERR_remove_state();
  36784. res = TEST_SUCCESS;
  36785. #endif
  36786. return res;
  36787. } /* End test_wc_ERR_remove_state */
  36788. /*
  36789. * Testing wc_ERR_print_errors_fp
  36790. */
  36791. static int test_wc_ERR_print_errors_fp(void)
  36792. {
  36793. EXPECT_DECLS;
  36794. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  36795. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  36796. long sz;
  36797. XFILE fp = XBADFILE;
  36798. WOLFSSL_ERROR(BAD_FUNC_ARG);
  36799. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  36800. XBADFILE);
  36801. wc_ERR_print_errors_fp(fp);
  36802. #if defined(DEBUG_WOLFSSL)
  36803. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  36804. #ifdef NO_ERROR_QUEUE
  36805. ExpectIntEQ(sz = XFTELL(fp), 0);
  36806. #else
  36807. ExpectIntNE(sz = XFTELL(fp), 0);
  36808. #endif
  36809. #endif
  36810. if (fp != XBADFILE)
  36811. XFCLOSE(fp);
  36812. (void)sz;
  36813. #endif
  36814. return EXPECT_RESULT();
  36815. } /* End test_wc_ERR_print_errors_fp */
  36816. #ifdef DEBUG_WOLFSSL
  36817. static void Logging_cb(const int logLevel, const char *const logMessage)
  36818. {
  36819. (void)logLevel;
  36820. (void)logMessage;
  36821. }
  36822. #endif
  36823. /*
  36824. * Testing wolfSSL_GetLoggingCb
  36825. */
  36826. static int test_wolfSSL_GetLoggingCb(void)
  36827. {
  36828. EXPECT_DECLS;
  36829. #ifdef DEBUG_WOLFSSL
  36830. /* Testing without wolfSSL_SetLoggingCb() */
  36831. ExpectNull(wolfSSL_GetLoggingCb());
  36832. /* Testing with wolfSSL_SetLoggingCb() */
  36833. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  36834. ExpectNotNull(wolfSSL_GetLoggingCb());
  36835. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  36836. #endif
  36837. ExpectNull(wolfSSL_GetLoggingCb());
  36838. return EXPECT_RESULT();
  36839. } /* End test_wolfSSL_GetLoggingCb */
  36840. #endif /* !NO_BIO */
  36841. static int test_wolfSSL_MD4(void)
  36842. {
  36843. EXPECT_DECLS;
  36844. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36845. MD4_CTX md4;
  36846. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36847. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36848. "789012345678901234567890";
  36849. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36850. "\xcc\x05\x36";
  36851. int msgSz = (int)XSTRLEN(msg);
  36852. XMEMSET(out, 0, sizeof(out));
  36853. MD4_Init(&md4);
  36854. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36855. MD4_Final(out, &md4);
  36856. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36857. #endif
  36858. return EXPECT_RESULT();
  36859. }
  36860. static int test_wolfSSL_MD5(void)
  36861. {
  36862. EXPECT_DECLS;
  36863. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36864. byte input1[] = "";
  36865. byte input2[] = "message digest";
  36866. byte hash[WC_MD5_DIGEST_SIZE];
  36867. unsigned char output1[] =
  36868. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  36869. unsigned char output2[] =
  36870. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  36871. WOLFSSL_MD5_CTX md5;
  36872. XMEMSET(&md5, 0, sizeof(md5));
  36873. /* Test cases for illegal parameters */
  36874. ExpectIntEQ(MD5_Init(NULL), 0);
  36875. ExpectIntEQ(MD5_Init(&md5), 1);
  36876. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  36877. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  36878. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  36879. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  36880. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  36881. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  36882. /* Init MD5 CTX */
  36883. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36884. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  36885. 1);
  36886. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36887. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36888. /* Init MD5 CTX */
  36889. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36890. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  36891. (int)XSTRLEN((const char*)input2)), 1);
  36892. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36893. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36894. #if !defined(NO_OLD_NAMES) && \
  36895. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  36896. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  36897. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  36898. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  36899. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  36900. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  36901. &hash);
  36902. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36903. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  36904. &hash);
  36905. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36906. {
  36907. byte data[] = "Data to be hashed.";
  36908. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  36909. ExpectNotNull(MD5(data, sizeof(data), NULL));
  36910. ExpectNotNull(MD5(data, sizeof(data), hash));
  36911. ExpectNotNull(MD5(NULL, 0, hash));
  36912. ExpectNull(MD5(NULL, sizeof(data), hash));
  36913. }
  36914. #endif
  36915. #endif
  36916. return EXPECT_RESULT();
  36917. }
  36918. static int test_wolfSSL_MD5_Transform(void)
  36919. {
  36920. EXPECT_DECLS;
  36921. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36922. byte input1[] = "";
  36923. byte input2[] = "abc";
  36924. byte local[WC_MD5_BLOCK_SIZE];
  36925. word32 sLen = 0;
  36926. #ifdef BIG_ENDIAN_ORDER
  36927. unsigned char output1[] =
  36928. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  36929. unsigned char output2[] =
  36930. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  36931. #else
  36932. unsigned char output1[] =
  36933. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  36934. unsigned char output2[] =
  36935. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  36936. #endif
  36937. union {
  36938. wc_Md5 native;
  36939. MD5_CTX compat;
  36940. } md5;
  36941. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  36942. XMEMSET(&local, 0, sizeof(local));
  36943. /* sanity check */
  36944. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  36945. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  36946. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  36947. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  36948. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36949. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  36950. /* Init MD5 CTX */
  36951. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  36952. /* Do Transform*/
  36953. sLen = (word32)XSTRLEN((char*)input1);
  36954. XMEMCPY(local, input1, sLen);
  36955. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36956. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  36957. /* Init MD5 CTX */
  36958. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  36959. sLen = (word32)XSTRLEN((char*)input2);
  36960. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  36961. XMEMCPY(local, input2, sLen);
  36962. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36963. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  36964. #endif
  36965. return EXPECT_RESULT();
  36966. }
  36967. static int test_wolfSSL_SHA(void)
  36968. {
  36969. EXPECT_DECLS;
  36970. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  36971. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  36972. (!defined(HAVE_FIPS) || \
  36973. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36974. {
  36975. const unsigned char in[] = "abc";
  36976. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  36977. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  36978. unsigned char out[WC_SHA_DIGEST_SIZE];
  36979. unsigned char* p = NULL;
  36980. WOLFSSL_SHA_CTX sha;
  36981. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36982. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  36983. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36984. /* SHA interface test */
  36985. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36986. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  36987. ExpectNotNull(SHA(in, 0, out));
  36988. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  36989. ExpectNotNull(SHA(NULL, 0, out));
  36990. ExpectNotNull(SHA(NULL, 0, NULL));
  36991. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  36992. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36993. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  36994. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  36995. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  36996. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  36997. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  36998. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36999. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  37000. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37001. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  37002. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37003. }
  37004. #endif
  37005. #if !defined(NO_SHA256)
  37006. {
  37007. const unsigned char in[] = "abc";
  37008. unsigned char expected[] =
  37009. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37010. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37011. "\x15\xAD";
  37012. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37013. unsigned char* p = NULL;
  37014. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37015. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37016. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37017. #else
  37018. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37019. #endif
  37020. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37021. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37022. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  37023. #else
  37024. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  37025. #endif
  37026. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  37027. }
  37028. #endif
  37029. #if defined(WOLFSSL_SHA384)
  37030. {
  37031. const unsigned char in[] = "abc";
  37032. unsigned char expected[] =
  37033. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37034. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37035. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37036. "\xc8\x25\xa7";
  37037. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37038. unsigned char* p = NULL;
  37039. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37040. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37041. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37042. #else
  37043. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37044. #endif
  37045. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37046. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37047. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  37048. #else
  37049. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  37050. #endif
  37051. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  37052. }
  37053. #endif
  37054. #if defined(WOLFSSL_SHA512)
  37055. {
  37056. const unsigned char in[] = "abc";
  37057. unsigned char expected[] =
  37058. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37059. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37060. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37061. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37062. "\xa5\x4c\xa4\x9f";
  37063. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37064. unsigned char* p = NULL;
  37065. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37066. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37067. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37068. #else
  37069. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37070. #endif
  37071. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37072. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37073. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  37074. #else
  37075. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  37076. #endif
  37077. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  37078. }
  37079. #endif
  37080. #endif
  37081. return EXPECT_RESULT();
  37082. }
  37083. static int test_wolfSSL_SHA_Transform(void)
  37084. {
  37085. EXPECT_DECLS;
  37086. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37087. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37088. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37089. byte input1[] = "";
  37090. byte input2[] = "abc";
  37091. byte local[WC_SHA_BLOCK_SIZE];
  37092. word32 sLen = 0;
  37093. #ifdef BIG_ENDIAN_ORDER
  37094. unsigned char output1[] =
  37095. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37096. "\x09\xf7\x3a\x93";
  37097. unsigned char output2[] =
  37098. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37099. "\x7c\x6e\x08\xb8";
  37100. #else
  37101. unsigned char output1[] =
  37102. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37103. "\x93\x3a\xf7\x09";
  37104. unsigned char output2[] =
  37105. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37106. "\xb8\x08\x6e\x7c";
  37107. #endif
  37108. union {
  37109. wc_Sha native;
  37110. SHA_CTX compat;
  37111. } sha;
  37112. union {
  37113. wc_Sha native;
  37114. SHA_CTX compat;
  37115. } sha1;
  37116. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37117. XMEMSET(&local, 0, sizeof(local));
  37118. /* sanity check */
  37119. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  37120. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37121. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37122. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  37123. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37124. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37125. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37126. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37127. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37128. /* Init SHA CTX */
  37129. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37130. /* Do Transform*/
  37131. sLen = (word32)XSTRLEN((char*)input1);
  37132. XMEMCPY(local, input1, sLen);
  37133. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37134. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37135. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37136. /* Init SHA CTX */
  37137. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37138. sLen = (word32)XSTRLEN((char*)input2);
  37139. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37140. XMEMCPY(local, input2, sLen);
  37141. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37142. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37143. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37144. /* SHA1 */
  37145. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37146. /* Init SHA CTX */
  37147. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37148. /* Do Transform*/
  37149. sLen = (word32)XSTRLEN((char*)input1);
  37150. XMEMCPY(local, input1, sLen);
  37151. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37152. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37153. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  37154. /* Init SHA CTX */
  37155. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37156. sLen = (word32)XSTRLEN((char*)input2);
  37157. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37158. XMEMCPY(local, input2, sLen);
  37159. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37160. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37161. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37162. #endif
  37163. #endif
  37164. return EXPECT_RESULT();
  37165. }
  37166. static int test_wolfSSL_SHA224(void)
  37167. {
  37168. EXPECT_DECLS;
  37169. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37170. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37171. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37172. unsigned char input[] =
  37173. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37174. unsigned char output[] =
  37175. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37176. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37177. size_t inLen;
  37178. byte hash[WC_SHA224_DIGEST_SIZE];
  37179. unsigned char* p;
  37180. inLen = XSTRLEN((char*)input);
  37181. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37182. ExpectNull(SHA224(NULL, inLen, hash));
  37183. ExpectNotNull(SHA224(input, 0, hash));
  37184. ExpectNotNull(SHA224(input, inLen, NULL));
  37185. ExpectNotNull(SHA224(NULL, 0, hash));
  37186. ExpectNotNull(SHA224(NULL, 0, NULL));
  37187. ExpectNotNull(SHA224(input, inLen, hash));
  37188. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37189. ExpectNotNull(p = SHA224(input, inLen, NULL));
  37190. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  37191. #endif
  37192. return EXPECT_RESULT();
  37193. }
  37194. static int test_wolfSSL_SHA256(void)
  37195. {
  37196. EXPECT_DECLS;
  37197. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37198. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37199. unsigned char input[] =
  37200. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37201. unsigned char output[] =
  37202. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37203. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37204. "\x06\xC1";
  37205. size_t inLen;
  37206. byte hash[WC_SHA256_DIGEST_SIZE];
  37207. inLen = XSTRLEN((char*)input);
  37208. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37209. ExpectNotNull(SHA256(input, inLen, hash));
  37210. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37211. #endif
  37212. return EXPECT_RESULT();
  37213. }
  37214. static int test_wolfSSL_SHA256_Transform(void)
  37215. {
  37216. EXPECT_DECLS;
  37217. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37218. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37219. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37220. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37221. !defined(WOLFSSL_KCAPI_HASH)
  37222. byte input1[] = "";
  37223. byte input2[] = "abc";
  37224. byte local[WC_SHA256_BLOCK_SIZE];
  37225. word32 sLen = 0;
  37226. #ifdef BIG_ENDIAN_ORDER
  37227. unsigned char output1[] =
  37228. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37229. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37230. unsigned char output2[] =
  37231. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37232. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37233. #else
  37234. unsigned char output1[] =
  37235. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37236. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37237. unsigned char output2[] =
  37238. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37239. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37240. #endif
  37241. union {
  37242. wc_Sha256 native;
  37243. SHA256_CTX compat;
  37244. } sha256;
  37245. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37246. XMEMSET(&local, 0, sizeof(local));
  37247. /* sanity check */
  37248. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  37249. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37250. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37251. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37252. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37253. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37254. /* Init SHA256 CTX */
  37255. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37256. /* Do Transform*/
  37257. sLen = (word32)XSTRLEN((char*)input1);
  37258. XMEMCPY(local, input1, sLen);
  37259. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37260. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  37261. 0);
  37262. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37263. /* Init SHA256 CTX */
  37264. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37265. sLen = (word32)XSTRLEN((char*)input2);
  37266. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37267. XMEMCPY(local, input2, sLen);
  37268. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37269. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  37270. 0);
  37271. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37272. #endif
  37273. #endif
  37274. return EXPECT_RESULT();
  37275. }
  37276. static int test_wolfSSL_SHA512_Transform(void)
  37277. {
  37278. EXPECT_DECLS;
  37279. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37280. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37281. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37282. !defined(WOLFSSL_KCAPI_HASH)
  37283. byte input1[] = "";
  37284. byte input2[] = "abc";
  37285. byte local[WC_SHA512_BLOCK_SIZE];
  37286. word32 sLen = 0;
  37287. #ifdef BIG_ENDIAN_ORDER
  37288. unsigned char output1[] =
  37289. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37290. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37291. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37292. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37293. unsigned char output2[] =
  37294. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37295. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37296. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37297. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37298. #else
  37299. unsigned char output1[] =
  37300. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37301. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37302. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37303. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37304. unsigned char output2[] =
  37305. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37306. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37307. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37308. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37309. #endif
  37310. union {
  37311. wc_Sha512 native;
  37312. SHA512_CTX compat;
  37313. } sha512;
  37314. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37315. XMEMSET(&local, 0, sizeof(local));
  37316. /* sanity check */
  37317. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  37318. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37319. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37320. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37321. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37322. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37323. /* Init SHA512 CTX */
  37324. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37325. /* Do Transform*/
  37326. sLen = (word32)XSTRLEN((char*)input1);
  37327. XMEMCPY(local, input1, sLen);
  37328. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37329. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37330. WC_SHA512_DIGEST_SIZE), 0);
  37331. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37332. /* Init SHA512 CTX */
  37333. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  37334. sLen = (word32)XSTRLEN((char*)input2);
  37335. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37336. XMEMCPY(local, input2, sLen);
  37337. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37338. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37339. WC_SHA512_DIGEST_SIZE), 0);
  37340. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37341. (void)input1;
  37342. #endif
  37343. #endif
  37344. return EXPECT_RESULT();
  37345. }
  37346. static int test_wolfSSL_SHA512_224_Transform(void)
  37347. {
  37348. EXPECT_DECLS;
  37349. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  37350. !defined(WOLFSSL_NOSHA512_224)
  37351. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37352. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37353. !defined(WOLFSSL_KCAPI_HASH)
  37354. byte input1[] = "";
  37355. byte input2[] = "abc";
  37356. byte local[WC_SHA512_BLOCK_SIZE];
  37357. word32 sLen = 0;
  37358. unsigned char output1[] =
  37359. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  37360. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  37361. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  37362. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  37363. unsigned char output2[] =
  37364. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  37365. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  37366. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  37367. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  37368. union {
  37369. wc_Sha512 native;
  37370. SHA512_CTX compat;
  37371. } sha512;
  37372. #ifdef BIG_ENDIAN_ORDER
  37373. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  37374. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  37375. #endif
  37376. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37377. XMEMSET(&local, 0, sizeof(local));
  37378. /* sanity check */
  37379. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  37380. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  37381. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  37382. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  37383. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  37384. BAD_FUNC_ARG);
  37385. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37386. /* Init SHA512 CTX */
  37387. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  37388. /* Do Transform*/
  37389. sLen = (word32)XSTRLEN((char*)input1);
  37390. XMEMCPY(local, input1, sLen);
  37391. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  37392. 1);
  37393. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37394. WC_SHA512_DIGEST_SIZE), 0);
  37395. /* frees resources */
  37396. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  37397. /* Init SHA512 CTX */
  37398. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  37399. sLen = (word32)XSTRLEN((char*)input2);
  37400. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37401. XMEMCPY(local, input2, sLen);
  37402. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  37403. 1);
  37404. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37405. WC_SHA512_DIGEST_SIZE), 0);
  37406. /* frees resources */
  37407. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  37408. #endif
  37409. #endif
  37410. return EXPECT_RESULT();
  37411. }
  37412. static int test_wolfSSL_SHA512_256_Transform(void)
  37413. {
  37414. EXPECT_DECLS;
  37415. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  37416. !defined(WOLFSSL_NOSHA512_256)
  37417. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37418. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37419. !defined(WOLFSSL_KCAPI_HASH)
  37420. byte input1[] = "";
  37421. byte input2[] = "abc";
  37422. byte local[WC_SHA512_BLOCK_SIZE];
  37423. word32 sLen = 0;
  37424. unsigned char output1[] =
  37425. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  37426. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  37427. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  37428. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  37429. unsigned char output2[] =
  37430. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  37431. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  37432. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  37433. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  37434. union {
  37435. wc_Sha512 native;
  37436. SHA512_CTX compat;
  37437. } sha512;
  37438. #ifdef BIG_ENDIAN_ORDER
  37439. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  37440. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  37441. #endif
  37442. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37443. XMEMSET(&local, 0, sizeof(local));
  37444. /* sanity check */
  37445. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  37446. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  37447. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  37448. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  37449. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  37450. BAD_FUNC_ARG);
  37451. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37452. /* Init SHA512 CTX */
  37453. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  37454. /* Do Transform*/
  37455. sLen = (word32)XSTRLEN((char*)input1);
  37456. XMEMCPY(local, input1, sLen);
  37457. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  37458. 1);
  37459. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37460. WC_SHA512_DIGEST_SIZE), 0);
  37461. /* frees resources */
  37462. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  37463. /* Init SHA512 CTX */
  37464. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  37465. sLen = (word32)XSTRLEN((char*)input2);
  37466. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37467. XMEMCPY(local, input2, sLen);
  37468. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  37469. 1);
  37470. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37471. WC_SHA512_DIGEST_SIZE), 0);
  37472. /* frees resources */
  37473. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  37474. #endif
  37475. #endif
  37476. return EXPECT_RESULT();
  37477. }
  37478. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  37479. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  37480. * buffer of 64 bytes.
  37481. *
  37482. * returns the size of the digest buffer on success and a negative value on
  37483. * failure.
  37484. */
  37485. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  37486. int* sz)
  37487. {
  37488. EXPECT_DECLS;
  37489. HMAC_CTX ctx1;
  37490. HMAC_CTX ctx2;
  37491. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  37492. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  37493. unsigned char long_key[] =
  37494. "0123456789012345678901234567890123456789"
  37495. "0123456789012345678901234567890123456789"
  37496. "0123456789012345678901234567890123456789"
  37497. "0123456789012345678901234567890123456789";
  37498. unsigned char msg[] = "message to hash";
  37499. unsigned int digestSz = 64;
  37500. int keySz = sizeof(key);
  37501. int long_keySz = sizeof(long_key);
  37502. int msgSz = sizeof(msg);
  37503. unsigned char digest2[64];
  37504. unsigned int digestSz2 = 64;
  37505. HMAC_CTX_init(&ctx1);
  37506. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37507. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37508. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37509. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37510. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37511. HMAC_CTX_cleanup(&ctx1);
  37512. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37513. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  37514. HMAC_CTX_cleanup(&ctx2);
  37515. ExpectIntEQ(digestSz, digestSz2);
  37516. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37517. /* test HMAC_Init with NULL key */
  37518. /* init after copy */
  37519. HMAC_CTX_init(&ctx1);
  37520. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37521. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37522. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37523. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37524. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37525. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37526. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37527. HMAC_CTX_cleanup(&ctx1);
  37528. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  37529. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37530. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37531. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37532. HMAC_CTX_cleanup(&ctx2);
  37533. ExpectIntEQ(digestSz, digestSz2);
  37534. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37535. /* long key */
  37536. HMAC_CTX_init(&ctx1);
  37537. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  37538. SSL_SUCCESS);
  37539. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37540. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37541. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37542. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37543. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37544. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37545. HMAC_CTX_cleanup(&ctx1);
  37546. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  37547. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37548. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37549. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37550. HMAC_CTX_cleanup(&ctx2);
  37551. ExpectIntEQ(digestSz, digestSz2);
  37552. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37553. /* init before copy */
  37554. HMAC_CTX_init(&ctx1);
  37555. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37556. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37557. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37558. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37559. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37560. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37561. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37562. HMAC_CTX_cleanup(&ctx1);
  37563. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37564. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37565. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37566. HMAC_CTX_cleanup(&ctx2);
  37567. ExpectIntEQ(digestSz, digestSz2);
  37568. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37569. *sz = digestSz;
  37570. return EXPECT_RESULT();
  37571. }
  37572. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  37573. static int test_wolfSSL_HMAC_CTX(void)
  37574. {
  37575. EXPECT_DECLS;
  37576. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  37577. unsigned char digest[64];
  37578. int digestSz;
  37579. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  37580. WOLFSSL_HMAC_CTX ctx1;
  37581. WOLFSSL_HMAC_CTX ctx2;
  37582. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  37583. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  37584. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  37585. wolfSSL_HMAC_CTX_free(NULL);
  37586. wolfSSL_HMAC_CTX_free(hmac_ctx);
  37587. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  37588. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  37589. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  37590. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  37591. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  37592. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  37593. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  37594. ((! defined(HAVE_FIPS_VERSION)) || \
  37595. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  37596. /* Copy object that hasn't had a digest set - MD5. */
  37597. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  37598. #else
  37599. /* Copy object that hasn't had a digest set. */
  37600. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  37601. #endif
  37602. HMAC_CTX_cleanup(NULL);
  37603. HMAC_CTX_cleanup(&ctx2);
  37604. ExpectNull(HMAC_CTX_get_md(NULL));
  37605. #ifndef NO_SHA
  37606. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  37607. TEST_SUCCESS);
  37608. ExpectIntEQ(digestSz, 20);
  37609. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  37610. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  37611. #endif /* !NO_SHA */
  37612. #ifdef WOLFSSL_SHA224
  37613. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  37614. TEST_SUCCESS);
  37615. ExpectIntEQ(digestSz, 28);
  37616. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  37617. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  37618. "\x02\x0E", digest, digestSz), 0);
  37619. #endif /* WOLFSSL_SHA224 */
  37620. #ifndef NO_SHA256
  37621. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  37622. TEST_SUCCESS);
  37623. ExpectIntEQ(digestSz, 32);
  37624. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  37625. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  37626. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  37627. #endif /* !NO_SHA256 */
  37628. #ifdef WOLFSSL_SHA384
  37629. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  37630. TEST_SUCCESS);
  37631. ExpectIntEQ(digestSz, 48);
  37632. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  37633. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  37634. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  37635. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  37636. digest, digestSz), 0);
  37637. #endif /* WOLFSSL_SHA384 */
  37638. #ifdef WOLFSSL_SHA512
  37639. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  37640. TEST_SUCCESS);
  37641. ExpectIntEQ(digestSz, 64);
  37642. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  37643. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  37644. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  37645. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  37646. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  37647. digest, digestSz), 0);
  37648. #endif /* WOLFSSL_SHA512 */
  37649. #ifdef WOLFSSL_SHA3
  37650. #ifndef WOLFSSL_NOSHA3_224
  37651. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  37652. TEST_SUCCESS);
  37653. ExpectIntEQ(digestSz, 28);
  37654. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  37655. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  37656. "\x29\x0f", digest, digestSz), 0);
  37657. #endif
  37658. #ifndef WOLFSSL_NOSHA3_256
  37659. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  37660. TEST_SUCCESS);
  37661. ExpectIntEQ(digestSz, 32);
  37662. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  37663. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  37664. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  37665. #endif
  37666. #ifndef WOLFSSL_NOSHA3_384
  37667. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  37668. TEST_SUCCESS);
  37669. ExpectIntEQ(digestSz, 48);
  37670. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  37671. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  37672. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  37673. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  37674. digest, digestSz), 0);
  37675. #endif
  37676. #ifndef WOLFSSL_NOSHA3_512
  37677. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  37678. TEST_SUCCESS);
  37679. ExpectIntEQ(digestSz, 64);
  37680. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  37681. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  37682. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  37683. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  37684. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  37685. digest, digestSz), 0);
  37686. #endif
  37687. #endif
  37688. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  37689. HAVE_FIPS_VERSION <= 2)
  37690. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  37691. TEST_SUCCESS);
  37692. ExpectIntEQ(digestSz, 16);
  37693. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  37694. "\xE4\x98\xDD", digest, digestSz), 0);
  37695. #endif /* !NO_MD5 */
  37696. #endif
  37697. return EXPECT_RESULT();
  37698. }
  37699. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37700. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37701. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37702. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  37703. {
  37704. EXPECT_DECLS;
  37705. static const unsigned char key[] = "simple test key";
  37706. HMAC_CTX* hmac = NULL;
  37707. ENGINE* e = NULL;
  37708. unsigned char hash[WC_MAX_DIGEST_SIZE];
  37709. unsigned int len;
  37710. ExpectNotNull(hmac = HMAC_CTX_new());
  37711. HMAC_CTX_init(hmac);
  37712. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  37713. ((! defined(HAVE_FIPS_VERSION)) || \
  37714. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  37715. /* Get size on object that hasn't had a digest set - MD5. */
  37716. ExpectIntEQ(HMAC_size(hmac), 16);
  37717. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  37718. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  37719. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  37720. #else
  37721. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  37722. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  37723. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  37724. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  37725. #endif
  37726. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  37727. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  37728. /* re-using test key as data to hash */
  37729. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  37730. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  37731. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  37732. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  37733. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  37734. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  37735. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  37736. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  37737. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  37738. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  37739. ExpectIntEQ(len, md_len);
  37740. ExpectIntEQ(HMAC_size(NULL), 0);
  37741. ExpectIntEQ(HMAC_size(hmac), md_len);
  37742. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  37743. HMAC_cleanup(NULL);
  37744. HMAC_cleanup(hmac);
  37745. HMAC_CTX_free(hmac);
  37746. len = 0;
  37747. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  37748. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  37749. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  37750. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  37751. ExpectIntEQ(len, md_len);
  37752. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  37753. /* With data. */
  37754. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  37755. &len));
  37756. /* With NULL data. */
  37757. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  37758. &len));
  37759. /* With zero length data. */
  37760. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  37761. return EXPECT_RESULT();
  37762. }
  37763. #endif
  37764. static int test_wolfSSL_HMAC(void)
  37765. {
  37766. EXPECT_DECLS;
  37767. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  37768. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  37769. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  37770. #ifndef NO_SHA256
  37771. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  37772. TEST_SUCCESS);
  37773. #endif
  37774. #ifdef WOLFSSL_SHA224
  37775. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  37776. TEST_SUCCESS);
  37777. #endif
  37778. #ifdef WOLFSSL_SHA384
  37779. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  37780. TEST_SUCCESS);
  37781. #endif
  37782. #ifdef WOLFSSL_SHA512
  37783. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  37784. TEST_SUCCESS);
  37785. #endif
  37786. #ifdef WOLFSSL_SHA3
  37787. #ifndef WOLFSSL_NOSHA3_224
  37788. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  37789. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  37790. #endif
  37791. #ifndef WOLFSSL_NOSHA3_256
  37792. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  37793. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  37794. #endif
  37795. #ifndef WOLFSSL_NOSHA3_384
  37796. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  37797. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  37798. #endif
  37799. #ifndef WOLFSSL_NOSHA3_512
  37800. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  37801. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  37802. #endif
  37803. #endif
  37804. #ifndef NO_SHA
  37805. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  37806. TEST_SUCCESS);
  37807. #endif
  37808. #endif
  37809. return EXPECT_RESULT();
  37810. }
  37811. static int test_wolfSSL_CMAC(void)
  37812. {
  37813. EXPECT_DECLS;
  37814. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  37815. defined(WOLFSSL_AES_DIRECT)
  37816. int i;
  37817. byte key[AES_256_KEY_SIZE];
  37818. CMAC_CTX* cmacCtx = NULL;
  37819. byte out[AES_BLOCK_SIZE];
  37820. size_t outLen = AES_BLOCK_SIZE;
  37821. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  37822. key[i] = i;
  37823. }
  37824. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37825. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  37826. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37827. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37828. NULL), 1);
  37829. /* re-using test key as data to hash */
  37830. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37831. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  37832. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  37833. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  37834. /* No Update works. */
  37835. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37836. NULL), 1);
  37837. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37838. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  37839. NULL), 1);
  37840. /* Test parameters with CMAC_Update. */
  37841. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  37842. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  37843. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  37844. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  37845. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  37846. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  37847. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  37848. /* Test parameters with CMAC_Final. */
  37849. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  37850. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  37851. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  37852. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  37853. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  37854. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  37855. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37856. CMAC_CTX_free(cmacCtx);
  37857. /* Test parameters with CMAC Init. */
  37858. cmacCtx = NULL;
  37859. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37860. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  37861. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  37862. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37863. NULL), 0);
  37864. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37865. NULL), 0);
  37866. /* give a key too small for the cipher, verify we get failure */
  37867. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  37868. NULL), 0);
  37869. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  37870. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  37871. /* Only AES-CBC supported. */
  37872. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  37873. NULL), 0);
  37874. #endif
  37875. CMAC_CTX_free(cmacCtx);
  37876. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  37877. cmacCtx = NULL;
  37878. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37879. /* No Init. */
  37880. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  37881. CMAC_CTX_free(cmacCtx);
  37882. /* Test AES-256-CBC */
  37883. cmacCtx = NULL;
  37884. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37885. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  37886. NULL), 1);
  37887. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37888. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37889. CMAC_CTX_free(cmacCtx);
  37890. /* Test AES-192-CBC */
  37891. cmacCtx = NULL;
  37892. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37893. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  37894. NULL), 1);
  37895. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  37896. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  37897. CMAC_CTX_free(cmacCtx);
  37898. cmacCtx = NULL;
  37899. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  37900. CMAC_CTX_free(cmacCtx);
  37901. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  37902. return EXPECT_RESULT();
  37903. }
  37904. static int test_wolfSSL_DES(void)
  37905. {
  37906. EXPECT_DECLS;
  37907. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37908. const_DES_cblock myDes;
  37909. DES_cblock iv;
  37910. DES_key_schedule key;
  37911. word32 i = 0;
  37912. DES_LONG dl = 0;
  37913. unsigned char msg[] = "hello wolfssl";
  37914. unsigned char weakKey[][8] = {
  37915. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  37916. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  37917. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  37918. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  37919. };
  37920. unsigned char semiWeakKey[][8] = {
  37921. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  37922. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  37923. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  37924. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  37925. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  37926. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  37927. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  37928. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  37929. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  37930. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  37931. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  37932. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  37933. };
  37934. DES_check_key(1);
  37935. DES_set_key(&myDes, &key);
  37936. /* check, check of odd parity */
  37937. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  37938. myDes[0] = 6; /* set even parity */
  37939. XMEMSET(key, 5, sizeof(DES_key_schedule));
  37940. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  37941. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  37942. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  37943. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  37944. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  37945. /* set odd parity for success case */
  37946. DES_set_odd_parity(&myDes);
  37947. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  37948. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  37949. myDes[3]);
  37950. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  37951. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37952. ExpectIntEQ(key[i], myDes[i]);
  37953. }
  37954. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  37955. /* check weak key */
  37956. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  37957. XMEMSET(key, 5, sizeof(DES_key_schedule));
  37958. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  37959. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  37960. DES_set_key_unchecked(NULL, NULL);
  37961. DES_set_key_unchecked(&myDes, NULL);
  37962. DES_set_key_unchecked(NULL, &key);
  37963. /* compare arrays, should be the same */
  37964. /* now do unchecked copy of a weak key over */
  37965. DES_set_key_unchecked(&myDes, &key);
  37966. /* compare arrays, should be the same */
  37967. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37968. ExpectIntEQ(key[i], myDes[i]);
  37969. }
  37970. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  37971. myDes[7] = 2;
  37972. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  37973. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  37974. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  37975. /* Test all weak keys. */
  37976. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  37977. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  37978. }
  37979. /* Test all semi-weak keys. */
  37980. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  37981. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  37982. }
  37983. /* check DES_key_sched API */
  37984. XMEMSET(key, 1, sizeof(DES_key_schedule));
  37985. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  37986. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  37987. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  37988. /* compare arrays, should be the same */
  37989. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  37990. ExpectIntEQ(key[i], myDes[i]);
  37991. }
  37992. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  37993. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  37994. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  37995. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  37996. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  37997. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  37998. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  37999. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  38000. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  38001. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  38002. * DES_cbc_encrypt on the input */
  38003. XMEMSET(iv, 0, sizeof(DES_cblock));
  38004. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  38005. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  38006. ExpectIntEQ(dl, 480052723);
  38007. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  38008. return EXPECT_RESULT();
  38009. }
  38010. static int test_wolfSSL_DES_ncbc(void)
  38011. {
  38012. EXPECT_DECLS;
  38013. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38014. const_DES_cblock myDes;
  38015. DES_cblock iv = {1};
  38016. DES_key_schedule key = {0};
  38017. unsigned char msg[] = "hello wolfssl";
  38018. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38019. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38020. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38021. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38022. /* partial block test */
  38023. DES_set_key(&key, &myDes);
  38024. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38025. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38026. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38027. DES_set_key(&key, &myDes);
  38028. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38029. *((byte*)&iv) = 1;
  38030. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38031. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38032. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38033. /* full block test */
  38034. DES_set_key(&key, &myDes);
  38035. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38036. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38037. *((byte*)&iv) = 1;
  38038. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38039. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38040. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38041. DES_set_key(&key, &myDes);
  38042. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38043. *((byte*)&iv) = 1;
  38044. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38045. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38046. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38047. #endif
  38048. return EXPECT_RESULT();
  38049. }
  38050. static int test_wolfSSL_DES_ecb_encrypt(void)
  38051. {
  38052. EXPECT_DECLS;
  38053. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  38054. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  38055. WOLFSSL_DES_key_schedule key;
  38056. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38057. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  38058. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  38059. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  38060. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  38061. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  38062. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  38063. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  38064. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  38065. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  38066. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  38067. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  38068. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  38069. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  38070. /* Encrypt messages */
  38071. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  38072. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  38073. {
  38074. /* Decrypt messages */
  38075. int ret1 = 0;
  38076. int ret2 = 0;
  38077. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  38078. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  38079. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  38080. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  38081. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  38082. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  38083. }
  38084. #endif
  38085. return EXPECT_RESULT();
  38086. }
  38087. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  38088. {
  38089. EXPECT_DECLS;
  38090. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38091. unsigned char input1[8], input2[8];
  38092. unsigned char output1[8], output2[8];
  38093. unsigned char back1[8], back2[8];
  38094. WOLFSSL_DES_cblock iv1, iv2;
  38095. WOLFSSL_DES_key_schedule key1, key2, key3;
  38096. int i;
  38097. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38098. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  38099. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  38100. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  38101. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  38102. XMEMSET(output1, 0, sizeof(output1));
  38103. XMEMSET(output2, 0, sizeof(output2));
  38104. XMEMSET(back1, 0, sizeof(back1));
  38105. XMEMSET(back2, 0, sizeof(back2));
  38106. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38107. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38108. /* Encrypt messages */
  38109. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  38110. DES_ENCRYPT);
  38111. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  38112. DES_ENCRYPT);
  38113. {
  38114. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38115. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38116. /* Decrypt messages */
  38117. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  38118. &iv1, DES_DECRYPT);
  38119. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  38120. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  38121. &iv2, DES_DECRYPT);
  38122. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  38123. }
  38124. for (i = 0; i < 8; i++) {
  38125. XMEMSET(output1, 0, sizeof(output1));
  38126. XMEMSET(output2, 0, sizeof(output2));
  38127. XMEMSET(back1, 0, sizeof(back1));
  38128. XMEMSET(back2, 0, sizeof(back2));
  38129. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38130. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38131. /* Encrypt partial messages */
  38132. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  38133. &iv1, DES_ENCRYPT);
  38134. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  38135. &iv2, DES_ENCRYPT);
  38136. {
  38137. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38138. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38139. /* Decrypt messages */
  38140. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  38141. &key3, &iv1, DES_DECRYPT);
  38142. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  38143. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  38144. &key3, &iv2, DES_DECRYPT);
  38145. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  38146. }
  38147. }
  38148. #endif
  38149. return EXPECT_RESULT();
  38150. }
  38151. static int test_wolfSSL_AES_encrypt(void)
  38152. {
  38153. EXPECT_DECLS;
  38154. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38155. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38156. AES_KEY enc;
  38157. AES_KEY dec;
  38158. const byte msg[] = {
  38159. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38160. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  38161. };
  38162. const byte exp[] = {
  38163. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  38164. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  38165. };
  38166. const byte key[] = {
  38167. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38168. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38169. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38170. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38171. };
  38172. byte eout[sizeof(msg)];
  38173. byte dout[sizeof(msg)];
  38174. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  38175. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  38176. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  38177. wolfSSL_AES_encrypt(msg, NULL, NULL);
  38178. wolfSSL_AES_encrypt(NULL, eout, NULL);
  38179. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  38180. wolfSSL_AES_encrypt(msg, eout, NULL);
  38181. wolfSSL_AES_encrypt(msg, NULL, &enc);
  38182. wolfSSL_AES_encrypt(NULL, eout, &enc);
  38183. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  38184. wolfSSL_AES_decrypt(eout, NULL, NULL);
  38185. wolfSSL_AES_decrypt(NULL, dout, NULL);
  38186. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  38187. wolfSSL_AES_decrypt(eout, dout, NULL);
  38188. wolfSSL_AES_decrypt(eout, NULL, &dec);
  38189. wolfSSL_AES_decrypt(NULL, dout, &dec);
  38190. wolfSSL_AES_encrypt(msg, eout, &enc);
  38191. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  38192. wolfSSL_AES_decrypt(eout, dout, &dec);
  38193. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  38194. #endif
  38195. return EXPECT_RESULT();
  38196. }
  38197. static int test_wolfSSL_AES_ecb_encrypt(void)
  38198. {
  38199. EXPECT_DECLS;
  38200. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38201. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38202. AES_KEY aes;
  38203. const byte msg[] =
  38204. {
  38205. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38206. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  38207. };
  38208. const byte verify[] =
  38209. {
  38210. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  38211. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  38212. };
  38213. const byte key[] =
  38214. {
  38215. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38216. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38217. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38218. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  38219. };
  38220. byte out[AES_BLOCK_SIZE];
  38221. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  38222. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38223. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  38224. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  38225. #ifdef HAVE_AES_DECRYPT
  38226. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  38227. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38228. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  38229. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  38230. #endif
  38231. /* test bad arguments */
  38232. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  38233. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  38234. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  38235. #endif
  38236. return EXPECT_RESULT();
  38237. }
  38238. static int test_wolfSSL_AES_cbc_encrypt(void)
  38239. {
  38240. EXPECT_DECLS;
  38241. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38242. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38243. AES_KEY aes;
  38244. AES_KEY* aesN = NULL;
  38245. size_t len = 0;
  38246. size_t lenB = 0;
  38247. int keySz0 = 0;
  38248. int keySzN = -1;
  38249. byte out[AES_BLOCK_SIZE] = {0};
  38250. byte* outN = NULL;
  38251. /* Test vectors retrieved from:
  38252. * <begin URL>
  38253. * https://csrc.nist.gov/
  38254. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38255. * documents/aes/KAT_AES.zip
  38256. * </end URL>
  38257. */
  38258. const byte* pt128N = NULL;
  38259. byte* key128N = NULL;
  38260. byte* iv128N = NULL;
  38261. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38262. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38263. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38264. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38265. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38266. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38267. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38268. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38269. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38270. len = sizeof(pt128);
  38271. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38272. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38273. ExpectIntNE(XMEMCMP(b, g, h), i)
  38274. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38275. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38276. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38277. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38278. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38279. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38280. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38281. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38282. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38283. /* Stressing wolfSSL_AES_set_encrypt_key */
  38284. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38285. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38286. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38287. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38288. /* Stressing wolfSSL_AES_set_decrypt_key */
  38289. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38290. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38291. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38292. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38293. #ifdef WOLFSSL_AES_128
  38294. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38295. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38296. RESET_IV(iv128tmp, iv128);
  38297. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38298. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38299. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38300. wc_AesFree((Aes*)&aes);
  38301. #ifdef HAVE_AES_DECRYPT
  38302. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38303. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38304. RESET_IV(iv128tmp, iv128);
  38305. len = sizeof(ct128);
  38306. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38307. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38308. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38309. wc_AesFree((Aes*)&aes);
  38310. #endif
  38311. #endif /* WOLFSSL_AES_128 */
  38312. #ifdef WOLFSSL_AES_192
  38313. {
  38314. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38315. * Appendix F.2.3 */
  38316. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38317. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38318. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38319. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38320. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38321. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38322. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38323. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38324. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38325. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38326. len = sizeof(pt192);
  38327. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38328. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38329. RESET_IV(iv192tmp, iv192);
  38330. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38331. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38332. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38333. wc_AesFree((Aes*)&aes);
  38334. #ifdef HAVE_AES_DECRYPT
  38335. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38336. len = sizeof(ct192);
  38337. RESET_IV(iv192tmp, iv192);
  38338. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38339. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38340. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38341. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38342. wc_AesFree((Aes*)&aes);
  38343. #endif
  38344. }
  38345. #endif /* WOLFSSL_AES_192 */
  38346. #ifdef WOLFSSL_AES_256
  38347. {
  38348. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38349. * Appendix F.2.5 */
  38350. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38351. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38352. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38353. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38354. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38355. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38356. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38357. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38358. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38359. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38360. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38361. len = sizeof(pt256);
  38362. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38363. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38364. RESET_IV(iv256tmp, iv256);
  38365. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38366. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38367. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38368. wc_AesFree((Aes*)&aes);
  38369. #ifdef HAVE_AES_DECRYPT
  38370. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38371. len = sizeof(ct256);
  38372. RESET_IV(iv256tmp, iv256);
  38373. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38374. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38375. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  38376. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  38377. wc_AesFree((Aes*)&aes);
  38378. #endif
  38379. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  38380. !defined(HAVE_SELFTEST)
  38381. {
  38382. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  38383. byte wrapPlain[sizeof(key256)] = { 0 };
  38384. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  38385. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  38386. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38387. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38388. 15), WOLFSSL_FAILURE);
  38389. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38390. sizeof(key256)), sizeof(wrapCipher));
  38391. wc_AesFree((Aes*)&aes);
  38392. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  38393. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38394. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38395. 23), WOLFSSL_FAILURE);
  38396. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38397. sizeof(wrapCipher)), sizeof(wrapPlain));
  38398. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38399. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  38400. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  38401. wc_AesFree((Aes*)&aes);
  38402. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  38403. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38404. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  38405. sizeof(key256)), sizeof(wrapCipher));
  38406. wc_AesFree((Aes*)&aes);
  38407. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  38408. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38409. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  38410. sizeof(wrapCipher)), sizeof(wrapPlain));
  38411. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38412. wc_AesFree((Aes*)&aes);
  38413. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  38414. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  38415. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  38416. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  38417. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  38418. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  38419. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  38420. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  38421. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  38422. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  38423. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  38424. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  38425. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  38426. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  38427. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  38428. 0);
  38429. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  38430. 0);
  38431. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  38432. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  38433. }
  38434. #endif /* HAVE_AES_KEYWRAP */
  38435. }
  38436. #endif /* WOLFSSL_AES_256 */
  38437. #endif
  38438. return EXPECT_RESULT();
  38439. }
  38440. static int test_wolfSSL_AES_cfb128_encrypt(void)
  38441. {
  38442. EXPECT_DECLS;
  38443. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  38444. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38445. AES_KEY aesEnc;
  38446. AES_KEY aesDec;
  38447. const byte msg[] = {
  38448. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38449. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  38450. };
  38451. const byte exp[] = {
  38452. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  38453. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  38454. };
  38455. const byte key[] = {
  38456. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38457. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38458. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38459. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38460. };
  38461. const byte ivData[] = {
  38462. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38463. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38464. };
  38465. byte out[AES_BLOCK_SIZE];
  38466. byte iv[AES_BLOCK_SIZE];
  38467. word32 i;
  38468. int num;
  38469. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  38470. XMEMCPY(iv, ivData, sizeof(iv));
  38471. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38472. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  38473. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  38474. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38475. #ifdef HAVE_AES_DECRYPT
  38476. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  38477. XMEMCPY(iv, ivData, sizeof(iv));
  38478. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38479. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  38480. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  38481. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38482. #endif
  38483. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  38484. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  38485. XMEMCPY(iv, ivData, sizeof(iv));
  38486. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38487. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  38488. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  38489. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  38490. if (i == 0) {
  38491. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38492. }
  38493. else {
  38494. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38495. }
  38496. #ifdef HAVE_AES_DECRYPT
  38497. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  38498. XMEMCPY(iv, ivData, sizeof(iv));
  38499. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38500. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  38501. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  38502. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  38503. if (i == 0) {
  38504. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38505. }
  38506. else {
  38507. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  38508. }
  38509. #endif
  38510. }
  38511. if (EXPECT_SUCCESS()) {
  38512. /* test bad arguments */
  38513. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  38514. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  38515. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  38516. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  38517. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  38518. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  38519. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  38520. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  38521. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  38522. }
  38523. #endif
  38524. return EXPECT_RESULT();
  38525. }
  38526. static int test_wolfSSL_CRYPTO_cts128(void)
  38527. {
  38528. EXPECT_DECLS;
  38529. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38530. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38531. byte tmp[64]; /* Largest vector size */
  38532. /* Test vectors taken form RFC3962 Appendix B */
  38533. const testVector vects[] = {
  38534. {
  38535. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38536. "\x20",
  38537. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  38538. "\x97",
  38539. 17, 17
  38540. },
  38541. {
  38542. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38543. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  38544. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  38545. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  38546. 31, 31
  38547. },
  38548. {
  38549. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38550. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38551. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38552. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38553. 32, 32
  38554. },
  38555. {
  38556. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38557. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38558. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38559. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38560. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38561. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38562. 47, 47
  38563. },
  38564. {
  38565. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38566. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38567. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38568. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38569. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38570. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38571. 48, 48
  38572. },
  38573. {
  38574. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38575. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38576. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38577. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38578. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38579. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38580. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38581. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38582. 64, 64
  38583. }
  38584. };
  38585. byte keyBytes[AES_128_KEY_SIZE] = {
  38586. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38587. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38588. };
  38589. size_t i;
  38590. AES_KEY encKey;
  38591. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38592. XMEMSET(tmp, 0, sizeof(tmp));
  38593. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38594. AES_KEY decKey;
  38595. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38596. &encKey), 0);
  38597. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38598. &decKey), 0);
  38599. XMEMSET(iv, 0, sizeof(iv));
  38600. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38601. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38602. vects[i].outLen);
  38603. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38604. XMEMSET(iv, 0, sizeof(iv));
  38605. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38606. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38607. vects[i].inLen);
  38608. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38609. }
  38610. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  38611. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  38612. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  38613. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  38614. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  38615. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  38616. (cbc128_f)AES_cbc_encrypt), 0);
  38617. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  38618. (cbc128_f)AES_cbc_encrypt), 0);
  38619. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  38620. (cbc128_f)AES_cbc_encrypt), 0);
  38621. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  38622. (cbc128_f)AES_cbc_encrypt), 0);
  38623. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  38624. (cbc128_f)AES_cbc_encrypt), 0);
  38625. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  38626. /* Length too small. */
  38627. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  38628. (cbc128_f)AES_cbc_encrypt), 0);
  38629. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  38630. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  38631. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  38632. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  38633. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  38634. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  38635. (cbc128_f)AES_cbc_encrypt), 0);
  38636. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  38637. (cbc128_f)AES_cbc_encrypt), 0);
  38638. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  38639. (cbc128_f)AES_cbc_encrypt), 0);
  38640. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  38641. (cbc128_f)AES_cbc_encrypt), 0);
  38642. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  38643. (cbc128_f)AES_cbc_encrypt), 0);
  38644. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  38645. /* Length too small. */
  38646. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  38647. (cbc128_f)AES_cbc_encrypt), 0);
  38648. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38649. return EXPECT_RESULT();
  38650. }
  38651. static int test_wolfSSL_RC4(void)
  38652. {
  38653. EXPECT_DECLS;
  38654. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  38655. WOLFSSL_RC4_KEY rc4Key;
  38656. unsigned char key[] = {
  38657. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38658. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38659. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38660. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  38661. };
  38662. unsigned char data[] = {
  38663. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38664. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38665. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38666. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  38667. };
  38668. unsigned char enc[sizeof(data)];
  38669. unsigned char dec[sizeof(data)];
  38670. word32 i;
  38671. word32 j;
  38672. wolfSSL_RC4_set_key(NULL, -1, NULL);
  38673. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  38674. wolfSSL_RC4_set_key(NULL, 0, NULL);
  38675. wolfSSL_RC4_set_key(NULL, -1, key);
  38676. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  38677. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  38678. wolfSSL_RC4_set_key(NULL, 0, key);
  38679. wolfSSL_RC4(NULL, 0, NULL, NULL);
  38680. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  38681. wolfSSL_RC4(NULL, 0, data, NULL);
  38682. wolfSSL_RC4(NULL, 0, NULL, enc);
  38683. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  38684. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  38685. wolfSSL_RC4(NULL, 0, data, enc);
  38686. ExpectIntEQ(1, 1);
  38687. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  38688. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  38689. XMEMSET(enc, 0, sizeof(enc));
  38690. XMEMSET(dec, 0, sizeof(dec));
  38691. /* Encrypt */
  38692. wolfSSL_RC4_set_key(&rc4Key, i, key);
  38693. wolfSSL_RC4(&rc4Key, j, data, enc);
  38694. /* Decrypt */
  38695. wolfSSL_RC4_set_key(&rc4Key, i, key);
  38696. wolfSSL_RC4(&rc4Key, j, enc, dec);
  38697. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  38698. }
  38699. }
  38700. #endif
  38701. return EXPECT_RESULT();
  38702. }
  38703. static int test_wolfSSL_OBJ(void)
  38704. {
  38705. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  38706. * mode
  38707. */
  38708. EXPECT_DECLS;
  38709. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  38710. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  38711. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  38712. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  38713. ASN1_OBJECT *obj = NULL;
  38714. ASN1_OBJECT *obj2 = NULL;
  38715. char buf[50];
  38716. XFILE fp = XBADFILE;
  38717. X509 *x509 = NULL;
  38718. X509_NAME *x509Name = NULL;
  38719. X509_NAME_ENTRY *x509NameEntry = NULL;
  38720. ASN1_OBJECT *asn1Name = NULL;
  38721. int numNames = 0;
  38722. BIO *bio = NULL;
  38723. int nid;
  38724. int i, j;
  38725. const char *f[] = {
  38726. #ifndef NO_RSA
  38727. "./certs/ca-cert.der",
  38728. #endif
  38729. #ifdef HAVE_ECC
  38730. "./certs/ca-ecc-cert.der",
  38731. "./certs/ca-ecc384-cert.der",
  38732. #endif
  38733. NULL};
  38734. ASN1_OBJECT *field_name_obj = NULL;
  38735. int lastpos = -1;
  38736. int tmp = -1;
  38737. ASN1_STRING *asn1 = NULL;
  38738. unsigned char *buf_dyn = NULL;
  38739. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  38740. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  38741. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  38742. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  38743. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  38744. ASN1_OBJECT_free(obj);
  38745. obj = NULL;
  38746. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  38747. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  38748. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  38749. #ifdef WOLFSSL_CERT_EXT
  38750. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  38751. #endif
  38752. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  38753. ExpectNotNull(obj2 = OBJ_dup(obj));
  38754. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  38755. ASN1_OBJECT_free(obj);
  38756. obj = NULL;
  38757. ASN1_OBJECT_free(obj2);
  38758. obj2 = NULL;
  38759. for (i = 0; f[i] != NULL; i++)
  38760. {
  38761. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  38762. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  38763. if (fp != XBADFILE) {
  38764. XFCLOSE(fp);
  38765. fp = XBADFILE;
  38766. }
  38767. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  38768. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  38769. /* Get the Common Name by using OBJ_txt2obj */
  38770. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  38771. do
  38772. {
  38773. lastpos = tmp;
  38774. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  38775. } while (tmp > -1);
  38776. ExpectIntNE(lastpos, -1);
  38777. ASN1_OBJECT_free(field_name_obj);
  38778. field_name_obj = NULL;
  38779. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  38780. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  38781. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  38782. /*
  38783. * All Common Names should be www.wolfssl.com
  38784. * This makes testing easier as we can test for the expected value.
  38785. */
  38786. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  38787. OPENSSL_free(buf_dyn);
  38788. buf_dyn = NULL;
  38789. bio = BIO_new(BIO_s_mem());
  38790. ExpectTrue(bio != NULL);
  38791. for (j = 0; j < numNames; j++)
  38792. {
  38793. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  38794. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  38795. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  38796. }
  38797. BIO_free(bio);
  38798. bio = NULL;
  38799. X509_free(x509);
  38800. x509 = NULL;
  38801. }
  38802. #ifdef HAVE_PKCS12
  38803. {
  38804. PKCS12 *p12 = NULL;
  38805. int boolRet;
  38806. EVP_PKEY *pkey = NULL;
  38807. const char *p12_f[] = {
  38808. #if !defined(NO_DES3) && !defined(NO_RSA)
  38809. "./certs/test-servercert.p12",
  38810. #endif
  38811. NULL};
  38812. for (i = 0; p12_f[i] != NULL; i++)
  38813. {
  38814. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  38815. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  38816. if (fp != XBADFILE) {
  38817. XFCLOSE(fp);
  38818. fp = XBADFILE;
  38819. }
  38820. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  38821. &pkey, &x509, NULL)) > 0);
  38822. wc_PKCS12_free(p12);
  38823. p12 = NULL;
  38824. EVP_PKEY_free(pkey);
  38825. x509Name = X509_get_issuer_name(x509);
  38826. ExpectNotNull(x509Name);
  38827. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  38828. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  38829. for (j = 0; j < numNames; j++)
  38830. {
  38831. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  38832. ExpectNotNull(asn1Name =
  38833. X509_NAME_ENTRY_get_object(x509NameEntry));
  38834. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  38835. }
  38836. BIO_free(bio);
  38837. bio = NULL;
  38838. X509_free(x509);
  38839. x509 = NULL;
  38840. }
  38841. }
  38842. #endif /* HAVE_PKCS12 */
  38843. #endif
  38844. return EXPECT_RESULT();
  38845. }
  38846. static int test_wolfSSL_OBJ_cmp(void)
  38847. {
  38848. EXPECT_DECLS;
  38849. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  38850. ASN1_OBJECT *obj = NULL;
  38851. ASN1_OBJECT *obj2 = NULL;
  38852. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  38853. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  38854. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  38855. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  38856. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  38857. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  38858. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  38859. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  38860. ASN1_OBJECT_free(obj);
  38861. ASN1_OBJECT_free(obj2);
  38862. #endif
  38863. return EXPECT_RESULT();
  38864. }
  38865. static int test_wolfSSL_OBJ_txt2nid(void)
  38866. {
  38867. EXPECT_DECLS;
  38868. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  38869. defined(WOLFSSL_APACHE_HTTPD)
  38870. int i;
  38871. static const struct {
  38872. const char* sn;
  38873. const char* ln;
  38874. const char* oid;
  38875. int nid;
  38876. } testVals[] = {
  38877. #ifdef WOLFSSL_APACHE_HTTPD
  38878. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  38879. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  38880. NID_id_on_dnsSRV },
  38881. { "msUPN", "Microsoft User Principal Name",
  38882. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  38883. #endif
  38884. { NULL, NULL, NULL, NID_undef }
  38885. };
  38886. /* Invalid cases */
  38887. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  38888. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  38889. /* Valid cases */
  38890. for (i = 0; testVals[i].sn != NULL; i++) {
  38891. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  38892. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  38893. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  38894. }
  38895. #endif
  38896. return EXPECT_RESULT();
  38897. }
  38898. static int test_wolfSSL_OBJ_txt2obj(void)
  38899. {
  38900. EXPECT_DECLS;
  38901. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  38902. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  38903. int i;
  38904. char buf[50];
  38905. ASN1_OBJECT* obj = NULL;
  38906. static const struct {
  38907. const char* oidStr;
  38908. const char* sn;
  38909. const char* ln;
  38910. } objs_list[] = {
  38911. #if defined(WOLFSSL_APACHE_HTTPD)
  38912. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  38913. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  38914. #endif
  38915. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  38916. { NULL, NULL, NULL }
  38917. };
  38918. static const struct {
  38919. const char* numeric;
  38920. const char* name;
  38921. } objs_named[] = {
  38922. /* In dictionary but not in normal list. */
  38923. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  38924. /* Made up OID. */
  38925. { "1.3.5.7", "1.3.5.7" },
  38926. { NULL, NULL }
  38927. };
  38928. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  38929. ASN1_OBJECT_free(obj);
  38930. obj = NULL;
  38931. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  38932. ASN1_OBJECT_free(obj);
  38933. obj = NULL;
  38934. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  38935. /* Test numerical value of oid (oidStr) */
  38936. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  38937. /* Convert object back to text to confirm oid is correct */
  38938. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38939. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38940. ASN1_OBJECT_free(obj);
  38941. obj = NULL;
  38942. XMEMSET(buf, 0, sizeof(buf));
  38943. /* Test short name (sn) */
  38944. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  38945. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  38946. /* Convert object back to text to confirm oid is correct */
  38947. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38948. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38949. ASN1_OBJECT_free(obj);
  38950. obj = NULL;
  38951. XMEMSET(buf, 0, sizeof(buf));
  38952. /* Test long name (ln) - should fail when no_name = 1 */
  38953. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  38954. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  38955. /* Convert object back to text to confirm oid is correct */
  38956. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38957. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  38958. ASN1_OBJECT_free(obj);
  38959. obj = NULL;
  38960. XMEMSET(buf, 0, sizeof(buf));
  38961. }
  38962. for (i = 0; objs_named[i].numeric != NULL; i++) {
  38963. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  38964. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  38965. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  38966. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  38967. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  38968. ASN1_OBJECT_free(obj);
  38969. obj = NULL;
  38970. }
  38971. #endif
  38972. return EXPECT_RESULT();
  38973. }
  38974. static int test_wolfSSL_PEM_write_bio_X509(void)
  38975. {
  38976. EXPECT_DECLS;
  38977. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  38978. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  38979. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  38980. !defined(NO_FILESYSTEM)
  38981. /* This test contains the hard coded expected
  38982. * lengths. Update if necessary */
  38983. XFILE fp = XBADFILE;
  38984. WOLFSSL_EVP_PKEY *priv = NULL;
  38985. BIO* input = NULL;
  38986. BIO* output = NULL;
  38987. X509* x509a = NULL;
  38988. X509* x509b = NULL;
  38989. ASN1_TIME* notBeforeA = NULL;
  38990. ASN1_TIME* notAfterA = NULL;
  38991. #ifndef NO_ASN_TIME
  38992. ASN1_TIME* notBeforeB = NULL;
  38993. ASN1_TIME* notAfterB = NULL;
  38994. #endif
  38995. int expectedLen;
  38996. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  38997. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  38998. if (fp != XBADFILE) {
  38999. XFCLOSE(fp);
  39000. fp = XBADFILE;
  39001. }
  39002. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  39003. "rb"));
  39004. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  39005. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  39006. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39007. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  39008. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  39009. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  39010. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39011. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39012. /* compare length against expected */
  39013. expectedLen = 2000;
  39014. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39015. #ifndef NO_ASN_TIME
  39016. /* read exported X509 PEM back into struct, sanity check on export,
  39017. * make sure notBefore/notAfter are the same and certs are identical. */
  39018. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39019. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  39020. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  39021. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  39022. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  39023. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39024. X509_free(x509b);
  39025. x509b = NULL;
  39026. #endif
  39027. /* Reset output buffer */
  39028. BIO_free(output);
  39029. output = NULL;
  39030. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39031. /* Test forcing the AKID to be generated just from KeyIdentifier */
  39032. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  39033. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  39034. x509a->authKeyId = x509a->authKeyIdSrc;
  39035. x509a->authKeyIdSrc = NULL;
  39036. x509a->authKeyIdSrcSz = 0;
  39037. }
  39038. /* Resign to re-generate the der */
  39039. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  39040. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39041. /* Check that we generate a smaller output since the AKID will
  39042. * only contain the KeyIdentifier without any additional
  39043. * information */
  39044. /* Here we copy the validity struct from the original */
  39045. expectedLen = 1688;
  39046. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39047. /* Reset buffers and x509 */
  39048. BIO_free(input);
  39049. input = NULL;
  39050. BIO_free(output);
  39051. output = NULL;
  39052. X509_free(x509a);
  39053. x509a = NULL;
  39054. /* test CA and basicConstSet values are encoded when
  39055. * the cert is a CA */
  39056. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  39057. /* read PEM into X509 struct */
  39058. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39059. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39060. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39061. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39062. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  39063. * values are maintained and certs are identical.*/
  39064. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39065. ExpectIntEQ(x509b->isCa, 1);
  39066. ExpectIntEQ(x509b->basicConstSet, 1);
  39067. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39068. X509_free(x509a);
  39069. x509a = NULL;
  39070. X509_free(x509b);
  39071. x509b = NULL;
  39072. BIO_free(input);
  39073. input = NULL;
  39074. BIO_free(output);
  39075. output = NULL;
  39076. /* test CA and basicConstSet values are encoded when
  39077. * the cert is not CA */
  39078. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  39079. /* read PEM into X509 struct */
  39080. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39081. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39082. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39083. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39084. /* read exported X509 PEM back into struct, ensure isCa and
  39085. * basicConstSet values are maintained and certs are identical */
  39086. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39087. ExpectIntEQ(x509b->isCa, 0);
  39088. ExpectIntEQ(x509b->basicConstSet, 1);
  39089. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39090. wolfSSL_EVP_PKEY_free(priv);
  39091. X509_free(x509a);
  39092. X509_free(x509b);
  39093. BIO_free(input);
  39094. BIO_free(output);
  39095. #endif
  39096. return EXPECT_RESULT();
  39097. }
  39098. static int test_wolfSSL_X509_NAME_ENTRY(void)
  39099. {
  39100. EXPECT_DECLS;
  39101. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  39102. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  39103. X509* x509 = NULL;
  39104. #ifndef NO_BIO
  39105. BIO* bio = NULL;
  39106. #endif
  39107. X509_NAME* nm = NULL;
  39108. X509_NAME_ENTRY* entry = NULL;
  39109. unsigned char cn[] = "another name to add";
  39110. #ifdef OPENSSL_ALL
  39111. int i;
  39112. int names_len = 0;
  39113. #endif
  39114. ExpectNotNull(x509 =
  39115. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39116. #ifndef NO_BIO
  39117. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39118. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  39119. #endif
  39120. #ifdef WOLFSSL_CERT_REQ
  39121. {
  39122. X509_REQ* req = NULL;
  39123. #ifndef NO_BIO
  39124. BIO* bReq = NULL;
  39125. #endif
  39126. ExpectNotNull(req =
  39127. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39128. #ifndef NO_BIO
  39129. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  39130. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  39131. BIO_free(bReq);
  39132. #endif
  39133. X509_free(req);
  39134. }
  39135. #endif
  39136. ExpectNotNull(nm = X509_get_subject_name(x509));
  39137. /* Test add entry */
  39138. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  39139. 0x0c, cn, (int)sizeof(cn)));
  39140. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39141. #ifdef WOLFSSL_CERT_EXT
  39142. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  39143. (byte*)"support@wolfssl.com", 19, -1,
  39144. 1), WOLFSSL_SUCCESS);
  39145. #endif
  39146. X509_NAME_ENTRY_free(entry);
  39147. entry = NULL;
  39148. #ifdef WOLFSSL_CERT_REQ
  39149. {
  39150. unsigned char srv_pkcs9p[] = "Server";
  39151. unsigned char fvrtDrnk[] = "tequila";
  39152. unsigned char* der = NULL;
  39153. char* subject = NULL;
  39154. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  39155. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  39156. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  39157. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  39158. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  39159. ExpectNotNull(der);
  39160. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  39161. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  39162. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  39163. #ifdef DEBUG_WOLFSSL
  39164. if (subject != NULL) {
  39165. fprintf(stderr, "\n\t%s\n", subject);
  39166. }
  39167. #endif
  39168. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  39169. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39170. }
  39171. #endif
  39172. /* Test add entry by text */
  39173. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  39174. 0x0c, cn, (int)sizeof(cn)));
  39175. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  39176. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  39177. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  39178. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  39179. #endif
  39180. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39181. X509_NAME_ENTRY_free(entry);
  39182. entry = NULL;
  39183. /* Test add entry by NID */
  39184. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  39185. cn, -1, -1, 0), SSL_SUCCESS);
  39186. #ifdef OPENSSL_ALL
  39187. /* stack of name entry */
  39188. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  39189. for (i = 0; i < names_len; i++) {
  39190. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  39191. }
  39192. #endif
  39193. #ifndef NO_BIO
  39194. BIO_free(bio);
  39195. #endif
  39196. X509_free(x509); /* free's nm */
  39197. #endif
  39198. return EXPECT_RESULT();
  39199. }
  39200. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39201. static int test_GENERAL_NAME_set0_othername(void) {
  39202. EXPECT_DECLS;
  39203. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39204. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39205. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39206. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39207. defined(WOLFSSL_FPKI)
  39208. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39209. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39210. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39211. const char * cert_fname = "./certs/server-cert.der";
  39212. const char * key_fname = "./certs/server-key.der";
  39213. X509* x509 = NULL;
  39214. GENERAL_NAME* gn = NULL;
  39215. GENERAL_NAMES* gns = NULL;
  39216. ASN1_OBJECT* upn_oid = NULL;
  39217. ASN1_UTF8STRING *utf8str = NULL;
  39218. ASN1_TYPE *value = NULL;
  39219. X509_EXTENSION * ext = NULL;
  39220. byte* pt = NULL;
  39221. byte der[4096];
  39222. int derSz = 0;
  39223. EVP_PKEY* priv = NULL;
  39224. XFILE f = XBADFILE;
  39225. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  39226. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  39227. if (f != XBADFILE) {
  39228. XFCLOSE(f);
  39229. f = XBADFILE;
  39230. }
  39231. ExpectNotNull(gn = GENERAL_NAME_new());
  39232. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39233. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39234. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39235. ExpectNotNull(value = ASN1_TYPE_new());
  39236. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39237. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  39238. wolfSSL_ASN1_STRING_free(utf8str);
  39239. }
  39240. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39241. if (EXPECT_FAIL()) {
  39242. ASN1_TYPE_free(value);
  39243. }
  39244. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39245. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39246. if (EXPECT_FAIL()) {
  39247. GENERAL_NAME_free(gn);
  39248. gn = NULL;
  39249. }
  39250. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39251. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  39252. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39253. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39254. if (f != XBADFILE) {
  39255. XFCLOSE(f);
  39256. f = XBADFILE;
  39257. }
  39258. pt = der;
  39259. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39260. (const unsigned char**)&pt, derSz));
  39261. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  39262. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39263. gns = NULL;
  39264. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39265. NULL));
  39266. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  39267. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  39268. ExpectIntEQ(gn->type, 0);
  39269. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39270. ASN1_OBJECT_free(upn_oid);
  39271. X509_EXTENSION_free(ext);
  39272. X509_free(x509);
  39273. EVP_PKEY_free(priv);
  39274. #endif
  39275. return EXPECT_RESULT();
  39276. }
  39277. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39278. static int test_othername_and_SID_ext(void) {
  39279. EXPECT_DECLS;
  39280. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39281. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39282. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39283. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39284. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  39285. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39286. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39287. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39288. const char* csr_fname = "./certs/csr.signed.der";
  39289. const char* key_fname = "./certs/server-key.der";
  39290. byte der[4096];
  39291. int derSz = 0;
  39292. X509_REQ* x509 = NULL;
  39293. STACK_OF(X509_EXTENSION) *exts = NULL;
  39294. X509_EXTENSION * san_ext = NULL;
  39295. X509_EXTENSION * ext = NULL;
  39296. GENERAL_NAME* gn = NULL;
  39297. GENERAL_NAMES* gns = NULL;
  39298. ASN1_OBJECT* upn_oid = NULL;
  39299. ASN1_UTF8STRING *utf8str = NULL;
  39300. ASN1_TYPE *value = NULL;
  39301. ASN1_STRING *extval = NULL;
  39302. /* SID extension. SID data format explained here:
  39303. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  39304. */
  39305. uint8_t SidExtension[] = {
  39306. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  39307. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  39308. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  39309. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  39310. uint8_t expectedAltName[] = {
  39311. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  39312. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  39313. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  39314. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  39315. X509_EXTENSION *sid_ext = NULL;
  39316. ASN1_OBJECT* sid_oid = NULL;
  39317. ASN1_OCTET_STRING *sid_data = NULL;
  39318. EVP_PKEY* priv = NULL;
  39319. XFILE f = XBADFILE;
  39320. byte* pt = NULL;
  39321. BIO* bio = NULL;
  39322. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  39323. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  39324. if (f != XBADFILE) {
  39325. XFCLOSE(f);
  39326. f = XBADFILE;
  39327. }
  39328. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  39329. ExpectNotNull(gn = GENERAL_NAME_new());
  39330. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39331. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39332. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39333. ExpectNotNull(value = ASN1_TYPE_new());
  39334. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39335. if (EXPECT_FAIL()) {
  39336. ASN1_UTF8STRING_free(utf8str);
  39337. }
  39338. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39339. if (EXPECT_FAIL()) {
  39340. ASN1_TYPE_free(value);
  39341. GENERAL_NAME_free(gn);
  39342. gn = NULL;
  39343. }
  39344. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39345. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39346. if (EXPECT_FAIL()) {
  39347. GENERAL_NAME_free(gn);
  39348. }
  39349. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39350. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  39351. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  39352. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  39353. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  39354. sid_data));
  39355. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  39356. /* Ensure an empty stack doesn't raise an error. */
  39357. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39358. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  39359. if (EXPECT_FAIL()) {
  39360. X509_EXTENSION_free(san_ext);
  39361. }
  39362. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  39363. if (EXPECT_FAIL()) {
  39364. X509_EXTENSION_free(sid_ext);
  39365. }
  39366. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39367. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39368. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39369. if (f != XBADFILE)
  39370. XFCLOSE(f);
  39371. pt = der;
  39372. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39373. (const unsigned char**)&pt, derSz));
  39374. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  39375. pt = der;
  39376. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  39377. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39378. gns = NULL;
  39379. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  39380. exts = NULL;
  39381. ASN1_OBJECT_free(upn_oid);
  39382. ASN1_OBJECT_free(sid_oid);
  39383. ASN1_OCTET_STRING_free(sid_data);
  39384. X509_REQ_free(x509);
  39385. x509 = NULL;
  39386. EVP_PKEY_free(priv);
  39387. /* At this point everything used to generate what is in der is cleaned up.
  39388. * We now read back from der to confirm the extensions were inserted
  39389. * correctly. */
  39390. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  39391. ExpectNotNull(bio);
  39392. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  39393. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  39394. ExpectNotNull(x509);
  39395. BIO_free(bio);
  39396. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  39397. x509));
  39398. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  39399. /* Check the SID extension. */
  39400. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  39401. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  39402. ExpectIntEQ(extval->length, sizeof(SidExtension));
  39403. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  39404. /* Check the AltNames extension. */
  39405. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  39406. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  39407. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  39408. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  39409. 0);
  39410. /* Cleanup */
  39411. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39412. NULL));
  39413. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  39414. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  39415. ExpectIntEQ(gn->type, 0);
  39416. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39417. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  39418. X509_REQ_free(x509);
  39419. #endif
  39420. return EXPECT_RESULT();
  39421. }
  39422. static int test_wolfSSL_X509_set_name(void)
  39423. {
  39424. EXPECT_DECLS;
  39425. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39426. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  39427. X509* x509 = NULL;
  39428. X509_NAME* name = NULL;
  39429. ExpectNotNull(name = X509_NAME_new());
  39430. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  39431. (byte*)"wolfssl.com", 11, 0, 1),
  39432. WOLFSSL_SUCCESS);
  39433. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  39434. (byte*)"support@wolfssl.com", 19, -1,
  39435. 1), WOLFSSL_SUCCESS);
  39436. ExpectNotNull(x509 = X509_new());
  39437. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  39438. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  39439. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  39440. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  39441. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  39442. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  39443. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  39444. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  39445. X509_free(x509);
  39446. X509_NAME_free(name);
  39447. #endif /* OPENSSL_ALL && !NO_CERTS */
  39448. return EXPECT_RESULT();
  39449. }
  39450. static int test_wolfSSL_X509_set_notAfter(void)
  39451. {
  39452. EXPECT_DECLS;
  39453. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  39454. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  39455. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  39456. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  39457. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  39458. /* Generalized time will overflow time_t if not long */
  39459. X509* x = NULL;
  39460. BIO* bio = NULL;
  39461. ASN1_TIME *asn_time = NULL;
  39462. ASN1_TIME *time_check = NULL;
  39463. const int year = 365*24*60*60;
  39464. const int day = 24*60*60;
  39465. const int hour = 60*60;
  39466. const int mini = 60;
  39467. int offset_day;
  39468. unsigned char buf[25];
  39469. time_t t;
  39470. /*
  39471. * Setup asn_time. APACHE HTTPD uses time(NULL)
  39472. */
  39473. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  39474. offset_day = 7;
  39475. /*
  39476. * Free these.
  39477. */
  39478. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  39479. ExpectNotNull(asn_time);
  39480. ExpectNotNull(x = X509_new());
  39481. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39482. /*
  39483. * Tests
  39484. */
  39485. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  39486. /* time_check is simply (ANS1_TIME*)x->notAfter */
  39487. ExpectNotNull(time_check = X509_get_notAfter(x));
  39488. /* ANS1_TIME_check validates by checking if argument can be parsed */
  39489. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  39490. /* Convert to human readable format and compare to intended date */
  39491. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  39492. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  39493. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  39494. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  39495. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  39496. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  39497. /*
  39498. * Cleanup
  39499. */
  39500. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39501. X509_free(x);
  39502. BIO_free(bio);
  39503. #endif
  39504. return EXPECT_RESULT();
  39505. }
  39506. static int test_wolfSSL_X509_set_notBefore(void)
  39507. {
  39508. EXPECT_DECLS;
  39509. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  39510. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  39511. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  39512. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  39513. X509* x = NULL;
  39514. BIO* bio = NULL;
  39515. ASN1_TIME *asn_time = NULL;
  39516. ASN1_TIME *time_check = NULL;
  39517. const int year = 365*24*60*60;
  39518. const int day = 24*60*60;
  39519. const int hour = 60*60;
  39520. const int mini = 60;
  39521. int offset_day;
  39522. unsigned char buf[25];
  39523. time_t t;
  39524. /*
  39525. * Setup asn_time. APACHE HTTPD uses time(NULL)
  39526. */
  39527. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  39528. offset_day = 7;
  39529. /*
  39530. * Free these.
  39531. */
  39532. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  39533. ExpectNotNull(asn_time);
  39534. ExpectNotNull(x = X509_new());
  39535. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39536. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  39537. /*
  39538. * Main Tests
  39539. */
  39540. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  39541. /* time_check == (ANS1_TIME*)x->notBefore */
  39542. ExpectNotNull(time_check = X509_get_notBefore(x));
  39543. /* ANS1_TIME_check validates by checking if argument can be parsed */
  39544. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  39545. /* Convert to human readable format and compare to intended date */
  39546. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  39547. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  39548. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  39549. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  39550. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  39551. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  39552. /*
  39553. * Cleanup
  39554. */
  39555. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  39556. X509_free(x);
  39557. BIO_free(bio);
  39558. #endif
  39559. return EXPECT_RESULT();
  39560. }
  39561. static int test_wolfSSL_X509_set_version(void)
  39562. {
  39563. EXPECT_DECLS;
  39564. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  39565. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  39566. X509* x509 = NULL;
  39567. long v = 2L;
  39568. long maxInt = INT_MAX;
  39569. ExpectNotNull(x509 = X509_new());
  39570. /* These should pass. */
  39571. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  39572. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  39573. /* Fail Case: When v(long) is greater than x509->version(int). */
  39574. v = maxInt+1;
  39575. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  39576. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  39577. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  39578. /* Cleanup */
  39579. X509_free(x509);
  39580. #endif
  39581. return EXPECT_RESULT();
  39582. }
  39583. #ifndef NO_BIO
  39584. static int test_wolfSSL_BIO_gets(void)
  39585. {
  39586. EXPECT_DECLS;
  39587. #if defined(OPENSSL_EXTRA)
  39588. BIO* bio = NULL;
  39589. BIO* bio2 = NULL;
  39590. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  39591. char emp[] = "";
  39592. char bio_buffer[20];
  39593. int bufferSz = 20;
  39594. #ifdef OPENSSL_ALL
  39595. BUF_MEM* emp_bm = NULL;
  39596. BUF_MEM* msg_bm = NULL;
  39597. #endif
  39598. /* try with bad args */
  39599. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  39600. #ifdef OPENSSL_ALL
  39601. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  39602. #endif
  39603. /* try with real msg */
  39604. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  39605. XMEMSET(bio_buffer, 0, bufferSz);
  39606. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  39607. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  39608. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  39609. ExpectFalse(bio2 != BIO_next(bio));
  39610. /* make buffer filled with no terminating characters */
  39611. XMEMSET(bio_buffer, 1, bufferSz);
  39612. /* BIO_gets reads a line of data */
  39613. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39614. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39615. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39616. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39617. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39618. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39619. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39620. #ifdef OPENSSL_ALL
  39621. /* test setting the mem_buf manually */
  39622. BIO_free(bio);
  39623. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  39624. ExpectNotNull(emp_bm = BUF_MEM_new());
  39625. ExpectNotNull(msg_bm = BUF_MEM_new());
  39626. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  39627. if (EXPECT_SUCCESS()) {
  39628. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  39629. msg_bm->data = NULL;
  39630. }
  39631. /* emp size is 1 for terminator */
  39632. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  39633. if (EXPECT_SUCCESS()) {
  39634. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  39635. emp_bm->data = emp;
  39636. msg_bm->data = msg;
  39637. }
  39638. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  39639. /* check reading an empty string */
  39640. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  39641. ExpectStrEQ(emp, bio_buffer);
  39642. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39643. /* BIO_gets reads a line of data */
  39644. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  39645. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39646. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39647. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39648. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39649. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39650. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39651. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39652. if (EXPECT_SUCCESS())
  39653. emp_bm->data = NULL;
  39654. BUF_MEM_free(emp_bm);
  39655. if (EXPECT_SUCCESS())
  39656. msg_bm->data = NULL;
  39657. BUF_MEM_free(msg_bm);
  39658. #endif
  39659. /* check not null terminated string */
  39660. BIO_free(bio);
  39661. bio = NULL;
  39662. msg[0] = 0x33;
  39663. msg[1] = 0x33;
  39664. msg[2] = 0x33;
  39665. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  39666. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  39667. ExpectIntEQ(bio_buffer[0], msg[0]);
  39668. ExpectIntEQ(bio_buffer[1], msg[1]);
  39669. ExpectIntNE(bio_buffer[2], msg[2]);
  39670. BIO_free(bio);
  39671. bio = NULL;
  39672. msg[3] = 0x33;
  39673. bio_buffer[3] = 0x33;
  39674. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  39675. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  39676. ExpectIntEQ(bio_buffer[0], msg[0]);
  39677. ExpectIntEQ(bio_buffer[1], msg[1]);
  39678. ExpectIntEQ(bio_buffer[2], msg[2]);
  39679. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  39680. /* check reading an empty string */
  39681. BIO_free(bio);
  39682. bio = NULL;
  39683. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  39684. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  39685. ExpectStrEQ(emp, bio_buffer);
  39686. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39687. /* check error cases */
  39688. BIO_free(bio);
  39689. bio = NULL;
  39690. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  39691. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39692. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  39693. #if !defined(NO_FILESYSTEM)
  39694. {
  39695. BIO* f_bio = NULL;
  39696. XFILE f = XBADFILE;
  39697. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  39698. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  39699. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  39700. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  39701. if (EXPECT_FAIL() && (f != XBADFILE)) {
  39702. XFCLOSE(f);
  39703. }
  39704. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  39705. BIO_free(f_bio);
  39706. f_bio = NULL;
  39707. }
  39708. #endif /* NO_FILESYSTEM */
  39709. BIO_free(bio);
  39710. bio = NULL;
  39711. BIO_free(bio2);
  39712. bio2 = NULL;
  39713. /* try with type BIO */
  39714. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  39715. sizeof(msg));
  39716. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  39717. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  39718. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  39719. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  39720. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  39721. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  39722. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  39723. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  39724. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  39725. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  39726. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  39727. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  39728. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  39729. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  39730. BIO_free(bio);
  39731. bio = NULL;
  39732. BIO_free(bio2);
  39733. bio2 = NULL;
  39734. /* check reading an empty string */
  39735. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  39736. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  39737. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  39738. ExpectStrEQ(emp, bio_buffer);
  39739. BIO_free(bio);
  39740. bio = NULL;
  39741. #endif
  39742. return EXPECT_RESULT();
  39743. }
  39744. static int test_wolfSSL_BIO_puts(void)
  39745. {
  39746. EXPECT_DECLS;
  39747. #if defined(OPENSSL_EXTRA)
  39748. BIO* bio = NULL;
  39749. char input[] = "hello\0world\n.....ok\n\0";
  39750. char output[128];
  39751. XMEMSET(output, 0, sizeof(output));
  39752. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39753. ExpectIntEQ(BIO_puts(bio, input), 5);
  39754. ExpectIntEQ(BIO_pending(bio), 5);
  39755. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  39756. ExpectIntEQ(BIO_pending(bio), 19);
  39757. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  39758. ExpectStrEQ(output, "helloworld\n");
  39759. ExpectIntEQ(BIO_pending(bio), 8);
  39760. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  39761. ExpectStrEQ(output, ".....ok\n");
  39762. ExpectIntEQ(BIO_pending(bio), 0);
  39763. ExpectIntEQ(BIO_puts(bio, ""), -1);
  39764. BIO_free(bio);
  39765. #endif
  39766. return EXPECT_RESULT();
  39767. }
  39768. static int test_wolfSSL_BIO_dump(void)
  39769. {
  39770. EXPECT_DECLS;
  39771. #if defined(OPENSSL_EXTRA)
  39772. BIO* bio;
  39773. static const unsigned char data[] = {
  39774. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  39775. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  39776. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  39777. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  39778. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  39779. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  39780. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  39781. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  39782. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  39783. 0xB4
  39784. };
  39785. /* Generated with OpenSSL. */
  39786. static const char expected[] =
  39787. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  39788. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  39789. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  39790. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  39791. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  39792. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  39793. static const char expectedAll[] =
  39794. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  39795. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  39796. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  39797. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  39798. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  39799. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  39800. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  39801. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  39802. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  39803. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  39804. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  39805. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  39806. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  39807. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  39808. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  39809. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  39810. char output[16 * 80];
  39811. int i;
  39812. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39813. /* Example key dumped. */
  39814. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  39815. sizeof(expected) - 1);
  39816. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  39817. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  39818. /* Try every possible value for a character. */
  39819. for (i = 0; i < 256; i++)
  39820. output[i] = i;
  39821. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  39822. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  39823. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  39824. BIO_free(bio);
  39825. #endif
  39826. return EXPECT_RESULT();
  39827. }
  39828. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39829. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  39830. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  39831. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  39832. {
  39833. (void)ssl;
  39834. (void)buf;
  39835. (void)sz;
  39836. (void)ctx;
  39837. return WOLFSSL_CBIO_ERR_WANT_READ;
  39838. }
  39839. #endif
  39840. static int test_wolfSSL_BIO_should_retry(void)
  39841. {
  39842. EXPECT_DECLS;
  39843. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  39844. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  39845. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  39846. tcp_ready ready;
  39847. func_args server_args;
  39848. THREAD_TYPE serverThread;
  39849. SOCKET_T sockfd = 0;
  39850. WOLFSSL_CTX* ctx = NULL;
  39851. WOLFSSL* ssl = NULL;
  39852. char msg[64] = "hello wolfssl!";
  39853. char reply[1024];
  39854. int msgSz = (int)XSTRLEN(msg);
  39855. int ret;
  39856. BIO* bio = NULL;
  39857. XMEMSET(&server_args, 0, sizeof(func_args));
  39858. #ifdef WOLFSSL_TIRTOS
  39859. fdOpenSession(Task_self());
  39860. #endif
  39861. StartTCP();
  39862. InitTcpReady(&ready);
  39863. #if defined(USE_WINDOWS_API)
  39864. /* use RNG to get random port if using windows */
  39865. ready.port = GetRandomPort();
  39866. #endif
  39867. server_args.signal = &ready;
  39868. start_thread(test_server_nofail, &server_args, &serverThread);
  39869. wait_tcp_ready(&server_args);
  39870. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39871. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  39872. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  39873. #endif
  39874. ExpectIntEQ(WOLFSSL_SUCCESS,
  39875. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39876. ExpectIntEQ(WOLFSSL_SUCCESS,
  39877. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39878. ExpectIntEQ(WOLFSSL_SUCCESS,
  39879. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39880. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  39881. /* force retry */
  39882. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  39883. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  39884. ExpectNotNull(ssl);
  39885. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  39886. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  39887. if (EXPECT_FAIL()) {
  39888. wolfSSL_free(ssl);
  39889. ssl = NULL;
  39890. }
  39891. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  39892. ExpectIntNE(BIO_should_retry(bio), 0);
  39893. ExpectIntEQ(BIO_should_read(bio), 0);
  39894. ExpectIntEQ(BIO_should_write(bio), 0);
  39895. /* now perform successful connection */
  39896. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  39897. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  39898. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  39899. ret = wolfSSL_get_error(ssl, -1);
  39900. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  39901. ExpectIntNE(BIO_should_retry(bio), 0);
  39902. if (ret == WOLFSSL_ERROR_WANT_READ)
  39903. ExpectIntEQ(BIO_should_read(bio), 1);
  39904. else
  39905. ExpectIntEQ(BIO_should_read(bio), 0);
  39906. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  39907. ExpectIntEQ(BIO_should_write(bio), 1);
  39908. else
  39909. ExpectIntEQ(BIO_should_write(bio), 0);
  39910. }
  39911. else {
  39912. ExpectIntEQ(BIO_should_retry(bio), 0);
  39913. ExpectIntEQ(BIO_should_read(bio), 0);
  39914. ExpectIntEQ(BIO_should_write(bio), 0);
  39915. }
  39916. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  39917. XSTRLEN("I hear you fa shizzle!")), 0);
  39918. BIO_free(bio);
  39919. wolfSSL_CTX_free(ctx);
  39920. CloseSocket(sockfd);
  39921. join_thread(serverThread);
  39922. FreeTcpReady(&ready);
  39923. #ifdef WOLFSSL_TIRTOS
  39924. fdOpenSession(Task_self());
  39925. #endif
  39926. #endif
  39927. return EXPECT_RESULT();
  39928. }
  39929. static int test_wolfSSL_BIO_connect(void)
  39930. {
  39931. EXPECT_DECLS;
  39932. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  39933. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  39934. tcp_ready ready;
  39935. func_args server_args;
  39936. THREAD_TYPE serverThread;
  39937. BIO *tcpBio = NULL;
  39938. BIO *sslBio = NULL;
  39939. SSL_CTX* ctx = NULL;
  39940. SSL *ssl = NULL;
  39941. SSL *sslPtr;
  39942. char msg[] = "hello wolfssl!";
  39943. char reply[30];
  39944. char buff[10] = {0};
  39945. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  39946. ExpectIntEQ(WOLFSSL_SUCCESS,
  39947. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  39948. ExpectIntEQ(WOLFSSL_SUCCESS,
  39949. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  39950. ExpectIntEQ(WOLFSSL_SUCCESS,
  39951. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  39952. /* Setup server */
  39953. XMEMSET(&server_args, 0, sizeof(func_args));
  39954. StartTCP();
  39955. InitTcpReady(&ready);
  39956. #if defined(USE_WINDOWS_API)
  39957. /* use RNG to get random port if using windows */
  39958. ready.port = GetRandomPort();
  39959. #endif
  39960. server_args.signal = &ready;
  39961. start_thread(test_server_nofail, &server_args, &serverThread);
  39962. wait_tcp_ready(&server_args);
  39963. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  39964. /* Start the test proper */
  39965. /* Setup the TCP BIO */
  39966. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  39967. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  39968. /* Setup the SSL object */
  39969. ExpectNotNull(ssl = SSL_new(ctx));
  39970. SSL_set_connect_state(ssl);
  39971. /* Setup the SSL BIO */
  39972. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  39973. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  39974. if (EXPECT_FAIL()) {
  39975. wolfSSL_free(ssl);
  39976. }
  39977. /* Verify that BIO_get_ssl works. */
  39978. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  39979. ExpectPtrEq(ssl, sslPtr);
  39980. /* Link BIO's so that sslBio uses tcpBio for IO */
  39981. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  39982. /* Do TCP connect */
  39983. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  39984. /* Do TLS handshake */
  39985. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  39986. /* Test writing */
  39987. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  39988. /* Expect length of default wolfSSL reply */
  39989. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  39990. /* Clean it all up */
  39991. BIO_free_all(sslBio);
  39992. /* Server clean up */
  39993. join_thread(serverThread);
  39994. FreeTcpReady(&ready);
  39995. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  39996. * after. */
  39997. XMEMSET(&server_args, 0, sizeof(func_args));
  39998. StartTCP();
  39999. InitTcpReady(&ready);
  40000. #if defined(USE_WINDOWS_API)
  40001. /* use RNG to get random port if using windows */
  40002. ready.port = GetRandomPort();
  40003. #endif
  40004. server_args.signal = &ready;
  40005. start_thread(test_server_nofail, &server_args, &serverThread);
  40006. wait_tcp_ready(&server_args);
  40007. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40008. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  40009. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  40010. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  40011. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40012. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40013. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40014. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40015. /* Attempt to close the TLS connection gracefully. */
  40016. BIO_ssl_shutdown(sslBio);
  40017. BIO_free_all(sslBio);
  40018. join_thread(serverThread);
  40019. FreeTcpReady(&ready);
  40020. SSL_CTX_free(ctx);
  40021. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40022. wc_ecc_fp_free(); /* free per thread cache */
  40023. #endif
  40024. #endif
  40025. return EXPECT_RESULT();
  40026. }
  40027. static int test_wolfSSL_BIO_tls(void)
  40028. {
  40029. EXPECT_DECLS;
  40030. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  40031. SSL_CTX* ctx = NULL;
  40032. SSL *ssl = NULL;
  40033. BIO *readBio = NULL;
  40034. BIO *writeBio = NULL;
  40035. int ret;
  40036. int err = 0;
  40037. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40038. ExpectNotNull(ssl = SSL_new(ctx));
  40039. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  40040. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  40041. /* Qt reads data from write-bio,
  40042. * then writes the read data into plain packet.
  40043. * Qt reads data from plain packet,
  40044. * then writes the read data into read-bio.
  40045. */
  40046. SSL_set_bio(ssl, readBio, writeBio);
  40047. do {
  40048. #ifdef WOLFSSL_ASYNC_CRYPT
  40049. if (err == WC_PENDING_E) {
  40050. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40051. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40052. }
  40053. #endif
  40054. ret = SSL_connect(ssl);
  40055. err = SSL_get_error(ssl, 0);
  40056. } while (err == WC_PENDING_E);
  40057. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  40058. /* in this use case, should return WANT READ
  40059. * so that Qt will read the data from plain packet for next state.
  40060. */
  40061. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  40062. SSL_free(ssl);
  40063. SSL_CTX_free(ctx);
  40064. #endif
  40065. return EXPECT_RESULT();
  40066. }
  40067. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40068. defined(HAVE_HTTP_CLIENT)
  40069. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  40070. {
  40071. BIO* clientBio;
  40072. SSL* sslClient;
  40073. SSL_CTX* ctx;
  40074. char connectAddr[20]; /* IP + port */;
  40075. (void)args;
  40076. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  40077. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  40078. AssertIntEQ(BIO_do_connect(clientBio), 1);
  40079. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40080. AssertNotNull(sslClient = SSL_new(ctx));
  40081. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  40082. SSL_set_bio(sslClient, clientBio, clientBio);
  40083. AssertIntEQ(SSL_connect(sslClient), 1);
  40084. SSL_free(sslClient);
  40085. SSL_CTX_free(ctx);
  40086. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40087. wc_ecc_fp_free(); /* free per thread cache */
  40088. #endif
  40089. WOLFSSL_RETURN_FROM_THREAD(0);
  40090. }
  40091. #endif
  40092. static int test_wolfSSL_BIO_accept(void)
  40093. {
  40094. EXPECT_DECLS;
  40095. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40096. defined(HAVE_HTTP_CLIENT)
  40097. BIO* serverBindBio = NULL;
  40098. BIO* serverAcceptBio = NULL;
  40099. SSL* sslServer = NULL;
  40100. SSL_CTX* ctx = NULL;
  40101. func_args args;
  40102. THREAD_TYPE thread;
  40103. char port[10]; /* 10 bytes should be enough to store the string
  40104. * representation of the port */
  40105. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  40106. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  40107. /* First BIO_do_accept binds the port */
  40108. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40109. XMEMSET(&args, 0, sizeof(func_args));
  40110. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  40111. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40112. /* Let's plug it into SSL to test */
  40113. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40114. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  40115. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40116. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  40117. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40118. ExpectNotNull(sslServer = SSL_new(ctx));
  40119. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  40120. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  40121. ExpectIntEQ(SSL_accept(sslServer), 1);
  40122. join_thread(thread);
  40123. BIO_free(serverBindBio);
  40124. SSL_free(sslServer);
  40125. SSL_CTX_free(ctx);
  40126. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40127. wc_ecc_fp_free(); /* free per thread cache */
  40128. #endif
  40129. #endif
  40130. return EXPECT_RESULT();
  40131. }
  40132. static int test_wolfSSL_BIO_write(void)
  40133. {
  40134. EXPECT_DECLS;
  40135. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  40136. BIO* bio = NULL;
  40137. BIO* bio64 = NULL;
  40138. BIO* bio_mem = NULL;
  40139. BIO* ptr = NULL;
  40140. int sz;
  40141. char msg[] = "conversion test";
  40142. char out[40];
  40143. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  40144. void* bufPtr = NULL;
  40145. BUF_MEM* buf = NULL;
  40146. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40147. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  40148. if (EXPECT_FAIL()) {
  40149. BIO_free(bio64);
  40150. }
  40151. /* now should convert to base64 then write to memory */
  40152. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40153. BIO_flush(bio);
  40154. /* test BIO chain */
  40155. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  40156. ExpectNotNull(buf);
  40157. ExpectIntEQ(buf->length, 25);
  40158. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  40159. ExpectPtrEq(buf->data, bufPtr);
  40160. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  40161. sz = sizeof(out);
  40162. XMEMSET(out, 0, sz);
  40163. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  40164. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40165. /* write then read should return the same message */
  40166. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40167. sz = sizeof(out);
  40168. XMEMSET(out, 0, sz);
  40169. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  40170. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40171. /* now try encoding with no line ending */
  40172. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  40173. #ifdef HAVE_EX_DATA
  40174. BIO_set_ex_data(bio64, 0, (void*) "data");
  40175. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  40176. #endif
  40177. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40178. BIO_flush(bio);
  40179. sz = sizeof(out);
  40180. XMEMSET(out, 0, sz);
  40181. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  40182. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40183. BIO_free_all(bio); /* frees bio64 also */
  40184. bio = NULL;
  40185. /* test with more than one bio64 in list */
  40186. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40187. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  40188. if (EXPECT_FAIL()) {
  40189. BIO_free(bio64);
  40190. bio64 = NULL;
  40191. }
  40192. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  40193. ExpectNotNull(BIO_push(bio64, bio_mem));
  40194. if (EXPECT_FAIL()) {
  40195. BIO_free(bio_mem);
  40196. }
  40197. /* now should convert to base64 when stored and then decode with read */
  40198. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  40199. BIO_flush(bio);
  40200. sz = sizeof(out);
  40201. XMEMSET(out, 0, sz);
  40202. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  40203. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  40204. BIO_clear_flags(bio64, ~0);
  40205. BIO_set_retry_read(bio);
  40206. BIO_free_all(bio); /* frees bio64s also */
  40207. bio = NULL;
  40208. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  40209. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40210. BIO_free(bio);
  40211. #endif
  40212. return EXPECT_RESULT();
  40213. }
  40214. static int test_wolfSSL_BIO_printf(void)
  40215. {
  40216. EXPECT_DECLS;
  40217. #if defined(OPENSSL_ALL)
  40218. BIO* bio = NULL;
  40219. int sz = 7;
  40220. char msg[] = "TLS 1.3 for the world";
  40221. char out[60];
  40222. char expected[] = "TLS 1.3 for the world : sz = 7";
  40223. XMEMSET(out, 0, sizeof(out));
  40224. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40225. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  40226. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  40227. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  40228. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  40229. BIO_free(bio);
  40230. #endif
  40231. return EXPECT_RESULT();
  40232. }
  40233. static int test_wolfSSL_BIO_f_md(void)
  40234. {
  40235. EXPECT_DECLS;
  40236. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  40237. BIO* bio = NULL;
  40238. BIO* mem = NULL;
  40239. char msg[] = "message to hash";
  40240. char out[60];
  40241. EVP_MD_CTX* ctx = NULL;
  40242. const unsigned char testKey[] =
  40243. {
  40244. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40245. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40246. 0x0b, 0x0b, 0x0b, 0x0b
  40247. };
  40248. const char testData[] = "Hi There";
  40249. const unsigned char testResult[] =
  40250. {
  40251. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  40252. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  40253. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  40254. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  40255. };
  40256. const unsigned char expectedHash[] =
  40257. {
  40258. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  40259. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  40260. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  40261. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  40262. };
  40263. const unsigned char emptyHash[] =
  40264. {
  40265. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  40266. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  40267. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  40268. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  40269. };
  40270. unsigned char check[sizeof(testResult) + 1];
  40271. size_t checkSz = -1;
  40272. EVP_PKEY* key = NULL;
  40273. XMEMSET(out, 0, sizeof(out));
  40274. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40275. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40276. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  40277. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  40278. /* should not be able to write/read yet since just digest wrapper and no
  40279. * data is passing through the bio */
  40280. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  40281. ExpectIntEQ(BIO_pending(bio), 0);
  40282. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  40283. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  40284. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40285. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  40286. BIO_reset(bio);
  40287. /* append BIO mem to bio in order to read/write */
  40288. ExpectNotNull(bio = BIO_push(bio, mem));
  40289. XMEMSET(out, 0, sizeof(out));
  40290. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  40291. ExpectIntEQ(BIO_pending(bio), 16);
  40292. /* this just reads the message and does not hash it (gets calls final) */
  40293. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  40294. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40295. /* create a message digest using BIO */
  40296. XMEMSET(out, 0, sizeof(out));
  40297. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  40298. ExpectIntEQ(BIO_pending(mem), 16);
  40299. ExpectIntEQ(BIO_pending(bio), 16);
  40300. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40301. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  40302. BIO_free(bio);
  40303. bio = NULL;
  40304. BIO_free(mem);
  40305. mem = NULL;
  40306. /* test with HMAC */
  40307. XMEMSET(out, 0, sizeof(out));
  40308. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40309. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40310. BIO_get_md_ctx(bio, &ctx);
  40311. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  40312. (int)sizeof(testKey)));
  40313. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  40314. ExpectNotNull(bio = BIO_push(bio, mem));
  40315. BIO_write(bio, testData, (int)strlen(testData));
  40316. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  40317. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  40318. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  40319. EVP_PKEY_free(key);
  40320. BIO_free(bio);
  40321. BIO_free(mem);
  40322. #endif
  40323. return EXPECT_RESULT();
  40324. }
  40325. static int test_wolfSSL_BIO_up_ref(void)
  40326. {
  40327. EXPECT_DECLS;
  40328. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40329. BIO* bio = NULL;
  40330. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40331. ExpectIntEQ(BIO_up_ref(NULL), 0);
  40332. ExpectIntEQ(BIO_up_ref(bio), 1);
  40333. BIO_free(bio);
  40334. ExpectIntEQ(BIO_up_ref(bio), 1);
  40335. BIO_free(bio);
  40336. BIO_free(bio);
  40337. #endif
  40338. return EXPECT_RESULT();
  40339. }
  40340. static int test_wolfSSL_BIO_reset(void)
  40341. {
  40342. EXPECT_DECLS;
  40343. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40344. BIO* bio = NULL;
  40345. byte buf[16];
  40346. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  40347. (word32)XSTRLEN("secure your data")));
  40348. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  40349. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  40350. XMEMSET(buf, 0, 16);
  40351. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  40352. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  40353. /* You cannot write to MEM BIO with read-only mode. */
  40354. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  40355. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  40356. XMEMSET(buf, 0, 16);
  40357. ExpectIntEQ(BIO_reset(bio), 0);
  40358. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  40359. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  40360. BIO_free(bio);
  40361. #endif
  40362. return EXPECT_RESULT();
  40363. }
  40364. #endif /* !NO_BIO */
  40365. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40366. /* test that the callback arg is correct */
  40367. static int certCbArg = 0;
  40368. static int certCb(WOLFSSL* ssl, void* arg)
  40369. {
  40370. if (ssl == NULL || arg != &certCbArg)
  40371. return 0;
  40372. if (wolfSSL_is_server(ssl)) {
  40373. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  40374. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40375. return 0;
  40376. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  40377. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40378. return 0;
  40379. }
  40380. else {
  40381. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  40382. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40383. return 0;
  40384. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  40385. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  40386. return 0;
  40387. }
  40388. return 1;
  40389. }
  40390. static int certSetupCb(WOLFSSL_CTX* ctx)
  40391. {
  40392. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  40393. return TEST_SUCCESS;
  40394. }
  40395. /**
  40396. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  40397. * to stop certificate and key loading
  40398. */
  40399. static int certClearCb(WOLFSSL* ssl)
  40400. {
  40401. /* Clear the loaded certs to force the callbacks to set them up */
  40402. SSL_certs_clear(ssl);
  40403. return TEST_SUCCESS;
  40404. }
  40405. #endif
  40406. static int test_wolfSSL_cert_cb(void)
  40407. {
  40408. EXPECT_DECLS;
  40409. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40410. test_ssl_cbf func_cb_client;
  40411. test_ssl_cbf func_cb_server;
  40412. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  40413. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  40414. func_cb_client.ctx_ready = certSetupCb;
  40415. func_cb_client.ssl_ready = certClearCb;
  40416. func_cb_server.ctx_ready = certSetupCb;
  40417. func_cb_server.ssl_ready = certClearCb;
  40418. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  40419. &func_cb_server, NULL), TEST_SUCCESS);
  40420. #endif
  40421. return EXPECT_RESULT();
  40422. }
  40423. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40424. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  40425. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  40426. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  40427. {
  40428. EXPECT_DECLS;
  40429. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  40430. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  40431. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  40432. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  40433. return EXPECT_RESULT();
  40434. }
  40435. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  40436. {
  40437. const byte* suites = NULL;
  40438. word16 suiteSz = 0;
  40439. const byte* hashSigAlgo = NULL;
  40440. word16 hashSigAlgoSz = 0;
  40441. word16 idx = 0;
  40442. int haveRSA = 0;
  40443. int haveECC = 0;
  40444. (void)arg;
  40445. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  40446. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  40447. return 0;
  40448. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  40449. hashSigAlgoSz == 0)
  40450. return 0;
  40451. for (idx = 0; idx < suiteSz; idx += 2) {
  40452. WOLFSSL_CIPHERSUITE_INFO info =
  40453. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  40454. if (info.rsaAuth)
  40455. haveRSA = 1;
  40456. else if (info.eccAuth)
  40457. haveECC = 1;
  40458. }
  40459. if (hashSigAlgoSz > 0) {
  40460. /* sigalgs extension takes precedence over ciphersuites */
  40461. haveRSA = 0;
  40462. haveECC = 0;
  40463. }
  40464. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  40465. int hashAlgo = 0;
  40466. int sigAlgo = 0;
  40467. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  40468. &hashAlgo, &sigAlgo) != 0)
  40469. return 0;
  40470. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  40471. haveRSA = 1;
  40472. else if (sigAlgo == ECDSAk)
  40473. haveECC = 1;
  40474. }
  40475. if (haveRSA) {
  40476. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  40477. != WOLFSSL_SUCCESS)
  40478. return 0;
  40479. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  40480. != WOLFSSL_SUCCESS)
  40481. return 0;
  40482. }
  40483. else if (haveECC) {
  40484. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  40485. != WOLFSSL_SUCCESS)
  40486. return 0;
  40487. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  40488. != WOLFSSL_SUCCESS)
  40489. return 0;
  40490. }
  40491. return 1;
  40492. }
  40493. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  40494. {
  40495. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  40496. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  40497. return TEST_SUCCESS;
  40498. }
  40499. #endif
  40500. /* Testing dynamic ciphers offered by client */
  40501. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  40502. {
  40503. EXPECT_DECLS;
  40504. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40505. test_ssl_cbf func_cb_client;
  40506. test_ssl_cbf func_cb_server;
  40507. struct {
  40508. method_provider client_meth;
  40509. const char* client_ciphers;
  40510. const char* client_sigalgs;
  40511. const char* client_ca;
  40512. method_provider server_meth;
  40513. } test_params[] = {
  40514. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  40515. #ifdef WOLFSSL_TLS13
  40516. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  40517. {wolfTLSv1_3_client_method,
  40518. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  40519. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  40520. #endif
  40521. #ifdef HAVE_ECC
  40522. {wolfTLSv1_3_client_method,
  40523. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  40524. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  40525. #endif
  40526. #endif
  40527. #ifndef WOLFSSL_NO_TLS12
  40528. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  40529. {wolfTLSv1_2_client_method,
  40530. "DHE-RSA-AES128-GCM-SHA256",
  40531. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  40532. #endif
  40533. #ifdef HAVE_ECC
  40534. {wolfTLSv1_2_client_method,
  40535. "ECDHE-ECDSA-AES128-GCM-SHA256",
  40536. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  40537. #endif
  40538. #endif
  40539. #endif
  40540. };
  40541. size_t i;
  40542. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  40543. if (testCount > 0) {
  40544. for (i = 0; i < testCount; i++) {
  40545. printf("\tTesting %s ciphers with %s sigalgs\n",
  40546. test_params[i].client_ciphers,
  40547. test_params[i].client_sigalgs);
  40548. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  40549. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  40550. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  40551. test_params[i].client_ciphers;
  40552. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  40553. test_params[i].client_sigalgs;
  40554. func_cb_client.method = test_params[i].client_meth;
  40555. func_cb_client.caPemFile = test_params[i].client_ca;
  40556. func_cb_client.ctx_ready =
  40557. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  40558. func_cb_server.ctx_ready =
  40559. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  40560. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  40561. func_cb_server.method = test_params[i].server_meth;
  40562. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  40563. &func_cb_server, NULL), TEST_SUCCESS);
  40564. }
  40565. }
  40566. #endif
  40567. return EXPECT_RESULT();
  40568. }
  40569. static int test_wolfSSL_ciphersuite_auth(void)
  40570. {
  40571. EXPECT_DECLS;
  40572. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  40573. WOLFSSL_CIPHERSUITE_INFO info;
  40574. (void)info;
  40575. #ifndef WOLFSSL_NO_TLS12
  40576. #ifdef HAVE_CHACHA
  40577. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40578. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  40579. ExpectIntEQ(info.rsaAuth, 1);
  40580. ExpectIntEQ(info.eccAuth, 0);
  40581. ExpectIntEQ(info.eccStatic, 0);
  40582. ExpectIntEQ(info.psk, 0);
  40583. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40584. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  40585. ExpectIntEQ(info.rsaAuth, 0);
  40586. ExpectIntEQ(info.eccAuth, 1);
  40587. ExpectIntEQ(info.eccStatic, 0);
  40588. ExpectIntEQ(info.psk, 0);
  40589. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  40590. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  40591. ExpectIntEQ(info.rsaAuth, 0);
  40592. ExpectIntEQ(info.eccAuth, 0);
  40593. ExpectIntEQ(info.eccStatic, 0);
  40594. ExpectIntEQ(info.psk, 1);
  40595. #endif
  40596. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  40597. #ifndef NO_RSA
  40598. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40599. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  40600. ExpectIntEQ(info.rsaAuth, 1);
  40601. ExpectIntEQ(info.eccAuth, 0);
  40602. ExpectIntEQ(info.eccStatic, 0);
  40603. ExpectIntEQ(info.psk, 0);
  40604. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40605. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  40606. ExpectIntEQ(info.rsaAuth, 1);
  40607. ExpectIntEQ(info.eccAuth, 0);
  40608. ExpectIntEQ(info.eccStatic, 1);
  40609. ExpectIntEQ(info.psk, 0);
  40610. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40611. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  40612. ExpectIntEQ(info.rsaAuth, 1);
  40613. ExpectIntEQ(info.eccAuth, 0);
  40614. ExpectIntEQ(info.eccStatic, 1);
  40615. ExpectIntEQ(info.psk, 0);
  40616. #endif
  40617. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40618. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  40619. ExpectIntEQ(info.rsaAuth, 0);
  40620. ExpectIntEQ(info.eccAuth, 1);
  40621. ExpectIntEQ(info.eccStatic, 0);
  40622. ExpectIntEQ(info.psk, 0);
  40623. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  40624. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  40625. ExpectIntEQ(info.rsaAuth, 0);
  40626. ExpectIntEQ(info.eccAuth, 1);
  40627. ExpectIntEQ(info.eccStatic, 1);
  40628. ExpectIntEQ(info.psk, 0);
  40629. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  40630. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  40631. ExpectIntEQ(info.rsaAuth, 0);
  40632. ExpectIntEQ(info.eccAuth, 0);
  40633. ExpectIntEQ(info.eccStatic, 0);
  40634. ExpectIntEQ(info.psk, 1);
  40635. #endif
  40636. #endif
  40637. #ifdef WOLFSSL_TLS13
  40638. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  40639. TLS_AES_128_GCM_SHA256);
  40640. ExpectIntEQ(info.rsaAuth, 0);
  40641. ExpectIntEQ(info.eccAuth, 0);
  40642. ExpectIntEQ(info.eccStatic, 0);
  40643. ExpectIntEQ(info.psk, 0);
  40644. #endif
  40645. #endif
  40646. return EXPECT_RESULT();
  40647. }
  40648. static int test_wolfSSL_sigalg_info(void)
  40649. {
  40650. EXPECT_DECLS;
  40651. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  40652. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  40653. word16 len = 0;
  40654. word16 idx = 0;
  40655. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  40656. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  40657. for (idx = 0; idx < len; idx += 2) {
  40658. int hashAlgo = 0;
  40659. int sigAlgo = 0;
  40660. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  40661. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  40662. ExpectIntNE(hashAlgo, 0);
  40663. ExpectIntNE(sigAlgo, 0);
  40664. }
  40665. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  40666. 0xFFFFFFFF, &len);
  40667. for (idx = 0; idx < len; idx += 2) {
  40668. int hashAlgo = 0;
  40669. int sigAlgo = 0;
  40670. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  40671. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  40672. ExpectIntNE(hashAlgo, 0);
  40673. }
  40674. #endif
  40675. return EXPECT_RESULT();
  40676. }
  40677. static int test_wolfSSL_SESSION(void)
  40678. {
  40679. EXPECT_DECLS;
  40680. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40681. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40682. !defined(NO_SESSION_CACHE)
  40683. WOLFSSL* ssl = NULL;
  40684. WOLFSSL_CTX* ctx = NULL;
  40685. WOLFSSL_SESSION* sess = NULL;
  40686. WOLFSSL_SESSION* sess_copy = NULL;
  40687. #ifdef OPENSSL_EXTRA
  40688. #ifdef HAVE_EXT_CACHE
  40689. unsigned char* sessDer = NULL;
  40690. unsigned char* ptr = NULL;
  40691. int sz = 0;
  40692. #endif
  40693. const unsigned char context[] = "user app context";
  40694. unsigned int contextSz = (unsigned int)sizeof(context);
  40695. #endif
  40696. int ret = 0, err = 0;
  40697. SOCKET_T sockfd;
  40698. tcp_ready ready;
  40699. func_args server_args;
  40700. THREAD_TYPE serverThread;
  40701. char msg[80];
  40702. const char* sendGET = "GET";
  40703. /* TLS v1.3 requires session tickets */
  40704. /* CHACHA and POLY1305 required for myTicketEncCb */
  40705. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  40706. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  40707. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  40708. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  40709. #else
  40710. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40711. #endif
  40712. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  40713. WOLFSSL_FILETYPE_PEM));
  40714. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  40715. WOLFSSL_FILETYPE_PEM));
  40716. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  40717. WOLFSSL_SUCCESS);
  40718. #ifdef WOLFSSL_ENCRYPTED_KEYS
  40719. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  40720. #endif
  40721. #ifdef HAVE_SESSION_TICKET
  40722. /* Use session tickets, for ticket tests below */
  40723. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  40724. #endif
  40725. XMEMSET(&server_args, 0, sizeof(func_args));
  40726. #ifdef WOLFSSL_TIRTOS
  40727. fdOpenSession(Task_self());
  40728. #endif
  40729. StartTCP();
  40730. InitTcpReady(&ready);
  40731. #if defined(USE_WINDOWS_API)
  40732. /* use RNG to get random port if using windows */
  40733. ready.port = GetRandomPort();
  40734. #endif
  40735. server_args.signal = &ready;
  40736. start_thread(test_server_nofail, &server_args, &serverThread);
  40737. wait_tcp_ready(&server_args);
  40738. /* client connection */
  40739. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40740. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  40741. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40742. #ifdef WOLFSSL_ASYNC_CRYPT
  40743. err = 0; /* Reset error */
  40744. #endif
  40745. do {
  40746. #ifdef WOLFSSL_ASYNC_CRYPT
  40747. if (err == WC_PENDING_E) {
  40748. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40749. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40750. }
  40751. #endif
  40752. ret = wolfSSL_connect(ssl);
  40753. err = wolfSSL_get_error(ssl, 0);
  40754. } while (err == WC_PENDING_E);
  40755. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  40756. #ifdef WOLFSSL_ASYNC_CRYPT
  40757. err = 0; /* Reset error */
  40758. #endif
  40759. do {
  40760. #ifdef WOLFSSL_ASYNC_CRYPT
  40761. if (err == WC_PENDING_E) {
  40762. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40763. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40764. }
  40765. #endif
  40766. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  40767. err = wolfSSL_get_error(ssl, 0);
  40768. } while (err == WC_PENDING_E);
  40769. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  40770. #ifdef WOLFSSL_ASYNC_CRYPT
  40771. err = 0; /* Reset error */
  40772. #endif
  40773. do {
  40774. #ifdef WOLFSSL_ASYNC_CRYPT
  40775. if (err == WC_PENDING_E) {
  40776. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40777. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40778. }
  40779. #endif
  40780. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  40781. err = wolfSSL_get_error(ssl, 0);
  40782. } while (err == WC_PENDING_E);
  40783. ExpectIntEQ(ret, 23);
  40784. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  40785. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  40786. #ifdef HAVE_EXT_CACHE
  40787. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  40788. * HAVE_EXT_CACHE we get new objects each time */
  40789. #endif
  40790. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  40791. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  40792. sess = wolfSSL_get_session(ssl);
  40793. #ifdef OPENSSL_EXTRA
  40794. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  40795. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  40796. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  40797. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  40798. #ifdef HAVE_SESSION_TICKET
  40799. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  40800. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  40801. SESSION_TICKET_HINT_DEFAULT);
  40802. #else
  40803. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  40804. #endif
  40805. #else
  40806. (void)sess;
  40807. #endif /* OPENSSL_EXTRA */
  40808. /* Retain copy of the session for later testing */
  40809. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  40810. wolfSSL_shutdown(ssl);
  40811. wolfSSL_free(ssl); ssl = NULL;
  40812. CloseSocket(sockfd);
  40813. join_thread(serverThread);
  40814. FreeTcpReady(&ready);
  40815. #ifdef WOLFSSL_TIRTOS
  40816. fdOpenSession(Task_self());
  40817. #endif
  40818. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  40819. {
  40820. X509 *x509 = NULL;
  40821. char buf[30];
  40822. int bufSz = 0;
  40823. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  40824. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  40825. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  40826. sizeof(buf))), 0);
  40827. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  40828. if (bufSz == 7) {
  40829. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  40830. }
  40831. if (bufSz == 16) {
  40832. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  40833. }
  40834. }
  40835. #endif
  40836. #ifdef HAVE_EXT_CACHE
  40837. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  40838. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  40839. sess_copy = NULL;
  40840. #endif
  40841. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  40842. /* get session from DER and update the timeout */
  40843. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  40844. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  40845. wolfSSL_SESSION_free(sess); sess = NULL;
  40846. sess = NULL;
  40847. ptr = sessDer;
  40848. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  40849. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  40850. (const unsigned char**)&ptr, sz));
  40851. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  40852. sessDer = NULL;
  40853. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  40854. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  40855. #endif
  40856. /* successful set session test */
  40857. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40858. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  40859. #ifdef HAVE_SESSION_TICKET
  40860. /* Test set/get session ticket */
  40861. {
  40862. const char* ticket = "This is a session ticket";
  40863. char buf[64] = {0};
  40864. word32 bufSz = (word32)sizeof(buf);
  40865. ExpectIntEQ(SSL_SUCCESS,
  40866. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  40867. (word32)XSTRLEN(ticket)));
  40868. ExpectIntEQ(SSL_SUCCESS,
  40869. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  40870. ExpectStrEQ(ticket, buf);
  40871. }
  40872. #endif
  40873. #ifdef OPENSSL_EXTRA
  40874. /* session timeout case */
  40875. /* make the session to be expired */
  40876. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  40877. XSLEEP_MS(1200);
  40878. /* SSL_set_session should reject specified session but return success
  40879. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  40880. */
  40881. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40882. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  40883. #else
  40884. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  40885. #endif
  40886. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  40887. #ifdef WOLFSSL_SESSION_ID_CTX
  40888. /* fail case with miss match session context IDs (use compatibility API) */
  40889. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  40890. SSL_SUCCESS);
  40891. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  40892. wolfSSL_free(ssl); ssl = NULL;
  40893. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  40894. SSL_FAILURE);
  40895. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  40896. SSL_SUCCESS);
  40897. ExpectNotNull(ssl = wolfSSL_new(ctx));
  40898. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  40899. #endif
  40900. #endif /* OPENSSL_EXTRA */
  40901. wolfSSL_free(ssl);
  40902. wolfSSL_SESSION_free(sess);
  40903. wolfSSL_CTX_free(ctx);
  40904. #endif
  40905. return EXPECT_RESULT();
  40906. }
  40907. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40908. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40909. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  40910. !defined(WOLFSSL_NO_TLS12)
  40911. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  40912. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  40913. {
  40914. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  40915. /* returns previous timeout value */
  40916. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  40917. #else
  40918. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  40919. #endif
  40920. }
  40921. /* set the session to timeout in a second */
  40922. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  40923. {
  40924. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  40925. }
  40926. /* store the client side session from the first successful connection */
  40927. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  40928. {
  40929. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  40930. NULL); /* ref count 1 */
  40931. }
  40932. /* wait till session is expired then set it in the WOLFSSL struct for use */
  40933. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  40934. {
  40935. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  40936. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40937. WOLFSSL_SUCCESS);
  40938. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  40939. }
  40940. /* set expired session in the WOLFSSL struct for use */
  40941. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  40942. {
  40943. XSLEEP_MS(1200); /* wait a second for session to expire */
  40944. /* set the expired session, call to set session fails but continuing on
  40945. after failure should be handled here */
  40946. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  40947. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40948. WOLFSSL_SUCCESS);
  40949. #else
  40950. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  40951. WOLFSSL_SUCCESS);
  40952. #endif
  40953. }
  40954. /* check that the expired session was not reused */
  40955. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  40956. {
  40957. /* since the session has expired it should not have been reused */
  40958. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  40959. }
  40960. #endif
  40961. static int test_wolfSSL_SESSION_expire_downgrade(void)
  40962. {
  40963. EXPECT_DECLS;
  40964. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40965. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40966. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  40967. !defined(WOLFSSL_NO_TLS12)
  40968. WOLFSSL_CTX* ctx = NULL;
  40969. callback_functions server_cbf, client_cbf;
  40970. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  40971. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  40972. /* force server side to use TLS 1.2 */
  40973. server_cbf.ctx = ctx;
  40974. server_cbf.method = wolfTLSv1_2_server_method;
  40975. client_cbf.method = wolfSSLv23_client_method;
  40976. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40977. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  40978. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  40979. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40980. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40981. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40982. /* set the previously created session and wait till expired */
  40983. server_cbf.ctx = ctx;
  40984. client_cbf.method = wolfSSLv23_client_method;
  40985. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40986. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  40987. client_cbf.on_result =
  40988. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  40989. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  40990. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  40991. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  40992. /* set the previously created expired session */
  40993. server_cbf.ctx = ctx;
  40994. client_cbf.method = wolfSSLv23_client_method;
  40995. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  40996. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  40997. client_cbf.on_result =
  40998. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  40999. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41000. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41001. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41002. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  41003. wolfSSL_CTX_free(ctx);
  41004. #endif
  41005. return EXPECT_RESULT();
  41006. }
  41007. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41008. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41009. static int clientSessRemCountMalloc = 0;
  41010. static int serverSessRemCountMalloc = 0;
  41011. static int clientSessRemCountFree = 0;
  41012. static int serverSessRemCountFree = 0;
  41013. static WOLFSSL_CTX* serverSessCtx = NULL;
  41014. static WOLFSSL_SESSION* serverSess = NULL;
  41015. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41016. !defined(NO_SESSION_CACHE_REF)
  41017. static WOLFSSL_CTX* clientSessCtx = NULL;
  41018. static WOLFSSL_SESSION* clientSess = NULL;
  41019. #endif
  41020. static int serverSessRemIdx = 3;
  41021. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  41022. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  41023. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  41024. {
  41025. int* side;
  41026. (void)ctx;
  41027. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  41028. if (side != NULL) {
  41029. if (*side == WOLFSSL_CLIENT_END)
  41030. clientSessRemCountFree++;
  41031. else
  41032. serverSessRemCountFree++;
  41033. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  41034. }
  41035. }
  41036. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  41037. {
  41038. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  41039. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  41040. !defined(NO_SESSION_CACHE_REF)
  41041. {
  41042. EXPECT_DECLS;
  41043. /* Allow downgrade, set min version, and disable TLS 1.3.
  41044. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  41045. * reference to the session cache. But with WOLFSSL_TLS13 and without
  41046. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  41047. * the session in the cache. In this case we need to downgrade to
  41048. * previous versions to just use the legacy session ID field. */
  41049. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  41050. SSL_SUCCESS);
  41051. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  41052. SSL_SUCCESS);
  41053. return EXPECT_RESULT();
  41054. }
  41055. #else
  41056. return TEST_SUCCESS;
  41057. #endif
  41058. }
  41059. static int SessRemSslSetupCb(WOLFSSL* ssl)
  41060. {
  41061. EXPECT_DECLS;
  41062. int* side;
  41063. if (SSL_is_server(ssl)) {
  41064. side = &sessRemCtx_Server;
  41065. serverSessRemCountMalloc++;
  41066. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  41067. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  41068. SSL_SUCCESS);
  41069. }
  41070. else {
  41071. side = &sessRemCtx_Client;
  41072. clientSessRemCountMalloc++;
  41073. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41074. !defined(NO_SESSION_CACHE_REF)
  41075. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  41076. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  41077. SSL_SUCCESS);
  41078. #endif
  41079. }
  41080. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  41081. serverSessRemIdx, side), SSL_SUCCESS);
  41082. return EXPECT_RESULT();
  41083. }
  41084. #endif
  41085. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  41086. {
  41087. EXPECT_DECLS;
  41088. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41089. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41090. /* Check that the remove callback gets called for external data in a
  41091. * session object */
  41092. test_ssl_cbf func_cb;
  41093. XMEMSET(&func_cb, 0, sizeof(func_cb));
  41094. func_cb.ctx_ready = SessRemCtxSetupCb;
  41095. func_cb.on_result = SessRemSslSetupCb;
  41096. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  41097. NULL), TEST_SUCCESS);
  41098. /* Both should have been allocated */
  41099. ExpectIntEQ(clientSessRemCountMalloc, 1);
  41100. ExpectIntEQ(serverSessRemCountMalloc, 1);
  41101. /* This should not be called yet. Session wasn't evicted from cache yet. */
  41102. ExpectIntEQ(clientSessRemCountFree, 0);
  41103. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41104. !defined(NO_SESSION_CACHE_REF)
  41105. /* Force a cache lookup */
  41106. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41107. /* Force a cache update */
  41108. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  41109. /* This should set the timeout to 0 and call the remove callback from within
  41110. * the session cache. */
  41111. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  41112. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41113. ExpectIntEQ(clientSessRemCountFree, 1);
  41114. #endif
  41115. /* Server session is in the cache so ex_data isn't free'd with the SSL
  41116. * object */
  41117. ExpectIntEQ(serverSessRemCountFree, 0);
  41118. /* Force a cache lookup */
  41119. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41120. /* Force a cache update */
  41121. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  41122. /* This should set the timeout to 0 and call the remove callback from within
  41123. * the session cache. */
  41124. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  41125. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41126. ExpectIntEQ(serverSessRemCountFree, 1);
  41127. /* Need to free the references that we kept */
  41128. SSL_CTX_free(serverSessCtx);
  41129. SSL_SESSION_free(serverSess);
  41130. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41131. !defined(NO_SESSION_CACHE_REF)
  41132. SSL_CTX_free(clientSessCtx);
  41133. SSL_SESSION_free(clientSess);
  41134. #endif
  41135. #endif
  41136. return EXPECT_RESULT();
  41137. }
  41138. static int test_wolfSSL_ticket_keys(void)
  41139. {
  41140. EXPECT_DECLS;
  41141. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  41142. !defined(NO_WOLFSSL_SERVER)
  41143. WOLFSSL_CTX* ctx = NULL;
  41144. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  41145. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  41146. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  41147. WOLFSSL_FAILURE);
  41148. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  41149. WOLFSSL_FAILURE);
  41150. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  41151. WOLFSSL_FAILURE);
  41152. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  41153. WOLFSSL_FAILURE);
  41154. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41155. WOLFSSL_FAILURE);
  41156. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41157. WOLFSSL_FAILURE);
  41158. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41159. WOLFSSL_FAILURE);
  41160. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  41161. WOLFSSL_FAILURE);
  41162. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  41163. WOLFSSL_FAILURE);
  41164. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  41165. WOLFSSL_FAILURE);
  41166. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  41167. WOLFSSL_FAILURE);
  41168. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41169. WOLFSSL_FAILURE);
  41170. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41171. WOLFSSL_FAILURE);
  41172. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41173. WOLFSSL_FAILURE);
  41174. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41175. WOLFSSL_SUCCESS);
  41176. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41177. WOLFSSL_SUCCESS);
  41178. wolfSSL_CTX_free(ctx);
  41179. #endif
  41180. return EXPECT_RESULT();
  41181. }
  41182. #ifndef NO_BIO
  41183. static int test_wolfSSL_d2i_PUBKEY(void)
  41184. {
  41185. EXPECT_DECLS;
  41186. #if defined(OPENSSL_EXTRA)
  41187. BIO* bio = NULL;
  41188. EVP_PKEY* pkey = NULL;
  41189. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41190. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  41191. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  41192. /* RSA PUBKEY test */
  41193. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  41194. sizeof_client_keypub_der_2048), 0);
  41195. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41196. EVP_PKEY_free(pkey);
  41197. pkey = NULL;
  41198. #endif
  41199. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  41200. /* ECC PUBKEY test */
  41201. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  41202. sizeof_ecc_clikeypub_der_256), 0);
  41203. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41204. EVP_PKEY_free(pkey);
  41205. pkey = NULL;
  41206. #endif
  41207. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  41208. /* DSA PUBKEY test */
  41209. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  41210. sizeof_dsa_pub_key_der_2048), 0);
  41211. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41212. EVP_PKEY_free(pkey);
  41213. pkey = NULL;
  41214. #endif
  41215. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  41216. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  41217. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  41218. (HAVE_FIPS_VERSION > 2))
  41219. /* DH PUBKEY test */
  41220. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  41221. sizeof_dh_pub_key_der_2048), 0);
  41222. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41223. EVP_PKEY_free(pkey);
  41224. pkey = NULL;
  41225. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41226. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  41227. BIO_free(bio);
  41228. (void)pkey;
  41229. #endif
  41230. return EXPECT_RESULT();
  41231. }
  41232. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  41233. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  41234. {
  41235. EXPECT_DECLS;
  41236. BIO* bio = NULL;
  41237. EVP_PKEY* pkey = NULL;
  41238. WOLFSSL_CTX* ctx = NULL;
  41239. #if defined(WOLFSSL_KEY_GEN)
  41240. unsigned char buff[4096];
  41241. unsigned char* bufPtr = buff;
  41242. #endif
  41243. /* test creating new EVP_PKEY with bad arg */
  41244. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  41245. /* test loading RSA key using BIO */
  41246. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41247. {
  41248. XFILE file = XBADFILE;
  41249. const char* fname = "./certs/server-key.der";
  41250. size_t sz = 0;
  41251. byte* buf = NULL;
  41252. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41253. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41254. ExpectTrue((sz = XFTELL(file)) != 0);
  41255. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41256. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41257. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41258. if (file != XBADFILE) {
  41259. XFCLOSE(file);
  41260. }
  41261. /* Test using BIO new mem and loading DER private key */
  41262. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41263. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41264. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41265. BIO_free(bio);
  41266. bio = NULL;
  41267. EVP_PKEY_free(pkey);
  41268. pkey = NULL;
  41269. }
  41270. #endif
  41271. /* test loading ECC key using BIO */
  41272. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  41273. {
  41274. XFILE file = XBADFILE;
  41275. const char* fname = "./certs/ecc-key.der";
  41276. size_t sz = 0;
  41277. byte* buf = NULL;
  41278. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41279. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41280. ExpectTrue((sz = XFTELL(file)) != 0);
  41281. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41282. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41283. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41284. if (file != XBADFILE)
  41285. XFCLOSE(file);
  41286. /* Test using BIO new mem and loading DER private key */
  41287. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41288. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41289. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41290. BIO_free(bio);
  41291. bio = NULL;
  41292. EVP_PKEY_free(pkey);
  41293. pkey = NULL;
  41294. }
  41295. #endif
  41296. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41297. #ifndef NO_WOLFSSL_SERVER
  41298. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41299. #else
  41300. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  41301. #endif
  41302. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  41303. {
  41304. RSA* rsa = NULL;
  41305. /* Tests bad parameters */
  41306. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  41307. /* RSA not set yet, expecting to fail*/
  41308. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  41309. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  41310. /* set RSA using bio*/
  41311. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  41312. sizeof_client_key_der_2048), 0);
  41313. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  41314. ExpectNotNull(rsa);
  41315. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  41316. /* i2d RSAprivate key tests */
  41317. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  41318. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  41319. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  41320. sizeof_client_key_der_2048);
  41321. bufPtr -= sizeof_client_key_der_2048;
  41322. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  41323. sizeof_client_key_der_2048), 0);
  41324. bufPtr = NULL;
  41325. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  41326. sizeof_client_key_der_2048);
  41327. ExpectNotNull(bufPtr);
  41328. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  41329. sizeof_client_key_der_2048), 0);
  41330. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  41331. RSA_free(rsa);
  41332. rsa = RSA_new();
  41333. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  41334. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  41335. RSA_free(rsa);
  41336. }
  41337. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  41338. SSL_CTX_free(ctx);
  41339. ctx = NULL;
  41340. BIO_free(bio);
  41341. bio = NULL;
  41342. return EXPECT_RESULT();
  41343. }
  41344. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  41345. #endif /* !NO_BIO */
  41346. static int test_wolfSSL_sk_GENERAL_NAME(void)
  41347. {
  41348. EXPECT_DECLS;
  41349. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41350. !defined(NO_RSA)
  41351. X509* x509 = NULL;
  41352. GENERAL_NAME* gn = NULL;
  41353. unsigned char buf[4096];
  41354. const unsigned char* bufPt = NULL;
  41355. int bytes = 0;
  41356. int i;
  41357. int j;
  41358. XFILE f = XBADFILE;
  41359. STACK_OF(GENERAL_NAME)* sk = NULL;
  41360. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  41361. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41362. if (f != XBADFILE)
  41363. XFCLOSE(f);
  41364. for (j = 0; j < 2; ++j) {
  41365. bufPt = buf;
  41366. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41367. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  41368. NID_subject_alt_name, NULL, NULL));
  41369. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  41370. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  41371. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  41372. if (gn != NULL) {
  41373. switch (gn->type) {
  41374. case GEN_DNS:
  41375. fprintf(stderr, "found type GEN_DNS\n");
  41376. break;
  41377. case GEN_EMAIL:
  41378. fprintf(stderr, "found type GEN_EMAIL\n");
  41379. break;
  41380. case GEN_URI:
  41381. fprintf(stderr, "found type GEN_URI\n");
  41382. break;
  41383. }
  41384. }
  41385. }
  41386. X509_free(x509);
  41387. x509 = NULL;
  41388. if (j == 0) {
  41389. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  41390. }
  41391. else {
  41392. /*
  41393. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  41394. * it was supposed to. This is a regression test for that bug.
  41395. */
  41396. GENERAL_NAMES_free(sk);
  41397. }
  41398. sk = NULL;
  41399. }
  41400. #endif
  41401. return EXPECT_RESULT();
  41402. }
  41403. static int test_wolfSSL_GENERAL_NAME_print(void)
  41404. {
  41405. EXPECT_DECLS;
  41406. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  41407. X509* x509 = NULL;
  41408. GENERAL_NAME* gn = NULL;
  41409. unsigned char buf[4096];
  41410. const unsigned char* bufPt = NULL;
  41411. int bytes;
  41412. XFILE f = XBADFILE;
  41413. STACK_OF(GENERAL_NAME)* sk = NULL;
  41414. BIO* out = NULL;
  41415. unsigned char outbuf[128];
  41416. X509_EXTENSION* ext = NULL;
  41417. AUTHORITY_INFO_ACCESS* aia = NULL;
  41418. ACCESS_DESCRIPTION* ad = NULL;
  41419. ASN1_IA5STRING *dnsname = NULL;
  41420. const unsigned char v4Addr[] = {192,168,53,1};
  41421. const unsigned char v6Addr[] =
  41422. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  41423. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  41424. const unsigned char email[] =
  41425. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  41426. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  41427. const char* dnsStr = "DNS:example.com";
  41428. const char* uriStr = "URI:http://127.0.0.1:22220";
  41429. const char* v4addStr = "IP Address:192.168.53.1";
  41430. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  41431. const char* emailStr = "email:info@wolfssl.com";
  41432. const char* othrStr = "othername:<unsupported>";
  41433. const char* x400Str = "X400Name:<unsupported>";
  41434. const char* ediStr = "EdiPartyName:<unsupported>";
  41435. /* BIO to output */
  41436. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  41437. /* test for NULL param */
  41438. gn = NULL;
  41439. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  41440. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  41441. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  41442. /* test for GEN_DNS */
  41443. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  41444. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41445. if (f != XBADFILE) {
  41446. XFCLOSE(f);
  41447. f = XBADFILE;
  41448. }
  41449. bufPt = buf;
  41450. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41451. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  41452. NID_subject_alt_name, NULL, NULL));
  41453. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  41454. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41455. XMEMSET(outbuf, 0, sizeof(outbuf));
  41456. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41457. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  41458. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  41459. gn = NULL;
  41460. sk = NULL;
  41461. X509_free(x509);
  41462. x509 = NULL;
  41463. /* Lets test for setting as well. */
  41464. ExpectNotNull(gn = GENERAL_NAME_new());
  41465. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  41466. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  41467. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  41468. dnsname = NULL;
  41469. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41470. XMEMSET(outbuf, 0, sizeof(outbuf));
  41471. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41472. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  41473. GENERAL_NAME_free(gn);
  41474. /* test for GEN_URI */
  41475. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  41476. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  41477. if (f != XBADFILE) {
  41478. XFCLOSE(f);
  41479. f = XBADFILE;
  41480. }
  41481. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  41482. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  41483. ext));
  41484. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  41485. if (ad != NULL) {
  41486. gn = ad->location;
  41487. }
  41488. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41489. gn = NULL;
  41490. XMEMSET(outbuf,0,sizeof(outbuf));
  41491. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41492. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  41493. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  41494. aia = NULL;
  41495. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  41496. ExpectNotNull(aia);
  41497. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  41498. aia = NULL;
  41499. X509_free(x509);
  41500. x509 = NULL;
  41501. /* test for GEN_IPADD */
  41502. /* ip v4 address */
  41503. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41504. if (gn != NULL) {
  41505. gn->type = GEN_IPADD;
  41506. if (gn->d.iPAddress != NULL) {
  41507. gn->d.iPAddress->length = sizeof(v4Addr);
  41508. }
  41509. }
  41510. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  41511. sizeof(v4Addr)), 1);
  41512. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41513. XMEMSET(outbuf,0,sizeof(outbuf));
  41514. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41515. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  41516. GENERAL_NAME_free(gn);
  41517. gn = NULL;
  41518. /* ip v6 address */
  41519. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41520. if (gn != NULL) {
  41521. gn->type = GEN_IPADD;
  41522. if (gn->d.iPAddress != NULL) {
  41523. gn->d.iPAddress->length = sizeof(v6Addr);
  41524. }
  41525. }
  41526. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  41527. sizeof(v6Addr)), 1);
  41528. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41529. XMEMSET(outbuf,0,sizeof(outbuf));
  41530. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41531. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  41532. GENERAL_NAME_free(gn);
  41533. gn = NULL;
  41534. /* test for GEN_EMAIL */
  41535. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41536. if (gn != NULL) {
  41537. gn->type = GEN_EMAIL;
  41538. if (gn->d.rfc822Name != NULL) {
  41539. gn->d.rfc822Name->length = sizeof(email);
  41540. }
  41541. }
  41542. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  41543. 1);
  41544. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41545. XMEMSET(outbuf,0,sizeof(outbuf));
  41546. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41547. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  41548. GENERAL_NAME_free(gn);
  41549. gn = NULL;
  41550. /* test for GEN_OTHERNAME */
  41551. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41552. if (gn != NULL) {
  41553. gn->type = GEN_OTHERNAME;
  41554. }
  41555. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41556. XMEMSET(outbuf,0,sizeof(outbuf));
  41557. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41558. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  41559. GENERAL_NAME_free(gn);
  41560. gn = NULL;
  41561. /* test for GEN_X400 */
  41562. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41563. if (gn != NULL) {
  41564. gn->type = GEN_X400;
  41565. }
  41566. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41567. XMEMSET(outbuf,0,sizeof(outbuf));
  41568. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41569. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  41570. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  41571. if (gn != NULL) {
  41572. gn->type = GEN_IA5;
  41573. }
  41574. GENERAL_NAME_free(gn);
  41575. gn = NULL;
  41576. /* test for GEN_EDIPARTY */
  41577. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  41578. if (gn != NULL) {
  41579. gn->type = GEN_EDIPARTY;
  41580. }
  41581. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  41582. XMEMSET(outbuf,0,sizeof(outbuf));
  41583. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  41584. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  41585. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  41586. if (gn != NULL) {
  41587. gn->type = GEN_IA5;
  41588. }
  41589. GENERAL_NAME_free(gn);
  41590. gn = NULL;
  41591. BIO_free(out);
  41592. #endif /* OPENSSL_ALL */
  41593. return EXPECT_RESULT();
  41594. }
  41595. static int test_wolfSSL_sk_DIST_POINT(void)
  41596. {
  41597. EXPECT_DECLS;
  41598. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41599. !defined(NO_RSA)
  41600. X509* x509 = NULL;
  41601. unsigned char buf[4096];
  41602. const unsigned char* bufPt;
  41603. int bytes = 0;
  41604. int i = 0;
  41605. int j = 0;
  41606. XFILE f = XBADFILE;
  41607. DIST_POINT* dp = NULL;
  41608. DIST_POINT_NAME* dpn = NULL;
  41609. GENERAL_NAME* gn = NULL;
  41610. ASN1_IA5STRING* uri = NULL;
  41611. STACK_OF(DIST_POINT)* dps = NULL;
  41612. STACK_OF(GENERAL_NAME)* gns = NULL;
  41613. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  41614. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  41615. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41616. if (f != XBADFILE)
  41617. XFCLOSE(f);
  41618. bufPt = buf;
  41619. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41620. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  41621. NID_crl_distribution_points, NULL, NULL));
  41622. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  41623. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  41624. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  41625. ExpectNotNull(dpn = dp->distpoint);
  41626. /* this should be type 0, fullname */
  41627. ExpectIntEQ(dpn->type, 0);
  41628. ExpectNotNull(gns = dp->distpoint->name.fullname);
  41629. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  41630. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  41631. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  41632. ExpectIntEQ(gn->type, GEN_URI);
  41633. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  41634. ExpectNotNull(uri->data);
  41635. ExpectIntGT(uri->length, 0);
  41636. }
  41637. }
  41638. X509_free(x509);
  41639. CRL_DIST_POINTS_free(dps);
  41640. #endif
  41641. return EXPECT_RESULT();
  41642. }
  41643. static int test_wolfSSL_verify_mode(void)
  41644. {
  41645. EXPECT_DECLS;
  41646. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  41647. WOLFSSL* ssl = NULL;
  41648. WOLFSSL_CTX* ctx = NULL;
  41649. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41650. ExpectNotNull(ssl = SSL_new(ctx));
  41651. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41652. SSL_free(ssl);
  41653. ssl = NULL;
  41654. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  41655. ExpectNotNull(ssl = SSL_new(ctx));
  41656. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41657. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  41658. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  41659. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  41660. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  41661. SSL_free(ssl);
  41662. ssl = NULL;
  41663. wolfSSL_CTX_set_verify(ctx,
  41664. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  41665. ExpectNotNull(ssl = SSL_new(ctx));
  41666. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  41667. ExpectIntEQ(SSL_get_verify_mode(ssl),
  41668. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41669. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  41670. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  41671. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41672. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  41673. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  41674. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  41675. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  41676. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41677. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  41678. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  41679. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  41680. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  41681. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  41682. #endif
  41683. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  41684. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  41685. SSL_free(ssl);
  41686. SSL_CTX_free(ctx);
  41687. #endif
  41688. return EXPECT_RESULT();
  41689. }
  41690. static int test_wolfSSL_verify_depth(void)
  41691. {
  41692. EXPECT_DECLS;
  41693. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41694. WOLFSSL* ssl = NULL;
  41695. WOLFSSL_CTX* ctx = NULL;
  41696. long depth;
  41697. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41698. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  41699. ExpectNotNull(ssl = SSL_new(ctx));
  41700. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  41701. SSL_free(ssl);
  41702. ssl = NULL;
  41703. SSL_CTX_set_verify_depth(ctx, -1);
  41704. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  41705. SSL_CTX_set_verify_depth(ctx, 2);
  41706. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  41707. ExpectNotNull(ssl = SSL_new(ctx));
  41708. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  41709. SSL_free(ssl);
  41710. SSL_CTX_free(ctx);
  41711. #endif
  41712. return EXPECT_RESULT();
  41713. }
  41714. static int test_wolfSSL_verify_result(void)
  41715. {
  41716. EXPECT_DECLS;
  41717. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  41718. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  41719. WOLFSSL* ssl = NULL;
  41720. WOLFSSL_CTX* ctx = NULL;
  41721. long result = 0xDEADBEEF;
  41722. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  41723. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41724. ExpectNotNull(ssl = SSL_new(ctx));
  41725. wolfSSL_set_verify_result(ssl, result);
  41726. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  41727. SSL_free(ssl);
  41728. SSL_CTX_free(ctx);
  41729. #endif
  41730. return EXPECT_RESULT();
  41731. }
  41732. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41733. static void sslMsgCb(int w, int version, int type, const void* buf,
  41734. size_t sz, SSL* ssl, void* arg)
  41735. {
  41736. int i;
  41737. unsigned char* pt = (unsigned char*)buf;
  41738. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  41739. (w)?"Writing":"Reading", (int)sz, version, type);
  41740. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  41741. fprintf(stderr, "\n");
  41742. (void)ssl;
  41743. (void)arg;
  41744. }
  41745. #endif /* OPENSSL_EXTRA */
  41746. static int test_wolfSSL_msg_callback(void)
  41747. {
  41748. EXPECT_DECLS;
  41749. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  41750. WOLFSSL* ssl = NULL;
  41751. WOLFSSL_CTX* ctx = NULL;
  41752. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41753. ExpectNotNull(ssl = SSL_new(ctx));
  41754. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  41755. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  41756. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  41757. SSL_free(ssl);
  41758. SSL_CTX_free(ctx);
  41759. #endif
  41760. return EXPECT_RESULT();
  41761. }
  41762. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  41763. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  41764. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  41765. static void binary_dump(void *ptr, int size)
  41766. {
  41767. #ifdef WOLFSSL_EVP_PRINT
  41768. int i = 0;
  41769. unsigned char *p = (unsigned char *) ptr;
  41770. fprintf(stderr, "{");
  41771. while ((p != NULL) && (i < size)) {
  41772. if ((i % 8) == 0) {
  41773. fprintf(stderr, "\n");
  41774. fprintf(stderr, " ");
  41775. }
  41776. fprintf(stderr, "0x%02x, ", p[i]);
  41777. i++;
  41778. }
  41779. fprintf(stderr, "\n};\n");
  41780. #else
  41781. (void) ptr;
  41782. (void) size;
  41783. #endif
  41784. }
  41785. static int last_val = 0x0f;
  41786. static int check_result(unsigned char *data, int len)
  41787. {
  41788. int i;
  41789. for ( ; len; ) {
  41790. last_val = (last_val + 1) % 16;
  41791. for (i = 0; i < 16; len--, i++, data++)
  41792. if (*data != last_val) {
  41793. return -1;
  41794. }
  41795. }
  41796. return 0;
  41797. }
  41798. static int r_offset;
  41799. static int w_offset;
  41800. static void init_offset(void)
  41801. {
  41802. r_offset = 0;
  41803. w_offset = 0;
  41804. }
  41805. static void get_record(unsigned char *data, unsigned char *buf, int len)
  41806. {
  41807. XMEMCPY(buf, data+r_offset, len);
  41808. r_offset += len;
  41809. }
  41810. static void set_record(unsigned char *data, unsigned char *buf, int len)
  41811. {
  41812. XMEMCPY(data+w_offset, buf, len);
  41813. w_offset += len;
  41814. }
  41815. static void set_plain(unsigned char *plain, int rec)
  41816. {
  41817. int i, j;
  41818. unsigned char *p = plain;
  41819. #define BLOCKSZ 16
  41820. for (i=0; i<(rec/BLOCKSZ); i++) {
  41821. for (j=0; j<BLOCKSZ; j++)
  41822. *p++ = (i % 16);
  41823. }
  41824. }
  41825. #endif
  41826. static int test_wolfSSL_EVP_Cipher_extra(void)
  41827. {
  41828. EXPECT_DECLS;
  41829. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  41830. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  41831. /* aes128-cbc, keylen=16, ivlen=16 */
  41832. byte aes128_cbc_key[] = {
  41833. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  41834. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  41835. };
  41836. byte aes128_cbc_iv[] = {
  41837. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  41838. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  41839. };
  41840. /* teset data size table */
  41841. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  41842. int test_drive2[] = {8, 3, 8, 512, 0};
  41843. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  41844. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  41845. int test_drive_len[100];
  41846. int ret = 0;
  41847. EVP_CIPHER_CTX *evp = NULL;
  41848. int ilen = 0;
  41849. int klen = 0;
  41850. int i, j;
  41851. const EVP_CIPHER *type;
  41852. byte *iv;
  41853. byte *key;
  41854. int ivlen;
  41855. int keylen;
  41856. #define RECORDS 16
  41857. #define BUFFSZ 512
  41858. byte plain [BUFFSZ * RECORDS];
  41859. byte cipher[BUFFSZ * RECORDS];
  41860. byte inb[BUFFSZ];
  41861. byte outb[BUFFSZ+16];
  41862. int outl = 0;
  41863. int inl;
  41864. iv = aes128_cbc_iv;
  41865. ivlen = sizeof(aes128_cbc_iv);
  41866. key = aes128_cbc_key;
  41867. keylen = sizeof(aes128_cbc_key);
  41868. type = EVP_aes_128_cbc();
  41869. set_plain(plain, BUFFSZ * RECORDS);
  41870. SSL_library_init();
  41871. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  41872. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41873. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41874. klen = EVP_CIPHER_CTX_key_length(evp);
  41875. if (klen > 0 && keylen != klen) {
  41876. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41877. }
  41878. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41879. if (ilen > 0 && ivlen != ilen) {
  41880. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41881. }
  41882. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41883. for (j = 0; j<RECORDS; j++)
  41884. {
  41885. inl = BUFFSZ;
  41886. get_record(plain, inb, inl);
  41887. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  41888. set_record(cipher, outb, outl);
  41889. }
  41890. for (i = 0; test_drive[i]; i++) {
  41891. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41892. init_offset();
  41893. test_drive_len[i] = 0;
  41894. for (j = 0; test_drive[i][j]; j++)
  41895. {
  41896. inl = test_drive[i][j];
  41897. test_drive_len[i] += inl;
  41898. get_record(plain, inb, inl);
  41899. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  41900. 0);
  41901. /* output to cipher buffer, so that following Dec test can detect
  41902. if any error */
  41903. set_record(cipher, outb, outl);
  41904. }
  41905. EVP_CipherFinal(evp, outb, &outl);
  41906. if (outl > 0)
  41907. set_record(cipher, outb, outl);
  41908. }
  41909. for (i = 0; test_drive[i]; i++) {
  41910. last_val = 0x0f;
  41911. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  41912. init_offset();
  41913. for (j = 0; test_drive[i][j]; j++) {
  41914. inl = test_drive[i][j];
  41915. get_record(cipher, inb, inl);
  41916. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  41917. 0);
  41918. binary_dump(outb, outl);
  41919. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  41920. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  41921. }
  41922. ret = EVP_CipherFinal(evp, outb, &outl);
  41923. binary_dump(outb, outl);
  41924. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  41925. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  41926. ExpectTrue(ret);
  41927. }
  41928. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  41929. EVP_CIPHER_CTX_free(evp);
  41930. evp = NULL;
  41931. /* Do an extra test to verify correct behavior with empty input. */
  41932. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  41933. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  41934. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  41935. klen = EVP_CIPHER_CTX_key_length(evp);
  41936. if (klen > 0 && keylen != klen) {
  41937. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  41938. }
  41939. ilen = EVP_CIPHER_CTX_iv_length(evp);
  41940. if (ilen > 0 && ivlen != ilen) {
  41941. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  41942. }
  41943. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  41944. /* outl should be set to 0 after passing NULL, 0 for input args. */
  41945. outl = -1;
  41946. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  41947. ExpectIntEQ(outl, 0);
  41948. EVP_CIPHER_CTX_free(evp);
  41949. #endif /* test_EVP_Cipher */
  41950. return EXPECT_RESULT();
  41951. }
  41952. static int test_wolfSSL_PEM_read_DHparams(void)
  41953. {
  41954. EXPECT_DECLS;
  41955. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  41956. !defined(NO_FILESYSTEM)
  41957. DH* dh = NULL;
  41958. XFILE fp = XBADFILE;
  41959. unsigned char derOut[300];
  41960. unsigned char* derOutBuf = derOut;
  41961. int derOutSz = 0;
  41962. unsigned char derExpected[300];
  41963. int derExpectedSz = 0;
  41964. XMEMSET(derOut, 0, sizeof(derOut));
  41965. XMEMSET(derExpected, 0, sizeof(derExpected));
  41966. /* open DH param file, read into DH struct */
  41967. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  41968. /* bad args */
  41969. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  41970. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  41971. /* good args */
  41972. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41973. if (fp != XBADFILE) {
  41974. XFCLOSE(fp);
  41975. fp = XBADFILE;
  41976. }
  41977. /* read in certs/dh2048.der for comparison against exported params */
  41978. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  41979. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  41980. fp), 0);
  41981. if (fp != XBADFILE) {
  41982. XFCLOSE(fp);
  41983. fp = XBADFILE;
  41984. }
  41985. /* export DH back to DER and compare */
  41986. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  41987. ExpectIntEQ(derOutSz, derExpectedSz);
  41988. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  41989. DH_free(dh);
  41990. dh = NULL;
  41991. /* Test parsing with X9.42 header */
  41992. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  41993. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  41994. if (fp != XBADFILE)
  41995. XFCLOSE(fp);
  41996. DH_free(dh);
  41997. dh = NULL;
  41998. #endif
  41999. return EXPECT_RESULT();
  42000. }
  42001. static int test_wolfSSL_X509_get_serialNumber(void)
  42002. {
  42003. EXPECT_DECLS;
  42004. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  42005. ASN1_INTEGER* a = NULL;
  42006. BIGNUM* bn = NULL;
  42007. X509* x509 = NULL;
  42008. char *serialHex = NULL;
  42009. byte serial[3];
  42010. int serialSz;
  42011. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42012. SSL_FILETYPE_PEM));
  42013. ExpectNotNull(a = X509_get_serialNumber(x509));
  42014. /* check on value of ASN1 Integer */
  42015. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  42016. a = NULL;
  42017. /* test setting serial number and then retrieving it */
  42018. ExpectNotNull(a = ASN1_INTEGER_new());
  42019. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  42020. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42021. serialSz = sizeof(serial);
  42022. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42023. WOLFSSL_SUCCESS);
  42024. ExpectIntEQ(serialSz, 1);
  42025. ExpectIntEQ(serial[0], 3);
  42026. ASN1_INTEGER_free(a);
  42027. a = NULL;
  42028. /* test setting serial number with 0's in it */
  42029. serial[0] = 0x01;
  42030. serial[1] = 0x00;
  42031. serial[2] = 0x02;
  42032. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  42033. if (a != NULL) {
  42034. a->data[0] = ASN_INTEGER;
  42035. a->data[1] = sizeof(serial);
  42036. XMEMCPY(&a->data[2], serial, sizeof(serial));
  42037. a->length = sizeof(serial) + 2;
  42038. }
  42039. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42040. XMEMSET(serial, 0, sizeof(serial));
  42041. serialSz = sizeof(serial);
  42042. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42043. WOLFSSL_SUCCESS);
  42044. ExpectIntEQ(serialSz, 3);
  42045. ExpectIntEQ(serial[0], 0x01);
  42046. ExpectIntEQ(serial[1], 0x00);
  42047. ExpectIntEQ(serial[2], 0x02);
  42048. ASN1_INTEGER_free(a);
  42049. a = NULL;
  42050. X509_free(x509); /* free's a */
  42051. ExpectNotNull(serialHex = BN_bn2hex(bn));
  42052. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42053. ExpectStrEQ(serialHex, "01");
  42054. #else
  42055. ExpectStrEQ(serialHex, "1");
  42056. #endif
  42057. OPENSSL_free(serialHex);
  42058. ExpectIntEQ(BN_get_word(bn), 1);
  42059. BN_free(bn);
  42060. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  42061. ExpectNotNull(a = ASN1_INTEGER_new());
  42062. if (a != NULL) {
  42063. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  42064. DYNAMIC_TYPE_OPENSSL));
  42065. a->isDynamic = 1;
  42066. ASN1_INTEGER_free(a);
  42067. }
  42068. #endif
  42069. return EXPECT_RESULT();
  42070. }
  42071. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  42072. {
  42073. EXPECT_DECLS;
  42074. #if defined(OPENSSL_EXTRA)
  42075. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  42076. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  42077. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  42078. #endif
  42079. return EXPECT_RESULT();
  42080. }
  42081. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  42082. {
  42083. EXPECT_DECLS;
  42084. #if defined(OPENSSL_EXTRA)
  42085. #define MAX_HEXSTR_BUFSZ 9
  42086. #define NUM_CASES 5
  42087. struct Output {
  42088. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  42089. long ret;
  42090. };
  42091. int i;
  42092. int j;
  42093. const char* inputs[NUM_CASES] = {
  42094. "aabcd1357e",
  42095. "01:12:23:34:a5:b6:c7:d8:e9",
  42096. ":01:02",
  42097. "012",
  42098. ":ab:ac:d"
  42099. };
  42100. struct Output expectedOutputs[NUM_CASES] = {
  42101. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  42102. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  42103. {{0x01, 0x02}, 2},
  42104. {{0x00}, 0},
  42105. {{0x00}, 0}
  42106. };
  42107. long len = 0;
  42108. unsigned char* returnedBuf = NULL;
  42109. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  42110. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  42111. if (returnedBuf == NULL) {
  42112. ExpectIntEQ(expectedOutputs[i].ret, 0);
  42113. continue;
  42114. }
  42115. ExpectIntEQ(expectedOutputs[i].ret, len);
  42116. for (j = 0; j < len; ++j) {
  42117. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  42118. }
  42119. OPENSSL_free(returnedBuf);
  42120. returnedBuf = NULL;
  42121. }
  42122. #endif
  42123. return EXPECT_RESULT();
  42124. }
  42125. static int test_wolfSSL_X509_CA_num(void)
  42126. {
  42127. EXPECT_DECLS;
  42128. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  42129. defined(HAVE_ECC) && !defined(NO_RSA)
  42130. WOLFSSL_X509_STORE *store = NULL;
  42131. WOLFSSL_X509 *x509_1 = NULL;
  42132. WOLFSSL_X509 *x509_2 = NULL;
  42133. int ca_num = 0;
  42134. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  42135. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42136. WOLFSSL_FILETYPE_PEM));
  42137. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  42138. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  42139. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  42140. WOLFSSL_FILETYPE_PEM));
  42141. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  42142. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  42143. wolfSSL_X509_free(x509_1);
  42144. wolfSSL_X509_free(x509_2);
  42145. wolfSSL_X509_STORE_free(store);
  42146. #endif
  42147. return EXPECT_RESULT();
  42148. }
  42149. static int test_wolfSSL_X509_check_ca(void)
  42150. {
  42151. EXPECT_DECLS;
  42152. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42153. WOLFSSL_X509 *x509 = NULL;
  42154. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42155. WOLFSSL_FILETYPE_PEM));
  42156. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  42157. wolfSSL_X509_free(x509);
  42158. #endif
  42159. return EXPECT_RESULT();
  42160. }
  42161. static int test_wolfSSL_X509_check_ip_asc(void)
  42162. {
  42163. EXPECT_DECLS;
  42164. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42165. WOLFSSL_X509 *x509 = NULL;
  42166. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  42167. WOLFSSL_FILETYPE_PEM));
  42168. #if 0
  42169. /* TODO: add cert gen for testing positive case */
  42170. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  42171. #endif
  42172. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  42173. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  42174. wolfSSL_X509_free(x509);
  42175. #endif
  42176. return EXPECT_RESULT();
  42177. }
  42178. static int test_wolfSSL_make_cert(void)
  42179. {
  42180. EXPECT_DECLS;
  42181. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  42182. defined(WOLFSSL_CERT_EXT)
  42183. int ret = 0;
  42184. Cert cert;
  42185. CertName name;
  42186. RsaKey key;
  42187. WC_RNG rng;
  42188. byte der[FOURK_BUF];
  42189. word32 idx = 0;
  42190. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  42191. #ifdef OPENSSL_EXTRA
  42192. const unsigned char* pt = NULL;
  42193. int certSz = 0;
  42194. X509* x509 = NULL;
  42195. X509_NAME* x509name = NULL;
  42196. X509_NAME_ENTRY* entry = NULL;
  42197. ASN1_STRING* entryValue = NULL;
  42198. #endif
  42199. XMEMSET(&name, 0, sizeof(CertName));
  42200. /* set up cert name */
  42201. XMEMCPY(name.country, "US", sizeof("US"));
  42202. name.countryEnc = CTC_PRINTABLE;
  42203. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  42204. name.stateEnc = CTC_UTF8;
  42205. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  42206. name.localityEnc = CTC_UTF8;
  42207. XMEMCPY(name.sur, "Test", sizeof("Test"));
  42208. name.surEnc = CTC_UTF8;
  42209. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  42210. name.orgEnc = CTC_UTF8;
  42211. XMEMCPY(name.unit, "Development", sizeof("Development"));
  42212. name.unitEnc = CTC_UTF8;
  42213. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  42214. name.commonNameEnc = CTC_UTF8;
  42215. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  42216. name.serialDevEnc = CTC_PRINTABLE;
  42217. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  42218. name.userIdEnc = CTC_PRINTABLE;
  42219. #ifdef WOLFSSL_MULTI_ATTRIB
  42220. #if CTC_MAX_ATTRIB > 2
  42221. {
  42222. NameAttrib* n;
  42223. n = &name.name[0];
  42224. n->id = ASN_DOMAIN_COMPONENT;
  42225. n->type = CTC_UTF8;
  42226. n->sz = sizeof("com");
  42227. XMEMCPY(n->value, "com", sizeof("com"));
  42228. n = &name.name[1];
  42229. n->id = ASN_DOMAIN_COMPONENT;
  42230. n->type = CTC_UTF8;
  42231. n->sz = sizeof("wolfssl");
  42232. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  42233. }
  42234. #endif
  42235. #endif /* WOLFSSL_MULTI_ATTRIB */
  42236. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  42237. #ifndef HAVE_FIPS
  42238. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  42239. #else
  42240. ExpectIntEQ(wc_InitRng(&rng), 0);
  42241. #endif
  42242. /* load test RSA key */
  42243. idx = 0;
  42244. #if defined(USE_CERT_BUFFERS_1024)
  42245. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  42246. sizeof_server_key_der_1024), 0);
  42247. #elif defined(USE_CERT_BUFFERS_2048)
  42248. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  42249. sizeof_server_key_der_2048), 0);
  42250. #else
  42251. /* error case, no RSA key loaded, happens later */
  42252. (void)idx;
  42253. #endif
  42254. XMEMSET(&cert, 0 , sizeof(Cert));
  42255. ExpectIntEQ(wc_InitCert(&cert), 0);
  42256. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  42257. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  42258. cert.serialSz = (int)sizeof(mySerial);
  42259. cert.isCA = 1;
  42260. #ifndef NO_SHA256
  42261. cert.sigType = CTC_SHA256wRSA;
  42262. #else
  42263. cert.sigType = CTC_SHAwRSA;
  42264. #endif
  42265. /* add SKID from the Public Key */
  42266. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42267. /* add AKID from the Public Key */
  42268. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42269. ret = 0;
  42270. do {
  42271. #if defined(WOLFSSL_ASYNC_CRYPT)
  42272. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  42273. #endif
  42274. if (ret >= 0) {
  42275. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  42276. }
  42277. } while (ret == WC_PENDING_E);
  42278. ExpectIntGT(ret, 0);
  42279. #ifdef OPENSSL_EXTRA
  42280. /* der holds a certificate with DC's now check X509 parsing of it */
  42281. certSz = ret;
  42282. pt = der;
  42283. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  42284. ExpectNotNull(x509name = X509_get_subject_name(x509));
  42285. #ifdef WOLFSSL_MULTI_ATTRIB
  42286. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42287. -1)), 5);
  42288. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42289. idx)), 6);
  42290. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42291. idx)), -1);
  42292. #endif /* WOLFSSL_MULTI_ATTRIB */
  42293. /* compare DN at index 0 */
  42294. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  42295. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42296. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  42297. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  42298. #ifndef WOLFSSL_MULTI_ATTRIB
  42299. /* compare Serial Number */
  42300. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  42301. -1)), 7);
  42302. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42303. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42304. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  42305. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  42306. #endif
  42307. #ifdef WOLFSSL_MULTI_ATTRIB
  42308. /* get first and second DC and compare result */
  42309. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42310. -1)), 5);
  42311. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42312. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42313. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  42314. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42315. idx)), 6);
  42316. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42317. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42318. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  42319. #endif /* WOLFSSL_MULTI_ATTRIB */
  42320. /* try invalid index locations for regression test and sanity check */
  42321. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  42322. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  42323. X509_free(x509);
  42324. #endif /* OPENSSL_EXTRA */
  42325. wc_FreeRsaKey(&key);
  42326. wc_FreeRng(&rng);
  42327. #endif
  42328. return EXPECT_RESULT();
  42329. }
  42330. static int test_wolfSSL_X509_get_version(void)
  42331. {
  42332. EXPECT_DECLS;
  42333. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42334. WOLFSSL_X509 *x509 = NULL;
  42335. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42336. WOLFSSL_FILETYPE_PEM));
  42337. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  42338. wolfSSL_X509_free(x509);
  42339. #endif
  42340. return EXPECT_RESULT();
  42341. }
  42342. #if defined(OPENSSL_ALL)
  42343. static int test_wolfSSL_sk_CIPHER_description(void)
  42344. {
  42345. EXPECT_DECLS;
  42346. #if !defined(NO_RSA)
  42347. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  42348. int i;
  42349. int numCiphers = 0;
  42350. const SSL_METHOD *method = NULL;
  42351. const SSL_CIPHER *cipher = NULL;
  42352. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  42353. SSL_CTX *ctx = NULL;
  42354. SSL *ssl = NULL;
  42355. char buf[256];
  42356. char test_str[9] = "0000000";
  42357. const char badStr[] = "unknown";
  42358. const char certPath[] = "./certs/client-cert.pem";
  42359. XMEMSET(buf, 0, sizeof(buf));
  42360. ExpectNotNull(method = TLSv1_2_client_method());
  42361. ExpectNotNull(ctx = SSL_CTX_new(method));
  42362. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42363. SSL_CTX_set_verify_depth(ctx, 4);
  42364. SSL_CTX_set_options(ctx, flags);
  42365. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  42366. WOLFSSL_SUCCESS);
  42367. ExpectNotNull(ssl = SSL_new(ctx));
  42368. /* SSL_get_ciphers returns a stack of all configured ciphers
  42369. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  42370. */
  42371. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  42372. /* loop through the amount of supportedCiphers */
  42373. numCiphers = sk_num(supportedCiphers);
  42374. for (i = 0; i < numCiphers; ++i) {
  42375. int j;
  42376. /* sk_value increments "sk->data.cipher->cipherOffset".
  42377. * wolfSSL_sk_CIPHER_description sets the description for
  42378. * the cipher based on the provided offset.
  42379. */
  42380. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  42381. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  42382. }
  42383. /* Search cipher description string for "unknown" descriptor */
  42384. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  42385. int k = 0;
  42386. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  42387. test_str[k] = badStr[k];
  42388. j++;
  42389. k++;
  42390. }
  42391. }
  42392. /* Fail if test_str == badStr == "unknown" */
  42393. ExpectStrNE(test_str,badStr);
  42394. }
  42395. SSL_free(ssl);
  42396. SSL_CTX_free(ctx);
  42397. #endif
  42398. return EXPECT_RESULT();
  42399. }
  42400. static int test_wolfSSL_get_ciphers_compat(void)
  42401. {
  42402. EXPECT_DECLS;
  42403. #if !defined(NO_RSA)
  42404. const SSL_METHOD *method = NULL;
  42405. const char certPath[] = "./certs/client-cert.pem";
  42406. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  42407. SSL_CTX *ctx = NULL;
  42408. WOLFSSL *ssl = NULL;
  42409. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  42410. ExpectNotNull(method = SSLv23_client_method());
  42411. ExpectNotNull(ctx = SSL_CTX_new(method));
  42412. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42413. SSL_CTX_set_verify_depth(ctx, 4);
  42414. SSL_CTX_set_options(ctx, flags);
  42415. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  42416. WOLFSSL_SUCCESS);
  42417. ExpectNotNull(ssl = SSL_new(ctx));
  42418. /* Test Bad NULL input */
  42419. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  42420. /* Test for Good input */
  42421. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  42422. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  42423. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  42424. SSL_free(ssl);
  42425. SSL_CTX_free(ctx);
  42426. #endif
  42427. return EXPECT_RESULT();
  42428. }
  42429. static int test_wolfSSL_X509_PUBKEY_get(void)
  42430. {
  42431. EXPECT_DECLS;
  42432. WOLFSSL_X509_PUBKEY pubkey;
  42433. WOLFSSL_X509_PUBKEY* key;
  42434. WOLFSSL_EVP_PKEY evpkey ;
  42435. WOLFSSL_EVP_PKEY* evpPkey;
  42436. WOLFSSL_EVP_PKEY* retEvpPkey;
  42437. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  42438. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  42439. key = &pubkey;
  42440. evpPkey = &evpkey;
  42441. evpPkey->type = WOLFSSL_SUCCESS;
  42442. key->pkey = evpPkey;
  42443. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  42444. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  42445. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  42446. key->pkey = NULL;
  42447. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  42448. return EXPECT_RESULT();
  42449. }
  42450. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  42451. {
  42452. EXPECT_DECLS;
  42453. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42454. DSA *dsa = NULL;
  42455. DSA *setDsa = NULL;
  42456. EVP_PKEY *pkey = NULL;
  42457. EVP_PKEY *set1Pkey = NULL;
  42458. SHA_CTX sha;
  42459. byte signature[DSA_SIG_SIZE];
  42460. byte hash[WC_SHA_DIGEST_SIZE];
  42461. word32 bytes;
  42462. int answer;
  42463. #ifdef USE_CERT_BUFFERS_1024
  42464. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  42465. int dsaKeySz = sizeof_dsa_key_der_1024;
  42466. byte tmp[ONEK_BUF];
  42467. XMEMSET(tmp, 0, sizeof(tmp));
  42468. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  42469. bytes = dsaKeySz;
  42470. #elif defined(USE_CERT_BUFFERS_2048)
  42471. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  42472. int dsaKeySz = sizeof_dsa_key_der_2048;
  42473. byte tmp[TWOK_BUF];
  42474. XMEMSET(tmp, 0, sizeof(tmp));
  42475. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  42476. bytes = dsaKeySz;
  42477. #else
  42478. byte tmp[TWOK_BUF];
  42479. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  42480. int dsaKeySz;
  42481. XFILE fp = XBADFILE;
  42482. XMEMSET(tmp, 0, sizeof(tmp));
  42483. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  42484. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  42485. if (fp != XBADFILE)
  42486. XFCLOSE(fp);
  42487. #endif /* END USE_CERT_BUFFERS_1024 */
  42488. /* Create hash to later Sign and Verify */
  42489. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  42490. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  42491. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  42492. /* Initialize pkey with der format dsa key */
  42493. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  42494. (long)dsaKeySz));
  42495. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  42496. /* Should Fail: NULL argument */
  42497. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  42498. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  42499. /* Should Pass: Initialized pkey argument */
  42500. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  42501. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  42502. #ifdef USE_CERT_BUFFERS_1024
  42503. ExpectIntEQ(DSA_bits(dsa), 1024);
  42504. #else
  42505. ExpectIntEQ(DSA_bits(dsa), 2048);
  42506. #endif
  42507. /* Sign */
  42508. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  42509. /* Verify. */
  42510. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  42511. WOLFSSL_SUCCESS);
  42512. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  42513. /* Should Fail: set1Pkey not initialized */
  42514. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  42515. /* Initialize set1Pkey */
  42516. set1Pkey = EVP_PKEY_new();
  42517. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  42518. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  42519. WOLFSSL_SUCCESS);
  42520. /* Should Pass: set dsa into set1Pkey */
  42521. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  42522. DSA_free(dsa);
  42523. DSA_free(setDsa);
  42524. EVP_PKEY_free(pkey);
  42525. EVP_PKEY_free(set1Pkey);
  42526. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42527. return EXPECT_RESULT();
  42528. } /* END test_EVP_PKEY_set1_get1_DSA */
  42529. static int test_wolfSSL_DSA_generate_parameters(void)
  42530. {
  42531. EXPECT_DECLS;
  42532. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  42533. !defined(HAVE_FIPS)
  42534. DSA *dsa = NULL;
  42535. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  42536. NULL));
  42537. DSA_free(dsa);
  42538. #endif
  42539. return EXPECT_RESULT();
  42540. }
  42541. static int test_wolfSSL_DSA_SIG(void)
  42542. {
  42543. EXPECT_DECLS;
  42544. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  42545. !defined(HAVE_FIPS)
  42546. DSA *dsa = NULL;
  42547. DSA *dsa2 = NULL;
  42548. DSA_SIG *sig = NULL;
  42549. const BIGNUM *p = NULL;
  42550. const BIGNUM *q = NULL;
  42551. const BIGNUM *g = NULL;
  42552. const BIGNUM *pub = NULL;
  42553. const BIGNUM *priv = NULL;
  42554. BIGNUM *dup_p = NULL;
  42555. BIGNUM *dup_q = NULL;
  42556. BIGNUM *dup_g = NULL;
  42557. BIGNUM *dup_pub = NULL;
  42558. BIGNUM *dup_priv = NULL;
  42559. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  42560. ExpectNotNull(dsa = DSA_new());
  42561. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  42562. NULL), 1);
  42563. ExpectIntEQ(DSA_generate_key(dsa), 1);
  42564. DSA_get0_pqg(dsa, &p, &q, &g);
  42565. DSA_get0_key(dsa, &pub, &priv);
  42566. ExpectNotNull(dup_p = BN_dup(p));
  42567. ExpectNotNull(dup_q = BN_dup(q));
  42568. ExpectNotNull(dup_g = BN_dup(g));
  42569. ExpectNotNull(dup_pub = BN_dup(pub));
  42570. ExpectNotNull(dup_priv = BN_dup(priv));
  42571. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  42572. ExpectNotNull(dsa2 = DSA_new());
  42573. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  42574. if (EXPECT_FAIL()) {
  42575. BN_free(dup_p);
  42576. BN_free(dup_q);
  42577. BN_free(dup_g);
  42578. }
  42579. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  42580. if (EXPECT_FAIL()) {
  42581. BN_free(dup_pub);
  42582. BN_free(dup_priv);
  42583. }
  42584. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  42585. DSA_free(dsa);
  42586. DSA_free(dsa2);
  42587. DSA_SIG_free(sig);
  42588. #endif
  42589. return EXPECT_RESULT();
  42590. }
  42591. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  42592. {
  42593. EXPECT_DECLS;
  42594. #ifdef HAVE_ECC
  42595. WOLFSSL_EC_KEY* ecKey = NULL;
  42596. WOLFSSL_EC_KEY* ecGet1 = NULL;
  42597. EVP_PKEY* pkey = NULL;
  42598. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42599. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42600. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  42601. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42602. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42603. /* Should fail since ecKey is empty */
  42604. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42605. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42606. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42607. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  42608. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  42609. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  42610. wolfSSL_EC_KEY_free(ecKey);
  42611. wolfSSL_EC_KEY_free(ecGet1);
  42612. EVP_PKEY_free(pkey);
  42613. #endif /* HAVE_ECC */
  42614. return EXPECT_RESULT();
  42615. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  42616. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  42617. {
  42618. EXPECT_DECLS;
  42619. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  42620. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42621. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  42622. DH *dh = NULL;
  42623. DH *setDh = NULL;
  42624. EVP_PKEY *pkey = NULL;
  42625. XFILE f = XBADFILE;
  42626. unsigned char buf[4096];
  42627. const unsigned char* pt = buf;
  42628. const char* dh2048 = "./certs/dh2048.der";
  42629. long len = 0;
  42630. int code = -1;
  42631. XMEMSET(buf, 0, sizeof(buf));
  42632. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  42633. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42634. if (f != XBADFILE)
  42635. XFCLOSE(f);
  42636. /* Load dh2048.der into DH with internal format */
  42637. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42638. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  42639. ExpectIntEQ(code, 0);
  42640. code = -1;
  42641. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42642. /* Set DH into PKEY */
  42643. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  42644. /* Get DH from PKEY */
  42645. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  42646. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  42647. ExpectIntEQ(code, 0);
  42648. EVP_PKEY_free(pkey);
  42649. DH_free(setDh);
  42650. setDh = NULL;
  42651. DH_free(dh);
  42652. dh = NULL;
  42653. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  42654. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  42655. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  42656. return EXPECT_RESULT();
  42657. } /* END test_EVP_PKEY_set1_get1_DH */
  42658. static int test_wolfSSL_CTX_ctrl(void)
  42659. {
  42660. EXPECT_DECLS;
  42661. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42662. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42663. char caFile[] = "./certs/client-ca.pem";
  42664. char clientFile[] = "./certs/client-cert.pem";
  42665. SSL_CTX* ctx = NULL;
  42666. X509* x509 = NULL;
  42667. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42668. byte buf[6000];
  42669. char file[] = "./certs/dsaparams.pem";
  42670. XFILE f = XBADFILE;
  42671. int bytes = 0;
  42672. BIO* bio = NULL;
  42673. DSA* dsa = NULL;
  42674. DH* dh = NULL;
  42675. #endif
  42676. #ifdef HAVE_ECC
  42677. WOLFSSL_EC_KEY* ecKey = NULL;
  42678. #endif
  42679. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42680. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  42681. WOLFSSL_FILETYPE_PEM));
  42682. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  42683. if (EXPECT_FAIL()) {
  42684. wolfSSL_X509_free(x509);
  42685. }
  42686. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  42687. WOLFSSL_FILETYPE_PEM));
  42688. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42689. /* Initialize DH */
  42690. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  42691. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  42692. if (f != XBADFILE)
  42693. XFCLOSE(f);
  42694. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  42695. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  42696. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  42697. #endif
  42698. #ifdef HAVE_ECC
  42699. /* Initialize WOLFSSL_EC_KEY */
  42700. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42701. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42702. #endif
  42703. /* additional test of getting EVP_PKEY key size from X509
  42704. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  42705. * allowed with user RSA */
  42706. {
  42707. EVP_PKEY* pkey = NULL;
  42708. #if defined(HAVE_ECC)
  42709. X509* ecX509 = NULL;
  42710. #endif /* HAVE_ECC */
  42711. ExpectNotNull(pkey = X509_get_pubkey(x509));
  42712. /* current RSA key is 2048 bit (256 bytes) */
  42713. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  42714. EVP_PKEY_free(pkey);
  42715. pkey = NULL;
  42716. #if defined(HAVE_ECC)
  42717. #if defined(USE_CERT_BUFFERS_256)
  42718. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  42719. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  42720. SSL_FILETYPE_ASN1));
  42721. #else
  42722. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  42723. cliEccCertFile, SSL_FILETYPE_PEM));
  42724. #endif
  42725. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  42726. /* current ECC key is 256 bit (32 bytes) */
  42727. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  42728. X509_free(ecX509);
  42729. EVP_PKEY_free(pkey);
  42730. #endif /* HAVE_ECC */
  42731. }
  42732. /* Tests should fail with passed in NULL pointer */
  42733. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  42734. SSL_FAILURE);
  42735. #if !defined(NO_DH) && !defined(NO_DSA)
  42736. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  42737. SSL_FAILURE);
  42738. #endif
  42739. #ifdef HAVE_ECC
  42740. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  42741. SSL_FAILURE);
  42742. #endif
  42743. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  42744. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  42745. */
  42746. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  42747. SSL_SUCCESS);
  42748. if (EXPECT_FAIL()) {
  42749. wolfSSL_X509_free(x509);
  42750. }
  42751. /* Test with SSL_CTRL_OPTIONS
  42752. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  42753. */
  42754. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  42755. NULL) == SSL_OP_NO_TLSv1);
  42756. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  42757. /* Test with SSL_CTRL_SET_TMP_DH
  42758. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  42759. */
  42760. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  42761. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  42762. SSL_SUCCESS);
  42763. #endif
  42764. /* Test with SSL_CTRL_SET_TMP_ECDH
  42765. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  42766. */
  42767. #ifdef HAVE_ECC
  42768. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  42769. SSL_SUCCESS);
  42770. #endif
  42771. #ifdef WOLFSSL_ENCRYPTED_KEYS
  42772. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  42773. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  42774. #endif
  42775. /* Test for min/max proto */
  42776. #ifndef WOLFSSL_NO_TLS12
  42777. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42778. 0, NULL), SSL_SUCCESS);
  42779. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  42780. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42781. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  42782. #endif
  42783. #ifdef WOLFSSL_TLS13
  42784. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42785. 0, NULL), SSL_SUCCESS);
  42786. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42787. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  42788. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  42789. #ifndef WOLFSSL_NO_TLS12
  42790. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  42791. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  42792. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  42793. #endif
  42794. #endif
  42795. /* Cleanup and Pass */
  42796. #if !defined(NO_DH) && !defined(NO_DSA)
  42797. #ifndef NO_BIO
  42798. BIO_free(bio);
  42799. DSA_free(dsa);
  42800. DH_free(dh);
  42801. dh = NULL;
  42802. #endif
  42803. #endif
  42804. #ifdef HAVE_ECC
  42805. wolfSSL_EC_KEY_free(ecKey);
  42806. #endif
  42807. SSL_CTX_free(ctx);
  42808. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42809. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  42810. return EXPECT_RESULT();
  42811. }
  42812. static int test_wolfSSL_EVP_PKEY_assign(void)
  42813. {
  42814. EXPECT_DECLS;
  42815. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  42816. int type;
  42817. WOLFSSL_EVP_PKEY* pkey = NULL;
  42818. #ifndef NO_RSA
  42819. WOLFSSL_RSA* rsa = NULL;
  42820. #endif
  42821. #ifndef NO_DSA
  42822. WOLFSSL_DSA* dsa = NULL;
  42823. #endif
  42824. #ifdef HAVE_ECC
  42825. WOLFSSL_EC_KEY* ecKey = NULL;
  42826. #endif
  42827. #ifndef NO_RSA
  42828. type = EVP_PKEY_RSA;
  42829. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42830. ExpectNotNull(rsa = wolfSSL_RSA_new());
  42831. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  42832. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42833. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  42834. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  42835. if (EXPECT_FAIL()) {
  42836. wolfSSL_RSA_free(rsa);
  42837. }
  42838. wolfSSL_EVP_PKEY_free(pkey);
  42839. pkey = NULL;
  42840. #endif /* NO_RSA */
  42841. #ifndef NO_DSA
  42842. type = EVP_PKEY_DSA;
  42843. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42844. ExpectNotNull(dsa = wolfSSL_DSA_new());
  42845. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  42846. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42847. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  42848. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  42849. if (EXPECT_FAIL()) {
  42850. wolfSSL_DSA_free(dsa);
  42851. }
  42852. wolfSSL_EVP_PKEY_free(pkey);
  42853. pkey = NULL;
  42854. #endif /* NO_DSA */
  42855. #ifdef HAVE_ECC
  42856. type = EVP_PKEY_EC;
  42857. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42858. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42859. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  42860. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  42861. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  42862. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  42863. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42864. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  42865. if (EXPECT_FAIL()) {
  42866. wolfSSL_EC_KEY_free(ecKey);
  42867. }
  42868. wolfSSL_EVP_PKEY_free(pkey);
  42869. pkey = NULL;
  42870. #endif /* HAVE_ECC */
  42871. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  42872. return EXPECT_RESULT();
  42873. }
  42874. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  42875. {
  42876. EXPECT_DECLS;
  42877. #if !defined(NO_DH) && \
  42878. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  42879. XFILE f = XBADFILE;
  42880. unsigned char buf[4096];
  42881. const unsigned char* pt = buf;
  42882. const char* params1 = "./certs/dh2048.der";
  42883. long len = 0;
  42884. WOLFSSL_DH* dh = NULL;
  42885. WOLFSSL_EVP_PKEY* pkey = NULL;
  42886. XMEMSET(buf, 0, sizeof(buf));
  42887. /* Load DH parameters DER. */
  42888. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  42889. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  42890. if (f != XBADFILE)
  42891. XFCLOSE(f);
  42892. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  42893. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  42894. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42895. /* Bad cases */
  42896. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  42897. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  42898. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  42899. /* Good case */
  42900. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  42901. if (EXPECT_FAIL()) {
  42902. wolfSSL_DH_free(dh);
  42903. }
  42904. EVP_PKEY_free(pkey);
  42905. #endif
  42906. return EXPECT_RESULT();
  42907. }
  42908. static int test_wolfSSL_EVP_PKEY_base_id(void)
  42909. {
  42910. EXPECT_DECLS;
  42911. WOLFSSL_EVP_PKEY* pkey = NULL;
  42912. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42913. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  42914. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  42915. EVP_PKEY_free(pkey);
  42916. return EXPECT_RESULT();
  42917. }
  42918. static int test_wolfSSL_EVP_PKEY_id(void)
  42919. {
  42920. EXPECT_DECLS;
  42921. WOLFSSL_EVP_PKEY* pkey = NULL;
  42922. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42923. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  42924. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  42925. EVP_PKEY_free(pkey);
  42926. return EXPECT_RESULT();
  42927. }
  42928. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  42929. {
  42930. EXPECT_DECLS;
  42931. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  42932. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  42933. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  42934. ECC_MIN_KEY_SZ <= 256)
  42935. EVP_PKEY_CTX* ctx = NULL;
  42936. EVP_PKEY* pkey = NULL;
  42937. /* Test error conditions. */
  42938. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  42939. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42940. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  42941. #ifndef NO_RSA
  42942. EVP_PKEY_CTX_free(ctx);
  42943. /* Parameter generation for RSA not supported yet. */
  42944. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  42945. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  42946. #endif
  42947. #ifdef HAVE_ECC
  42948. EVP_PKEY_CTX_free(ctx);
  42949. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  42950. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  42951. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  42952. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  42953. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  42954. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  42955. WOLFSSL_SUCCESS);
  42956. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42957. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42958. #endif
  42959. EVP_PKEY_CTX_free(ctx);
  42960. EVP_PKEY_free(pkey);
  42961. #endif
  42962. return EXPECT_RESULT();
  42963. }
  42964. static int test_wolfSSL_EVP_PKEY_keygen(void)
  42965. {
  42966. EXPECT_DECLS;
  42967. WOLFSSL_EVP_PKEY* pkey = NULL;
  42968. EVP_PKEY_CTX* ctx = NULL;
  42969. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42970. WOLFSSL_EVP_PKEY* params = NULL;
  42971. DH* dh = NULL;
  42972. const BIGNUM* pubkey = NULL;
  42973. const BIGNUM* privkey = NULL;
  42974. ASN1_INTEGER* asn1int = NULL;
  42975. unsigned int length = 0;
  42976. byte* derBuffer = NULL;
  42977. #endif
  42978. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42979. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42980. /* Bad cases */
  42981. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  42982. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  42983. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  42984. /* Good case */
  42985. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  42986. EVP_PKEY_CTX_free(ctx);
  42987. ctx = NULL;
  42988. EVP_PKEY_free(pkey);
  42989. pkey = NULL;
  42990. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  42991. /* Test DH keygen */
  42992. {
  42993. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  42994. ExpectNotNull(dh = DH_get_2048_256());
  42995. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  42996. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  42997. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  42998. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  42999. DH_free(dh);
  43000. dh = NULL;
  43001. EVP_PKEY_CTX_free(ctx);
  43002. EVP_PKEY_free(params);
  43003. /* try exporting generated key to DER, to verify */
  43004. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  43005. DH_get0_key(dh, &pubkey, &privkey);
  43006. ExpectNotNull(pubkey);
  43007. ExpectNotNull(privkey);
  43008. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  43009. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  43010. ASN1_INTEGER_free(asn1int);
  43011. DH_free(dh);
  43012. dh = NULL;
  43013. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43014. EVP_PKEY_free(pkey);
  43015. }
  43016. #endif
  43017. return EXPECT_RESULT();
  43018. }
  43019. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  43020. {
  43021. EXPECT_DECLS;
  43022. WOLFSSL_EVP_PKEY* pkey = NULL;
  43023. EVP_PKEY_CTX *ctx = NULL;
  43024. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43025. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43026. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43027. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  43028. EVP_PKEY_CTX_free(ctx);
  43029. EVP_PKEY_free(pkey);
  43030. return EXPECT_RESULT();
  43031. }
  43032. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  43033. {
  43034. EXPECT_DECLS;
  43035. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  43036. WOLFSSL_EVP_PKEY* pkey = NULL;
  43037. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43038. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  43039. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  43040. EVP_PKEY_free(pkey);
  43041. #endif
  43042. return EXPECT_RESULT();
  43043. }
  43044. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  43045. {
  43046. EXPECT_DECLS;
  43047. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  43048. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  43049. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  43050. !defined(NO_FILESYSTEM)
  43051. WOLFSSL_EVP_PKEY* params = NULL;
  43052. WOLFSSL_EVP_PKEY* copy = NULL;
  43053. DH* dh = NULL;
  43054. BIGNUM* p1;
  43055. BIGNUM* g1;
  43056. BIGNUM* q1;
  43057. BIGNUM* p2;
  43058. BIGNUM* g2;
  43059. BIGNUM* q2;
  43060. /* create DH with DH_get_2048_256 params */
  43061. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43062. ExpectNotNull(dh = DH_get_2048_256());
  43063. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43064. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  43065. (const BIGNUM**)&q1,
  43066. (const BIGNUM**)&g1);
  43067. DH_free(dh);
  43068. dh = NULL;
  43069. /* create DH with random generated DH params */
  43070. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  43071. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  43072. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  43073. DH_free(dh);
  43074. dh = NULL;
  43075. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  43076. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  43077. ExpectNotNull(dh->p);
  43078. ExpectNotNull(dh->g);
  43079. ExpectNotNull(dh->q);
  43080. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  43081. (const BIGNUM**)&q2,
  43082. (const BIGNUM**)&g2);
  43083. ExpectIntEQ(BN_cmp(p1, p2), 0);
  43084. ExpectIntEQ(BN_cmp(q1, q2), 0);
  43085. ExpectIntEQ(BN_cmp(g1, g2), 0);
  43086. DH_free(dh);
  43087. dh = NULL;
  43088. EVP_PKEY_free(copy);
  43089. EVP_PKEY_free(params);
  43090. #endif
  43091. return EXPECT_RESULT();
  43092. }
  43093. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  43094. {
  43095. EXPECT_DECLS;
  43096. WOLFSSL_EVP_PKEY* pkey = NULL;
  43097. EVP_PKEY_CTX* ctx = NULL;
  43098. int bits = 2048;
  43099. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43100. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43101. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  43102. WOLFSSL_SUCCESS);
  43103. EVP_PKEY_CTX_free(ctx);
  43104. EVP_PKEY_free(pkey);
  43105. return EXPECT_RESULT();
  43106. }
  43107. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  43108. {
  43109. EXPECT_DECLS;
  43110. /* This is large enough to be used for all key sizes */
  43111. byte key[AES_256_KEY_SIZE] = {0};
  43112. byte iv[AES_BLOCK_SIZE] = {0};
  43113. int i;
  43114. int nids[] = {
  43115. #ifdef HAVE_AES_CBC
  43116. NID_aes_128_cbc,
  43117. #endif
  43118. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43119. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43120. #ifdef HAVE_AESGCM
  43121. NID_aes_128_gcm,
  43122. #endif
  43123. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43124. #ifdef WOLFSSL_AES_COUNTER
  43125. NID_aes_128_ctr,
  43126. #endif
  43127. #ifndef NO_DES3
  43128. NID_des_cbc,
  43129. NID_des_ede3_cbc,
  43130. #endif
  43131. };
  43132. int iv_lengths[] = {
  43133. #ifdef HAVE_AES_CBC
  43134. AES_BLOCK_SIZE,
  43135. #endif
  43136. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43137. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43138. #ifdef HAVE_AESGCM
  43139. GCM_NONCE_MID_SZ,
  43140. #endif
  43141. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43142. #ifdef WOLFSSL_AES_COUNTER
  43143. AES_BLOCK_SIZE,
  43144. #endif
  43145. #ifndef NO_DES3
  43146. DES_BLOCK_SIZE,
  43147. DES_BLOCK_SIZE,
  43148. #endif
  43149. };
  43150. int nidsLen = (sizeof(nids)/sizeof(int));
  43151. for (i = 0; i < nidsLen; i++) {
  43152. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43153. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43154. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43155. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43156. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  43157. EVP_CIPHER_CTX_free(ctx);
  43158. }
  43159. return EXPECT_RESULT();
  43160. }
  43161. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  43162. {
  43163. EXPECT_DECLS;
  43164. byte key[AES_256_KEY_SIZE] = {0};
  43165. byte iv[AES_BLOCK_SIZE] = {0};
  43166. int i;
  43167. int nids[] = {
  43168. #ifdef HAVE_AES_CBC
  43169. NID_aes_128_cbc,
  43170. NID_aes_256_cbc,
  43171. #endif
  43172. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43173. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43174. #ifdef HAVE_AESGCM
  43175. NID_aes_128_gcm,
  43176. NID_aes_256_gcm,
  43177. #endif
  43178. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43179. #ifdef WOLFSSL_AES_COUNTER
  43180. NID_aes_128_ctr,
  43181. NID_aes_256_ctr,
  43182. #endif
  43183. #ifndef NO_DES3
  43184. NID_des_cbc,
  43185. NID_des_ede3_cbc,
  43186. #endif
  43187. };
  43188. int key_lengths[] = {
  43189. #ifdef HAVE_AES_CBC
  43190. AES_128_KEY_SIZE,
  43191. AES_256_KEY_SIZE,
  43192. #endif
  43193. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43194. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43195. #ifdef HAVE_AESGCM
  43196. AES_128_KEY_SIZE,
  43197. AES_256_KEY_SIZE,
  43198. #endif
  43199. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43200. #ifdef WOLFSSL_AES_COUNTER
  43201. AES_128_KEY_SIZE,
  43202. AES_256_KEY_SIZE,
  43203. #endif
  43204. #ifndef NO_DES3
  43205. DES_KEY_SIZE,
  43206. DES3_KEY_SIZE,
  43207. #endif
  43208. };
  43209. int nidsLen = (sizeof(nids)/sizeof(int));
  43210. for (i = 0; i < nidsLen; i++) {
  43211. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43212. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43213. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43214. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43215. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  43216. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  43217. WOLFSSL_SUCCESS);
  43218. EVP_CIPHER_CTX_free(ctx);
  43219. }
  43220. return EXPECT_RESULT();
  43221. }
  43222. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  43223. {
  43224. EXPECT_DECLS;
  43225. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  43226. int ivLen, keyLen;
  43227. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43228. #ifdef HAVE_AESGCM
  43229. byte key[AES_128_KEY_SIZE] = {0};
  43230. byte iv[AES_BLOCK_SIZE] = {0};
  43231. const EVP_CIPHER *init = EVP_aes_128_gcm();
  43232. #else
  43233. byte key[DES3_KEY_SIZE] = {0};
  43234. byte iv[DES_BLOCK_SIZE] = {0};
  43235. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43236. #endif
  43237. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43238. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43239. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  43240. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  43241. /* Bad cases */
  43242. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  43243. WOLFSSL_FAILURE);
  43244. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  43245. WOLFSSL_FAILURE);
  43246. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  43247. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  43248. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  43249. WOLFSSL_FAILURE);
  43250. /* Good case */
  43251. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  43252. EVP_CIPHER_CTX_free(ctx);
  43253. #endif
  43254. return EXPECT_RESULT();
  43255. }
  43256. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  43257. {
  43258. EXPECT_DECLS;
  43259. WOLFSSL_ENGINE* e = NULL;
  43260. int id = 0;
  43261. EVP_PKEY_CTX *ctx = NULL;
  43262. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  43263. EVP_PKEY_CTX_free(ctx);
  43264. return EXPECT_RESULT();
  43265. }
  43266. static int test_wolfSSL_EVP_rc4(void)
  43267. {
  43268. EXPECT_DECLS;
  43269. #if !defined(NO_RC4)
  43270. ExpectNotNull(wolfSSL_EVP_rc4());
  43271. #endif
  43272. return EXPECT_RESULT();
  43273. }
  43274. static int test_wolfSSL_EVP_enc_null(void)
  43275. {
  43276. EXPECT_DECLS;
  43277. ExpectNotNull(wolfSSL_EVP_enc_null());
  43278. return EXPECT_RESULT();
  43279. }
  43280. static int test_wolfSSL_EVP_rc2_cbc(void)
  43281. {
  43282. EXPECT_DECLS;
  43283. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  43284. ExpectNull(wolfSSL_EVP_rc2_cbc());
  43285. #endif
  43286. return EXPECT_RESULT();
  43287. }
  43288. static int test_wolfSSL_EVP_mdc2(void)
  43289. {
  43290. EXPECT_DECLS;
  43291. #if !defined(NO_WOLFSSL_STUB)
  43292. ExpectNull(wolfSSL_EVP_mdc2());
  43293. #endif
  43294. return EXPECT_RESULT();
  43295. }
  43296. static int test_wolfSSL_EVP_md4(void)
  43297. {
  43298. EXPECT_DECLS;
  43299. #if !defined(NO_MD4)
  43300. ExpectNotNull(wolfSSL_EVP_md4());
  43301. #endif
  43302. return EXPECT_RESULT();
  43303. }
  43304. static int test_wolfSSL_EVP_aes_256_gcm(void)
  43305. {
  43306. EXPECT_DECLS;
  43307. #ifdef HAVE_AESGCM
  43308. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  43309. #endif
  43310. return EXPECT_RESULT();
  43311. }
  43312. static int test_wolfSSL_EVP_aes_192_gcm(void)
  43313. {
  43314. EXPECT_DECLS;
  43315. #ifdef HAVE_AESGCM
  43316. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  43317. #endif
  43318. return EXPECT_RESULT();
  43319. }
  43320. static int test_wolfSSL_EVP_aes_256_ccm(void)
  43321. {
  43322. EXPECT_DECLS;
  43323. #ifdef HAVE_AESCCM
  43324. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  43325. #endif
  43326. return EXPECT_RESULT();
  43327. }
  43328. static int test_wolfSSL_EVP_aes_192_ccm(void)
  43329. {
  43330. EXPECT_DECLS;
  43331. #ifdef HAVE_AESCCM
  43332. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  43333. #endif
  43334. return EXPECT_RESULT();
  43335. }
  43336. static int test_wolfSSL_EVP_aes_128_ccm(void)
  43337. {
  43338. EXPECT_DECLS;
  43339. #ifdef HAVE_AESCCM
  43340. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  43341. #endif
  43342. return EXPECT_RESULT();
  43343. }
  43344. static int test_wolfSSL_EVP_ripemd160(void)
  43345. {
  43346. EXPECT_DECLS;
  43347. #if !defined(NO_WOLFSSL_STUB)
  43348. ExpectNull(wolfSSL_EVP_ripemd160());
  43349. #endif
  43350. return EXPECT_RESULT();
  43351. }
  43352. static int test_wolfSSL_EVP_get_digestbynid(void)
  43353. {
  43354. EXPECT_DECLS;
  43355. #ifndef NO_MD5
  43356. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  43357. #endif
  43358. #ifndef NO_SHA
  43359. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  43360. #endif
  43361. #ifndef NO_SHA256
  43362. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  43363. #endif
  43364. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  43365. return EXPECT_RESULT();
  43366. }
  43367. static int test_wolfSSL_EVP_MD_nid(void)
  43368. {
  43369. EXPECT_DECLS;
  43370. #ifndef NO_MD5
  43371. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  43372. #endif
  43373. #ifndef NO_SHA
  43374. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  43375. #endif
  43376. #ifndef NO_SHA256
  43377. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  43378. #endif
  43379. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  43380. return EXPECT_RESULT();
  43381. }
  43382. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  43383. {
  43384. EXPECT_DECLS;
  43385. #if defined(HAVE_ECC)
  43386. WOLFSSL_EVP_PKEY* pkey = NULL;
  43387. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  43388. ExpectNotNull(pkey = EVP_PKEY_new());
  43389. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  43390. EVP_PKEY_free(pkey);
  43391. #endif
  43392. return EXPECT_RESULT();
  43393. }
  43394. static int test_wolfSSL_EVP_X_STATE(void)
  43395. {
  43396. EXPECT_DECLS;
  43397. #if !defined(NO_DES3) && !defined(NO_RC4)
  43398. byte key[DES3_KEY_SIZE] = {0};
  43399. byte iv[DES_IV_SIZE] = {0};
  43400. EVP_CIPHER_CTX *ctx = NULL;
  43401. const EVP_CIPHER *init = NULL;
  43402. /* Bad test cases */
  43403. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  43404. ExpectNotNull(init = EVP_des_ede3_cbc());
  43405. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43406. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43407. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  43408. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  43409. EVP_CIPHER_CTX_free(ctx);
  43410. ctx = NULL;
  43411. /* Good test case */
  43412. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  43413. ExpectNotNull(init = wolfSSL_EVP_rc4());
  43414. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43415. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43416. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  43417. EVP_CIPHER_CTX_free(ctx);
  43418. #endif
  43419. return EXPECT_RESULT();
  43420. }
  43421. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  43422. {
  43423. EXPECT_DECLS;
  43424. #if !defined(NO_DES3) && !defined(NO_RC4)
  43425. byte key[DES3_KEY_SIZE] = {0};
  43426. byte iv[DES_IV_SIZE] = {0};
  43427. EVP_CIPHER_CTX *ctx = NULL;
  43428. const EVP_CIPHER *init = NULL;
  43429. /* Bad test cases */
  43430. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  43431. ExpectNotNull(init = EVP_des_ede3_cbc());
  43432. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43433. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43434. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  43435. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  43436. EVP_CIPHER_CTX_free(ctx);
  43437. ctx = NULL;
  43438. /* Good test case */
  43439. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  43440. ExpectNotNull(init = wolfSSL_EVP_rc4());
  43441. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43442. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43443. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  43444. EVP_CIPHER_CTX_free(ctx);
  43445. #endif
  43446. return EXPECT_RESULT();
  43447. }
  43448. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  43449. {
  43450. EXPECT_DECLS;
  43451. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  43452. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  43453. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  43454. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  43455. #ifdef HAVE_AES_CBC
  43456. #ifdef WOLFSSL_AES_128
  43457. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  43458. #endif
  43459. #ifdef WOLFSSL_AES_192
  43460. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  43461. #endif
  43462. #ifdef WOLFSSL_AES_256
  43463. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  43464. #endif
  43465. #endif
  43466. #ifdef HAVE_AESGCM
  43467. #ifdef WOLFSSL_AES_128
  43468. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  43469. #endif
  43470. #ifdef WOLFSSL_AES_192
  43471. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  43472. #endif
  43473. #ifdef WOLFSSL_AES_256
  43474. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  43475. #endif
  43476. #endif
  43477. #ifdef HAVE_AESCCM
  43478. #ifdef WOLFSSL_AES_128
  43479. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  43480. #endif
  43481. #ifdef WOLFSSL_AES_192
  43482. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  43483. #endif
  43484. #ifdef WOLFSSL_AES_256
  43485. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  43486. #endif
  43487. #endif
  43488. #ifdef WOLFSSL_AES_COUNTER
  43489. #ifdef WOLFSSL_AES_128
  43490. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  43491. #endif
  43492. #ifdef WOLFSSL_AES_192
  43493. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  43494. #endif
  43495. #ifdef WOLFSSL_AES_256
  43496. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  43497. #endif
  43498. #endif
  43499. #ifdef HAVE_AES_ECB
  43500. #ifdef WOLFSSL_AES_128
  43501. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  43502. #endif
  43503. #ifdef WOLFSSL_AES_192
  43504. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  43505. #endif
  43506. #ifdef WOLFSSL_AES_256
  43507. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  43508. #endif
  43509. #endif
  43510. #ifdef WOLFSSL_AES_OFB
  43511. #ifdef WOLFSSL_AES_128
  43512. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  43513. #endif
  43514. #ifdef WOLFSSL_AES_192
  43515. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  43516. #endif
  43517. #ifdef WOLFSSL_AES_256
  43518. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  43519. #endif
  43520. #endif
  43521. #ifndef NO_RC4
  43522. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  43523. #endif
  43524. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43525. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  43526. #endif
  43527. #endif
  43528. #ifdef WOLFSSL_SM4_ECB
  43529. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  43530. #endif
  43531. #ifdef WOLFSSL_SM4_CBC
  43532. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  43533. #endif
  43534. #ifdef WOLFSSL_SM4_CTR
  43535. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  43536. #endif
  43537. #ifdef WOLFSSL_SM4_GCM
  43538. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  43539. #endif
  43540. #ifdef WOLFSSL_SM4_CCM
  43541. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  43542. #endif
  43543. return EXPECT_RESULT();
  43544. }
  43545. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  43546. {
  43547. EXPECT_DECLS;
  43548. int nids[] = {
  43549. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43550. #ifdef WOLFSSL_AES_128
  43551. NID_aes_128_cbc,
  43552. #endif
  43553. #ifdef WOLFSSL_AES_192
  43554. NID_aes_192_cbc,
  43555. #endif
  43556. #ifdef WOLFSSL_AES_256
  43557. NID_aes_256_cbc,
  43558. #endif
  43559. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  43560. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43561. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43562. #ifdef HAVE_AESGCM
  43563. #ifdef WOLFSSL_AES_128
  43564. NID_aes_128_gcm,
  43565. #endif
  43566. #ifdef WOLFSSL_AES_192
  43567. NID_aes_192_gcm,
  43568. #endif
  43569. #ifdef WOLFSSL_AES_256
  43570. NID_aes_256_gcm,
  43571. #endif
  43572. #endif /* HAVE_AESGCM */
  43573. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43574. #ifdef WOLFSSL_AES_COUNTER
  43575. #ifdef WOLFSSL_AES_128
  43576. NID_aes_128_ctr,
  43577. #endif
  43578. #ifdef WOLFSSL_AES_192
  43579. NID_aes_192_ctr,
  43580. #endif
  43581. #ifdef WOLFSSL_AES_256
  43582. NID_aes_256_ctr,
  43583. #endif
  43584. #endif
  43585. #ifndef NO_DES3
  43586. NID_des_cbc,
  43587. NID_des_ede3_cbc,
  43588. #endif
  43589. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43590. NID_chacha20_poly1305,
  43591. #endif
  43592. };
  43593. int iv_lengths[] = {
  43594. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43595. #ifdef WOLFSSL_AES_128
  43596. AES_BLOCK_SIZE,
  43597. #endif
  43598. #ifdef WOLFSSL_AES_192
  43599. AES_BLOCK_SIZE,
  43600. #endif
  43601. #ifdef WOLFSSL_AES_256
  43602. AES_BLOCK_SIZE,
  43603. #endif
  43604. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  43605. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43606. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43607. #ifdef HAVE_AESGCM
  43608. #ifdef WOLFSSL_AES_128
  43609. GCM_NONCE_MID_SZ,
  43610. #endif
  43611. #ifdef WOLFSSL_AES_192
  43612. GCM_NONCE_MID_SZ,
  43613. #endif
  43614. #ifdef WOLFSSL_AES_256
  43615. GCM_NONCE_MID_SZ,
  43616. #endif
  43617. #endif /* HAVE_AESGCM */
  43618. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43619. #ifdef WOLFSSL_AES_COUNTER
  43620. #ifdef WOLFSSL_AES_128
  43621. AES_BLOCK_SIZE,
  43622. #endif
  43623. #ifdef WOLFSSL_AES_192
  43624. AES_BLOCK_SIZE,
  43625. #endif
  43626. #ifdef WOLFSSL_AES_256
  43627. AES_BLOCK_SIZE,
  43628. #endif
  43629. #endif
  43630. #ifndef NO_DES3
  43631. DES_BLOCK_SIZE,
  43632. DES_BLOCK_SIZE,
  43633. #endif
  43634. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  43635. CHACHA20_POLY1305_AEAD_IV_SIZE,
  43636. #endif
  43637. };
  43638. int i;
  43639. int nidsLen = (sizeof(nids)/sizeof(int));
  43640. for (i = 0; i < nidsLen; i++) {
  43641. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  43642. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  43643. }
  43644. return EXPECT_RESULT();
  43645. }
  43646. static int test_wolfSSL_EVP_SignInit_ex(void)
  43647. {
  43648. EXPECT_DECLS;
  43649. WOLFSSL_EVP_MD_CTX mdCtx;
  43650. WOLFSSL_ENGINE* e = 0;
  43651. const EVP_MD* md = EVP_sha256();
  43652. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43653. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  43654. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43655. return EXPECT_RESULT();
  43656. }
  43657. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  43658. {
  43659. EXPECT_DECLS;
  43660. #if !defined(NO_SHA256)
  43661. WOLFSSL_EVP_MD_CTX mdCtx;
  43662. unsigned int s = 0;
  43663. unsigned char md[WC_SHA256_DIGEST_SIZE];
  43664. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  43665. /* Bad Case */
  43666. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  43667. (HAVE_FIPS_VERSION > 2))
  43668. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43669. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  43670. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  43671. #else
  43672. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43673. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  43674. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  43675. #endif
  43676. /* Good Case */
  43677. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  43678. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  43679. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  43680. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  43681. #endif
  43682. return EXPECT_RESULT();
  43683. }
  43684. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  43685. {
  43686. EXPECT_DECLS;
  43687. #if defined(OPENSSL_EXTRA)
  43688. EVP_PKEY* pkey = NULL;
  43689. EVP_PKEY_CTX* ctx = NULL;
  43690. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43691. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43692. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  43693. /* void */
  43694. EVP_PKEY_CTX_free(ctx);
  43695. #else
  43696. /* int */
  43697. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  43698. #endif
  43699. EVP_PKEY_free(pkey);
  43700. #endif
  43701. return EXPECT_RESULT();
  43702. }
  43703. static int test_wolfSSL_EVP_PKEY_param_check(void)
  43704. {
  43705. EXPECT_DECLS;
  43706. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  43707. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43708. DH *dh = NULL;
  43709. DH *setDh = NULL;
  43710. EVP_PKEY *pkey = NULL;
  43711. EVP_PKEY_CTX* ctx = NULL;
  43712. FILE* f = NULL;
  43713. unsigned char buf[512];
  43714. const unsigned char* pt = buf;
  43715. const char* dh2048 = "./certs/dh2048.der";
  43716. long len = 0;
  43717. int code = -1;
  43718. XMEMSET(buf, 0, sizeof(buf));
  43719. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  43720. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43721. if (f != XBADFILE)
  43722. XFCLOSE(f);
  43723. /* Load dh2048.der into DH with internal format */
  43724. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  43725. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43726. ExpectIntEQ(code, 0);
  43727. code = -1;
  43728. pkey = wolfSSL_EVP_PKEY_new();
  43729. /* Set DH into PKEY */
  43730. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43731. /* create ctx from pkey */
  43732. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43733. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  43734. /* TODO: more invalid cases */
  43735. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  43736. EVP_PKEY_CTX_free(ctx);
  43737. EVP_PKEY_free(pkey);
  43738. DH_free(setDh);
  43739. setDh = NULL;
  43740. DH_free(dh);
  43741. dh = NULL;
  43742. #endif
  43743. #endif
  43744. return EXPECT_RESULT();
  43745. }
  43746. static int test_wolfSSL_EVP_BytesToKey(void)
  43747. {
  43748. EXPECT_DECLS;
  43749. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  43750. byte key[AES_BLOCK_SIZE] = {0};
  43751. byte iv[AES_BLOCK_SIZE] = {0};
  43752. int count = 0;
  43753. const EVP_MD* md = EVP_sha256();
  43754. const EVP_CIPHER *type;
  43755. const unsigned char *salt = (unsigned char *)"salt1234";
  43756. int sz = 5;
  43757. const byte data[] = {
  43758. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43759. 0x72,0x6c,0x64
  43760. };
  43761. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  43762. /* Bad cases */
  43763. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  43764. 0);
  43765. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  43766. 16);
  43767. md = "2";
  43768. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43769. WOLFSSL_FAILURE);
  43770. /* Good case */
  43771. md = EVP_sha256();
  43772. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  43773. 16);
  43774. #endif
  43775. return EXPECT_RESULT();
  43776. }
  43777. static int test_evp_cipher_aes_gcm(void)
  43778. {
  43779. EXPECT_DECLS;
  43780. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  43781. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  43782. (HAVE_FIPS_VERSION >= 2)))
  43783. /*
  43784. * This test checks data at various points in the encrypt/decrypt process
  43785. * against known values produced using the same test with OpenSSL. This
  43786. * interop testing is critical for verifying the correctness of our
  43787. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  43788. * a flow supported by OpenSSL that uses the control command
  43789. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  43790. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  43791. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  43792. * wolfSSL OpenSSH clients because there was a bug in this flow that
  43793. * happened to "cancel out" if both sides of the connection had the bug.
  43794. */
  43795. enum {
  43796. NUM_ENCRYPTIONS = 3,
  43797. AAD_SIZE = 4
  43798. };
  43799. byte plainText1[] = {
  43800. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  43801. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  43802. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  43803. };
  43804. byte plainText2[] = {
  43805. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  43806. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  43807. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  43808. };
  43809. byte plainText3[] = {
  43810. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  43811. 0x5b, 0x57, 0x10
  43812. };
  43813. byte* plainTexts[NUM_ENCRYPTIONS] = {
  43814. plainText1,
  43815. plainText2,
  43816. plainText3
  43817. };
  43818. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  43819. sizeof(plainText1),
  43820. sizeof(plainText2),
  43821. sizeof(plainText3)
  43822. };
  43823. byte aad1[AAD_SIZE] = {
  43824. 0x00, 0x00, 0x00, 0x01
  43825. };
  43826. byte aad2[AAD_SIZE] = {
  43827. 0x00, 0x00, 0x00, 0x10
  43828. };
  43829. byte aad3[AAD_SIZE] = {
  43830. 0x00, 0x00, 0x01, 0x00
  43831. };
  43832. byte* aads[NUM_ENCRYPTIONS] = {
  43833. aad1,
  43834. aad2,
  43835. aad3
  43836. };
  43837. const byte iv[GCM_NONCE_MID_SZ] = {
  43838. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  43839. };
  43840. byte currentIv[GCM_NONCE_MID_SZ];
  43841. const byte key[] = {
  43842. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  43843. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  43844. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  43845. };
  43846. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  43847. {
  43848. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43849. 0xEF
  43850. },
  43851. {
  43852. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43853. 0xF0
  43854. },
  43855. {
  43856. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  43857. 0xF1
  43858. }
  43859. };
  43860. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  43861. {
  43862. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  43863. 0x3D, 0x32, 0x18, 0x34, 0xA9
  43864. },
  43865. {
  43866. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  43867. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  43868. },
  43869. {
  43870. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  43871. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  43872. }
  43873. };
  43874. const byte expCipherText1[] = {
  43875. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  43876. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  43877. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  43878. };
  43879. const byte expCipherText2[] = {
  43880. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  43881. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  43882. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  43883. };
  43884. const byte expCipherText3[] = {
  43885. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  43886. 0x80, 0x5E, 0x6B,
  43887. };
  43888. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  43889. expCipherText1,
  43890. expCipherText2,
  43891. expCipherText3
  43892. };
  43893. byte* cipherText = NULL;
  43894. byte* calcPlainText = NULL;
  43895. byte tag[AES_BLOCK_SIZE];
  43896. EVP_CIPHER_CTX* encCtx = NULL;
  43897. EVP_CIPHER_CTX* decCtx = NULL;
  43898. int i, j, outl;
  43899. /****************************************************/
  43900. for (i = 0; i < 3; ++i) {
  43901. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  43902. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  43903. /* First iteration, set key before IV. */
  43904. if (i == 0) {
  43905. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  43906. SSL_SUCCESS);
  43907. /*
  43908. * The call to EVP_CipherInit below (with NULL key) should clear the
  43909. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  43910. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  43911. * behavior.
  43912. */
  43913. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43914. (void*)iv), SSL_SUCCESS);
  43915. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  43916. SSL_SUCCESS);
  43917. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43918. currentIv), SSL_FAILURE);
  43919. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  43920. SSL_SUCCESS);
  43921. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  43922. SSL_SUCCESS);
  43923. }
  43924. /* Second iteration, IV before key. */
  43925. else {
  43926. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  43927. SSL_SUCCESS);
  43928. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  43929. SSL_SUCCESS);
  43930. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  43931. SSL_SUCCESS);
  43932. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  43933. SSL_SUCCESS);
  43934. }
  43935. /*
  43936. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  43937. * been issued first.
  43938. */
  43939. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43940. currentIv), SSL_FAILURE);
  43941. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43942. (void*)iv), SSL_SUCCESS);
  43943. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  43944. (void*)iv), SSL_SUCCESS);
  43945. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  43946. /*************** Encrypt ***************/
  43947. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43948. currentIv), SSL_SUCCESS);
  43949. /* Check current IV against expected. */
  43950. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43951. /* Add AAD. */
  43952. if (i == 2) {
  43953. /* Test streaming API. */
  43954. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  43955. AAD_SIZE), SSL_SUCCESS);
  43956. }
  43957. else {
  43958. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  43959. AAD_SIZE);
  43960. }
  43961. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  43962. DYNAMIC_TYPE_TMP_BUFFER));
  43963. /* Encrypt plaintext. */
  43964. if (i == 2) {
  43965. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  43966. plainTexts[j], plainTextSzs[j]),
  43967. SSL_SUCCESS);
  43968. }
  43969. else {
  43970. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  43971. plainTextSzs[j]), plainTextSzs[j]);
  43972. }
  43973. if (i == 2) {
  43974. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  43975. SSL_SUCCESS);
  43976. }
  43977. else {
  43978. /*
  43979. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  43980. * akin to calling EVP_CipherFinal.
  43981. */
  43982. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  43983. }
  43984. /* Check ciphertext against expected. */
  43985. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  43986. 0);
  43987. /* Get and check tag against expected. */
  43988. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  43989. sizeof(tag), tag), SSL_SUCCESS);
  43990. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  43991. /*************** Decrypt ***************/
  43992. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  43993. currentIv), SSL_SUCCESS);
  43994. /* Check current IV against expected. */
  43995. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  43996. /* Add AAD. */
  43997. if (i == 2) {
  43998. /* Test streaming API. */
  43999. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  44000. AAD_SIZE), SSL_SUCCESS);
  44001. }
  44002. else {
  44003. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  44004. AAD_SIZE);
  44005. }
  44006. /* Set expected tag. */
  44007. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  44008. sizeof(tag), tag), SSL_SUCCESS);
  44009. /* Decrypt ciphertext. */
  44010. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44011. DYNAMIC_TYPE_TMP_BUFFER));
  44012. if (i == 2) {
  44013. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  44014. cipherText, plainTextSzs[j]),
  44015. SSL_SUCCESS);
  44016. }
  44017. else {
  44018. /* This first EVP_Cipher call will check the tag, too. */
  44019. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  44020. plainTextSzs[j]), plainTextSzs[j]);
  44021. }
  44022. if (i == 2) {
  44023. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  44024. SSL_SUCCESS);
  44025. }
  44026. else {
  44027. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  44028. }
  44029. /* Check plaintext against expected. */
  44030. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  44031. 0);
  44032. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44033. cipherText = NULL;
  44034. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44035. calcPlainText = NULL;
  44036. }
  44037. EVP_CIPHER_CTX_free(encCtx);
  44038. encCtx = NULL;
  44039. EVP_CIPHER_CTX_free(decCtx);
  44040. decCtx = NULL;
  44041. }
  44042. #endif
  44043. return EXPECT_RESULT();
  44044. }
  44045. static int test_wolfSSL_OBJ_ln(void)
  44046. {
  44047. EXPECT_DECLS;
  44048. const int nid_set[] = {
  44049. NID_commonName,
  44050. NID_serialNumber,
  44051. NID_countryName,
  44052. NID_localityName,
  44053. NID_stateOrProvinceName,
  44054. NID_organizationName,
  44055. NID_organizationalUnitName,
  44056. NID_domainComponent,
  44057. NID_businessCategory,
  44058. NID_jurisdictionCountryName,
  44059. NID_jurisdictionStateOrProvinceName,
  44060. NID_emailAddress
  44061. };
  44062. const char* ln_set[] = {
  44063. "commonName",
  44064. "serialNumber",
  44065. "countryName",
  44066. "localityName",
  44067. "stateOrProvinceName",
  44068. "organizationName",
  44069. "organizationalUnitName",
  44070. "domainComponent",
  44071. "businessCategory",
  44072. "jurisdictionCountryName",
  44073. "jurisdictionStateOrProvinceName",
  44074. "emailAddress",
  44075. };
  44076. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  44077. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  44078. #ifdef HAVE_ECC
  44079. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44080. {
  44081. EC_builtin_curve r[27];
  44082. size_t nCurves = sizeof(r) / sizeof(r[0]);
  44083. nCurves = EC_get_builtin_curves(r, nCurves);
  44084. for (i = 0; i < nCurves; i++) {
  44085. /* skip ECC_CURVE_INVALID */
  44086. if (r[i].nid != ECC_CURVE_INVALID) {
  44087. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  44088. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  44089. }
  44090. }
  44091. }
  44092. #endif
  44093. #endif
  44094. for (i = 0; i < maxIdx; i++) {
  44095. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  44096. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  44097. }
  44098. return EXPECT_RESULT();
  44099. }
  44100. static int test_wolfSSL_OBJ_sn(void)
  44101. {
  44102. EXPECT_DECLS;
  44103. int i = 0, maxIdx = 7;
  44104. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  44105. NID_stateOrProvinceName,NID_organizationName,
  44106. NID_organizationalUnitName,NID_emailAddress};
  44107. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  44108. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  44109. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  44110. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  44111. WOLFSSL_EMAIL_ADDR};
  44112. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  44113. for (i = 0; i < maxIdx; i++) {
  44114. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  44115. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  44116. }
  44117. return EXPECT_RESULT();
  44118. }
  44119. #if !defined(NO_BIO)
  44120. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  44121. {
  44122. return lh_strhash(s[3]);
  44123. }
  44124. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  44125. {
  44126. return XSTRCMP(a[3], b[3]);
  44127. }
  44128. #endif
  44129. static int test_wolfSSL_TXT_DB(void)
  44130. {
  44131. EXPECT_DECLS;
  44132. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44133. BIO *bio = NULL;
  44134. TXT_DB *db = NULL;
  44135. const int columns = 6;
  44136. const char *fields[6] = {
  44137. "V",
  44138. "320926161116Z",
  44139. "",
  44140. "12BD",
  44141. "unknown",
  44142. "/CN=rsa doe",
  44143. };
  44144. char** fields_copy = NULL;
  44145. /* Test read */
  44146. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  44147. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  44148. ExpectNotNull(db = TXT_DB_read(bio, columns));
  44149. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  44150. DYNAMIC_TYPE_OPENSSL));
  44151. if (fields_copy != NULL) {
  44152. XMEMCPY(fields_copy, fields, sizeof(fields));
  44153. }
  44154. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  44155. if (EXPECT_FAIL()) {
  44156. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  44157. }
  44158. BIO_free(bio);
  44159. bio = NULL;
  44160. /* Test write */
  44161. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44162. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  44163. BIO_free(bio);
  44164. /* Test index */
  44165. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  44166. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  44167. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44168. fields[3] = "12DA";
  44169. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44170. fields[3] = "FFFF";
  44171. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44172. fields[3] = "";
  44173. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44174. TXT_DB_free(db);
  44175. #endif
  44176. return EXPECT_RESULT();
  44177. }
  44178. static int test_wolfSSL_NCONF(void)
  44179. {
  44180. EXPECT_DECLS;
  44181. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44182. const char* confFile = "./tests/NCONF_test.cnf";
  44183. CONF* conf = NULL;
  44184. long eline = 0;
  44185. long num = 0;
  44186. ExpectNotNull(conf = NCONF_new(NULL));
  44187. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  44188. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  44189. ExpectIntEQ(num, 1234);
  44190. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  44191. ExpectIntEQ(num, 4321);
  44192. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  44193. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  44194. "./test-dir/file1");
  44195. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  44196. "./test-dir/file1:./test-dir/file2:./section1:file2");
  44197. NCONF_free(conf);
  44198. #endif
  44199. return EXPECT_RESULT();
  44200. }
  44201. #endif /* OPENSSL_ALL */
  44202. static int test_wolfSSL_X509V3_EXT_get(void) {
  44203. EXPECT_DECLS;
  44204. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44205. XFILE f = XBADFILE;
  44206. int numOfExt =0;
  44207. int extNid = 0;
  44208. int i = 0;
  44209. WOLFSSL_X509* x509 = NULL;
  44210. WOLFSSL_X509_EXTENSION* ext = NULL;
  44211. const WOLFSSL_v3_ext_method* method = NULL;
  44212. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44213. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44214. if (f != XBADFILE)
  44215. XFCLOSE(f);
  44216. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  44217. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44218. for (i = 0; i < numOfExt; i++) {
  44219. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44220. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  44221. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44222. ExpectIntEQ(method->ext_nid, extNid);
  44223. }
  44224. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  44225. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  44226. wolfSSL_X509_free(x509);
  44227. #endif
  44228. return EXPECT_RESULT();
  44229. }
  44230. static int test_wolfSSL_X509V3_EXT_nconf(void)
  44231. {
  44232. EXPECT_DECLS;
  44233. #ifdef OPENSSL_ALL
  44234. const char *ext_names[] = {
  44235. "subjectKeyIdentifier",
  44236. "authorityKeyIdentifier",
  44237. "subjectAltName",
  44238. "keyUsage",
  44239. "extendedKeyUsage",
  44240. };
  44241. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  44242. int ext_nids[] = {
  44243. NID_subject_key_identifier,
  44244. NID_authority_key_identifier,
  44245. NID_subject_alt_name,
  44246. NID_key_usage,
  44247. NID_ext_key_usage,
  44248. };
  44249. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  44250. const char *ext_values[] = {
  44251. "hash",
  44252. "hash",
  44253. "DNS:example.com, IP:127.0.0.1",
  44254. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  44255. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  44256. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  44257. "OCSPSigning",
  44258. };
  44259. size_t i;
  44260. X509_EXTENSION* ext = NULL;
  44261. X509* x509 = NULL;
  44262. unsigned int keyUsageFlags;
  44263. unsigned int extKeyUsageFlags;
  44264. ExpectNotNull(x509 = X509_new());
  44265. /* keyUsage / extKeyUsage should match string above */
  44266. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44267. | KU_NON_REPUDIATION
  44268. | KU_KEY_ENCIPHERMENT
  44269. | KU_DATA_ENCIPHERMENT
  44270. | KU_KEY_AGREEMENT
  44271. | KU_KEY_CERT_SIGN
  44272. | KU_CRL_SIGN
  44273. | KU_ENCIPHER_ONLY
  44274. | KU_DECIPHER_ONLY;
  44275. extKeyUsageFlags = XKU_SSL_CLIENT
  44276. | XKU_SSL_SERVER
  44277. | XKU_CODE_SIGN
  44278. | XKU_SMIME
  44279. | XKU_TIMESTAMP
  44280. | XKU_OCSP_SIGN;
  44281. for (i = 0; i < ext_names_count; i++) {
  44282. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44283. ext_values[i]));
  44284. X509_EXTENSION_free(ext);
  44285. ext = NULL;
  44286. }
  44287. for (i = 0; i < ext_nids_count; i++) {
  44288. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  44289. ext_values[i]));
  44290. X509_EXTENSION_free(ext);
  44291. ext = NULL;
  44292. }
  44293. /* Test adding extension to X509 */
  44294. for (i = 0; i < ext_nids_count; i++) {
  44295. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44296. ext_values[i]));
  44297. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  44298. if (ext_nids[i] == NID_key_usage) {
  44299. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44300. }
  44301. else if (ext_nids[i] == NID_ext_key_usage) {
  44302. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  44303. }
  44304. X509_EXTENSION_free(ext);
  44305. ext = NULL;
  44306. }
  44307. X509_free(x509);
  44308. #endif
  44309. return EXPECT_RESULT();
  44310. }
  44311. static int test_wolfSSL_X509V3_EXT(void) {
  44312. EXPECT_DECLS;
  44313. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44314. XFILE f = XBADFILE;
  44315. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  44316. char* str = NULL;
  44317. unsigned char* data = NULL;
  44318. const WOLFSSL_v3_ext_method* method = NULL;
  44319. WOLFSSL_X509* x509 = NULL;
  44320. WOLFSSL_X509_EXTENSION* ext = NULL;
  44321. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  44322. WOLFSSL_ASN1_OBJECT *obj = NULL;
  44323. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  44324. WOLFSSL_ASN1_STRING* asn1str = NULL;
  44325. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  44326. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  44327. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  44328. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  44329. WOLFSSL_GENERAL_NAME* gn = NULL;
  44330. /* Check NULL argument */
  44331. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  44332. /* Using OCSP cert with X509V3 extensions */
  44333. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  44334. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44335. if (f != XBADFILE)
  44336. XFCLOSE(f);
  44337. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44338. /* Basic Constraints */
  44339. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44340. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44341. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  44342. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  44343. ExpectIntEQ(bc->ca, 1);
  44344. ExpectNull(bc->pathlen);
  44345. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  44346. bc = NULL;
  44347. i++;
  44348. /* Subject Key Identifier */
  44349. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44350. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44351. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  44352. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  44353. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  44354. asn1str));
  44355. X509_EXTENSION_free(ext2);
  44356. ext2 = NULL;
  44357. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44358. ExpectNotNull(method->i2s);
  44359. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  44360. wolfSSL_ASN1_STRING_free(asn1str);
  44361. asn1str = NULL;
  44362. if (str != NULL) {
  44363. actual = strcmp(str,
  44364. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  44365. }
  44366. ExpectIntEQ(actual, 0);
  44367. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44368. str = NULL;
  44369. i++;
  44370. /* Authority Key Identifier */
  44371. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44372. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44373. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  44374. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  44375. ext));
  44376. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44377. ExpectNotNull(asn1str = aKeyId->keyid);
  44378. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  44379. asn1str));
  44380. asn1str = NULL;
  44381. if (str != NULL) {
  44382. actual = strcmp(str,
  44383. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  44384. }
  44385. ExpectIntEQ(actual, 0);
  44386. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44387. str = NULL;
  44388. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  44389. aKeyId = NULL;
  44390. i++;
  44391. /* Key Usage */
  44392. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44393. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44394. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  44395. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  44396. #if defined(WOLFSSL_QT)
  44397. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  44398. #else
  44399. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  44400. #endif
  44401. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  44402. if (data != NULL) {
  44403. #ifdef BIG_ENDIAN_ORDER
  44404. actual = data[1];
  44405. #else
  44406. actual = data[0];
  44407. #endif
  44408. }
  44409. ExpectIntEQ(actual, expected);
  44410. wolfSSL_ASN1_STRING_free(asn1str);
  44411. asn1str = NULL;
  44412. #if 1
  44413. i++;
  44414. /* Authority Info Access */
  44415. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44416. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44417. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  44418. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  44419. ext));
  44420. #if defined(WOLFSSL_QT)
  44421. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  44422. #else
  44423. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  44424. #endif
  44425. /* URI entry is an ACCESS_DESCRIPTION type */
  44426. #if defined(WOLFSSL_QT)
  44427. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  44428. #else
  44429. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  44430. #endif
  44431. ExpectNotNull(adObj = ad->method);
  44432. /* Make sure nid is OCSP */
  44433. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  44434. /* GENERAL_NAME stores URI as an ASN1_STRING */
  44435. ExpectNotNull(gn = ad->location);
  44436. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  44437. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  44438. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  44439. #if defined(WOLFSSL_QT)
  44440. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  44441. #else
  44442. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  44443. #endif
  44444. if (str != NULL) {
  44445. actual = strcmp(str, "http://127.0.0.1:22220");
  44446. }
  44447. ExpectIntEQ(actual, 0);
  44448. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  44449. aia = NULL;
  44450. #else
  44451. (void) aia; (void) ad; (void) adObj; (void) gn;
  44452. #endif
  44453. wolfSSL_X509_free(x509);
  44454. #endif
  44455. return EXPECT_RESULT();
  44456. }
  44457. static int test_wolfSSL_X509_get_extension_flags(void)
  44458. {
  44459. EXPECT_DECLS;
  44460. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44461. XFILE f = XBADFILE;
  44462. X509* x509 = NULL;
  44463. unsigned int extFlags;
  44464. unsigned int keyUsageFlags;
  44465. unsigned int extKeyUsageFlags;
  44466. /* client-int-cert.pem has the following extension flags. */
  44467. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  44468. /* and the following key usage flags. */
  44469. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44470. | KU_NON_REPUDIATION
  44471. | KU_KEY_ENCIPHERMENT;
  44472. /* and the following extended key usage flags. */
  44473. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  44474. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  44475. XBADFILE);
  44476. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44477. if (f != XBADFILE) {
  44478. XFCLOSE(f);
  44479. f = XBADFILE;
  44480. }
  44481. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  44482. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44483. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  44484. X509_free(x509);
  44485. x509 = NULL;
  44486. /* client-cert-ext.pem has the following extension flags. */
  44487. extFlags = EXFLAG_KUSAGE;
  44488. /* and the following key usage flags. */
  44489. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44490. | KU_KEY_CERT_SIGN
  44491. | KU_CRL_SIGN;
  44492. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  44493. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44494. if (f != XBADFILE)
  44495. XFCLOSE(f);
  44496. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  44497. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44498. X509_free(x509);
  44499. #endif /* OPENSSL_ALL */
  44500. return EXPECT_RESULT();
  44501. }
  44502. static int test_wolfSSL_X509_get_ext(void)
  44503. {
  44504. EXPECT_DECLS;
  44505. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44506. int ret = 0;
  44507. XFILE f = XBADFILE;
  44508. WOLFSSL_X509* x509 = NULL;
  44509. WOLFSSL_X509_EXTENSION* foundExtension;
  44510. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44511. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44512. if (f != XBADFILE)
  44513. XFCLOSE(f);
  44514. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  44515. /* wolfSSL_X509_get_ext() valid input */
  44516. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  44517. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  44518. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  44519. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  44520. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  44521. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  44522. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  44523. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  44524. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  44525. wolfSSL_X509_free(x509);
  44526. #endif
  44527. return EXPECT_RESULT();
  44528. }
  44529. static int test_wolfSSL_X509_get_ext_by_NID(void)
  44530. {
  44531. EXPECT_DECLS;
  44532. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44533. int rc = 0;
  44534. XFILE f = XBADFILE;
  44535. WOLFSSL_X509* x509 = NULL;
  44536. ASN1_OBJECT* obj = NULL;
  44537. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44538. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44539. if (f != XBADFILE)
  44540. XFCLOSE(f);
  44541. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  44542. -1), 0);
  44543. /* Start search from last location (should fail) */
  44544. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  44545. rc), -1);
  44546. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  44547. -2), -1);
  44548. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  44549. -1), -1);
  44550. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  44551. /* NID_ext_key_usage, check also its nid and oid */
  44552. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  44553. -1);
  44554. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  44555. x509, rc)));
  44556. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  44557. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  44558. wolfSSL_X509_free(x509);
  44559. #endif
  44560. return EXPECT_RESULT();
  44561. }
  44562. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  44563. {
  44564. EXPECT_DECLS;
  44565. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44566. int rc = 0;
  44567. XFILE f = XBADFILE;
  44568. WOLFSSL_X509* x509 = NULL;
  44569. WOLFSSL_X509_EXTENSION* ext = NULL;
  44570. WOLFSSL_ASN1_STRING* sanString = NULL;
  44571. byte* sanDer = NULL;
  44572. const byte expectedDer[] = {
  44573. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  44574. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  44575. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44576. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  44577. if (f != XBADFILE)
  44578. XFCLOSE(f);
  44579. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  44580. ExpectNotNull(ext = X509_get_ext(x509, rc));
  44581. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  44582. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  44583. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  44584. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  44585. X509_free(x509);
  44586. #endif
  44587. return EXPECT_RESULT();
  44588. }
  44589. static int test_wolfSSL_X509_EXTENSION_new(void)
  44590. {
  44591. EXPECT_DECLS;
  44592. #if defined (OPENSSL_ALL)
  44593. WOLFSSL_X509_EXTENSION* ext = NULL;
  44594. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  44595. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  44596. wolfSSL_X509_EXTENSION_free(ext);
  44597. #endif
  44598. return EXPECT_RESULT();
  44599. }
  44600. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  44601. {
  44602. EXPECT_DECLS;
  44603. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44604. WOLFSSL_X509* x509 = NULL;
  44605. WOLFSSL_X509_EXTENSION* ext = NULL;
  44606. WOLFSSL_ASN1_OBJECT* o = NULL;
  44607. XFILE file = XBADFILE;
  44608. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44609. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44610. if (file != XBADFILE)
  44611. XFCLOSE(file);
  44612. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  44613. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44614. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  44615. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  44616. ExpectIntEQ(o->nid, 128);
  44617. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  44618. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  44619. wolfSSL_X509_free(x509);
  44620. #endif
  44621. return EXPECT_RESULT();
  44622. }
  44623. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  44624. {
  44625. EXPECT_DECLS;
  44626. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44627. WOLFSSL_X509* x509 = NULL;
  44628. WOLFSSL_X509_EXTENSION* ext = NULL;
  44629. WOLFSSL_ASN1_STRING* str = NULL;
  44630. XFILE file = XBADFILE;
  44631. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44632. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44633. if (file != XBADFILE)
  44634. XFCLOSE(file);
  44635. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44636. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  44637. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  44638. wolfSSL_X509_free(x509);
  44639. #endif
  44640. return EXPECT_RESULT();
  44641. }
  44642. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  44643. {
  44644. EXPECT_DECLS;
  44645. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44646. WOLFSSL_X509* x509 = NULL;
  44647. WOLFSSL_X509_EXTENSION* ext = NULL;
  44648. XFILE file = XBADFILE;
  44649. int crit = 0;
  44650. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44651. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  44652. if (file != XBADFILE)
  44653. XFCLOSE(file);
  44654. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  44655. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  44656. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  44657. wolfSSL_X509_free(x509);
  44658. #endif
  44659. return EXPECT_RESULT();
  44660. }
  44661. static int test_wolfSSL_X509V3_EXT_print(void)
  44662. {
  44663. EXPECT_DECLS;
  44664. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  44665. !defined(NO_RSA)
  44666. {
  44667. XFILE f = XBADFILE;
  44668. WOLFSSL_X509* x509 = NULL;
  44669. X509_EXTENSION * ext = NULL;
  44670. int loc = 0;
  44671. BIO *bio = NULL;
  44672. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  44673. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44674. if (f != XBADFILE)
  44675. fclose(f);
  44676. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  44677. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44678. NID_basic_constraints, -1), -1);
  44679. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44680. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44681. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44682. NID_subject_key_identifier, -1), -1);
  44683. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44684. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44685. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  44686. NID_authority_key_identifier, -1), -1);
  44687. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  44688. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  44689. wolfSSL_BIO_free(bio);
  44690. wolfSSL_X509_free(x509);
  44691. }
  44692. {
  44693. X509 *x509 = NULL;
  44694. BIO *bio = NULL;
  44695. X509_EXTENSION *ext = NULL;
  44696. unsigned int i = 0;
  44697. unsigned int idx = 0;
  44698. /* Some NIDs to test with */
  44699. int nids[] = {
  44700. /* NID_key_usage, currently X509_get_ext returns this as a bit
  44701. * string, which messes up X509V3_EXT_print */
  44702. /* NID_ext_key_usage, */
  44703. NID_subject_alt_name,
  44704. };
  44705. int* n = NULL;
  44706. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  44707. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  44708. WOLFSSL_FILETYPE_PEM));
  44709. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  44710. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  44711. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  44712. * update the index */
  44713. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  44714. ExpectNotNull(ext = X509_get_ext(x509, idx));
  44715. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  44716. ExpectIntGT(fprintf(stderr, "\n"), 0);
  44717. }
  44718. BIO_free(bio);
  44719. X509_free(x509);
  44720. }
  44721. #endif
  44722. return EXPECT_RESULT();
  44723. }
  44724. static int test_wolfSSL_X509_cmp(void)
  44725. {
  44726. EXPECT_DECLS;
  44727. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44728. XFILE file1 = XBADFILE;
  44729. XFILE file2 = XBADFILE;
  44730. WOLFSSL_X509* cert1 = NULL;
  44731. WOLFSSL_X509* cert2 = NULL;
  44732. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44733. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  44734. XBADFILE);
  44735. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  44736. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  44737. if (file1 != XBADFILE)
  44738. fclose(file1);
  44739. if (file2 != XBADFILE)
  44740. fclose(file2);
  44741. /* wolfSSL_X509_cmp() testing matching certs */
  44742. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  44743. /* wolfSSL_X509_cmp() testing mismatched certs */
  44744. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  44745. /* wolfSSL_X509_cmp() testing NULL, valid args */
  44746. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  44747. /* wolfSSL_X509_cmp() testing valid, NULL args */
  44748. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  44749. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  44750. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  44751. wolfSSL_X509_free(cert1);
  44752. wolfSSL_X509_free(cert2);
  44753. #endif
  44754. return EXPECT_RESULT();
  44755. }
  44756. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  44757. {
  44758. EXPECT_DECLS;
  44759. #if defined(OPENSSL_ALL)
  44760. EVP_PKEY* pkey;
  44761. pkey = EVP_PKEY_new();
  44762. ExpectNotNull(pkey);
  44763. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  44764. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44765. EVP_PKEY_free(pkey);
  44766. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  44767. EVP_PKEY_free(pkey);
  44768. EVP_PKEY_free(pkey);
  44769. #endif
  44770. return EXPECT_RESULT();
  44771. }
  44772. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  44773. {
  44774. EXPECT_DECLS;
  44775. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44776. EVP_PKEY* pkey = NULL;
  44777. const unsigned char* p;
  44778. unsigned char *der = NULL;
  44779. unsigned char *tmp = NULL;
  44780. int derLen;
  44781. p = client_keypub_der_2048;
  44782. /* Check that key can be successfully decoded. */
  44783. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  44784. sizeof_client_keypub_der_2048));
  44785. /* Check that key can be successfully encoded. */
  44786. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44787. /* Ensure that the encoded version matches the original. */
  44788. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  44789. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44790. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44791. * is advanced. */
  44792. tmp = der;
  44793. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44794. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  44795. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  44796. ExpectTrue(der + derLen == tmp);
  44797. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44798. EVP_PKEY_free(pkey);
  44799. #endif
  44800. return EXPECT_RESULT();
  44801. }
  44802. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  44803. {
  44804. EXPECT_DECLS;
  44805. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  44806. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44807. EVP_PKEY* pkey = NULL;
  44808. const unsigned char* p;
  44809. unsigned char *der = NULL;
  44810. unsigned char *tmp = NULL;
  44811. int derLen;
  44812. unsigned char pub_buf[65];
  44813. const int pub_len = 65;
  44814. BN_CTX* ctx;
  44815. EC_GROUP* curve = NULL;
  44816. EC_KEY* ephemeral_key = NULL;
  44817. const EC_POINT* h;
  44818. /* Generate an x963 key pair and get public part into pub_buf */
  44819. ExpectNotNull(ctx = BN_CTX_new());
  44820. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  44821. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  44822. NID_X9_62_prime256v1));
  44823. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  44824. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  44825. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  44826. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  44827. /* Prepare the EVP_PKEY */
  44828. ExpectNotNull(pkey = EVP_PKEY_new());
  44829. p = pub_buf;
  44830. /* Check that key can be successfully decoded. */
  44831. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  44832. pub_len));
  44833. /* Check that key can be successfully encoded. */
  44834. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  44835. /* Ensure that the encoded version matches the original. */
  44836. ExpectIntEQ(derLen, pub_len);
  44837. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44838. /* Do same test except with pre-allocated buffer to ensure the der pointer
  44839. * is advanced. */
  44840. tmp = der;
  44841. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  44842. ExpectIntEQ(derLen, pub_len);
  44843. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  44844. ExpectTrue(der + derLen == tmp);
  44845. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44846. EVP_PKEY_free(pkey);
  44847. EC_KEY_free(ephemeral_key);
  44848. EC_GROUP_free(curve);
  44849. #endif
  44850. return EXPECT_RESULT();
  44851. }
  44852. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  44853. {
  44854. EXPECT_DECLS;
  44855. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  44856. DSA* dsa = NULL;
  44857. byte derIn[] = {
  44858. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  44859. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  44860. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  44861. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  44862. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  44863. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  44864. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  44865. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  44866. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  44867. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  44868. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  44869. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  44870. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  44871. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  44872. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  44873. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  44874. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  44875. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  44876. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  44877. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  44878. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  44879. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  44880. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  44881. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  44882. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  44883. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  44884. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  44885. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  44886. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  44887. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  44888. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  44889. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  44890. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  44891. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  44892. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  44893. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  44894. 0x1e, 0xde, 0xc4
  44895. };
  44896. int derInLen = sizeof(derIn);
  44897. byte* derOut = NULL;
  44898. int derOutLen;
  44899. byte* p = derIn;
  44900. /* Check that params can be successfully decoded. */
  44901. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  44902. /* Check that params can be successfully encoded. */
  44903. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  44904. /* Ensure that the encoded version matches the original. */
  44905. ExpectIntEQ(derInLen, derOutLen);
  44906. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  44907. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  44908. DSA_free(dsa);
  44909. #endif
  44910. return EXPECT_RESULT();
  44911. }
  44912. static int test_wolfSSL_i2d_PrivateKey(void)
  44913. {
  44914. EXPECT_DECLS;
  44915. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  44916. !defined(NO_ASN) && !defined(NO_PWDBASED)
  44917. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44918. {
  44919. EVP_PKEY* pkey = NULL;
  44920. const unsigned char* server_key =
  44921. (const unsigned char*)server_key_der_2048;
  44922. unsigned char buf[FOURK_BUF];
  44923. unsigned char* pt = NULL;
  44924. int bufSz = 0;
  44925. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  44926. (long)sizeof_server_key_der_2048));
  44927. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  44928. pt = buf;
  44929. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  44930. ExpectIntNE((pt - buf), 0);
  44931. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  44932. EVP_PKEY_free(pkey);
  44933. }
  44934. #endif
  44935. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44936. {
  44937. EVP_PKEY* pkey = NULL;
  44938. const unsigned char* client_key =
  44939. (const unsigned char*)ecc_clikey_der_256;
  44940. unsigned char buf[FOURK_BUF];
  44941. unsigned char* pt = NULL;
  44942. int bufSz = 0;
  44943. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  44944. (long)sizeof_ecc_clikey_der_256)));
  44945. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  44946. pt = buf;
  44947. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  44948. ExpectIntNE((pt - buf), 0);
  44949. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  44950. EVP_PKEY_free(pkey);
  44951. }
  44952. #endif
  44953. #endif
  44954. return EXPECT_RESULT();
  44955. }
  44956. static int test_wolfSSL_OCSP_id_get0_info(void)
  44957. {
  44958. EXPECT_DECLS;
  44959. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  44960. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44961. X509* cert = NULL;
  44962. X509* issuer = NULL;
  44963. OCSP_CERTID* id = NULL;
  44964. OCSP_CERTID* id2 = NULL;
  44965. ASN1_STRING* name = NULL;
  44966. ASN1_OBJECT* pmd = NULL;
  44967. ASN1_STRING* keyHash = NULL;
  44968. ASN1_INTEGER* serial = NULL;
  44969. ASN1_INTEGER* x509Int = NULL;
  44970. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  44971. SSL_FILETYPE_PEM));
  44972. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  44973. SSL_FILETYPE_PEM));
  44974. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  44975. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  44976. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  44977. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  44978. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  44979. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  44980. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  44981. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  44982. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  44983. ExpectNotNull(serial);
  44984. /* compare serial number to one in cert, should be equal */
  44985. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  44986. ExpectIntEQ(x509Int->length, serial->length);
  44987. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  44988. /* test OCSP_id_cmp */
  44989. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  44990. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  44991. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  44992. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  44993. if (id != NULL) {
  44994. id->issuerHash[0] = ~id->issuerHash[0];
  44995. }
  44996. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  44997. OCSP_CERTID_free(id);
  44998. OCSP_CERTID_free(id2);
  44999. X509_free(cert); /* free's x509Int */
  45000. X509_free(issuer);
  45001. #endif
  45002. return EXPECT_RESULT();
  45003. }
  45004. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  45005. {
  45006. EXPECT_DECLS;
  45007. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45008. WOLFSSL_OCSP_CERTID certId;
  45009. byte* targetBuffer = NULL;
  45010. byte* p;
  45011. /* OCSP CertID bytes taken from PCAP */
  45012. byte rawCertId[] = {
  45013. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45014. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45015. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45016. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45017. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45018. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45019. 0xcf, 0xbc, 0x91
  45020. };
  45021. int ret = 0;
  45022. int i;
  45023. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  45024. certId.rawCertId = rawCertId;
  45025. certId.rawCertIdSize = sizeof(rawCertId);
  45026. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  45027. DYNAMIC_TYPE_TMP_BUFFER));
  45028. p = targetBuffer;
  45029. /* Function returns the size of the encoded data. */
  45030. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  45031. /* If target buffer is not null, function increments targetBuffer to point
  45032. * just past the end of the encoded data. */
  45033. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  45034. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45035. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45036. }
  45037. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45038. targetBuffer = NULL;
  45039. /* If target buffer is null, function allocates memory for a buffer and
  45040. * copies the encoded data into it. targetBuffer then points to the start of
  45041. * this newly allocate buffer. */
  45042. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  45043. sizeof(rawCertId));
  45044. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45045. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45046. }
  45047. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  45048. #endif
  45049. return EXPECT_RESULT();
  45050. }
  45051. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  45052. {
  45053. EXPECT_DECLS;
  45054. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45055. WOLFSSL_OCSP_CERTID* certId;
  45056. WOLFSSL_OCSP_CERTID* certIdGood;
  45057. WOLFSSL_OCSP_CERTID* certIdBad;
  45058. const unsigned char* rawCertIdPtr;
  45059. const unsigned char rawCertId[] = {
  45060. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45061. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45062. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45063. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45064. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45065. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45066. 0xcf, 0xbc, 0x91
  45067. };
  45068. rawCertIdPtr = &rawCertId[0];
  45069. /* If the cert ID is NULL the function should allocate it and copy the
  45070. * data to it. */
  45071. certId = NULL;
  45072. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45073. sizeof(rawCertId)));
  45074. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45075. if (certId != NULL) {
  45076. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45077. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  45078. }
  45079. /* If the cert ID is not NULL the function will just copy the data to it. */
  45080. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  45081. DYNAMIC_TYPE_TMP_BUFFER));
  45082. ExpectNotNull(certId);
  45083. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  45084. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  45085. rawCertIdPtr = &rawCertId[0];
  45086. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45087. sizeof(rawCertId)));
  45088. ExpectPtrEq(certIdGood, certId);
  45089. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45090. if (certId != NULL) {
  45091. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45092. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45093. certId = NULL;
  45094. }
  45095. /* The below tests should fail when passed bad parameters. NULL should
  45096. * always be returned. */
  45097. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  45098. sizeof(rawCertId)));
  45099. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  45100. sizeof(rawCertId)));
  45101. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  45102. #endif
  45103. return EXPECT_RESULT();
  45104. }
  45105. static int test_wolfSSL_OCSP_id_cmp(void)
  45106. {
  45107. EXPECT_DECLS;
  45108. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45109. OCSP_CERTID id1;
  45110. OCSP_CERTID id2;
  45111. XMEMSET(&id1, 0, sizeof(id1));
  45112. XMEMSET(&id2, 0, sizeof(id2));
  45113. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  45114. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45115. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  45116. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  45117. #endif
  45118. return EXPECT_RESULT();
  45119. }
  45120. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  45121. {
  45122. EXPECT_DECLS;
  45123. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45124. WOLFSSL_OCSP_SINGLERESP single;
  45125. const WOLFSSL_OCSP_CERTID* certId;
  45126. XMEMSET(&single, 0, sizeof(single));
  45127. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  45128. ExpectPtrEq(&single, certId);
  45129. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  45130. #endif
  45131. return EXPECT_RESULT();
  45132. }
  45133. static int test_wolfSSL_OCSP_single_get0_status(void)
  45134. {
  45135. EXPECT_DECLS;
  45136. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45137. WOLFSSL_OCSP_SINGLERESP single;
  45138. CertStatus certStatus;
  45139. WOLFSSL_ASN1_TIME* thisDate;
  45140. WOLFSSL_ASN1_TIME* nextDate;
  45141. int ret, i;
  45142. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45143. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  45144. /* Fill the date fields with some dummy data. */
  45145. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  45146. certStatus.thisDateParsed.data[i] = i;
  45147. certStatus.nextDateParsed.data[i] = i;
  45148. }
  45149. certStatus.status = CERT_GOOD;
  45150. single.status = &certStatus;
  45151. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  45152. &nextDate);
  45153. ExpectIntEQ(ret, CERT_GOOD);
  45154. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  45155. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  45156. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  45157. CERT_GOOD);
  45158. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  45159. NULL), CERT_GOOD);
  45160. #endif
  45161. return EXPECT_RESULT();
  45162. }
  45163. static int test_wolfSSL_OCSP_resp_count(void)
  45164. {
  45165. EXPECT_DECLS;
  45166. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45167. WOLFSSL_OCSP_BASICRESP basicResp;
  45168. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45169. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45170. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45171. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45172. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45173. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  45174. basicResp.single = &singleRespOne;
  45175. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  45176. singleRespOne.next = &singleRespTwo;
  45177. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  45178. #endif
  45179. return EXPECT_RESULT();
  45180. }
  45181. static int test_wolfSSL_OCSP_resp_get0(void)
  45182. {
  45183. EXPECT_DECLS;
  45184. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45185. WOLFSSL_OCSP_BASICRESP basicResp;
  45186. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45187. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45188. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45189. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45190. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45191. basicResp.single = &singleRespOne;
  45192. singleRespOne.next = &singleRespTwo;
  45193. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  45194. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  45195. #endif
  45196. return EXPECT_RESULT();
  45197. }
  45198. static int test_wolfSSL_EVP_PKEY_derive(void)
  45199. {
  45200. EXPECT_DECLS;
  45201. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45202. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  45203. EVP_PKEY_CTX *ctx = NULL;
  45204. unsigned char *skey = NULL;
  45205. size_t skeylen;
  45206. EVP_PKEY *pkey = NULL;
  45207. EVP_PKEY *peerkey = NULL;
  45208. const unsigned char* key;
  45209. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  45210. /* DH */
  45211. key = dh_key_der_2048;
  45212. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45213. sizeof_dh_key_der_2048)));
  45214. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  45215. key = dh_key_der_2048;
  45216. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45217. sizeof_dh_key_der_2048)));
  45218. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  45219. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45220. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45221. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45222. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45223. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45224. DYNAMIC_TYPE_OPENSSL));
  45225. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45226. EVP_PKEY_CTX_free(ctx);
  45227. ctx = NULL;
  45228. EVP_PKEY_free(peerkey);
  45229. peerkey = NULL;
  45230. EVP_PKEY_free(pkey);
  45231. pkey = NULL;
  45232. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45233. skey = NULL;
  45234. #endif
  45235. #ifdef HAVE_ECC
  45236. /* ECDH */
  45237. key = ecc_clikey_der_256;
  45238. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  45239. sizeof_ecc_clikey_der_256)));
  45240. key = ecc_clikeypub_der_256;
  45241. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  45242. sizeof_ecc_clikeypub_der_256)));
  45243. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45244. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45245. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45246. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45247. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45248. DYNAMIC_TYPE_OPENSSL));
  45249. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45250. EVP_PKEY_CTX_free(ctx);
  45251. EVP_PKEY_free(peerkey);
  45252. EVP_PKEY_free(pkey);
  45253. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45254. #endif /* HAVE_ECC */
  45255. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  45256. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45257. return EXPECT_RESULT();
  45258. }
  45259. static int test_wolfSSL_EVP_PBE_scrypt(void)
  45260. {
  45261. EXPECT_DECLS;
  45262. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  45263. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  45264. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  45265. int ret;
  45266. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  45267. int pwdlen = sizeof(pwd);
  45268. const byte salt[] = {'N','a','C','l'};
  45269. int saltlen = sizeof(salt);
  45270. byte key[80];
  45271. word64 numOvr32 = (word64)INT32_MAX + 1;
  45272. /* expected derived key for N:16, r:1, p:1 */
  45273. const byte expectedKey[] = {
  45274. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  45275. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  45276. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  45277. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  45278. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  45279. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  45280. 0xE7, 0xE9, 0xC0, 0x9A};
  45281. /* N r p mx key keylen */
  45282. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  45283. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  45284. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  45285. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  45286. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  45287. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  45288. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  45289. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  45290. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  45291. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  45292. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  45293. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  45294. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  45295. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  45296. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  45297. ExpectIntEQ(ret, 1); /* should succeed */
  45298. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  45299. key, 64);
  45300. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  45301. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  45302. key, 64);
  45303. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  45304. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  45305. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  45306. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  45307. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  45308. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  45309. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  45310. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  45311. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  45312. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  45313. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  45314. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  45315. ExpectIntEQ(ret, 1);
  45316. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  45317. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  45318. #endif /* !NO_PWDBASED && !NO_SHA256 */
  45319. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  45320. return EXPECT_RESULT();
  45321. }
  45322. static int test_no_op_functions(void)
  45323. {
  45324. EXPECT_DECLS;
  45325. #if defined(OPENSSL_EXTRA)
  45326. /* this makes sure wolfSSL can compile and run these no-op functions */
  45327. SSL_load_error_strings();
  45328. ENGINE_load_builtin_engines();
  45329. OpenSSL_add_all_ciphers();
  45330. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  45331. #endif
  45332. return EXPECT_RESULT();
  45333. }
  45334. static int test_wolfSSL_CRYPTO_memcmp(void)
  45335. {
  45336. EXPECT_DECLS;
  45337. #ifdef OPENSSL_EXTRA
  45338. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45339. "implementation of TLS/SSL for embedded devices to the cloud.";
  45340. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45341. "implementation of TLS/SSL for embedded devices to the cloud.";
  45342. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45343. "implementation of TLS/SSL for embedded devices to the cloud!";
  45344. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  45345. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  45346. #endif
  45347. return EXPECT_RESULT();
  45348. }
  45349. /*----------------------------------------------------------------------------*
  45350. | wolfCrypt ASN
  45351. *----------------------------------------------------------------------------*/
  45352. static int test_wc_CreateEncryptedPKCS8Key(void)
  45353. {
  45354. EXPECT_DECLS;
  45355. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  45356. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  45357. WC_RNG rng;
  45358. byte* encKey = NULL;
  45359. word32 encKeySz = 0;
  45360. word32 decKeySz = 0;
  45361. const char password[] = "Lorem ipsum dolor sit amet";
  45362. word32 passwordSz = (word32)XSTRLEN(password);
  45363. word32 tradIdx = 0;
  45364. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45365. ExpectIntEQ(wc_InitRng(&rng), 0);
  45366. /* Call with NULL for out buffer to get necessary length. */
  45367. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  45368. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  45369. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  45370. LENGTH_ONLY_E);
  45371. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  45372. DYNAMIC_TYPE_TMP_BUFFER));
  45373. /* Call with the allocated out buffer. */
  45374. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  45375. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  45376. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  45377. 0);
  45378. /* Decrypt the encrypted PKCS8 key we just made. */
  45379. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  45380. passwordSz)), 0);
  45381. /* encKey now holds the decrypted key (decrypted in place). */
  45382. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  45383. /* Check that the decrypted key matches the key prior to encryption. */
  45384. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  45385. sizeof_server_key_der_2048), 0);
  45386. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  45387. wc_FreeRng(&rng);
  45388. #endif
  45389. return EXPECT_RESULT();
  45390. }
  45391. static int test_wc_GetPkcs8TraditionalOffset(void)
  45392. {
  45393. EXPECT_DECLS;
  45394. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  45395. int length;
  45396. int derSz = 0;
  45397. word32 inOutIdx;
  45398. const char* path = "./certs/server-keyPkcs8.der";
  45399. XFILE file = XBADFILE;
  45400. byte der[2048];
  45401. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  45402. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  45403. if (file != XBADFILE)
  45404. XFCLOSE(file);
  45405. /* valid case */
  45406. inOutIdx = 0;
  45407. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  45408. 0);
  45409. /* inOutIdx > sz */
  45410. inOutIdx = 4000;
  45411. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  45412. BAD_FUNC_ARG);
  45413. /* null input */
  45414. inOutIdx = 0;
  45415. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  45416. BAD_FUNC_ARG);
  45417. /* invalid input, fill buffer with 1's */
  45418. XMEMSET(der, 1, sizeof(der));
  45419. inOutIdx = 0;
  45420. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  45421. ASN_PARSE_E);
  45422. #endif /* NO_ASN */
  45423. return EXPECT_RESULT();
  45424. }
  45425. static int test_wc_SetSubjectRaw(void)
  45426. {
  45427. EXPECT_DECLS;
  45428. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45429. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45430. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45431. WOLFSSL_X509* x509 = NULL;
  45432. int peerCertSz;
  45433. const byte* peerCertBuf = NULL;
  45434. Cert forgedCert;
  45435. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  45436. WOLFSSL_FILETYPE_ASN1));
  45437. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45438. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  45439. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  45440. wolfSSL_FreeX509(x509);
  45441. #endif
  45442. return EXPECT_RESULT();
  45443. }
  45444. static int test_wc_GetSubjectRaw(void)
  45445. {
  45446. EXPECT_DECLS;
  45447. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45448. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  45449. Cert cert;
  45450. byte *subjectRaw;
  45451. ExpectIntEQ(0, wc_InitCert(&cert));
  45452. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  45453. #endif
  45454. return EXPECT_RESULT();
  45455. }
  45456. static int test_wc_SetIssuerRaw(void)
  45457. {
  45458. EXPECT_DECLS;
  45459. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45460. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45461. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45462. WOLFSSL_X509* x509 = NULL;
  45463. int peerCertSz;
  45464. const byte* peerCertBuf;
  45465. Cert forgedCert;
  45466. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  45467. WOLFSSL_FILETYPE_ASN1));
  45468. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45469. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  45470. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  45471. wolfSSL_FreeX509(x509);
  45472. #endif
  45473. return EXPECT_RESULT();
  45474. }
  45475. static int test_wc_SetIssueBuffer(void)
  45476. {
  45477. EXPECT_DECLS;
  45478. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45479. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  45480. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  45481. WOLFSSL_X509* x509 = NULL;
  45482. int peerCertSz;
  45483. const byte* peerCertBuf;
  45484. Cert forgedCert;
  45485. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  45486. WOLFSSL_FILETYPE_ASN1));
  45487. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  45488. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  45489. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  45490. wolfSSL_FreeX509(x509);
  45491. #endif
  45492. return EXPECT_RESULT();
  45493. }
  45494. /*
  45495. * Testing wc_SetSubjectKeyId
  45496. */
  45497. static int test_wc_SetSubjectKeyId(void)
  45498. {
  45499. EXPECT_DECLS;
  45500. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45501. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  45502. Cert cert;
  45503. const char* file = "certs/ecc-client-keyPub.pem";
  45504. ExpectIntEQ(0, wc_InitCert(&cert));
  45505. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  45506. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  45507. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  45508. #endif
  45509. return EXPECT_RESULT();
  45510. } /* END test_wc_SetSubjectKeyId */
  45511. /*
  45512. * Testing wc_SetSubject
  45513. */
  45514. static int test_wc_SetSubject(void)
  45515. {
  45516. EXPECT_DECLS;
  45517. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  45518. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  45519. Cert cert;
  45520. const char* file = "./certs/ca-ecc-cert.pem";
  45521. ExpectIntEQ(0, wc_InitCert(&cert));
  45522. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  45523. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  45524. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  45525. #endif
  45526. return EXPECT_RESULT();
  45527. } /* END test_wc_SetSubject */
  45528. static int test_CheckCertSignature(void)
  45529. {
  45530. EXPECT_DECLS;
  45531. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  45532. WOLFSSL_CERT_MANAGER* cm = NULL;
  45533. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  45534. XFILE fp = XBADFILE;
  45535. byte cert[4096];
  45536. int certSz;
  45537. #endif
  45538. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  45539. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  45540. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  45541. #ifndef NO_RSA
  45542. #ifdef USE_CERT_BUFFERS_1024
  45543. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  45544. sizeof_server_cert_der_1024, NULL, cm));
  45545. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45546. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  45547. WOLFSSL_FILETYPE_ASN1));
  45548. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  45549. sizeof_server_cert_der_1024, NULL, cm));
  45550. #elif defined(USE_CERT_BUFFERS_2048)
  45551. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  45552. sizeof_server_cert_der_2048, NULL, cm));
  45553. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45554. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  45555. WOLFSSL_FILETYPE_ASN1));
  45556. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  45557. sizeof_server_cert_der_2048, NULL, cm));
  45558. #endif
  45559. #endif
  45560. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45561. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  45562. sizeof_serv_ecc_der_256, NULL, cm));
  45563. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  45564. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  45565. WOLFSSL_FILETYPE_ASN1));
  45566. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  45567. NULL, cm));
  45568. #endif
  45569. #if !defined(NO_FILESYSTEM)
  45570. wolfSSL_CertManagerFree(cm);
  45571. cm = NULL;
  45572. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  45573. #ifndef NO_RSA
  45574. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  45575. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  45576. if (fp != XBADFILE) {
  45577. XFCLOSE(fp);
  45578. fp = XBADFILE;
  45579. }
  45580. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  45581. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  45582. "./certs/ca-cert.pem", NULL));
  45583. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  45584. #endif
  45585. #ifdef HAVE_ECC
  45586. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  45587. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  45588. if (fp != XBADFILE) {
  45589. XFCLOSE(fp);
  45590. fp = XBADFILE;
  45591. }
  45592. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  45593. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  45594. "./certs/ca-ecc-cert.pem", NULL));
  45595. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  45596. #endif
  45597. #endif
  45598. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  45599. (void)fp;
  45600. (void)cert;
  45601. (void)certSz;
  45602. #endif
  45603. wolfSSL_CertManagerFree(cm);
  45604. #endif
  45605. return EXPECT_RESULT();
  45606. }
  45607. static int test_wc_ParseCert(void)
  45608. {
  45609. EXPECT_DECLS;
  45610. #if !defined(NO_CERTS) && !defined(NO_RSA)
  45611. DecodedCert decodedCert;
  45612. const byte* rawCert = client_cert_der_2048;
  45613. const int rawCertSize = sizeof_client_cert_der_2048;
  45614. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  45615. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45616. #ifndef IGNORE_NAME_CONSTRAINTS
  45617. /* check that the subjects emailAddress was not put in the alt name list */
  45618. ExpectNotNull(decodedCert.subjectEmail);
  45619. ExpectNull(decodedCert.altEmailNames);
  45620. #endif
  45621. wc_FreeDecodedCert(&decodedCert);
  45622. #endif
  45623. return EXPECT_RESULT();
  45624. }
  45625. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  45626. * the API safely errors out on badly-formed ASN input.
  45627. * NOTE: Test not compatible with released FIPS implementations!
  45628. */
  45629. static int test_wc_ParseCert_Error(void)
  45630. {
  45631. EXPECT_DECLS;
  45632. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  45633. (!defined(HAVE_FIPS) || \
  45634. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  45635. DecodedCert decodedCert;
  45636. int i;
  45637. /* Certificate data */
  45638. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  45639. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  45640. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  45641. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  45642. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  45643. /* Test data */
  45644. const struct testStruct {
  45645. const byte* c;
  45646. const int cSz;
  45647. const int expRet;
  45648. } t[] = {
  45649. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  45650. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  45651. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  45652. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  45653. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  45654. };
  45655. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  45656. for (i = 0; i < tSz; i++) {
  45657. WOLFSSL_MSG_EX("i == %d", i);
  45658. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  45659. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  45660. wc_FreeDecodedCert(&decodedCert);
  45661. }
  45662. #endif
  45663. return EXPECT_RESULT();
  45664. }
  45665. static int test_MakeCertWithPathLen(void)
  45666. {
  45667. EXPECT_DECLS;
  45668. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  45669. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  45670. const byte expectedPathLen = 7;
  45671. Cert cert;
  45672. DecodedCert decodedCert;
  45673. byte der[FOURK_BUF];
  45674. int derSize = 0;
  45675. WC_RNG rng;
  45676. ecc_key key;
  45677. int ret;
  45678. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45679. XMEMSET(&key, 0, sizeof(ecc_key));
  45680. XMEMSET(&cert, 0, sizeof(Cert));
  45681. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  45682. ExpectIntEQ(wc_InitRng(&rng), 0);
  45683. ExpectIntEQ(wc_ecc_init(&key), 0);
  45684. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  45685. ExpectIntEQ(wc_InitCert(&cert), 0);
  45686. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  45687. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  45688. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  45689. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  45690. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  45691. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  45692. CTC_NAME_SIZE);
  45693. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  45694. CTC_NAME_SIZE);
  45695. cert.selfSigned = 1;
  45696. cert.isCA = 1;
  45697. cert.pathLen = expectedPathLen;
  45698. cert.pathLenSet = 1;
  45699. cert.sigType = CTC_SHA256wECDSA;
  45700. #ifdef WOLFSSL_CERT_EXT
  45701. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  45702. #endif
  45703. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  45704. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  45705. FOURK_BUF, NULL, &key, &rng), 0);
  45706. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  45707. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45708. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  45709. wc_FreeDecodedCert(&decodedCert);
  45710. ret = wc_ecc_free(&key);
  45711. ExpectIntEQ(ret, 0);
  45712. ret = wc_FreeRng(&rng);
  45713. ExpectIntEQ(ret, 0);
  45714. #endif
  45715. return EXPECT_RESULT();
  45716. }
  45717. static int test_MakeCertWithCaFalse(void)
  45718. {
  45719. EXPECT_DECLS;
  45720. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  45721. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  45722. const byte expectedIsCa = 0;
  45723. Cert cert;
  45724. DecodedCert decodedCert;
  45725. byte der[FOURK_BUF];
  45726. int derSize = 0;
  45727. WC_RNG rng;
  45728. ecc_key key;
  45729. int ret;
  45730. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45731. XMEMSET(&key, 0, sizeof(ecc_key));
  45732. XMEMSET(&cert, 0, sizeof(Cert));
  45733. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  45734. ExpectIntEQ(wc_InitRng(&rng), 0);
  45735. ExpectIntEQ(wc_ecc_init(&key), 0);
  45736. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  45737. ExpectIntEQ(wc_InitCert(&cert), 0);
  45738. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  45739. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  45740. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  45741. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  45742. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  45743. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  45744. CTC_NAME_SIZE);
  45745. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  45746. CTC_NAME_SIZE);
  45747. cert.selfSigned = 1;
  45748. cert.isCA = expectedIsCa;
  45749. cert.isCaSet = 1;
  45750. cert.sigType = CTC_SHA256wECDSA;
  45751. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  45752. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  45753. FOURK_BUF, NULL, &key, &rng), 0);
  45754. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  45755. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  45756. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  45757. wc_FreeDecodedCert(&decodedCert);
  45758. ret = wc_ecc_free(&key);
  45759. ExpectIntEQ(ret, 0);
  45760. ret = wc_FreeRng(&rng);
  45761. ExpectIntEQ(ret, 0);
  45762. #endif
  45763. return EXPECT_RESULT();
  45764. }
  45765. /*----------------------------------------------------------------------------*
  45766. | wolfCrypt ECC
  45767. *----------------------------------------------------------------------------*/
  45768. static int test_wc_ecc_get_curve_size_from_name(void)
  45769. {
  45770. EXPECT_DECLS;
  45771. #ifdef HAVE_ECC
  45772. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45773. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  45774. #endif
  45775. /* invalid case */
  45776. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  45777. /* NULL input */
  45778. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  45779. #endif /* HAVE_ECC */
  45780. return EXPECT_RESULT();
  45781. }
  45782. static int test_wc_ecc_get_curve_id_from_name(void)
  45783. {
  45784. EXPECT_DECLS;
  45785. #ifdef HAVE_ECC
  45786. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45787. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  45788. ECC_SECP256R1);
  45789. #endif
  45790. /* invalid case */
  45791. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  45792. /* NULL input */
  45793. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  45794. #endif /* HAVE_ECC */
  45795. return EXPECT_RESULT();
  45796. }
  45797. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  45798. !defined(HAVE_SELFTEST) && \
  45799. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  45800. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  45801. {
  45802. EXPECT_DECLS;
  45803. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45804. ecc_key* key;
  45805. const ecc_set_type* params = NULL;
  45806. int ret;
  45807. #endif
  45808. WOLFSSL_EC_KEY *ecKey = NULL;
  45809. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45810. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  45811. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  45812. if (EXPECT_SUCCESS()) {
  45813. ret = EC_KEY_generate_key(ecKey);
  45814. } else
  45815. ret = 0;
  45816. if (ret == 1) {
  45817. /* normal test */
  45818. key = (ecc_key*)ecKey->internal;
  45819. if (key != NULL) {
  45820. params = key->dp;
  45821. }
  45822. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  45823. ECC_SECP256R1);
  45824. }
  45825. #endif
  45826. /* invalid case, NULL input*/
  45827. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  45828. wolfSSL_EC_KEY_free(ecKey);
  45829. return EXPECT_RESULT();
  45830. }
  45831. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  45832. static int test_wc_ecc_get_curve_id_from_params(void)
  45833. {
  45834. EXPECT_DECLS;
  45835. #ifdef HAVE_ECC
  45836. const byte prime[] =
  45837. {
  45838. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45839. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45840. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45841. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  45842. };
  45843. const byte primeInvalid[] =
  45844. {
  45845. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45846. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45847. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45848. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  45849. };
  45850. const byte Af[] =
  45851. {
  45852. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  45853. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45854. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  45855. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  45856. };
  45857. const byte Bf[] =
  45858. {
  45859. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  45860. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  45861. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  45862. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  45863. };
  45864. const byte order[] =
  45865. {
  45866. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  45867. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  45868. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  45869. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  45870. };
  45871. const byte Gx[] =
  45872. {
  45873. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  45874. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  45875. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  45876. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  45877. };
  45878. const byte Gy[] =
  45879. {
  45880. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  45881. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  45882. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  45883. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  45884. };
  45885. int cofactor = 1;
  45886. int fieldSize = 256;
  45887. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  45888. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  45889. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  45890. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  45891. ECC_SECP256R1);
  45892. #endif
  45893. /* invalid case, fieldSize = 0 */
  45894. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  45895. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45896. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  45897. /* invalid case, NULL prime */
  45898. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  45899. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45900. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  45901. /* invalid case, invalid prime */
  45902. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  45903. primeInvalid, sizeof(primeInvalid),
  45904. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  45905. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  45906. #endif
  45907. return EXPECT_RESULT();
  45908. }
  45909. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  45910. {
  45911. EXPECT_DECLS;
  45912. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  45913. WOLFSSL_RSA* rsa = NULL;
  45914. WOLFSSL_EVP_PKEY* pkey = NULL;
  45915. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  45916. const char* in = "What is easy to do is easy not to do.";
  45917. size_t inlen = XSTRLEN(in);
  45918. size_t outEncLen = 0;
  45919. byte* outEnc = NULL;
  45920. byte* outDec = NULL;
  45921. size_t outDecLen = 0;
  45922. size_t rsaKeySz = 2048/8; /* Bytes */
  45923. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45924. byte* inTmp = NULL;
  45925. byte* outEncTmp = NULL;
  45926. byte* outDecTmp = NULL;
  45927. #endif
  45928. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45929. DYNAMIC_TYPE_TMP_BUFFER));
  45930. if (outEnc != NULL) {
  45931. XMEMSET(outEnc, 0, rsaKeySz);
  45932. }
  45933. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45934. DYNAMIC_TYPE_TMP_BUFFER));
  45935. if (outDec != NULL) {
  45936. XMEMSET(outDec, 0, rsaKeySz);
  45937. }
  45938. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45939. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45940. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  45941. if (EXPECT_FAIL()) {
  45942. RSA_free(rsa);
  45943. }
  45944. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45945. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45946. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  45947. WOLFSSL_SUCCESS);
  45948. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  45949. since ctx uses it.*/
  45950. ExpectIntEQ(pkey->ref.count, 2);
  45951. EVP_PKEY_free(pkey);
  45952. ExpectIntEQ(pkey->ref.count, 1);
  45953. /* Encrypt data */
  45954. /* Check that we can get the required output buffer length by passing in a
  45955. * NULL output buffer. */
  45956. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  45957. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45958. ExpectIntEQ(rsaKeySz, outEncLen);
  45959. /* Now do the actual encryption. */
  45960. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  45961. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  45962. /* Decrypt data */
  45963. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45964. /* Check that we can get the required output buffer length by passing in a
  45965. * NULL output buffer. */
  45966. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  45967. WOLFSSL_SUCCESS);
  45968. ExpectIntEQ(rsaKeySz, outDecLen);
  45969. /* Now do the actual decryption. */
  45970. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  45971. WOLFSSL_SUCCESS);
  45972. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  45973. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  45974. /* The input length must be the same size as the RSA key.*/
  45975. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45976. DYNAMIC_TYPE_TMP_BUFFER));
  45977. if (inTmp != NULL) {
  45978. XMEMSET(inTmp, 9, rsaKeySz);
  45979. }
  45980. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45981. DYNAMIC_TYPE_TMP_BUFFER));
  45982. if (outEncTmp != NULL) {
  45983. XMEMSET(outEncTmp, 0, rsaKeySz);
  45984. }
  45985. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  45986. DYNAMIC_TYPE_TMP_BUFFER));
  45987. if (outDecTmp != NULL) {
  45988. XMEMSET(outDecTmp, 0, rsaKeySz);
  45989. }
  45990. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  45991. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  45992. WOLFSSL_SUCCESS);
  45993. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  45994. WOLFSSL_SUCCESS);
  45995. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  45996. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  45997. outEncLen), WOLFSSL_SUCCESS);
  45998. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  45999. #endif
  46000. EVP_PKEY_CTX_free(ctx);
  46001. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46002. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46003. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46004. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46005. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46006. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46007. #endif
  46008. #endif
  46009. return EXPECT_RESULT();
  46010. }
  46011. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46012. !defined(HAVE_SELFTEST)
  46013. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46014. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46015. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46016. #endif
  46017. #endif
  46018. #endif
  46019. #if defined(OPENSSL_EXTRA)
  46020. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46021. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46022. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46023. #endif
  46024. #endif
  46025. #endif
  46026. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46027. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46028. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46029. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46030. #endif
  46031. #endif
  46032. #endif
  46033. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46034. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  46035. {
  46036. EXPECT_DECLS;
  46037. #if defined(OPENSSL_EXTRA)
  46038. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46039. !defined(HAVE_SELFTEST)
  46040. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46041. WOLFSSL_RSA* rsa = NULL;
  46042. #endif
  46043. #endif
  46044. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46045. WOLFSSL_DSA* dsa = NULL;
  46046. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46047. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46048. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46049. WOLFSSL_EC_KEY* ecKey = NULL;
  46050. #endif
  46051. #endif
  46052. WOLFSSL_EVP_PKEY* pkey = NULL;
  46053. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46054. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  46055. const char* in = "What is easy to do is easy not to do.";
  46056. size_t inlen = XSTRLEN(in);
  46057. byte hash[SHA256_DIGEST_LENGTH] = {0};
  46058. byte zero[SHA256_DIGEST_LENGTH] = {0};
  46059. SHA256_CTX c;
  46060. byte* sig = NULL;
  46061. byte* sigVerify = NULL;
  46062. size_t siglen;
  46063. size_t siglenOnlyLen;
  46064. size_t keySz = 2048/8; /* Bytes */
  46065. ExpectNotNull(sig =
  46066. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46067. ExpectNotNull(sigVerify =
  46068. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46069. siglen = keySz;
  46070. ExpectNotNull(XMEMSET(sig, 0, keySz));
  46071. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  46072. /* Generate hash */
  46073. SHA256_Init(&c);
  46074. SHA256_Update(&c, in, inlen);
  46075. SHA256_Final(hash, &c);
  46076. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46077. /* workaround for small stack cache case */
  46078. wc_Sha256Free((wc_Sha256*)&c);
  46079. #endif
  46080. /* Generate key */
  46081. ExpectNotNull(pkey = EVP_PKEY_new());
  46082. switch (keyType) {
  46083. case EVP_PKEY_RSA:
  46084. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46085. !defined(HAVE_SELFTEST)
  46086. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46087. {
  46088. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46089. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46090. }
  46091. #endif
  46092. #endif
  46093. break;
  46094. case EVP_PKEY_DSA:
  46095. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46096. ExpectNotNull(dsa = DSA_new());
  46097. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  46098. NULL, 0, NULL, NULL, NULL), 1);
  46099. ExpectIntEQ(DSA_generate_key(dsa), 1);
  46100. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  46101. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46102. break;
  46103. case EVP_PKEY_EC:
  46104. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46105. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46106. {
  46107. ExpectNotNull(ecKey = EC_KEY_new());
  46108. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  46109. ExpectIntEQ(
  46110. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46111. if (EXPECT_FAIL()) {
  46112. EC_KEY_free(ecKey);
  46113. }
  46114. }
  46115. #endif
  46116. #endif
  46117. break;
  46118. }
  46119. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46120. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46121. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46122. !defined(HAVE_SELFTEST)
  46123. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46124. if (keyType == EVP_PKEY_RSA)
  46125. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46126. WOLFSSL_SUCCESS);
  46127. #endif
  46128. #endif
  46129. /* Check returning only length */
  46130. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  46131. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46132. ExpectIntGT(siglenOnlyLen, 0);
  46133. /* Sign data */
  46134. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  46135. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46136. ExpectIntGE(siglenOnlyLen, siglen);
  46137. /* Verify signature */
  46138. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  46139. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46140. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46141. !defined(HAVE_SELFTEST)
  46142. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46143. if (keyType == EVP_PKEY_RSA)
  46144. ExpectIntEQ(
  46145. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  46146. WOLFSSL_SUCCESS);
  46147. #endif
  46148. #endif
  46149. ExpectIntEQ(EVP_PKEY_verify(
  46150. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  46151. WOLFSSL_SUCCESS);
  46152. ExpectIntEQ(EVP_PKEY_verify(
  46153. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  46154. WOLFSSL_FAILURE);
  46155. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46156. !defined(HAVE_SELFTEST)
  46157. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46158. if (keyType == EVP_PKEY_RSA) {
  46159. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  46160. /* Try RSA sign/verify with no padding. */
  46161. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46162. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46163. WOLFSSL_SUCCESS);
  46164. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46165. siglen), WOLFSSL_SUCCESS);
  46166. ExpectIntGE(siglenOnlyLen, siglen);
  46167. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46168. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46169. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  46170. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46171. siglen), WOLFSSL_SUCCESS);
  46172. #endif
  46173. /* Wrong padding schemes. */
  46174. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46175. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  46176. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46177. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46178. siglen), WOLFSSL_SUCCESS);
  46179. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46180. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46181. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46182. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46183. siglen), WOLFSSL_SUCCESS);
  46184. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46185. WOLFSSL_SUCCESS);
  46186. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46187. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  46188. }
  46189. #endif
  46190. #endif
  46191. /* error cases */
  46192. siglen = keySz; /* Reset because sig size may vary slightly */
  46193. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  46194. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46195. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  46196. WOLFSSL_SUCCESS);
  46197. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  46198. WOLFSSL_SUCCESS);
  46199. EVP_PKEY_free(pkey);
  46200. pkey = NULL;
  46201. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46202. DSA_free(dsa);
  46203. dsa = NULL;
  46204. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46205. EVP_PKEY_CTX_free(ctx_verify);
  46206. ctx_verify = NULL;
  46207. EVP_PKEY_CTX_free(ctx);
  46208. ctx = NULL;
  46209. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46210. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46211. #endif /* OPENSSL_EXTRA */
  46212. return EXPECT_RESULT();
  46213. }
  46214. #endif
  46215. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  46216. {
  46217. EXPECT_DECLS;
  46218. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46219. !defined(HAVE_SELFTEST)
  46220. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46221. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  46222. #endif
  46223. #endif
  46224. return EXPECT_RESULT();
  46225. }
  46226. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  46227. {
  46228. EXPECT_DECLS;
  46229. #if defined(OPENSSL_EXTRA)
  46230. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46231. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  46232. #endif
  46233. #endif
  46234. return EXPECT_RESULT();
  46235. }
  46236. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  46237. {
  46238. EXPECT_DECLS;
  46239. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46240. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46241. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  46242. #endif
  46243. #endif
  46244. return EXPECT_RESULT();
  46245. }
  46246. static int test_EVP_PKEY_rsa(void)
  46247. {
  46248. EXPECT_DECLS;
  46249. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46250. WOLFSSL_RSA* rsa = NULL;
  46251. WOLFSSL_EVP_PKEY* pkey = NULL;
  46252. ExpectNotNull(rsa = wolfSSL_RSA_new());
  46253. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46254. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  46255. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  46256. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46257. if (EXPECT_FAIL()) {
  46258. wolfSSL_RSA_free(rsa);
  46259. }
  46260. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  46261. wolfSSL_EVP_PKEY_free(pkey);
  46262. #endif
  46263. return EXPECT_RESULT();
  46264. }
  46265. static int test_EVP_PKEY_ec(void)
  46266. {
  46267. EXPECT_DECLS;
  46268. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46269. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46270. WOLFSSL_EC_KEY* ecKey = NULL;
  46271. WOLFSSL_EVP_PKEY* pkey = NULL;
  46272. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  46273. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46274. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  46275. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  46276. /* Should fail since ecKey is empty */
  46277. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  46278. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  46279. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46280. if (EXPECT_FAIL()) {
  46281. wolfSSL_EC_KEY_free(ecKey);
  46282. }
  46283. wolfSSL_EVP_PKEY_free(pkey);
  46284. #endif
  46285. #endif
  46286. return EXPECT_RESULT();
  46287. }
  46288. static int test_EVP_PKEY_cmp(void)
  46289. {
  46290. EXPECT_DECLS;
  46291. #if defined(OPENSSL_EXTRA)
  46292. EVP_PKEY *a = NULL;
  46293. EVP_PKEY *b = NULL;
  46294. const unsigned char *in;
  46295. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  46296. in = client_key_der_2048;
  46297. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46298. &in, (long)sizeof_client_key_der_2048));
  46299. in = client_key_der_2048;
  46300. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46301. &in, (long)sizeof_client_key_der_2048));
  46302. /* Test success case RSA */
  46303. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46304. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  46305. #else
  46306. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46307. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46308. EVP_PKEY_free(b);
  46309. b = NULL;
  46310. EVP_PKEY_free(a);
  46311. a = NULL;
  46312. #endif
  46313. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46314. in = ecc_clikey_der_256;
  46315. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46316. &in, (long)sizeof_ecc_clikey_der_256));
  46317. in = ecc_clikey_der_256;
  46318. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46319. &in, (long)sizeof_ecc_clikey_der_256));
  46320. /* Test success case ECC */
  46321. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46322. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  46323. #else
  46324. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46325. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46326. EVP_PKEY_free(b);
  46327. b = NULL;
  46328. EVP_PKEY_free(a);
  46329. a = NULL;
  46330. #endif
  46331. /* Test failure cases */
  46332. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  46333. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46334. in = client_key_der_2048;
  46335. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46336. &in, (long)sizeof_client_key_der_2048));
  46337. in = ecc_clikey_der_256;
  46338. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46339. &in, (long)sizeof_ecc_clikey_der_256));
  46340. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46341. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  46342. #else
  46343. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  46344. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46345. EVP_PKEY_free(b);
  46346. b = NULL;
  46347. EVP_PKEY_free(a);
  46348. a = NULL;
  46349. #endif
  46350. /* invalid or empty failure cases */
  46351. a = EVP_PKEY_new();
  46352. b = EVP_PKEY_new();
  46353. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46354. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  46355. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  46356. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  46357. #ifdef NO_RSA
  46358. /* Type check will fail since RSA is the default EVP key type */
  46359. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  46360. #else
  46361. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46362. #endif
  46363. #else
  46364. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  46365. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  46366. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  46367. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  46368. #endif
  46369. EVP_PKEY_free(b);
  46370. EVP_PKEY_free(a);
  46371. (void)in;
  46372. #endif
  46373. return EXPECT_RESULT();
  46374. }
  46375. static int test_ERR_load_crypto_strings(void)
  46376. {
  46377. #if defined(OPENSSL_ALL)
  46378. ERR_load_crypto_strings();
  46379. return TEST_SUCCESS;
  46380. #else
  46381. return TEST_SKIPPED;
  46382. #endif
  46383. }
  46384. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  46385. static void free_x509(X509* x)
  46386. {
  46387. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  46388. }
  46389. #endif
  46390. static int test_sk_X509(void)
  46391. {
  46392. EXPECT_DECLS;
  46393. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  46394. {
  46395. STACK_OF(X509)* s = NULL;
  46396. ExpectNotNull(s = sk_X509_new_null());
  46397. ExpectIntEQ(sk_X509_num(s), 0);
  46398. sk_X509_pop_free(s, NULL);
  46399. ExpectNotNull(s = sk_X509_new_null());
  46400. ExpectIntEQ(sk_X509_num(s), 0);
  46401. sk_X509_pop_free(s, NULL);
  46402. ExpectNotNull(s = sk_X509_new_null());
  46403. sk_X509_push(s, (X509*)1);
  46404. ExpectIntEQ(sk_X509_num(s), 1);
  46405. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  46406. sk_X509_push(s, (X509*)2);
  46407. ExpectIntEQ(sk_X509_num(s), 2);
  46408. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  46409. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  46410. sk_X509_push(s, (X509*)2);
  46411. sk_X509_pop_free(s, free_x509);
  46412. }
  46413. {
  46414. /* Push a list of 10 X509s onto stack, then verify that
  46415. * value(), push(), shift(), and pop() behave as expected. */
  46416. STACK_OF(X509)* s = NULL;
  46417. X509* xList[10];
  46418. int i = 0;
  46419. const int len = (sizeof(xList) / sizeof(xList[0]));
  46420. for (i = 0; i < len; ++i) {
  46421. xList[i] = NULL;
  46422. ExpectNotNull(xList[i] = X509_new());
  46423. }
  46424. /* test push, pop, and free */
  46425. ExpectNotNull(s = sk_X509_new_null());
  46426. for (i = 0; i < len; ++i) {
  46427. sk_X509_push(s, xList[i]);
  46428. ExpectIntEQ(sk_X509_num(s), i + 1);
  46429. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  46430. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  46431. }
  46432. /* pop returns and removes last pushed on stack, which is index 0
  46433. * in sk_x509_value */
  46434. for (i = 0; i < len; ++i) {
  46435. X509 * x = sk_X509_value(s, 0);
  46436. X509 * y = sk_X509_pop(s);
  46437. X509 * z = xList[len - 1 - i];
  46438. ExpectIntEQ((x == y), 1);
  46439. ExpectIntEQ((x == z), 1);
  46440. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  46441. }
  46442. sk_free(s);
  46443. s = NULL;
  46444. /* test push, shift, and free */
  46445. ExpectNotNull(s = sk_X509_new_null());
  46446. for (i = 0; i < len; ++i) {
  46447. sk_X509_push(s, xList[i]);
  46448. ExpectIntEQ(sk_X509_num(s), i + 1);
  46449. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  46450. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  46451. }
  46452. /* shift returns and removes first pushed on stack, which is index i
  46453. * in sk_x509_value() */
  46454. for (i = 0; i < len; ++i) {
  46455. X509 * x = sk_X509_value(s, len - 1 - i);
  46456. X509 * y = sk_X509_shift(s);
  46457. X509 * z = xList[i];
  46458. ExpectIntEQ((x == y), 1);
  46459. ExpectIntEQ((x == z), 1);
  46460. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  46461. }
  46462. sk_free(s);
  46463. for (i = 0; i < len; ++i)
  46464. X509_free(xList[i]);
  46465. }
  46466. #endif
  46467. return EXPECT_RESULT();
  46468. }
  46469. static int test_sk_X509_CRL(void)
  46470. {
  46471. EXPECT_DECLS;
  46472. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  46473. X509_CRL* crl = NULL;
  46474. XFILE fp = XBADFILE;
  46475. STACK_OF(X509_CRL)* s = NULL;
  46476. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  46477. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46478. NULL, NULL));
  46479. if (fp != XBADFILE)
  46480. XFCLOSE(fp);
  46481. ExpectNotNull(s = sk_X509_CRL_new());
  46482. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  46483. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  46484. if (EXPECT_FAIL()) {
  46485. X509_CRL_free(crl);
  46486. }
  46487. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  46488. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  46489. sk_X509_CRL_free(s);
  46490. #endif
  46491. return EXPECT_RESULT();
  46492. }
  46493. static int test_X509_get_signature_nid(void)
  46494. {
  46495. EXPECT_DECLS;
  46496. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46497. X509* x509 = NULL;
  46498. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  46499. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  46500. SSL_FILETYPE_PEM));
  46501. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  46502. X509_free(x509);
  46503. #endif
  46504. return EXPECT_RESULT();
  46505. }
  46506. static int test_X509_REQ(void)
  46507. {
  46508. EXPECT_DECLS;
  46509. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  46510. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  46511. X509_NAME* name = NULL;
  46512. #ifndef NO_RSA
  46513. X509_NAME* subject = NULL;
  46514. #endif
  46515. #if !defined(NO_RSA) || defined(HAVE_ECC)
  46516. X509_REQ* req = NULL;
  46517. EVP_PKEY* priv = NULL;
  46518. EVP_PKEY* pub = NULL;
  46519. unsigned char* der = NULL;
  46520. int len;
  46521. #endif
  46522. #ifndef NO_RSA
  46523. EVP_MD_CTX *mctx = NULL;
  46524. EVP_PKEY_CTX *pkctx = NULL;
  46525. #ifdef USE_CERT_BUFFERS_1024
  46526. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  46527. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  46528. #elif defined(USE_CERT_BUFFERS_2048)
  46529. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  46530. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  46531. #endif
  46532. #endif
  46533. #ifdef HAVE_ECC
  46534. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  46535. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  46536. #endif
  46537. ExpectNotNull(name = X509_NAME_new());
  46538. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  46539. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  46540. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  46541. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  46542. #ifndef NO_RSA
  46543. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  46544. (long)sizeof_client_key_der_2048));
  46545. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  46546. (long)sizeof_client_keypub_der_2048));
  46547. ExpectNotNull(req = X509_REQ_new());
  46548. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  46549. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  46550. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  46551. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  46552. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  46553. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46554. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  46555. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  46556. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  46557. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46558. len = i2d_X509_REQ(req, &der);
  46559. DEBUG_WRITE_DER(der, len, "req.der");
  46560. #ifdef USE_CERT_BUFFERS_1024
  46561. ExpectIntEQ(len, 381);
  46562. #else
  46563. ExpectIntEQ(len, 643);
  46564. #endif
  46565. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46566. der = NULL;
  46567. mctx = EVP_MD_CTX_new();
  46568. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  46569. WOLFSSL_SUCCESS);
  46570. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  46571. EVP_MD_CTX_free(mctx);
  46572. mctx = NULL;
  46573. X509_REQ_free(NULL);
  46574. X509_REQ_free(req);
  46575. req = NULL;
  46576. /* Test getting the subject from a newly created X509_REQ */
  46577. ExpectNotNull(req = X509_REQ_new());
  46578. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  46579. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  46580. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  46581. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  46582. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  46583. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  46584. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  46585. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  46586. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  46587. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  46588. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  46589. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  46590. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  46591. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46592. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46593. len = i2d_X509_REQ(req, &der);
  46594. DEBUG_WRITE_DER(der, len, "req2.der");
  46595. #ifdef USE_CERT_BUFFERS_1024
  46596. ExpectIntEQ(len, 435);
  46597. #else
  46598. ExpectIntEQ(len, 696);
  46599. #endif
  46600. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46601. der = NULL;
  46602. EVP_PKEY_free(pub);
  46603. pub = NULL;
  46604. EVP_PKEY_free(priv);
  46605. priv = NULL;
  46606. X509_REQ_free(req);
  46607. req = NULL;
  46608. #endif
  46609. #ifdef HAVE_ECC
  46610. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  46611. sizeof_ecc_clikey_der_256));
  46612. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  46613. sizeof_ecc_clikeypub_der_256));
  46614. ExpectNotNull(req = X509_REQ_new());
  46615. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  46616. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  46617. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  46618. /* Signature is random and may be shorter or longer. */
  46619. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  46620. ExpectIntLE(len, 253);
  46621. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  46622. X509_REQ_free(req);
  46623. EVP_PKEY_free(pub);
  46624. EVP_PKEY_free(priv);
  46625. #ifdef FP_ECC
  46626. wc_ecc_fp_free();
  46627. #endif
  46628. #endif /* HAVE_ECC */
  46629. X509_NAME_free(name);
  46630. #endif
  46631. return EXPECT_RESULT();
  46632. }
  46633. static int test_wolfssl_PKCS7(void)
  46634. {
  46635. EXPECT_DECLS;
  46636. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  46637. !defined(NO_RSA)
  46638. PKCS7* pkcs7 = NULL;
  46639. byte data[FOURK_BUF];
  46640. word32 len = sizeof(data);
  46641. const byte* p = data;
  46642. byte content[] = "Test data to encode.";
  46643. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  46644. BIO* bio = NULL;
  46645. byte key[sizeof(client_key_der_2048)];
  46646. word32 keySz = (word32)sizeof(key);
  46647. byte* out = NULL;
  46648. #endif
  46649. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  46650. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  46651. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  46652. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  46653. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46654. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  46655. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  46656. PKCS7_free(pkcs7);
  46657. pkcs7 = NULL;
  46658. /* fail case, without PKCS7_NOVERIFY */
  46659. p = data;
  46660. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46661. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  46662. 0), WOLFSSL_FAILURE);
  46663. PKCS7_free(pkcs7);
  46664. pkcs7 = NULL;
  46665. /* success case, with PKCS7_NOVERIFY */
  46666. p = data;
  46667. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  46668. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  46669. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  46670. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  46671. /* test i2d */
  46672. XMEMCPY(key, client_key_der_2048, keySz);
  46673. if (pkcs7 != NULL) {
  46674. pkcs7->privateKey = key;
  46675. pkcs7->privateKeySz = (word32)sizeof(key);
  46676. pkcs7->encryptOID = RSAk;
  46677. #ifdef NO_SHA
  46678. pkcs7->hashOID = SHA256h;
  46679. #else
  46680. pkcs7->hashOID = SHAh;
  46681. #endif
  46682. }
  46683. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46684. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  46685. #ifndef NO_ASN_TIME
  46686. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  46687. #else
  46688. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  46689. #endif
  46690. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46691. BIO_free(bio);
  46692. #endif
  46693. PKCS7_free(NULL);
  46694. PKCS7_free(pkcs7);
  46695. #endif
  46696. return EXPECT_RESULT();
  46697. }
  46698. static int test_wolfSSL_PKCS7_sign(void)
  46699. {
  46700. EXPECT_DECLS;
  46701. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  46702. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46703. PKCS7* p7 = NULL;
  46704. PKCS7* p7Ver = NULL;
  46705. byte* out = NULL;
  46706. byte* tmpPtr = NULL;
  46707. int outLen = 0;
  46708. int flags = 0;
  46709. byte data[] = "Test data to encode.";
  46710. const char* cert = "./certs/server-cert.pem";
  46711. const char* key = "./certs/server-key.pem";
  46712. const char* ca = "./certs/ca-cert.pem";
  46713. WOLFSSL_BIO* certBio = NULL;
  46714. WOLFSSL_BIO* keyBio = NULL;
  46715. WOLFSSL_BIO* caBio = NULL;
  46716. WOLFSSL_BIO* inBio = NULL;
  46717. X509* signCert = NULL;
  46718. EVP_PKEY* signKey = NULL;
  46719. X509* caCert = NULL;
  46720. X509_STORE* store = NULL;
  46721. #ifndef NO_PKCS7_STREAM
  46722. int z;
  46723. int ret;
  46724. #endif /* !NO_PKCS7_STREAM */
  46725. /* read signer cert/key into BIO */
  46726. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  46727. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  46728. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  46729. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  46730. /* read CA cert into store (for verify) */
  46731. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  46732. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  46733. ExpectNotNull(store = X509_STORE_new());
  46734. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  46735. /* data to be signed into BIO */
  46736. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46737. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46738. /* PKCS7_sign, bad args: signer NULL */
  46739. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  46740. /* PKCS7_sign, bad args: signer key NULL */
  46741. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  46742. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  46743. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  46744. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  46745. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  46746. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  46747. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  46748. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  46749. {
  46750. flags = PKCS7_BINARY;
  46751. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46752. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46753. /* verify with d2i_PKCS7 */
  46754. tmpPtr = out;
  46755. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46756. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46757. PKCS7_free(p7Ver);
  46758. p7Ver = NULL;
  46759. /* verify with wc_PKCS7_VerifySignedData */
  46760. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46761. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  46762. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46763. #ifndef NO_PKCS7_STREAM
  46764. /* verify with wc_PKCS7_VerifySignedData streaming */
  46765. wc_PKCS7_Free(p7Ver);
  46766. p7Ver = NULL;
  46767. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46768. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  46769. /* test for streaming */
  46770. ret = -1;
  46771. for (z = 0; z < outLen && ret != 0; z++) {
  46772. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  46773. if (ret < 0){
  46774. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  46775. }
  46776. }
  46777. ExpectIntEQ(ret, 0);
  46778. #endif /* !NO_PKCS7_STREAM */
  46779. /* compare the signer found to expected signer */
  46780. ExpectIntNE(p7Ver->verifyCertSz, 0);
  46781. tmpPtr = NULL;
  46782. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  46783. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  46784. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  46785. tmpPtr = NULL;
  46786. wc_PKCS7_Free(p7Ver);
  46787. p7Ver = NULL;
  46788. ExpectNotNull(out);
  46789. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46790. out = NULL;
  46791. PKCS7_free(p7);
  46792. p7 = NULL;
  46793. }
  46794. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  46795. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  46796. {
  46797. /* re-populate input BIO, may have been consumed */
  46798. BIO_free(inBio);
  46799. inBio = NULL;
  46800. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46801. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46802. flags = PKCS7_BINARY | PKCS7_STREAM;
  46803. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46804. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  46805. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46806. /* PKCS7_final, bad args: PKCS7 null */
  46807. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  46808. /* PKCS7_final, bad args: PKCS7 null */
  46809. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  46810. tmpPtr = out;
  46811. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46812. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  46813. PKCS7_free(p7Ver);
  46814. p7Ver = NULL;
  46815. ExpectNotNull(out);
  46816. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46817. out = NULL;
  46818. PKCS7_free(p7);
  46819. p7 = NULL;
  46820. }
  46821. /* TEST SUCCESS: Detached, not streaming, not MIME */
  46822. {
  46823. /* re-populate input BIO, may have been consumed */
  46824. BIO_free(inBio);
  46825. inBio = NULL;
  46826. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46827. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46828. flags = PKCS7_BINARY | PKCS7_DETACHED;
  46829. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46830. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46831. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  46832. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46833. if (p7Ver != NULL) {
  46834. p7Ver->content = data;
  46835. p7Ver->contentSz = sizeof(data);
  46836. }
  46837. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46838. wc_PKCS7_Free(p7Ver);
  46839. p7Ver = NULL;
  46840. #ifndef NO_PKCS7_STREAM
  46841. /* verify with wc_PKCS7_VerifySignedData streaming */
  46842. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46843. if (p7Ver != NULL) {
  46844. p7Ver->content = data;
  46845. p7Ver->contentSz = sizeof(data);
  46846. }
  46847. /* test for streaming */
  46848. ret = -1;
  46849. for (z = 0; z < outLen && ret != 0; z++) {
  46850. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  46851. if (ret < 0){
  46852. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  46853. }
  46854. }
  46855. ExpectIntEQ(ret, 0);
  46856. wc_PKCS7_Free(p7Ver);
  46857. p7Ver = NULL;
  46858. #endif /* !NO_PKCS7_STREAM */
  46859. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  46860. * yet support detached content */
  46861. tmpPtr = out;
  46862. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  46863. PKCS7_free(p7Ver);
  46864. p7Ver = NULL;
  46865. ExpectNotNull(out);
  46866. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46867. out = NULL;
  46868. PKCS7_free(p7);
  46869. p7 = NULL;
  46870. }
  46871. /* TEST SUCCESS: Detached, streaming, not MIME */
  46872. {
  46873. /* re-populate input BIO, may have been consumed */
  46874. BIO_free(inBio);
  46875. inBio = NULL;
  46876. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  46877. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  46878. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  46879. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  46880. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  46881. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  46882. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  46883. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46884. if (p7Ver != NULL) {
  46885. p7Ver->content = data;
  46886. p7Ver->contentSz = sizeof(data);
  46887. }
  46888. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  46889. wc_PKCS7_Free(p7Ver);
  46890. p7Ver = NULL;
  46891. ExpectNotNull(out);
  46892. #ifndef NO_PKCS7_STREAM
  46893. /* verify with wc_PKCS7_VerifySignedData streaming */
  46894. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  46895. if (p7Ver != NULL) {
  46896. p7Ver->content = data;
  46897. p7Ver->contentSz = sizeof(data);
  46898. }
  46899. /* test for streaming */
  46900. ret = -1;
  46901. for (z = 0; z < outLen && ret != 0; z++) {
  46902. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  46903. if (ret < 0){
  46904. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  46905. }
  46906. }
  46907. ExpectIntEQ(ret, 0);
  46908. ExpectNotNull(out);
  46909. wc_PKCS7_Free(p7Ver);
  46910. p7Ver = NULL;
  46911. #endif /* !NO_PKCS7_STREAM */
  46912. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46913. PKCS7_free(p7);
  46914. p7 = NULL;
  46915. }
  46916. X509_STORE_free(store);
  46917. X509_free(caCert);
  46918. X509_free(signCert);
  46919. EVP_PKEY_free(signKey);
  46920. BIO_free(inBio);
  46921. BIO_free(keyBio);
  46922. BIO_free(certBio);
  46923. BIO_free(caBio);
  46924. #endif
  46925. return EXPECT_RESULT();
  46926. }
  46927. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  46928. {
  46929. EXPECT_DECLS;
  46930. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  46931. PKCS7_SIGNED* pkcs7 = NULL;
  46932. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  46933. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  46934. PKCS7_SIGNED_free(pkcs7);
  46935. #endif
  46936. return EXPECT_RESULT();
  46937. }
  46938. #ifndef NO_BIO
  46939. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  46940. {
  46941. EXPECT_DECLS;
  46942. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  46943. PKCS7* pkcs7 = NULL;
  46944. BIO* bio = NULL;
  46945. const byte* cert_buf = NULL;
  46946. int ret = 0;
  46947. WC_RNG rng;
  46948. const byte data[] = { /* Hello World */
  46949. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  46950. 0x72,0x6c,0x64
  46951. };
  46952. #ifndef NO_RSA
  46953. #if defined(USE_CERT_BUFFERS_2048)
  46954. byte key[sizeof(client_key_der_2048)];
  46955. byte cert[sizeof(client_cert_der_2048)];
  46956. word32 keySz = (word32)sizeof(key);
  46957. word32 certSz = (word32)sizeof(cert);
  46958. XMEMSET(key, 0, keySz);
  46959. XMEMSET(cert, 0, certSz);
  46960. XMEMCPY(key, client_key_der_2048, keySz);
  46961. XMEMCPY(cert, client_cert_der_2048, certSz);
  46962. #elif defined(USE_CERT_BUFFERS_1024)
  46963. byte key[sizeof_client_key_der_1024];
  46964. byte cert[sizeof(sizeof_client_cert_der_1024)];
  46965. word32 keySz = (word32)sizeof(key);
  46966. word32 certSz = (word32)sizeof(cert);
  46967. XMEMSET(key, 0, keySz);
  46968. XMEMSET(cert, 0, certSz);
  46969. XMEMCPY(key, client_key_der_1024, keySz);
  46970. XMEMCPY(cert, client_cert_der_1024, certSz);
  46971. #else
  46972. unsigned char cert[ONEK_BUF];
  46973. unsigned char key[ONEK_BUF];
  46974. XFILE fp = XBADFILE;
  46975. int certSz;
  46976. int keySz;
  46977. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  46978. XBADFILE);
  46979. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  46980. fp), 0);
  46981. if (fp != XBADFILE) {
  46982. XFCLOSE(fp);
  46983. fp = XBADFILE;
  46984. }
  46985. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  46986. XBADFILE);
  46987. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  46988. 0);
  46989. if (fp != XBADFILE) {
  46990. XFCLOSE(fp);
  46991. fp = XBADFILE;
  46992. }
  46993. #endif
  46994. #elif defined(HAVE_ECC)
  46995. #if defined(USE_CERT_BUFFERS_256)
  46996. unsigned char cert[sizeof(cliecc_cert_der_256)];
  46997. unsigned char key[sizeof(ecc_clikey_der_256)];
  46998. int certSz = (int)sizeof(cert);
  46999. int keySz = (int)sizeof(key);
  47000. XMEMSET(cert, 0, certSz);
  47001. XMEMSET(key, 0, keySz);
  47002. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  47003. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  47004. #else
  47005. unsigned char cert[ONEK_BUF];
  47006. unsigned char key[ONEK_BUF];
  47007. XFILE fp = XBADFILE;
  47008. int certSz, keySz;
  47009. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  47010. XBADFILE);
  47011. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  47012. fp), 0);
  47013. if (fp != XBADFILE) {
  47014. XFCLOSE(fp);
  47015. fp = XBADFILE;
  47016. }
  47017. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  47018. XBADFILE);
  47019. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  47020. 0);
  47021. if (fp != XBADFILE) {
  47022. XFCLOSE(fp);
  47023. fp = XBADFILE;
  47024. }
  47025. #endif
  47026. #else
  47027. #error PKCS7 requires ECC or RSA
  47028. #endif
  47029. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  47030. /* initialize with DER encoded cert */
  47031. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  47032. /* init rng */
  47033. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47034. ExpectIntEQ(wc_InitRng(&rng), 0);
  47035. if (pkcs7 != NULL) {
  47036. pkcs7->rng = &rng;
  47037. pkcs7->content = (byte*)data; /* not used for ex */
  47038. pkcs7->contentSz = (word32)sizeof(data);
  47039. pkcs7->contentOID = SIGNED_DATA;
  47040. pkcs7->privateKey = key;
  47041. pkcs7->privateKeySz = (word32)sizeof(key);
  47042. pkcs7->encryptOID = RSAk;
  47043. #ifdef NO_SHA
  47044. pkcs7->hashOID = SHA256h;
  47045. #else
  47046. pkcs7->hashOID = SHAh;
  47047. #endif
  47048. pkcs7->signedAttribs = NULL;
  47049. pkcs7->signedAttribsSz = 0;
  47050. }
  47051. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47052. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  47053. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  47054. /* Read PKCS#7 PEM from BIO */
  47055. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  47056. ExpectIntGE(ret, 0);
  47057. BIO_free(bio);
  47058. wc_PKCS7_Free(pkcs7);
  47059. wc_FreeRng(&rng);
  47060. #endif
  47061. return EXPECT_RESULT();
  47062. }
  47063. #ifdef HAVE_SMIME
  47064. static int test_wolfSSL_SMIME_read_PKCS7(void)
  47065. {
  47066. EXPECT_DECLS;
  47067. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  47068. !defined(NO_RSA)
  47069. PKCS7* pkcs7 = NULL;
  47070. BIO* bio = NULL;
  47071. BIO* bcont = NULL;
  47072. BIO* out = NULL;
  47073. const byte* outBuf = NULL;
  47074. int outBufLen = 0;
  47075. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  47076. XFILE smimeTestFile = XBADFILE;
  47077. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  47078. XBADFILE);
  47079. /* smime-test.p7s */
  47080. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  47081. ExpectNotNull(bio);
  47082. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47083. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47084. ExpectNotNull(pkcs7);
  47085. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47086. PKCS7_NOVERIFY), SSL_SUCCESS);
  47087. XFCLOSE(smimeTestFile);
  47088. if (bcont) BIO_free(bcont);
  47089. bcont = NULL;
  47090. wolfSSL_PKCS7_free(pkcs7);
  47091. pkcs7 = NULL;
  47092. /* smime-test-multipart.p7s */
  47093. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  47094. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47095. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47096. ExpectNotNull(pkcs7);
  47097. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47098. PKCS7_NOVERIFY), SSL_SUCCESS);
  47099. XFCLOSE(smimeTestFile);
  47100. if (bcont) BIO_free(bcont);
  47101. bcont = NULL;
  47102. wolfSSL_PKCS7_free(pkcs7);
  47103. pkcs7 = NULL;
  47104. /* smime-test-multipart-badsig.p7s */
  47105. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  47106. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47107. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47108. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  47109. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47110. PKCS7_NOVERIFY), SSL_FAILURE);
  47111. XFCLOSE(smimeTestFile);
  47112. if (bcont) BIO_free(bcont);
  47113. bcont = NULL;
  47114. wolfSSL_PKCS7_free(pkcs7);
  47115. pkcs7 = NULL;
  47116. /* smime-test-canon.p7s */
  47117. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47118. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47119. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47120. ExpectNotNull(pkcs7);
  47121. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47122. PKCS7_NOVERIFY), SSL_SUCCESS);
  47123. XFCLOSE(smimeTestFile);
  47124. if (bcont) BIO_free(bcont);
  47125. bcont = NULL;
  47126. wolfSSL_PKCS7_free(pkcs7);
  47127. pkcs7 = NULL;
  47128. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  47129. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47130. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47131. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47132. ExpectNotNull(pkcs7);
  47133. out = wolfSSL_BIO_new(BIO_s_mem());
  47134. ExpectNotNull(out);
  47135. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  47136. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  47137. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  47138. /* Content-Type should not show up at beginning of output buffer */
  47139. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  47140. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  47141. BIO_free(out);
  47142. BIO_free(bio);
  47143. if (bcont) BIO_free(bcont);
  47144. wolfSSL_PKCS7_free(pkcs7);
  47145. #endif
  47146. return EXPECT_RESULT();
  47147. }
  47148. static int test_wolfSSL_SMIME_write_PKCS7(void)
  47149. {
  47150. EXPECT_DECLS;
  47151. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  47152. PKCS7* p7 = NULL;
  47153. PKCS7* p7Ver = NULL;
  47154. int flags = 0;
  47155. byte data[] = "Test data to encode.";
  47156. const char* cert = "./certs/server-cert.pem";
  47157. const char* key = "./certs/server-key.pem";
  47158. const char* ca = "./certs/ca-cert.pem";
  47159. WOLFSSL_BIO* certBio = NULL;
  47160. WOLFSSL_BIO* keyBio = NULL;
  47161. WOLFSSL_BIO* caBio = NULL;
  47162. WOLFSSL_BIO* inBio = NULL;
  47163. WOLFSSL_BIO* outBio = NULL;
  47164. WOLFSSL_BIO* content = NULL;
  47165. X509* signCert = NULL;
  47166. EVP_PKEY* signKey = NULL;
  47167. X509* caCert = NULL;
  47168. X509_STORE* store = NULL;
  47169. /* read signer cert/key into BIO */
  47170. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47171. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47172. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47173. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47174. /* read CA cert into store (for verify) */
  47175. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47176. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47177. ExpectNotNull(store = X509_STORE_new());
  47178. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47179. /* generate and verify SMIME: not detached */
  47180. {
  47181. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47182. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47183. flags = PKCS7_STREAM;
  47184. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47185. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47186. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47187. /* bad arg: out NULL */
  47188. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  47189. /* bad arg: pkcs7 NULL */
  47190. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  47191. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47192. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47193. BIO_free(content);
  47194. content = NULL;
  47195. BIO_free(inBio);
  47196. inBio = NULL;
  47197. BIO_free(outBio);
  47198. outBio = NULL;
  47199. PKCS7_free(p7Ver);
  47200. p7Ver = NULL;
  47201. PKCS7_free(p7);
  47202. p7 = NULL;
  47203. }
  47204. /* generate and verify SMIME: not detached, add Content-Type */
  47205. {
  47206. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47207. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47208. flags = PKCS7_STREAM | PKCS7_TEXT;
  47209. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47210. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47211. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47212. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47213. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47214. BIO_free(content);
  47215. content = NULL;
  47216. BIO_free(inBio);
  47217. inBio = NULL;
  47218. BIO_free(outBio);
  47219. outBio = NULL;
  47220. PKCS7_free(p7Ver);
  47221. p7Ver = NULL;
  47222. PKCS7_free(p7);
  47223. p7 = NULL;
  47224. }
  47225. /* generate and verify SMIME: detached */
  47226. {
  47227. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47228. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47229. flags = PKCS7_DETACHED | PKCS7_STREAM;
  47230. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47231. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47232. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47233. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47234. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47235. BIO_free(content);
  47236. content = NULL;
  47237. BIO_free(inBio);
  47238. inBio = NULL;
  47239. BIO_free(outBio);
  47240. outBio = NULL;
  47241. PKCS7_free(p7Ver);
  47242. p7Ver = NULL;
  47243. PKCS7_free(p7);
  47244. p7 = NULL;
  47245. }
  47246. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  47247. {
  47248. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47249. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47250. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  47251. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47252. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47253. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47254. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47255. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47256. BIO_free(content);
  47257. content = NULL;
  47258. BIO_free(inBio);
  47259. inBio = NULL;
  47260. BIO_free(outBio);
  47261. outBio = NULL;
  47262. PKCS7_free(p7Ver);
  47263. p7Ver = NULL;
  47264. PKCS7_free(p7);
  47265. p7 = NULL;
  47266. }
  47267. X509_STORE_free(store);
  47268. X509_free(caCert);
  47269. X509_free(signCert);
  47270. EVP_PKEY_free(signKey);
  47271. BIO_free(keyBio);
  47272. BIO_free(certBio);
  47273. BIO_free(caBio);
  47274. #endif
  47275. return EXPECT_RESULT();
  47276. }
  47277. #endif /* HAVE_SMIME */
  47278. #endif /* !NO_BIO */
  47279. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  47280. * returns 0) */
  47281. static int test_X509_STORE_No_SSL_CTX(void)
  47282. {
  47283. EXPECT_DECLS;
  47284. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47285. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47286. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47287. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47288. defined(HAVE_CRL) && !defined(NO_RSA)
  47289. X509_STORE * store = NULL;
  47290. X509_STORE_CTX * storeCtx = NULL;
  47291. X509_CRL * crl = NULL;
  47292. X509 * ca = NULL;
  47293. X509 * cert = NULL;
  47294. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47295. const char srvCert[] = "./certs/server-cert.pem";
  47296. const char caCert[] = "./certs/ca-cert.pem";
  47297. const char caDir[] = "./certs/crl/hash_pem";
  47298. XFILE fp = XBADFILE;
  47299. X509_LOOKUP * lookup = NULL;
  47300. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47301. /* Set up store with CA */
  47302. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47303. SSL_FILETYPE_PEM)));
  47304. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47305. /* Add CRL lookup directory to store
  47306. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  47307. * of crl.pem */
  47308. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47309. X509_LOOKUP_hash_dir())));
  47310. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  47311. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  47312. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47313. SSL_SUCCESS);
  47314. /* Add CRL to store NOT containing the verified certificate, which
  47315. * forces use of the CRL lookup directory */
  47316. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  47317. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47318. NULL, NULL));
  47319. if (fp != XBADFILE)
  47320. XFCLOSE(fp);
  47321. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47322. /* Create verification context outside of an SSL session */
  47323. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  47324. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47325. SSL_FILETYPE_PEM)));
  47326. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47327. /* Perform verification, which should NOT indicate CRL missing due to the
  47328. * store CM's X509 store pointer being NULL */
  47329. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  47330. X509_CRL_free(crl);
  47331. X509_STORE_free(store);
  47332. X509_STORE_CTX_free(storeCtx);
  47333. X509_free(cert);
  47334. X509_free(ca);
  47335. #endif
  47336. return EXPECT_RESULT();
  47337. }
  47338. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  47339. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  47340. static int test_X509_LOOKUP_add_dir(void)
  47341. {
  47342. EXPECT_DECLS;
  47343. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47344. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47345. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47346. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47347. defined(HAVE_CRL) && !defined(NO_RSA)
  47348. X509_STORE * store = NULL;
  47349. X509_STORE_CTX * storeCtx = NULL;
  47350. X509_CRL * crl = NULL;
  47351. X509 * ca = NULL;
  47352. X509 * cert = NULL;
  47353. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47354. const char srvCert[] = "./certs/server-cert.pem";
  47355. const char caCert[] = "./certs/ca-cert.pem";
  47356. const char caDir[] = "./certs/crl/hash_der";
  47357. XFILE fp = XBADFILE;
  47358. X509_LOOKUP * lookup = NULL;
  47359. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47360. /* Set up store with CA */
  47361. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47362. SSL_FILETYPE_PEM)));
  47363. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47364. /* Add CRL lookup directory to store.
  47365. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  47366. * of crl.der */
  47367. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47368. X509_LOOKUP_hash_dir())));
  47369. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  47370. SSL_SUCCESS);
  47371. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47372. SSL_SUCCESS);
  47373. /* Add CRL to store NOT containing the verified certificate, which
  47374. * forces use of the CRL lookup directory */
  47375. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  47376. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47377. NULL, NULL));
  47378. if (fp != XBADFILE) {
  47379. XFCLOSE(fp);
  47380. fp = XBADFILE;
  47381. }
  47382. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47383. /* Create verification context outside of an SSL session */
  47384. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  47385. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47386. SSL_FILETYPE_PEM)));
  47387. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47388. /* Perform verification, which should NOT return CRL missing */
  47389. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  47390. X509_CRL_free(crl);
  47391. crl = NULL;
  47392. X509_STORE_free(store);
  47393. store = NULL;
  47394. X509_STORE_CTX_free(storeCtx);
  47395. storeCtx = NULL;
  47396. X509_free(cert);
  47397. cert = NULL;
  47398. X509_free(ca);
  47399. ca = NULL;
  47400. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  47401. * We should get CRL_MISSING at the end, because the lookup
  47402. * dir has only ASN1 CRLs. */
  47403. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47404. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47405. SSL_FILETYPE_PEM)));
  47406. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47407. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47408. X509_LOOKUP_hash_dir())));
  47409. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  47410. SSL_SUCCESS);
  47411. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47412. SSL_SUCCESS);
  47413. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  47414. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47415. NULL, NULL));
  47416. if (fp != XBADFILE) {
  47417. XFCLOSE(fp);
  47418. fp = XBADFILE;
  47419. }
  47420. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47421. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  47422. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47423. SSL_FILETYPE_PEM)));
  47424. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47425. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  47426. * in dir containing only ASN1/DER. */
  47427. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  47428. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  47429. X509_CRL_free(crl);
  47430. X509_STORE_free(store);
  47431. X509_STORE_CTX_free(storeCtx);
  47432. X509_free(cert);
  47433. X509_free(ca);
  47434. #endif
  47435. return EXPECT_RESULT();
  47436. }
  47437. /*----------------------------------------------------------------------------*
  47438. | Certificate Failure Checks
  47439. *----------------------------------------------------------------------------*/
  47440. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  47441. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  47442. #if !defined(NO_RSA) || defined(HAVE_ECC)
  47443. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  47444. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  47445. int type)
  47446. {
  47447. int ret;
  47448. WOLFSSL_CERT_MANAGER* cm = NULL;
  47449. switch (type) {
  47450. case TESTING_RSA:
  47451. #ifdef NO_RSA
  47452. fprintf(stderr, "RSA disabled, skipping test\n");
  47453. return ASN_SIG_CONFIRM_E;
  47454. #else
  47455. break;
  47456. #endif
  47457. case TESTING_ECC:
  47458. #ifndef HAVE_ECC
  47459. fprintf(stderr, "ECC disabled, skipping test\n");
  47460. return ASN_SIG_CONFIRM_E;
  47461. #else
  47462. break;
  47463. #endif
  47464. default:
  47465. fprintf(stderr, "Bad function argument\n");
  47466. return BAD_FUNC_ARG;
  47467. }
  47468. cm = wolfSSL_CertManagerNew();
  47469. if (cm == NULL) {
  47470. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  47471. return -1;
  47472. }
  47473. #ifndef NO_FILESYSTEM
  47474. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  47475. if (ret != WOLFSSL_SUCCESS) {
  47476. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  47477. wolfSSL_CertManagerFree(cm);
  47478. return ret;
  47479. }
  47480. #else
  47481. (void)ca;
  47482. #endif
  47483. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  47484. WOLFSSL_FILETYPE_ASN1);
  47485. /* Let ExpectIntEQ handle return code */
  47486. wolfSSL_CertManagerFree(cm);
  47487. return ret;
  47488. }
  47489. #endif
  47490. #if !defined(NO_FILESYSTEM)
  47491. static int test_RsaSigFailure_cm(void)
  47492. {
  47493. EXPECT_DECLS;
  47494. #ifndef NO_RSA
  47495. const char* ca_cert = "./certs/ca-cert.pem";
  47496. const char* server_cert = "./certs/server-cert.der";
  47497. byte* cert_buf = NULL;
  47498. size_t cert_sz = 0;
  47499. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  47500. if (cert_buf != NULL) {
  47501. /* corrupt DER - invert last byte, which is signature */
  47502. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  47503. /* test bad cert */
  47504. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  47505. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  47506. WOLFSSL_FATAL_ERROR);
  47507. #else
  47508. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  47509. ASN_SIG_CONFIRM_E);
  47510. #endif
  47511. }
  47512. /* load_file() uses malloc. */
  47513. if (cert_buf != NULL) {
  47514. free(cert_buf);
  47515. }
  47516. #endif /* !NO_RSA */
  47517. return EXPECT_RESULT();
  47518. }
  47519. static int test_EccSigFailure_cm(void)
  47520. {
  47521. EXPECT_DECLS;
  47522. #ifdef HAVE_ECC
  47523. /* self-signed ECC cert, so use server cert as CA */
  47524. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  47525. const char* server_cert = "./certs/server-ecc.der";
  47526. byte* cert_buf = NULL;
  47527. size_t cert_sz = 0;
  47528. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  47529. if (cert_buf != NULL) {
  47530. /* corrupt DER - invert last byte, which is signature */
  47531. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  47532. /* test bad cert */
  47533. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  47534. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  47535. WOLFSSL_FATAL_ERROR);
  47536. #else
  47537. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  47538. ASN_SIG_CONFIRM_E);
  47539. #endif
  47540. }
  47541. /* load_file() uses malloc. */
  47542. if (cert_buf != NULL) {
  47543. free(cert_buf);
  47544. }
  47545. #ifdef FP_ECC
  47546. wc_ecc_fp_free();
  47547. #endif
  47548. #endif /* HAVE_ECC */
  47549. return EXPECT_RESULT();
  47550. }
  47551. #endif /* !NO_FILESYSTEM */
  47552. #endif /* NO_CERTS */
  47553. #ifdef WOLFSSL_TLS13
  47554. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  47555. #ifdef WC_SHA384_DIGEST_SIZE
  47556. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  47557. #else
  47558. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  47559. #endif
  47560. #endif
  47561. #ifdef WOLFSSL_EARLY_DATA
  47562. static const char earlyData[] = "Early Data";
  47563. static char earlyDataBuffer[1];
  47564. #endif
  47565. static int test_tls13_apis(void)
  47566. {
  47567. EXPECT_DECLS;
  47568. int ret;
  47569. #ifndef WOLFSSL_NO_TLS12
  47570. #ifndef NO_WOLFSSL_CLIENT
  47571. WOLFSSL_CTX* clientTls12Ctx = NULL;
  47572. WOLFSSL* clientTls12Ssl = NULL;
  47573. #endif
  47574. #ifndef NO_WOLFSSL_SERVER
  47575. WOLFSSL_CTX* serverTls12Ctx = NULL;
  47576. WOLFSSL* serverTls12Ssl = NULL;
  47577. #endif
  47578. #endif
  47579. #ifndef NO_WOLFSSL_CLIENT
  47580. WOLFSSL_CTX* clientCtx = NULL;
  47581. WOLFSSL* clientSsl = NULL;
  47582. #endif
  47583. #ifndef NO_WOLFSSL_SERVER
  47584. WOLFSSL_CTX* serverCtx = NULL;
  47585. WOLFSSL* serverSsl = NULL;
  47586. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47587. const char* ourCert = svrCertFile;
  47588. const char* ourKey = svrKeyFile;
  47589. #endif
  47590. #endif
  47591. int required;
  47592. #ifdef WOLFSSL_EARLY_DATA
  47593. int outSz;
  47594. #endif
  47595. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  47596. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  47597. #ifdef HAVE_PQC
  47598. WOLFSSL_KYBER_LEVEL1
  47599. #else
  47600. WOLFSSL_ECC_SECP256R1
  47601. #endif
  47602. };
  47603. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  47604. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  47605. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  47606. int numGroups = 2;
  47607. #endif
  47608. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47609. char groupList[] =
  47610. #ifndef NO_ECC_SECP
  47611. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  47612. "P-521:"
  47613. #endif
  47614. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  47615. "P-384:"
  47616. #endif
  47617. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  47618. "P-256"
  47619. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  47620. ":P256_KYBER_LEVEL1"
  47621. #endif
  47622. #endif
  47623. #endif /* !defined(NO_ECC_SECP) */
  47624. #ifdef HAVE_PQC
  47625. ":KYBER_LEVEL1"
  47626. #endif
  47627. "";
  47628. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  47629. (void)ret;
  47630. #ifndef WOLFSSL_NO_TLS12
  47631. #ifndef NO_WOLFSSL_CLIENT
  47632. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  47633. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  47634. #endif
  47635. #ifndef NO_WOLFSSL_SERVER
  47636. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  47637. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47638. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  47639. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  47640. WOLFSSL_FILETYPE_PEM);
  47641. #endif
  47642. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  47643. #endif
  47644. #endif
  47645. #ifndef NO_WOLFSSL_CLIENT
  47646. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  47647. clientSsl = wolfSSL_new(clientCtx);
  47648. #endif
  47649. #ifndef NO_WOLFSSL_SERVER
  47650. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  47651. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  47652. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  47653. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  47654. #endif
  47655. serverSsl = wolfSSL_new(serverCtx);
  47656. ExpectNotNull(serverSsl);
  47657. #endif
  47658. #ifdef WOLFSSL_SEND_HRR_COOKIE
  47659. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  47660. #ifndef NO_WOLFSSL_CLIENT
  47661. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  47662. #endif
  47663. #ifndef NO_WOLFSSL_SERVER
  47664. #ifndef WOLFSSL_NO_TLS12
  47665. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  47666. BAD_FUNC_ARG);
  47667. #endif
  47668. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  47669. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  47670. WOLFSSL_SUCCESS);
  47671. #endif
  47672. #endif
  47673. #ifdef HAVE_SUPPORTED_CURVES
  47674. #ifdef HAVE_ECC
  47675. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  47676. BAD_FUNC_ARG);
  47677. #ifndef NO_WOLFSSL_SERVER
  47678. do {
  47679. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  47680. #ifdef WOLFSSL_ASYNC_CRYPT
  47681. if (ret == WC_PENDING_E)
  47682. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  47683. #endif
  47684. }
  47685. while (ret == WC_PENDING_E);
  47686. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47687. #endif
  47688. #ifndef NO_WOLFSSL_CLIENT
  47689. #ifndef WOLFSSL_NO_TLS12
  47690. do {
  47691. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  47692. #ifdef WOLFSSL_ASYNC_CRYPT
  47693. if (ret == WC_PENDING_E)
  47694. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  47695. #endif
  47696. }
  47697. while (ret == WC_PENDING_E);
  47698. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47699. #endif
  47700. do {
  47701. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  47702. #ifdef WOLFSSL_ASYNC_CRYPT
  47703. if (ret == WC_PENDING_E)
  47704. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  47705. #endif
  47706. }
  47707. while (ret == WC_PENDING_E);
  47708. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  47709. #endif
  47710. #elif defined(HAVE_CURVE25519)
  47711. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  47712. #ifndef NO_WOLFSSL_SERVER
  47713. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  47714. WOLFSSL_SUCCESS);
  47715. #endif
  47716. #ifndef NO_WOLFSSL_CLIENT
  47717. #ifndef WOLFSSL_NO_TLS12
  47718. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  47719. WOLFSSL_SUCCESS);
  47720. #endif
  47721. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  47722. WOLFSSL_SUCCESS);
  47723. #endif
  47724. #elif defined(HAVE_CURVE448)
  47725. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  47726. #ifndef NO_WOLFSSL_SERVER
  47727. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  47728. WOLFSSL_SUCCESS);
  47729. #endif
  47730. #ifndef NO_WOLFSSL_CLIENT
  47731. #ifndef WOLFSSL_NO_TLS12
  47732. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  47733. WOLFSSL_SUCCESS);
  47734. #endif
  47735. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  47736. WOLFSSL_SUCCESS);
  47737. #endif
  47738. #else
  47739. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  47740. BAD_FUNC_ARG);
  47741. #ifndef NO_WOLFSSL_CLIENT
  47742. #ifndef WOLFSSL_NO_TLS12
  47743. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  47744. NOT_COMPILED_IN);
  47745. #endif
  47746. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  47747. NOT_COMPILED_IN);
  47748. #endif
  47749. #endif
  47750. #if defined(HAVE_PQC)
  47751. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  47752. #ifndef NO_WOLFSSL_SERVER
  47753. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  47754. WOLFSSL_SUCCESS);
  47755. #endif
  47756. #ifndef NO_WOLFSSL_CLIENT
  47757. #ifndef WOLFSSL_NO_TLS12
  47758. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  47759. BAD_FUNC_ARG);
  47760. #endif
  47761. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  47762. WOLFSSL_SUCCESS);
  47763. #endif
  47764. #endif
  47765. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  47766. #ifndef NO_WOLFSSL_SERVER
  47767. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  47768. #endif
  47769. #ifndef NO_WOLFSSL_CLIENT
  47770. #ifndef WOLFSSL_NO_TLS12
  47771. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  47772. #endif
  47773. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  47774. #endif
  47775. #endif /* HAVE_SUPPORTED_CURVES */
  47776. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  47777. #ifndef NO_WOLFSSL_CLIENT
  47778. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  47779. #endif
  47780. #ifndef NO_WOLFSSL_SERVER
  47781. #ifndef WOLFSSL_NO_TLS12
  47782. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  47783. #endif
  47784. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  47785. #endif
  47786. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  47787. #ifndef NO_WOLFSSL_CLIENT
  47788. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  47789. #endif
  47790. #ifndef NO_WOLFSSL_SERVER
  47791. #ifndef WOLFSSL_NO_TLS12
  47792. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  47793. #endif
  47794. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  47795. #endif
  47796. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  47797. #ifndef NO_WOLFSSL_CLIENT
  47798. #ifndef WOLFSSL_NO_TLS12
  47799. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  47800. #endif
  47801. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  47802. #endif
  47803. #ifndef NO_WOLFSSL_SERVER
  47804. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  47805. #endif
  47806. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  47807. #ifndef NO_WOLFSSL_CLIENT
  47808. #ifndef WOLFSSL_NO_TLS12
  47809. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  47810. #endif
  47811. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  47812. #endif
  47813. #ifndef NO_WOLFSSL_SERVER
  47814. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  47815. #endif
  47816. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  47817. #ifndef NO_WOLFSSL_CLIENT
  47818. #ifndef WOLFSSL_NO_TLS12
  47819. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  47820. #endif
  47821. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  47822. #endif
  47823. #ifndef NO_WOLFSSL_SERVER
  47824. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  47825. #endif
  47826. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  47827. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  47828. #ifndef NO_WOLFSSL_CLIENT
  47829. #ifndef WOLFSSL_NO_TLS12
  47830. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  47831. BAD_FUNC_ARG);
  47832. #endif
  47833. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  47834. #endif
  47835. #ifndef NO_WOLFSSL_SERVER
  47836. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  47837. #endif
  47838. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  47839. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  47840. #ifndef NO_WOLFSSL_SERVER
  47841. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  47842. #endif
  47843. #ifndef NO_WOLFSSL_CLIENT
  47844. #ifndef WOLFSSL_NO_TLS12
  47845. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  47846. BAD_FUNC_ARG);
  47847. #endif
  47848. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  47849. #endif
  47850. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  47851. #ifndef NO_WOLFSSL_SERVER
  47852. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  47853. #endif
  47854. #ifndef NO_WOLFSSL_CLIENT
  47855. #ifndef WOLFSSL_NO_TLS12
  47856. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  47857. BAD_FUNC_ARG);
  47858. #endif
  47859. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  47860. #endif
  47861. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  47862. #ifndef NO_WOLFSSL_CLIENT
  47863. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  47864. #endif
  47865. #ifndef NO_WOLFSSL_SERVER
  47866. #ifndef WOLFSSL_NO_TLS12
  47867. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  47868. BAD_FUNC_ARG);
  47869. #endif
  47870. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  47871. #endif
  47872. #endif
  47873. #ifdef HAVE_ECC
  47874. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  47875. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  47876. #ifndef NO_WOLFSSL_SERVER
  47877. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  47878. #endif
  47879. #ifndef NO_WOLFSSL_CLIENT
  47880. #ifndef WOLFSSL_NO_TLS12
  47881. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  47882. #endif
  47883. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  47884. #endif
  47885. #endif
  47886. #ifdef HAVE_SUPPORTED_CURVES
  47887. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  47888. #ifndef NO_WOLFSSL_CLIENT
  47889. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  47890. #endif
  47891. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  47892. #ifndef NO_WOLFSSL_CLIENT
  47893. #ifndef WOLFSSL_NO_TLS12
  47894. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  47895. BAD_FUNC_ARG);
  47896. #endif
  47897. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  47898. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  47899. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  47900. WOLFSSL_SUCCESS);
  47901. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  47902. BAD_FUNC_ARG);
  47903. #endif
  47904. #ifndef NO_WOLFSSL_SERVER
  47905. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  47906. WOLFSSL_SUCCESS);
  47907. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  47908. BAD_FUNC_ARG);
  47909. #endif
  47910. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  47911. #ifndef NO_WOLFSSL_CLIENT
  47912. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  47913. #endif
  47914. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  47915. #ifndef NO_WOLFSSL_CLIENT
  47916. #ifndef WOLFSSL_NO_TLS12
  47917. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  47918. BAD_FUNC_ARG);
  47919. #endif
  47920. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  47921. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  47922. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  47923. WOLFSSL_SUCCESS);
  47924. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  47925. BAD_FUNC_ARG);
  47926. #endif
  47927. #ifndef NO_WOLFSSL_SERVER
  47928. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  47929. WOLFSSL_SUCCESS);
  47930. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  47931. BAD_FUNC_ARG);
  47932. #endif
  47933. #ifdef OPENSSL_EXTRA
  47934. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  47935. #ifndef NO_WOLFSSL_CLIENT
  47936. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  47937. WOLFSSL_FAILURE);
  47938. #endif
  47939. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  47940. WOLFSSL_FAILURE);
  47941. #ifndef NO_WOLFSSL_CLIENT
  47942. #ifndef WOLFSSL_NO_TLS12
  47943. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  47944. WOLFSSL_FAILURE);
  47945. #endif
  47946. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  47947. WOLFSSL_SUCCESS);
  47948. #endif
  47949. #ifndef NO_WOLFSSL_SERVER
  47950. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  47951. WOLFSSL_SUCCESS);
  47952. #endif
  47953. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  47954. #ifndef NO_WOLFSSL_CLIENT
  47955. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  47956. #endif
  47957. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  47958. #ifndef NO_WOLFSSL_CLIENT
  47959. #ifndef WOLFSSL_NO_TLS12
  47960. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  47961. WOLFSSL_FAILURE);
  47962. #endif
  47963. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  47964. WOLFSSL_SUCCESS);
  47965. #endif
  47966. #ifndef NO_WOLFSSL_SERVER
  47967. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  47968. WOLFSSL_SUCCESS);
  47969. #endif
  47970. #endif /* OPENSSL_EXTRA */
  47971. #endif /* HAVE_SUPPORTED_CURVES */
  47972. #endif /* HAVE_ECC */
  47973. #ifdef WOLFSSL_EARLY_DATA
  47974. #ifndef OPENSSL_EXTRA
  47975. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47976. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47977. #else
  47978. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  47979. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  47980. #endif
  47981. #ifndef NO_WOLFSSL_CLIENT
  47982. #ifndef OPENSSL_EXTRA
  47983. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47984. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47985. #else
  47986. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  47987. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  47988. #endif
  47989. #endif
  47990. #ifndef NO_WOLFSSL_SERVER
  47991. #ifndef WOLFSSL_NO_TLS12
  47992. #ifndef OPENSSL_EXTRA
  47993. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47994. BAD_FUNC_ARG);
  47995. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  47996. #else
  47997. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  47998. BAD_FUNC_ARG);
  47999. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48000. #endif
  48001. #endif
  48002. #ifndef OPENSSL_EXTRA
  48003. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48004. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  48005. WOLFSSL_SUCCESS);
  48006. #else
  48007. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  48008. #endif
  48009. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  48010. #else
  48011. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  48012. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  48013. #endif
  48014. #endif
  48015. #ifndef OPENSSL_EXTRA
  48016. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48017. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48018. #else
  48019. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48020. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48021. #endif
  48022. #ifndef NO_WOLFSSL_CLIENT
  48023. #ifndef OPENSSL_EXTRA
  48024. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48025. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48026. #else
  48027. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  48028. #endif
  48029. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  48030. #else
  48031. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48032. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  48033. #endif
  48034. #endif
  48035. #ifndef NO_WOLFSSL_SERVER
  48036. #ifndef WOLFSSL_NO_TLS12
  48037. #ifndef OPENSSL_EXTRA
  48038. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48039. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48040. #else
  48041. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48042. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48043. #endif
  48044. #endif
  48045. #ifndef OPENSSL_EXTRA
  48046. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48047. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  48048. #else
  48049. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  48050. #endif
  48051. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  48052. #else
  48053. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  48054. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  48055. #endif
  48056. #endif
  48057. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  48058. &outSz), BAD_FUNC_ARG);
  48059. #ifndef NO_WOLFSSL_CLIENT
  48060. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  48061. &outSz), BAD_FUNC_ARG);
  48062. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  48063. BAD_FUNC_ARG);
  48064. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48065. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  48066. #endif
  48067. #ifndef NO_WOLFSSL_SERVER
  48068. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  48069. sizeof(earlyData), &outSz), SIDE_ERROR);
  48070. #endif
  48071. #ifndef NO_WOLFSSL_CLIENT
  48072. #ifndef WOLFSSL_NO_TLS12
  48073. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  48074. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  48075. #endif
  48076. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48077. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  48078. #endif
  48079. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  48080. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48081. #ifndef NO_WOLFSSL_SERVER
  48082. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  48083. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48084. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  48085. &outSz), BAD_FUNC_ARG);
  48086. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48087. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  48088. #endif
  48089. #ifndef NO_WOLFSSL_CLIENT
  48090. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  48091. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  48092. #endif
  48093. #ifndef NO_WOLFSSL_SERVER
  48094. #ifndef WOLFSSL_NO_TLS12
  48095. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  48096. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48097. #endif
  48098. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48099. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  48100. #endif
  48101. #endif
  48102. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  48103. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  48104. #endif
  48105. #ifndef NO_WOLFSSL_SERVER
  48106. wolfSSL_free(serverSsl);
  48107. wolfSSL_CTX_free(serverCtx);
  48108. #endif
  48109. #ifndef NO_WOLFSSL_CLIENT
  48110. wolfSSL_free(clientSsl);
  48111. wolfSSL_CTX_free(clientCtx);
  48112. #endif
  48113. #ifndef WOLFSSL_NO_TLS12
  48114. #ifndef NO_WOLFSSL_SERVER
  48115. wolfSSL_free(serverTls12Ssl);
  48116. wolfSSL_CTX_free(serverTls12Ctx);
  48117. #endif
  48118. #ifndef NO_WOLFSSL_CLIENT
  48119. wolfSSL_free(clientTls12Ssl);
  48120. wolfSSL_CTX_free(clientTls12Ctx);
  48121. #endif
  48122. #endif
  48123. return EXPECT_RESULT();
  48124. }
  48125. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48126. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48127. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48128. /* Called when writing. */
  48129. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48130. {
  48131. (void)ssl;
  48132. (void)buf;
  48133. (void)sz;
  48134. (void)ctx;
  48135. /* Force error return from wolfSSL_accept_TLSv13(). */
  48136. return WANT_WRITE;
  48137. }
  48138. /* Called when reading. */
  48139. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48140. {
  48141. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  48142. int len = (int)msg->length;
  48143. (void)ssl;
  48144. (void)sz;
  48145. /* Pass back as much of message as will fit in buffer. */
  48146. if (len > sz)
  48147. len = sz;
  48148. XMEMCPY(buf, msg->buffer, len);
  48149. /* Move over returned data. */
  48150. msg->buffer += len;
  48151. msg->length -= len;
  48152. /* Amount actually copied. */
  48153. return len;
  48154. }
  48155. #endif
  48156. static int test_tls13_cipher_suites(void)
  48157. {
  48158. EXPECT_DECLS;
  48159. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48160. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48161. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48162. WOLFSSL_CTX* ctx = NULL;
  48163. WOLFSSL *ssl = NULL;
  48164. int i;
  48165. byte clientHello[] = {
  48166. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  48167. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  48168. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  48169. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  48170. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  48171. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  48172. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  48173. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  48174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48175. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  48176. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  48177. 0x13, 0x01,
  48178. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  48179. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  48180. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  48181. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  48182. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  48183. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  48184. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  48185. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  48186. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  48187. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  48188. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  48189. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  48190. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  48191. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  48192. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  48193. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  48194. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  48195. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  48196. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  48197. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  48198. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  48199. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  48200. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  48201. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  48202. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  48203. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  48204. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  48205. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  48206. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  48207. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  48208. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  48209. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  48210. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  48211. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  48212. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  48213. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48215. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  48216. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  48217. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  48218. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  48219. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  48220. };
  48221. WOLFSSL_BUFFER_INFO msg;
  48222. /* Offset into ClientHello message data of first cipher suite. */
  48223. const int csOff = 78;
  48224. /* Server cipher list. */
  48225. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  48226. /* Suite list with duplicates. */
  48227. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  48228. "TLS13-AES128-GCM-SHA256:"
  48229. "TLS13-AES256-GCM-SHA384:"
  48230. "TLS13-AES256-GCM-SHA384:"
  48231. "TLS13-AES128-GCM-SHA256";
  48232. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48233. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48234. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48235. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48236. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48237. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  48238. #endif
  48239. /* Set up wolfSSL context. */
  48240. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  48241. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48242. WOLFSSL_FILETYPE_PEM));
  48243. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48244. WOLFSSL_FILETYPE_PEM));
  48245. /* Read from 'msg'. */
  48246. wolfSSL_SetIORecv(ctx, CsRecv);
  48247. /* No where to send to - dummy sender. */
  48248. wolfSSL_SetIOSend(ctx, CsSend);
  48249. /* Test cipher suite list with many copies of a cipher suite. */
  48250. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48251. msg.buffer = clientHello;
  48252. msg.length = (unsigned int)sizeof(clientHello);
  48253. wolfSSL_SetIOReadCtx(ssl, &msg);
  48254. /* Force server to have as many occurrences of same cipher suite as
  48255. * possible. */
  48256. if (ssl != NULL) {
  48257. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  48258. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  48259. for (i = 0; i < suites->suiteSz; i += 2) {
  48260. suites->suites[i + 0] = TLS13_BYTE;
  48261. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  48262. }
  48263. }
  48264. /* Test multiple occurrences of same cipher suite. */
  48265. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48266. wolfSSL_free(ssl);
  48267. ssl = NULL;
  48268. /* Set client order opposite to server order:
  48269. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  48270. clientHello[csOff + 0] = TLS13_BYTE;
  48271. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  48272. clientHello[csOff + 2] = TLS13_BYTE;
  48273. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  48274. /* Test server order negotiation. */
  48275. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48276. msg.buffer = clientHello;
  48277. msg.length = (unsigned int)sizeof(clientHello);
  48278. wolfSSL_SetIOReadCtx(ssl, &msg);
  48279. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48280. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48281. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  48282. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48283. /* Check refined order - server order. */
  48284. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48285. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48286. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48287. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48288. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48289. wolfSSL_free(ssl);
  48290. ssl = NULL;
  48291. /* Test client order negotiation. */
  48292. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48293. msg.buffer = clientHello;
  48294. msg.length = (unsigned int)sizeof(clientHello);
  48295. wolfSSL_SetIOReadCtx(ssl, &msg);
  48296. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48297. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48298. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  48299. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  48300. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48301. /* Check refined order - client order. */
  48302. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48303. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48304. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48305. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48306. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48307. wolfSSL_free(ssl);
  48308. ssl = NULL;
  48309. /* Check duplicate detection is working. */
  48310. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  48311. ExpectIntEQ(ctx->suites->suiteSz, 4);
  48312. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48313. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48314. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48315. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48316. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48317. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  48318. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  48319. ExpectIntEQ(ctx->suites->suiteSz, 4);
  48320. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48321. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48322. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48323. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48324. #endif
  48325. wolfSSL_CTX_free(ctx);
  48326. #endif
  48327. return EXPECT_RESULT();
  48328. }
  48329. #endif
  48330. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  48331. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  48332. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  48333. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  48334. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  48335. const unsigned char* priv, unsigned int privSz,
  48336. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  48337. unsigned char* out, unsigned int* outlen,
  48338. void* ctx)
  48339. {
  48340. int result;
  48341. /* Test fail when context associated with WOLFSSL is NULL */
  48342. if (ctx == NULL) {
  48343. return -1;
  48344. }
  48345. (void)ssl;
  48346. /* return 0 on success */
  48347. PRIVATE_KEY_UNLOCK();
  48348. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  48349. PRIVATE_KEY_LOCK();
  48350. return result;
  48351. }
  48352. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  48353. EXPECT_DECLS;
  48354. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  48355. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  48356. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  48357. WOLFSSL_SUCCESS);
  48358. #endif
  48359. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  48360. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  48361. WOLFSSL_SUCCESS);
  48362. #endif
  48363. return EXPECT_RESULT();
  48364. }
  48365. static int test_dh_ssl_setup(WOLFSSL* ssl)
  48366. {
  48367. EXPECT_DECLS;
  48368. static int dh_test_ctx = 1;
  48369. int ret;
  48370. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  48371. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  48372. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  48373. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  48374. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48375. }
  48376. return EXPECT_RESULT();
  48377. }
  48378. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  48379. {
  48380. EXPECT_DECLS;
  48381. int ret;
  48382. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  48383. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  48384. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  48385. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  48386. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48387. }
  48388. return EXPECT_RESULT();
  48389. }
  48390. #endif
  48391. static int test_DhCallbacks(void)
  48392. {
  48393. EXPECT_DECLS;
  48394. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  48395. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  48396. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  48397. WOLFSSL_CTX *ctx = NULL;
  48398. WOLFSSL *ssl = NULL;
  48399. int test;
  48400. test_ssl_cbf func_cb_client;
  48401. test_ssl_cbf func_cb_server;
  48402. /* Test that DH callback APIs work. */
  48403. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48404. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  48405. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  48406. /* load client ca cert */
  48407. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  48408. WOLFSSL_SUCCESS);
  48409. /* test with NULL arguments */
  48410. wolfSSL_SetDhAgreeCtx(NULL, &test);
  48411. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  48412. /* test success case */
  48413. test = 1;
  48414. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48415. wolfSSL_SetDhAgreeCtx(ssl, &test);
  48416. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  48417. wolfSSL_free(ssl);
  48418. wolfSSL_CTX_free(ctx);
  48419. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  48420. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  48421. /* set callbacks to use DH functions */
  48422. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  48423. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  48424. func_cb_client.method = wolfTLSv1_2_client_method;
  48425. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  48426. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  48427. func_cb_server.method = wolfTLSv1_2_server_method;
  48428. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  48429. &func_cb_server, NULL), TEST_SUCCESS);
  48430. /* Test fail */
  48431. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  48432. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  48433. /* set callbacks to use DH functions */
  48434. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  48435. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  48436. func_cb_client.method = wolfTLSv1_2_client_method;
  48437. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  48438. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  48439. func_cb_server.method = wolfTLSv1_2_server_method;
  48440. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  48441. &func_cb_server, NULL), TEST_FAIL);
  48442. #endif
  48443. return EXPECT_RESULT();
  48444. }
  48445. #endif /* HAVE_PK_CALLBACKS */
  48446. #ifdef HAVE_HASHDRBG
  48447. #ifdef TEST_RESEED_INTERVAL
  48448. static int test_wc_RNG_GenerateBlock_Reseed(void)
  48449. {
  48450. EXPECT_DECLS;
  48451. int i;
  48452. WC_RNG rng;
  48453. byte key[32];
  48454. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48455. ExpectIntEQ(wc_InitRng(&rng), 0);
  48456. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  48457. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  48458. }
  48459. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  48460. return EXPECT_RESULT();
  48461. }
  48462. #endif /* TEST_RESEED_INTERVAL */
  48463. static int test_wc_RNG_GenerateBlock(void)
  48464. {
  48465. EXPECT_DECLS;
  48466. int i;
  48467. WC_RNG rng;
  48468. byte key[32];
  48469. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48470. ExpectIntEQ(wc_InitRng(&rng), 0);
  48471. for (i = 0; i < 10; i++) {
  48472. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  48473. }
  48474. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  48475. return EXPECT_RESULT();
  48476. }
  48477. #endif /* HAVE_HASHDRBG */
  48478. /*
  48479. * Testing get_rand_digit
  48480. */
  48481. static int test_get_rand_digit(void)
  48482. {
  48483. EXPECT_DECLS;
  48484. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  48485. WC_RNG rng;
  48486. mp_digit d;
  48487. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48488. ExpectIntEQ(wc_InitRng(&rng), 0);
  48489. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  48490. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  48491. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  48492. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  48493. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  48494. #endif
  48495. return EXPECT_RESULT();
  48496. } /* End test_get_rand_digit*/
  48497. /*
  48498. * Testing get_digit_count
  48499. */
  48500. static int test_get_digit_count(void)
  48501. {
  48502. EXPECT_DECLS;
  48503. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  48504. mp_int a;
  48505. XMEMSET(&a, 0, sizeof(mp_int));
  48506. ExpectIntEQ(mp_init(&a), 0);
  48507. ExpectIntEQ(get_digit_count(NULL), 0);
  48508. ExpectIntEQ(get_digit_count(&a), 0);
  48509. mp_clear(&a);
  48510. #endif
  48511. return EXPECT_RESULT();
  48512. } /* End test_get_digit_count*/
  48513. /*
  48514. * Testing mp_cond_copy
  48515. */
  48516. static int test_mp_cond_copy(void)
  48517. {
  48518. EXPECT_DECLS;
  48519. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  48520. defined(WOLFSSL_PUBLIC_MP)
  48521. mp_int a;
  48522. mp_int b;
  48523. int copy = 0;
  48524. XMEMSET(&a, 0, sizeof(mp_int));
  48525. XMEMSET(&b, 0, sizeof(mp_int));
  48526. ExpectIntEQ(mp_init(&a), MP_OKAY);
  48527. ExpectIntEQ(mp_init(&b), MP_OKAY);
  48528. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  48529. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  48530. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  48531. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  48532. mp_clear(&a);
  48533. mp_clear(&b);
  48534. #endif
  48535. return EXPECT_RESULT();
  48536. } /* End test_mp_cond_copy*/
  48537. /*
  48538. * Testing mp_rand
  48539. */
  48540. static int test_mp_rand(void)
  48541. {
  48542. EXPECT_DECLS;
  48543. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  48544. mp_int a;
  48545. WC_RNG rng;
  48546. int digits = 1;
  48547. XMEMSET(&a, 0, sizeof(mp_int));
  48548. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48549. ExpectIntEQ(mp_init(&a), MP_OKAY);
  48550. ExpectIntEQ(wc_InitRng(&rng), 0);
  48551. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  48552. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  48553. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  48554. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  48555. mp_clear(&a);
  48556. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  48557. #endif
  48558. return EXPECT_RESULT();
  48559. } /* End test_mp_rand*/
  48560. /*
  48561. * Testing get_digit
  48562. */
  48563. static int test_get_digit(void)
  48564. {
  48565. EXPECT_DECLS;
  48566. #if defined(WOLFSSL_PUBLIC_MP)
  48567. mp_int a;
  48568. int n = 0;
  48569. XMEMSET(&a, 0, sizeof(mp_int));
  48570. ExpectIntEQ(mp_init(&a), MP_OKAY);
  48571. ExpectIntEQ(get_digit(NULL, n), 0);
  48572. ExpectIntEQ(get_digit(&a, n), 0);
  48573. mp_clear(&a);
  48574. #endif
  48575. return EXPECT_RESULT();
  48576. } /* End test_get_digit*/
  48577. /*
  48578. * Testing wc_export_int
  48579. */
  48580. static int test_wc_export_int(void)
  48581. {
  48582. EXPECT_DECLS;
  48583. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  48584. defined(WOLFSSL_PUBLIC_MP)
  48585. mp_int mp;
  48586. byte buf[32];
  48587. word32 keySz = (word32)sizeof(buf);
  48588. word32 len = (word32)sizeof(buf);
  48589. XMEMSET(&mp, 0, sizeof(mp_int));
  48590. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  48591. ExpectIntEQ(mp_set(&mp, 1234), 0);
  48592. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  48593. BAD_FUNC_ARG);
  48594. len = sizeof(buf)-1;
  48595. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  48596. BUFFER_E);
  48597. len = sizeof(buf);
  48598. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  48599. len = 4; /* test input too small */
  48600. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  48601. len = sizeof(buf);
  48602. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  48603. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  48604. ExpectIntEQ(len, 5);
  48605. mp_clear(&mp);
  48606. #endif
  48607. return EXPECT_RESULT();
  48608. } /* End test_wc_export_int*/
  48609. static int test_wc_InitRngNonce(void)
  48610. {
  48611. EXPECT_DECLS;
  48612. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  48613. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  48614. HAVE_FIPS_VERSION >= 2))
  48615. WC_RNG rng;
  48616. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  48617. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  48618. word32 nonceSz = sizeof(nonce);
  48619. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  48620. ExpectIntEQ(wc_FreeRng(&rng), 0);
  48621. #endif
  48622. return EXPECT_RESULT();
  48623. } /* End test_wc_InitRngNonce*/
  48624. /*
  48625. * Testing wc_InitRngNonce_ex
  48626. */
  48627. static int test_wc_InitRngNonce_ex(void)
  48628. {
  48629. EXPECT_DECLS;
  48630. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  48631. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  48632. HAVE_FIPS_VERSION >= 2))
  48633. WC_RNG rng;
  48634. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  48635. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  48636. word32 nonceSz = sizeof(nonce);
  48637. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  48638. 0);
  48639. ExpectIntEQ(wc_FreeRng(&rng), 0);
  48640. #endif
  48641. return EXPECT_RESULT();
  48642. } /* End test_wc_InitRngNonce_ex */
  48643. static int test_wolfSSL_X509_CRL(void)
  48644. {
  48645. EXPECT_DECLS;
  48646. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  48647. X509_CRL *crl = NULL;
  48648. char pem[][100] = {
  48649. "./certs/crl/crl.pem",
  48650. "./certs/crl/crl2.pem",
  48651. "./certs/crl/caEccCrl.pem",
  48652. "./certs/crl/eccCliCRL.pem",
  48653. "./certs/crl/eccSrvCRL.pem",
  48654. ""
  48655. };
  48656. #ifndef NO_BIO
  48657. BIO *bio = NULL;
  48658. #endif
  48659. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  48660. char der[][100] = {
  48661. "./certs/crl/crl.der",
  48662. "./certs/crl/crl2.der",
  48663. ""};
  48664. #endif
  48665. XFILE fp = XBADFILE;
  48666. int i;
  48667. for (i = 0; pem[i][0] != '\0'; i++)
  48668. {
  48669. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  48670. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48671. NULL, NULL));
  48672. ExpectNotNull(crl);
  48673. X509_CRL_free(crl);
  48674. if (fp != XBADFILE) {
  48675. XFCLOSE(fp);
  48676. fp = XBADFILE;
  48677. }
  48678. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  48679. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  48680. NULL));
  48681. if (EXPECT_FAIL()) {
  48682. crl = NULL;
  48683. }
  48684. ExpectNotNull(crl);
  48685. X509_CRL_free(crl);
  48686. crl = NULL;
  48687. if (fp != XBADFILE) {
  48688. XFCLOSE(fp);
  48689. fp = XBADFILE;
  48690. }
  48691. }
  48692. #ifndef NO_BIO
  48693. for (i = 0; pem[i][0] != '\0'; i++)
  48694. {
  48695. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  48696. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  48697. X509_CRL_free(crl);
  48698. crl = NULL;
  48699. BIO_free(bio);
  48700. bio = NULL;
  48701. }
  48702. #endif
  48703. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  48704. for (i = 0; der[i][0] != '\0'; i++) {
  48705. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  48706. ExpectTrue((fp != XBADFILE));
  48707. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  48708. ExpectNotNull(crl);
  48709. X509_CRL_free(crl);
  48710. if (fp != XBADFILE) {
  48711. XFCLOSE(fp);
  48712. fp = XBADFILE;
  48713. }
  48714. fp = XFOPEN(der[i], "rb");
  48715. ExpectTrue((fp != XBADFILE));
  48716. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  48717. if (EXPECT_FAIL()) {
  48718. crl = NULL;
  48719. }
  48720. ExpectNotNull(crl);
  48721. X509_CRL_free(crl);
  48722. crl = NULL;
  48723. if (fp != XBADFILE) {
  48724. XFCLOSE(fp);
  48725. fp = XBADFILE;
  48726. }
  48727. }
  48728. #endif
  48729. #endif
  48730. return EXPECT_RESULT();
  48731. }
  48732. static int test_wolfSSL_X509_load_crl_file(void)
  48733. {
  48734. EXPECT_DECLS;
  48735. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48736. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  48737. int i;
  48738. char pem[][100] = {
  48739. "./certs/crl/crl.pem",
  48740. "./certs/crl/crl2.pem",
  48741. "./certs/crl/caEccCrl.pem",
  48742. "./certs/crl/eccCliCRL.pem",
  48743. "./certs/crl/eccSrvCRL.pem",
  48744. #ifdef WC_RSA_PSS
  48745. "./certs/crl/crl_rsapss.pem",
  48746. #endif
  48747. ""
  48748. };
  48749. char der[][100] = {
  48750. "./certs/crl/crl.der",
  48751. "./certs/crl/crl2.der",
  48752. ""
  48753. };
  48754. WOLFSSL_X509_STORE* store = NULL;
  48755. WOLFSSL_X509_LOOKUP* lookup = NULL;
  48756. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  48757. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  48758. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  48759. X509_FILETYPE_PEM), 1);
  48760. #ifdef WC_RSA_PSS
  48761. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  48762. X509_FILETYPE_PEM), 1);
  48763. #endif
  48764. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  48765. X509_FILETYPE_PEM), 1);
  48766. if (store) {
  48767. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  48768. WOLFSSL_FILETYPE_PEM), 1);
  48769. /* since store hasn't yet known the revoked cert*/
  48770. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48771. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  48772. }
  48773. for (i = 0; pem[i][0] != '\0'; i++) {
  48774. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  48775. 1);
  48776. }
  48777. if (store) {
  48778. /* since store knows crl list */
  48779. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48780. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  48781. CRL_CERT_REVOKED);
  48782. #ifdef WC_RSA_PSS
  48783. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48784. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  48785. CRL_CERT_REVOKED);
  48786. #endif
  48787. }
  48788. /* once feeing store */
  48789. X509_STORE_free(store);
  48790. store = NULL;
  48791. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  48792. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  48793. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  48794. X509_FILETYPE_PEM), 1);
  48795. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  48796. X509_FILETYPE_PEM), 1);
  48797. if (store) {
  48798. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  48799. WOLFSSL_FILETYPE_PEM), 1);
  48800. /* since store hasn't yet known the revoked cert*/
  48801. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48802. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  48803. }
  48804. for (i = 0; der[i][0] != '\0'; i++) {
  48805. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  48806. 1);
  48807. }
  48808. if (store) {
  48809. /* since store knows crl list */
  48810. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  48811. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  48812. CRL_CERT_REVOKED);
  48813. }
  48814. /* test for incorrect parameter */
  48815. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  48816. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  48817. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  48818. X509_STORE_free(store);
  48819. store = NULL;
  48820. #endif
  48821. return EXPECT_RESULT();
  48822. }
  48823. static int test_wolfSSL_i2d_X509(void)
  48824. {
  48825. EXPECT_DECLS;
  48826. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  48827. const unsigned char* cert_buf = server_cert_der_2048;
  48828. unsigned char* out = NULL;
  48829. unsigned char* tmp = NULL;
  48830. X509* cert = NULL;
  48831. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  48832. /* Pointer should be advanced */
  48833. ExpectPtrGT(cert_buf, server_cert_der_2048);
  48834. ExpectIntGT(i2d_X509(cert, &out), 0);
  48835. ExpectNotNull(out);
  48836. tmp = out;
  48837. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  48838. ExpectPtrGT(tmp, out);
  48839. if (out != NULL)
  48840. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  48841. X509_free(cert);
  48842. #endif
  48843. return EXPECT_RESULT();
  48844. }
  48845. static int test_wolfSSL_d2i_X509_REQ(void)
  48846. {
  48847. EXPECT_DECLS;
  48848. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  48849. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  48850. !defined(WOLFSSL_SP_MATH)
  48851. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  48852. * were generated by libest
  48853. * ./certs/csr.attr.der contains sample attributes
  48854. * ./certs/csr.ext.der contains sample extensions */
  48855. const char* csrFile = "./certs/csr.signed.der";
  48856. const char* csrPopFile = "./certs/csr.attr.der";
  48857. const char* csrExtFile = "./certs/csr.ext.der";
  48858. /* ./certs/csr.dsa.pem is generated using
  48859. * openssl req -newkey dsa:certs/dsaparams.pem \
  48860. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  48861. * -outform PEM
  48862. * with the passphrase "wolfSSL"
  48863. */
  48864. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48865. const char* csrDsaFile = "./certs/csr.dsa.pem";
  48866. XFILE f = XBADFILE;
  48867. #endif
  48868. BIO* bio = NULL;
  48869. X509* req = NULL;
  48870. EVP_PKEY *pub_key = NULL;
  48871. {
  48872. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  48873. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48874. /*
  48875. * Extract the public key from the CSR
  48876. */
  48877. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48878. /*
  48879. * Verify the signature in the CSR
  48880. */
  48881. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48882. X509_free(req);
  48883. req = NULL;
  48884. BIO_free(bio);
  48885. bio = NULL;
  48886. EVP_PKEY_free(pub_key);
  48887. pub_key = NULL;
  48888. }
  48889. {
  48890. #ifdef OPENSSL_ALL
  48891. X509_ATTRIBUTE* attr = NULL;
  48892. ASN1_TYPE *at = NULL;
  48893. #endif
  48894. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  48895. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48896. /*
  48897. * Extract the public key from the CSR
  48898. */
  48899. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48900. /*
  48901. * Verify the signature in the CSR
  48902. */
  48903. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48904. #ifdef OPENSSL_ALL
  48905. /*
  48906. * Obtain the challenge password from the CSR
  48907. */
  48908. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  48909. -1), 1);
  48910. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  48911. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48912. ExpectNotNull(at->value.asn1_string);
  48913. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  48914. "2xIE+qqp/rhyTXP+");
  48915. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  48916. #endif
  48917. X509_free(req);
  48918. req = NULL;
  48919. BIO_free(bio);
  48920. bio = NULL;
  48921. EVP_PKEY_free(pub_key);
  48922. pub_key = NULL;
  48923. }
  48924. {
  48925. #ifdef OPENSSL_ALL
  48926. X509_ATTRIBUTE* attr = NULL;
  48927. ASN1_TYPE *at = NULL;
  48928. STACK_OF(X509_EXTENSION) *exts = NULL;
  48929. #endif
  48930. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  48931. /* This CSR contains an Extension Request attribute so
  48932. * we test extension parsing in a CSR attribute here. */
  48933. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  48934. /*
  48935. * Extract the public key from the CSR
  48936. */
  48937. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48938. /*
  48939. * Verify the signature in the CSR
  48940. */
  48941. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48942. #ifdef OPENSSL_ALL
  48943. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  48944. req));
  48945. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  48946. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  48947. /*
  48948. * Obtain the challenge password from the CSR
  48949. */
  48950. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  48951. -1), 0);
  48952. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  48953. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  48954. ExpectNotNull(at->value.asn1_string);
  48955. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  48956. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  48957. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  48958. #endif
  48959. X509_free(req);
  48960. req = NULL;
  48961. BIO_free(bio);
  48962. bio = NULL;
  48963. EVP_PKEY_free(pub_key);
  48964. pub_key = NULL;
  48965. }
  48966. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  48967. {
  48968. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  48969. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  48970. /*
  48971. * Extract the public key from the CSR
  48972. */
  48973. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  48974. /*
  48975. * Verify the signature in the CSR
  48976. */
  48977. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48978. X509_free(req);
  48979. req = NULL;
  48980. BIO_free(bio);
  48981. /* Run the same test, but with a file pointer instead of a BIO.
  48982. * (PEM_read_X509_REQ)*/
  48983. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  48984. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  48985. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  48986. X509_free(req);
  48987. EVP_PKEY_free(pub_key);
  48988. }
  48989. #endif /* !NO_DSA && !HAVE_SELFTEST */
  48990. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  48991. return EXPECT_RESULT();
  48992. }
  48993. static int test_wolfSSL_PEM_read_X509(void)
  48994. {
  48995. EXPECT_DECLS;
  48996. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  48997. !defined(NO_RSA)
  48998. X509 *x509 = NULL;
  48999. XFILE fp = XBADFILE;
  49000. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  49001. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  49002. X509_free(x509);
  49003. if (fp != XBADFILE)
  49004. XFCLOSE(fp);
  49005. #endif
  49006. return EXPECT_RESULT();
  49007. }
  49008. static int test_wolfSSL_PEM_read(void)
  49009. {
  49010. EXPECT_DECLS;
  49011. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  49012. const char* filename = "./certs/server-keyEnc.pem";
  49013. XFILE fp = XBADFILE;
  49014. char* name = NULL;
  49015. char* header = NULL;
  49016. byte* data = NULL;
  49017. long len;
  49018. EVP_CIPHER_INFO cipher;
  49019. WOLFSSL_BIO* bio = NULL;
  49020. byte* fileData = NULL;
  49021. size_t fileDataSz = 0;
  49022. byte* out;
  49023. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  49024. /* Fail cases. */
  49025. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  49026. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  49027. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  49028. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  49029. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49030. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49031. ExpectIntGT(XSTRLEN(header), 0);
  49032. ExpectIntGT(len, 0);
  49033. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49034. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49035. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49036. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49037. DYNAMIC_TYPE_TMP_BUFFER));
  49038. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49039. if (fp != XBADFILE) {
  49040. XFCLOSE(fp);
  49041. fp = XBADFILE;
  49042. }
  49043. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49044. /* Fail cases. */
  49045. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  49046. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  49047. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  49048. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  49049. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49050. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49051. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49052. /* Fail cases. */
  49053. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  49054. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  49055. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  49056. #ifndef NO_DES3
  49057. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  49058. #endif
  49059. /* Fail cases. */
  49060. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  49061. (void*)"yassl123"), WOLFSSL_FAILURE);
  49062. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  49063. (void*)"yassl123"), WOLFSSL_FAILURE);
  49064. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  49065. (void*)"yassl123"), WOLFSSL_FAILURE);
  49066. #if !defined(NO_DES3) && !defined(NO_MD5)
  49067. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49068. (void*)"yassl123"), WOLFSSL_SUCCESS);
  49069. #endif
  49070. BIO_free(bio);
  49071. bio = NULL;
  49072. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49073. fileData = NULL;
  49074. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49075. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49076. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49077. name = NULL;
  49078. header = NULL;
  49079. data = NULL;
  49080. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  49081. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49082. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49083. ExpectIntEQ(XSTRLEN(header), 0);
  49084. ExpectIntGT(len, 0);
  49085. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49086. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49087. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49088. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49089. DYNAMIC_TYPE_TMP_BUFFER));
  49090. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49091. if (fp != XBADFILE)
  49092. XFCLOSE(fp);
  49093. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49094. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49095. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49096. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49097. BIO_free(bio);
  49098. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49099. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49100. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49101. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49102. #endif
  49103. return EXPECT_RESULT();
  49104. }
  49105. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  49106. {
  49107. EXPECT_DECLS;
  49108. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49109. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49110. const byte iv[12] = { 0 };
  49111. const byte key[16] = { 0 };
  49112. const byte cleartext[16] = { 0 };
  49113. const byte aad[] = {
  49114. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  49115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  49116. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  49117. 0x4f
  49118. };
  49119. byte out1Part[16];
  49120. byte outTag1Part[16];
  49121. byte out2Part[16];
  49122. byte outTag2Part[16];
  49123. byte decryptBuf[16];
  49124. int len = 0;
  49125. int tlen;
  49126. EVP_CIPHER_CTX* ctx = NULL;
  49127. /* ENCRYPT */
  49128. /* Send AAD and data in 1 part */
  49129. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49130. tlen = 0;
  49131. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49132. 1);
  49133. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49134. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49135. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  49136. sizeof(cleartext)), 1);
  49137. tlen += len;
  49138. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  49139. tlen += len;
  49140. ExpectIntEQ(tlen, sizeof(cleartext));
  49141. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49142. outTag1Part), 1);
  49143. EVP_CIPHER_CTX_free(ctx);
  49144. ctx = NULL;
  49145. /* DECRYPT */
  49146. /* Send AAD and data in 1 part */
  49147. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49148. tlen = 0;
  49149. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49150. 1);
  49151. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49152. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49153. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  49154. sizeof(cleartext)), 1);
  49155. tlen += len;
  49156. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49157. outTag1Part), 1);
  49158. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  49159. tlen += len;
  49160. ExpectIntEQ(tlen, sizeof(cleartext));
  49161. EVP_CIPHER_CTX_free(ctx);
  49162. ctx = NULL;
  49163. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49164. /* ENCRYPT */
  49165. /* Send AAD and data in 2 parts */
  49166. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49167. tlen = 0;
  49168. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49169. 1);
  49170. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49171. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49172. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49173. 1);
  49174. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  49175. tlen += len;
  49176. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  49177. sizeof(cleartext) - 1), 1);
  49178. tlen += len;
  49179. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  49180. tlen += len;
  49181. ExpectIntEQ(tlen, sizeof(cleartext));
  49182. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49183. outTag2Part), 1);
  49184. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  49185. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  49186. EVP_CIPHER_CTX_free(ctx);
  49187. ctx = NULL;
  49188. /* DECRYPT */
  49189. /* Send AAD and data in 2 parts */
  49190. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49191. tlen = 0;
  49192. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49193. 1);
  49194. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49195. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49196. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49197. 1);
  49198. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  49199. tlen += len;
  49200. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  49201. sizeof(cleartext) - 1), 1);
  49202. tlen += len;
  49203. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49204. outTag1Part), 1);
  49205. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  49206. tlen += len;
  49207. ExpectIntEQ(tlen, sizeof(cleartext));
  49208. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49209. /* Test AAD reuse */
  49210. EVP_CIPHER_CTX_free(ctx);
  49211. #endif
  49212. return EXPECT_RESULT();
  49213. }
  49214. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  49215. {
  49216. EXPECT_DECLS;
  49217. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49218. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49219. /* Zero length plain text */
  49220. byte key[] = {
  49221. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49222. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49223. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49224. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49225. }; /* align */
  49226. byte iv[] = {
  49227. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49228. }; /* align */
  49229. byte plaintxt[1];
  49230. int ivSz = 12;
  49231. int plaintxtSz = 0;
  49232. unsigned char tag[16];
  49233. unsigned char tag_kat[] = {
  49234. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  49235. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  49236. };
  49237. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49238. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49239. int ciphertxtSz = 0;
  49240. int decryptedtxtSz = 0;
  49241. int len = 0;
  49242. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49243. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49244. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  49245. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49246. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49247. plaintxtSz));
  49248. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49249. ciphertxtSz += len;
  49250. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  49251. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49252. ExpectIntEQ(0, ciphertxtSz);
  49253. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  49254. EVP_CIPHER_CTX_init(de);
  49255. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  49256. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49257. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49258. decryptedtxtSz = len;
  49259. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  49260. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49261. decryptedtxtSz += len;
  49262. ExpectIntEQ(0, decryptedtxtSz);
  49263. EVP_CIPHER_CTX_free(en);
  49264. EVP_CIPHER_CTX_free(de);
  49265. #endif
  49266. return EXPECT_RESULT();
  49267. }
  49268. static int test_wolfssl_EVP_aes_gcm(void)
  49269. {
  49270. EXPECT_DECLS;
  49271. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49272. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49273. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49274. byte *key = (byte*)"01234567890123456789012345678901";
  49275. /* A 128 bit IV */
  49276. byte *iv = (byte*)"0123456789012345";
  49277. int ivSz = AES_BLOCK_SIZE;
  49278. /* Message to be encrypted */
  49279. byte *plaintxt = (byte*)"for things to change you have to change";
  49280. /* Additional non-confidential data */
  49281. byte *aad = (byte*)"Don't spend major time on minor things.";
  49282. unsigned char tag[AES_BLOCK_SIZE] = {0};
  49283. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49284. int aadSz = (int)XSTRLEN((char*)aad);
  49285. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49286. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49287. int ciphertxtSz = 0;
  49288. int decryptedtxtSz = 0;
  49289. int len = 0;
  49290. int i = 0;
  49291. EVP_CIPHER_CTX en[2];
  49292. EVP_CIPHER_CTX de[2];
  49293. for (i = 0; i < 2; i++) {
  49294. EVP_CIPHER_CTX_init(&en[i]);
  49295. if (i == 0) {
  49296. /* Default uses 96-bits IV length */
  49297. #ifdef WOLFSSL_AES_128
  49298. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  49299. key, iv));
  49300. #elif defined(WOLFSSL_AES_192)
  49301. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  49302. key, iv));
  49303. #elif defined(WOLFSSL_AES_256)
  49304. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  49305. key, iv));
  49306. #endif
  49307. }
  49308. else {
  49309. #ifdef WOLFSSL_AES_128
  49310. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  49311. NULL, NULL));
  49312. #elif defined(WOLFSSL_AES_192)
  49313. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  49314. NULL, NULL));
  49315. #elif defined(WOLFSSL_AES_256)
  49316. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  49317. NULL, NULL));
  49318. #endif
  49319. /* non-default must to set the IV length first */
  49320. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  49321. ivSz, NULL));
  49322. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49323. }
  49324. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49325. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49326. plaintxtSz));
  49327. ciphertxtSz = len;
  49328. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49329. ciphertxtSz += len;
  49330. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  49331. AES_BLOCK_SIZE, tag));
  49332. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49333. EVP_CIPHER_CTX_init(&de[i]);
  49334. if (i == 0) {
  49335. /* Default uses 96-bits IV length */
  49336. #ifdef WOLFSSL_AES_128
  49337. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  49338. key, iv));
  49339. #elif defined(WOLFSSL_AES_192)
  49340. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  49341. key, iv));
  49342. #elif defined(WOLFSSL_AES_256)
  49343. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  49344. key, iv));
  49345. #endif
  49346. }
  49347. else {
  49348. #ifdef WOLFSSL_AES_128
  49349. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  49350. NULL, NULL));
  49351. #elif defined(WOLFSSL_AES_192)
  49352. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  49353. NULL, NULL));
  49354. #elif defined(WOLFSSL_AES_256)
  49355. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  49356. NULL, NULL));
  49357. #endif
  49358. /* non-default must to set the IV length first */
  49359. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  49360. ivSz, NULL));
  49361. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49362. }
  49363. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49364. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49365. ciphertxtSz));
  49366. decryptedtxtSz = len;
  49367. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  49368. AES_BLOCK_SIZE, tag));
  49369. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49370. decryptedtxtSz += len;
  49371. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49372. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49373. /* modify tag*/
  49374. if (i == 0) {
  49375. /* Default uses 96-bits IV length */
  49376. #ifdef WOLFSSL_AES_128
  49377. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  49378. key, iv));
  49379. #elif defined(WOLFSSL_AES_192)
  49380. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  49381. key, iv));
  49382. #elif defined(WOLFSSL_AES_256)
  49383. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  49384. key, iv));
  49385. #endif
  49386. }
  49387. else {
  49388. #ifdef WOLFSSL_AES_128
  49389. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  49390. NULL, NULL));
  49391. #elif defined(WOLFSSL_AES_192)
  49392. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  49393. NULL, NULL));
  49394. #elif defined(WOLFSSL_AES_256)
  49395. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  49396. NULL, NULL));
  49397. #endif
  49398. /* non-default must to set the IV length first */
  49399. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  49400. ivSz, NULL));
  49401. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49402. }
  49403. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49404. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49405. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  49406. AES_BLOCK_SIZE, tag));
  49407. /* fail due to wrong tag */
  49408. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49409. ciphertxtSz));
  49410. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49411. ExpectIntEQ(0, len);
  49412. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49413. }
  49414. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  49415. return EXPECT_RESULT();
  49416. }
  49417. static int test_wolfssl_EVP_aria_gcm(void)
  49418. {
  49419. int res = TEST_SKIPPED;
  49420. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  49421. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49422. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49423. byte *key = (byte*)"01234567890123456789012345678901";
  49424. /* A 128 bit IV */
  49425. byte *iv = (byte*)"0123456789012345";
  49426. int ivSz = ARIA_BLOCK_SIZE;
  49427. /* Message to be encrypted */
  49428. const int plaintxtSz = 40;
  49429. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  49430. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  49431. /* Additional non-confidential data */
  49432. byte *aad = (byte*)"Don't spend major time on minor things.";
  49433. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  49434. int aadSz = (int)XSTRLEN((char*)aad);
  49435. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  49436. byte decryptedtxt[plaintxtSz];
  49437. int ciphertxtSz = 0;
  49438. int decryptedtxtSz = 0;
  49439. int len = 0;
  49440. int i = 0;
  49441. #define TEST_ARIA_GCM_COUNT 6
  49442. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  49443. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  49444. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  49445. EVP_CIPHER_CTX_init(&en[i]);
  49446. switch (i) {
  49447. case 0:
  49448. /* Default uses 96-bits IV length */
  49449. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  49450. break;
  49451. case 1:
  49452. /* Default uses 96-bits IV length */
  49453. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  49454. break;
  49455. case 2:
  49456. /* Default uses 96-bits IV length */
  49457. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  49458. break;
  49459. case 3:
  49460. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  49461. /* non-default must to set the IV length first */
  49462. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49463. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49464. break;
  49465. case 4:
  49466. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  49467. /* non-default must to set the IV length first */
  49468. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49469. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49470. break;
  49471. case 5:
  49472. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  49473. /* non-default must to set the IV length first */
  49474. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49475. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49476. break;
  49477. }
  49478. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  49479. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49480. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  49481. ciphertxtSz = len;
  49482. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49483. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  49484. ciphertxtSz += len;
  49485. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  49486. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  49487. EVP_CIPHER_CTX_init(&de[i]);
  49488. switch (i) {
  49489. case 0:
  49490. /* Default uses 96-bits IV length */
  49491. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  49492. break;
  49493. case 1:
  49494. /* Default uses 96-bits IV length */
  49495. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  49496. break;
  49497. case 2:
  49498. /* Default uses 96-bits IV length */
  49499. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  49500. break;
  49501. case 3:
  49502. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  49503. /* non-default must to set the IV length first */
  49504. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49505. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49506. break;
  49507. case 4:
  49508. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  49509. /* non-default must to set the IV length first */
  49510. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49511. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49512. break;
  49513. case 5:
  49514. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  49515. /* non-default must to set the IV length first */
  49516. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49517. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49518. break;
  49519. }
  49520. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  49521. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49522. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  49523. decryptedtxtSz = len;
  49524. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  49525. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49526. decryptedtxtSz += len;
  49527. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  49528. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49529. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  49530. /* modify tag*/
  49531. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49532. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49533. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  49534. /* fail due to wrong tag */
  49535. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  49536. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49537. AssertIntEQ(0, len);
  49538. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  49539. }
  49540. res = TEST_RES_CHECK(1);
  49541. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  49542. return res;
  49543. }
  49544. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  49545. {
  49546. EXPECT_DECLS;
  49547. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  49548. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49549. /* Zero length plain text */
  49550. byte key[] = {
  49551. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49552. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49553. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49554. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49555. }; /* align */
  49556. byte iv[] = {
  49557. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49558. }; /* align */
  49559. byte plaintxt[1];
  49560. int ivSz = 12;
  49561. int plaintxtSz = 0;
  49562. unsigned char tag[16];
  49563. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49564. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49565. int ciphertxtSz = 0;
  49566. int decryptedtxtSz = 0;
  49567. int len = 0;
  49568. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49569. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49570. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  49571. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49572. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49573. plaintxtSz));
  49574. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49575. ciphertxtSz += len;
  49576. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  49577. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49578. ExpectIntEQ(0, ciphertxtSz);
  49579. EVP_CIPHER_CTX_init(de);
  49580. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  49581. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  49582. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49583. decryptedtxtSz = len;
  49584. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  49585. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49586. decryptedtxtSz += len;
  49587. ExpectIntEQ(0, decryptedtxtSz);
  49588. EVP_CIPHER_CTX_free(en);
  49589. EVP_CIPHER_CTX_free(de);
  49590. #endif
  49591. return EXPECT_RESULT();
  49592. }
  49593. static int test_wolfssl_EVP_aes_ccm(void)
  49594. {
  49595. EXPECT_DECLS;
  49596. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  49597. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49598. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49599. byte *key = (byte*)"01234567890123456789012345678901";
  49600. /* A 128 bit IV */
  49601. byte *iv = (byte*)"0123456789012";
  49602. int ivSz = (int)XSTRLEN((char*)iv);
  49603. /* Message to be encrypted */
  49604. byte *plaintxt = (byte*)"for things to change you have to change";
  49605. /* Additional non-confidential data */
  49606. byte *aad = (byte*)"Don't spend major time on minor things.";
  49607. unsigned char tag[AES_BLOCK_SIZE] = {0};
  49608. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49609. int aadSz = (int)XSTRLEN((char*)aad);
  49610. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49611. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49612. int ciphertxtSz = 0;
  49613. int decryptedtxtSz = 0;
  49614. int len = 0;
  49615. int i = 0;
  49616. int ret;
  49617. EVP_CIPHER_CTX en[2];
  49618. EVP_CIPHER_CTX de[2];
  49619. for (i = 0; i < 2; i++) {
  49620. EVP_CIPHER_CTX_init(&en[i]);
  49621. if (i == 0) {
  49622. /* Default uses 96-bits IV length */
  49623. #ifdef WOLFSSL_AES_128
  49624. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  49625. key, iv));
  49626. #elif defined(WOLFSSL_AES_192)
  49627. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  49628. key, iv));
  49629. #elif defined(WOLFSSL_AES_256)
  49630. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  49631. key, iv));
  49632. #endif
  49633. }
  49634. else {
  49635. #ifdef WOLFSSL_AES_128
  49636. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  49637. NULL, NULL));
  49638. #elif defined(WOLFSSL_AES_192)
  49639. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  49640. NULL, NULL));
  49641. #elif defined(WOLFSSL_AES_256)
  49642. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  49643. NULL, NULL));
  49644. #endif
  49645. /* non-default must to set the IV length first */
  49646. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  49647. ivSz, NULL));
  49648. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49649. }
  49650. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49651. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49652. plaintxtSz));
  49653. ciphertxtSz = len;
  49654. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  49655. ciphertxtSz += len;
  49656. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  49657. AES_BLOCK_SIZE, tag));
  49658. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  49659. ExpectIntEQ(ret, 1);
  49660. EVP_CIPHER_CTX_init(&de[i]);
  49661. if (i == 0) {
  49662. /* Default uses 96-bits IV length */
  49663. #ifdef WOLFSSL_AES_128
  49664. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  49665. key, iv));
  49666. #elif defined(WOLFSSL_AES_192)
  49667. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  49668. key, iv));
  49669. #elif defined(WOLFSSL_AES_256)
  49670. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  49671. key, iv));
  49672. #endif
  49673. }
  49674. else {
  49675. #ifdef WOLFSSL_AES_128
  49676. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  49677. NULL, NULL));
  49678. #elif defined(WOLFSSL_AES_192)
  49679. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  49680. NULL, NULL));
  49681. #elif defined(WOLFSSL_AES_256)
  49682. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  49683. NULL, NULL));
  49684. #endif
  49685. /* non-default must to set the IV length first */
  49686. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  49687. ivSz, NULL));
  49688. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  49689. }
  49690. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49691. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49692. ciphertxtSz));
  49693. decryptedtxtSz = len;
  49694. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49695. AES_BLOCK_SIZE, tag));
  49696. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49697. decryptedtxtSz += len;
  49698. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  49699. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  49700. /* modify tag*/
  49701. tag[AES_BLOCK_SIZE-1]+=0xBB;
  49702. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  49703. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  49704. AES_BLOCK_SIZE, tag));
  49705. /* fail due to wrong tag */
  49706. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  49707. ciphertxtSz));
  49708. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  49709. ExpectIntEQ(0, len);
  49710. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  49711. ExpectIntEQ(ret, 1);
  49712. }
  49713. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  49714. return EXPECT_RESULT();
  49715. }
  49716. static int test_wolfssl_EVP_chacha20_poly1305(void)
  49717. {
  49718. EXPECT_DECLS;
  49719. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  49720. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  49721. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  49722. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49723. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  49724. byte cipherText[sizeof(plainText)];
  49725. byte decryptedText[sizeof(plainText)];
  49726. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  49727. EVP_CIPHER_CTX* ctx = NULL;
  49728. int outSz;
  49729. /* Encrypt. */
  49730. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49731. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  49732. NULL), WOLFSSL_SUCCESS);
  49733. /* Invalid IV length. */
  49734. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49735. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  49736. /* Valid IV length. */
  49737. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49738. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  49739. /* Invalid tag length. */
  49740. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49741. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  49742. /* Valid tag length. */
  49743. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49744. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  49745. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49746. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  49747. WOLFSSL_SUCCESS);
  49748. ExpectIntEQ(outSz, sizeof(aad));
  49749. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49750. sizeof(plainText)), WOLFSSL_SUCCESS);
  49751. ExpectIntEQ(outSz, sizeof(plainText));
  49752. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49753. ExpectIntEQ(outSz, 0);
  49754. /* Invalid tag length. */
  49755. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  49756. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  49757. /* Valid tag length. */
  49758. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  49759. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  49760. EVP_CIPHER_CTX_free(ctx);
  49761. ctx = NULL;
  49762. /* Decrypt. */
  49763. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49764. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  49765. NULL), WOLFSSL_SUCCESS);
  49766. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  49767. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  49768. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49769. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  49770. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49771. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  49772. WOLFSSL_SUCCESS);
  49773. ExpectIntEQ(outSz, sizeof(aad));
  49774. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49775. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49776. ExpectIntEQ(outSz, sizeof(cipherText));
  49777. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49778. WOLFSSL_SUCCESS);
  49779. ExpectIntEQ(outSz, 0);
  49780. EVP_CIPHER_CTX_free(ctx);
  49781. ctx = NULL;
  49782. /* Test partial Inits. CipherInit() allow setting of key and iv
  49783. * in separate calls. */
  49784. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49785. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  49786. key, NULL, 1), WOLFSSL_SUCCESS);
  49787. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49788. WOLFSSL_SUCCESS);
  49789. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  49790. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  49791. ExpectIntEQ(outSz, sizeof(aad));
  49792. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49793. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49794. ExpectIntEQ(outSz, sizeof(cipherText));
  49795. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49796. WOLFSSL_SUCCESS);
  49797. ExpectIntEQ(outSz, 0);
  49798. EVP_CIPHER_CTX_free(ctx);
  49799. #endif
  49800. return EXPECT_RESULT();
  49801. }
  49802. static int test_wolfssl_EVP_chacha20(void)
  49803. {
  49804. EXPECT_DECLS;
  49805. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  49806. byte key[CHACHA_MAX_KEY_SZ];
  49807. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  49808. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49809. byte cipherText[sizeof(plainText)];
  49810. byte decryptedText[sizeof(plainText)];
  49811. EVP_CIPHER_CTX* ctx = NULL;
  49812. int outSz;
  49813. XMEMSET(key, 0, sizeof(key));
  49814. XMEMSET(iv, 0, sizeof(iv));
  49815. /* Encrypt. */
  49816. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49817. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  49818. NULL), WOLFSSL_SUCCESS);
  49819. /* Any tag length must fail - not an AEAD cipher. */
  49820. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  49821. 16, NULL), WOLFSSL_FAILURE);
  49822. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49823. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49824. sizeof(plainText)), WOLFSSL_SUCCESS);
  49825. ExpectIntEQ(outSz, sizeof(plainText));
  49826. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  49827. ExpectIntEQ(outSz, 0);
  49828. EVP_CIPHER_CTX_free(ctx);
  49829. ctx = NULL;
  49830. /* Decrypt. */
  49831. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49832. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  49833. NULL), WOLFSSL_SUCCESS);
  49834. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49835. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49836. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49837. ExpectIntEQ(outSz, sizeof(cipherText));
  49838. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49839. WOLFSSL_SUCCESS);
  49840. ExpectIntEQ(outSz, 0);
  49841. EVP_CIPHER_CTX_free(ctx);
  49842. ctx = NULL;
  49843. /* Test partial Inits. CipherInit() allow setting of key and iv
  49844. * in separate calls. */
  49845. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49846. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  49847. key, NULL, 1), WOLFSSL_SUCCESS);
  49848. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  49849. WOLFSSL_SUCCESS);
  49850. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49851. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49852. ExpectIntEQ(outSz, sizeof(cipherText));
  49853. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  49854. WOLFSSL_SUCCESS);
  49855. ExpectIntEQ(outSz, 0);
  49856. EVP_CIPHER_CTX_free(ctx);
  49857. #endif
  49858. return EXPECT_RESULT();
  49859. }
  49860. static int test_wolfssl_EVP_sm4_ecb(void)
  49861. {
  49862. int res = TEST_SKIPPED;
  49863. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  49864. EXPECT_DECLS;
  49865. byte key[SM4_KEY_SIZE];
  49866. byte plainText[SM4_BLOCK_SIZE] = {
  49867. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  49868. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  49869. };
  49870. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49871. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49872. EVP_CIPHER_CTX* ctx;
  49873. int outSz;
  49874. XMEMSET(key, 0, sizeof(key));
  49875. /* Encrypt. */
  49876. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49877. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  49878. WOLFSSL_SUCCESS);
  49879. /* Any tag length must fail - not an AEAD cipher. */
  49880. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49881. WOLFSSL_FAILURE);
  49882. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  49883. WOLFSSL_SUCCESS);
  49884. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49885. sizeof(plainText)), WOLFSSL_SUCCESS);
  49886. ExpectIntEQ(outSz, sizeof(plainText));
  49887. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  49888. WOLFSSL_SUCCESS);
  49889. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  49890. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49891. EVP_CIPHER_CTX_free(ctx);
  49892. /* Decrypt. */
  49893. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49894. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  49895. WOLFSSL_SUCCESS);
  49896. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  49897. WOLFSSL_SUCCESS);
  49898. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49899. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49900. ExpectIntEQ(outSz, sizeof(plainText));
  49901. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49902. WOLFSSL_SUCCESS);
  49903. ExpectIntEQ(outSz, 0);
  49904. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49905. EVP_CIPHER_CTX_free(ctx);
  49906. res = EXPECT_RESULT();
  49907. #endif
  49908. return res;
  49909. }
  49910. static int test_wolfssl_EVP_sm4_cbc(void)
  49911. {
  49912. int res = TEST_SKIPPED;
  49913. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  49914. EXPECT_DECLS;
  49915. byte key[SM4_KEY_SIZE];
  49916. byte iv[SM4_BLOCK_SIZE];
  49917. byte plainText[SM4_BLOCK_SIZE] = {
  49918. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  49919. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  49920. };
  49921. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49922. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  49923. EVP_CIPHER_CTX* ctx;
  49924. int outSz;
  49925. XMEMSET(key, 0, sizeof(key));
  49926. XMEMSET(iv, 0, sizeof(iv));
  49927. /* Encrypt. */
  49928. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49929. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  49930. WOLFSSL_SUCCESS);
  49931. /* Any tag length must fail - not an AEAD cipher. */
  49932. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49933. WOLFSSL_FAILURE);
  49934. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49935. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49936. sizeof(plainText)), WOLFSSL_SUCCESS);
  49937. ExpectIntEQ(outSz, sizeof(plainText));
  49938. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  49939. WOLFSSL_SUCCESS);
  49940. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  49941. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  49942. EVP_CIPHER_CTX_free(ctx);
  49943. /* Decrypt. */
  49944. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49945. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  49946. WOLFSSL_SUCCESS);
  49947. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49948. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49949. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49950. ExpectIntEQ(outSz, sizeof(plainText));
  49951. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49952. WOLFSSL_SUCCESS);
  49953. ExpectIntEQ(outSz, 0);
  49954. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49955. EVP_CIPHER_CTX_free(ctx);
  49956. /* Test partial Inits. CipherInit() allow setting of key and iv
  49957. * in separate calls. */
  49958. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49959. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  49960. WOLFSSL_SUCCESS);
  49961. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  49962. WOLFSSL_SUCCESS);
  49963. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  49964. sizeof(cipherText)), WOLFSSL_SUCCESS);
  49965. ExpectIntEQ(outSz, sizeof(plainText));
  49966. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  49967. WOLFSSL_SUCCESS);
  49968. ExpectIntEQ(outSz, 0);
  49969. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  49970. EVP_CIPHER_CTX_free(ctx);
  49971. res = EXPECT_RESULT();
  49972. #endif
  49973. return res;
  49974. }
  49975. static int test_wolfssl_EVP_sm4_ctr(void)
  49976. {
  49977. int res = TEST_SKIPPED;
  49978. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  49979. EXPECT_DECLS;
  49980. byte key[SM4_KEY_SIZE];
  49981. byte iv[SM4_BLOCK_SIZE];
  49982. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  49983. byte cipherText[sizeof(plainText)];
  49984. byte decryptedText[sizeof(plainText)];
  49985. EVP_CIPHER_CTX* ctx;
  49986. int outSz;
  49987. XMEMSET(key, 0, sizeof(key));
  49988. XMEMSET(iv, 0, sizeof(iv));
  49989. /* Encrypt. */
  49990. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  49991. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  49992. WOLFSSL_SUCCESS);
  49993. /* Any tag length must fail - not an AEAD cipher. */
  49994. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  49995. WOLFSSL_FAILURE);
  49996. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  49997. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  49998. sizeof(plainText)), WOLFSSL_SUCCESS);
  49999. ExpectIntEQ(outSz, sizeof(plainText));
  50000. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50001. ExpectIntEQ(outSz, 0);
  50002. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50003. EVP_CIPHER_CTX_free(ctx);
  50004. /* Decrypt. */
  50005. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50006. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50007. WOLFSSL_SUCCESS);
  50008. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50009. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50010. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50011. ExpectIntEQ(outSz, sizeof(cipherText));
  50012. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50013. WOLFSSL_SUCCESS);
  50014. ExpectIntEQ(outSz, 0);
  50015. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50016. EVP_CIPHER_CTX_free(ctx);
  50017. /* Test partial Inits. CipherInit() allow setting of key and iv
  50018. * in separate calls. */
  50019. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50020. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  50021. WOLFSSL_SUCCESS);
  50022. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50023. WOLFSSL_SUCCESS);
  50024. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50025. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50026. ExpectIntEQ(outSz, sizeof(cipherText));
  50027. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50028. WOLFSSL_SUCCESS);
  50029. ExpectIntEQ(outSz, 0);
  50030. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50031. EVP_CIPHER_CTX_free(ctx);
  50032. res = EXPECT_RESULT();
  50033. #endif
  50034. return res;
  50035. }
  50036. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  50037. {
  50038. int res = TEST_SKIPPED;
  50039. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50040. /* Zero length plain text */
  50041. EXPECT_DECLS;
  50042. byte key[] = {
  50043. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50047. }; /* align */
  50048. byte iv[] = {
  50049. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50050. }; /* align */
  50051. byte plaintxt[1];
  50052. int ivSz = 12;
  50053. int plaintxtSz = 0;
  50054. unsigned char tag[16];
  50055. unsigned char tag_kat[16] = {
  50056. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  50057. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  50058. };
  50059. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50060. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50061. int ciphertxtSz = 0;
  50062. int decryptedtxtSz = 0;
  50063. int len = 0;
  50064. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50065. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50066. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  50067. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50068. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50069. plaintxtSz));
  50070. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50071. ciphertxtSz += len;
  50072. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50073. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50074. ExpectIntEQ(0, ciphertxtSz);
  50075. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50076. EVP_CIPHER_CTX_init(de);
  50077. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  50078. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50079. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50080. decryptedtxtSz = len;
  50081. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50082. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50083. decryptedtxtSz += len;
  50084. ExpectIntEQ(0, decryptedtxtSz);
  50085. EVP_CIPHER_CTX_free(en);
  50086. EVP_CIPHER_CTX_free(de);
  50087. res = EXPECT_RESULT();
  50088. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50089. return res;
  50090. }
  50091. static int test_wolfssl_EVP_sm4_gcm(void)
  50092. {
  50093. int res = TEST_SKIPPED;
  50094. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50095. EXPECT_DECLS;
  50096. byte *key = (byte*)"0123456789012345";
  50097. /* A 128 bit IV */
  50098. byte *iv = (byte*)"0123456789012345";
  50099. int ivSz = SM4_BLOCK_SIZE;
  50100. /* Message to be encrypted */
  50101. byte *plaintxt = (byte*)"for things to change you have to change";
  50102. /* Additional non-confidential data */
  50103. byte *aad = (byte*)"Don't spend major time on minor things.";
  50104. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50105. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50106. int aadSz = (int)XSTRLEN((char*)aad);
  50107. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50108. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50109. int ciphertxtSz = 0;
  50110. int decryptedtxtSz = 0;
  50111. int len = 0;
  50112. int i = 0;
  50113. EVP_CIPHER_CTX en[2];
  50114. EVP_CIPHER_CTX de[2];
  50115. for (i = 0; i < 2; i++) {
  50116. EVP_CIPHER_CTX_init(&en[i]);
  50117. if (i == 0) {
  50118. /* Default uses 96-bits IV length */
  50119. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  50120. iv));
  50121. }
  50122. else {
  50123. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  50124. NULL));
  50125. /* non-default must to set the IV length first */
  50126. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50127. ivSz, NULL));
  50128. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50129. }
  50130. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50131. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50132. plaintxtSz));
  50133. ciphertxtSz = len;
  50134. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50135. ciphertxtSz += len;
  50136. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50137. SM4_BLOCK_SIZE, tag));
  50138. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50139. EVP_CIPHER_CTX_init(&de[i]);
  50140. if (i == 0) {
  50141. /* Default uses 96-bits IV length */
  50142. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  50143. iv));
  50144. }
  50145. else {
  50146. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  50147. NULL));
  50148. /* non-default must to set the IV length first */
  50149. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50150. ivSz, NULL));
  50151. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50152. }
  50153. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50154. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50155. ciphertxtSz));
  50156. decryptedtxtSz = len;
  50157. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50158. SM4_BLOCK_SIZE, tag));
  50159. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50160. decryptedtxtSz += len;
  50161. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50162. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50163. /* modify tag*/
  50164. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50165. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50166. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50167. SM4_BLOCK_SIZE, tag));
  50168. /* fail due to wrong tag */
  50169. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50170. ciphertxtSz));
  50171. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50172. ExpectIntEQ(0, len);
  50173. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50174. }
  50175. res = EXPECT_RESULT();
  50176. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50177. return res;
  50178. }
  50179. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  50180. {
  50181. int res = TEST_SKIPPED;
  50182. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50183. /* Zero length plain text */
  50184. EXPECT_DECLS;
  50185. byte key[] = {
  50186. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50187. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50188. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50189. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50190. }; /* align */
  50191. byte iv[] = {
  50192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50193. }; /* align */
  50194. byte plaintxt[1];
  50195. int ivSz = 12;
  50196. int plaintxtSz = 0;
  50197. unsigned char tag[16];
  50198. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50199. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50200. int ciphertxtSz = 0;
  50201. int decryptedtxtSz = 0;
  50202. int len = 0;
  50203. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50204. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50205. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  50206. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50207. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50208. plaintxtSz));
  50209. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50210. ciphertxtSz += len;
  50211. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50212. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50213. ExpectIntEQ(0, ciphertxtSz);
  50214. EVP_CIPHER_CTX_init(de);
  50215. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  50216. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50217. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50218. decryptedtxtSz = len;
  50219. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50220. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50221. decryptedtxtSz += len;
  50222. ExpectIntEQ(0, decryptedtxtSz);
  50223. EVP_CIPHER_CTX_free(en);
  50224. EVP_CIPHER_CTX_free(de);
  50225. res = EXPECT_RESULT();
  50226. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50227. return res;
  50228. }
  50229. static int test_wolfssl_EVP_sm4_ccm(void)
  50230. {
  50231. int res = TEST_SKIPPED;
  50232. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50233. EXPECT_DECLS;
  50234. byte *key = (byte*)"0123456789012345";
  50235. byte *iv = (byte*)"0123456789012";
  50236. int ivSz = (int)XSTRLEN((char*)iv);
  50237. /* Message to be encrypted */
  50238. byte *plaintxt = (byte*)"for things to change you have to change";
  50239. /* Additional non-confidential data */
  50240. byte *aad = (byte*)"Don't spend major time on minor things.";
  50241. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50242. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50243. int aadSz = (int)XSTRLEN((char*)aad);
  50244. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50245. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50246. int ciphertxtSz = 0;
  50247. int decryptedtxtSz = 0;
  50248. int len = 0;
  50249. int i = 0;
  50250. EVP_CIPHER_CTX en[2];
  50251. EVP_CIPHER_CTX de[2];
  50252. for (i = 0; i < 2; i++) {
  50253. EVP_CIPHER_CTX_init(&en[i]);
  50254. if (i == 0) {
  50255. /* Default uses 96-bits IV length */
  50256. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  50257. iv));
  50258. }
  50259. else {
  50260. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  50261. NULL));
  50262. /* non-default must to set the IV length first */
  50263. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50264. ivSz, NULL));
  50265. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50266. }
  50267. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50268. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50269. plaintxtSz));
  50270. ciphertxtSz = len;
  50271. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50272. ciphertxtSz += len;
  50273. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50274. SM4_BLOCK_SIZE, tag));
  50275. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50276. EVP_CIPHER_CTX_init(&de[i]);
  50277. if (i == 0) {
  50278. /* Default uses 96-bits IV length */
  50279. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  50280. iv));
  50281. }
  50282. else {
  50283. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  50284. NULL));
  50285. /* non-default must to set the IV length first */
  50286. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50287. ivSz, NULL));
  50288. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50289. }
  50290. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50291. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50292. ciphertxtSz));
  50293. decryptedtxtSz = len;
  50294. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50295. SM4_BLOCK_SIZE, tag));
  50296. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50297. decryptedtxtSz += len;
  50298. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50299. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50300. /* modify tag*/
  50301. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50302. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50303. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50304. SM4_BLOCK_SIZE, tag));
  50305. /* fail due to wrong tag */
  50306. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50307. ciphertxtSz));
  50308. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50309. ExpectIntEQ(0, len);
  50310. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50311. }
  50312. res = EXPECT_RESULT();
  50313. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50314. return res;
  50315. }
  50316. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  50317. {
  50318. EXPECT_DECLS;
  50319. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  50320. EVP_PKEY_CTX* ctx = NULL;
  50321. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  50322. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  50323. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  50324. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  50325. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  50326. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  50327. byte outKey[34];
  50328. size_t outKeySz = sizeof(outKey);
  50329. /* These expected outputs were gathered by running the same test below using
  50330. * OpenSSL. */
  50331. const byte extractAndExpand[] = {
  50332. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  50333. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  50334. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  50335. };
  50336. const byte extractOnly[] = {
  50337. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  50338. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  50339. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  50340. };
  50341. const byte expandOnly[] = {
  50342. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  50343. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  50344. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  50345. };
  50346. const byte extractAndExpandAddInfo[] = {
  50347. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  50348. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  50349. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  50350. };
  50351. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  50352. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  50353. /* NULL ctx. */
  50354. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  50355. /* NULL md. */
  50356. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  50357. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  50358. /* NULL ctx. */
  50359. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  50360. WOLFSSL_FAILURE);
  50361. /* NULL salt is ok. */
  50362. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  50363. WOLFSSL_SUCCESS);
  50364. /* Salt length <= 0. */
  50365. /* Length 0 salt is ok. */
  50366. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  50367. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  50368. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  50369. WOLFSSL_SUCCESS);
  50370. /* NULL ctx. */
  50371. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  50372. WOLFSSL_FAILURE);
  50373. /* NULL key. */
  50374. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  50375. WOLFSSL_FAILURE);
  50376. /* Key length <= 0 */
  50377. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  50378. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  50379. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  50380. WOLFSSL_SUCCESS);
  50381. /* NULL ctx. */
  50382. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  50383. WOLFSSL_FAILURE);
  50384. /* NULL info is ok. */
  50385. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  50386. WOLFSSL_SUCCESS);
  50387. /* Info length <= 0 */
  50388. /* Length 0 info is ok. */
  50389. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  50390. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  50391. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  50392. WOLFSSL_SUCCESS);
  50393. /* NULL ctx. */
  50394. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  50395. WOLFSSL_FAILURE);
  50396. /* Extract and expand (default). */
  50397. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  50398. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  50399. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  50400. /* Extract only. */
  50401. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  50402. WOLFSSL_SUCCESS);
  50403. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  50404. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  50405. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  50406. outKeySz = sizeof(outKey);
  50407. /* Expand only. */
  50408. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  50409. WOLFSSL_SUCCESS);
  50410. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  50411. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  50412. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  50413. outKeySz = sizeof(outKey);
  50414. /* Extract and expand with appended additional info. */
  50415. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  50416. WOLFSSL_SUCCESS);
  50417. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  50418. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  50419. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  50420. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  50421. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  50422. EVP_PKEY_CTX_free(ctx);
  50423. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  50424. return EXPECT_RESULT();
  50425. }
  50426. #ifndef NO_BIO
  50427. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  50428. {
  50429. EXPECT_DECLS;
  50430. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  50431. BIO* bio = NULL;
  50432. X509_INFO* info = NULL;
  50433. STACK_OF(X509_INFO)* sk = NULL;
  50434. char* subject = NULL;
  50435. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  50436. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  50437. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  50438. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  50439. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  50440. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  50441. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  50442. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  50443. /* using dereference to maintain testing for Apache port*/
  50444. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  50445. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  50446. 0, 0));
  50447. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  50448. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  50449. X509_INFO_free(info);
  50450. info = NULL;
  50451. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  50452. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  50453. 0, 0));
  50454. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  50455. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  50456. X509_INFO_free(info);
  50457. ExpectNull(info = sk_X509_INFO_pop(sk));
  50458. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  50459. BIO_free(bio);
  50460. #endif
  50461. return EXPECT_RESULT();
  50462. }
  50463. #endif /* !NO_BIO */
  50464. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  50465. {
  50466. EXPECT_DECLS;
  50467. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  50468. X509 *x509 = NULL;
  50469. X509_NAME* name = NULL;
  50470. int idx = 0;
  50471. X509_NAME_ENTRY *ne = NULL;
  50472. ASN1_OBJECT *object = NULL;
  50473. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  50474. WOLFSSL_FILETYPE_PEM));
  50475. ExpectNotNull(name = X509_get_subject_name(x509));
  50476. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  50477. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  50478. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  50479. X509_free(x509);
  50480. #endif
  50481. return EXPECT_RESULT();
  50482. }
  50483. static int test_wolfSSL_X509_STORE_get1_certs(void)
  50484. {
  50485. EXPECT_DECLS;
  50486. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  50487. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  50488. X509_STORE_CTX *storeCtx = NULL;
  50489. X509_STORE *store = NULL;
  50490. X509 *caX509 = NULL;
  50491. X509 *svrX509 = NULL;
  50492. X509_NAME *subject = NULL;
  50493. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  50494. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  50495. SSL_FILETYPE_PEM));
  50496. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  50497. SSL_FILETYPE_PEM)));
  50498. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  50499. ExpectNotNull(store = X509_STORE_new());
  50500. ExpectNotNull(subject = X509_get_subject_name(caX509));
  50501. /* Errors */
  50502. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  50503. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  50504. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  50505. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  50506. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  50507. SSL_SUCCESS);
  50508. /* Should find the cert */
  50509. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  50510. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  50511. sk_X509_pop_free(certs, NULL);
  50512. certs = NULL;
  50513. /* Should not find the cert */
  50514. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  50515. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  50516. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  50517. sk_X509_pop_free(certs, NULL);
  50518. certs = NULL;
  50519. X509_STORE_free(store);
  50520. X509_STORE_CTX_free(storeCtx);
  50521. X509_free(svrX509);
  50522. X509_free(caX509);
  50523. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  50524. return EXPECT_RESULT();
  50525. }
  50526. static int test_wolfSSL_dup_CA_list(void)
  50527. {
  50528. int res = TEST_SKIPPED;
  50529. #if defined(OPENSSL_ALL)
  50530. EXPECT_DECLS;
  50531. STACK_OF(X509_NAME) *originalStack = NULL;
  50532. STACK_OF(X509_NAME) *copyStack = NULL;
  50533. int originalCount = 0;
  50534. int copyCount = 0;
  50535. X509_NAME *name = NULL;
  50536. int i;
  50537. originalStack = sk_X509_NAME_new_null();
  50538. ExpectNotNull(originalStack);
  50539. for (i = 0; i < 3; i++) {
  50540. name = X509_NAME_new();
  50541. ExpectNotNull(name);
  50542. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  50543. }
  50544. copyStack = SSL_dup_CA_list(originalStack);
  50545. ExpectNotNull(copyStack);
  50546. originalCount = sk_X509_NAME_num(originalStack);
  50547. copyCount = sk_X509_NAME_num(copyStack);
  50548. AssertIntEQ(originalCount, copyCount);
  50549. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  50550. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  50551. originalStack = NULL;
  50552. copyStack = NULL;
  50553. res = EXPECT_RESULT();
  50554. #endif /* OPENSSL_ALL */
  50555. return res;
  50556. }
  50557. static int test_ForceZero(void)
  50558. {
  50559. EXPECT_DECLS;
  50560. unsigned char data[32];
  50561. unsigned int i, j, len;
  50562. /* Test case with 0 length */
  50563. ForceZero(data, 0);
  50564. /* Test ForceZero */
  50565. for (i = 0; i < sizeof(data); i++) {
  50566. for (len = 1; len < sizeof(data) - i; len++) {
  50567. for (j = 0; j < sizeof(data); j++)
  50568. data[j] = j + 1;
  50569. ForceZero(data + i, len);
  50570. for (j = 0; j < sizeof(data); j++) {
  50571. if (j < i || j >= i + len) {
  50572. ExpectIntNE(data[j], 0x00);
  50573. }
  50574. else {
  50575. ExpectIntEQ(data[j], 0x00);
  50576. }
  50577. }
  50578. }
  50579. }
  50580. return EXPECT_RESULT();
  50581. }
  50582. #ifndef NO_BIO
  50583. static int test_wolfSSL_X509_print(void)
  50584. {
  50585. EXPECT_DECLS;
  50586. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50587. !defined(NO_RSA) && defined(XSNPRINTF)
  50588. X509 *x509 = NULL;
  50589. BIO *bio = NULL;
  50590. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  50591. const X509_ALGOR *cert_sig_alg = NULL;
  50592. #endif
  50593. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  50594. WOLFSSL_FILETYPE_PEM));
  50595. /* print to memory */
  50596. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  50597. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  50598. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  50599. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  50600. /* Will print IP address subject alt name. */
  50601. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  50602. #elif defined(NO_ASN_TIME)
  50603. /* Will print IP address subject alt name but not Validity. */
  50604. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  50605. #else
  50606. /* Will print IP address subject alt name. */
  50607. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  50608. #endif
  50609. #elif defined(NO_ASN_TIME)
  50610. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  50611. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  50612. #else
  50613. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  50614. #endif
  50615. BIO_free(bio);
  50616. bio = NULL;
  50617. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50618. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  50619. /* Print signature */
  50620. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  50621. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  50622. #endif
  50623. /* print to stderr */
  50624. #if !defined(NO_WOLFSSL_DIR)
  50625. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  50626. #endif
  50627. /* print again */
  50628. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  50629. X509_free(x509);
  50630. BIO_free(bio);
  50631. #endif
  50632. return EXPECT_RESULT();
  50633. }
  50634. static int test_wolfSSL_X509_CRL_print(void)
  50635. {
  50636. EXPECT_DECLS;
  50637. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  50638. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  50639. X509_CRL* crl = NULL;
  50640. BIO *bio = NULL;
  50641. XFILE fp = XBADFILE;
  50642. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  50643. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50644. NULL, NULL));
  50645. if (fp != XBADFILE)
  50646. XFCLOSE(fp);
  50647. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  50648. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  50649. X509_CRL_free(crl);
  50650. BIO_free(bio);
  50651. #endif
  50652. return EXPECT_RESULT();
  50653. }
  50654. static int test_wolfSSL_BIO_get_len(void)
  50655. {
  50656. EXPECT_DECLS;
  50657. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  50658. BIO *bio = NULL;
  50659. const char txt[] = "Some example text to push to the BIO.";
  50660. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  50661. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50662. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  50663. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  50664. BIO_free(bio);
  50665. bio = NULL;
  50666. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50667. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  50668. BIO_free(bio);
  50669. #endif
  50670. return EXPECT_RESULT();
  50671. }
  50672. #endif /* !NO_BIO */
  50673. static int test_wolfSSL_RSA(void)
  50674. {
  50675. EXPECT_DECLS;
  50676. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  50677. RSA* rsa = NULL;
  50678. const BIGNUM *n = NULL;
  50679. const BIGNUM *e = NULL;
  50680. const BIGNUM *d = NULL;
  50681. const BIGNUM *p = NULL;
  50682. const BIGNUM *q = NULL;
  50683. const BIGNUM *dmp1 = NULL;
  50684. const BIGNUM *dmq1 = NULL;
  50685. const BIGNUM *iqmp = NULL;
  50686. ExpectNotNull(rsa = RSA_new());
  50687. ExpectIntEQ(RSA_size(NULL), 0);
  50688. ExpectIntEQ(RSA_size(rsa), 0);
  50689. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  50690. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  50691. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  50692. #ifdef WOLFSSL_RSA_KEY_CHECK
  50693. ExpectIntEQ(RSA_check_key(rsa), 0);
  50694. #endif
  50695. RSA_free(rsa);
  50696. rsa = NULL;
  50697. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50698. ExpectIntEQ(RSA_size(rsa), 256);
  50699. #ifdef WOLFSSL_RSA_KEY_CHECK
  50700. ExpectIntEQ(RSA_check_key(NULL), 0);
  50701. ExpectIntEQ(RSA_check_key(rsa), 1);
  50702. #endif
  50703. /* sanity check */
  50704. ExpectIntEQ(RSA_bits(NULL), 0);
  50705. /* key */
  50706. ExpectIntEQ(RSA_bits(rsa), 2048);
  50707. RSA_get0_key(rsa, &n, &e, &d);
  50708. ExpectPtrEq(rsa->n, n);
  50709. ExpectPtrEq(rsa->e, e);
  50710. ExpectPtrEq(rsa->d, d);
  50711. n = NULL;
  50712. e = NULL;
  50713. d = NULL;
  50714. ExpectNotNull(n = BN_new());
  50715. ExpectNotNull(e = BN_new());
  50716. ExpectNotNull(d = BN_new());
  50717. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  50718. if (EXPECT_FAIL()) {
  50719. BN_free((BIGNUM*)n);
  50720. BN_free((BIGNUM*)e);
  50721. BN_free((BIGNUM*)d);
  50722. }
  50723. ExpectPtrEq(rsa->n, n);
  50724. ExpectPtrEq(rsa->e, e);
  50725. ExpectPtrEq(rsa->d, d);
  50726. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  50727. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  50728. /* crt_params */
  50729. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  50730. ExpectPtrEq(rsa->dmp1, dmp1);
  50731. ExpectPtrEq(rsa->dmq1, dmq1);
  50732. ExpectPtrEq(rsa->iqmp, iqmp);
  50733. dmp1 = NULL;
  50734. dmq1 = NULL;
  50735. iqmp = NULL;
  50736. ExpectNotNull(dmp1 = BN_new());
  50737. ExpectNotNull(dmq1 = BN_new());
  50738. ExpectNotNull(iqmp = BN_new());
  50739. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  50740. (BIGNUM*)iqmp), 1);
  50741. if (EXPECT_FAIL()) {
  50742. BN_free((BIGNUM*)dmp1);
  50743. BN_free((BIGNUM*)dmq1);
  50744. BN_free((BIGNUM*)iqmp);
  50745. }
  50746. ExpectPtrEq(rsa->dmp1, dmp1);
  50747. ExpectPtrEq(rsa->dmq1, dmq1);
  50748. ExpectPtrEq(rsa->iqmp, iqmp);
  50749. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  50750. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  50751. (BIGNUM*)iqmp), 0);
  50752. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  50753. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  50754. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  50755. ExpectNull(dmp1);
  50756. ExpectNull(dmq1);
  50757. ExpectNull(iqmp);
  50758. /* factors */
  50759. RSA_get0_factors(rsa, NULL, NULL);
  50760. RSA_get0_factors(rsa, &p, &q);
  50761. ExpectPtrEq(rsa->p, p);
  50762. ExpectPtrEq(rsa->q, q);
  50763. p = NULL;
  50764. q = NULL;
  50765. ExpectNotNull(p = BN_new());
  50766. ExpectNotNull(q = BN_new());
  50767. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  50768. if (EXPECT_FAIL()) {
  50769. BN_free((BIGNUM*)p);
  50770. BN_free((BIGNUM*)q);
  50771. }
  50772. ExpectPtrEq(rsa->p, p);
  50773. ExpectPtrEq(rsa->q, q);
  50774. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  50775. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  50776. RSA_get0_factors(NULL, NULL, NULL);
  50777. RSA_get0_factors(NULL, &p, &q);
  50778. ExpectNull(p);
  50779. ExpectNull(q);
  50780. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  50781. ExpectIntEQ(RSA_bits(rsa), 21);
  50782. RSA_free(rsa);
  50783. rsa = NULL;
  50784. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  50785. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  50786. ExpectIntEQ(RSA_size(rsa), 384);
  50787. ExpectIntEQ(RSA_bits(rsa), 3072);
  50788. RSA_free(rsa);
  50789. rsa = NULL;
  50790. #endif
  50791. /* remove for now with odd key size until adjusting rsa key size check with
  50792. wc_MakeRsaKey()
  50793. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  50794. RSA_free(rsa);
  50795. rsa = NULL;
  50796. */
  50797. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  50798. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  50799. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  50800. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  50801. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  50802. {
  50803. byte buff[FOURK_BUF];
  50804. byte der[FOURK_BUF];
  50805. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  50806. XFILE f = XBADFILE;
  50807. int bytes = 0;
  50808. /* test loading encrypted RSA private pem w/o password */
  50809. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  50810. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  50811. if (f != XBADFILE)
  50812. XFCLOSE(f);
  50813. XMEMSET(der, 0, sizeof(der));
  50814. /* test that error value is returned with no password */
  50815. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  50816. 0);
  50817. }
  50818. #endif
  50819. #endif
  50820. return EXPECT_RESULT();
  50821. }
  50822. static int test_wolfSSL_RSA_DER(void)
  50823. {
  50824. EXPECT_DECLS;
  50825. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  50826. RSA *rsa = NULL;
  50827. int i;
  50828. const unsigned char *buff = NULL;
  50829. unsigned char *newBuff = NULL;
  50830. struct tbl_s
  50831. {
  50832. const unsigned char *der;
  50833. int sz;
  50834. } tbl[] = {
  50835. #ifdef USE_CERT_BUFFERS_1024
  50836. {client_key_der_1024, sizeof_client_key_der_1024},
  50837. {server_key_der_1024, sizeof_server_key_der_1024},
  50838. #endif
  50839. #ifdef USE_CERT_BUFFERS_2048
  50840. {client_key_der_2048, sizeof_client_key_der_2048},
  50841. {server_key_der_2048, sizeof_server_key_der_2048},
  50842. #endif
  50843. {NULL, 0}
  50844. };
  50845. /* Public Key DER */
  50846. struct tbl_s pub[] = {
  50847. #ifdef USE_CERT_BUFFERS_1024
  50848. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  50849. #endif
  50850. #ifdef USE_CERT_BUFFERS_2048
  50851. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  50852. #endif
  50853. {NULL, 0}
  50854. };
  50855. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  50856. buff = pub[0].der;
  50857. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  50858. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  50859. buff = tbl[0].der;
  50860. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  50861. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  50862. rsa = RSA_new();
  50863. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  50864. RSA_free(rsa);
  50865. rsa = NULL;
  50866. for (i = 0; tbl[i].der != NULL; i++)
  50867. {
  50868. /* Passing in pointer results in pointer moving. */
  50869. buff = tbl[i].der;
  50870. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  50871. ExpectNotNull(rsa);
  50872. RSA_free(rsa);
  50873. rsa = NULL;
  50874. }
  50875. for (i = 0; tbl[i].der != NULL; i++)
  50876. {
  50877. /* Passing in pointer results in pointer moving. */
  50878. buff = tbl[i].der;
  50879. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  50880. ExpectNotNull(rsa);
  50881. RSA_free(rsa);
  50882. rsa = NULL;
  50883. }
  50884. for (i = 0; pub[i].der != NULL; i++)
  50885. {
  50886. buff = pub[i].der;
  50887. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  50888. ExpectNotNull(rsa);
  50889. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  50890. newBuff = NULL;
  50891. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  50892. ExpectNotNull(newBuff);
  50893. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  50894. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50895. RSA_free(rsa);
  50896. rsa = NULL;
  50897. }
  50898. #endif
  50899. return EXPECT_RESULT();
  50900. }
  50901. static int test_wolfSSL_RSA_print(void)
  50902. {
  50903. EXPECT_DECLS;
  50904. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  50905. !defined(NO_STDIO_FILESYSTEM) && \
  50906. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  50907. !defined(NO_BIO) && defined(XFPRINTF)
  50908. BIO *bio = NULL;
  50909. WOLFSSL_RSA* rsa = NULL;
  50910. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  50911. ExpectNotNull(rsa = RSA_new());
  50912. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  50913. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  50914. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  50915. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  50916. /* Some very large number of indent spaces. */
  50917. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  50918. /* RSA is empty. */
  50919. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  50920. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  50921. RSA_free(rsa);
  50922. rsa = NULL;
  50923. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  50924. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  50925. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  50926. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  50927. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  50928. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  50929. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  50930. BIO_free(bio);
  50931. RSA_free(rsa);
  50932. #endif
  50933. return EXPECT_RESULT();
  50934. }
  50935. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  50936. {
  50937. EXPECT_DECLS;
  50938. #ifndef NO_RSA
  50939. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50940. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  50941. RSA *rsa = NULL;
  50942. const unsigned char *derBuf = client_key_der_2048;
  50943. unsigned char em[256] = {0}; /* len = 2048/8 */
  50944. /* Random data simulating a hash */
  50945. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  50946. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  50947. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  50948. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  50949. };
  50950. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  50951. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  50952. RSA_PSS_SALTLEN_DIGEST), 0);
  50953. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  50954. RSA_PSS_SALTLEN_DIGEST), 0);
  50955. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  50956. RSA_PSS_SALTLEN_DIGEST), 0);
  50957. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  50958. RSA_PSS_SALTLEN_DIGEST), 0);
  50959. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  50960. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  50961. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50962. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  50963. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50964. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  50965. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50966. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  50967. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50968. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50969. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  50970. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  50971. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50972. RSA_PSS_SALTLEN_DIGEST), 1);
  50973. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50974. RSA_PSS_SALTLEN_DIGEST), 1);
  50975. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50976. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50977. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50978. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  50979. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  50980. RSA_PSS_SALTLEN_MAX), 1);
  50981. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  50982. RSA_PSS_SALTLEN_MAX), 1);
  50983. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  50984. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  50985. RSA_free(rsa);
  50986. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  50987. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  50988. #endif
  50989. return EXPECT_RESULT();
  50990. }
  50991. static int test_wolfSSL_RSA_sign_sha3(void)
  50992. {
  50993. EXPECT_DECLS;
  50994. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  50995. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  50996. RSA* rsa = NULL;
  50997. const unsigned char *derBuf = client_key_der_2048;
  50998. unsigned char sigRet[256] = {0};
  50999. unsigned int sigLen = sizeof(sigRet);
  51000. /* Random data simulating a hash */
  51001. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  51002. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51003. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51004. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51005. };
  51006. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51007. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  51008. rsa), 1);
  51009. RSA_free(rsa);
  51010. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51011. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  51012. return EXPECT_RESULT();
  51013. }
  51014. static int test_wolfSSL_RSA_get0_key(void)
  51015. {
  51016. EXPECT_DECLS;
  51017. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51018. RSA *rsa = NULL;
  51019. const BIGNUM* n = NULL;
  51020. const BIGNUM* e = NULL;
  51021. const BIGNUM* d = NULL;
  51022. const unsigned char* der;
  51023. int derSz;
  51024. #ifdef USE_CERT_BUFFERS_1024
  51025. der = client_key_der_1024;
  51026. derSz = sizeof_client_key_der_1024;
  51027. #elif defined(USE_CERT_BUFFERS_2048)
  51028. der = client_key_der_2048;
  51029. derSz = sizeof_client_key_der_2048;
  51030. #else
  51031. der = NULL;
  51032. derSz = 0;
  51033. #endif
  51034. if (der != NULL) {
  51035. RSA_get0_key(NULL, NULL, NULL, NULL);
  51036. RSA_get0_key(rsa, NULL, NULL, NULL);
  51037. RSA_get0_key(NULL, &n, &e, &d);
  51038. ExpectNull(n);
  51039. ExpectNull(e);
  51040. ExpectNull(d);
  51041. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  51042. ExpectNotNull(rsa);
  51043. RSA_get0_key(rsa, NULL, NULL, NULL);
  51044. RSA_get0_key(rsa, &n, NULL, NULL);
  51045. ExpectNotNull(n);
  51046. RSA_get0_key(rsa, NULL, &e, NULL);
  51047. ExpectNotNull(e);
  51048. RSA_get0_key(rsa, NULL, NULL, &d);
  51049. ExpectNotNull(d);
  51050. RSA_get0_key(rsa, &n, &e, &d);
  51051. ExpectNotNull(n);
  51052. ExpectNotNull(e);
  51053. ExpectNotNull(d);
  51054. RSA_free(rsa);
  51055. }
  51056. #endif
  51057. return EXPECT_RESULT();
  51058. }
  51059. static int test_wolfSSL_RSA_meth(void)
  51060. {
  51061. EXPECT_DECLS;
  51062. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51063. RSA *rsa = NULL;
  51064. RSA_METHOD *rsa_meth = NULL;
  51065. #ifdef WOLFSSL_KEY_GEN
  51066. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51067. RSA_free(rsa);
  51068. rsa = NULL;
  51069. #else
  51070. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51071. #endif
  51072. ExpectNotNull(RSA_get_default_method());
  51073. wolfSSL_RSA_meth_free(NULL);
  51074. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  51075. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  51076. RSA_METHOD_FLAG_NO_CHECK));
  51077. #ifndef NO_WOLFSSL_STUB
  51078. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  51079. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  51080. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  51081. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  51082. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  51083. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  51084. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  51085. #endif
  51086. ExpectIntEQ(RSA_flags(NULL), 0);
  51087. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51088. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51089. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  51090. ExpectNotNull(rsa = RSA_new());
  51091. /* No method set. */
  51092. ExpectIntEQ(RSA_flags(rsa), 0);
  51093. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51094. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51095. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51096. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  51097. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  51098. if (EXPECT_FAIL()) {
  51099. wolfSSL_RSA_meth_free(rsa_meth);
  51100. }
  51101. ExpectNull(RSA_get_method(NULL));
  51102. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  51103. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  51104. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51105. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51106. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  51107. RSA_METHOD_FLAG_NO_CHECK);
  51108. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51109. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51110. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  51111. /* rsa_meth is freed here */
  51112. RSA_free(rsa);
  51113. #endif
  51114. return EXPECT_RESULT();
  51115. }
  51116. static int test_wolfSSL_RSA_verify(void)
  51117. {
  51118. EXPECT_DECLS;
  51119. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  51120. #ifndef NO_BIO
  51121. XFILE fp = XBADFILE;
  51122. RSA *pKey = NULL;
  51123. RSA *pubKey = NULL;
  51124. X509 *cert = NULL;
  51125. const char *text = "Hello wolfSSL !";
  51126. unsigned char hash[SHA256_DIGEST_LENGTH];
  51127. unsigned char signature[2048/8];
  51128. unsigned int signatureLength;
  51129. byte *buf = NULL;
  51130. BIO *bio = NULL;
  51131. SHA256_CTX c;
  51132. EVP_PKEY *evpPkey = NULL;
  51133. EVP_PKEY *evpPubkey = NULL;
  51134. size_t sz;
  51135. /* generate hash */
  51136. SHA256_Init(&c);
  51137. SHA256_Update(&c, text, strlen(text));
  51138. SHA256_Final(hash, &c);
  51139. #ifdef WOLFSSL_SMALL_STACK_CACHE
  51140. /* workaround for small stack cache case */
  51141. wc_Sha256Free((wc_Sha256*)&c);
  51142. #endif
  51143. /* read privete key file */
  51144. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  51145. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  51146. ExpectTrue((sz = XFTELL(fp)) > 0);
  51147. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  51148. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  51149. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  51150. if (fp != XBADFILE) {
  51151. XFCLOSE(fp);
  51152. fp = XBADFILE;
  51153. }
  51154. /* read private key and sign hash data */
  51155. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  51156. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  51157. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  51158. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  51159. signature, &signatureLength, pKey), SSL_SUCCESS);
  51160. /* read public key and verify signed data */
  51161. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  51162. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  51163. if (fp != XBADFILE)
  51164. XFCLOSE(fp);
  51165. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  51166. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  51167. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51168. signatureLength, pubKey), SSL_SUCCESS);
  51169. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  51170. signatureLength, NULL), SSL_FAILURE);
  51171. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  51172. signatureLength, pubKey), SSL_FAILURE);
  51173. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  51174. signatureLength, pubKey), SSL_FAILURE);
  51175. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51176. signatureLength, NULL), SSL_FAILURE);
  51177. RSA_free(pKey);
  51178. EVP_PKEY_free(evpPkey);
  51179. RSA_free(pubKey);
  51180. EVP_PKEY_free(evpPubkey);
  51181. X509_free(cert);
  51182. BIO_free(bio);
  51183. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  51184. #endif
  51185. #endif
  51186. return EXPECT_RESULT();
  51187. }
  51188. static int test_wolfSSL_RSA_sign(void)
  51189. {
  51190. EXPECT_DECLS;
  51191. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51192. RSA *rsa;
  51193. unsigned char hash[SHA256_DIGEST_LENGTH];
  51194. #ifdef USE_CERT_BUFFERS_1024
  51195. const unsigned char* privDer = client_key_der_1024;
  51196. size_t privDerSz = sizeof_client_key_der_1024;
  51197. const unsigned char* pubDer = client_keypub_der_1024;
  51198. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51199. unsigned char signature[1024/8];
  51200. #else
  51201. const unsigned char* privDer = client_key_der_2048;
  51202. size_t privDerSz = sizeof_client_key_der_2048;
  51203. const unsigned char* pubDer = client_keypub_der_2048;
  51204. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51205. unsigned char signature[2048/8];
  51206. #endif
  51207. unsigned int signatureLen;
  51208. const unsigned char* der;
  51209. XMEMSET(hash, 0, sizeof(hash));
  51210. der = privDer;
  51211. rsa = NULL;
  51212. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51213. /* Invalid parameters. */
  51214. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  51215. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  51216. &signatureLen, rsa), 0);
  51217. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  51218. &signatureLen, rsa), 0);
  51219. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  51220. &signatureLen, rsa), 0);
  51221. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51222. NULL, rsa), 0);
  51223. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51224. &signatureLen, NULL), 0);
  51225. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51226. &signatureLen, rsa), 1);
  51227. RSA_free(rsa);
  51228. der = pubDer;
  51229. rsa = NULL;
  51230. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51231. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51232. signatureLen, rsa), 1);
  51233. RSA_free(rsa);
  51234. #endif
  51235. return EXPECT_RESULT();
  51236. }
  51237. static int test_wolfSSL_RSA_sign_ex(void)
  51238. {
  51239. EXPECT_DECLS;
  51240. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51241. RSA *rsa = NULL;
  51242. unsigned char hash[SHA256_DIGEST_LENGTH];
  51243. #ifdef USE_CERT_BUFFERS_1024
  51244. const unsigned char* privDer = client_key_der_1024;
  51245. size_t privDerSz = sizeof_client_key_der_1024;
  51246. const unsigned char* pubDer = client_keypub_der_1024;
  51247. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51248. unsigned char signature[1024/8];
  51249. #else
  51250. const unsigned char* privDer = client_key_der_2048;
  51251. size_t privDerSz = sizeof_client_key_der_2048;
  51252. const unsigned char* pubDer = client_keypub_der_2048;
  51253. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51254. unsigned char signature[2048/8];
  51255. #endif
  51256. unsigned int signatureLen;
  51257. const unsigned char* der;
  51258. unsigned char encodedHash[51];
  51259. unsigned int encodedHashLen;
  51260. const unsigned char expEncHash[] = {
  51261. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  51262. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  51263. 0x00, 0x04, 0x20,
  51264. /* Hash data */
  51265. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51266. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51267. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51268. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51269. };
  51270. XMEMSET(hash, 0, sizeof(hash));
  51271. ExpectNotNull(rsa = wolfSSL_RSA_new());
  51272. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51273. &signatureLen, rsa, 1), 0);
  51274. wolfSSL_RSA_free(rsa);
  51275. der = privDer;
  51276. rsa = NULL;
  51277. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51278. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  51279. -1), 0);
  51280. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  51281. signature, &signatureLen, rsa, 1), 0);
  51282. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  51283. &signatureLen, rsa, 1), 0);
  51284. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  51285. &signatureLen, rsa, 1), 0);
  51286. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51287. NULL, rsa, 1), 0);
  51288. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51289. &signatureLen, NULL, 1), 0);
  51290. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51291. &signatureLen, rsa, -1), 0);
  51292. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  51293. &signatureLen, rsa, 0), 0);
  51294. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  51295. &signatureLen, rsa, 0), 0);
  51296. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51297. NULL, rsa, 0), 0);
  51298. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51299. &signatureLen, rsa, 1), 1);
  51300. /* Test returning encoded hash. */
  51301. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  51302. &encodedHashLen, rsa, 0), 1);
  51303. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  51304. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  51305. RSA_free(rsa);
  51306. der = pubDer;
  51307. rsa = NULL;
  51308. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51309. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51310. signatureLen, rsa), 1);
  51311. RSA_free(rsa);
  51312. #endif
  51313. return EXPECT_RESULT();
  51314. }
  51315. static int test_wolfSSL_RSA_public_decrypt(void)
  51316. {
  51317. EXPECT_DECLS;
  51318. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51319. RSA *rsa;
  51320. unsigned char msg[SHA256_DIGEST_LENGTH];
  51321. #ifdef USE_CERT_BUFFERS_1024
  51322. const unsigned char* pubDer = client_keypub_der_1024;
  51323. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51324. unsigned char decMsg[1024/8];
  51325. const unsigned char encMsg[] = {
  51326. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  51327. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  51328. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  51329. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  51330. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  51331. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  51332. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  51333. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  51334. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  51335. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  51336. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  51337. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  51338. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  51339. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  51340. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  51341. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  51342. };
  51343. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  51344. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  51345. defined(WC_RSA_NO_PADDING)
  51346. const unsigned char encMsgNoPad[] = {
  51347. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  51348. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  51349. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  51350. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  51351. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  51352. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  51353. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  51354. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  51355. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  51356. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  51357. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  51358. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  51359. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  51360. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  51361. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  51362. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  51363. };
  51364. #endif
  51365. #else
  51366. const unsigned char* pubDer = client_keypub_der_2048;
  51367. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51368. unsigned char decMsg[2048/8];
  51369. const unsigned char encMsg[] = {
  51370. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  51371. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  51372. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  51373. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  51374. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  51375. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  51376. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  51377. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  51378. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  51379. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  51380. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  51381. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  51382. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  51383. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  51384. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  51385. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  51386. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  51387. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  51388. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  51389. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  51390. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  51391. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  51392. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  51393. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  51394. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  51395. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  51396. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  51397. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  51398. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  51399. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  51400. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  51401. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  51402. };
  51403. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  51404. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  51405. defined(WC_RSA_NO_PADDING)
  51406. const unsigned char encMsgNoPad[] = {
  51407. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  51408. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  51409. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  51410. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  51411. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  51412. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  51413. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  51414. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  51415. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  51416. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  51417. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  51418. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  51419. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  51420. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  51421. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  51422. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  51423. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  51424. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  51425. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  51426. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  51427. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  51428. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  51429. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  51430. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  51431. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  51432. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  51433. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  51434. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  51435. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  51436. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  51437. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  51438. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  51439. };
  51440. #endif
  51441. #endif
  51442. const unsigned char* der;
  51443. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  51444. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  51445. defined(WC_RSA_NO_PADDING)
  51446. int i;
  51447. #endif
  51448. XMEMSET(msg, 0, sizeof(msg));
  51449. der = pubDer;
  51450. rsa = NULL;
  51451. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51452. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  51453. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  51454. RSA_PKCS1_PADDING), -1);
  51455. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  51456. RSA_PKCS1_PADDING), -1);
  51457. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  51458. RSA_PKCS1_PADDING), -1);
  51459. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  51460. RSA_PKCS1_PADDING), -1);
  51461. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  51462. RSA_PKCS1_PSS_PADDING), -1);
  51463. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  51464. RSA_PKCS1_PADDING), 32);
  51465. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  51466. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  51467. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  51468. defined(WC_RSA_NO_PADDING)
  51469. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  51470. rsa, RSA_NO_PADDING), sizeof(decMsg));
  51471. /* Zeros before actual data. */
  51472. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  51473. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  51474. }
  51475. /* Check actual data. */
  51476. XMEMSET(msg, 0x01, sizeof(msg));
  51477. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  51478. #endif
  51479. RSA_free(rsa);
  51480. #endif
  51481. return EXPECT_RESULT();
  51482. }
  51483. static int test_wolfSSL_RSA_private_encrypt(void)
  51484. {
  51485. EXPECT_DECLS;
  51486. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51487. RSA *rsa;
  51488. unsigned char msg[SHA256_DIGEST_LENGTH];
  51489. #ifdef USE_CERT_BUFFERS_1024
  51490. const unsigned char* privDer = client_key_der_1024;
  51491. size_t privDerSz = sizeof_client_key_der_1024;
  51492. unsigned char encMsg[1024/8];
  51493. const unsigned char expEncMsg[] = {
  51494. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  51495. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  51496. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  51497. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  51498. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  51499. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  51500. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  51501. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  51502. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  51503. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  51504. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  51505. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  51506. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  51507. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  51508. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  51509. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  51510. };
  51511. #ifdef WC_RSA_NO_PADDING
  51512. const unsigned char expEncMsgNoPad[] = {
  51513. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  51514. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  51515. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  51516. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  51517. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  51518. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  51519. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  51520. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  51521. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  51522. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  51523. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  51524. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  51525. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  51526. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  51527. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  51528. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  51529. };
  51530. #endif
  51531. #else
  51532. const unsigned char* privDer = client_key_der_2048;
  51533. size_t privDerSz = sizeof_client_key_der_2048;
  51534. unsigned char encMsg[2048/8];
  51535. const unsigned char expEncMsg[] = {
  51536. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  51537. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  51538. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  51539. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  51540. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  51541. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  51542. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  51543. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  51544. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  51545. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  51546. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  51547. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  51548. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  51549. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  51550. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  51551. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  51552. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  51553. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  51554. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  51555. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  51556. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  51557. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  51558. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  51559. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  51560. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  51561. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  51562. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  51563. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  51564. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  51565. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  51566. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  51567. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  51568. };
  51569. #ifdef WC_RSA_NO_PADDING
  51570. const unsigned char expEncMsgNoPad[] = {
  51571. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  51572. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  51573. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  51574. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  51575. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  51576. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  51577. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  51578. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  51579. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  51580. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  51581. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  51582. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  51583. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  51584. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  51585. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  51586. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  51587. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  51588. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  51589. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  51590. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  51591. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  51592. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  51593. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  51594. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  51595. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  51596. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  51597. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  51598. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  51599. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  51600. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  51601. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  51602. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  51603. };
  51604. #endif
  51605. #endif
  51606. const unsigned char* der;
  51607. XMEMSET(msg, 0x00, sizeof(msg));
  51608. der = privDer;
  51609. rsa = NULL;
  51610. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51611. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  51612. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  51613. -1);
  51614. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  51615. RSA_PKCS1_PADDING), -1);
  51616. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  51617. RSA_PKCS1_PADDING), -1);
  51618. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  51619. RSA_PKCS1_PADDING), -1);
  51620. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51621. RSA_PKCS1_PSS_PADDING), -1);
  51622. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51623. RSA_PKCS1_PADDING), sizeof(encMsg));
  51624. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  51625. #ifdef WC_RSA_NO_PADDING
  51626. /* Non-zero message. */
  51627. XMEMSET(msg, 0x01, sizeof(msg));
  51628. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  51629. RSA_NO_PADDING), sizeof(encMsg));
  51630. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  51631. #endif
  51632. RSA_free(rsa);
  51633. #endif
  51634. return EXPECT_RESULT();
  51635. }
  51636. static int test_wolfSSL_RSA_public_encrypt(void)
  51637. {
  51638. EXPECT_DECLS;
  51639. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51640. RSA* rsa = NULL;
  51641. const unsigned char msg[2048/8] = { 0 };
  51642. unsigned char encMsg[2048/8];
  51643. ExpectNotNull(rsa = RSA_new());
  51644. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  51645. RSA_PKCS1_PADDING), -1);
  51646. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  51647. RSA_PKCS1_PADDING), -1);
  51648. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  51649. RSA_PKCS1_PADDING), -1);
  51650. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  51651. RSA_PKCS1_PADDING), -1);
  51652. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  51653. RSA_PKCS1_PSS_PADDING), -1);
  51654. /* Empty RSA key. */
  51655. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  51656. RSA_PKCS1_PADDING), -1);
  51657. RSA_free(rsa);
  51658. #endif
  51659. return EXPECT_RESULT();
  51660. }
  51661. static int test_wolfSSL_RSA_private_decrypt(void)
  51662. {
  51663. EXPECT_DECLS;
  51664. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51665. RSA* rsa = NULL;
  51666. unsigned char msg[2048/8];
  51667. const unsigned char encMsg[2048/8] = { 0 };
  51668. ExpectNotNull(rsa = RSA_new());
  51669. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  51670. RSA_PKCS1_PADDING), -1);
  51671. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  51672. RSA_PKCS1_PADDING), -1);
  51673. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  51674. RSA_PKCS1_PADDING), -1);
  51675. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  51676. RSA_PKCS1_PADDING), -1);
  51677. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  51678. RSA_PKCS1_PSS_PADDING), -1);
  51679. /* Empty RSA key. */
  51680. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  51681. RSA_PKCS1_PADDING), -1);
  51682. RSA_free(rsa);
  51683. #endif
  51684. return EXPECT_RESULT();
  51685. }
  51686. static int test_wolfSSL_RSA_GenAdd(void)
  51687. {
  51688. EXPECT_DECLS;
  51689. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51690. RSA *rsa;
  51691. #ifdef USE_CERT_BUFFERS_1024
  51692. const unsigned char* privDer = client_key_der_1024;
  51693. size_t privDerSz = sizeof_client_key_der_1024;
  51694. const unsigned char* pubDer = client_keypub_der_1024;
  51695. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51696. #else
  51697. const unsigned char* privDer = client_key_der_2048;
  51698. size_t privDerSz = sizeof_client_key_der_2048;
  51699. const unsigned char* pubDer = client_keypub_der_2048;
  51700. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51701. #endif
  51702. const unsigned char* der;
  51703. der = privDer;
  51704. rsa = NULL;
  51705. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51706. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  51707. #ifndef RSA_LOW_MEM
  51708. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  51709. #else
  51710. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  51711. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  51712. #endif
  51713. RSA_free(rsa);
  51714. der = pubDer;
  51715. rsa = NULL;
  51716. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51717. /* Need private values. */
  51718. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  51719. RSA_free(rsa);
  51720. #endif
  51721. return EXPECT_RESULT();
  51722. }
  51723. static int test_wolfSSL_RSA_blinding_on(void)
  51724. {
  51725. EXPECT_DECLS;
  51726. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  51727. RSA *rsa;
  51728. WOLFSSL_BN_CTX *bnCtx = NULL;
  51729. #ifdef USE_CERT_BUFFERS_1024
  51730. const unsigned char* privDer = client_key_der_1024;
  51731. size_t privDerSz = sizeof_client_key_der_1024;
  51732. #else
  51733. const unsigned char* privDer = client_key_der_2048;
  51734. size_t privDerSz = sizeof_client_key_der_2048;
  51735. #endif
  51736. const unsigned char* der;
  51737. der = privDer;
  51738. rsa = NULL;
  51739. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51740. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  51741. /* Does nothing so all parameters are valid. */
  51742. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  51743. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  51744. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  51745. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  51746. wolfSSL_BN_CTX_free(bnCtx);
  51747. RSA_free(rsa);
  51748. #endif
  51749. return EXPECT_RESULT();
  51750. }
  51751. static int test_wolfSSL_RSA_ex_data(void)
  51752. {
  51753. EXPECT_DECLS;
  51754. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51755. RSA* rsa = NULL;
  51756. unsigned char data[1];
  51757. ExpectNotNull(rsa = RSA_new());
  51758. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  51759. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  51760. #ifdef MAX_EX_DATA
  51761. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  51762. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  51763. #endif
  51764. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  51765. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  51766. #ifdef HAVE_EX_DATA
  51767. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  51768. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  51769. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  51770. #else
  51771. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  51772. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  51773. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  51774. #endif
  51775. RSA_free(rsa);
  51776. #endif /* !NO_RSA && OPENSSL_EXTRA */
  51777. return EXPECT_RESULT();
  51778. }
  51779. static int test_wolfSSL_RSA_LoadDer(void)
  51780. {
  51781. EXPECT_DECLS;
  51782. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  51783. defined(OPENSSL_EXTRA_X509_SMALL))
  51784. RSA *rsa = NULL;
  51785. #ifdef USE_CERT_BUFFERS_1024
  51786. const unsigned char* privDer = client_key_der_1024;
  51787. size_t privDerSz = sizeof_client_key_der_1024;
  51788. #else
  51789. const unsigned char* privDer = client_key_der_2048;
  51790. size_t privDerSz = sizeof_client_key_der_2048;
  51791. #endif
  51792. ExpectNotNull(rsa = RSA_new());
  51793. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  51794. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  51795. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  51796. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  51797. RSA_free(rsa);
  51798. #endif /* !NO_RSA && OPENSSL_EXTRA */
  51799. return EXPECT_RESULT();
  51800. }
  51801. /* Local API. */
  51802. static int test_wolfSSL_RSA_To_Der(void)
  51803. {
  51804. EXPECT_DECLS;
  51805. #ifdef WOLFSSL_TEST_STATIC_BUILD
  51806. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51807. RSA* rsa;
  51808. #ifdef USE_CERT_BUFFERS_1024
  51809. const unsigned char* privDer = client_key_der_1024;
  51810. size_t privDerSz = sizeof_client_key_der_1024;
  51811. const unsigned char* pubDer = client_keypub_der_1024;
  51812. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51813. unsigned char out[sizeof(client_key_der_1024)];
  51814. #else
  51815. const unsigned char* privDer = client_key_der_2048;
  51816. size_t privDerSz = sizeof_client_key_der_2048;
  51817. const unsigned char* pubDer = client_keypub_der_2048;
  51818. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51819. unsigned char out[sizeof(client_key_der_2048)];
  51820. #endif
  51821. const unsigned char* der;
  51822. unsigned char* outDer = NULL;
  51823. der = privDer;
  51824. rsa = NULL;
  51825. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51826. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51827. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  51828. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  51829. outDer = out;
  51830. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  51831. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  51832. outDer = NULL;
  51833. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  51834. ExpectNotNull(outDer);
  51835. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  51836. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  51837. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  51838. outDer = out;
  51839. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  51840. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  51841. RSA_free(rsa);
  51842. ExpectNotNull(rsa = RSA_new());
  51843. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51844. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  51845. RSA_free(rsa);
  51846. der = pubDer;
  51847. rsa = NULL;
  51848. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51849. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  51850. RSA_free(rsa);
  51851. #endif
  51852. #endif
  51853. return EXPECT_RESULT();
  51854. }
  51855. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  51856. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  51857. {
  51858. EXPECT_DECLS;
  51859. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  51860. XFILE file = XBADFILE;
  51861. const char* fname = "./certs/server-keyPub.pem";
  51862. RSA *rsa = NULL;
  51863. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  51864. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  51865. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  51866. ExpectIntEQ(RSA_size(rsa), 256);
  51867. RSA_free(rsa);
  51868. if (file != XBADFILE)
  51869. XFCLOSE(file);
  51870. #endif
  51871. return EXPECT_RESULT();
  51872. }
  51873. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  51874. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  51875. {
  51876. EXPECT_DECLS;
  51877. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51878. defined(WOLFSSL_KEY_GEN)
  51879. RSA* rsa = NULL;
  51880. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  51881. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  51882. /* Valid but stub so returns 0. */
  51883. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  51884. #endif
  51885. return EXPECT_RESULT();
  51886. }
  51887. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  51888. {
  51889. EXPECT_DECLS;
  51890. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  51891. (defined(WOLFSSL_PEM_TO_DER) || \
  51892. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  51893. RSA* rsa = NULL;
  51894. #ifdef USE_CERT_BUFFERS_1024
  51895. const unsigned char* privDer = client_key_der_1024;
  51896. size_t privDerSz = sizeof_client_key_der_1024;
  51897. #else
  51898. const unsigned char* privDer = client_key_der_2048;
  51899. size_t privDerSz = sizeof_client_key_der_2048;
  51900. #endif
  51901. const unsigned char* der;
  51902. #ifndef NO_AES
  51903. unsigned char passwd[] = "password";
  51904. #endif
  51905. ExpectNotNull(rsa = RSA_new());
  51906. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  51907. NULL, NULL), 0);
  51908. RSA_free(rsa);
  51909. der = privDer;
  51910. rsa = NULL;
  51911. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51912. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  51913. NULL, NULL), 0);
  51914. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  51915. NULL, NULL), 0);
  51916. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  51917. NULL, NULL), 1);
  51918. #ifndef NO_AES
  51919. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  51920. NULL, 0, NULL, NULL), 1);
  51921. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  51922. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  51923. #endif
  51924. RSA_free(rsa);
  51925. #endif
  51926. return EXPECT_RESULT();
  51927. }
  51928. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  51929. {
  51930. EXPECT_DECLS;
  51931. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  51932. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  51933. RSA* rsa = NULL;
  51934. #ifdef USE_CERT_BUFFERS_1024
  51935. const unsigned char* privDer = client_key_der_1024;
  51936. size_t privDerSz = sizeof_client_key_der_1024;
  51937. #else
  51938. const unsigned char* privDer = client_key_der_2048;
  51939. size_t privDerSz = sizeof_client_key_der_2048;
  51940. #endif
  51941. const unsigned char* der;
  51942. #ifndef NO_AES
  51943. unsigned char passwd[] = "password";
  51944. #endif
  51945. unsigned char* pem = NULL;
  51946. int plen;
  51947. ExpectNotNull(rsa = RSA_new());
  51948. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51949. &plen), 0);
  51950. RSA_free(rsa);
  51951. der = privDer;
  51952. rsa = NULL;
  51953. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51954. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  51955. &plen), 0);
  51956. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  51957. &plen), 0);
  51958. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51959. NULL), 0);
  51960. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  51961. &plen), 1);
  51962. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51963. pem = NULL;
  51964. #ifndef NO_AES
  51965. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  51966. NULL, 0, &pem, &plen), 1);
  51967. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51968. pem = NULL;
  51969. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  51970. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  51971. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  51972. #endif
  51973. RSA_free(rsa);
  51974. #endif
  51975. return EXPECT_RESULT();
  51976. }
  51977. static int test_wolfSSL_DH(void)
  51978. {
  51979. EXPECT_DECLS;
  51980. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  51981. DH *dh = NULL;
  51982. BIGNUM* p;
  51983. BIGNUM* q;
  51984. BIGNUM* g;
  51985. BIGNUM* pub = NULL;
  51986. BIGNUM* priv = NULL;
  51987. #if defined(OPENSSL_ALL)
  51988. #if !defined(HAVE_FIPS) || \
  51989. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  51990. FILE* f = NULL;
  51991. unsigned char buf[268];
  51992. const unsigned char* pt = buf;
  51993. long len = 0;
  51994. dh = NULL;
  51995. XMEMSET(buf, 0, sizeof(buf));
  51996. /* Test 2048 bit parameters */
  51997. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  51998. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  51999. if (f != XBADFILE)
  52000. XFCLOSE(f);
  52001. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  52002. ExpectNotNull(dh->p);
  52003. ExpectNotNull(dh->g);
  52004. ExpectTrue(pt == buf);
  52005. ExpectIntEQ(DH_generate_key(dh), 1);
  52006. ExpectIntEQ(DH_generate_key(dh), 1);
  52007. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  52008. ExpectNotNull(pub = BN_new());
  52009. ExpectIntEQ(BN_set_word(pub, 1), 1);
  52010. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  52011. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  52012. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  52013. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  52014. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  52015. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  52016. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  52017. BN_free(pub);
  52018. pub = NULL;
  52019. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52020. (const BIGNUM**)&q,
  52021. (const BIGNUM**)&g);
  52022. ExpectPtrEq(p, dh->p);
  52023. ExpectPtrEq(q, dh->q);
  52024. ExpectPtrEq(g, dh->g);
  52025. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52026. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52027. ExpectPtrEq(pub, dh->pub_key);
  52028. ExpectPtrEq(priv, dh->priv_key);
  52029. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  52030. ExpectPtrEq(pub, dh->pub_key);
  52031. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  52032. ExpectPtrEq(priv, dh->priv_key);
  52033. pub = NULL;
  52034. priv = NULL;
  52035. ExpectNotNull(pub = BN_new());
  52036. ExpectNotNull(priv = BN_new());
  52037. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  52038. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52039. if (EXPECT_FAIL()) {
  52040. BN_free(pub);
  52041. BN_free(priv);
  52042. }
  52043. pub = NULL;
  52044. priv = NULL;
  52045. ExpectNotNull(pub = BN_new());
  52046. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  52047. if (EXPECT_FAIL()) {
  52048. BN_free(pub);
  52049. }
  52050. ExpectNotNull(priv = BN_new());
  52051. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  52052. if (EXPECT_FAIL()) {
  52053. BN_free(priv);
  52054. }
  52055. ExpectPtrEq(pub, dh->pub_key);
  52056. ExpectPtrEq(priv, dh->priv_key);
  52057. pub = NULL;
  52058. priv = NULL;
  52059. DH_free(dh);
  52060. dh = NULL;
  52061. ExpectNotNull(dh = DH_new());
  52062. p = NULL;
  52063. ExpectNotNull(p = BN_new());
  52064. ExpectIntEQ(BN_set_word(p, 1), 1);
  52065. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52066. ExpectNotNull(pub = BN_new());
  52067. ExpectNotNull(priv = BN_new());
  52068. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52069. if (EXPECT_FAIL()) {
  52070. BN_free(pub);
  52071. BN_free(priv);
  52072. }
  52073. pub = NULL;
  52074. priv = NULL;
  52075. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52076. BN_free(p);
  52077. p = NULL;
  52078. DH_free(dh);
  52079. dh = NULL;
  52080. #ifdef WOLFSSL_KEY_GEN
  52081. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  52082. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  52083. DH_free(dh);
  52084. dh = NULL;
  52085. #endif
  52086. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  52087. #endif /* OPENSSL_ALL */
  52088. (void)dh;
  52089. (void)p;
  52090. (void)q;
  52091. (void)g;
  52092. (void)pub;
  52093. (void)priv;
  52094. ExpectNotNull(dh = wolfSSL_DH_new());
  52095. /* invalid parameters test */
  52096. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  52097. (const BIGNUM**)&q,
  52098. (const BIGNUM**)&g);
  52099. DH_get0_pqg(dh, NULL,
  52100. (const BIGNUM**)&q,
  52101. (const BIGNUM**)&g);
  52102. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  52103. DH_get0_pqg(dh, NULL, NULL, NULL);
  52104. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52105. (const BIGNUM**)&q,
  52106. (const BIGNUM**)&g);
  52107. ExpectPtrEq(p, NULL);
  52108. ExpectPtrEq(q, NULL);
  52109. ExpectPtrEq(g, NULL);
  52110. DH_free(dh);
  52111. dh = NULL;
  52112. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  52113. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  52114. #if defined(OPENSSL_ALL) || \
  52115. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52116. dh = wolfSSL_DH_new();
  52117. ExpectNotNull(dh);
  52118. p = wolfSSL_BN_new();
  52119. ExpectNotNull(p);
  52120. ExpectIntEQ(BN_set_word(p, 11), 1);
  52121. g = wolfSSL_BN_new();
  52122. ExpectNotNull(g);
  52123. ExpectIntEQ(BN_set_word(g, 2), 1);
  52124. q = wolfSSL_BN_new();
  52125. ExpectNotNull(q);
  52126. ExpectIntEQ(BN_set_word(q, 5), 1);
  52127. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  52128. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  52129. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  52130. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  52131. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  52132. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  52133. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  52134. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  52135. /* Don't need q. */
  52136. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52137. if (EXPECT_FAIL()) {
  52138. BN_free(p);
  52139. BN_free(g);
  52140. }
  52141. p = NULL;
  52142. g = NULL;
  52143. /* Setting again will free the p and g. */
  52144. wolfSSL_BN_free(q);
  52145. q = NULL;
  52146. DH_free(dh);
  52147. dh = NULL;
  52148. dh = wolfSSL_DH_new();
  52149. ExpectNotNull(dh);
  52150. p = wolfSSL_BN_new();
  52151. ExpectNotNull(p);
  52152. ExpectIntEQ(BN_set_word(p, 11), 1);
  52153. g = wolfSSL_BN_new();
  52154. ExpectNotNull(g);
  52155. ExpectIntEQ(BN_set_word(g, 2), 1);
  52156. q = wolfSSL_BN_new();
  52157. ExpectNotNull(q);
  52158. ExpectIntEQ(BN_set_word(q, 5), 1);
  52159. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  52160. /* p, q and g are now owned by dh - don't free. */
  52161. if (EXPECT_FAIL()) {
  52162. BN_free(p);
  52163. BN_free(q);
  52164. BN_free(g);
  52165. }
  52166. p = NULL;
  52167. q = NULL;
  52168. g = NULL;
  52169. p = wolfSSL_BN_new();
  52170. ExpectNotNull(p);
  52171. ExpectIntEQ(BN_set_word(p, 11), 1);
  52172. g = wolfSSL_BN_new();
  52173. ExpectNotNull(g);
  52174. ExpectIntEQ(BN_set_word(g, 2), 1);
  52175. q = wolfSSL_BN_new();
  52176. ExpectNotNull(q);
  52177. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  52178. if (EXPECT_FAIL()) {
  52179. BN_free(p);
  52180. }
  52181. p = NULL;
  52182. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  52183. if (EXPECT_FAIL()) {
  52184. BN_free(q);
  52185. }
  52186. q = NULL;
  52187. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  52188. if (EXPECT_FAIL()) {
  52189. BN_free(g);
  52190. }
  52191. g = NULL;
  52192. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  52193. /* p, q and g are now owned by dh - don't free. */
  52194. DH_free(dh);
  52195. dh = NULL;
  52196. ExpectIntEQ(DH_generate_key(NULL), 0);
  52197. ExpectNotNull(dh = DH_new());
  52198. ExpectIntEQ(DH_generate_key(dh), 0);
  52199. p = wolfSSL_BN_new();
  52200. ExpectNotNull(p);
  52201. ExpectIntEQ(BN_set_word(p, 0), 1);
  52202. g = wolfSSL_BN_new();
  52203. ExpectNotNull(g);
  52204. ExpectIntEQ(BN_set_word(g, 2), 1);
  52205. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52206. if (EXPECT_FAIL()) {
  52207. BN_free(p);
  52208. BN_free(g);
  52209. }
  52210. p = NULL;
  52211. g = NULL;
  52212. ExpectIntEQ(DH_generate_key(dh), 0);
  52213. DH_free(dh);
  52214. dh = NULL;
  52215. #endif
  52216. #endif
  52217. /* Test DH_up_ref() */
  52218. dh = wolfSSL_DH_new();
  52219. ExpectNotNull(dh);
  52220. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  52221. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  52222. DH_free(dh); /* decrease ref count */
  52223. DH_free(dh); /* free WOLFSSL_DH */
  52224. dh = NULL;
  52225. q = NULL;
  52226. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  52227. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  52228. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  52229. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  52230. #ifdef HAVE_FFDHE_2048
  52231. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52232. DH_free(dh);
  52233. dh = NULL;
  52234. q = NULL;
  52235. #endif
  52236. #ifdef HAVE_FFDHE_3072
  52237. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  52238. DH_free(dh);
  52239. dh = NULL;
  52240. q = NULL;
  52241. #endif
  52242. #ifdef HAVE_FFDHE_4096
  52243. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  52244. DH_free(dh);
  52245. dh = NULL;
  52246. q = NULL;
  52247. #endif
  52248. #else
  52249. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52250. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  52251. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  52252. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  52253. #endif /* OPENSSL_EXTRA && !NO_DH */
  52254. return EXPECT_RESULT();
  52255. }
  52256. static int test_wolfSSL_DH_dup(void)
  52257. {
  52258. EXPECT_DECLS;
  52259. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  52260. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  52261. defined(OPENSSL_EXTRA)
  52262. DH *dh = NULL;
  52263. DH *dhDup = NULL;
  52264. ExpectNotNull(dh = wolfSSL_DH_new());
  52265. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  52266. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  52267. #if defined(OPENSSL_ALL) || \
  52268. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52269. {
  52270. WOLFSSL_BIGNUM* p = NULL;
  52271. WOLFSSL_BIGNUM* g = NULL;
  52272. ExpectNotNull(p = wolfSSL_BN_new());
  52273. ExpectNotNull(g = wolfSSL_BN_new());
  52274. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  52275. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  52276. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52277. if (EXPECT_FAIL()) {
  52278. wolfSSL_BN_free(p);
  52279. wolfSSL_BN_free(g);
  52280. }
  52281. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  52282. wolfSSL_DH_free(dhDup);
  52283. }
  52284. #endif
  52285. wolfSSL_DH_free(dh);
  52286. #endif
  52287. #endif
  52288. return EXPECT_RESULT();
  52289. }
  52290. static int test_wolfSSL_DH_check(void)
  52291. {
  52292. EXPECT_DECLS;
  52293. #ifdef OPENSSL_ALL
  52294. #ifndef NO_DH
  52295. #ifndef NO_BIO
  52296. #ifndef NO_DSA
  52297. byte buf[6000];
  52298. char file[] = "./certs/dsaparams.pem";
  52299. XFILE f = XBADFILE;
  52300. int bytes;
  52301. BIO* bio = NULL;
  52302. DSA* dsa = NULL;
  52303. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  52304. static const byte dh2048[] = {
  52305. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  52306. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  52307. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  52308. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  52309. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  52310. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  52311. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  52312. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  52313. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  52314. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  52315. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  52316. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  52317. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  52318. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  52319. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  52320. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  52321. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  52322. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  52323. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  52324. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  52325. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  52326. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  52327. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  52328. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  52329. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  52330. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  52331. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  52332. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  52333. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  52334. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  52335. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  52336. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  52337. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  52338. 0x93, 0x02, 0x01, 0x02
  52339. };
  52340. const byte* params;
  52341. #endif
  52342. DH* dh = NULL;
  52343. WOLFSSL_BIGNUM* p = NULL;
  52344. WOLFSSL_BIGNUM* g = NULL;
  52345. WOLFSSL_BIGNUM* pTmp = NULL;
  52346. WOLFSSL_BIGNUM* gTmp = NULL;
  52347. int codes = -1;
  52348. #ifndef NO_DSA
  52349. /* Initialize DH */
  52350. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  52351. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  52352. if (f != XBADFILE)
  52353. XFCLOSE(f);
  52354. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  52355. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  52356. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  52357. ExpectNotNull(dh);
  52358. BIO_free(bio);
  52359. DSA_free(dsa);
  52360. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  52361. params = dh2048;
  52362. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  52363. (long)sizeof(dh2048)));
  52364. #else
  52365. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  52366. #endif
  52367. /* Test assumed to be valid dh.
  52368. * Should return WOLFSSL_SUCCESS
  52369. * codes should be 0
  52370. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  52371. */
  52372. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52373. ExpectIntEQ(codes, 0);
  52374. /* Test NULL dh: expected BAD_FUNC_ARG */
  52375. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  52376. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  52377. if (dh != NULL) {
  52378. pTmp = dh->p;
  52379. dh->p = NULL;
  52380. }
  52381. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52382. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  52383. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  52384. /* set dh->p back to normal so it won't fail on next tests */
  52385. if (dh != NULL) {
  52386. dh->p = pTmp;
  52387. pTmp = NULL;
  52388. }
  52389. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  52390. if (dh != NULL) {
  52391. gTmp = dh->g;
  52392. dh->g = NULL;
  52393. }
  52394. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52395. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  52396. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  52397. if (dh != NULL) {
  52398. dh->g = gTmp;
  52399. gTmp = NULL;
  52400. }
  52401. /* Cleanup */
  52402. DH_free(dh);
  52403. dh = NULL;
  52404. dh = DH_new();
  52405. ExpectNotNull(dh);
  52406. /* Check empty DH. */
  52407. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52408. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  52409. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  52410. /* Check non-prime valued p. */
  52411. ExpectNotNull(p = BN_new());
  52412. ExpectIntEQ(BN_set_word(p, 4), 1);
  52413. ExpectNotNull(g = BN_new());
  52414. ExpectIntEQ(BN_set_word(g, 2), 1);
  52415. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  52416. if (EXPECT_FAIL()) {
  52417. wolfSSL_BN_free(p);
  52418. wolfSSL_BN_free(g);
  52419. }
  52420. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  52421. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  52422. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  52423. DH_free(dh);
  52424. dh = NULL;
  52425. #endif
  52426. #endif /* !NO_DH && !NO_DSA */
  52427. #endif
  52428. return EXPECT_RESULT();
  52429. }
  52430. static int test_wolfSSL_DH_prime(void)
  52431. {
  52432. EXPECT_DECLS;
  52433. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52434. WOLFSSL_BIGNUM* bn = NULL;
  52435. #if WOLFSSL_MAX_BN_BITS >= 768
  52436. WOLFSSL_BIGNUM* bn2 = NULL;
  52437. #endif
  52438. bn = wolfSSL_DH_768_prime(NULL);
  52439. #if WOLFSSL_MAX_BN_BITS >= 768
  52440. ExpectNotNull(bn);
  52441. bn2 = wolfSSL_DH_768_prime(bn);
  52442. ExpectNotNull(bn2);
  52443. ExpectTrue(bn == bn2);
  52444. wolfSSL_BN_free(bn);
  52445. bn = NULL;
  52446. #else
  52447. ExpectNull(bn);
  52448. #endif
  52449. bn = wolfSSL_DH_1024_prime(NULL);
  52450. #if WOLFSSL_MAX_BN_BITS >= 1024
  52451. ExpectNotNull(bn);
  52452. wolfSSL_BN_free(bn);
  52453. bn = NULL;
  52454. #else
  52455. ExpectNull(bn);
  52456. #endif
  52457. bn = wolfSSL_DH_2048_prime(NULL);
  52458. #if WOLFSSL_MAX_BN_BITS >= 2048
  52459. ExpectNotNull(bn);
  52460. wolfSSL_BN_free(bn);
  52461. bn = NULL;
  52462. #else
  52463. ExpectNull(bn);
  52464. #endif
  52465. bn = wolfSSL_DH_3072_prime(NULL);
  52466. #if WOLFSSL_MAX_BN_BITS >= 3072
  52467. ExpectNotNull(bn);
  52468. wolfSSL_BN_free(bn);
  52469. bn = NULL;
  52470. #else
  52471. ExpectNull(bn);
  52472. #endif
  52473. bn = wolfSSL_DH_4096_prime(NULL);
  52474. #if WOLFSSL_MAX_BN_BITS >= 4096
  52475. ExpectNotNull(bn);
  52476. wolfSSL_BN_free(bn);
  52477. bn = NULL;
  52478. #else
  52479. ExpectNull(bn);
  52480. #endif
  52481. bn = wolfSSL_DH_6144_prime(NULL);
  52482. #if WOLFSSL_MAX_BN_BITS >= 6144
  52483. ExpectNotNull(bn);
  52484. wolfSSL_BN_free(bn);
  52485. bn = NULL;
  52486. #else
  52487. ExpectNull(bn);
  52488. #endif
  52489. bn = wolfSSL_DH_8192_prime(NULL);
  52490. #if WOLFSSL_MAX_BN_BITS >= 8192
  52491. ExpectNotNull(bn);
  52492. wolfSSL_BN_free(bn);
  52493. bn = NULL;
  52494. #else
  52495. ExpectNull(bn);
  52496. #endif
  52497. #endif
  52498. return EXPECT_RESULT();
  52499. }
  52500. static int test_wolfSSL_DH_1536_prime(void)
  52501. {
  52502. EXPECT_DECLS;
  52503. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52504. BIGNUM* bn = NULL;
  52505. unsigned char bits[200];
  52506. int sz = 192; /* known binary size */
  52507. const byte expected[] = {
  52508. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  52509. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  52510. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  52511. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  52512. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  52513. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  52514. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  52515. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  52516. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  52517. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  52518. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  52519. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  52520. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  52521. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  52522. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  52523. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  52524. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  52525. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  52526. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  52527. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  52528. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  52529. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  52530. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  52531. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  52532. };
  52533. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  52534. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  52535. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  52536. BN_free(bn);
  52537. #endif
  52538. return EXPECT_RESULT();
  52539. }
  52540. static int test_wolfSSL_DH_get_2048_256(void)
  52541. {
  52542. EXPECT_DECLS;
  52543. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52544. WOLFSSL_DH* dh = NULL;
  52545. const WOLFSSL_BIGNUM* pBn;
  52546. const WOLFSSL_BIGNUM* gBn;
  52547. const WOLFSSL_BIGNUM* qBn;
  52548. const byte pExpected[] = {
  52549. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  52550. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  52551. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  52552. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  52553. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  52554. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  52555. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  52556. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  52557. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  52558. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  52559. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  52560. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  52561. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  52562. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  52563. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  52564. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  52565. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  52566. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  52567. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  52568. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  52569. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  52570. 0x1E, 0x1A, 0x15, 0x97
  52571. };
  52572. const byte gExpected[] = {
  52573. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  52574. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  52575. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  52576. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  52577. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  52578. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  52579. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  52580. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  52581. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  52582. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  52583. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  52584. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  52585. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  52586. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  52587. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  52588. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  52589. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  52590. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  52591. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  52592. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  52593. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  52594. 0x6C, 0xC4, 0x16, 0x59
  52595. };
  52596. const byte qExpected[] = {
  52597. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  52598. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  52599. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  52600. };
  52601. int pSz = 0;
  52602. int qSz = 0;
  52603. int gSz = 0;
  52604. byte* pReturned = NULL;
  52605. byte* qReturned = NULL;
  52606. byte* gReturned = NULL;
  52607. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  52608. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  52609. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  52610. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  52611. DYNAMIC_TYPE_TMP_BUFFER));
  52612. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  52613. ExpectIntEQ(pSz, sizeof(pExpected));
  52614. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  52615. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  52616. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  52617. DYNAMIC_TYPE_TMP_BUFFER));
  52618. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  52619. ExpectIntEQ(qSz, sizeof(qExpected));
  52620. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  52621. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  52622. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  52623. DYNAMIC_TYPE_TMP_BUFFER));
  52624. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  52625. ExpectIntEQ(gSz, sizeof(gExpected));
  52626. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  52627. wolfSSL_DH_free(dh);
  52628. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52629. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52630. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52631. #endif
  52632. return EXPECT_RESULT();
  52633. }
  52634. static int test_wolfSSL_PEM_write_DHparams(void)
  52635. {
  52636. EXPECT_DECLS;
  52637. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  52638. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  52639. DH* dh = NULL;
  52640. BIO* bio = NULL;
  52641. XFILE fp = XBADFILE;
  52642. byte pem[2048];
  52643. int pemSz = 0;
  52644. const char expected[] =
  52645. "-----BEGIN DH PARAMETERS-----\n"
  52646. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  52647. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  52648. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  52649. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  52650. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  52651. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  52652. "-----END DH PARAMETERS-----\n";
  52653. const char badPem[] =
  52654. "-----BEGIN DH PARAMETERS-----\n"
  52655. "-----END DH PARAMETERS-----\n";
  52656. const char emptySeqPem[] =
  52657. "-----BEGIN DH PARAMETERS-----\n"
  52658. "MAA=\n"
  52659. "-----END DH PARAMETERS-----\n";
  52660. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  52661. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  52662. if (fp != XBADFILE) {
  52663. XFCLOSE(fp);
  52664. fp = XBADFILE;
  52665. }
  52666. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  52667. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52668. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52669. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  52670. (int)sizeof(badPem));
  52671. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52672. BIO_free(bio);
  52673. bio = NULL;
  52674. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52675. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52676. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  52677. (int)sizeof(emptySeqPem));
  52678. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52679. BIO_free(bio);
  52680. bio = NULL;
  52681. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52682. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  52683. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  52684. BIO_free(bio);
  52685. bio = NULL;
  52686. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  52687. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  52688. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  52689. DH_free(dh);
  52690. dh = NULL;
  52691. dh = wolfSSL_DH_new();
  52692. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  52693. if (fp != XBADFILE) {
  52694. XFCLOSE(fp);
  52695. fp = XBADFILE;
  52696. }
  52697. wolfSSL_DH_free(dh);
  52698. dh = NULL;
  52699. /* check results */
  52700. XMEMSET(pem, 0, sizeof(pem));
  52701. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  52702. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  52703. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  52704. if (fp != XBADFILE)
  52705. XFCLOSE(fp);
  52706. #endif
  52707. return EXPECT_RESULT();
  52708. }
  52709. static int test_wolfSSL_d2i_DHparams(void)
  52710. {
  52711. EXPECT_DECLS;
  52712. #ifdef OPENSSL_ALL
  52713. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  52714. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52715. XFILE f = XBADFILE;
  52716. unsigned char buf[4096];
  52717. const unsigned char* pt = buf;
  52718. #ifdef HAVE_FFDHE_2048
  52719. const char* params1 = "./certs/dh2048.der";
  52720. #endif
  52721. #ifdef HAVE_FFDHE_3072
  52722. const char* params2 = "./certs/dh3072.der";
  52723. #endif
  52724. long len = 0;
  52725. WOLFSSL_DH* dh = NULL;
  52726. XMEMSET(buf, 0, sizeof(buf));
  52727. /* Test 2048 bit parameters */
  52728. #ifdef HAVE_FFDHE_2048
  52729. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  52730. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52731. if (f != XBADFILE) {
  52732. XFCLOSE(f);
  52733. f = XBADFILE;
  52734. }
  52735. /* Valid case */
  52736. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52737. ExpectNotNull(dh->p);
  52738. ExpectNotNull(dh->g);
  52739. ExpectTrue(pt == buf);
  52740. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  52741. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  52742. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  52743. /* Invalid cases */
  52744. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  52745. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  52746. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  52747. DH_free(dh);
  52748. dh = NULL;
  52749. *buf = 0;
  52750. pt = buf;
  52751. #endif /* HAVE_FFDHE_2048 */
  52752. /* Test 3072 bit parameters */
  52753. #ifdef HAVE_FFDHE_3072
  52754. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  52755. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52756. if (f != XBADFILE) {
  52757. XFCLOSE(f);
  52758. f = XBADFILE;
  52759. }
  52760. /* Valid case */
  52761. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  52762. ExpectNotNull(dh->p);
  52763. ExpectNotNull(dh->g);
  52764. ExpectTrue(pt != buf);
  52765. ExpectIntEQ(DH_generate_key(dh), 1);
  52766. /* Invalid cases */
  52767. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  52768. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  52769. DH_free(dh);
  52770. dh = NULL;
  52771. #endif /* HAVE_FFDHE_3072 */
  52772. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52773. #endif /* !NO_DH */
  52774. #endif
  52775. return EXPECT_RESULT();
  52776. }
  52777. static int test_wolfSSL_DH_LoadDer(void)
  52778. {
  52779. EXPECT_DECLS;
  52780. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  52781. defined(OPENSSL_EXTRA)
  52782. static const byte dh2048[] = {
  52783. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  52784. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  52785. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  52786. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  52787. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  52788. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  52789. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  52790. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  52791. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  52792. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  52793. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  52794. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  52795. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  52796. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  52797. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  52798. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  52799. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  52800. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  52801. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  52802. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  52803. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  52804. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  52805. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  52806. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  52807. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  52808. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  52809. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  52810. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  52811. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  52812. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  52813. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  52814. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  52815. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  52816. 0x93, 0x02, 0x01, 0x02
  52817. };
  52818. WOLFSSL_DH* dh = NULL;
  52819. ExpectNotNull(dh = wolfSSL_DH_new());
  52820. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  52821. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  52822. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  52823. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  52824. wolfSSL_DH_free(dh);
  52825. #endif
  52826. return EXPECT_RESULT();
  52827. }
  52828. static int test_wolfSSL_i2d_DHparams(void)
  52829. {
  52830. EXPECT_DECLS;
  52831. #ifdef OPENSSL_ALL
  52832. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  52833. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52834. XFILE f = XBADFILE;
  52835. unsigned char buf[4096];
  52836. const unsigned char* pt;
  52837. unsigned char* pt2;
  52838. #ifdef HAVE_FFDHE_2048
  52839. const char* params1 = "./certs/dh2048.der";
  52840. #endif
  52841. #ifdef HAVE_FFDHE_3072
  52842. const char* params2 = "./certs/dh3072.der";
  52843. #endif
  52844. long len = 0;
  52845. WOLFSSL_DH* dh = NULL;
  52846. /* Test 2048 bit parameters */
  52847. #ifdef HAVE_FFDHE_2048
  52848. pt = buf;
  52849. pt2 = buf;
  52850. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  52851. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52852. if (f != XBADFILE) {
  52853. XFCLOSE(f);
  52854. f = XBADFILE;
  52855. }
  52856. /* Valid case */
  52857. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52858. ExpectTrue(pt == buf);
  52859. ExpectIntEQ(DH_generate_key(dh), 1);
  52860. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  52861. /* Invalid case */
  52862. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  52863. /* Return length only */
  52864. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  52865. DH_free(dh);
  52866. dh = NULL;
  52867. *buf = 0;
  52868. #endif
  52869. /* Test 3072 bit parameters */
  52870. #ifdef HAVE_FFDHE_3072
  52871. pt = buf;
  52872. pt2 = buf;
  52873. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  52874. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52875. if (f != XBADFILE) {
  52876. XFCLOSE(f);
  52877. f = XBADFILE;
  52878. }
  52879. /* Valid case */
  52880. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  52881. ExpectTrue(pt == buf);
  52882. ExpectIntEQ(DH_generate_key(dh), 1);
  52883. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  52884. /* Invalid case */
  52885. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  52886. /* Return length only */
  52887. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  52888. DH_free(dh);
  52889. dh = NULL;
  52890. #endif
  52891. dh = DH_new();
  52892. ExpectNotNull(dh);
  52893. pt2 = buf;
  52894. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  52895. DH_free(dh);
  52896. dh = NULL;
  52897. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52898. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  52899. #endif
  52900. return EXPECT_RESULT();
  52901. }
  52902. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  52903. /*----------------------------------------------------------------------------*
  52904. | EC
  52905. *----------------------------------------------------------------------------*/
  52906. static int test_wolfSSL_EC_GROUP(void)
  52907. {
  52908. EXPECT_DECLS;
  52909. #ifdef OPENSSL_EXTRA
  52910. EC_GROUP *group = NULL;
  52911. EC_GROUP *group2 = NULL;
  52912. EC_GROUP *group3 = NULL;
  52913. #ifndef HAVE_ECC_BRAINPOOL
  52914. EC_GROUP *group4 = NULL;
  52915. #endif
  52916. WOLFSSL_BIGNUM* order = NULL;
  52917. int group_bits;
  52918. int i;
  52919. static const int knownEccNids[] = {
  52920. NID_X9_62_prime192v1,
  52921. NID_X9_62_prime192v2,
  52922. NID_X9_62_prime192v3,
  52923. NID_X9_62_prime239v1,
  52924. NID_X9_62_prime239v2,
  52925. NID_X9_62_prime239v3,
  52926. NID_X9_62_prime256v1,
  52927. NID_secp112r1,
  52928. NID_secp112r2,
  52929. NID_secp128r1,
  52930. NID_secp128r2,
  52931. NID_secp160r1,
  52932. NID_secp160r2,
  52933. NID_secp224r1,
  52934. NID_secp384r1,
  52935. NID_secp521r1,
  52936. NID_secp160k1,
  52937. NID_secp192k1,
  52938. NID_secp224k1,
  52939. NID_secp256k1,
  52940. NID_brainpoolP160r1,
  52941. NID_brainpoolP192r1,
  52942. NID_brainpoolP224r1,
  52943. NID_brainpoolP256r1,
  52944. NID_brainpoolP320r1,
  52945. NID_brainpoolP384r1,
  52946. NID_brainpoolP512r1,
  52947. };
  52948. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  52949. static const int knownEccEnums[] = {
  52950. ECC_SECP192R1,
  52951. ECC_PRIME192V2,
  52952. ECC_PRIME192V3,
  52953. ECC_PRIME239V1,
  52954. ECC_PRIME239V2,
  52955. ECC_PRIME239V3,
  52956. ECC_SECP256R1,
  52957. ECC_SECP112R1,
  52958. ECC_SECP112R2,
  52959. ECC_SECP128R1,
  52960. ECC_SECP128R2,
  52961. ECC_SECP160R1,
  52962. ECC_SECP160R2,
  52963. ECC_SECP224R1,
  52964. ECC_SECP384R1,
  52965. ECC_SECP521R1,
  52966. ECC_SECP160K1,
  52967. ECC_SECP192K1,
  52968. ECC_SECP224K1,
  52969. ECC_SECP256K1,
  52970. ECC_BRAINPOOLP160R1,
  52971. ECC_BRAINPOOLP192R1,
  52972. ECC_BRAINPOOLP224R1,
  52973. ECC_BRAINPOOLP256R1,
  52974. ECC_BRAINPOOLP320R1,
  52975. ECC_BRAINPOOLP384R1,
  52976. ECC_BRAINPOOLP512R1,
  52977. };
  52978. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  52979. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  52980. ExpectNotNull(group2 = EC_GROUP_dup(group));
  52981. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  52982. #ifndef HAVE_ECC_BRAINPOOL
  52983. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  52984. NID_brainpoolP256r1));
  52985. #endif
  52986. ExpectNull(EC_GROUP_dup(NULL));
  52987. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  52988. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  52989. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  52990. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  52991. #ifndef HAVE_ECC_BRAINPOOL
  52992. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  52993. #endif
  52994. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  52995. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  52996. ExpectNotNull(order = BN_new());
  52997. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  52998. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  52999. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  53000. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  53001. wolfSSL_BN_free(order);
  53002. ExpectNotNull(EC_GROUP_method_of(group));
  53003. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  53004. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  53005. NID_X9_62_prime_field);
  53006. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  53007. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  53008. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  53009. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  53010. #ifndef NO_WOLFSSL_STUB
  53011. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  53012. #endif
  53013. #ifndef HAVE_ECC_BRAINPOOL
  53014. EC_GROUP_free(group4);
  53015. #endif
  53016. EC_GROUP_free(group3);
  53017. EC_GROUP_free(group2);
  53018. EC_GROUP_free(group);
  53019. for (i = 0; i < knowEccNidsLen; i++) {
  53020. group = NULL;
  53021. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  53022. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  53023. EC_GROUP_free(group);
  53024. }
  53025. for (i = 0; i < knowEccEnumsLen; i++) {
  53026. group = NULL;
  53027. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  53028. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  53029. EC_GROUP_free(group);
  53030. }
  53031. #endif
  53032. return EXPECT_RESULT();
  53033. }
  53034. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  53035. {
  53036. EXPECT_DECLS;
  53037. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53038. EC_GROUP *group = NULL;
  53039. BIO* bio = NULL;
  53040. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53041. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53042. EC_GROUP *ret = NULL;
  53043. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  53044. "BgUrgQQAIg==\n"
  53045. "-----END EC PARAMETERS-----";
  53046. #endif
  53047. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  53048. "MAA=\n"
  53049. "-----END EC PARAMETERS-----";
  53050. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  53051. "BgA=\n"
  53052. "-----END EC PARAMETERS-----";
  53053. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  53054. "BgE=\n"
  53055. "-----END EC PARAMETERS-----";
  53056. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  53057. "BgE*\n"
  53058. "-----END EC PARAMETERS-----";
  53059. /* Test that first parameter, bio, being NULL fails. */
  53060. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  53061. /* Test that reading named parameters works. */
  53062. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  53063. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  53064. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53065. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53066. BIO_free(bio);
  53067. bio = NULL;
  53068. EC_GROUP_free(group);
  53069. group = NULL;
  53070. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53071. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53072. /* Test that reusing group works. */
  53073. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53074. sizeof(ec_nc_p384)));
  53075. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53076. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53077. BIO_free(bio);
  53078. bio = NULL;
  53079. EC_GROUP_free(group);
  53080. group = NULL;
  53081. /* Test that returning through group works. */
  53082. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53083. sizeof(ec_nc_p384)));
  53084. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53085. ExpectIntEQ(group == ret, 1);
  53086. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53087. BIO_free(bio);
  53088. bio = NULL;
  53089. EC_GROUP_free(group);
  53090. group = NULL;
  53091. #endif
  53092. /* Test 0x30, 0x00 (not and object id) fails. */
  53093. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  53094. sizeof(ec_nc_bad_1)));
  53095. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53096. BIO_free(bio);
  53097. bio = NULL;
  53098. /* Test 0x06, 0x00 (empty object id) fails. */
  53099. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  53100. sizeof(ec_nc_bad_2)));
  53101. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53102. BIO_free(bio);
  53103. bio = NULL;
  53104. /* Test 0x06, 0x01 (badly formed object id) fails. */
  53105. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  53106. sizeof(ec_nc_bad_3)));
  53107. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53108. BIO_free(bio);
  53109. bio = NULL;
  53110. /* Test invalid PEM encoding - invalid character. */
  53111. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  53112. sizeof(ec_nc_bad_4)));
  53113. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53114. BIO_free(bio);
  53115. #endif
  53116. return EXPECT_RESULT();
  53117. }
  53118. static int test_wolfSSL_EC_POINT(void)
  53119. {
  53120. EXPECT_DECLS;
  53121. #if !defined(WOLFSSL_SP_MATH) && \
  53122. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  53123. #ifdef OPENSSL_EXTRA
  53124. BN_CTX* ctx = NULL;
  53125. EC_GROUP* group = NULL;
  53126. #ifndef HAVE_ECC_BRAINPOOL
  53127. EC_GROUP* group2 = NULL;
  53128. #endif
  53129. EC_POINT* Gxy = NULL;
  53130. EC_POINT* new_point = NULL;
  53131. EC_POINT* set_point = NULL;
  53132. EC_POINT* infinity = NULL;
  53133. BIGNUM* k = NULL;
  53134. BIGNUM* Gx = NULL;
  53135. BIGNUM* Gy = NULL;
  53136. BIGNUM* Gz = NULL;
  53137. BIGNUM* X = NULL;
  53138. BIGNUM* Y = NULL;
  53139. BIGNUM* set_point_bn = NULL;
  53140. char* hexStr = NULL;
  53141. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  53142. "17AF381913FF7A96314EA47055EA0FD0";
  53143. /* NISTP256R1 Gx/Gy */
  53144. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  53145. "77037D812DEB33A0F4A13945D898C296";
  53146. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53147. "2BCE33576B315ECECBB6406837BF51F5";
  53148. #ifndef HAVE_SELFTEST
  53149. EC_POINT *tmp = NULL;
  53150. size_t bin_len;
  53151. unsigned int blen = 0;
  53152. unsigned char* buf = NULL;
  53153. unsigned char bufInf[1] = { 0x00 };
  53154. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  53155. "77037D812DEB33A0F4A13945D898C296"
  53156. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53157. "2BCE33576B315ECECBB6406837BF51F5";
  53158. const unsigned char binUncompG[] = {
  53159. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53160. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53161. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53162. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53163. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53164. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53165. };
  53166. const unsigned char binUncompGBad[] = {
  53167. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53168. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53169. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53170. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53171. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53172. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53173. };
  53174. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  53175. "77037D812DEB33A0F4A13945D898C296";
  53176. #ifdef HAVE_COMP_KEY
  53177. const unsigned char binCompG[] = {
  53178. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53179. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53180. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53181. };
  53182. #endif
  53183. #endif
  53184. ExpectNotNull(ctx = BN_CTX_new());
  53185. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53186. #ifndef HAVE_ECC_BRAINPOOL
  53187. /* Used to make groups curve_idx == -1. */
  53188. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  53189. #endif
  53190. ExpectNull(EC_POINT_new(NULL));
  53191. ExpectNotNull(Gxy = EC_POINT_new(group));
  53192. ExpectNotNull(new_point = EC_POINT_new(group));
  53193. ExpectNotNull(set_point = EC_POINT_new(group));
  53194. ExpectNotNull(X = BN_new());
  53195. ExpectNotNull(Y = BN_new());
  53196. ExpectNotNull(set_point_bn = BN_new());
  53197. ExpectNotNull(infinity = EC_POINT_new(group));
  53198. /* load test values */
  53199. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  53200. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  53201. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  53202. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  53203. /* populate coordinates for input point */
  53204. if (Gxy != NULL) {
  53205. Gxy->X = Gx;
  53206. Gxy->Y = Gy;
  53207. Gxy->Z = Gz;
  53208. }
  53209. /* Test handling of NULL point. */
  53210. EC_POINT_clear_free(NULL);
  53211. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53212. NULL, NULL, ctx), 0);
  53213. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53214. NULL, NULL, ctx), 0);
  53215. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53216. NULL, NULL, ctx), 0);
  53217. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53218. X, NULL, ctx), 0);
  53219. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53220. NULL, Y, ctx), 0);
  53221. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53222. X, Y, ctx), 0);
  53223. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53224. X, Y, ctx), 0);
  53225. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53226. NULL, Y, ctx), 0);
  53227. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53228. X, NULL, ctx), 0);
  53229. /* Getting point at infinity returns an error. */
  53230. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  53231. X, Y, ctx), 0);
  53232. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53233. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53234. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53235. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  53236. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  53237. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  53238. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  53239. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  53240. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  53241. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  53242. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  53243. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  53244. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  53245. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  53246. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  53247. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  53248. /* perform point multiplication */
  53249. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  53250. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53251. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53252. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53253. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  53254. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53255. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53256. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53257. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  53258. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53259. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53260. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53261. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  53262. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  53263. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  53264. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  53265. /* Set point to something. */
  53266. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  53267. #else
  53268. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  53269. ctx), 1);
  53270. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53271. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53272. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53273. #endif
  53274. /* check if point X coordinate is zero */
  53275. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53276. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  53277. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  53278. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  53279. #endif
  53280. /* extract the coordinates from point */
  53281. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  53282. ctx), WOLFSSL_SUCCESS);
  53283. /* check if point X coordinate is zero */
  53284. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  53285. /* set the same X and Y points in another object */
  53286. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  53287. ctx), WOLFSSL_SUCCESS);
  53288. /* compare points as they should be the same */
  53289. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  53290. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  53291. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  53292. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  53293. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  53294. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  53295. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  53296. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  53297. /* Test copying */
  53298. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  53299. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  53300. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  53301. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  53302. /* Test inverting */
  53303. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  53304. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  53305. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  53306. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  53307. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53308. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53309. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53310. {
  53311. EC_POINT* orig_point = NULL;
  53312. ExpectNotNull(orig_point = EC_POINT_new(group));
  53313. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  53314. 1);
  53315. /* new_point should be set_point inverted so adding it will revert
  53316. * the point back to set_point */
  53317. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  53318. NULL), 1);
  53319. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  53320. EC_POINT_free(orig_point);
  53321. }
  53322. #endif
  53323. /* Test getting affine converts from projective. */
  53324. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  53325. /* Force non-affine coordinates */
  53326. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  53327. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  53328. if (new_point != NULL) {
  53329. new_point->inSet = 0;
  53330. }
  53331. /* extract the coordinates from point */
  53332. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  53333. ctx), WOLFSSL_SUCCESS);
  53334. /* check if point ordinates have changed. */
  53335. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  53336. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  53337. /* Test check for infinity */
  53338. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  53339. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  53340. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  53341. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  53342. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  53343. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  53344. #else
  53345. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  53346. #endif
  53347. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  53348. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  53349. /* check bn2hex */
  53350. hexStr = BN_bn2hex(k);
  53351. ExpectStrEQ(hexStr, kTest);
  53352. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  53353. defined(XFPRINTF)
  53354. BN_print_fp(stderr, k);
  53355. fprintf(stderr, "\n");
  53356. #endif
  53357. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53358. hexStr = BN_bn2hex(Gx);
  53359. ExpectStrEQ(hexStr, kGx);
  53360. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  53361. defined(XFPRINTF)
  53362. BN_print_fp(stderr, Gx);
  53363. fprintf(stderr, "\n");
  53364. #endif
  53365. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53366. hexStr = BN_bn2hex(Gy);
  53367. ExpectStrEQ(hexStr, kGy);
  53368. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  53369. defined(XFPRINTF)
  53370. BN_print_fp(stderr, Gy);
  53371. fprintf(stderr, "\n");
  53372. #endif
  53373. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53374. #ifndef HAVE_SELFTEST
  53375. /* Test point to hex */
  53376. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  53377. ctx));
  53378. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53379. ctx));
  53380. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  53381. ctx));
  53382. #ifndef HAVE_ECC_BRAINPOOL
  53383. /* Group not supported in wolfCrypt. */
  53384. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53385. ctx));
  53386. #endif
  53387. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  53388. ExpectNotNull(hexStr);
  53389. ExpectStrEQ(hexStr, uncompG);
  53390. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53391. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  53392. ExpectNotNull(hexStr);
  53393. ExpectStrEQ(hexStr, compG);
  53394. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  53395. /* Test point to oct */
  53396. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  53397. NULL, 0, ctx), 0);
  53398. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53399. NULL, 0, ctx), 0);
  53400. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  53401. NULL, 0, ctx), 0);
  53402. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53403. NULL, 0, ctx);
  53404. ExpectIntEQ(bin_len, sizeof(binUncompG));
  53405. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  53406. DYNAMIC_TYPE_ECC));
  53407. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  53408. buf, bin_len, ctx), bin_len);
  53409. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  53410. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  53411. /* Infinity (x=0, y=0) encodes as '0x00'. */
  53412. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  53413. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  53414. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  53415. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  53416. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  53417. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  53418. ExpectIntEQ(bufInf[0], 0);
  53419. wolfSSL_EC_POINT_dump(NULL, NULL);
  53420. /* Test point i2d */
  53421. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  53422. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  53423. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  53424. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  53425. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  53426. ExpectIntEQ(blen, sizeof(binUncompG));
  53427. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  53428. blen -= 1;
  53429. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  53430. blen += 1;
  53431. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  53432. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  53433. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  53434. #ifdef HAVE_COMP_KEY
  53435. /* Test point to oct compressed */
  53436. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  53437. 0, ctx);
  53438. ExpectIntEQ(bin_len, sizeof(binCompG));
  53439. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  53440. DYNAMIC_TYPE_ECC));
  53441. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  53442. bin_len, ctx), bin_len);
  53443. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  53444. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  53445. #endif
  53446. /* Test point BN */
  53447. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  53448. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  53449. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  53450. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  53451. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  53452. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  53453. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  53454. /* Test oct to point */
  53455. ExpectNotNull(tmp = EC_POINT_new(group));
  53456. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  53457. ctx), 0);
  53458. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  53459. ctx), 0);
  53460. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  53461. ctx), 0);
  53462. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  53463. sizeof(binUncompGBad), ctx), 0);
  53464. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  53465. ctx), 1);
  53466. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  53467. EC_POINT_free(tmp);
  53468. tmp = NULL;
  53469. /* Test setting BN ordinates. */
  53470. ExpectNotNull(tmp = EC_POINT_new(group));
  53471. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  53472. NULL, ctx), 0);
  53473. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  53474. NULL, ctx), 0);
  53475. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  53476. NULL, ctx), 0);
  53477. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  53478. NULL, ctx), 0);
  53479. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  53480. Gy, ctx), 0);
  53481. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  53482. ctx), 0);
  53483. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  53484. ctx), 0);
  53485. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  53486. Gy, ctx), 0);
  53487. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  53488. NULL, ctx), 0);
  53489. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  53490. ctx), 1);
  53491. EC_POINT_free(tmp);
  53492. tmp = NULL;
  53493. /* Test point d2i */
  53494. ExpectNotNull(tmp = EC_POINT_new(group));
  53495. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  53496. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  53497. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  53498. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  53499. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  53500. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  53501. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  53502. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  53503. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  53504. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  53505. EC_POINT_free(tmp);
  53506. tmp = NULL;
  53507. #ifdef HAVE_COMP_KEY
  53508. /* Test oct compressed to point */
  53509. ExpectNotNull(tmp = EC_POINT_new(group));
  53510. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  53511. 1);
  53512. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  53513. EC_POINT_free(tmp);
  53514. tmp = NULL;
  53515. /* Test point d2i - compressed */
  53516. ExpectNotNull(tmp = EC_POINT_new(group));
  53517. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  53518. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  53519. EC_POINT_free(tmp);
  53520. tmp = NULL;
  53521. #endif
  53522. #endif
  53523. /* test BN_mod_add */
  53524. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  53525. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  53526. 1);
  53527. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  53528. /* cleanup */
  53529. BN_free(X);
  53530. BN_free(Y);
  53531. BN_free(k);
  53532. BN_free(set_point_bn);
  53533. EC_POINT_free(infinity);
  53534. EC_POINT_free(new_point);
  53535. EC_POINT_free(set_point);
  53536. EC_POINT_clear_free(Gxy);
  53537. #ifndef HAVE_ECC_BRAINPOOL
  53538. EC_GROUP_free(group2);
  53539. #endif
  53540. EC_GROUP_free(group);
  53541. BN_CTX_free(ctx);
  53542. #endif
  53543. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  53544. return EXPECT_RESULT();
  53545. }
  53546. static int test_wolfSSL_SPAKE(void)
  53547. {
  53548. EXPECT_DECLS;
  53549. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  53550. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  53551. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53552. BIGNUM* x = NULL; /* kdc priv */
  53553. BIGNUM* y = NULL; /* client priv */
  53554. BIGNUM* w = NULL; /* shared value */
  53555. byte M_bytes[] = {
  53556. /* uncompressed */
  53557. 0x04,
  53558. /* x */
  53559. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  53560. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  53561. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  53562. /* y */
  53563. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  53564. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  53565. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  53566. };
  53567. EC_POINT* M = NULL; /* shared value */
  53568. byte N_bytes[] = {
  53569. /* uncompressed */
  53570. 0x04,
  53571. /* x */
  53572. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  53573. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  53574. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  53575. /* y */
  53576. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  53577. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  53578. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  53579. };
  53580. EC_POINT* N = NULL; /* shared value */
  53581. EC_POINT* T = NULL; /* kdc pub */
  53582. EC_POINT* tmp1 = NULL; /* kdc pub */
  53583. EC_POINT* tmp2 = NULL; /* kdc pub */
  53584. EC_POINT* S = NULL; /* client pub */
  53585. EC_POINT* client_secret = NULL;
  53586. EC_POINT* kdc_secret = NULL;
  53587. EC_GROUP* group = NULL;
  53588. BN_CTX* bn_ctx = NULL;
  53589. /* Values taken from a test run of Kerberos 5 */
  53590. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53591. ExpectNotNull(bn_ctx = BN_CTX_new());
  53592. ExpectNotNull(M = EC_POINT_new(group));
  53593. ExpectNotNull(N = EC_POINT_new(group));
  53594. ExpectNotNull(T = EC_POINT_new(group));
  53595. ExpectNotNull(tmp1 = EC_POINT_new(group));
  53596. ExpectNotNull(tmp2 = EC_POINT_new(group));
  53597. ExpectNotNull(S = EC_POINT_new(group));
  53598. ExpectNotNull(client_secret = EC_POINT_new(group));
  53599. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  53600. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  53601. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  53602. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  53603. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  53604. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  53605. "506AB395478F0AAB647752CF117B36250"), 1);
  53606. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  53607. 1);
  53608. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  53609. 1);
  53610. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  53611. /* kdc */
  53612. /* T=x*P+w*M */
  53613. /* All in one function call */
  53614. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  53615. /* Spread into separate calls */
  53616. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  53617. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  53618. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  53619. 1);
  53620. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  53621. /* client */
  53622. /* S=y*P+w*N */
  53623. /* All in one function call */
  53624. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  53625. /* Spread into separate calls */
  53626. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  53627. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  53628. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  53629. 1);
  53630. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  53631. /* K=y*(T-w*M) */
  53632. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  53633. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  53634. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  53635. 1);
  53636. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  53637. bn_ctx), 1);
  53638. /* kdc */
  53639. /* K=x*(S-w*N) */
  53640. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  53641. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  53642. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  53643. 1);
  53644. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  53645. 1);
  53646. /* kdc_secret == client_secret */
  53647. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  53648. BN_free(x);
  53649. BN_free(y);
  53650. BN_free(w);
  53651. EC_POINT_free(M);
  53652. EC_POINT_free(N);
  53653. EC_POINT_free(T);
  53654. EC_POINT_free(tmp1);
  53655. EC_POINT_free(tmp2);
  53656. EC_POINT_free(S);
  53657. EC_POINT_free(client_secret);
  53658. EC_POINT_free(kdc_secret);
  53659. EC_GROUP_free(group);
  53660. BN_CTX_free(bn_ctx);
  53661. #endif
  53662. return EXPECT_RESULT();
  53663. }
  53664. static int test_wolfSSL_EC_KEY_generate(void)
  53665. {
  53666. EXPECT_DECLS;
  53667. #ifdef OPENSSL_EXTRA
  53668. WOLFSSL_EC_KEY* key = NULL;
  53669. #ifndef HAVE_ECC_BRAINPOOL
  53670. WOLFSSL_EC_GROUP* group = NULL;
  53671. #endif
  53672. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53673. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  53674. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  53675. wolfSSL_EC_KEY_free(key);
  53676. key = NULL;
  53677. #ifndef HAVE_ECC_BRAINPOOL
  53678. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  53679. NID_brainpoolP256r1));
  53680. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  53681. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  53682. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  53683. wolfSSL_EC_KEY_free(key);
  53684. wolfSSL_EC_GROUP_free(group);
  53685. #endif
  53686. #endif
  53687. return EXPECT_RESULT();
  53688. }
  53689. static int test_EC_i2d(void)
  53690. {
  53691. EXPECT_DECLS;
  53692. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  53693. EC_KEY *key = NULL;
  53694. EC_KEY *copy = NULL;
  53695. int len = 0;
  53696. unsigned char *buf = NULL;
  53697. unsigned char *p = NULL;
  53698. const unsigned char *tmp = NULL;
  53699. const unsigned char octBad[] = {
  53700. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53701. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53702. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53703. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53704. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53705. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53706. };
  53707. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53708. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  53709. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  53710. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53711. DYNAMIC_TYPE_TMP_BUFFER));
  53712. p = buf;
  53713. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  53714. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  53715. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  53716. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  53717. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  53718. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  53719. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  53720. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  53721. tmp = buf;
  53722. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  53723. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  53724. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  53725. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  53726. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  53727. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  53728. tmp = buf;
  53729. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  53730. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  53731. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  53732. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  53733. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  53734. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  53735. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  53736. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  53737. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  53738. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  53739. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  53740. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  53741. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  53742. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53743. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  53744. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53745. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  53746. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53747. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  53748. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53749. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  53750. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  53751. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  53752. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  53753. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  53754. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  53755. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  53756. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53757. buf = NULL;
  53758. buf = NULL;
  53759. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  53760. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53761. DYNAMIC_TYPE_TMP_BUFFER));
  53762. p = buf;
  53763. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  53764. p = NULL;
  53765. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  53766. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53767. p = NULL;
  53768. /* Bad point is also an invalid private key. */
  53769. tmp = octBad;
  53770. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  53771. tmp = buf;
  53772. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  53773. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53774. buf = NULL;
  53775. buf = NULL;
  53776. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  53777. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  53778. DYNAMIC_TYPE_TMP_BUFFER));
  53779. p = buf;
  53780. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  53781. p = NULL;
  53782. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  53783. tmp = buf;
  53784. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  53785. tmp = octBad;
  53786. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  53787. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  53788. ExpectIntEQ(EC_KEY_check_key(key), 1);
  53789. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  53790. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  53791. EC_KEY_free(key);
  53792. EC_KEY_free(copy);
  53793. #endif
  53794. return EXPECT_RESULT();
  53795. }
  53796. static int test_wolfSSL_EC_curve(void)
  53797. {
  53798. EXPECT_DECLS;
  53799. #if defined(OPENSSL_EXTRA)
  53800. int nid = NID_secp160k1;
  53801. const char* nid_name = NULL;
  53802. ExpectNull(EC_curve_nid2nist(NID_sha256));
  53803. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  53804. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  53805. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  53806. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  53807. #endif
  53808. return EXPECT_RESULT();
  53809. }
  53810. static int test_wolfSSL_EC_KEY_dup(void)
  53811. {
  53812. EXPECT_DECLS;
  53813. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  53814. WOLFSSL_EC_KEY* ecKey = NULL;
  53815. WOLFSSL_EC_KEY* dupKey = NULL;
  53816. ecc_key* srcKey = NULL;
  53817. ecc_key* destKey = NULL;
  53818. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53819. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53820. /* Valid cases */
  53821. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53822. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  53823. /* Compare pubkey */
  53824. if (ecKey != NULL) {
  53825. srcKey = (ecc_key*)ecKey->internal;
  53826. }
  53827. if (dupKey != NULL) {
  53828. destKey = (ecc_key*)dupKey->internal;
  53829. }
  53830. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  53831. /* compare EC_GROUP */
  53832. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  53833. /* compare EC_POINT */
  53834. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  53835. dupKey->pub_key, NULL), MP_EQ);
  53836. /* compare BIGNUM */
  53837. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  53838. wolfSSL_EC_KEY_free(dupKey);
  53839. dupKey = NULL;
  53840. /* Invalid cases */
  53841. /* NULL key */
  53842. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  53843. /* NULL ecc_key */
  53844. if (ecKey != NULL) {
  53845. wc_ecc_free((ecc_key*)ecKey->internal);
  53846. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  53847. ecKey->internal = NULL; /* Set ecc_key to NULL */
  53848. }
  53849. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53850. wolfSSL_EC_KEY_free(ecKey);
  53851. ecKey = NULL;
  53852. wolfSSL_EC_KEY_free(dupKey);
  53853. dupKey = NULL;
  53854. /* NULL Group */
  53855. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53856. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53857. if (ecKey != NULL) {
  53858. wolfSSL_EC_GROUP_free(ecKey->group);
  53859. ecKey->group = NULL; /* Set group to NULL */
  53860. }
  53861. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53862. wolfSSL_EC_KEY_free(ecKey);
  53863. ecKey = NULL;
  53864. wolfSSL_EC_KEY_free(dupKey);
  53865. dupKey = NULL;
  53866. /* NULL public key */
  53867. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53868. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53869. if (ecKey != NULL) {
  53870. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  53871. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  53872. }
  53873. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53874. if (ecKey != NULL) {
  53875. wolfSSL_EC_POINT_free(ecKey->pub_key);
  53876. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  53877. }
  53878. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53879. wolfSSL_EC_KEY_free(ecKey);
  53880. ecKey = NULL;
  53881. wolfSSL_EC_KEY_free(dupKey);
  53882. dupKey = NULL;
  53883. /* NULL private key */
  53884. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53885. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  53886. if (ecKey != NULL) {
  53887. wolfSSL_BN_free(ecKey->priv_key);
  53888. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  53889. }
  53890. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53891. wolfSSL_EC_KEY_free(ecKey);
  53892. ecKey = NULL;
  53893. wolfSSL_EC_KEY_free(dupKey);
  53894. dupKey = NULL;
  53895. /* Test EC_KEY_up_ref */
  53896. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  53897. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  53898. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  53899. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  53900. /* reference count doesn't follow duplicate */
  53901. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  53902. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  53903. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  53904. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  53905. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  53906. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  53907. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  53908. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  53909. #endif
  53910. return EXPECT_RESULT();
  53911. }
  53912. static int test_wolfSSL_EC_KEY_set_group(void)
  53913. {
  53914. EXPECT_DECLS;
  53915. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  53916. defined(OPENSSL_EXTRA)
  53917. EC_KEY *key = NULL;
  53918. EC_GROUP *group = NULL;
  53919. const EC_GROUP *group2 = NULL;
  53920. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53921. ExpectNotNull(key = EC_KEY_new());
  53922. ExpectNull(EC_KEY_get0_group(NULL));
  53923. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  53924. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  53925. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  53926. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  53927. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  53928. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  53929. EC_GROUP_free(group);
  53930. EC_KEY_free(key);
  53931. #endif
  53932. return EXPECT_RESULT();
  53933. }
  53934. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  53935. {
  53936. EXPECT_DECLS;
  53937. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53938. BIO* bio = NULL;
  53939. EC_KEY* key = NULL;
  53940. /* Error condition: NULL key. */
  53941. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  53942. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  53943. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  53944. /* Conversion form defaults to uncompressed. */
  53945. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53946. #ifdef HAVE_COMP_KEY
  53947. /* Explicitly set to compressed. */
  53948. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  53949. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  53950. #else
  53951. /* Will still work just won't change anything. */
  53952. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  53953. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53954. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  53955. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  53956. #endif
  53957. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  53958. BIO_free(bio);
  53959. EC_KEY_free(key);
  53960. #endif
  53961. return EXPECT_RESULT();
  53962. }
  53963. static int test_wolfSSL_EC_KEY_private_key(void)
  53964. {
  53965. EXPECT_DECLS;
  53966. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53967. WOLFSSL_EC_KEY* key = NULL;
  53968. WOLFSSL_BIGNUM* priv = NULL;
  53969. WOLFSSL_BIGNUM* priv2 = NULL;
  53970. WOLFSSL_BIGNUM* bn;
  53971. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  53972. ExpectNotNull(priv = wolfSSL_BN_new());
  53973. ExpectNotNull(priv2 = wolfSSL_BN_new());
  53974. ExpectIntNE(BN_set_word(priv, 2), 0);
  53975. ExpectIntNE(BN_set_word(priv2, 2), 0);
  53976. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  53977. /* No private key set. */
  53978. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  53979. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  53980. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  53981. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  53982. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  53983. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53984. ExpectPtrNE(bn, priv);
  53985. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  53986. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  53987. ExpectPtrNE(bn, priv2);
  53988. wolfSSL_BN_free(priv2);
  53989. wolfSSL_BN_free(priv);
  53990. wolfSSL_EC_KEY_free(key);
  53991. #endif
  53992. return EXPECT_RESULT();
  53993. }
  53994. static int test_wolfSSL_EC_KEY_public_key(void)
  53995. {
  53996. EXPECT_DECLS;
  53997. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53998. WOLFSSL_EC_KEY* key = NULL;
  53999. WOLFSSL_EC_POINT* pub = NULL;
  54000. WOLFSSL_EC_POINT* point = NULL;
  54001. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54002. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  54003. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  54004. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54005. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  54006. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  54007. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  54008. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  54009. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  54010. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  54011. ExpectPtrEq(point, pub);
  54012. wolfSSL_EC_KEY_free(key);
  54013. #endif
  54014. return EXPECT_RESULT();
  54015. }
  54016. static int test_wolfSSL_EC_KEY_print_fp(void)
  54017. {
  54018. EXPECT_DECLS;
  54019. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  54020. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  54021. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  54022. !defined(NO_STDIO_FILESYSTEM)
  54023. EC_KEY* key = NULL;
  54024. /* Bad file pointer. */
  54025. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  54026. /* NULL key. */
  54027. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  54028. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  54029. /* Negative indent. */
  54030. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  54031. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54032. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54033. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54034. wolfSSL_EC_KEY_free(key);
  54035. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  54036. NID_X9_62_prime256v1)));
  54037. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54038. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54039. wolfSSL_EC_KEY_free(key);
  54040. #endif
  54041. return EXPECT_RESULT();
  54042. }
  54043. static int test_wolfSSL_EC_get_builtin_curves(void)
  54044. {
  54045. EXPECT_DECLS;
  54046. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54047. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54048. EC_builtin_curve* curves = NULL;
  54049. size_t crv_len = 0;
  54050. size_t i = 0;
  54051. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  54052. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  54053. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  54054. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  54055. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  54056. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  54057. if (curves[i].comment != NULL) {
  54058. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  54059. }
  54060. }
  54061. if (crv_len > 1) {
  54062. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  54063. }
  54064. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54065. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54066. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  54067. return EXPECT_RESULT();
  54068. }
  54069. static int test_wolfSSL_ECDSA_SIG(void)
  54070. {
  54071. EXPECT_DECLS;
  54072. #ifdef OPENSSL_EXTRA
  54073. WOLFSSL_ECDSA_SIG* sig = NULL;
  54074. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  54075. WOLFSSL_BIGNUM* r = NULL;
  54076. WOLFSSL_BIGNUM* s = NULL;
  54077. const WOLFSSL_BIGNUM* r2 = NULL;
  54078. const WOLFSSL_BIGNUM* s2 = NULL;
  54079. const unsigned char* cp = NULL;
  54080. unsigned char* p = NULL;
  54081. unsigned char outSig[8];
  54082. unsigned char sigData[8] =
  54083. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54084. unsigned char sigDataBad[8] =
  54085. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54086. wolfSSL_ECDSA_SIG_free(NULL);
  54087. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  54088. ExpectNotNull(r = wolfSSL_BN_new());
  54089. ExpectNotNull(s = wolfSSL_BN_new());
  54090. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  54091. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  54092. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  54093. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  54094. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  54095. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54096. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  54097. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  54098. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  54099. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  54100. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  54101. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  54102. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  54103. r2 = NULL;
  54104. s2 = NULL;
  54105. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54106. ExpectNull(r2);
  54107. ExpectNull(s2);
  54108. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  54109. if (EXPECT_FAIL()) {
  54110. wolfSSL_BN_free(r);
  54111. wolfSSL_BN_free(s);
  54112. }
  54113. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  54114. ExpectPtrEq(r2, r);
  54115. ExpectPtrEq(s2, s);
  54116. r2 = NULL;
  54117. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  54118. ExpectPtrEq(r2, r);
  54119. s2 = NULL;
  54120. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  54121. ExpectPtrEq(s2, s);
  54122. /* r and s are freed when sig is freed. */
  54123. wolfSSL_ECDSA_SIG_free(sig);
  54124. sig = NULL;
  54125. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  54126. cp = sigDataBad;
  54127. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  54128. cp = sigData;
  54129. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  54130. ExpectIntEQ((cp == sigData + 8), 1);
  54131. cp = sigData;
  54132. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  54133. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  54134. ExpectIntEQ((sig == sig2), 1);
  54135. cp = outSig;
  54136. p = outSig;
  54137. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  54138. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  54139. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  54140. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  54141. ExpectIntEQ((p == outSig + 8), 1);
  54142. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  54143. wolfSSL_ECDSA_SIG_free(sig);
  54144. #endif
  54145. return EXPECT_RESULT();
  54146. }
  54147. static int test_ECDSA_size_sign(void)
  54148. {
  54149. EXPECT_DECLS;
  54150. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  54151. EC_KEY* key = NULL;
  54152. ECDSA_SIG* ecdsaSig = NULL;
  54153. int id;
  54154. byte hash[WC_MAX_DIGEST_SIZE];
  54155. byte hash2[WC_MAX_DIGEST_SIZE];
  54156. byte sig[ECC_MAX_SIG_SIZE];
  54157. unsigned int sigSz = sizeof(sig);
  54158. XMEMSET(hash, 123, sizeof(hash));
  54159. XMEMSET(hash2, 234, sizeof(hash2));
  54160. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  54161. ExpectIntEQ(id, ECC_SECP256R1);
  54162. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54163. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54164. ExpectIntGE(ECDSA_size(NULL), 0);
  54165. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  54166. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  54167. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  54168. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  54169. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  54170. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  54171. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  54172. ExpectIntGE(ECDSA_size(key), sigSz);
  54173. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  54174. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  54175. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  54176. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  54177. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  54178. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  54179. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  54180. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  54181. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  54182. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  54183. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  54184. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  54185. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  54186. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  54187. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  54188. ECDSA_SIG_free(ecdsaSig);
  54189. EC_KEY_free(key);
  54190. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  54191. return EXPECT_RESULT();
  54192. }
  54193. static int test_ECDH_compute_key(void)
  54194. {
  54195. EXPECT_DECLS;
  54196. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54197. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54198. EC_KEY* key1 = NULL;
  54199. EC_KEY* key2 = NULL;
  54200. EC_POINT* pub1 = NULL;
  54201. EC_POINT* pub2 = NULL;
  54202. byte secret1[32];
  54203. byte secret2[32];
  54204. int i;
  54205. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54206. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  54207. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  54208. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54209. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  54210. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  54211. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  54212. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  54213. 0);
  54214. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  54215. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  54216. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  54217. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  54218. 0);
  54219. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  54220. 0);
  54221. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  54222. NULL), 0);
  54223. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  54224. sizeof(secret1));
  54225. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  54226. sizeof(secret2));
  54227. for (i = 0; i < (int)sizeof(secret1); i++) {
  54228. ExpectIntEQ(secret1[i], secret2[i]);
  54229. }
  54230. EC_KEY_free(key2);
  54231. EC_KEY_free(key1);
  54232. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  54233. * !WOLF_CRYPTO_CB_ONLY_ECC */
  54234. return EXPECT_RESULT();
  54235. }
  54236. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  54237. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  54238. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  54239. !defined(NO_ASN_TIME)
  54240. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  54241. int expectedDerSz)
  54242. {
  54243. EXPECT_DECLS;
  54244. X509* x509 = NULL;
  54245. BIGNUM* serial_number = NULL;
  54246. X509_NAME* name = NULL;
  54247. time_t epoch_off = 0;
  54248. ASN1_INTEGER* asn1_serial_number;
  54249. long not_before, not_after;
  54250. int derSz;
  54251. ExpectNotNull(x509 = X509_new());
  54252. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  54253. ExpectNotNull(serial_number = BN_new());
  54254. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  54255. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  54256. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  54257. /* version 3 */
  54258. ExpectIntNE(X509_set_version(x509, 2L), 0);
  54259. ExpectNotNull(name = X509_NAME_new());
  54260. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  54261. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  54262. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  54263. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  54264. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  54265. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  54266. not_before = (long)wc_Time(NULL);
  54267. not_after = not_before + (365 * 24 * 60 * 60);
  54268. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  54269. &epoch_off));
  54270. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  54271. &epoch_off));
  54272. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  54273. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  54274. ExpectIntGE(derSz, expectedDerSz);
  54275. BN_free(serial_number);
  54276. X509_NAME_free(name);
  54277. X509_free(x509);
  54278. return EXPECT_RESULT();
  54279. }
  54280. #endif
  54281. static int test_openssl_generate_key_and_cert(void)
  54282. {
  54283. EXPECT_DECLS;
  54284. #if defined(OPENSSL_EXTRA)
  54285. int expectedDerSz;
  54286. EVP_PKEY* pkey = NULL;
  54287. #ifdef HAVE_ECC
  54288. EC_KEY* ec_key = NULL;
  54289. #endif
  54290. #if !defined(NO_RSA)
  54291. int key_length = 2048;
  54292. BIGNUM* exponent = NULL;
  54293. RSA* rsa = NULL;
  54294. ExpectNotNull(pkey = EVP_PKEY_new());
  54295. ExpectNotNull(exponent = BN_new());
  54296. ExpectNotNull(rsa = RSA_new());
  54297. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  54298. #ifndef WOLFSSL_KEY_GEN
  54299. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  54300. #if defined(USE_CERT_BUFFERS_1024)
  54301. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  54302. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  54303. key_length = 1024;
  54304. #elif defined(USE_CERT_BUFFERS_2048)
  54305. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  54306. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  54307. #else
  54308. RSA_free(rsa);
  54309. rsa = NULL;
  54310. #endif
  54311. #else
  54312. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  54313. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  54314. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  54315. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  54316. #endif
  54317. if (rsa) {
  54318. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  54319. if (EXPECT_FAIL()) {
  54320. RSA_free(rsa);
  54321. }
  54322. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  54323. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  54324. expectedDerSz = 743;
  54325. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  54326. expectedDerSz), TEST_SUCCESS);
  54327. #endif
  54328. }
  54329. EVP_PKEY_free(pkey);
  54330. pkey = NULL;
  54331. BN_free(exponent);
  54332. #endif /* !NO_RSA */
  54333. #ifdef HAVE_ECC
  54334. ExpectNotNull(pkey = EVP_PKEY_new());
  54335. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54336. #ifndef NO_WOLFSSL_STUB
  54337. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  54338. #endif
  54339. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  54340. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  54341. if (EXPECT_FAIL()) {
  54342. EC_KEY_free(ec_key);
  54343. }
  54344. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  54345. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  54346. expectedDerSz = 344;
  54347. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  54348. TEST_SUCCESS);
  54349. #endif
  54350. EVP_PKEY_free(pkey);
  54351. #endif /* HAVE_ECC */
  54352. (void)pkey;
  54353. (void)expectedDerSz;
  54354. #endif /* OPENSSL_EXTRA */
  54355. return EXPECT_RESULT();
  54356. }
  54357. static int test_stubs_are_stubs(void)
  54358. {
  54359. EXPECT_DECLS;
  54360. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  54361. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  54362. WOLFSSL_CTX* ctx = NULL;
  54363. WOLFSSL_CTX* ctxN = NULL;
  54364. #ifndef NO_WOLFSSL_CLIENT
  54365. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54366. #elif !defined(NO_WOLFSSL_SERVER)
  54367. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54368. #endif
  54369. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  54370. ExpectIntEQ((int) x(z), 0)
  54371. /* test logic, all stubs return same result regardless of ctx being NULL
  54372. * as there are no sanity checks, it's just a stub! If at some
  54373. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  54374. * if invalid inputs are supplied. Test calling both
  54375. * with and without valid inputs, if a stub functionality remains unchanged.
  54376. */
  54377. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  54378. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  54379. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  54380. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  54381. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  54382. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  54383. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  54384. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  54385. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  54386. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  54387. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  54388. wolfSSL_CTX_free(ctx);
  54389. ctx = NULL;
  54390. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  54391. * !NO_WOLFSSL_SERVER) */
  54392. return EXPECT_RESULT();
  54393. }
  54394. static int test_CONF_modules_xxx(void)
  54395. {
  54396. int res = TEST_SKIPPED;
  54397. #if defined(OPENSSL_EXTRA)
  54398. CONF_modules_free();
  54399. CONF_modules_unload(0);
  54400. CONF_modules_unload(1);
  54401. CONF_modules_unload(-1);
  54402. res = TEST_SUCCESS;
  54403. #endif /* OPENSSL_EXTRA */
  54404. return res;
  54405. }
  54406. static int test_CRYPTO_set_dynlock_xxx(void)
  54407. {
  54408. int res = TEST_SKIPPED;
  54409. #if defined(OPENSSL_EXTRA)
  54410. CRYPTO_set_dynlock_create_callback(
  54411. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  54412. CRYPTO_set_dynlock_create_callback(
  54413. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  54414. CRYPTO_set_dynlock_destroy_callback(
  54415. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  54416. CRYPTO_set_dynlock_destroy_callback(
  54417. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  54418. CRYPTO_set_dynlock_lock_callback(
  54419. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  54420. CRYPTO_set_dynlock_lock_callback(
  54421. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  54422. res = TEST_SUCCESS;
  54423. #endif /* OPENSSL_EXTRA */
  54424. return res;
  54425. }
  54426. static int test_CRYPTO_THREADID_xxx(void)
  54427. {
  54428. EXPECT_DECLS;
  54429. #if defined(OPENSSL_EXTRA)
  54430. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  54431. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  54432. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  54433. #endif /* OPENSSL_EXTRA */
  54434. return EXPECT_RESULT();
  54435. }
  54436. static int test_ENGINE_cleanup(void)
  54437. {
  54438. int res = TEST_SKIPPED;
  54439. #if defined(OPENSSL_EXTRA)
  54440. ENGINE_cleanup();
  54441. res = TEST_SUCCESS;
  54442. #endif /* OPENSSL_EXTRA */
  54443. return res;
  54444. }
  54445. static int test_wolfSSL_CTX_LoadCRL(void)
  54446. {
  54447. EXPECT_DECLS;
  54448. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  54449. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  54450. WOLFSSL_CTX* ctx = NULL;
  54451. WOLFSSL* ssl = NULL;
  54452. const char* badPath = "dummypath";
  54453. const char* validPath = "./certs/crl";
  54454. const char* validFilePath = "./certs/crl/cliCrl.pem";
  54455. const char* issuerCert = "./certs/client-cert.pem";
  54456. int derType = WOLFSSL_FILETYPE_ASN1;
  54457. int pemType = WOLFSSL_FILETYPE_PEM;
  54458. #ifdef HAVE_CRL_MONITOR
  54459. int monitor = WOLFSSL_CRL_MONITOR;
  54460. #else
  54461. int monitor = 0;
  54462. #endif
  54463. WOLFSSL_CERT_MANAGER* cm = NULL;
  54464. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  54465. BAD_FUNC_ARG)
  54466. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  54467. NOT_COMPILED_IN)
  54468. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  54469. WOLFSSL_SUCCESS)
  54470. #ifndef NO_WOLFSSL_CLIENT
  54471. #define NEW_CTX(ctx) ExpectNotNull( \
  54472. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  54473. #elif !defined(NO_WOLFSSL_SERVER)
  54474. #define NEW_CTX(ctx) ExpectNotNull( \
  54475. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  54476. #else
  54477. #define NEW_CTX(ctx) return
  54478. #endif
  54479. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  54480. NEW_CTX(ctx);
  54481. #ifndef HAVE_CRL_MONITOR
  54482. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  54483. wolfSSL_CTX_free(ctx);
  54484. NEW_CTX(ctx);
  54485. #endif
  54486. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  54487. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  54488. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  54489. wolfSSL_CTX_free(ctx);
  54490. ctx = NULL;
  54491. NEW_CTX(ctx);
  54492. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  54493. WOLFSSL_SUCCESS);
  54494. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  54495. wolfSSL_CTX_free(ctx);
  54496. ctx = NULL;
  54497. NEW_CTX(ctx);
  54498. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  54499. WOLFSSL_SUCCESS);
  54500. ExpectNotNull(ssl = wolfSSL_new(ctx));
  54501. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  54502. wolfSSL_free(ssl);
  54503. ssl = NULL;
  54504. wolfSSL_CTX_free(ctx);
  54505. ctx = NULL;
  54506. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  54507. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  54508. WOLFSSL_SUCCESS);
  54509. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  54510. WOLFSSL_SUCCESS);
  54511. wolfSSL_CertManagerFree(cm);
  54512. #endif
  54513. return EXPECT_RESULT();
  54514. }
  54515. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  54516. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  54517. {
  54518. EXPECT_DECLS;
  54519. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  54520. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  54521. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  54522. return EXPECT_RESULT();
  54523. }
  54524. #endif
  54525. static int test_multiple_crls_same_issuer(void)
  54526. {
  54527. EXPECT_DECLS;
  54528. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  54529. test_ssl_cbf client_cbs, server_cbs;
  54530. struct {
  54531. const char* server_cert;
  54532. const char* server_key;
  54533. } test_params[] = {
  54534. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  54535. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  54536. };
  54537. size_t i;
  54538. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  54539. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54540. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54541. server_cbs.certPemFile = test_params[i].server_cert;
  54542. server_cbs.keyPemFile = test_params[i].server_key;
  54543. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  54544. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  54545. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54546. &server_cbs, NULL), TEST_FAIL);
  54547. }
  54548. #endif
  54549. return EXPECT_RESULT();
  54550. }
  54551. static int test_SetTmpEC_DHE_Sz(void)
  54552. {
  54553. EXPECT_DECLS;
  54554. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  54555. WOLFSSL_CTX *ctx = NULL;
  54556. WOLFSSL *ssl = NULL;
  54557. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54558. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  54559. ExpectNotNull(ssl = wolfSSL_new(ctx));
  54560. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  54561. wolfSSL_free(ssl);
  54562. wolfSSL_CTX_free(ctx);
  54563. #endif
  54564. return EXPECT_RESULT();
  54565. }
  54566. static int test_wolfSSL_CTX_get0_privatekey(void)
  54567. {
  54568. EXPECT_DECLS;
  54569. #ifdef OPENSSL_ALL
  54570. WOLFSSL_CTX* ctx = NULL;
  54571. (void)ctx;
  54572. #ifndef NO_RSA
  54573. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54574. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  54575. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  54576. WOLFSSL_FILETYPE_PEM));
  54577. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  54578. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  54579. WOLFSSL_FILETYPE_PEM));
  54580. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  54581. wolfSSL_CTX_free(ctx);
  54582. ctx = NULL;
  54583. #endif
  54584. #ifdef HAVE_ECC
  54585. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  54586. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  54587. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  54588. WOLFSSL_FILETYPE_PEM));
  54589. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  54590. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  54591. WOLFSSL_FILETYPE_PEM));
  54592. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  54593. wolfSSL_CTX_free(ctx);
  54594. #endif
  54595. #endif
  54596. return EXPECT_RESULT();
  54597. }
  54598. static int test_wolfSSL_dtls_set_mtu(void)
  54599. {
  54600. EXPECT_DECLS;
  54601. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  54602. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  54603. !defined(WOLFSSL_NO_TLS12)
  54604. WOLFSSL_CTX* ctx = NULL;
  54605. WOLFSSL* ssl = NULL;
  54606. const char* testCertFile;
  54607. const char* testKeyFile;
  54608. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  54609. #ifndef NO_RSA
  54610. testCertFile = svrCertFile;
  54611. testKeyFile = svrKeyFile;
  54612. #elif defined(HAVE_ECC)
  54613. testCertFile = eccCertFile;
  54614. testKeyFile = eccKeyFile;
  54615. #endif
  54616. if (testCertFile != NULL && testKeyFile != NULL) {
  54617. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  54618. WOLFSSL_FILETYPE_PEM));
  54619. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54620. WOLFSSL_FILETYPE_PEM));
  54621. }
  54622. ExpectNotNull(ssl = wolfSSL_new(ctx));
  54623. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  54624. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  54625. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  54626. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  54627. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  54628. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  54629. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  54630. wolfSSL_free(ssl);
  54631. wolfSSL_CTX_free(ctx);
  54632. #endif
  54633. return EXPECT_RESULT();
  54634. }
  54635. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  54636. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  54637. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  54638. enum HandShakeType hsType, word16 length)
  54639. {
  54640. size_t idx = 0;
  54641. byte* l;
  54642. /* record layer */
  54643. /* handshake type */
  54644. out[idx++] = handshake;
  54645. /* protocol version */
  54646. out[idx++] = 0xfe;
  54647. out[idx++] = 0xfd; /* DTLS 1.2 */
  54648. /* epoch 0 */
  54649. XMEMSET(out + idx, 0, 2);
  54650. idx += 2;
  54651. /* sequence number */
  54652. XMEMSET(out + idx, 0, 6);
  54653. c32toa(seq, out + idx + 2);
  54654. idx += 6;
  54655. /* length in BE */
  54656. if (length)
  54657. c16toa(length, out + idx);
  54658. else
  54659. c16toa(outSz - idx - 2, out + idx);
  54660. idx += 2;
  54661. /* handshake layer */
  54662. /* handshake type */
  54663. out[idx++] = (byte)hsType;
  54664. /* length */
  54665. l = out + idx;
  54666. idx += 3;
  54667. /* message seq */
  54668. c16toa(0, out + idx);
  54669. idx += 2;
  54670. /* frag offset */
  54671. c32to24(0, out + idx);
  54672. idx += 3;
  54673. /* frag length */
  54674. c32to24((word32)outSz - (word32)idx - 3, l);
  54675. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  54676. idx += 3;
  54677. XMEMSET(out + idx, 0, outSz - idx);
  54678. }
  54679. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  54680. {
  54681. byte msg[] = "This is a msg for the client";
  54682. byte reply[40];
  54683. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  54684. reply[sizeof(reply) - 1] = '\0';
  54685. fprintf(stderr, "Client message: %s\n", reply);
  54686. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  54687. }
  54688. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  54689. {
  54690. byte ch[50];
  54691. int fd = wolfSSL_get_fd(ssl);
  54692. byte msg[] = "This is a msg for the server";
  54693. byte reply[40];
  54694. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  54695. /* Server should ignore this datagram */
  54696. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  54697. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  54698. /* Server should ignore this datagram */
  54699. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  54700. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  54701. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  54702. reply[sizeof(reply) - 1] = '\0';
  54703. fprintf(stderr, "Server response: %s\n", reply);
  54704. }
  54705. static int test_wolfSSL_dtls_plaintext(void)
  54706. {
  54707. callback_functions func_cb_client;
  54708. callback_functions func_cb_server;
  54709. size_t i;
  54710. struct test_params {
  54711. method_provider client_meth;
  54712. method_provider server_meth;
  54713. ssl_callback on_result_server;
  54714. ssl_callback on_result_client;
  54715. } params[] = {
  54716. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54717. test_wolfSSL_dtls_plaintext_server,
  54718. test_wolfSSL_dtls_plaintext_client},
  54719. };
  54720. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54721. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54722. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54723. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54724. func_cb_server.method = params[i].server_meth;
  54725. func_cb_client.method = params[i].client_meth;
  54726. func_cb_client.on_result = params[i].on_result_client;
  54727. func_cb_server.on_result = params[i].on_result_server;
  54728. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54729. if (!func_cb_client.return_code)
  54730. return TEST_FAIL;
  54731. if (!func_cb_server.return_code)
  54732. return TEST_FAIL;
  54733. }
  54734. return TEST_RES_CHECK(1);
  54735. }
  54736. #else
  54737. static int test_wolfSSL_dtls_plaintext(void) {
  54738. return TEST_SKIPPED;
  54739. }
  54740. #endif
  54741. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  54742. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  54743. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  54744. {
  54745. byte b[1100]; /* buffer for the messages to send */
  54746. size_t idx = 0;
  54747. size_t seq_offset = 0;
  54748. size_t msg_offset = 0;
  54749. int i;
  54750. int fd = wolfSSL_get_fd(ssl);
  54751. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  54752. word32 seq_number = 100; /* start high so server definitely reads this */
  54753. word16 msg_number = 50; /* start high so server has to buffer this */
  54754. AssertIntEQ(ret, 1);
  54755. /* Now let's start spamming the peer with fragments it needs to store */
  54756. XMEMSET(b, -1, sizeof(b));
  54757. /* record layer */
  54758. /* handshake type */
  54759. b[idx++] = 22;
  54760. /* protocol version */
  54761. b[idx++] = 0xfe;
  54762. b[idx++] = 0xfd; /* DTLS 1.2 */
  54763. /* epoch 0 */
  54764. XMEMSET(b + idx, 0, 2);
  54765. idx += 2;
  54766. /* sequence number */
  54767. XMEMSET(b + idx, 0, 6);
  54768. seq_offset = idx + 2; /* increment only the low 32 bits */
  54769. idx += 6;
  54770. /* static length in BE */
  54771. c16toa(42, b + idx);
  54772. idx += 2;
  54773. /* handshake layer */
  54774. /* cert type */
  54775. b[idx++] = 11;
  54776. /* length */
  54777. c32to24(1000, b + idx);
  54778. idx += 3;
  54779. /* message seq */
  54780. c16toa(0, b + idx);
  54781. msg_offset = idx;
  54782. idx += 2;
  54783. /* frag offset */
  54784. c32to24(500, b + idx);
  54785. idx += 3;
  54786. /* frag length */
  54787. c32to24(30, b + idx);
  54788. idx += 3;
  54789. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  54790. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  54791. seq_number++, msg_number++, i++) {
  54792. struct timespec delay;
  54793. XMEMSET(&delay, 0, sizeof(delay));
  54794. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  54795. c32toa(seq_number, b + seq_offset);
  54796. c16toa(msg_number, b + msg_offset);
  54797. ret = (int)send(fd, b, 55, 0);
  54798. nanosleep(&delay, NULL);
  54799. }
  54800. }
  54801. #ifdef WOLFSSL_DTLS13
  54802. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  54803. {
  54804. const word16 sendCountMax = 100;
  54805. byte b[150]; /* buffer for the messages to send */
  54806. size_t idx = 0;
  54807. size_t msg_offset = 0;
  54808. int fd = wolfSSL_get_fd(ssl);
  54809. word16 msg_number = 10; /* start high so server has to buffer this */
  54810. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  54811. AssertIntEQ(ret, 1);
  54812. /* Now let's start spamming the peer with fragments it needs to store */
  54813. XMEMSET(b, -1, sizeof(b));
  54814. /* handshake type */
  54815. b[idx++] = 11;
  54816. /* length */
  54817. c32to24(10000, b + idx);
  54818. idx += 3;
  54819. /* message_seq */
  54820. msg_offset = idx;
  54821. idx += 2;
  54822. /* fragment_offset */
  54823. c32to24(5000, b + idx);
  54824. idx += 3;
  54825. /* fragment_length */
  54826. c32to24(100, b + idx);
  54827. idx += 3;
  54828. /* fragment contents */
  54829. idx += 100;
  54830. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  54831. byte sendBuf[150];
  54832. int sendSz = sizeof(sendBuf);
  54833. struct timespec delay;
  54834. XMEMSET(&delay, 0, sizeof(delay));
  54835. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  54836. c16toa(msg_number, b + msg_offset);
  54837. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  54838. (int)idx, handshake, 0, 0, 0);
  54839. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  54840. nanosleep(&delay, NULL);
  54841. }
  54842. }
  54843. #endif
  54844. static int test_wolfSSL_dtls_fragments(void)
  54845. {
  54846. EXPECT_DECLS;
  54847. callback_functions func_cb_client;
  54848. callback_functions func_cb_server;
  54849. size_t i;
  54850. struct test_params {
  54851. method_provider client_meth;
  54852. method_provider server_meth;
  54853. ssl_callback spammer;
  54854. } params[] = {
  54855. #if !defined(WOLFSSL_NO_TLS12)
  54856. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  54857. test_wolfSSL_dtls12_fragments_spammer},
  54858. #endif
  54859. #ifdef WOLFSSL_DTLS13
  54860. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  54861. test_wolfSSL_dtls13_fragments_spammer},
  54862. #endif
  54863. };
  54864. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54865. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  54866. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  54867. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  54868. func_cb_server.method = params[i].server_meth;
  54869. func_cb_client.method = params[i].client_meth;
  54870. func_cb_client.ssl_ready = params[i].spammer;
  54871. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  54872. ExpectFalse(func_cb_client.return_code);
  54873. ExpectFalse(func_cb_server.return_code);
  54874. /* The socket should be closed by the server resulting in a
  54875. * socket error, fatal error or reading a close notify alert */
  54876. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  54877. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  54878. func_cb_client.last_err != FATAL_ERROR) {
  54879. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  54880. }
  54881. /* Check the server returned an error indicating the msg buffer
  54882. * was full */
  54883. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  54884. if (EXPECT_FAIL())
  54885. break;
  54886. }
  54887. return EXPECT_RESULT();
  54888. }
  54889. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  54890. {
  54891. int fd, ret;
  54892. byte alert_msg[] = {
  54893. 0x15, /* alert type */
  54894. 0xfe, 0xfd, /* version */
  54895. 0x00, 0x00, /* epoch */
  54896. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  54897. 0x00, 0x02, /* length */
  54898. 0x02, /* level: fatal */
  54899. 0x46 /* protocol version */
  54900. };
  54901. fd = wolfSSL_get_fd(ssl);
  54902. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  54903. AssertIntGT(ret, 0);
  54904. }
  54905. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  54906. {
  54907. callback_functions client_cbs, server_cbs;
  54908. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54909. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54910. client_cbs.doUdp = server_cbs.doUdp = 1;
  54911. if (version12) {
  54912. #if !defined(WOLFSSL_NO_TLS12)
  54913. client_cbs.method = wolfDTLSv1_2_client_method;
  54914. server_cbs.method = wolfDTLSv1_2_server_method;
  54915. #else
  54916. return TEST_SKIPPED;
  54917. #endif
  54918. }
  54919. else
  54920. {
  54921. #ifdef WOLFSSL_DTLS13
  54922. client_cbs.method = wolfDTLSv1_3_client_method;
  54923. server_cbs.method = wolfDTLSv1_3_server_method;
  54924. #else
  54925. return TEST_SKIPPED;
  54926. #endif /* WOLFSSL_DTLS13 */
  54927. }
  54928. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  54929. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54930. if (!client_cbs.return_code)
  54931. return TEST_FAIL;
  54932. if (!server_cbs.return_code)
  54933. return TEST_FAIL;
  54934. return TEST_SUCCESS;
  54935. }
  54936. static int test_wolfSSL_ignore_alert_before_cookie(void)
  54937. {
  54938. int ret;
  54939. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  54940. if (ret != 0)
  54941. return ret;
  54942. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  54943. if (ret != 0)
  54944. return ret;
  54945. return 0;
  54946. }
  54947. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  54948. {
  54949. int ret;
  54950. int fd;
  54951. byte bad_msg[] = {
  54952. 0x17, /* app data */
  54953. 0xaa, 0xfd, /* bad version */
  54954. 0x00, 0x01, /* epoch 1 */
  54955. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  54956. 0x00, 0x26, /* length: 38 bytes */
  54957. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  54958. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  54959. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  54960. 0x03, 0x18, 0x72
  54961. };
  54962. fd = wolfSSL_get_fd(ssl);
  54963. AssertIntGE(fd, 0);
  54964. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  54965. AssertIntEQ(ret, sizeof(bad_msg));
  54966. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  54967. AssertIntEQ(ret, sizeof("badrecordtest"));
  54968. }
  54969. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  54970. {
  54971. byte buf[100];
  54972. int ret;
  54973. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  54974. AssertIntGT(ret, 0);
  54975. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  54976. }
  54977. static int _test_wolfSSL_dtls_bad_record(
  54978. method_provider client_method, method_provider server_method)
  54979. {
  54980. callback_functions client_cbs, server_cbs;
  54981. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54982. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54983. client_cbs.doUdp = server_cbs.doUdp = 1;
  54984. client_cbs.method = client_method;
  54985. server_cbs.method = server_method;
  54986. client_cbs.on_result = test_wolfSSL_send_bad_record;
  54987. server_cbs.on_result = test_wolfSSL_read_string;
  54988. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54989. if (!client_cbs.return_code)
  54990. return TEST_FAIL;
  54991. if (!server_cbs.return_code)
  54992. return TEST_FAIL;
  54993. return TEST_SUCCESS;
  54994. }
  54995. static int test_wolfSSL_dtls_bad_record(void)
  54996. {
  54997. int ret = TEST_SUCCESS;
  54998. #if !defined(WOLFSSL_NO_TLS12)
  54999. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  55000. wolfDTLSv1_2_server_method);
  55001. #endif
  55002. #ifdef WOLFSSL_DTLS13
  55003. if (ret == TEST_SUCCESS) {
  55004. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  55005. wolfDTLSv1_3_server_method);
  55006. }
  55007. #endif /* WOLFSSL_DTLS13 */
  55008. return ret;
  55009. }
  55010. #else
  55011. static int test_wolfSSL_dtls_fragments(void) {
  55012. return TEST_SKIPPED;
  55013. }
  55014. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  55015. return TEST_SKIPPED;
  55016. }
  55017. static int test_wolfSSL_dtls_bad_record(void) {
  55018. return TEST_SKIPPED;
  55019. }
  55020. #endif
  55021. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  55022. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55023. defined(HAVE_IO_TESTS_DEPENDENCIES)
  55024. static byte test_AEAD_fail_decryption = 0;
  55025. static byte test_AEAD_seq_num = 0;
  55026. static byte test_AEAD_done = 0;
  55027. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  55028. {
  55029. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  55030. if (ret > 0) {
  55031. if (test_AEAD_fail_decryption) {
  55032. /* Modify the packet to trigger a decryption failure */
  55033. buf[ret/2] ^= 0xFF;
  55034. if (test_AEAD_fail_decryption == 1)
  55035. test_AEAD_fail_decryption = 0;
  55036. }
  55037. }
  55038. (void)ctx;
  55039. return ret;
  55040. }
  55041. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  55042. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  55043. {
  55044. if (sendLimit)
  55045. w64Zero(sendLimit);
  55046. switch (ssl->specs.bulk_cipher_algorithm) {
  55047. case wolfssl_aes_gcm:
  55048. if (sendLimit)
  55049. *sendLimit = AEAD_AES_LIMIT;
  55050. FALL_THROUGH;
  55051. case wolfssl_chacha:
  55052. if (hardLimit)
  55053. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  55054. if (keyUpdateLimit)
  55055. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  55056. break;
  55057. case wolfssl_aes_ccm:
  55058. if (sendLimit)
  55059. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  55060. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  55061. if (hardLimit)
  55062. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  55063. if (keyUpdateLimit)
  55064. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  55065. }
  55066. else {
  55067. if (hardLimit)
  55068. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  55069. if (keyUpdateLimit)
  55070. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  55071. }
  55072. break;
  55073. default:
  55074. fprintf(stderr, "Unrecognized bulk cipher");
  55075. AssertFalse(1);
  55076. break;
  55077. }
  55078. }
  55079. static void test_AEAD_limit_client(WOLFSSL* ssl)
  55080. {
  55081. int ret;
  55082. int i;
  55083. int didReKey = 0;
  55084. char msgBuf[20];
  55085. w64wrapper hardLimit;
  55086. w64wrapper keyUpdateLimit;
  55087. w64wrapper counter;
  55088. w64wrapper sendLimit;
  55089. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  55090. w64Zero(&counter);
  55091. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  55092. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  55093. for (i = 0; i < 10; i++) {
  55094. /* Test some failed decryptions */
  55095. test_AEAD_fail_decryption = 1;
  55096. w64Increment(&counter);
  55097. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55098. /* Should succeed since decryption failures are dropped */
  55099. AssertIntGT(ret, 0);
  55100. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  55101. }
  55102. test_AEAD_fail_decryption = 1;
  55103. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  55104. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55105. /* 100 read calls should be enough to complete the key update */
  55106. w64Zero(&counter);
  55107. for (i = 0; i < 100; i++) {
  55108. /* Key update should be sent and negotiated */
  55109. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55110. AssertIntGT(ret, 0);
  55111. /* Epoch after one key update is 4 */
  55112. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  55113. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  55114. didReKey = 1;
  55115. break;
  55116. }
  55117. }
  55118. AssertTrue(didReKey);
  55119. if (!w64IsZero(sendLimit)) {
  55120. /* Test the sending limit for AEAD ciphers */
  55121. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  55122. test_AEAD_seq_num = 1;
  55123. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55124. AssertIntGT(ret, 0);
  55125. didReKey = 0;
  55126. w64Zero(&counter);
  55127. /* 100 read calls should be enough to complete the key update */
  55128. for (i = 0; i < 100; i++) {
  55129. /* Key update should be sent and negotiated */
  55130. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55131. AssertIntGT(ret, 0);
  55132. /* Epoch after another key update is 5 */
  55133. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  55134. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  55135. didReKey = 1;
  55136. break;
  55137. }
  55138. }
  55139. AssertTrue(didReKey);
  55140. }
  55141. test_AEAD_fail_decryption = 2;
  55142. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  55143. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55144. /* Connection should fail with a DECRYPT_ERROR */
  55145. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55146. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  55147. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  55148. test_AEAD_done = 1;
  55149. }
  55150. int counter = 0;
  55151. static void test_AEAD_limit_server(WOLFSSL* ssl)
  55152. {
  55153. char msgBuf[] = "Sending data";
  55154. int ret = WOLFSSL_SUCCESS;
  55155. w64wrapper sendLimit;
  55156. SOCKET_T fd = wolfSSL_get_fd(ssl);
  55157. struct timespec delay;
  55158. XMEMSET(&delay, 0, sizeof(delay));
  55159. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  55160. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  55161. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  55162. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  55163. while (!test_AEAD_done && ret > 0) {
  55164. counter++;
  55165. if (test_AEAD_seq_num) {
  55166. /* We need to update the seq number so that we can understand the
  55167. * peer. Otherwise we will incorrectly interpret the seq number. */
  55168. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  55169. AssertNotNull(e);
  55170. e->nextPeerSeqNumber = sendLimit;
  55171. test_AEAD_seq_num = 0;
  55172. }
  55173. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55174. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55175. nanosleep(&delay, NULL);
  55176. }
  55177. }
  55178. static int test_wolfSSL_dtls_AEAD_limit(void)
  55179. {
  55180. callback_functions func_cb_client;
  55181. callback_functions func_cb_server;
  55182. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55183. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55184. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55185. func_cb_server.method = wolfDTLSv1_3_server_method;
  55186. func_cb_client.method = wolfDTLSv1_3_client_method;
  55187. func_cb_server.on_result = test_AEAD_limit_server;
  55188. func_cb_client.on_result = test_AEAD_limit_client;
  55189. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55190. if (!func_cb_client.return_code)
  55191. return TEST_FAIL;
  55192. if (!func_cb_server.return_code)
  55193. return TEST_FAIL;
  55194. return TEST_SUCCESS;
  55195. }
  55196. #else
  55197. static int test_wolfSSL_dtls_AEAD_limit(void)
  55198. {
  55199. return TEST_SKIPPED;
  55200. }
  55201. #endif
  55202. #if defined(WOLFSSL_DTLS) && \
  55203. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  55204. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  55205. {
  55206. int fd, ret;
  55207. byte ch_msg[] = {
  55208. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55209. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55210. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  55211. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  55212. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  55213. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55214. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55215. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55216. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55217. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55218. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55219. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55220. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55221. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  55222. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  55223. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  55224. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  55225. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  55226. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  55227. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  55228. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  55229. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  55230. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  55231. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  55232. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  55233. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  55234. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  55235. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  55236. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  55237. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  55238. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  55239. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  55240. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  55241. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  55242. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  55243. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  55244. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  55245. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  55246. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  55247. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  55248. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  55249. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  55250. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  55251. 0x31, 0x68, 0x4c
  55252. };
  55253. fd = wolfSSL_get_fd(ssl);
  55254. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  55255. AssertIntGT(ret, 0);
  55256. /* consume the HRR otherwise handshake will fail */
  55257. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  55258. AssertIntGT(ret, 0);
  55259. }
  55260. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  55261. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  55262. {
  55263. int fd, ret;
  55264. byte ch_msh_invalid_cookie[] = {
  55265. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  55266. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  55267. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  55268. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  55269. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  55270. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55271. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55272. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55273. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55274. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55275. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55276. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55277. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55278. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  55279. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55280. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55281. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55282. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55283. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55284. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  55285. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  55286. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  55287. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  55288. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  55289. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  55290. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  55291. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  55292. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  55293. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  55294. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  55295. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  55296. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  55297. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  55298. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  55299. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  55300. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  55301. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  55302. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  55303. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  55304. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  55305. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  55306. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  55307. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  55308. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  55309. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  55310. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  55311. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  55312. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  55313. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  55314. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  55315. 0x23, 0x00, 0x00
  55316. };
  55317. byte alert_reply[50];
  55318. byte expected_alert_reply[] = {
  55319. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  55320. 0x02, 0x02, 0x2f
  55321. };
  55322. fd = wolfSSL_get_fd(ssl);
  55323. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  55324. AssertIntGT(ret, 0);
  55325. /* should reply with an illegal_parameter reply */
  55326. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  55327. AssertIntEQ(ret, sizeof(expected_alert_reply));
  55328. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  55329. }
  55330. #endif
  55331. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  55332. {
  55333. #ifndef NO_MD5
  55334. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  55335. #elif !defined(NO_SHA)
  55336. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  55337. #elif !defined(NO_SHA256)
  55338. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  55339. #else
  55340. #error "We need a digest to hash the WOLFSSL object"
  55341. #endif
  55342. byte hashBuf[WC_MAX_DIGEST_SIZE];
  55343. wc_HashAlg hash;
  55344. const TLSX* exts = ssl->extensions;
  55345. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  55346. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  55347. * InitHandshakeHashes */
  55348. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  55349. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  55350. /* Following fields are not important to compare */
  55351. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  55352. sslCopy.buffers.inputBuffer.buffer = NULL;
  55353. sslCopy.buffers.inputBuffer.bufferSize = 0;
  55354. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  55355. sslCopy.buffers.inputBuffer.offset = 0;
  55356. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  55357. sslCopy.buffers.outputBuffer.buffer = NULL;
  55358. sslCopy.buffers.outputBuffer.bufferSize = 0;
  55359. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  55360. sslCopy.buffers.outputBuffer.offset = 0;
  55361. sslCopy.error = 0;
  55362. sslCopy.curSize = 0;
  55363. sslCopy.curStartIdx = 0;
  55364. sslCopy.keys.curSeq_lo = 0;
  55365. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  55366. #ifdef WOLFSSL_DTLS13
  55367. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  55368. sslCopy.dtls13FastTimeout = 0;
  55369. #endif
  55370. sslCopy.keys.dtls_peer_handshake_number = 0;
  55371. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  55372. sslCopy.hsHashes = NULL;
  55373. #ifdef WOLFSSL_ASYNC_IO
  55374. #ifdef WOLFSSL_ASYNC_CRYPT
  55375. sslCopy.asyncDev = NULL;
  55376. #endif
  55377. sslCopy.async = NULL;
  55378. #endif
  55379. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  55380. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  55381. /* hash extension list */
  55382. while (exts != NULL) {
  55383. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  55384. exts = exts->next;
  55385. }
  55386. /* Hash suites */
  55387. if (sslCopy.suites != NULL) {
  55388. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  55389. sizeof(struct Suites)), 0);
  55390. }
  55391. /* Hash hsHashes */
  55392. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  55393. sizeof(*hsHashes)), 0);
  55394. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  55395. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  55396. return MakeWordFromHash(hashBuf);
  55397. }
  55398. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  55399. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  55400. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  55401. char *buf, int sz, void *ctx)
  55402. {
  55403. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  55404. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  55405. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  55406. }
  55407. else {
  55408. return WOLFSSL_CBIO_ERR_WANT_READ;
  55409. }
  55410. }
  55411. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  55412. {
  55413. /* Compare the ssl object before and after one ClientHello msg */
  55414. SOCKET_T fd = wolfSSL_get_fd(ssl);
  55415. int res;
  55416. int err;
  55417. word32 initHash;
  55418. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  55419. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  55420. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  55421. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  55422. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  55423. (void)initHash;
  55424. res = tcp_select(fd, 5);
  55425. /* We are expecting a msg. A timeout indicates failure. */
  55426. AssertIntEQ(res, TEST_RECV_READY);
  55427. res = wolfSSL_accept(ssl);
  55428. err = wolfSSL_get_error(ssl, res);
  55429. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  55430. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  55431. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  55432. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  55433. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  55434. }
  55435. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  55436. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  55437. {
  55438. int ret;
  55439. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  55440. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  55441. test_wolfSSL_dtls_compare_stateless(ssl);
  55442. }
  55443. #endif
  55444. static int test_wolfSSL_dtls_stateless(void)
  55445. {
  55446. callback_functions client_cbs, server_cbs;
  55447. size_t i;
  55448. struct {
  55449. method_provider client_meth;
  55450. method_provider server_meth;
  55451. ssl_callback client_ssl_ready;
  55452. ssl_callback server_ssl_ready;
  55453. } test_params[] = {
  55454. #if !defined(WOLFSSL_NO_TLS12)
  55455. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55456. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  55457. #endif
  55458. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  55459. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55460. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  55461. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55462. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  55463. #endif
  55464. };
  55465. if (0 == sizeof(test_params)){
  55466. return TEST_SKIPPED;
  55467. }
  55468. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  55469. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55470. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55471. client_cbs.doUdp = server_cbs.doUdp = 1;
  55472. client_cbs.method = test_params[i].client_meth;
  55473. server_cbs.method = test_params[i].server_meth;
  55474. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  55475. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  55476. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55477. if (!client_cbs.return_code)
  55478. return TEST_FAIL;
  55479. if (!server_cbs.return_code)
  55480. return TEST_FAIL;
  55481. }
  55482. return TEST_SUCCESS;
  55483. }
  55484. #else
  55485. static int test_wolfSSL_dtls_stateless(void)
  55486. {
  55487. return TEST_SKIPPED;
  55488. }
  55489. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  55490. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  55491. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  55492. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  55493. !defined(WOLFSSL_NO_CLIENT_AUTH))
  55494. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  55495. {
  55496. int ret;
  55497. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  55498. fprintf(stderr, "loading cert %s failed\n", certA);
  55499. fprintf(stderr, "Error: (%d): %s\n", ret,
  55500. wolfSSL_ERR_reason_error_string(ret));
  55501. return -1;
  55502. }
  55503. return 0;
  55504. }
  55505. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  55506. {
  55507. int ret;
  55508. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  55509. != WOLFSSL_SUCCESS) {
  55510. fprintf(stderr, "could not verify the cert: %s\n", certA);
  55511. fprintf(stderr, "Error: (%d): %s\n", ret,
  55512. wolfSSL_ERR_reason_error_string(ret));
  55513. return -1;
  55514. }
  55515. else {
  55516. fprintf(stderr, "successfully verified: %s\n", certA);
  55517. }
  55518. return 0;
  55519. }
  55520. #define LOAD_ONE_CA(a, b, c, d) \
  55521. do { \
  55522. (a) = load_ca_into_cm(c, d); \
  55523. if ((a) != 0) \
  55524. return (b); \
  55525. else \
  55526. (b)--; \
  55527. } while(0)
  55528. #define VERIFY_ONE_CERT(a, b, c, d) \
  55529. do { \
  55530. (a) = verify_cert_with_cm(c, d);\
  55531. if ((a) != 0) \
  55532. return (b); \
  55533. else \
  55534. (b)--; \
  55535. } while(0)
  55536. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  55537. {
  55538. int ret;
  55539. int i = -1;
  55540. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  55541. char chainGArr[9][50] = {"certs/ca-cert.pem",
  55542. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  55543. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  55544. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  55545. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  55546. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  55547. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  55548. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  55549. "certs/test-pathlen/chainG-entity.pem"};
  55550. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  55551. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  55552. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  55553. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  55554. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  55555. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  55556. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  55557. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  55558. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  55559. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  55560. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  55561. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  55562. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  55563. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  55564. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  55565. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  55566. /* test validating the entity twice, should have no effect on pathLen since
  55567. * entity/leaf cert */
  55568. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  55569. return ret;
  55570. }
  55571. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  55572. {
  55573. int ret;
  55574. int i = -1;
  55575. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  55576. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  55577. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  55578. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  55579. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  55580. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  55581. */
  55582. char chainHArr[6][50] = {"certs/ca-cert.pem",
  55583. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  55584. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  55585. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  55586. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  55587. "certs/test-pathlen/chainH-entity.pem"};
  55588. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  55589. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  55590. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  55591. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  55592. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  55593. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  55594. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  55595. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  55596. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  55597. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  55598. return ret;
  55599. }
  55600. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  55601. {
  55602. int ret;
  55603. int i = -1;
  55604. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  55605. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  55606. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  55607. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  55608. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  55609. */
  55610. char chainIArr[5][50] = {"certs/ca-cert.pem",
  55611. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  55612. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  55613. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  55614. "certs/test-pathlen/chainI-entity.pem"};
  55615. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  55616. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  55617. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  55618. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  55619. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  55620. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  55621. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  55622. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  55623. return ret;
  55624. }
  55625. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  55626. {
  55627. int ret;
  55628. int i = -1;
  55629. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  55630. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  55631. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  55632. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  55633. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  55634. */
  55635. char chainJArr[6][50] = {"certs/ca-cert.pem",
  55636. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  55637. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  55638. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  55639. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  55640. "certs/test-pathlen/chainJ-entity.pem"};
  55641. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  55642. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  55643. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  55644. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  55645. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  55646. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  55647. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  55648. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  55649. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  55650. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  55651. return ret;
  55652. }
  55653. static int test_various_pathlen_chains(void)
  55654. {
  55655. EXPECT_DECLS;
  55656. WOLFSSL_CERT_MANAGER* cm = NULL;
  55657. /* Test chain G (large chain with varying pathLens) */
  55658. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55659. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  55660. ExpectIntEQ(test_chainG(cm), -1);
  55661. #else
  55662. ExpectIntEQ(test_chainG(cm), 0);
  55663. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  55664. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55665. wolfSSL_CertManagerFree(cm);
  55666. /* end test chain G */
  55667. /* Test chain H (5 chain with same pathLens) */
  55668. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55669. ExpectIntLT(test_chainH(cm), 0);
  55670. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55671. wolfSSL_CertManagerFree(cm);
  55672. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55673. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55674. wolfSSL_CertManagerFree(cm);
  55675. /* end test chain H */
  55676. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  55677. * followed by 2 ICA's, should pass) */
  55678. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55679. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  55680. ExpectIntEQ(test_chainI(cm), -1);
  55681. #else
  55682. ExpectIntEQ(test_chainI(cm), 0);
  55683. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  55684. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55685. wolfSSL_CertManagerFree(cm);
  55686. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55687. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55688. wolfSSL_CertManagerFree(cm);
  55689. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  55690. * this time followed by 3 ICA's, should fail */
  55691. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55692. ExpectIntLT(test_chainJ(cm), 0);
  55693. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55694. wolfSSL_CertManagerFree(cm);
  55695. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55696. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  55697. wolfSSL_CertManagerFree(cm);
  55698. return EXPECT_RESULT();
  55699. }
  55700. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  55701. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  55702. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  55703. {
  55704. EXPECT_DECLS;
  55705. byte ekm[100] = {0};
  55706. (void)ctx;
  55707. /* Success Cases */
  55708. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55709. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  55710. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55711. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  55712. /* Use some random context */
  55713. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55714. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  55715. /* Failure cases */
  55716. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55717. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  55718. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55719. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  55720. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55721. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  55722. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55723. "extended master secret", XSTR_SIZEOF("extended master secret"),
  55724. NULL, 0, 0), 0);
  55725. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  55726. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  55727. return EXPECT_RESULT();
  55728. }
  55729. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  55730. {
  55731. wolfSSL_KeepArrays(ssl);
  55732. return TEST_SUCCESS;
  55733. }
  55734. static int test_export_keying_material(void)
  55735. {
  55736. EXPECT_DECLS;
  55737. test_ssl_cbf serverCb;
  55738. test_ssl_cbf clientCb;
  55739. XMEMSET(&serverCb, 0, sizeof(serverCb));
  55740. XMEMSET(&clientCb, 0, sizeof(clientCb));
  55741. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  55742. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  55743. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  55744. return EXPECT_RESULT();
  55745. }
  55746. #endif /* HAVE_KEYING_MATERIAL */
  55747. static int test_wolfSSL_THREADID_hash(void)
  55748. {
  55749. EXPECT_DECLS;
  55750. #if defined(OPENSSL_EXTRA)
  55751. CRYPTO_THREADID id;
  55752. CRYPTO_THREADID_current(NULL);
  55753. /* Hash result is unsigned long. */
  55754. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  55755. XMEMSET(&id, 0, sizeof(id));
  55756. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  55757. #endif /* OPENSSL_EXTRA */
  55758. return EXPECT_RESULT();
  55759. }
  55760. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  55761. {
  55762. EXPECT_DECLS;
  55763. #if defined(OPENSSL_EXTRA)
  55764. WOLFSSL_CTX* ctx = NULL;
  55765. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  55766. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  55767. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  55768. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  55769. #endif /* OPENSSL_EXTRA */
  55770. return EXPECT_RESULT();
  55771. }
  55772. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  55773. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  55774. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  55775. {
  55776. EXPECT_DECLS;
  55777. callback_functions* callbacks = NULL;
  55778. WOLFSSL_CTX* ctx = NULL;
  55779. WOLFSSL* ssl = NULL;
  55780. SOCKET_T sfd = 0;
  55781. SOCKET_T cfd = 0;
  55782. word16 port;
  55783. char msg[] = "I hear you fa shizzle!";
  55784. int len = (int) XSTRLEN(msg);
  55785. char input[1024];
  55786. int ret = 0;
  55787. int err = 0;
  55788. if (!args)
  55789. WOLFSSL_RETURN_FROM_THREAD(0);
  55790. ((func_args*)args)->return_code = TEST_FAIL;
  55791. callbacks = ((func_args*)args)->callbacks;
  55792. ctx = wolfSSL_CTX_new(callbacks->method());
  55793. #if defined(USE_WINDOWS_API)
  55794. port = ((func_args*)args)->signal->port;
  55795. #else
  55796. /* Let tcp_listen assign port */
  55797. port = 0;
  55798. #endif
  55799. #ifdef WOLFSSL_TIRTOS
  55800. fdOpenSession(Task_self());
  55801. #endif
  55802. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  55803. caCertFile, 0));
  55804. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  55805. svrCertFile, WOLFSSL_FILETYPE_PEM));
  55806. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  55807. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  55808. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  55809. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  55810. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55811. #elif !defined(NO_DH)
  55812. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  55813. #endif
  55814. if (callbacks->ctx_ready)
  55815. callbacks->ctx_ready(ctx);
  55816. ssl = wolfSSL_new(ctx);
  55817. ExpectNotNull(ssl);
  55818. /* listen and accept */
  55819. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  55820. CloseSocket(sfd);
  55821. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  55822. if (callbacks->ssl_ready)
  55823. callbacks->ssl_ready(ssl);
  55824. if (EXPECT_SUCCESS()) {
  55825. do {
  55826. err = 0; /* Reset error */
  55827. ret = wolfSSL_accept(ssl);
  55828. if (ret != WOLFSSL_SUCCESS) {
  55829. err = wolfSSL_get_error(ssl, 0);
  55830. }
  55831. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  55832. }
  55833. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55834. /* read and write data */
  55835. XMEMSET(input, 0, sizeof(input));
  55836. while (EXPECT_SUCCESS()) {
  55837. ret = wolfSSL_read(ssl, input, sizeof(input));
  55838. if (ret > 0) {
  55839. break;
  55840. }
  55841. else {
  55842. err = wolfSSL_get_error(ssl,ret);
  55843. if (err == WOLFSSL_ERROR_WANT_READ) {
  55844. continue;
  55845. }
  55846. break;
  55847. }
  55848. }
  55849. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  55850. do {
  55851. ret = wolfSSL_write(ssl, msg, len);
  55852. if (ret > 0) {
  55853. break;
  55854. }
  55855. } while (ret < 0);
  55856. }
  55857. /* bidirectional shutdown */
  55858. while (EXPECT_SUCCESS()) {
  55859. ret = wolfSSL_shutdown(ssl);
  55860. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  55861. if (ret == WOLFSSL_SUCCESS) {
  55862. break;
  55863. }
  55864. }
  55865. if (EXPECT_SUCCESS()) {
  55866. /* wait for the peer to disconnect the tcp connection */
  55867. do {
  55868. ret = wolfSSL_read(ssl, input, sizeof(input));
  55869. err = wolfSSL_get_error(ssl, ret);
  55870. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  55871. }
  55872. /* detect TCP disconnect */
  55873. ExpectIntLE(ret,WOLFSSL_FAILURE);
  55874. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  55875. ((func_args*)args)->return_code = EXPECT_RESULT();
  55876. wolfSSL_free(ssl);
  55877. wolfSSL_CTX_free(ctx);
  55878. CloseSocket(cfd);
  55879. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  55880. wc_ecc_fp_free(); /* free per thread cache */
  55881. #endif
  55882. WOLFSSL_RETURN_FROM_THREAD(0);
  55883. }
  55884. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  55885. {
  55886. EXPECT_DECLS;
  55887. callback_functions* callbacks = NULL;
  55888. WOLFSSL_CTX* ctx = NULL;
  55889. WOLFSSL* ssl = NULL;
  55890. SOCKET_T sfd = 0;
  55891. char msg[] = "hello wolfssl server!";
  55892. int len = (int) XSTRLEN(msg);
  55893. char input[1024];
  55894. int idx;
  55895. int ret, err;
  55896. if (!args)
  55897. WOLFSSL_RETURN_FROM_THREAD(0);
  55898. ((func_args*)args)->return_code = TEST_FAIL;
  55899. callbacks = ((func_args*)args)->callbacks;
  55900. ctx = wolfSSL_CTX_new(callbacks->method());
  55901. #ifdef WOLFSSL_TIRTOS
  55902. fdOpenSession(Task_self());
  55903. #endif
  55904. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  55905. caCertFile, 0));
  55906. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  55907. cliCertFile, WOLFSSL_FILETYPE_PEM));
  55908. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  55909. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  55910. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  55911. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  55912. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  55913. if (EXPECT_SUCCESS()) {
  55914. do {
  55915. err = 0; /* Reset error */
  55916. ret = wolfSSL_connect(ssl);
  55917. if (ret != WOLFSSL_SUCCESS) {
  55918. err = wolfSSL_get_error(ssl, 0);
  55919. }
  55920. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  55921. }
  55922. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  55923. if (EXPECT_SUCCESS()) {
  55924. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  55925. input[idx] = 0;
  55926. }
  55927. }
  55928. if (EXPECT_SUCCESS()) {
  55929. ret = wolfSSL_shutdown(ssl);
  55930. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  55931. ret = wolfSSL_shutdown(ssl);
  55932. }
  55933. }
  55934. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  55935. ((func_args*)args)->return_code = EXPECT_RESULT();
  55936. wolfSSL_free(ssl);
  55937. wolfSSL_CTX_free(ctx);
  55938. CloseSocket(sfd);
  55939. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  55940. wc_ecc_fp_free(); /* free per thread cache */
  55941. #endif
  55942. WOLFSSL_RETURN_FROM_THREAD(0);
  55943. }
  55944. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  55945. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  55946. /* This test is to check wolfSSL_read behaves as same as
  55947. * openSSL when it is called after SSL_shutdown completes.
  55948. */
  55949. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  55950. {
  55951. EXPECT_DECLS;
  55952. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  55953. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  55954. tcp_ready ready;
  55955. func_args client_args;
  55956. func_args server_args;
  55957. THREAD_TYPE serverThread;
  55958. THREAD_TYPE clientThread;
  55959. callback_functions server_cbf;
  55960. callback_functions client_cbf;
  55961. #ifdef WOLFSSL_TIRTOS
  55962. fdOpenSession(Task_self());
  55963. #endif
  55964. StartTCP();
  55965. InitTcpReady(&ready);
  55966. #if defined(USE_WINDOWS_API)
  55967. /* use RNG to get random port if using windows */
  55968. ready.port = GetRandomPort();
  55969. #endif
  55970. XMEMSET(&client_args, 0, sizeof(func_args));
  55971. XMEMSET(&server_args, 0, sizeof(func_args));
  55972. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  55973. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  55974. server_cbf.method = wolfTLSv1_2_server_method;
  55975. client_cbf.method = wolfTLSv1_2_client_method;
  55976. server_args.callbacks = &server_cbf;
  55977. client_args.callbacks = &client_cbf;
  55978. server_args.signal = &ready;
  55979. client_args.signal = &ready;
  55980. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  55981. wait_tcp_ready(&server_args);
  55982. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  55983. join_thread(clientThread);
  55984. join_thread(serverThread);
  55985. ExpectTrue(client_args.return_code);
  55986. ExpectTrue(server_args.return_code);
  55987. FreeTcpReady(&ready);
  55988. #endif
  55989. return EXPECT_RESULT();
  55990. }
  55991. static int test_wolfSSL_CTX_get_min_proto_version(void)
  55992. {
  55993. EXPECT_DECLS;
  55994. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  55995. WOLFSSL_CTX *ctx = NULL;
  55996. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55997. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  55998. WOLFSSL_SUCCESS);
  55999. #ifdef WOLFSSL_ALLOW_SSLV3
  56000. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56001. #else
  56002. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56003. #endif
  56004. wolfSSL_CTX_free(ctx);
  56005. ctx = NULL;
  56006. #ifdef WOLFSSL_ALLOW_TLSV10
  56007. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  56008. #else
  56009. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56010. #endif
  56011. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  56012. WOLFSSL_SUCCESS);
  56013. #ifdef WOLFSSL_ALLOW_TLSV10
  56014. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56015. #else
  56016. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56017. #endif
  56018. wolfSSL_CTX_free(ctx);
  56019. ctx = NULL;
  56020. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56021. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  56022. WOLFSSL_SUCCESS);
  56023. #ifndef NO_OLD_TLS
  56024. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56025. #else
  56026. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56027. #endif
  56028. wolfSSL_CTX_free(ctx);
  56029. ctx = NULL;
  56030. #ifndef WOLFSSL_NO_TLS12
  56031. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  56032. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  56033. WOLFSSL_SUCCESS);
  56034. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  56035. wolfSSL_CTX_free(ctx);
  56036. ctx = NULL;
  56037. #endif
  56038. #ifdef WOLFSSL_TLS13
  56039. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  56040. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  56041. WOLFSSL_SUCCESS);
  56042. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  56043. wolfSSL_CTX_free(ctx);
  56044. ctx = NULL;
  56045. #endif
  56046. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56047. return EXPECT_RESULT();
  56048. }
  56049. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56050. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56051. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56052. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  56053. static int test_wolfSSL_set_SSL_CTX(void)
  56054. {
  56055. EXPECT_DECLS;
  56056. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  56057. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  56058. !defined(NO_RSA)
  56059. WOLFSSL_CTX *ctx1 = NULL;
  56060. WOLFSSL_CTX *ctx2 = NULL;
  56061. WOLFSSL *ssl = NULL;
  56062. const byte *session_id1 = (const byte *)"CTX1";
  56063. const byte *session_id2 = (const byte *)"CTX2";
  56064. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56065. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  56066. WOLFSSL_FILETYPE_PEM));
  56067. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  56068. WOLFSSL_FILETYPE_PEM));
  56069. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  56070. WOLFSSL_SUCCESS);
  56071. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  56072. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  56073. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  56074. WOLFSSL_SUCCESS);
  56075. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56076. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  56077. WOLFSSL_FILETYPE_PEM));
  56078. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  56079. WOLFSSL_FILETYPE_PEM));
  56080. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  56081. WOLFSSL_SUCCESS);
  56082. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  56083. WOLFSSL_SUCCESS);
  56084. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  56085. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  56086. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  56087. WOLFSSL_SUCCESS);
  56088. #ifdef HAVE_SESSION_TICKET
  56089. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  56090. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  56091. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  56092. #endif
  56093. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  56094. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56095. #ifdef WOLFSSL_INT_H
  56096. #ifdef WOLFSSL_SESSION_ID_CTX
  56097. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  56098. #endif
  56099. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  56100. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  56101. #endif
  56102. #ifdef HAVE_SESSION_TICKET
  56103. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56104. #endif
  56105. /* Set the ctx1 that has TLSv1.3 as max proto version */
  56106. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  56107. /* MUST not change proto versions of ssl */
  56108. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56109. #ifdef HAVE_SESSION_TICKET
  56110. /* MUST not change */
  56111. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56112. #endif
  56113. /* MUST change */
  56114. #ifdef WOLFSSL_INT_H
  56115. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  56116. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  56117. #ifdef WOLFSSL_SESSION_ID_CTX
  56118. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  56119. #endif
  56120. #endif
  56121. wolfSSL_free(ssl);
  56122. wolfSSL_CTX_free(ctx1);
  56123. wolfSSL_CTX_free(ctx2);
  56124. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56125. return EXPECT_RESULT();
  56126. }
  56127. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56128. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56129. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56130. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  56131. static int test_wolfSSL_security_level(void)
  56132. {
  56133. EXPECT_DECLS;
  56134. #if defined(OPENSSL_EXTRA)
  56135. SSL_CTX *ctx = NULL;
  56136. #ifdef WOLFSSL_TLS13
  56137. #ifdef NO_WOLFSSL_SERVER
  56138. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56139. #else
  56140. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56141. #endif
  56142. SSL_CTX_set_security_level(NULL, 1);
  56143. SSL_CTX_set_security_level(ctx, 1);
  56144. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  56145. /* Stub so nothing happens. */
  56146. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  56147. SSL_CTX_free(ctx);
  56148. #else
  56149. (void)ctx;
  56150. #endif
  56151. #endif
  56152. return EXPECT_RESULT();
  56153. }
  56154. static int test_wolfSSL_SSL_in_init(void)
  56155. {
  56156. EXPECT_DECLS;
  56157. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  56158. SSL_CTX* ctx = NULL;
  56159. SSL* ssl = NULL;
  56160. const char* testCertFile;
  56161. const char* testKeyFile;
  56162. #ifdef WOLFSSL_TLS13
  56163. #ifdef NO_WOLFSSL_SERVER
  56164. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56165. #else
  56166. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56167. #endif
  56168. #else
  56169. #ifdef NO_WOLFSSL_SERVER
  56170. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56171. #else
  56172. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56173. #endif
  56174. #endif
  56175. #ifndef NO_RSA
  56176. testCertFile = svrCertFile;
  56177. testKeyFile = svrKeyFile;
  56178. #elif defined(HAVE_ECC)
  56179. testCertFile = eccCertFile;
  56180. testKeyFile = eccKeyFile;
  56181. #else
  56182. testCertFile = NULL;
  56183. testKeyFile = NULL;
  56184. #endif
  56185. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  56186. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56187. SSL_FILETYPE_PEM));
  56188. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56189. SSL_FILETYPE_PEM));
  56190. }
  56191. ExpectNotNull(ssl = SSL_new(ctx));
  56192. ExpectIntEQ(SSL_in_init(ssl), 1);
  56193. SSL_CTX_free(ctx);
  56194. SSL_free(ssl);
  56195. #endif
  56196. return EXPECT_RESULT();
  56197. }
  56198. static int test_wolfSSL_CTX_set_timeout(void)
  56199. {
  56200. EXPECT_DECLS;
  56201. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  56202. int timeout;
  56203. WOLFSSL_CTX* ctx = NULL;
  56204. (void)timeout;
  56205. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56206. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  56207. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  56208. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  56209. */
  56210. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56211. /* giving 0 as timeout value sets default timeout */
  56212. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  56213. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  56214. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  56215. #else
  56216. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56217. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  56218. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  56219. #endif
  56220. wolfSSL_CTX_free(ctx);
  56221. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  56222. return EXPECT_RESULT();
  56223. }
  56224. static int test_wolfSSL_OpenSSL_version(void)
  56225. {
  56226. EXPECT_DECLS;
  56227. #if defined(OPENSSL_EXTRA)
  56228. const char* ver;
  56229. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  56230. ExpectNotNull(ver = OpenSSL_version(0));
  56231. #else
  56232. ExpectNotNull(ver = OpenSSL_version());
  56233. #endif
  56234. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  56235. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  56236. #endif
  56237. return EXPECT_RESULT();
  56238. }
  56239. static int test_CONF_CTX_CMDLINE(void)
  56240. {
  56241. EXPECT_DECLS;
  56242. #if defined(OPENSSL_ALL)
  56243. SSL_CTX* ctx = NULL;
  56244. SSL_CONF_CTX* cctx = NULL;
  56245. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  56246. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56247. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  56248. /* set flags */
  56249. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  56250. WOLFSSL_CONF_FLAG_CMDLINE);
  56251. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  56252. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  56253. /* cmd invalid command */
  56254. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  56255. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  56256. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  56257. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  56258. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  56259. /* cmd Certificate and Private Key*/
  56260. {
  56261. #if !defined(NO_CERTS) && !defined(NO_RSA)
  56262. const char* ourCert = svrCertFile;
  56263. const char* ourKey = svrKeyFile;
  56264. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  56265. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  56266. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  56267. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  56268. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56269. #endif
  56270. }
  56271. /* cmd curves */
  56272. {
  56273. #if defined(HAVE_ECC)
  56274. const char* curve = "secp256r1";
  56275. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  56276. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  56277. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56278. #endif
  56279. }
  56280. /* cmd CipherString */
  56281. {
  56282. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  56283. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  56284. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  56285. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56286. }
  56287. /* cmd DH parameter */
  56288. {
  56289. #if !defined(NO_DH) && !defined(NO_BIO)
  56290. const char* ourdhcert = "./certs/dh2048.pem";
  56291. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  56292. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  56293. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56294. #endif
  56295. }
  56296. SSL_CTX_free(ctx);
  56297. SSL_CONF_CTX_free(cctx);
  56298. #endif /* OPENSSL_EXTRA */
  56299. return EXPECT_RESULT();
  56300. }
  56301. static int test_CONF_CTX_FILE(void)
  56302. {
  56303. EXPECT_DECLS;
  56304. #if defined(OPENSSL_ALL)
  56305. SSL_CTX* ctx = NULL;
  56306. SSL_CONF_CTX* cctx = NULL;
  56307. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  56308. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56309. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  56310. /* set flags */
  56311. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  56312. WOLFSSL_CONF_FLAG_FILE);
  56313. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  56314. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  56315. /* sanity check */
  56316. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  56317. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  56318. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  56319. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  56320. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  56321. /* cmd Certificate and Private Key*/
  56322. {
  56323. #if !defined(NO_CERTS) && !defined(NO_RSA)
  56324. const char* ourCert = svrCertFile;
  56325. const char* ourKey = svrKeyFile;
  56326. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  56327. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  56328. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  56329. WOLFSSL_SUCCESS);
  56330. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  56331. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56332. #endif
  56333. }
  56334. /* cmd curves */
  56335. {
  56336. #if defined(HAVE_ECC)
  56337. const char* curve = "secp256r1";
  56338. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  56339. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  56340. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56341. #endif
  56342. }
  56343. /* cmd CipherString */
  56344. {
  56345. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  56346. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  56347. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  56348. WOLFSSL_SUCCESS);
  56349. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56350. }
  56351. /* cmd DH parameter */
  56352. {
  56353. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  56354. const char* ourdhcert = "./certs/dh3072.pem";
  56355. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  56356. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  56357. WOLFSSL_SUCCESS);
  56358. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56359. #endif
  56360. }
  56361. SSL_CTX_free(ctx);
  56362. SSL_CONF_CTX_free(cctx);
  56363. #endif /* OPENSSL_EXTRA */
  56364. return EXPECT_RESULT();
  56365. }
  56366. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  56367. {
  56368. EXPECT_DECLS;
  56369. #ifdef HAVE_EX_DATA
  56370. int idx1, idx2;
  56371. /* test for unsupported class index */
  56372. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  56373. 0,NULL, NULL, NULL, NULL ), -1);
  56374. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  56375. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  56376. 0,NULL, NULL, NULL, NULL ), -1);
  56377. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  56378. 0,NULL, NULL, NULL, NULL ), -1);
  56379. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  56380. 0,NULL, NULL, NULL, NULL ), -1);
  56381. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  56382. 0,NULL, NULL, NULL, NULL ), -1);
  56383. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  56384. 0,NULL, NULL, NULL, NULL ), -1);
  56385. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  56386. 0,NULL, NULL, NULL, NULL ), -1);
  56387. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  56388. 0,NULL, NULL, NULL, NULL ), -1);
  56389. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  56390. 0,NULL, NULL, NULL, NULL ), -1);
  56391. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  56392. 0,NULL, NULL, NULL, NULL ), -1);
  56393. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  56394. 0,NULL, NULL, NULL, NULL ), -1);
  56395. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  56396. 0,NULL, NULL, NULL, NULL ), -1);
  56397. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  56398. 0,NULL, NULL, NULL, NULL ), -1);
  56399. /* test for supported class index */
  56400. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  56401. 0,NULL, NULL, NULL, NULL );
  56402. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  56403. 0,NULL, NULL, NULL, NULL );
  56404. ExpectIntNE(idx1, -1);
  56405. ExpectIntNE(idx2, -1);
  56406. ExpectIntNE(idx1, idx2);
  56407. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  56408. 0,NULL, NULL, NULL, NULL );
  56409. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  56410. 0,NULL, NULL, NULL, NULL );
  56411. ExpectIntNE(idx1, -1);
  56412. ExpectIntNE(idx2, -1);
  56413. ExpectIntNE(idx1, idx2);
  56414. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  56415. 0,NULL, NULL, NULL, NULL );
  56416. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  56417. 0,NULL, NULL, NULL, NULL );
  56418. ExpectIntNE(idx1, -1);
  56419. ExpectIntNE(idx2, -1);
  56420. ExpectIntNE(idx1, idx2);
  56421. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  56422. 0,NULL, NULL, NULL, NULL );
  56423. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  56424. 0,NULL, NULL, NULL, NULL );
  56425. ExpectIntNE(idx1, -1);
  56426. ExpectIntNE(idx2, -1);
  56427. ExpectIntNE(idx1, idx2);
  56428. #endif /* HAVE_EX_DATA */
  56429. return EXPECT_RESULT();
  56430. }
  56431. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  56432. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56433. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56434. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56435. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  56436. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  56437. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  56438. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  56439. #define SESSION_NEW_IDX_PTR "Testing"
  56440. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  56441. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  56442. {
  56443. AssertNotNull(p);
  56444. AssertNull(ptr);
  56445. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  56446. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  56447. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  56448. }
  56449. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  56450. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  56451. void* arg)
  56452. {
  56453. EXPECT_DECLS;
  56454. ExpectNotNull(out);
  56455. ExpectNotNull(in);
  56456. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  56457. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  56458. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  56459. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  56460. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  56461. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  56462. if (EXPECT_SUCCESS()) {
  56463. return SSL_SUCCESS;
  56464. }
  56465. else {
  56466. return SSL_FAILURE;
  56467. }
  56468. }
  56469. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  56470. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  56471. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  56472. {
  56473. EXPECT_DECLS;
  56474. ExpectNotNull(p);
  56475. ExpectNull(ptr);
  56476. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  56477. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  56478. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  56479. if (EXPECT_SUCCESS()) {
  56480. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  56481. }
  56482. }
  56483. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  56484. {
  56485. EXPECT_DECLS;
  56486. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  56487. (void*)SESSION_NEW_IDX_PTR,
  56488. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  56489. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  56490. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  56491. SSL_SESSION* s = SSL_SESSION_new();
  56492. SSL_SESSION* d = NULL;
  56493. ExpectNotNull(s);
  56494. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  56495. ExpectNotNull(d = SSL_SESSION_dup(s));
  56496. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  56497. SSL_SESSION_free(s);
  56498. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  56499. SSL_SESSION_free(d);
  56500. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  56501. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  56502. crypto_ex_cb_ctx_session = NULL;
  56503. return EXPECT_RESULT();
  56504. }
  56505. #else
  56506. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  56507. {
  56508. return TEST_SKIPPED;
  56509. }
  56510. #endif
  56511. static int test_wolfSSL_set_psk_use_session_callback(void)
  56512. {
  56513. EXPECT_DECLS;
  56514. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  56515. SSL_CTX* ctx = NULL;
  56516. SSL* ssl = NULL;
  56517. const char* testCertFile;
  56518. const char* testKeyFile;
  56519. #ifdef WOLFSSL_TLS13
  56520. #ifdef NO_WOLFSSL_SERVER
  56521. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56522. #else
  56523. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56524. #endif
  56525. #else
  56526. #ifdef NO_WOLFSSL_SERVER
  56527. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56528. #else
  56529. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56530. #endif
  56531. #endif
  56532. #ifndef NO_RSA
  56533. testCertFile = svrCertFile;
  56534. testKeyFile = svrKeyFile;
  56535. #elif defined(HAVE_ECC)
  56536. testCertFile = eccCertFile;
  56537. testKeyFile = eccKeyFile;
  56538. #else
  56539. testCertFile = NULL;
  56540. testKeyFile = NULL;
  56541. #endif
  56542. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  56543. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56544. SSL_FILETYPE_PEM));
  56545. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56546. SSL_FILETYPE_PEM));
  56547. }
  56548. ExpectNotNull(ssl = SSL_new(ctx));
  56549. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  56550. SSL_CTX_free(ctx);
  56551. SSL_free(ssl);
  56552. #endif
  56553. return EXPECT_RESULT();
  56554. }
  56555. static int test_wolfSSL_ERR_strings(void)
  56556. {
  56557. EXPECT_DECLS;
  56558. #if !defined(NO_ERROR_STRINGS)
  56559. const char* err1 = "unsupported cipher suite";
  56560. const char* err2 = "wolfSSL PEM routines";
  56561. const char* err = NULL;
  56562. (void)err;
  56563. (void)err1;
  56564. (void)err2;
  56565. #if defined(OPENSSL_EXTRA)
  56566. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  56567. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  56568. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  56569. ExpectIntEQ((*err == '\0'), 1);
  56570. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  56571. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  56572. #else
  56573. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  56574. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  56575. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  56576. ExpectIntEQ((*err == '\0'), 1);
  56577. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  56578. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  56579. ExpectIntEQ((*err == '\0'), 1);
  56580. #endif
  56581. #endif
  56582. return EXPECT_RESULT();
  56583. }
  56584. static int test_wolfSSL_EVP_shake128(void)
  56585. {
  56586. EXPECT_DECLS;
  56587. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  56588. defined(WOLFSSL_SHAKE128)
  56589. const EVP_MD* md = NULL;
  56590. ExpectNotNull(md = EVP_shake128());
  56591. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  56592. #endif
  56593. return EXPECT_RESULT();
  56594. }
  56595. static int test_wolfSSL_EVP_shake256(void)
  56596. {
  56597. EXPECT_DECLS;
  56598. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  56599. defined(WOLFSSL_SHAKE256)
  56600. const EVP_MD* md = NULL;
  56601. ExpectNotNull(md = EVP_shake256());
  56602. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  56603. #endif
  56604. return EXPECT_RESULT();
  56605. }
  56606. /*
  56607. * Testing EVP digest API with SM3
  56608. */
  56609. static int test_wolfSSL_EVP_sm3(void)
  56610. {
  56611. int res = TEST_SKIPPED;
  56612. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  56613. EXPECT_DECLS;
  56614. const EVP_MD* md = NULL;
  56615. EVP_MD_CTX* mdCtx = NULL;
  56616. byte data[WC_SM3_BLOCK_SIZE * 4];
  56617. byte hash[WC_SM3_DIGEST_SIZE];
  56618. byte calcHash[WC_SM3_DIGEST_SIZE];
  56619. byte expHash[WC_SM3_DIGEST_SIZE] = {
  56620. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  56621. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  56622. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  56623. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  56624. };
  56625. word32 chunk;
  56626. word32 i;
  56627. unsigned int sz;
  56628. int ret;
  56629. XMEMSET(data, 0, sizeof(data));
  56630. md = EVP_sm3();
  56631. ExpectTrue(md != NULL);
  56632. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  56633. mdCtx = EVP_MD_CTX_new();
  56634. ExpectTrue(mdCtx != NULL);
  56635. /* Invalid Parameters */
  56636. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  56637. /* Valid Parameters */
  56638. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  56639. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  56640. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  56641. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  56642. /* Valid Parameters */
  56643. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  56644. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  56645. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  56646. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  56647. WOLFSSL_SUCCESS);
  56648. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  56649. WOLFSSL_SUCCESS);
  56650. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  56651. WOLFSSL_SUCCESS);
  56652. /* Ensure too many bytes for lengths. */
  56653. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  56654. WOLFSSL_SUCCESS);
  56655. /* Invalid Parameters */
  56656. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  56657. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  56658. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  56659. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  56660. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  56661. /* Valid Parameters */
  56662. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  56663. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  56664. /* Chunk tests. */
  56665. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  56666. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  56667. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  56668. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  56669. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  56670. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  56671. WOLFSSL_SUCCESS);
  56672. }
  56673. if (i < (word32)sizeof(data)) {
  56674. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  56675. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  56676. }
  56677. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  56678. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  56679. }
  56680. /* Not testing when the low 32-bit length overflows. */
  56681. ret = EVP_MD_CTX_cleanup(mdCtx);
  56682. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56683. wolfSSL_EVP_MD_CTX_free(mdCtx);
  56684. res = EXPECT_RESULT();
  56685. #endif
  56686. return res;
  56687. } /* END test_EVP_sm3 */
  56688. static int test_EVP_blake2(void)
  56689. {
  56690. EXPECT_DECLS;
  56691. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  56692. const EVP_MD* md = NULL;
  56693. (void)md;
  56694. #if defined(HAVE_BLAKE2)
  56695. ExpectNotNull(md = EVP_blake2b512());
  56696. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  56697. #endif
  56698. #if defined(HAVE_BLAKE2S)
  56699. ExpectNotNull(md = EVP_blake2s256());
  56700. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  56701. #endif
  56702. #endif
  56703. return EXPECT_RESULT();
  56704. }
  56705. #if defined(OPENSSL_EXTRA)
  56706. static void list_md_fn(const EVP_MD* m, const char* from,
  56707. const char* to, void* arg)
  56708. {
  56709. const char* mn;
  56710. BIO *bio;
  56711. (void) from;
  56712. (void) to;
  56713. (void) arg;
  56714. (void) mn;
  56715. (void) bio;
  56716. if (!m) {
  56717. /* alias */
  56718. AssertNull(m);
  56719. AssertNotNull(to);
  56720. }
  56721. else {
  56722. AssertNotNull(m);
  56723. AssertNull(to);
  56724. }
  56725. AssertNotNull(from);
  56726. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  56727. mn = EVP_get_digestbyname(from);
  56728. /* print to stderr */
  56729. AssertNotNull(arg);
  56730. bio = BIO_new(BIO_s_file());
  56731. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  56732. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  56733. BIO_free(bio);
  56734. #endif
  56735. }
  56736. #endif
  56737. static int test_EVP_MD_do_all(void)
  56738. {
  56739. int res = TEST_SKIPPED;
  56740. #if defined(OPENSSL_EXTRA)
  56741. EVP_MD_do_all(NULL, stderr);
  56742. EVP_MD_do_all(list_md_fn, stderr);
  56743. res = TEST_SUCCESS;
  56744. #endif
  56745. return res;
  56746. }
  56747. #if defined(OPENSSL_EXTRA)
  56748. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  56749. {
  56750. (void)arg;
  56751. (void)nm;
  56752. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  56753. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  56754. /* print to stderr */
  56755. AssertNotNull(arg);
  56756. BIO *bio = BIO_new(BIO_s_file());
  56757. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  56758. BIO_printf(bio, "%s\n", nm);
  56759. BIO_free(bio);
  56760. #endif
  56761. }
  56762. #endif
  56763. static int test_OBJ_NAME_do_all(void)
  56764. {
  56765. int res = TEST_SKIPPED;
  56766. #if defined(OPENSSL_EXTRA)
  56767. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  56768. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  56769. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  56770. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  56771. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  56772. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  56773. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  56774. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  56775. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  56776. res = TEST_SUCCESS;
  56777. #endif
  56778. return res;
  56779. }
  56780. static int test_SSL_CIPHER_get_xxx(void)
  56781. {
  56782. EXPECT_DECLS;
  56783. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  56784. !defined(NO_FILESYSTEM)
  56785. const SSL_CIPHER* cipher = NULL;
  56786. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  56787. int i, numCiphers = 0;
  56788. SSL_CTX* ctx = NULL;
  56789. SSL* ssl = NULL;
  56790. const char* testCertFile;
  56791. const char* testKeyFile;
  56792. char buf[256] = {0};
  56793. const char* cipher_id = NULL;
  56794. int expect_nid1 = NID_undef;
  56795. int expect_nid2 = NID_undef;
  56796. int expect_nid3 = NID_undef;
  56797. int expect_nid4 = NID_undef;
  56798. int expect_nid5 = 0;
  56799. const char* cipher_id2 = NULL;
  56800. int expect_nid21 = NID_undef;
  56801. int expect_nid22 = NID_undef;
  56802. int expect_nid23 = NID_undef;
  56803. int expect_nid24 = NID_undef;
  56804. int expect_nid25 = 0;
  56805. (void)cipher;
  56806. (void)supportedCiphers;
  56807. (void)i;
  56808. (void)numCiphers;
  56809. (void)ctx;
  56810. (void)ssl;
  56811. (void)testCertFile;
  56812. (void)testKeyFile;
  56813. #if defined(WOLFSSL_TLS13)
  56814. cipher_id = "TLS13-AES128-GCM-SHA256";
  56815. expect_nid1 = NID_auth_rsa;
  56816. expect_nid2 = NID_aes_128_gcm;
  56817. expect_nid3 = NID_sha256;
  56818. expect_nid4 = NID_kx_any;
  56819. expect_nid5 = 1;
  56820. #if !defined(WOLFSSL_NO_TLS12)
  56821. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  56822. expect_nid21 = NID_auth_rsa;
  56823. expect_nid22 = NID_aes_256_gcm;
  56824. expect_nid23 = NID_sha384;
  56825. expect_nid24 = NID_kx_ecdhe;
  56826. expect_nid25 = 1;
  56827. #endif
  56828. #endif
  56829. #ifdef NO_WOLFSSL_SERVER
  56830. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56831. #else
  56832. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56833. #endif
  56834. if (cipher_id) {
  56835. #ifndef NO_RSA
  56836. testCertFile = svrCertFile;
  56837. testKeyFile = svrKeyFile;
  56838. #elif defined(HAVE_ECC)
  56839. testCertFile = eccCertFile;
  56840. testKeyFile = eccKeyFile;
  56841. #else
  56842. testCertFile = NULL;
  56843. testKeyFile = NULL;
  56844. #endif
  56845. if (testCertFile != NULL && testKeyFile != NULL) {
  56846. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56847. SSL_FILETYPE_PEM));
  56848. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56849. SSL_FILETYPE_PEM));
  56850. }
  56851. ExpectNotNull(ssl = SSL_new(ctx));
  56852. ExpectIntEQ(SSL_in_init(ssl), 1);
  56853. supportedCiphers = SSL_get_ciphers(ssl);
  56854. numCiphers = sk_num(supportedCiphers);
  56855. for (i = 0; i < numCiphers; ++i) {
  56856. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  56857. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  56858. }
  56859. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  56860. break;
  56861. }
  56862. }
  56863. /* test case for */
  56864. if (i != numCiphers) {
  56865. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  56866. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  56867. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  56868. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  56869. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  56870. }
  56871. if (cipher_id2) {
  56872. for (i = 0; i < numCiphers; ++i) {
  56873. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  56874. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  56875. }
  56876. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  56877. break;
  56878. }
  56879. }
  56880. /* test case for */
  56881. if (i != numCiphers) {
  56882. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  56883. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  56884. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  56885. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  56886. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  56887. }
  56888. }
  56889. }
  56890. SSL_CTX_free(ctx);
  56891. SSL_free(ssl);
  56892. #endif
  56893. return EXPECT_RESULT();
  56894. }
  56895. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56896. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  56897. int* keyFormat)
  56898. {
  56899. int ret;
  56900. byte* key_buf = NULL;
  56901. size_t key_sz = 0;
  56902. EncryptedInfo encInfo;
  56903. XMEMSET(&encInfo, 0, sizeof(encInfo));
  56904. ret = load_file(privKeyFile, &key_buf, &key_sz);
  56905. if (ret == 0) {
  56906. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  56907. NULL, &encInfo, keyFormat);
  56908. }
  56909. if (key_buf != NULL) {
  56910. free(key_buf); key_buf = NULL;
  56911. }
  56912. (void)encInfo; /* not used in this test */
  56913. #ifdef DEBUG_WOLFSSL
  56914. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  56915. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  56916. (*pDer)->length);
  56917. #endif
  56918. return ret;
  56919. }
  56920. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  56921. {
  56922. int ret = CRYPTOCB_UNAVAILABLE;
  56923. const char* privKeyFile = (const char*)ctx;
  56924. DerBuffer* pDer = NULL;
  56925. int keyFormat = 0;
  56926. if (info->algo_type == WC_ALGO_TYPE_PK) {
  56927. #ifdef DEBUG_WOLFSSL
  56928. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  56929. #endif
  56930. #ifndef NO_RSA
  56931. if (info->pk.type == WC_PK_TYPE_RSA) {
  56932. switch (info->pk.rsa.type) {
  56933. case RSA_PUBLIC_ENCRYPT:
  56934. case RSA_PUBLIC_DECRYPT:
  56935. /* perform software based RSA public op */
  56936. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  56937. break;
  56938. case RSA_PRIVATE_ENCRYPT:
  56939. case RSA_PRIVATE_DECRYPT:
  56940. {
  56941. RsaKey key;
  56942. /* perform software based RSA private op */
  56943. #ifdef DEBUG_WOLFSSL
  56944. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  56945. #endif
  56946. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  56947. &keyFormat);
  56948. if (ret != 0) {
  56949. return ret;
  56950. }
  56951. ret = wc_InitRsaKey(&key, HEAP_HINT);
  56952. if (ret == 0) {
  56953. word32 keyIdx = 0;
  56954. /* load RSA private key and perform private transform */
  56955. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  56956. &key, pDer->length);
  56957. if (ret == 0) {
  56958. ret = wc_RsaFunction(
  56959. info->pk.rsa.in, info->pk.rsa.inLen,
  56960. info->pk.rsa.out, info->pk.rsa.outLen,
  56961. info->pk.rsa.type, &key, info->pk.rsa.rng);
  56962. }
  56963. else {
  56964. /* if decode fails, then fall-back to software based crypto */
  56965. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  56966. "key decode failed %d, falling back to "
  56967. "software\n", ret);
  56968. ret = CRYPTOCB_UNAVAILABLE;
  56969. }
  56970. wc_FreeRsaKey(&key);
  56971. }
  56972. wc_FreeDer(&pDer); pDer = NULL;
  56973. break;
  56974. }
  56975. }
  56976. #ifdef DEBUG_WOLFSSL
  56977. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  56978. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  56979. #endif
  56980. }
  56981. #endif /* !NO_RSA */
  56982. #ifdef HAVE_ECC
  56983. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  56984. /* mark this key as ephemeral */
  56985. if (info->pk.eckg.key != NULL) {
  56986. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  56987. sizeof(info->pk.eckg.key->label));
  56988. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  56989. }
  56990. }
  56991. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  56992. ecc_key key;
  56993. /* perform software based ECC sign */
  56994. #ifdef DEBUG_WOLFSSL
  56995. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  56996. #endif
  56997. if (info->pk.eccsign.key != NULL &&
  56998. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  56999. /* this is an empheral key */
  57000. #ifdef DEBUG_WOLFSSL
  57001. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  57002. "ephemeral key\n");
  57003. #endif
  57004. return CRYPTOCB_UNAVAILABLE;
  57005. }
  57006. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57007. if (ret != 0) {
  57008. return ret;
  57009. }
  57010. ret = wc_ecc_init(&key);
  57011. if (ret == 0) {
  57012. word32 keyIdx = 0;
  57013. /* load ECC private key and perform private transform */
  57014. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  57015. &key, pDer->length);
  57016. if (ret == 0) {
  57017. ret = wc_ecc_sign_hash(
  57018. info->pk.eccsign.in, info->pk.eccsign.inlen,
  57019. info->pk.eccsign.out, info->pk.eccsign.outlen,
  57020. info->pk.eccsign.rng, &key);
  57021. }
  57022. else {
  57023. /* if decode fails, then fall-back to software based crypto */
  57024. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  57025. "decode failed %d, falling back to software\n", ret);
  57026. ret = CRYPTOCB_UNAVAILABLE;
  57027. }
  57028. wc_ecc_free(&key);
  57029. }
  57030. wc_FreeDer(&pDer); pDer = NULL;
  57031. #ifdef DEBUG_WOLFSSL
  57032. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  57033. ret, *info->pk.eccsign.outlen);
  57034. #endif
  57035. }
  57036. #endif /* HAVE_ECC */
  57037. #ifdef HAVE_ED25519
  57038. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  57039. ed25519_key key;
  57040. /* perform software based ED25519 sign */
  57041. #ifdef DEBUG_WOLFSSL
  57042. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  57043. #endif
  57044. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57045. if (ret != 0) {
  57046. return ret;
  57047. }
  57048. ret = wc_ed25519_init(&key);
  57049. if (ret == 0) {
  57050. word32 keyIdx = 0;
  57051. /* load ED25519 private key and perform private transform */
  57052. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  57053. &key, pDer->length);
  57054. if (ret == 0) {
  57055. /* calculate public key */
  57056. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  57057. if (ret == 0) {
  57058. key.pubKeySet = 1;
  57059. ret = wc_ed25519_sign_msg_ex(
  57060. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  57061. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  57062. &key, info->pk.ed25519sign.type,
  57063. info->pk.ed25519sign.context,
  57064. info->pk.ed25519sign.contextLen);
  57065. }
  57066. }
  57067. else {
  57068. /* if decode fails, then fall-back to software based crypto */
  57069. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  57070. "decode failed %d, falling back to software\n", ret);
  57071. ret = CRYPTOCB_UNAVAILABLE;
  57072. }
  57073. wc_ed25519_free(&key);
  57074. }
  57075. wc_FreeDer(&pDer); pDer = NULL;
  57076. #ifdef DEBUG_WOLFSSL
  57077. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  57078. ret, *info->pk.ed25519sign.outLen);
  57079. #endif
  57080. }
  57081. #endif /* HAVE_ED25519 */
  57082. }
  57083. (void)thisDevId;
  57084. (void)keyFormat;
  57085. return ret;
  57086. }
  57087. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57088. static int test_wc_CryptoCb_TLS(int tlsVer,
  57089. const char* cliCaPemFile, const char* cliCertPemFile,
  57090. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  57091. const char* svrCaPemFile, const char* svrCertPemFile,
  57092. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  57093. {
  57094. EXPECT_DECLS;
  57095. callback_functions client_cbf;
  57096. callback_functions server_cbf;
  57097. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57098. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57099. if (tlsVer == WOLFSSL_TLSV1_3) {
  57100. #ifdef WOLFSSL_TLS13
  57101. server_cbf.method = wolfTLSv1_3_server_method;
  57102. client_cbf.method = wolfTLSv1_3_client_method;
  57103. #endif
  57104. }
  57105. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57106. #ifndef WOLFSSL_NO_TLS12
  57107. server_cbf.method = wolfTLSv1_2_server_method;
  57108. client_cbf.method = wolfTLSv1_2_client_method;
  57109. #endif
  57110. }
  57111. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57112. #ifndef NO_OLD_TLS
  57113. server_cbf.method = wolfTLSv1_1_server_method;
  57114. client_cbf.method = wolfTLSv1_1_client_method;
  57115. #endif
  57116. }
  57117. else if (tlsVer == WOLFSSL_TLSV1) {
  57118. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57119. server_cbf.method = wolfTLSv1_server_method;
  57120. client_cbf.method = wolfTLSv1_client_method;
  57121. #endif
  57122. }
  57123. else if (tlsVer == WOLFSSL_SSLV3) {
  57124. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57125. defined(WOLFSSL_STATIC_RSA)
  57126. server_cbf.method = wolfSSLv3_server_method;
  57127. client_cbf.method = wolfSSLv3_client_method;
  57128. #endif
  57129. }
  57130. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57131. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57132. server_cbf.method = wolfDTLSv1_2_server_method;
  57133. client_cbf.method = wolfDTLSv1_2_client_method;
  57134. #endif
  57135. }
  57136. else if (tlsVer == WOLFSSL_DTLSV1) {
  57137. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57138. server_cbf.method = wolfDTLSv1_server_method;
  57139. client_cbf.method = wolfDTLSv1_client_method;
  57140. #endif
  57141. }
  57142. if (server_cbf.method == NULL) {
  57143. /* not enabled */
  57144. return TEST_SUCCESS;
  57145. }
  57146. /* Setup the keys for the TLS test */
  57147. client_cbf.certPemFile = cliCertPemFile;
  57148. client_cbf.keyPemFile = cliPubKeyPemFile;
  57149. client_cbf.caPemFile = cliCaPemFile;
  57150. server_cbf.certPemFile = svrCertPemFile;
  57151. server_cbf.keyPemFile = svrPubKeyPemFile;
  57152. server_cbf.caPemFile = svrCaPemFile;
  57153. /* Setup a crypto callback with pointer to private key file for testing */
  57154. client_cbf.devId = 1;
  57155. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  57156. (void*)cliPrivKeyPemFile);
  57157. server_cbf.devId = 2;
  57158. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  57159. (void*)svrPrivKeyPemFile);
  57160. /* Perform TLS server and client test */
  57161. /* First test is at WOLFSSL_CTX level */
  57162. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57163. /* Check for success */
  57164. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57165. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57166. if (EXPECT_SUCCESS()) {
  57167. /* Second test is a WOLFSSL object level */
  57168. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57169. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57170. }
  57171. /* Check for success */
  57172. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57173. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57174. /* Un register the devId's */
  57175. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  57176. client_cbf.devId = INVALID_DEVID;
  57177. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  57178. server_cbf.devId = INVALID_DEVID;
  57179. return EXPECT_RESULT();
  57180. }
  57181. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  57182. static int test_wc_CryptoCb(void)
  57183. {
  57184. EXPECT_DECLS;
  57185. #ifdef WOLF_CRYPTO_CB
  57186. /* TODO: Add crypto callback API tests */
  57187. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57188. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  57189. int tlsVer;
  57190. #endif
  57191. #ifndef NO_RSA
  57192. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57193. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57194. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  57195. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  57196. TEST_SUCCESS);
  57197. }
  57198. #endif
  57199. #ifdef HAVE_ECC
  57200. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57201. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57202. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  57203. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  57204. TEST_SUCCESS);
  57205. }
  57206. #endif
  57207. #ifdef HAVE_ED25519
  57208. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  57209. if (tlsVer == WOLFSSL_DTLSV1) continue;
  57210. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57211. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  57212. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  57213. TEST_SUCCESS);
  57214. }
  57215. #endif
  57216. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  57217. #endif /* WOLF_CRYPTO_CB */
  57218. return EXPECT_RESULT();
  57219. }
  57220. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57221. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57222. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  57223. const char* cliCaPemFile, const char* cliCertPemFile,
  57224. const char* cliPrivKeyPemFile,
  57225. const char* svrCaPemFile, const char* svrCertPemFile,
  57226. const char* svrPrivKeyPemFile,
  57227. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  57228. {
  57229. EXPECT_DECLS;
  57230. callback_functions client_cbf;
  57231. callback_functions server_cbf;
  57232. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57233. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57234. if (tlsVer == WOLFSSL_TLSV1_3) {
  57235. #ifdef WOLFSSL_TLS13
  57236. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  57237. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  57238. #endif
  57239. }
  57240. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57241. #ifndef WOLFSSL_NO_TLS12
  57242. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  57243. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  57244. #endif
  57245. }
  57246. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57247. #ifndef NO_OLD_TLS
  57248. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  57249. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  57250. #endif
  57251. }
  57252. else if (tlsVer == WOLFSSL_TLSV1) {
  57253. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57254. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  57255. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  57256. #endif
  57257. }
  57258. else if (tlsVer == WOLFSSL_SSLV3) {
  57259. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57260. defined(WOLFSSL_STATIC_RSA)
  57261. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  57262. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  57263. #endif
  57264. }
  57265. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57266. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57267. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  57268. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  57269. #endif
  57270. }
  57271. else if (tlsVer == WOLFSSL_DTLSV1) {
  57272. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57273. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  57274. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  57275. #endif
  57276. }
  57277. if (server_cbf.method_ex == NULL) {
  57278. /* not enabled */
  57279. return TEST_SUCCESS;
  57280. }
  57281. /* Setup the keys for the TLS test */
  57282. client_cbf.certPemFile = cliCertPemFile;
  57283. client_cbf.keyPemFile = cliPrivKeyPemFile;
  57284. client_cbf.caPemFile = cliCaPemFile;
  57285. server_cbf.certPemFile = svrCertPemFile;
  57286. server_cbf.keyPemFile = svrPrivKeyPemFile;
  57287. server_cbf.caPemFile = svrCaPemFile;
  57288. client_cbf.mem = cliMem;
  57289. client_cbf.memSz = cliMemSz;
  57290. server_cbf.mem = svrMem;
  57291. server_cbf.memSz = svrMemSz;
  57292. client_cbf.devId = INVALID_DEVID;
  57293. server_cbf.devId = INVALID_DEVID;
  57294. /* Perform TLS server and client test */
  57295. /* First test is at WOLFSSL_CTX level */
  57296. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57297. /* Check for success */
  57298. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57299. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57300. if (EXPECT_SUCCESS()) {
  57301. /* Second test is a WOLFSSL object level */
  57302. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57303. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57304. }
  57305. /* Check for success */
  57306. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57307. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57308. return EXPECT_RESULT();
  57309. }
  57310. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  57311. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  57312. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  57313. {
  57314. EXPECT_DECLS;
  57315. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  57316. WOLFSSL_MEM_STATS mem_stats;
  57317. WOLFSSL_MEM_CONN_STATS ssl_stats;
  57318. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  57319. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  57320. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57321. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  57322. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57323. #endif
  57324. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  57325. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  57326. /* this should fail because kMaxCtxClients == 2 */
  57327. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  57328. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  57329. #ifdef DEBUG_WOLFSSL
  57330. wolfSSL_PrintStatsConn(&ssl_stats);
  57331. #endif
  57332. (void)ssl_stats;
  57333. }
  57334. /* display collected statistics */
  57335. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  57336. #ifdef DEBUG_WOLFSSL
  57337. wolfSSL_PrintStats(&mem_stats);
  57338. #endif
  57339. (void)mem_stats;
  57340. }
  57341. wolfSSL_free(ssl1);
  57342. wolfSSL_free(ssl2);
  57343. return EXPECT_RESULT();
  57344. }
  57345. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  57346. static int test_wolfSSL_CTX_StaticMemory(void)
  57347. {
  57348. EXPECT_DECLS;
  57349. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  57350. wolfSSL_method_func method_func;
  57351. WOLFSSL_CTX* ctx;
  57352. const int kMaxCtxClients = 2;
  57353. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57354. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  57355. int tlsVer;
  57356. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  57357. #endif
  57358. #endif
  57359. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  57360. #ifndef NO_WOLFSSL_SERVER
  57361. #ifndef WOLFSSL_NO_TLS12
  57362. method_func = wolfTLSv1_2_server_method_ex;
  57363. #else
  57364. method_func = wolfTLSv1_3_server_method_ex;
  57365. #endif
  57366. #else
  57367. #ifndef WOLFSSL_NO_TLS12
  57368. method_func = wolfTLSv1_2_client_method_ex;
  57369. #else
  57370. method_func = wolfTLSv1_3_client_method_ex;
  57371. #endif
  57372. #endif
  57373. /* Test creating CTX directly from static memory pool */
  57374. ctx = NULL;
  57375. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  57376. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  57377. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  57378. wolfSSL_CTX_free(ctx);
  57379. ctx = NULL;
  57380. /* Test for heap allocated CTX, then assigning static pool to it */
  57381. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  57382. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  57383. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  57384. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  57385. wolfSSL_CTX_free(ctx);
  57386. /* TLS Level Tests using static memory */
  57387. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57388. #ifndef NO_RSA
  57389. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57390. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  57391. svrCertFile, cliCertFile, cliKeyFile,
  57392. cliCertFile, svrCertFile, svrKeyFile,
  57393. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  57394. TEST_SUCCESS);
  57395. }
  57396. #endif
  57397. #ifdef HAVE_ECC
  57398. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57399. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  57400. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  57401. cliEccCertFile, eccCertFile, eccKeyFile,
  57402. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  57403. TEST_SUCCESS);
  57404. }
  57405. #endif
  57406. #ifdef HAVE_ED25519
  57407. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  57408. if (tlsVer == WOLFSSL_DTLSV1) continue;
  57409. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  57410. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  57411. cliEdCertFile, edCertFile, edKeyFile,
  57412. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  57413. TEST_SUCCESS);
  57414. }
  57415. #endif
  57416. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  57417. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  57418. return EXPECT_RESULT();
  57419. }
  57420. static int test_openssl_FIPS_drbg(void)
  57421. {
  57422. EXPECT_DECLS;
  57423. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  57424. DRBG_CTX* dctx = NULL;
  57425. byte data1[32], data2[32], zeroData[32];
  57426. byte testSeed[16];
  57427. size_t dlen = sizeof(data1);
  57428. int i;
  57429. XMEMSET(data1, 0, dlen);
  57430. XMEMSET(data2, 0, dlen);
  57431. XMEMSET(zeroData, 0, sizeof(zeroData));
  57432. for (i = 0; i < (int)sizeof(testSeed); i++) {
  57433. testSeed[i] = (byte)i;
  57434. }
  57435. ExpectNotNull(dctx = FIPS_get_default_drbg());
  57436. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  57437. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  57438. WOLFSSL_SUCCESS);
  57439. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  57440. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  57441. WOLFSSL_SUCCESS);
  57442. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  57443. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  57444. WOLFSSL_SUCCESS);
  57445. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  57446. WOLFSSL_SUCCESS);
  57447. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  57448. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  57449. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  57450. #ifndef HAVE_GLOBAL_RNG
  57451. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  57452. wolfSSL_FIPS_drbg_free(dctx);
  57453. #endif
  57454. #endif
  57455. return EXPECT_RESULT();
  57456. }
  57457. static int test_wolfSSL_FIPS_mode(void)
  57458. {
  57459. EXPECT_DECLS;
  57460. #if defined(OPENSSL_ALL)
  57461. #ifdef HAVE_FIPS
  57462. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  57463. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  57464. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  57465. #else
  57466. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  57467. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  57468. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  57469. #endif
  57470. #endif
  57471. return EXPECT_RESULT();
  57472. }
  57473. #ifdef WOLFSSL_DTLS
  57474. /* Prints out the current window */
  57475. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  57476. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  57477. int i;
  57478. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  57479. word32 b = w[i];
  57480. int j;
  57481. /* Prints out a 32 bit binary number in big endian order */
  57482. for (j = 0; j < 32; j++, b <<= 1) {
  57483. if (b & (((word32)1) << 31))
  57484. fprintf(stderr, "1");
  57485. else
  57486. fprintf(stderr, "0");
  57487. }
  57488. fprintf(stderr, " ");
  57489. }
  57490. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  57491. #else
  57492. (void)h;
  57493. (void)l;
  57494. (void)w;
  57495. #endif
  57496. }
  57497. /* a - cur_hi
  57498. * b - cur_lo
  57499. * c - next_hi
  57500. * d - next_lo
  57501. * e - window
  57502. * f - expected next_hi
  57503. * g - expected next_lo
  57504. * h - expected window[1]
  57505. * i - expected window[0]
  57506. */
  57507. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  57508. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  57509. DUW_TEST_print_window_binary((a), (b), (e)); \
  57510. ExpectIntEQ((c), (f)); \
  57511. ExpectIntEQ((d), (g)); \
  57512. ExpectIntEQ((e)[1], (h)); \
  57513. ExpectIntEQ((e)[0], (i)); \
  57514. } while (0)
  57515. static int test_wolfSSL_DtlsUpdateWindow(void)
  57516. {
  57517. EXPECT_DECLS;
  57518. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  57519. word32 next_lo = 0;
  57520. word16 next_hi = 0;
  57521. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  57522. fprintf(stderr, "\n");
  57523. #endif
  57524. XMEMSET(window, 0, sizeof window);
  57525. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  57526. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  57527. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  57528. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  57529. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  57530. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  57531. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  57532. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  57533. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  57534. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  57535. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  57536. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  57537. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  57538. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  57539. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  57540. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  57541. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  57542. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  57543. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  57544. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  57545. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  57546. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  57547. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  57548. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  57549. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  57550. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  57551. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  57552. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  57553. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  57554. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  57555. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  57556. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  57557. return EXPECT_RESULT();
  57558. }
  57559. #endif /* WOLFSSL_DTLS */
  57560. #ifdef WOLFSSL_DTLS
  57561. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  57562. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  57563. {
  57564. DtlsMsg* cur;
  57565. static byte msg[100];
  57566. static byte msgInit = 0;
  57567. if (!msgInit) {
  57568. int i;
  57569. for (i = 0; i < 100; i++)
  57570. msg[i] = i + 1;
  57571. msgInit = 1;
  57572. }
  57573. /* Sanitize test parameters */
  57574. if (len > sizeof(msg))
  57575. return -1;
  57576. if (f_offset + f_len > sizeof(msg))
  57577. return -1;
  57578. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  57579. if (ssl->dtls_rx_msg_list == NULL)
  57580. return -100;
  57581. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  57582. return -200;
  57583. if (cur->fragBucketListCount != f_count)
  57584. return -300;
  57585. if (cur->ready != ready)
  57586. return -400;
  57587. if (cur->bytesReceived != bytesReceived)
  57588. return -500;
  57589. if (ready) {
  57590. if (cur->fragBucketList != NULL)
  57591. return -600;
  57592. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  57593. return -700;
  57594. }
  57595. else {
  57596. DtlsFragBucket* fb;
  57597. if (cur->fragBucketList == NULL)
  57598. return -800;
  57599. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  57600. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  57601. return -900;
  57602. }
  57603. }
  57604. return 0;
  57605. }
  57606. static int test_wolfSSL_DTLS_fragment_buckets(void)
  57607. {
  57608. EXPECT_DECLS;
  57609. WOLFSSL ssl[1];
  57610. XMEMSET(ssl, 0, sizeof(*ssl));
  57611. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  57612. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57613. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  57614. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  57615. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  57616. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  57617. /* Test all permutations of 3 regions */
  57618. /* 1 2 3 */
  57619. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57620. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  57621. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  57622. /* 1 3 2 */
  57623. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57624. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  57625. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  57626. /* 2 1 3 */
  57627. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  57628. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  57629. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  57630. /* 2 3 1 */
  57631. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  57632. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  57633. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  57634. /* 3 1 2 */
  57635. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  57636. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  57637. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  57638. /* 3 2 1 */
  57639. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  57640. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  57641. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  57642. /* Test overlapping regions */
  57643. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  57644. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  57645. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  57646. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  57647. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  57648. /* Test overlapping multiple regions */
  57649. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57650. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  57651. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  57652. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  57653. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  57654. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  57655. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  57656. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  57657. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  57658. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  57659. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  57660. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  57661. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  57662. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  57663. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  57664. ssl->dtls_rx_msg_list = NULL;
  57665. ssl->dtls_rx_msg_list_sz = 0;
  57666. return EXPECT_RESULT();
  57667. }
  57668. #endif
  57669. #if !defined(NO_FILESYSTEM) && \
  57670. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57671. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57672. static int test_wolfSSL_dtls_stateless2(void)
  57673. {
  57674. EXPECT_DECLS;
  57675. WOLFSSL *ssl_c = NULL;
  57676. WOLFSSL *ssl_c2 = NULL;
  57677. WOLFSSL *ssl_s = NULL;
  57678. struct test_memio_ctx test_ctx;
  57679. WOLFSSL_CTX *ctx_c = NULL;
  57680. WOLFSSL_CTX *ctx_s = NULL;
  57681. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57682. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57683. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57684. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  57685. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57686. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57687. /* send CH */
  57688. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57689. (ssl_c2->error == WANT_READ));
  57690. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57691. (ssl_s->error == WANT_READ));
  57692. ExpectIntNE(test_ctx.c_len, 0);
  57693. /* consume HRR */
  57694. test_ctx.c_len = 0;
  57695. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57696. wolfSSL_free(ssl_c2);
  57697. wolfSSL_free(ssl_c);
  57698. wolfSSL_free(ssl_s);
  57699. wolfSSL_CTX_free(ctx_c);
  57700. wolfSSL_CTX_free(ctx_s);
  57701. return EXPECT_RESULT();
  57702. }
  57703. #ifdef HAVE_MAX_FRAGMENT
  57704. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  57705. {
  57706. EXPECT_DECLS;
  57707. WOLFSSL *ssl_c = NULL;
  57708. WOLFSSL *ssl_c2 = NULL;
  57709. WOLFSSL *ssl_s = NULL;
  57710. struct test_memio_ctx test_ctx;
  57711. WOLFSSL_CTX *ctx_c = NULL;
  57712. WOLFSSL_CTX *ctx_s = NULL;
  57713. word16 max_fragment = 0;
  57714. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57715. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57716. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57717. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  57718. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  57719. WOLFSSL_SUCCESS);
  57720. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57721. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57722. if (ssl_s != NULL) {
  57723. max_fragment = ssl_s->max_fragment;
  57724. }
  57725. /* send CH */
  57726. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57727. (ssl_c2->error == WANT_READ));
  57728. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57729. (ssl_s->error == WANT_READ));
  57730. /* CH without cookie shouldn't change state */
  57731. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  57732. ExpectIntNE(test_ctx.c_len, 0);
  57733. /* consume HRR from buffer */
  57734. test_ctx.c_len = 0;
  57735. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57736. wolfSSL_free(ssl_c2);
  57737. wolfSSL_free(ssl_c);
  57738. wolfSSL_free(ssl_s);
  57739. wolfSSL_CTX_free(ctx_c);
  57740. wolfSSL_CTX_free(ctx_s);
  57741. return EXPECT_RESULT();
  57742. }
  57743. #endif /* HAVE_MAX_FRAGMENT */
  57744. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  57745. #define ROUNDS_WITH_HVR 4
  57746. #define ROUNDS_WITHOUT_HVR 2
  57747. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  57748. static int buf_is_hvr(const byte *data, int len)
  57749. {
  57750. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  57751. return 0;
  57752. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  57753. }
  57754. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  57755. {
  57756. EXPECT_DECLS;
  57757. struct test_memio_ctx test_ctx;
  57758. WOLFSSL_CTX *ctx_c = NULL;
  57759. WOLFSSL_CTX *ctx_s = NULL;
  57760. WOLFSSL *ssl_c = NULL;
  57761. WOLFSSL *ssl_s = NULL;
  57762. WOLFSSL_SESSION *sess = NULL;
  57763. int round_trips;
  57764. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57765. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  57766. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57767. #ifdef HAVE_SESSION_TICKET
  57768. if (useticket) {
  57769. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  57770. }
  57771. #endif
  57772. round_trips = ROUNDS_WITH_HVR;
  57773. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  57774. &round_trips), 0);
  57775. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  57776. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57777. wolfSSL_shutdown(ssl_c);
  57778. wolfSSL_shutdown(ssl_s);
  57779. wolfSSL_free(ssl_c);
  57780. ssl_c = NULL;
  57781. wolfSSL_free(ssl_s);
  57782. ssl_s = NULL;
  57783. test_ctx.c_len = test_ctx.s_len = 0;
  57784. /* make resumption invalid */
  57785. if (bad && (sess != NULL)) {
  57786. if (useticket) {
  57787. #ifdef HAVE_SESSION_TICKET
  57788. if (sess->ticket != NULL) {
  57789. sess->ticket[0] = !sess->ticket[0];
  57790. }
  57791. #endif /* HAVE_SESSION_TICKET */
  57792. }
  57793. else {
  57794. sess->sessionID[0] = !sess->sessionID[0];
  57795. }
  57796. }
  57797. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  57798. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  57799. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  57800. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  57801. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  57802. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  57803. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  57804. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57805. (ssl_c->error == WANT_READ));
  57806. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57807. (ssl_s->error == WANT_READ));
  57808. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  57809. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  57810. if (!useticket) {
  57811. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  57812. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  57813. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  57814. }
  57815. wolfSSL_SESSION_free(sess);
  57816. wolfSSL_free(ssl_c);
  57817. wolfSSL_free(ssl_s);
  57818. wolfSSL_CTX_free(ctx_c);
  57819. wolfSSL_CTX_free(ctx_s);
  57820. return EXPECT_RESULT();
  57821. }
  57822. static int test_wolfSSL_dtls_stateless_resume(void)
  57823. {
  57824. EXPECT_DECLS;
  57825. #ifdef HAVE_SESSION_TICKET
  57826. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  57827. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  57828. #endif /* HAVE_SESION_TICKET */
  57829. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  57830. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  57831. return EXPECT_RESULT();
  57832. }
  57833. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  57834. #if !defined(NO_OLD_TLS)
  57835. static int test_wolfSSL_dtls_stateless_downgrade(void)
  57836. {
  57837. EXPECT_DECLS;
  57838. WOLFSSL_CTX *ctx_c = NULL;
  57839. WOLFSSL_CTX *ctx_c2 = NULL;
  57840. WOLFSSL_CTX *ctx_s = NULL;
  57841. WOLFSSL *ssl_c = NULL;
  57842. WOLFSSL *ssl_c2 = NULL;
  57843. WOLFSSL *ssl_s = NULL;
  57844. struct test_memio_ctx test_ctx;
  57845. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57846. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57847. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  57848. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  57849. WOLFSSL_SUCCESS);
  57850. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  57851. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  57852. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  57853. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  57854. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  57855. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  57856. /* send CH */
  57857. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  57858. (ssl_c2->error == WANT_READ));
  57859. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57860. (ssl_s->error == WANT_READ));
  57861. ExpectIntNE(test_ctx.c_len, 0);
  57862. /* consume HRR */
  57863. test_ctx.c_len = 0;
  57864. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57865. wolfSSL_free(ssl_c2);
  57866. wolfSSL_free(ssl_c);
  57867. wolfSSL_free(ssl_s);
  57868. wolfSSL_CTX_free(ctx_c);
  57869. wolfSSL_CTX_free(ctx_c2);
  57870. wolfSSL_CTX_free(ctx_s);
  57871. return EXPECT_RESULT();
  57872. }
  57873. #endif /* !defined(NO_OLD_TLS) */
  57874. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57875. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  57876. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57877. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57878. static int test_WOLFSSL_dtls_version_alert(void)
  57879. {
  57880. EXPECT_DECLS;
  57881. struct test_memio_ctx test_ctx;
  57882. WOLFSSL_CTX *ctx_c = NULL;
  57883. WOLFSSL_CTX *ctx_s = NULL;
  57884. WOLFSSL *ssl_c = NULL;
  57885. WOLFSSL *ssl_s = NULL;
  57886. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57887. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57888. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  57889. /* client hello */
  57890. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57891. (ssl_c->error == WANT_READ));
  57892. /* hrr */
  57893. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57894. (ssl_s->error == WANT_READ));
  57895. /* client hello 1 */
  57896. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57897. (ssl_c->error == WANT_READ));
  57898. /* server hello */
  57899. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57900. (ssl_s->error == WANT_READ));
  57901. /* should fail */
  57902. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  57903. (ssl_c->error == VERSION_ERROR));
  57904. /* shuould fail */
  57905. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  57906. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  57907. wolfSSL_free(ssl_c);
  57908. wolfSSL_free(ssl_s);
  57909. wolfSSL_CTX_free(ctx_c);
  57910. wolfSSL_CTX_free(ctx_s);
  57911. return EXPECT_RESULT();
  57912. }
  57913. #else
  57914. static int test_WOLFSSL_dtls_version_alert(void)
  57915. {
  57916. return TEST_SKIPPED;
  57917. }
  57918. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  57919. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  57920. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  57921. */
  57922. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  57923. && defined(WOLFSSL_TLS13) && \
  57924. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  57925. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57926. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  57927. {
  57928. struct test_memio_ctx *test_ctx;
  57929. byte buf[2048];
  57930. int idx, sz;
  57931. word32 tmp;
  57932. int ret;
  57933. idx = 5; /* space for record header */
  57934. buf[idx] = session_ticket; /* type */
  57935. idx++;
  57936. tmp = OPAQUE32_LEN +
  57937. OPAQUE32_LEN +
  57938. OPAQUE8_LEN + nonceLength +
  57939. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  57940. c32to24(tmp, buf + idx);
  57941. idx += OPAQUE24_LEN;
  57942. c32toa((word32)12345, buf+idx); /* lifetime */
  57943. idx += OPAQUE32_LEN;
  57944. c32toa((word32)12345, buf+idx); /* add */
  57945. idx += OPAQUE32_LEN;
  57946. buf[idx] = nonceLength; /* nonce length */
  57947. idx++;
  57948. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  57949. idx += nonceLength;
  57950. tmp = 1; /* ticket len */
  57951. c16toa((word16)tmp, buf+idx);
  57952. idx += 2;
  57953. buf[idx] = 0xFF; /* ticket */
  57954. idx++;
  57955. tmp = 0; /* ext len */
  57956. c16toa((word16)tmp, buf+idx);
  57957. idx += 2;
  57958. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  57959. handshake, 0, 0, 0);
  57960. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  57961. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  57962. return !(ret == sz);
  57963. }
  57964. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  57965. {
  57966. int ret = 0;
  57967. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  57968. ret = -1;
  57969. }
  57970. else {
  57971. int i;
  57972. for (i = 0; i < len; i++) {
  57973. if (sess->ticketNonce.data[i] != len) {
  57974. ret = -1;
  57975. break;
  57976. }
  57977. }
  57978. }
  57979. return ret;
  57980. }
  57981. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57982. {
  57983. EXPECT_DECLS;
  57984. char *buf[1024];
  57985. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  57986. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  57987. (ssl_c->error == WANT_READ));
  57988. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  57989. return EXPECT_RESULT();
  57990. }
  57991. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  57992. {
  57993. EXPECT_DECLS;
  57994. WOLFSSL_SESSION *sess = NULL;
  57995. WOLFSSL_SESSION *cached = NULL;
  57996. WOLFSSL_CTX *ctx = ssl_c->ctx;
  57997. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  57998. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  57999. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  58000. NULL, ssl_c->options.side, 1,NULL), 0);
  58001. ExpectNotNull(cached = wolfSSL_SESSION_new());
  58002. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  58003. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  58004. wolfSSL_SESSION_free(cached);
  58005. wolfSSL_SESSION_free(sess);
  58006. return EXPECT_RESULT();
  58007. }
  58008. static int test_ticket_nonce_malloc(void)
  58009. {
  58010. EXPECT_DECLS;
  58011. struct test_memio_ctx test_ctx;
  58012. WOLFSSL_CTX *ctx_c = NULL;
  58013. WOLFSSL_CTX *ctx_s = NULL;
  58014. WOLFSSL *ssl_c = NULL;
  58015. WOLFSSL *ssl_s = NULL;
  58016. byte small;
  58017. byte medium;
  58018. byte big;
  58019. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58020. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58021. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58022. /* will send ticket manually */
  58023. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  58024. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58025. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58026. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  58027. (ssl_s->options.handShakeDone == 0)) {
  58028. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  58029. (ssl_c->error == WANT_READ));
  58030. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  58031. (ssl_s->error == WANT_READ));
  58032. }
  58033. small = TLS13_TICKET_NONCE_STATIC_SZ;
  58034. medium = small + 20 <= 255 ? small + 20 : 255;
  58035. big = medium + 20 <= 255 ? small + 20 : 255;
  58036. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58037. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  58038. ssl_c->session->ticketNonce.dataStatic);
  58039. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58040. TEST_SUCCESS);
  58041. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  58042. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58043. TEST_SUCCESS);
  58044. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58045. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58046. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58047. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  58048. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58049. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58050. wolfSSL_free(ssl_c);
  58051. wolfSSL_free(ssl_s);
  58052. wolfSSL_CTX_free(ctx_c);
  58053. wolfSSL_CTX_free(ctx_s);
  58054. return EXPECT_RESULT();
  58055. }
  58056. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  58057. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  58058. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  58059. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58060. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  58061. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58062. static int test_ticket_ret_create(void)
  58063. {
  58064. EXPECT_DECLS;
  58065. WOLFSSL_CTX *ctx_c = NULL;
  58066. WOLFSSL_CTX *ctx_s = NULL;
  58067. WOLFSSL *ssl_c = NULL;
  58068. WOLFSSL *ssl_s = NULL;
  58069. byte ticket[SESSION_TICKET_LEN];
  58070. struct test_memio_ctx test_ctx;
  58071. WOLFSSL_SESSION *sess = NULL;
  58072. word16 ticketLen = 0;
  58073. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58074. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58075. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58076. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58077. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58078. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  58079. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58080. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58081. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  58082. if (sess != NULL) {
  58083. ticketLen = sess->ticketLen;
  58084. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  58085. }
  58086. wolfSSL_free(ssl_c);
  58087. ssl_c = NULL;
  58088. wolfSSL_free(ssl_s);
  58089. ssl_s = NULL;
  58090. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58091. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58092. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58093. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58094. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58095. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58096. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58097. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58098. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  58099. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  58100. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  58101. wolfSSL_SESSION_free(sess);
  58102. wolfSSL_free(ssl_c);
  58103. wolfSSL_free(ssl_s);
  58104. wolfSSL_CTX_free(ctx_c);
  58105. wolfSSL_CTX_free(ctx_s);
  58106. return EXPECT_RESULT();
  58107. }
  58108. #else
  58109. static int test_ticket_ret_create(void)
  58110. {
  58111. return TEST_SKIPPED;
  58112. }
  58113. #endif
  58114. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  58115. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  58116. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58117. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58118. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58119. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  58120. {
  58121. int ret;
  58122. WOLFSSL_SESSION* session = NULL;
  58123. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58124. if (!wolfSSL_is_server(ssl)) {
  58125. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58126. AssertNotNull(session);
  58127. }
  58128. do {
  58129. ret = wolfSSL_shutdown(ssl);
  58130. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58131. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58132. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58133. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58134. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58135. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58136. #endif
  58137. if (!wolfSSL_is_server(ssl)) {
  58138. /* client */
  58139. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58140. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58141. wolfSSL_set_session(ssl, session);
  58142. wolfSSL_SESSION_free(session);
  58143. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58144. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58145. }
  58146. else {
  58147. /* server */
  58148. /* Different ciphersuite so that the ticket will be invalidated based on
  58149. * the ciphersuite */
  58150. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  58151. WOLFSSL_SUCCESS);
  58152. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  58153. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58154. }
  58155. }
  58156. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  58157. {
  58158. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  58159. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58160. WOLFSSL_SUCCESS);
  58161. }
  58162. static int test_ticket_and_psk_mixing(void)
  58163. {
  58164. EXPECT_DECLS;
  58165. /* Test mixing tickets and regular PSK */
  58166. callback_functions client_cbs, server_cbs;
  58167. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58168. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58169. client_cbs.method = wolfTLSv1_3_client_method;
  58170. server_cbs.method = wolfTLSv1_3_server_method;
  58171. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  58172. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58173. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58174. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58175. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58176. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58177. return EXPECT_RESULT();
  58178. }
  58179. #else
  58180. static int test_ticket_and_psk_mixing(void)
  58181. {
  58182. return TEST_SKIPPED;
  58183. }
  58184. #endif
  58185. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  58186. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  58187. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58188. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58189. static int test_prioritize_psk_cb_called = FALSE;
  58190. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  58191. const char* identity, unsigned char* key, unsigned int key_max_len,
  58192. const char** ciphersuite)
  58193. {
  58194. test_prioritize_psk_cb_called = TRUE;
  58195. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  58196. }
  58197. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  58198. {
  58199. int ret;
  58200. WOLFSSL_SESSION* session = NULL;
  58201. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58202. if (!wolfSSL_is_server(ssl)) {
  58203. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58204. AssertNotNull(session);
  58205. }
  58206. do {
  58207. ret = wolfSSL_shutdown(ssl);
  58208. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58209. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58210. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58211. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  58212. * important. */
  58213. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58214. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58215. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58216. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58217. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58218. #endif
  58219. if (!wolfSSL_is_server(ssl)) {
  58220. /* client */
  58221. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58222. wolfSSL_set_session(ssl, session);
  58223. wolfSSL_SESSION_free(session);
  58224. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58225. }
  58226. else {
  58227. /* server */
  58228. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  58229. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58230. #ifdef WOLFSSL_PRIORITIZE_PSK
  58231. /* The ticket should be first tried with all ciphersuites and chosen */
  58232. AssertFalse(test_prioritize_psk_cb_called);
  58233. #else
  58234. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  58235. * callback that sets this var. */
  58236. AssertTrue(test_prioritize_psk_cb_called);
  58237. #endif
  58238. }
  58239. }
  58240. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  58241. {
  58242. if (!wolfSSL_is_server(ssl))
  58243. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58244. WOLFSSL_SUCCESS);
  58245. else
  58246. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58247. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58248. }
  58249. static int test_prioritize_psk(void)
  58250. {
  58251. EXPECT_DECLS;
  58252. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  58253. * of the PSK's will be followed instead of the ciphersuite. */
  58254. callback_functions client_cbs, server_cbs;
  58255. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58256. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58257. client_cbs.method = wolfTLSv1_3_client_method;
  58258. server_cbs.method = wolfTLSv1_3_server_method;
  58259. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  58260. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  58261. client_cbs.on_result = test_prioritize_psk_on_result;
  58262. server_cbs.on_result = test_prioritize_psk_on_result;
  58263. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58264. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58265. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58266. return EXPECT_RESULT();
  58267. }
  58268. #else
  58269. static int test_prioritize_psk(void)
  58270. {
  58271. return TEST_SKIPPED;
  58272. }
  58273. #endif
  58274. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  58275. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58276. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58277. !defined(WOLFSSL_NO_TLS12)
  58278. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  58279. {
  58280. EXPECT_DECLS;
  58281. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  58282. /* Set TLS 1.3 specific suite */
  58283. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  58284. return EXPECT_RESULT();
  58285. }
  58286. static int test_wolfSSL_CTX_set_ciphersuites(void)
  58287. {
  58288. EXPECT_DECLS;
  58289. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  58290. * do a 1.2 connection. */
  58291. test_ssl_cbf client_cbs, server_cbs;
  58292. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58293. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58294. client_cbs.method = wolfTLSv1_2_client_method;
  58295. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  58296. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  58297. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58298. &server_cbs, NULL), TEST_SUCCESS);
  58299. return EXPECT_RESULT();
  58300. }
  58301. #else
  58302. static int test_wolfSSL_CTX_set_ciphersuites(void)
  58303. {
  58304. return TEST_SKIPPED;
  58305. }
  58306. #endif
  58307. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  58308. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58309. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  58310. {
  58311. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  58312. return TEST_SUCCESS;
  58313. }
  58314. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  58315. {
  58316. EXPECT_DECLS;
  58317. WOLFSSL_ALERT_HISTORY h;
  58318. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  58319. ExpectIntEQ(h.last_rx.level, alert_fatal);
  58320. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  58321. return EXPECT_RESULT();
  58322. }
  58323. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  58324. {
  58325. EXPECT_DECLS;
  58326. test_ssl_cbf client_cbs, server_cbs;
  58327. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58328. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58329. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  58330. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  58331. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  58332. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  58333. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  58334. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58335. &server_cbs, NULL), TEST_FAIL);
  58336. return EXPECT_RESULT();
  58337. }
  58338. #else
  58339. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  58340. {
  58341. return TEST_SKIPPED;
  58342. }
  58343. #endif
  58344. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  58345. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58346. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58347. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  58348. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  58349. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  58350. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  58351. static int test_TLS_13_ticket_different_ciphers_run = 0;
  58352. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  58353. {
  58354. EXPECT_DECLS;
  58355. switch (test_TLS_13_ticket_different_ciphers_run) {
  58356. case 0:
  58357. /* First run */
  58358. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58359. WOLFSSL_SUCCESS);
  58360. if (wolfSSL_is_server(ssl)) {
  58361. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  58362. wolfSSL_get_SSL_CTX(ssl));
  58363. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  58364. test_TLS_13_ticket_different_ciphers_ctx));
  58365. }
  58366. break;
  58367. case 1:
  58368. /* Second run */
  58369. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58370. "TLS13-AES128-GCM-SHA256"),
  58371. WOLFSSL_SUCCESS);
  58372. if (!wolfSSL_is_server(ssl)) {
  58373. ExpectIntEQ(wolfSSL_set_session(ssl,
  58374. test_TLS_13_ticket_different_ciphers_session),
  58375. WOLFSSL_SUCCESS);
  58376. }
  58377. break;
  58378. default:
  58379. /* Bad state? */
  58380. Fail(("Should not enter here"), ("Should not enter here"));
  58381. }
  58382. return EXPECT_RESULT();
  58383. }
  58384. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  58385. {
  58386. EXPECT_DECLS;
  58387. switch (test_TLS_13_ticket_different_ciphers_run) {
  58388. case 0:
  58389. /* First run */
  58390. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  58391. wolfSSL_get1_session(ssl));
  58392. break;
  58393. case 1:
  58394. /* Second run */
  58395. ExpectTrue(wolfSSL_session_reused(ssl));
  58396. break;
  58397. default:
  58398. /* Bad state? */
  58399. Fail(("Should not enter here"), ("Should not enter here"));
  58400. }
  58401. return EXPECT_RESULT();
  58402. }
  58403. static int test_TLS_13_ticket_different_ciphers(void)
  58404. {
  58405. EXPECT_DECLS;
  58406. /* Check that we handle the connection when the ticket doesn't match
  58407. * the first ciphersuite. */
  58408. test_ssl_cbf client_cbs, server_cbs;
  58409. struct test_params {
  58410. method_provider client_meth;
  58411. method_provider server_meth;
  58412. int doUdp;
  58413. } params[] = {
  58414. #ifdef WOLFSSL_DTLS13
  58415. /* Test that the stateless code handles sessions correctly */
  58416. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  58417. #endif
  58418. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  58419. };
  58420. size_t i;
  58421. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  58422. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58423. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58424. test_TLS_13_ticket_different_ciphers_run = 0;
  58425. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  58426. client_cbs.method = params[i].client_meth;
  58427. server_cbs.method = params[i].server_meth;
  58428. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  58429. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  58430. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  58431. server_cbs.ticNoInit = 1;
  58432. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58433. &server_cbs, NULL), TEST_SUCCESS);
  58434. test_TLS_13_ticket_different_ciphers_run++;
  58435. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  58436. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58437. &server_cbs, NULL), TEST_SUCCESS);
  58438. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  58439. test_TLS_13_ticket_different_ciphers_session = NULL;
  58440. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  58441. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  58442. }
  58443. return EXPECT_RESULT();
  58444. }
  58445. #else
  58446. static int test_TLS_13_ticket_different_ciphers(void)
  58447. {
  58448. return TEST_SKIPPED;
  58449. }
  58450. #endif
  58451. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  58452. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58453. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  58454. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  58455. byte test_extra_alerts_wrong_cs_sh[] = {
  58456. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  58457. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  58458. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  58459. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  58460. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  58461. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  58462. 0xbb, 0x07, 0x54, 0x1c,
  58463. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  58464. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  58465. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  58466. };
  58467. static int test_extra_alerts_wrong_cs(void)
  58468. {
  58469. EXPECT_DECLS;
  58470. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  58471. struct test_memio_ctx test_ctx;
  58472. WOLFSSL_CTX *ctx_c = NULL;
  58473. WOLFSSL_ALERT_HISTORY h;
  58474. WOLFSSL *ssl_c = NULL;
  58475. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58476. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  58477. wolfTLSv1_2_client_method, NULL), 0);
  58478. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  58479. WOLFSSL_SUCCESS);
  58480. /* CH */
  58481. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58482. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58483. WOLFSSL_ERROR_WANT_READ);
  58484. /* consume CH */
  58485. test_ctx.s_len = 0;
  58486. /* inject SH */
  58487. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  58488. sizeof(test_extra_alerts_wrong_cs_sh));
  58489. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  58490. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58491. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58492. WOLFSSL_ERROR_WANT_READ);
  58493. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58494. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  58495. ExpectIntEQ(h.last_tx.level, alert_fatal);
  58496. wolfSSL_free(ssl_c);
  58497. wolfSSL_CTX_free(ctx_c);
  58498. #endif
  58499. return EXPECT_RESULT();
  58500. }
  58501. #else
  58502. static int test_extra_alerts_wrong_cs(void)
  58503. {
  58504. return TEST_SKIPPED;
  58505. }
  58506. #endif
  58507. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  58508. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  58509. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  58510. {
  58511. tail_len -= msg_length;
  58512. XMEMMOVE(msg, msg + msg_length, tail_len);
  58513. *len = *len - msg_length;
  58514. }
  58515. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  58516. byte *found)
  58517. {
  58518. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  58519. const unsigned int _RECORD_HEADER_SZ = 5;
  58520. const int _change_cipher_hs = 55;
  58521. const int _change_cipher = 20;
  58522. const int _handshake = 22;
  58523. unsigned int tail_len;
  58524. byte *idx, *curr;
  58525. word8 currType;
  58526. word16 rLength;
  58527. word32 hLength;
  58528. idx = buf;
  58529. tail_len = *len;
  58530. *found = 0;
  58531. while (tail_len > _RECORD_HEADER_SZ) {
  58532. curr = idx;
  58533. currType = *idx;
  58534. ato16(idx + 3, &rLength);
  58535. idx += _RECORD_HEADER_SZ;
  58536. tail_len -= _RECORD_HEADER_SZ;
  58537. if (tail_len < rLength)
  58538. return -1;
  58539. if (type == _change_cipher_hs && currType == _change_cipher) {
  58540. if (rLength != 1)
  58541. return -1;
  58542. /* match */
  58543. test_remove_msg(curr, *len - (int)(curr - buf),
  58544. len, _RECORD_HEADER_SZ + 1);
  58545. *found = 1;
  58546. return 0;
  58547. }
  58548. if (currType != _handshake) {
  58549. idx += rLength;
  58550. tail_len -= rLength;
  58551. continue;
  58552. }
  58553. if (rLength < _HANDSHAKE_HEADER_SZ)
  58554. return -1;
  58555. currType = *idx;
  58556. ato24(idx+1, &hLength);
  58557. hLength += _HANDSHAKE_HEADER_SZ;
  58558. if (tail_len < hLength)
  58559. return -1;
  58560. if (currType != type) {
  58561. idx += hLength;
  58562. tail_len -= hLength;
  58563. continue;
  58564. }
  58565. /* match */
  58566. test_remove_msg(curr, *len - (int)(curr - buf), len,
  58567. hLength + _RECORD_HEADER_SZ);
  58568. *found = 1;
  58569. return 0;
  58570. }
  58571. /* not found */
  58572. return 0;
  58573. }
  58574. static int test_remove_hs_message(byte hs_message_type,
  58575. int extra_round, byte alert_type)
  58576. {
  58577. EXPECT_DECLS;
  58578. WOLFSSL_CTX *ctx_c = NULL;
  58579. WOLFSSL_CTX *ctx_s = NULL;
  58580. WOLFSSL *ssl_c = NULL;
  58581. WOLFSSL *ssl_s = NULL;
  58582. struct test_memio_ctx test_ctx;
  58583. WOLFSSL_ALERT_HISTORY h;
  58584. byte found = 0;
  58585. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58586. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58587. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58588. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58589. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58590. WOLFSSL_ERROR_WANT_READ);
  58591. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58592. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58593. WOLFSSL_ERROR_WANT_READ);
  58594. if (extra_round) {
  58595. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58596. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58597. WOLFSSL_ERROR_WANT_READ);
  58598. /* this will complete handshake from server side */
  58599. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58600. }
  58601. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  58602. &test_ctx.c_len, hs_message_type, &found), 0);
  58603. if (!found) {
  58604. wolfSSL_free(ssl_c);
  58605. wolfSSL_CTX_free(ctx_c);
  58606. wolfSSL_free(ssl_s);
  58607. wolfSSL_CTX_free(ctx_s);
  58608. return TEST_SKIPPED;
  58609. }
  58610. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58611. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58612. WOLFSSL_ERROR_WANT_READ);
  58613. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58614. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  58615. ExpectIntEQ(h.last_tx.level, alert_fatal);
  58616. wolfSSL_free(ssl_c);
  58617. wolfSSL_CTX_free(ctx_c);
  58618. wolfSSL_free(ssl_s);
  58619. wolfSSL_CTX_free(ctx_s);
  58620. return EXPECT_RESULT();
  58621. }
  58622. static int test_extra_alerts_skip_hs(void)
  58623. {
  58624. EXPECT_DECLS;
  58625. const byte _server_key_exchange = 12;
  58626. const byte _server_hello = 2;
  58627. const byte _certificate = 11;
  58628. /* server_hello */
  58629. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  58630. unexpected_message), TEST_FAIL);
  58631. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  58632. 0xff), TEST_FAIL);
  58633. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  58634. unexpected_message), TEST_FAIL);
  58635. return EXPECT_RESULT();
  58636. }
  58637. #else
  58638. static int test_extra_alerts_skip_hs(void)
  58639. {
  58640. return TEST_SKIPPED;
  58641. }
  58642. #endif
  58643. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58644. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  58645. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  58646. unsigned char* key, unsigned int key_max_len)
  58647. {
  58648. (void)ssl;
  58649. (void)id;
  58650. (void)key_max_len;
  58651. /* zero means error */
  58652. key[0] = 0x10;
  58653. return 1;
  58654. }
  58655. static int test_extra_alerts_bad_psk(void)
  58656. {
  58657. EXPECT_DECLS;
  58658. WOLFSSL_CTX *ctx_c = NULL;
  58659. WOLFSSL_CTX *ctx_s = NULL;
  58660. WOLFSSL *ssl_c = NULL;
  58661. WOLFSSL *ssl_s = NULL;
  58662. struct test_memio_ctx test_ctx;
  58663. WOLFSSL_ALERT_HISTORY h;
  58664. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58665. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58666. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58667. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  58668. WOLFSSL_SUCCESS);
  58669. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  58670. WOLFSSL_SUCCESS);
  58671. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  58672. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58673. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58674. WOLFSSL_ERROR_WANT_READ);
  58675. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58676. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58677. WOLFSSL_ERROR_WANT_READ);
  58678. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58679. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58680. WOLFSSL_ERROR_WANT_READ);
  58681. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58682. ExpectIntEQ(h.last_tx.code, handshake_failure);
  58683. ExpectIntEQ(h.last_tx.level, alert_fatal);
  58684. wolfSSL_free(ssl_c);
  58685. wolfSSL_CTX_free(ctx_c);
  58686. wolfSSL_free(ssl_s);
  58687. wolfSSL_CTX_free(ctx_s);
  58688. return EXPECT_RESULT();
  58689. }
  58690. #else
  58691. static int test_extra_alerts_bad_psk(void)
  58692. {
  58693. return TEST_SKIPPED;
  58694. }
  58695. #endif
  58696. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58697. && !defined(NO_PSK)
  58698. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  58699. const char* hint, char* identity, unsigned int id_max_len,
  58700. unsigned char* key, unsigned int key_max_len)
  58701. {
  58702. (void)ssl;
  58703. (void)hint;
  58704. (void)key_max_len;
  58705. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  58706. XSTRNCPY(identity, "Client_identity", id_max_len);
  58707. key[0] = 0x20;
  58708. return 1;
  58709. }
  58710. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  58711. const char* id, unsigned char* key, unsigned int key_max_len)
  58712. {
  58713. (void)ssl;
  58714. (void)id;
  58715. (void)key_max_len;
  58716. /* zero means error */
  58717. key[0] = 0x10;
  58718. return 1;
  58719. }
  58720. #endif
  58721. static int test_tls13_bad_psk_binder(void)
  58722. {
  58723. EXPECT_DECLS;
  58724. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  58725. && !defined(NO_PSK)
  58726. WOLFSSL_CTX *ctx_c = NULL;
  58727. WOLFSSL_CTX *ctx_s = NULL;
  58728. WOLFSSL *ssl_c = NULL;
  58729. WOLFSSL *ssl_s = NULL;
  58730. struct test_memio_ctx test_ctx;
  58731. WOLFSSL_ALERT_HISTORY h;
  58732. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58733. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58734. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58735. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  58736. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  58737. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58738. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58739. WOLFSSL_ERROR_WANT_READ);
  58740. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  58741. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58742. BAD_BINDER);
  58743. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  58744. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58745. FATAL_ERROR);
  58746. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  58747. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  58748. ExpectIntEQ(h.last_rx.level, alert_fatal);
  58749. wolfSSL_free(ssl_c);
  58750. wolfSSL_CTX_free(ctx_c);
  58751. wolfSSL_free(ssl_s);
  58752. wolfSSL_CTX_free(ctx_s);
  58753. #endif
  58754. return EXPECT_RESULT();
  58755. }
  58756. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  58757. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58758. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  58759. int sz, void *ctx)
  58760. {
  58761. static int sentServerHello = FALSE;
  58762. if (!sentServerHello) {
  58763. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  58764. size_t i;
  58765. if (sz < (int)sizeof(renegExt))
  58766. return WOLFSSL_CBIO_ERR_GENERAL;
  58767. /* Remove SCR from ServerHello */
  58768. for (i = 0; i < sz - sizeof(renegExt); i++) {
  58769. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  58770. /* Found the extension. Change it to something unrecognized. */
  58771. buf[i+1] = 0x11;
  58772. break;
  58773. }
  58774. }
  58775. sentServerHello = TRUE;
  58776. }
  58777. return EmbedSend(ssl, buf, sz, ctx);
  58778. }
  58779. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  58780. {
  58781. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  58782. }
  58783. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  58784. {
  58785. WOLFSSL_ALERT_HISTORY h;
  58786. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  58787. AssertIntEQ(h.last_rx.code, handshake_failure);
  58788. AssertIntEQ(h.last_rx.level, alert_fatal);
  58789. }
  58790. static int test_harden_no_secure_renegotiation(void)
  58791. {
  58792. EXPECT_DECLS;
  58793. callback_functions client_cbs, server_cbs;
  58794. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58795. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58796. client_cbs.method = wolfTLSv1_2_client_method;
  58797. server_cbs.method = wolfTLSv1_2_server_method;
  58798. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  58799. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  58800. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58801. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  58802. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  58803. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  58804. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  58805. server_cbs.last_err == FATAL_ERROR);
  58806. return EXPECT_RESULT();
  58807. }
  58808. #else
  58809. static int test_harden_no_secure_renegotiation(void)
  58810. {
  58811. return TEST_SKIPPED;
  58812. }
  58813. #endif
  58814. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58815. static int test_override_alt_cert_chain_cert_cb(int preverify,
  58816. WOLFSSL_X509_STORE_CTX* store)
  58817. {
  58818. fprintf(stderr, "preverify: %d\n", preverify);
  58819. fprintf(stderr, "store->error: %d\n", store->error);
  58820. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  58821. if (store->error == OCSP_INVALID_STATUS) {
  58822. fprintf(stderr, "Overriding OCSP error\n");
  58823. return 1;
  58824. }
  58825. #ifndef WOLFSSL_ALT_CERT_CHAINS
  58826. else if ((store->error == ASN_NO_SIGNER_E ||
  58827. store->error == ASN_SELF_SIGNED_E
  58828. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  58829. defined(HAVE_WEBSERVER)
  58830. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  58831. #endif
  58832. ) && store->error_depth == store->totalCerts - 1) {
  58833. fprintf(stderr, "Overriding no signer error only for root cert\n");
  58834. return 1;
  58835. }
  58836. #endif
  58837. else
  58838. return preverify;
  58839. }
  58840. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  58841. int urlSz, unsigned char* request, int requestSz,
  58842. unsigned char** response)
  58843. {
  58844. (void)ioCtx;
  58845. (void)url;
  58846. (void)urlSz;
  58847. (void)request;
  58848. (void)requestSz;
  58849. (void)response;
  58850. return -1;
  58851. }
  58852. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  58853. {
  58854. EXPECT_DECLS;
  58855. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  58856. test_override_alt_cert_chain_cert_cb);
  58857. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  58858. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  58859. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  58860. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  58861. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  58862. WOLFSSL_SUCCESS);
  58863. return EXPECT_RESULT();
  58864. }
  58865. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  58866. {
  58867. EXPECT_DECLS;
  58868. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  58869. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  58870. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  58871. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  58872. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  58873. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  58874. WOLFSSL_SUCCESS);
  58875. return EXPECT_RESULT();
  58876. }
  58877. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  58878. {
  58879. EXPECT_DECLS;
  58880. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  58881. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  58882. return EXPECT_RESULT();
  58883. }
  58884. static int test_override_alt_cert_chain(void)
  58885. {
  58886. EXPECT_DECLS;
  58887. size_t i;
  58888. struct test_params {
  58889. ctx_cb client_ctx_cb;
  58890. ctx_cb server_ctx_cb;
  58891. int result;
  58892. } params[] = {
  58893. {test_override_alt_cert_chain_client_ctx_ready,
  58894. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  58895. {test_override_alt_cert_chain_client_ctx_ready2,
  58896. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  58897. };
  58898. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  58899. test_ssl_cbf client_cbs, server_cbs;
  58900. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58901. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58902. fprintf(stderr, "test config: %d\n", (int)i);
  58903. client_cbs.ctx_ready = params[i].client_ctx_cb;
  58904. server_cbs.ctx_ready = params[i].server_ctx_cb;
  58905. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58906. &server_cbs, NULL), params[i].result);
  58907. ExpectIntEQ(client_cbs.return_code, params[i].result);
  58908. ExpectIntEQ(server_cbs.return_code, params[i].result);
  58909. }
  58910. return EXPECT_RESULT();
  58911. }
  58912. #else
  58913. static int test_override_alt_cert_chain(void)
  58914. {
  58915. return TEST_SKIPPED;
  58916. }
  58917. #endif
  58918. #if defined(HAVE_RPK)
  58919. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  58920. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  58921. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  58922. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  58923. {
  58924. int ret = WOLFSSL_SUCCESS;
  58925. (void)mode;
  58926. (void)strctx;
  58927. WOLFSSL_ENTER("MyRpkVerifyCb");
  58928. return ret;
  58929. }
  58930. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  58931. static WC_INLINE int test_rpk_memio_setup(
  58932. struct test_memio_ctx *ctx,
  58933. WOLFSSL_CTX **ctx_c,
  58934. WOLFSSL_CTX **ctx_s,
  58935. WOLFSSL **ssl_c,
  58936. WOLFSSL **ssl_s,
  58937. method_provider method_c,
  58938. method_provider method_s,
  58939. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  58940. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  58941. const char* pkey_c, int fmt_kc, /* client private key and format */
  58942. const char* pkey_s, int fmt_ks /* server private key and format */
  58943. )
  58944. {
  58945. int ret;
  58946. if (ctx_c != NULL && *ctx_c == NULL) {
  58947. *ctx_c = wolfSSL_CTX_new(method_c());
  58948. if (*ctx_c == NULL) {
  58949. return -1;
  58950. }
  58951. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  58952. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  58953. if (ret != WOLFSSL_SUCCESS) {
  58954. return -1;
  58955. }
  58956. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  58957. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  58958. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  58959. if (ret != WOLFSSL_SUCCESS) {
  58960. return -1;
  58961. }
  58962. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  58963. if (ret != WOLFSSL_SUCCESS) {
  58964. return -1;
  58965. }
  58966. }
  58967. if (ctx_s != NULL && *ctx_s == NULL) {
  58968. *ctx_s = wolfSSL_CTX_new(method_s());
  58969. if (*ctx_s == NULL) {
  58970. return -1;
  58971. }
  58972. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  58973. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  58974. if (ret != WOLFSSL_SUCCESS) {
  58975. return -1;
  58976. }
  58977. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  58978. if (ret != WOLFSSL_SUCCESS) {
  58979. return -1;
  58980. }
  58981. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  58982. if (ret != WOLFSSL_SUCCESS) {
  58983. return -1;
  58984. }
  58985. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  58986. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  58987. if (ctx->s_ciphers != NULL) {
  58988. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  58989. if (ret != WOLFSSL_SUCCESS) {
  58990. return -1;
  58991. }
  58992. }
  58993. }
  58994. if (ctx_c != NULL && ssl_c != NULL) {
  58995. *ssl_c = wolfSSL_new(*ctx_c);
  58996. if (*ssl_c == NULL) {
  58997. return -1;
  58998. }
  58999. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  59000. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  59001. }
  59002. if (ctx_s != NULL && ssl_s != NULL) {
  59003. *ssl_s = wolfSSL_new(*ctx_s);
  59004. if (*ssl_s == NULL) {
  59005. return -1;
  59006. }
  59007. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  59008. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  59009. #if !defined(NO_DH)
  59010. SetDH(*ssl_s);
  59011. #endif
  59012. }
  59013. return 0;
  59014. }
  59015. #endif /* HAVE_RPK */
  59016. static int test_rpk_set_xxx_cert_type(void)
  59017. {
  59018. EXPECT_DECLS;
  59019. #if defined(HAVE_RPK)
  59020. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  59021. WOLFSSL_CTX* ctx = NULL;
  59022. WOLFSSL* ssl = NULL;
  59023. int tp;
  59024. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  59025. ExpectNotNull(ctx);
  59026. ssl = wolfSSL_new(ctx);
  59027. ExpectNotNull(ssl);
  59028. /*--------------------------------------------*/
  59029. /* tests for wolfSSL_CTX_set_client_cert_type */
  59030. /*--------------------------------------------*/
  59031. /* illegal parameter test caces */
  59032. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  59033. MAX_CLIENT_CERT_TYPE_CNT),
  59034. BAD_FUNC_ARG);
  59035. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59036. sizeof(ctype)),
  59037. BAD_FUNC_ARG);
  59038. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59039. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59040. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59041. MAX_CLIENT_CERT_TYPE_CNT),
  59042. BAD_FUNC_ARG);
  59043. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59044. ctype[1] = 10; /* set unknown cert type */
  59045. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59046. MAX_CLIENT_CERT_TYPE_CNT),
  59047. BAD_FUNC_ARG);
  59048. /* pass larger type count */
  59049. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59050. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59051. ctype[2] = 1; /* pass unacceptable type count */
  59052. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59053. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59054. BAD_FUNC_ARG);
  59055. /* should accept NULL for type buffer */
  59056. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  59057. MAX_CLIENT_CERT_TYPE_CNT),
  59058. WOLFSSL_SUCCESS);
  59059. /* should accept zero for type count */
  59060. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59061. 0),
  59062. WOLFSSL_SUCCESS);
  59063. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59064. MAX_CLIENT_CERT_TYPE_CNT),
  59065. WOLFSSL_SUCCESS);
  59066. /*--------------------------------------------*/
  59067. /* tests for wolfSSL_CTX_set_server_cert_type */
  59068. /*--------------------------------------------*/
  59069. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  59070. MAX_SERVER_CERT_TYPE_CNT),
  59071. BAD_FUNC_ARG);
  59072. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59073. sizeof(ctype)),
  59074. BAD_FUNC_ARG);
  59075. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59076. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59077. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59078. MAX_SERVER_CERT_TYPE_CNT),
  59079. BAD_FUNC_ARG);
  59080. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59081. ctype[1] = 10; /* set unknown cert type */
  59082. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59083. MAX_SERVER_CERT_TYPE_CNT),
  59084. BAD_FUNC_ARG);
  59085. /* pass larger type count */
  59086. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59087. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59088. ctype[2] = 1; /* pass unacceptable type count */
  59089. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59090. MAX_SERVER_CERT_TYPE_CNT + 1),
  59091. BAD_FUNC_ARG);
  59092. /* should accept NULL for type buffer */
  59093. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  59094. MAX_SERVER_CERT_TYPE_CNT),
  59095. WOLFSSL_SUCCESS);
  59096. /* should accept zero for type count */
  59097. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59098. 0),
  59099. WOLFSSL_SUCCESS);
  59100. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59101. MAX_CLIENT_CERT_TYPE_CNT),
  59102. WOLFSSL_SUCCESS);
  59103. /*--------------------------------------------*/
  59104. /* tests for wolfSSL_set_client_cert_type */
  59105. /*--------------------------------------------*/
  59106. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  59107. MAX_CLIENT_CERT_TYPE_CNT),
  59108. BAD_FUNC_ARG);
  59109. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59110. sizeof(ctype)),
  59111. BAD_FUNC_ARG);
  59112. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59113. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59114. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59115. MAX_CLIENT_CERT_TYPE_CNT),
  59116. BAD_FUNC_ARG);
  59117. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59118. ctype[1] = 10; /* set unknown cert type */
  59119. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59120. MAX_CLIENT_CERT_TYPE_CNT),
  59121. BAD_FUNC_ARG);
  59122. /* pass larger type count */
  59123. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59124. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59125. ctype[2] = 1; /* pass unacceptable type count */
  59126. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59127. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59128. BAD_FUNC_ARG);
  59129. /* should accept NULL for type buffer */
  59130. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  59131. MAX_CLIENT_CERT_TYPE_CNT),
  59132. WOLFSSL_SUCCESS);
  59133. /* should accept zero for type count */
  59134. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59135. 0),
  59136. WOLFSSL_SUCCESS);
  59137. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59138. MAX_CLIENT_CERT_TYPE_CNT),
  59139. WOLFSSL_SUCCESS);
  59140. /*--------------------------------------------*/
  59141. /* tests for wolfSSL_CTX_set_server_cert_type */
  59142. /*--------------------------------------------*/
  59143. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  59144. MAX_SERVER_CERT_TYPE_CNT),
  59145. BAD_FUNC_ARG);
  59146. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59147. sizeof(ctype)),
  59148. BAD_FUNC_ARG);
  59149. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59150. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59151. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59152. MAX_SERVER_CERT_TYPE_CNT),
  59153. BAD_FUNC_ARG);
  59154. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59155. ctype[1] = 10; /* set unknown cert type */
  59156. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59157. MAX_SERVER_CERT_TYPE_CNT),
  59158. BAD_FUNC_ARG);
  59159. /* pass larger type count */
  59160. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59161. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59162. ctype[2] = 1; /* pass unacceptable type count */
  59163. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59164. MAX_SERVER_CERT_TYPE_CNT + 1),
  59165. BAD_FUNC_ARG);
  59166. /* should accept NULL for type buffer */
  59167. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  59168. MAX_SERVER_CERT_TYPE_CNT),
  59169. WOLFSSL_SUCCESS);
  59170. /* should accept zero for type count */
  59171. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59172. 0),
  59173. WOLFSSL_SUCCESS);
  59174. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59175. MAX_SERVER_CERT_TYPE_CNT),
  59176. WOLFSSL_SUCCESS);
  59177. /*------------------------------------------------*/
  59178. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  59179. /*------------------------------------------------*/
  59180. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  59181. BAD_FUNC_ARG);
  59182. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  59183. BAD_FUNC_ARG);
  59184. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  59185. BAD_FUNC_ARG);
  59186. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  59187. BAD_FUNC_ARG);
  59188. /* clean up */
  59189. wolfSSL_free(ssl);
  59190. wolfSSL_CTX_free(ctx);
  59191. #endif
  59192. return EXPECT_RESULT();
  59193. }
  59194. static int test_tls13_rpk_handshake(void)
  59195. {
  59196. EXPECT_DECLS;
  59197. #if defined(HAVE_RPK)
  59198. int ret = 0;
  59199. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  59200. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  59201. struct test_memio_ctx test_ctx;
  59202. int err;
  59203. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  59204. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  59205. int typeCnt_c;
  59206. int typeCnt_s;
  59207. int tp;
  59208. (void)err;
  59209. (void)typeCnt_c;
  59210. (void)typeCnt_s;
  59211. (void)certType_c;
  59212. (void)certType_s;
  59213. /* TLS1.2
  59214. * Both client and server load x509 cert and start handshaking.
  59215. * Check no negotiation occurred.
  59216. */
  59217. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59218. ExpectIntEQ(
  59219. test_rpk_memio_setup(
  59220. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59221. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  59222. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59223. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59224. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59225. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  59226. , 0);
  59227. /* set client certificate type in client end */
  59228. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59229. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59230. typeCnt_c = 2;
  59231. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59232. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59233. typeCnt_s = 2;
  59234. /* both clien and server do not call client/server_cert_type APIs,
  59235. * expecting default settings works and no negotiation performed.
  59236. */
  59237. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59238. return TEST_FAIL;
  59239. /* confirm no negotiation occurred */
  59240. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59241. WOLFSSL_SUCCESS);
  59242. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59243. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59244. WOLFSSL_SUCCESS);
  59245. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59246. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59247. WOLFSSL_SUCCESS);
  59248. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59249. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59250. WOLFSSL_SUCCESS);
  59251. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59252. wolfSSL_free(ssl_c);
  59253. wolfSSL_CTX_free(ctx_c);
  59254. wolfSSL_free(ssl_s);
  59255. wolfSSL_CTX_free(ctx_s);
  59256. ssl_c = ssl_s = NULL;
  59257. ctx_c = ctx_s = NULL;
  59258. /* Both client and server load x509 cert and start handshaking.
  59259. * Check no negotiation occurred.
  59260. */
  59261. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59262. ExpectIntEQ(
  59263. test_rpk_memio_setup(
  59264. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59265. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59266. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59267. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59268. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59269. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59270. , 0);
  59271. /* set client certificate type in client end */
  59272. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59273. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59274. typeCnt_c = 2;
  59275. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59276. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59277. typeCnt_s = 2;
  59278. /* both clien and server do not call client/server_cert_type APIs,
  59279. * expecting default settings works and no negotiation performed.
  59280. */
  59281. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59282. return TEST_FAIL;
  59283. /* confirm no negotiation occurred */
  59284. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59285. WOLFSSL_SUCCESS);
  59286. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59287. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59288. WOLFSSL_SUCCESS);
  59289. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59290. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59291. WOLFSSL_SUCCESS);
  59292. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59293. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59294. WOLFSSL_SUCCESS);
  59295. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59296. wolfSSL_free(ssl_c);
  59297. wolfSSL_CTX_free(ctx_c);
  59298. wolfSSL_free(ssl_s);
  59299. wolfSSL_CTX_free(ctx_s);
  59300. ssl_c = ssl_s = NULL;
  59301. ctx_c = ctx_s = NULL;
  59302. /* Both client and server load RPK cert and start handshaking.
  59303. * Confirm negotiated cert types match as expected.
  59304. */
  59305. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59306. ExpectIntEQ(
  59307. test_rpk_memio_setup(
  59308. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59309. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59310. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59311. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59312. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59313. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59314. , 0);
  59315. /* set client certificate type in client end */
  59316. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59317. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59318. typeCnt_c = 2;
  59319. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59320. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59321. typeCnt_s = 2;
  59322. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59323. WOLFSSL_SUCCESS);
  59324. /* set server certificate type in client end */
  59325. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59326. WOLFSSL_SUCCESS);
  59327. /* set client certificate type in server end */
  59328. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59329. WOLFSSL_SUCCESS);
  59330. /* set server certificate type in server end */
  59331. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59332. WOLFSSL_SUCCESS);
  59333. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59334. return TEST_FAIL;
  59335. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59336. WOLFSSL_SUCCESS);
  59337. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59338. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59339. WOLFSSL_SUCCESS);
  59340. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59341. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59342. WOLFSSL_SUCCESS);
  59343. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59344. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59345. WOLFSSL_SUCCESS);
  59346. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59347. wolfSSL_free(ssl_c);
  59348. wolfSSL_CTX_free(ctx_c);
  59349. wolfSSL_free(ssl_s);
  59350. wolfSSL_CTX_free(ctx_s);
  59351. ssl_c = ssl_s = NULL;
  59352. ctx_c = ctx_s = NULL;
  59353. /* TLS1.2
  59354. * Both client and server load RPK cert and start handshaking.
  59355. * Confirm negotiated cert types match as expected.
  59356. */
  59357. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59358. ExpectIntEQ(
  59359. test_rpk_memio_setup(
  59360. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59361. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  59362. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59363. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59364. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59365. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59366. , 0);
  59367. /* set client certificate type in client end */
  59368. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59369. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59370. typeCnt_c = 2;
  59371. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59372. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59373. typeCnt_s = 2;
  59374. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59375. WOLFSSL_SUCCESS);
  59376. /* set server certificate type in client end */
  59377. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59378. WOLFSSL_SUCCESS);
  59379. /* set client certificate type in server end */
  59380. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59381. WOLFSSL_SUCCESS);
  59382. /* set server certificate type in server end */
  59383. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59384. WOLFSSL_SUCCESS);
  59385. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59386. return TEST_FAIL;
  59387. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59388. WOLFSSL_SUCCESS);
  59389. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59390. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59391. WOLFSSL_SUCCESS);
  59392. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59393. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59394. WOLFSSL_SUCCESS);
  59395. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59396. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59397. WOLFSSL_SUCCESS);
  59398. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59399. wolfSSL_free(ssl_c);
  59400. wolfSSL_CTX_free(ctx_c);
  59401. wolfSSL_free(ssl_s);
  59402. wolfSSL_CTX_free(ctx_s);
  59403. ssl_c = ssl_s = NULL;
  59404. ctx_c = ctx_s = NULL;
  59405. /* Both client and server load x509 cert.
  59406. * Have client call set_client_cert_type with both RPK and x509.
  59407. * This doesn't makes client add client cert type extension to ClientHello,
  59408. * since it does not load RPK cert actually.
  59409. */
  59410. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59411. ExpectIntEQ(
  59412. test_rpk_memio_setup(
  59413. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59414. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59415. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59416. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59417. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59418. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59419. , 0);
  59420. /* set client certificate type in client end */
  59421. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59422. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59423. typeCnt_c = 2;
  59424. /* client indicates both RPK and x509 certs are available but loaded RPK
  59425. * cert only. It does not have client add client-cert-type extension in CH.
  59426. */
  59427. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59428. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59429. typeCnt_c = 2;
  59430. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59431. WOLFSSL_SUCCESS);
  59432. /* client indicates both RPK and x509 certs are acceptable */
  59433. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59434. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59435. typeCnt_s = 2;
  59436. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59437. WOLFSSL_SUCCESS);
  59438. /* server indicates both RPK and x509 certs are acceptable */
  59439. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59440. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59441. typeCnt_c = 2;
  59442. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59443. WOLFSSL_SUCCESS);
  59444. /* server should indicate only RPK cert is available */
  59445. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59446. certType_s[1] = -1;
  59447. typeCnt_s = 1;
  59448. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59449. WOLFSSL_SUCCESS);
  59450. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59451. return TEST_FAIL;
  59452. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  59453. * be returned as cert type.
  59454. */
  59455. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59456. WOLFSSL_SUCCESS);
  59457. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59458. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59459. WOLFSSL_SUCCESS);
  59460. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59461. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59462. WOLFSSL_SUCCESS);
  59463. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59464. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59465. WOLFSSL_SUCCESS);
  59466. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59467. wolfSSL_free(ssl_c);
  59468. wolfSSL_CTX_free(ctx_c);
  59469. wolfSSL_free(ssl_s);
  59470. wolfSSL_CTX_free(ctx_s);
  59471. ssl_c = ssl_s = NULL;
  59472. ctx_c = ctx_s = NULL;
  59473. /* Have client load RPK cert and have server load x509 cert.
  59474. * Check the negotiation result from both ends.
  59475. */
  59476. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59477. ExpectIntEQ(
  59478. test_rpk_memio_setup(
  59479. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59480. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59481. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59482. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59483. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59484. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59485. , 0);
  59486. /* have client tell to use RPK cert */
  59487. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59488. certType_c[1] = -1;
  59489. typeCnt_c = 1;
  59490. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59491. WOLFSSL_SUCCESS);
  59492. /* have client tell to accept both RPK and x509 cert */
  59493. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59494. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  59495. typeCnt_s = 2;
  59496. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59497. WOLFSSL_SUCCESS);
  59498. /* have server accept to both RPK and x509 cert */
  59499. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  59500. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  59501. typeCnt_c = 2;
  59502. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59503. WOLFSSL_SUCCESS);
  59504. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  59505. * end, expecting the default setting works.
  59506. */
  59507. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59508. return TEST_FAIL;
  59509. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59510. WOLFSSL_SUCCESS);
  59511. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59512. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59513. WOLFSSL_SUCCESS);
  59514. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59515. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59516. WOLFSSL_SUCCESS);
  59517. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59518. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59519. WOLFSSL_SUCCESS);
  59520. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59521. wolfSSL_free(ssl_c);
  59522. wolfSSL_CTX_free(ctx_c);
  59523. wolfSSL_free(ssl_s);
  59524. wolfSSL_CTX_free(ctx_s);
  59525. ssl_c = ssl_s = NULL;
  59526. ctx_c = ctx_s = NULL;
  59527. /* Have both client and server load RPK cert, however, have server
  59528. * indicate its cert type x509.
  59529. * Client is expected to detect the cert type mismatch then to send alert
  59530. * with "unsupported_certificate".
  59531. */
  59532. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59533. ExpectIntEQ(
  59534. test_rpk_memio_setup(
  59535. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59536. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59537. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59538. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  59539. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59540. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59541. , 0);
  59542. /* have client tell to use RPK cert */
  59543. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59544. certType_c[1] = -1;
  59545. typeCnt_c = 1;
  59546. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59547. WOLFSSL_SUCCESS);
  59548. /* have client tell to accept both RPK and x509 cert */
  59549. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59550. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  59551. typeCnt_s = 2;
  59552. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59553. WOLFSSL_SUCCESS);
  59554. /* have server accept to both RPK and x509 cert */
  59555. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  59556. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  59557. typeCnt_c = 2;
  59558. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59559. WOLFSSL_SUCCESS);
  59560. /* have server tell to use x509 cert intentionally. This will bring
  59561. * certificate type mismatch in client side.
  59562. */
  59563. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59564. certType_s[1] = -1;
  59565. typeCnt_s = 1;
  59566. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59567. WOLFSSL_SUCCESS);
  59568. /* expect client detect cert type mismatch then send Alert */
  59569. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  59570. if (ret != -1)
  59571. return TEST_FAIL;
  59572. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  59573. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59574. WOLFSSL_SUCCESS);
  59575. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59576. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59577. WOLFSSL_SUCCESS);
  59578. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59579. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59580. WOLFSSL_SUCCESS);
  59581. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59582. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59583. WOLFSSL_SUCCESS);
  59584. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59585. wolfSSL_free(ssl_c);
  59586. wolfSSL_CTX_free(ctx_c);
  59587. wolfSSL_free(ssl_s);
  59588. wolfSSL_CTX_free(ctx_s);
  59589. ssl_c = ssl_s = NULL;
  59590. ctx_c = ctx_s = NULL;
  59591. /* Have client load x509 cert and server load RPK cert,
  59592. * however, have client indicate its cert type RPK.
  59593. * Server is expected to detect the cert type mismatch then to send alert
  59594. * with "unsupported_certificate".
  59595. */
  59596. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59597. ExpectIntEQ(
  59598. test_rpk_memio_setup(
  59599. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59600. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59601. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59602. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59603. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59604. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59605. , 0);
  59606. /* have client tell to use RPK cert intentionally */
  59607. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59608. certType_c[1] = -1;
  59609. typeCnt_c = 1;
  59610. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59611. WOLFSSL_SUCCESS);
  59612. /* have client tell to accept both RPK and x509 cert */
  59613. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59614. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  59615. typeCnt_s = 2;
  59616. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59617. WOLFSSL_SUCCESS);
  59618. /* have server accept to both RPK and x509 cert */
  59619. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  59620. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  59621. typeCnt_c = 2;
  59622. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59623. WOLFSSL_SUCCESS);
  59624. /* have server tell to use x509 cert intentionally. This will bring
  59625. * certificate type mismatch in client side.
  59626. */
  59627. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  59628. certType_s[1] = -1;
  59629. typeCnt_s = 1;
  59630. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59631. WOLFSSL_SUCCESS);
  59632. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  59633. /* expect server detect cert type mismatch then send Alert */
  59634. ExpectIntNE(ret, 0);
  59635. err = wolfSSL_get_error(ssl_c, ret);
  59636. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  59637. /* client did not load RPK cert actually, so negotiation did not happen */
  59638. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59639. WOLFSSL_SUCCESS);
  59640. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59641. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59642. WOLFSSL_SUCCESS);
  59643. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59644. /* client did not load RPK cert actually, so negotiation did not happen */
  59645. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59646. WOLFSSL_SUCCESS);
  59647. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59648. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59649. WOLFSSL_SUCCESS);
  59650. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  59651. wolfSSL_free(ssl_c);
  59652. wolfSSL_CTX_free(ctx_c);
  59653. wolfSSL_free(ssl_s);
  59654. wolfSSL_CTX_free(ctx_s);
  59655. ssl_c = ssl_s = NULL;
  59656. ctx_c = ctx_s = NULL;
  59657. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59658. /* Both client and server load RPK cert and set certificate verify
  59659. * callbacks then start handshaking.
  59660. * Confirm both side can refer the peer's cert.
  59661. */
  59662. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59663. ExpectIntEQ(
  59664. test_rpk_memio_setup(
  59665. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59666. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59667. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59668. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59669. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59670. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59671. , 0);
  59672. /* set client certificate type in client end */
  59673. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59674. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59675. typeCnt_c = 2;
  59676. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59677. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59678. typeCnt_s = 2;
  59679. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  59680. WOLFSSL_SUCCESS);
  59681. /* set server certificate type in client end */
  59682. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  59683. WOLFSSL_SUCCESS);
  59684. /* set client certificate type in server end */
  59685. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  59686. WOLFSSL_SUCCESS);
  59687. /* set server certificate type in server end */
  59688. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  59689. WOLFSSL_SUCCESS);
  59690. /* set certificate verify callback to both client and server */
  59691. int isServer = 0;
  59692. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  59693. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  59694. isServer = 1;
  59695. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  59696. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  59697. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  59698. if (ret != 0)
  59699. return TEST_FAIL;
  59700. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59701. WOLFSSL_SUCCESS);
  59702. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59703. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59704. WOLFSSL_SUCCESS);
  59705. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59706. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59707. WOLFSSL_SUCCESS);
  59708. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59709. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59710. WOLFSSL_SUCCESS);
  59711. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  59712. wolfSSL_free(ssl_c);
  59713. wolfSSL_CTX_free(ctx_c);
  59714. wolfSSL_free(ssl_s);
  59715. wolfSSL_CTX_free(ctx_s);
  59716. ssl_c = ssl_s = NULL;
  59717. ctx_c = ctx_s = NULL;
  59718. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  59719. #endif /* HAVE_RPK */
  59720. return EXPECT_RESULT();
  59721. }
  59722. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  59723. static int test_dtls13_bad_epoch_ch(void)
  59724. {
  59725. EXPECT_DECLS;
  59726. WOLFSSL_CTX *ctx_c = NULL;
  59727. WOLFSSL_CTX *ctx_s = NULL;
  59728. WOLFSSL *ssl_c = NULL;
  59729. WOLFSSL *ssl_s = NULL;
  59730. struct test_memio_ctx test_ctx;
  59731. const int EPOCH_OFF = 3;
  59732. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59733. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59734. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59735. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  59736. * with just one message */
  59737. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  59738. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59739. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59740. WOLFSSL_ERROR_WANT_READ);
  59741. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  59742. /* first CH should use epoch 0x0 */
  59743. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  59744. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  59745. /* change epoch to 2 */
  59746. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  59747. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59748. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59749. WOLFSSL_ERROR_WANT_READ);
  59750. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  59751. /* resend the CH */
  59752. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  59753. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59754. wolfSSL_free(ssl_c);
  59755. wolfSSL_CTX_free(ctx_c);
  59756. wolfSSL_free(ssl_s);
  59757. wolfSSL_CTX_free(ctx_s);
  59758. return EXPECT_RESULT();
  59759. }
  59760. #else
  59761. static int test_dtls13_bad_epoch_ch(void)
  59762. {
  59763. return TEST_SKIPPED;
  59764. }
  59765. #endif
  59766. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  59767. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  59768. {
  59769. EXPECT_DECLS;
  59770. WOLFSSL_SESSION *sess = NULL;
  59771. /* Setup the session to avoid errors */
  59772. ssl->session->timeout = -1;
  59773. ssl->session->side = WOLFSSL_CLIENT_END;
  59774. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  59775. defined(HAVE_SESSION_TICKET))
  59776. ssl->session->version = ssl->version;
  59777. #endif
  59778. /* Force a short session ID to be sent */
  59779. ssl->session->sessionIDSz = 4;
  59780. #ifndef NO_SESSION_CACHE_REF
  59781. /* Allow the client cache to be used */
  59782. ssl->session->idLen = 4;
  59783. #endif
  59784. ssl->session->isSetup = 1;
  59785. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  59786. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  59787. return EXPECT_RESULT();
  59788. }
  59789. static int test_short_session_id(void)
  59790. {
  59791. EXPECT_DECLS;
  59792. test_ssl_cbf client_cbf;
  59793. test_ssl_cbf server_cbf;
  59794. size_t i;
  59795. struct {
  59796. method_provider client_meth;
  59797. method_provider server_meth;
  59798. const char* tls_version;
  59799. } params[] = {
  59800. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  59801. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  59802. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  59803. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  59804. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  59805. #ifdef WOLFSSL_DTLS13
  59806. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  59807. #endif
  59808. #endif
  59809. #ifndef WOLFSSL_NO_TLS12
  59810. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  59811. #ifdef WOLFSSL_DTLS
  59812. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  59813. #endif
  59814. #endif
  59815. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  59816. !defined(NO_DES3))
  59817. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  59818. #ifdef WOLFSSL_DTLS
  59819. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  59820. #endif
  59821. #endif
  59822. };
  59823. fprintf(stderr, "\n");
  59824. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  59825. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  59826. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  59827. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  59828. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  59829. client_cbf.method = params[i].client_meth;
  59830. server_cbf.method = params[i].server_meth;
  59831. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  59832. &server_cbf, NULL), TEST_SUCCESS);
  59833. }
  59834. return EXPECT_RESULT();
  59835. }
  59836. #else
  59837. static int test_short_session_id(void)
  59838. {
  59839. return TEST_SKIPPED;
  59840. }
  59841. #endif
  59842. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  59843. && defined(WOLFSSL_DTLS13)
  59844. static byte* test_find_string(const char *string,
  59845. byte *buf, int buf_size)
  59846. {
  59847. int string_size, i;
  59848. string_size = (int)XSTRLEN(string);
  59849. for (i = 0; i < buf_size - string_size - 1; i++) {
  59850. if (XSTRCMP((char*)&buf[i], string) == 0)
  59851. return &buf[i];
  59852. }
  59853. return NULL;
  59854. }
  59855. static int test_wolfSSL_dtls13_null_cipher(void)
  59856. {
  59857. EXPECT_DECLS;
  59858. WOLFSSL_CTX *ctx_c = NULL;
  59859. WOLFSSL_CTX *ctx_s = NULL;
  59860. WOLFSSL *ssl_c = NULL;
  59861. WOLFSSL *ssl_s = NULL;
  59862. struct test_memio_ctx test_ctx;
  59863. const char *test_str = "test";
  59864. int test_str_size;
  59865. byte buf[255], *ptr = NULL;
  59866. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59867. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  59868. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59869. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59870. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59871. test_str_size = XSTRLEN("test") + 1;
  59872. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59873. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  59874. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  59875. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  59876. /* check that the packet was sent cleartext */
  59877. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  59878. test_ctx.s_len));
  59879. if (ptr != NULL) {
  59880. /* modify the message */
  59881. *ptr = 'H';
  59882. /* bad messages should be ignored in DTLS */
  59883. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  59884. ExpectIntEQ(ssl_s->error, WANT_READ);
  59885. }
  59886. wolfSSL_free(ssl_c);
  59887. wolfSSL_free(ssl_s);
  59888. wolfSSL_CTX_free(ctx_c);
  59889. wolfSSL_CTX_free(ctx_s);
  59890. return TEST_SUCCESS;
  59891. }
  59892. #else
  59893. static int test_wolfSSL_dtls13_null_cipher(void)
  59894. {
  59895. return TEST_SKIPPED;
  59896. }
  59897. #endif
  59898. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59899. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59900. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  59901. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  59902. {
  59903. SOCKADDR_S peer;
  59904. XSOCKLENT len;
  59905. int ret;
  59906. XMEMSET((byte*)&peer, 0, sizeof(peer));
  59907. len = sizeof(peer);
  59908. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  59909. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  59910. return -1;
  59911. switch (peer.ss_family) {
  59912. #ifdef WOLFSSL_IPV6
  59913. case WOLFSSL_IP6: {
  59914. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  59915. break;
  59916. }
  59917. #endif /* WOLFSSL_IPV6 */
  59918. case WOLFSSL_IP4:
  59919. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  59920. break;
  59921. default:
  59922. return -1;
  59923. }
  59924. return 0;
  59925. }
  59926. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  59927. {
  59928. char buf[1] = {'t'};
  59929. SOCKADDR_IN_T addr;
  59930. int sock_fd;
  59931. word16 port;
  59932. int err;
  59933. (void)ssl;
  59934. (void)ctx;
  59935. if (ssl == NULL)
  59936. return -1;
  59937. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  59938. if (err != 0)
  59939. return -1;
  59940. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  59941. if (sock_fd == -1)
  59942. return -1;
  59943. build_addr(&addr, wolfSSLIP, port, 1, 0);
  59944. /* send a packet to the server. Being another socket, the kernel will ensure
  59945. * the source port will be different. */
  59946. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  59947. sizeof(addr));
  59948. close(sock_fd);
  59949. if (err == -1)
  59950. return -1;
  59951. return 0;
  59952. }
  59953. /* setup a SSL session but just after the handshake send a packet to the server
  59954. * with a source address different than the one of the connected client. The I/O
  59955. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  59956. * is done in test_dtls_msg_from_other_peer_cb */
  59957. static int test_dtls_msg_from_other_peer(void)
  59958. {
  59959. EXPECT_DECLS;
  59960. callback_functions client_cbs;
  59961. callback_functions server_cbs;
  59962. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  59963. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  59964. client_cbs.method = wolfDTLSv1_2_client_method;
  59965. server_cbs.method = wolfDTLSv1_2_server_method;
  59966. client_cbs.doUdp = 1;
  59967. server_cbs.doUdp = 1;
  59968. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  59969. test_dtls_msg_from_other_peer_cb);
  59970. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  59971. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  59972. return EXPECT_RESULT();
  59973. }
  59974. #else
  59975. static int test_dtls_msg_from_other_peer(void)
  59976. {
  59977. return TEST_SKIPPED;
  59978. }
  59979. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59980. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59981. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  59982. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  59983. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59984. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  59985. static int test_dtls_ipv6_check(void)
  59986. {
  59987. EXPECT_DECLS;
  59988. WOLFSSL_CTX *ctx_c = NULL;
  59989. WOLFSSL_CTX *ctx_s = NULL;
  59990. WOLFSSL *ssl_c = NULL;
  59991. WOLFSSL *ssl_s = NULL;
  59992. SOCKADDR_IN fake_addr6;
  59993. int sockfd = -1;
  59994. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  59995. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  59996. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  59997. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  59998. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59999. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  60000. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60001. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  60002. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  60003. /* mimic a sockaddr_in6 struct, this way we can't test without
  60004. * WOLFSSL_IPV6 */
  60005. fake_addr6.sin_family = WOLFSSL_IP6;
  60006. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  60007. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60008. /* can't return error here, as the peer is opaque for wolfssl library at
  60009. * this point */
  60010. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  60011. WOLFSSL_SUCCESS);
  60012. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  60013. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  60014. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60015. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  60016. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  60017. WOLFSSL_SUCCESS);
  60018. /* reuse the socket */
  60019. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60020. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  60021. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60022. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  60023. if (sockfd != -1)
  60024. close(sockfd);
  60025. wolfSSL_free(ssl_c);
  60026. wolfSSL_CTX_free(ctx_c);
  60027. wolfSSL_free(ssl_s);
  60028. wolfSSL_CTX_free(ctx_s);
  60029. return EXPECT_RESULT();
  60030. }
  60031. #else
  60032. static int test_dtls_ipv6_check(void)
  60033. {
  60034. return TEST_SKIPPED;
  60035. }
  60036. #endif
  60037. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60038. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60039. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  60040. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  60041. {
  60042. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60043. }
  60044. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  60045. {
  60046. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  60047. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  60048. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  60049. }
  60050. else {
  60051. char testMsg[] = "Message after SCR";
  60052. char msgBuf[sizeof(testMsg)];
  60053. int ret;
  60054. if (!wolfSSL_is_server(ssl)) {
  60055. AssertIntEQ(WOLFSSL_SUCCESS,
  60056. wolfSSL_set_session(ssl,
  60057. test_wolfSSL_SCR_after_resumption_session));
  60058. }
  60059. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60060. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60061. sizeof(testMsg));
  60062. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60063. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  60064. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60065. AssertIntEQ(ret, sizeof(msgBuf));
  60066. }
  60067. }
  60068. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  60069. {
  60070. AssertIntEQ(WOLFSSL_SUCCESS,
  60071. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  60072. }
  60073. static int test_wolfSSL_SCR_after_resumption(void)
  60074. {
  60075. EXPECT_DECLS;
  60076. callback_functions func_cb_client;
  60077. callback_functions func_cb_server;
  60078. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60079. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60080. func_cb_client.method = wolfTLSv1_2_client_method;
  60081. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60082. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60083. func_cb_server.method = wolfTLSv1_2_server_method;
  60084. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60085. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60086. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60087. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60088. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  60089. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60090. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60091. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60092. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60093. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  60094. return EXPECT_RESULT();
  60095. }
  60096. #else
  60097. static int test_wolfSSL_SCR_after_resumption(void)
  60098. {
  60099. return TEST_SKIPPED;
  60100. }
  60101. #endif
  60102. static int test_wolfSSL_configure_args(void)
  60103. {
  60104. EXPECT_DECLS;
  60105. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  60106. ExpectNotNull(wolfSSL_configure_args());
  60107. #endif
  60108. return EXPECT_RESULT();
  60109. }
  60110. static int test_dtls_no_extensions(void)
  60111. {
  60112. EXPECT_DECLS;
  60113. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60114. !defined(WOLFSSL_NO_TLS12)
  60115. WOLFSSL *ssl_s = NULL;
  60116. WOLFSSL_CTX *ctx_s = NULL;
  60117. struct test_memio_ctx test_ctx;
  60118. const byte chNoExtensions[] = {
  60119. /* Handshake type */
  60120. 0x16,
  60121. /* Version */
  60122. 0xfe, 0xff,
  60123. /* Epoch */
  60124. 0x00, 0x00,
  60125. /* Seq number */
  60126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60127. /* Length */
  60128. 0x00, 0x40,
  60129. /* CH type */
  60130. 0x01,
  60131. /* Length */
  60132. 0x00, 0x00, 0x34,
  60133. /* Msg Seq */
  60134. 0x00, 0x00,
  60135. /* Frag offset */
  60136. 0x00, 0x00, 0x00,
  60137. /* Frag length */
  60138. 0x00, 0x00, 0x34,
  60139. /* Version */
  60140. 0xfe, 0xff,
  60141. /* Random */
  60142. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  60143. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  60144. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  60145. /* Session Length */
  60146. 0x00,
  60147. /* Cookie Length */
  60148. 0x00,
  60149. /* CS Length */
  60150. 0x00, 0x0c,
  60151. /* CS */
  60152. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  60153. /* Comp Meths Length */
  60154. 0x01,
  60155. /* Comp Meths */
  60156. 0x00
  60157. /* And finally... no extensions */
  60158. };
  60159. int i;
  60160. #ifdef OPENSSL_EXTRA
  60161. int repeats = 2;
  60162. #else
  60163. int repeats = 1;
  60164. #endif
  60165. for (i = 0; i < repeats; i++) {
  60166. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60167. ssl_s = NULL;
  60168. ctx_s = NULL;
  60169. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  60170. NULL, wolfDTLS_server_method), 0);
  60171. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  60172. test_ctx.s_len = sizeof(chNoExtensions);
  60173. #ifdef OPENSSL_EXTRA
  60174. if (i > 0) {
  60175. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  60176. WOLFSSL_SUCCESS);
  60177. }
  60178. #endif
  60179. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  60180. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60181. /* Expecting a handshake msg. Either HVR or SH. */
  60182. ExpectIntGT(test_ctx.c_len, 0);
  60183. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  60184. wolfSSL_free(ssl_s);
  60185. wolfSSL_CTX_free(ctx_s);
  60186. }
  60187. #endif
  60188. return EXPECT_RESULT();
  60189. }
  60190. static int test_TLSX_CA_NAMES_bad_extension(void)
  60191. {
  60192. EXPECT_DECLS;
  60193. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60194. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  60195. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  60196. defined(HAVE_NULL_CIPHER)
  60197. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  60198. * parse the CA Names extension. Otherwise it will return other non-related
  60199. * errors. If CA Names will be parsed in more configurations, that should
  60200. * be reflected in the macro guard above. */
  60201. WOLFSSL *ssl_c = NULL;
  60202. WOLFSSL_CTX *ctx_c = NULL;
  60203. struct test_memio_ctx test_ctx;
  60204. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  60205. const byte shBadCaNamesExt[] = {
  60206. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60207. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60208. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60209. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60210. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60211. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60212. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  60213. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  60214. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  60215. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60216. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  60217. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  60218. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  60219. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  60220. };
  60221. const byte shBadCaNamesExt2[] = {
  60222. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60223. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60224. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60225. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60226. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60227. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60228. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  60229. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  60230. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  60231. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60232. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  60233. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  60234. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  60235. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  60236. };
  60237. int i = 0;
  60238. for (i = 0; i < 2; i++) {
  60239. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60240. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60241. wolfTLSv1_3_client_method, NULL), 0);
  60242. switch (i) {
  60243. case 0:
  60244. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  60245. sizeof(shBadCaNamesExt));
  60246. test_ctx.c_len = sizeof(shBadCaNamesExt);
  60247. break;
  60248. case 1:
  60249. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  60250. sizeof(shBadCaNamesExt2));
  60251. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  60252. break;
  60253. }
  60254. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60255. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  60256. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  60257. #else
  60258. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  60259. #endif
  60260. wolfSSL_free(ssl_c);
  60261. ssl_c = NULL;
  60262. wolfSSL_CTX_free(ctx_c);
  60263. ctx_c = NULL;
  60264. }
  60265. #endif
  60266. return EXPECT_RESULT();
  60267. }
  60268. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60269. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60270. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  60271. {
  60272. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  60273. WOLFSSL_SUCCESS);
  60274. }
  60275. static int test_dtls_1_0_hvr_downgrade(void)
  60276. {
  60277. EXPECT_DECLS;
  60278. callback_functions func_cb_client;
  60279. callback_functions func_cb_server;
  60280. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60281. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60282. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  60283. func_cb_client.method = wolfDTLS_client_method;
  60284. func_cb_server.method = wolfDTLSv1_2_server_method;
  60285. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  60286. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60287. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60288. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60289. return EXPECT_RESULT();
  60290. }
  60291. #else
  60292. static int test_dtls_1_0_hvr_downgrade(void)
  60293. {
  60294. EXPECT_DECLS;
  60295. return EXPECT_RESULT();
  60296. }
  60297. #endif
  60298. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  60299. defined(HAVE_SESSION_TICKET)
  60300. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  60301. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  60302. {
  60303. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  60304. AssertNotNull(test_session_ticket_no_id_session);
  60305. }
  60306. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  60307. {
  60308. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  60309. }
  60310. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  60311. {
  60312. test_session_ticket_no_id_session->sessionIDSz = 0;
  60313. AssertIntEQ(WOLFSSL_SUCCESS,
  60314. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  60315. }
  60316. static int test_session_ticket_no_id(void)
  60317. {
  60318. /* We are testing an expired (invalid crypto context in out case since the
  60319. * ctx changes) session ticket being sent with the session ID being 0
  60320. * length. */
  60321. EXPECT_DECLS;
  60322. callback_functions func_cb_client;
  60323. callback_functions func_cb_server;
  60324. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60325. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60326. func_cb_client.method = wolfTLSv1_2_client_method;
  60327. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  60328. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  60329. func_cb_server.method = wolfTLSv1_2_server_method;
  60330. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  60331. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60332. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60333. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60334. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60335. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60336. func_cb_client.method = wolfTLSv1_2_client_method;
  60337. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  60338. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  60339. func_cb_server.method = wolfTLSv1_2_server_method;
  60340. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  60341. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60342. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60343. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60344. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  60345. return EXPECT_RESULT();
  60346. }
  60347. #else
  60348. static int test_session_ticket_no_id(void)
  60349. {
  60350. EXPECT_DECLS;
  60351. return EXPECT_RESULT();
  60352. }
  60353. #endif
  60354. static int test_session_ticket_hs_update(void)
  60355. {
  60356. EXPECT_DECLS;
  60357. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60358. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  60359. struct test_memio_ctx test_ctx;
  60360. struct test_memio_ctx test_ctx2;
  60361. struct test_memio_ctx test_ctx3;
  60362. WOLFSSL_CTX *ctx_c = NULL;
  60363. WOLFSSL_CTX *ctx_s = NULL;
  60364. WOLFSSL *ssl_c = NULL;
  60365. WOLFSSL *ssl_c2 = NULL;
  60366. WOLFSSL *ssl_c3 = NULL;
  60367. WOLFSSL *ssl_s = NULL;
  60368. WOLFSSL *ssl_s2 = NULL;
  60369. WOLFSSL *ssl_s3 = NULL;
  60370. WOLFSSL_SESSION *sess = NULL;
  60371. byte read_data[1];
  60372. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60373. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  60374. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  60375. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60376. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  60377. /* Generate tickets */
  60378. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60379. wolfSSL_SetLoggingPrefix("client");
  60380. /* Read the ticket msg */
  60381. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  60382. WOLFSSL_FATAL_ERROR);
  60383. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60384. WOLFSSL_ERROR_WANT_READ);
  60385. wolfSSL_SetLoggingPrefix(NULL);
  60386. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  60387. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  60388. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  60389. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  60390. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  60391. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  60392. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  60393. wolfSSL_SetLoggingPrefix("client");
  60394. /* Exchange initial flights for the second connection */
  60395. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  60396. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  60397. WOLFSSL_ERROR_WANT_READ);
  60398. wolfSSL_SetLoggingPrefix(NULL);
  60399. wolfSSL_SetLoggingPrefix("server");
  60400. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  60401. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  60402. WOLFSSL_ERROR_WANT_READ);
  60403. wolfSSL_SetLoggingPrefix(NULL);
  60404. /* Complete third connection so that new tickets are exchanged */
  60405. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  60406. /* Read the ticket msg */
  60407. wolfSSL_SetLoggingPrefix("client");
  60408. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  60409. WOLFSSL_FATAL_ERROR);
  60410. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  60411. WOLFSSL_ERROR_WANT_READ);
  60412. wolfSSL_SetLoggingPrefix(NULL);
  60413. /* Complete second connection */
  60414. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  60415. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  60416. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  60417. wolfSSL_free(ssl_c);
  60418. wolfSSL_free(ssl_c2);
  60419. wolfSSL_free(ssl_c3);
  60420. wolfSSL_free(ssl_s);
  60421. wolfSSL_free(ssl_s2);
  60422. wolfSSL_free(ssl_s3);
  60423. wolfSSL_CTX_free(ctx_c);
  60424. wolfSSL_CTX_free(ctx_s);
  60425. wolfSSL_SESSION_free(sess);
  60426. #endif
  60427. return EXPECT_RESULT();
  60428. }
  60429. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60430. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60431. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  60432. {
  60433. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  60434. WOLFSSL_SUCCESS);
  60435. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60436. }
  60437. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  60438. {
  60439. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60440. }
  60441. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  60442. {
  60443. char testMsg[] = "Message after SCR";
  60444. char msgBuf[sizeof(testMsg)];
  60445. if (wolfSSL_is_server(ssl)) {
  60446. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  60447. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  60448. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  60449. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60450. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60451. sizeof(testMsg));
  60452. }
  60453. else {
  60454. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60455. sizeof(testMsg));
  60456. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  60457. }
  60458. }
  60459. static int test_dtls_downgrade_scr_server(void)
  60460. {
  60461. EXPECT_DECLS;
  60462. callback_functions func_cb_client;
  60463. callback_functions func_cb_server;
  60464. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60465. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60466. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  60467. func_cb_client.method = wolfDTLSv1_2_client_method;
  60468. func_cb_server.method = wolfDTLS_server_method;
  60469. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  60470. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  60471. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  60472. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  60473. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60474. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60475. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60476. return EXPECT_RESULT();
  60477. }
  60478. #else
  60479. static int test_dtls_downgrade_scr_server(void)
  60480. {
  60481. EXPECT_DECLS;
  60482. return EXPECT_RESULT();
  60483. }
  60484. #endif
  60485. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60486. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60487. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  60488. {
  60489. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  60490. WOLFSSL_SUCCESS);
  60491. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60492. }
  60493. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  60494. {
  60495. char testMsg[] = "Message after SCR";
  60496. char msgBuf[sizeof(testMsg)];
  60497. if (wolfSSL_is_server(ssl)) {
  60498. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  60499. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  60500. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  60501. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60502. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60503. sizeof(testMsg));
  60504. }
  60505. else {
  60506. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60507. sizeof(testMsg));
  60508. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  60509. }
  60510. }
  60511. static int test_dtls_downgrade_scr(void)
  60512. {
  60513. EXPECT_DECLS;
  60514. callback_functions func_cb_client;
  60515. callback_functions func_cb_server;
  60516. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60517. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60518. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  60519. func_cb_client.method = wolfDTLS_client_method;
  60520. func_cb_server.method = wolfDTLSv1_2_server_method;
  60521. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  60522. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  60523. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  60524. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60525. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60526. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60527. return EXPECT_RESULT();
  60528. }
  60529. #else
  60530. static int test_dtls_downgrade_scr(void)
  60531. {
  60532. EXPECT_DECLS;
  60533. return EXPECT_RESULT();
  60534. }
  60535. #endif
  60536. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  60537. && !defined(WOLFSSL_NO_TLS12)
  60538. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  60539. char *data, int sz, void *ctx)
  60540. {
  60541. static int call_counter = 0;
  60542. call_counter++;
  60543. (void)ssl;
  60544. (void)data;
  60545. (void)sz;
  60546. (void)ctx;
  60547. switch (call_counter) {
  60548. case 1:
  60549. case 2:
  60550. return WOLFSSL_CBIO_ERR_TIMEOUT;
  60551. case 3:
  60552. return WOLFSSL_CBIO_ERR_WANT_READ;
  60553. default:
  60554. AssertIntLE(call_counter, 3);
  60555. return -1;
  60556. }
  60557. }
  60558. #endif
  60559. /* Make sure we don't send acks before getting a server hello */
  60560. static int test_dtls_client_hello_timeout_downgrade(void)
  60561. {
  60562. EXPECT_DECLS;
  60563. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  60564. && !defined(WOLFSSL_NO_TLS12)
  60565. WOLFSSL_CTX *ctx_c = NULL;
  60566. WOLFSSL_CTX *ctx_s = NULL;
  60567. WOLFSSL *ssl_c = NULL;
  60568. WOLFSSL *ssl_s = NULL;
  60569. struct test_memio_ctx test_ctx;
  60570. DtlsRecordLayerHeader* dtlsRH;
  60571. size_t len;
  60572. byte sequence_number[8];
  60573. int i;
  60574. for (i = 0; i < 2; i++) {
  60575. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60576. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60577. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  60578. if (i == 0) {
  60579. /* First time simulate timeout in IO layer */
  60580. /* CH1 */
  60581. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60582. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60583. /* HVR */
  60584. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60585. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60586. /* CH2 */
  60587. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60588. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60589. /* SH flight */
  60590. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60591. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60592. /* Drop the SH */
  60593. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  60594. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60595. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  60596. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  60597. (sizeof(DtlsRecordLayerHeader) + len));
  60598. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  60599. /* Read the remainder of the flight */
  60600. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60601. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60602. wolfSSL_SSLSetIORecv(ssl_c,
  60603. test_dtls_client_hello_timeout_downgrade_read_cb);
  60604. /* CH3 */
  60605. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60606. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60607. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  60608. }
  60609. else {
  60610. /* Second time call wolfSSL_dtls_got_timeout */
  60611. /* CH1 */
  60612. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60613. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60614. /* HVR */
  60615. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60616. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60617. /* CH2 */
  60618. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60619. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60620. /* SH flight */
  60621. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60622. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60623. /* Drop the SH */
  60624. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  60625. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60626. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  60627. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  60628. (sizeof(DtlsRecordLayerHeader) + len));
  60629. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  60630. /* Read the remainder of the flight */
  60631. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60632. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60633. /* Quick timeout should be set as we received at least one msg */
  60634. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  60635. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60636. /* Quick timeout should be cleared after a quick timeout */
  60637. /* CH3 */
  60638. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  60639. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60640. }
  60641. /* Parse out to make sure we got exactly one ClientHello message */
  60642. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60643. /* Second ClientHello after HVR */
  60644. sequence_number[7] = 2;
  60645. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  60646. ExpectIntEQ(dtlsRH->type, handshake);
  60647. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60648. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60649. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60650. sizeof(sequence_number)), 0);
  60651. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60652. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60653. /* Connection should be able to continue */
  60654. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60655. wolfSSL_free(ssl_c);
  60656. wolfSSL_free(ssl_s);
  60657. wolfSSL_CTX_free(ctx_c);
  60658. wolfSSL_CTX_free(ctx_s);
  60659. ssl_c = NULL;
  60660. ssl_s = NULL;
  60661. ctx_c = NULL;
  60662. ctx_s = NULL;
  60663. if (!EXPECT_SUCCESS())
  60664. break;
  60665. }
  60666. #endif
  60667. return EXPECT_RESULT();
  60668. }
  60669. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60670. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  60671. int sz, void *ctx)
  60672. {
  60673. static int call_counter = 0;
  60674. call_counter++;
  60675. (void)ssl;
  60676. (void)data;
  60677. (void)sz;
  60678. (void)ctx;
  60679. switch (call_counter) {
  60680. case 1:
  60681. return WOLFSSL_CBIO_ERR_TIMEOUT;
  60682. case 2:
  60683. return WOLFSSL_CBIO_ERR_WANT_READ;
  60684. default:
  60685. AssertIntLE(call_counter, 2);
  60686. return -1;
  60687. }
  60688. }
  60689. #endif
  60690. /* Make sure we don't send acks before getting a server hello */
  60691. static int test_dtls_client_hello_timeout(void)
  60692. {
  60693. EXPECT_DECLS;
  60694. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60695. WOLFSSL *ssl_c = NULL;
  60696. WOLFSSL_CTX *ctx_c = NULL;
  60697. struct test_memio_ctx test_ctx;
  60698. DtlsRecordLayerHeader* dtlsRH;
  60699. size_t idx;
  60700. size_t len;
  60701. byte sequence_number[8];
  60702. int i;
  60703. for (i = 0; i < 2; i++) {
  60704. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60705. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60706. wolfDTLSv1_3_client_method, NULL), 0);
  60707. if (i == 0) {
  60708. /* First time simulate timeout in IO layer */
  60709. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  60710. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60711. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60712. }
  60713. else {
  60714. /* Second time call wolfSSL_dtls_got_timeout */
  60715. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60716. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60717. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60718. }
  60719. /* Parse out to make sure we got exactly two ClientHello messages */
  60720. idx = 0;
  60721. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60722. /* First ClientHello */
  60723. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  60724. ExpectIntEQ(dtlsRH->type, handshake);
  60725. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60726. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60727. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60728. sizeof(sequence_number)), 0);
  60729. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60730. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60731. idx += sizeof(DtlsRecordLayerHeader) + len;
  60732. /* Second ClientHello */
  60733. sequence_number[7] = 1;
  60734. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  60735. ExpectIntEQ(dtlsRH->type, handshake);
  60736. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60737. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60738. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60739. sizeof(sequence_number)), 0);
  60740. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60741. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  60742. wolfSSL_free(ssl_c);
  60743. wolfSSL_CTX_free(ctx_c);
  60744. ssl_c = NULL;
  60745. ctx_c = NULL;
  60746. if (!EXPECT_SUCCESS())
  60747. break;
  60748. }
  60749. #endif
  60750. return EXPECT_RESULT();
  60751. }
  60752. /* DTLS test when dropping the changed cipher spec message */
  60753. static int test_dtls_dropped_ccs(void)
  60754. {
  60755. EXPECT_DECLS;
  60756. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60757. && !defined(WOLFSSL_NO_TLS12)
  60758. WOLFSSL_CTX *ctx_c = NULL;
  60759. WOLFSSL_CTX *ctx_s = NULL;
  60760. WOLFSSL *ssl_c = NULL;
  60761. WOLFSSL *ssl_s = NULL;
  60762. struct test_memio_ctx test_ctx;
  60763. DtlsRecordLayerHeader* dtlsRH;
  60764. size_t len;
  60765. byte data[1];
  60766. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60767. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60768. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60769. /* CH1 */
  60770. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60771. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60772. /* HVR */
  60773. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60774. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60775. /* CH2 */
  60776. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60777. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60778. /* Server first flight */
  60779. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60780. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60781. /* Client flight */
  60782. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60783. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60784. /* Server ccs + finished */
  60785. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  60786. /* Drop the ccs */
  60787. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  60788. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  60789. ExpectIntEQ(len, 1);
  60790. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  60791. if (EXPECT_SUCCESS()) {
  60792. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  60793. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  60794. (sizeof(DtlsRecordLayerHeader) + len));
  60795. }
  60796. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  60797. /* Client rtx flight */
  60798. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60799. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60800. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60801. /* Server ccs + finished rtx */
  60802. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  60803. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60804. /* Client processes finished */
  60805. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  60806. wolfSSL_free(ssl_c);
  60807. wolfSSL_free(ssl_s);
  60808. wolfSSL_CTX_free(ctx_c);
  60809. wolfSSL_CTX_free(ctx_s);
  60810. #endif
  60811. return EXPECT_RESULT();
  60812. }
  60813. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60814. && !defined(WOLFSSL_NO_TLS12)
  60815. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  60816. byte seq_num)
  60817. {
  60818. EXPECT_DECLS;
  60819. DtlsRecordLayerHeader* dtlsRH;
  60820. byte sequence_number[8];
  60821. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  60822. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  60823. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  60824. ExpectIntEQ(dtlsRH->type, handshake);
  60825. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  60826. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  60827. sequence_number[7] = seq_num;
  60828. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  60829. sizeof(sequence_number)), 0);
  60830. return EXPECT_RESULT();
  60831. }
  60832. #endif
  60833. /*
  60834. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  60835. * and after a HelloRetryRequest. This is testing the server side as it is
  60836. * operating statelessly and should copy the sequence number of the ClientHello.
  60837. */
  60838. static int test_dtls_seq_num_downgrade(void)
  60839. {
  60840. EXPECT_DECLS;
  60841. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  60842. && !defined(WOLFSSL_NO_TLS12)
  60843. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60844. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60845. struct test_memio_ctx test_ctx;
  60846. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60847. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60848. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  60849. /* CH1 */
  60850. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60851. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60852. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  60853. test_ctx.s_len, 0), TEST_SUCCESS);
  60854. /* HVR */
  60855. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60856. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60857. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  60858. test_ctx.c_len, 0), TEST_SUCCESS);
  60859. /* CH2 */
  60860. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  60861. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  60862. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  60863. test_ctx.s_len, 1), TEST_SUCCESS);
  60864. /* Server first flight */
  60865. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  60866. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60867. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  60868. test_ctx.c_len, 1), TEST_SUCCESS);
  60869. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60870. wolfSSL_free(ssl_c);
  60871. wolfSSL_CTX_free(ctx_c);
  60872. wolfSSL_free(ssl_s);
  60873. wolfSSL_CTX_free(ctx_s);
  60874. #endif
  60875. return EXPECT_RESULT();
  60876. }
  60877. /**
  60878. * Make sure we don't send RSA Signature Hash Algorithms in the
  60879. * CertificateRequest when we don't have any such ciphers set.
  60880. * @return EXPECT_RESULT()
  60881. */
  60882. static int test_certreq_sighash_algos(void)
  60883. {
  60884. EXPECT_DECLS;
  60885. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60886. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  60887. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  60888. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  60889. WOLFSSL_CTX *ctx_c = NULL;
  60890. WOLFSSL_CTX *ctx_s = NULL;
  60891. WOLFSSL *ssl_c = NULL;
  60892. WOLFSSL *ssl_s = NULL;
  60893. struct test_memio_ctx test_ctx;
  60894. int idx = 0;
  60895. int maxIdx = 0;
  60896. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60897. test_ctx.c_ciphers = test_ctx.s_ciphers =
  60898. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  60899. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60900. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  60901. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  60902. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  60903. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  60904. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  60905. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60906. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  60907. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60908. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  60909. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60910. WOLFSSL_ERROR_WANT_READ);
  60911. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  60912. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60913. WOLFSSL_ERROR_WANT_READ);
  60914. /* Find the CertificateRequest message */
  60915. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  60916. word16 len;
  60917. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  60918. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  60919. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  60920. ato16(test_ctx.c_buff + idx, &len);
  60921. idx += OPAQUE16_LEN;
  60922. if (test_ctx.c_buff[idx] == certificate_request) {
  60923. idx++;
  60924. /* length */
  60925. idx += OPAQUE24_LEN;
  60926. /* cert types */
  60927. idx += 1 + test_ctx.c_buff[idx];
  60928. /* Sig algos */
  60929. ato16(test_ctx.c_buff + idx, &len);
  60930. idx += OPAQUE16_LEN;
  60931. maxIdx = idx + (int)len;
  60932. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  60933. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  60934. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  60935. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  60936. else
  60937. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  60938. }
  60939. break;
  60940. }
  60941. else {
  60942. idx += (int)len;
  60943. }
  60944. }
  60945. ExpectIntLT(idx, test_ctx.c_len);
  60946. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60947. wolfSSL_free(ssl_c);
  60948. wolfSSL_free(ssl_s);
  60949. wolfSSL_CTX_free(ctx_c);
  60950. wolfSSL_CTX_free(ctx_s);
  60951. #endif
  60952. return EXPECT_RESULT();
  60953. }
  60954. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60955. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  60956. {
  60957. EXPECT_DECLS;
  60958. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  60959. myVerifyAction = VERIFY_USE_PREVERFIY;
  60960. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60961. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60962. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60963. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60964. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  60965. WOLFSSL_SUCCESS);
  60966. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60967. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  60968. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60969. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60970. "./certs/crl/ca-int.pem",
  60971. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60972. return EXPECT_RESULT();
  60973. }
  60974. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  60975. {
  60976. EXPECT_DECLS;
  60977. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  60978. myVerifyAction = VERIFY_USE_PREVERFIY;
  60979. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60980. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60981. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60982. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60983. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  60984. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  60985. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  60986. WOLFSSL_SUCCESS);
  60987. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60988. "./certs/crl/ca-int2.pem",
  60989. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60990. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60991. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  60992. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60993. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  60994. "./certs/crl/ca-int.pem",
  60995. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60996. return EXPECT_RESULT();
  60997. }
  60998. #endif
  60999. static int test_revoked_loaded_int_cert(void)
  61000. {
  61001. EXPECT_DECLS;
  61002. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61003. test_ssl_cbf client_cbf;
  61004. test_ssl_cbf server_cbf;
  61005. struct {
  61006. const char* certPemFile;
  61007. const char* keyPemFile;
  61008. ctx_cb client_ctx_ready;
  61009. } test_params[] = {
  61010. {"./certs/intermediate/ca-int2-cert.pem",
  61011. "./certs/intermediate/ca-int2-key.pem",
  61012. test_revoked_loaded_int_cert_ctx_ready1},
  61013. {"./certs/intermediate/server-chain.pem",
  61014. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61015. {"./certs/intermediate/server-chain-short.pem",
  61016. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61017. };
  61018. size_t i;
  61019. printf("\n");
  61020. for (i = 0; i < XELEM_CNT(test_params); i++) {
  61021. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61022. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61023. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  61024. server_cbf.certPemFile = test_params[i].certPemFile;
  61025. server_cbf.keyPemFile = test_params[i].keyPemFile;
  61026. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  61027. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61028. &server_cbf, NULL), TEST_FAIL);
  61029. #ifndef WOLFSSL_HAPROXY
  61030. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  61031. #else
  61032. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  61033. #endif
  61034. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  61035. if (!EXPECT_SUCCESS())
  61036. break;
  61037. printf("\t%s passed\n", test_params[i].certPemFile);
  61038. }
  61039. #endif
  61040. return EXPECT_RESULT();
  61041. }
  61042. static int test_dtls13_frag_ch_pq(void)
  61043. {
  61044. EXPECT_DECLS;
  61045. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61046. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  61047. WOLFSSL_CTX *ctx_c = NULL;
  61048. WOLFSSL_CTX *ctx_s = NULL;
  61049. WOLFSSL *ssl_c = NULL;
  61050. WOLFSSL *ssl_s = NULL;
  61051. struct test_memio_ctx test_ctx;
  61052. const char *test_str = "test";
  61053. int test_str_size;
  61054. byte buf[255];
  61055. int group = WOLFSSL_KYBER_LEVEL5;
  61056. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61057. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61058. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61059. /* Add in a large post-quantum key share to make the CH long. */
  61060. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  61061. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  61062. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61063. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61064. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  61065. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  61066. test_str_size = XSTRLEN("test") + 1;
  61067. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61068. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61069. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61070. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61071. wolfSSL_free(ssl_c);
  61072. wolfSSL_free(ssl_s);
  61073. wolfSSL_CTX_free(ctx_c);
  61074. wolfSSL_CTX_free(ctx_s);
  61075. #endif
  61076. return EXPECT_RESULT();
  61077. }
  61078. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61079. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61080. static int test_dtls_frag_ch_count_records(byte* b, int len)
  61081. {
  61082. DtlsRecordLayerHeader* dtlsRH;
  61083. int records = 0;
  61084. size_t recordLen;
  61085. while (len > 0) {
  61086. records++;
  61087. dtlsRH = (DtlsRecordLayerHeader*)b;
  61088. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  61089. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  61090. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  61091. }
  61092. return records;
  61093. }
  61094. #endif
  61095. static int test_dtls_frag_ch(void)
  61096. {
  61097. EXPECT_DECLS;
  61098. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61099. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61100. WOLFSSL_CTX *ctx_c = NULL;
  61101. WOLFSSL_CTX *ctx_s = NULL;
  61102. WOLFSSL *ssl_c = NULL;
  61103. WOLFSSL *ssl_s = NULL;
  61104. struct test_memio_ctx test_ctx;
  61105. static unsigned int DUMMY_MTU = 256;
  61106. unsigned char four_frag_CH[] = {
  61107. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61108. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61109. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  61110. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  61111. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  61112. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61113. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61114. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61115. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61116. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  61117. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  61118. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  61119. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  61120. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  61121. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  61122. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  61123. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  61124. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  61125. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  61126. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61127. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  61128. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  61129. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  61130. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  61131. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  61132. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  61133. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  61134. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  61135. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  61136. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  61137. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  61138. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  61139. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  61140. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  61141. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  61142. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  61143. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  61144. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  61145. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  61146. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  61147. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  61148. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  61149. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  61150. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  61151. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  61152. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  61153. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  61154. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  61155. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  61156. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  61157. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  61158. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  61159. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  61160. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  61161. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  61162. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  61163. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  61164. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  61165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  61166. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  61167. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  61168. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  61169. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  61170. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  61171. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  61172. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  61173. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  61174. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  61175. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  61176. 0xa5, 0xcb, 0x68, 0xf3
  61177. };
  61178. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61179. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61180. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61181. /* Fragment msgs */
  61182. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  61183. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  61184. /* Add in some key shares to make the CH long */
  61185. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  61186. WOLFSSL_SUCCESS);
  61187. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  61188. WOLFSSL_SUCCESS);
  61189. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  61190. WOLFSSL_SUCCESS);
  61191. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  61192. WOLFSSL_SUCCESS);
  61193. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61194. /* Reject fragmented first CH */
  61195. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  61196. sizeof(four_frag_CH)), 4);
  61197. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  61198. test_ctx.s_len = sizeof(four_frag_CH);
  61199. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  61200. int s_len = test_ctx.s_len;
  61201. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61202. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61203. /* Fail if we didn't advance the buffer to avoid infinite loops */
  61204. ExpectIntLT(test_ctx.s_len, s_len);
  61205. }
  61206. /* Expect all fragments read */
  61207. ExpectIntEQ(test_ctx.s_len, 0);
  61208. /* Expect quietly dropping fragmented first CH */
  61209. ExpectIntEQ(test_ctx.c_len, 0);
  61210. /* CH1 */
  61211. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61212. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61213. /* Count records. Expect 1 unfragmented CH */
  61214. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61215. test_ctx.s_len), 1);
  61216. /* HRR */
  61217. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61218. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61219. /* CH2 */
  61220. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61221. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61222. /* Count records. Expect fragmented CH */
  61223. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61224. test_ctx.s_len), 1);
  61225. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61226. wolfSSL_free(ssl_c);
  61227. wolfSSL_free(ssl_s);
  61228. wolfSSL_CTX_free(ctx_c);
  61229. wolfSSL_CTX_free(ctx_s);
  61230. ssl_c = ssl_s = NULL;
  61231. ctx_c = ctx_s = NULL;
  61232. #endif
  61233. return EXPECT_RESULT();
  61234. }
  61235. static int test_dtls_empty_keyshare_with_cookie(void)
  61236. {
  61237. EXPECT_DECLS;
  61238. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61239. WOLFSSL_CTX *ctx_s = NULL;
  61240. WOLFSSL *ssl_s = NULL;
  61241. struct test_memio_ctx test_ctx;
  61242. unsigned char ch_empty_keyshare_with_cookie[] = {
  61243. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  61244. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  61245. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  61246. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  61247. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  61248. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61249. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61250. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61251. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61252. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  61253. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  61254. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  61255. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  61256. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  61257. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  61258. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  61259. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  61260. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  61261. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  61262. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  61263. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  61264. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  61265. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  61266. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  61267. };
  61268. DtlsRecordLayerHeader* dtlsRH;
  61269. byte sequence_number[8];
  61270. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61271. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61272. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  61273. sizeof(ch_empty_keyshare_with_cookie));
  61274. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  61275. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  61276. NULL, wolfDTLSv1_3_server_method), 0);
  61277. /* CH1 */
  61278. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61279. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61280. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  61281. ExpectIntEQ(test_ctx.c_len, 15);
  61282. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61283. ExpectIntEQ(dtlsRH->type, alert);
  61284. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61285. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61286. sequence_number[7] = 1;
  61287. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61288. sizeof(sequence_number)), 0);
  61289. ExpectIntEQ(dtlsRH->length[0], 0);
  61290. ExpectIntEQ(dtlsRH->length[1], 2);
  61291. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  61292. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  61293. wolfSSL_free(ssl_s);
  61294. wolfSSL_CTX_free(ctx_s);
  61295. #endif
  61296. return EXPECT_RESULT();
  61297. }
  61298. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61299. defined(HAVE_LIBOQS)
  61300. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  61301. {
  61302. int group = WOLFSSL_KYBER_LEVEL5;
  61303. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  61304. }
  61305. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  61306. {
  61307. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  61308. }
  61309. #endif
  61310. static int test_tls13_pq_groups(void)
  61311. {
  61312. EXPECT_DECLS;
  61313. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61314. defined(HAVE_LIBOQS)
  61315. callback_functions func_cb_client;
  61316. callback_functions func_cb_server;
  61317. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61318. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61319. func_cb_client.method = wolfTLSv1_3_client_method;
  61320. func_cb_server.method = wolfTLSv1_3_server_method;
  61321. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  61322. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  61323. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  61324. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61325. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61326. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61327. #endif
  61328. return EXPECT_RESULT();
  61329. }
  61330. static int test_tls13_early_data(void)
  61331. {
  61332. EXPECT_DECLS;
  61333. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61334. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  61335. int written = 0;
  61336. int read = 0;
  61337. size_t i;
  61338. int splitEarlyData;
  61339. char msg[] = "This is early data";
  61340. char msg2[] = "This is client data";
  61341. char msg3[] = "This is server data";
  61342. char msg4[] = "This is server immediate data";
  61343. char msgBuf[50];
  61344. struct {
  61345. method_provider client_meth;
  61346. method_provider server_meth;
  61347. const char* tls_version;
  61348. int isUdp;
  61349. } params[] = {
  61350. #ifdef WOLFSSL_TLS13
  61351. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61352. "TLS 1.3", 0 },
  61353. #endif
  61354. #ifdef WOLFSSL_DTLS13
  61355. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  61356. "DTLS 1.3", 1 },
  61357. #endif
  61358. };
  61359. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  61360. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  61361. struct test_memio_ctx test_ctx;
  61362. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61363. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61364. WOLFSSL_SESSION *sess = NULL;
  61365. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61366. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  61367. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  61368. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  61369. /* Get a ticket so that we can do 0-RTT on the next connection */
  61370. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61371. /* Make sure we read the ticket */
  61372. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  61373. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61374. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61375. wolfSSL_free(ssl_c);
  61376. ssl_c = NULL;
  61377. wolfSSL_free(ssl_s);
  61378. ssl_s = NULL;
  61379. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61380. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61381. params[i].client_meth, params[i].server_meth), 0);
  61382. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  61383. #ifdef WOLFSSL_DTLS13
  61384. if (params[i].isUdp) {
  61385. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  61386. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  61387. #else
  61388. /* Let's test this but we generally don't recommend turning off the
  61389. * cookie exchange */
  61390. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  61391. #endif
  61392. }
  61393. #endif
  61394. /* Test 0-RTT data */
  61395. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  61396. &written), sizeof(msg));
  61397. ExpectIntEQ(written, sizeof(msg));
  61398. if (splitEarlyData) {
  61399. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  61400. &written), sizeof(msg));
  61401. ExpectIntEQ(written, sizeof(msg));
  61402. }
  61403. /* Read first 0-RTT data (if split otherwise entire data) */
  61404. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  61405. &read), sizeof(msg));
  61406. ExpectIntEQ(read, sizeof(msg));
  61407. ExpectStrEQ(msg, msgBuf);
  61408. /* Test 0.5-RTT data */
  61409. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  61410. if (splitEarlyData) {
  61411. /* Read second 0-RTT data */
  61412. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  61413. &read), sizeof(msg));
  61414. ExpectIntEQ(read, sizeof(msg));
  61415. ExpectStrEQ(msg, msgBuf);
  61416. }
  61417. if (params[i].isUdp) {
  61418. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61419. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  61420. /* Read server 0.5-RTT data */
  61421. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  61422. ExpectStrEQ(msg4, msgBuf);
  61423. /* Complete handshake */
  61424. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61425. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61426. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  61427. * a user would loop until it is true but here we control both sides so we
  61428. * just assert the expected value. wolfSSL_read_early_data does not provide
  61429. * handshake status to us with non-blocking IO and we can't use
  61430. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  61431. * parsing logic. */
  61432. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  61433. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  61434. &read), 0);
  61435. ExpectIntEQ(read, 0);
  61436. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  61437. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61438. }
  61439. else {
  61440. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61441. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  61442. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  61443. &read), 0);
  61444. ExpectIntEQ(read, 0);
  61445. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  61446. /* Read server 0.5-RTT data */
  61447. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  61448. ExpectStrEQ(msg4, msgBuf);
  61449. }
  61450. /* Test bi-directional write */
  61451. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  61452. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  61453. ExpectStrEQ(msg2, msgBuf);
  61454. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  61455. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  61456. ExpectStrEQ(msg3, msgBuf);
  61457. ExpectTrue(wolfSSL_session_reused(ssl_c));
  61458. ExpectTrue(wolfSSL_session_reused(ssl_s));
  61459. wolfSSL_SESSION_free(sess);
  61460. wolfSSL_free(ssl_c);
  61461. wolfSSL_free(ssl_s);
  61462. wolfSSL_CTX_free(ctx_c);
  61463. wolfSSL_CTX_free(ctx_s);
  61464. }
  61465. }
  61466. #endif
  61467. return EXPECT_RESULT();
  61468. }
  61469. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  61470. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  61471. {
  61472. EXPECT_DECLS;
  61473. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  61474. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  61475. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  61476. return EXPECT_RESULT();
  61477. }
  61478. #endif
  61479. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  61480. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  61481. {
  61482. EXPECT_DECLS;
  61483. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  61484. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  61485. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  61486. return EXPECT_RESULT();
  61487. }
  61488. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  61489. {
  61490. EXPECT_DECLS;
  61491. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  61492. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  61493. 0), 1);
  61494. return EXPECT_RESULT();
  61495. }
  61496. #endif
  61497. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  61498. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  61499. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  61500. {
  61501. EXPECT_DECLS;
  61502. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  61503. return EXPECT_RESULT();
  61504. }
  61505. #endif
  61506. static int test_self_signed_stapling(void)
  61507. {
  61508. EXPECT_DECLS;
  61509. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  61510. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  61511. test_ssl_cbf client_cbf;
  61512. test_ssl_cbf server_cbf;
  61513. size_t i;
  61514. struct {
  61515. method_provider client_meth;
  61516. method_provider server_meth;
  61517. ctx_cb client_ctx;
  61518. const char* tls_version;
  61519. } params[] = {
  61520. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  61521. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61522. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  61523. #endif
  61524. #ifndef WOLFSSL_NO_TLS12
  61525. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  61526. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61527. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  61528. #endif
  61529. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  61530. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61531. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  61532. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61533. test_self_signed_stapling_client_v2_multi_ctx_ready,
  61534. "TLSv1_2 v2 multi" },
  61535. #endif
  61536. #endif
  61537. };
  61538. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  61539. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61540. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61541. printf("\nTesting self-signed cert with status request: %s\n",
  61542. params[i].tls_version);
  61543. client_cbf.method = params[i].client_meth;
  61544. client_cbf.ctx_ready = params[i].client_ctx;
  61545. server_cbf.method = params[i].server_meth;
  61546. server_cbf.certPemFile = "certs/ca-cert.pem";
  61547. server_cbf.keyPemFile = "certs/ca-key.pem";
  61548. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  61549. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61550. &server_cbf, NULL), TEST_SUCCESS);
  61551. }
  61552. #endif
  61553. return EXPECT_RESULT();
  61554. }
  61555. static int test_tls_multi_handshakes_one_record(void)
  61556. {
  61557. EXPECT_DECLS;
  61558. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  61559. struct test_memio_ctx test_ctx;
  61560. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61561. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61562. RecordLayerHeader* rh = NULL;
  61563. byte *len ;
  61564. int newRecIdx = RECORD_HEADER_SZ;
  61565. int idx = 0;
  61566. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61567. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61568. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  61569. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61570. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61571. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  61572. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61573. /* Combine server handshake msgs into one record */
  61574. while (idx < test_ctx.c_len) {
  61575. word16 recLen;
  61576. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  61577. len = &rh->length[0];
  61578. ato16((const byte*)len, &recLen);
  61579. idx += RECORD_HEADER_SZ;
  61580. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  61581. (size_t)recLen);
  61582. newRecIdx += recLen;
  61583. idx += recLen;
  61584. }
  61585. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  61586. len = &rh->length[0];
  61587. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  61588. test_ctx.c_len = newRecIdx;
  61589. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61590. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61591. wolfSSL_free(ssl_c);
  61592. wolfSSL_free(ssl_s);
  61593. wolfSSL_CTX_free(ctx_c);
  61594. wolfSSL_CTX_free(ctx_s);
  61595. #endif
  61596. return EXPECT_RESULT();
  61597. }
  61598. static int test_write_dup(void)
  61599. {
  61600. EXPECT_DECLS;
  61601. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  61602. size_t i, j;
  61603. char hiWorld[] = "dup message";
  61604. char readData[sizeof(hiWorld) + 5];
  61605. struct {
  61606. method_provider client_meth;
  61607. method_provider server_meth;
  61608. const char* version_name;
  61609. int version;
  61610. } methods[] = {
  61611. #ifndef WOLFSSL_NO_TLS12
  61612. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  61613. #endif
  61614. #ifdef WOLFSSL_TLS13
  61615. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  61616. #endif
  61617. };
  61618. struct {
  61619. const char* cipher;
  61620. int version;
  61621. } ciphers[] = {
  61622. /* For simplicity the macros are copied from internal.h */
  61623. /* TLS 1.2 */
  61624. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  61625. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  61626. #ifndef NO_RSA
  61627. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  61628. #endif
  61629. #endif
  61630. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  61631. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  61632. #endif
  61633. #endif
  61634. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  61635. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  61636. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61637. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  61638. #endif
  61639. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61640. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  61641. #endif
  61642. #endif
  61643. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  61644. && !defined(NO_TLS) && !defined(NO_AES)
  61645. #ifdef HAVE_AESGCM
  61646. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61647. #ifndef NO_RSA
  61648. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  61649. #endif
  61650. #endif
  61651. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61652. #ifndef NO_RSA
  61653. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  61654. #endif
  61655. #endif
  61656. #endif
  61657. #endif
  61658. /* TLS 1.3 */
  61659. #ifdef WOLFSSL_TLS13
  61660. #ifdef HAVE_AESGCM
  61661. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61662. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  61663. #endif
  61664. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61665. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  61666. #endif
  61667. #endif
  61668. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  61669. #ifndef NO_SHA256
  61670. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  61671. #endif
  61672. #endif
  61673. #ifdef HAVE_AESCCM
  61674. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  61675. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  61676. #endif
  61677. #endif
  61678. #endif
  61679. };
  61680. for (i = 0; i < XELEM_CNT(methods); i++) {
  61681. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  61682. struct test_memio_ctx test_ctx;
  61683. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61684. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61685. WOLFSSL *ssl_c2 = NULL;
  61686. if (methods[i].version != ciphers[j].version)
  61687. continue;
  61688. if (i == 0 && j == 0)
  61689. printf("\n");
  61690. printf("Testing %s with %s... ", methods[i].version_name,
  61691. ciphers[j].cipher);
  61692. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61693. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  61694. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61695. methods[i].client_meth, methods[i].server_meth), 0);
  61696. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61697. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  61698. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  61699. WRITE_DUP_WRITE_E);
  61700. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  61701. sizeof(hiWorld));
  61702. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  61703. sizeof(hiWorld));
  61704. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  61705. sizeof(hiWorld));
  61706. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  61707. WRITE_DUP_READ_E);
  61708. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  61709. sizeof(hiWorld));
  61710. if (EXPECT_SUCCESS())
  61711. printf("ok\n");
  61712. else
  61713. printf("failed\n");
  61714. wolfSSL_free(ssl_c);
  61715. wolfSSL_free(ssl_c2);
  61716. wolfSSL_free(ssl_s);
  61717. wolfSSL_CTX_free(ctx_c);
  61718. wolfSSL_CTX_free(ctx_s);
  61719. }
  61720. }
  61721. #endif
  61722. return EXPECT_RESULT();
  61723. }
  61724. static int test_read_write_hs(void)
  61725. {
  61726. EXPECT_DECLS;
  61727. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  61728. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  61729. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  61730. struct test_memio_ctx test_ctx;
  61731. uint8_t test_buffer[16];
  61732. unsigned int test;
  61733. /* test == 0 : client writes, server reads */
  61734. /* test == 1 : server writes, client reads */
  61735. for (test = 0; test < 2; test++) {
  61736. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61737. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61738. wolfTLSv1_2_client_method,
  61739. wolfTLSv1_2_server_method), 0);
  61740. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  61741. /* CH -> */
  61742. if (test == 0) {
  61743. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61744. } else {
  61745. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61746. sizeof(test_buffer)), -1);
  61747. }
  61748. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61749. /* <- SH + SKE + SHD */
  61750. if (test == 0) {
  61751. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61752. sizeof(test_buffer)), -1);
  61753. } else {
  61754. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  61755. }
  61756. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61757. /* -> CKE + CLIENT FINISHED */
  61758. if (test == 0) {
  61759. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61760. } else {
  61761. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61762. sizeof(test_buffer)), -1);
  61763. }
  61764. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61765. /* abide clang static analyzer */
  61766. if (ssl_s != NULL) {
  61767. /* disable group message to separate sending of ChangeCipherspec
  61768. * from Finished */
  61769. ssl_s->options.groupMessages = 0;
  61770. }
  61771. /* allow writing of CS, but not FINISHED */
  61772. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  61773. /* <- CS */
  61774. if (test == 0) {
  61775. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61776. sizeof(test_buffer)), -1);
  61777. } else {
  61778. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  61779. }
  61780. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  61781. /* move CS message where the client can read it */
  61782. memmove(test_ctx.c_buff,
  61783. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  61784. test_ctx.c_len = 6;
  61785. /* read CS */
  61786. if (test == 0) {
  61787. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  61788. } else {
  61789. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61790. sizeof(test_buffer)), -1);
  61791. }
  61792. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61793. ExpectIntEQ(test_ctx.c_len, 0);
  61794. if (test == 0) {
  61795. /* send SERVER FINISHED */
  61796. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61797. sizeof(test_buffer)), -1);
  61798. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  61799. WOLFSSL_ERROR_WANT_READ);
  61800. } else {
  61801. /* send SERVER FINISHED + App Data */
  61802. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  61803. }
  61804. ExpectIntGT(test_ctx.c_len, 0);
  61805. /* Send and receive the data */
  61806. if (test == 0) {
  61807. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  61808. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  61809. sizeof(test_buffer)), 5);
  61810. } else {
  61811. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  61812. sizeof(test_buffer)), 5);
  61813. }
  61814. ExpectBufEQ(test_buffer, "hello", 5);
  61815. wolfSSL_free(ssl_c);
  61816. wolfSSL_free(ssl_s);
  61817. wolfSSL_CTX_free(ctx_c);
  61818. wolfSSL_CTX_free(ctx_s);
  61819. ssl_c = ssl_s = NULL;
  61820. ctx_c = ctx_s = NULL;
  61821. }
  61822. #endif
  61823. return EXPECT_RESULT();
  61824. }
  61825. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  61826. static const char* test_get_signature_nid_siglag;
  61827. static int test_get_signature_nid_sig;
  61828. static int test_get_signature_nid_hash;
  61829. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  61830. {
  61831. EXPECT_DECLS;
  61832. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  61833. if (!wolfSSL_is_server(ssl)) {
  61834. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  61835. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  61836. }
  61837. return EXPECT_RESULT();
  61838. }
  61839. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  61840. {
  61841. EXPECT_DECLS;
  61842. int nid = 0;
  61843. (void)ctx;
  61844. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  61845. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61846. ExpectIntEQ(nid, test_get_signature_nid_sig);
  61847. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61848. ExpectIntEQ(nid, test_get_signature_nid_hash);
  61849. }
  61850. else /* No sigalg info on static ciphersuite */
  61851. return TEST_SUCCESS;
  61852. return EXPECT_RESULT();
  61853. }
  61854. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  61855. {
  61856. EXPECT_DECLS;
  61857. int nid = 0;
  61858. (void)ctx;
  61859. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61860. ExpectIntEQ(nid, test_get_signature_nid_sig);
  61861. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  61862. ExpectIntEQ(nid, test_get_signature_nid_hash);
  61863. return EXPECT_RESULT();
  61864. }
  61865. #endif
  61866. static int test_get_signature_nid(void)
  61867. {
  61868. EXPECT_DECLS;
  61869. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  61870. test_ssl_cbf client_cbf;
  61871. test_ssl_cbf server_cbf;
  61872. size_t i;
  61873. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  61874. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  61875. caCertFile }
  61876. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  61877. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  61878. caEccCertFile }
  61879. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  61880. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  61881. caCertFile }
  61882. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  61883. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  61884. caEccCertFile }
  61885. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  61886. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  61887. caEdCertFile }
  61888. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  61889. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  61890. caEd448CertFile }
  61891. struct {
  61892. const char* siglag;
  61893. int sig_nid;
  61894. int hash_nid;
  61895. int tls_ver;
  61896. const char* server_cert;
  61897. const char* server_key;
  61898. const char* client_ca;
  61899. } params[] = {
  61900. #ifndef NO_RSA
  61901. #ifndef NO_SHA256
  61902. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  61903. #ifdef WC_RSA_PSS
  61904. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  61905. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  61906. #endif
  61907. #endif
  61908. #ifdef WOLFSSL_SHA512
  61909. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  61910. #ifdef WC_RSA_PSS
  61911. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  61912. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  61913. #endif
  61914. #endif
  61915. #endif
  61916. #ifdef HAVE_ECC
  61917. #ifndef NO_SHA256
  61918. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  61919. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  61920. #endif
  61921. #endif
  61922. #ifdef HAVE_ED25519
  61923. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  61924. #endif
  61925. #ifdef HAVE_ED448
  61926. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  61927. #endif
  61928. };
  61929. printf("\n");
  61930. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  61931. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61932. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61933. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  61934. params[i].siglag);
  61935. switch (params[i].tls_ver) {
  61936. #ifndef WOLFSSL_NO_TLS12
  61937. case WOLFSSL_TLSV1_2:
  61938. client_cbf.method = wolfTLSv1_2_client_method;
  61939. server_cbf.method = wolfTLSv1_2_server_method;
  61940. break;
  61941. #endif
  61942. #ifdef WOLFSSL_TLS13
  61943. case WOLFSSL_TLSV1_3:
  61944. client_cbf.method = wolfTLSv1_3_client_method;
  61945. server_cbf.method = wolfTLSv1_3_server_method;
  61946. break;
  61947. #endif
  61948. default:
  61949. printf("skipping\n");
  61950. continue;
  61951. }
  61952. test_get_signature_nid_siglag = params[i].siglag;
  61953. test_get_signature_nid_sig = params[i].sig_nid;
  61954. test_get_signature_nid_hash = params[i].hash_nid;
  61955. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  61956. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  61957. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  61958. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  61959. server_cbf.certPemFile = params[i].server_cert;
  61960. server_cbf.keyPemFile = params[i].server_key;
  61961. client_cbf.caPemFile = params[i].client_ca;
  61962. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61963. &server_cbf, NULL), TEST_SUCCESS);
  61964. if (EXPECT_SUCCESS())
  61965. printf("passed\n");
  61966. }
  61967. #endif
  61968. return EXPECT_RESULT();
  61969. }
  61970. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61971. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  61972. {
  61973. #ifndef NO_MD5
  61974. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  61975. #elif !defined(NO_SHA)
  61976. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  61977. #elif !defined(NO_SHA256)
  61978. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  61979. #else
  61980. #error "We need a digest to hash the Signer object"
  61981. #endif
  61982. byte hashBuf[WC_MAX_DIGEST_SIZE];
  61983. wc_HashAlg hash;
  61984. size_t i;
  61985. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  61986. for (i = 0; i < CA_TABLE_SIZE; i++) {
  61987. Signer* cur;
  61988. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  61989. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  61990. sizeof(*cur)), 0);
  61991. }
  61992. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  61993. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  61994. return MakeWordFromHash(hashBuf);
  61995. }
  61996. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  61997. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  61998. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  61999. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  62000. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  62001. {
  62002. EXPECT_DECLS;
  62003. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  62004. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  62005. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62006. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  62007. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  62008. return EXPECT_RESULT();
  62009. }
  62010. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  62011. {
  62012. EXPECT_DECLS;
  62013. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  62014. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62015. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62016. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62017. return EXPECT_RESULT();
  62018. }
  62019. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62020. {
  62021. EXPECT_DECLS;
  62022. WOLFSSL_CERT_MANAGER* cm;
  62023. (void)ssl;
  62024. /* WARNING: this approach bypasses the reference counter check in
  62025. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  62026. * cause unexpected behaviour when other active connections try accessing
  62027. * the caTable. */
  62028. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  62029. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  62030. WOLFSSL_SUCCESS);
  62031. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62032. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62033. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  62034. return EXPECT_RESULT();
  62035. }
  62036. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  62037. {
  62038. EXPECT_DECLS;
  62039. WOLFSSL_CTX* ctx;
  62040. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  62041. return EXPECT_RESULT();
  62042. }
  62043. #endif
  62044. static int test_tls_cert_store_unchanged(void)
  62045. {
  62046. EXPECT_DECLS;
  62047. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62048. test_ssl_cbf client_cbf;
  62049. test_ssl_cbf server_cbf;
  62050. int i;
  62051. for (i = 0; i < 2; i++) {
  62052. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62053. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62054. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  62055. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  62056. sizeof(test_tls_cert_store_unchanged_before_hashes));
  62057. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  62058. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  62059. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62060. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62061. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62062. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62063. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62064. switch (i) {
  62065. case 0:
  62066. client_cbf.on_ctx_cleanup =
  62067. test_tls_cert_store_unchanged_ctx_cleanup;
  62068. server_cbf.on_ctx_cleanup =
  62069. test_tls_cert_store_unchanged_ctx_cleanup;
  62070. break;
  62071. case 1:
  62072. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62073. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62074. break;
  62075. default:
  62076. Fail(("Should not enter here"), ("Entered here"));
  62077. }
  62078. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  62079. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  62080. server_cbf.caPemFile = caCertFile;
  62081. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62082. &server_cbf, NULL), TEST_SUCCESS);
  62083. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  62084. test_tls_cert_store_unchanged_after_hashes,
  62085. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62086. }
  62087. #endif
  62088. return EXPECT_RESULT();
  62089. }
  62090. /*----------------------------------------------------------------------------*
  62091. | Main
  62092. *----------------------------------------------------------------------------*/
  62093. typedef int (*TEST_FUNC)(void);
  62094. typedef struct {
  62095. const char *name;
  62096. TEST_FUNC func;
  62097. byte run:1;
  62098. byte fail:1;
  62099. } TEST_CASE;
  62100. #define TEST_DECL(func) { #func, func, 0, 0 }
  62101. int testAll = 1;
  62102. TEST_CASE testCases[] = {
  62103. TEST_DECL(test_fileAccess),
  62104. /*********************************
  62105. * wolfcrypt
  62106. *********************************/
  62107. TEST_DECL(test_ForceZero),
  62108. TEST_DECL(test_wolfCrypt_Init),
  62109. /* Locking with Compat Mutex */
  62110. TEST_DECL(test_wc_SetMutexCb),
  62111. TEST_DECL(test_wc_LockMutex_ex),
  62112. /* Digests */
  62113. TEST_DECL(test_wc_InitMd5),
  62114. TEST_DECL(test_wc_Md5Update),
  62115. TEST_DECL(test_wc_Md5Final),
  62116. TEST_DECL(test_wc_InitSha),
  62117. TEST_DECL(test_wc_ShaUpdate),
  62118. TEST_DECL(test_wc_ShaFinal),
  62119. TEST_DECL(test_wc_InitSha256),
  62120. TEST_DECL(test_wc_Sha256Update),
  62121. TEST_DECL(test_wc_Sha256Final),
  62122. TEST_DECL(test_wc_Sha256FinalRaw),
  62123. TEST_DECL(test_wc_Sha256GetFlags),
  62124. TEST_DECL(test_wc_Sha256Free),
  62125. TEST_DECL(test_wc_Sha256GetHash),
  62126. TEST_DECL(test_wc_Sha256Copy),
  62127. TEST_DECL(test_wc_InitSha224),
  62128. TEST_DECL(test_wc_Sha224Update),
  62129. TEST_DECL(test_wc_Sha224Final),
  62130. TEST_DECL(test_wc_Sha224SetFlags),
  62131. TEST_DECL(test_wc_Sha224GetFlags),
  62132. TEST_DECL(test_wc_Sha224Free),
  62133. TEST_DECL(test_wc_Sha224GetHash),
  62134. TEST_DECL(test_wc_Sha224Copy),
  62135. TEST_DECL(test_wc_InitSha512),
  62136. TEST_DECL(test_wc_Sha512Update),
  62137. TEST_DECL(test_wc_Sha512Final),
  62138. TEST_DECL(test_wc_Sha512GetFlags),
  62139. TEST_DECL(test_wc_Sha512FinalRaw),
  62140. TEST_DECL(test_wc_Sha512Free),
  62141. TEST_DECL(test_wc_Sha512GetHash),
  62142. TEST_DECL(test_wc_Sha512Copy),
  62143. TEST_DECL(test_wc_InitSha512_224),
  62144. TEST_DECL(test_wc_Sha512_224Update),
  62145. TEST_DECL(test_wc_Sha512_224Final),
  62146. TEST_DECL(test_wc_Sha512_224GetFlags),
  62147. TEST_DECL(test_wc_Sha512_224FinalRaw),
  62148. TEST_DECL(test_wc_Sha512_224Free),
  62149. TEST_DECL(test_wc_Sha512_224GetHash),
  62150. TEST_DECL(test_wc_Sha512_224Copy),
  62151. TEST_DECL(test_wc_InitSha512_256),
  62152. TEST_DECL(test_wc_Sha512_256Update),
  62153. TEST_DECL(test_wc_Sha512_256Final),
  62154. TEST_DECL(test_wc_Sha512_256GetFlags),
  62155. TEST_DECL(test_wc_Sha512_256FinalRaw),
  62156. TEST_DECL(test_wc_Sha512_256Free),
  62157. TEST_DECL(test_wc_Sha512_256GetHash),
  62158. TEST_DECL(test_wc_Sha512_256Copy),
  62159. TEST_DECL(test_wc_InitSha384),
  62160. TEST_DECL(test_wc_Sha384Update),
  62161. TEST_DECL(test_wc_Sha384Final),
  62162. TEST_DECL(test_wc_Sha384GetFlags),
  62163. TEST_DECL(test_wc_Sha384FinalRaw),
  62164. TEST_DECL(test_wc_Sha384Free),
  62165. TEST_DECL(test_wc_Sha384GetHash),
  62166. TEST_DECL(test_wc_Sha384Copy),
  62167. TEST_DECL(test_wc_InitBlake2b),
  62168. TEST_DECL(test_wc_InitBlake2b_WithKey),
  62169. TEST_DECL(test_wc_InitBlake2s_WithKey),
  62170. TEST_DECL(test_wc_InitRipeMd),
  62171. TEST_DECL(test_wc_RipeMdUpdate),
  62172. TEST_DECL(test_wc_RipeMdFinal),
  62173. TEST_DECL(test_wc_InitSha3),
  62174. TEST_DECL(testing_wc_Sha3_Update),
  62175. TEST_DECL(test_wc_Sha3_224_Final),
  62176. TEST_DECL(test_wc_Sha3_256_Final),
  62177. TEST_DECL(test_wc_Sha3_384_Final),
  62178. TEST_DECL(test_wc_Sha3_512_Final),
  62179. TEST_DECL(test_wc_Sha3_224_Copy),
  62180. TEST_DECL(test_wc_Sha3_256_Copy),
  62181. TEST_DECL(test_wc_Sha3_384_Copy),
  62182. TEST_DECL(test_wc_Sha3_512_Copy),
  62183. TEST_DECL(test_wc_Sha3_GetFlags),
  62184. TEST_DECL(test_wc_InitShake256),
  62185. TEST_DECL(testing_wc_Shake256_Update),
  62186. TEST_DECL(test_wc_Shake256_Final),
  62187. TEST_DECL(test_wc_Shake256_Copy),
  62188. TEST_DECL(test_wc_Shake256Hash),
  62189. /* SM3 Digest */
  62190. TEST_DECL(test_wc_InitSm3Free),
  62191. TEST_DECL(test_wc_Sm3UpdateFinal),
  62192. TEST_DECL(test_wc_Sm3GetHash),
  62193. TEST_DECL(test_wc_Sm3Copy),
  62194. TEST_DECL(test_wc_Sm3FinalRaw),
  62195. TEST_DECL(test_wc_Sm3GetSetFlags),
  62196. TEST_DECL(test_wc_Sm3Hash),
  62197. TEST_DECL(test_wc_HashInit),
  62198. TEST_DECL(test_wc_HashSetFlags),
  62199. TEST_DECL(test_wc_HashGetFlags),
  62200. /* HMAC */
  62201. TEST_DECL(test_wc_Md5HmacSetKey),
  62202. TEST_DECL(test_wc_Md5HmacUpdate),
  62203. TEST_DECL(test_wc_Md5HmacFinal),
  62204. TEST_DECL(test_wc_ShaHmacSetKey),
  62205. TEST_DECL(test_wc_ShaHmacUpdate),
  62206. TEST_DECL(test_wc_ShaHmacFinal),
  62207. TEST_DECL(test_wc_Sha224HmacSetKey),
  62208. TEST_DECL(test_wc_Sha224HmacUpdate),
  62209. TEST_DECL(test_wc_Sha224HmacFinal),
  62210. TEST_DECL(test_wc_Sha256HmacSetKey),
  62211. TEST_DECL(test_wc_Sha256HmacUpdate),
  62212. TEST_DECL(test_wc_Sha256HmacFinal),
  62213. TEST_DECL(test_wc_Sha384HmacSetKey),
  62214. TEST_DECL(test_wc_Sha384HmacUpdate),
  62215. TEST_DECL(test_wc_Sha384HmacFinal),
  62216. /* CMAC */
  62217. TEST_DECL(test_wc_InitCmac),
  62218. TEST_DECL(test_wc_CmacUpdate),
  62219. TEST_DECL(test_wc_CmacFinal),
  62220. TEST_DECL(test_wc_AesCmacGenerate),
  62221. /* Cipher */
  62222. TEST_DECL(test_wc_AesGcmStream),
  62223. TEST_DECL(test_wc_Des3_SetIV),
  62224. TEST_DECL(test_wc_Des3_SetKey),
  62225. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  62226. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  62227. TEST_DECL(test_wc_Des3_EcbEncrypt),
  62228. TEST_DECL(test_wc_Chacha_SetKey),
  62229. TEST_DECL(test_wc_Chacha_Process),
  62230. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  62231. TEST_DECL(test_wc_Poly1305SetKey),
  62232. TEST_DECL(test_wc_CamelliaSetKey),
  62233. TEST_DECL(test_wc_CamelliaSetIV),
  62234. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  62235. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  62236. TEST_DECL(test_wc_Arc4SetKey),
  62237. TEST_DECL(test_wc_Arc4Process),
  62238. TEST_DECL(test_wc_Rc2SetKey),
  62239. TEST_DECL(test_wc_Rc2SetIV),
  62240. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  62241. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  62242. /* AES cipher and GMAC. */
  62243. TEST_DECL(test_wc_AesSetKey),
  62244. TEST_DECL(test_wc_AesSetIV),
  62245. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  62246. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  62247. TEST_DECL(test_wc_AesGcmSetKey),
  62248. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  62249. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  62250. TEST_DECL(test_wc_GmacSetKey),
  62251. TEST_DECL(test_wc_GmacUpdate),
  62252. TEST_DECL(test_wc_AesCcmSetKey),
  62253. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  62254. #if defined(WOLFSSL_AES_EAX) && \
  62255. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  62256. TEST_DECL(test_wc_AesEaxVectors),
  62257. TEST_DECL(test_wc_AesEaxEncryptAuth),
  62258. TEST_DECL(test_wc_AesEaxDecryptAuth),
  62259. #endif /* WOLFSSL_AES_EAX */
  62260. /* SM4 cipher */
  62261. TEST_DECL(test_wc_Sm4),
  62262. TEST_DECL(test_wc_Sm4Ecb),
  62263. TEST_DECL(test_wc_Sm4Cbc),
  62264. TEST_DECL(test_wc_Sm4Ctr),
  62265. TEST_DECL(test_wc_Sm4Gcm),
  62266. TEST_DECL(test_wc_Sm4Ccm),
  62267. /* RNG tests */
  62268. #ifdef HAVE_HASHDRBG
  62269. #ifdef TEST_RESEED_INTERVAL
  62270. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  62271. #endif
  62272. TEST_DECL(test_wc_RNG_GenerateBlock),
  62273. #endif
  62274. TEST_DECL(test_get_rand_digit),
  62275. TEST_DECL(test_wc_InitRngNonce),
  62276. TEST_DECL(test_wc_InitRngNonce_ex),
  62277. /* MP API tests */
  62278. TEST_DECL(test_get_digit_count),
  62279. TEST_DECL(test_mp_cond_copy),
  62280. TEST_DECL(test_mp_rand),
  62281. TEST_DECL(test_get_digit),
  62282. TEST_DECL(test_wc_export_int),
  62283. /* RSA */
  62284. TEST_DECL(test_wc_InitRsaKey),
  62285. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  62286. TEST_DECL(test_wc_RsaPublicKeyDecode),
  62287. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  62288. TEST_DECL(test_wc_MakeRsaKey),
  62289. TEST_DECL(test_wc_CheckProbablePrime),
  62290. TEST_DECL(test_wc_RsaPSS_Verify),
  62291. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  62292. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  62293. TEST_DECL(test_wc_RsaKeyToDer),
  62294. TEST_DECL(test_wc_RsaKeyToPublicDer),
  62295. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  62296. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  62297. TEST_DECL(test_wc_RsaEncryptSize),
  62298. TEST_DECL(test_wc_RsaSSL_SignVerify),
  62299. TEST_DECL(test_wc_RsaFlattenPublicKey),
  62300. TEST_DECL(test_RsaDecryptBoundsCheck),
  62301. /* DSA */
  62302. TEST_DECL(test_wc_InitDsaKey),
  62303. TEST_DECL(test_wc_DsaSignVerify),
  62304. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  62305. TEST_DECL(test_wc_MakeDsaKey),
  62306. TEST_DECL(test_wc_DsaKeyToDer),
  62307. TEST_DECL(test_wc_DsaKeyToPublicDer),
  62308. TEST_DECL(test_wc_DsaImportParamsRaw),
  62309. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  62310. TEST_DECL(test_wc_DsaExportParamsRaw),
  62311. TEST_DECL(test_wc_DsaExportKeyRaw),
  62312. /* DH */
  62313. TEST_DECL(test_wc_DhPublicKeyDecode),
  62314. /* wolfCrypt ECC tests */
  62315. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  62316. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  62317. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  62318. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  62319. !defined(HAVE_SELFTEST) && \
  62320. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  62321. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  62322. #endif
  62323. TEST_DECL(test_wc_ecc_make_key),
  62324. TEST_DECL(test_wc_ecc_init),
  62325. TEST_DECL(test_wc_ecc_check_key),
  62326. TEST_DECL(test_wc_ecc_get_generator),
  62327. TEST_DECL(test_wc_ecc_size),
  62328. TEST_DECL(test_wc_ecc_params),
  62329. TEST_DECL(test_wc_ecc_signVerify_hash),
  62330. TEST_DECL(test_wc_ecc_shared_secret),
  62331. TEST_DECL(test_wc_ecc_export_x963),
  62332. TEST_DECL(test_wc_ecc_export_x963_ex),
  62333. TEST_DECL(test_wc_ecc_import_x963),
  62334. TEST_DECL(test_wc_ecc_import_private_key),
  62335. TEST_DECL(test_wc_ecc_export_private_only),
  62336. TEST_DECL(test_wc_ecc_rs_to_sig),
  62337. TEST_DECL(test_wc_ecc_import_raw),
  62338. TEST_DECL(test_wc_ecc_import_unsigned),
  62339. TEST_DECL(test_wc_ecc_sig_size),
  62340. TEST_DECL(test_wc_ecc_ctx_new),
  62341. TEST_DECL(test_wc_ecc_ctx_reset),
  62342. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  62343. TEST_DECL(test_wc_ecc_ctx_set_info),
  62344. TEST_DECL(test_wc_ecc_encryptDecrypt),
  62345. TEST_DECL(test_wc_ecc_del_point),
  62346. TEST_DECL(test_wc_ecc_pointFns),
  62347. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  62348. TEST_DECL(test_wc_ecc_verify_hash_ex),
  62349. TEST_DECL(test_wc_ecc_mulmod),
  62350. TEST_DECL(test_wc_ecc_is_valid_idx),
  62351. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  62352. TEST_DECL(test_wc_ecc_sig_size_calc),
  62353. TEST_DECL(test_wc_EccPrivateKeyToDer),
  62354. /* SM2 elliptic curve */
  62355. TEST_DECL(test_wc_ecc_sm2_make_key),
  62356. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  62357. TEST_DECL(test_wc_ecc_sm2_create_digest),
  62358. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  62359. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  62360. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  62361. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  62362. /* Curve25519 */
  62363. TEST_DECL(test_wc_curve25519_init),
  62364. TEST_DECL(test_wc_curve25519_size),
  62365. TEST_DECL(test_wc_curve25519_export_key_raw),
  62366. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  62367. TEST_DECL(test_wc_curve25519_make_key),
  62368. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  62369. TEST_DECL(test_wc_curve25519_make_pub),
  62370. TEST_DECL(test_wc_curve25519_export_public_ex),
  62371. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  62372. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  62373. TEST_DECL(test_wc_curve25519_import_private),
  62374. /* ED25519 */
  62375. TEST_DECL(test_wc_ed25519_make_key),
  62376. TEST_DECL(test_wc_ed25519_init),
  62377. TEST_DECL(test_wc_ed25519_sign_msg),
  62378. TEST_DECL(test_wc_ed25519_import_public),
  62379. TEST_DECL(test_wc_ed25519_import_private_key),
  62380. TEST_DECL(test_wc_ed25519_export),
  62381. TEST_DECL(test_wc_ed25519_size),
  62382. TEST_DECL(test_wc_ed25519_exportKey),
  62383. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  62384. TEST_DECL(test_wc_Ed25519KeyToDer),
  62385. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  62386. /* Curve448 */
  62387. TEST_DECL(test_wc_curve448_make_key),
  62388. TEST_DECL(test_wc_curve448_shared_secret_ex),
  62389. TEST_DECL(test_wc_curve448_export_public_ex),
  62390. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  62391. TEST_DECL(test_wc_curve448_export_key_raw),
  62392. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  62393. TEST_DECL(test_wc_curve448_import_private),
  62394. TEST_DECL(test_wc_curve448_init),
  62395. TEST_DECL(test_wc_curve448_size),
  62396. /* Ed448 */
  62397. TEST_DECL(test_wc_ed448_make_key),
  62398. TEST_DECL(test_wc_ed448_init),
  62399. TEST_DECL(test_wc_ed448_sign_msg),
  62400. TEST_DECL(test_wc_ed448_import_public),
  62401. TEST_DECL(test_wc_ed448_import_private_key),
  62402. TEST_DECL(test_wc_ed448_export),
  62403. TEST_DECL(test_wc_ed448_size),
  62404. TEST_DECL(test_wc_ed448_exportKey),
  62405. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  62406. TEST_DECL(test_wc_Ed448KeyToDer),
  62407. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  62408. /* Signature API */
  62409. TEST_DECL(test_wc_SignatureGetSize_ecc),
  62410. TEST_DECL(test_wc_SignatureGetSize_rsa),
  62411. /* PEM and DER APIs. */
  62412. TEST_DECL(test_wc_PemToDer),
  62413. TEST_DECL(test_wc_AllocDer),
  62414. TEST_DECL(test_wc_CertPemToDer),
  62415. TEST_DECL(test_wc_KeyPemToDer),
  62416. TEST_DECL(test_wc_PubKeyPemToDer),
  62417. TEST_DECL(test_wc_PemPubKeyToDer),
  62418. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  62419. TEST_DECL(test_wc_CheckCertSigPubKey),
  62420. /* wolfCrypt ASN tests */
  62421. TEST_DECL(test_ToTraditional),
  62422. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  62423. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  62424. /* Certificate */
  62425. TEST_DECL(test_wc_SetSubjectRaw),
  62426. TEST_DECL(test_wc_GetSubjectRaw),
  62427. TEST_DECL(test_wc_SetIssuerRaw),
  62428. TEST_DECL(test_wc_SetIssueBuffer),
  62429. TEST_DECL(test_wc_SetSubjectKeyId),
  62430. TEST_DECL(test_wc_SetSubject),
  62431. TEST_DECL(test_CheckCertSignature),
  62432. TEST_DECL(test_wc_ParseCert),
  62433. TEST_DECL(test_wc_ParseCert_Error),
  62434. TEST_DECL(test_MakeCertWithPathLen),
  62435. TEST_DECL(test_MakeCertWithCaFalse),
  62436. TEST_DECL(test_wc_SetKeyUsage),
  62437. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  62438. TEST_DECL(test_wc_SetSubjectBuffer),
  62439. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  62440. /* wolfcrypt PKCS#7 */
  62441. TEST_DECL(test_wc_PKCS7_New),
  62442. TEST_DECL(test_wc_PKCS7_Init),
  62443. TEST_DECL(test_wc_PKCS7_InitWithCert),
  62444. TEST_DECL(test_wc_PKCS7_EncodeData),
  62445. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  62446. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  62447. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  62448. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  62449. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  62450. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  62451. TEST_DECL(test_wc_PKCS7_Degenerate),
  62452. TEST_DECL(test_wc_PKCS7_BER),
  62453. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  62454. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  62455. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  62456. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  62457. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  62458. /* wolfCrypt PKCS#12 */
  62459. TEST_DECL(test_wc_i2d_PKCS12),
  62460. /*
  62461. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  62462. * avoid memory leaks.
  62463. */
  62464. TEST_DECL(test_wolfCrypt_Cleanup),
  62465. TEST_DECL(test_wolfSSL_Init),
  62466. TEST_DECL(test_dual_alg_support),
  62467. /*********************************
  62468. * OpenSSL compatibility API tests
  62469. *********************************/
  62470. /* If at some point a stub get implemented this test should fail indicating
  62471. * a need to implement a new test case
  62472. */
  62473. TEST_DECL(test_stubs_are_stubs),
  62474. /* ASN.1 compatibility API tests */
  62475. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  62476. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  62477. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  62478. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  62479. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  62480. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  62481. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  62482. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  62483. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  62484. TEST_DECL(test_wolfSSL_ASN1_get_object),
  62485. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  62486. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  62487. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  62488. TEST_DECL(test_wolfSSL_ASN1_STRING),
  62489. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  62490. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  62491. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  62492. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  62493. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  62494. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  62495. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  62496. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  62497. TEST_DECL(test_wolfSSL_ASN1_TIME),
  62498. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  62499. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  62500. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  62501. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  62502. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  62503. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  62504. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  62505. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  62506. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  62507. TEST_DECL(test_wolfSSL_lhash),
  62508. TEST_DECL(test_wolfSSL_certs),
  62509. TEST_DECL(test_wolfSSL_private_keys),
  62510. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  62511. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  62512. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  62513. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  62514. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  62515. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  62516. #ifndef NO_BIO
  62517. TEST_DECL(test_wolfSSL_BIO),
  62518. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  62519. TEST_DECL(test_wolfSSL_PEM_read_bio),
  62520. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  62521. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  62522. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  62523. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  62524. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  62525. #endif
  62526. /* EVP API testing */
  62527. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  62528. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  62529. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  62530. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  62531. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  62532. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  62533. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  62534. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  62535. TEST_DECL(test_wolfSSL_EVP_shake128),
  62536. TEST_DECL(test_wolfSSL_EVP_shake256),
  62537. TEST_DECL(test_wolfSSL_EVP_sm3),
  62538. TEST_DECL(test_EVP_blake2),
  62539. #ifdef OPENSSL_ALL
  62540. TEST_DECL(test_wolfSSL_EVP_md4),
  62541. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  62542. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  62543. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  62544. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  62545. #endif
  62546. TEST_DECL(test_EVP_MD_do_all),
  62547. TEST_DECL(test_wolfSSL_EVP_MD_size),
  62548. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  62549. TEST_DECL(test_wolfSSL_EVP_Digest),
  62550. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  62551. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  62552. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  62553. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  62554. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  62555. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  62556. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  62557. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  62558. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  62559. TEST_DECL(test_wolfssl_EVP_chacha20),
  62560. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  62561. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  62562. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  62563. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  62564. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  62565. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  62566. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  62567. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  62568. #ifdef OPENSSL_ALL
  62569. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  62570. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  62571. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  62572. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  62573. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  62574. TEST_DECL(test_wolfSSL_EVP_rc4),
  62575. TEST_DECL(test_wolfSSL_EVP_enc_null),
  62576. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  62577. TEST_DECL(test_wolfSSL_EVP_mdc2),
  62578. TEST_DECL(test_evp_cipher_aes_gcm),
  62579. #endif
  62580. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  62581. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  62582. #ifdef OPENSSL_EXTRA
  62583. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  62584. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  62585. #endif
  62586. #ifdef OPENSSL_ALL
  62587. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  62588. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  62589. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  62590. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  62591. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  62592. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  62593. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  62594. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  62595. #endif
  62596. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  62597. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  62598. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  62599. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  62600. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  62601. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  62602. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  62603. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  62604. #ifndef NO_BIO
  62605. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  62606. #endif
  62607. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  62608. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  62609. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  62610. #ifndef NO_BIO
  62611. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  62612. #endif /* !NO_BIO */
  62613. #endif
  62614. #ifdef OPENSSL_ALL
  62615. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  62616. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  62617. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  62618. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  62619. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  62620. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  62621. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  62622. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  62623. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  62624. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  62625. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  62626. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  62627. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  62628. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  62629. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  62630. #endif
  62631. TEST_DECL(test_EVP_PKEY_rsa),
  62632. TEST_DECL(test_EVP_PKEY_ec),
  62633. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  62634. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  62635. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  62636. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  62637. TEST_DECL(test_EVP_PKEY_cmp),
  62638. #ifdef OPENSSL_ALL
  62639. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  62640. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  62641. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  62642. #endif
  62643. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  62644. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  62645. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  62646. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  62647. #endif
  62648. #ifndef NO_BIO
  62649. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  62650. TEST_DECL(test_wolfSSL_GetLoggingCb),
  62651. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  62652. TEST_DECL(test_wc_ERR_remove_state),
  62653. TEST_DECL(test_wc_ERR_print_errors_fp),
  62654. #endif
  62655. TEST_DECL(test_wolfSSL_configure_args),
  62656. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  62657. TEST_DECL(test_wolfSSL_set1_curves_list),
  62658. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  62659. TEST_DECL(test_wolfSSL_OtherName),
  62660. TEST_DECL(test_wolfSSL_FPKI),
  62661. TEST_DECL(test_wolfSSL_URI),
  62662. TEST_DECL(test_wolfSSL_TBS),
  62663. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  62664. TEST_DECL(test_X509_STORE_untrusted),
  62665. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  62666. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  62667. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  62668. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  62669. TEST_DECL(test_wolfSSL_X509_Name_canon),
  62670. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  62671. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  62672. TEST_DECL(test_wolfSSL_X509_NID),
  62673. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  62674. TEST_DECL(test_wolfSSL_get0_param),
  62675. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  62676. TEST_DECL(test_wolfSSL_set1_host),
  62677. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  62678. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  62679. TEST_DECL(test_wolfSSL_X509_STORE),
  62680. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  62681. TEST_DECL(test_X509_STORE_get0_objects),
  62682. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  62683. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  62684. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  62685. TEST_DECL(test_wolfSSL_X509_cmp_time),
  62686. TEST_DECL(test_wolfSSL_X509_time_adj),
  62687. /* X509 tests */
  62688. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  62689. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  62690. TEST_DECL(test_wolfSSL_X509_check_host),
  62691. TEST_DECL(test_wolfSSL_X509_check_email),
  62692. TEST_DECL(test_wolfSSL_X509_check_private_key),
  62693. TEST_DECL(test_wolfSSL_X509),
  62694. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  62695. TEST_DECL(test_wolfSSL_X509_sign),
  62696. TEST_DECL(test_wolfSSL_X509_sign2),
  62697. TEST_DECL(test_wolfSSL_X509_verify),
  62698. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  62699. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  62700. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  62701. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  62702. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  62703. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  62704. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  62705. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  62706. TEST_DECL(test_wolfSSL_X509_NAME),
  62707. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  62708. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  62709. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  62710. TEST_DECL(test_wolfSSL_X509_set_name),
  62711. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  62712. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  62713. TEST_DECL(test_wolfSSL_X509_set_version),
  62714. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  62715. TEST_DECL(test_wolfSSL_X509_CRL),
  62716. TEST_DECL(test_wolfSSL_i2d_X509),
  62717. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  62718. TEST_DECL(test_wolfSSL_PEM_read_X509),
  62719. TEST_DECL(test_wolfSSL_X509_check_ca),
  62720. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  62721. TEST_DECL(test_wolfSSL_make_cert),
  62722. #ifndef NO_BIO
  62723. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  62724. TEST_DECL(test_wolfSSL_X509_INFO),
  62725. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  62726. #endif
  62727. #ifdef OPENSSL_ALL
  62728. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  62729. #endif
  62730. TEST_DECL(test_wolfSSL_X509_CA_num),
  62731. TEST_DECL(test_wolfSSL_X509_get_version),
  62732. #ifndef NO_BIO
  62733. TEST_DECL(test_wolfSSL_X509_print),
  62734. TEST_DECL(test_wolfSSL_X509_CRL_print),
  62735. #endif
  62736. TEST_DECL(test_X509_get_signature_nid),
  62737. /* X509 extension testing. */
  62738. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  62739. TEST_DECL(test_wolfSSL_X509_get_ext),
  62740. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  62741. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  62742. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  62743. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  62744. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  62745. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  62746. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  62747. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  62748. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  62749. TEST_DECL(test_wolfSSL_X509V3_EXT),
  62750. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  62751. TEST_DECL(test_wolfSSL_X509_cmp),
  62752. TEST_DECL(test_GENERAL_NAME_set0_othername),
  62753. TEST_DECL(test_othername_and_SID_ext),
  62754. TEST_DECL(test_wolfSSL_dup_CA_list),
  62755. /* OpenSSL sk_X509 API test */
  62756. TEST_DECL(test_sk_X509),
  62757. /* OpenSSL sk_X509_CRL API test */
  62758. TEST_DECL(test_sk_X509_CRL),
  62759. /* OpenSSL X509 REQ API test */
  62760. TEST_DECL(test_X509_REQ),
  62761. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  62762. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  62763. TEST_DECL(test_X509_LOOKUP_add_dir),
  62764. /* RAND compatibility API */
  62765. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  62766. TEST_DECL(test_wolfSSL_RAND_bytes),
  62767. TEST_DECL(test_wolfSSL_RAND),
  62768. /* BN compatibility API */
  62769. TEST_DECL(test_wolfSSL_BN_CTX),
  62770. TEST_DECL(test_wolfSSL_BN),
  62771. TEST_DECL(test_wolfSSL_BN_init),
  62772. TEST_DECL(test_wolfSSL_BN_enc_dec),
  62773. TEST_DECL(test_wolfSSL_BN_word),
  62774. TEST_DECL(test_wolfSSL_BN_bits),
  62775. TEST_DECL(test_wolfSSL_BN_shift),
  62776. TEST_DECL(test_wolfSSL_BN_math),
  62777. TEST_DECL(test_wolfSSL_BN_math_mod),
  62778. TEST_DECL(test_wolfSSL_BN_math_other),
  62779. TEST_DECL(test_wolfSSL_BN_rand),
  62780. TEST_DECL(test_wolfSSL_BN_prime),
  62781. /* OpenSSL PKCS5 API test */
  62782. TEST_DECL(test_wolfSSL_PKCS5),
  62783. /* OpenSSL PKCS8 API test */
  62784. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  62785. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  62786. /* OpenSSL PKCS7 API test */
  62787. TEST_DECL(test_wolfssl_PKCS7),
  62788. TEST_DECL(test_wolfSSL_PKCS7_certs),
  62789. TEST_DECL(test_wolfSSL_PKCS7_sign),
  62790. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  62791. #ifndef NO_BIO
  62792. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  62793. #ifdef HAVE_SMIME
  62794. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  62795. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  62796. #endif /* HAVE_SMIME */
  62797. #endif /* !NO_BIO */
  62798. /* OpenSSL PKCS12 API test */
  62799. TEST_DECL(test_wolfSSL_PKCS12),
  62800. /* Can't memory test as callbacks use Assert. */
  62801. TEST_DECL(test_error_queue_per_thread),
  62802. TEST_DECL(test_wolfSSL_ERR_put_error),
  62803. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  62804. #ifndef NO_BIO
  62805. TEST_DECL(test_wolfSSL_ERR_print_errors),
  62806. #endif
  62807. TEST_DECL(test_OBJ_NAME_do_all),
  62808. TEST_DECL(test_wolfSSL_OBJ),
  62809. TEST_DECL(test_wolfSSL_OBJ_cmp),
  62810. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  62811. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  62812. #ifdef OPENSSL_ALL
  62813. TEST_DECL(test_wolfSSL_OBJ_ln),
  62814. TEST_DECL(test_wolfSSL_OBJ_sn),
  62815. #endif
  62816. #ifndef NO_BIO
  62817. TEST_DECL(test_wolfSSL_BIO_gets),
  62818. TEST_DECL(test_wolfSSL_BIO_puts),
  62819. TEST_DECL(test_wolfSSL_BIO_dump),
  62820. /* Can't memory test as server hangs. */
  62821. TEST_DECL(test_wolfSSL_BIO_should_retry),
  62822. TEST_DECL(test_wolfSSL_BIO_write),
  62823. TEST_DECL(test_wolfSSL_BIO_printf),
  62824. TEST_DECL(test_wolfSSL_BIO_f_md),
  62825. TEST_DECL(test_wolfSSL_BIO_up_ref),
  62826. TEST_DECL(test_wolfSSL_BIO_reset),
  62827. TEST_DECL(test_wolfSSL_BIO_get_len),
  62828. #endif
  62829. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62830. TEST_DECL(test_wolfSSL_check_domain),
  62831. #endif
  62832. TEST_DECL(test_wolfSSL_cert_cb),
  62833. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  62834. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  62835. TEST_DECL(test_wolfSSL_sigalg_info),
  62836. /* Can't memory test as tcp_connect aborts. */
  62837. TEST_DECL(test_wolfSSL_SESSION),
  62838. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  62839. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  62840. TEST_DECL(test_wolfSSL_ticket_keys),
  62841. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  62842. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  62843. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  62844. TEST_DECL(test_wolfSSL_verify_mode),
  62845. TEST_DECL(test_wolfSSL_verify_depth),
  62846. TEST_DECL(test_wolfSSL_verify_result),
  62847. TEST_DECL(test_wolfSSL_msg_callback),
  62848. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  62849. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  62850. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  62851. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  62852. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  62853. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  62854. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  62855. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  62856. TEST_DECL(test_wolfSSL_PEM_read),
  62857. TEST_DECL(test_wolfSSL_OpenSSL_version),
  62858. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  62859. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  62860. TEST_DECL(test_CONF_modules_xxx),
  62861. #ifdef OPENSSL_ALL
  62862. TEST_DECL(test_wolfSSL_TXT_DB),
  62863. TEST_DECL(test_wolfSSL_NCONF),
  62864. #endif
  62865. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  62866. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  62867. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  62868. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  62869. TEST_DECL(test_CRYPTO_THREADID_xxx),
  62870. TEST_DECL(test_ENGINE_cleanup),
  62871. /* test the no op functions for compatibility */
  62872. TEST_DECL(test_no_op_functions),
  62873. /* OpenSSL error API tests */
  62874. TEST_DECL(test_ERR_load_crypto_strings),
  62875. #ifdef OPENSSL_ALL
  62876. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  62877. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  62878. TEST_DECL(test_wolfSSL_CTX_ctrl),
  62879. #endif /* OPENSSL_ALL */
  62880. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  62881. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  62882. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  62883. /*********************************
  62884. * Crypto API tests
  62885. *********************************/
  62886. TEST_DECL(test_wolfSSL_MD4),
  62887. TEST_DECL(test_wolfSSL_MD5),
  62888. TEST_DECL(test_wolfSSL_MD5_Transform),
  62889. TEST_DECL(test_wolfSSL_SHA),
  62890. TEST_DECL(test_wolfSSL_SHA_Transform),
  62891. TEST_DECL(test_wolfSSL_SHA224),
  62892. TEST_DECL(test_wolfSSL_SHA256),
  62893. TEST_DECL(test_wolfSSL_SHA256_Transform),
  62894. TEST_DECL(test_wolfSSL_SHA512_Transform),
  62895. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  62896. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  62897. TEST_DECL(test_wolfSSL_HMAC_CTX),
  62898. TEST_DECL(test_wolfSSL_HMAC),
  62899. TEST_DECL(test_wolfSSL_CMAC),
  62900. TEST_DECL(test_wolfSSL_DES),
  62901. TEST_DECL(test_wolfSSL_DES_ncbc),
  62902. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  62903. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  62904. TEST_DECL(test_wolfSSL_AES_encrypt),
  62905. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  62906. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  62907. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  62908. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  62909. TEST_DECL(test_wolfSSL_RC4),
  62910. TEST_DECL(test_wolfSSL_RSA),
  62911. TEST_DECL(test_wolfSSL_RSA_DER),
  62912. TEST_DECL(test_wolfSSL_RSA_print),
  62913. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  62914. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  62915. TEST_DECL(test_wolfSSL_RSA_get0_key),
  62916. TEST_DECL(test_wolfSSL_RSA_meth),
  62917. TEST_DECL(test_wolfSSL_RSA_verify),
  62918. TEST_DECL(test_wolfSSL_RSA_sign),
  62919. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  62920. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  62921. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  62922. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  62923. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  62924. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  62925. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  62926. TEST_DECL(test_wolfSSL_RSA_ex_data),
  62927. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  62928. TEST_DECL(test_wolfSSL_RSA_To_Der),
  62929. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  62930. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  62931. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  62932. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  62933. TEST_DECL(test_wolfSSL_DH),
  62934. TEST_DECL(test_wolfSSL_DH_dup),
  62935. TEST_DECL(test_wolfSSL_DH_check),
  62936. TEST_DECL(test_wolfSSL_DH_prime),
  62937. TEST_DECL(test_wolfSSL_DH_1536_prime),
  62938. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  62939. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  62940. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  62941. TEST_DECL(test_wolfSSL_d2i_DHparams),
  62942. TEST_DECL(test_wolfSSL_DH_LoadDer),
  62943. TEST_DECL(test_wolfSSL_i2d_DHparams),
  62944. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  62945. TEST_DECL(test_wolfSSL_EC_GROUP),
  62946. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  62947. TEST_DECL(test_wolfSSL_EC_POINT),
  62948. TEST_DECL(test_wolfSSL_SPAKE),
  62949. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  62950. TEST_DECL(test_EC_i2d),
  62951. TEST_DECL(test_wolfSSL_EC_curve),
  62952. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  62953. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  62954. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  62955. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  62956. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  62957. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  62958. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  62959. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  62960. TEST_DECL(test_ECDSA_size_sign),
  62961. TEST_DECL(test_ECDH_compute_key),
  62962. #endif
  62963. #ifdef OPENSSL_EXTRA
  62964. TEST_DECL(test_ED25519),
  62965. TEST_DECL(test_ED448),
  62966. #endif
  62967. TEST_DECL(test_DSA_do_sign_verify),
  62968. #ifdef OPENSSL_ALL
  62969. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  62970. TEST_DECL(test_wolfSSL_DSA_SIG),
  62971. #endif
  62972. TEST_DECL(test_openssl_generate_key_and_cert),
  62973. TEST_DECL(test_wolfSSL_FIPS_mode),
  62974. TEST_DECL(test_openssl_FIPS_drbg),
  62975. /*********************************
  62976. * CertManager API tests
  62977. *********************************/
  62978. TEST_DECL(test_wolfSSL_CertManagerAPI),
  62979. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  62980. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  62981. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  62982. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  62983. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  62984. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  62985. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  62986. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  62987. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  62988. TEST_DECL(test_wolfSSL_CertManagerCRL),
  62989. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  62990. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  62991. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  62992. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  62993. !defined(WOLFSSL_NO_CLIENT_AUTH))
  62994. TEST_DECL(test_various_pathlen_chains),
  62995. #endif
  62996. /*********************************
  62997. * SSL/TLS API tests
  62998. *********************************/
  62999. TEST_DECL(test_wolfSSL_Method_Allocators),
  63000. #ifndef NO_WOLFSSL_SERVER
  63001. TEST_DECL(test_wolfSSL_CTX_new),
  63002. #endif
  63003. TEST_DECL(test_server_wolfSSL_new),
  63004. TEST_DECL(test_client_wolfSSL_new),
  63005. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  63006. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  63007. TEST_DECL(test_for_double_Free),
  63008. #endif
  63009. TEST_DECL(test_wolfSSL_set_options),
  63010. #ifdef WOLFSSL_TLS13
  63011. /* TLS v1.3 API tests */
  63012. TEST_DECL(test_tls13_apis),
  63013. TEST_DECL(test_tls13_cipher_suites),
  63014. #endif
  63015. TEST_DECL(test_wolfSSL_tmp_dh),
  63016. TEST_DECL(test_wolfSSL_ctrl),
  63017. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  63018. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  63019. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  63020. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  63021. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  63022. #endif
  63023. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  63024. TEST_DECL(test_wolfSSL_security_level),
  63025. TEST_DECL(test_wolfSSL_SSL_in_init),
  63026. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  63027. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  63028. TEST_DECL(test_CONF_CTX_FILE),
  63029. TEST_DECL(test_CONF_CTX_CMDLINE),
  63030. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63031. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  63032. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  63033. /* Bad certificate signature tests */
  63034. TEST_DECL(test_EccSigFailure_cm),
  63035. TEST_DECL(test_RsaSigFailure_cm),
  63036. #endif /* NO_CERTS */
  63037. /* PKCS8 testing */
  63038. TEST_DECL(test_wolfSSL_no_password_cb),
  63039. TEST_DECL(test_wolfSSL_PKCS8),
  63040. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  63041. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  63042. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63043. TEST_DECL(test_wolfSSL_get_finished),
  63044. /* Uses Assert in handshake callback. */
  63045. TEST_DECL(test_wolfSSL_CTX_add_session),
  63046. /* Large number of memory allocations. */
  63047. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  63048. /* Large number of memory allocations. */
  63049. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  63050. /* Large number of memory allocations. */
  63051. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  63052. /* Large number of memory allocations. */
  63053. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  63054. /* Large number of memory allocations. */
  63055. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  63056. /* Large number of memory allocations. */
  63057. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  63058. #endif
  63059. TEST_DECL(test_SSL_CIPHER_get_xxx),
  63060. TEST_DECL(test_wolfSSL_ERR_strings),
  63061. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  63062. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  63063. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  63064. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  63065. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  63066. /* Large number of memory allocations. */
  63067. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  63068. TEST_DECL(test_wolfSSL_CertRsaPss),
  63069. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  63070. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  63071. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  63072. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  63073. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  63074. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  63075. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  63076. TEST_DECL(test_multiple_crls_same_issuer),
  63077. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  63078. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  63079. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  63080. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  63081. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  63082. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  63083. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  63084. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  63085. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  63086. TEST_DECL(test_SetTmpEC_DHE_Sz),
  63087. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  63088. #ifdef WOLFSSL_DTLS
  63089. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  63090. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  63091. #endif
  63092. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  63093. /* Uses Assert in handshake callback. */
  63094. TEST_DECL(test_wolfSSL_dtls_plaintext),
  63095. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63096. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63097. TEST_DECL(test_wolfSSL_read_write),
  63098. /* Can't memory test as server hangs if client fails before second connect.
  63099. */
  63100. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  63101. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  63102. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  63103. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  63104. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  63105. /* Can't memory test as server hangs. */
  63106. TEST_DECL(test_wolfSSL_dtls_export),
  63107. /* Uses Assert in handshake callback. */
  63108. TEST_DECL(test_wolfSSL_tls_export),
  63109. #endif
  63110. TEST_DECL(test_wolfSSL_dtls_export_peers),
  63111. TEST_DECL(test_wolfSSL_SetMinVersion),
  63112. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  63113. /* wolfSSL handshake APIs. */
  63114. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  63115. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  63116. TEST_DECL(test_wolfSSL_BUF),
  63117. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  63118. /* Can't memory test as server hangs. */
  63119. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  63120. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  63121. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  63122. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  63123. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  63124. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  63125. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  63126. /* Can't memory test as server hangs. */
  63127. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  63128. TEST_DECL(test_wolfSSL_Tls13_postauth),
  63129. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  63130. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  63131. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  63132. TEST_DECL(test_wolfSSL_THREADID_hash),
  63133. /* TLS extensions tests */
  63134. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63135. #ifdef HAVE_SNI
  63136. TEST_DECL(test_wolfSSL_UseSNI_params),
  63137. /* Uses Assert in handshake callback. */
  63138. TEST_DECL(test_wolfSSL_UseSNI_connection),
  63139. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  63140. #endif /* HAVE_SNI */
  63141. #endif
  63142. TEST_DECL(test_wolfSSL_UseTrustedCA),
  63143. TEST_DECL(test_wolfSSL_UseMaxFragment),
  63144. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  63145. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  63146. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  63147. /* Uses Assert in handshake callback. */
  63148. TEST_DECL(test_wolfSSL_UseALPN_connection),
  63149. TEST_DECL(test_wolfSSL_UseALPN_params),
  63150. #endif
  63151. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  63152. /* Uses Assert in handshake callback. */
  63153. TEST_DECL(test_wolfSSL_set_alpn_protos),
  63154. #endif
  63155. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  63156. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  63157. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  63158. TEST_DECL(test_tls_ext_duplicate),
  63159. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  63160. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63161. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  63162. /* Uses Assert in handshake callback. */
  63163. TEST_DECL(test_wolfSSL_Tls13_ECH),
  63164. #endif
  63165. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  63166. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  63167. /* OCSP Stapling */
  63168. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  63169. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  63170. TEST_DECL(test_self_signed_stapling),
  63171. /* Multicast */
  63172. TEST_DECL(test_wolfSSL_mcast),
  63173. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  63174. TEST_DECL(test_wolfSSL_msgCb),
  63175. TEST_DECL(test_wolfSSL_either_side),
  63176. TEST_DECL(test_wolfSSL_DTLS_either_side),
  63177. /* Uses Assert in handshake callback. */
  63178. TEST_DECL(test_wolfSSL_dtls_fragments),
  63179. /* Uses Assert in handshake callback. */
  63180. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  63181. /* Uses Assert in handshake callback. */
  63182. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  63183. /* Uses Assert in handshake callback. */
  63184. TEST_DECL(test_wolfSSL_dtls_bad_record),
  63185. /* Uses Assert in handshake callback. */
  63186. TEST_DECL(test_wolfSSL_dtls_stateless),
  63187. TEST_DECL(test_generate_cookie),
  63188. #ifndef NO_BIO
  63189. /* Can't memory test as server hangs. */
  63190. TEST_DECL(test_wolfSSL_BIO_connect),
  63191. /* Can't memory test as server Asserts in thread. */
  63192. TEST_DECL(test_wolfSSL_BIO_accept),
  63193. TEST_DECL(test_wolfSSL_BIO_tls),
  63194. #endif
  63195. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  63196. TEST_DECL(test_DhCallbacks),
  63197. #endif
  63198. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63199. TEST_DECL(test_export_keying_material),
  63200. #endif
  63201. /* Can't memory test as client/server Asserts in thread. */
  63202. TEST_DECL(test_ticket_and_psk_mixing),
  63203. /* Can't memory test as client/server Asserts in thread. */
  63204. TEST_DECL(test_prioritize_psk),
  63205. /* Can't memory test as client/server hangs. */
  63206. TEST_DECL(test_wc_CryptoCb),
  63207. /* Can't memory test as client/server hangs. */
  63208. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  63209. #if !defined(NO_FILESYSTEM) && \
  63210. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63211. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63212. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  63213. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  63214. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  63215. #ifdef HAVE_MAX_FRAGMENT
  63216. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  63217. #endif /* HAVE_MAX_FRAGMENT */
  63218. #ifndef NO_RSA
  63219. TEST_DECL(test_wolfSSL_dtls_stateless2),
  63220. #if !defined(NO_OLD_TLS)
  63221. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  63222. #endif /* !defined(NO_OLD_TLS) */
  63223. #endif /* ! NO_RSA */
  63224. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63225. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  63226. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  63227. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  63228. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  63229. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  63230. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  63231. && defined(WOLFSSL_TLS13) && \
  63232. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  63233. TEST_DECL(test_ticket_nonce_malloc),
  63234. #endif
  63235. TEST_DECL(test_ticket_ret_create),
  63236. TEST_DECL(test_extra_alerts_wrong_cs),
  63237. TEST_DECL(test_extra_alerts_skip_hs),
  63238. TEST_DECL(test_extra_alerts_bad_psk),
  63239. TEST_DECL(test_tls13_bad_psk_binder),
  63240. /* Can't memory test as client/server Asserts. */
  63241. TEST_DECL(test_harden_no_secure_renegotiation),
  63242. TEST_DECL(test_override_alt_cert_chain),
  63243. TEST_DECL(test_rpk_set_xxx_cert_type),
  63244. TEST_DECL(test_tls13_rpk_handshake),
  63245. TEST_DECL(test_dtls13_bad_epoch_ch),
  63246. TEST_DECL(test_short_session_id),
  63247. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  63248. /* Can't memory test as client/server hangs. */
  63249. TEST_DECL(test_dtls_msg_from_other_peer),
  63250. TEST_DECL(test_dtls_ipv6_check),
  63251. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  63252. TEST_DECL(test_dtls_no_extensions),
  63253. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  63254. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  63255. TEST_DECL(test_session_ticket_no_id),
  63256. TEST_DECL(test_session_ticket_hs_update),
  63257. TEST_DECL(test_dtls_downgrade_scr_server),
  63258. TEST_DECL(test_dtls_downgrade_scr),
  63259. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  63260. TEST_DECL(test_dtls_client_hello_timeout),
  63261. TEST_DECL(test_dtls_dropped_ccs),
  63262. TEST_DECL(test_dtls_seq_num_downgrade),
  63263. TEST_DECL(test_certreq_sighash_algos),
  63264. TEST_DECL(test_revoked_loaded_int_cert),
  63265. TEST_DECL(test_dtls_frag_ch),
  63266. TEST_DECL(test_dtls13_frag_ch_pq),
  63267. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  63268. TEST_DECL(test_tls13_pq_groups),
  63269. TEST_DECL(test_tls13_early_data),
  63270. TEST_DECL(test_tls_multi_handshakes_one_record),
  63271. TEST_DECL(test_write_dup),
  63272. TEST_DECL(test_read_write_hs),
  63273. TEST_DECL(test_get_signature_nid),
  63274. TEST_DECL(test_tls_cert_store_unchanged),
  63275. /* This test needs to stay at the end to clean up any caches allocated. */
  63276. TEST_DECL(test_wolfSSL_Cleanup)
  63277. };
  63278. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  63279. static void TestSetup(void)
  63280. {
  63281. /* Stub, for now. Add common test setup code here. */
  63282. }
  63283. static void TestCleanup(void)
  63284. {
  63285. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  63286. /* Clear any errors added to the error queue during the test run. */
  63287. wolfSSL_ERR_clear_error();
  63288. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  63289. }
  63290. /* Print out all API test cases with numeric identifier.
  63291. */
  63292. void ApiTest_PrintTestCases(void)
  63293. {
  63294. int i;
  63295. printf("All Test Cases:\n");
  63296. for (i = 0; i < TEST_CASE_CNT; i++) {
  63297. printf("%3d: %s\n", i + 1, testCases[i].name);
  63298. }
  63299. }
  63300. /* Add test case with index to the list to run.
  63301. *
  63302. * @param [in] idx Index of test case to run starting at 1.
  63303. * @return 0 on success.
  63304. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  63305. */
  63306. int ApiTest_RunIdx(int idx)
  63307. {
  63308. if (idx < 1 || idx > TEST_CASE_CNT) {
  63309. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  63310. return BAD_FUNC_ARG;
  63311. }
  63312. testAll = 0;
  63313. testCases[idx-1].run = 1;
  63314. return 0;
  63315. }
  63316. /* Add test case with name to the list to run.
  63317. *
  63318. * @param [in] name Name of test case to run.
  63319. * @return 0 on success.
  63320. * @return BAD_FUNC_ARG when name is not a known test case name.
  63321. */
  63322. int ApiTest_RunName(char* name)
  63323. {
  63324. int i;
  63325. for (i = 0; i < TEST_CASE_CNT; i++) {
  63326. if (XSTRCMP(testCases[i].name, name) == 0) {
  63327. testAll = 0;
  63328. testCases[i].run = 1;
  63329. return 0;
  63330. }
  63331. }
  63332. printf("Test case name not found: %s\n", name);
  63333. printf("Use --list to see all test case names.\n");
  63334. return BAD_FUNC_ARG;
  63335. }
  63336. /* Converts the result code to a string.
  63337. *
  63338. * @param [in] res Test result code.
  63339. * @return String describing test result.
  63340. */
  63341. static const char* apitest_res_string(int res)
  63342. {
  63343. const char* str = "invalid result";
  63344. switch (res) {
  63345. case TEST_SUCCESS:
  63346. str = "passed";
  63347. break;
  63348. case TEST_FAIL:
  63349. str = "failed";
  63350. break;
  63351. case TEST_SKIPPED:
  63352. str = "skipped";
  63353. break;
  63354. }
  63355. return str;
  63356. }
  63357. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63358. static double gettime_secs(void)
  63359. #if defined(_MSC_VER) && defined(_WIN32)
  63360. {
  63361. /* there's no gettimeofday for Windows, so we'll use system time */
  63362. #define EPOCH_DIFF 11644473600LL
  63363. FILETIME currentFileTime;
  63364. GetSystemTimePreciseAsFileTime(&currentFileTime);
  63365. ULARGE_INTEGER uli = { 0, 0 };
  63366. uli.LowPart = currentFileTime.dwLowDateTime;
  63367. uli.HighPart = currentFileTime.dwHighDateTime;
  63368. /* Convert to seconds since Unix epoch */
  63369. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  63370. }
  63371. #else
  63372. {
  63373. struct timeval tv;
  63374. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  63375. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  63376. }
  63377. #endif
  63378. #endif
  63379. int ApiTest(void)
  63380. {
  63381. int i;
  63382. int ret;
  63383. int res = 0;
  63384. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63385. double timeDiff;
  63386. #endif
  63387. printf(" Begin API Tests\n");
  63388. fflush(stdout);
  63389. /* we must perform init and cleanup if not all tests are running */
  63390. if (!testAll) {
  63391. #ifdef WOLFCRYPT_ONLY
  63392. if (wolfCrypt_Init() != 0) {
  63393. printf("wolfCrypt Initialization failed\n");
  63394. res = 1;
  63395. }
  63396. #else
  63397. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  63398. printf("wolfSSL Initialization failed\n");
  63399. res = 1;
  63400. }
  63401. #endif
  63402. }
  63403. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  63404. if (res == 0) {
  63405. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  63406. printf("failed to create tmp dir\n");
  63407. res = 1;
  63408. }
  63409. else {
  63410. tmpDirNameSet = 1;
  63411. }
  63412. }
  63413. #endif
  63414. if (res == 0) {
  63415. for (i = 0; i < TEST_CASE_CNT; ++i) {
  63416. EXPECT_DECLS;
  63417. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  63418. currentTestName = testCases[i].name;
  63419. #endif
  63420. /* When not testing all cases then skip if not marked for running.
  63421. */
  63422. if (!testAll && !testCases[i].run) {
  63423. continue;
  63424. }
  63425. TestSetup();
  63426. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  63427. fflush(stdout);
  63428. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63429. timeDiff = gettime_secs();
  63430. #endif
  63431. ret = testCases[i].func();
  63432. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63433. timeDiff = gettime_secs() - timeDiff;
  63434. #endif
  63435. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  63436. if (ret != TEST_SKIPPED) {
  63437. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  63438. }
  63439. else
  63440. #endif
  63441. {
  63442. printf(" %s\n", apitest_res_string(ret));
  63443. }
  63444. fflush(stdout);
  63445. /* if return code is < 0 and not skipped then assert error */
  63446. Expect((ret > 0 || ret == TEST_SKIPPED),
  63447. ("Test failed\n"),
  63448. ("ret %d", ret));
  63449. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  63450. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  63451. TestCleanup();
  63452. }
  63453. }
  63454. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  63455. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  63456. wc_ecc_fp_free(); /* free per thread cache */
  63457. #endif
  63458. if (!testAll) {
  63459. #ifdef WOLFCRYPT_ONLY
  63460. wolfCrypt_Cleanup();
  63461. #else
  63462. wolfSSL_Cleanup();
  63463. #endif
  63464. }
  63465. (void)testDevId;
  63466. if (res != 0) {
  63467. printf("\nFAILURES:\n");
  63468. for (i = 0; i < TEST_CASE_CNT; ++i) {
  63469. if (testCases[i].fail) {
  63470. printf(" %3d: %s\n", i + 1, testCases[i].name);
  63471. }
  63472. }
  63473. printf("\n");
  63474. fflush(stdout);
  63475. }
  63476. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  63477. if (tmpDirNameSet) {
  63478. printf("\nBinary dumps of the memio streams can be found in the\n"
  63479. "%s directory. This can be imported into\n"
  63480. "Wireshark by transforming the file with\n"
  63481. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  63482. "And then loading test_output.dump.hex into Wireshark using\n"
  63483. "the \"Import from Hex Dump...\" option and selecting the\n"
  63484. "TCP encapsulation option.\n", tmpDirName);
  63485. }
  63486. #endif
  63487. printf(" End API Tests\n");
  63488. fflush(stdout);
  63489. return res;
  63490. }