1
0

api.c 2.5 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224732257322673227732287322973230732317323273233732347323573236732377323873239732407324173242732437324473245732467324773248732497325073251732527325373254732557325673257732587325973260732617326273263732647326573266732677326873269732707327173272732737327473275732767327773278732797328073281732827328373284732857328673287732887328973290732917329273293732947329573296732977329873299733007330173302733037330473305733067330773308733097331073311733127331373314733157331673317733187331973320733217332273323733247332573326733277332873329733307333173332733337333473335733367333773338733397334073341733427334373344733457334673347733487334973350733517335273353733547335573356733577335873359733607336173362733637336473365733667336773368733697337073371733727337373374733757337673377733787337973380733817338273383733847338573386733877338873389733907339173392733937339473395733967339773398733997340073401734027340373404734057340673407734087340973410734117341273413734147341573416734177341873419734207342173422734237342473425734267342773428734297343073431734327343373434734357343673437734387343973440734417344273443734447344573446734477344873449734507345173452734537345473455734567345773458734597346073461734627346373464734657346673467734687346973470734717347273473734747347573476734777347873479734807348173482734837348473485734867348773488734897349073491734927349373494734957349673497734987349973500735017350273503735047350573506735077350873509735107351173512735137351473515735167351773518735197352073521735227352373524735257352673527735287352973530735317353273533735347353573536735377353873539735407354173542735437354473545735467354773548735497355073551735527355373554735557355673557735587355973560735617356273563735647356573566735677356873569735707357173572735737357473575735767357773578735797358073581735827358373584735857358673587735887358973590735917359273593735947359573596735977359873599736007360173602736037360473605736067360773608736097361073611736127361373614736157361673617736187361973620736217362273623736247362573626736277362873629736307363173632736337363473635736367363773638736397364073641736427364373644736457364673647736487364973650736517365273653736547365573656736577365873659736607366173662736637366473665736667366773668736697367073671736727367373674736757367673677736787367973680736817368273683736847368573686736877368873689736907369173692736937369473695736967369773698736997370073701737027370373704737057370673707737087370973710737117371273713737147371573716737177371873719737207372173722737237372473725737267372773728737297373073731737327373373734737357373673737737387373973740737417374273743737447374573746737477374873749737507375173752737537375473755737567375773758737597376073761737627376373764737657376673767737687376973770737717377273773737747377573776737777377873779737807378173782737837378473785737867378773788737897379073791737927379373794737957379673797737987379973800738017380273803738047380573806738077380873809738107381173812738137381473815738167381773818738197382073821738227382373824738257382673827738287382973830738317383273833738347383573836738377383873839738407384173842738437384473845738467384773848738497385073851738527385373854738557385673857738587385973860738617386273863738647386573866738677386873869738707387173872738737387473875738767387773878738797388073881738827388373884738857388673887738887388973890738917389273893738947389573896738977389873899739007390173902739037390473905739067390773908739097391073911739127391373914739157391673917739187391973920739217392273923739247392573926739277392873929739307393173932739337393473935739367393773938739397394073941739427394373944739457394673947739487394973950739517395273953739547395573956739577395873959739607396173962739637396473965739667396773968739697397073971739727397373974739757397673977739787397973980739817398273983739847398573986739877398873989739907399173992739937399473995739967399773998739997400074001740027400374004740057400674007740087400974010740117401274013740147401574016740177401874019740207402174022740237402474025740267402774028740297403074031740327403374034740357403674037740387403974040740417404274043740447404574046740477404874049740507405174052740537405474055740567405774058740597406074061740627406374064740657406674067740687406974070740717407274073740747407574076740777407874079740807408174082740837408474085740867408774088740897409074091740927409374094740957409674097740987409974100741017410274103741047410574106741077410874109741107411174112741137411474115741167411774118741197412074121741227412374124741257412674127741287412974130741317413274133741347413574136741377413874139741407414174142741437414474145741467414774148741497415074151741527415374154741557415674157741587415974160741617416274163741647416574166741677416874169741707417174172741737417474175741767417774178741797418074181741827418374184741857418674187741887418974190741917419274193741947419574196741977419874199742007420174202742037420474205742067420774208742097421074211742127421374214742157421674217742187421974220742217422274223742247422574226742277422874229742307423174232742337423474235742367423774238742397424074241742427424374244742457424674247742487424974250742517425274253742547425574256742577425874259742607426174262742637426474265742667426774268742697427074271742727427374274742757427674277742787427974280742817428274283742847428574286742877428874289742907429174292742937429474295742967429774298742997430074301743027430374304743057430674307743087430974310743117431274313743147431574316743177431874319743207432174322743237432474325743267432774328743297433074331743327433374334743357433674337743387433974340743417434274343743447434574346743477434874349743507435174352743537435474355743567435774358743597436074361743627436374364743657436674367743687436974370743717437274373743747437574376743777437874379743807438174382743837438474385743867438774388743897439074391743927439374394743957439674397743987439974400744017440274403744047440574406744077440874409744107441174412744137441474415744167441774418744197442074421744227442374424744257442674427744287442974430744317443274433744347443574436744377443874439744407444174442744437444474445744467444774448744497445074451744527445374454
  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_CURVE25519
  318. #include <wolfssl/openssl/ec25519.h>
  319. #endif
  320. #ifdef HAVE_ED25519
  321. #include <wolfssl/openssl/ed25519.h>
  322. #endif
  323. #ifdef HAVE_CURVE448
  324. #include <wolfssl/openssl/ec448.h>
  325. #endif
  326. #ifdef HAVE_ED448
  327. #include <wolfssl/openssl/ed448.h>
  328. #endif
  329. #endif /* OPENSSL_EXTRA */
  330. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  331. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  332. #include <wolfssl/wolfcrypt/srp.h>
  333. #endif
  334. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  335. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  336. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  337. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  338. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  339. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  340. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  341. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  342. * number tracking */
  343. #include "wolfssl/internal.h"
  344. #endif
  345. /* force enable test buffers */
  346. #ifndef USE_CERT_BUFFERS_2048
  347. #define USE_CERT_BUFFERS_2048
  348. #endif
  349. #ifndef USE_CERT_BUFFERS_256
  350. #define USE_CERT_BUFFERS_256
  351. #endif
  352. #include <wolfssl/certs_test.h>
  353. #include "tests/utils.h"
  354. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  355. * have default (hidden) visibility, and in the absence of visibility, it's
  356. * benign to mask out the library implementation.
  357. */
  358. #define WOLFSSL_MISC_INCLUDED
  359. #include <wolfcrypt/src/misc.c>
  360. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  361. /* FIPS build has replaced ecc.h. */
  362. #define wc_ecc_key_get_priv(key) (&((key)->k))
  363. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  364. #endif
  365. typedef struct testVector {
  366. const char* input;
  367. const char* output;
  368. size_t inLen;
  369. size_t outLen;
  370. } testVector;
  371. #if defined(HAVE_PKCS7)
  372. typedef struct {
  373. const byte* content;
  374. word32 contentSz;
  375. int contentOID;
  376. int encryptOID;
  377. int keyWrapOID;
  378. int keyAgreeOID;
  379. byte* cert;
  380. size_t certSz;
  381. byte* privateKey;
  382. word32 privateKeySz;
  383. } pkcs7EnvelopedVector;
  384. #ifndef NO_PKCS7_ENCRYPTED_DATA
  385. typedef struct {
  386. const byte* content;
  387. word32 contentSz;
  388. int contentOID;
  389. int encryptOID;
  390. byte* encryptionKey;
  391. word32 encryptionKeySz;
  392. } pkcs7EncryptedVector;
  393. #endif
  394. #endif /* HAVE_PKCS7 */
  395. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  396. typedef int (*ssl_cb)(WOLFSSL* ssl);
  397. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  398. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  399. typedef struct test_ssl_cbf {
  400. method_provider method;
  401. ctx_cb ctx_ready;
  402. ssl_cb ssl_ready;
  403. ssl_cb on_result;
  404. ctx_cb on_ctx_cleanup;
  405. ssl_cb on_cleanup;
  406. hs_cb on_handshake;
  407. WOLFSSL_CTX* ctx;
  408. const char* caPemFile;
  409. const char* certPemFile;
  410. const char* keyPemFile;
  411. const char* crlPemFile;
  412. #ifdef WOLFSSL_STATIC_MEMORY
  413. byte* mem;
  414. word32 memSz;
  415. wolfSSL_method_func method_ex;
  416. #endif
  417. int devId;
  418. int return_code;
  419. int last_err;
  420. unsigned char isSharedCtx:1;
  421. unsigned char loadToSSL:1;
  422. unsigned char ticNoInit:1;
  423. unsigned char doUdp:1;
  424. } test_ssl_cbf;
  425. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  426. typedef struct test_ssl_memio_ctx {
  427. WOLFSSL_CTX* s_ctx;
  428. WOLFSSL_CTX* c_ctx;
  429. WOLFSSL* s_ssl;
  430. WOLFSSL* c_ssl;
  431. const char* c_ciphers;
  432. const char* s_ciphers;
  433. char* c_msg;
  434. int c_msglen;
  435. char* s_msg;
  436. int s_msglen;
  437. test_ssl_cbf s_cb;
  438. test_ssl_cbf c_cb;
  439. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  440. int c_len;
  441. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  442. int s_len;
  443. } test_ssl_memio_ctx;
  444. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  445. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  446. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  447. const char* currentTestName;
  448. char tmpDirName[16];
  449. int tmpDirNameSet = 0;
  450. #endif
  451. /*----------------------------------------------------------------------------*
  452. | Constants
  453. *----------------------------------------------------------------------------*/
  454. /* Test result constants and macros. */
  455. /* Test succeeded. */
  456. #define TEST_SUCCESS (1)
  457. /* Test failed. */
  458. #define TEST_FAIL (0)
  459. /* Test skipped - not run. */
  460. #define TEST_SKIPPED (-7777)
  461. /* Returns the result based on whether check is true.
  462. *
  463. * @param [in] check Condition for success.
  464. * @return When condition is true: TEST_SUCCESS.
  465. * @return When condition is false: TEST_FAIL.
  466. */
  467. #ifdef DEBUG_WOLFSSL_VERBOSE
  468. #define XSTRINGIFY(s) STRINGIFY(s)
  469. #define STRINGIFY(s) #s
  470. #define TEST_RES_CHECK(check) ({ \
  471. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  472. if (_ret == TEST_FAIL) { \
  473. fprintf(stderr, " check \"%s\" at %d ", \
  474. XSTRINGIFY(check), __LINE__); \
  475. } \
  476. _ret; })
  477. #else
  478. #define TEST_RES_CHECK(check) \
  479. ((check) ? TEST_SUCCESS : TEST_FAIL)
  480. #endif /* DEBUG_WOLFSSL_VERBOSE */
  481. #define TEST_STRING "Everyone gets Friday off."
  482. #define TEST_STRING_SZ 25
  483. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  484. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  485. #define TEST_RSA_BITS 1024
  486. #else
  487. #define TEST_RSA_BITS 2048
  488. #endif
  489. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  490. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  491. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  492. static const char* bogusFile =
  493. #ifdef _WIN32
  494. "NUL"
  495. #else
  496. "/dev/null"
  497. #endif
  498. ;
  499. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  500. enum {
  501. TESTING_RSA = 1,
  502. TESTING_ECC = 2
  503. };
  504. #ifdef WOLFSSL_QNX_CAAM
  505. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  506. static int testDevId = WOLFSSL_CAAM_DEVID;
  507. #else
  508. static int testDevId = INVALID_DEVID;
  509. #endif
  510. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  511. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  512. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  513. #define HAVE_IO_TESTS_DEPENDENCIES
  514. #endif
  515. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  516. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  517. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  518. #endif
  519. /*----------------------------------------------------------------------------*
  520. | BIO with fixed read/write size
  521. *----------------------------------------------------------------------------*/
  522. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  523. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  524. int len)
  525. {
  526. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  527. len = 0;
  528. }
  529. else {
  530. if (bio->wrSz - bio->wrIdx < len) {
  531. len = bio->wrSz - bio->wrIdx;
  532. }
  533. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  534. bio->wrIdx += len;
  535. }
  536. return len;
  537. }
  538. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  539. {
  540. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  541. len = 0;
  542. }
  543. else {
  544. if (bio->wrSz - bio->rdIdx < len) {
  545. len = bio->wrSz - bio->rdIdx;
  546. }
  547. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  548. bio->rdIdx += len;
  549. }
  550. return len;
  551. }
  552. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  553. {
  554. static WOLFSSL_BIO_METHOD meth;
  555. meth.type = WOLFSSL_BIO_BIO;
  556. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  557. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  558. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  559. return &meth;
  560. }
  561. #endif
  562. /*----------------------------------------------------------------------------*
  563. | Setup
  564. *----------------------------------------------------------------------------*/
  565. static int test_wolfSSL_Init(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  569. return EXPECT_RESULT();
  570. }
  571. static int test_wolfSSL_Cleanup(void)
  572. {
  573. EXPECT_DECLS;
  574. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  575. return EXPECT_RESULT();
  576. }
  577. /* Initialize the wolfCrypt state.
  578. * POST: 0 success.
  579. */
  580. static int test_wolfCrypt_Init(void)
  581. {
  582. EXPECT_DECLS;
  583. ExpectIntEQ(wolfCrypt_Init(), 0);
  584. return EXPECT_RESULT();
  585. } /* END test_wolfCrypt_Init */
  586. static int test_wolfCrypt_Cleanup(void)
  587. {
  588. EXPECT_DECLS;
  589. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  590. return EXPECT_RESULT();
  591. }
  592. #ifdef WOLFSSL_STATIC_MEMORY
  593. #define TEST_LSM_STATIC_SIZE 440000
  594. /* Create new bucket list, using the default list, adding
  595. * one dang large buffer size. */
  596. #define TEST_LSM_DEF_BUCKETS (WOLFMEM_DEF_BUCKETS+1)
  597. #define TEST_LSM_BUCKETS WOLFMEM_BUCKETS,(LARGEST_MEM_BUCKET*2)
  598. #define TEST_LSM_DIST WOLFMEM_DIST,1
  599. #endif
  600. static int test_wc_LoadStaticMemory_ex(void)
  601. {
  602. EXPECT_DECLS;
  603. #ifdef WOLFSSL_STATIC_MEMORY
  604. byte staticMemory[TEST_LSM_STATIC_SIZE];
  605. word32 sizeList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_BUCKETS };
  606. word32 distList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_DIST };
  607. WOLFSSL_HEAP_HINT* heap;
  608. /* For this test, the size and dist lists will be the ones configured
  609. * for the build, or default. The value of WOLFMEM_DEF_BUCKETS is 9,
  610. * so these lists are 10 long. For most tests, the value of
  611. * WOLFMEM_DEF_BUCKETS is used. There's a test case where one is added
  612. * to that, to make sure the list size is larger than
  613. * WOLFMEM_MAX_BUCKETS. */
  614. /* Pass in zero everything. */
  615. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
  616. BAD_FUNC_ARG);
  617. /* Set the heap pointer to NULL. */
  618. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL,
  619. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  620. staticMemory, (word32)sizeof(staticMemory),
  621. 0, 1),
  622. BAD_FUNC_ARG);
  623. /* Set other pointer values to NULL one at a time. */
  624. heap = NULL;
  625. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  626. WOLFMEM_DEF_BUCKETS, NULL, distList,
  627. staticMemory, (word32)sizeof(staticMemory),
  628. 0, 1),
  629. BAD_FUNC_ARG);
  630. heap = NULL;
  631. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  632. WOLFMEM_DEF_BUCKETS, sizeList, NULL,
  633. staticMemory, (word32)sizeof(staticMemory),
  634. 0, 1),
  635. BAD_FUNC_ARG);
  636. heap = NULL;
  637. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  638. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  639. NULL, (word32)sizeof(staticMemory),
  640. 0, 1),
  641. BAD_FUNC_ARG);
  642. /* Set the size of the static buffer to 0. */
  643. heap = NULL;
  644. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  645. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  646. staticMemory, 0,
  647. 0, 1),
  648. BUFFER_E);
  649. /* Set the size of the static buffer to one less than minimum allowed. */
  650. heap = NULL;
  651. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  652. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  653. staticMemory,
  654. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) - 1,
  655. 0, 1),
  656. BUFFER_E);
  657. /* Set the size of the static buffer to exactly the minimum size. */
  658. heap = NULL;
  659. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  660. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  661. staticMemory,
  662. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)),
  663. 0, 1),
  664. 0);
  665. wc_UnloadStaticMemory(heap);
  666. /* Use more buckets than able. Success case. */
  667. heap = NULL;
  668. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  669. WOLFMEM_DEF_BUCKETS*2, sizeList, distList,
  670. staticMemory, (word32)sizeof(staticMemory),
  671. 0, 1),
  672. 0);
  673. wc_UnloadStaticMemory(heap);
  674. /* Success case. */
  675. heap = NULL;
  676. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  677. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  678. staticMemory, (word32)sizeof(staticMemory),
  679. 0, 1),
  680. 0);
  681. wc_UnloadStaticMemory(heap);
  682. #endif /* WOLFSSL_STATIC_MEMORY */
  683. return EXPECT_RESULT();
  684. }
  685. /*----------------------------------------------------------------------------*
  686. | Platform dependent function test
  687. *----------------------------------------------------------------------------*/
  688. static int test_fileAccess(void)
  689. {
  690. EXPECT_DECLS;
  691. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  692. const char *fname[] = {
  693. svrCertFile, svrKeyFile, caCertFile,
  694. eccCertFile, eccKeyFile, eccRsaCertFile,
  695. cliCertFile, cliCertDerFile, cliKeyFile,
  696. dhParamFile,
  697. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  698. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  699. NULL
  700. };
  701. const char derfile[] = "./certs/server-cert.der";
  702. XFILE f = XBADFILE;
  703. size_t sz;
  704. byte *buff = NULL;
  705. int i;
  706. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  707. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  708. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  709. XFCLOSE(f);
  710. }
  711. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  712. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  713. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  714. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  715. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  716. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  717. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  718. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  719. XFCLOSE(f);
  720. #endif
  721. return EXPECT_RESULT();
  722. }
  723. /*----------------------------------------------------------------------------*
  724. | Method Allocators
  725. *----------------------------------------------------------------------------*/
  726. static int test_wolfSSL_Method_Allocators(void)
  727. {
  728. EXPECT_DECLS;
  729. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  730. do { \
  731. WOLFSSL_METHOD *method = NULL; \
  732. condition(method = allocator()); \
  733. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  734. } while (0)
  735. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  736. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  737. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  738. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  739. #ifndef NO_OLD_TLS
  740. #ifdef WOLFSSL_ALLOW_SSLV3
  741. #ifndef NO_WOLFSSL_SERVER
  742. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  743. #endif
  744. #ifndef NO_WOLFSSL_CLIENT
  745. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  746. #endif
  747. #endif
  748. #ifdef WOLFSSL_ALLOW_TLSV10
  749. #ifndef NO_WOLFSSL_SERVER
  750. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  751. #endif
  752. #ifndef NO_WOLFSSL_CLIENT
  753. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  754. #endif
  755. #endif
  756. #ifndef NO_WOLFSSL_SERVER
  757. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  758. #endif
  759. #ifndef NO_WOLFSSL_CLIENT
  760. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  761. #endif
  762. #endif /* !NO_OLD_TLS */
  763. #ifndef WOLFSSL_NO_TLS12
  764. #ifndef NO_WOLFSSL_SERVER
  765. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  766. #endif
  767. #ifndef NO_WOLFSSL_CLIENT
  768. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  769. #endif
  770. #endif /* !WOLFSSL_NO_TLS12 */
  771. #ifdef WOLFSSL_TLS13
  772. #ifndef NO_WOLFSSL_SERVER
  773. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  774. #endif
  775. #ifndef NO_WOLFSSL_CLIENT
  776. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  777. #endif
  778. #endif /* WOLFSSL_TLS13 */
  779. #ifndef NO_WOLFSSL_SERVER
  780. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  781. #endif
  782. #ifndef NO_WOLFSSL_CLIENT
  783. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  784. #endif
  785. #ifdef WOLFSSL_DTLS
  786. #ifndef NO_OLD_TLS
  787. #ifndef NO_WOLFSSL_SERVER
  788. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  789. #endif
  790. #ifndef NO_WOLFSSL_CLIENT
  791. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  792. #endif
  793. #endif
  794. #ifndef WOLFSSL_NO_TLS12
  795. #ifndef NO_WOLFSSL_SERVER
  796. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  797. #endif
  798. #ifndef NO_WOLFSSL_CLIENT
  799. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  800. #endif
  801. #endif
  802. #endif /* WOLFSSL_DTLS */
  803. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  804. /* Stubs */
  805. #ifndef NO_WOLFSSL_SERVER
  806. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  807. #endif
  808. #ifndef NO_WOLFSSL_CLIENT
  809. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  810. #endif
  811. #endif
  812. /* Test Either Method (client or server) */
  813. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  814. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  815. #ifndef NO_OLD_TLS
  816. #ifdef WOLFSSL_ALLOW_TLSV10
  817. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  818. #endif
  819. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  820. #endif /* !NO_OLD_TLS */
  821. #ifndef WOLFSSL_NO_TLS12
  822. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  823. #endif /* !WOLFSSL_NO_TLS12 */
  824. #ifdef WOLFSSL_TLS13
  825. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  826. #endif /* WOLFSSL_TLS13 */
  827. #ifdef WOLFSSL_DTLS
  828. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  829. #ifndef NO_OLD_TLS
  830. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  831. #endif /* !NO_OLD_TLS */
  832. #ifndef WOLFSSL_NO_TLS12
  833. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  834. #endif /* !WOLFSSL_NO_TLS12 */
  835. #endif /* WOLFSSL_DTLS */
  836. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  837. return EXPECT_RESULT();
  838. }
  839. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  840. /*----------------------------------------------------------------------------*
  841. | Dual algorithm Certificate Tests
  842. *----------------------------------------------------------------------------*/
  843. #define LARGE_TEMP_SZ 4096
  844. /* To better understand this, please see the X9.146 example in wolfssl-examples
  845. * repo. */
  846. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  847. char *sapkiFile, char *altPrivFile)
  848. {
  849. EXPECT_DECLS;
  850. FILE* file = NULL;
  851. Cert newCert;
  852. DecodedCert preTBS;
  853. byte caKeyBuf[LARGE_TEMP_SZ];
  854. word32 caKeySz = LARGE_TEMP_SZ;
  855. byte sapkiBuf[LARGE_TEMP_SZ];
  856. word32 sapkiSz = LARGE_TEMP_SZ;
  857. byte altPrivBuf[LARGE_TEMP_SZ];
  858. word32 altPrivSz = LARGE_TEMP_SZ;
  859. byte altSigAlgBuf[LARGE_TEMP_SZ];
  860. word32 altSigAlgSz = LARGE_TEMP_SZ;
  861. byte scratchBuf[LARGE_TEMP_SZ];
  862. word32 scratchSz = LARGE_TEMP_SZ;
  863. byte preTbsBuf[LARGE_TEMP_SZ];
  864. word32 preTbsSz = LARGE_TEMP_SZ;
  865. byte altSigValBuf[LARGE_TEMP_SZ];
  866. word32 altSigValSz = LARGE_TEMP_SZ;
  867. byte *outBuf = NULL;
  868. word32 outSz = LARGE_TEMP_SZ;
  869. WC_RNG rng;
  870. RsaKey caKey;
  871. ecc_key altCaKey;
  872. word32 idx = 0;
  873. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  874. DYNAMIC_TYPE_TMP_BUFFER));
  875. ExpectIntEQ(wc_InitRng(&rng), 0);
  876. XMEMSET(caKeyBuf, 0, caKeySz);
  877. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  878. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  879. if (file) {
  880. fclose(file);
  881. file = NULL;
  882. }
  883. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  884. idx = 0;
  885. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  886. 0);
  887. XMEMSET(sapkiBuf, 0, sapkiSz);
  888. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  889. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  890. if (file) {
  891. fclose(file);
  892. file = NULL;
  893. }
  894. XMEMSET(altPrivBuf, 0, altPrivSz);
  895. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  896. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  897. if (file) {
  898. fclose(file);
  899. file = NULL;
  900. }
  901. wc_ecc_init(&altCaKey);
  902. idx = 0;
  903. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  904. (word32)altPrivSz), 0);
  905. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  906. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  907. oidSigType, 0), 0);
  908. wc_InitCert(&newCert);
  909. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  910. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  911. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  912. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  913. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  914. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  915. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  916. newCert.sigType = CTC_SHA256wRSA;
  917. newCert.isCA = 1;
  918. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  919. (const byte *)"This is NOT a critical extension", 32), 0);
  920. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  921. sapkiSz), 0);
  922. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  923. altSigAlgSz), 0);
  924. XMEMSET(scratchBuf, 0, scratchSz);
  925. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  926. &caKey, &rng), 0);
  927. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  928. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  929. XMEMSET(preTbsBuf, 0, preTbsSz);
  930. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  931. XMEMSET(altSigValBuf, 0, altSigValSz);
  932. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  933. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  934. &rng), 0);
  935. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  936. altSigValSz), 0);
  937. /* Finally, generate the new certificate. */
  938. XMEMSET(outBuf, 0, outSz);
  939. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  940. 0);
  941. *out = outBuf;
  942. wc_FreeRsaKey(&caKey);
  943. wc_FreeRng(&rng);
  944. wc_FreeDecodedCert(&preTBS);
  945. return outSz;
  946. }
  947. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  948. char *sapkiFile, char *altPrivFile,
  949. char *serverKeyFile,
  950. byte *caCertBuf, int caCertSz)
  951. {
  952. EXPECT_DECLS;
  953. FILE* file = NULL;
  954. Cert newCert;
  955. DecodedCert preTBS;
  956. byte serverKeyBuf[LARGE_TEMP_SZ];
  957. word32 serverKeySz = LARGE_TEMP_SZ;
  958. byte caKeyBuf[LARGE_TEMP_SZ];
  959. word32 caKeySz = LARGE_TEMP_SZ;
  960. byte sapkiBuf[LARGE_TEMP_SZ];
  961. word32 sapkiSz = LARGE_TEMP_SZ;
  962. byte altPrivBuf[LARGE_TEMP_SZ];
  963. word32 altPrivSz = LARGE_TEMP_SZ;
  964. byte altSigAlgBuf[LARGE_TEMP_SZ];
  965. word32 altSigAlgSz = LARGE_TEMP_SZ;
  966. byte scratchBuf[LARGE_TEMP_SZ];
  967. word32 scratchSz = LARGE_TEMP_SZ;
  968. byte preTbsBuf[LARGE_TEMP_SZ];
  969. word32 preTbsSz = LARGE_TEMP_SZ;
  970. byte altSigValBuf[LARGE_TEMP_SZ];
  971. word32 altSigValSz = LARGE_TEMP_SZ;
  972. byte *outBuf = NULL;
  973. word32 outSz = LARGE_TEMP_SZ;
  974. WC_RNG rng;
  975. RsaKey caKey;
  976. RsaKey serverKey;
  977. ecc_key altCaKey;
  978. word32 idx = 0;
  979. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  980. DYNAMIC_TYPE_TMP_BUFFER));
  981. ExpectIntEQ(wc_InitRng(&rng), 0);
  982. XMEMSET(serverKeyBuf, 0, serverKeySz);
  983. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  984. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  985. 0);
  986. if (file) {
  987. fclose(file);
  988. file = NULL;
  989. }
  990. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  991. idx = 0;
  992. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  993. (word32)serverKeySz), 0);
  994. XMEMSET(caKeyBuf, 0, caKeySz);
  995. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  996. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  997. if (file) {
  998. fclose(file);
  999. file = NULL;
  1000. }
  1001. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  1002. idx = 0;
  1003. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  1004. (word32)caKeySz), 0);
  1005. XMEMSET(sapkiBuf, 0, sapkiSz);
  1006. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  1007. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  1008. if (file) {
  1009. fclose(file);
  1010. file = NULL;
  1011. }
  1012. XMEMSET(altPrivBuf, 0, altPrivSz);
  1013. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  1014. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  1015. if (file) {
  1016. fclose(file);
  1017. file = NULL;
  1018. }
  1019. wc_ecc_init(&altCaKey);
  1020. idx = 0;
  1021. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  1022. (word32)altPrivSz), 0);
  1023. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  1024. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  1025. oidSigType, 0), 0);
  1026. wc_InitCert(&newCert);
  1027. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  1028. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  1029. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  1030. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  1031. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  1032. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  1033. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  1034. newCert.sigType = CTC_SHA256wRSA;
  1035. newCert.isCA = 0;
  1036. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  1037. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  1038. (const byte *)"This is NOT a critical extension", 32), 0);
  1039. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  1040. sapkiSz), 0);
  1041. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  1042. altSigAlgSz), 0);
  1043. XMEMSET(scratchBuf, 0, scratchSz);
  1044. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  1045. &rng), 0);
  1046. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  1047. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  1048. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  1049. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  1050. XMEMSET(preTbsBuf, 0, preTbsSz);
  1051. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  1052. XMEMSET(altSigValBuf, 0, altSigValSz);
  1053. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  1054. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  1055. &rng), 0);
  1056. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  1057. altSigValBuf, altSigValSz), 0);
  1058. /* Finally, generate the new certificate. */
  1059. XMEMSET(outBuf, 0, outSz);
  1060. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  1061. 0);
  1062. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  1063. outSz, &caKey, NULL, &rng), 0);
  1064. *out = outBuf;
  1065. wc_FreeRsaKey(&caKey);
  1066. wc_FreeRsaKey(&serverKey);
  1067. wc_FreeRng(&rng);
  1068. wc_FreeDecodedCert(&preTBS);
  1069. return outSz;
  1070. }
  1071. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  1072. byte *serverCert, word32 serverCertSz,
  1073. byte *serverKey, word32 serverKeySz,
  1074. int negative_test)
  1075. {
  1076. EXPECT_DECLS;
  1077. WOLFSSL_CTX *ctx_c = NULL;
  1078. WOLFSSL_CTX *ctx_s = NULL;
  1079. WOLFSSL *ssl_c = NULL;
  1080. WOLFSSL *ssl_s = NULL;
  1081. struct test_memio_ctx test_ctx;
  1082. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  1083. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  1084. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  1085. caCert, caCertSz, serverCert, serverCertSz,
  1086. serverKey, serverKeySz), 0);
  1087. if (negative_test) {
  1088. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  1089. }
  1090. else {
  1091. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  1092. }
  1093. wolfSSL_free(ssl_c);
  1094. wolfSSL_free(ssl_s);
  1095. wolfSSL_CTX_free(ctx_c);
  1096. wolfSSL_CTX_free(ctx_s);
  1097. return EXPECT_RESULT();
  1098. }
  1099. static int extCount = 0;
  1100. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1101. const unsigned char* der, word32 derSz)
  1102. {
  1103. (void) oid;
  1104. (void) oidSz;
  1105. (void) crit;
  1106. (void) der;
  1107. (void) derSz;
  1108. extCount ++;
  1109. /* Accept all extensions. This is only a test. Normally we would be much more
  1110. * careful about critical extensions. */
  1111. return 1;
  1112. }
  1113. static int test_dual_alg_support(void)
  1114. {
  1115. EXPECT_DECLS;
  1116. /* Root CA and server keys will be the same. This is only appropriate for
  1117. * testing. */
  1118. char keyFile[] = "./certs/ca-key.der";
  1119. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1120. char altPrivFile[] = "./certs/ecc-key.der";
  1121. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1122. byte *serverKey = NULL;
  1123. size_t serverKeySz = 0;
  1124. byte *root = NULL;
  1125. int rootSz = 0;
  1126. byte *server = NULL;
  1127. int serverSz = 0;
  1128. WOLFSSL_CERT_MANAGER* cm = NULL;
  1129. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1130. /* Base normal case. */
  1131. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1132. ExpectNotNull(root);
  1133. ExpectIntGT(rootSz, 0);
  1134. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1135. altPrivFile, keyFile, root, rootSz);
  1136. ExpectNotNull(server);
  1137. ExpectIntGT(serverSz, 0);
  1138. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1139. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1140. TEST_SUCCESS);
  1141. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1142. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1143. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1144. * the alternative signature and then set negative_test to true for the
  1145. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1146. * because the signature won't verify. The exception is if
  1147. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1148. * and this is no longer a negative test. */
  1149. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1150. ExpectNotNull(root);
  1151. ExpectIntGT(rootSz, 0);
  1152. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1153. wrongPrivFile, keyFile, root, rootSz);
  1154. ExpectNotNull(server);
  1155. ExpectIntGT(serverSz, 0);
  1156. #ifdef WOLFSSL_TRUST_PEER_CERT
  1157. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1158. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1159. TEST_SUCCESS);
  1160. #else
  1161. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1162. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1163. TEST_SUCCESS);
  1164. #endif
  1165. /* Lets see if CertManager can find the new extensions */
  1166. extCount = 0;
  1167. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1168. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1169. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1170. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1171. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1172. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1173. /* There is only 1 unknown extension (1.2.3.4.5). The other ones are known
  1174. * because they are for the dual alg extensions. */
  1175. ExpectIntEQ(extCount, 1);
  1176. wolfSSL_CertManagerFree(cm);
  1177. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1178. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1179. free(serverKey);
  1180. return EXPECT_RESULT();
  1181. }
  1182. #else
  1183. static int test_dual_alg_support(void)
  1184. {
  1185. return TEST_SKIPPED;
  1186. }
  1187. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1188. /*----------------------------------------------------------------------------*
  1189. | Context
  1190. *----------------------------------------------------------------------------*/
  1191. #ifndef NO_WOLFSSL_SERVER
  1192. static int test_wolfSSL_CTX_new(void)
  1193. {
  1194. EXPECT_DECLS;
  1195. WOLFSSL_CTX *ctx;
  1196. WOLFSSL_METHOD* method;
  1197. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1198. ExpectNotNull(method = wolfSSLv23_server_method());
  1199. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1200. wolfSSL_CTX_free(ctx);
  1201. return EXPECT_RESULT();
  1202. }
  1203. #endif
  1204. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1205. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1206. static int test_for_double_Free(void)
  1207. {
  1208. EXPECT_DECLS;
  1209. WOLFSSL_CTX* ctx = NULL;
  1210. WOLFSSL* ssl = NULL;
  1211. int skipTest = 0;
  1212. const char* testCertFile;
  1213. const char* testKeyFile;
  1214. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1215. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1216. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1217. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1218. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1219. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1220. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1221. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1222. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1223. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1224. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1225. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1226. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1227. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1228. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1229. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1230. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1231. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1232. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1233. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1234. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1235. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1236. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1237. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1238. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1239. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1240. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1241. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1242. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1243. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1244. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1245. #ifdef OPENSSL_EXTRA
  1246. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1247. "!SRP:!kRSA:!aNULL:!eNULL";
  1248. #endif
  1249. #ifndef NO_RSA
  1250. testCertFile = svrCertFile;
  1251. testKeyFile = svrKeyFile;
  1252. #elif defined(HAVE_ECC)
  1253. testCertFile = eccCertFile;
  1254. testKeyFile = eccKeyFile;
  1255. #else
  1256. skipTest = 1;
  1257. #endif
  1258. if (skipTest != 1) {
  1259. #ifndef NO_WOLFSSL_SERVER
  1260. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1261. #else
  1262. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1263. #endif
  1264. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1265. WOLFSSL_FILETYPE_PEM));
  1266. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1267. WOLFSSL_FILETYPE_PEM));
  1268. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1269. /* First test freeing SSL, then CTX */
  1270. wolfSSL_free(ssl);
  1271. ssl = NULL;
  1272. wolfSSL_CTX_free(ctx);
  1273. ctx = NULL;
  1274. #ifndef NO_WOLFSSL_CLIENT
  1275. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1276. #else
  1277. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1278. #endif
  1279. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1280. WOLFSSL_FILETYPE_PEM));
  1281. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1282. WOLFSSL_FILETYPE_PEM));
  1283. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1284. /* Next test freeing CTX then SSL */
  1285. wolfSSL_CTX_free(ctx);
  1286. ctx = NULL;
  1287. wolfSSL_free(ssl);
  1288. ssl = NULL;
  1289. #ifndef NO_WOLFSSL_SERVER
  1290. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1291. #else
  1292. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1293. #endif
  1294. /* Test setting ciphers at ctx level */
  1295. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1296. WOLFSSL_FILETYPE_PEM));
  1297. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1298. WOLFSSL_FILETYPE_PEM));
  1299. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1300. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1301. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1302. /* only update TLSv13 suites */
  1303. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1304. #endif
  1305. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1306. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1307. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1308. /* only update pre-TLSv13 suites */
  1309. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1310. "ECDHE-RSA-AES128-GCM-SHA256"));
  1311. #endif
  1312. #ifdef OPENSSL_EXTRA
  1313. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1314. #endif
  1315. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1316. wolfSSL_CTX_free(ctx);
  1317. ctx = NULL;
  1318. wolfSSL_free(ssl);
  1319. ssl = NULL;
  1320. #ifndef NO_WOLFSSL_CLIENT
  1321. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1322. #else
  1323. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1324. #endif
  1325. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1326. WOLFSSL_FILETYPE_PEM));
  1327. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1328. WOLFSSL_FILETYPE_PEM));
  1329. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1330. /* test setting ciphers at SSL level */
  1331. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1332. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1333. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1334. /* only update TLSv13 suites */
  1335. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1336. #endif
  1337. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1338. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1339. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1340. /* only update pre-TLSv13 suites */
  1341. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1342. #endif
  1343. wolfSSL_CTX_free(ctx);
  1344. ctx = NULL;
  1345. wolfSSL_free(ssl);
  1346. ssl = NULL;
  1347. }
  1348. return EXPECT_RESULT();
  1349. }
  1350. #endif
  1351. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1352. {
  1353. EXPECT_DECLS;
  1354. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1355. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1356. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1357. const char* testCertFile;
  1358. const char* testKeyFile;
  1359. WOLFSSL_CTX* ctx = NULL;
  1360. WOLFSSL* ssl = NULL;
  1361. const byte cipherList[] =
  1362. {
  1363. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1364. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1365. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1366. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1367. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1368. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1369. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1370. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1371. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1372. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1373. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1374. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1375. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1376. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1377. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1378. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1379. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1380. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1381. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1382. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1383. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1384. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1385. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1386. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1387. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1388. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1389. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1390. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1391. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1392. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1393. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1394. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1395. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1396. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1397. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1398. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1399. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1400. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1401. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1402. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1403. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1404. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1405. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1406. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1407. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1408. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1409. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1410. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1411. /* SHA256 */
  1412. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1413. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1414. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1415. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1416. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1417. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1418. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1419. /* SHA384 */
  1420. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1421. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1422. /* AES-GCM */
  1423. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1424. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1425. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1426. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1427. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1428. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1429. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1430. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1431. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1432. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1433. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1434. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1435. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1436. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1437. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1438. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1439. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1440. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1441. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1442. * also, in some of the other AES-CCM suites
  1443. * there will be second byte number conflicts
  1444. * with non-ECC AES-GCM */
  1445. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1446. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1447. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1448. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1449. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1450. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1451. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1452. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1453. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1454. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1455. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1456. /* Camellia */
  1457. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1458. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1459. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1460. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1461. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1462. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1463. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1464. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1465. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1466. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1467. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1468. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1469. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1470. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1471. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1472. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1473. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1474. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1475. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1476. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1477. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1478. /* TLS v1.3 cipher suites */
  1479. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1480. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1481. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1482. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1483. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1484. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1485. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1486. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1487. };
  1488. #ifndef NO_RSA
  1489. testCertFile = svrCertFile;
  1490. testKeyFile = svrKeyFile;
  1491. #elif defined(HAVE_ECC)
  1492. testCertFile = eccCertFile;
  1493. testKeyFile = eccKeyFile;
  1494. #endif
  1495. #ifndef NO_WOLFSSL_SERVER
  1496. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1497. #else
  1498. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1499. #endif
  1500. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1501. sizeof(cipherList)));
  1502. wolfSSL_CTX_free(ctx);
  1503. ctx = NULL;
  1504. #ifndef NO_WOLFSSL_SERVER
  1505. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1506. #else
  1507. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1508. #endif
  1509. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1510. WOLFSSL_FILETYPE_PEM));
  1511. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1512. WOLFSSL_FILETYPE_PEM));
  1513. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1514. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1515. sizeof(cipherList)));
  1516. wolfSSL_free(ssl);
  1517. wolfSSL_CTX_free(ctx);
  1518. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1519. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1520. return EXPECT_RESULT();
  1521. }
  1522. static int test_wolfSSL_CTX_use_certificate_file(void)
  1523. {
  1524. EXPECT_DECLS;
  1525. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1526. WOLFSSL_CTX *ctx = NULL;
  1527. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1528. /* invalid context */
  1529. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1530. WOLFSSL_FILETYPE_PEM));
  1531. /* invalid cert file */
  1532. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1533. WOLFSSL_FILETYPE_PEM));
  1534. /* invalid cert type */
  1535. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1536. #ifdef NO_RSA
  1537. /* rsa needed */
  1538. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1539. WOLFSSL_FILETYPE_PEM));
  1540. #else
  1541. /* success */
  1542. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1543. WOLFSSL_FILETYPE_PEM));
  1544. #endif
  1545. wolfSSL_CTX_free(ctx);
  1546. #endif
  1547. return EXPECT_RESULT();
  1548. }
  1549. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1550. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1551. {
  1552. EXPECT_DECLS;
  1553. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1554. WOLFSSL_CTX* ctx = NULL;
  1555. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1556. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1557. server_cert_der_2048), WOLFSSL_SUCCESS);
  1558. wolfSSL_CTX_free(ctx);
  1559. #endif
  1560. return EXPECT_RESULT();
  1561. }
  1562. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1563. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1564. * context using buffer.
  1565. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1566. * --enable-testcert flag.
  1567. */
  1568. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1569. {
  1570. EXPECT_DECLS;
  1571. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1572. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1573. WOLFSSL_CTX* ctx = NULL;
  1574. int ret;
  1575. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1576. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1577. server_cert_der_2048, sizeof_server_cert_der_2048,
  1578. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1579. wolfSSL_CTX_free(ctx);
  1580. #endif
  1581. return EXPECT_RESULT();
  1582. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1583. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1584. {
  1585. EXPECT_DECLS;
  1586. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1587. WOLFSSL_CTX *ctx = NULL;
  1588. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1589. /* invalid context */
  1590. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1591. WOLFSSL_FILETYPE_PEM));
  1592. /* invalid key file */
  1593. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1594. WOLFSSL_FILETYPE_PEM));
  1595. /* invalid key type */
  1596. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1597. /* success */
  1598. #ifdef NO_RSA
  1599. /* rsa needed */
  1600. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1601. WOLFSSL_FILETYPE_PEM));
  1602. #else
  1603. /* success */
  1604. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1605. WOLFSSL_FILETYPE_PEM));
  1606. #endif
  1607. wolfSSL_CTX_free(ctx);
  1608. #endif
  1609. return EXPECT_RESULT();
  1610. }
  1611. /* test both file and buffer versions along with unloading trusted peer certs */
  1612. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1613. {
  1614. EXPECT_DECLS;
  1615. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1616. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1617. WOLFSSL_CTX *ctx = NULL;
  1618. WOLFSSL* ssl = NULL;
  1619. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1620. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1621. #if !defined(NO_FILESYSTEM)
  1622. /* invalid file */
  1623. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1624. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1625. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1626. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1627. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1628. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1629. /* success */
  1630. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1631. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1632. /* unload cert */
  1633. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1634. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1635. /* invalid file */
  1636. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1637. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1638. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1639. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1640. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1641. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1642. /* success */
  1643. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1644. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1645. #ifdef WOLFSSL_LOCAL_X509_STORE
  1646. /* unload cert */
  1647. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1648. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1649. #endif
  1650. #endif
  1651. /* Test of loading certs from buffers */
  1652. /* invalid buffer */
  1653. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1654. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1655. /* success */
  1656. #ifdef USE_CERT_BUFFERS_1024
  1657. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1658. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1659. #endif
  1660. #ifdef USE_CERT_BUFFERS_2048
  1661. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1662. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1663. #endif
  1664. /* unload cert */
  1665. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1666. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1667. wolfSSL_free(ssl);
  1668. wolfSSL_CTX_free(ctx);
  1669. #endif
  1670. return EXPECT_RESULT();
  1671. }
  1672. static int test_wolfSSL_CTX_load_verify_locations(void)
  1673. {
  1674. EXPECT_DECLS;
  1675. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1676. WOLFSSL_CTX *ctx = NULL;
  1677. #ifndef NO_RSA
  1678. WOLFSSL_CERT_MANAGER* cm = NULL;
  1679. #ifdef PERSIST_CERT_CACHE
  1680. int cacheSz = 0;
  1681. unsigned char* cache = NULL;
  1682. int used = 0;
  1683. #ifndef NO_FILESYSTEM
  1684. const char* cacheFile = "./tests/cert_cache.tmp";
  1685. #endif
  1686. int i;
  1687. int t;
  1688. int* p;
  1689. #endif
  1690. #endif
  1691. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1692. const char* load_certs_path = "./certs/external";
  1693. const char* load_no_certs_path = "./examples";
  1694. const char* load_expired_path = "./certs/test/expired";
  1695. #endif
  1696. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1697. /* invalid arguments */
  1698. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1699. WOLFSSL_FAILURE);
  1700. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1701. WOLFSSL_FAILURE);
  1702. /* invalid ca file */
  1703. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1704. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1705. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1706. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1707. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1708. /* invalid path */
  1709. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1710. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1711. #endif
  1712. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1713. /* test ignoring the invalid path */
  1714. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1715. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1716. #endif
  1717. /* load ca cert */
  1718. #ifdef NO_RSA
  1719. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1720. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1721. #else /* Skip the following test without RSA certs. */
  1722. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1723. WOLFSSL_SUCCESS);
  1724. #ifdef PERSIST_CERT_CACHE
  1725. /* Get cert cache size */
  1726. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1727. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1728. DYNAMIC_TYPE_TMP_BUFFER));
  1729. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1730. BAD_FUNC_ARG);
  1731. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1732. BAD_FUNC_ARG);
  1733. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1734. BAD_FUNC_ARG);
  1735. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1736. BAD_FUNC_ARG);
  1737. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1738. BAD_FUNC_ARG);
  1739. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1740. BAD_FUNC_ARG);
  1741. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1742. BAD_FUNC_ARG);
  1743. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1744. BAD_FUNC_ARG);
  1745. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1746. BAD_FUNC_ARG);
  1747. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1748. BUFFER_E);
  1749. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1750. ExpectIntEQ(cacheSz, used);
  1751. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1752. BAD_FUNC_ARG);
  1753. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1754. BAD_FUNC_ARG);
  1755. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1756. BAD_FUNC_ARG);
  1757. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1758. BAD_FUNC_ARG);
  1759. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1760. BAD_FUNC_ARG);
  1761. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1762. BAD_FUNC_ARG);
  1763. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1764. BAD_FUNC_ARG);
  1765. /* Smaller than header. */
  1766. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1767. for (i = 1; i < cacheSz; i++) {
  1768. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1769. BUFFER_E);
  1770. }
  1771. if (EXPECT_SUCCESS()) {
  1772. /* Modify header for bad results! */
  1773. p = (int*)cache;
  1774. /* version */
  1775. t = p[0]; p[0] = 0xff;
  1776. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1777. CACHE_MATCH_ERROR);
  1778. p[0] = t; p++;
  1779. /* rows */
  1780. t = p[0]; p[0] = 0xff;
  1781. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1782. CACHE_MATCH_ERROR);
  1783. p[0] = t; p++;
  1784. /* columns[0] */
  1785. t = p[0]; p[0] = -1;
  1786. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1787. PARSE_ERROR);
  1788. p[0] = t; p += CA_TABLE_SIZE;
  1789. /* signerSz*/
  1790. t = p[0]; p[0] = 0xff;
  1791. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1792. CACHE_MATCH_ERROR);
  1793. p[0] = t;
  1794. }
  1795. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1796. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1797. #ifndef NO_FILESYSTEM
  1798. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1799. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1800. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1801. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1802. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1803. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1804. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1805. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1806. WOLFSSL_BAD_FILE);
  1807. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1808. /* File contents is not a cache. */
  1809. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1810. CACHE_MATCH_ERROR);
  1811. #endif
  1812. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1813. #endif
  1814. /* Test unloading CA's */
  1815. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1816. #ifdef PERSIST_CERT_CACHE
  1817. /* Verify no certs (result is less than cacheSz) */
  1818. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1819. #endif
  1820. /* load ca cert again */
  1821. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1822. WOLFSSL_SUCCESS);
  1823. /* Test getting CERT_MANAGER */
  1824. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1825. /* Test unloading CA's using CM */
  1826. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1827. #ifdef PERSIST_CERT_CACHE
  1828. /* Verify no certs (result is less than cacheSz) */
  1829. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1830. #endif
  1831. #endif
  1832. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1833. /* Test loading CA certificates using a path */
  1834. #ifdef NO_RSA
  1835. /* failure here okay since certs in external directory are RSA */
  1836. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1837. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1838. #else
  1839. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1840. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1841. #endif
  1842. /* Test loading path with no files */
  1843. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1844. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1845. /* Test loading expired CA certificates */
  1846. #ifdef NO_RSA
  1847. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1848. load_expired_path,
  1849. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1850. WOLFSSL_SUCCESS);
  1851. #else
  1852. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1853. load_expired_path,
  1854. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1855. WOLFSSL_SUCCESS);
  1856. #endif
  1857. /* Test loading CA certificates and ignoring all errors */
  1858. #ifdef NO_RSA
  1859. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1860. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1861. #else
  1862. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1863. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1864. #endif
  1865. #endif
  1866. wolfSSL_CTX_free(ctx);
  1867. #endif
  1868. return EXPECT_RESULT();
  1869. }
  1870. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1871. {
  1872. int res = TEST_SKIPPED;
  1873. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1874. (!defined(NO_RSA) || defined(HAVE_ECC))
  1875. WOLFSSL_CTX* ctx;
  1876. byte dirValid = 0;
  1877. int ret = 0;
  1878. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1879. if (ctx == NULL) {
  1880. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1881. ret = -1;
  1882. }
  1883. if (ret == 0) {
  1884. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1885. dirValid = 1;
  1886. #else
  1887. word32 numDirs;
  1888. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1889. if (caDirs == NULL || numDirs == 0) {
  1890. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1891. ret = -1;
  1892. }
  1893. else {
  1894. ReadDirCtx dirCtx;
  1895. word32 i;
  1896. for (i = 0; i < numDirs; ++i) {
  1897. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1898. /* Directory isn't empty. */
  1899. dirValid = 1;
  1900. wc_ReadDirClose(&dirCtx);
  1901. break;
  1902. }
  1903. }
  1904. }
  1905. #endif
  1906. }
  1907. /*
  1908. * If the directory isn't empty, we should be able to load CA
  1909. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1910. * usable.
  1911. */
  1912. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1913. WOLFSSL_SUCCESS) {
  1914. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1915. ret = -1;
  1916. }
  1917. #ifdef OPENSSL_EXTRA
  1918. if (ret == 0 &&
  1919. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1920. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1921. ret = -1;
  1922. }
  1923. #endif /* OPENSSL_EXTRA */
  1924. wolfSSL_CTX_free(ctx);
  1925. res = TEST_RES_CHECK(ret == 0);
  1926. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1927. return res;
  1928. }
  1929. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1930. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1931. int file_type)
  1932. {
  1933. int ret;
  1934. WOLFSSL_CERT_MANAGER* cm;
  1935. cm = wolfSSL_CertManagerNew();
  1936. if (cm == NULL) {
  1937. fprintf(stderr, "test_cm_load_ca failed\n");
  1938. return -1;
  1939. }
  1940. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1941. wolfSSL_CertManagerFree(cm);
  1942. return ret;
  1943. }
  1944. static int test_cm_load_ca_file(const char* ca_cert_file)
  1945. {
  1946. int ret = 0;
  1947. byte* cert_buf = NULL;
  1948. size_t cert_sz = 0;
  1949. #if defined(WOLFSSL_PEM_TO_DER)
  1950. DerBuffer* pDer = NULL;
  1951. #endif
  1952. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1953. if (ret == 0) {
  1954. /* normal test */
  1955. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1956. if (ret == WOLFSSL_SUCCESS) {
  1957. /* test including null terminator in length */
  1958. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1959. if (tmp == NULL) {
  1960. ret = MEMORY_E;
  1961. }
  1962. else {
  1963. cert_buf = tmp;
  1964. cert_buf[cert_sz] = '\0';
  1965. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1966. WOLFSSL_FILETYPE_PEM);
  1967. }
  1968. }
  1969. #if defined(WOLFSSL_PEM_TO_DER)
  1970. if (ret == WOLFSSL_SUCCESS) {
  1971. /* test loading DER */
  1972. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1973. if (ret == 0 && pDer != NULL) {
  1974. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1975. WOLFSSL_FILETYPE_ASN1);
  1976. wc_FreeDer(&pDer);
  1977. }
  1978. }
  1979. #endif
  1980. }
  1981. free(cert_buf);
  1982. return ret;
  1983. }
  1984. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1985. int file_type, word32 flags)
  1986. {
  1987. int ret;
  1988. WOLFSSL_CERT_MANAGER* cm;
  1989. cm = wolfSSL_CertManagerNew();
  1990. if (cm == NULL) {
  1991. fprintf(stderr, "test_cm_load_ca failed\n");
  1992. return -1;
  1993. }
  1994. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1995. 0, flags);
  1996. wolfSSL_CertManagerFree(cm);
  1997. return ret;
  1998. }
  1999. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  2000. {
  2001. int ret = 0;
  2002. byte* cert_buf = NULL;
  2003. size_t cert_sz = 0;
  2004. #if defined(WOLFSSL_PEM_TO_DER)
  2005. DerBuffer* pDer = NULL;
  2006. #endif
  2007. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  2008. if (ret == 0) {
  2009. /* normal test */
  2010. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  2011. WOLFSSL_FILETYPE_PEM, flags);
  2012. if (ret == WOLFSSL_SUCCESS) {
  2013. /* test including null terminator in length */
  2014. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  2015. if (tmp == NULL) {
  2016. ret = MEMORY_E;
  2017. }
  2018. else {
  2019. cert_buf = tmp;
  2020. cert_buf[cert_sz] = '\0';
  2021. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  2022. WOLFSSL_FILETYPE_PEM, flags);
  2023. }
  2024. }
  2025. #if defined(WOLFSSL_PEM_TO_DER)
  2026. if (ret == WOLFSSL_SUCCESS) {
  2027. /* test loading DER */
  2028. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  2029. if (ret == 0 && pDer != NULL) {
  2030. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  2031. WOLFSSL_FILETYPE_ASN1, flags);
  2032. wc_FreeDer(&pDer);
  2033. }
  2034. }
  2035. #endif
  2036. }
  2037. free(cert_buf);
  2038. return ret;
  2039. }
  2040. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  2041. static int test_wolfSSL_CertManagerAPI(void)
  2042. {
  2043. EXPECT_DECLS;
  2044. #ifndef NO_CERTS
  2045. WOLFSSL_CERT_MANAGER* cm = NULL;
  2046. unsigned char c;
  2047. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2048. wolfSSL_CertManagerFree(NULL);
  2049. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  2050. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  2051. #ifdef WOLFSSL_TRUST_PEER_CERT
  2052. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  2053. #endif
  2054. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  2055. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  2056. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2057. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  2058. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2059. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  2060. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2061. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  2062. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2063. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  2064. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2065. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  2066. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2067. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  2068. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2069. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  2070. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2071. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  2072. WOLFSSL_BAD_FILETYPE);
  2073. #endif
  2074. #if !defined(NO_FILESYSTEM)
  2075. {
  2076. const char* ca_cert = "./certs/ca-cert.pem";
  2077. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2078. const char* ca_cert_der = "./certs/ca-cert.der";
  2079. #endif
  2080. const char* ca_path = "./certs";
  2081. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2082. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  2083. BAD_FUNC_ARG);
  2084. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2085. BAD_FUNC_ARG);
  2086. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  2087. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  2088. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  2089. WOLFSSL_BAD_FILETYPE);
  2090. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  2091. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  2092. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2093. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2094. #endif
  2095. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2096. WOLFSSL_FATAL_ERROR);
  2097. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2098. WOLFSSL_FATAL_ERROR);
  2099. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2100. WOLFSSL_FATAL_ERROR);
  2101. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2102. WOLFSSL_FATAL_ERROR);
  2103. }
  2104. #endif
  2105. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2106. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2107. #elif !defined(HAVE_CRL)
  2108. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2109. #endif
  2110. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2111. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2112. #ifdef HAVE_CRL
  2113. /* Test APIs when CRL is disabled. */
  2114. #ifdef HAVE_CRL_IO
  2115. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2116. #endif
  2117. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2118. sizeof_server_cert_der_2048), 1);
  2119. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2120. #endif
  2121. /* OCSP */
  2122. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2123. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2124. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2125. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2126. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2127. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2128. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2129. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2130. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2131. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2132. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2133. #endif
  2134. #ifdef HAVE_OCSP
  2135. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2136. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2137. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2138. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2139. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2140. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2141. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2142. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2143. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2144. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2145. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2146. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2147. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2148. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2149. BAD_FUNC_ARG);
  2150. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2151. BAD_FUNC_ARG);
  2152. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2153. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2154. BAD_FUNC_ARG);
  2155. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2156. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2157. /* Test APIs when OCSP is disabled. */
  2158. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2159. NULL, NULL, NULL, NULL), 1);
  2160. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2161. #endif
  2162. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2163. if (EXPECT_SUCCESS()) {
  2164. wolfSSL_CertManagerFree(cm);
  2165. }
  2166. wolfSSL_CertManagerFree(cm);
  2167. cm = NULL;
  2168. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2169. #ifdef HAVE_OCSP
  2170. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2171. WOLFSSL_OCSP_CHECKALL), 1);
  2172. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2173. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2174. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2175. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2176. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2177. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2178. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2179. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2180. #endif
  2181. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2182. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2183. #endif
  2184. #ifdef WOLFSSL_TRUST_PEER_CERT
  2185. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2186. #endif
  2187. wolfSSL_CertManagerFree(cm);
  2188. #endif
  2189. return EXPECT_RESULT();
  2190. }
  2191. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2192. {
  2193. EXPECT_DECLS;
  2194. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2195. const char* ca_cert = "./certs/ca-cert.pem";
  2196. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2197. int ret;
  2198. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2199. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2200. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2201. #elif defined(NO_RSA)
  2202. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2203. #else
  2204. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2205. #endif
  2206. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2207. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2208. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2209. #elif defined(NO_RSA)
  2210. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2211. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2212. !defined(NO_ASN_TIME)
  2213. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2214. #else
  2215. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2216. #endif
  2217. #endif
  2218. return EXPECT_RESULT();
  2219. }
  2220. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2221. {
  2222. EXPECT_DECLS;
  2223. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2224. const char* ca_cert = "./certs/ca-cert.pem";
  2225. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2226. int ret;
  2227. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2228. 1);
  2229. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2230. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2231. #elif defined(NO_RSA)
  2232. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2233. #else
  2234. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2235. #endif
  2236. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2237. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2238. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2239. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2240. #elif defined(NO_RSA)
  2241. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2242. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2243. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2244. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2245. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2246. #else
  2247. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2248. #endif
  2249. #endif
  2250. return EXPECT_RESULT();
  2251. }
  2252. static int test_wolfSSL_CertManagerGetCerts(void)
  2253. {
  2254. EXPECT_DECLS;
  2255. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2256. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2257. defined(WOLFSSL_SIGNER_DER_CERT)
  2258. WOLFSSL_CERT_MANAGER* cm = NULL;
  2259. WOLFSSL_STACK* sk = NULL;
  2260. X509* x509 = NULL;
  2261. X509* cert1 = NULL;
  2262. FILE* file1 = NULL;
  2263. #ifdef DEBUG_WOLFSSL_VERBOSE
  2264. WOLFSSL_BIO* bio = NULL;
  2265. #endif
  2266. int i = 0;
  2267. int ret = 0;
  2268. const byte* der = NULL;
  2269. int derSz = 0;
  2270. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2271. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2272. if (file1 != NULL) {
  2273. fclose(file1);
  2274. }
  2275. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2276. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2277. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2278. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2279. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2280. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2281. * and full OpenSSL compatibility */
  2282. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2283. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2284. #else
  2285. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2286. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2287. #endif
  2288. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2289. "./certs/ca-cert.pem", NULL));
  2290. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2291. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2292. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2293. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2294. #ifdef DEBUG_WOLFSSL_VERBOSE
  2295. bio = BIO_new(wolfSSL_BIO_s_file());
  2296. if (bio != NULL) {
  2297. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2298. X509_print(bio, x509);
  2299. BIO_free(bio);
  2300. }
  2301. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2302. }
  2303. wolfSSL_X509_free(cert1);
  2304. sk_X509_pop_free(sk, NULL);
  2305. wolfSSL_CertManagerFree(cm);
  2306. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2307. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2308. defined(WOLFSSL_SIGNER_DER_CERT) */
  2309. return EXPECT_RESULT();
  2310. }
  2311. static int test_wolfSSL_CertManagerSetVerify(void)
  2312. {
  2313. EXPECT_DECLS;
  2314. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2315. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2316. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2317. WOLFSSL_CERT_MANAGER* cm = NULL;
  2318. int tmp = myVerifyAction;
  2319. const char* ca_cert = "./certs/ca-cert.pem";
  2320. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2321. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2322. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2323. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2324. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2325. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2326. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2327. #else
  2328. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2329. WOLFSSL_SUCCESS);
  2330. #endif
  2331. /* Use the test CB that always accepts certs */
  2332. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2333. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2334. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2335. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2336. {
  2337. const char* verifyCert = "./certs/server-cert.der";
  2338. /* Use the test CB that always fails certs */
  2339. myVerifyAction = VERIFY_FORCE_FAIL;
  2340. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2341. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2342. }
  2343. #endif
  2344. wolfSSL_CertManagerFree(cm);
  2345. myVerifyAction = tmp;
  2346. #endif
  2347. return EXPECT_RESULT();
  2348. }
  2349. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2350. defined(DEBUG_UNIT_TEST_CERTS)
  2351. /* Used when debugging name constraint tests. Not static to allow use in
  2352. * multiple locations with complex define guards. */
  2353. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2354. {
  2355. BIO* out = BIO_new_file(fileName, "wb");
  2356. if (out != NULL) {
  2357. PEM_write_bio_X509(out, x509);
  2358. BIO_free(out);
  2359. }
  2360. }
  2361. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2362. {
  2363. BIO* out = BIO_new_file(fileName, "wb");
  2364. if (out != NULL) {
  2365. BIO_write(out, der, derSz);
  2366. BIO_free(out);
  2367. }
  2368. }
  2369. #else
  2370. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2371. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2372. #endif
  2373. static int test_wolfSSL_CertManagerNameConstraint(void)
  2374. {
  2375. EXPECT_DECLS;
  2376. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2377. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2378. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2379. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2380. !defined(NO_SHA256)
  2381. WOLFSSL_CERT_MANAGER* cm = NULL;
  2382. WOLFSSL_EVP_PKEY *priv = NULL;
  2383. WOLFSSL_X509_NAME* name = NULL;
  2384. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2385. const char* server_cert = "./certs/test/server-goodcn.pem";
  2386. int i = 0;
  2387. static const byte extNameConsOid[] = {85, 29, 30};
  2388. RsaKey key;
  2389. WC_RNG rng;
  2390. byte *der = NULL;
  2391. int derSz = 0;
  2392. word32 idx = 0;
  2393. byte *pt;
  2394. WOLFSSL_X509 *x509 = NULL;
  2395. WOLFSSL_X509 *ca = NULL;
  2396. wc_InitRng(&rng);
  2397. /* load in CA private key for signing */
  2398. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2399. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2400. sizeof_server_key_der_2048), 0);
  2401. /* get ca certificate then alter it */
  2402. ExpectNotNull(der =
  2403. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2404. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2405. WOLFSSL_FILETYPE_ASN1));
  2406. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2407. if (EXPECT_SUCCESS() && (der != NULL)) {
  2408. XMEMCPY(der, pt, derSz);
  2409. /* find the name constraint extension and alter it */
  2410. pt = der;
  2411. for (i = 0; i < derSz - 3; i++) {
  2412. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2413. pt += 3;
  2414. break;
  2415. }
  2416. pt++;
  2417. }
  2418. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2419. /* go to the length value and set it to 0 */
  2420. while (i < derSz && *pt != 0x81) {
  2421. pt++;
  2422. i++;
  2423. }
  2424. ExpectIntNE(i, derSz); /* did not place to alter */
  2425. pt++;
  2426. *pt = 0x00;
  2427. }
  2428. /* resign the altered certificate */
  2429. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2430. FOURK_BUF, &key, NULL, &rng)), 0);
  2431. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2432. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2433. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2434. wolfSSL_CertManagerFree(cm);
  2435. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2436. wolfSSL_X509_free(x509);
  2437. wc_FreeRsaKey(&key);
  2438. wc_FreeRng(&rng);
  2439. /* add email alt name to satisfy constraint */
  2440. pt = (byte*)server_key_der_2048;
  2441. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2442. (const unsigned char**)&pt, sizeof_server_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 = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2447. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2448. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2449. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2450. /* Good cert test with proper alt email name */
  2451. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2452. WOLFSSL_FILETYPE_PEM));
  2453. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2454. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2455. name = NULL;
  2456. ExpectNotNull(name = X509_NAME_new());
  2457. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2458. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2459. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2460. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2461. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2462. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2463. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2464. X509_NAME_free(name);
  2465. name = NULL;
  2466. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2467. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2468. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2469. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2470. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2471. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2472. wolfSSL_X509_free(x509);
  2473. x509 = NULL;
  2474. /* Cert with bad alt name list */
  2475. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2476. WOLFSSL_FILETYPE_PEM));
  2477. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2478. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2479. name = NULL;
  2480. ExpectNotNull(name = X509_NAME_new());
  2481. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2482. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2483. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2484. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2485. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2486. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2487. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2488. X509_NAME_free(name);
  2489. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2490. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2491. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2492. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2493. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2494. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2495. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2496. wolfSSL_CertManagerFree(cm);
  2497. wolfSSL_X509_free(x509);
  2498. wolfSSL_X509_free(ca);
  2499. wolfSSL_EVP_PKEY_free(priv);
  2500. #endif
  2501. return EXPECT_RESULT();
  2502. }
  2503. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2504. {
  2505. EXPECT_DECLS;
  2506. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2507. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2508. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2509. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2510. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2511. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2512. const char* server_cert = "./certs/server-cert.pem";
  2513. WOLFSSL_CERT_MANAGER* cm = NULL;
  2514. WOLFSSL_X509 *x509 = NULL;
  2515. WOLFSSL_X509 *ca = NULL;
  2516. const unsigned char *der = NULL;
  2517. const unsigned char *pt;
  2518. WOLFSSL_EVP_PKEY *priv = NULL;
  2519. WOLFSSL_X509_NAME* name = NULL;
  2520. int derSz = 0;
  2521. /* C=US*/
  2522. char altName[] = {
  2523. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2524. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2525. };
  2526. /* C=ID */
  2527. char altNameFail[] = {
  2528. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2529. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2530. };
  2531. /* C=US ST=California*/
  2532. char altNameExc[] = {
  2533. 0x30, 0x22,
  2534. 0x31, 0x0B,
  2535. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2536. 0x31, 0x13,
  2537. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2538. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2539. };
  2540. /* load in CA private key for signing */
  2541. pt = ca_key_der_2048;
  2542. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2543. sizeof_ca_key_der_2048));
  2544. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2545. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2546. WOLFSSL_FILETYPE_ASN1));
  2547. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2548. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2549. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2550. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2551. WOLFSSL_FILETYPE_PEM));
  2552. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2553. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2554. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2555. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2556. #else
  2557. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2558. #endif
  2559. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2560. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2561. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2562. /* add in matching DIR alt name and resign */
  2563. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2564. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2565. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2566. #else
  2567. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2568. #endif
  2569. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2570. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2571. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2572. wolfSSL_X509_free(x509);
  2573. x509 = NULL;
  2574. /* check verify fail */
  2575. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2576. WOLFSSL_FILETYPE_PEM));
  2577. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2578. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2579. /* add in miss matching DIR alt name and resign */
  2580. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2581. ASN_DIR_TYPE);
  2582. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2583. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2584. #else
  2585. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2586. #endif
  2587. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2588. #ifndef WOLFSSL_NO_ASN_STRICT
  2589. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2590. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2591. #else
  2592. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2593. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2594. #endif
  2595. /* check that it still fails if one bad altname and one good altname is in
  2596. * the certificate */
  2597. wolfSSL_X509_free(x509);
  2598. x509 = NULL;
  2599. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2600. WOLFSSL_FILETYPE_PEM));
  2601. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2602. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2603. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2604. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2605. ASN_DIR_TYPE);
  2606. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2607. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2608. #else
  2609. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2610. #endif
  2611. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2612. #ifndef WOLFSSL_NO_ASN_STRICT
  2613. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2614. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2615. #else
  2616. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2617. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2618. #endif
  2619. /* check it fails with switching position of bad altname */
  2620. wolfSSL_X509_free(x509);
  2621. x509 = NULL;
  2622. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2623. WOLFSSL_FILETYPE_PEM));
  2624. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2625. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2626. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2627. ASN_DIR_TYPE);
  2628. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2629. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2630. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2631. #else
  2632. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2633. #endif
  2634. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2635. #ifndef WOLFSSL_NO_ASN_STRICT
  2636. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2637. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2638. #else
  2639. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2640. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2641. #endif
  2642. wolfSSL_CertManagerFree(cm);
  2643. wolfSSL_X509_free(x509);
  2644. x509 = NULL;
  2645. wolfSSL_X509_free(ca);
  2646. ca = NULL;
  2647. /* now test with excluded name constraint */
  2648. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2649. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2650. WOLFSSL_FILETYPE_ASN1));
  2651. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2652. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2653. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2654. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2655. WOLFSSL_FILETYPE_PEM));
  2656. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2657. ASN_DIR_TYPE);
  2658. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2659. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2660. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2661. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2662. #else
  2663. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2664. #endif
  2665. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2666. #ifndef WOLFSSL_NO_ASN_STRICT
  2667. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2668. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2669. #else
  2670. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2671. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2672. #endif
  2673. wolfSSL_CertManagerFree(cm);
  2674. wolfSSL_X509_free(x509);
  2675. wolfSSL_X509_free(ca);
  2676. wolfSSL_EVP_PKEY_free(priv);
  2677. #endif
  2678. return EXPECT_RESULT();
  2679. }
  2680. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2681. {
  2682. EXPECT_DECLS;
  2683. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2684. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2685. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2686. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2687. !defined(NO_SHA256)
  2688. WOLFSSL_CERT_MANAGER* cm = NULL;
  2689. WOLFSSL_EVP_PKEY *priv = NULL;
  2690. WOLFSSL_X509_NAME* name = NULL;
  2691. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2692. const char* server_cert = "./certs/test/server-goodcn.pem";
  2693. byte *der = NULL;
  2694. int derSz = 0;
  2695. byte *pt;
  2696. WOLFSSL_X509 *x509 = NULL;
  2697. WOLFSSL_X509 *ca = NULL;
  2698. pt = (byte*)server_key_der_2048;
  2699. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2700. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2701. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2702. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2703. WOLFSSL_FILETYPE_ASN1));
  2704. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2705. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2706. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2707. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2708. /* check satisfying .wolfssl.com constraint passes */
  2709. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2710. WOLFSSL_FILETYPE_PEM));
  2711. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2712. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2713. name = NULL;
  2714. ExpectNotNull(name = X509_NAME_new());
  2715. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2716. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2717. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2718. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2719. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2720. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2721. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2722. X509_NAME_free(name);
  2723. name = NULL;
  2724. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2725. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2726. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2727. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2728. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2729. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2730. wolfSSL_X509_free(x509);
  2731. x509 = NULL;
  2732. /* check satisfying .random.com constraint passes */
  2733. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2734. WOLFSSL_FILETYPE_PEM));
  2735. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2736. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2737. name = NULL;
  2738. ExpectNotNull(name = X509_NAME_new());
  2739. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2740. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2741. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2742. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2743. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2744. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2745. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2746. X509_NAME_free(name);
  2747. name = NULL;
  2748. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2749. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2750. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2751. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2752. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2753. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2754. wolfSSL_X509_free(x509);
  2755. x509 = NULL;
  2756. /* check fail case when neither constraint is matched */
  2757. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2758. WOLFSSL_FILETYPE_PEM));
  2759. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2760. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2761. name = NULL;
  2762. ExpectNotNull(name = X509_NAME_new());
  2763. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2764. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2765. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2766. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2767. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2768. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2769. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2770. X509_NAME_free(name);
  2771. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2772. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2773. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2774. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2775. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2776. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2777. wolfSSL_CertManagerFree(cm);
  2778. wolfSSL_X509_free(x509);
  2779. wolfSSL_X509_free(ca);
  2780. wolfSSL_EVP_PKEY_free(priv);
  2781. #endif
  2782. return EXPECT_RESULT();
  2783. }
  2784. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2785. {
  2786. EXPECT_DECLS;
  2787. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2788. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2789. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2790. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2791. !defined(NO_SHA256)
  2792. WOLFSSL_CERT_MANAGER* cm = NULL;
  2793. WOLFSSL_EVP_PKEY *priv = NULL;
  2794. WOLFSSL_X509_NAME* name = NULL;
  2795. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2796. const char* server_cert = "./certs/test/server-goodcn.pem";
  2797. byte *der = NULL;
  2798. int derSz;
  2799. byte *pt;
  2800. WOLFSSL_X509 *x509 = NULL;
  2801. WOLFSSL_X509 *ca = NULL;
  2802. pt = (byte*)server_key_der_2048;
  2803. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2804. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2805. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2806. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2807. WOLFSSL_FILETYPE_ASN1));
  2808. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2809. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2810. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2811. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2812. /* check satisfying wolfssl.com constraint passes */
  2813. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2814. WOLFSSL_FILETYPE_PEM));
  2815. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2816. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2817. name = NULL;
  2818. ExpectNotNull(name = X509_NAME_new());
  2819. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2820. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2821. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2822. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2823. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2824. X509_NAME_free(name);
  2825. name = NULL;
  2826. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2827. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2828. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2829. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2830. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2831. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2832. wolfSSL_X509_free(x509);
  2833. x509 = NULL;
  2834. /* check satisfying example.com constraint passes */
  2835. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2836. WOLFSSL_FILETYPE_PEM));
  2837. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2838. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2839. name = NULL;
  2840. ExpectNotNull(name = X509_NAME_new());
  2841. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2842. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2843. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2844. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2845. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2846. X509_NAME_free(name);
  2847. name = NULL;
  2848. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2849. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2850. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2851. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2852. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2853. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2854. wolfSSL_X509_free(x509);
  2855. x509 = NULL;
  2856. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2857. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2858. WOLFSSL_FILETYPE_PEM));
  2859. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2860. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2861. name = NULL;
  2862. ExpectNotNull(name = X509_NAME_new());
  2863. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2864. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2865. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2866. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2867. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2868. X509_NAME_free(name);
  2869. name = NULL;
  2870. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2871. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2872. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2873. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2874. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-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. /* check fail when one DNS in the list is bad */
  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, "www.wolfssl.com", ASN_DNS_TYPE);
  2895. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2896. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2897. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2898. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2899. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2900. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2901. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2902. wolfSSL_X509_free(x509);
  2903. x509 = NULL;
  2904. /* check fail case when neither constraint is matched */
  2905. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2906. WOLFSSL_FILETYPE_PEM));
  2907. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2908. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2909. name = NULL;
  2910. ExpectNotNull(name = X509_NAME_new());
  2911. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2912. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2913. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2914. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2915. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2916. X509_NAME_free(name);
  2917. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2918. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2919. DEBUG_WRITE_CERT_X509(x509, "bad-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_CertManagerFree(cm);
  2924. wolfSSL_X509_free(x509);
  2925. wolfSSL_X509_free(ca);
  2926. wolfSSL_EVP_PKEY_free(priv);
  2927. #endif
  2928. return EXPECT_RESULT();
  2929. }
  2930. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2931. {
  2932. EXPECT_DECLS;
  2933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2934. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2935. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2936. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2937. !defined(NO_SHA256)
  2938. WOLFSSL_CERT_MANAGER* cm = NULL;
  2939. WOLFSSL_EVP_PKEY *priv = NULL;
  2940. WOLFSSL_X509_NAME* name = NULL;
  2941. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2942. const char* server_cert = "./certs/test/server-goodcn.pem";
  2943. byte *der = NULL;
  2944. int derSz;
  2945. byte *pt;
  2946. WOLFSSL_X509 *x509 = NULL;
  2947. WOLFSSL_X509 *ca = NULL;
  2948. pt = (byte*)server_key_der_2048;
  2949. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2950. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2951. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2952. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2953. WOLFSSL_FILETYPE_ASN1));
  2954. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2955. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2956. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2957. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2958. /* check satisfying wolfssl.com constraint passes */
  2959. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2960. WOLFSSL_FILETYPE_PEM));
  2961. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2962. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2963. name = NULL;
  2964. ExpectNotNull(name = X509_NAME_new());
  2965. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2966. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2967. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2968. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2969. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2970. X509_NAME_free(name);
  2971. name = NULL;
  2972. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2973. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2974. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2975. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2976. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2977. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2978. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2979. wolfSSL_X509_free(x509);
  2980. x509 = NULL;
  2981. /* fail with DNS check because of common name */
  2982. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2983. WOLFSSL_FILETYPE_PEM));
  2984. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2985. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2986. name = NULL;
  2987. ExpectNotNull(name = X509_NAME_new());
  2988. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2989. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2990. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2991. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2992. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2993. X509_NAME_free(name);
  2994. name = NULL;
  2995. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2996. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2997. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2998. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2999. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3000. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3001. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3002. wolfSSL_X509_free(x509);
  3003. x509 = NULL;
  3004. /* fail on permitted DNS name constraint */
  3005. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3006. WOLFSSL_FILETYPE_PEM));
  3007. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3008. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3009. name = NULL;
  3010. ExpectNotNull(name = X509_NAME_new());
  3011. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3012. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3013. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3014. X509_NAME_free(name);
  3015. name = NULL;
  3016. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  3017. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  3018. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3019. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3020. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  3021. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3022. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3023. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3024. wolfSSL_X509_free(x509);
  3025. x509 = NULL;
  3026. /* fail on permitted email name constraint */
  3027. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3028. WOLFSSL_FILETYPE_PEM));
  3029. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3030. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3031. name = NULL;
  3032. ExpectNotNull(name = X509_NAME_new());
  3033. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3034. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3035. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3036. X509_NAME_free(name);
  3037. name = NULL;
  3038. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3039. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3040. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  3041. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3042. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  3043. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3044. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3045. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3046. wolfSSL_X509_free(x509);
  3047. x509 = NULL;
  3048. /* success with empty email name */
  3049. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3050. WOLFSSL_FILETYPE_PEM));
  3051. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3052. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3053. name = NULL;
  3054. ExpectNotNull(name = X509_NAME_new());
  3055. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3056. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3057. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3058. X509_NAME_free(name);
  3059. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3060. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3061. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  3062. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3063. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3064. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3065. wolfSSL_X509_free(x509);
  3066. wolfSSL_CertManagerFree(cm);
  3067. wolfSSL_X509_free(ca);
  3068. wolfSSL_EVP_PKEY_free(priv);
  3069. #endif
  3070. return EXPECT_RESULT();
  3071. }
  3072. static int test_wolfSSL_CertManagerCRL(void)
  3073. {
  3074. EXPECT_DECLS;
  3075. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  3076. !defined(NO_RSA)
  3077. const char* ca_cert = "./certs/ca-cert.pem";
  3078. const char* crl1 = "./certs/crl/crl.pem";
  3079. const char* crl2 = "./certs/crl/crl2.pem";
  3080. #ifdef WC_RSA_PSS
  3081. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  3082. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  3083. #endif
  3084. const unsigned char crl_buff[] = {
  3085. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  3086. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  3087. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  3088. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  3089. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  3090. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  3091. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  3092. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3093. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3094. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3095. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3096. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3097. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3098. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3099. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3100. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3101. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3102. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3103. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3104. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3105. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3106. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3107. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3108. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3109. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3110. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3111. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3112. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3113. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3114. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3115. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3116. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3117. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3118. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3119. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3120. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3121. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3122. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3123. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3124. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3125. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3126. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3127. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3128. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3129. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3130. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3131. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3132. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3133. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3134. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3135. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3136. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3137. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3138. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3139. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3140. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3141. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3142. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3143. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3144. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3145. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3146. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3147. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3148. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3149. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3150. };
  3151. WOLFSSL_CERT_MANAGER* cm = NULL;
  3152. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3153. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3154. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3155. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3156. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3157. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3158. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3159. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3160. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3161. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3162. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3163. BAD_FUNC_ARG);
  3164. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3165. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3166. BAD_FUNC_ARG);
  3167. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3168. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3169. BAD_FUNC_ARG);
  3170. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3171. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3172. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3173. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3174. #ifdef HAVE_CRL_IO
  3175. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3176. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3177. #endif
  3178. #ifndef NO_FILESYSTEM
  3179. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3180. 0), BAD_FUNC_ARG);
  3181. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3182. 0), BAD_FUNC_ARG);
  3183. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3184. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3185. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3186. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3187. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3188. BAD_FUNC_ARG);
  3189. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3190. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3191. ASN_PARSE_E);
  3192. #endif
  3193. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3194. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3195. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3196. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3197. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3198. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3199. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3200. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3201. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3202. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3203. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3204. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3205. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3206. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3207. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3208. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3209. ExpectIntEQ(WOLFSSL_SUCCESS,
  3210. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3211. ExpectIntEQ(WOLFSSL_SUCCESS,
  3212. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3213. ExpectIntEQ(WOLFSSL_SUCCESS,
  3214. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3215. wolfSSL_CertManagerFreeCRL(cm);
  3216. ExpectIntEQ(WOLFSSL_SUCCESS,
  3217. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3218. ExpectIntEQ(WOLFSSL_SUCCESS,
  3219. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3220. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3221. sizeof_server_cert_der_2048), CRL_MISSING);
  3222. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3223. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3224. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3225. WOLFSSL_FILETYPE_ASN1), 1);
  3226. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3227. /* loading should fail without the CA set */
  3228. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3229. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3230. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3231. ExpectIntEQ(WOLFSSL_SUCCESS,
  3232. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3233. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3234. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3235. #endif
  3236. wolfSSL_CertManagerFree(cm);
  3237. #endif
  3238. return EXPECT_RESULT();
  3239. }
  3240. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3241. {
  3242. EXPECT_DECLS;
  3243. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3244. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3245. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3246. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3247. defined(HAVE_LIGHTY)
  3248. WOLFSSL_CERT_MANAGER* cm = NULL;
  3249. /* Raw OCSP response bytes captured using the following setup:
  3250. * - Run responder with
  3251. * openssl ocsp -port 9999 -ndays 9999
  3252. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3253. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3254. * -rkey certs/ocsp/ocsp-responder-key.pem
  3255. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3256. * - Run client with
  3257. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3258. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3259. * -cert certs/ocsp/server1-cert.pem
  3260. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3261. * - Select the response packet in Wireshark, and export it using
  3262. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3263. * packets only". After importing into the editor, remove the initial
  3264. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3265. */
  3266. static const byte response[] = {
  3267. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3268. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3269. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3270. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3271. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3272. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3273. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3274. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3275. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3276. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3277. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3278. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3279. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3280. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3281. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3282. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3283. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3284. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3285. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3286. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3287. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3288. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3289. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3290. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3291. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3292. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3293. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3294. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3295. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3296. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3297. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3298. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3299. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3300. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3301. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3302. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3303. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3304. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3305. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3306. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3307. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3308. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3309. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3310. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3311. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3312. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3313. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3314. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3315. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3316. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3317. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3318. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3319. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3320. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3321. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3322. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3323. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3324. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3325. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3326. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3327. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3328. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3329. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3330. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3331. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3332. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3333. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3334. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3335. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3336. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3337. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3338. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3339. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3340. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3341. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3342. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3343. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3344. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3345. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3346. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3347. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3348. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3349. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3350. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3351. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3352. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3353. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3354. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3355. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3356. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3357. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3358. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3359. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3360. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3361. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3362. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3363. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3364. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3365. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3366. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3367. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3368. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3369. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3370. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3371. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3372. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3373. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3374. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3375. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3376. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3377. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3378. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3379. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3380. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3381. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3382. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3383. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3384. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3385. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3386. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3387. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3388. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3389. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3390. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3391. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3392. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3393. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3394. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3395. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3396. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3397. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3398. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3399. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3400. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3401. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3402. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3403. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3404. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3405. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3406. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3407. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3408. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3409. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3410. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3411. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3412. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3413. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3414. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3415. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3416. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3417. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3418. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3419. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3420. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3421. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3422. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3423. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3424. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3425. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3426. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3427. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3428. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3429. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3430. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3431. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3432. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3433. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3434. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3435. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3436. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3437. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3438. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3439. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3440. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3441. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3442. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3443. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3444. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3445. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3446. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3447. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3448. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3449. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3450. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3451. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3452. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3453. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3454. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3455. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3456. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3457. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3458. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3459. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3460. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3461. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3462. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3463. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3464. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3465. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3466. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3467. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3468. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3469. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3470. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3471. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3472. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3473. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3474. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3475. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3476. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3477. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3478. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3479. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3480. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3481. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3482. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3483. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3484. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3485. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3486. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3487. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3488. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3489. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3490. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3491. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3492. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3493. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3494. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3495. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3496. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3497. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3498. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3499. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3500. };
  3501. OcspEntry entry[1];
  3502. CertStatus status[1];
  3503. OcspRequest* request = NULL;
  3504. #ifndef NO_FILESYSTEM
  3505. const char* ca_cert = "./certs/ca-cert.pem";
  3506. #endif
  3507. byte serial[] = {0x05};
  3508. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3509. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3510. XMEMSET(entry, 0, sizeof(OcspEntry));
  3511. XMEMSET(status, 0, sizeof(CertStatus));
  3512. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3513. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3514. DYNAMIC_TYPE_OCSP_REQUEST));
  3515. if ((request != NULL) && (request->serial != NULL)) {
  3516. request->serialSz = sizeof(serial);
  3517. XMEMCPY(request->serial, serial, sizeof(serial));
  3518. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3519. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3520. }
  3521. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3522. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3523. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3524. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3525. /* Response should be valid. */
  3526. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3527. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3528. /* Flip a byte in the request serial number, response should be invalid
  3529. * now. */
  3530. if ((request != NULL) && (request->serial != NULL))
  3531. request->serial[0] ^= request->serial[0];
  3532. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3533. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3534. #ifndef NO_FILESYSTEM
  3535. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3536. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3537. ExpectIntEQ(WOLFSSL_SUCCESS,
  3538. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3539. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3540. sizeof(server_cert_der_2048)), 1);
  3541. #endif
  3542. wolfSSL_OCSP_REQUEST_free(request);
  3543. wolfSSL_CertManagerFree(cm);
  3544. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3545. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3546. #endif /* HAVE_OCSP */
  3547. return EXPECT_RESULT();
  3548. }
  3549. static int test_wolfSSL_CheckOCSPResponse(void)
  3550. {
  3551. EXPECT_DECLS;
  3552. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3553. defined(OPENSSL_ALL)
  3554. const char* responseFile = "./certs/ocsp/test-response.der";
  3555. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3556. const char* responseNoInternFile =
  3557. "./certs/ocsp/test-response-nointern.der";
  3558. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3559. OcspResponse* res = NULL;
  3560. byte data[4096];
  3561. const unsigned char* pt;
  3562. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3563. * gcc sanitizer with --enable-heapmath.
  3564. */
  3565. XFILE f = XBADFILE;
  3566. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3567. WOLFSSL_X509_STORE* st = NULL;
  3568. WOLFSSL_X509* issuer = NULL;
  3569. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3570. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3571. if (f != XBADFILE) {
  3572. XFCLOSE(f);
  3573. f = XBADFILE;
  3574. }
  3575. pt = data;
  3576. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3577. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3578. SSL_FILETYPE_PEM));
  3579. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3580. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3581. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3582. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3583. wolfSSL_OCSP_BASICRESP_free(bs);
  3584. bs = NULL;
  3585. wolfSSL_OCSP_RESPONSE_free(res);
  3586. res = NULL;
  3587. wolfSSL_X509_STORE_free(st);
  3588. st = NULL;
  3589. wolfSSL_X509_free(issuer);
  3590. issuer = NULL;
  3591. /* check loading a response with optional certs */
  3592. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3593. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3594. if (f != XBADFILE)
  3595. XFCLOSE(f);
  3596. f = XBADFILE;
  3597. pt = data;
  3598. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3599. wolfSSL_OCSP_RESPONSE_free(res);
  3600. res = NULL;
  3601. /* check loading a response with multiple certs */
  3602. {
  3603. WOLFSSL_CERT_MANAGER* cm = NULL;
  3604. OcspEntry *entry = NULL;
  3605. CertStatus* status = NULL;
  3606. OcspRequest* request = NULL;
  3607. byte serial1[] = {0x01};
  3608. byte serial[] = {0x02};
  3609. byte issuerHash[] = {
  3610. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3611. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3612. 0x37, 0x15, 0x8A, 0x88
  3613. };
  3614. byte issuerKeyHash[] = {
  3615. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3616. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3617. 0x7E, 0x72, 0x15, 0x21
  3618. };
  3619. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3620. DYNAMIC_TYPE_OPENSSL));
  3621. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3622. DYNAMIC_TYPE_OPENSSL));
  3623. if (entry != NULL)
  3624. XMEMSET(entry, 0, sizeof(OcspEntry));
  3625. if (status != NULL)
  3626. XMEMSET(status, 0, sizeof(CertStatus));
  3627. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3628. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3629. DYNAMIC_TYPE_OCSP_REQUEST));
  3630. if (request != NULL && request->serial != NULL) {
  3631. request->serialSz = sizeof(serial);
  3632. XMEMCPY(request->serial, serial, sizeof(serial));
  3633. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3634. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3635. sizeof(issuerKeyHash));
  3636. }
  3637. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3638. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3639. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3640. WOLFSSL_SUCCESS);
  3641. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3642. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3643. if (f != XBADFILE)
  3644. XFCLOSE(f);
  3645. f = XBADFILE;
  3646. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3647. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3648. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3649. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3650. ExpectNotNull(entry->status);
  3651. if (request != NULL && request->serial != NULL)
  3652. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3653. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3654. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3655. /* store both status's in the entry to check that "next" is not
  3656. * overwritten */
  3657. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3658. status->next = entry->status;
  3659. entry->status = status;
  3660. }
  3661. if (request != NULL && request->serial != NULL)
  3662. XMEMCPY(request->serial, serial, sizeof(serial));
  3663. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3664. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3665. ExpectNotNull(entry->status->next);
  3666. /* compare the status found */
  3667. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3668. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3669. status->serialSz), 0);
  3670. if (status != NULL && entry != NULL && entry->status != status) {
  3671. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3672. }
  3673. wolfSSL_OCSP_CERTID_free(entry);
  3674. wolfSSL_OCSP_REQUEST_free(request);
  3675. wolfSSL_CertManagerFree(cm);
  3676. }
  3677. #if defined(WC_RSA_PSS)
  3678. {
  3679. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3680. /* check loading a response with RSA-PSS signature */
  3681. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3682. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3683. if (f != XBADFILE)
  3684. XFCLOSE(f);
  3685. pt = data;
  3686. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3687. /* try to verify the response */
  3688. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3689. SSL_FILETYPE_PEM));
  3690. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3691. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3692. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3693. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3694. WOLFSSL_SUCCESS);
  3695. wolfSSL_OCSP_BASICRESP_free(bs);
  3696. wolfSSL_OCSP_RESPONSE_free(res);
  3697. wolfSSL_X509_STORE_free(st);
  3698. wolfSSL_X509_free(issuer);
  3699. }
  3700. #endif
  3701. #endif /* HAVE_OCSP */
  3702. return EXPECT_RESULT();
  3703. }
  3704. static int test_wolfSSL_FPKI(void)
  3705. {
  3706. EXPECT_DECLS;
  3707. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3708. XFILE f = XBADFILE;
  3709. const char* fpkiCert = "./certs/fpki-cert.der";
  3710. DecodedCert cert;
  3711. byte buf[4096];
  3712. byte* uuid = NULL;
  3713. byte* fascn = NULL;
  3714. word32 fascnSz;
  3715. word32 uuidSz;
  3716. int bytes = 0;
  3717. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3718. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3719. if (f != XBADFILE)
  3720. XFCLOSE(f);
  3721. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3722. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3723. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3724. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3725. DYNAMIC_TYPE_TMP_BUFFER));
  3726. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3727. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3728. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3729. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3730. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3731. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3732. wc_FreeDecodedCert(&cert);
  3733. #endif
  3734. return EXPECT_RESULT();
  3735. }
  3736. /* use RID in confuncture with other names to test parsing of unknown other
  3737. * names */
  3738. static int test_wolfSSL_OtherName(void)
  3739. {
  3740. EXPECT_DECLS;
  3741. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3742. XFILE f = XBADFILE;
  3743. const char* ridCert = "./certs/rid-cert.der";
  3744. DecodedCert cert;
  3745. byte buf[4096];
  3746. int bytes = 0;
  3747. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3748. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3749. if (f != XBADFILE)
  3750. XFCLOSE(f);
  3751. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3752. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3753. wc_FreeDecodedCert(&cert);
  3754. #endif
  3755. return EXPECT_RESULT();
  3756. }
  3757. static int test_wolfSSL_CertRsaPss(void)
  3758. {
  3759. EXPECT_DECLS;
  3760. /* FIPS v2 and below don't support long salts. */
  3761. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3762. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3763. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3764. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3765. XFILE f = XBADFILE;
  3766. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3767. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3768. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3769. RSA_MAX_SIZE >= 3072
  3770. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3771. #endif
  3772. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3773. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3774. #endif
  3775. DecodedCert cert;
  3776. byte buf[4096];
  3777. int bytes = 0;
  3778. WOLFSSL_CERT_MANAGER* cm = NULL;
  3779. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3780. ExpectIntEQ(WOLFSSL_SUCCESS,
  3781. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3782. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3783. ExpectIntEQ(WOLFSSL_SUCCESS,
  3784. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3785. #endif
  3786. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3787. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3788. if (f != XBADFILE) {
  3789. XFCLOSE(f);
  3790. f = XBADFILE;
  3791. }
  3792. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3793. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3794. wc_FreeDecodedCert(&cert);
  3795. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3796. RSA_MAX_SIZE >= 3072
  3797. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3798. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3799. if (f != XBADFILE)
  3800. XFCLOSE(f);
  3801. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3802. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3803. wc_FreeDecodedCert(&cert);
  3804. #endif
  3805. wolfSSL_CertManagerFree(cm);
  3806. #endif
  3807. return EXPECT_RESULT();
  3808. }
  3809. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3810. {
  3811. EXPECT_DECLS;
  3812. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3813. !defined(NO_WOLFSSL_CLIENT)
  3814. WOLFSSL_CTX* ctx = NULL;
  3815. const char* ca_cert = "./certs/ca-cert.pem";
  3816. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3817. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3818. /* test good CA */
  3819. ExpectTrue(WOLFSSL_SUCCESS ==
  3820. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3821. WOLFSSL_LOAD_FLAG_NONE));
  3822. /* test expired CA */
  3823. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3824. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3825. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3826. #else
  3827. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3828. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3829. #endif
  3830. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3831. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3832. wolfSSL_CTX_free(ctx);
  3833. #endif
  3834. return EXPECT_RESULT();
  3835. }
  3836. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3837. {
  3838. EXPECT_DECLS;
  3839. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3840. defined(USE_CERT_BUFFERS_2048)
  3841. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3842. WOLFSSL_CTX* ctx;
  3843. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3844. byte ca_expired_cert[TWOK_BUF];
  3845. word32 sizeof_ca_expired_cert = 0;
  3846. XFILE fp = XBADFILE;
  3847. #ifndef NO_WOLFSSL_CLIENT
  3848. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3849. #else
  3850. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3851. #endif
  3852. ExpectNotNull(ctx);
  3853. /* test good CA */
  3854. ExpectTrue(WOLFSSL_SUCCESS ==
  3855. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3856. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3857. WOLFSSL_LOAD_FLAG_NONE));
  3858. /* load expired CA */
  3859. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3860. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3861. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3862. sizeof(ca_expired_cert), fp), 0);
  3863. if (fp != XBADFILE)
  3864. XFCLOSE(fp);
  3865. /* test expired CA failure */
  3866. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3867. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3868. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3869. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3870. #else
  3871. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3872. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3873. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3874. #endif
  3875. /* test expired CA success */
  3876. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3877. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3878. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3879. wolfSSL_CTX_free(ctx);
  3880. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3881. #endif
  3882. return EXPECT_RESULT();
  3883. }
  3884. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3885. {
  3886. EXPECT_DECLS;
  3887. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3888. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3889. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3890. WOLFSSL_CTX* ctx = NULL;
  3891. #ifndef NO_WOLFSSL_CLIENT
  3892. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3893. #else
  3894. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3895. #endif
  3896. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3897. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3898. WOLFSSL_FILETYPE_ASN1));
  3899. wolfSSL_CTX_free(ctx);
  3900. #endif
  3901. return EXPECT_RESULT();
  3902. }
  3903. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3904. {
  3905. EXPECT_DECLS;
  3906. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3907. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3908. WOLFSSL_CTX* ctx;
  3909. WOLFSSL* ssl = NULL;
  3910. const char *certChain[] = {
  3911. "./certs/intermediate/client-int-cert.pem",
  3912. "./certs/intermediate/ca-int2-cert.pem",
  3913. "./certs/intermediate/ca-int-cert.pem",
  3914. "./certs/ca-cert.pem",
  3915. NULL
  3916. };
  3917. const char** cert;
  3918. WOLFSSL_X509* x509 = NULL;
  3919. WOLF_STACK_OF(X509)* chain = NULL;
  3920. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3921. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3922. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3923. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3924. WOLFSSL_FILETYPE_PEM));
  3925. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3926. X509_free(x509);
  3927. x509 = NULL;
  3928. }
  3929. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3930. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3931. WOLFSSL_FILETYPE_PEM));
  3932. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3933. X509_free(x509);
  3934. x509 = NULL;
  3935. }
  3936. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3937. ExpectIntEQ(sk_X509_num(chain), 3);
  3938. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3939. ExpectIntEQ(sk_X509_num(chain), 3);
  3940. SSL_free(ssl);
  3941. SSL_CTX_free(ctx);
  3942. #endif
  3943. return EXPECT_RESULT();
  3944. }
  3945. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3946. {
  3947. EXPECT_DECLS;
  3948. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3949. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3950. const char* server_chain_der = "./certs/server-cert-chain.der";
  3951. const char* client_single_pem = "./certs/client-cert.pem";
  3952. WOLFSSL_CTX* ctx;
  3953. (void)server_chain_der;
  3954. (void)client_single_pem;
  3955. (void)ctx;
  3956. #ifndef NO_WOLFSSL_CLIENT
  3957. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3958. #else
  3959. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3960. #endif
  3961. ExpectNotNull(ctx);
  3962. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3963. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3964. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3965. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3966. wolfSSL_CTX_free(ctx);
  3967. #endif
  3968. return EXPECT_RESULT();
  3969. }
  3970. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3971. {
  3972. EXPECT_DECLS;
  3973. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3974. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3975. WOLFSSL_CTX *ctx = NULL;
  3976. (void)ctx;
  3977. #ifndef NO_WOLFSSL_CLIENT
  3978. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3979. #else
  3980. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3981. #endif
  3982. /* invalid context */
  3983. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3984. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3985. /* invalid dhParamFile file */
  3986. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3987. NULL, WOLFSSL_FILETYPE_PEM));
  3988. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3989. bogusFile, WOLFSSL_FILETYPE_PEM));
  3990. /* success */
  3991. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3992. WOLFSSL_FILETYPE_PEM));
  3993. wolfSSL_CTX_free(ctx);
  3994. #endif
  3995. return EXPECT_RESULT();
  3996. }
  3997. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3998. {
  3999. EXPECT_DECLS;
  4000. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4001. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4002. WOLFSSL_CTX *ctx = NULL;
  4003. #ifndef NO_WOLFSSL_CLIENT
  4004. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4005. #else
  4006. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4007. #endif
  4008. /* invalid context */
  4009. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  4010. dh_key_der_2048, sizeof_dh_key_der_2048,
  4011. WOLFSSL_FILETYPE_ASN1));
  4012. /* invalid dhParamFile file */
  4013. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  4014. 0, WOLFSSL_FILETYPE_ASN1));
  4015. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4016. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  4017. WOLFSSL_FILETYPE_ASN1));
  4018. /* success */
  4019. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4020. dh_key_der_2048, sizeof_dh_key_der_2048,
  4021. WOLFSSL_FILETYPE_ASN1));
  4022. wolfSSL_CTX_free(ctx);
  4023. #endif
  4024. return EXPECT_RESULT();
  4025. }
  4026. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  4027. {
  4028. EXPECT_DECLS;
  4029. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4030. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4031. WOLFSSL_CTX *ctx;
  4032. (void)ctx;
  4033. #ifndef NO_WOLFSSL_CLIENT
  4034. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4035. #else
  4036. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4037. #endif
  4038. ExpectNotNull(ctx);
  4039. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4040. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  4041. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4042. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  4043. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4044. dh_key_der_2048, sizeof_dh_key_der_2048,
  4045. WOLFSSL_FILETYPE_ASN1));
  4046. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4047. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4048. dh_key_der_2048, sizeof_dh_key_der_2048,
  4049. WOLFSSL_FILETYPE_ASN1));
  4050. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  4051. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4052. dh_key_der_2048, sizeof_dh_key_der_2048,
  4053. WOLFSSL_FILETYPE_ASN1));
  4054. wolfSSL_CTX_free(ctx);
  4055. #endif
  4056. return EXPECT_RESULT();
  4057. }
  4058. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  4059. {
  4060. EXPECT_DECLS;
  4061. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  4062. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4063. WOLFSSL_CTX* ctx = NULL;
  4064. const char* derCert = "./certs/server-cert.der";
  4065. const char* nullPath = NULL;
  4066. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  4067. const char* emptyPath = "";
  4068. /* der load Case 1 ctx NULL */
  4069. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4070. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4071. #ifndef NO_WOLFSSL_CLIENT
  4072. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4073. #else
  4074. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4075. #endif
  4076. /* Case 2 filePath NULL */
  4077. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  4078. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4079. /* Case 3 invalid format */
  4080. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4081. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  4082. /* Case 4 filePath not valid */
  4083. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  4084. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4085. /* Case 5 filePath empty */
  4086. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  4087. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4088. #ifndef NO_RSA
  4089. /* Case 6 success case */
  4090. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4091. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4092. #endif
  4093. wolfSSL_CTX_free(ctx);
  4094. #endif
  4095. return EXPECT_RESULT();
  4096. }
  4097. static int test_wolfSSL_CTX_enable_disable(void)
  4098. {
  4099. EXPECT_DECLS;
  4100. #ifndef NO_CERTS
  4101. WOLFSSL_CTX* ctx = NULL;
  4102. #ifdef HAVE_CRL
  4103. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4104. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4105. #endif
  4106. #ifdef HAVE_OCSP
  4107. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4108. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4109. #endif
  4110. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4111. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4112. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4113. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4114. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4115. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4116. #endif
  4117. #ifndef NO_WOLFSSL_CLIENT
  4118. #ifdef HAVE_EXTENDED_MASTER
  4119. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4120. #endif
  4121. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4122. #ifdef HAVE_EXTENDED_MASTER
  4123. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4124. #endif
  4125. #elif !defined(NO_WOLFSSL_SERVER)
  4126. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4127. #endif
  4128. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4129. #ifdef HAVE_CRL
  4130. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4131. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4132. #endif
  4133. #ifdef HAVE_OCSP
  4134. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4135. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4136. WOLFSSL_SUCCESS);
  4137. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4138. WOLFSSL_SUCCESS);
  4139. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4140. WOLFSSL_SUCCESS);
  4141. #endif
  4142. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4143. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4144. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4145. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4146. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4147. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4148. #endif
  4149. wolfSSL_CTX_free(ctx);
  4150. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4151. #endif /* NO_CERTS */
  4152. return EXPECT_RESULT();
  4153. }
  4154. static int test_wolfSSL_CTX_ticket_API(void)
  4155. {
  4156. EXPECT_DECLS;
  4157. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4158. WOLFSSL_CTX* ctx = NULL;
  4159. void *userCtx = (void*)"this is my ctx";
  4160. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4161. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4162. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4163. wolfSSL_CTX_free(ctx);
  4164. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4165. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4166. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4167. return EXPECT_RESULT();
  4168. }
  4169. static int test_wolfSSL_set_minmax_proto_version(void)
  4170. {
  4171. EXPECT_DECLS;
  4172. #ifdef OPENSSL_EXTRA
  4173. WOLFSSL_CTX *ctx = NULL;
  4174. WOLFSSL *ssl = NULL;
  4175. (void)ssl;
  4176. #ifndef NO_WOLFSSL_CLIENT
  4177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4178. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4179. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4180. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4181. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4182. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4183. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4184. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4185. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4186. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4187. wolfSSL_free(ssl);
  4188. wolfSSL_CTX_free(ctx);
  4189. ctx = NULL;
  4190. #endif
  4191. #ifndef NO_WOLFSSL_SERVER
  4192. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4193. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4194. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4195. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4196. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4197. wolfSSL_CTX_free(ctx);
  4198. #endif
  4199. #endif
  4200. return EXPECT_RESULT();
  4201. }
  4202. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4203. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4204. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4205. {
  4206. EXPECT_DECLS;
  4207. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4208. return EXPECT_RESULT();
  4209. }
  4210. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4211. {
  4212. EXPECT_DECLS;
  4213. /* Set TLS 1.2 */
  4214. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4215. WOLFSSL_SUCCESS);
  4216. return EXPECT_RESULT();
  4217. }
  4218. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4219. * what was set at ctx creation. */
  4220. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4221. {
  4222. EXPECT_DECLS;
  4223. test_ssl_cbf client_cbs;
  4224. test_ssl_cbf server_cbs;
  4225. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4226. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4227. client_cbs.method = wolfTLS_client_method;
  4228. server_cbs.method = wolfTLS_server_method;
  4229. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4230. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4231. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4232. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4233. &server_cbs, NULL), TEST_SUCCESS);
  4234. return EXPECT_RESULT();
  4235. }
  4236. #else
  4237. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4238. {
  4239. return TEST_SKIPPED;
  4240. }
  4241. #endif
  4242. /*----------------------------------------------------------------------------*
  4243. | SSL
  4244. *----------------------------------------------------------------------------*/
  4245. static int test_server_wolfSSL_new(void)
  4246. {
  4247. EXPECT_DECLS;
  4248. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4249. !defined(NO_WOLFSSL_SERVER)
  4250. WOLFSSL_CTX *ctx = NULL;
  4251. WOLFSSL_CTX *ctx_nocert = NULL;
  4252. WOLFSSL *ssl = NULL;
  4253. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4254. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4255. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4256. WOLFSSL_FILETYPE_PEM));
  4257. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4258. WOLFSSL_FILETYPE_PEM));
  4259. /* invalid context */
  4260. ExpectNull(ssl = wolfSSL_new(NULL));
  4261. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4262. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4263. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4264. #endif
  4265. /* success */
  4266. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4267. wolfSSL_free(ssl);
  4268. wolfSSL_CTX_free(ctx);
  4269. wolfSSL_CTX_free(ctx_nocert);
  4270. #endif
  4271. return EXPECT_RESULT();
  4272. }
  4273. static int test_client_wolfSSL_new(void)
  4274. {
  4275. EXPECT_DECLS;
  4276. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4277. !defined(NO_WOLFSSL_CLIENT)
  4278. WOLFSSL_CTX *ctx = NULL;
  4279. WOLFSSL_CTX *ctx_nocert = NULL;
  4280. WOLFSSL *ssl = NULL;
  4281. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4282. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4283. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4284. /* invalid context */
  4285. ExpectNull(ssl = wolfSSL_new(NULL));
  4286. /* success */
  4287. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4288. wolfSSL_free(ssl);
  4289. ssl = NULL;
  4290. /* success */
  4291. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4292. wolfSSL_free(ssl);
  4293. wolfSSL_CTX_free(ctx);
  4294. wolfSSL_CTX_free(ctx_nocert);
  4295. #endif
  4296. return EXPECT_RESULT();
  4297. }
  4298. static int test_wolfSSL_SetTmpDH_file(void)
  4299. {
  4300. EXPECT_DECLS;
  4301. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4302. !defined(NO_WOLFSSL_SERVER)
  4303. WOLFSSL_CTX *ctx = NULL;
  4304. WOLFSSL *ssl = NULL;
  4305. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4306. #ifndef NO_RSA
  4307. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4308. WOLFSSL_FILETYPE_PEM));
  4309. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4310. WOLFSSL_FILETYPE_PEM));
  4311. #elif defined(HAVE_ECC)
  4312. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4313. WOLFSSL_FILETYPE_PEM));
  4314. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4315. WOLFSSL_FILETYPE_PEM));
  4316. #elif defined(HAVE_ED25519)
  4317. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4318. WOLFSSL_FILETYPE_PEM));
  4319. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4320. WOLFSSL_FILETYPE_PEM));
  4321. #elif defined(HAVE_ED448)
  4322. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4323. WOLFSSL_FILETYPE_PEM));
  4324. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4325. WOLFSSL_FILETYPE_PEM));
  4326. #endif
  4327. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4328. /* invalid ssl */
  4329. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4330. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4331. /* invalid dhParamFile file */
  4332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4333. NULL, WOLFSSL_FILETYPE_PEM));
  4334. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4335. bogusFile, WOLFSSL_FILETYPE_PEM));
  4336. /* success */
  4337. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4338. WOLFSSL_FILETYPE_PEM));
  4339. wolfSSL_free(ssl);
  4340. wolfSSL_CTX_free(ctx);
  4341. #endif
  4342. return EXPECT_RESULT();
  4343. }
  4344. static int test_wolfSSL_SetTmpDH_buffer(void)
  4345. {
  4346. EXPECT_DECLS;
  4347. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4348. WOLFSSL_CTX *ctx = NULL;
  4349. WOLFSSL *ssl = NULL;
  4350. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4351. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4352. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4353. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4354. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4355. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4356. /* invalid ssl */
  4357. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4358. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4359. /* invalid dhParamFile file */
  4360. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4361. 0, WOLFSSL_FILETYPE_ASN1));
  4362. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4363. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4364. /* success */
  4365. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4366. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4367. wolfSSL_free(ssl);
  4368. wolfSSL_CTX_free(ctx);
  4369. #endif
  4370. return EXPECT_RESULT();
  4371. }
  4372. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4373. {
  4374. EXPECT_DECLS;
  4375. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4376. WOLFSSL_CTX *ctx = NULL;
  4377. WOLFSSL_CTX *ctx2 = NULL;
  4378. WOLFSSL *ssl = NULL;
  4379. WOLFSSL *ssl2 = NULL;
  4380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4381. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4382. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4383. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4384. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4385. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4386. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4387. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4388. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4389. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4390. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4391. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4392. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4393. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4394. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4395. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4396. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4397. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4398. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4399. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4400. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4401. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4402. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4403. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4404. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4405. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4406. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4407. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4408. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4409. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4410. wolfSSL_free(ssl2);
  4411. wolfSSL_CTX_free(ctx2);
  4412. wolfSSL_free(ssl);
  4413. wolfSSL_CTX_free(ctx);
  4414. #endif
  4415. return EXPECT_RESULT();
  4416. }
  4417. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4418. * allowed.
  4419. * POST: return 1 on success.
  4420. */
  4421. static int test_wolfSSL_SetMinVersion(void)
  4422. {
  4423. int res = TEST_SKIPPED;
  4424. #ifndef NO_WOLFSSL_CLIENT
  4425. int failFlag = WOLFSSL_SUCCESS;
  4426. WOLFSSL_CTX* ctx = NULL;
  4427. WOLFSSL* ssl = NULL;
  4428. int itr;
  4429. #ifndef NO_OLD_TLS
  4430. const int versions[] = {
  4431. #ifdef WOLFSSL_ALLOW_TLSV10
  4432. WOLFSSL_TLSV1,
  4433. #endif
  4434. WOLFSSL_TLSV1_1,
  4435. WOLFSSL_TLSV1_2};
  4436. #elif !defined(WOLFSSL_NO_TLS12)
  4437. const int versions[] = { WOLFSSL_TLSV1_2 };
  4438. #else
  4439. const int versions[] = { WOLFSSL_TLSV1_3 };
  4440. #endif
  4441. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4442. ssl = wolfSSL_new(ctx);
  4443. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4444. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4445. failFlag = WOLFSSL_FAILURE;
  4446. }
  4447. }
  4448. wolfSSL_free(ssl);
  4449. wolfSSL_CTX_free(ctx);
  4450. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4451. #endif
  4452. return res;
  4453. } /* END test_wolfSSL_SetMinVersion */
  4454. #ifdef OPENSSL_EXTRA
  4455. static int test_EC25519(void)
  4456. {
  4457. EXPECT_DECLS;
  4458. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4459. byte priv[CURVE25519_KEYSIZE];
  4460. unsigned int privSz = CURVE25519_KEYSIZE;
  4461. byte pub[CURVE25519_KEYSIZE];
  4462. unsigned int pubSz = CURVE25519_KEYSIZE;
  4463. byte priv2[CURVE25519_KEYSIZE];
  4464. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4465. byte pub2[CURVE25519_KEYSIZE];
  4466. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4467. byte shared[CURVE25519_KEYSIZE];
  4468. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4469. byte shared2[CURVE25519_KEYSIZE];
  4470. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4471. /* Bad parameter testing of key generation. */
  4472. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4473. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4474. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4475. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4476. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4477. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4478. /* Bad length */
  4479. privSz = 1;
  4480. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4481. privSz = CURVE25519_KEYSIZE;
  4482. pubSz = 1;
  4483. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4484. pubSz = CURVE25519_KEYSIZE;
  4485. /* Good case of generating key. */
  4486. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4487. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4488. 1);
  4489. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4490. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4491. /* Bad parameter testing of shared key. */
  4492. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4493. NULL, pubSz), 0);
  4494. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4495. NULL, pubSz), 0);
  4496. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4497. pub, pubSz), 0);
  4498. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4499. pub, pubSz), 0);
  4500. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4501. NULL, pubSz), 0);
  4502. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4503. pub, pubSz), 0);
  4504. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4505. pub, pubSz), 0);
  4506. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4507. pub, pubSz), 0);
  4508. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4509. NULL, pubSz), 0);
  4510. /* Bad length. */
  4511. sharedSz = 1;
  4512. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4513. pub, pubSz), 0);
  4514. sharedSz = CURVE25519_KEYSIZE;
  4515. privSz = 1;
  4516. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4517. pub, pubSz), 0);
  4518. privSz = CURVE25519_KEYSIZE;
  4519. pubSz = 1;
  4520. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4521. pub, pubSz), 0);
  4522. pubSz = CURVE25519_KEYSIZE;
  4523. /* Good case of shared key. */
  4524. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4525. pub2, pub2Sz), 1);
  4526. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4527. pub, pubSz), 1);
  4528. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4529. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4530. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4531. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4532. return EXPECT_RESULT();
  4533. }
  4534. static int test_ED25519(void)
  4535. {
  4536. EXPECT_DECLS;
  4537. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4538. defined(WOLFSSL_KEY_GEN)
  4539. byte priv[ED25519_PRV_KEY_SIZE];
  4540. unsigned int privSz = (unsigned int)sizeof(priv);
  4541. byte pub[ED25519_PUB_KEY_SIZE];
  4542. unsigned int pubSz = (unsigned int)sizeof(pub);
  4543. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4544. const char* msg = TEST_STRING;
  4545. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4546. byte sig[ED25519_SIG_SIZE];
  4547. unsigned int sigSz = (unsigned int)sizeof(sig);
  4548. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4549. /* Bad parameter testing of key generation. */
  4550. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4551. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4552. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4553. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4554. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4555. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4556. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4557. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4558. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4559. /* Bad length. */
  4560. privSz = 1;
  4561. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4562. privSz = ED25519_PRV_KEY_SIZE;
  4563. pubSz = 1;
  4564. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4565. pubSz = ED25519_PUB_KEY_SIZE;
  4566. /* Good case of generating key. */
  4567. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4568. 1);
  4569. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4570. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4571. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4572. /* Bad parameter testing of signing. */
  4573. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4574. NULL), 0);
  4575. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4576. NULL), 0);
  4577. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4578. NULL), 0);
  4579. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4580. NULL), 0);
  4581. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4582. &sigSz), 0);
  4583. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4584. &sigSz), 0);
  4585. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4586. &sigSz), 0);
  4587. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4588. &sigSz), 0);
  4589. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4590. NULL), 0);
  4591. /* Bad length. */
  4592. privSz = 1;
  4593. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4594. &sigSz), 0);
  4595. privSz = ED25519_PRV_KEY_SIZE;
  4596. sigSz = 1;
  4597. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4598. &sigSz), 0);
  4599. sigSz = ED25519_SIG_SIZE;
  4600. /* Good case of signing. */
  4601. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4602. &sigSz), 1);
  4603. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4604. #ifdef HAVE_ED25519_VERIFY
  4605. /* Bad parameter testing of verification. */
  4606. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4607. sigSz), 0);
  4608. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4609. sigSz), 0);
  4610. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4611. sigSz), 0);
  4612. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4613. sigSz), 0);
  4614. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4615. sigSz), 0);
  4616. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4617. sigSz), 0);
  4618. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4619. sigSz), 0);
  4620. /* Bad length. */
  4621. pubSz = 1;
  4622. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4623. sigSz), 0);
  4624. pubSz = ED25519_PUB_KEY_SIZE;
  4625. sigSz = 1;
  4626. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4627. sigSz), 0);
  4628. sigSz = ED25519_SIG_SIZE;
  4629. /* Good case of verification. */
  4630. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4631. sigSz), 1);
  4632. /* Bad signature. */
  4633. if (EXPECT_SUCCESS()) {
  4634. sig[1] ^= 0x80;
  4635. }
  4636. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4637. sigSz), 0);
  4638. #endif /* HAVE_ED25519_VERIFY */
  4639. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4640. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4641. return EXPECT_RESULT();
  4642. }
  4643. static int test_EC448(void)
  4644. {
  4645. EXPECT_DECLS;
  4646. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  4647. byte priv[CURVE448_KEY_SIZE];
  4648. unsigned int privSz = CURVE448_KEY_SIZE;
  4649. byte pub[CURVE448_KEY_SIZE];
  4650. unsigned int pubSz = CURVE448_KEY_SIZE;
  4651. byte priv2[CURVE448_KEY_SIZE];
  4652. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  4653. byte pub2[CURVE448_KEY_SIZE];
  4654. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  4655. byte shared[CURVE448_KEY_SIZE];
  4656. unsigned int sharedSz = CURVE448_KEY_SIZE;
  4657. byte shared2[CURVE448_KEY_SIZE];
  4658. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  4659. /* Bad parameter testing of key generation. */
  4660. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  4661. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4662. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4663. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  4664. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4665. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  4666. /* Bad length. */
  4667. privSz = 1;
  4668. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4669. privSz = CURVE448_KEY_SIZE;
  4670. pubSz = 1;
  4671. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4672. pubSz = CURVE448_KEY_SIZE;
  4673. /* Good case of generating key. */
  4674. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4675. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  4676. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  4677. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  4678. /* Bad parameter testing of shared key. */
  4679. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  4680. NULL, pubSz), 0);
  4681. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  4682. NULL, pubSz), 0);
  4683. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4684. pub, pubSz), 0);
  4685. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4686. pub, pubSz), 0);
  4687. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4688. NULL, pubSz), 0);
  4689. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4690. pub, pubSz), 0);
  4691. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  4692. pub, pubSz), 0);
  4693. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4694. pub, pubSz), 0);
  4695. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4696. NULL, pubSz), 0);
  4697. /* Bad length. */
  4698. sharedSz = 1;
  4699. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4700. pub, pubSz), 0);
  4701. sharedSz = CURVE448_KEY_SIZE;
  4702. privSz = 1;
  4703. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4704. pub, pubSz), 0);
  4705. privSz = CURVE448_KEY_SIZE;
  4706. pubSz = 1;
  4707. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4708. pub, pubSz), 0);
  4709. pubSz = CURVE448_KEY_SIZE;
  4710. /* Good case of shared key. */
  4711. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4712. pub2, pub2Sz), 1);
  4713. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4714. pub, pubSz), 1);
  4715. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  4716. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  4717. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4718. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  4719. return EXPECT_RESULT();
  4720. }
  4721. static int test_ED448(void)
  4722. {
  4723. EXPECT_DECLS;
  4724. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4725. defined(WOLFSSL_KEY_GEN)
  4726. byte priv[ED448_PRV_KEY_SIZE];
  4727. unsigned int privSz = (unsigned int)sizeof(priv);
  4728. byte pub[ED448_PUB_KEY_SIZE];
  4729. unsigned int pubSz = (unsigned int)sizeof(pub);
  4730. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4731. const char* msg = TEST_STRING;
  4732. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4733. byte sig[ED448_SIG_SIZE];
  4734. unsigned int sigSz = (unsigned int)sizeof(sig);
  4735. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4736. /* Bad parameter testing of key generation. */
  4737. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  4738. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  4739. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  4740. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  4741. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4742. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4743. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  4744. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4745. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  4746. /* Bad length. */
  4747. privSz = 1;
  4748. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4749. privSz = ED448_PRV_KEY_SIZE;
  4750. pubSz = 1;
  4751. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4752. pubSz = ED448_PUB_KEY_SIZE;
  4753. /* Good case of generating key. */
  4754. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4755. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4756. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4757. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4758. /* Bad parameter testing of signing. */
  4759. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4760. NULL), 0);
  4761. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4762. NULL), 0);
  4763. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  4764. NULL), 0);
  4765. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  4766. NULL), 0);
  4767. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4768. &sigSz), 0);
  4769. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  4770. &sigSz), 0);
  4771. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  4772. &sigSz), 0);
  4773. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  4774. &sigSz), 0);
  4775. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4776. NULL), 0);
  4777. /* Bad length. */
  4778. privSz = 1;
  4779. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4780. &sigSz), 0);
  4781. privSz = ED448_PRV_KEY_SIZE;
  4782. sigSz = 1;
  4783. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4784. &sigSz), 0);
  4785. sigSz = ED448_SIG_SIZE;
  4786. /* Good case of signing. */
  4787. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4788. &sigSz), 1);
  4789. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4790. #ifdef HAVE_ED448_VERIFY
  4791. /* Bad parameter testing of verification. */
  4792. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  4793. sigSz), 0);
  4794. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4795. sigSz), 0);
  4796. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  4797. sigSz), 0);
  4798. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  4799. sigSz), 0);
  4800. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  4801. sigSz), 0);
  4802. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4803. sigSz), 0);
  4804. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4805. sigSz), 0);
  4806. /* Bad length. */
  4807. pubSz = 1;
  4808. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4809. sigSz), 0);
  4810. pubSz = ED448_PUB_KEY_SIZE;
  4811. sigSz = 1;
  4812. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4813. sigSz), 0);
  4814. sigSz = ED448_SIG_SIZE;
  4815. /* Good case of verification. */
  4816. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4817. sigSz), 1);
  4818. /* Bad signature. */
  4819. if (EXPECT_SUCCESS()) {
  4820. sig[1] ^= 0x80;
  4821. }
  4822. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4823. sigSz), 0);
  4824. #endif /* HAVE_ED448_VERIFY */
  4825. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4826. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4827. return EXPECT_RESULT();
  4828. }
  4829. #endif /* OPENSSL_EXTRA */
  4830. #include <wolfssl/openssl/pem.h>
  4831. /*----------------------------------------------------------------------------*
  4832. | EVP
  4833. *----------------------------------------------------------------------------*/
  4834. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4835. {
  4836. EXPECT_DECLS;
  4837. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4838. WOLFSSL_BIO* rbio = NULL;
  4839. WOLFSSL_BIO* wbio = NULL;
  4840. WOLFSSL_EVP_PKEY* pkey = NULL;
  4841. char line[256] = { 0 };
  4842. char line1[256] = { 0 };
  4843. int i = 0;
  4844. /* test error cases */
  4845. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4846. /*
  4847. * test RSA public key print
  4848. * in this test, pass '3' for indent
  4849. */
  4850. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4851. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4852. sizeof_client_keypub_der_1024));
  4853. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4854. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4855. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4856. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4857. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4858. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4859. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4860. strcpy(line1, " Modulus:\n");
  4861. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4862. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4863. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4864. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4865. /* skip to the end of modulus element*/
  4866. for (i = 0; i < 8 ;i++) {
  4867. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4868. }
  4869. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4870. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4871. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4872. /* should reach EOF */
  4873. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4874. EVP_PKEY_free(pkey);
  4875. pkey = NULL;
  4876. BIO_free(rbio);
  4877. BIO_free(wbio);
  4878. rbio = NULL;
  4879. wbio = NULL;
  4880. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4881. /*
  4882. * test DSA public key print
  4883. */
  4884. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4885. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4886. sizeof_dsa_pub_key_der_2048));
  4887. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4888. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4889. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4890. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4891. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4892. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4893. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4894. strcpy(line1, "pub:\n");
  4895. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4896. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4897. strcpy(line1,
  4898. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4899. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4900. /* skip to the end of pub element*/
  4901. for (i = 0; i < 17 ;i++) {
  4902. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4903. }
  4904. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4905. strcpy(line1, "P:\n");
  4906. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4907. /* skip to the end of P element*/
  4908. for (i = 0; i < 18 ;i++) {
  4909. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4910. }
  4911. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4912. strcpy(line1, "Q:\n");
  4913. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4914. /* skip to the end of Q element*/
  4915. for (i = 0; i < 3 ;i++) {
  4916. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4917. }
  4918. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4919. strcpy(line1, "G:\n");
  4920. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4921. /* skip to the end of G element*/
  4922. for (i = 0; i < 18 ;i++) {
  4923. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4924. }
  4925. /* should reach EOF */
  4926. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4927. EVP_PKEY_free(pkey);
  4928. pkey = NULL;
  4929. BIO_free(rbio);
  4930. BIO_free(wbio);
  4931. rbio = NULL;
  4932. wbio = NULL;
  4933. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4934. /*
  4935. * test ECC public key print
  4936. */
  4937. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4938. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4939. sizeof_ecc_clikeypub_der_256));
  4940. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4941. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4942. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4943. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4944. strcpy(line1, "Public-Key: (256 bit)\n");
  4945. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4946. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4947. strcpy(line1, "pub:\n");
  4948. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4949. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4950. strcpy(line1,
  4951. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4952. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4953. /* skip to the end of pub element*/
  4954. for (i = 0; i < 4 ;i++) {
  4955. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4956. }
  4957. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4958. strcpy(line1, "ASN1 OID: prime256v1\n");
  4959. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4960. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4961. strcpy(line1, "NIST CURVE: P-256\n");
  4962. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4963. /* should reach EOF */
  4964. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4965. EVP_PKEY_free(pkey);
  4966. pkey = NULL;
  4967. BIO_free(rbio);
  4968. BIO_free(wbio);
  4969. rbio = NULL;
  4970. wbio = NULL;
  4971. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4972. /*
  4973. * test DH public key print
  4974. */
  4975. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4976. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4977. sizeof_dh_pub_key_der_2048));
  4978. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4979. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4980. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4981. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4982. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4983. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4984. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4985. strcpy(line1, "public-key:\n");
  4986. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4987. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4988. strcpy(line1,
  4989. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4990. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4991. /* skip to the end of public-key element*/
  4992. for (i = 0; i < 17 ;i++) {
  4993. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4994. }
  4995. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4996. strcpy(line1, "prime:\n");
  4997. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4998. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4999. strcpy(line1,
  5000. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  5001. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5002. /* skip to the end of prime element*/
  5003. for (i = 0; i < 17 ;i++) {
  5004. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5005. }
  5006. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5007. strcpy(line1, "generator: 2 (0x02)\n");
  5008. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5009. /* should reach EOF */
  5010. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5011. EVP_PKEY_free(pkey);
  5012. pkey = NULL;
  5013. BIO_free(rbio);
  5014. BIO_free(wbio);
  5015. rbio = NULL;
  5016. wbio = NULL;
  5017. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  5018. /* to prevent "unused variable" warning */
  5019. (void)pkey;
  5020. (void)wbio;
  5021. (void)rbio;
  5022. (void)line;
  5023. (void)line1;
  5024. (void)i;
  5025. #endif /* OPENSSL_EXTRA */
  5026. return EXPECT_RESULT();
  5027. }
  5028. /* Test functions for base64 encode/decode */
  5029. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  5030. {
  5031. EXPECT_DECLS;
  5032. #if defined(OPENSSL_EXTRA) && \
  5033. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5034. EVP_ENCODE_CTX* ctx = NULL;
  5035. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5036. ExpectIntEQ(ctx->remaining,0);
  5037. ExpectIntEQ(ctx->data[0],0);
  5038. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  5039. EVP_ENCODE_CTX_free(ctx);
  5040. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5041. return EXPECT_RESULT();
  5042. }
  5043. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  5044. {
  5045. EXPECT_DECLS;
  5046. #if defined(OPENSSL_EXTRA) && \
  5047. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5048. EVP_ENCODE_CTX* ctx = NULL;
  5049. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5050. EVP_ENCODE_CTX_free(ctx);
  5051. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5052. return EXPECT_RESULT();
  5053. }
  5054. static int test_wolfSSL_EVP_EncodeInit(void)
  5055. {
  5056. EXPECT_DECLS;
  5057. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5058. EVP_ENCODE_CTX* ctx = NULL;
  5059. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5060. ExpectIntEQ(ctx->remaining, 0);
  5061. ExpectIntEQ(ctx->data[0], 0);
  5062. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5063. if (ctx != NULL) {
  5064. /* make ctx dirty */
  5065. ctx->remaining = 10;
  5066. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  5067. }
  5068. EVP_EncodeInit(ctx);
  5069. ExpectIntEQ(ctx->remaining, 0);
  5070. ExpectIntEQ(ctx->data[0], 0);
  5071. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5072. EVP_ENCODE_CTX_free(ctx);
  5073. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5074. return EXPECT_RESULT();
  5075. }
  5076. static int test_wolfSSL_EVP_EncodeUpdate(void)
  5077. {
  5078. EXPECT_DECLS;
  5079. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5080. int outl;
  5081. int total;
  5082. const unsigned char plain0[] = {"Th"};
  5083. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  5084. const unsigned char plain2[] = {"This is additional data."};
  5085. const unsigned char encBlock0[] = {"VGg="};
  5086. const unsigned char enc0[] = {"VGg=\n"};
  5087. /* expected encoded result for the first output 64 chars plus trailing LF*/
  5088. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  5089. const unsigned char enc2[] =
  5090. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  5091. unsigned char encOutBuff[300];
  5092. EVP_ENCODE_CTX* ctx = NULL;
  5093. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5094. EVP_EncodeInit(ctx);
  5095. /* illegal parameter test */
  5096. ExpectIntEQ(
  5097. EVP_EncodeUpdate(
  5098. NULL, /* pass NULL as ctx */
  5099. encOutBuff,
  5100. &outl,
  5101. plain1,
  5102. sizeof(plain1)-1),
  5103. 0 /* expected result code 0: fail */
  5104. );
  5105. ExpectIntEQ(
  5106. EVP_EncodeUpdate(
  5107. ctx,
  5108. NULL, /* pass NULL as out buff */
  5109. &outl,
  5110. plain1,
  5111. sizeof(plain1)-1),
  5112. 0 /* expected result code 0: fail */
  5113. );
  5114. ExpectIntEQ(
  5115. EVP_EncodeUpdate(
  5116. ctx,
  5117. encOutBuff,
  5118. NULL, /* pass NULL as outl */
  5119. plain1,
  5120. sizeof(plain1)-1),
  5121. 0 /* expected result code 0: fail */
  5122. );
  5123. ExpectIntEQ(
  5124. EVP_EncodeUpdate(
  5125. ctx,
  5126. encOutBuff,
  5127. &outl,
  5128. NULL, /* pass NULL as in */
  5129. sizeof(plain1)-1),
  5130. 0 /* expected result code 0: fail */
  5131. );
  5132. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5133. /* meaningless parameter test */
  5134. ExpectIntEQ(
  5135. EVP_EncodeUpdate(
  5136. ctx,
  5137. encOutBuff,
  5138. &outl,
  5139. plain1,
  5140. 0), /* pass zero input */
  5141. 1 /* expected result code 1: success */
  5142. );
  5143. /* very small data encoding test */
  5144. EVP_EncodeInit(ctx);
  5145. ExpectIntEQ(
  5146. EVP_EncodeUpdate(
  5147. ctx,
  5148. encOutBuff,
  5149. &outl,
  5150. plain0,
  5151. sizeof(plain0)-1),
  5152. 1 /* expected result code 1: success */
  5153. );
  5154. ExpectIntEQ(outl,0);
  5155. if (EXPECT_SUCCESS()) {
  5156. EVP_EncodeFinal(
  5157. ctx,
  5158. encOutBuff + outl,
  5159. &outl);
  5160. }
  5161. ExpectIntEQ( outl, sizeof(enc0)-1);
  5162. ExpectIntEQ(
  5163. XSTRNCMP(
  5164. (const char*)encOutBuff,
  5165. (const char*)enc0,sizeof(enc0) ),
  5166. 0);
  5167. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5168. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5169. sizeof(encBlock0)-1);
  5170. ExpectStrEQ(encOutBuff, encBlock0);
  5171. /* pass small size( < 48bytes ) input, then make sure they are not
  5172. * encoded and just stored in ctx
  5173. */
  5174. EVP_EncodeInit(ctx);
  5175. total = 0;
  5176. outl = 0;
  5177. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5178. ExpectIntEQ(
  5179. EVP_EncodeUpdate(
  5180. ctx,
  5181. encOutBuff, /* buffer for output */
  5182. &outl, /* size of output */
  5183. plain1, /* input */
  5184. sizeof(plain1)-1), /* size of input */
  5185. 1); /* expected result code 1:success */
  5186. total += outl;
  5187. ExpectIntEQ(outl, 0); /* no output expected */
  5188. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5189. ExpectTrue(
  5190. XSTRNCMP((const char*)(ctx->data),
  5191. (const char*)plain1,
  5192. ctx->remaining) ==0 );
  5193. ExpectTrue(encOutBuff[0] == 0);
  5194. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5195. * the stored data and the new input together
  5196. */
  5197. ExpectIntEQ(
  5198. EVP_EncodeUpdate(
  5199. ctx,
  5200. encOutBuff + outl, /* buffer for output */
  5201. &outl, /* size of output */
  5202. plain2, /* additional input */
  5203. sizeof(plain2) -1), /* size of additional input */
  5204. 1); /* expected result code 1:success */
  5205. total += outl;
  5206. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5207. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5208. ExpectIntEQ(
  5209. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5210. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5211. EVP_EncodeFinal(
  5212. ctx,
  5213. encOutBuff + outl,
  5214. &outl);
  5215. total += outl;
  5216. ExpectIntNE(total,0);
  5217. ExpectIntNE(outl,0);
  5218. ExpectIntEQ(XSTRNCMP(
  5219. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5220. /* test with illeagal parameters */
  5221. outl = 1;
  5222. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5223. ExpectIntEQ(outl, 0);
  5224. outl = 1;
  5225. EVP_EncodeFinal(ctx, NULL, &outl);
  5226. ExpectIntEQ(outl, 0);
  5227. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5228. EVP_EncodeFinal(NULL, NULL, NULL);
  5229. EVP_ENCODE_CTX_free(ctx);
  5230. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5231. return EXPECT_RESULT();
  5232. }
  5233. static int test_wolfSSL_EVP_EncodeFinal(void)
  5234. {
  5235. int res = TEST_SKIPPED;
  5236. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5237. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5238. * test_wolfSSL_EVP_EncodeUpdate
  5239. */
  5240. res = TEST_SUCCESS;
  5241. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5242. return res;
  5243. }
  5244. static int test_wolfSSL_EVP_DecodeInit(void)
  5245. {
  5246. EXPECT_DECLS;
  5247. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5248. EVP_ENCODE_CTX* ctx = NULL;
  5249. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5250. ExpectIntEQ( ctx->remaining,0);
  5251. ExpectIntEQ( ctx->data[0],0);
  5252. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5253. if (ctx != NULL) {
  5254. /* make ctx dirty */
  5255. ctx->remaining = 10;
  5256. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5257. }
  5258. EVP_DecodeInit(ctx);
  5259. ExpectIntEQ( ctx->remaining,0);
  5260. ExpectIntEQ( ctx->data[0],0);
  5261. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5262. EVP_ENCODE_CTX_free(ctx);
  5263. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5264. return EXPECT_RESULT();
  5265. }
  5266. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5267. {
  5268. EXPECT_DECLS;
  5269. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5270. int outl;
  5271. unsigned char decOutBuff[300];
  5272. EVP_ENCODE_CTX* ctx = NULL;
  5273. static const unsigned char enc1[] =
  5274. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5275. /* const unsigned char plain1[] =
  5276. {"This is a base64 decoding test."} */
  5277. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5278. EVP_DecodeInit(ctx);
  5279. /* illegal parameter tests */
  5280. /* pass NULL as ctx */
  5281. ExpectIntEQ(
  5282. EVP_DecodeUpdate(
  5283. NULL, /* pass NULL as ctx */
  5284. decOutBuff,
  5285. &outl,
  5286. enc1,
  5287. sizeof(enc1)-1),
  5288. -1 /* expected result code -1: fail */
  5289. );
  5290. ExpectIntEQ( outl, 0);
  5291. /* pass NULL as output */
  5292. ExpectIntEQ(
  5293. EVP_DecodeUpdate(
  5294. ctx,
  5295. NULL, /* pass NULL as out buff */
  5296. &outl,
  5297. enc1,
  5298. sizeof(enc1)-1),
  5299. -1 /* expected result code -1: fail */
  5300. );
  5301. ExpectIntEQ( outl, 0);
  5302. /* pass NULL as outl */
  5303. ExpectIntEQ(
  5304. EVP_DecodeUpdate(
  5305. ctx,
  5306. decOutBuff,
  5307. NULL, /* pass NULL as outl */
  5308. enc1,
  5309. sizeof(enc1)-1),
  5310. -1 /* expected result code -1: fail */
  5311. );
  5312. /* pass NULL as input */
  5313. ExpectIntEQ(
  5314. EVP_DecodeUpdate(
  5315. ctx,
  5316. decOutBuff,
  5317. &outl,
  5318. NULL, /* pass NULL as in */
  5319. sizeof(enc1)-1),
  5320. -1 /* expected result code -1: fail */
  5321. );
  5322. ExpectIntEQ( outl, 0);
  5323. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5324. /* pass zero length input */
  5325. ExpectIntEQ(
  5326. EVP_DecodeUpdate(
  5327. ctx,
  5328. decOutBuff,
  5329. &outl,
  5330. enc1,
  5331. 0), /* pass zero as input len */
  5332. 1 /* expected result code 1: success */
  5333. );
  5334. /* decode correct base64 string */
  5335. {
  5336. static const unsigned char enc2[] =
  5337. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5338. static const unsigned char plain2[] =
  5339. {"This is a base64 decoding test."};
  5340. EVP_EncodeInit(ctx);
  5341. ExpectIntEQ(
  5342. EVP_DecodeUpdate(
  5343. ctx,
  5344. decOutBuff,
  5345. &outl,
  5346. enc2,
  5347. sizeof(enc2)-1),
  5348. 0 /* expected result code 0: success */
  5349. );
  5350. ExpectIntEQ(outl,sizeof(plain2) -1);
  5351. ExpectIntEQ(
  5352. EVP_DecodeFinal(
  5353. ctx,
  5354. decOutBuff + outl,
  5355. &outl),
  5356. 1 /* expected result code 1: success */
  5357. );
  5358. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5359. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5360. sizeof(plain2) -1 ),0);
  5361. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5362. sizeof(plain2)-1);
  5363. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5364. sizeof(plain2) -1 ),0);
  5365. }
  5366. /* decode correct base64 string which does not have '\n' in its last*/
  5367. {
  5368. static const unsigned char enc3[] =
  5369. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5370. static const unsigned char plain3[] =
  5371. {"This is a base64 decoding test."}; /* 31 chars */
  5372. EVP_EncodeInit(ctx);
  5373. ExpectIntEQ(
  5374. EVP_DecodeUpdate(
  5375. ctx,
  5376. decOutBuff,
  5377. &outl,
  5378. enc3,
  5379. sizeof(enc3)-1),
  5380. 0 /* expected result code 0: success */
  5381. );
  5382. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5383. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5384. sizeof(plain3) -1 ),0);
  5385. ExpectIntEQ(
  5386. EVP_DecodeFinal(
  5387. ctx,
  5388. decOutBuff + outl,
  5389. &outl),
  5390. 1 /* expected result code 1: success */
  5391. );
  5392. ExpectIntEQ(outl,0 );
  5393. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5394. sizeof(plain3)-1);
  5395. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5396. sizeof(plain3) -1 ),0);
  5397. }
  5398. /* decode string which has a padding char ('=') in the illegal position*/
  5399. {
  5400. static const unsigned char enc4[] =
  5401. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5402. EVP_EncodeInit(ctx);
  5403. ExpectIntEQ(
  5404. EVP_DecodeUpdate(
  5405. ctx,
  5406. decOutBuff,
  5407. &outl,
  5408. enc4,
  5409. sizeof(enc4)-1),
  5410. -1 /* expected result code -1: error */
  5411. );
  5412. ExpectIntEQ(outl,0);
  5413. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5414. }
  5415. /* small data decode test */
  5416. {
  5417. static const unsigned char enc00[] = {"VG"};
  5418. static const unsigned char enc01[] = {"g=\n"};
  5419. static const unsigned char plain4[] = {"Th"};
  5420. EVP_EncodeInit(ctx);
  5421. ExpectIntEQ(
  5422. EVP_DecodeUpdate(
  5423. ctx,
  5424. decOutBuff,
  5425. &outl,
  5426. enc00,
  5427. sizeof(enc00)-1),
  5428. 1 /* expected result code 1: success */
  5429. );
  5430. ExpectIntEQ(outl,0);
  5431. ExpectIntEQ(
  5432. EVP_DecodeUpdate(
  5433. ctx,
  5434. decOutBuff + outl,
  5435. &outl,
  5436. enc01,
  5437. sizeof(enc01)-1),
  5438. 0 /* expected result code 0: success */
  5439. );
  5440. ExpectIntEQ(outl,sizeof(plain4)-1);
  5441. /* test with illegal parameters */
  5442. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5443. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5444. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5445. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5446. if (EXPECT_SUCCESS()) {
  5447. EVP_DecodeFinal(
  5448. ctx,
  5449. decOutBuff + outl,
  5450. &outl);
  5451. }
  5452. ExpectIntEQ( outl, 0);
  5453. ExpectIntEQ(
  5454. XSTRNCMP(
  5455. (const char*)decOutBuff,
  5456. (const char*)plain4,sizeof(plain4)-1 ),
  5457. 0);
  5458. }
  5459. EVP_ENCODE_CTX_free(ctx);
  5460. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5461. return EXPECT_RESULT();
  5462. }
  5463. static int test_wolfSSL_EVP_DecodeFinal(void)
  5464. {
  5465. int res = TEST_SKIPPED;
  5466. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5467. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5468. * test_wolfSSL_EVP_DecodeUpdate
  5469. */
  5470. res = TEST_SUCCESS;
  5471. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5472. return res;
  5473. }
  5474. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5475. */
  5476. #ifdef OPENSSL_EXTRA
  5477. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5478. {
  5479. EXPECT_DECLS;
  5480. #ifndef NO_AES
  5481. const WOLFSSL_EVP_CIPHER* c;
  5482. c = wolfSSL_EVP_get_cipherbynid(419);
  5483. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5484. defined(WOLFSSL_AES_128)
  5485. ExpectNotNull(c);
  5486. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5487. #else
  5488. ExpectNull(c);
  5489. #endif
  5490. c = wolfSSL_EVP_get_cipherbynid(423);
  5491. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5492. defined(WOLFSSL_AES_192)
  5493. ExpectNotNull(c);
  5494. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5495. #else
  5496. ExpectNull(c);
  5497. #endif
  5498. c = wolfSSL_EVP_get_cipherbynid(427);
  5499. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5500. defined(WOLFSSL_AES_256)
  5501. ExpectNotNull(c);
  5502. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5503. #else
  5504. ExpectNull(c);
  5505. #endif
  5506. c = wolfSSL_EVP_get_cipherbynid(904);
  5507. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5508. ExpectNotNull(c);
  5509. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5510. #else
  5511. ExpectNull(c);
  5512. #endif
  5513. c = wolfSSL_EVP_get_cipherbynid(905);
  5514. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5515. ExpectNotNull(c);
  5516. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5517. #else
  5518. ExpectNull(c);
  5519. #endif
  5520. c = wolfSSL_EVP_get_cipherbynid(906);
  5521. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5522. ExpectNotNull(c);
  5523. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5524. #else
  5525. ExpectNull(c);
  5526. #endif
  5527. c = wolfSSL_EVP_get_cipherbynid(418);
  5528. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5529. ExpectNotNull(c);
  5530. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5531. #else
  5532. ExpectNull(c);
  5533. #endif
  5534. c = wolfSSL_EVP_get_cipherbynid(422);
  5535. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5536. ExpectNotNull(c);
  5537. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5538. #else
  5539. ExpectNull(c);
  5540. #endif
  5541. c = wolfSSL_EVP_get_cipherbynid(426);
  5542. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5543. ExpectNotNull(c);
  5544. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5545. #else
  5546. ExpectNull(c);
  5547. #endif
  5548. #endif /* !NO_AES */
  5549. #ifndef NO_DES3
  5550. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5551. #ifdef WOLFSSL_DES_ECB
  5552. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5553. #endif
  5554. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5555. #ifdef WOLFSSL_DES_ECB
  5556. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5557. #endif
  5558. #endif /* !NO_DES3 */
  5559. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5560. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5561. #endif
  5562. /* test for nid is out of range */
  5563. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5564. return EXPECT_RESULT();
  5565. }
  5566. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5567. {
  5568. EXPECT_DECLS;
  5569. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5570. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5571. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5572. const EVP_CIPHER *test;
  5573. byte key[AES_BLOCK_SIZE] = {0};
  5574. byte iv[AES_BLOCK_SIZE] = {0};
  5575. ExpectNotNull(ctx);
  5576. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5577. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5578. test = EVP_CIPHER_CTX_cipher(ctx);
  5579. ExpectTrue(init == test);
  5580. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5581. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5582. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5583. EVP_CIPHER_CTX_free(ctx);
  5584. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5585. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5586. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5587. return EXPECT_RESULT();
  5588. }
  5589. #endif /* OPENSSL_EXTRA */
  5590. /*----------------------------------------------------------------------------*
  5591. | IO
  5592. *----------------------------------------------------------------------------*/
  5593. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5594. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5595. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5596. #ifdef WC_SHA512_DIGEST_SIZE
  5597. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5598. #else
  5599. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5600. #endif
  5601. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5602. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5603. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5604. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5605. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5606. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5607. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5608. defined(HAVE_AES_CBC)
  5609. typedef struct openssl_key_ctx {
  5610. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5611. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5612. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5613. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5614. } openssl_key_ctx;
  5615. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5616. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5617. static WC_INLINE int OpenSSLTicketInit(void)
  5618. {
  5619. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5620. if (ret != 0) return ret;
  5621. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5622. sizeof(myOpenSSLKey_ctx.name));
  5623. if (ret != 0) return ret;
  5624. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5625. sizeof(myOpenSSLKey_ctx.key));
  5626. if (ret != 0) return ret;
  5627. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5628. sizeof(myOpenSSLKey_ctx.hmacKey));
  5629. if (ret != 0) return ret;
  5630. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5631. sizeof(myOpenSSLKey_ctx.iv));
  5632. if (ret != 0) return ret;
  5633. return 0;
  5634. }
  5635. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5636. byte name[WOLFSSL_TICKET_NAME_SZ],
  5637. byte iv[WOLFSSL_TICKET_IV_SZ],
  5638. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5639. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5640. (void)ssl;
  5641. if (enc) {
  5642. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5643. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5644. }
  5645. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5646. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5647. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5648. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5649. return 0;
  5650. }
  5651. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5652. if (enc)
  5653. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5654. else
  5655. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5656. return 1;
  5657. }
  5658. static WC_INLINE void OpenSSLTicketCleanup(void)
  5659. {
  5660. wc_FreeRng(&myOpenSSLKey_rng);
  5661. }
  5662. #endif
  5663. #endif
  5664. /* helper functions */
  5665. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5666. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5667. void *ctx)
  5668. {
  5669. struct test_ssl_memio_ctx *test_ctx;
  5670. byte *buf;
  5671. int *len;
  5672. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5673. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5674. buf = test_ctx->c_buff;
  5675. len = &test_ctx->c_len;
  5676. }
  5677. else {
  5678. buf = test_ctx->s_buff;
  5679. len = &test_ctx->s_len;
  5680. }
  5681. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5682. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5683. XMEMCPY(buf + *len, data, sz);
  5684. *len += sz;
  5685. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5686. {
  5687. /* This can be imported into Wireshark by transforming the file with
  5688. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5689. * And then loading test_output.dump.hex into Wireshark using the
  5690. * "Import from Hex Dump..." option ion and selecting the TCP
  5691. * encapsulation option. */
  5692. char dump_file_name[64];
  5693. WOLFSSL_BIO *dump_file;
  5694. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5695. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5696. if (dump_file != NULL) {
  5697. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5698. wolfSSL_BIO_free(dump_file);
  5699. }
  5700. }
  5701. #endif
  5702. return sz;
  5703. }
  5704. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5705. void *ctx)
  5706. {
  5707. struct test_ssl_memio_ctx *test_ctx;
  5708. int read_sz;
  5709. byte *buf;
  5710. int *len;
  5711. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5712. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5713. buf = test_ctx->s_buff;
  5714. len = &test_ctx->s_len;
  5715. }
  5716. else {
  5717. buf = test_ctx->c_buff;
  5718. len = &test_ctx->c_len;
  5719. }
  5720. if (*len == 0)
  5721. return WOLFSSL_CBIO_ERR_WANT_READ;
  5722. read_sz = sz < *len ? sz : *len;
  5723. XMEMCPY(data, buf, read_sz);
  5724. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5725. *len -= read_sz;
  5726. return read_sz;
  5727. }
  5728. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5729. {
  5730. EXPECT_DECLS;
  5731. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5732. int c_sharedCtx = 0;
  5733. int s_sharedCtx = 0;
  5734. #endif
  5735. const char* clientCertFile = cliCertFile;
  5736. const char* clientKeyFile = cliKeyFile;
  5737. const char* serverCertFile = svrCertFile;
  5738. const char* serverKeyFile = svrKeyFile;
  5739. /********************************
  5740. * Create WOLFSSL_CTX for client.
  5741. ********************************/
  5742. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5743. if (ctx->c_ctx != NULL) {
  5744. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5745. }
  5746. else
  5747. #endif
  5748. {
  5749. WOLFSSL_METHOD* method = NULL;
  5750. if (ctx->c_cb.method != NULL) {
  5751. method = ctx->c_cb.method();
  5752. }
  5753. else {
  5754. method = wolfSSLv23_client_method();
  5755. }
  5756. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5757. }
  5758. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5759. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5760. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5761. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5762. #endif
  5763. if (ctx->c_cb.caPemFile != NULL)
  5764. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5765. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5766. else
  5767. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5768. caCertFile, 0), WOLFSSL_SUCCESS);
  5769. if (ctx->c_cb.certPemFile != NULL) {
  5770. clientCertFile = ctx->c_cb.certPemFile;
  5771. }
  5772. if (ctx->c_cb.keyPemFile != NULL) {
  5773. clientKeyFile = ctx->c_cb.keyPemFile;
  5774. }
  5775. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5776. if (!c_sharedCtx)
  5777. #endif
  5778. {
  5779. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5780. clientCertFile), WOLFSSL_SUCCESS);
  5781. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5782. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5783. }
  5784. #ifdef HAVE_CRL
  5785. if (ctx->c_cb.crlPemFile != NULL) {
  5786. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5787. WOLFSSL_SUCCESS);
  5788. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5789. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5790. }
  5791. #endif
  5792. if (ctx->c_ciphers != NULL) {
  5793. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5794. WOLFSSL_SUCCESS);
  5795. }
  5796. if (ctx->c_cb.ctx_ready != NULL) {
  5797. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5798. }
  5799. /********************************
  5800. * Create WOLFSSL_CTX for server.
  5801. ********************************/
  5802. if (ctx->s_ctx != NULL) {
  5803. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5804. s_sharedCtx = 1;
  5805. #endif
  5806. ctx->s_cb.isSharedCtx = 1;
  5807. }
  5808. else
  5809. {
  5810. WOLFSSL_METHOD* method = NULL;
  5811. if (ctx->s_cb.method != NULL) {
  5812. method = ctx->s_cb.method();
  5813. }
  5814. else {
  5815. method = wolfSSLv23_server_method();
  5816. }
  5817. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5818. ctx->s_cb.isSharedCtx = 0;
  5819. }
  5820. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5821. #if defined(HAVE_SESSION_TICKET) && \
  5822. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5823. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5824. OpenSSLTicketInit();
  5825. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5826. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5827. TicketInit();
  5828. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5829. #endif
  5830. #endif
  5831. }
  5832. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5833. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5834. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5835. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5836. if (ctx->s_cb.caPemFile != NULL)
  5837. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5838. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5839. else
  5840. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5841. cliCertFile, 0), WOLFSSL_SUCCESS);
  5842. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5843. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5844. #endif
  5845. if (ctx->s_cb.certPemFile != NULL) {
  5846. serverCertFile = ctx->s_cb.certPemFile;
  5847. }
  5848. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5849. if (!s_sharedCtx)
  5850. #endif
  5851. {
  5852. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5853. serverCertFile), WOLFSSL_SUCCESS);
  5854. }
  5855. if (ctx->s_cb.keyPemFile != NULL) {
  5856. serverKeyFile = ctx->s_cb.keyPemFile;
  5857. }
  5858. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5859. if (!s_sharedCtx)
  5860. #endif
  5861. {
  5862. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5863. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5864. }
  5865. if (ctx->s_ciphers != NULL) {
  5866. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5867. WOLFSSL_SUCCESS);
  5868. }
  5869. if (ctx->s_cb.ctx_ready != NULL) {
  5870. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5871. }
  5872. /****************************
  5873. * Create WOLFSSL for client.
  5874. ****************************/
  5875. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5876. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5877. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5878. if (0
  5879. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5880. || c_sharedCtx
  5881. #endif
  5882. )
  5883. {
  5884. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5885. clientCertFile), WOLFSSL_SUCCESS);
  5886. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5887. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5888. }
  5889. if (ctx->c_cb.ssl_ready != NULL) {
  5890. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5891. }
  5892. /****************************
  5893. * Create WOLFSSL for server.
  5894. ****************************/
  5895. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5896. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5897. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5898. if (0
  5899. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5900. || s_sharedCtx
  5901. #endif
  5902. )
  5903. {
  5904. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5905. serverCertFile), WOLFSSL_SUCCESS);
  5906. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5907. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5908. }
  5909. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5910. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5911. #elif !defined(NO_DH)
  5912. /* will repick suites with DHE, higher priority than PSK */
  5913. SetDH(ctx->s_ssl);
  5914. #endif
  5915. if (ctx->s_cb.ssl_ready != NULL) {
  5916. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5917. }
  5918. return EXPECT_RESULT();
  5919. }
  5920. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5921. int* rounds)
  5922. {
  5923. int handshake_complete = 0;
  5924. int hs_c = 0;
  5925. int hs_s = 0;
  5926. int failing_s = 0;
  5927. int failing_c = 0;
  5928. int ret;
  5929. int err;
  5930. if (rounds != NULL) {
  5931. *rounds = 0;
  5932. }
  5933. while ((!handshake_complete) && (max_rounds > 0)) {
  5934. if (!hs_c) {
  5935. wolfSSL_SetLoggingPrefix("client");
  5936. ret = wolfSSL_connect(ctx->c_ssl);
  5937. wolfSSL_SetLoggingPrefix(NULL);
  5938. if (ret == WOLFSSL_SUCCESS) {
  5939. hs_c = 1;
  5940. }
  5941. else {
  5942. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5943. if (err != WOLFSSL_ERROR_WANT_READ &&
  5944. err != WOLFSSL_ERROR_WANT_WRITE) {
  5945. char buff[WOLFSSL_MAX_ERROR_SZ];
  5946. fprintf(stderr, "error = %d, %s\n", err,
  5947. wolfSSL_ERR_error_string(err, buff));
  5948. failing_c = 1;
  5949. hs_c = 1;
  5950. if (failing_c && failing_s) {
  5951. break;
  5952. }
  5953. }
  5954. }
  5955. }
  5956. if (!hs_s) {
  5957. wolfSSL_SetLoggingPrefix("server");
  5958. ret = wolfSSL_accept(ctx->s_ssl);
  5959. wolfSSL_SetLoggingPrefix(NULL);
  5960. if (ret == WOLFSSL_SUCCESS) {
  5961. hs_s = 1;
  5962. }
  5963. else {
  5964. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5965. if (err != WOLFSSL_ERROR_WANT_READ &&
  5966. err != WOLFSSL_ERROR_WANT_WRITE) {
  5967. char buff[WOLFSSL_MAX_ERROR_SZ];
  5968. fprintf(stderr, "error = %d, %s\n", err,
  5969. wolfSSL_ERR_error_string(err, buff));
  5970. failing_s = 1;
  5971. hs_s = 1;
  5972. if (failing_c && failing_s) {
  5973. break;
  5974. }
  5975. }
  5976. }
  5977. }
  5978. handshake_complete = hs_c && hs_s;
  5979. max_rounds--;
  5980. if (rounds != NULL) {
  5981. *rounds += 1;
  5982. }
  5983. }
  5984. if (!handshake_complete || failing_c || failing_s) {
  5985. return TEST_FAIL;
  5986. }
  5987. return TEST_SUCCESS;
  5988. }
  5989. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5990. {
  5991. EXPECT_DECLS;
  5992. char input[1024];
  5993. int idx = 0;
  5994. const char* msg_c = "hello wolfssl!";
  5995. int msglen_c = (int)XSTRLEN(msg_c);
  5996. const char* msg_s = "I hear you fa shizzle!";
  5997. int msglen_s = (int)XSTRLEN(msg_s);
  5998. if (ctx->c_msg != NULL) {
  5999. msg_c = ctx->c_msg;
  6000. msglen_c = ctx->c_msglen;
  6001. }
  6002. if (ctx->s_msg != NULL) {
  6003. msg_s = ctx->s_msg;
  6004. msglen_s = ctx->s_msglen;
  6005. }
  6006. wolfSSL_SetLoggingPrefix("client");
  6007. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  6008. wolfSSL_SetLoggingPrefix("server");
  6009. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  6010. if (idx >= 0) {
  6011. input[idx] = '\0';
  6012. }
  6013. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  6014. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  6015. ctx->s_cb.return_code = EXPECT_RESULT();
  6016. wolfSSL_SetLoggingPrefix("client");
  6017. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  6018. wolfSSL_SetLoggingPrefix(NULL);
  6019. if (idx >= 0) {
  6020. input[idx] = '\0';
  6021. }
  6022. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  6023. ctx->c_cb.return_code = EXPECT_RESULT();
  6024. if (ctx->c_cb.on_result != NULL) {
  6025. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  6026. }
  6027. if (ctx->s_cb.on_result != NULL) {
  6028. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  6029. }
  6030. return EXPECT_RESULT();
  6031. }
  6032. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  6033. {
  6034. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  6035. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  6036. if (ctx->c_cb.on_cleanup != NULL) {
  6037. ctx->c_cb.on_cleanup(ctx->c_ssl);
  6038. }
  6039. if (ctx->s_cb.on_cleanup != NULL) {
  6040. ctx->s_cb.on_cleanup(ctx->s_ssl);
  6041. }
  6042. wolfSSL_shutdown(ctx->s_ssl);
  6043. wolfSSL_shutdown(ctx->c_ssl);
  6044. wolfSSL_free(ctx->s_ssl);
  6045. wolfSSL_free(ctx->c_ssl);
  6046. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  6047. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  6048. }
  6049. if (!ctx->c_cb.isSharedCtx) {
  6050. wolfSSL_CTX_free(ctx->c_ctx);
  6051. ctx->c_ctx = NULL;
  6052. }
  6053. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  6054. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  6055. }
  6056. if (!ctx->s_cb.isSharedCtx) {
  6057. wolfSSL_CTX_free(ctx->s_ctx);
  6058. ctx->s_ctx = NULL;
  6059. }
  6060. if (!ctx->s_cb.ticNoInit) {
  6061. #if defined(HAVE_SESSION_TICKET) && \
  6062. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6063. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6064. OpenSSLTicketCleanup();
  6065. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6066. TicketCleanup();
  6067. #endif
  6068. #endif
  6069. }
  6070. }
  6071. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  6072. test_ssl_cbf* server_cb, cbType client_on_handshake)
  6073. {
  6074. EXPECT_DECLS;
  6075. struct test_ssl_memio_ctx test_ctx;
  6076. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6077. size_t msg_len;
  6078. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6079. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  6080. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  6081. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  6082. test_ctx.c_ctx = client_cb->ctx;
  6083. test_ctx.s_ctx = server_cb->ctx;
  6084. test_ctx.c_cb.return_code = TEST_FAIL;
  6085. test_ctx.s_cb.return_code = TEST_FAIL;
  6086. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  6087. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  6088. if (client_on_handshake != NULL) {
  6089. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  6090. TEST_SUCCESS);
  6091. }
  6092. if (client_cb->on_handshake != NULL) {
  6093. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6094. TEST_SUCCESS);
  6095. }
  6096. if (server_cb->on_handshake != NULL) {
  6097. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6098. TEST_SUCCESS);
  6099. }
  6100. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6101. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6102. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6103. MD_MAX_SIZE);
  6104. ExpectIntGE(msg_len, 0);
  6105. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6106. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6107. MD_MAX_SIZE);
  6108. ExpectIntGE(msg_len, 0);
  6109. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6110. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6111. test_ssl_memio_cleanup(&test_ctx);
  6112. client_cb->return_code = test_ctx.c_cb.return_code;
  6113. client_cb->last_err = test_ctx.c_cb.last_err;
  6114. server_cb->return_code = test_ctx.s_cb.return_code;
  6115. server_cb->last_err = test_ctx.s_cb.last_err;
  6116. return EXPECT_RESULT();
  6117. }
  6118. #endif
  6119. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6120. #ifdef WOLFSSL_SESSION_EXPORT
  6121. #ifdef WOLFSSL_DTLS
  6122. /* set up function for sending session information */
  6123. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6124. {
  6125. WOLFSSL_CTX* ctx = NULL;
  6126. WOLFSSL* ssl = NULL;
  6127. AssertNotNull(inSsl);
  6128. AssertNotNull(buf);
  6129. AssertIntNE(0, sz);
  6130. /* Set ctx to DTLS 1.2 */
  6131. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6132. AssertNotNull(ctx);
  6133. ssl = wolfSSL_new(ctx);
  6134. AssertNotNull(ssl);
  6135. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6136. wolfSSL_free(ssl);
  6137. wolfSSL_CTX_free(ctx);
  6138. (void)userCtx;
  6139. return 0;
  6140. }
  6141. #endif
  6142. /* returns negative value on fail and positive (including 0) on success */
  6143. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6144. {
  6145. int ret, err, loop_count, count, timeout = 10;
  6146. char msg[] = "I hear you fa shizzle!";
  6147. char input[1024];
  6148. loop_count = ((func_args*)args)->argc;
  6149. #ifdef WOLFSSL_ASYNC_CRYPT
  6150. err = 0; /* Reset error */
  6151. #endif
  6152. do {
  6153. #ifdef WOLFSSL_ASYNC_CRYPT
  6154. if (err == WC_PENDING_E) {
  6155. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6156. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6157. }
  6158. #endif
  6159. ret = wolfSSL_accept(ssl);
  6160. err = wolfSSL_get_error(ssl, 0);
  6161. if (err == WOLFSSL_ERROR_WANT_READ ||
  6162. err == WOLFSSL_ERROR_WANT_WRITE) {
  6163. int select_ret;
  6164. err = WC_PENDING_E;
  6165. select_ret = tcp_select(*sockfd, timeout);
  6166. if (select_ret == TEST_TIMEOUT) {
  6167. return WOLFSSL_FATAL_ERROR;
  6168. }
  6169. }
  6170. } while (err == WC_PENDING_E);
  6171. if (ret != WOLFSSL_SUCCESS) {
  6172. char buff[WOLFSSL_MAX_ERROR_SZ];
  6173. fprintf(stderr, "error = %d, %s\n", err,
  6174. wolfSSL_ERR_error_string(err, buff));
  6175. return ret;
  6176. }
  6177. for (count = 0; count < loop_count; count++) {
  6178. int select_ret;
  6179. select_ret = tcp_select(*sockfd, timeout);
  6180. if (select_ret == TEST_TIMEOUT) {
  6181. ret = WOLFSSL_FATAL_ERROR;
  6182. break;
  6183. }
  6184. do {
  6185. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6186. if (ret > 0) {
  6187. input[ret] = '\0';
  6188. fprintf(stderr, "Client message: %s\n", input);
  6189. }
  6190. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6191. do {
  6192. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6193. return WOLFSSL_FATAL_ERROR;
  6194. }
  6195. err = wolfSSL_get_error(ssl, ret);
  6196. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6197. }
  6198. return ret;
  6199. }
  6200. #endif /* WOLFSSL_SESSION_EXPORT */
  6201. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6202. {
  6203. SOCKET_T sockfd = 0;
  6204. SOCKET_T clientfd = 0;
  6205. word16 port;
  6206. callback_functions* cbf;
  6207. WOLFSSL_CTX* ctx = NULL;
  6208. WOLFSSL* ssl = NULL;
  6209. func_args* opts = (func_args*)args;
  6210. char msg[] = "I hear you fa shizzle!";
  6211. char input[1024];
  6212. int idx;
  6213. int ret, err = 0;
  6214. int sharedCtx = 0;
  6215. int doUdp = 0;
  6216. SOCKADDR_IN_T cliAddr;
  6217. socklen_t cliLen;
  6218. const char* certFile = svrCertFile;
  6219. const char* keyFile = svrKeyFile;
  6220. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6221. size_t msg_len = 0;
  6222. #endif
  6223. wolfSSL_SetLoggingPrefix("server");
  6224. #ifdef WOLFSSL_TIRTOS
  6225. fdOpenSession(Task_self());
  6226. #endif
  6227. opts->return_code = TEST_FAIL;
  6228. cbf = opts->callbacks;
  6229. if (cbf != NULL && cbf->ctx) {
  6230. ctx = cbf->ctx;
  6231. sharedCtx = 1;
  6232. }
  6233. else
  6234. {
  6235. WOLFSSL_METHOD* method = NULL;
  6236. if (cbf != NULL && cbf->method != NULL) {
  6237. method = cbf->method();
  6238. }
  6239. else {
  6240. method = wolfSSLv23_server_method();
  6241. }
  6242. ctx = wolfSSL_CTX_new(method);
  6243. }
  6244. if (ctx == NULL) {
  6245. /* Release the wait for TCP ready. */
  6246. signal_ready(opts->signal);
  6247. goto done;
  6248. }
  6249. if (cbf == NULL || !cbf->ticNoInit) {
  6250. #if defined(HAVE_SESSION_TICKET) && \
  6251. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6252. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6253. OpenSSLTicketInit();
  6254. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6255. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6256. TicketInit();
  6257. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6258. #endif
  6259. #endif
  6260. }
  6261. #if defined(USE_WINDOWS_API)
  6262. port = opts->signal->port;
  6263. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6264. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6265. /* Let tcp_listen assign port */
  6266. port = 0;
  6267. #else
  6268. /* Use default port */
  6269. port = wolfSSLPort;
  6270. #endif
  6271. if (cbf != NULL)
  6272. doUdp = cbf->doUdp;
  6273. /* do it here to detect failure */
  6274. tcp_accept(
  6275. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6276. if (doUdp) {
  6277. cliLen = sizeof(cliAddr);
  6278. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6279. (struct sockaddr*)&cliAddr, &cliLen);
  6280. AssertIntGT(idx, 0);
  6281. }
  6282. else {
  6283. CloseSocket(sockfd);
  6284. }
  6285. wolfSSL_CTX_set_verify(ctx,
  6286. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6287. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6288. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6289. #endif
  6290. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6291. != WOLFSSL_SUCCESS) {
  6292. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6293. goto done;
  6294. }
  6295. if (cbf != NULL && cbf->certPemFile != NULL)
  6296. certFile = cbf->certPemFile;
  6297. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6298. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6299. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6300. #else
  6301. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6302. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6303. #endif
  6304. /*err_sys("can't load server cert chain file, "
  6305. "Please run from wolfSSL home dir");*/
  6306. goto done;
  6307. }
  6308. if (cbf != NULL && cbf->keyPemFile != NULL)
  6309. keyFile = cbf->keyPemFile;
  6310. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6311. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6312. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6313. #else
  6314. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6315. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6316. #endif
  6317. /*err_sys("can't load server key file, "
  6318. "Please run from wolfSSL home dir");*/
  6319. goto done;
  6320. }
  6321. #ifdef HAVE_CRL
  6322. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6323. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6324. goto done;
  6325. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6326. != WOLFSSL_SUCCESS)
  6327. goto done;
  6328. }
  6329. #endif
  6330. /* call ctx setup callback */
  6331. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6332. cbf->ctx_ready(ctx);
  6333. }
  6334. ssl = wolfSSL_new(ctx);
  6335. if (ssl == NULL) {
  6336. goto done;
  6337. }
  6338. if (doUdp) {
  6339. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6340. if (err != WOLFSSL_SUCCESS)
  6341. goto done;
  6342. }
  6343. #ifdef WOLFSSL_SESSION_EXPORT
  6344. /* only add in more complex nonblocking case with session export tests */
  6345. if (args && opts->argc > 0) {
  6346. /* set as nonblock and time out for waiting on read/write */
  6347. tcp_set_nonblocking(&clientfd);
  6348. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6349. }
  6350. #endif
  6351. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6352. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6353. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6354. #else
  6355. if (wolfSSL_use_certificate_file(ssl, certFile,
  6356. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6357. #endif
  6358. /*err_sys("can't load server cert chain file, "
  6359. "Please run from wolfSSL home dir");*/
  6360. goto done;
  6361. }
  6362. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6363. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6364. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6365. #else
  6366. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6367. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6368. #endif
  6369. /*err_sys("can't load server key file, "
  6370. "Please run from wolfSSL home dir");*/
  6371. goto done;
  6372. }
  6373. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6374. /*err_sys("SSL_set_fd failed");*/
  6375. goto done;
  6376. }
  6377. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6378. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6379. #elif !defined(NO_DH)
  6380. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6381. #endif
  6382. /* call ssl setup callback */
  6383. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6384. cbf->ssl_ready(ssl);
  6385. }
  6386. #ifdef WOLFSSL_SESSION_EXPORT
  6387. /* only add in more complex nonblocking case with session export tests */
  6388. if (opts->argc > 0) {
  6389. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6390. if (ret >= 0) {
  6391. opts->return_code = TEST_SUCCESS;
  6392. }
  6393. #ifdef WOLFSSL_TIRTOS
  6394. Task_yield();
  6395. #endif
  6396. goto done;
  6397. }
  6398. #endif
  6399. #ifdef WOLFSSL_ASYNC_CRYPT
  6400. err = 0; /* Reset error */
  6401. #endif
  6402. do {
  6403. #ifdef WOLFSSL_ASYNC_CRYPT
  6404. if (err == WC_PENDING_E) {
  6405. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6406. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6407. }
  6408. #endif
  6409. ret = wolfSSL_negotiate(ssl);
  6410. err = wolfSSL_get_error(ssl, 0);
  6411. } while (err == WC_PENDING_E);
  6412. if (ret != WOLFSSL_SUCCESS) {
  6413. char buff[WOLFSSL_MAX_ERROR_SZ];
  6414. fprintf(stderr, "error = %d, %s\n", err,
  6415. wolfSSL_ERR_error_string(err, buff));
  6416. /*err_sys("SSL_accept failed");*/
  6417. goto done;
  6418. }
  6419. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6420. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6421. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6422. AssertIntGE(msg_len, 0);
  6423. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6424. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6425. AssertIntGE(msg_len, 0);
  6426. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6427. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6428. if (idx > 0) {
  6429. input[idx] = '\0';
  6430. fprintf(stderr, "Client message: %s\n", input);
  6431. }
  6432. else if (idx < 0) {
  6433. goto done;
  6434. }
  6435. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6436. /*err_sys("SSL_write failed");*/
  6437. goto done;
  6438. }
  6439. if (cbf != NULL && cbf->on_result != NULL)
  6440. cbf->on_result(ssl);
  6441. #ifdef WOLFSSL_TIRTOS
  6442. Task_yield();
  6443. #endif
  6444. opts->return_code = TEST_SUCCESS;
  6445. done:
  6446. if (cbf != NULL)
  6447. cbf->last_err = err;
  6448. if (cbf != NULL && cbf->on_cleanup != NULL)
  6449. cbf->on_cleanup(ssl);
  6450. wolfSSL_shutdown(ssl);
  6451. wolfSSL_free(ssl);
  6452. if (!sharedCtx)
  6453. wolfSSL_CTX_free(ctx);
  6454. CloseSocket(clientfd);
  6455. #ifdef WOLFSSL_TIRTOS
  6456. fdCloseSession(Task_self());
  6457. #endif
  6458. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6459. && defined(HAVE_THREAD_LS)
  6460. wc_ecc_fp_free(); /* free per thread cache */
  6461. #endif
  6462. if (cbf == NULL || !cbf->ticNoInit) {
  6463. #if defined(HAVE_SESSION_TICKET) && \
  6464. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6465. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6466. OpenSSLTicketCleanup();
  6467. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6468. TicketCleanup();
  6469. #endif
  6470. #endif
  6471. }
  6472. wolfSSL_SetLoggingPrefix(NULL);
  6473. WOLFSSL_RETURN_FROM_THREAD(0);
  6474. }
  6475. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6476. !defined(WOLFSSL_NO_TLS12)
  6477. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6478. {
  6479. SOCKET_T sockfd;
  6480. SOCKET_T clientfd = -1;
  6481. word16 port;
  6482. callback_functions* cbf;
  6483. WOLFSSL_CTX* ctx = 0;
  6484. WOLFSSL* ssl = 0;
  6485. char msg[] = "I hear you fa shizzle!";
  6486. char input[1024];
  6487. int idx;
  6488. int ret, err = 0;
  6489. int sharedCtx = 0;
  6490. func_args* opts = (func_args*)args;
  6491. int loop_count = opts->argc;
  6492. int count = 0;
  6493. #ifdef WOLFSSL_TIRTOS
  6494. fdOpenSession(Task_self());
  6495. #endif
  6496. opts->return_code = TEST_FAIL;
  6497. cbf = opts->callbacks;
  6498. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6499. if (cbf != NULL && cbf->ctx) {
  6500. ctx = cbf->ctx;
  6501. sharedCtx = 1;
  6502. }
  6503. else
  6504. #endif
  6505. {
  6506. WOLFSSL_METHOD* method = NULL;
  6507. if (cbf != NULL && cbf->method != NULL) {
  6508. method = cbf->method();
  6509. }
  6510. else {
  6511. method = wolfSSLv23_server_method();
  6512. }
  6513. ctx = wolfSSL_CTX_new(method);
  6514. }
  6515. #if defined(USE_WINDOWS_API)
  6516. port = opts->signal->port;
  6517. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6518. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6519. /* Let tcp_listen assign port */
  6520. port = 0;
  6521. #else
  6522. /* Use default port */
  6523. port = wolfSSLPort;
  6524. #endif
  6525. wolfSSL_CTX_set_verify(ctx,
  6526. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6527. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6528. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6529. #endif
  6530. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6531. != WOLFSSL_SUCCESS) {
  6532. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6533. /* Release the wait for TCP ready. */
  6534. signal_ready(opts->signal);
  6535. goto done;
  6536. }
  6537. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6538. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6539. /*err_sys("can't load server cert chain file, "
  6540. "Please run from wolfSSL home dir");*/
  6541. /* Release the wait for TCP ready. */
  6542. signal_ready(opts->signal);
  6543. goto done;
  6544. }
  6545. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6546. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6547. /*err_sys("can't load server key file, "
  6548. "Please run from wolfSSL home dir");*/
  6549. /* Release the wait for TCP ready. */
  6550. signal_ready(opts->signal);
  6551. goto done;
  6552. }
  6553. /* call ctx setup callback */
  6554. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6555. cbf->ctx_ready(ctx);
  6556. }
  6557. while (count != loop_count) {
  6558. ssl = wolfSSL_new(ctx);
  6559. if (ssl == NULL) {
  6560. signal_ready(opts->signal);
  6561. goto done;
  6562. }
  6563. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6564. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6565. /*err_sys("can't load server cert chain file, "
  6566. "Please run from wolfSSL home dir");*/
  6567. /* Release the wait for TCP ready. */
  6568. signal_ready(opts->signal);
  6569. goto done;
  6570. }
  6571. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6572. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6573. /*err_sys("can't load server key file, "
  6574. "Please run from wolfSSL home dir");*/
  6575. /* Release the wait for TCP ready. */
  6576. signal_ready(opts->signal);
  6577. goto done;
  6578. }
  6579. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6580. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6581. #elif !defined(NO_DH)
  6582. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6583. #endif
  6584. /* call ssl setup callback */
  6585. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6586. cbf->ssl_ready(ssl);
  6587. }
  6588. /* do it here to detect failure */
  6589. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6590. 0);
  6591. CloseSocket(sockfd);
  6592. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6593. /*err_sys("SSL_set_fd failed");*/
  6594. goto done;
  6595. }
  6596. #ifdef WOLFSSL_ASYNC_CRYPT
  6597. err = 0; /* Reset error */
  6598. #endif
  6599. do {
  6600. #ifdef WOLFSSL_ASYNC_CRYPT
  6601. if (err == WC_PENDING_E) {
  6602. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6603. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6604. }
  6605. #endif
  6606. ret = wolfSSL_accept(ssl);
  6607. err = wolfSSL_get_error(ssl, 0);
  6608. } while (err == WC_PENDING_E);
  6609. if (ret != WOLFSSL_SUCCESS) {
  6610. char buff[WOLFSSL_MAX_ERROR_SZ];
  6611. fprintf(stderr, "error = %d, %s\n", err,
  6612. wolfSSL_ERR_error_string(err, buff));
  6613. /*err_sys("SSL_accept failed");*/
  6614. goto done;
  6615. }
  6616. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6617. if (idx > 0) {
  6618. input[idx] = '\0';
  6619. fprintf(stderr, "Client message: %s\n", input);
  6620. }
  6621. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6622. /*err_sys("SSL_write failed");*/
  6623. goto done;
  6624. }
  6625. /* free ssl for this connection */
  6626. wolfSSL_shutdown(ssl);
  6627. wolfSSL_free(ssl); ssl = NULL;
  6628. CloseSocket(clientfd);
  6629. clientfd = -1;
  6630. count++;
  6631. }
  6632. #ifdef WOLFSSL_TIRTOS
  6633. Task_yield();
  6634. #endif
  6635. opts->return_code = TEST_SUCCESS;
  6636. done:
  6637. if (ssl != NULL) {
  6638. wolfSSL_shutdown(ssl);
  6639. wolfSSL_free(ssl);
  6640. }
  6641. if (!sharedCtx)
  6642. wolfSSL_CTX_free(ctx);
  6643. if (clientfd >= 0)
  6644. CloseSocket(clientfd);
  6645. #ifdef WOLFSSL_TIRTOS
  6646. fdCloseSession(Task_self());
  6647. #endif
  6648. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6649. && defined(HAVE_THREAD_LS)
  6650. wc_ecc_fp_free(); /* free per thread cache */
  6651. #endif
  6652. WOLFSSL_RETURN_FROM_THREAD(0);
  6653. }
  6654. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6655. static int test_client_nofail(void* args, cbType cb)
  6656. {
  6657. #if !defined(NO_WOLFSSL_CLIENT)
  6658. SOCKET_T sockfd = 0;
  6659. callback_functions* cbf;
  6660. WOLFSSL_CTX* ctx = 0;
  6661. WOLFSSL* ssl = 0;
  6662. WOLFSSL_CIPHER* cipher;
  6663. char msg[64] = "hello wolfssl!";
  6664. char reply[1024];
  6665. int input;
  6666. int msgSz = (int)XSTRLEN(msg);
  6667. int ret, err = 0;
  6668. int cipherSuite;
  6669. int sharedCtx = 0;
  6670. int doUdp = 0;
  6671. const char* cipherName1, *cipherName2;
  6672. wolfSSL_SetLoggingPrefix("client");
  6673. #ifdef WOLFSSL_TIRTOS
  6674. fdOpenSession(Task_self());
  6675. #endif
  6676. ((func_args*)args)->return_code = TEST_FAIL;
  6677. cbf = ((func_args*)args)->callbacks;
  6678. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6679. if (cbf != NULL && cbf->ctx) {
  6680. ctx = cbf->ctx;
  6681. sharedCtx = cbf->isSharedCtx;
  6682. }
  6683. else
  6684. #endif
  6685. {
  6686. WOLFSSL_METHOD* method = NULL;
  6687. if (cbf != NULL && cbf->method != NULL) {
  6688. method = cbf->method();
  6689. }
  6690. else {
  6691. method = wolfSSLv23_client_method();
  6692. }
  6693. ctx = wolfSSL_CTX_new(method);
  6694. }
  6695. if (cbf != NULL)
  6696. doUdp = cbf->doUdp;
  6697. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6698. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6699. #endif
  6700. /* Do connect here so server detects failures */
  6701. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6702. doUdp, 0, NULL);
  6703. /* Connect the socket so that we don't have to set the peer later on */
  6704. if (doUdp)
  6705. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6706. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6707. {
  6708. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6709. goto done;
  6710. }
  6711. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6712. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6713. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6714. #else
  6715. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6716. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6717. #endif
  6718. /*err_sys("can't load client cert file, "
  6719. "Please run from wolfSSL home dir");*/
  6720. goto done;
  6721. }
  6722. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6723. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6724. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6725. #else
  6726. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6727. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6728. #endif
  6729. /*err_sys("can't load client key file, "
  6730. "Please run from wolfSSL home dir");*/
  6731. goto done;
  6732. }
  6733. #ifdef HAVE_CRL
  6734. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6735. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6736. goto done;
  6737. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6738. != WOLFSSL_SUCCESS)
  6739. goto done;
  6740. }
  6741. #endif
  6742. /* call ctx setup callback */
  6743. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6744. cbf->ctx_ready(ctx);
  6745. }
  6746. ssl = wolfSSL_new(ctx);
  6747. if (ssl == NULL) {
  6748. goto done;
  6749. }
  6750. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6751. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6752. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6753. #else
  6754. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6755. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6756. #endif
  6757. /*err_sys("can't load client cert file, "
  6758. "Please run from wolfSSL home dir");*/
  6759. goto done;
  6760. }
  6761. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6762. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6763. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6764. #else
  6765. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6766. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6767. #endif
  6768. /*err_sys("can't load client key file, "
  6769. "Please run from wolfSSL home dir");*/
  6770. goto done;
  6771. }
  6772. if (!doUdp) {
  6773. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6774. /*err_sys("SSL_set_fd failed");*/
  6775. goto done;
  6776. }
  6777. }
  6778. else {
  6779. #ifdef WOLFSSL_DTLS
  6780. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6781. /*err_sys("SSL_set_fd failed");*/
  6782. goto done;
  6783. }
  6784. #else
  6785. goto done;
  6786. #endif
  6787. }
  6788. /* call ssl setup callback */
  6789. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6790. cbf->ssl_ready(ssl);
  6791. }
  6792. #ifdef WOLFSSL_ASYNC_CRYPT
  6793. err = 0; /* Reset error */
  6794. #endif
  6795. do {
  6796. #ifdef WOLFSSL_ASYNC_CRYPT
  6797. if (err == WC_PENDING_E) {
  6798. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6799. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6800. }
  6801. #endif
  6802. ret = wolfSSL_negotiate(ssl);
  6803. err = wolfSSL_get_error(ssl, 0);
  6804. } while (err == WC_PENDING_E);
  6805. if (ret != WOLFSSL_SUCCESS) {
  6806. char buff[WOLFSSL_MAX_ERROR_SZ];
  6807. fprintf(stderr, "error = %d, %s\n", err,
  6808. wolfSSL_ERR_error_string(err, buff));
  6809. /*err_sys("SSL_connect failed");*/
  6810. goto done;
  6811. }
  6812. /* test the various get cipher methods */
  6813. /* Internal cipher suite names */
  6814. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6815. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6816. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6817. (cipherSuite >> 8), cipherSuite & 0xFF);
  6818. AssertStrEQ(cipherName1, cipherName2);
  6819. /* IANA Cipher Suites Names */
  6820. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6821. then it's the internal cipher suite name */
  6822. cipher = wolfSSL_get_current_cipher(ssl);
  6823. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6824. cipherName2 = wolfSSL_get_cipher(ssl);
  6825. AssertStrEQ(cipherName1, cipherName2);
  6826. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6827. !defined(WOLFSSL_QT)
  6828. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6829. (cipherSuite >> 8), cipherSuite & 0xFF);
  6830. AssertStrEQ(cipherName1, cipherName2);
  6831. #endif
  6832. if (cb != NULL)
  6833. (cb)(ctx, ssl);
  6834. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6835. /*err_sys("SSL_write failed");*/
  6836. goto done;
  6837. }
  6838. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6839. if (input > 0) {
  6840. reply[input] = '\0';
  6841. fprintf(stderr, "Server response: %s\n", reply);
  6842. }
  6843. if (cbf != NULL && cbf->on_result != NULL)
  6844. cbf->on_result(ssl);
  6845. ((func_args*)args)->return_code = TEST_SUCCESS;
  6846. done:
  6847. if (cbf != NULL)
  6848. cbf->last_err = err;
  6849. if (cbf != NULL && cbf->on_cleanup != NULL)
  6850. cbf->on_cleanup(ssl);
  6851. wolfSSL_free(ssl);
  6852. if (!sharedCtx)
  6853. wolfSSL_CTX_free(ctx);
  6854. CloseSocket(sockfd);
  6855. #ifdef WOLFSSL_TIRTOS
  6856. fdCloseSession(Task_self());
  6857. #endif
  6858. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6859. && defined(HAVE_THREAD_LS)
  6860. wc_ecc_fp_free(); /* free per thread cache */
  6861. #endif
  6862. #else
  6863. (void)args;
  6864. (void)cb;
  6865. #endif /* !NO_WOLFSSL_CLIENT */
  6866. wolfSSL_SetLoggingPrefix(NULL);
  6867. return 0;
  6868. }
  6869. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6870. callback_functions* server_cb, cbType client_on_handshake)
  6871. {
  6872. func_args client_args;
  6873. func_args server_args;
  6874. tcp_ready ready;
  6875. THREAD_TYPE serverThread;
  6876. XMEMSET(&client_args, 0, sizeof(func_args));
  6877. XMEMSET(&server_args, 0, sizeof(func_args));
  6878. #ifdef WOLFSSL_TIRTOS
  6879. fdOpenSession(Task_self());
  6880. #endif
  6881. StartTCP();
  6882. InitTcpReady(&ready);
  6883. #if defined(USE_WINDOWS_API)
  6884. /* use RNG to get random port if using windows */
  6885. ready.port = GetRandomPort();
  6886. #endif
  6887. server_args.signal = &ready;
  6888. server_args.callbacks = server_cb;
  6889. client_args.signal = &ready;
  6890. client_args.callbacks = client_cb;
  6891. start_thread(test_server_nofail, &server_args, &serverThread);
  6892. wait_tcp_ready(&server_args);
  6893. test_client_nofail(&client_args, client_on_handshake);
  6894. join_thread(serverThread);
  6895. client_cb->return_code = client_args.return_code;
  6896. server_cb->return_code = server_args.return_code;
  6897. FreeTcpReady(&ready);
  6898. #ifdef WOLFSSL_TIRTOS
  6899. fdOpenSession(Task_self());
  6900. #endif
  6901. }
  6902. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6903. callback_functions* server_cb)
  6904. {
  6905. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6906. }
  6907. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6908. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6909. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6910. void* server_args)
  6911. {
  6912. SOCKET_T sockfd = 0;
  6913. callback_functions* cbf;
  6914. WOLFSSL_CTX* ctx = 0;
  6915. WOLFSSL* ssl = 0;
  6916. WOLFSSL_SESSION* session = NULL;
  6917. char msg[64] = "hello wolfssl!";
  6918. char reply[1024];
  6919. int input;
  6920. int msgSz = (int)XSTRLEN(msg);
  6921. int ret, err = 0;
  6922. int sharedCtx = 0;
  6923. #ifdef WOLFSSL_TIRTOS
  6924. fdOpenSession(Task_self());
  6925. #endif
  6926. ((func_args*)args)->return_code = TEST_FAIL;
  6927. cbf = ((func_args*)args)->callbacks;
  6928. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6929. if (cbf != NULL && cbf->ctx) {
  6930. ctx = cbf->ctx;
  6931. sharedCtx = 1;
  6932. }
  6933. else
  6934. #endif
  6935. {
  6936. WOLFSSL_METHOD* method = NULL;
  6937. if (cbf != NULL && cbf->method != NULL) {
  6938. method = cbf->method();
  6939. }
  6940. else {
  6941. method = wolfSSLv23_client_method();
  6942. }
  6943. ctx = wolfSSL_CTX_new(method);
  6944. }
  6945. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6946. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6947. #endif
  6948. /* Do connect here so server detects failures */
  6949. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6950. 0, 0, NULL);
  6951. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6952. WOLFSSL_SUCCESS) {
  6953. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6954. goto done;
  6955. }
  6956. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6957. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6958. /*err_sys("can't load client cert file, "
  6959. "Please run from wolfSSL home dir");*/
  6960. goto done;
  6961. }
  6962. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6963. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6964. /*err_sys("can't load client key file, "
  6965. "Please run from wolfSSL home dir");*/
  6966. goto done;
  6967. }
  6968. /* call ctx setup callback */
  6969. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6970. cbf->ctx_ready(ctx);
  6971. }
  6972. ssl = wolfSSL_new(ctx);
  6973. if (ssl == NULL) {
  6974. goto done;
  6975. }
  6976. /* keep handshake resources for re-using WOLFSSL obj */
  6977. wolfSSL_KeepArrays(ssl);
  6978. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6979. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6980. goto done;
  6981. }
  6982. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6983. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6984. /*err_sys("can't load client cert file, "
  6985. "Please run from wolfSSL home dir");*/
  6986. goto done;
  6987. }
  6988. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6989. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6990. /*err_sys("can't load client key file, "
  6991. "Please run from wolfSSL home dir");*/
  6992. goto done;
  6993. }
  6994. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6995. /*err_sys("SSL_set_fd failed");*/
  6996. goto done;
  6997. }
  6998. /* call ssl setup callback */
  6999. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7000. cbf->ssl_ready(ssl);
  7001. }
  7002. #ifdef WOLFSSL_ASYNC_CRYPT
  7003. err = 0; /* Reset error */
  7004. #endif
  7005. do {
  7006. #ifdef WOLFSSL_ASYNC_CRYPT
  7007. if (err == WC_PENDING_E) {
  7008. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7009. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7010. }
  7011. #endif
  7012. ret = wolfSSL_connect(ssl);
  7013. err = wolfSSL_get_error(ssl, 0);
  7014. } while (err == WC_PENDING_E);
  7015. if (ret != WOLFSSL_SUCCESS) {
  7016. char buff[WOLFSSL_MAX_ERROR_SZ];
  7017. fprintf(stderr, "error = %d, %s\n", err,
  7018. wolfSSL_ERR_error_string(err, buff));
  7019. /*err_sys("SSL_connect failed");*/
  7020. goto done;
  7021. }
  7022. /* Build first session */
  7023. if (cb != NULL)
  7024. cb(ctx, ssl);
  7025. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7026. /*err_sys("SSL_write failed");*/
  7027. goto done;
  7028. }
  7029. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7030. if (input > 0) {
  7031. reply[input] = '\0';
  7032. fprintf(stderr, "Server response: %s\n", reply);
  7033. }
  7034. /* Session Resumption by re-using WOLFSSL object */
  7035. wolfSSL_set_quiet_shutdown(ssl, 1);
  7036. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  7037. /* err_sys ("SSL shutdown failed"); */
  7038. goto done;
  7039. }
  7040. session = wolfSSL_get1_session(ssl);
  7041. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  7042. wolfSSL_SESSION_free(session);
  7043. /* err_sys ("SSL_clear failed"); */
  7044. goto done;
  7045. }
  7046. wolfSSL_set_session(ssl, session);
  7047. wolfSSL_SESSION_free(session);
  7048. session = NULL;
  7049. /* close socket once */
  7050. CloseSocket(sockfd);
  7051. sockfd = 0;
  7052. /* wait until server ready */
  7053. wait_tcp_ready((func_args*)server_args);
  7054. fprintf(stderr, "session resumption\n");
  7055. /* Do re-connect */
  7056. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7057. 0, 0, NULL);
  7058. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7059. /*err_sys("SSL_set_fd failed");*/
  7060. goto done;
  7061. }
  7062. #ifdef WOLFSSL_ASYNC_CRYPT
  7063. err = 0; /* Reset error */
  7064. #endif
  7065. do {
  7066. #ifdef WOLFSSL_ASYNC_CRYPT
  7067. if (err == WC_PENDING_E) {
  7068. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7069. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7070. }
  7071. #endif
  7072. ret = wolfSSL_connect(ssl);
  7073. err = wolfSSL_get_error(ssl, 0);
  7074. } while (err == WC_PENDING_E);
  7075. if (ret != WOLFSSL_SUCCESS) {
  7076. char buff[WOLFSSL_MAX_ERROR_SZ];
  7077. fprintf(stderr, "error = %d, %s\n", err,
  7078. wolfSSL_ERR_error_string(err, buff));
  7079. /*err_sys("SSL_connect failed");*/
  7080. goto done;
  7081. }
  7082. /* Build first session */
  7083. if (cb != NULL)
  7084. cb(ctx, ssl);
  7085. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7086. /*err_sys("SSL_write failed");*/
  7087. goto done;
  7088. }
  7089. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7090. if (input > 0) {
  7091. reply[input] = '\0';
  7092. fprintf(stderr, "Server response: %s\n", reply);
  7093. }
  7094. ((func_args*)args)->return_code = TEST_SUCCESS;
  7095. done:
  7096. wolfSSL_free(ssl);
  7097. if (!sharedCtx)
  7098. wolfSSL_CTX_free(ctx);
  7099. CloseSocket(sockfd);
  7100. #ifdef WOLFSSL_TIRTOS
  7101. fdCloseSession(Task_self());
  7102. #endif
  7103. return;
  7104. }
  7105. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7106. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7107. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7108. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7109. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7110. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7111. #define HAVE_ALPN_PROTOS_SUPPORT
  7112. #endif
  7113. /* Generic TLS client / server with callbacks for API unit tests
  7114. * Used by SNI / ALPN / crypto callback helper functions */
  7115. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7116. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7117. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7118. #define ENABLE_TLS_CALLBACK_TEST
  7119. #endif
  7120. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7121. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7122. /* TLS server for API unit testing - generic */
  7123. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7124. {
  7125. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7126. WOLFSSL_CTX* ctx = NULL;
  7127. WOLFSSL* ssl = NULL;
  7128. SOCKET_T sfd = 0;
  7129. SOCKET_T cfd = 0;
  7130. word16 port;
  7131. char msg[] = "I hear you fa shizzle!";
  7132. int len = (int) XSTRLEN(msg);
  7133. char input[1024];
  7134. int idx;
  7135. int ret, err = 0;
  7136. ((func_args*)args)->return_code = TEST_FAIL;
  7137. #if defined(USE_WINDOWS_API)
  7138. port = ((func_args*)args)->signal->port;
  7139. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7140. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7141. /* Let tcp_listen assign port */
  7142. port = 0;
  7143. #else
  7144. /* Use default port */
  7145. port = wolfSSLPort;
  7146. #endif
  7147. #ifdef WOLFSSL_DTLS
  7148. if (callbacks->method == wolfDTLS_server_method
  7149. #ifdef WOLFSSL_STATIC_MEMORY
  7150. || callbacks->method_ex == wolfDTLS_server_method_ex
  7151. #endif
  7152. #ifndef NO_OLD_TLS
  7153. || callbacks->method == wolfDTLSv1_server_method
  7154. #ifdef WOLFSSL_STATIC_MEMORY
  7155. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7156. #endif
  7157. #endif
  7158. #ifndef WOLFSSL_NO_TLS12
  7159. || callbacks->method == wolfDTLSv1_2_server_method
  7160. #ifdef WOLFSSL_STATIC_MEMORY
  7161. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7162. #endif
  7163. #endif
  7164. #ifdef WOLFSSL_DTLS13
  7165. || callbacks->method == wolfDTLSv1_3_server_method
  7166. #ifdef WOLFSSL_STATIC_MEMORY
  7167. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7168. #endif
  7169. #endif
  7170. ) {
  7171. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7172. }
  7173. else
  7174. #endif
  7175. {
  7176. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7177. }
  7178. #ifdef WOLFSSL_STATIC_MEMORY
  7179. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7180. callbacks->memSz > 0) {
  7181. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7182. callbacks->mem, callbacks->memSz, 0, 1);
  7183. if (ret != WOLFSSL_SUCCESS) {
  7184. fprintf(stderr, "CTX static new failed %d\n", ret);
  7185. goto cleanup;
  7186. }
  7187. }
  7188. #else
  7189. ctx = wolfSSL_CTX_new(callbacks->method());
  7190. #endif
  7191. if (ctx == NULL) {
  7192. fprintf(stderr, "CTX new failed\n");
  7193. goto cleanup;
  7194. }
  7195. /* set defaults */
  7196. if (callbacks->caPemFile == NULL)
  7197. callbacks->caPemFile = cliCertFile;
  7198. if (callbacks->certPemFile == NULL)
  7199. callbacks->certPemFile = svrCertFile;
  7200. if (callbacks->keyPemFile == NULL)
  7201. callbacks->keyPemFile = svrKeyFile;
  7202. #ifdef WOLFSSL_TIRTOS
  7203. fdOpenSession(Task_self());
  7204. #endif
  7205. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7206. wolfSSL_CTX_set_verify(ctx,
  7207. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7208. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7209. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7210. #endif
  7211. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7212. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7213. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7214. goto cleanup;
  7215. }
  7216. #endif
  7217. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7218. WOLFSSL_SUCCESS) {
  7219. goto cleanup;
  7220. }
  7221. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7222. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7223. goto cleanup;
  7224. }
  7225. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7226. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7227. goto cleanup;
  7228. }
  7229. #ifdef HAVE_CRL
  7230. if (callbacks->crlPemFile != NULL) {
  7231. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7232. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7233. goto cleanup;
  7234. }
  7235. }
  7236. #endif
  7237. if (callbacks->ctx_ready)
  7238. callbacks->ctx_ready(ctx);
  7239. ssl = wolfSSL_new(ctx);
  7240. if (ssl == NULL) {
  7241. fprintf(stderr, "SSL new failed\n");
  7242. goto cleanup;
  7243. }
  7244. if (wolfSSL_dtls(ssl)) {
  7245. SOCKADDR_IN_T cliAddr;
  7246. socklen_t cliLen;
  7247. cliLen = sizeof(cliAddr);
  7248. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7249. (struct sockaddr*)&cliAddr, &cliLen);
  7250. if (idx <= 0) {
  7251. goto cleanup;
  7252. }
  7253. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7254. }
  7255. else {
  7256. CloseSocket(sfd);
  7257. }
  7258. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7259. goto cleanup;
  7260. }
  7261. if (callbacks->loadToSSL) {
  7262. wolfSSL_SetDevId(ssl, callbacks->devId);
  7263. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7264. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7265. goto cleanup;
  7266. }
  7267. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7268. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7269. goto cleanup;
  7270. }
  7271. }
  7272. #ifdef NO_PSK
  7273. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7274. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7275. #elif !defined(NO_DH)
  7276. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7277. #endif
  7278. #endif
  7279. if (callbacks->ssl_ready)
  7280. callbacks->ssl_ready(ssl);
  7281. #ifdef WOLFSSL_ASYNC_CRYPT
  7282. err = 0; /* Reset error */
  7283. #endif
  7284. do {
  7285. #ifdef WOLFSSL_ASYNC_CRYPT
  7286. if (err == WC_PENDING_E) {
  7287. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7288. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7289. }
  7290. #endif
  7291. ret = wolfSSL_accept(ssl);
  7292. err = wolfSSL_get_error(ssl, ret);
  7293. } while (err == WC_PENDING_E);
  7294. if (ret != WOLFSSL_SUCCESS) {
  7295. char buff[WOLFSSL_MAX_ERROR_SZ];
  7296. fprintf(stderr, "accept error = %d, %s\n", err,
  7297. wolfSSL_ERR_error_string(err, buff));
  7298. /*err_sys("SSL_accept failed");*/
  7299. }
  7300. else {
  7301. #ifdef WOLFSSL_ASYNC_CRYPT
  7302. err = 0; /* Reset error */
  7303. #endif
  7304. do {
  7305. #ifdef WOLFSSL_ASYNC_CRYPT
  7306. if (err == WC_PENDING_E) {
  7307. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7308. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7309. }
  7310. #endif
  7311. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7312. err = wolfSSL_get_error(ssl, idx);
  7313. } while (err == WC_PENDING_E);
  7314. if (idx > 0) {
  7315. input[idx] = 0;
  7316. fprintf(stderr, "Client message: %s\n", input);
  7317. }
  7318. #ifdef WOLFSSL_ASYNC_CRYPT
  7319. err = 0; /* Reset error */
  7320. #endif
  7321. do {
  7322. #ifdef WOLFSSL_ASYNC_CRYPT
  7323. if (err == WC_PENDING_E) {
  7324. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7325. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7326. }
  7327. #endif
  7328. ret = wolfSSL_write(ssl, msg, len);
  7329. err = wolfSSL_get_error(ssl, ret);
  7330. } while (err == WC_PENDING_E);
  7331. if (len != ret) {
  7332. goto cleanup;
  7333. }
  7334. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7335. defined(WOLFSSL_DTLS)
  7336. if (wolfSSL_dtls(ssl)) {
  7337. byte* import;
  7338. word32 sz;
  7339. wolfSSL_dtls_export(ssl, NULL, &sz);
  7340. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7341. if (import == NULL) {
  7342. goto cleanup;
  7343. }
  7344. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7345. if (idx < 0) {
  7346. goto cleanup;
  7347. }
  7348. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7349. goto cleanup;
  7350. }
  7351. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7352. }
  7353. #endif
  7354. #ifdef WOLFSSL_TIRTOS
  7355. Task_yield();
  7356. #endif
  7357. ((func_args*)args)->return_code = TEST_SUCCESS;
  7358. }
  7359. if (callbacks->on_result)
  7360. callbacks->on_result(ssl);
  7361. wolfSSL_shutdown(ssl);
  7362. cleanup:
  7363. wolfSSL_free(ssl);
  7364. wolfSSL_CTX_free(ctx);
  7365. CloseSocket(cfd);
  7366. #ifdef WOLFSSL_TIRTOS
  7367. fdCloseSession(Task_self());
  7368. #endif
  7369. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7370. && defined(HAVE_THREAD_LS)
  7371. wc_ecc_fp_free(); /* free per thread cache */
  7372. #endif
  7373. WOLFSSL_RETURN_FROM_THREAD(0);
  7374. }
  7375. /* TLS Client for API unit testing - generic */
  7376. static void run_wolfssl_client(void* args)
  7377. {
  7378. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7379. WOLFSSL_CTX* ctx = NULL;
  7380. WOLFSSL* ssl = NULL;
  7381. SOCKET_T sfd = 0;
  7382. char msg[] = "hello wolfssl server!";
  7383. int len = (int) XSTRLEN(msg);
  7384. char input[1024];
  7385. int ret, err = 0;
  7386. ((func_args*)args)->return_code = TEST_FAIL;
  7387. /* set defaults */
  7388. if (callbacks->caPemFile == NULL)
  7389. callbacks->caPemFile = caCertFile;
  7390. if (callbacks->certPemFile == NULL)
  7391. callbacks->certPemFile = cliCertFile;
  7392. if (callbacks->keyPemFile == NULL)
  7393. callbacks->keyPemFile = cliKeyFile;
  7394. #ifdef WOLFSSL_STATIC_MEMORY
  7395. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7396. callbacks->memSz > 0) {
  7397. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7398. callbacks->mem, callbacks->memSz, 0, 1);
  7399. if (ret != WOLFSSL_SUCCESS) {
  7400. fprintf(stderr, "CTX static new failed %d\n", ret);
  7401. goto cleanup;
  7402. }
  7403. }
  7404. #else
  7405. ctx = wolfSSL_CTX_new(callbacks->method());
  7406. #endif
  7407. if (ctx == NULL) {
  7408. fprintf(stderr, "CTX new failed\n");
  7409. goto cleanup;
  7410. }
  7411. #ifdef WOLFSSL_TIRTOS
  7412. fdOpenSession(Task_self());
  7413. #endif
  7414. if (!callbacks->loadToSSL) {
  7415. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7416. }
  7417. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7418. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7419. #endif
  7420. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7421. WOLFSSL_SUCCESS) {
  7422. goto cleanup;
  7423. }
  7424. if (!callbacks->loadToSSL) {
  7425. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7426. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7427. goto cleanup;
  7428. }
  7429. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7430. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7431. goto cleanup;
  7432. }
  7433. }
  7434. #ifdef HAVE_CRL
  7435. if (callbacks->crlPemFile != NULL) {
  7436. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7437. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7438. goto cleanup;
  7439. }
  7440. }
  7441. #endif
  7442. if (callbacks->ctx_ready)
  7443. callbacks->ctx_ready(ctx);
  7444. ssl = wolfSSL_new(ctx);
  7445. if (wolfSSL_dtls(ssl)) {
  7446. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7447. 1, 0, ssl);
  7448. }
  7449. else {
  7450. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7451. 0, 0, ssl);
  7452. }
  7453. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7454. goto cleanup;
  7455. }
  7456. if (callbacks->loadToSSL) {
  7457. wolfSSL_SetDevId(ssl, callbacks->devId);
  7458. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7459. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7460. goto cleanup;
  7461. }
  7462. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7463. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7464. goto cleanup;
  7465. }
  7466. }
  7467. if (callbacks->ssl_ready)
  7468. callbacks->ssl_ready(ssl);
  7469. #ifdef WOLFSSL_ASYNC_CRYPT
  7470. err = 0; /* Reset error */
  7471. #endif
  7472. do {
  7473. #ifdef WOLFSSL_ASYNC_CRYPT
  7474. if (err == WC_PENDING_E) {
  7475. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7476. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7477. }
  7478. #endif
  7479. ret = wolfSSL_connect(ssl);
  7480. err = wolfSSL_get_error(ssl, ret);
  7481. } while (err == WC_PENDING_E);
  7482. if (ret != WOLFSSL_SUCCESS) {
  7483. char buff[WOLFSSL_MAX_ERROR_SZ];
  7484. fprintf(stderr, "error = %d, %s\n", err,
  7485. wolfSSL_ERR_error_string(err, buff));
  7486. /*err_sys("SSL_connect failed");*/
  7487. }
  7488. else {
  7489. #ifdef WOLFSSL_ASYNC_CRYPT
  7490. err = 0; /* Reset error */
  7491. #endif
  7492. do {
  7493. #ifdef WOLFSSL_ASYNC_CRYPT
  7494. if (err == WC_PENDING_E) {
  7495. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7496. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7497. }
  7498. #endif
  7499. ret = wolfSSL_write(ssl, msg, len);
  7500. err = wolfSSL_get_error(ssl, ret);
  7501. } while (err == WC_PENDING_E);
  7502. if (len != ret)
  7503. goto cleanup;
  7504. #ifdef WOLFSSL_ASYNC_CRYPT
  7505. err = 0; /* Reset error */
  7506. #endif
  7507. do {
  7508. #ifdef WOLFSSL_ASYNC_CRYPT
  7509. if (err == WC_PENDING_E) {
  7510. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7511. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7512. }
  7513. #endif
  7514. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7515. err = wolfSSL_get_error(ssl, ret);
  7516. } while (err == WC_PENDING_E);
  7517. if (ret > 0) {
  7518. input[ret] = '\0'; /* null term */
  7519. fprintf(stderr, "Server response: %s\n", input);
  7520. }
  7521. ((func_args*)args)->return_code = TEST_SUCCESS;
  7522. }
  7523. if (callbacks->on_result)
  7524. callbacks->on_result(ssl);
  7525. cleanup:
  7526. wolfSSL_free(ssl);
  7527. wolfSSL_CTX_free(ctx);
  7528. CloseSocket(sfd);
  7529. #ifdef WOLFSSL_TIRTOS
  7530. fdCloseSession(Task_self());
  7531. #endif
  7532. }
  7533. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7534. static int test_wolfSSL_read_write(void)
  7535. {
  7536. /* The unit testing for read and write shall happen simultaneously, since
  7537. * one can't do anything with one without the other. (Except for a failure
  7538. * test case.) This function will call all the others that will set up,
  7539. * execute, and report their test findings.
  7540. *
  7541. * Set up the success case first. This function will become the template
  7542. * for the other tests. This should eventually be renamed
  7543. *
  7544. * The success case isn't interesting, how can this fail?
  7545. * - Do not give the client context a CA certificate. The connect should
  7546. * fail. Do not need server for this?
  7547. * - Using NULL for the ssl object on server. Do not need client for this.
  7548. * - Using NULL for the ssl object on client. Do not need server for this.
  7549. * - Good ssl objects for client and server. Client write() without server
  7550. * read().
  7551. * - Good ssl objects for client and server. Server write() without client
  7552. * read().
  7553. * - Forgetting the password callback?
  7554. */
  7555. tcp_ready ready;
  7556. func_args client_args;
  7557. func_args server_args;
  7558. THREAD_TYPE serverThread;
  7559. EXPECT_DECLS;
  7560. XMEMSET(&client_args, 0, sizeof(func_args));
  7561. XMEMSET(&server_args, 0, sizeof(func_args));
  7562. #ifdef WOLFSSL_TIRTOS
  7563. fdOpenSession(Task_self());
  7564. #endif
  7565. StartTCP();
  7566. InitTcpReady(&ready);
  7567. #if defined(USE_WINDOWS_API)
  7568. /* use RNG to get random port if using windows */
  7569. ready.port = GetRandomPort();
  7570. #endif
  7571. server_args.signal = &ready;
  7572. client_args.signal = &ready;
  7573. start_thread(test_server_nofail, &server_args, &serverThread);
  7574. wait_tcp_ready(&server_args);
  7575. test_client_nofail(&client_args, NULL);
  7576. join_thread(serverThread);
  7577. ExpectTrue(client_args.return_code);
  7578. ExpectTrue(server_args.return_code);
  7579. FreeTcpReady(&ready);
  7580. #ifdef WOLFSSL_TIRTOS
  7581. fdOpenSession(Task_self());
  7582. #endif
  7583. return EXPECT_RESULT();
  7584. }
  7585. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7586. {
  7587. EXPECT_DECLS;
  7588. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7589. !defined(WOLFSSL_NO_TLS12)
  7590. /* The unit test for session resumption by re-using WOLFSSL object.
  7591. * WOLFSSL object is not cleared after first session. It reuse the object
  7592. * for second connection.
  7593. */
  7594. tcp_ready ready;
  7595. func_args client_args;
  7596. func_args server_args;
  7597. THREAD_TYPE serverThread;
  7598. callback_functions client_cbf;
  7599. callback_functions server_cbf;
  7600. XMEMSET(&client_args, 0, sizeof(func_args));
  7601. XMEMSET(&server_args, 0, sizeof(func_args));
  7602. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7603. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7604. #ifdef WOLFSSL_TIRTOS
  7605. fdOpenSession(Task_self());
  7606. #endif
  7607. StartTCP();
  7608. InitTcpReady(&ready);
  7609. #if defined(USE_WINDOWS_API)
  7610. /* use RNG to get random port if using windows */
  7611. ready.port = GetRandomPort();
  7612. #endif
  7613. client_cbf.method = wolfTLSv1_2_client_method;
  7614. server_cbf.method = wolfTLSv1_2_server_method;
  7615. client_args.callbacks = &client_cbf;
  7616. server_args.callbacks = &server_cbf;
  7617. server_args.signal = &ready;
  7618. client_args.signal = &ready;
  7619. /* the var is used for loop number */
  7620. server_args.argc = 2;
  7621. start_thread(test_server_loop, &server_args, &serverThread);
  7622. wait_tcp_ready(&server_args);
  7623. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7624. join_thread(serverThread);
  7625. ExpectTrue(client_args.return_code);
  7626. ExpectTrue(server_args.return_code);
  7627. FreeTcpReady(&ready);
  7628. #ifdef WOLFSSL_TIRTOS
  7629. fdOpenSession(Task_self());
  7630. #endif
  7631. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7632. * !defined(WOLFSSL_TLS13) */
  7633. return EXPECT_RESULT();
  7634. }
  7635. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7636. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7637. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7638. WOLFSSL_CTX* ctx)
  7639. {
  7640. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7641. myVerifyAction = VERIFY_USE_PREVERFIY;
  7642. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7643. return TEST_SUCCESS;
  7644. }
  7645. #endif
  7646. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7647. {
  7648. EXPECT_DECLS;
  7649. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7650. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7651. test_ssl_cbf client_cbf;
  7652. test_ssl_cbf server_cbf;
  7653. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7654. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7655. #ifdef WOLFSSL_TLS13
  7656. client_cbf.method = wolfTLSv1_3_client_method;
  7657. #endif /* WOLFSSL_TLS13 */
  7658. client_cbf.ctx_ready =
  7659. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7660. /* test case 1 verify depth is equal to peer chain */
  7661. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7662. &server_cbf, NULL), TEST_SUCCESS);
  7663. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7664. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7665. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7666. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7667. return EXPECT_RESULT();
  7668. }
  7669. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7670. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7671. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7672. WOLFSSL_CTX* ctx)
  7673. {
  7674. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7675. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7676. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7677. return TEST_SUCCESS;
  7678. }
  7679. #endif
  7680. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7681. {
  7682. EXPECT_DECLS;
  7683. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7684. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7685. test_ssl_cbf client_cbf;
  7686. test_ssl_cbf server_cbf;
  7687. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7688. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7689. #ifdef WOLFSSL_TLS13
  7690. client_cbf.method = wolfTLSv1_3_client_method;
  7691. #endif /* WOLFSSL_TLS13 */
  7692. client_cbf.ctx_ready =
  7693. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7694. /* test case 2
  7695. * verify depth is zero, number of peer's chain is 2.
  7696. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7697. * callback.
  7698. */
  7699. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7700. &server_cbf, NULL), TEST_SUCCESS);
  7701. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7702. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7703. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7704. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7705. return EXPECT_RESULT();
  7706. }
  7707. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7708. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7709. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7710. WOLFSSL_CTX* ctx)
  7711. {
  7712. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7713. myVerifyAction = VERIFY_USE_PREVERFIY;
  7714. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7715. return TEST_SUCCESS;
  7716. }
  7717. #endif
  7718. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7719. {
  7720. EXPECT_DECLS;
  7721. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7722. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7723. test_ssl_cbf client_cbf;
  7724. test_ssl_cbf server_cbf;
  7725. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7726. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7727. #ifdef WOLFSSL_TLS13
  7728. client_cbf.method = wolfTLSv1_3_client_method;
  7729. #endif /* WOLFSSL_TLS13 */
  7730. client_cbf.ctx_ready =
  7731. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7732. /* test case 3
  7733. * verify depth is zero, number of peer's chain is 2
  7734. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7735. * therefore, handshake becomes failure.
  7736. */
  7737. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7738. &server_cbf, NULL), TEST_FAIL);
  7739. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7740. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7741. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7742. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7743. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7744. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7745. return EXPECT_RESULT();
  7746. }
  7747. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7748. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7749. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7750. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7751. {
  7752. EXPECT_DECLS;
  7753. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7754. return EXPECT_RESULT();
  7755. }
  7756. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7757. {
  7758. EXPECT_DECLS;
  7759. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7760. return EXPECT_RESULT();
  7761. }
  7762. #endif
  7763. static int test_wolfSSL_CTX_set_cipher_list(void)
  7764. {
  7765. EXPECT_DECLS;
  7766. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7767. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7768. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7769. WOLFSSL_CTX* ctxClient = NULL;
  7770. WOLFSSL* sslClient = NULL;
  7771. test_ssl_cbf client_cbf;
  7772. test_ssl_cbf server_cbf;
  7773. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7774. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7775. server_cbf.method = wolfTLSv1_2_server_method;
  7776. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7777. client_cbf.method = wolfTLSv1_2_client_method;
  7778. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7779. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7780. &server_cbf, NULL), TEST_SUCCESS);
  7781. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7782. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7783. /* check with cipher string that has '+' */
  7784. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7785. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7786. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7787. /* check for the existence of an ECDHE ECDSA cipher suite */
  7788. if (EXPECT_SUCCESS()) {
  7789. int i = 0;
  7790. int found = 0;
  7791. const char* suite;
  7792. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7793. WOLFSSL_CIPHER* current;
  7794. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7795. do {
  7796. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7797. if (current) {
  7798. suite = wolfSSL_CIPHER_get_name(current);
  7799. if (suite && XSTRSTR(suite, "ECDSA")) {
  7800. found = 1;
  7801. break;
  7802. }
  7803. }
  7804. } while (current);
  7805. ExpectIntEQ(found, 1);
  7806. }
  7807. wolfSSL_free(sslClient);
  7808. wolfSSL_CTX_free(ctxClient);
  7809. #endif
  7810. return EXPECT_RESULT();
  7811. }
  7812. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7813. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7814. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7815. WOLFSSL* ssl)
  7816. {
  7817. EXPECT_DECLS;
  7818. size_t msg_len;
  7819. (void)ctx;
  7820. /* get_finished test */
  7821. /* 1. get own sent message */
  7822. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7823. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7824. ExpectIntGE(msg_len, 0);
  7825. /* 2. get peer message */
  7826. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7827. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7828. ExpectIntGE(msg_len, 0);
  7829. return EXPECT_RESULT();
  7830. }
  7831. #endif
  7832. static int test_wolfSSL_get_finished(void)
  7833. {
  7834. EXPECT_DECLS;
  7835. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7836. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7837. test_ssl_cbf client_cbf;
  7838. test_ssl_cbf server_cbf;
  7839. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7840. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7841. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7842. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7843. TEST_SUCCESS);
  7844. /* test received msg vs sent msg */
  7845. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7846. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7847. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7848. return EXPECT_RESULT();
  7849. }
  7850. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7851. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7852. !defined(NO_SESSION_CACHE)
  7853. /* Sessions to restore/store */
  7854. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7855. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7856. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7857. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7858. {
  7859. /* Don't store sessions. Lookup is still enabled. */
  7860. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7861. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7862. #ifdef OPENSSL_EXTRA
  7863. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7864. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7865. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7866. #endif
  7867. /* Require both peers to provide certs */
  7868. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7869. }
  7870. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7871. {
  7872. WOLFSSL_SESSION** sess;
  7873. if (wolfSSL_is_server(ssl))
  7874. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7875. else
  7876. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7877. if (*sess == NULL) {
  7878. #ifdef NO_SESSION_CACHE_REF
  7879. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7880. #else
  7881. /* Test for backwards compatibility */
  7882. if (wolfSSL_is_server(ssl)) {
  7883. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7884. }
  7885. else {
  7886. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7887. }
  7888. #endif
  7889. /* Now save the session in the internal store to make it available
  7890. * for lookup. For TLS 1.3, we can't save the session without
  7891. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7892. * session from cache. */
  7893. if (wolfSSL_is_server(ssl)
  7894. #ifndef WOLFSSL_TICKET_HAVE_ID
  7895. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7896. #endif
  7897. )
  7898. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7899. *sess), WOLFSSL_SUCCESS);
  7900. }
  7901. else {
  7902. /* If we have a session retrieved then remaining connections should be
  7903. * resuming on that session */
  7904. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7905. }
  7906. /* Save CTX to be able to decrypt tickets */
  7907. if (wolfSSL_is_server(ssl) &&
  7908. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7909. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7910. = wolfSSL_get_SSL_CTX(ssl));
  7911. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7912. WOLFSSL_SUCCESS);
  7913. }
  7914. #ifdef SESSION_CERTS
  7915. #ifndef WOLFSSL_TICKET_HAVE_ID
  7916. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7917. wolfSSL_session_reused(ssl))
  7918. #endif
  7919. {
  7920. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7921. * for all connections. TLS 1.3 only has tickets so if we don't
  7922. * include the session id in the ticket then the certificates
  7923. * will not be available on resumption. */
  7924. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7925. AssertNotNull(peer);
  7926. wolfSSL_X509_free(peer);
  7927. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7928. #ifdef OPENSSL_EXTRA
  7929. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7930. #endif
  7931. }
  7932. #endif /* SESSION_CERTS */
  7933. }
  7934. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7935. {
  7936. /* Set the session to reuse for the client */
  7937. AssertIntEQ(wolfSSL_set_session(ssl,
  7938. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7939. }
  7940. #endif
  7941. static int test_wolfSSL_CTX_add_session(void)
  7942. {
  7943. EXPECT_DECLS;
  7944. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7945. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7946. !defined(NO_SESSION_CACHE)
  7947. tcp_ready ready;
  7948. func_args client_args;
  7949. func_args server_args;
  7950. THREAD_TYPE serverThread;
  7951. callback_functions client_cb;
  7952. callback_functions server_cb;
  7953. method_provider methods[][2] = {
  7954. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7955. !defined(NO_DES3))
  7956. /* Without AES there are almost no ciphersuites available. This leads
  7957. * to no ciphersuites being available and an error. */
  7958. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7959. #endif
  7960. #ifndef WOLFSSL_NO_TLS12
  7961. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7962. #endif
  7963. /* Needs the default ticket callback since it is tied to the
  7964. * connection context and this makes it easy to carry over the ticket
  7965. * crypto context between connections */
  7966. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7967. defined(HAVE_SESSION_TICKET)
  7968. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7969. #endif
  7970. };
  7971. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7972. size_t i, j;
  7973. for (i = 0; i < methodsLen; i++) {
  7974. /* First run creates a connection while the second+ run will attempt
  7975. * to resume the connection. The trick is that the internal cache
  7976. * is turned off. wolfSSL_CTX_add_session should put the session in
  7977. * the cache anyway. */
  7978. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7979. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7980. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7981. #ifdef NO_SESSION_CACHE_REF
  7982. for (j = 0; j < 4; j++) {
  7983. #else
  7984. /* The session may be overwritten in this case. Do only one resumption
  7985. * to stop this test from failing intermittently. */
  7986. for (j = 0; j < 2; j++) {
  7987. #endif
  7988. #ifdef WOLFSSL_TIRTOS
  7989. fdOpenSession(Task_self());
  7990. #endif
  7991. StartTCP();
  7992. InitTcpReady(&ready);
  7993. XMEMSET(&client_args, 0, sizeof(func_args));
  7994. XMEMSET(&server_args, 0, sizeof(func_args));
  7995. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7996. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7997. client_cb.method = methods[i][0];
  7998. server_cb.method = methods[i][1];
  7999. server_args.signal = &ready;
  8000. server_args.callbacks = &server_cb;
  8001. client_args.signal = &ready;
  8002. client_args.callbacks = &client_cb;
  8003. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  8004. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  8005. server_cb.isSharedCtx = 1;
  8006. }
  8007. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8008. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8009. if (j != 0)
  8010. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  8011. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8012. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8013. server_cb.ticNoInit = 1; /* Use default builtin */
  8014. start_thread(test_server_nofail, &server_args, &serverThread);
  8015. wait_tcp_ready(&server_args);
  8016. test_client_nofail(&client_args, NULL);
  8017. join_thread(serverThread);
  8018. ExpectTrue(client_args.return_code);
  8019. ExpectTrue(server_args.return_code);
  8020. FreeTcpReady(&ready);
  8021. if (EXPECT_FAIL())
  8022. break;
  8023. }
  8024. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  8025. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  8026. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  8027. if (EXPECT_FAIL())
  8028. break;
  8029. }
  8030. #endif
  8031. return EXPECT_RESULT();
  8032. }
  8033. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8034. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8035. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8036. defined(HAVE_SESSION_TICKET) && \
  8037. !defined(TITAN_SESSION_CACHE) && \
  8038. !defined(HUGE_SESSION_CACHE) && \
  8039. !defined(BIG_SESSION_CACHE) && \
  8040. !defined(MEDIUM_SESSION_CACHE)
  8041. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  8042. /* Sessions to restore/store */
  8043. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  8044. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  8045. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  8046. static int twcase_new_session_called = 0;
  8047. static int twcase_remove_session_called = 0;
  8048. static int twcase_get_session_called = 0;
  8049. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  8050. #define SESSION_CACHE_SIZE 33
  8051. typedef struct {
  8052. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  8053. WOLFSSL_SESSION* value;
  8054. } hashTable_entry;
  8055. typedef struct {
  8056. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  8057. size_t capacity; /* size of entries */
  8058. size_t length; /* number of items in the hash table */
  8059. wolfSSL_Mutex htLock; /* lock */
  8060. }hashTable;
  8061. static hashTable server_sessionCache;
  8062. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  8063. {
  8064. int i;
  8065. unsigned int len;
  8066. (void)ssl;
  8067. /*
  8068. * This example uses a hash table.
  8069. * Steps you should take for a non-demo code:
  8070. * - acquire a lock for the file named according to the session id
  8071. * - open the file
  8072. * - encrypt and write the SSL_SESSION object to the file
  8073. * - release the lock
  8074. *
  8075. * Return:
  8076. * 0: The callback does not wish to hold a reference of the sess
  8077. * 1: The callback wants to hold a reference of the sess. The callback is
  8078. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  8079. */
  8080. if (sess == NULL)
  8081. return 0;
  8082. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8083. return 0;
  8084. }
  8085. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8086. if (server_sessionCache.entries[i].value == NULL) {
  8087. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  8088. server_sessionCache.entries[i].value = sess;
  8089. server_sessionCache.length++;
  8090. break;
  8091. }
  8092. }
  8093. ++twcase_new_session_called;
  8094. wc_UnLockMutex(&server_sessionCache.htLock);
  8095. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8096. twcase_new_session_called);
  8097. return 1;
  8098. }
  8099. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8100. {
  8101. int i;
  8102. (void)ctx;
  8103. (void)sess;
  8104. if (sess == NULL)
  8105. return;
  8106. /*
  8107. * This example uses a hash table.
  8108. * Steps you should take for a non-demo code:
  8109. * - acquire a lock for the file named according to the session id
  8110. * - remove the file
  8111. * - release the lock
  8112. */
  8113. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8114. return;
  8115. }
  8116. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8117. if (server_sessionCache.entries[i].key != NULL &&
  8118. XMEMCMP(server_sessionCache.entries[i].key,
  8119. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8120. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8121. server_sessionCache.entries[i].value = NULL;
  8122. server_sessionCache.entries[i].key = NULL;
  8123. server_sessionCache.length--;
  8124. break;
  8125. }
  8126. }
  8127. ++twcase_remove_session_called;
  8128. wc_UnLockMutex(&server_sessionCache.htLock);
  8129. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8130. twcase_remove_session_called);
  8131. }
  8132. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8133. const unsigned char *id, int len, int *ref)
  8134. {
  8135. int i;
  8136. (void)ssl;
  8137. (void)id;
  8138. (void)len;
  8139. /*
  8140. * This example uses a hash table.
  8141. * Steps you should take for a non-demo code:
  8142. * - acquire a lock for the file named according to the session id in the
  8143. * 2nd arg
  8144. * - read and decrypt contents of file and create a new SSL_SESSION
  8145. * - object release the lock
  8146. * - return the new session object
  8147. */
  8148. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8149. ++twcase_get_session_called);
  8150. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8151. * be responsible for the pointer then set to 0. */
  8152. *ref = 1;
  8153. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8154. if (server_sessionCache.entries[i].key != NULL &&
  8155. XMEMCMP(server_sessionCache.entries[i].key, id,
  8156. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8157. return server_sessionCache.entries[i].value;
  8158. }
  8159. }
  8160. return NULL;
  8161. }
  8162. static int twcase_get_sessionCb_cleanup(void)
  8163. {
  8164. int i;
  8165. int cnt = 0;
  8166. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8167. * for freeing sessions */
  8168. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8169. if (server_sessionCache.entries[i].value != NULL) {
  8170. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8171. cnt++;
  8172. }
  8173. }
  8174. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8175. cnt);
  8176. return TEST_SUCCESS;
  8177. }
  8178. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8179. {
  8180. EXPECT_DECLS;
  8181. /* off - Disable internal cache */
  8182. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8183. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8184. #ifdef OPENSSL_EXTRA
  8185. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8186. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8187. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8188. #endif
  8189. /* off - Do not setup external cache */
  8190. /* Require both peers to provide certs */
  8191. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8192. return EXPECT_RESULT();
  8193. }
  8194. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8195. {
  8196. /* on - internal cache is on by default */
  8197. /* off - Do not setup external cache */
  8198. /* Require both peers to provide certs */
  8199. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8200. return TEST_SUCCESS;
  8201. }
  8202. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8203. {
  8204. EXPECT_DECLS;
  8205. /* off - Disable internal cache */
  8206. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8207. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8208. #ifdef OPENSSL_EXTRA
  8209. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8210. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8211. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8212. #endif
  8213. /* on - Enable external cache */
  8214. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8215. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8216. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8217. /* Require both peers to provide certs */
  8218. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8219. return EXPECT_RESULT();
  8220. }
  8221. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8222. {
  8223. /* on - internal cache is on by default */
  8224. /* on - Enable external cache */
  8225. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8226. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8227. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8228. /* Require both peers to provide certs */
  8229. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8230. return TEST_SUCCESS;
  8231. }
  8232. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8233. {
  8234. /* on - internal cache is on by default */
  8235. /* on - Enable external cache */
  8236. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8237. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8238. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8239. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8240. /* Require both peers to provide certs */
  8241. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8242. return TEST_SUCCESS;
  8243. }
  8244. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8245. {
  8246. EXPECT_DECLS;
  8247. WOLFSSL_SESSION** sess;
  8248. if (wolfSSL_is_server(ssl))
  8249. sess = &twcase_server_first_session_ptr;
  8250. else
  8251. return TEST_SUCCESS;
  8252. if (*sess == NULL) {
  8253. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8254. /* Now save the session in the internal store to make it available
  8255. * for lookup. For TLS 1.3, we can't save the session without
  8256. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8257. * session from cache. */
  8258. if (wolfSSL_is_server(ssl)
  8259. #ifndef WOLFSSL_TICKET_HAVE_ID
  8260. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8261. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8262. #endif
  8263. ) {
  8264. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8265. *sess), WOLFSSL_SUCCESS);
  8266. }
  8267. }
  8268. /* Save CTX to be able to decrypt tickets */
  8269. if (twcase_server_current_ctx_ptr == NULL) {
  8270. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8271. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8272. WOLFSSL_SUCCESS);
  8273. }
  8274. #ifdef SESSION_CERTS
  8275. #ifndef WOLFSSL_TICKET_HAVE_ID
  8276. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8277. wolfSSL_session_reused(ssl))
  8278. #endif
  8279. {
  8280. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8281. * for all connections. TLS 1.3 only has tickets so if we don't
  8282. * include the session id in the ticket then the certificates
  8283. * will not be available on resumption. */
  8284. WOLFSSL_X509* peer = NULL;
  8285. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8286. wolfSSL_X509_free(peer);
  8287. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8288. }
  8289. #endif
  8290. return EXPECT_RESULT();
  8291. }
  8292. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8293. {
  8294. EXPECT_DECLS;
  8295. WOLFSSL_SESSION** sess;
  8296. sess = &twcase_client_first_session_ptr;
  8297. if (*sess == NULL) {
  8298. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8299. }
  8300. else {
  8301. /* If we have a session retrieved then remaining connections should be
  8302. * resuming on that session */
  8303. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8304. }
  8305. #ifdef SESSION_CERTS
  8306. #ifndef WOLFSSL_TICKET_HAVE_ID
  8307. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8308. wolfSSL_session_reused(ssl))
  8309. #endif
  8310. {
  8311. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8312. ExpectNotNull(peer);
  8313. wolfSSL_X509_free(peer);
  8314. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8315. #ifdef OPENSSL_EXTRA
  8316. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8317. #endif
  8318. }
  8319. #endif
  8320. return EXPECT_RESULT();
  8321. }
  8322. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8323. {
  8324. EXPECT_DECLS;
  8325. /* Set the session to reuse for the client */
  8326. ExpectNotNull(ssl);
  8327. ExpectNotNull(twcase_client_first_session_ptr);
  8328. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8329. WOLFSSL_SUCCESS);
  8330. return EXPECT_RESULT();
  8331. }
  8332. struct test_add_session_ext_params {
  8333. method_provider client_meth;
  8334. method_provider server_meth;
  8335. const char* tls_version;
  8336. };
  8337. static int test_wolfSSL_CTX_add_session_ext(
  8338. struct test_add_session_ext_params* param)
  8339. {
  8340. EXPECT_DECLS;
  8341. /* Test the default 33 sessions */
  8342. int j;
  8343. /* Clear cache before starting */
  8344. wolfSSL_CTX_flush_sessions(NULL, -1);
  8345. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8346. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8347. return BAD_MUTEX_E;
  8348. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8349. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8350. for (j = 0; j < 5; j++) {
  8351. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8352. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8353. test_ssl_cbf client_cb;
  8354. test_ssl_cbf server_cb;
  8355. (void)dtls;
  8356. /* Test five cache configurations */
  8357. twcase_client_first_session_ptr = NULL;
  8358. twcase_server_first_session_ptr = NULL;
  8359. twcase_server_current_ctx_ptr = NULL;
  8360. twcase_new_session_called = 0;
  8361. twcase_remove_session_called = 0;
  8362. twcase_get_session_called = 0;
  8363. /* connection 1 - first connection */
  8364. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8365. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8366. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8367. client_cb.method = param->client_meth;
  8368. server_cb.method = param->server_meth;
  8369. if (dtls)
  8370. client_cb.doUdp = server_cb.doUdp = 1;
  8371. /* Setup internal and external cache */
  8372. switch (j) {
  8373. case 0:
  8374. /* SSL_OP_NO_TICKET stateful ticket case */
  8375. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8376. break;
  8377. case 1:
  8378. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8379. break;
  8380. case 2:
  8381. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8382. break;
  8383. case 3:
  8384. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8385. break;
  8386. case 4:
  8387. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8388. break;
  8389. }
  8390. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8391. /* Add session to internal cache and save SSL session for testing */
  8392. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8393. /* Save client SSL session for testing */
  8394. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8395. server_cb.ticNoInit = 1; /* Use default builtin */
  8396. /* Don't free/release ctx */
  8397. server_cb.ctx = twcase_server_current_ctx_ptr;
  8398. server_cb.isSharedCtx = 1;
  8399. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8400. &server_cb, NULL), TEST_SUCCESS);
  8401. ExpectIntEQ(twcase_get_session_called, 0);
  8402. if (EXPECT_FAIL()) {
  8403. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8404. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8405. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8406. break;
  8407. }
  8408. switch (j) {
  8409. case 0:
  8410. case 1:
  8411. case 2:
  8412. /* cache cannot be searched with out a connection */
  8413. /* Add a new session */
  8414. ExpectIntEQ(twcase_new_session_called, 1);
  8415. /* In twcase_server_sess_ctx_pre_shutdown
  8416. * wolfSSL_CTX_add_session which evicts the existing session
  8417. * in cache and adds it back in */
  8418. ExpectIntLE(twcase_remove_session_called, 1);
  8419. break;
  8420. case 3:
  8421. case 4:
  8422. /* no external cache */
  8423. ExpectIntEQ(twcase_new_session_called, 0);
  8424. ExpectIntEQ(twcase_remove_session_called, 0);
  8425. break;
  8426. }
  8427. /* connection 2 - session resume */
  8428. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8429. twcase_new_session_called = 0;
  8430. twcase_remove_session_called = 0;
  8431. twcase_get_session_called = 0;
  8432. server_cb.on_result = 0;
  8433. client_cb.on_result = 0;
  8434. server_cb.ticNoInit = 1; /* Use default builtin */
  8435. server_cb.ctx = twcase_server_current_ctx_ptr;
  8436. /* try session resumption */
  8437. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8438. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8439. &server_cb, NULL), TEST_SUCCESS);
  8440. /* Clear cache before checking */
  8441. wolfSSL_CTX_flush_sessions(NULL, -1);
  8442. switch (j) {
  8443. case 0:
  8444. if (tls13) {
  8445. /* (D)TLSv1.3 stateful case */
  8446. /* cache hit */
  8447. /* DTLS accesses cache once for stateless parsing and
  8448. * once for stateful parsing */
  8449. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8450. /* (D)TLSv1.3 creates a new ticket,
  8451. * updates both internal and external cache */
  8452. ExpectIntEQ(twcase_new_session_called, 1);
  8453. /* A new session ID is created for a new ticket */
  8454. ExpectIntEQ(twcase_remove_session_called, 2);
  8455. }
  8456. else {
  8457. /* non (D)TLSv1.3 case, no update */
  8458. /* DTLS accesses cache once for stateless parsing and
  8459. * once for stateful parsing */
  8460. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8461. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8462. #else
  8463. ExpectIntEQ(twcase_get_session_called, 1);
  8464. #endif
  8465. ExpectIntEQ(twcase_new_session_called, 0);
  8466. /* Called on session added in
  8467. * twcase_server_sess_ctx_pre_shutdown */
  8468. ExpectIntEQ(twcase_remove_session_called, 1);
  8469. }
  8470. break;
  8471. case 1:
  8472. if (tls13) {
  8473. /* (D)TLSv1.3 case */
  8474. /* cache hit */
  8475. ExpectIntEQ(twcase_get_session_called, 1);
  8476. /* (D)TLSv1.3 creates a new ticket,
  8477. * updates both internal and external cache */
  8478. ExpectIntEQ(twcase_new_session_called, 1);
  8479. /* Called on session added in
  8480. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8481. ExpectIntEQ(twcase_remove_session_called, 1);
  8482. }
  8483. else {
  8484. /* non (D)TLSv1.3 case */
  8485. /* cache hit */
  8486. /* DTLS accesses cache once for stateless parsing and
  8487. * once for stateful parsing */
  8488. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8489. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8490. #else
  8491. ExpectIntEQ(twcase_get_session_called, 1);
  8492. #endif
  8493. ExpectIntEQ(twcase_new_session_called, 0);
  8494. /* Called on session added in
  8495. * twcase_server_sess_ctx_pre_shutdown */
  8496. ExpectIntEQ(twcase_remove_session_called, 1);
  8497. }
  8498. break;
  8499. case 2:
  8500. if (tls13) {
  8501. /* (D)TLSv1.3 case */
  8502. /* cache hit */
  8503. ExpectIntEQ(twcase_get_session_called, 1);
  8504. /* (D)TLSv1.3 creates a new ticket,
  8505. * updates both internal and external cache */
  8506. ExpectIntEQ(twcase_new_session_called, 1);
  8507. /* Called on session added in
  8508. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8509. ExpectIntEQ(twcase_remove_session_called, 1);
  8510. }
  8511. else {
  8512. /* non (D)TLSv1.3 case */
  8513. /* cache hit */
  8514. /* DTLS accesses cache once for stateless parsing and
  8515. * once for stateful parsing */
  8516. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8517. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8518. #else
  8519. ExpectIntEQ(twcase_get_session_called, 1);
  8520. #endif
  8521. ExpectIntEQ(twcase_new_session_called, 0);
  8522. /* Called on session added in
  8523. * twcase_server_sess_ctx_pre_shutdown */
  8524. ExpectIntEQ(twcase_remove_session_called, 1);
  8525. }
  8526. break;
  8527. case 3:
  8528. case 4:
  8529. /* no external cache */
  8530. ExpectIntEQ(twcase_get_session_called, 0);
  8531. ExpectIntEQ(twcase_new_session_called, 0);
  8532. ExpectIntEQ(twcase_remove_session_called, 0);
  8533. break;
  8534. }
  8535. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8536. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8537. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8538. if (EXPECT_FAIL())
  8539. break;
  8540. }
  8541. twcase_get_sessionCb_cleanup();
  8542. XMEMSET(&server_sessionCache.entries, 0,
  8543. sizeof(server_sessionCache.entries));
  8544. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8545. wc_FreeMutex(&server_sessionCache.htLock);
  8546. return EXPECT_RESULT();
  8547. }
  8548. #endif
  8549. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8550. {
  8551. EXPECT_DECLS;
  8552. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8553. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8554. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8555. defined(HAVE_SESSION_TICKET) && \
  8556. !defined(TITAN_SESSION_CACHE) && \
  8557. !defined(HUGE_SESSION_CACHE) && \
  8558. !defined(BIG_SESSION_CACHE) && \
  8559. !defined(MEDIUM_SESSION_CACHE)
  8560. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8561. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8562. struct test_add_session_ext_params param[1] = {
  8563. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8564. };
  8565. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8566. #endif
  8567. #endif
  8568. return EXPECT_RESULT();
  8569. }
  8570. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8571. {
  8572. EXPECT_DECLS;
  8573. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8574. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8575. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8576. defined(HAVE_SESSION_TICKET) && \
  8577. !defined(TITAN_SESSION_CACHE) && \
  8578. !defined(HUGE_SESSION_CACHE) && \
  8579. !defined(BIG_SESSION_CACHE) && \
  8580. !defined(MEDIUM_SESSION_CACHE)
  8581. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8582. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8583. #ifdef WOLFSSL_DTLS13
  8584. struct test_add_session_ext_params param[1] = {
  8585. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8586. };
  8587. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8588. #endif
  8589. #endif
  8590. #endif
  8591. return EXPECT_RESULT();
  8592. }
  8593. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8594. {
  8595. EXPECT_DECLS;
  8596. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8597. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8598. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8599. defined(HAVE_SESSION_TICKET) && \
  8600. !defined(TITAN_SESSION_CACHE) && \
  8601. !defined(HUGE_SESSION_CACHE) && \
  8602. !defined(BIG_SESSION_CACHE) && \
  8603. !defined(MEDIUM_SESSION_CACHE)
  8604. #ifndef WOLFSSL_NO_TLS12
  8605. struct test_add_session_ext_params param[1] = {
  8606. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8607. };
  8608. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8609. #endif
  8610. #endif
  8611. return EXPECT_RESULT();
  8612. }
  8613. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8614. {
  8615. EXPECT_DECLS;
  8616. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8617. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8618. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8619. defined(HAVE_SESSION_TICKET) && \
  8620. !defined(TITAN_SESSION_CACHE) && \
  8621. !defined(HUGE_SESSION_CACHE) && \
  8622. !defined(BIG_SESSION_CACHE) && \
  8623. !defined(MEDIUM_SESSION_CACHE)
  8624. #ifndef WOLFSSL_NO_TLS12
  8625. #ifdef WOLFSSL_DTLS
  8626. struct test_add_session_ext_params param[1] = {
  8627. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8628. };
  8629. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8630. #endif
  8631. #endif
  8632. #endif
  8633. return EXPECT_RESULT();
  8634. }
  8635. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8636. {
  8637. EXPECT_DECLS;
  8638. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8639. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8640. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8641. defined(HAVE_SESSION_TICKET) && \
  8642. !defined(TITAN_SESSION_CACHE) && \
  8643. !defined(HUGE_SESSION_CACHE) && \
  8644. !defined(BIG_SESSION_CACHE) && \
  8645. !defined(MEDIUM_SESSION_CACHE)
  8646. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8647. !defined(NO_DES3))
  8648. struct test_add_session_ext_params param[1] = {
  8649. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8650. };
  8651. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8652. #endif
  8653. #endif
  8654. return EXPECT_RESULT();
  8655. }
  8656. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8657. {
  8658. EXPECT_DECLS;
  8659. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8660. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8661. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8662. defined(HAVE_SESSION_TICKET) && \
  8663. !defined(TITAN_SESSION_CACHE) && \
  8664. !defined(HUGE_SESSION_CACHE) && \
  8665. !defined(BIG_SESSION_CACHE) && \
  8666. !defined(MEDIUM_SESSION_CACHE)
  8667. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8668. !defined(NO_DES3))
  8669. #ifdef WOLFSSL_DTLS
  8670. struct test_add_session_ext_params param[1] = {
  8671. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8672. };
  8673. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8674. #endif
  8675. #endif
  8676. #endif
  8677. return EXPECT_RESULT();
  8678. }
  8679. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8680. /* canned export of a session using older version 3 */
  8681. static unsigned char version_3[] = {
  8682. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8683. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8684. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8685. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8686. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8687. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8688. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8689. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8690. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8691. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8692. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8693. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8695. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8696. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8697. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8698. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8699. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8700. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8701. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8702. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8703. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8704. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8705. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8706. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8707. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8708. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8709. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8710. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8711. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8712. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8713. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8714. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8715. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8716. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8717. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8718. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8719. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8720. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8721. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8722. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8723. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8724. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8725. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8726. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8727. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8728. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8729. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8730. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8731. 0x2E, 0x31, 0xED, 0x4F
  8732. };
  8733. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8734. static int test_wolfSSL_dtls_export(void)
  8735. {
  8736. EXPECT_DECLS;
  8737. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8738. tcp_ready ready;
  8739. func_args client_args;
  8740. func_args server_args;
  8741. THREAD_TYPE serverThread;
  8742. callback_functions server_cbf;
  8743. callback_functions client_cbf;
  8744. #ifdef WOLFSSL_TIRTOS
  8745. fdOpenSession(Task_self());
  8746. #endif
  8747. InitTcpReady(&ready);
  8748. #if defined(USE_WINDOWS_API)
  8749. /* use RNG to get random port if using windows */
  8750. ready.port = GetRandomPort();
  8751. #endif
  8752. /* set using dtls */
  8753. XMEMSET(&client_args, 0, sizeof(func_args));
  8754. XMEMSET(&server_args, 0, sizeof(func_args));
  8755. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8756. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8757. server_cbf.method = wolfDTLSv1_2_server_method;
  8758. client_cbf.method = wolfDTLSv1_2_client_method;
  8759. server_args.callbacks = &server_cbf;
  8760. client_args.callbacks = &client_cbf;
  8761. server_args.signal = &ready;
  8762. client_args.signal = &ready;
  8763. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8764. wait_tcp_ready(&server_args);
  8765. run_wolfssl_client(&client_args);
  8766. join_thread(serverThread);
  8767. ExpectTrue(client_args.return_code);
  8768. ExpectTrue(server_args.return_code);
  8769. FreeTcpReady(&ready);
  8770. #ifdef WOLFSSL_TIRTOS
  8771. fdOpenSession(Task_self());
  8772. #endif
  8773. if (EXPECT_SUCCESS()) {
  8774. SOCKET_T sockfd = 0;
  8775. WOLFSSL_CTX* ctx = NULL;
  8776. WOLFSSL* ssl = NULL;
  8777. char msg[64] = "hello wolfssl!";
  8778. char reply[1024];
  8779. int msgSz = (int)XSTRLEN(msg);
  8780. byte *session, *window;
  8781. unsigned int sessionSz = 0;
  8782. unsigned int windowSz = 0;
  8783. #ifndef TEST_IPV6
  8784. struct sockaddr_in peerAddr;
  8785. #else
  8786. struct sockaddr_in6 peerAddr;
  8787. #endif /* TEST_IPV6 */
  8788. int i;
  8789. /* Set ctx to DTLS 1.2 */
  8790. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8791. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8792. /* test importing version 3 */
  8793. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8794. /* test importing bad length and bad version */
  8795. version_3[2] += 1;
  8796. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8797. version_3[2] -= 1; version_3[1] = 0XA0;
  8798. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8799. wolfSSL_free(ssl);
  8800. wolfSSL_CTX_free(ctx);
  8801. /* check storing client state after connection and storing window only */
  8802. #ifdef WOLFSSL_TIRTOS
  8803. fdOpenSession(Task_self());
  8804. #endif
  8805. InitTcpReady(&ready);
  8806. #if defined(USE_WINDOWS_API)
  8807. /* use RNG to get random port if using windows */
  8808. ready.port = GetRandomPort();
  8809. #endif
  8810. /* set using dtls */
  8811. XMEMSET(&server_args, 0, sizeof(func_args));
  8812. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8813. server_cbf.method = wolfDTLSv1_2_server_method;
  8814. server_cbf.doUdp = 1;
  8815. server_args.callbacks = &server_cbf;
  8816. server_args.argc = 3; /* set loop_count to 3 */
  8817. server_args.signal = &ready;
  8818. start_thread(test_server_nofail, &server_args, &serverThread);
  8819. wait_tcp_ready(&server_args);
  8820. /* create and connect with client */
  8821. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8822. ExpectIntEQ(WOLFSSL_SUCCESS,
  8823. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8824. ExpectIntEQ(WOLFSSL_SUCCESS,
  8825. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8826. ExpectIntEQ(WOLFSSL_SUCCESS,
  8827. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8828. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8829. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8830. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8831. /* store server information connected too */
  8832. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8833. #ifndef TEST_IPV6
  8834. peerAddr.sin_family = AF_INET;
  8835. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8836. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8837. #else
  8838. peerAddr.sin6_family = AF_INET6;
  8839. ExpectIntEQ(
  8840. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8841. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8842. #endif
  8843. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8844. WOLFSSL_SUCCESS);
  8845. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8846. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8847. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8848. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8849. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8850. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8851. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8852. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8853. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8854. wolfSSL_free(ssl);
  8855. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8856. /* restore state */
  8857. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8858. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8859. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8860. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8861. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8862. WOLFSSL_SUCCESS);
  8863. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8864. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8865. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8866. wolfSSL_free(ssl);
  8867. }
  8868. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8869. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8870. wolfSSL_CTX_free(ctx);
  8871. fprintf(stderr, "done and waiting for server\n");
  8872. join_thread(serverThread);
  8873. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8874. FreeTcpReady(&ready);
  8875. #ifdef WOLFSSL_TIRTOS
  8876. fdOpenSession(Task_self());
  8877. #endif
  8878. }
  8879. #endif
  8880. return EXPECT_RESULT();
  8881. }
  8882. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8883. #ifdef WOLFSSL_TLS13
  8884. static const byte canned_client_tls13_session[] = {
  8885. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8886. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8887. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8888. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8889. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8890. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8891. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8892. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8893. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8894. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8895. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8896. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8897. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8898. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8899. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8900. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8901. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8902. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8903. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8904. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8905. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8906. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8907. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8908. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8909. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8910. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8911. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8912. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8913. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8914. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8915. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8916. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8917. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8918. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8919. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8920. 0x00, 0x03
  8921. };
  8922. static const byte canned_server_tls13_session[] = {
  8923. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8924. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8925. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8926. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8927. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8928. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8929. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8930. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8931. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8932. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8933. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8934. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8935. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8936. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8937. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8938. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8939. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8940. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8941. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8942. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8943. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8944. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8945. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8946. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8947. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8948. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8949. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8950. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8951. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8952. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8953. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8954. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8955. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8956. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8957. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8958. 0x00, 0x04
  8959. };
  8960. #endif /* WOLFSSL_TLS13 */
  8961. static const byte canned_client_session[] = {
  8962. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8963. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8964. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8965. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8966. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8967. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8968. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8969. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8970. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8971. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8972. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8973. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8974. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8975. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8976. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8977. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8978. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8979. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8980. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8981. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8982. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8983. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8984. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8985. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8986. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8987. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8988. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8989. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8990. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8991. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8992. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8993. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8994. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8995. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8996. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8997. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8998. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8999. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9000. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9001. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9002. 0x00, 0x03
  9003. };
  9004. static const byte canned_server_session[] = {
  9005. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  9006. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  9007. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  9008. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9009. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  9010. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9011. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  9012. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  9013. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  9014. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9015. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9016. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  9017. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9018. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9019. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9020. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9021. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9022. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9023. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9024. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9025. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9026. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9027. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9028. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9029. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9030. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9031. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9032. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9033. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9034. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9035. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9036. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9038. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9039. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9040. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9041. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9042. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9043. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9044. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9045. 0x00, 0x04
  9046. };
  9047. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  9048. {
  9049. SOCKET_T sockfd = 0;
  9050. SOCKET_T clientfd = 0;
  9051. word16 port;
  9052. callback_functions* cbf;
  9053. WOLFSSL_CTX* ctx = 0;
  9054. WOLFSSL* ssl = 0;
  9055. char msg[] = "I hear you fa shizzle!";
  9056. char input[1024];
  9057. int idx;
  9058. #ifdef WOLFSSL_TIRTOS
  9059. fdOpenSession(Task_self());
  9060. #endif
  9061. ((func_args*)args)->return_code = TEST_FAIL;
  9062. cbf = ((func_args*)args)->callbacks;
  9063. #if defined(USE_WINDOWS_API)
  9064. port = ((func_args*)args)->signal->port;
  9065. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  9066. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  9067. /* Let tcp_listen assign port */
  9068. port = 0;
  9069. #else
  9070. /* Use default port */
  9071. port = wolfSSLPort;
  9072. #endif
  9073. /* do it here to detect failure */
  9074. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  9075. CloseSocket(sockfd);
  9076. {
  9077. WOLFSSL_METHOD* method = NULL;
  9078. if (cbf != NULL && cbf->method != NULL) {
  9079. method = cbf->method();
  9080. }
  9081. else {
  9082. method = wolfTLSv1_2_server_method();
  9083. }
  9084. ctx = wolfSSL_CTX_new(method);
  9085. }
  9086. if (ctx == NULL) {
  9087. goto done;
  9088. }
  9089. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9090. /* call ctx setup callback */
  9091. if (cbf != NULL && cbf->ctx_ready != NULL) {
  9092. cbf->ctx_ready(ctx);
  9093. }
  9094. ssl = wolfSSL_new(ctx);
  9095. if (ssl == NULL) {
  9096. goto done;
  9097. }
  9098. wolfSSL_set_fd(ssl, clientfd);
  9099. /* call ssl setup callback */
  9100. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9101. cbf->ssl_ready(ssl);
  9102. }
  9103. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9104. if (idx > 0) {
  9105. input[idx] = '\0';
  9106. fprintf(stderr, "Client message export/import: %s\n", input);
  9107. }
  9108. else {
  9109. fprintf(stderr, "ret = %d error = %d\n", idx,
  9110. wolfSSL_get_error(ssl, idx));
  9111. goto done;
  9112. }
  9113. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9114. /*err_sys("SSL_write failed");*/
  9115. WOLFSSL_RETURN_FROM_THREAD(0);
  9116. }
  9117. #ifdef WOLFSSL_TIRTOS
  9118. Task_yield();
  9119. #endif
  9120. ((func_args*)args)->return_code = TEST_SUCCESS;
  9121. done:
  9122. wolfSSL_shutdown(ssl);
  9123. wolfSSL_free(ssl);
  9124. wolfSSL_CTX_free(ctx);
  9125. CloseSocket(clientfd);
  9126. #ifdef WOLFSSL_TIRTOS
  9127. fdCloseSession(Task_self());
  9128. #endif
  9129. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9130. && defined(HAVE_THREAD_LS)
  9131. wc_ecc_fp_free(); /* free per thread cache */
  9132. #endif
  9133. #if defined(HAVE_SESSION_TICKET) && \
  9134. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9135. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9136. OpenSSLTicketCleanup();
  9137. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9138. TicketCleanup();
  9139. #endif
  9140. #endif
  9141. WOLFSSL_RETURN_FROM_THREAD(0);
  9142. }
  9143. static void load_tls12_canned_server(WOLFSSL* ssl)
  9144. {
  9145. int clientfd = wolfSSL_get_fd(ssl);
  9146. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9147. sizeof(canned_server_session)), sizeof(canned_server_session));
  9148. wolfSSL_set_fd(ssl, clientfd);
  9149. }
  9150. #ifdef WOLFSSL_TLS13
  9151. static void load_tls13_canned_server(WOLFSSL* ssl)
  9152. {
  9153. int clientfd = wolfSSL_get_fd(ssl);
  9154. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9155. sizeof(canned_server_tls13_session)),
  9156. sizeof(canned_server_tls13_session));
  9157. wolfSSL_set_fd(ssl, clientfd);
  9158. }
  9159. #endif
  9160. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9161. static int test_wolfSSL_tls_export_run(int v)
  9162. {
  9163. EXPECT_DECLS;
  9164. SOCKET_T sockfd = 0;
  9165. WOLFSSL_CTX* ctx = 0;
  9166. WOLFSSL* ssl = 0;
  9167. char msg[64] = "hello wolfssl!";
  9168. char reply[1024];
  9169. word32 replySz;
  9170. int msgSz = (int)XSTRLEN(msg);
  9171. const byte* clientSession = NULL;
  9172. int clientSessionSz = 0;
  9173. tcp_ready ready;
  9174. func_args server_args;
  9175. THREAD_TYPE serverThread;
  9176. callback_functions server_cbf;
  9177. #ifdef WOLFSSL_TIRTOS
  9178. fdOpenSession(Task_self());
  9179. #endif
  9180. InitTcpReady(&ready);
  9181. #if defined(USE_WINDOWS_API)
  9182. /* use RNG to get random port if using windows */
  9183. ready.port = GetRandomPort();
  9184. #endif
  9185. XMEMSET(&server_args, 0, sizeof(func_args));
  9186. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9187. switch (v) {
  9188. case WOLFSSL_TLSV1_2:
  9189. server_cbf.method = wolfTLSv1_2_server_method;
  9190. server_cbf.ssl_ready = load_tls12_canned_server;
  9191. /* setup the client side */
  9192. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9193. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9194. clientSession = canned_client_session;
  9195. clientSessionSz = sizeof(canned_client_session);
  9196. break;
  9197. #ifdef WOLFSSL_TLS13
  9198. case WOLFSSL_TLSV1_3:
  9199. server_cbf.method = wolfTLSv1_3_server_method;
  9200. server_cbf.ssl_ready = load_tls13_canned_server;
  9201. /* setup the client side */
  9202. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9203. clientSession = canned_client_tls13_session;
  9204. clientSessionSz = sizeof(canned_client_tls13_session);
  9205. break;
  9206. #endif
  9207. }
  9208. server_args.callbacks = &server_cbf;
  9209. server_args.signal = &ready;
  9210. start_thread(tls_export_server, &server_args, &serverThread);
  9211. wait_tcp_ready(&server_args);
  9212. #ifdef WOLFSSL_TIRTOS
  9213. fdOpenSession(Task_self());
  9214. #endif
  9215. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9216. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9217. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9218. clientSessionSz);
  9219. replySz = sizeof(reply);
  9220. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9221. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9222. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9223. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9224. #endif
  9225. wolfSSL_set_fd(ssl, sockfd);
  9226. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9227. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9228. wolfSSL_free(ssl);
  9229. wolfSSL_CTX_free(ctx);
  9230. CloseSocket(sockfd);
  9231. #ifdef WOLFSSL_TIRTOS
  9232. fdCloseSession(Task_self());
  9233. #endif
  9234. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9235. && defined(HAVE_THREAD_LS)
  9236. wc_ecc_fp_free(); /* free per thread cache */
  9237. #endif
  9238. join_thread(serverThread);
  9239. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9240. FreeTcpReady(&ready);
  9241. #ifdef WOLFSSL_TIRTOS
  9242. fdOpenSession(Task_self());
  9243. #endif
  9244. return EXPECT_RESULT();
  9245. }
  9246. #endif
  9247. static int test_wolfSSL_tls_export(void)
  9248. {
  9249. int res = TEST_SKIPPED;
  9250. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9251. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9252. #ifdef WOLFSSL_TLS13
  9253. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9254. #endif
  9255. res = TEST_RES_CHECK(1);
  9256. #endif
  9257. return res;
  9258. }
  9259. /*----------------------------------------------------------------------------*
  9260. | TLS extensions tests
  9261. *----------------------------------------------------------------------------*/
  9262. #ifdef ENABLE_TLS_CALLBACK_TEST
  9263. /* Connection test runner - generic */
  9264. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9265. callback_functions* server_callbacks)
  9266. {
  9267. tcp_ready ready;
  9268. func_args client_args;
  9269. func_args server_args;
  9270. THREAD_TYPE serverThread;
  9271. XMEMSET(&client_args, 0, sizeof(func_args));
  9272. XMEMSET(&server_args, 0, sizeof(func_args));
  9273. StartTCP();
  9274. client_args.callbacks = client_callbacks;
  9275. server_args.callbacks = server_callbacks;
  9276. #ifdef WOLFSSL_TIRTOS
  9277. fdOpenSession(Task_self());
  9278. #endif
  9279. /* RUN Server side */
  9280. InitTcpReady(&ready);
  9281. #if defined(USE_WINDOWS_API)
  9282. /* use RNG to get random port if using windows */
  9283. ready.port = GetRandomPort();
  9284. #endif
  9285. server_args.signal = &ready;
  9286. client_args.signal = &ready;
  9287. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9288. wait_tcp_ready(&server_args);
  9289. /* RUN Client side */
  9290. run_wolfssl_client(&client_args);
  9291. join_thread(serverThread);
  9292. FreeTcpReady(&ready);
  9293. #ifdef WOLFSSL_TIRTOS
  9294. fdCloseSession(Task_self());
  9295. #endif
  9296. client_callbacks->return_code = client_args.return_code;
  9297. server_callbacks->return_code = server_args.return_code;
  9298. }
  9299. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9300. #ifdef HAVE_SNI
  9301. static int test_wolfSSL_UseSNI_params(void)
  9302. {
  9303. EXPECT_DECLS;
  9304. #if !defined(NO_WOLFSSL_CLIENT)
  9305. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9306. WOLFSSL *ssl = wolfSSL_new(ctx);
  9307. ExpectNotNull(ctx);
  9308. ExpectNotNull(ssl);
  9309. /* invalid [ctx|ssl] */
  9310. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9311. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9312. /* invalid type */
  9313. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9314. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9315. /* invalid data */
  9316. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9317. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9318. /* success case */
  9319. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9320. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9321. wolfSSL_free(ssl);
  9322. wolfSSL_CTX_free(ctx);
  9323. #endif /* !NO_WOLFSSL_CLIENT */
  9324. return EXPECT_RESULT();
  9325. }
  9326. /* BEGIN of connection tests callbacks */
  9327. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9328. {
  9329. AssertIntEQ(WOLFSSL_SUCCESS,
  9330. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9331. }
  9332. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9333. {
  9334. AssertIntEQ(WOLFSSL_SUCCESS,
  9335. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9336. }
  9337. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9338. {
  9339. AssertIntEQ(WOLFSSL_SUCCESS,
  9340. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9341. }
  9342. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9343. {
  9344. use_SNI_at_ssl(ssl);
  9345. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9346. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9347. }
  9348. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9349. {
  9350. use_SNI_at_ssl(ssl);
  9351. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9352. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9353. }
  9354. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9355. {
  9356. use_SNI_at_ctx(ctx);
  9357. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9358. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9359. }
  9360. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9361. {
  9362. use_SNI_at_ssl(ssl);
  9363. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9364. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9365. }
  9366. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9367. {
  9368. use_SNI_at_ctx(ctx);
  9369. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9370. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9371. }
  9372. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9373. {
  9374. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9375. }
  9376. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9377. {
  9378. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9379. }
  9380. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9381. {
  9382. byte type = WOLFSSL_SNI_HOST_NAME;
  9383. void* request = (void*) &type; /* to be overwritten */
  9384. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9385. AssertNotNull(request);
  9386. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9387. AssertNull(request);
  9388. }
  9389. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9390. {
  9391. byte type = WOLFSSL_SNI_HOST_NAME;
  9392. void* request = NULL;
  9393. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9394. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9395. AssertNotNull(request);
  9396. AssertStrEQ("www.wolfssl.com", (char*)request);
  9397. }
  9398. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9399. {
  9400. byte type = WOLFSSL_SNI_HOST_NAME;
  9401. void* request = NULL;
  9402. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9403. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9404. AssertNotNull(request);
  9405. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9406. }
  9407. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9408. {
  9409. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9410. }
  9411. /* END of connection tests callbacks */
  9412. static int test_wolfSSL_UseSNI_connection(void)
  9413. {
  9414. int res = TEST_SKIPPED;
  9415. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9416. callback_functions client_cb;
  9417. callback_functions server_cb;
  9418. size_t i;
  9419. #ifdef WOLFSSL_STATIC_MEMORY
  9420. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9421. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9422. #endif
  9423. struct {
  9424. method_provider client_meth;
  9425. method_provider server_meth;
  9426. #ifdef WOLFSSL_STATIC_MEMORY
  9427. wolfSSL_method_func client_meth_ex;
  9428. wolfSSL_method_func server_meth_ex;
  9429. #endif
  9430. } methods[] = {
  9431. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9432. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9433. #ifdef WOLFSSL_STATIC_MEMORY
  9434. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9435. #endif
  9436. },
  9437. #endif
  9438. #ifndef WOLFSSL_NO_TLS12
  9439. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9440. #ifdef WOLFSSL_STATIC_MEMORY
  9441. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9442. #endif
  9443. },
  9444. #endif
  9445. #ifdef WOLFSSL_TLS13
  9446. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9447. #ifdef WOLFSSL_STATIC_MEMORY
  9448. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9449. #endif
  9450. },
  9451. #endif
  9452. };
  9453. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9454. for (i = 0; i < methodsSz; i++) {
  9455. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9456. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9457. client_cb.method = methods[i].client_meth;
  9458. server_cb.method = methods[i].server_meth;
  9459. client_cb.devId = testDevId;
  9460. server_cb.devId = testDevId;
  9461. #ifdef WOLFSSL_STATIC_MEMORY
  9462. client_cb.method_ex = methods[i].client_meth_ex;
  9463. server_cb.method_ex = methods[i].server_meth_ex;
  9464. client_cb.mem = cliMem;
  9465. client_cb.memSz = (word32)sizeof(cliMem);
  9466. server_cb.mem = svrMem;
  9467. server_cb.memSz = (word32)sizeof(svrMem);;
  9468. #endif
  9469. /* success case at ctx */
  9470. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9471. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9472. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9473. test_wolfSSL_client_server(&client_cb, &server_cb);
  9474. /* success case at ssl */
  9475. fprintf(stderr, "\tsuccess case at ssl\n");
  9476. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9477. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9478. test_wolfSSL_client_server(&client_cb, &server_cb);
  9479. /* default mismatch behavior */
  9480. fprintf(stderr, "\tdefault mismatch behavior\n");
  9481. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9482. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9483. test_wolfSSL_client_server(&client_cb, &server_cb);
  9484. /* continue on mismatch */
  9485. fprintf(stderr, "\tcontinue on mismatch\n");
  9486. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9487. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9488. test_wolfSSL_client_server(&client_cb, &server_cb);
  9489. /* fake answer on mismatch */
  9490. fprintf(stderr, "\tfake answer on mismatch\n");
  9491. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9492. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9493. test_wolfSSL_client_server(&client_cb, &server_cb);
  9494. /* sni abort - success */
  9495. fprintf(stderr, "\tsni abort - success\n");
  9496. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9497. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9498. test_wolfSSL_client_server(&client_cb, &server_cb);
  9499. /* sni abort - abort when absent (ctx) */
  9500. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9501. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9502. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9503. test_wolfSSL_client_server(&client_cb, &server_cb);
  9504. /* sni abort - abort when absent (ssl) */
  9505. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9506. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9507. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9508. test_wolfSSL_client_server(&client_cb, &server_cb);
  9509. /* sni abort - success when overwritten */
  9510. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9511. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9512. 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;
  9513. test_wolfSSL_client_server(&client_cb, &server_cb);
  9514. /* sni abort - success when allowing mismatches */
  9515. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9516. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9517. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9518. test_wolfSSL_client_server(&client_cb, &server_cb);
  9519. }
  9520. res = TEST_RES_CHECK(1);
  9521. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9522. return res;
  9523. }
  9524. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9525. {
  9526. EXPECT_DECLS;
  9527. byte buff[] = { /* www.paypal.com */
  9528. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9529. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9530. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9531. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9532. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9533. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9534. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9535. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9536. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9537. };
  9538. byte buff2[] = { /* api.textmate.org */
  9539. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9540. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9541. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9542. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9543. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9544. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9545. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9546. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9547. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9548. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9549. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9550. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9551. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9552. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9553. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9554. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9555. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9556. };
  9557. byte buff3[] = { /* no sni extension */
  9558. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9559. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9560. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9561. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9562. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9563. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9564. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9565. };
  9566. byte buff4[] = { /* last extension has zero size */
  9567. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9568. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9569. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9570. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9571. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9572. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9573. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9574. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9575. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9576. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9577. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9578. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9579. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9580. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9581. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9582. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9583. 0x12, 0x00, 0x00
  9584. };
  9585. byte buff5[] = { /* SSL v2.0 client hello */
  9586. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9587. /* dummy bytes below, just to pass size check */
  9588. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9589. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9590. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9591. };
  9592. byte result[32] = {0};
  9593. word32 length = 32;
  9594. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9595. 0, result, &length));
  9596. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9597. 0, result, &length));
  9598. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9599. 1, result, &length));
  9600. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9601. 0, result, &length));
  9602. buff[0] = 0x16;
  9603. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9604. 0, result, &length));
  9605. buff[1] = 0x03;
  9606. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9607. sizeof(buff), 0, result, &length));
  9608. buff[2] = 0x03;
  9609. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9610. sizeof(buff), 0, result, &length));
  9611. buff[4] = 0x64;
  9612. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9613. 0, result, &length));
  9614. if (EXPECT_SUCCESS())
  9615. result[length] = 0;
  9616. ExpectStrEQ("www.paypal.com", (const char*) result);
  9617. length = 32;
  9618. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9619. 0, result, &length));
  9620. if (EXPECT_SUCCESS())
  9621. result[length] = 0;
  9622. ExpectStrEQ("api.textmate.org", (const char*) result);
  9623. /* SSL v2.0 tests */
  9624. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9625. sizeof(buff5), 0, result, &length));
  9626. buff5[2] = 0x02;
  9627. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9628. sizeof(buff5), 0, result, &length));
  9629. buff5[2] = 0x01; buff5[6] = 0x08;
  9630. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9631. sizeof(buff5), 0, result, &length));
  9632. buff5[6] = 0x09; buff5[8] = 0x01;
  9633. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9634. sizeof(buff5), 0, result, &length));
  9635. return EXPECT_RESULT();
  9636. }
  9637. #endif /* HAVE_SNI */
  9638. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9639. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9640. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9641. /* Dummy peer functions to satisfy the exporter/importer */
  9642. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9643. int* ipSz, unsigned short* port, int* fam)
  9644. {
  9645. (void)ssl;
  9646. ip[0] = -1;
  9647. *ipSz = 1;
  9648. *port = 1;
  9649. *fam = 2;
  9650. return 1;
  9651. }
  9652. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9653. int ipSz, unsigned short port, int fam)
  9654. {
  9655. (void)ssl;
  9656. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9657. return 0;
  9658. return 1;
  9659. }
  9660. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9661. WOLFSSL **ssl)
  9662. {
  9663. EXPECT_DECLS;
  9664. unsigned char* sessionBuf = NULL;
  9665. unsigned int sessionSz = 0;
  9666. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9667. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9668. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9669. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9670. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9671. ExpectNotNull(sessionBuf =
  9672. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9673. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9674. wolfSSL_free(*ssl);
  9675. *ssl = NULL;
  9676. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9677. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9678. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9679. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9680. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9681. return EXPECT_RESULT();
  9682. }
  9683. #endif
  9684. static int test_wolfSSL_dtls_export_peers(void)
  9685. {
  9686. EXPECT_DECLS;
  9687. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9688. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9689. test_ssl_cbf client_cbf;
  9690. test_ssl_cbf server_cbf;
  9691. size_t i, j;
  9692. struct test_params {
  9693. method_provider client_meth;
  9694. method_provider server_meth;
  9695. const char* dtls_version;
  9696. } params[] = {
  9697. #ifndef NO_OLD_TLS
  9698. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9699. #endif
  9700. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9701. /* TODO DTLS 1.3 exporting not supported
  9702. #ifdef WOLFSSL_DTLS13
  9703. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9704. #endif
  9705. */
  9706. };
  9707. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9708. for (j = 0; j <= 0b11; j++) {
  9709. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9710. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9711. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9712. client_cbf.method = params[i].client_meth;
  9713. server_cbf.method = params[i].server_meth;
  9714. if (j & 0b01) {
  9715. client_cbf.on_handshake =
  9716. test_wolfSSL_dtls_export_peers_on_handshake;
  9717. printf(" With client export;");
  9718. }
  9719. if (j & 0b10) {
  9720. server_cbf.on_handshake =
  9721. test_wolfSSL_dtls_export_peers_on_handshake;
  9722. printf(" With server export;");
  9723. }
  9724. printf("\n");
  9725. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9726. &server_cbf, NULL), TEST_SUCCESS);
  9727. if (!EXPECT_SUCCESS())
  9728. break;
  9729. }
  9730. }
  9731. #endif
  9732. return EXPECT_RESULT();
  9733. }
  9734. static int test_wolfSSL_UseTrustedCA(void)
  9735. {
  9736. EXPECT_DECLS;
  9737. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9738. && !defined(NO_RSA)
  9739. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9740. WOLFSSL_CTX *ctx = NULL;
  9741. WOLFSSL *ssl = NULL;
  9742. byte id[20];
  9743. #ifndef NO_WOLFSSL_SERVER
  9744. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9745. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9746. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9747. #else
  9748. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9749. #endif
  9750. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9751. XMEMSET(id, 0, sizeof(id));
  9752. /* error cases */
  9753. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9754. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9755. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9756. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9757. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9758. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9759. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9760. #ifdef NO_SHA
  9761. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9762. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9763. #endif
  9764. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9765. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9766. /* success cases */
  9767. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9768. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9769. #ifndef NO_SHA
  9770. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9771. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9772. #endif
  9773. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9774. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9775. wolfSSL_free(ssl);
  9776. wolfSSL_CTX_free(ctx);
  9777. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9778. #endif /* HAVE_TRUSTED_CA */
  9779. return EXPECT_RESULT();
  9780. }
  9781. static int test_wolfSSL_UseMaxFragment(void)
  9782. {
  9783. EXPECT_DECLS;
  9784. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9785. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9786. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9787. #ifndef NO_WOLFSSL_SERVER
  9788. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9789. #else
  9790. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9791. #endif
  9792. WOLFSSL *ssl = NULL;
  9793. #ifdef OPENSSL_EXTRA
  9794. int (*UseMaxFragment)(SSL *s, unsigned char mode);
  9795. int (*CTX_UseMaxFragment)(SSL_CTX *c, unsigned char mode);
  9796. #else
  9797. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9798. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9799. #endif
  9800. #ifndef NO_WOLFSSL_SERVER
  9801. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9802. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9803. #endif
  9804. ExpectNotNull(ctx);
  9805. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9806. #ifdef OPENSSL_EXTRA
  9807. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9808. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9809. #else
  9810. UseMaxFragment = wolfSSL_UseMaxFragment;
  9811. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9812. #endif
  9813. /* error cases */
  9814. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9815. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9816. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9817. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9818. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9819. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9820. /* success case */
  9821. #ifdef OPENSSL_EXTRA
  9822. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9823. #else
  9824. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9825. #endif
  9826. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9827. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9828. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9829. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9830. #ifdef OPENSSL_EXTRA
  9831. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9832. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9833. #else
  9834. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9835. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9836. #endif
  9837. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9838. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9839. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9840. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9841. #ifdef OPENSSL_EXTRA
  9842. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9843. #else
  9844. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9845. #endif
  9846. wolfSSL_free(ssl);
  9847. wolfSSL_CTX_free(ctx);
  9848. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9849. #endif
  9850. return EXPECT_RESULT();
  9851. }
  9852. static int test_wolfSSL_UseTruncatedHMAC(void)
  9853. {
  9854. EXPECT_DECLS;
  9855. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9856. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9857. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9858. #ifndef NO_WOLFSSL_SERVER
  9859. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9860. #else
  9861. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9862. #endif
  9863. WOLFSSL *ssl = NULL;
  9864. ExpectNotNull(ctx);
  9865. #ifndef NO_WOLFSSL_SERVER
  9866. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9867. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9868. #endif
  9869. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9870. /* error cases */
  9871. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9872. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9873. /* success case */
  9874. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9875. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9876. wolfSSL_free(ssl);
  9877. wolfSSL_CTX_free(ctx);
  9878. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9879. #endif
  9880. return EXPECT_RESULT();
  9881. }
  9882. static int test_wolfSSL_UseSupportedCurve(void)
  9883. {
  9884. EXPECT_DECLS;
  9885. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9886. !defined(NO_TLS)
  9887. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9888. WOLFSSL *ssl = wolfSSL_new(ctx);
  9889. ExpectNotNull(ctx);
  9890. ExpectNotNull(ssl);
  9891. /* error cases */
  9892. ExpectIntNE(WOLFSSL_SUCCESS,
  9893. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9894. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9895. ExpectIntNE(WOLFSSL_SUCCESS,
  9896. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9897. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9898. /* success case */
  9899. ExpectIntEQ(WOLFSSL_SUCCESS,
  9900. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9901. ExpectIntEQ(WOLFSSL_SUCCESS,
  9902. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9903. wolfSSL_free(ssl);
  9904. wolfSSL_CTX_free(ctx);
  9905. #endif
  9906. return EXPECT_RESULT();
  9907. }
  9908. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9909. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9910. {
  9911. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9912. }
  9913. static void use_ALPN_all(WOLFSSL* ssl)
  9914. {
  9915. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9916. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9917. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9918. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9919. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9920. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9921. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9922. }
  9923. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9924. {
  9925. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9926. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9927. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9928. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9929. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9930. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9931. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9932. }
  9933. static void use_ALPN_one(WOLFSSL* ssl)
  9934. {
  9935. /* spdy/2 */
  9936. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9937. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9938. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9939. }
  9940. static void use_ALPN_unknown(WOLFSSL* ssl)
  9941. {
  9942. /* http/2.0 */
  9943. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9944. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9945. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9946. }
  9947. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9948. {
  9949. /* http/2.0 */
  9950. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9951. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9952. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9953. }
  9954. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9955. {
  9956. /* spdy/3 */
  9957. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9958. char *proto = NULL;
  9959. word16 protoSz = 0;
  9960. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9961. /* check value */
  9962. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9963. if (proto) {
  9964. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9965. }
  9966. }
  9967. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9968. {
  9969. char *proto = NULL;
  9970. word16 protoSz = 0;
  9971. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9972. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9973. /* check value */
  9974. AssertIntEQ(1, (0 == protoSz));
  9975. AssertIntEQ(1, (NULL == proto));
  9976. }
  9977. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9978. {
  9979. /* http/1.1 */
  9980. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9981. char *proto;
  9982. word16 protoSz = 0;
  9983. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9984. /* check value */
  9985. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9986. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9987. }
  9988. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9989. {
  9990. /* spdy/2 */
  9991. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9992. char *proto;
  9993. word16 protoSz = 0;
  9994. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9995. /* check value */
  9996. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9997. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9998. }
  9999. static void verify_ALPN_client_list(WOLFSSL* ssl)
  10000. {
  10001. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  10002. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  10003. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  10004. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  10005. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10006. char *clist = NULL;
  10007. word16 clistSz = 0;
  10008. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  10009. &clistSz));
  10010. /* check value */
  10011. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  10012. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  10013. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  10014. }
  10015. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10016. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10017. /* ALPN select callback, success with spdy/2 */
  10018. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  10019. unsigned char *outlen, const unsigned char *in,
  10020. unsigned int inlen, void *arg)
  10021. {
  10022. /* spdy/2 */
  10023. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10024. (void)ssl;
  10025. (void)arg;
  10026. /* adding +1 since LEN byte comes first */
  10027. if (inlen < sizeof(proto) + 1) {
  10028. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10029. }
  10030. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  10031. *out = in + 1;
  10032. *outlen = (unsigned char)sizeof(proto);
  10033. return SSL_TLSEXT_ERR_OK;
  10034. }
  10035. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10036. }
  10037. /* ALPN select callback, force failure */
  10038. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  10039. unsigned char *outlen, const unsigned char *in,
  10040. unsigned int inlen, void *arg)
  10041. {
  10042. (void)ssl;
  10043. (void)out;
  10044. (void)outlen;
  10045. (void)in;
  10046. (void)inlen;
  10047. (void)arg;
  10048. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10049. }
  10050. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  10051. {
  10052. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  10053. }
  10054. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  10055. {
  10056. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  10057. }
  10058. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  10059. static int test_wolfSSL_UseALPN_connection(void)
  10060. {
  10061. int res = TEST_SKIPPED;
  10062. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10063. callback_functions client_cb;
  10064. callback_functions server_cb;
  10065. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10066. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10067. client_cb.method = wolfSSLv23_client_method;
  10068. server_cb.method = wolfSSLv23_server_method;
  10069. client_cb.devId = testDevId;
  10070. server_cb.devId = testDevId;
  10071. /* success case same list */
  10072. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10073. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  10074. test_wolfSSL_client_server(&client_cb, &server_cb);
  10075. /* success case only one for server */
  10076. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10077. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  10078. test_wolfSSL_client_server(&client_cb, &server_cb);
  10079. /* success case only one for client */
  10080. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  10081. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  10082. test_wolfSSL_client_server(&client_cb, &server_cb);
  10083. /* success case none for client */
  10084. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10085. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  10086. test_wolfSSL_client_server(&client_cb, &server_cb);
  10087. /* success case mismatch behavior but option 'continue' set */
  10088. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  10089. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  10090. test_wolfSSL_client_server(&client_cb, &server_cb);
  10091. /* success case read protocol send by client */
  10092. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10093. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  10094. test_wolfSSL_client_server(&client_cb, &server_cb);
  10095. /* mismatch behavior with same list
  10096. * the first and only this one must be taken */
  10097. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10098. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10099. test_wolfSSL_client_server(&client_cb, &server_cb);
  10100. /* default mismatch behavior */
  10101. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10102. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10103. test_wolfSSL_client_server(&client_cb, &server_cb);
  10104. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10105. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10106. /* WOLFSSL-level ALPN select callback tests */
  10107. /* Callback: success (one protocol, spdy/2) */
  10108. client_cb.ctx_ready = NULL;
  10109. client_cb.ssl_ready = use_ALPN_one;
  10110. client_cb.on_result = verify_ALPN_matching_spdy2;
  10111. server_cb.ctx_ready = NULL;
  10112. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10113. server_cb.on_result = verify_ALPN_matching_spdy2;
  10114. test_wolfSSL_client_server(&client_cb, &server_cb);
  10115. /* Callback: failure (one client protocol, spdy/2) */
  10116. client_cb.ctx_ready = NULL;
  10117. client_cb.ssl_ready = use_ALPN_one;
  10118. client_cb.on_result = NULL;
  10119. server_cb.ctx_ready = NULL;
  10120. server_cb.ssl_ready = use_ALPN_failure_callback;
  10121. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10122. test_wolfSSL_client_server(&client_cb, &server_cb);
  10123. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10124. res = TEST_RES_CHECK(1);
  10125. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10126. return res;
  10127. }
  10128. static int test_wolfSSL_UseALPN_params(void)
  10129. {
  10130. EXPECT_DECLS;
  10131. #ifndef NO_WOLFSSL_CLIENT
  10132. /* "http/1.1" */
  10133. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10134. /* "spdy/1" */
  10135. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10136. /* "spdy/2" */
  10137. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10138. /* "spdy/3" */
  10139. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10140. char buff[256];
  10141. word32 idx;
  10142. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10143. WOLFSSL *ssl = wolfSSL_new(ctx);
  10144. ExpectNotNull(ctx);
  10145. ExpectNotNull(ssl);
  10146. /* error cases */
  10147. ExpectIntNE(WOLFSSL_SUCCESS,
  10148. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10149. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10150. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10151. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10152. /* success case */
  10153. /* http1 only */
  10154. ExpectIntEQ(WOLFSSL_SUCCESS,
  10155. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10156. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10157. /* http1, spdy1 */
  10158. XMEMCPY(buff, http1, sizeof(http1));
  10159. idx = sizeof(http1);
  10160. buff[idx++] = ',';
  10161. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10162. idx += sizeof(spdy1);
  10163. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10164. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10165. /* http1, spdy2, spdy1 */
  10166. XMEMCPY(buff, http1, sizeof(http1));
  10167. idx = sizeof(http1);
  10168. buff[idx++] = ',';
  10169. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10170. idx += sizeof(spdy2);
  10171. buff[idx++] = ',';
  10172. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10173. idx += sizeof(spdy1);
  10174. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10175. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10176. /* spdy3, http1, spdy2, spdy1 */
  10177. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10178. idx = sizeof(spdy3);
  10179. buff[idx++] = ',';
  10180. XMEMCPY(buff+idx, http1, sizeof(http1));
  10181. idx += sizeof(http1);
  10182. buff[idx++] = ',';
  10183. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10184. idx += sizeof(spdy2);
  10185. buff[idx++] = ',';
  10186. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10187. idx += sizeof(spdy1);
  10188. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10189. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10190. wolfSSL_free(ssl);
  10191. wolfSSL_CTX_free(ctx);
  10192. #endif
  10193. return EXPECT_RESULT();
  10194. }
  10195. #endif /* HAVE_ALPN */
  10196. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10197. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10198. {
  10199. unsigned char p[] = {
  10200. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10201. 6, 's', 'p', 'd', 'y', '/', '2',
  10202. 6, 's', 'p', 'd', 'y', '/', '1',
  10203. };
  10204. unsigned char p_len = sizeof(p);
  10205. int ret;
  10206. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10207. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10208. AssertIntEQ(ret, 0);
  10209. #else
  10210. AssertIntEQ(ret, SSL_SUCCESS);
  10211. #endif
  10212. }
  10213. static void set_alpn_protos(SSL* ssl)
  10214. {
  10215. unsigned char p[] = {
  10216. 6, 's', 'p', 'd', 'y', '/', '3',
  10217. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10218. 6, 's', 'p', 'd', 'y', '/', '2',
  10219. 6, 's', 'p', 'd', 'y', '/', '1',
  10220. };
  10221. unsigned char p_len = sizeof(p);
  10222. int ret;
  10223. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10224. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10225. AssertIntEQ(ret, 0);
  10226. #else
  10227. AssertIntEQ(ret, SSL_SUCCESS);
  10228. #endif
  10229. }
  10230. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10231. {
  10232. /* "spdy/3" */
  10233. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10234. const unsigned char *proto;
  10235. unsigned int protoSz = 0;
  10236. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10237. /* check value */
  10238. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10239. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10240. }
  10241. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10242. {
  10243. /* "http/1.1" */
  10244. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10245. const unsigned char *proto;
  10246. unsigned int protoSz = 0;
  10247. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10248. /* check value */
  10249. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10250. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10251. }
  10252. static int test_wolfSSL_set_alpn_protos(void)
  10253. {
  10254. int res = TEST_SKIPPED;
  10255. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10256. callback_functions client_cb;
  10257. callback_functions server_cb;
  10258. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10259. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10260. client_cb.method = wolfSSLv23_client_method;
  10261. server_cb.method = wolfSSLv23_server_method;
  10262. client_cb.devId = testDevId;
  10263. server_cb.devId = testDevId;
  10264. /* use CTX_alpn_protos */
  10265. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10266. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10267. test_wolfSSL_client_server(&client_cb, &server_cb);
  10268. /* use set_alpn_protos */
  10269. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10270. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10271. test_wolfSSL_client_server(&client_cb, &server_cb);
  10272. res = TEST_SUCCESS;
  10273. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10274. return res;
  10275. }
  10276. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10277. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10278. {
  10279. EXPECT_DECLS;
  10280. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10281. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10282. WOLFSSL *ssl = wolfSSL_new(ctx);
  10283. ExpectNotNull(ctx);
  10284. ExpectNotNull(ssl);
  10285. /* error cases */
  10286. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10287. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10288. /* success cases */
  10289. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10290. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10291. wolfSSL_free(ssl);
  10292. wolfSSL_CTX_free(ctx);
  10293. #endif
  10294. return EXPECT_RESULT();
  10295. }
  10296. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10297. {
  10298. EXPECT_DECLS;
  10299. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10300. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10301. WOLFSSL *ssl = wolfSSL_new(ctx);
  10302. ExpectNotNull(ctx);
  10303. ExpectNotNull(ssl);
  10304. /* error cases */
  10305. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10306. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10307. /* success cases */
  10308. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10309. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10310. wolfSSL_free(ssl);
  10311. wolfSSL_CTX_free(ctx);
  10312. #endif
  10313. return EXPECT_RESULT();
  10314. }
  10315. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10316. static int test_wolfSSL_SCR_Reconnect(void)
  10317. {
  10318. EXPECT_DECLS;
  10319. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10320. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10321. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10322. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10323. struct test_memio_ctx test_ctx;
  10324. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10325. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10326. byte data;
  10327. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10328. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10329. test_ctx.s_ciphers =
  10330. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10331. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10332. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10333. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10334. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10335. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10336. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10337. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10338. /* WOLFSSL_FATAL_ERROR since it will block */
  10339. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10340. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10341. WOLFSSL_ERROR_WANT_READ);
  10342. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10343. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10344. WOLFSSL_ERROR_WANT_READ);
  10345. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10346. wolfSSL_free(ssl_c);
  10347. ssl_c = NULL;
  10348. wolfSSL_free(ssl_s);
  10349. ssl_s = NULL;
  10350. wolfSSL_CTX_free(ctx_c);
  10351. ctx_c = NULL;
  10352. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10353. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10354. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10355. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10356. wolfSSL_free(ssl_s);
  10357. wolfSSL_free(ssl_c);
  10358. wolfSSL_CTX_free(ctx_s);
  10359. wolfSSL_CTX_free(ctx_c);
  10360. #endif
  10361. return EXPECT_RESULT();
  10362. }
  10363. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10364. (!defined(NO_RSA) || defined(HAVE_ECC))
  10365. /* Called when writing. */
  10366. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10367. {
  10368. (void)ssl;
  10369. (void)buf;
  10370. (void)sz;
  10371. (void)ctx;
  10372. /* Force error return from wolfSSL_accept_TLSv13(). */
  10373. return WANT_WRITE;
  10374. }
  10375. /* Called when reading. */
  10376. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10377. {
  10378. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10379. int len = (int)msg->length;
  10380. (void)ssl;
  10381. (void)sz;
  10382. /* Pass back as much of message as will fit in buffer. */
  10383. if (len > sz)
  10384. len = sz;
  10385. XMEMCPY(buf, msg->buffer, len);
  10386. /* Move over returned data. */
  10387. msg->buffer += len;
  10388. msg->length -= len;
  10389. /* Amount actually copied. */
  10390. return len;
  10391. }
  10392. #endif
  10393. /* Test the detection of duplicate known TLS extensions.
  10394. * Specifically in a ClientHello.
  10395. */
  10396. static int test_tls_ext_duplicate(void)
  10397. {
  10398. EXPECT_DECLS;
  10399. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10400. !defined(NO_FILESYSTEM)
  10401. const unsigned char clientHelloDupTlsExt[] = {
  10402. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10403. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10404. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10405. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10406. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10407. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10408. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10409. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10410. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10411. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10412. 0x00, 0x9e, 0x01, 0x00,
  10413. /* Extensions - duplicate signature algorithms. */
  10414. 0x00, 0x19, 0x00, 0x0d,
  10415. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10416. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10417. /* Supported Versions extension for TLS 1.3. */
  10418. 0x00, 0x2b,
  10419. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10420. };
  10421. WOLFSSL_BUFFER_INFO msg;
  10422. const char* testCertFile;
  10423. const char* testKeyFile;
  10424. WOLFSSL_CTX *ctx = NULL;
  10425. WOLFSSL *ssl = NULL;
  10426. #ifndef NO_RSA
  10427. testCertFile = svrCertFile;
  10428. testKeyFile = svrKeyFile;
  10429. #elif defined(HAVE_ECC)
  10430. testCertFile = eccCertFile;
  10431. testKeyFile = eccKeyFile;
  10432. #endif
  10433. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10434. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10435. WOLFSSL_FILETYPE_PEM));
  10436. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10437. WOLFSSL_FILETYPE_PEM));
  10438. /* Read from 'msg'. */
  10439. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10440. /* No where to send to - dummy sender. */
  10441. wolfSSL_SetIOSend(ctx, DummySend);
  10442. ssl = wolfSSL_new(ctx);
  10443. ExpectNotNull(ssl);
  10444. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10445. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10446. wolfSSL_SetIOReadCtx(ssl, &msg);
  10447. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10448. /* can return duplicate ext error or socket error if the peer closed down
  10449. * while sending alert */
  10450. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10451. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10452. }
  10453. wolfSSL_free(ssl);
  10454. wolfSSL_CTX_free(ctx);
  10455. #endif
  10456. return EXPECT_RESULT();
  10457. }
  10458. /*----------------------------------------------------------------------------*
  10459. | X509 Tests
  10460. *----------------------------------------------------------------------------*/
  10461. static int test_wolfSSL_X509_NAME_get_entry(void)
  10462. {
  10463. EXPECT_DECLS;
  10464. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10465. #if defined(OPENSSL_ALL) || \
  10466. (defined(OPENSSL_EXTRA) && \
  10467. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10468. /* use openssl like name to test mapping */
  10469. X509_NAME_ENTRY* ne = NULL;
  10470. X509_NAME* name = NULL;
  10471. X509* x509 = NULL;
  10472. #ifndef NO_FILESYSTEM
  10473. ASN1_STRING* asn = NULL;
  10474. char* subCN = NULL;
  10475. #endif
  10476. int idx = 0;
  10477. ASN1_OBJECT *object = NULL;
  10478. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10479. defined(WOLFSSL_NGINX)
  10480. #ifndef NO_BIO
  10481. BIO* bio = NULL;
  10482. #endif
  10483. #endif
  10484. #ifndef NO_FILESYSTEM
  10485. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10486. WOLFSSL_FILETYPE_PEM));
  10487. ExpectNotNull(name = X509_get_subject_name(x509));
  10488. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10489. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10490. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10491. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10492. wolfSSL_FreeX509(x509);
  10493. x509 = NULL;
  10494. #endif
  10495. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10496. WOLFSSL_FILETYPE_PEM));
  10497. ExpectNotNull(name = X509_get_subject_name(x509));
  10498. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10499. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10500. defined(WOLFSSL_NGINX)
  10501. #ifndef NO_BIO
  10502. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10503. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10504. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10505. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10506. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10507. BIO_free(bio);
  10508. #endif
  10509. #endif
  10510. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10511. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10512. wolfSSL_FreeX509(x509);
  10513. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10514. #endif /* !NO_CERTS && !NO_RSA */
  10515. return EXPECT_RESULT();
  10516. }
  10517. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10518. static int test_wolfSSL_PKCS12(void)
  10519. {
  10520. EXPECT_DECLS;
  10521. /* .p12 file is encrypted with DES3 */
  10522. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10523. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10524. * Password Key
  10525. */
  10526. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10527. !defined(NO_STDIO_FILESYSTEM) && \
  10528. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10529. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10530. byte buf[6000];
  10531. char file[] = "./certs/test-servercert.p12";
  10532. char order[] = "./certs/ecc-rsa-server.p12";
  10533. #ifdef WC_RC2
  10534. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10535. #endif
  10536. char pass[] = "a password";
  10537. const char goodPsw[] = "wolfSSL test";
  10538. const char badPsw[] = "bad";
  10539. #ifdef HAVE_ECC
  10540. WOLFSSL_X509_NAME *subject = NULL;
  10541. WOLFSSL_X509 *x509 = NULL;
  10542. #endif
  10543. XFILE f = XBADFILE;
  10544. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10545. WOLFSSL_BIO *bio = NULL;
  10546. WOLFSSL_EVP_PKEY *pkey = NULL;
  10547. WC_PKCS12 *pkcs12 = NULL;
  10548. WC_PKCS12 *pkcs12_2 = NULL;
  10549. WOLFSSL_X509 *cert = NULL;
  10550. WOLFSSL_X509 *tmp = NULL;
  10551. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10552. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10553. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10554. WOLFSSL_CTX *ctx = NULL;
  10555. WOLFSSL *ssl = NULL;
  10556. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10557. #endif
  10558. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10559. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10560. if (f != XBADFILE) {
  10561. XFCLOSE(f);
  10562. f = XBADFILE;
  10563. }
  10564. goodPswLen = (int)XSTRLEN(goodPsw);
  10565. badPswLen = (int)XSTRLEN(badPsw);
  10566. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10567. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10568. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10569. ExpectNotNull(pkcs12);
  10570. BIO_free(bio);
  10571. bio = NULL;
  10572. /* check verify MAC directly */
  10573. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10574. /* check verify MAC fail case directly */
  10575. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10576. /* check verify MAC fail case */
  10577. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10578. ExpectNull(pkey);
  10579. ExpectNull(cert);
  10580. /* check parse with no extra certs kept */
  10581. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10582. 1);
  10583. ExpectNotNull(pkey);
  10584. ExpectNotNull(cert);
  10585. wolfSSL_EVP_PKEY_free(pkey);
  10586. pkey = NULL;
  10587. wolfSSL_X509_free(cert);
  10588. cert = NULL;
  10589. /* check parse with extra certs kept */
  10590. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10591. 1);
  10592. ExpectNotNull(pkey);
  10593. ExpectNotNull(cert);
  10594. ExpectNotNull(ca);
  10595. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10596. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10597. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10598. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10599. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10600. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10601. #else
  10602. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10603. #endif
  10604. /* Copy stack structure */
  10605. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10606. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10607. /* CTX now owns the tmp_ca stack structure */
  10608. tmp_ca = NULL;
  10609. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10610. ExpectNotNull(tmp_ca);
  10611. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10612. /* Check that the main cert is also set */
  10613. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10614. ExpectNotNull(ssl = SSL_new(ctx));
  10615. ExpectNotNull(SSL_get_certificate(ssl));
  10616. SSL_free(ssl);
  10617. SSL_CTX_free(ctx);
  10618. ctx = NULL;
  10619. #endif
  10620. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10621. /* should be 2 other certs on stack */
  10622. ExpectNotNull(tmp = sk_X509_pop(ca));
  10623. X509_free(tmp);
  10624. ExpectNotNull(tmp = sk_X509_pop(ca));
  10625. X509_free(tmp);
  10626. ExpectNull(sk_X509_pop(ca));
  10627. EVP_PKEY_free(pkey);
  10628. pkey = NULL;
  10629. X509_free(cert);
  10630. cert = NULL;
  10631. sk_X509_pop_free(ca, X509_free);
  10632. ca = NULL;
  10633. /* check PKCS12_create */
  10634. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10635. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10636. SSL_SUCCESS);
  10637. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10638. -1, -1, 100, -1, 0)));
  10639. EVP_PKEY_free(pkey);
  10640. pkey = NULL;
  10641. X509_free(cert);
  10642. cert = NULL;
  10643. sk_X509_pop_free(ca, NULL);
  10644. ca = NULL;
  10645. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10646. SSL_SUCCESS);
  10647. PKCS12_free(pkcs12_2);
  10648. pkcs12_2 = NULL;
  10649. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10650. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10651. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10652. 2000, 1, 0)));
  10653. EVP_PKEY_free(pkey);
  10654. pkey = NULL;
  10655. X509_free(cert);
  10656. cert = NULL;
  10657. sk_X509_pop_free(ca, NULL);
  10658. ca = NULL;
  10659. /* convert to DER then back and parse */
  10660. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10661. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10662. PKCS12_free(pkcs12_2);
  10663. pkcs12_2 = NULL;
  10664. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10665. BIO_free(bio);
  10666. bio = NULL;
  10667. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10668. SSL_SUCCESS);
  10669. /* should be 2 other certs on stack */
  10670. ExpectNotNull(tmp = sk_X509_pop(ca));
  10671. X509_free(tmp);
  10672. ExpectNotNull(tmp = sk_X509_pop(ca));
  10673. X509_free(tmp);
  10674. ExpectNull(sk_X509_pop(ca));
  10675. #ifndef NO_RC4
  10676. PKCS12_free(pkcs12_2);
  10677. pkcs12_2 = NULL;
  10678. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10679. NID_pbe_WithSHA1And128BitRC4,
  10680. NID_pbe_WithSHA1And128BitRC4,
  10681. 2000, 1, 0)));
  10682. EVP_PKEY_free(pkey);
  10683. pkey = NULL;
  10684. X509_free(cert);
  10685. cert = NULL;
  10686. sk_X509_pop_free(ca, NULL);
  10687. ca = NULL;
  10688. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10689. SSL_SUCCESS);
  10690. #endif /* NO_RC4 */
  10691. EVP_PKEY_free(pkey);
  10692. pkey = NULL;
  10693. X509_free(cert);
  10694. cert = NULL;
  10695. PKCS12_free(pkcs12);
  10696. pkcs12 = NULL;
  10697. PKCS12_free(pkcs12_2);
  10698. pkcs12_2 = NULL;
  10699. sk_X509_pop_free(ca, NULL);
  10700. ca = NULL;
  10701. #ifdef HAVE_ECC
  10702. /* test order of parsing */
  10703. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10704. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10705. if (f != XBADFILE) {
  10706. XFCLOSE(f);
  10707. f = XBADFILE;
  10708. }
  10709. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10710. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10711. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10712. WOLFSSL_SUCCESS);
  10713. /* check use of pkey after parse */
  10714. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10715. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10716. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10717. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10718. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10719. #else
  10720. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10721. #endif
  10722. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10723. SSL_CTX_free(ctx);
  10724. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10725. #endif
  10726. ExpectNotNull(pkey);
  10727. ExpectNotNull(cert);
  10728. ExpectNotNull(ca);
  10729. /* compare subject lines of certificates */
  10730. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10731. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10732. SSL_FILETYPE_PEM));
  10733. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10734. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10735. X509_free(x509);
  10736. x509 = NULL;
  10737. /* test expected fail case */
  10738. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10739. SSL_FILETYPE_PEM));
  10740. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10741. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10742. X509_free(x509);
  10743. x509 = NULL;
  10744. X509_free(cert);
  10745. cert = NULL;
  10746. /* get subject line from ca stack */
  10747. ExpectNotNull(cert = sk_X509_pop(ca));
  10748. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10749. /* compare subject from certificate in ca to expected */
  10750. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10751. SSL_FILETYPE_PEM));
  10752. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10753. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10754. /* modify case and compare subject from certificate in ca to expected.
  10755. * The first bit of the name is:
  10756. * /C=US/ST=Washington
  10757. * So we'll change subject->name[1] to 'c' (lower case) */
  10758. if (subject != NULL) {
  10759. subject->name[1] = 'c';
  10760. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10761. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10762. }
  10763. EVP_PKEY_free(pkey);
  10764. pkey = NULL;
  10765. X509_free(x509);
  10766. x509 = NULL;
  10767. X509_free(cert);
  10768. cert = NULL;
  10769. BIO_free(bio);
  10770. bio = NULL;
  10771. PKCS12_free(pkcs12);
  10772. pkcs12 = NULL;
  10773. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10774. ca = NULL;
  10775. /* test order of parsing */
  10776. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10777. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10778. if (f != XBADFILE) {
  10779. XFCLOSE(f);
  10780. f = XBADFILE;
  10781. }
  10782. /* check verify MAC fail case */
  10783. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10784. ExpectNull(pkey);
  10785. ExpectNull(cert);
  10786. /* check parse with no extra certs kept */
  10787. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10788. 1);
  10789. ExpectNotNull(pkey);
  10790. ExpectNotNull(cert);
  10791. wolfSSL_EVP_PKEY_free(pkey);
  10792. pkey = NULL;
  10793. wolfSSL_X509_free(cert);
  10794. cert = NULL;
  10795. /* check parse with extra certs kept */
  10796. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10797. 1);
  10798. ExpectNotNull(pkey);
  10799. ExpectNotNull(cert);
  10800. ExpectNotNull(ca);
  10801. wolfSSL_EVP_PKEY_free(pkey);
  10802. pkey = NULL;
  10803. wolfSSL_X509_free(cert);
  10804. cert = NULL;
  10805. sk_X509_pop_free(ca, NULL);
  10806. ca = NULL;
  10807. PKCS12_free(pkcs12);
  10808. pkcs12 = NULL;
  10809. #endif /* HAVE_ECC */
  10810. #ifdef WC_RC2
  10811. /* test PKCS#12 with RC2 encryption */
  10812. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10813. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10814. if (f != XBADFILE) {
  10815. XFCLOSE(f);
  10816. f = XBADFILE;
  10817. }
  10818. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10819. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10820. /* check verify MAC fail case */
  10821. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10822. ExpectNull(pkey);
  10823. ExpectNull(cert);
  10824. /* check parse with not extra certs kept */
  10825. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10826. WOLFSSL_SUCCESS);
  10827. ExpectNotNull(pkey);
  10828. ExpectNotNull(cert);
  10829. wolfSSL_EVP_PKEY_free(pkey);
  10830. pkey = NULL;
  10831. wolfSSL_X509_free(cert);
  10832. cert = NULL;
  10833. /* check parse with extra certs kept */
  10834. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10835. WOLFSSL_SUCCESS);
  10836. ExpectNotNull(pkey);
  10837. ExpectNotNull(cert);
  10838. ExpectNotNull(ca);
  10839. wolfSSL_EVP_PKEY_free(pkey);
  10840. wolfSSL_X509_free(cert);
  10841. sk_X509_pop_free(ca, NULL);
  10842. BIO_free(bio);
  10843. bio = NULL;
  10844. PKCS12_free(pkcs12);
  10845. pkcs12 = NULL;
  10846. #endif /* WC_RC2 */
  10847. /* Test i2d_PKCS12_bio */
  10848. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10849. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10850. if (f != XBADFILE)
  10851. XFCLOSE(f);
  10852. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10853. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10854. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10855. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10856. PKCS12_free(pkcs12);
  10857. BIO_free(bio);
  10858. (void)order;
  10859. #endif /* OPENSSL_EXTRA */
  10860. #endif /* HAVE_FIPS */
  10861. return EXPECT_RESULT();
  10862. }
  10863. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10864. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10865. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10866. #define TEST_PKCS8_ENC
  10867. #endif
  10868. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10869. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10870. /* used to keep track if FailTestCallback was called */
  10871. static int failTestCallbackCalled = 0;
  10872. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10873. {
  10874. (void)passwd;
  10875. (void)sz;
  10876. (void)rw;
  10877. (void)userdata;
  10878. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10879. failTestCallbackCalled = 1;
  10880. return -1;
  10881. }
  10882. #endif
  10883. static int test_wolfSSL_no_password_cb(void)
  10884. {
  10885. EXPECT_DECLS;
  10886. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10887. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10888. WOLFSSL_CTX* ctx = NULL;
  10889. byte buff[FOURK_BUF];
  10890. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10891. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10892. XFILE f = XBADFILE;
  10893. int bytes = 0;
  10894. #ifndef NO_WOLFSSL_CLIENT
  10895. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10896. #else
  10897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10898. #endif
  10899. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10900. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10901. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10902. if (f != XBADFILE) {
  10903. XFCLOSE(f);
  10904. f = XBADFILE;
  10905. }
  10906. ExpectIntLE(bytes, sizeof(buff));
  10907. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10908. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10909. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10910. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10911. if (f != XBADFILE)
  10912. XFCLOSE(f);
  10913. ExpectIntLE(bytes, sizeof(buff));
  10914. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10915. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10916. wolfSSL_CTX_free(ctx);
  10917. /* Password callback should not be called by default */
  10918. ExpectIntEQ(failTestCallbackCalled, 0);
  10919. #endif
  10920. return EXPECT_RESULT();
  10921. }
  10922. #ifdef TEST_PKCS8_ENC
  10923. /* for PKCS8 test case */
  10924. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10925. {
  10926. int flag = 0;
  10927. (void)rw;
  10928. if (userdata != NULL) {
  10929. flag = *((int*)userdata); /* user set data */
  10930. }
  10931. switch (flag) {
  10932. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10933. * can be anything the user wishes to be passed to the callback
  10934. * associated with the WOLFSSL_CTX */
  10935. XSTRNCPY(passwd, "yassl123", sz);
  10936. return 8;
  10937. default:
  10938. return BAD_FUNC_ARG;
  10939. }
  10940. }
  10941. #endif /* TEST_PKCS8_ENC */
  10942. /* Testing functions dealing with PKCS8 */
  10943. static int test_wolfSSL_PKCS8(void)
  10944. {
  10945. EXPECT_DECLS;
  10946. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10947. !defined(WOLFCRYPT_ONLY)
  10948. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10949. byte buff[FOURK_BUF];
  10950. byte der[FOURK_BUF];
  10951. #ifndef NO_RSA
  10952. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10953. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10954. #endif
  10955. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10956. #ifdef HAVE_ECC
  10957. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10958. #endif
  10959. XFILE f = XBADFILE;
  10960. int bytes = 0;
  10961. WOLFSSL_CTX* ctx = NULL;
  10962. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  10963. int ret;
  10964. ecc_key key;
  10965. word32 x = 0;
  10966. #endif
  10967. #ifdef TEST_PKCS8_ENC
  10968. #if !defined(NO_RSA) && !defined(NO_SHA)
  10969. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10970. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10971. #endif
  10972. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10973. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10974. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10975. #endif
  10976. int flag;
  10977. #endif
  10978. (void)der;
  10979. #ifndef NO_WOLFSSL_CLIENT
  10980. #ifndef WOLFSSL_NO_TLS12
  10981. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10982. #else
  10983. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10984. #endif
  10985. #else
  10986. #ifndef WOLFSSL_NO_TLS12
  10987. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10988. #else
  10989. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10990. #endif
  10991. #endif
  10992. #ifdef TEST_PKCS8_ENC
  10993. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10994. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10995. flag = 1; /* used by password callback as return code */
  10996. #if !defined(NO_RSA) && !defined(NO_SHA)
  10997. /* test loading PEM PKCS8 encrypted file */
  10998. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10999. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11000. if (f != XBADFILE) {
  11001. XFCLOSE(f);
  11002. f = XBADFILE;
  11003. }
  11004. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11005. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11006. /* this next case should fail because of password callback return code */
  11007. flag = 0; /* used by password callback as return code */
  11008. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11009. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11010. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11011. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11012. "yassl123"), 0);
  11013. /* test that error value is returned with a bad password */
  11014. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11015. "bad"), 0);
  11016. /* test loading PEM PKCS8 encrypted file */
  11017. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  11018. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11019. if (f != XBADFILE) {
  11020. XFCLOSE(f);
  11021. f = XBADFILE;
  11022. }
  11023. flag = 1; /* used by password callback as return code */
  11024. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11025. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11026. /* this next case should fail because of password callback return code */
  11027. flag = 0; /* used by password callback as return code */
  11028. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11029. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11030. #endif /* !NO_RSA && !NO_SHA */
  11031. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11032. /* test loading PEM PKCS8 encrypted ECC Key file */
  11033. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  11034. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11035. if (f != XBADFILE) {
  11036. XFCLOSE(f);
  11037. f = XBADFILE;
  11038. }
  11039. flag = 1; /* used by password callback as return code */
  11040. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11041. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11042. /* this next case should fail because of password callback return code */
  11043. flag = 0; /* used by password callback as return code */
  11044. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11045. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11046. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11047. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11048. "yassl123"), 0);
  11049. /* test that error value is returned with a bad password */
  11050. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11051. "bad"), 0);
  11052. /* test loading DER PKCS8 encrypted ECC Key file */
  11053. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  11054. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11055. if (f != XBADFILE) {
  11056. XFCLOSE(f);
  11057. f = XBADFILE;
  11058. }
  11059. flag = 1; /* used by password callback as return code */
  11060. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11061. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11062. /* this next case should fail because of password callback return code */
  11063. flag = 0; /* used by password callback as return code */
  11064. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11065. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11066. /* leave flag as "okay" */
  11067. flag = 1;
  11068. #endif /* HAVE_ECC && !NO_SHA */
  11069. #endif /* TEST_PKCS8_ENC */
  11070. #ifndef NO_RSA
  11071. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  11072. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  11073. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11074. if (f != XBADFILE) {
  11075. XFCLOSE(f);
  11076. f = XBADFILE;
  11077. }
  11078. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11079. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11080. /* test loading PEM PKCS8 private key file (not encrypted) */
  11081. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  11082. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11083. if (f != XBADFILE) {
  11084. XFCLOSE(f);
  11085. f = XBADFILE;
  11086. }
  11087. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11088. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11089. #endif /* !NO_RSA */
  11090. /* Test PKCS8 PEM ECC key no crypt */
  11091. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11092. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11093. if (f != XBADFILE) {
  11094. XFCLOSE(f);
  11095. f = XBADFILE;
  11096. }
  11097. #ifdef HAVE_ECC
  11098. /* Test PKCS8 PEM ECC key no crypt */
  11099. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11100. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11101. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11102. /* decrypt PKCS8 PEM to key in DER format */
  11103. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11104. (word32)sizeof(der), NULL)), 0);
  11105. ret = wc_ecc_init(&key);
  11106. if (ret == 0) {
  11107. ret = wc_EccPrivateKeyDecode(der, &x, &key, (word32)bytes);
  11108. wc_ecc_free(&key);
  11109. }
  11110. ExpectIntEQ(ret, 0);
  11111. #endif
  11112. /* Test PKCS8 DER ECC key no crypt */
  11113. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11114. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11115. if (f != XBADFILE)
  11116. XFCLOSE(f);
  11117. /* Test using a PKCS8 ECC PEM */
  11118. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11119. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11120. #else
  11121. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11122. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11123. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11124. #endif /* HAVE_ECC */
  11125. wolfSSL_CTX_free(ctx);
  11126. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11127. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11128. return EXPECT_RESULT();
  11129. }
  11130. static int test_wolfSSL_PKCS8_ED25519(void)
  11131. {
  11132. EXPECT_DECLS;
  11133. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11134. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11135. defined(HAVE_ED25519_KEY_IMPORT)
  11136. const byte encPrivKey[] = \
  11137. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11138. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11139. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11140. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11141. "2L9W4v7swXkV+X+a1ww=\n"
  11142. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11143. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11144. byte der[FOURK_BUF];
  11145. WOLFSSL_CTX* ctx = NULL;
  11146. int bytes;
  11147. XMEMSET(der, 0, sizeof(der));
  11148. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11149. (word32)sizeof(der), password)), 0);
  11150. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11151. #ifndef NO_WOLFSSL_SERVER
  11152. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11153. #else
  11154. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11155. #endif
  11156. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11157. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11158. wolfSSL_CTX_free(ctx);
  11159. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11160. #endif
  11161. return EXPECT_RESULT();
  11162. }
  11163. static int test_wolfSSL_PKCS8_ED448(void)
  11164. {
  11165. EXPECT_DECLS;
  11166. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11167. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11168. defined(HAVE_ED448_KEY_IMPORT)
  11169. const byte encPrivKey[] = \
  11170. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11171. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11172. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11173. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11174. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11175. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11176. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11177. byte der[FOURK_BUF];
  11178. WOLFSSL_CTX* ctx = NULL;
  11179. int bytes;
  11180. XMEMSET(der, 0, sizeof(der));
  11181. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11182. (word32)sizeof(der), password)), 0);
  11183. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11184. #ifndef NO_WOLFSSL_SERVER
  11185. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11186. #else
  11187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11188. #endif
  11189. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11190. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11191. wolfSSL_CTX_free(ctx);
  11192. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11193. #endif
  11194. return EXPECT_RESULT();
  11195. }
  11196. /* Testing functions dealing with PKCS5 */
  11197. static int test_wolfSSL_PKCS5(void)
  11198. {
  11199. EXPECT_DECLS;
  11200. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11201. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11202. const char* passwd = "myfipsPa$$W0rd";
  11203. #else
  11204. const char *passwd = "pass1234";
  11205. #endif
  11206. const unsigned char *salt = (unsigned char *)"salt1234";
  11207. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11208. DYNAMIC_TYPE_TMP_BUFFER);
  11209. int ret = 0;
  11210. ExpectNotNull(out);
  11211. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11212. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11213. WOLFSSL_SUCCESS);
  11214. #ifdef WOLFSSL_SHA512
  11215. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11216. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11217. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11218. #endif
  11219. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11220. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11221. return EXPECT_RESULT();
  11222. }
  11223. /* test parsing URI from certificate */
  11224. static int test_wolfSSL_URI(void)
  11225. {
  11226. EXPECT_DECLS;
  11227. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11228. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11229. defined(OPENSSL_EXTRA))
  11230. WOLFSSL_X509* x509 = NULL;
  11231. const char uri[] = "./certs/client-uri-cert.pem";
  11232. const char urn[] = "./certs/client-absolute-urn.pem";
  11233. const char badUri[] = "./certs/client-relative-uri.pem";
  11234. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11235. WOLFSSL_FILETYPE_PEM));
  11236. wolfSSL_FreeX509(x509);
  11237. x509 = NULL;
  11238. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11239. WOLFSSL_FILETYPE_PEM));
  11240. wolfSSL_FreeX509(x509);
  11241. x509 = NULL;
  11242. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11243. && !defined(WOLFSSL_FPKI)
  11244. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11245. WOLFSSL_FILETYPE_PEM));
  11246. #else
  11247. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11248. WOLFSSL_FILETYPE_PEM));
  11249. #endif
  11250. wolfSSL_FreeX509(x509);
  11251. #endif
  11252. return EXPECT_RESULT();
  11253. }
  11254. static int test_wolfSSL_TBS(void)
  11255. {
  11256. EXPECT_DECLS;
  11257. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11258. && defined(OPENSSL_EXTRA)
  11259. WOLFSSL_X509* x509 = NULL;
  11260. const unsigned char* tbs;
  11261. int tbsSz;
  11262. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11263. WOLFSSL_FILETYPE_PEM));
  11264. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11265. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11266. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11267. ExpectIntEQ(tbsSz, 1003);
  11268. wolfSSL_FreeX509(x509);
  11269. #endif
  11270. return EXPECT_RESULT();
  11271. }
  11272. static int test_wolfSSL_X509_verify(void)
  11273. {
  11274. EXPECT_DECLS;
  11275. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11276. defined(OPENSSL_EXTRA)
  11277. WOLFSSL_X509* ca = NULL;
  11278. WOLFSSL_X509* serv = NULL;
  11279. WOLFSSL_EVP_PKEY* pkey = NULL;
  11280. unsigned char buf[2048];
  11281. const unsigned char* pt = NULL;
  11282. int bufSz;
  11283. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11284. WOLFSSL_FILETYPE_PEM));
  11285. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11286. WOLFSSL_SUCCESS);
  11287. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11288. WOLFSSL_SUCCESS);
  11289. ExpectIntEQ(bufSz, 294);
  11290. bufSz = 2048;
  11291. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11292. WOLFSSL_SUCCESS);
  11293. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11294. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11295. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11296. WOLFSSL_FILETYPE_PEM));
  11297. /* success case */
  11298. pt = buf;
  11299. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11300. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11301. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11302. wolfSSL_EVP_PKEY_free(pkey);
  11303. pkey = NULL;
  11304. /* fail case */
  11305. bufSz = 2048;
  11306. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11307. WOLFSSL_SUCCESS);
  11308. pt = buf;
  11309. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11310. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11311. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11312. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11313. wolfSSL_EVP_PKEY_free(pkey);
  11314. wolfSSL_FreeX509(ca);
  11315. wolfSSL_FreeX509(serv);
  11316. #endif
  11317. return EXPECT_RESULT();
  11318. }
  11319. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11320. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11321. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11322. /* create certificate with version 2 */
  11323. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11324. {
  11325. EXPECT_DECLS;
  11326. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11327. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11328. unsigned char *der = NULL, *key = NULL, *pt;
  11329. char *header = NULL, *name = NULL;
  11330. int derSz;
  11331. long keySz;
  11332. XFILE fp = XBADFILE;
  11333. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11334. time_t t;
  11335. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11336. WOLFSSL_FILETYPE_PEM));
  11337. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11338. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11339. WOLFSSL_SUCCESS);
  11340. if (fp != XBADFILE)
  11341. XFCLOSE(fp);
  11342. pt = key;
  11343. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11344. (const unsigned char**)&pt, keySz));
  11345. /* create the version 2 certificate */
  11346. ExpectNotNull(x509v2 = X509_new());
  11347. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11348. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11349. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11350. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11351. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11352. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11353. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11354. t = time(NULL);
  11355. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11356. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11357. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11358. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11359. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11360. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11361. ExpectIntGT(derSz, 0);
  11362. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11363. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11364. /* TODO: Replace with API call */
  11365. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11366. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11367. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11368. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11369. wolfSSL_X509_free(x509);
  11370. wolfSSL_X509_free(x509v2);
  11371. wolfSSL_EVP_PKEY_free(priv);
  11372. wolfSSL_EVP_PKEY_free(pub);
  11373. wolfSSL_ASN1_TIME_free(notBefore);
  11374. wolfSSL_ASN1_TIME_free(notAfter);
  11375. return EXPECT_RESULT();
  11376. }
  11377. /* override certificate version error */
  11378. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11379. {
  11380. EXPECT_DECLS;
  11381. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11382. ExpectIntEQ(store->error, ASN_VERSION_E);
  11383. #else
  11384. ExpectIntEQ(store->error, 0);
  11385. #endif
  11386. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11387. (void)preverify;
  11388. return EXPECT_RESULT() == TEST_SUCCESS;
  11389. }
  11390. /* set verify callback that will override bad certificate version */
  11391. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11392. {
  11393. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11394. return TEST_SUCCESS;
  11395. }
  11396. #endif
  11397. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11398. {
  11399. EXPECT_DECLS;
  11400. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11401. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11402. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11403. test_ssl_cbf func_cb_client;
  11404. test_ssl_cbf func_cb_server;
  11405. /* test server rejects a client certificate that is not version 3 */
  11406. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11407. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11408. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11409. #ifndef WOLFSSL_NO_TLS12
  11410. func_cb_client.method = wolfTLSv1_2_client_method;
  11411. #else
  11412. func_cb_client.method = wolfTLSv1_3_client_method;
  11413. #endif
  11414. #ifndef WOLFSSL_NO_TLS12
  11415. func_cb_server.method = wolfTLSv1_2_server_method;
  11416. #else
  11417. func_cb_server.method = wolfTLSv1_3_server_method;
  11418. #endif
  11419. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11420. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11421. &func_cb_server, NULL), TEST_FAIL);
  11422. #else
  11423. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11424. &func_cb_server, NULL), TEST_SUCCESS);
  11425. #endif
  11426. #endif
  11427. return EXPECT_RESULT();
  11428. }
  11429. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11430. {
  11431. EXPECT_DECLS;
  11432. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11433. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11434. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11435. test_ssl_cbf func_cb_client;
  11436. test_ssl_cbf func_cb_server;
  11437. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11438. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11439. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11440. func_cb_server.ctx_ready = &test_set_override_x509;
  11441. #ifndef WOLFSSL_NO_TLS12
  11442. func_cb_client.method = wolfTLSv1_2_client_method;
  11443. #else
  11444. func_cb_client.method = wolfTLSv1_3_client_method;
  11445. #endif
  11446. #ifndef WOLFSSL_NO_TLS12
  11447. func_cb_server.method = wolfTLSv1_2_server_method;
  11448. #else
  11449. func_cb_server.method = wolfTLSv1_3_server_method;
  11450. #endif
  11451. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11452. &func_cb_server, NULL), TEST_SUCCESS);
  11453. #endif
  11454. return EXPECT_RESULT();
  11455. }
  11456. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11457. * version allowed.
  11458. * POST: 1 on success.
  11459. */
  11460. static int test_wolfSSL_CTX_SetMinVersion(void)
  11461. {
  11462. int res = TEST_SKIPPED;
  11463. #ifndef NO_WOLFSSL_CLIENT
  11464. int failFlag = WOLFSSL_SUCCESS;
  11465. WOLFSSL_CTX* ctx;
  11466. int itr;
  11467. #ifndef NO_OLD_TLS
  11468. const int versions[] = {
  11469. #ifdef WOLFSSL_ALLOW_TLSV10
  11470. WOLFSSL_TLSV1,
  11471. #endif
  11472. WOLFSSL_TLSV1_1,
  11473. WOLFSSL_TLSV1_2 };
  11474. #elif !defined(WOLFSSL_NO_TLS12)
  11475. const int versions[] = { WOLFSSL_TLSV1_2 };
  11476. #elif defined(WOLFSSL_TLS13)
  11477. const int versions[] = { WOLFSSL_TLSV1_3 };
  11478. #else
  11479. const int versions[0];
  11480. #endif
  11481. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11482. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11483. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11484. != WOLFSSL_SUCCESS) {
  11485. failFlag = WOLFSSL_FAILURE;
  11486. }
  11487. }
  11488. wolfSSL_CTX_free(ctx);
  11489. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11490. #endif
  11491. return res;
  11492. } /* END test_wolfSSL_CTX_SetMinVersion */
  11493. /*----------------------------------------------------------------------------*
  11494. | OCSP Stapling
  11495. *----------------------------------------------------------------------------*/
  11496. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11497. * need to contact the CA, lowering the cost of cert revocation checking.
  11498. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11499. * POST: 1 returned for success.
  11500. */
  11501. static int test_wolfSSL_UseOCSPStapling(void)
  11502. {
  11503. EXPECT_DECLS;
  11504. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11505. !defined(NO_WOLFSSL_CLIENT)
  11506. WOLFSSL_CTX* ctx = NULL;
  11507. WOLFSSL* ssl = NULL;
  11508. #ifndef NO_WOLFSSL_CLIENT
  11509. #ifndef WOLFSSL_NO_TLS12
  11510. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11511. #else
  11512. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11513. #endif
  11514. #else
  11515. #ifndef WOLFSSL_NO_TLS12
  11516. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11517. #else
  11518. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11519. #endif
  11520. #endif
  11521. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11522. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11523. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11524. #ifndef NO_WOLFSSL_CLIENT
  11525. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11526. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11527. #else
  11528. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11529. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11530. #endif
  11531. wolfSSL_free(ssl);
  11532. wolfSSL_CTX_free(ctx);
  11533. #endif
  11534. return EXPECT_RESULT();
  11535. } /* END test_wolfSSL_UseOCSPStapling */
  11536. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11537. * stapling eliminates the need to contact the CA and lowers cert revocation
  11538. * check.
  11539. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11540. */
  11541. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11542. {
  11543. EXPECT_DECLS;
  11544. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11545. !defined(NO_WOLFSSL_CLIENT)
  11546. WOLFSSL_CTX* ctx = NULL;
  11547. WOLFSSL* ssl = NULL;
  11548. #ifndef NO_WOLFSSL_CLIENT
  11549. #ifndef WOLFSSL_NO_TLS12
  11550. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11551. #else
  11552. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11553. #endif
  11554. #else
  11555. #ifndef WOLFSSL_NO_TLS12
  11556. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11557. #else
  11558. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11559. #endif
  11560. #endif
  11561. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11562. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11563. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11564. #ifndef NO_WOLFSSL_CLIENT
  11565. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11566. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11567. #else
  11568. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11569. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11570. #endif
  11571. wolfSSL_free(ssl);
  11572. wolfSSL_CTX_free(ctx);
  11573. #endif
  11574. return EXPECT_RESULT();
  11575. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11576. /*----------------------------------------------------------------------------*
  11577. | Multicast Tests
  11578. *----------------------------------------------------------------------------*/
  11579. static int test_wolfSSL_mcast(void)
  11580. {
  11581. EXPECT_DECLS;
  11582. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11583. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11584. WOLFSSL_CTX* ctx = NULL;
  11585. WOLFSSL* ssl = NULL;
  11586. byte preMasterSecret[512];
  11587. byte clientRandom[32];
  11588. byte serverRandom[32];
  11589. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11590. byte buf[256];
  11591. word16 newId;
  11592. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11593. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11594. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11595. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11596. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11597. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11598. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11599. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11600. WOLFSSL_SUCCESS);
  11601. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11602. ExpectIntLE(newId, 100);
  11603. wolfSSL_free(ssl);
  11604. wolfSSL_CTX_free(ctx);
  11605. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11606. * WOLFSSL_SNIFFER) */
  11607. return EXPECT_RESULT();
  11608. }
  11609. /*----------------------------------------------------------------------------*
  11610. | Wolfcrypt
  11611. *----------------------------------------------------------------------------*/
  11612. /*
  11613. * Unit test for the wc_InitBlake2b()
  11614. */
  11615. static int test_wc_InitBlake2b(void)
  11616. {
  11617. EXPECT_DECLS;
  11618. #ifdef HAVE_BLAKE2
  11619. Blake2b blake;
  11620. /* Test good arg. */
  11621. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11622. /* Test bad arg. */
  11623. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11624. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11625. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11626. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11627. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11628. #endif
  11629. return EXPECT_RESULT();
  11630. } /* END test_wc_InitBlake2b*/
  11631. /*
  11632. * Unit test for the wc_InitBlake2b_WithKey()
  11633. */
  11634. static int test_wc_InitBlake2b_WithKey(void)
  11635. {
  11636. EXPECT_DECLS;
  11637. #ifdef HAVE_BLAKE2
  11638. Blake2b blake;
  11639. word32 digestSz = BLAKE2B_KEYBYTES;
  11640. byte key[BLAKE2B_KEYBYTES];
  11641. word32 keylen = BLAKE2B_KEYBYTES;
  11642. XMEMSET(key, 0, sizeof(key));
  11643. /* Test good arg. */
  11644. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11645. /* Test bad args. */
  11646. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11647. BAD_FUNC_ARG);
  11648. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11649. BAD_FUNC_ARG);
  11650. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11651. #endif
  11652. return EXPECT_RESULT();
  11653. } /* END wc_InitBlake2b_WithKey*/
  11654. /*
  11655. * Unit test for the wc_InitBlake2s_WithKey()
  11656. */
  11657. static int test_wc_InitBlake2s_WithKey(void)
  11658. {
  11659. EXPECT_DECLS;
  11660. #ifdef HAVE_BLAKE2S
  11661. Blake2s blake;
  11662. word32 digestSz = BLAKE2S_KEYBYTES;
  11663. byte *key = (byte*)"01234567890123456789012345678901";
  11664. word32 keylen = BLAKE2S_KEYBYTES;
  11665. /* Test good arg. */
  11666. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11667. /* Test bad args. */
  11668. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11669. BAD_FUNC_ARG);
  11670. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11671. BAD_FUNC_ARG);
  11672. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11673. #endif
  11674. return EXPECT_RESULT();
  11675. } /* END wc_InitBlake2s_WithKey*/
  11676. /*
  11677. * Unit test for the wc_InitMd5()
  11678. */
  11679. static int test_wc_InitMd5(void)
  11680. {
  11681. EXPECT_DECLS;
  11682. #ifndef NO_MD5
  11683. wc_Md5 md5;
  11684. /* Test good arg. */
  11685. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11686. /* Test bad arg. */
  11687. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11688. wc_Md5Free(&md5);
  11689. #endif
  11690. return EXPECT_RESULT();
  11691. } /* END test_wc_InitMd5 */
  11692. /*
  11693. * Testing wc_UpdateMd5()
  11694. */
  11695. static int test_wc_Md5Update(void)
  11696. {
  11697. EXPECT_DECLS;
  11698. #ifndef NO_MD5
  11699. wc_Md5 md5;
  11700. byte hash[WC_MD5_DIGEST_SIZE];
  11701. testVector a, b, c;
  11702. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11703. /* Input */
  11704. a.input = "a";
  11705. a.inLen = XSTRLEN(a.input);
  11706. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11707. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11708. /* Update input. */
  11709. a.input = "abc";
  11710. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11711. "\x72";
  11712. a.inLen = XSTRLEN(a.input);
  11713. a.outLen = XSTRLEN(a.output);
  11714. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11715. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11716. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11717. /* Pass in bad values. */
  11718. b.input = NULL;
  11719. b.inLen = 0;
  11720. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11721. c.input = NULL;
  11722. c.inLen = WC_MD5_DIGEST_SIZE;
  11723. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11724. BAD_FUNC_ARG);
  11725. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11726. BAD_FUNC_ARG);
  11727. wc_Md5Free(&md5);
  11728. #endif
  11729. return EXPECT_RESULT();
  11730. } /* END test_wc_Md5Update() */
  11731. /*
  11732. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11733. */
  11734. static int test_wc_Md5Final(void)
  11735. {
  11736. EXPECT_DECLS;
  11737. #ifndef NO_MD5
  11738. /* Instantiate */
  11739. wc_Md5 md5;
  11740. byte* hash_test[3];
  11741. byte hash1[WC_MD5_DIGEST_SIZE];
  11742. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11743. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11744. int times, i;
  11745. /* Initialize */
  11746. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11747. hash_test[0] = hash1;
  11748. hash_test[1] = hash2;
  11749. hash_test[2] = hash3;
  11750. times = sizeof(hash_test)/sizeof(byte*);
  11751. for (i = 0; i < times; i++) {
  11752. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11753. }
  11754. /* Test bad args. */
  11755. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11756. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11757. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11758. wc_Md5Free(&md5);
  11759. #endif
  11760. return EXPECT_RESULT();
  11761. }
  11762. /*
  11763. * Unit test for the wc_InitSha()
  11764. */
  11765. static int test_wc_InitSha(void)
  11766. {
  11767. EXPECT_DECLS;
  11768. #ifndef NO_SHA
  11769. wc_Sha sha;
  11770. /* Test good arg. */
  11771. ExpectIntEQ(wc_InitSha(&sha), 0);
  11772. /* Test bad arg. */
  11773. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11774. wc_ShaFree(&sha);
  11775. #endif
  11776. return EXPECT_RESULT();
  11777. } /* END test_wc_InitSha */
  11778. /*
  11779. * Tesing wc_ShaUpdate()
  11780. */
  11781. static int test_wc_ShaUpdate(void)
  11782. {
  11783. EXPECT_DECLS;
  11784. #ifndef NO_SHA
  11785. wc_Sha sha;
  11786. byte hash[WC_SHA_DIGEST_SIZE];
  11787. testVector a, b, c;
  11788. ExpectIntEQ(wc_InitSha(&sha), 0);
  11789. /* Input. */
  11790. a.input = "a";
  11791. a.inLen = XSTRLEN(a.input);
  11792. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11793. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11794. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11795. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11796. /* Update input. */
  11797. a.input = "abc";
  11798. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11799. "\x6C\x9C\xD0\xD8\x9D";
  11800. a.inLen = XSTRLEN(a.input);
  11801. a.outLen = XSTRLEN(a.output);
  11802. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11803. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11804. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11805. /* Try passing in bad values. */
  11806. b.input = NULL;
  11807. b.inLen = 0;
  11808. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11809. c.input = NULL;
  11810. c.inLen = WC_SHA_DIGEST_SIZE;
  11811. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11812. BAD_FUNC_ARG);
  11813. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11814. BAD_FUNC_ARG);
  11815. wc_ShaFree(&sha);
  11816. #endif
  11817. return EXPECT_RESULT();
  11818. } /* END test_wc_ShaUpdate() */
  11819. /*
  11820. * Unit test on wc_ShaFinal
  11821. */
  11822. static int test_wc_ShaFinal(void)
  11823. {
  11824. EXPECT_DECLS;
  11825. #ifndef NO_SHA
  11826. wc_Sha sha;
  11827. byte* hash_test[3];
  11828. byte hash1[WC_SHA_DIGEST_SIZE];
  11829. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11830. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11831. int times, i;
  11832. /* Initialize*/
  11833. ExpectIntEQ(wc_InitSha(&sha), 0);
  11834. hash_test[0] = hash1;
  11835. hash_test[1] = hash2;
  11836. hash_test[2] = hash3;
  11837. times = sizeof(hash_test)/sizeof(byte*);
  11838. for (i = 0; i < times; i++) {
  11839. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11840. }
  11841. /* Test bad args. */
  11842. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11843. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11844. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11845. wc_ShaFree(&sha);
  11846. #endif
  11847. return EXPECT_RESULT();
  11848. } /* END test_wc_ShaFinal */
  11849. /*
  11850. * Unit test for wc_InitSha256()
  11851. */
  11852. static int test_wc_InitSha256(void)
  11853. {
  11854. EXPECT_DECLS;
  11855. #ifndef NO_SHA256
  11856. wc_Sha256 sha256;
  11857. /* Test good arg. */
  11858. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11859. /* Test bad arg. */
  11860. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11861. wc_Sha256Free(&sha256);
  11862. #endif
  11863. return EXPECT_RESULT();
  11864. } /* END test_wc_InitSha256 */
  11865. /*
  11866. * Unit test for wc_Sha256Update()
  11867. */
  11868. static int test_wc_Sha256Update(void)
  11869. {
  11870. EXPECT_DECLS;
  11871. #ifndef NO_SHA256
  11872. wc_Sha256 sha256;
  11873. byte hash[WC_SHA256_DIGEST_SIZE];
  11874. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11875. testVector a, b, c;
  11876. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11877. /* Input. */
  11878. a.input = "a";
  11879. a.inLen = XSTRLEN(a.input);
  11880. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11881. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11882. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11883. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11884. /* Update input. */
  11885. a.input = "abc";
  11886. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11887. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11888. "\x15\xAD";
  11889. a.inLen = XSTRLEN(a.input);
  11890. a.outLen = XSTRLEN(a.output);
  11891. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11892. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11893. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11894. /* Unaligned check. */
  11895. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11896. 0);
  11897. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11898. /* Try passing in bad values */
  11899. b.input = NULL;
  11900. b.inLen = 0;
  11901. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11902. c.input = NULL;
  11903. c.inLen = WC_SHA256_DIGEST_SIZE;
  11904. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11905. BAD_FUNC_ARG);
  11906. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11907. BAD_FUNC_ARG);
  11908. wc_Sha256Free(&sha256);
  11909. #endif
  11910. return EXPECT_RESULT();
  11911. } /* END test_wc_Sha256Update */
  11912. /*
  11913. * Unit test function for wc_Sha256Final()
  11914. */
  11915. static int test_wc_Sha256Final(void)
  11916. {
  11917. EXPECT_DECLS;
  11918. #ifndef NO_SHA256
  11919. wc_Sha256 sha256;
  11920. byte* hash_test[3];
  11921. byte hash1[WC_SHA256_DIGEST_SIZE];
  11922. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11923. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11924. int times, i;
  11925. /* Initialize */
  11926. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11927. hash_test[0] = hash1;
  11928. hash_test[1] = hash2;
  11929. hash_test[2] = hash3;
  11930. times = sizeof(hash_test) / sizeof(byte*);
  11931. for (i = 0; i < times; i++) {
  11932. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11933. }
  11934. /* Test bad args. */
  11935. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11936. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11937. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11938. wc_Sha256Free(&sha256);
  11939. #endif
  11940. return EXPECT_RESULT();
  11941. } /* END test_wc_Sha256Final */
  11942. /*
  11943. * Unit test function for wc_Sha256FinalRaw()
  11944. */
  11945. static int test_wc_Sha256FinalRaw(void)
  11946. {
  11947. EXPECT_DECLS;
  11948. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11949. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11950. !defined(WOLFSSL_NO_HASH_RAW)
  11951. wc_Sha256 sha256;
  11952. byte* hash_test[3];
  11953. byte hash1[WC_SHA256_DIGEST_SIZE];
  11954. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11955. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11956. int times, i;
  11957. /* Initialize */
  11958. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11959. hash_test[0] = hash1;
  11960. hash_test[1] = hash2;
  11961. hash_test[2] = hash3;
  11962. times = sizeof(hash_test) / sizeof(byte*);
  11963. for (i = 0; i < times; i++) {
  11964. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11965. }
  11966. /* Test bad args. */
  11967. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11968. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11969. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11970. wc_Sha256Free(&sha256);
  11971. #endif
  11972. return EXPECT_RESULT();
  11973. } /* END test_wc_Sha256FinalRaw */
  11974. /*
  11975. * Unit test function for wc_Sha256GetFlags()
  11976. */
  11977. static int test_wc_Sha256GetFlags(void)
  11978. {
  11979. EXPECT_DECLS;
  11980. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11981. wc_Sha256 sha256;
  11982. word32 flags = 0;
  11983. /* Initialize */
  11984. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11985. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11986. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11987. wc_Sha256Free(&sha256);
  11988. #endif
  11989. return EXPECT_RESULT();
  11990. } /* END test_wc_Sha256GetFlags */
  11991. /*
  11992. * Unit test function for wc_Sha256Free()
  11993. */
  11994. static int test_wc_Sha256Free(void)
  11995. {
  11996. EXPECT_DECLS;
  11997. #ifndef NO_SHA256
  11998. wc_Sha256Free(NULL);
  11999. /* Set result to SUCCESS. */
  12000. ExpectTrue(1);
  12001. #endif
  12002. return EXPECT_RESULT();
  12003. } /* END test_wc_Sha256Free */
  12004. /*
  12005. * Unit test function for wc_Sha256GetHash()
  12006. */
  12007. static int test_wc_Sha256GetHash(void)
  12008. {
  12009. EXPECT_DECLS;
  12010. #ifndef NO_SHA256
  12011. wc_Sha256 sha256;
  12012. byte hash1[WC_SHA256_DIGEST_SIZE];
  12013. /* Initialize */
  12014. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12015. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  12016. /* test bad arguments*/
  12017. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  12018. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  12019. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  12020. wc_Sha256Free(&sha256);
  12021. #endif
  12022. return EXPECT_RESULT();
  12023. } /* END test_wc_Sha256GetHash */
  12024. /*
  12025. * Unit test function for wc_Sha256Copy()
  12026. */
  12027. static int test_wc_Sha256Copy(void)
  12028. {
  12029. EXPECT_DECLS;
  12030. #ifndef NO_SHA256
  12031. wc_Sha256 sha256;
  12032. wc_Sha256 temp;
  12033. XMEMSET(&sha256, 0, sizeof(sha256));
  12034. XMEMSET(&temp, 0, sizeof(temp));
  12035. /* Initialize */
  12036. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12037. ExpectIntEQ(wc_InitSha256(&temp), 0);
  12038. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  12039. /* test bad arguments*/
  12040. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  12041. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  12042. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  12043. wc_Sha256Free(&sha256);
  12044. wc_Sha256Free(&temp);
  12045. #endif
  12046. return EXPECT_RESULT();
  12047. } /* END test_wc_Sha256Copy */
  12048. /*
  12049. * Testing wc_InitSha512()
  12050. */
  12051. static int test_wc_InitSha512(void)
  12052. {
  12053. EXPECT_DECLS;
  12054. #ifdef WOLFSSL_SHA512
  12055. wc_Sha512 sha512;
  12056. /* Test good arg. */
  12057. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12058. /* Test bad arg. */
  12059. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  12060. wc_Sha512Free(&sha512);
  12061. #endif
  12062. return EXPECT_RESULT();
  12063. } /* END test_wc_InitSha512 */
  12064. /*
  12065. * wc_Sha512Update() test.
  12066. */
  12067. static int test_wc_Sha512Update(void)
  12068. {
  12069. EXPECT_DECLS;
  12070. #ifdef WOLFSSL_SHA512
  12071. wc_Sha512 sha512;
  12072. byte hash[WC_SHA512_DIGEST_SIZE];
  12073. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  12074. testVector a, b, c;
  12075. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12076. /* Input. */
  12077. a.input = "a";
  12078. a.inLen = XSTRLEN(a.input);
  12079. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  12080. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  12081. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  12082. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12083. /* Update input. */
  12084. a.input = "abc";
  12085. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  12086. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  12087. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  12088. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  12089. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  12090. a.inLen = XSTRLEN(a.input);
  12091. a.outLen = XSTRLEN(a.output);
  12092. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  12093. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12094. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  12095. /* Unaligned check. */
  12096. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  12097. 0);
  12098. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  12099. /* Try passing in bad values */
  12100. b.input = NULL;
  12101. b.inLen = 0;
  12102. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  12103. c.input = NULL;
  12104. c.inLen = WC_SHA512_DIGEST_SIZE;
  12105. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12106. BAD_FUNC_ARG);
  12107. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12108. BAD_FUNC_ARG);
  12109. wc_Sha512Free(&sha512);
  12110. #endif
  12111. return EXPECT_RESULT();
  12112. } /* END test_wc_Sha512Update */
  12113. #ifdef WOLFSSL_SHA512
  12114. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12115. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12116. /* Performs test for
  12117. * - wc_Sha512Final/wc_Sha512FinalRaw
  12118. * - wc_Sha512_224Final/wc_Sha512_224Final
  12119. * - wc_Sha512_256Final/wc_Sha512_256Final
  12120. * parameter:
  12121. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12122. * WC_HASH_TYPE_SHA512_256
  12123. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12124. *return 0 on success
  12125. */
  12126. static int test_Sha512_Family_Final(int type, int isRaw)
  12127. {
  12128. EXPECT_DECLS;
  12129. wc_Sha512 sha512;
  12130. byte* hash_test[3];
  12131. byte hash1[WC_SHA512_DIGEST_SIZE];
  12132. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12133. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12134. int times, i;
  12135. int(*initFp)(wc_Sha512*);
  12136. int(*finalFp)(wc_Sha512*, byte*);
  12137. void(*freeFp)(wc_Sha512*);
  12138. if (type == WC_HASH_TYPE_SHA512) {
  12139. initFp = wc_InitSha512;
  12140. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12141. !defined(WOLFSSL_NO_HASH_RAW)
  12142. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12143. #else
  12144. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12145. #endif
  12146. freeFp = wc_Sha512Free;
  12147. }
  12148. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12149. #if !defined(WOLFSSL_NOSHA512_224)
  12150. else if (type == WC_HASH_TYPE_SHA512_224) {
  12151. initFp = wc_InitSha512_224;
  12152. #if !defined(WOLFSSL_NO_HASH_RAW)
  12153. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12154. #else
  12155. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12156. #endif
  12157. freeFp = wc_Sha512_224Free;
  12158. }
  12159. #endif
  12160. #if !defined(WOLFSSL_NOSHA512_256)
  12161. else if (type == WC_HASH_TYPE_SHA512_256) {
  12162. initFp = wc_InitSha512_256;
  12163. #if !defined(WOLFSSL_NO_HASH_RAW)
  12164. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12165. #else
  12166. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12167. #endif
  12168. freeFp = wc_Sha512_256Free;
  12169. }
  12170. #endif
  12171. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12172. else
  12173. return TEST_FAIL;
  12174. /* Initialize */
  12175. ExpectIntEQ(initFp(&sha512), 0);
  12176. hash_test[0] = hash1;
  12177. hash_test[1] = hash2;
  12178. hash_test[2] = hash3;
  12179. times = sizeof(hash_test) / sizeof(byte *);
  12180. /* Good test args. */
  12181. for (i = 0; i < times; i++) {
  12182. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12183. }
  12184. /* Test bad args. */
  12185. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12186. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12187. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12188. freeFp(&sha512);
  12189. return EXPECT_RESULT();
  12190. }
  12191. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12192. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12193. #endif /* WOLFSSL_SHA512 */
  12194. /*
  12195. * Unit test function for wc_Sha512Final()
  12196. */
  12197. static int test_wc_Sha512Final(void)
  12198. {
  12199. EXPECT_DECLS;
  12200. #ifdef WOLFSSL_SHA512
  12201. wc_Sha512 sha512;
  12202. byte* hash_test[3];
  12203. byte hash1[WC_SHA512_DIGEST_SIZE];
  12204. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12205. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12206. int times, i;
  12207. /* Initialize */
  12208. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12209. hash_test[0] = hash1;
  12210. hash_test[1] = hash2;
  12211. hash_test[2] = hash3;
  12212. times = sizeof(hash_test) / sizeof(byte *);
  12213. for (i = 0; i < times; i++) {
  12214. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12215. }
  12216. /* Test bad args. */
  12217. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12218. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12219. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12220. wc_Sha512Free(&sha512);
  12221. #endif
  12222. return EXPECT_RESULT();
  12223. } /* END test_wc_Sha512Final */
  12224. /*
  12225. * Unit test function for wc_Sha512GetFlags()
  12226. */
  12227. static int test_wc_Sha512GetFlags(void)
  12228. {
  12229. EXPECT_DECLS;
  12230. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12231. wc_Sha512 sha512;
  12232. word32 flags = 0;
  12233. /* Initialize */
  12234. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12235. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12236. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12237. wc_Sha512Free(&sha512);
  12238. #endif
  12239. return EXPECT_RESULT();
  12240. } /* END test_wc_Sha512GetFlags */
  12241. /*
  12242. * Unit test function for wc_Sha512FinalRaw()
  12243. */
  12244. static int test_wc_Sha512FinalRaw(void)
  12245. {
  12246. EXPECT_DECLS;
  12247. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12248. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12249. !defined(WOLFSSL_NO_HASH_RAW)
  12250. wc_Sha512 sha512;
  12251. byte* hash_test[3];
  12252. byte hash1[WC_SHA512_DIGEST_SIZE];
  12253. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12254. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12255. int times, i;
  12256. /* Initialize */
  12257. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12258. hash_test[0] = hash1;
  12259. hash_test[1] = hash2;
  12260. hash_test[2] = hash3;
  12261. times = sizeof(hash_test) / sizeof(byte*);
  12262. /* Good test args. */
  12263. for (i = 0; i < times; i++) {
  12264. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12265. }
  12266. /* Test bad args. */
  12267. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12268. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12269. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12270. wc_Sha512Free(&sha512);
  12271. #endif
  12272. return EXPECT_RESULT();
  12273. } /* END test_wc_Sha512FinalRaw */
  12274. /*
  12275. * Unit test function for wc_Sha512Free()
  12276. */
  12277. static int test_wc_Sha512Free(void)
  12278. {
  12279. EXPECT_DECLS;
  12280. #ifdef WOLFSSL_SHA512
  12281. wc_Sha512Free(NULL);
  12282. /* Set result to SUCCESS. */
  12283. ExpectTrue(1);
  12284. #endif
  12285. return EXPECT_RESULT();
  12286. } /* END test_wc_Sha512Free */
  12287. #ifdef WOLFSSL_SHA512
  12288. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12289. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12290. static int test_Sha512_Family_GetHash(int type )
  12291. {
  12292. EXPECT_DECLS;
  12293. int(*initFp)(wc_Sha512*);
  12294. int(*ghashFp)(wc_Sha512*, byte*);
  12295. wc_Sha512 sha512;
  12296. byte hash1[WC_SHA512_DIGEST_SIZE];
  12297. if (type == WC_HASH_TYPE_SHA512) {
  12298. initFp = wc_InitSha512;
  12299. ghashFp = wc_Sha512GetHash;
  12300. }
  12301. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12302. #if !defined(WOLFSSL_NOSHA512_224)
  12303. else if (type == WC_HASH_TYPE_SHA512_224) {
  12304. initFp = wc_InitSha512_224;
  12305. ghashFp = wc_Sha512_224GetHash;
  12306. }
  12307. #endif
  12308. #if !defined(WOLFSSL_NOSHA512_256)
  12309. else if (type == WC_HASH_TYPE_SHA512_256) {
  12310. initFp = wc_InitSha512_256;
  12311. ghashFp = wc_Sha512_256GetHash;
  12312. }
  12313. #endif
  12314. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12315. else {
  12316. initFp = NULL;
  12317. ghashFp = NULL;
  12318. }
  12319. if (initFp == NULL || ghashFp == NULL)
  12320. return TEST_FAIL;
  12321. ExpectIntEQ(initFp(&sha512), 0);
  12322. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12323. /* test bad arguments*/
  12324. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12325. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12326. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12327. wc_Sha512Free(&sha512);
  12328. return EXPECT_RESULT();
  12329. }
  12330. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12331. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12332. #endif /* WOLFSSL_SHA512 */
  12333. /*
  12334. * Unit test function for wc_Sha512GetHash()
  12335. */
  12336. static int test_wc_Sha512GetHash(void)
  12337. {
  12338. EXPECT_DECLS;
  12339. #ifdef WOLFSSL_SHA512
  12340. wc_Sha512 sha512;
  12341. byte hash1[WC_SHA512_DIGEST_SIZE];
  12342. /* Initialize */
  12343. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12344. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12345. /* test bad arguments*/
  12346. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12347. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12348. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12349. wc_Sha512Free(&sha512);
  12350. #endif
  12351. return EXPECT_RESULT();
  12352. } /* END test_wc_Sha512GetHash */
  12353. /*
  12354. * Unit test function for wc_Sha512Copy()
  12355. */
  12356. static int test_wc_Sha512Copy(void)
  12357. {
  12358. EXPECT_DECLS;
  12359. #ifdef WOLFSSL_SHA512
  12360. wc_Sha512 sha512;
  12361. wc_Sha512 temp;
  12362. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12363. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12364. /* Initialize */
  12365. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12366. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12367. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12368. /* test bad arguments*/
  12369. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12370. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12371. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12372. wc_Sha512Free(&sha512);
  12373. wc_Sha512Free(&temp);
  12374. #endif
  12375. return EXPECT_RESULT();
  12376. } /* END test_wc_Sha512Copy */
  12377. static int test_wc_InitSha512_224(void)
  12378. {
  12379. EXPECT_DECLS;
  12380. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12381. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12382. wc_Sha512 sha512;
  12383. /* Test good arg. */
  12384. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12385. /* Test bad arg. */
  12386. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12387. wc_Sha512_224Free(&sha512);
  12388. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12389. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12390. return EXPECT_RESULT();
  12391. }
  12392. static int test_wc_Sha512_224Update(void)
  12393. {
  12394. EXPECT_DECLS;
  12395. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12396. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12397. wc_Sha512 sha512;
  12398. byte hash[WC_SHA512_DIGEST_SIZE];
  12399. testVector a, c;
  12400. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12401. /* Input. */
  12402. a.input = "a";
  12403. a.inLen = XSTRLEN(a.input);
  12404. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12405. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12406. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12407. 0);
  12408. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12409. /* Update input. */
  12410. a.input = "abc";
  12411. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12412. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12413. a.inLen = XSTRLEN(a.input);
  12414. a.outLen = XSTRLEN(a.output);
  12415. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12416. 0);
  12417. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12418. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12419. c.input = NULL;
  12420. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12421. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12422. BAD_FUNC_ARG);
  12423. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12424. BAD_FUNC_ARG);
  12425. wc_Sha512_224Free(&sha512);
  12426. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12427. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12428. return EXPECT_RESULT();
  12429. }
  12430. static int test_wc_Sha512_224Final(void)
  12431. {
  12432. EXPECT_DECLS;
  12433. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12434. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12435. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12436. TEST_SUCCESS);
  12437. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12438. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12439. return EXPECT_RESULT();
  12440. }
  12441. static int test_wc_Sha512_224GetFlags(void)
  12442. {
  12443. EXPECT_DECLS;
  12444. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12445. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12446. wc_Sha512 sha512;
  12447. wc_Sha512 copy;
  12448. word32 flags = 0;
  12449. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12450. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12451. /* Initialize */
  12452. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12453. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12454. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12455. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12456. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12457. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12458. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12459. wc_Sha512_224Free(&copy);
  12460. wc_Sha512_224Free(&sha512);
  12461. #endif
  12462. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12463. return EXPECT_RESULT();
  12464. }
  12465. static int test_wc_Sha512_224FinalRaw(void)
  12466. {
  12467. EXPECT_DECLS;
  12468. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12469. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12470. !defined(WOLFSSL_NO_HASH_RAW)
  12471. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12472. TEST_SUCCESS);
  12473. #endif
  12474. return EXPECT_RESULT();
  12475. }
  12476. static int test_wc_Sha512_224Free(void)
  12477. {
  12478. EXPECT_DECLS;
  12479. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12480. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12481. wc_Sha512_224Free(NULL);
  12482. /* Set result to SUCCESS. */
  12483. ExpectTrue(1);
  12484. #endif
  12485. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12486. return EXPECT_RESULT();
  12487. }
  12488. static int test_wc_Sha512_224GetHash(void)
  12489. {
  12490. EXPECT_DECLS;
  12491. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12492. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12493. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12494. TEST_SUCCESS);
  12495. #endif
  12496. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12497. return EXPECT_RESULT();
  12498. }
  12499. static int test_wc_Sha512_224Copy(void)
  12500. {
  12501. EXPECT_DECLS;
  12502. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12503. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12504. wc_Sha512 sha512;
  12505. wc_Sha512 temp;
  12506. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12507. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12508. /* Initialize */
  12509. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12510. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12511. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12512. /* test bad arguments*/
  12513. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12514. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12515. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12516. wc_Sha512_224Free(&sha512);
  12517. wc_Sha512_224Free(&temp);
  12518. #endif
  12519. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12520. return EXPECT_RESULT();
  12521. }
  12522. static int test_wc_InitSha512_256(void)
  12523. {
  12524. EXPECT_DECLS;
  12525. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12526. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12527. wc_Sha512 sha512;
  12528. /* Test good arg. */
  12529. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12530. /* Test bad arg. */
  12531. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12532. wc_Sha512_256Free(&sha512);
  12533. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12534. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12535. return EXPECT_RESULT();
  12536. }
  12537. static int test_wc_Sha512_256Update(void)
  12538. {
  12539. EXPECT_DECLS;
  12540. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12541. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12542. wc_Sha512 sha512;
  12543. byte hash[WC_SHA512_DIGEST_SIZE];
  12544. testVector a, c;
  12545. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12546. /* Input. */
  12547. a.input = "a";
  12548. a.inLen = XSTRLEN(a.input);
  12549. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12550. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12551. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12552. 0);
  12553. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12554. /* Update input. */
  12555. a.input = "abc";
  12556. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12557. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12558. "\x07\xe7\xaf\x23";
  12559. a.inLen = XSTRLEN(a.input);
  12560. a.outLen = XSTRLEN(a.output);
  12561. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12562. 0);
  12563. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12564. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12565. c.input = NULL;
  12566. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12567. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12568. BAD_FUNC_ARG);
  12569. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12570. BAD_FUNC_ARG);
  12571. wc_Sha512_256Free(&sha512);
  12572. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12573. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12574. return EXPECT_RESULT();
  12575. }
  12576. static int test_wc_Sha512_256Final(void)
  12577. {
  12578. EXPECT_DECLS;
  12579. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12580. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12581. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12582. TEST_SUCCESS);
  12583. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12584. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12585. return EXPECT_RESULT();
  12586. }
  12587. static int test_wc_Sha512_256GetFlags(void)
  12588. {
  12589. EXPECT_DECLS;
  12590. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12591. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12592. wc_Sha512 sha512, copy;
  12593. word32 flags = 0;
  12594. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12595. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12596. /* Initialize */
  12597. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12598. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12599. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12600. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12601. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12602. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12603. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12604. wc_Sha512_256Free(&sha512);
  12605. #endif
  12606. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12607. return EXPECT_RESULT();
  12608. }
  12609. static int test_wc_Sha512_256FinalRaw(void)
  12610. {
  12611. EXPECT_DECLS;
  12612. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12613. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12614. !defined(WOLFSSL_NO_HASH_RAW)
  12615. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12616. TEST_SUCCESS);
  12617. #endif
  12618. return EXPECT_RESULT();
  12619. }
  12620. static int test_wc_Sha512_256Free(void)
  12621. {
  12622. EXPECT_DECLS;
  12623. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12624. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12625. wc_Sha512_256Free(NULL);
  12626. /* Set result to SUCCESS. */
  12627. ExpectTrue(1);
  12628. #endif
  12629. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12630. return EXPECT_RESULT();
  12631. }
  12632. static int test_wc_Sha512_256GetHash(void)
  12633. {
  12634. EXPECT_DECLS;
  12635. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12636. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12637. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12638. TEST_SUCCESS);
  12639. #endif
  12640. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12641. return EXPECT_RESULT();
  12642. }
  12643. static int test_wc_Sha512_256Copy(void)
  12644. {
  12645. EXPECT_DECLS;
  12646. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12647. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12648. wc_Sha512 sha512;
  12649. wc_Sha512 temp;
  12650. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12651. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12652. /* Initialize */
  12653. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12654. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12655. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12656. /* test bad arguments*/
  12657. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12658. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12659. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12660. wc_Sha512_256Free(&sha512);
  12661. wc_Sha512_256Free(&temp);
  12662. #endif
  12663. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12664. return EXPECT_RESULT();
  12665. }
  12666. /*
  12667. * Testing wc_InitSha384()
  12668. */
  12669. static int test_wc_InitSha384(void)
  12670. {
  12671. EXPECT_DECLS;
  12672. #ifdef WOLFSSL_SHA384
  12673. wc_Sha384 sha384;
  12674. /* Test good arg. */
  12675. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12676. /* Test bad arg. */
  12677. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12678. wc_Sha384Free(&sha384);
  12679. #endif
  12680. return EXPECT_RESULT();
  12681. } /* END test_wc_InitSha384 */
  12682. /*
  12683. * test wc_Sha384Update()
  12684. */
  12685. static int test_wc_Sha384Update(void)
  12686. {
  12687. EXPECT_DECLS;
  12688. #ifdef WOLFSSL_SHA384
  12689. wc_Sha384 sha384;
  12690. byte hash[WC_SHA384_DIGEST_SIZE];
  12691. testVector a, b, c;
  12692. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12693. /* Input */
  12694. a.input = "a";
  12695. a.inLen = XSTRLEN(a.input);
  12696. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12697. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12698. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12699. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12700. /* Update input. */
  12701. a.input = "abc";
  12702. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12703. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12704. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12705. "\xc8\x25\xa7";
  12706. a.inLen = XSTRLEN(a.input);
  12707. a.outLen = XSTRLEN(a.output);
  12708. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12709. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12710. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12711. /* Pass in bad values. */
  12712. b.input = NULL;
  12713. b.inLen = 0;
  12714. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12715. c.input = NULL;
  12716. c.inLen = WC_SHA384_DIGEST_SIZE;
  12717. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12718. BAD_FUNC_ARG);
  12719. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12720. BAD_FUNC_ARG);
  12721. wc_Sha384Free(&sha384);
  12722. #endif
  12723. return EXPECT_RESULT();
  12724. } /* END test_wc_Sha384Update */
  12725. /*
  12726. * Unit test function for wc_Sha384Final();
  12727. */
  12728. static int test_wc_Sha384Final(void)
  12729. {
  12730. EXPECT_DECLS;
  12731. #ifdef WOLFSSL_SHA384
  12732. wc_Sha384 sha384;
  12733. byte* hash_test[3];
  12734. byte hash1[WC_SHA384_DIGEST_SIZE];
  12735. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12736. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12737. int times, i;
  12738. /* Initialize */
  12739. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12740. hash_test[0] = hash1;
  12741. hash_test[1] = hash2;
  12742. hash_test[2] = hash3;
  12743. times = sizeof(hash_test) / sizeof(byte*);
  12744. /* Good test args. */
  12745. for (i = 0; i < times; i++) {
  12746. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12747. }
  12748. /* Test bad args. */
  12749. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12750. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12751. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12752. wc_Sha384Free(&sha384);
  12753. #endif
  12754. return EXPECT_RESULT();
  12755. } /* END test_wc_Sha384Final */
  12756. /*
  12757. * Unit test function for wc_Sha384GetFlags()
  12758. */
  12759. static int test_wc_Sha384GetFlags(void)
  12760. {
  12761. EXPECT_DECLS;
  12762. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12763. wc_Sha384 sha384;
  12764. word32 flags = 0;
  12765. /* Initialize */
  12766. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12767. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12768. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12769. wc_Sha384Free(&sha384);
  12770. #endif
  12771. return EXPECT_RESULT();
  12772. } /* END test_wc_Sha384GetFlags */
  12773. /*
  12774. * Unit test function for wc_Sha384FinalRaw()
  12775. */
  12776. static int test_wc_Sha384FinalRaw(void)
  12777. {
  12778. EXPECT_DECLS;
  12779. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12780. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12781. !defined(WOLFSSL_NO_HASH_RAW)
  12782. wc_Sha384 sha384;
  12783. byte* hash_test[3];
  12784. byte hash1[WC_SHA384_DIGEST_SIZE];
  12785. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12786. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12787. int times, i;
  12788. /* Initialize */
  12789. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12790. hash_test[0] = hash1;
  12791. hash_test[1] = hash2;
  12792. hash_test[2] = hash3;
  12793. times = sizeof(hash_test) / sizeof(byte*);
  12794. /* Good test args. */
  12795. for (i = 0; i < times; i++) {
  12796. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12797. }
  12798. /* Test bad args. */
  12799. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12800. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12801. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12802. wc_Sha384Free(&sha384);
  12803. #endif
  12804. return EXPECT_RESULT();
  12805. } /* END test_wc_Sha384FinalRaw */
  12806. /*
  12807. * Unit test function for wc_Sha384Free()
  12808. */
  12809. static int test_wc_Sha384Free(void)
  12810. {
  12811. EXPECT_DECLS;
  12812. #ifdef WOLFSSL_SHA384
  12813. wc_Sha384Free(NULL);
  12814. /* Set result to SUCCESS. */
  12815. ExpectTrue(1);
  12816. #endif
  12817. return EXPECT_RESULT();
  12818. } /* END test_wc_Sha384Free */
  12819. /*
  12820. * Unit test function for wc_Sha384GetHash()
  12821. */
  12822. static int test_wc_Sha384GetHash(void)
  12823. {
  12824. EXPECT_DECLS;
  12825. #ifdef WOLFSSL_SHA384
  12826. wc_Sha384 sha384;
  12827. byte hash1[WC_SHA384_DIGEST_SIZE];
  12828. /* Initialize */
  12829. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12830. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12831. /* test bad arguments*/
  12832. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12833. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12834. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12835. wc_Sha384Free(&sha384);
  12836. #endif
  12837. return EXPECT_RESULT();
  12838. } /* END test_wc_Sha384GetHash */
  12839. /*
  12840. * Unit test function for wc_Sha384Copy()
  12841. */
  12842. static int test_wc_Sha384Copy(void)
  12843. {
  12844. EXPECT_DECLS;
  12845. #ifdef WOLFSSL_SHA384
  12846. wc_Sha384 sha384;
  12847. wc_Sha384 temp;
  12848. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12849. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12850. /* Initialize */
  12851. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12852. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12853. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12854. /* test bad arguments*/
  12855. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12856. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12857. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12858. wc_Sha384Free(&sha384);
  12859. wc_Sha384Free(&temp);
  12860. #endif
  12861. return EXPECT_RESULT();
  12862. } /* END test_wc_Sha384Copy */
  12863. /*
  12864. * Testing wc_InitSha224();
  12865. */
  12866. static int test_wc_InitSha224(void)
  12867. {
  12868. EXPECT_DECLS;
  12869. #ifdef WOLFSSL_SHA224
  12870. wc_Sha224 sha224;
  12871. /* Test good arg. */
  12872. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12873. /* Test bad arg. */
  12874. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12875. wc_Sha224Free(&sha224);
  12876. #endif
  12877. return EXPECT_RESULT();
  12878. } /* END test_wc_InitSha224 */
  12879. /*
  12880. * Unit test on wc_Sha224Update
  12881. */
  12882. static int test_wc_Sha224Update(void)
  12883. {
  12884. EXPECT_DECLS;
  12885. #ifdef WOLFSSL_SHA224
  12886. wc_Sha224 sha224;
  12887. byte hash[WC_SHA224_DIGEST_SIZE];
  12888. testVector a, b, c;
  12889. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12890. /* Input. */
  12891. a.input = "a";
  12892. a.inLen = XSTRLEN(a.input);
  12893. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12894. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12895. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12896. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12897. /* Update input. */
  12898. a.input = "abc";
  12899. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12900. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12901. a.inLen = XSTRLEN(a.input);
  12902. a.outLen = XSTRLEN(a.output);
  12903. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12904. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12905. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12906. /* Pass in bad values. */
  12907. b.input = NULL;
  12908. b.inLen = 0;
  12909. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12910. c.input = NULL;
  12911. c.inLen = WC_SHA224_DIGEST_SIZE;
  12912. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12913. BAD_FUNC_ARG);
  12914. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12915. BAD_FUNC_ARG);
  12916. wc_Sha224Free(&sha224);
  12917. #endif
  12918. return EXPECT_RESULT();
  12919. } /* END test_wc_Sha224Update */
  12920. /*
  12921. * Unit test for wc_Sha224Final();
  12922. */
  12923. static int test_wc_Sha224Final(void)
  12924. {
  12925. EXPECT_DECLS;
  12926. #ifdef WOLFSSL_SHA224
  12927. wc_Sha224 sha224;
  12928. byte* hash_test[3];
  12929. byte hash1[WC_SHA224_DIGEST_SIZE];
  12930. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12931. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12932. int times, i;
  12933. /* Initialize */
  12934. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12935. hash_test[0] = hash1;
  12936. hash_test[1] = hash2;
  12937. hash_test[2] = hash3;
  12938. times = sizeof(hash_test) / sizeof(byte*);
  12939. /* Good test args. */
  12940. /* Testing oversized buffers. */
  12941. for (i = 0; i < times; i++) {
  12942. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12943. }
  12944. /* Test bad args. */
  12945. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12946. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12947. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12948. wc_Sha224Free(&sha224);
  12949. #endif
  12950. return EXPECT_RESULT();
  12951. } /* END test_wc_Sha224Final */
  12952. /*
  12953. * Unit test function for wc_Sha224SetFlags()
  12954. */
  12955. static int test_wc_Sha224SetFlags(void)
  12956. {
  12957. EXPECT_DECLS;
  12958. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12959. wc_Sha224 sha224;
  12960. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12961. /* Initialize */
  12962. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12963. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12964. flags = 0;
  12965. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12966. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12967. wc_Sha224Free(&sha224);
  12968. #endif
  12969. return EXPECT_RESULT();
  12970. } /* END test_wc_Sha224SetFlags */
  12971. /*
  12972. * Unit test function for wc_Sha224GetFlags()
  12973. */
  12974. static int test_wc_Sha224GetFlags(void)
  12975. {
  12976. EXPECT_DECLS;
  12977. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12978. wc_Sha224 sha224;
  12979. word32 flags = 0;
  12980. /* Initialize */
  12981. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12982. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12983. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12984. wc_Sha224Free(&sha224);
  12985. #endif
  12986. return EXPECT_RESULT();
  12987. } /* END test_wc_Sha224GetFlags */
  12988. /*
  12989. * Unit test function for wc_Sha224Free()
  12990. */
  12991. static int test_wc_Sha224Free(void)
  12992. {
  12993. EXPECT_DECLS;
  12994. #ifdef WOLFSSL_SHA224
  12995. wc_Sha224Free(NULL);
  12996. /* Set result to SUCCESS. */
  12997. ExpectTrue(1);
  12998. #endif
  12999. return EXPECT_RESULT();
  13000. } /* END test_wc_Sha224Free */
  13001. /*
  13002. * Unit test function for wc_Sha224GetHash()
  13003. */
  13004. static int test_wc_Sha224GetHash(void)
  13005. {
  13006. EXPECT_DECLS;
  13007. #ifdef WOLFSSL_SHA224
  13008. wc_Sha224 sha224;
  13009. byte hash1[WC_SHA224_DIGEST_SIZE];
  13010. /* Initialize */
  13011. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13012. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  13013. /* test bad arguments*/
  13014. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  13015. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  13016. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  13017. wc_Sha224Free(&sha224);
  13018. #endif
  13019. return EXPECT_RESULT();
  13020. } /* END test_wc_Sha224GetHash */
  13021. /*
  13022. * Unit test function for wc_Sha224Copy()
  13023. */
  13024. static int test_wc_Sha224Copy(void)
  13025. {
  13026. EXPECT_DECLS;
  13027. #ifdef WOLFSSL_SHA224
  13028. wc_Sha224 sha224;
  13029. wc_Sha224 temp;
  13030. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  13031. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  13032. /* Initialize */
  13033. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13034. ExpectIntEQ(wc_InitSha224(&temp), 0);
  13035. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  13036. /* test bad arguments*/
  13037. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  13038. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  13039. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  13040. wc_Sha224Free(&sha224);
  13041. wc_Sha224Free(&temp);
  13042. #endif
  13043. return EXPECT_RESULT();
  13044. } /* END test_wc_Sha224Copy */
  13045. /*
  13046. * Testing wc_InitRipeMd()
  13047. */
  13048. static int test_wc_InitRipeMd(void)
  13049. {
  13050. EXPECT_DECLS;
  13051. #ifdef WOLFSSL_RIPEMD
  13052. RipeMd ripemd;
  13053. /* Test good arg. */
  13054. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13055. /* Test bad arg. */
  13056. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  13057. #endif
  13058. return EXPECT_RESULT();
  13059. } /* END test_wc_InitRipeMd */
  13060. /*
  13061. * Testing wc_RipeMdUpdate()
  13062. */
  13063. static int test_wc_RipeMdUpdate(void)
  13064. {
  13065. EXPECT_DECLS;
  13066. #ifdef WOLFSSL_RIPEMD
  13067. RipeMd ripemd;
  13068. byte hash[RIPEMD_DIGEST_SIZE];
  13069. testVector a, b, c;
  13070. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13071. /* Input */
  13072. a.input = "a";
  13073. a.inLen = XSTRLEN(a.input);
  13074. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13075. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13076. /* Update input. */
  13077. a.input = "abc";
  13078. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  13079. "\xb0\x87\xf1\x5a\x0b\xfc";
  13080. a.inLen = XSTRLEN(a.input);
  13081. a.outLen = XSTRLEN(a.output);
  13082. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13083. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13084. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  13085. /* Pass in bad values. */
  13086. b.input = NULL;
  13087. b.inLen = 0;
  13088. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  13089. c.input = NULL;
  13090. c.inLen = RIPEMD_DIGEST_SIZE;
  13091. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  13092. BAD_FUNC_ARG);
  13093. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13094. BAD_FUNC_ARG);
  13095. #endif
  13096. return EXPECT_RESULT();
  13097. } /* END test_wc_RipeMdUdpate */
  13098. /*
  13099. * Unit test function for wc_RipeMdFinal()
  13100. */
  13101. static int test_wc_RipeMdFinal(void)
  13102. {
  13103. EXPECT_DECLS;
  13104. #ifdef WOLFSSL_RIPEMD
  13105. RipeMd ripemd;
  13106. byte* hash_test[3];
  13107. byte hash1[RIPEMD_DIGEST_SIZE];
  13108. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13109. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13110. int times, i;
  13111. /* Initialize */
  13112. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13113. hash_test[0] = hash1;
  13114. hash_test[1] = hash2;
  13115. hash_test[2] = hash3;
  13116. times = sizeof(hash_test) / sizeof(byte*);
  13117. /* Testing oversized buffers. */
  13118. for (i = 0; i < times; i++) {
  13119. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13120. }
  13121. /* Test bad args. */
  13122. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13123. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13124. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13125. #endif
  13126. return EXPECT_RESULT();
  13127. } /* END test_wc_RipeMdFinal */
  13128. /*
  13129. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13130. * wc_InitSha3_512
  13131. */
  13132. static int test_wc_InitSha3(void)
  13133. {
  13134. EXPECT_DECLS;
  13135. #if defined(WOLFSSL_SHA3)
  13136. wc_Sha3 sha3;
  13137. (void)sha3;
  13138. #if !defined(WOLFSSL_NOSHA3_224)
  13139. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13140. /* Test bad args. */
  13141. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13142. wc_Sha3_224_Free(&sha3);
  13143. #endif /* NOSHA3_224 */
  13144. #if !defined(WOLFSSL_NOSHA3_256)
  13145. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13146. /* Test bad args. */
  13147. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13148. wc_Sha3_256_Free(&sha3);
  13149. #endif /* NOSHA3_256 */
  13150. #if !defined(WOLFSSL_NOSHA3_384)
  13151. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13152. /* Test bad args. */
  13153. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13154. wc_Sha3_384_Free(&sha3);
  13155. #endif /* NOSHA3_384 */
  13156. #if !defined(WOLFSSL_NOSHA3_512)
  13157. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13158. /* Test bad args. */
  13159. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13160. wc_Sha3_512_Free(&sha3);
  13161. #endif /* NOSHA3_512 */
  13162. #endif
  13163. return EXPECT_RESULT();
  13164. } /* END test_wc_InitSha3 */
  13165. /*
  13166. * Testing wc_Sha3_Update()
  13167. */
  13168. static int testing_wc_Sha3_Update(void)
  13169. {
  13170. EXPECT_DECLS;
  13171. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13172. !defined(WOLFSSL_AFALG_XILINX)
  13173. wc_Sha3 sha3;
  13174. byte msg[] = "Everybody's working for the weekend.";
  13175. byte msg2[] = "Everybody gets Friday off.";
  13176. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13177. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13178. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13179. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13180. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13181. word32 msglen = sizeof(msg) - 1;
  13182. word32 msg2len = sizeof(msg2);
  13183. word32 msgCmplen = sizeof(msgCmp);
  13184. #if !defined(WOLFSSL_NOSHA3_224)
  13185. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13186. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13187. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13188. ExpectTrue(sha3.i == msglen);
  13189. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13190. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13191. /* Pass bad args. */
  13192. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13193. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13194. wc_Sha3_224_Free(&sha3);
  13195. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13196. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13197. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13198. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13199. wc_Sha3_224_Free(&sha3);
  13200. #endif /* SHA3_224 */
  13201. #if !defined(WOLFSSL_NOSHA3_256)
  13202. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13203. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13204. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13205. ExpectTrue(sha3.i == msglen);
  13206. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13207. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13208. /* Pass bad args. */
  13209. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13210. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13211. wc_Sha3_256_Free(&sha3);
  13212. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13213. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13214. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13215. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13216. wc_Sha3_256_Free(&sha3);
  13217. #endif /* SHA3_256 */
  13218. #if !defined(WOLFSSL_NOSHA3_384)
  13219. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13220. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13221. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13222. ExpectTrue(sha3.i == msglen);
  13223. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13224. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13225. /* Pass bad args. */
  13226. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13227. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13228. wc_Sha3_384_Free(&sha3);
  13229. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13230. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13231. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13232. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13233. wc_Sha3_384_Free(&sha3);
  13234. #endif /* SHA3_384 */
  13235. #if !defined(WOLFSSL_NOSHA3_512)
  13236. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13237. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13238. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13239. ExpectTrue(sha3.i == msglen);
  13240. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13241. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13242. /* Pass bad args. */
  13243. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13244. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13245. wc_Sha3_512_Free(&sha3);
  13246. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13247. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13248. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13249. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13250. wc_Sha3_512_Free(&sha3);
  13251. #endif /* SHA3_512 */
  13252. #endif /* WOLFSSL_SHA3 */
  13253. return EXPECT_RESULT();
  13254. } /* END testing_wc_Sha3_Update */
  13255. /*
  13256. * Testing wc_Sha3_224_Final()
  13257. */
  13258. static int test_wc_Sha3_224_Final(void)
  13259. {
  13260. EXPECT_DECLS;
  13261. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13262. wc_Sha3 sha3;
  13263. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13264. "nopnopq";
  13265. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13266. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13267. "\x64\xea\xd0\xfc\xce\x33";
  13268. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13269. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13270. /* Init stack variables. */
  13271. XMEMSET(hash, 0, sizeof(hash));
  13272. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13273. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13274. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13275. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13276. /* Test bad args. */
  13277. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13278. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13279. wc_Sha3_224_Free(&sha3);
  13280. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13281. /* Init stack variables. */
  13282. XMEMSET(hash, 0, sizeof(hash));
  13283. XMEMSET(hashRet, 0, sizeof(hashRet));
  13284. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13285. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13286. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13287. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13288. /* Test bad args. */
  13289. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13290. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13291. wc_Sha3_224_Free(&sha3);
  13292. #endif
  13293. return EXPECT_RESULT();
  13294. } /* END test_wc_Sha3_224_Final */
  13295. /*
  13296. * Testing wc_Sha3_256_Final()
  13297. */
  13298. static int test_wc_Sha3_256_Final(void)
  13299. {
  13300. EXPECT_DECLS;
  13301. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13302. wc_Sha3 sha3;
  13303. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13304. "nopnopq";
  13305. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13306. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13307. "\xdd\x97\x49\x6d\x33\x76";
  13308. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13309. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13310. /* Init stack variables. */
  13311. XMEMSET(hash, 0, sizeof(hash));
  13312. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13313. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13314. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13315. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13316. /* Test bad args. */
  13317. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13318. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13319. wc_Sha3_256_Free(&sha3);
  13320. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13321. /* Init stack variables. */
  13322. XMEMSET(hash, 0, sizeof(hash));
  13323. XMEMSET(hashRet, 0, sizeof(hashRet));
  13324. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13325. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13326. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13327. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13328. /* Test bad args. */
  13329. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13330. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13331. wc_Sha3_256_Free(&sha3);
  13332. #endif
  13333. return EXPECT_RESULT();
  13334. } /* END test_wc_Sha3_256_Final */
  13335. /*
  13336. * Testing wc_Sha3_384_Final()
  13337. */
  13338. static int test_wc_Sha3_384_Final(void)
  13339. {
  13340. EXPECT_DECLS;
  13341. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13342. wc_Sha3 sha3;
  13343. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13344. "nopnopq";
  13345. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13346. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13347. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13348. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13349. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13350. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13351. /* Init stack variables. */
  13352. XMEMSET(hash, 0, sizeof(hash));
  13353. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13354. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13355. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13356. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13357. /* Test bad args. */
  13358. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13359. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13360. wc_Sha3_384_Free(&sha3);
  13361. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13362. /* Init stack variables. */
  13363. XMEMSET(hash, 0, sizeof(hash));
  13364. XMEMSET(hashRet, 0, sizeof(hashRet));
  13365. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13366. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13367. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13368. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13369. /* Test bad args. */
  13370. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13371. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13372. wc_Sha3_384_Free(&sha3);
  13373. #endif
  13374. return EXPECT_RESULT();
  13375. } /* END test_wc_Sha3_384_Final */
  13376. /*
  13377. * Testing wc_Sha3_512_Final()
  13378. */
  13379. static int test_wc_Sha3_512_Final(void)
  13380. {
  13381. EXPECT_DECLS;
  13382. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13383. !defined(WOLFSSL_NOSHA3_384)
  13384. wc_Sha3 sha3;
  13385. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13386. "nopnopq";
  13387. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13388. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13389. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13390. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13391. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13392. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13393. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13394. /* Init stack variables. */
  13395. XMEMSET(hash, 0, sizeof(hash));
  13396. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13397. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13398. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13399. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13400. /* Test bad args. */
  13401. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13402. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13403. wc_Sha3_512_Free(&sha3);
  13404. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13405. /* Init stack variables. */
  13406. XMEMSET(hash, 0, sizeof(hash));
  13407. XMEMSET(hashRet, 0, sizeof(hashRet));
  13408. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13409. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13410. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13411. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13412. /* Test bad args. */
  13413. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13414. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13415. wc_Sha3_512_Free(&sha3);
  13416. #endif
  13417. return EXPECT_RESULT();
  13418. } /* END test_wc_Sha3_512_Final */
  13419. /*
  13420. * Testing wc_Sha3_224_Copy()
  13421. */
  13422. static int test_wc_Sha3_224_Copy(void)
  13423. {
  13424. EXPECT_DECLS;
  13425. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13426. wc_Sha3 sha3, sha3Cpy;
  13427. const char* msg = TEST_STRING;
  13428. word32 msglen = (word32)TEST_STRING_SZ;
  13429. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13430. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13431. XMEMSET(hash, 0, sizeof(hash));
  13432. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13433. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13434. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13435. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13436. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13437. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13438. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13439. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13440. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13441. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13442. /* Test bad args. */
  13443. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13444. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13445. wc_Sha3_224_Free(&sha3);
  13446. wc_Sha3_224_Free(&sha3Cpy);
  13447. #endif
  13448. return EXPECT_RESULT();
  13449. } /* END test_wc_Sha3_224_Copy */
  13450. /*
  13451. * Testing wc_Sha3_256_Copy()
  13452. */
  13453. static int test_wc_Sha3_256_Copy(void)
  13454. {
  13455. EXPECT_DECLS;
  13456. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13457. wc_Sha3 sha3, sha3Cpy;
  13458. const char* msg = TEST_STRING;
  13459. word32 msglen = (word32)TEST_STRING_SZ;
  13460. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13461. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13462. XMEMSET(hash, 0, sizeof(hash));
  13463. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13464. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13465. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13466. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13467. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13468. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13469. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13470. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13471. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13472. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13473. /* Test bad args. */
  13474. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13475. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13476. wc_Sha3_256_Free(&sha3);
  13477. wc_Sha3_256_Free(&sha3Cpy);
  13478. #endif
  13479. return EXPECT_RESULT();
  13480. } /* END test_wc_Sha3_256_Copy */
  13481. /*
  13482. * Testing wc_Sha3_384_Copy()
  13483. */
  13484. static int test_wc_Sha3_384_Copy(void)
  13485. {
  13486. EXPECT_DECLS;
  13487. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13488. wc_Sha3 sha3, sha3Cpy;
  13489. const char* msg = TEST_STRING;
  13490. word32 msglen = (word32)TEST_STRING_SZ;
  13491. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13492. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13493. XMEMSET(hash, 0, sizeof(hash));
  13494. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13495. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13496. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13497. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13498. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13499. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13500. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13501. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13502. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13503. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13504. /* Test bad args. */
  13505. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13506. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13507. wc_Sha3_384_Free(&sha3);
  13508. wc_Sha3_384_Free(&sha3Cpy);
  13509. #endif
  13510. return EXPECT_RESULT();
  13511. } /* END test_wc_Sha3_384_Copy */
  13512. /*
  13513. * Testing wc_Sha3_512_Copy()
  13514. */
  13515. static int test_wc_Sha3_512_Copy(void)
  13516. {
  13517. EXPECT_DECLS;
  13518. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13519. wc_Sha3 sha3, sha3Cpy;
  13520. const char* msg = TEST_STRING;
  13521. word32 msglen = (word32)TEST_STRING_SZ;
  13522. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13523. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13524. XMEMSET(hash, 0, sizeof(hash));
  13525. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13526. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13527. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13528. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13529. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13530. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13531. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13532. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13533. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13534. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13535. /* Test bad args. */
  13536. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13537. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13538. wc_Sha3_512_Free(&sha3);
  13539. wc_Sha3_512_Free(&sha3Cpy);
  13540. #endif
  13541. return EXPECT_RESULT();
  13542. } /* END test_wc_Sha3_512_Copy */
  13543. /*
  13544. * Unit test function for wc_Sha3_GetFlags()
  13545. */
  13546. static int test_wc_Sha3_GetFlags(void)
  13547. {
  13548. EXPECT_DECLS;
  13549. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13550. wc_Sha3 sha3;
  13551. word32 flags = 0;
  13552. /* Initialize */
  13553. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13554. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13555. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13556. wc_Sha3_224_Free(&sha3);
  13557. #endif
  13558. return EXPECT_RESULT();
  13559. } /* END test_wc_Sha3_GetFlags */
  13560. static int test_wc_InitShake256(void)
  13561. {
  13562. EXPECT_DECLS;
  13563. #ifdef WOLFSSL_SHAKE256
  13564. wc_Shake shake;
  13565. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13566. /* Test bad args. */
  13567. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13568. wc_Shake256_Free(&shake);
  13569. #endif
  13570. return EXPECT_RESULT();
  13571. }
  13572. static int testing_wc_Shake256_Update(void)
  13573. {
  13574. EXPECT_DECLS;
  13575. #ifdef WOLFSSL_SHAKE256
  13576. wc_Shake shake;
  13577. byte msg[] = "Everybody's working for the weekend.";
  13578. byte msg2[] = "Everybody gets Friday off.";
  13579. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13580. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13581. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13582. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13583. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13584. word32 msglen = sizeof(msg) - 1;
  13585. word32 msg2len = sizeof(msg2);
  13586. word32 msgCmplen = sizeof(msgCmp);
  13587. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13588. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13589. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13590. ExpectTrue(shake.i == msglen);
  13591. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13592. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13593. /* Pass bad args. */
  13594. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13595. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13596. wc_Shake256_Free(&shake);
  13597. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13598. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13599. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13600. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13601. wc_Shake256_Free(&shake);
  13602. #endif /* WOLFSSL_SHAKE256 */
  13603. return EXPECT_RESULT();
  13604. }
  13605. static int test_wc_Shake256_Final(void)
  13606. {
  13607. EXPECT_DECLS;
  13608. #ifdef WOLFSSL_SHAKE256
  13609. wc_Shake shake;
  13610. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13611. "nopnopq";
  13612. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13613. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13614. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13615. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13616. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13617. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13618. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13619. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13620. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13621. byte hash[114];
  13622. /* Init stack variables. */
  13623. XMEMSET(hash, 0, sizeof(hash));
  13624. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13625. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13626. 0);
  13627. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13628. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13629. /* Test bad args. */
  13630. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13631. BAD_FUNC_ARG);
  13632. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13633. BAD_FUNC_ARG);
  13634. wc_Shake256_Free(&shake);
  13635. #endif
  13636. return EXPECT_RESULT();
  13637. }
  13638. /*
  13639. * Testing wc_Shake256_Copy()
  13640. */
  13641. static int test_wc_Shake256_Copy(void)
  13642. {
  13643. EXPECT_DECLS;
  13644. #ifdef WOLFSSL_SHAKE256
  13645. wc_Shake shake, shakeCpy;
  13646. const char* msg = TEST_STRING;
  13647. word32 msglen = (word32)TEST_STRING_SZ;
  13648. byte hash[144];
  13649. byte hashCpy[144];
  13650. word32 hashLen = sizeof(hash);
  13651. word32 hashLenCpy = sizeof(hashCpy);
  13652. XMEMSET(hash, 0, sizeof(hash));
  13653. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13654. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13655. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13656. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13657. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13658. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13659. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13660. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13661. /* Test bad args. */
  13662. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13663. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13664. wc_Shake256_Free(&shake);
  13665. wc_Shake256_Free(&shakeCpy);
  13666. #endif
  13667. return EXPECT_RESULT();
  13668. } /* END test_wc_Shake256_Copy */
  13669. /*
  13670. * Unit test function for wc_Shake256Hash()
  13671. */
  13672. static int test_wc_Shake256Hash(void)
  13673. {
  13674. EXPECT_DECLS;
  13675. #ifdef WOLFSSL_SHAKE256
  13676. const byte data[] = { /* Hello World */
  13677. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13678. 0x72,0x6c,0x64
  13679. };
  13680. word32 len = sizeof(data);
  13681. byte hash[144];
  13682. word32 hashLen = sizeof(hash);
  13683. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13684. #endif
  13685. return EXPECT_RESULT();
  13686. } /* END test_wc_Shake256Hash */
  13687. /*
  13688. * Testing wc_InitSm3(), wc_Sm3Free()
  13689. */
  13690. static int test_wc_InitSm3Free(void)
  13691. {
  13692. EXPECT_DECLS;
  13693. #ifdef WOLFSSL_SM3
  13694. wc_Sm3 sm3;
  13695. /* Invalid Parameters */
  13696. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13697. /* Valid Parameters */
  13698. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13699. wc_Sm3Free(NULL);
  13700. wc_Sm3Free(&sm3);
  13701. #endif
  13702. return EXPECT_RESULT();
  13703. } /* END test_wc_InitSm3 */
  13704. /*
  13705. * Testing wc_Sm3Update(), wc_Sm3Final()
  13706. */
  13707. static int test_wc_Sm3UpdateFinal(void)
  13708. {
  13709. EXPECT_DECLS;
  13710. #ifdef WOLFSSL_SM3
  13711. wc_Sm3 sm3;
  13712. byte data[WC_SM3_BLOCK_SIZE * 4];
  13713. byte hash[WC_SM3_DIGEST_SIZE];
  13714. byte calcHash[WC_SM3_DIGEST_SIZE];
  13715. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13716. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13717. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13718. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13719. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13720. };
  13721. word32 chunk;
  13722. word32 i;
  13723. XMEMSET(data, 0, sizeof(data));
  13724. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13725. /* Invalid Parameters */
  13726. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13727. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13728. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13729. /* Valid Parameters */
  13730. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13731. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13732. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13733. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13734. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13735. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13736. /* Ensure too many bytes for lengths. */
  13737. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13738. /* Invalid Parameters */
  13739. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13740. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13741. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13742. /* Valid Parameters */
  13743. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13744. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13745. /* Chunk tests. */
  13746. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13747. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13748. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13749. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13750. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13751. }
  13752. if (i < (word32)sizeof(data)) {
  13753. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13754. 0);
  13755. }
  13756. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13757. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13758. }
  13759. /* Not testing when the low 32-bit length overflows. */
  13760. wc_Sm3Free(&sm3);
  13761. #endif
  13762. return EXPECT_RESULT();
  13763. } /* END test_wc_Sm3Update */
  13764. /*
  13765. * Testing wc_Sm3GetHash()
  13766. */
  13767. static int test_wc_Sm3GetHash(void)
  13768. {
  13769. EXPECT_DECLS;
  13770. #ifdef WOLFSSL_SM3
  13771. wc_Sm3 sm3;
  13772. byte hash[WC_SM3_DIGEST_SIZE];
  13773. byte calcHash[WC_SM3_DIGEST_SIZE];
  13774. byte data[WC_SM3_BLOCK_SIZE];
  13775. XMEMSET(data, 0, sizeof(data));
  13776. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13777. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13778. /* Invalid Parameters */
  13779. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13780. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13781. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13782. /* Valid Parameters */
  13783. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13784. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13785. /* With update. */
  13786. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13787. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13788. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13789. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13790. wc_Sm3Free(&sm3);
  13791. #endif
  13792. return EXPECT_RESULT();
  13793. } /* END test_wc_Sm3Update */
  13794. /*
  13795. * Testing wc_Sm3Copy()
  13796. */
  13797. static int test_wc_Sm3Copy(void)
  13798. {
  13799. EXPECT_DECLS;
  13800. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13801. wc_Sm3 sm3;
  13802. wc_Sm3 sm3Copy;
  13803. byte hash[WC_SM3_DIGEST_SIZE];
  13804. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13805. byte data[WC_SM3_BLOCK_SIZE + 1];
  13806. int i;
  13807. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13808. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13809. /* Invalid Parameters */
  13810. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13811. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13812. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13813. /* Valid Parameters */
  13814. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13815. /* Ensure all parts of data updated during hashing are copied. */
  13816. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13817. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13818. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13819. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13820. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13821. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13822. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13823. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13824. }
  13825. wc_Sm3Free(&sm3Copy);
  13826. wc_Sm3Free(&sm3);
  13827. #endif
  13828. return EXPECT_RESULT();
  13829. } /* END test_wc_Sm3Copy */
  13830. /*
  13831. * Testing wc_Sm3FinalRaw()
  13832. */
  13833. static int test_wc_Sm3FinalRaw(void)
  13834. {
  13835. EXPECT_DECLS;
  13836. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13837. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13838. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13839. !defined(WOLFSSL_NO_HASH_RAW)
  13840. wc_Sm3 sm3;
  13841. byte hash1[WC_SM3_DIGEST_SIZE];
  13842. byte hash2[WC_SM3_DIGEST_SIZE];
  13843. byte hash3[WC_SM3_DIGEST_SIZE];
  13844. byte* hash_test[3] = { hash1, hash2, hash3 };
  13845. int times;
  13846. int i;
  13847. XMEMSET(&sm3, 0, sizeof(sm3));
  13848. /* Initialize */
  13849. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13850. /* Invalid Parameters */
  13851. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13852. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13853. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13854. times = sizeof(hash_test) / sizeof(byte*);
  13855. for (i = 0; i < times; i++) {
  13856. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13857. }
  13858. wc_Sm3Free(&sm3);
  13859. #endif
  13860. return EXPECT_RESULT();
  13861. } /* END test_wc_Sm3FinalRaw */
  13862. /*
  13863. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13864. */
  13865. static int test_wc_Sm3GetSetFlags(void)
  13866. {
  13867. EXPECT_DECLS;
  13868. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13869. wc_Sm3 sm3;
  13870. wc_Sm3 sm3Copy;
  13871. word32 flags = 0;
  13872. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13873. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13874. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13875. ExpectIntEQ(flags, 0);
  13876. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13877. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13878. ExpectIntEQ(flags, 0);
  13879. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13880. ExpectIntEQ(flags, 0);
  13881. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13882. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13883. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13884. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13885. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13886. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13887. wc_Sm3Free(&sm3Copy);
  13888. wc_Sm3Free(&sm3);
  13889. #endif
  13890. return EXPECT_RESULT();
  13891. } /* END test_wc_Sm3Update */
  13892. /*
  13893. * Testing wc_Sm3Hash()
  13894. */
  13895. static int test_wc_Sm3Hash(void)
  13896. {
  13897. EXPECT_DECLS;
  13898. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13899. byte data[WC_SM3_BLOCK_SIZE];
  13900. byte hash[WC_SM3_DIGEST_SIZE];
  13901. /* Invalid parameters. */
  13902. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13903. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13904. /* Valid parameters. */
  13905. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13906. #endif
  13907. return EXPECT_RESULT();
  13908. } /* END test_wc_Sm3Hash */
  13909. /*
  13910. * Test function for wc_HmacSetKey
  13911. */
  13912. static int test_wc_Md5HmacSetKey(void)
  13913. {
  13914. EXPECT_DECLS;
  13915. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13916. Hmac hmac;
  13917. int ret, times, itr;
  13918. const char* keys[]=
  13919. {
  13920. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13921. #ifndef HAVE_FIPS
  13922. "Jefe", /* smaller than minimum FIPS key size */
  13923. #endif
  13924. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13925. };
  13926. times = sizeof(keys) / sizeof(char*);
  13927. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13928. for (itr = 0; itr < times; itr++) {
  13929. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13930. (word32)XSTRLEN(keys[itr]));
  13931. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13932. wc_HmacFree(&hmac);
  13933. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13934. #else
  13935. ExpectIntEQ(ret, 0);
  13936. #endif
  13937. }
  13938. /* Bad args. */
  13939. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13940. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13941. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13942. BAD_FUNC_ARG);
  13943. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13944. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13945. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13946. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13947. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13948. #elif defined(HAVE_FIPS)
  13949. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13950. #else
  13951. ExpectIntEQ(ret, 0);
  13952. #endif
  13953. wc_HmacFree(&hmac);
  13954. #endif
  13955. return EXPECT_RESULT();
  13956. } /* END test_wc_Md5HmacSetKey */
  13957. /*
  13958. * testing wc_HmacSetKey() on wc_Sha hash.
  13959. */
  13960. static int test_wc_ShaHmacSetKey(void)
  13961. {
  13962. EXPECT_DECLS;
  13963. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13964. Hmac hmac;
  13965. int ret, times, itr;
  13966. const char* keys[]=
  13967. {
  13968. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13969. "\x0b\x0b\x0b",
  13970. #ifndef HAVE_FIPS
  13971. "Jefe", /* smaller than minimum FIPS key size */
  13972. #endif
  13973. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13974. "\xAA\xAA\xAA"
  13975. };
  13976. times = sizeof(keys) / sizeof(char*);
  13977. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13978. for (itr = 0; itr < times; itr++) {
  13979. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13980. (word32)XSTRLEN(keys[itr])), 0);
  13981. }
  13982. /* Bad args. */
  13983. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13984. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13985. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13986. BAD_FUNC_ARG);
  13987. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13988. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13989. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13990. #ifdef HAVE_FIPS
  13991. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13992. #else
  13993. ExpectIntEQ(ret, 0);
  13994. #endif
  13995. wc_HmacFree(&hmac);
  13996. #endif
  13997. return EXPECT_RESULT();
  13998. } /* END test_wc_ShaHmacSetKey() */
  13999. /*
  14000. * testing wc_HmacSetKey() on Sha224 hash.
  14001. */
  14002. static int test_wc_Sha224HmacSetKey(void)
  14003. {
  14004. EXPECT_DECLS;
  14005. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14006. Hmac hmac;
  14007. int ret, times, itr;
  14008. const char* keys[]=
  14009. {
  14010. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14011. "\x0b\x0b\x0b",
  14012. #ifndef HAVE_FIPS
  14013. "Jefe", /* smaller than minimum FIPS key size */
  14014. #endif
  14015. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14016. "\xAA\xAA\xAA"
  14017. };
  14018. times = sizeof(keys) / sizeof(char*);
  14019. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14020. for (itr = 0; itr < times; itr++) {
  14021. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  14022. (word32)XSTRLEN(keys[itr])), 0);
  14023. }
  14024. /* Bad args. */
  14025. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  14026. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14027. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  14028. BAD_FUNC_ARG);
  14029. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14030. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14031. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  14032. #ifdef HAVE_FIPS
  14033. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14034. #else
  14035. ExpectIntEQ(ret, 0);
  14036. #endif
  14037. wc_HmacFree(&hmac);
  14038. #endif
  14039. return EXPECT_RESULT();
  14040. } /* END test_wc_Sha224HmacSetKey() */
  14041. /*
  14042. * testing wc_HmacSetKey() on Sha256 hash
  14043. */
  14044. static int test_wc_Sha256HmacSetKey(void)
  14045. {
  14046. EXPECT_DECLS;
  14047. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14048. Hmac hmac;
  14049. int ret, times, itr;
  14050. const char* keys[]=
  14051. {
  14052. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14053. "\x0b\x0b\x0b",
  14054. #ifndef HAVE_FIPS
  14055. "Jefe", /* smaller than minimum FIPS key size */
  14056. #endif
  14057. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14058. "\xAA\xAA\xAA"
  14059. };
  14060. times = sizeof(keys) / sizeof(char*);
  14061. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14062. for (itr = 0; itr < times; itr++) {
  14063. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14064. (word32)XSTRLEN(keys[itr])), 0);
  14065. }
  14066. /* Bad args. */
  14067. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14068. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14069. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  14070. BAD_FUNC_ARG);
  14071. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14072. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14073. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14074. #ifdef HAVE_FIPS
  14075. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14076. #else
  14077. ExpectIntEQ(ret, 0);
  14078. #endif
  14079. wc_HmacFree(&hmac);
  14080. #endif
  14081. return EXPECT_RESULT();
  14082. } /* END test_wc_Sha256HmacSetKey() */
  14083. /*
  14084. * testing wc_HmacSetKey on Sha384 hash.
  14085. */
  14086. static int test_wc_Sha384HmacSetKey(void)
  14087. {
  14088. EXPECT_DECLS;
  14089. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14090. Hmac hmac;
  14091. int ret, times, itr;
  14092. const char* keys[]=
  14093. {
  14094. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14095. "\x0b\x0b\x0b",
  14096. #ifndef HAVE_FIPS
  14097. "Jefe", /* smaller than minimum FIPS key size */
  14098. #endif
  14099. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14100. "\xAA\xAA\xAA"
  14101. };
  14102. times = sizeof(keys) / sizeof(char*);
  14103. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14104. for (itr = 0; itr < times; itr++) {
  14105. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14106. (word32)XSTRLEN(keys[itr])), 0);
  14107. }
  14108. /* Bad args. */
  14109. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14110. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14111. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14112. BAD_FUNC_ARG);
  14113. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14114. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14115. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14116. #ifdef HAVE_FIPS
  14117. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14118. #else
  14119. ExpectIntEQ(ret, 0);
  14120. #endif
  14121. wc_HmacFree(&hmac);
  14122. #endif
  14123. return EXPECT_RESULT();
  14124. } /* END test_wc_Sha384HmacSetKey() */
  14125. /*
  14126. * testing wc_HmacUpdate on wc_Md5 hash.
  14127. */
  14128. static int test_wc_Md5HmacUpdate(void)
  14129. {
  14130. EXPECT_DECLS;
  14131. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14132. Hmac hmac;
  14133. testVector a, b;
  14134. #ifdef HAVE_FIPS
  14135. const char* keys =
  14136. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14137. #else
  14138. const char* keys = "Jefe";
  14139. #endif
  14140. a.input = "what do ya want for nothing?";
  14141. a.inLen = XSTRLEN(a.input);
  14142. b.input = "Hi There";
  14143. b.inLen = XSTRLEN(b.input);
  14144. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14145. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14146. (word32)XSTRLEN(keys)), 0);
  14147. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14148. /* Update Hmac. */
  14149. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14150. /* Test bad args. */
  14151. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14152. BAD_FUNC_ARG);
  14153. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14154. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14155. wc_HmacFree(&hmac);
  14156. #endif
  14157. return EXPECT_RESULT();
  14158. } /* END test_wc_Md5HmacUpdate */
  14159. /*
  14160. * testing wc_HmacUpdate on SHA hash.
  14161. */
  14162. static int test_wc_ShaHmacUpdate(void)
  14163. {
  14164. EXPECT_DECLS;
  14165. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14166. Hmac hmac;
  14167. testVector a, b;
  14168. #ifdef HAVE_FIPS
  14169. const char* keys =
  14170. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14171. #else
  14172. const char* keys = "Jefe";
  14173. #endif
  14174. a.input = "what do ya want for nothing?";
  14175. a.inLen = XSTRLEN(a.input);
  14176. b.input = "Hi There";
  14177. b.inLen = XSTRLEN(b.input);
  14178. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14179. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14180. (word32)XSTRLEN(keys)), 0);
  14181. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14182. /* Update Hmac. */
  14183. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14184. /* Test bad args. */
  14185. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14186. BAD_FUNC_ARG);
  14187. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14188. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14189. wc_HmacFree(&hmac);
  14190. #endif
  14191. return EXPECT_RESULT();
  14192. } /* END test_wc_ShaHmacUpdate */
  14193. /*
  14194. * testing wc_HmacUpdate on SHA224 hash.
  14195. */
  14196. static int test_wc_Sha224HmacUpdate(void)
  14197. {
  14198. EXPECT_DECLS;
  14199. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14200. Hmac hmac;
  14201. testVector a, b;
  14202. #ifdef HAVE_FIPS
  14203. const char* keys =
  14204. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14205. #else
  14206. const char* keys = "Jefe";
  14207. #endif
  14208. a.input = "what do ya want for nothing?";
  14209. a.inLen = XSTRLEN(a.input);
  14210. b.input = "Hi There";
  14211. b.inLen = XSTRLEN(b.input);
  14212. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14213. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14214. (word32)XSTRLEN(keys)), 0);
  14215. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14216. /* Update Hmac. */
  14217. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14218. /* Test bad args. */
  14219. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14220. BAD_FUNC_ARG);
  14221. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14222. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14223. wc_HmacFree(&hmac);
  14224. #endif
  14225. return EXPECT_RESULT();
  14226. } /* END test_wc_Sha224HmacUpdate */
  14227. /*
  14228. * testing wc_HmacUpdate on SHA256 hash.
  14229. */
  14230. static int test_wc_Sha256HmacUpdate(void)
  14231. {
  14232. EXPECT_DECLS;
  14233. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14234. Hmac hmac;
  14235. testVector a, b;
  14236. #ifdef HAVE_FIPS
  14237. const char* keys =
  14238. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14239. #else
  14240. const char* keys = "Jefe";
  14241. #endif
  14242. a.input = "what do ya want for nothing?";
  14243. a.inLen = XSTRLEN(a.input);
  14244. b.input = "Hi There";
  14245. b.inLen = XSTRLEN(b.input);
  14246. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14247. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14248. (word32)XSTRLEN(keys)), 0);
  14249. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14250. /* Update Hmac. */
  14251. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14252. /* Test bad args. */
  14253. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14254. BAD_FUNC_ARG);
  14255. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14256. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14257. wc_HmacFree(&hmac);
  14258. #endif
  14259. return EXPECT_RESULT();
  14260. } /* END test_wc_Sha256HmacUpdate */
  14261. /*
  14262. * testing wc_HmacUpdate on SHA384 hash.
  14263. */
  14264. static int test_wc_Sha384HmacUpdate(void)
  14265. {
  14266. EXPECT_DECLS;
  14267. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14268. Hmac hmac;
  14269. testVector a, b;
  14270. #ifdef HAVE_FIPS
  14271. const char* keys =
  14272. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14273. #else
  14274. const char* keys = "Jefe";
  14275. #endif
  14276. a.input = "what do ya want for nothing?";
  14277. a.inLen = XSTRLEN(a.input);
  14278. b.input = "Hi There";
  14279. b.inLen = XSTRLEN(b.input);
  14280. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14281. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14282. (word32)XSTRLEN(keys)), 0);
  14283. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14284. /* Update Hmac. */
  14285. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14286. /* Test bad args. */
  14287. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14288. BAD_FUNC_ARG);
  14289. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14290. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14291. wc_HmacFree(&hmac);
  14292. #endif
  14293. return EXPECT_RESULT();
  14294. } /* END test_wc_Sha384HmacUpdate */
  14295. /*
  14296. * Testing wc_HmacFinal() with MD5
  14297. */
  14298. static int test_wc_Md5HmacFinal(void)
  14299. {
  14300. EXPECT_DECLS;
  14301. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14302. Hmac hmac;
  14303. byte hash[WC_MD5_DIGEST_SIZE];
  14304. testVector a;
  14305. const char* key;
  14306. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14307. a.input = "Hi There";
  14308. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14309. "\x9d";
  14310. a.inLen = XSTRLEN(a.input);
  14311. a.outLen = XSTRLEN(a.output);
  14312. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14313. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14314. 0);
  14315. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14316. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14317. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14318. /* Try bad parameters. */
  14319. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14320. #ifndef HAVE_FIPS
  14321. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14322. #endif
  14323. wc_HmacFree(&hmac);
  14324. #endif
  14325. return EXPECT_RESULT();
  14326. } /* END test_wc_Md5HmacFinal */
  14327. /*
  14328. * Testing wc_HmacFinal() with SHA
  14329. */
  14330. static int test_wc_ShaHmacFinal(void)
  14331. {
  14332. EXPECT_DECLS;
  14333. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14334. Hmac hmac;
  14335. byte hash[WC_SHA_DIGEST_SIZE];
  14336. testVector a;
  14337. const char* key;
  14338. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14339. "\x0b\x0b\x0b";
  14340. a.input = "Hi There";
  14341. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14342. "\x8e\xf1\x46\xbe\x00";
  14343. a.inLen = XSTRLEN(a.input);
  14344. a.outLen = XSTRLEN(a.output);
  14345. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14346. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14347. 0);
  14348. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14349. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14350. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14351. /* Try bad parameters. */
  14352. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14353. #ifndef HAVE_FIPS
  14354. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14355. #endif
  14356. wc_HmacFree(&hmac);
  14357. #endif
  14358. return EXPECT_RESULT();
  14359. } /* END test_wc_ShaHmacFinal */
  14360. /*
  14361. * Testing wc_HmacFinal() with SHA224
  14362. */
  14363. static int test_wc_Sha224HmacFinal(void)
  14364. {
  14365. EXPECT_DECLS;
  14366. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14367. Hmac hmac;
  14368. byte hash[WC_SHA224_DIGEST_SIZE];
  14369. testVector a;
  14370. const char* key;
  14371. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14372. "\x0b\x0b\x0b";
  14373. a.input = "Hi There";
  14374. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14375. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14376. a.inLen = XSTRLEN(a.input);
  14377. a.outLen = XSTRLEN(a.output);
  14378. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14379. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14380. (word32)XSTRLEN(key)), 0);
  14381. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14382. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14383. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14384. /* Try bad parameters. */
  14385. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14386. #ifndef HAVE_FIPS
  14387. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14388. #endif
  14389. wc_HmacFree(&hmac);
  14390. #endif
  14391. return EXPECT_RESULT();
  14392. } /* END test_wc_Sha224HmacFinal */
  14393. /*
  14394. * Testing wc_HmacFinal() with SHA256
  14395. */
  14396. static int test_wc_Sha256HmacFinal(void)
  14397. {
  14398. EXPECT_DECLS;
  14399. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14400. Hmac hmac;
  14401. byte hash[WC_SHA256_DIGEST_SIZE];
  14402. testVector a;
  14403. const char* key;
  14404. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14405. "\x0b\x0b\x0b";
  14406. a.input = "Hi There";
  14407. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14408. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14409. "\xcf\xf7";
  14410. a.inLen = XSTRLEN(a.input);
  14411. a.outLen = XSTRLEN(a.output);
  14412. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14413. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14414. (word32)XSTRLEN(key)), 0);
  14415. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14416. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14417. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14418. /* Try bad parameters. */
  14419. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14420. #ifndef HAVE_FIPS
  14421. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14422. #endif
  14423. wc_HmacFree(&hmac);
  14424. #endif
  14425. return EXPECT_RESULT();
  14426. } /* END test_wc_Sha256HmacFinal */
  14427. /*
  14428. * Testing wc_HmacFinal() with SHA384
  14429. */
  14430. static int test_wc_Sha384HmacFinal(void)
  14431. {
  14432. EXPECT_DECLS;
  14433. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14434. Hmac hmac;
  14435. byte hash[WC_SHA384_DIGEST_SIZE];
  14436. testVector a;
  14437. const char* key;
  14438. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14439. "\x0b\x0b\x0b";
  14440. a.input = "Hi There";
  14441. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14442. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14443. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14444. "\xfa\x9c\xb6";
  14445. a.inLen = XSTRLEN(a.input);
  14446. a.outLen = XSTRLEN(a.output);
  14447. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14448. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14449. (word32)XSTRLEN(key)), 0);
  14450. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14451. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14452. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14453. /* Try bad parameters. */
  14454. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14455. #ifndef HAVE_FIPS
  14456. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14457. #endif
  14458. wc_HmacFree(&hmac);
  14459. #endif
  14460. return EXPECT_RESULT();
  14461. } /* END test_wc_Sha384HmacFinal */
  14462. /*
  14463. * Testing wc_InitCmac()
  14464. */
  14465. static int test_wc_InitCmac(void)
  14466. {
  14467. EXPECT_DECLS;
  14468. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14469. Cmac cmac1;
  14470. Cmac cmac2;
  14471. Cmac cmac3;
  14472. /* AES 128 key. */
  14473. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14474. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14475. /* AES 192 key. */
  14476. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14477. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14478. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14479. /* AES 256 key. */
  14480. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14481. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14482. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14483. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14484. word32 key1Sz = (word32)sizeof(key1) - 1;
  14485. word32 key2Sz = (word32)sizeof(key2) - 1;
  14486. word32 key3Sz = (word32)sizeof(key3) - 1;
  14487. int type = WC_CMAC_AES;
  14488. (void)key1;
  14489. (void)key1Sz;
  14490. (void)key2;
  14491. (void)key2Sz;
  14492. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14493. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14494. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14495. #ifdef WOLFSSL_AES_128
  14496. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14497. #endif
  14498. #ifdef WOLFSSL_AES_192
  14499. wc_AesFree(&cmac1.aes);
  14500. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14501. #endif
  14502. #ifdef WOLFSSL_AES_256
  14503. wc_AesFree(&cmac2.aes);
  14504. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14505. #endif
  14506. wc_AesFree(&cmac3.aes);
  14507. /* Test bad args. */
  14508. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14509. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14510. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14511. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14512. #endif
  14513. return EXPECT_RESULT();
  14514. } /* END test_wc_InitCmac */
  14515. /*
  14516. * Testing wc_CmacUpdate()
  14517. */
  14518. static int test_wc_CmacUpdate(void)
  14519. {
  14520. EXPECT_DECLS;
  14521. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14522. Cmac cmac;
  14523. byte key[] = {
  14524. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14525. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14526. };
  14527. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14528. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14529. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14530. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14531. "\xf4";
  14532. word32 inSz = (word32)sizeof(in) - 1;
  14533. word32 keySz = (word32)sizeof(key);
  14534. int type = WC_CMAC_AES;
  14535. XMEMSET(&cmac, 0, sizeof(Cmac));
  14536. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14537. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14538. /* Test bad args. */
  14539. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14540. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14541. wc_AesFree(&cmac.aes);
  14542. #endif
  14543. return EXPECT_RESULT();
  14544. } /* END test_wc_CmacUpdate */
  14545. /*
  14546. * Testing wc_CmacFinal()
  14547. */
  14548. static int test_wc_CmacFinal(void)
  14549. {
  14550. EXPECT_DECLS;
  14551. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14552. Cmac cmac;
  14553. byte key[] = {
  14554. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14555. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14556. };
  14557. byte msg[] = {
  14558. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14559. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14560. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14561. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14562. 0xf4
  14563. };
  14564. /* Test vectors from CMACGenAES128.rsp from
  14565. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14566. * Per RFC4493 truncation of lsb is possible.
  14567. */
  14568. byte expMac[] = {
  14569. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14570. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14571. };
  14572. byte mac[AES_BLOCK_SIZE];
  14573. word32 msgSz = (word32)sizeof(msg);
  14574. word32 keySz = (word32)sizeof(key);
  14575. word32 macSz = sizeof(mac);
  14576. word32 badMacSz = 17;
  14577. int expMacSz = sizeof(expMac);
  14578. int type = WC_CMAC_AES;
  14579. XMEMSET(&cmac, 0, sizeof(Cmac));
  14580. XMEMSET(mac, 0, macSz);
  14581. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14582. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14583. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14584. /* Pass in bad args. */
  14585. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14586. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14587. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14588. /* For the last call, use the API with implicit wc_CmacFree(). */
  14589. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14590. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14591. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14592. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14593. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14594. /* Pass in bad args. */
  14595. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14596. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14597. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14598. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14599. #endif
  14600. return EXPECT_RESULT();
  14601. } /* END test_wc_CmacFinal */
  14602. /*
  14603. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14604. */
  14605. static int test_wc_AesCmacGenerate(void)
  14606. {
  14607. EXPECT_DECLS;
  14608. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14609. byte key[] = {
  14610. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14611. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14612. };
  14613. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14614. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14615. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14616. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14617. byte mac[AES_BLOCK_SIZE];
  14618. word32 keySz = sizeof(key);
  14619. word32 macSz = sizeof(mac);
  14620. word32 msgSz = sizeof(msg) - 1;
  14621. word32 expMacSz = sizeof(expMac) - 1;
  14622. XMEMSET(mac, 0, macSz);
  14623. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14624. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14625. /* Pass in bad args. */
  14626. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14627. BAD_FUNC_ARG);
  14628. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14629. BAD_FUNC_ARG);
  14630. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14631. BAD_FUNC_ARG);
  14632. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14633. BAD_FUNC_ARG);
  14634. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14635. /* Test bad args. */
  14636. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14637. BAD_FUNC_ARG);
  14638. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14639. BAD_FUNC_ARG);
  14640. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14641. BAD_FUNC_ARG);
  14642. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14643. BAD_FUNC_ARG);
  14644. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14645. BAD_FUNC_ARG);
  14646. #endif
  14647. return EXPECT_RESULT();
  14648. } /* END test_wc_AesCmacGenerate */
  14649. /*
  14650. * Testing streaming AES-GCM API.
  14651. */
  14652. static int test_wc_AesGcmStream(void)
  14653. {
  14654. EXPECT_DECLS;
  14655. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14656. defined(WOLFSSL_AESGCM_STREAM)
  14657. int i;
  14658. WC_RNG rng[1];
  14659. Aes aesEnc[1];
  14660. Aes aesDec[1];
  14661. byte tag[AES_BLOCK_SIZE];
  14662. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14663. byte out[AES_BLOCK_SIZE * 3 + 2];
  14664. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14665. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14666. byte key[AES_128_KEY_SIZE] = { 0, };
  14667. byte iv[AES_IV_SIZE] = { 1, };
  14668. byte ivOut[AES_IV_SIZE];
  14669. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14670. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14671. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14672. };
  14673. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14674. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14675. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14676. };
  14677. static const byte expTag[AES_BLOCK_SIZE] = {
  14678. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14679. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14680. };
  14681. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14682. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14683. XMEMSET(&aesDec, 0, sizeof(Aes));
  14684. /* Create a random for generating IV/nonce. */
  14685. ExpectIntEQ(wc_InitRng(rng), 0);
  14686. /* Initialize data structures. */
  14687. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14688. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14689. /* BadParameters to streaming init. */
  14690. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14691. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14692. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14693. BAD_FUNC_ARG);
  14694. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14695. BAD_FUNC_ARG);
  14696. /* Bad parameters to encrypt update. */
  14697. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14698. BAD_FUNC_ARG);
  14699. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14700. BAD_FUNC_ARG);
  14701. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14702. BAD_FUNC_ARG);
  14703. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14704. BAD_FUNC_ARG);
  14705. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14706. BAD_FUNC_ARG);
  14707. /* Bad parameters to decrypt update. */
  14708. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14709. BAD_FUNC_ARG);
  14710. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14711. BAD_FUNC_ARG);
  14712. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14713. BAD_FUNC_ARG);
  14714. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14715. BAD_FUNC_ARG);
  14716. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14717. BAD_FUNC_ARG);
  14718. /* Bad parameters to encrypt final. */
  14719. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14720. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14721. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14722. BAD_FUNC_ARG);
  14723. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14724. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14725. BAD_FUNC_ARG);
  14726. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14727. BAD_FUNC_ARG);
  14728. /* Bad parameters to decrypt final. */
  14729. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14730. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14731. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14732. BAD_FUNC_ARG);
  14733. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14734. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14735. BAD_FUNC_ARG);
  14736. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14737. BAD_FUNC_ARG);
  14738. /* Check calling final before setting key fails. */
  14739. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14740. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14741. /* Check calling update before setting key else fails. */
  14742. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14743. MISSING_KEY);
  14744. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14745. MISSING_KEY);
  14746. /* Set key but not IV. */
  14747. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14748. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14749. /* Check calling final before setting IV fails. */
  14750. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14751. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14752. /* Check calling update before setting IV else fails. */
  14753. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14754. MISSING_IV);
  14755. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14756. MISSING_IV);
  14757. /* Set IV using fixed part IV and external IV APIs. */
  14758. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14759. rng), 0);
  14760. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14761. GCM_NONCE_MID_SZ), 0);
  14762. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14763. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14764. /* Encrypt and decrypt data. */
  14765. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14766. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14767. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14768. /* Finalize and check tag matches. */
  14769. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14770. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14771. /* Set key and IV through streaming init API. */
  14772. wc_AesFree(aesEnc);
  14773. wc_AesFree(aesDec);
  14774. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14775. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14776. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14777. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14778. /* Encrypt/decrypt one block and AAD of one block. */
  14779. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14780. AES_BLOCK_SIZE), 0);
  14781. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14782. AES_BLOCK_SIZE), 0);
  14783. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14784. /* Finalize and check tag matches. */
  14785. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14786. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14787. /* Set key and IV through streaming init API. */
  14788. wc_AesFree(aesEnc);
  14789. wc_AesFree(aesDec);
  14790. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14791. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14792. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14793. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14794. /* No data to encrypt/decrypt one byte of AAD. */
  14795. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14796. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14797. /* Finalize and check tag matches. */
  14798. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14799. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14800. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14801. /* Set key and IV through streaming init API. */
  14802. wc_AesFree(aesEnc);
  14803. wc_AesFree(aesDec);
  14804. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14805. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14806. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14807. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14808. /* Encrypt/decrypt one byte and no AAD. */
  14809. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14810. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14811. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14812. /* Finalize and check tag matches. */
  14813. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14814. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14815. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14816. /* Set key and IV through streaming init API. */
  14817. wc_AesFree(aesEnc);
  14818. wc_AesFree(aesDec);
  14819. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14820. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14821. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14822. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14823. /* Encryption AES is one byte at a time */
  14824. for (i = 0; i < (int)sizeof(aad); i++) {
  14825. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14826. 0);
  14827. }
  14828. for (i = 0; i < (int)sizeof(in); i++) {
  14829. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14830. 0);
  14831. }
  14832. /* Decryption AES is two bytes at a time */
  14833. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14834. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14835. 0);
  14836. }
  14837. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14838. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14839. 0), 0);
  14840. }
  14841. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14842. /* Finalize and check tag matches. */
  14843. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14844. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14845. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14846. /* Check streaming encryption can be decrypted with one shot. */
  14847. wc_AesFree(aesDec);
  14848. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14849. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14850. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14851. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14852. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14853. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14854. wc_AesFree(aesEnc);
  14855. wc_AesFree(aesDec);
  14856. wc_FreeRng(rng);
  14857. #endif
  14858. return EXPECT_RESULT();
  14859. } /* END test_wc_AesGcmStream */
  14860. /*
  14861. * Testing streaming SM4 API.
  14862. */
  14863. static int test_wc_Sm4(void)
  14864. {
  14865. int res = TEST_SKIPPED;
  14866. #ifdef WOLFSSL_SM4
  14867. EXPECT_DECLS;
  14868. wc_Sm4 sm4;
  14869. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14870. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14871. unsigned char key[SM4_KEY_SIZE];
  14872. #endif
  14873. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14874. unsigned char iv[SM4_IV_SIZE];
  14875. #endif
  14876. /* Invalid parameters - wc_Sm4Init */
  14877. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14878. /* Valid cases - wc_Sm4Init */
  14879. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14880. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14881. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14882. XMEMSET(key, 0, sizeof(key));
  14883. /* Invalid parameters - wc_Sm4SetKey. */
  14884. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14885. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14886. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14887. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14888. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14889. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14890. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14891. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14892. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14893. /* Valid cases - wc_Sm4SetKey. */
  14894. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14895. #endif
  14896. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14897. XMEMSET(iv, 0, sizeof(iv));
  14898. /* Invalid parameters - wc_Sm4SetIV. */
  14899. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14900. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14901. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14902. /* Valid cases - wc_Sm4SetIV. */
  14903. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14904. #endif
  14905. /* Valid cases - wc_Sm4Free */
  14906. wc_Sm4Free(NULL);
  14907. wc_Sm4Free(&sm4);
  14908. res = EXPECT_RESULT();
  14909. #endif
  14910. return res;
  14911. } /* END test_wc_Sm4 */
  14912. /*
  14913. * Testing block based SM4-ECB API.
  14914. */
  14915. static int test_wc_Sm4Ecb(void)
  14916. {
  14917. int res = TEST_SKIPPED;
  14918. #ifdef WOLFSSL_SM4_ECB
  14919. EXPECT_DECLS;
  14920. wc_Sm4 sm4;
  14921. unsigned char key[SM4_KEY_SIZE];
  14922. unsigned char in[SM4_BLOCK_SIZE * 2];
  14923. unsigned char out[SM4_BLOCK_SIZE * 2];
  14924. unsigned char out2[SM4_BLOCK_SIZE];
  14925. XMEMSET(key, 0, sizeof(key));
  14926. XMEMSET(in, 0, sizeof(in));
  14927. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14928. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14929. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14930. /* Tested in test_wc_Sm4. */
  14931. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14932. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14933. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14934. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14935. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14936. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14937. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14938. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14939. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14940. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14941. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14942. /* Valid cases - wc_Sm4EcbEncrypt. */
  14943. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14944. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14945. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14946. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14947. /* In and out are same pointer. */
  14948. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14949. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14950. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14951. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14952. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14953. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14954. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14955. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14956. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14957. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14958. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14959. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14960. /* Valid cases - wc_Sm4EcbDecrypt. */
  14961. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14962. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14963. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14964. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14965. /* In and out are same pointer. */
  14966. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14967. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14968. wc_Sm4Free(&sm4);
  14969. res = EXPECT_RESULT();
  14970. #endif
  14971. return res;
  14972. } /* END test_wc_Sm4Ecb */
  14973. /*
  14974. * Testing block based SM4-CBC API.
  14975. */
  14976. static int test_wc_Sm4Cbc(void)
  14977. {
  14978. int res = TEST_SKIPPED;
  14979. #ifdef WOLFSSL_SM4_CBC
  14980. EXPECT_DECLS;
  14981. wc_Sm4 sm4;
  14982. unsigned char key[SM4_KEY_SIZE];
  14983. unsigned char iv[SM4_IV_SIZE];
  14984. unsigned char in[SM4_BLOCK_SIZE * 2];
  14985. unsigned char out[SM4_BLOCK_SIZE * 2];
  14986. unsigned char out2[SM4_BLOCK_SIZE];
  14987. XMEMSET(key, 0, sizeof(key));
  14988. XMEMSET(iv, 0, sizeof(iv));
  14989. XMEMSET(in, 0, sizeof(in));
  14990. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14991. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14992. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14993. /* Tested in test_wc_Sm4. */
  14994. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14995. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14996. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14997. /* Tested in test_wc_Sm4. */
  14998. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14999. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  15000. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15001. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15002. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15003. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15004. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15005. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15006. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15007. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15008. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15009. /* Valid cases - wc_Sm4CbcEncrypt. */
  15010. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  15011. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15012. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15013. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15014. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15015. /* In and out are same pointer. */
  15016. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15017. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15018. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15019. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  15020. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15021. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15022. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15023. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15024. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15025. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15026. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15027. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15028. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15029. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15030. /* Valid cases - wc_Sm4CbcDecrypt. */
  15031. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  15032. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15033. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15034. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15035. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15036. /* In and out are same pointer. */
  15037. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15038. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15039. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15040. wc_Sm4Free(&sm4);
  15041. res = EXPECT_RESULT();
  15042. #endif
  15043. return res;
  15044. } /* END test_wc_Sm4Cbc */
  15045. /*
  15046. * Testing streaming SM4-CTR API.
  15047. */
  15048. static int test_wc_Sm4Ctr(void)
  15049. {
  15050. int res = TEST_SKIPPED;
  15051. #ifdef WOLFSSL_SM4_CTR
  15052. EXPECT_DECLS;
  15053. wc_Sm4 sm4;
  15054. unsigned char key[SM4_KEY_SIZE];
  15055. unsigned char iv[SM4_IV_SIZE];
  15056. unsigned char in[SM4_BLOCK_SIZE * 4];
  15057. unsigned char out[SM4_BLOCK_SIZE * 4];
  15058. unsigned char out2[SM4_BLOCK_SIZE * 4];
  15059. word32 chunk;
  15060. word32 i;
  15061. XMEMSET(key, 0, sizeof(key));
  15062. XMEMSET(iv, 0, sizeof(iv));
  15063. XMEMSET(in, 0, sizeof(in));
  15064. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15065. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15066. /* Tested in test_wc_Sm4. */
  15067. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15068. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  15069. /* Tested in test_wc_Sm4. */
  15070. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15071. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  15072. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15073. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  15074. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  15075. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  15076. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15077. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15078. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15079. /* Valid cases - wc_Sm4CtrEncrypt. */
  15080. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  15081. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  15082. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15083. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  15084. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  15085. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15086. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15087. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  15088. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15089. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15090. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15091. /* In and out are same pointer. Also check encrypt of cipher text produces
  15092. * plaintext.
  15093. */
  15094. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15095. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  15096. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15097. /* Chunking tests. */
  15098. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15099. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  15100. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  15101. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15102. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  15103. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15104. }
  15105. if (i < (word32)sizeof(in)) {
  15106. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15107. (word32)sizeof(in) - i), 0);
  15108. }
  15109. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15110. }
  15111. for (i = 0; i < (word32)sizeof(iv); i++) {
  15112. iv[i] = 0xff;
  15113. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15114. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15115. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15116. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15117. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15118. }
  15119. wc_Sm4Free(&sm4);
  15120. res = EXPECT_RESULT();
  15121. #endif
  15122. return res;
  15123. } /* END test_wc_Sm4Ctr */
  15124. /*
  15125. * Testing stream SM4-GCM API.
  15126. */
  15127. static int test_wc_Sm4Gcm(void)
  15128. {
  15129. int res = TEST_SKIPPED;
  15130. #ifdef WOLFSSL_SM4_GCM
  15131. EXPECT_DECLS;
  15132. wc_Sm4 sm4;
  15133. unsigned char key[SM4_KEY_SIZE];
  15134. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15135. unsigned char in[SM4_BLOCK_SIZE * 2];
  15136. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15137. unsigned char out[SM4_BLOCK_SIZE * 2];
  15138. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15139. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15140. unsigned char tag[SM4_BLOCK_SIZE];
  15141. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15142. word32 i;
  15143. XMEMSET(key, 0, sizeof(key));
  15144. XMEMSET(nonce, 0, sizeof(nonce));
  15145. XMEMSET(in, 0, sizeof(in));
  15146. XMEMSET(in2, 0, sizeof(in2));
  15147. XMEMSET(aad, 0, sizeof(aad));
  15148. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15149. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15150. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15151. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15152. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15153. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15154. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15155. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15156. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15157. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15158. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15159. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15160. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15161. /* Valid parameters - wc_Sm4GcmSetKey. */
  15162. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15163. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15164. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15165. 0), BAD_FUNC_ARG);
  15166. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15167. 0), BAD_FUNC_ARG);
  15168. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15169. 0), BAD_FUNC_ARG);
  15170. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15171. 0), BAD_FUNC_ARG);
  15172. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15173. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15174. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15175. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15176. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15177. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15178. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15179. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15180. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15181. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15182. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15183. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15184. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15185. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15186. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15187. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15188. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15189. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15190. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15191. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15192. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15193. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15194. 0), BAD_FUNC_ARG);
  15195. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15196. 0), BAD_FUNC_ARG);
  15197. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15198. 0), BAD_FUNC_ARG);
  15199. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15200. 0), BAD_FUNC_ARG);
  15201. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15202. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15203. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15204. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15205. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15206. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15207. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15208. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15209. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15210. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15211. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15212. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15213. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15214. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15215. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15216. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15217. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15218. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15219. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15220. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15221. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15222. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15223. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15224. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15225. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15226. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15227. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15228. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15229. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15230. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15231. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15232. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15233. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15234. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15235. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15236. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15237. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15238. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15239. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15240. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15241. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15242. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15243. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15244. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15245. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15246. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15247. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15248. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15249. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15250. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15251. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15252. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15253. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15254. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15255. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15256. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15257. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15258. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15259. SM4_GCM_AUTH_E);
  15260. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15261. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15262. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15263. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15264. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15265. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15266. }
  15267. /* Check different in/out sizes. */
  15268. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15269. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15270. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15271. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15272. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15273. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15274. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15275. XMEMCPY(out2, out, i - 1);
  15276. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15277. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15278. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15279. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15280. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15281. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15282. }
  15283. /* Force the counter to roll over in first byte. */
  15284. {
  15285. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15286. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15287. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15288. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15289. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15290. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15291. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15292. }
  15293. wc_Sm4Free(&sm4);
  15294. res = EXPECT_RESULT();
  15295. #endif
  15296. return res;
  15297. } /* END test_wc_Sm4Gcm */
  15298. /*
  15299. * Testing stream SM4-CCM API.
  15300. */
  15301. static int test_wc_Sm4Ccm(void)
  15302. {
  15303. int res = TEST_SKIPPED;
  15304. #ifdef WOLFSSL_SM4_CCM
  15305. EXPECT_DECLS;
  15306. wc_Sm4 sm4;
  15307. unsigned char key[SM4_KEY_SIZE];
  15308. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15309. unsigned char in[SM4_BLOCK_SIZE * 2];
  15310. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15311. unsigned char out[SM4_BLOCK_SIZE * 2];
  15312. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15313. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15314. unsigned char tag[SM4_BLOCK_SIZE];
  15315. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15316. word32 i;
  15317. XMEMSET(key, 0, sizeof(key));
  15318. XMEMSET(nonce, 0, sizeof(nonce));
  15319. XMEMSET(in, 0, sizeof(in));
  15320. XMEMSET(in2, 0, sizeof(in2));
  15321. XMEMSET(aad, 0, sizeof(aad));
  15322. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15323. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15324. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15325. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15326. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15327. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15328. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15329. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15330. 0), BAD_FUNC_ARG);
  15331. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15332. 0), BAD_FUNC_ARG);
  15333. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15334. 0), BAD_FUNC_ARG);
  15335. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15336. 0), BAD_FUNC_ARG);
  15337. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15338. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15339. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15340. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15341. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15342. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15343. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15344. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15345. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15346. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15347. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15348. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15349. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15350. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15351. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15352. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15353. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15354. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15355. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15356. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15357. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15358. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15359. 0), BAD_FUNC_ARG);
  15360. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15361. 0), BAD_FUNC_ARG);
  15362. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15363. 0), BAD_FUNC_ARG);
  15364. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15365. 0), BAD_FUNC_ARG);
  15366. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15367. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15368. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15369. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15370. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15371. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15372. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15373. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15374. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15375. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15376. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15377. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15378. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15379. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15380. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15381. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15382. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15383. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15384. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15385. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15386. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15387. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15388. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15389. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15390. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15391. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15392. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15393. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15394. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15395. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15396. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15397. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15398. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15399. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15400. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15401. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15402. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15403. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15404. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15405. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15406. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15407. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15408. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15409. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15410. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15411. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15412. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15413. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15414. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15415. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15416. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15417. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15418. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15419. }
  15420. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15421. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15422. SM4_CCM_AUTH_E);
  15423. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15424. for (i = 0; i < 4; i++) {
  15425. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15426. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15427. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15428. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15429. }
  15430. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15431. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15432. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15433. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15434. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15435. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15436. }
  15437. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15438. * Even values in range 4..SM4_BLOCK_SIZE.
  15439. */
  15440. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15441. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15442. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15443. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15444. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15445. }
  15446. /* Check different in/out sizes. */
  15447. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15448. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15449. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15450. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15451. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15452. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15453. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15454. XMEMCPY(out2, out, i - 1);
  15455. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15456. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15457. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15458. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15459. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15460. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15461. }
  15462. /* Force the counter to roll over in first byte. */
  15463. {
  15464. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15465. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15466. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15467. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15468. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15469. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15470. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15471. }
  15472. wc_Sm4Free(&sm4);
  15473. res = EXPECT_RESULT();
  15474. #endif
  15475. return res;
  15476. } /* END test_wc_Sm4Ccm */
  15477. /*
  15478. * unit test for wc_Des3_SetIV()
  15479. */
  15480. static int test_wc_Des3_SetIV(void)
  15481. {
  15482. EXPECT_DECLS;
  15483. #ifndef NO_DES3
  15484. Des3 des;
  15485. const byte key[] = {
  15486. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15487. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15488. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15489. };
  15490. const byte iv[] = {
  15491. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15492. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15493. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15494. };
  15495. XMEMSET(&des, 0, sizeof(Des3));
  15496. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15497. /* DES_ENCRYPTION or DES_DECRYPTION */
  15498. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15499. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15500. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15501. /* Test explicitly wc_Des3_SetIV() */
  15502. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15503. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15504. #endif
  15505. wc_Des3Free(&des);
  15506. #endif
  15507. return EXPECT_RESULT();
  15508. } /* END test_wc_Des3_SetIV */
  15509. /*
  15510. * unit test for wc_Des3_SetKey()
  15511. */
  15512. static int test_wc_Des3_SetKey(void)
  15513. {
  15514. EXPECT_DECLS;
  15515. #ifndef NO_DES3
  15516. Des3 des;
  15517. const byte key[] = {
  15518. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15519. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15520. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15521. };
  15522. const byte iv[] = {
  15523. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15524. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15525. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15526. };
  15527. XMEMSET(&des, 0, sizeof(Des3));
  15528. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15529. /* DES_ENCRYPTION or DES_DECRYPTION */
  15530. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15531. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15532. /* Test bad args. */
  15533. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15534. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15535. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15536. /* Default case. Should return 0. */
  15537. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15538. wc_Des3Free(&des);
  15539. #endif
  15540. return EXPECT_RESULT();
  15541. } /* END test_wc_Des3_SetKey */
  15542. /*
  15543. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15544. */
  15545. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15546. {
  15547. EXPECT_DECLS;
  15548. #ifndef NO_DES3
  15549. Des3 des;
  15550. byte cipher[24];
  15551. byte plain[24];
  15552. const byte key[] = {
  15553. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15554. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15555. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15556. };
  15557. const byte iv[] = {
  15558. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15559. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15560. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15561. };
  15562. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15563. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15564. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15565. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15566. };
  15567. XMEMSET(&des, 0, sizeof(Des3));
  15568. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15569. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15570. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15571. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15572. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15573. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15574. /* Pass in bad args. */
  15575. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15576. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15577. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15578. BAD_FUNC_ARG);
  15579. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15580. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15581. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15582. wc_Des3Free(&des);
  15583. #endif
  15584. return EXPECT_RESULT();
  15585. } /* END wc_Des3_CbcEncrypt */
  15586. /*
  15587. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15588. */
  15589. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15590. {
  15591. EXPECT_DECLS;
  15592. #ifndef NO_DES3
  15593. word32 vectorSz, cipherSz;
  15594. byte cipher[24];
  15595. byte plain[24];
  15596. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15597. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15598. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15599. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15600. };
  15601. byte key[] = {
  15602. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15603. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15604. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15605. };
  15606. byte iv[] = {
  15607. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15608. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15609. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15610. };
  15611. vectorSz = sizeof(byte) * 24;
  15612. cipherSz = sizeof(byte) * 24;
  15613. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15614. 0);
  15615. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15616. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15617. /* pass in bad args. */
  15618. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15619. BAD_FUNC_ARG);
  15620. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15621. BAD_FUNC_ARG);
  15622. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15623. BAD_FUNC_ARG);
  15624. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15625. 0);
  15626. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15627. BAD_FUNC_ARG);
  15628. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15629. BAD_FUNC_ARG);
  15630. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15631. BAD_FUNC_ARG);
  15632. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15633. 0);
  15634. #endif
  15635. return EXPECT_RESULT();
  15636. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15637. /*
  15638. * Unit test for wc_Des3_EcbEncrypt
  15639. */
  15640. static int test_wc_Des3_EcbEncrypt(void)
  15641. {
  15642. EXPECT_DECLS;
  15643. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15644. Des3 des;
  15645. byte cipher[24];
  15646. word32 cipherSz = sizeof(cipher);
  15647. const byte key[] = {
  15648. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15649. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15650. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15651. };
  15652. const byte iv[] = {
  15653. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15654. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15655. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15656. };
  15657. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15658. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15659. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15660. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15661. };
  15662. XMEMSET(&des, 0, sizeof(Des3));
  15663. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15664. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15665. /* Bad Cases */
  15666. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15667. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15668. BAD_FUNC_ARG);
  15669. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15670. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15671. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15672. /* Good Cases */
  15673. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15674. wc_Des3Free(&des);
  15675. #endif
  15676. return EXPECT_RESULT();
  15677. } /* END test_wc_Des3_EcbEncrypt */
  15678. /*
  15679. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15680. */
  15681. static int test_wc_Chacha_SetKey(void)
  15682. {
  15683. EXPECT_DECLS;
  15684. #ifdef HAVE_CHACHA
  15685. ChaCha ctx;
  15686. const byte key[] = {
  15687. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15688. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15689. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15690. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15691. };
  15692. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15693. byte cipher[128];
  15694. XMEMSET(cipher, 0, sizeof(cipher));
  15695. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15696. /* Test bad args. */
  15697. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15698. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15699. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15700. /* Test bad args. */
  15701. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15702. #endif
  15703. return EXPECT_RESULT();
  15704. } /* END test_wc_Chacha_SetKey */
  15705. /*
  15706. * unit test for wc_Poly1305SetKey()
  15707. */
  15708. static int test_wc_Poly1305SetKey(void)
  15709. {
  15710. EXPECT_DECLS;
  15711. #ifdef HAVE_POLY1305
  15712. Poly1305 ctx;
  15713. const byte key[] =
  15714. {
  15715. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15718. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15719. };
  15720. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15721. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15722. /* Test bad args. */
  15723. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15724. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15725. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15726. #endif
  15727. return EXPECT_RESULT();
  15728. } /* END test_wc_Poly1305_SetKey() */
  15729. /*
  15730. * Testing wc_Chacha_Process()
  15731. */
  15732. static int test_wc_Chacha_Process(void)
  15733. {
  15734. EXPECT_DECLS;
  15735. #ifdef HAVE_CHACHA
  15736. ChaCha enc, dec;
  15737. byte cipher[128];
  15738. byte plain[128];
  15739. const byte key[] =
  15740. {
  15741. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15742. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15743. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15744. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15745. };
  15746. const char* input = "Everybody gets Friday off.";
  15747. word32 keySz = sizeof(key)/sizeof(byte);
  15748. unsigned long int inlen = XSTRLEN(input);
  15749. /* Initialize stack variables. */
  15750. XMEMSET(cipher, 0, 128);
  15751. XMEMSET(plain, 0, 128);
  15752. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15753. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15754. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15755. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15756. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15757. 0);
  15758. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15759. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15760. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15761. /* test checking and using leftovers, currently just in C code */
  15762. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15763. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15764. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15765. (word32)inlen - 2), 0);
  15766. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15767. (byte*)input + (inlen - 2), 2), 0);
  15768. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15769. (word32)inlen - 2), 0);
  15770. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15771. (byte*)input + (inlen - 2), 2), 0);
  15772. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15773. /* check edge cases with counter increment */
  15774. {
  15775. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15776. const byte expected[] = {
  15777. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15778. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15779. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15780. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15781. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15782. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15783. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15784. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15785. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15786. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15787. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15788. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15789. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15790. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15791. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15792. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15793. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15794. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15795. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15796. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15797. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15798. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15799. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15800. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15801. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15802. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15803. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15804. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15805. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15806. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15807. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15808. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15809. };
  15810. const byte iv2[] = {
  15811. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15812. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15813. };
  15814. byte input2[256];
  15815. int i;
  15816. for (i = 0; i < 256; i++)
  15817. input2[i] = i;
  15818. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15819. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15820. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15821. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15822. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15823. /* partial */
  15824. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15825. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15826. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15827. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15828. }
  15829. #endif
  15830. /* Test bad args. */
  15831. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15832. BAD_FUNC_ARG);
  15833. #endif
  15834. return EXPECT_RESULT();
  15835. } /* END test_wc_Chacha_Process */
  15836. /*
  15837. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15838. */
  15839. static int test_wc_ChaCha20Poly1305_aead(void)
  15840. {
  15841. EXPECT_DECLS;
  15842. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15843. const byte key[] = {
  15844. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15845. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15846. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15847. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15848. };
  15849. const byte plaintext[] = {
  15850. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15851. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15852. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15853. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15854. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15855. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15856. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15857. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15858. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15859. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15860. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15861. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15862. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15863. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15864. 0x74, 0x2e
  15865. };
  15866. const byte iv[] = {
  15867. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15868. 0x44, 0x45, 0x46, 0x47
  15869. };
  15870. const byte aad[] = { /* additional data */
  15871. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15872. 0xc4, 0xc5, 0xc6, 0xc7
  15873. };
  15874. const byte cipher[] = { /* expected output from operation */
  15875. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15876. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15877. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15878. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15879. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15880. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15881. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15882. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15883. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15884. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15885. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15886. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15887. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15888. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15889. 0x61, 0x16
  15890. };
  15891. const byte authTag[] = { /* expected output from operation */
  15892. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15893. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15894. };
  15895. byte generatedCiphertext[272];
  15896. byte generatedPlaintext[272];
  15897. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15898. /* Initialize stack variables. */
  15899. XMEMSET(generatedCiphertext, 0, 272);
  15900. XMEMSET(generatedPlaintext, 0, 272);
  15901. /* Test Encrypt */
  15902. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15903. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15904. 0);
  15905. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15906. sizeof(cipher)/sizeof(byte)), 0);
  15907. /* Test bad args. */
  15908. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15909. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15910. BAD_FUNC_ARG);
  15911. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15912. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15913. BAD_FUNC_ARG);
  15914. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15915. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15916. BAD_FUNC_ARG);
  15917. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15918. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15919. BAD_FUNC_ARG);
  15920. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15921. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15922. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15923. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15924. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15925. sizeof(cipher), authTag, generatedPlaintext), 0);
  15926. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15927. sizeof(plaintext)/sizeof(byte)), 0);
  15928. /* Test bad args. */
  15929. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15930. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15931. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15932. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15933. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15934. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15935. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15936. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15937. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15938. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15939. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15940. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15941. #endif
  15942. return EXPECT_RESULT();
  15943. } /* END test_wc_ChaCha20Poly1305_aead */
  15944. /*
  15945. * Testing function for wc_Rc2SetKey().
  15946. */
  15947. static int test_wc_Rc2SetKey(void)
  15948. {
  15949. EXPECT_DECLS;
  15950. #ifdef WC_RC2
  15951. Rc2 rc2;
  15952. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15953. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15954. /* valid key and IV */
  15955. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15956. iv, 40), 0);
  15957. /* valid key, no IV */
  15958. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15959. NULL, 40), 0);
  15960. /* bad arguments */
  15961. /* null Rc2 struct */
  15962. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15963. iv, 40), BAD_FUNC_ARG);
  15964. /* null key */
  15965. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15966. iv, 40), BAD_FUNC_ARG);
  15967. /* key size == 0 */
  15968. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15969. /* key size > 128 */
  15970. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15971. /* effective bits == 0 */
  15972. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15973. iv, 0), WC_KEY_SIZE_E);
  15974. /* effective bits > 1024 */
  15975. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15976. iv, 1025), WC_KEY_SIZE_E);
  15977. #endif
  15978. return EXPECT_RESULT();
  15979. } /* END test_wc_Rc2SetKey */
  15980. /*
  15981. * Testing function for wc_Rc2SetIV().
  15982. */
  15983. static int test_wc_Rc2SetIV(void)
  15984. {
  15985. EXPECT_DECLS;
  15986. #ifdef WC_RC2
  15987. Rc2 rc2;
  15988. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15989. /* valid IV */
  15990. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15991. /* valid NULL IV */
  15992. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15993. /* bad arguments */
  15994. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15995. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15996. #endif
  15997. return EXPECT_RESULT();
  15998. } /* END test_wc_Rc2SetIV */
  15999. /*
  16000. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  16001. */
  16002. static int test_wc_Rc2EcbEncryptDecrypt(void)
  16003. {
  16004. EXPECT_DECLS;
  16005. #ifdef WC_RC2
  16006. Rc2 rc2;
  16007. int effectiveKeyBits = 63;
  16008. byte cipher[RC2_BLOCK_SIZE];
  16009. byte plain[RC2_BLOCK_SIZE];
  16010. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16011. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16012. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  16013. XMEMSET(cipher, 0, sizeof(cipher));
  16014. XMEMSET(plain, 0, sizeof(plain));
  16015. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16016. NULL, effectiveKeyBits), 0);
  16017. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  16018. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  16019. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  16020. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  16021. /* Rc2EcbEncrypt bad arguments */
  16022. /* null Rc2 struct */
  16023. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  16024. BAD_FUNC_ARG);
  16025. /* null out buffer */
  16026. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  16027. BAD_FUNC_ARG);
  16028. /* null input buffer */
  16029. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  16030. BAD_FUNC_ARG);
  16031. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16032. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  16033. /* Rc2EcbDecrypt bad arguments */
  16034. /* null Rc2 struct */
  16035. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  16036. BAD_FUNC_ARG);
  16037. /* null out buffer */
  16038. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  16039. BAD_FUNC_ARG);
  16040. /* null input buffer */
  16041. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  16042. BAD_FUNC_ARG);
  16043. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16044. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  16045. #endif
  16046. return EXPECT_RESULT();
  16047. } /* END test_wc_Rc2EcbEncryptDecrypt */
  16048. /*
  16049. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  16050. */
  16051. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16052. {
  16053. EXPECT_DECLS;
  16054. #ifdef WC_RC2
  16055. Rc2 rc2;
  16056. int effectiveKeyBits = 63;
  16057. byte cipher[RC2_BLOCK_SIZE*2];
  16058. byte plain[RC2_BLOCK_SIZE*2];
  16059. /* vector taken from test.c */
  16060. byte key[] = {
  16061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16062. };
  16063. byte iv[] = {
  16064. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16065. };
  16066. byte input[] = {
  16067. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16068. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16069. };
  16070. byte output[] = {
  16071. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16072. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16073. };
  16074. XMEMSET(cipher, 0, sizeof(cipher));
  16075. XMEMSET(plain, 0, sizeof(plain));
  16076. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16077. iv, effectiveKeyBits), 0);
  16078. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  16079. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  16080. /* reset IV for decrypt */
  16081. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16082. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  16083. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  16084. /* Rc2CbcEncrypt bad arguments */
  16085. /* null Rc2 struct */
  16086. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  16087. BAD_FUNC_ARG);
  16088. /* null out buffer */
  16089. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  16090. BAD_FUNC_ARG);
  16091. /* null input buffer */
  16092. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  16093. BAD_FUNC_ARG);
  16094. /* Rc2CbcDecrypt bad arguments */
  16095. /* in size is 0 */
  16096. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  16097. /* null Rc2 struct */
  16098. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  16099. BAD_FUNC_ARG);
  16100. /* null out buffer */
  16101. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  16102. BAD_FUNC_ARG);
  16103. /* null input buffer */
  16104. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16105. BAD_FUNC_ARG);
  16106. #endif
  16107. return EXPECT_RESULT();
  16108. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16109. /*
  16110. * Testing function for wc_AesSetIV
  16111. */
  16112. static int test_wc_AesSetIV(void)
  16113. {
  16114. int res = TEST_SKIPPED;
  16115. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16116. Aes aes;
  16117. int ret = 0;
  16118. byte key16[] =
  16119. {
  16120. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16121. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16122. };
  16123. byte iv1[] = "1234567890abcdef";
  16124. byte iv2[] = "0987654321fedcba";
  16125. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16126. if (ret != 0)
  16127. return ret;
  16128. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16129. iv1, AES_ENCRYPTION);
  16130. if (ret == 0) {
  16131. ret = wc_AesSetIV(&aes, iv2);
  16132. }
  16133. /* Test bad args. */
  16134. if (ret == 0) {
  16135. ret = wc_AesSetIV(NULL, iv1);
  16136. if (ret == BAD_FUNC_ARG) {
  16137. /* NULL iv should return 0. */
  16138. ret = wc_AesSetIV(&aes, NULL);
  16139. }
  16140. else {
  16141. ret = WOLFSSL_FATAL_ERROR;
  16142. }
  16143. }
  16144. wc_AesFree(&aes);
  16145. res = TEST_RES_CHECK(ret == 0);
  16146. #endif
  16147. return res;
  16148. } /* test_wc_AesSetIV */
  16149. /*
  16150. * Testing function for wc_AesSetKey().
  16151. */
  16152. static int test_wc_AesSetKey(void)
  16153. {
  16154. EXPECT_DECLS;
  16155. #ifndef NO_AES
  16156. Aes aes;
  16157. byte key16[] = {
  16158. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16159. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16160. };
  16161. #ifdef WOLFSSL_AES_192
  16162. byte key24[] = {
  16163. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16164. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16165. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16166. };
  16167. #endif
  16168. #ifdef WOLFSSL_AES_256
  16169. byte key32[] = {
  16170. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16171. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16172. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16173. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16174. };
  16175. #endif
  16176. byte badKey16[] = {
  16177. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16178. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16179. };
  16180. byte iv[] = "1234567890abcdef";
  16181. XMEMSET(&aes, 0, sizeof(Aes));
  16182. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16183. #ifdef WOLFSSL_AES_128
  16184. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16185. iv, AES_ENCRYPTION), 0);
  16186. #endif
  16187. #ifdef WOLFSSL_AES_192
  16188. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16189. iv, AES_ENCRYPTION), 0);
  16190. #endif
  16191. #ifdef WOLFSSL_AES_256
  16192. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16193. iv, AES_ENCRYPTION), 0);
  16194. #endif
  16195. /* Pass in bad args. */
  16196. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16197. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16198. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16199. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16200. BAD_FUNC_ARG);
  16201. wc_AesFree(&aes);
  16202. #endif
  16203. return EXPECT_RESULT();
  16204. } /* END test_wc_AesSetKey */
  16205. /*
  16206. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16207. * and wc_AesCbcDecryptWithKey()
  16208. */
  16209. static int test_wc_AesCbcEncryptDecrypt(void)
  16210. {
  16211. EXPECT_DECLS;
  16212. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16213. defined(WOLFSSL_AES_256)
  16214. Aes aes;
  16215. byte key32[] = {
  16216. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16217. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16218. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16219. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16220. };
  16221. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16222. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16223. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16224. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16225. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16226. };
  16227. byte iv[] = "1234567890abcdef";
  16228. byte enc[sizeof(vector)];
  16229. byte dec[sizeof(vector)];
  16230. byte dec2[sizeof(vector)];
  16231. /* Init stack variables. */
  16232. XMEMSET(&aes, 0, sizeof(Aes));
  16233. XMEMSET(enc, 0, sizeof(enc));
  16234. XMEMSET(dec, 0, sizeof(vector));
  16235. XMEMSET(dec2, 0, sizeof(vector));
  16236. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16237. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16238. AES_ENCRYPTION), 0);
  16239. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16240. /* Re init for decrypt and set flag. */
  16241. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16242. AES_DECRYPTION), 0);
  16243. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16244. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16245. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16246. sizeof(key32)/sizeof(byte), iv), 0);
  16247. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16248. /* Pass in bad args */
  16249. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16250. BAD_FUNC_ARG);
  16251. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16252. BAD_FUNC_ARG);
  16253. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16254. BAD_FUNC_ARG);
  16255. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16256. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16257. BAD_LENGTH_E);
  16258. #endif
  16259. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16260. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16261. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16262. "mode (v2), skip test");
  16263. #else
  16264. /* Test passing in size of 0 */
  16265. XMEMSET(enc, 0, sizeof(enc));
  16266. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16267. /* Check enc was not modified */
  16268. {
  16269. int i;
  16270. for (i = 0; i < (int)sizeof(enc); i++)
  16271. ExpectIntEQ(enc[i], 0);
  16272. }
  16273. #endif
  16274. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16275. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16276. BAD_FUNC_ARG);
  16277. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16278. BAD_FUNC_ARG);
  16279. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16280. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16281. BAD_LENGTH_E);
  16282. #else
  16283. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16284. BAD_FUNC_ARG);
  16285. #endif
  16286. /* Test passing in size of 0 */
  16287. XMEMSET(dec, 0, sizeof(dec));
  16288. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16289. /* Check dec was not modified */
  16290. {
  16291. int i;
  16292. for (i = 0; i < (int)sizeof(dec); i++)
  16293. ExpectIntEQ(dec[i], 0);
  16294. }
  16295. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16296. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16297. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16298. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16299. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16300. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16301. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16302. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16303. wc_AesFree(&aes);
  16304. #endif
  16305. return EXPECT_RESULT();
  16306. } /* END test_wc_AesCbcEncryptDecrypt */
  16307. /*
  16308. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16309. */
  16310. static int test_wc_AesCtrEncryptDecrypt(void)
  16311. {
  16312. EXPECT_DECLS;
  16313. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16314. Aes aesEnc;
  16315. Aes aesDec;
  16316. byte key32[] = {
  16317. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16318. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16319. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16320. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16321. };
  16322. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16323. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16324. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16325. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16326. };
  16327. byte iv[] = "1234567890abcdef";
  16328. byte enc[AES_BLOCK_SIZE * 2];
  16329. byte dec[AES_BLOCK_SIZE * 2];
  16330. /* Init stack variables. */
  16331. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16332. XMEMSET(&aesDec, 0, sizeof(Aes));
  16333. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16334. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16335. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16336. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16337. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16338. AES_ENCRYPTION), 0);
  16339. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16340. sizeof(vector)/sizeof(byte)), 0);
  16341. /* Decrypt with wc_AesCtrEncrypt() */
  16342. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16343. AES_ENCRYPTION), 0);
  16344. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16345. 0);
  16346. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16347. /* Test bad args. */
  16348. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16349. BAD_FUNC_ARG);
  16350. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16351. BAD_FUNC_ARG);
  16352. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16353. BAD_FUNC_ARG);
  16354. wc_AesFree(&aesEnc);
  16355. wc_AesFree(&aesDec);
  16356. #endif
  16357. return EXPECT_RESULT();
  16358. } /* END test_wc_AesCtrEncryptDecrypt */
  16359. /*
  16360. * test function for wc_AesGcmSetKey()
  16361. */
  16362. static int test_wc_AesGcmSetKey(void)
  16363. {
  16364. EXPECT_DECLS;
  16365. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16366. Aes aes;
  16367. #ifdef WOLFSSL_AES_128
  16368. byte key16[] = {
  16369. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16370. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16371. };
  16372. #endif
  16373. #ifdef WOLFSSL_AES_192
  16374. byte key24[] = {
  16375. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16376. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16377. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16378. };
  16379. #endif
  16380. #ifdef WOLFSSL_AES_256
  16381. byte key32[] = {
  16382. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16383. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16384. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16385. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16386. };
  16387. #endif
  16388. byte badKey16[] = {
  16389. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16390. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16391. };
  16392. byte badKey24[] = {
  16393. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16394. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16395. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16396. };
  16397. byte badKey32[] = {
  16398. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16399. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16400. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16401. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16402. };
  16403. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16404. #ifdef WOLFSSL_AES_128
  16405. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16406. #endif
  16407. #ifdef WOLFSSL_AES_192
  16408. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16409. #endif
  16410. #ifdef WOLFSSL_AES_256
  16411. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16412. #endif
  16413. /* Pass in bad args. */
  16414. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16415. BAD_FUNC_ARG);
  16416. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16417. BAD_FUNC_ARG);
  16418. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16419. BAD_FUNC_ARG);
  16420. wc_AesFree(&aes);
  16421. #endif
  16422. return EXPECT_RESULT();
  16423. } /* END test_wc_AesGcmSetKey */
  16424. /*
  16425. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16426. */
  16427. static int test_wc_AesGcmEncryptDecrypt(void)
  16428. {
  16429. EXPECT_DECLS;
  16430. /* WOLFSSL_AFALG requires 12 byte IV */
  16431. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16432. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16433. Aes aes;
  16434. byte key32[] = {
  16435. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16436. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16437. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16438. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16439. };
  16440. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16441. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16442. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16443. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16444. };
  16445. const byte a[] = {
  16446. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16447. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16448. 0xab, 0xad, 0xda, 0xd2
  16449. };
  16450. byte iv[] = "1234567890a";
  16451. byte longIV[] = "1234567890abcdefghij";
  16452. byte enc[sizeof(vector)];
  16453. byte resultT[AES_BLOCK_SIZE];
  16454. byte dec[sizeof(vector)];
  16455. /* Init stack variables. */
  16456. XMEMSET(&aes, 0, sizeof(Aes));
  16457. XMEMSET(enc, 0, sizeof(vector));
  16458. XMEMSET(dec, 0, sizeof(vector));
  16459. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16460. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16461. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16462. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16463. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16464. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16465. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16466. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16467. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16468. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16469. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16470. BAD_FUNC_ARG);
  16471. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16472. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16473. BAD_FUNC_ARG);
  16474. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16475. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16476. BAD_FUNC_ARG);
  16477. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16478. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16479. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16480. /* FIPS does not check the lower bound of ivSz */
  16481. #else
  16482. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16483. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16484. #endif
  16485. /* This case is now considered good. Long IVs are now allowed.
  16486. * Except for the original FIPS release, it still has an upper
  16487. * bound on the IV length. */
  16488. #if (!defined(HAVE_FIPS) || \
  16489. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16490. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16491. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16492. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16493. 0);
  16494. #else
  16495. (void)longIV;
  16496. #endif /* Old FIPS */
  16497. /* END wc_AesGcmEncrypt */
  16498. #ifdef HAVE_AES_DECRYPT
  16499. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16500. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16501. BAD_FUNC_ARG);
  16502. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16503. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16504. BAD_FUNC_ARG);
  16505. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16506. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16507. BAD_FUNC_ARG);
  16508. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16509. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16510. BAD_FUNC_ARG);
  16511. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16512. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16513. BAD_FUNC_ARG);
  16514. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16515. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16516. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16517. AES_GCM_AUTH_E);
  16518. #else
  16519. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16520. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16521. BAD_FUNC_ARG);
  16522. #endif
  16523. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16524. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16525. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16526. /* FIPS does not check the lower bound of ivSz */
  16527. #else
  16528. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16529. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16530. #endif
  16531. #endif /* HAVE_AES_DECRYPT */
  16532. wc_AesFree(&aes);
  16533. #endif
  16534. return EXPECT_RESULT();
  16535. } /* END test_wc_AesGcmEncryptDecrypt */
  16536. /*
  16537. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16538. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16539. */
  16540. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16541. {
  16542. EXPECT_DECLS;
  16543. #if (!defined(HAVE_FIPS) || \
  16544. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16545. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16546. const byte key[] = {
  16547. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16548. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16549. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16550. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16551. };
  16552. const byte in[] = {
  16553. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16554. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16555. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16556. };
  16557. const byte aad[] = {
  16558. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16559. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16560. 0xab, 0xad, 0xda, 0xd2
  16561. };
  16562. Aes aesEnc;
  16563. Aes aesDec;
  16564. byte iv[] = "1234567890abcdefghij";
  16565. byte out[sizeof(in)];
  16566. byte plain[sizeof(in)];
  16567. byte tag[AES_BLOCK_SIZE];
  16568. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16569. XMEMSET(&aesDec, 0, sizeof(Aes));
  16570. XMEMSET(out, 0, sizeof(out));
  16571. XMEMSET(plain, 0, sizeof(plain));
  16572. XMEMSET(tag, 0, sizeof(tag));
  16573. /* Perform one-shot encryption using long IV */
  16574. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16575. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16576. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16577. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16578. /* Perform streaming decryption using long IV */
  16579. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16580. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16581. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16582. sizeof(aad)), 0);
  16583. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16584. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16585. /* Free resources */
  16586. wc_AesFree(&aesEnc);
  16587. wc_AesFree(&aesDec);
  16588. #endif
  16589. return EXPECT_RESULT();
  16590. } /* END wc_AesGcmMixedEncDecLongIV */
  16591. /*
  16592. * unit test for wc_GmacSetKey()
  16593. */
  16594. static int test_wc_GmacSetKey(void)
  16595. {
  16596. EXPECT_DECLS;
  16597. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16598. Gmac gmac;
  16599. byte key16[] = {
  16600. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16601. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16602. };
  16603. #ifdef WOLFSSL_AES_192
  16604. byte key24[] = {
  16605. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16606. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16607. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16608. };
  16609. #endif
  16610. #ifdef WOLFSSL_AES_256
  16611. byte key32[] = {
  16612. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16613. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16614. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16615. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16616. };
  16617. #endif
  16618. byte badKey16[] = {
  16619. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16620. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16621. };
  16622. byte badKey24[] = {
  16623. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16624. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16625. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16626. };
  16627. byte badKey32[] = {
  16628. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16629. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16630. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16631. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16632. };
  16633. XMEMSET(&gmac, 0, sizeof(Gmac));
  16634. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16635. #ifdef WOLFSSL_AES_128
  16636. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16637. #endif
  16638. #ifdef WOLFSSL_AES_192
  16639. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16640. #endif
  16641. #ifdef WOLFSSL_AES_256
  16642. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16643. #endif
  16644. /* Pass in bad args. */
  16645. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16646. BAD_FUNC_ARG);
  16647. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16648. BAD_FUNC_ARG);
  16649. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16650. BAD_FUNC_ARG);
  16651. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16652. BAD_FUNC_ARG);
  16653. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16654. BAD_FUNC_ARG);
  16655. wc_AesFree(&gmac.aes);
  16656. #endif
  16657. return EXPECT_RESULT();
  16658. } /* END test_wc_GmacSetKey */
  16659. /*
  16660. * unit test for wc_GmacUpdate
  16661. */
  16662. static int test_wc_GmacUpdate(void)
  16663. {
  16664. EXPECT_DECLS;
  16665. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16666. Gmac gmac;
  16667. #ifdef WOLFSSL_AES_128
  16668. const byte key16[] = {
  16669. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16670. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16671. };
  16672. #endif
  16673. #ifdef WOLFSSL_AES_192
  16674. byte key24[] = {
  16675. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16676. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16677. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16678. };
  16679. #endif
  16680. #ifdef WOLFSSL_AES_256
  16681. byte key32[] = {
  16682. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16683. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16684. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16685. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16686. };
  16687. #endif
  16688. #ifdef WOLFSSL_AES_128
  16689. const byte authIn[] = {
  16690. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16691. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16692. };
  16693. #endif
  16694. #ifdef WOLFSSL_AES_192
  16695. const byte authIn2[] = {
  16696. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16697. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16698. };
  16699. #endif
  16700. const byte authIn3[] = {
  16701. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16702. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16703. };
  16704. #ifdef WOLFSSL_AES_128
  16705. const byte tag1[] = { /* Known. */
  16706. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16707. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16708. };
  16709. #endif
  16710. #ifdef WOLFSSL_AES_192
  16711. const byte tag2[] = { /* Known */
  16712. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16713. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16714. };
  16715. #endif
  16716. const byte tag3[] = { /* Known */
  16717. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16718. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16719. };
  16720. #ifdef WOLFSSL_AES_128
  16721. const byte iv[] = {
  16722. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16723. 0xe2, 0x8c, 0x8f, 0x16
  16724. };
  16725. #endif
  16726. #ifdef WOLFSSL_AES_192
  16727. const byte iv2[] = {
  16728. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16729. 0x7e, 0x1a, 0x6f, 0xbc
  16730. };
  16731. #endif
  16732. const byte iv3[] = {
  16733. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16734. 0xc3, 0xfb, 0x6c, 0x8a
  16735. };
  16736. byte tagOut[16];
  16737. byte tagOut2[24];
  16738. byte tagOut3[32];
  16739. /* Init stack variables. */
  16740. XMEMSET(&gmac, 0, sizeof(Gmac));
  16741. XMEMSET(tagOut, 0, sizeof(tagOut));
  16742. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16743. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16744. #ifdef WOLFSSL_AES_128
  16745. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16746. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16747. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16748. tagOut, sizeof(tag1)), 0);
  16749. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16750. wc_AesFree(&gmac.aes);
  16751. #endif
  16752. #ifdef WOLFSSL_AES_192
  16753. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16754. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16755. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16756. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16757. tagOut2, sizeof(tag2)), 0);
  16758. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16759. wc_AesFree(&gmac.aes);
  16760. #endif
  16761. #ifdef WOLFSSL_AES_256
  16762. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16763. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16764. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16765. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16766. tagOut3, sizeof(tag3)), 0);
  16767. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16768. wc_AesFree(&gmac.aes);
  16769. #endif
  16770. /* Pass bad args. */
  16771. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16772. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16773. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16774. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16775. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16776. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16777. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16778. wc_AesFree(&gmac.aes);
  16779. #endif
  16780. return EXPECT_RESULT();
  16781. } /* END test_wc_GmacUpdate */
  16782. /*
  16783. * testing wc_CamelliaSetKey
  16784. */
  16785. static int test_wc_CamelliaSetKey(void)
  16786. {
  16787. EXPECT_DECLS;
  16788. #ifdef HAVE_CAMELLIA
  16789. Camellia camellia;
  16790. /*128-bit key*/
  16791. static const byte key16[] = {
  16792. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16793. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16794. };
  16795. /* 192-bit key */
  16796. static const byte key24[] = {
  16797. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16798. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16799. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16800. };
  16801. /* 256-bit key */
  16802. static const byte key32[] = {
  16803. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16804. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16805. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16806. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16807. };
  16808. static const byte iv[] = {
  16809. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16810. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16811. };
  16812. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16813. 0);
  16814. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16815. NULL), 0);
  16816. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16817. 0);
  16818. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16819. NULL), 0);
  16820. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16821. 0);
  16822. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16823. NULL), 0);
  16824. /* Bad args. */
  16825. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16826. BAD_FUNC_ARG);
  16827. #endif
  16828. return EXPECT_RESULT();
  16829. } /* END test_wc_CammeliaSetKey */
  16830. /*
  16831. * Testing wc_CamelliaSetIV()
  16832. */
  16833. static int test_wc_CamelliaSetIV(void)
  16834. {
  16835. EXPECT_DECLS;
  16836. #ifdef HAVE_CAMELLIA
  16837. Camellia camellia;
  16838. static const byte iv[] = {
  16839. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16840. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16841. };
  16842. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16843. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16844. /* Bad args. */
  16845. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16846. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16847. #endif
  16848. return EXPECT_RESULT();
  16849. } /* END test_wc_CamelliaSetIV*/
  16850. /*
  16851. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16852. */
  16853. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16854. {
  16855. EXPECT_DECLS;
  16856. #ifdef HAVE_CAMELLIA
  16857. Camellia camellia;
  16858. static const byte key24[] = {
  16859. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16860. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16861. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16862. };
  16863. static const byte iv[] = {
  16864. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16865. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16866. };
  16867. static const byte plainT[] = {
  16868. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16869. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16870. };
  16871. byte enc[sizeof(plainT)];
  16872. byte dec[sizeof(enc)];
  16873. /* Init stack variables.*/
  16874. XMEMSET(enc, 0, 16);
  16875. XMEMSET(enc, 0, 16);
  16876. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16877. 0);
  16878. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16879. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16880. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16881. /* Pass bad args. */
  16882. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16883. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16884. BAD_FUNC_ARG);
  16885. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16886. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16887. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16888. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16889. #endif
  16890. return EXPECT_RESULT();
  16891. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16892. /*
  16893. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16894. */
  16895. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16896. {
  16897. EXPECT_DECLS;
  16898. #ifdef HAVE_CAMELLIA
  16899. Camellia camellia;
  16900. static const byte key24[] = {
  16901. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16902. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16903. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16904. };
  16905. static const byte plainT[] = {
  16906. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16907. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16908. };
  16909. byte enc[CAMELLIA_BLOCK_SIZE];
  16910. byte dec[CAMELLIA_BLOCK_SIZE];
  16911. /* Init stack variables. */
  16912. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16913. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16914. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16915. NULL), 0);
  16916. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16917. CAMELLIA_BLOCK_SIZE), 0);
  16918. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16919. NULL), 0);
  16920. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16921. 0);
  16922. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16923. /* Pass in bad args. */
  16924. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16925. BAD_FUNC_ARG);
  16926. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16927. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16928. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16929. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16930. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16931. BAD_FUNC_ARG);
  16932. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16933. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16934. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16935. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16936. #endif
  16937. return EXPECT_RESULT();
  16938. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16939. /*
  16940. * Testing wc_Arc4SetKey()
  16941. */
  16942. static int test_wc_Arc4SetKey(void)
  16943. {
  16944. EXPECT_DECLS;
  16945. #ifndef NO_RC4
  16946. Arc4 arc;
  16947. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16948. int keyLen = 8;
  16949. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, (word32)keyLen), 0);
  16950. /* Test bad args. */
  16951. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, (word32)keyLen), BAD_FUNC_ARG);
  16952. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , (word32)keyLen), BAD_FUNC_ARG);
  16953. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16954. #endif
  16955. return EXPECT_RESULT();
  16956. } /* END test_wc_Arc4SetKey */
  16957. /*
  16958. * Testing wc_Arc4Process for ENC/DEC.
  16959. */
  16960. static int test_wc_Arc4Process(void)
  16961. {
  16962. EXPECT_DECLS;
  16963. #ifndef NO_RC4
  16964. Arc4 enc;
  16965. Arc4 dec;
  16966. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16967. int keyLen = 8;
  16968. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16969. byte cipher[8];
  16970. byte plain[8];
  16971. /* Init stack variables */
  16972. XMEMSET(&enc, 0, sizeof(Arc4));
  16973. XMEMSET(&dec, 0, sizeof(Arc4));
  16974. XMEMSET(cipher, 0, sizeof(cipher));
  16975. XMEMSET(plain, 0, sizeof(plain));
  16976. /* Use for async. */
  16977. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16978. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16979. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, (word32)keyLen), 0);
  16980. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, (word32)keyLen), 0);
  16981. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, (word32)keyLen), 0);
  16982. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, (word32)keyLen), 0);
  16983. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16984. /* Bad args. */
  16985. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, (word32)keyLen), BAD_FUNC_ARG);
  16986. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, (word32)keyLen), BAD_FUNC_ARG);
  16987. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, (word32)keyLen), BAD_FUNC_ARG);
  16988. wc_Arc4Free(&enc);
  16989. wc_Arc4Free(&dec);
  16990. #endif
  16991. return EXPECT_RESULT();
  16992. } /* END test_wc_Arc4Process */
  16993. /*
  16994. * Testing wc_Init RsaKey()
  16995. */
  16996. static int test_wc_InitRsaKey(void)
  16997. {
  16998. EXPECT_DECLS;
  16999. #ifndef NO_RSA
  17000. RsaKey key;
  17001. XMEMSET(&key, 0, sizeof(RsaKey));
  17002. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17003. /* Test bad args. */
  17004. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  17005. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17006. #endif
  17007. return EXPECT_RESULT();
  17008. } /* END test_wc_InitRsaKey */
  17009. /*
  17010. * Testing wc_RsaPrivateKeyDecode()
  17011. */
  17012. static int test_wc_RsaPrivateKeyDecode(void)
  17013. {
  17014. EXPECT_DECLS;
  17015. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17016. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17017. RsaKey key;
  17018. byte* tmp = NULL;
  17019. word32 idx = 0;
  17020. int bytes = 0;
  17021. XMEMSET(&key, 0, sizeof(RsaKey));
  17022. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  17023. DYNAMIC_TYPE_TMP_BUFFER));
  17024. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17025. if (tmp != NULL) {
  17026. #ifdef USE_CERT_BUFFERS_1024
  17027. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17028. bytes = sizeof_client_key_der_1024;
  17029. #else
  17030. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17031. bytes = sizeof_client_key_der_2048;
  17032. #endif /* Use cert buffers. */
  17033. }
  17034. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  17035. /* Test bad args. */
  17036. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  17037. BAD_FUNC_ARG);
  17038. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  17039. BAD_FUNC_ARG);
  17040. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17041. BAD_FUNC_ARG);
  17042. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17043. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17044. #endif
  17045. return EXPECT_RESULT();
  17046. } /* END test_wc_RsaPrivateKeyDecode */
  17047. /*
  17048. * Testing wc_RsaPublicKeyDecode()
  17049. */
  17050. static int test_wc_RsaPublicKeyDecode(void)
  17051. {
  17052. EXPECT_DECLS;
  17053. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17054. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17055. RsaKey keyPub;
  17056. byte* tmp = NULL;
  17057. word32 idx = 0;
  17058. int bytes = 0;
  17059. word32 keySz = 0;
  17060. word32 tstKeySz = 0;
  17061. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17062. XFILE f = XBADFILE;
  17063. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17064. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17065. byte buf[4096];
  17066. #endif
  17067. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  17068. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17069. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  17070. if (tmp != NULL) {
  17071. #ifdef USE_CERT_BUFFERS_1024
  17072. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17073. bytes = sizeof_client_keypub_der_1024;
  17074. keySz = 1024;
  17075. #else
  17076. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17077. bytes = sizeof_client_keypub_der_2048;
  17078. keySz = 2048;
  17079. #endif
  17080. }
  17081. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  17082. /* Pass in bad args. */
  17083. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  17084. BAD_FUNC_ARG);
  17085. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  17086. BAD_FUNC_ARG);
  17087. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17088. BAD_FUNC_ARG);
  17089. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  17090. /* Test for getting modulus key size */
  17091. idx = 0;
  17092. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17093. &tstKeySz, NULL, NULL), 0);
  17094. ExpectIntEQ(tstKeySz, keySz/8);
  17095. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17096. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  17097. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17098. if (f != XBADFILE) {
  17099. XFCLOSE(f);
  17100. f = XBADFILE;
  17101. }
  17102. idx = 0;
  17103. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17104. NULL), 0);
  17105. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17106. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17107. if (f != XBADFILE)
  17108. XFCLOSE(f);
  17109. idx = 0;
  17110. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17111. NULL), 0);
  17112. #endif
  17113. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17114. #endif
  17115. return EXPECT_RESULT();
  17116. } /* END test_wc_RsaPublicKeyDecode */
  17117. /*
  17118. * Testing wc_RsaPublicKeyDecodeRaw()
  17119. */
  17120. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17121. {
  17122. EXPECT_DECLS;
  17123. #if !defined(NO_RSA)
  17124. RsaKey key;
  17125. const byte n = 0x23;
  17126. const byte e = 0x03;
  17127. int nSz = sizeof(n);
  17128. int eSz = sizeof(e);
  17129. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17130. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17131. /* Pass in bad args. */
  17132. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17133. BAD_FUNC_ARG);
  17134. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17135. BAD_FUNC_ARG);
  17136. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17137. BAD_FUNC_ARG);
  17138. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17139. #endif
  17140. return EXPECT_RESULT();
  17141. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17142. /*
  17143. * Testing wc_RsaPrivateKeyDecodeRaw()
  17144. */
  17145. static int test_wc_RsaPrivateKeyDecodeRaw(void)
  17146. {
  17147. EXPECT_DECLS;
  17148. #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) \
  17149. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  17150. RsaKey key;
  17151. const byte n = 33;
  17152. const byte e = 3;
  17153. const byte d = 7;
  17154. const byte u = 2;
  17155. const byte p = 3;
  17156. const byte q = 11;
  17157. const byte dp = 1;
  17158. const byte dq = 7;
  17159. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17160. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17161. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17162. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17163. NULL, 0, &key), 0);
  17164. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17165. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17166. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17167. NULL, 0, &key), 0);
  17168. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17169. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17170. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17171. &dq, sizeof(dq), &key), 0);
  17172. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17173. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17174. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17175. &dq, sizeof(dq), &key), 0);
  17176. /* Pass in bad args. */
  17177. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(NULL, sizeof(n),
  17178. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17179. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17180. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17181. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, 0,
  17182. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17183. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17184. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17185. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17186. NULL, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17187. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17188. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17189. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17190. &e, 0, &d, sizeof(d), &u, sizeof(u),
  17191. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17192. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17193. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17194. &e, sizeof(e), NULL, sizeof(d), &u, sizeof(u),
  17195. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17196. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17197. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17198. &e, sizeof(e), &d, 0, &u, sizeof(u),
  17199. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17200. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17201. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17202. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17203. NULL, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17204. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17205. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17206. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17207. &p, 0, &q, sizeof(q), &dp, sizeof(dp),
  17208. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17209. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17210. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17211. &p, sizeof(p), NULL, sizeof(q), &dp, sizeof(dp),
  17212. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17213. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17214. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17215. &p, sizeof(p), &q, 0, &dp, sizeof(dp),
  17216. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17217. #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
  17218. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17219. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17220. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17221. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17222. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17223. &e, sizeof(e), &d, sizeof(d), NULL, sizeof(u),
  17224. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17225. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17226. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17227. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17228. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17229. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17230. #endif
  17231. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17232. #endif
  17233. return EXPECT_RESULT();
  17234. } /* END test_wc_RsaPrivateKeyDecodeRaw */
  17235. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17236. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17237. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17238. * trying until it gets a probable prime. */
  17239. #ifdef HAVE_FIPS
  17240. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17241. {
  17242. int ret;
  17243. for (;;) {
  17244. ret = wc_MakeRsaKey(key, size, e, rng);
  17245. if (ret != PRIME_GEN_E) break;
  17246. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17247. "trying again.\n");
  17248. }
  17249. return ret;
  17250. }
  17251. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17252. #else
  17253. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17254. #endif
  17255. #endif
  17256. /*
  17257. * Testing wc_MakeRsaKey()
  17258. */
  17259. static int test_wc_MakeRsaKey(void)
  17260. {
  17261. EXPECT_DECLS;
  17262. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17263. RsaKey genKey;
  17264. WC_RNG rng;
  17265. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17266. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17267. int bits = 1024;
  17268. #else
  17269. int bits = 2048;
  17270. #endif
  17271. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17272. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17273. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17274. ExpectIntEQ(wc_InitRng(&rng), 0);
  17275. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17276. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17277. /* Test bad args. */
  17278. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17279. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17280. BAD_FUNC_ARG);
  17281. /* e < 3 */
  17282. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17283. /* e & 1 == 0 */
  17284. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17285. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17286. #endif
  17287. return EXPECT_RESULT();
  17288. } /* END test_wc_MakeRsaKey */
  17289. /*
  17290. * Test the bounds checking on the cipher text versus the key modulus.
  17291. * 1. Make a new RSA key.
  17292. * 2. Set c to 1.
  17293. * 3. Decrypt c into k. (error)
  17294. * 4. Copy the key modulus to c and sub 1 from the copy.
  17295. * 5. Decrypt c into k. (error)
  17296. * Valid bounds test cases are covered by all the other RSA tests.
  17297. */
  17298. static int test_RsaDecryptBoundsCheck(void)
  17299. {
  17300. EXPECT_DECLS;
  17301. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17302. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17303. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17304. WC_RNG rng;
  17305. RsaKey key;
  17306. byte flatC[256];
  17307. word32 flatCSz;
  17308. byte out[256];
  17309. word32 outSz = sizeof(out);
  17310. XMEMSET(&key, 0, sizeof(RsaKey));
  17311. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17312. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17313. ExpectIntEQ(wc_InitRng(&rng), 0);
  17314. if (EXPECT_SUCCESS()) {
  17315. const byte* derKey;
  17316. word32 derKeySz;
  17317. word32 idx = 0;
  17318. #ifdef USE_CERT_BUFFERS_1024
  17319. derKey = server_key_der_1024;
  17320. derKeySz = (word32)sizeof_server_key_der_1024;
  17321. flatCSz = 128;
  17322. #else
  17323. derKey = server_key_der_2048;
  17324. derKeySz = (word32)sizeof_server_key_der_2048;
  17325. flatCSz = 256;
  17326. #endif
  17327. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17328. }
  17329. if (EXPECT_SUCCESS()) {
  17330. XMEMSET(flatC, 0, flatCSz);
  17331. flatC[flatCSz-1] = 1;
  17332. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17333. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17334. if (EXPECT_SUCCESS()) {
  17335. mp_int c;
  17336. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17337. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17338. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17339. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17340. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17341. mp_clear(&c);
  17342. }
  17343. }
  17344. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17345. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17346. #endif
  17347. return EXPECT_RESULT();
  17348. } /* END test_wc_RsaDecryptBoundsCheck */
  17349. /*
  17350. * Testing wc_SetKeyUsage()
  17351. */
  17352. static int test_wc_SetKeyUsage(void)
  17353. {
  17354. EXPECT_DECLS;
  17355. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17356. Cert myCert;
  17357. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17358. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17359. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17360. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17361. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17362. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17363. /* Test bad args. */
  17364. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17365. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17366. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17367. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17368. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17369. KEYUSAGE_E);
  17370. #endif
  17371. return EXPECT_RESULT();
  17372. } /* END test_wc_SetKeyUsage */
  17373. /*
  17374. * Testing wc_CheckProbablePrime()
  17375. */
  17376. static int test_wc_CheckProbablePrime(void)
  17377. {
  17378. EXPECT_DECLS;
  17379. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17380. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17381. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17382. RsaKey key;
  17383. WC_RNG rng;
  17384. byte e[3];
  17385. word32 eSz = (word32)sizeof(e);
  17386. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17387. word32 nSz = (word32)sizeof(n);
  17388. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17389. word32 dSz = (word32)sizeof(d);
  17390. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17391. word32 pSz = (word32)sizeof(p);
  17392. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17393. word32 qSz = (word32)sizeof(q);
  17394. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17395. int* isPrime;
  17396. int test[5];
  17397. isPrime = test;
  17398. XMEMSET(&key, 0, sizeof(RsaKey));
  17399. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17400. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17401. ExpectIntEQ(wc_InitRng(&rng), 0);
  17402. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17403. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17404. WC_RSA_EXPONENT, &rng), 0);
  17405. PRIVATE_KEY_UNLOCK();
  17406. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17407. &qSz), 0);
  17408. PRIVATE_KEY_LOCK();
  17409. /* Bad cases */
  17410. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17411. BAD_FUNC_ARG);
  17412. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17413. BAD_FUNC_ARG);
  17414. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17415. BAD_FUNC_ARG);
  17416. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17417. BAD_FUNC_ARG);
  17418. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17419. BAD_FUNC_ARG);
  17420. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17421. BAD_FUNC_ARG);
  17422. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17423. BAD_FUNC_ARG);
  17424. /* Good case */
  17425. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17426. 0);
  17427. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17428. wc_FreeRng(&rng);
  17429. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17430. #endif
  17431. return EXPECT_RESULT();
  17432. } /* END test_wc_CheckProbablePrime */
  17433. /*
  17434. * Testing wc_RsaPSS_Verify()
  17435. */
  17436. static int test_wc_RsaPSS_Verify(void)
  17437. {
  17438. EXPECT_DECLS;
  17439. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17440. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17441. RsaKey key;
  17442. WC_RNG rng;
  17443. int sz = 256;
  17444. const char* szMessage = "This is the string to be signed";
  17445. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17446. unsigned char pDecrypted[2048/8];
  17447. byte* pt = pDecrypted;
  17448. word32 outLen = sizeof(pDecrypted);
  17449. XMEMSET(&key, 0, sizeof(RsaKey));
  17450. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17451. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17452. ExpectIntEQ(wc_InitRng(&rng), 0);
  17453. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17454. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17455. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17456. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17457. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17458. /* Bad cases */
  17459. ExpectIntEQ(wc_RsaPSS_Verify(NULL, (word32)sz, pt, outLen,
  17460. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17461. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17462. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17463. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, (word32)sz, NULL, outLen,
  17464. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17465. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17466. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17467. /* Good case */
  17468. ExpectIntGT(wc_RsaPSS_Verify(pSignature, (word32)sz, pt, outLen,
  17469. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17470. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17471. wc_FreeRng(&rng);
  17472. #endif
  17473. return EXPECT_RESULT();
  17474. } /* END test_wc_RsaPSS_Verify */
  17475. /*
  17476. * Testing wc_RsaPSS_VerifyCheck()
  17477. */
  17478. static int test_wc_RsaPSS_VerifyCheck(void)
  17479. {
  17480. EXPECT_DECLS;
  17481. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17482. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17483. RsaKey key;
  17484. WC_RNG rng;
  17485. int sz = 256; /* 2048/8 */
  17486. byte digest[32];
  17487. word32 digestSz = sizeof(digest);
  17488. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17489. word32 pSignatureSz = sizeof(pSignature);
  17490. unsigned char pDecrypted[2048/8];
  17491. byte* pt = pDecrypted;
  17492. word32 outLen = sizeof(pDecrypted);
  17493. XMEMSET(&key, 0, sizeof(RsaKey));
  17494. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17495. XMEMSET(digest, 0, sizeof(digest));
  17496. XMEMSET(pSignature, 0, sizeof(pSignature));
  17497. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17498. ExpectIntEQ(wc_InitRng(&rng), 0);
  17499. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17500. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17501. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17502. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17503. 0);
  17504. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17505. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17506. /* Bad cases */
  17507. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, (word32)sz, pt, outLen, digest,
  17508. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17509. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17510. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17511. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, NULL, outLen, digest,
  17512. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17513. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17514. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17515. /* Good case */
  17516. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, pt, outLen, digest,
  17517. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17518. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17519. wc_FreeRng(&rng);
  17520. #endif
  17521. return EXPECT_RESULT();
  17522. } /* END test_wc_RsaPSS_VerifyCheck */
  17523. /*
  17524. * Testing wc_RsaPSS_VerifyCheckInline()
  17525. */
  17526. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17527. {
  17528. EXPECT_DECLS;
  17529. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17530. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17531. RsaKey key;
  17532. WC_RNG rng;
  17533. int sz = 256;
  17534. byte digest[32];
  17535. word32 digestSz = sizeof(digest);
  17536. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17537. unsigned char pDecrypted[2048/8];
  17538. byte* pt = pDecrypted;
  17539. XMEMSET(&key, 0, sizeof(RsaKey));
  17540. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17541. XMEMSET(digest, 0, sizeof(digest));
  17542. XMEMSET(pSignature, 0, sizeof(pSignature));
  17543. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17544. ExpectIntEQ(wc_InitRng(&rng), 0);
  17545. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17546. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17547. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17548. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17549. 0);
  17550. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17551. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17552. /* Bad Cases */
  17553. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, (word32)sz, &pt, digest,
  17554. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17555. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17556. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17557. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17558. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17559. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17560. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17561. /* Good case */
  17562. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17563. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17564. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17565. wc_FreeRng(&rng);
  17566. #endif
  17567. return EXPECT_RESULT();
  17568. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17569. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17570. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17571. {
  17572. (void)flag;
  17573. (void)type;
  17574. (void)file;
  17575. (void)line;
  17576. }
  17577. #endif
  17578. /*
  17579. * Testing wc_LockMutex_ex
  17580. */
  17581. static int test_wc_LockMutex_ex(void)
  17582. {
  17583. EXPECT_DECLS;
  17584. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17585. int flag = CRYPTO_LOCK;
  17586. int type = 0;
  17587. const char* file = "./test-LockMutex_ex.txt";
  17588. int line = 0;
  17589. /* without SetMutexCb */
  17590. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17591. /* with SetMutexCb */
  17592. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17593. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17594. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17595. #endif
  17596. return EXPECT_RESULT();
  17597. } /* End test_wc_LockMutex_ex*/
  17598. /*
  17599. * Testing wc_SetMutexCb
  17600. */
  17601. static int test_wc_SetMutexCb(void)
  17602. {
  17603. EXPECT_DECLS;
  17604. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17605. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17606. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17607. #endif
  17608. return EXPECT_RESULT();
  17609. } /* End test_wc_SetMutexCb*/
  17610. /*
  17611. * Testing wc_RsaKeyToDer()
  17612. */
  17613. static int test_wc_RsaKeyToDer(void)
  17614. {
  17615. EXPECT_DECLS;
  17616. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17617. RsaKey genKey;
  17618. WC_RNG rng;
  17619. byte* der = NULL;
  17620. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17621. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17622. int bits = 1024;
  17623. word32 derSz = 611;
  17624. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17625. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17626. #else
  17627. int bits = 2048;
  17628. word32 derSz = 1196;
  17629. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17630. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17631. #endif
  17632. XMEMSET(&rng, 0, sizeof(rng));
  17633. XMEMSET(&genKey, 0, sizeof(genKey));
  17634. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17635. /* Init structures. */
  17636. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17637. ExpectIntEQ(wc_InitRng(&rng), 0);
  17638. /* Make key. */
  17639. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17640. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17641. /* Pass good/bad args. */
  17642. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17643. /* Get just the output length */
  17644. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17645. /* Try Public Key. */
  17646. genKey.type = 0;
  17647. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17648. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17649. /* Put back to Private Key */
  17650. genKey.type = 1;
  17651. #endif
  17652. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17653. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17654. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17655. #endif
  17656. return EXPECT_RESULT();
  17657. } /* END test_wc_RsaKeyToDer */
  17658. /*
  17659. * Testing wc_RsaKeyToPublicDer()
  17660. */
  17661. static int test_wc_RsaKeyToPublicDer(void)
  17662. {
  17663. EXPECT_DECLS;
  17664. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17665. RsaKey key;
  17666. WC_RNG rng;
  17667. byte* der = NULL;
  17668. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17669. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17670. int bits = 1024;
  17671. word32 derLen = 162;
  17672. #else
  17673. int bits = 2048;
  17674. word32 derLen = 294;
  17675. #endif
  17676. int ret;
  17677. XMEMSET(&rng, 0, sizeof(rng));
  17678. XMEMSET(&key, 0, sizeof(key));
  17679. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17680. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17681. ExpectIntEQ(wc_InitRng(&rng), 0);
  17682. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17683. /* test getting size only */
  17684. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17685. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17686. /* test getting size only */
  17687. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17688. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17689. /* Pass in bad args. */
  17690. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17691. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17692. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17693. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17694. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17695. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17696. #endif
  17697. return EXPECT_RESULT();
  17698. } /* END test_wc_RsaKeyToPublicDer */
  17699. /*
  17700. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17701. */
  17702. static int test_wc_RsaPublicEncryptDecrypt(void)
  17703. {
  17704. EXPECT_DECLS;
  17705. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17706. RsaKey key;
  17707. WC_RNG rng;
  17708. const char inStr[] = TEST_STRING;
  17709. const word32 plainLen = (word32)TEST_STRING_SZ;
  17710. const word32 inLen = (word32)TEST_STRING_SZ;
  17711. int bits = TEST_RSA_BITS;
  17712. const word32 cipherLen = TEST_RSA_BYTES;
  17713. word32 cipherLenResult = cipherLen;
  17714. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17715. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17716. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17717. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17718. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17719. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17720. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17721. ExpectNotNull(in);
  17722. ExpectNotNull(plain);
  17723. ExpectNotNull(cipher);
  17724. #endif
  17725. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17726. /* Initialize stack structures. */
  17727. XMEMSET(&key, 0, sizeof(RsaKey));
  17728. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17729. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17730. ExpectIntEQ(wc_InitRng(&rng), 0);
  17731. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17732. /* Encrypt. */
  17733. ExpectIntGT(cipherLenResult = (word32)wc_RsaPublicEncrypt(in, inLen, cipher,
  17734. cipherLen, &key, &rng), 0);
  17735. /* Pass bad args - tested in another testing function.*/
  17736. /* Decrypt */
  17737. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17738. /* Bind rng */
  17739. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17740. #endif
  17741. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17742. &key), 0);
  17743. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17744. /* Pass bad args - tested in another testing function.*/
  17745. WC_FREE_VAR(in, NULL);
  17746. WC_FREE_VAR(plain, NULL);
  17747. WC_FREE_VAR(cipher, NULL);
  17748. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17749. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17750. #endif
  17751. return EXPECT_RESULT();
  17752. } /* END test_wc_RsaPublicEncryptDecrypt */
  17753. /*
  17754. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17755. */
  17756. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17757. {
  17758. EXPECT_DECLS;
  17759. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17760. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17761. RsaKey key;
  17762. WC_RNG rng;
  17763. const char inStr[] = TEST_STRING;
  17764. const word32 inLen = (word32)TEST_STRING_SZ;
  17765. const word32 plainSz = (word32)TEST_STRING_SZ;
  17766. byte* res = NULL;
  17767. int idx = 0;
  17768. int bits = TEST_RSA_BITS;
  17769. const word32 cipherSz = TEST_RSA_BYTES;
  17770. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17771. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17772. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17773. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17774. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17775. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17776. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17777. ExpectNotNull(in);
  17778. ExpectNotNull(plain);
  17779. ExpectNotNull(cipher);
  17780. #endif
  17781. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17782. /* Initialize stack structures. */
  17783. XMEMSET(&key, 0, sizeof(RsaKey));
  17784. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17785. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17786. ExpectIntEQ(wc_InitRng(&rng), 0);
  17787. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17788. /* Encrypt */
  17789. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17790. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17791. /* Pass bad args - tested in another testing function.*/
  17792. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17793. /* Decrypt */
  17794. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17795. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17796. #endif
  17797. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17798. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17799. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17800. /* Pass bad args - tested in another testing function.*/
  17801. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17802. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17803. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17804. #endif
  17805. WC_FREE_VAR(in, NULL);
  17806. WC_FREE_VAR(plain, NULL);
  17807. WC_FREE_VAR(cipher, NULL);
  17808. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17809. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17810. #endif
  17811. return EXPECT_RESULT();
  17812. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17813. /*
  17814. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17815. */
  17816. static int test_wc_RsaSSL_SignVerify(void)
  17817. {
  17818. EXPECT_DECLS;
  17819. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17820. RsaKey key;
  17821. WC_RNG rng;
  17822. const char inStr[] = TEST_STRING;
  17823. const word32 plainSz = (word32)TEST_STRING_SZ;
  17824. const word32 inLen = (word32)TEST_STRING_SZ;
  17825. word32 idx = 0;
  17826. int bits = TEST_RSA_BITS;
  17827. const word32 outSz = TEST_RSA_BYTES;
  17828. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17829. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17830. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17831. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17832. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17833. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17834. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17835. ExpectNotNull(in);
  17836. ExpectNotNull(out);
  17837. ExpectNotNull(plain);
  17838. #endif
  17839. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17840. XMEMSET(&key, 0, sizeof(RsaKey));
  17841. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17842. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17843. ExpectIntEQ(wc_InitRng(&rng), 0);
  17844. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17845. /* Sign. */
  17846. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17847. idx = (int)outSz;
  17848. /* Test bad args. */
  17849. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17850. BAD_FUNC_ARG);
  17851. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17852. BAD_FUNC_ARG);
  17853. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17854. BAD_FUNC_ARG);
  17855. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17856. BAD_FUNC_ARG);
  17857. /* Verify. */
  17858. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17859. /* Pass bad args. */
  17860. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17861. BAD_FUNC_ARG);
  17862. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17863. BAD_FUNC_ARG);
  17864. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17865. BAD_FUNC_ARG);
  17866. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17867. BAD_FUNC_ARG);
  17868. WC_FREE_VAR(in, NULL);
  17869. WC_FREE_VAR(out, NULL);
  17870. WC_FREE_VAR(plain, NULL);
  17871. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17872. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17873. #endif
  17874. return EXPECT_RESULT();
  17875. } /* END test_wc_RsaSSL_SignVerify */
  17876. /*
  17877. * Testing wc_RsaEncryptSize()
  17878. */
  17879. static int test_wc_RsaEncryptSize(void)
  17880. {
  17881. EXPECT_DECLS;
  17882. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17883. RsaKey key;
  17884. WC_RNG rng;
  17885. XMEMSET(&key, 0, sizeof(RsaKey));
  17886. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17887. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17888. ExpectIntEQ(wc_InitRng(&rng), 0);
  17889. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17890. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17891. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17892. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17893. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17894. #endif
  17895. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17896. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17897. /* Pass in bad arg. */
  17898. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17899. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17900. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17901. #endif
  17902. return EXPECT_RESULT();
  17903. } /* END test_wc_RsaEncryptSize*/
  17904. /*
  17905. * Testing wc_RsaFlattenPublicKey()
  17906. */
  17907. static int test_wc_RsaFlattenPublicKey(void)
  17908. {
  17909. EXPECT_DECLS;
  17910. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17911. RsaKey key;
  17912. WC_RNG rng;
  17913. byte e[256];
  17914. byte n[256];
  17915. word32 eSz = sizeof(e);
  17916. word32 nSz = sizeof(n);
  17917. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17918. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17919. int bits = 1024;
  17920. #else
  17921. int bits = 2048;
  17922. #endif
  17923. XMEMSET(&key, 0, sizeof(RsaKey));
  17924. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17925. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17926. ExpectIntEQ(wc_InitRng(&rng), 0);
  17927. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17928. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17929. /* Pass bad args. */
  17930. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17931. BAD_FUNC_ARG);
  17932. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17933. BAD_FUNC_ARG);
  17934. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17935. BAD_FUNC_ARG);
  17936. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17937. BAD_FUNC_ARG);
  17938. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17939. BAD_FUNC_ARG);
  17940. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17941. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17942. #endif
  17943. return EXPECT_RESULT();
  17944. } /* END test_wc_RsaFlattenPublicKey */
  17945. /*
  17946. * unit test for wc_AesCcmSetKey
  17947. */
  17948. static int test_wc_AesCcmSetKey(void)
  17949. {
  17950. EXPECT_DECLS;
  17951. #ifdef HAVE_AESCCM
  17952. Aes aes;
  17953. const byte key16[] = {
  17954. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17955. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17956. };
  17957. const byte key24[] = {
  17958. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17959. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17960. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17961. };
  17962. const byte key32[] = {
  17963. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17964. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17965. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17966. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17967. };
  17968. XMEMSET(&aes, 0, sizeof(Aes));
  17969. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17970. #ifdef WOLFSSL_AES_128
  17971. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17972. #endif
  17973. #ifdef WOLFSSL_AES_192
  17974. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17975. #endif
  17976. #ifdef WOLFSSL_AES_256
  17977. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17978. #endif
  17979. /* Test bad args. */
  17980. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17981. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17982. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17983. wc_AesFree(&aes);
  17984. #endif
  17985. return EXPECT_RESULT();
  17986. } /* END test_wc_AesCcmSetKey */
  17987. /*
  17988. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17989. */
  17990. static int test_wc_AesCcmEncryptDecrypt(void)
  17991. {
  17992. EXPECT_DECLS;
  17993. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17994. Aes aes;
  17995. const byte key16[] = {
  17996. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17997. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17998. };
  17999. /* plaintext */
  18000. const byte plainT[] = {
  18001. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18002. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18003. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18004. };
  18005. /* nonce */
  18006. const byte iv[] = {
  18007. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18008. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18009. };
  18010. const byte c[] = { /* cipher text. */
  18011. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18012. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18013. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18014. };
  18015. const byte t[] = { /* Auth tag */
  18016. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18017. };
  18018. const byte authIn[] = {
  18019. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18020. };
  18021. byte cipherOut[sizeof(plainT)];
  18022. byte authTag[sizeof(t)];
  18023. #ifdef HAVE_AES_DECRYPT
  18024. byte plainOut[sizeof(cipherOut)];
  18025. #endif
  18026. XMEMSET(&aes, 0, sizeof(Aes));
  18027. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  18028. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  18029. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18030. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  18031. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  18032. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  18033. #ifdef HAVE_AES_DECRYPT
  18034. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18035. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  18036. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  18037. #endif
  18038. /* Pass in bad args. Encrypt*/
  18039. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18040. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18041. BAD_FUNC_ARG);
  18042. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18043. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18044. BAD_FUNC_ARG);
  18045. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18046. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18047. BAD_FUNC_ARG);
  18048. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18049. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18050. BAD_FUNC_ARG);
  18051. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18052. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  18053. BAD_FUNC_ARG);
  18054. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18055. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18056. BAD_FUNC_ARG);
  18057. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18058. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18059. BAD_FUNC_ARG);
  18060. #ifdef HAVE_AES_DECRYPT
  18061. /* Pass in bad args. Decrypt*/
  18062. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18063. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18064. BAD_FUNC_ARG);
  18065. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18066. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18067. BAD_FUNC_ARG);
  18068. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18069. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18070. BAD_FUNC_ARG);
  18071. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18072. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18073. BAD_FUNC_ARG);
  18074. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18075. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  18076. BAD_FUNC_ARG);
  18077. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18078. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18079. BAD_FUNC_ARG);
  18080. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18081. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18082. BAD_FUNC_ARG);
  18083. #endif
  18084. wc_AesFree(&aes);
  18085. #endif /* HAVE_AESCCM */
  18086. return EXPECT_RESULT();
  18087. } /* END test_wc_AesCcmEncryptDecrypt */
  18088. #if defined(WOLFSSL_AES_EAX) && \
  18089. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18090. /*
  18091. * Testing test_wc_AesEaxVectors()
  18092. */
  18093. static int test_wc_AesEaxVectors(void)
  18094. {
  18095. EXPECT_DECLS;
  18096. typedef struct {
  18097. byte key[AES_256_KEY_SIZE];
  18098. int key_length;
  18099. byte iv[AES_BLOCK_SIZE];
  18100. int iv_length;
  18101. byte aad[AES_BLOCK_SIZE * 2];
  18102. int aad_length;
  18103. byte msg[AES_BLOCK_SIZE * 5];
  18104. int msg_length;
  18105. byte ct[AES_BLOCK_SIZE * 5];
  18106. int ct_length;
  18107. byte tag[AES_BLOCK_SIZE];
  18108. int tag_length;
  18109. int valid;
  18110. } AadVector;
  18111. /* Test vectors obtained from Google wycheproof project
  18112. * https://github.com/google/wycheproof
  18113. * from testvectors/aes_eax_test.json
  18114. */
  18115. const AadVector vectors[] = {
  18116. {
  18117. /* key, key length */
  18118. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  18119. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  18120. /* iv, iv length */
  18121. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  18122. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  18123. /* aad, aad length */
  18124. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  18125. /* msg, msg length */
  18126. {0x00}, 0,
  18127. /* ct, ct length */
  18128. {0x00}, 0,
  18129. /* tag, tag length */
  18130. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  18131. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  18132. /* valid */
  18133. 1,
  18134. },
  18135. {
  18136. /* key, key length */
  18137. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  18138. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  18139. /* iv, iv length */
  18140. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  18141. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  18142. /* aad, aad length */
  18143. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  18144. /* msg, msg length */
  18145. {0xf7, 0xfb}, 2,
  18146. /* ct, ct length */
  18147. {0x19, 0xdd}, 2,
  18148. /* tag, tag length */
  18149. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  18150. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  18151. /* valid */
  18152. 1,
  18153. },
  18154. {
  18155. /* key, key length */
  18156. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  18157. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  18158. /* iv, iv length */
  18159. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  18160. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  18161. /* aad, aad length */
  18162. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  18163. /* msg, msg length */
  18164. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  18165. /* ct, ct length */
  18166. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  18167. /* tag, tag length */
  18168. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  18169. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  18170. /* valid */
  18171. 1,
  18172. },
  18173. {
  18174. /* key, key length */
  18175. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  18176. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  18177. /* iv, iv length */
  18178. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  18179. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  18180. /* aad, aad length */
  18181. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  18182. /* msg, msg length */
  18183. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  18184. /* ct, ct length */
  18185. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  18186. /* tag, tag length */
  18187. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  18188. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  18189. /* valid */
  18190. 1,
  18191. },
  18192. {
  18193. /* key, key length */
  18194. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  18195. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  18196. /* iv, iv length */
  18197. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18198. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18199. /* aad, aad length */
  18200. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18201. /* msg, msg length */
  18202. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18203. /* ct, ct length */
  18204. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18205. /* tag, tag length */
  18206. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18207. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18208. /* valid */
  18209. 1,
  18210. },
  18211. {
  18212. /* key, key length */
  18213. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18214. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18215. /* iv, iv length */
  18216. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18217. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18218. /* aad, aad length */
  18219. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18220. /* msg, msg length */
  18221. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18222. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18223. /* ct, ct length */
  18224. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18225. 0x0e, 0x72, 0x84, 0x14}, 12,
  18226. /* tag, tag length */
  18227. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18228. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18229. /* valid */
  18230. 1,
  18231. },
  18232. {
  18233. /* key, key length */
  18234. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18235. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18236. /* iv, iv length */
  18237. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18238. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18239. /* aad, aad length */
  18240. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18241. /* msg, msg length */
  18242. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18243. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18244. 0x36}, 17,
  18245. /* ct, ct length */
  18246. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18247. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18248. 0x30}, 17,
  18249. /* tag, tag length */
  18250. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18251. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18252. /* valid */
  18253. 1,
  18254. },
  18255. {
  18256. /* key, key length */
  18257. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18258. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18259. /* iv, iv length */
  18260. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18261. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18262. /* aad, aad length */
  18263. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18264. /* msg, msg length */
  18265. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18266. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18267. 0x2d, 0x56}, 18,
  18268. /* ct, ct length */
  18269. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18270. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18271. 0x8c, 0xc3}, 18,
  18272. /* tag, tag length */
  18273. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18274. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18275. /* valid */
  18276. 1,
  18277. },
  18278. {
  18279. /* key, key length */
  18280. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18281. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18282. /* iv, iv length */
  18283. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18284. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18285. /* aad, aad length */
  18286. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18287. /* msg, msg length */
  18288. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18289. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18290. 0xef, 0x11}, 18,
  18291. /* ct, ct length */
  18292. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18293. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18294. 0xb1, 0xe8}, 18,
  18295. /* tag, tag length */
  18296. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18297. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18298. /* valid */
  18299. 1,
  18300. },
  18301. {
  18302. /* key, key length */
  18303. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18304. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18305. /* iv, iv length */
  18306. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18307. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18308. /* aad, aad length */
  18309. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18310. /* msg, msg length */
  18311. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18312. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18313. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18314. /* ct, ct length */
  18315. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18316. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18317. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18318. /* tag, tag length */
  18319. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18320. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18321. /* valid */
  18322. 1,
  18323. },
  18324. {
  18325. /* key, key length */
  18326. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18327. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18328. /* iv, iv length */
  18329. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18330. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18331. /* aad, aad length */
  18332. {0x00}, 0,
  18333. /* msg, msg length */
  18334. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18335. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18336. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18337. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18338. /* ct, ct length */
  18339. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18340. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18341. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18342. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18343. /* tag, tag length */
  18344. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18345. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18346. /* valid */
  18347. 1,
  18348. },
  18349. {
  18350. /* key, key length */
  18351. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18352. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18353. /* iv, iv length */
  18354. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18355. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18356. /* aad, aad length */
  18357. {0x00}, 0,
  18358. /* msg, msg length */
  18359. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18361. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18362. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18363. /* ct, ct length */
  18364. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18365. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18366. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18367. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18368. /* tag, tag length */
  18369. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18370. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18371. /* valid */
  18372. 1,
  18373. },
  18374. {
  18375. /* key, key length */
  18376. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18377. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18378. /* iv, iv length */
  18379. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18380. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18381. /* aad, aad length */
  18382. {0x00}, 0,
  18383. /* msg, msg length */
  18384. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18385. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18386. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18387. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18388. /* ct, ct length */
  18389. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18390. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18391. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18392. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18393. /* tag, tag length */
  18394. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18395. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18396. /* valid */
  18397. 1,
  18398. },
  18399. {
  18400. /* key, key length */
  18401. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18402. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18403. /* iv, iv length */
  18404. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18405. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18406. /* aad, aad length */
  18407. {0x00}, 0,
  18408. /* msg, msg length */
  18409. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18410. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18411. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18412. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18413. /* ct, ct length */
  18414. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18415. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18416. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18417. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18418. /* tag, tag length */
  18419. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18420. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18421. /* valid */
  18422. 1,
  18423. },
  18424. {
  18425. /* key, key length */
  18426. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18427. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18428. /* iv, iv length */
  18429. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18430. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18431. /* aad, aad length */
  18432. {0x00}, 0,
  18433. /* msg, msg length */
  18434. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18435. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18436. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18437. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18438. /* ct, ct length */
  18439. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18440. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18441. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18442. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18443. /* tag, tag length */
  18444. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18445. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18446. /* valid */
  18447. 1,
  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. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18455. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18456. /* aad, aad length */
  18457. {0x00}, 0,
  18458. /* msg, msg length */
  18459. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18460. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18461. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18462. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18463. /* ct, ct length */
  18464. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18465. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18466. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18467. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18468. /* tag, tag length */
  18469. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18470. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18471. /* valid */
  18472. 1,
  18473. },
  18474. {
  18475. /* key, key length */
  18476. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18477. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18478. /* iv, iv length */
  18479. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18480. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18481. /* aad, aad length */
  18482. {0x00}, 0,
  18483. /* msg, msg length */
  18484. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18485. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18486. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18487. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18488. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18489. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18490. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18491. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18492. /* ct, ct length */
  18493. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18494. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18495. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18496. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18497. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18498. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18499. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18500. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18501. /* tag, tag length */
  18502. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18503. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18504. /* valid */
  18505. 1,
  18506. },
  18507. {
  18508. /* key, key length */
  18509. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18510. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18511. /* iv, iv length */
  18512. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18513. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18514. /* aad, aad length */
  18515. {0x00}, 0,
  18516. /* msg, msg length */
  18517. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18518. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18519. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18520. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18521. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18522. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18523. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18524. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18525. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18526. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18527. /* ct, ct length */
  18528. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18529. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18530. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18531. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18532. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18533. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18534. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18535. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18536. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18537. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18538. /* tag, tag length */
  18539. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18540. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18541. /* valid */
  18542. 1,
  18543. },
  18544. {
  18545. /* key, key length */
  18546. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18547. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18548. /* iv, iv length */
  18549. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18550. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18551. /* aad, aad length */
  18552. {0x00}, 0,
  18553. /* msg, msg length */
  18554. {0x00}, 0,
  18555. /* ct, ct length */
  18556. {0x00}, 0,
  18557. /* tag, tag length */
  18558. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18559. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18560. /* valid */
  18561. 1,
  18562. },
  18563. {
  18564. /* key, key length */
  18565. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18566. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18567. /* iv, iv length */
  18568. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18569. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18570. /* aad, aad length */
  18571. {0x00}, 0,
  18572. /* msg, msg length */
  18573. {0x01}, 1,
  18574. /* ct, ct length */
  18575. {0x1d}, 1,
  18576. /* tag, tag length */
  18577. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18578. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18579. /* valid */
  18580. 1,
  18581. },
  18582. {
  18583. /* key, key length */
  18584. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18585. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18586. /* iv, iv length */
  18587. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18588. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18589. /* aad, aad length */
  18590. {0x00}, 0,
  18591. /* msg, msg length */
  18592. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18593. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18594. /* ct, ct length */
  18595. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18596. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18597. /* tag, tag length */
  18598. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18599. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18600. /* valid */
  18601. 1,
  18602. },
  18603. {
  18604. /* key, key length */
  18605. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18606. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18607. /* iv, iv length */
  18608. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18609. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18610. /* aad, aad length */
  18611. {0x00}, 0,
  18612. /* msg, msg length */
  18613. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18614. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18615. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18616. /* ct, ct length */
  18617. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18618. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18619. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18620. /* tag, tag length */
  18621. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18622. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18623. /* valid */
  18624. 1,
  18625. },
  18626. {
  18627. /* key, key length */
  18628. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18629. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18630. /* iv, iv length */
  18631. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18632. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18633. /* aad, aad length */
  18634. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18635. /* msg, msg length */
  18636. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18637. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18638. /* ct, ct length */
  18639. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18640. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18641. /* tag, tag length */
  18642. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18643. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18644. /* valid */
  18645. 1,
  18646. },
  18647. {
  18648. /* key, key length */
  18649. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18650. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18651. /* iv, iv length */
  18652. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18653. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18654. /* aad, aad length */
  18655. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18656. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18657. /* msg, msg length */
  18658. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18659. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18660. /* ct, ct length */
  18661. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18662. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18663. /* tag, tag length */
  18664. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18665. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18666. /* valid */
  18667. 1,
  18668. },
  18669. {
  18670. /* key, key length */
  18671. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18672. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18673. /* iv, iv length */
  18674. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18675. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18676. /* aad, aad length */
  18677. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18678. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18679. /* msg, msg length */
  18680. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18681. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18682. /* ct, ct length */
  18683. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18684. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18685. /* tag, tag length */
  18686. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18687. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18688. /* valid */
  18689. 1,
  18690. },
  18691. {
  18692. /* key, key length */
  18693. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18694. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18695. /* iv, iv length */
  18696. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18697. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18698. /* aad, aad length */
  18699. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18700. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18701. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18702. /* msg, msg length */
  18703. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18704. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18705. /* ct, ct length */
  18706. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18707. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18708. /* tag, tag length */
  18709. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18710. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18711. /* valid */
  18712. 1,
  18713. },
  18714. {
  18715. /* key, key length */
  18716. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18717. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18718. /* iv, iv length */
  18719. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18720. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18721. /* aad, aad length */
  18722. {0x00}, 0,
  18723. /* msg, msg length */
  18724. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18725. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18726. /* ct, ct length */
  18727. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18728. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18729. /* tag, tag length */
  18730. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18731. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18732. /* valid */
  18733. 0,
  18734. },
  18735. {
  18736. /* key, key length */
  18737. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18738. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18739. /* iv, iv length */
  18740. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18741. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18742. /* aad, aad length */
  18743. {0x00}, 0,
  18744. /* msg, msg length */
  18745. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18746. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18747. /* ct, ct length */
  18748. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18749. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18750. /* tag, tag length */
  18751. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18752. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18753. /* valid */
  18754. 0,
  18755. },
  18756. {
  18757. /* key, key length */
  18758. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18759. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18760. /* iv, iv length */
  18761. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18762. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18763. /* aad, aad length */
  18764. {0x00}, 0,
  18765. /* msg, msg length */
  18766. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18767. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18768. /* ct, ct length */
  18769. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18770. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18771. /* tag, tag length */
  18772. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18773. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18774. /* valid */
  18775. 0,
  18776. },
  18777. {
  18778. /* key, key length */
  18779. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18780. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18781. /* iv, iv length */
  18782. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18783. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18784. /* aad, aad length */
  18785. {0x00}, 0,
  18786. /* msg, msg length */
  18787. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18788. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18789. /* ct, ct length */
  18790. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18791. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18792. /* tag, tag length */
  18793. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18794. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18795. /* valid */
  18796. 0,
  18797. },
  18798. {
  18799. /* key, key length */
  18800. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18801. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18802. /* iv, iv length */
  18803. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18804. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18805. /* aad, aad length */
  18806. {0x00}, 0,
  18807. /* msg, msg length */
  18808. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18809. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18810. /* ct, ct length */
  18811. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18812. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18813. /* tag, tag length */
  18814. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18815. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18816. /* valid */
  18817. 0,
  18818. },
  18819. {
  18820. /* key, key length */
  18821. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18822. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18823. /* iv, iv length */
  18824. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18825. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18826. /* aad, aad length */
  18827. {0x00}, 0,
  18828. /* msg, msg length */
  18829. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18830. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18831. /* ct, ct length */
  18832. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18833. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18834. /* tag, tag length */
  18835. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18836. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18837. /* valid */
  18838. 0,
  18839. },
  18840. {
  18841. /* key, key length */
  18842. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18843. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18844. /* iv, iv length */
  18845. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18846. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18847. /* aad, aad length */
  18848. {0x00}, 0,
  18849. /* msg, msg length */
  18850. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18851. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18852. /* ct, ct length */
  18853. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18854. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18855. /* tag, tag length */
  18856. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18857. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18858. /* valid */
  18859. 0,
  18860. },
  18861. {
  18862. /* key, key length */
  18863. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18864. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18865. /* iv, iv length */
  18866. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18867. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18868. /* aad, aad length */
  18869. {0x00}, 0,
  18870. /* msg, msg length */
  18871. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18872. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18873. /* ct, ct length */
  18874. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18875. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18876. /* tag, tag length */
  18877. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18878. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18879. /* valid */
  18880. 0,
  18881. },
  18882. {
  18883. /* key, key length */
  18884. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18885. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18886. /* iv, iv length */
  18887. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18888. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18889. /* aad, aad length */
  18890. {0x00}, 0,
  18891. /* msg, msg length */
  18892. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18893. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18894. /* ct, ct length */
  18895. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18896. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18897. /* tag, tag length */
  18898. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18899. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18900. /* valid */
  18901. 0,
  18902. },
  18903. {
  18904. /* key, key length */
  18905. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18906. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18907. /* iv, iv length */
  18908. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18909. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18910. /* aad, aad length */
  18911. {0x00}, 0,
  18912. /* msg, msg length */
  18913. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18914. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18915. /* ct, ct length */
  18916. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18917. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18918. /* tag, tag length */
  18919. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18920. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18921. /* valid */
  18922. 0,
  18923. },
  18924. {
  18925. /* key, key length */
  18926. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18927. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18928. /* iv, iv length */
  18929. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18930. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18931. /* aad, aad length */
  18932. {0x00}, 0,
  18933. /* msg, msg length */
  18934. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18935. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18936. /* ct, ct length */
  18937. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18938. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18939. /* tag, tag length */
  18940. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18941. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18942. /* valid */
  18943. 0,
  18944. },
  18945. {
  18946. /* key, key length */
  18947. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18948. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18949. /* iv, iv length */
  18950. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18951. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18952. /* aad, aad length */
  18953. {0x00}, 0,
  18954. /* msg, msg length */
  18955. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18956. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18957. /* ct, ct length */
  18958. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18959. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18960. /* tag, tag length */
  18961. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18962. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18963. /* valid */
  18964. 0,
  18965. },
  18966. {
  18967. /* key, key length */
  18968. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18969. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18970. /* iv, iv length */
  18971. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18972. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18973. /* aad, aad length */
  18974. {0x00}, 0,
  18975. /* msg, msg length */
  18976. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18977. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18978. /* ct, ct length */
  18979. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18980. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18981. /* tag, tag length */
  18982. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18983. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18984. /* valid */
  18985. 0,
  18986. },
  18987. {
  18988. /* key, key length */
  18989. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18990. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18991. /* iv, iv length */
  18992. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18993. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18994. /* aad, aad length */
  18995. {0x00}, 0,
  18996. /* msg, msg length */
  18997. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18998. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18999. /* ct, ct length */
  19000. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19001. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19002. /* tag, tag length */
  19003. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19004. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  19005. /* valid */
  19006. 0,
  19007. },
  19008. {
  19009. /* key, key length */
  19010. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19011. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19012. /* iv, iv length */
  19013. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19014. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19015. /* aad, aad length */
  19016. {0x00}, 0,
  19017. /* msg, msg length */
  19018. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19019. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19020. /* ct, ct length */
  19021. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19022. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19023. /* tag, tag length */
  19024. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19025. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  19026. /* valid */
  19027. 0,
  19028. },
  19029. {
  19030. /* key, key length */
  19031. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19032. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19033. /* iv, iv length */
  19034. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19035. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19036. /* aad, aad length */
  19037. {0x00}, 0,
  19038. /* msg, msg length */
  19039. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19040. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19041. /* ct, ct length */
  19042. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19043. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19044. /* tag, tag length */
  19045. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19046. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  19047. /* valid */
  19048. 0,
  19049. },
  19050. {
  19051. /* key, key length */
  19052. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19053. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19054. /* iv, iv length */
  19055. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19056. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19057. /* aad, aad length */
  19058. {0x00}, 0,
  19059. /* msg, msg length */
  19060. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19061. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19062. /* ct, ct length */
  19063. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19064. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19065. /* tag, tag length */
  19066. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19067. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  19068. /* valid */
  19069. 0,
  19070. },
  19071. {
  19072. /* key, key length */
  19073. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19074. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19075. /* iv, iv length */
  19076. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19077. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19078. /* aad, aad length */
  19079. {0x00}, 0,
  19080. /* msg, msg length */
  19081. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19082. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19083. /* ct, ct length */
  19084. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19085. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19086. /* tag, tag length */
  19087. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19088. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  19089. /* valid */
  19090. 0,
  19091. },
  19092. {
  19093. /* key, key length */
  19094. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19095. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19096. /* iv, iv length */
  19097. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19098. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19099. /* aad, aad length */
  19100. {0x00}, 0,
  19101. /* msg, msg length */
  19102. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19103. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19104. /* ct, ct length */
  19105. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19106. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19107. /* tag, tag length */
  19108. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19109. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19110. /* valid */
  19111. 0,
  19112. },
  19113. {
  19114. /* key, key length */
  19115. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19116. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19117. /* iv, iv length */
  19118. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19119. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19120. /* aad, aad length */
  19121. {0x00}, 0,
  19122. /* msg, msg length */
  19123. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19124. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19125. /* ct, ct length */
  19126. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19127. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19128. /* tag, tag length */
  19129. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19130. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19131. /* valid */
  19132. 0,
  19133. },
  19134. {
  19135. /* key, key length */
  19136. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19137. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19138. /* iv, iv length */
  19139. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19140. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19141. /* aad, aad length */
  19142. {0x00}, 0,
  19143. /* msg, msg length */
  19144. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19145. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19146. /* ct, ct length */
  19147. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19148. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19149. /* tag, tag length */
  19150. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  19151. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19152. /* valid */
  19153. 0,
  19154. },
  19155. {
  19156. /* key, key length */
  19157. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19158. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19159. /* iv, iv length */
  19160. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19161. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19162. /* aad, aad length */
  19163. {0x00}, 0,
  19164. /* msg, msg length */
  19165. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19166. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19167. /* ct, ct length */
  19168. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19169. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19170. /* tag, tag length */
  19171. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19172. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19173. /* valid */
  19174. 0,
  19175. },
  19176. {
  19177. /* key, key length */
  19178. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19179. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19180. /* iv, iv length */
  19181. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19182. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19183. /* aad, aad length */
  19184. {0x00}, 0,
  19185. /* msg, msg length */
  19186. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19187. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19188. /* ct, ct length */
  19189. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19190. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19191. /* tag, tag length */
  19192. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  19193. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  19194. /* valid */
  19195. 0,
  19196. },
  19197. {
  19198. /* key, key length */
  19199. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19200. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19201. /* iv, iv length */
  19202. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19203. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19204. /* aad, aad length */
  19205. {0x00}, 0,
  19206. /* msg, msg length */
  19207. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19208. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19209. /* ct, ct length */
  19210. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19211. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19212. /* tag, tag length */
  19213. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19215. /* valid */
  19216. 0,
  19217. },
  19218. {
  19219. /* key, key length */
  19220. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19221. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19222. /* iv, iv length */
  19223. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19224. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19225. /* aad, aad length */
  19226. {0x00}, 0,
  19227. /* msg, msg length */
  19228. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19229. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19230. /* ct, ct length */
  19231. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19232. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19233. /* tag, tag length */
  19234. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19235. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19236. /* valid */
  19237. 0,
  19238. },
  19239. {
  19240. /* key, key length */
  19241. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19242. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19243. /* iv, iv length */
  19244. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19245. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19246. /* aad, aad length */
  19247. {0x00}, 0,
  19248. /* msg, msg length */
  19249. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19250. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19251. /* ct, ct length */
  19252. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19253. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19254. /* tag, tag length */
  19255. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19256. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19257. /* valid */
  19258. 0,
  19259. },
  19260. {
  19261. /* key, key length */
  19262. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19263. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19264. /* iv, iv length */
  19265. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19266. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19267. /* aad, aad length */
  19268. {0x00}, 0,
  19269. /* msg, msg length */
  19270. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19271. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19272. /* ct, ct length */
  19273. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19274. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19275. /* tag, tag length */
  19276. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19277. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19278. /* valid */
  19279. 0,
  19280. },
  19281. };
  19282. byte ciphertext[sizeof(vectors[0].ct)];
  19283. byte authtag[sizeof(vectors[0].tag)];
  19284. int i;
  19285. int len;
  19286. int ret;
  19287. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19288. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19289. len = sizeof(authtag);
  19290. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19291. ciphertext,
  19292. vectors[i].msg, vectors[i].msg_length,
  19293. vectors[i].iv, vectors[i].iv_length,
  19294. authtag, len,
  19295. vectors[i].aad, vectors[i].aad_length),
  19296. 0);
  19297. /* check ciphertext matches vector */
  19298. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19299. 0);
  19300. /* check that computed tag matches vector only for vectors marked as valid */
  19301. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19302. if (vectors[i].valid) {
  19303. ExpectIntEQ(ret, 0);
  19304. }
  19305. else {
  19306. ExpectIntNE(ret, 0);
  19307. }
  19308. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19309. /* Decrypt, checking that the computed auth tags match */
  19310. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19311. ciphertext,
  19312. vectors[i].ct, vectors[i].ct_length,
  19313. vectors[i].iv, vectors[i].iv_length,
  19314. authtag, len,
  19315. vectors[i].aad, vectors[i].aad_length),
  19316. 0);
  19317. /* check decrypted ciphertext matches vector plaintext */
  19318. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19319. 0);
  19320. }
  19321. return EXPECT_RESULT();
  19322. } /* END test_wc_AesEaxVectors */
  19323. /*
  19324. * Testing test_wc_AesEaxEncryptAuth()
  19325. */
  19326. static int test_wc_AesEaxEncryptAuth(void)
  19327. {
  19328. EXPECT_DECLS;
  19329. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19330. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19331. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19332. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19333. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19334. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19335. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19336. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19337. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19338. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19339. byte ciphertext[sizeof(msg)];
  19340. byte authtag[AES_BLOCK_SIZE];
  19341. int i;
  19342. int len;
  19343. len = sizeof(authtag);
  19344. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19345. ciphertext,
  19346. msg, sizeof(msg),
  19347. iv, sizeof(iv),
  19348. authtag, (word32)len,
  19349. aad, sizeof(aad)),
  19350. 0);
  19351. /* Test null checking */
  19352. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19353. ciphertext,
  19354. msg, sizeof(msg),
  19355. iv, sizeof(iv),
  19356. authtag, (word32)len,
  19357. aad, sizeof(aad)),
  19358. BAD_FUNC_ARG);
  19359. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19360. NULL,
  19361. msg, sizeof(msg),
  19362. iv, sizeof(iv),
  19363. authtag, (word32)len,
  19364. aad, sizeof(aad)),
  19365. BAD_FUNC_ARG);
  19366. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19367. ciphertext,
  19368. NULL, sizeof(msg),
  19369. iv, sizeof(iv),
  19370. authtag, (word32)len,
  19371. aad, sizeof(aad)),
  19372. BAD_FUNC_ARG);
  19373. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19374. ciphertext,
  19375. msg, sizeof(msg),
  19376. NULL, sizeof(iv),
  19377. authtag, (word32)len,
  19378. aad, sizeof(aad)),
  19379. BAD_FUNC_ARG);
  19380. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19381. ciphertext,
  19382. msg, sizeof(msg),
  19383. iv, sizeof(iv),
  19384. NULL, (word32)len,
  19385. aad, sizeof(aad)),
  19386. BAD_FUNC_ARG);
  19387. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19388. ciphertext,
  19389. msg, sizeof(msg),
  19390. iv, sizeof(iv),
  19391. authtag, (word32)len,
  19392. NULL, sizeof(aad)),
  19393. BAD_FUNC_ARG);
  19394. /* Test bad key lengths */
  19395. for (i = 0; i <= 32; i++) {
  19396. int exp_ret;
  19397. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19398. || i == AES_256_KEY_SIZE) {
  19399. exp_ret = 0;
  19400. }
  19401. else {
  19402. exp_ret = BAD_FUNC_ARG;
  19403. }
  19404. ExpectIntEQ(wc_AesEaxEncryptAuth(key, (word32)i,
  19405. ciphertext,
  19406. msg, sizeof(msg),
  19407. iv, sizeof(iv),
  19408. authtag, (word32)len,
  19409. aad, sizeof(aad)),
  19410. exp_ret);
  19411. }
  19412. /* Test auth tag size out of range */
  19413. len = AES_BLOCK_SIZE + 1;
  19414. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19415. ciphertext,
  19416. msg, sizeof(msg),
  19417. iv, sizeof(iv),
  19418. authtag, (word32)len,
  19419. aad, sizeof(aad)),
  19420. BAD_FUNC_ARG);
  19421. return EXPECT_RESULT();
  19422. } /* END test_wc_AesEaxEncryptAuth() */
  19423. /*
  19424. * Testing test_wc_AesEaxDecryptAuth()
  19425. */
  19426. static int test_wc_AesEaxDecryptAuth(void)
  19427. {
  19428. EXPECT_DECLS;
  19429. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19430. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19431. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19432. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19433. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19434. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19435. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19436. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19437. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19438. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19439. /* Garbage tag that should always fail for above aad */
  19440. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19441. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19442. byte plaintext[sizeof(ct)];
  19443. int i;
  19444. int len;
  19445. len = sizeof(tag);
  19446. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19447. plaintext,
  19448. ct, sizeof(ct),
  19449. iv, sizeof(iv),
  19450. tag, (word32)len,
  19451. aad, sizeof(aad)),
  19452. AES_EAX_AUTH_E);
  19453. /* Test null checking */
  19454. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19455. plaintext,
  19456. ct, sizeof(ct),
  19457. iv, sizeof(iv),
  19458. tag, (word32)len,
  19459. aad, sizeof(aad)),
  19460. BAD_FUNC_ARG);
  19461. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19462. NULL,
  19463. ct, sizeof(ct),
  19464. iv, sizeof(iv),
  19465. tag, (word32)len,
  19466. aad, sizeof(aad)),
  19467. BAD_FUNC_ARG);
  19468. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19469. plaintext,
  19470. NULL, sizeof(ct),
  19471. iv, sizeof(iv),
  19472. tag, (word32)len,
  19473. aad, sizeof(aad)),
  19474. BAD_FUNC_ARG);
  19475. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19476. plaintext,
  19477. ct, sizeof(ct),
  19478. NULL, sizeof(iv),
  19479. tag, (word32)len,
  19480. aad, sizeof(aad)),
  19481. BAD_FUNC_ARG);
  19482. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19483. plaintext,
  19484. ct, sizeof(ct),
  19485. iv, sizeof(iv),
  19486. NULL, (word32)len,
  19487. aad, sizeof(aad)),
  19488. BAD_FUNC_ARG);
  19489. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19490. plaintext,
  19491. ct, sizeof(ct),
  19492. iv, sizeof(iv),
  19493. tag, (word32)len,
  19494. NULL, sizeof(aad)),
  19495. BAD_FUNC_ARG);
  19496. /* Test bad key lengths */
  19497. for (i = 0; i <= 32; i++) {
  19498. int exp_ret;
  19499. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19500. || i == AES_256_KEY_SIZE) {
  19501. exp_ret = AES_EAX_AUTH_E;
  19502. }
  19503. else {
  19504. exp_ret = BAD_FUNC_ARG;
  19505. }
  19506. ExpectIntEQ(wc_AesEaxDecryptAuth(key, (word32)i,
  19507. plaintext,
  19508. ct, sizeof(ct),
  19509. iv, sizeof(iv),
  19510. tag, (word32)len,
  19511. aad, sizeof(aad)),
  19512. exp_ret);
  19513. }
  19514. /* Test auth tag size out of range */
  19515. len = AES_BLOCK_SIZE + 1;
  19516. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19517. plaintext,
  19518. ct, sizeof(ct),
  19519. iv, sizeof(iv),
  19520. tag, (word32)len,
  19521. aad, sizeof(aad)),
  19522. BAD_FUNC_ARG);
  19523. return EXPECT_RESULT();
  19524. } /* END test_wc_AesEaxDecryptAuth() */
  19525. #endif /* WOLFSSL_AES_EAX &&
  19526. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19527. */
  19528. /*
  19529. * Testing wc_InitDsaKey()
  19530. */
  19531. static int test_wc_InitDsaKey(void)
  19532. {
  19533. EXPECT_DECLS;
  19534. #ifndef NO_DSA
  19535. DsaKey key;
  19536. XMEMSET(&key, 0, sizeof(DsaKey));
  19537. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19538. /* Pass in bad args. */
  19539. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19540. wc_FreeDsaKey(&key);
  19541. #endif
  19542. return EXPECT_RESULT();
  19543. } /* END test_wc_InitDsaKey */
  19544. /*
  19545. * Testing wc_DsaSign() and wc_DsaVerify()
  19546. */
  19547. static int test_wc_DsaSignVerify(void)
  19548. {
  19549. EXPECT_DECLS;
  19550. #if !defined(NO_DSA)
  19551. DsaKey key;
  19552. WC_RNG rng;
  19553. wc_Sha sha;
  19554. byte signature[DSA_SIG_SIZE];
  19555. byte hash[WC_SHA_DIGEST_SIZE];
  19556. word32 idx = 0;
  19557. word32 bytes;
  19558. int answer;
  19559. #ifdef USE_CERT_BUFFERS_1024
  19560. byte tmp[ONEK_BUF];
  19561. XMEMSET(tmp, 0, sizeof(tmp));
  19562. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19563. bytes = sizeof_dsa_key_der_1024;
  19564. #elif defined(USE_CERT_BUFFERS_2048)
  19565. byte tmp[TWOK_BUF];
  19566. XMEMSET(tmp, 0, sizeof(tmp));
  19567. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19568. bytes = sizeof_dsa_key_der_2048;
  19569. #else
  19570. byte tmp[TWOK_BUF];
  19571. XFILE fp = XBADFILE;
  19572. XMEMSET(tmp, 0, sizeof(tmp));
  19573. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19574. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19575. if (fp != XBADFILE)
  19576. XFCLOSE(fp);
  19577. #endif /* END USE_CERT_BUFFERS_1024 */
  19578. ExpectIntEQ(wc_InitSha(&sha), 0);
  19579. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19580. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19581. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19582. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19583. ExpectIntEQ(wc_InitRng(&rng), 0);
  19584. /* Sign. */
  19585. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19586. /* Test bad args. */
  19587. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19588. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19589. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19590. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19591. /* Verify. */
  19592. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19593. ExpectIntEQ(answer, 1);
  19594. /* Pass in bad args. */
  19595. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19596. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19597. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19598. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19599. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19600. /* hard set q to 0 and test fail case */
  19601. mp_free(&key.q);
  19602. mp_init(&key.q);
  19603. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19604. mp_set(&key.q, 1);
  19605. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19606. #endif
  19607. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19608. wc_FreeDsaKey(&key);
  19609. wc_ShaFree(&sha);
  19610. #endif
  19611. return EXPECT_RESULT();
  19612. } /* END test_wc_DsaSign */
  19613. /*
  19614. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19615. */
  19616. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19617. {
  19618. EXPECT_DECLS;
  19619. #if !defined(NO_DSA)
  19620. DsaKey key;
  19621. word32 bytes = 0;
  19622. word32 idx = 0;
  19623. int ret = 0;
  19624. #ifdef USE_CERT_BUFFERS_1024
  19625. byte tmp[ONEK_BUF];
  19626. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19627. bytes = sizeof_dsa_key_der_1024;
  19628. #elif defined(USE_CERT_BUFFERS_2048)
  19629. byte tmp[TWOK_BUF];
  19630. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19631. bytes = sizeof_dsa_key_der_2048;
  19632. #else
  19633. byte tmp[TWOK_BUF];
  19634. XFILE fp = XBADFILE;
  19635. XMEMSET(tmp, 0, sizeof(tmp));
  19636. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19637. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19638. if (fp != XBADFILE)
  19639. XFCLOSE(fp);
  19640. #endif /* END USE_CERT_BUFFERS_1024 */
  19641. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19642. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19643. /* Test bad args. */
  19644. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19645. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19646. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19647. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19648. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19649. wc_FreeDsaKey(&key);
  19650. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19651. idx = 0; /* Reset */
  19652. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19653. /* Test bad args. */
  19654. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19655. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19656. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19657. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19658. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19659. wc_FreeDsaKey(&key);
  19660. #endif /* !NO_DSA */
  19661. return EXPECT_RESULT();
  19662. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19663. /*
  19664. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19665. */
  19666. static int test_wc_MakeDsaKey(void)
  19667. {
  19668. EXPECT_DECLS;
  19669. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19670. DsaKey genKey;
  19671. WC_RNG rng;
  19672. XMEMSET(&genKey, 0, sizeof(genKey));
  19673. XMEMSET(&rng, 0, sizeof(rng));
  19674. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19675. ExpectIntEQ(wc_InitRng(&rng), 0);
  19676. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19677. /* Test bad args. */
  19678. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19679. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19680. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19681. BAD_FUNC_ARG);
  19682. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19683. /* Test bad args. */
  19684. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19685. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19686. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19687. wc_FreeDsaKey(&genKey);
  19688. #endif
  19689. return EXPECT_RESULT();
  19690. } /* END test_wc_MakeDsaKey */
  19691. /*
  19692. * Testing wc_DsaKeyToDer()
  19693. */
  19694. static int test_wc_DsaKeyToDer(void)
  19695. {
  19696. EXPECT_DECLS;
  19697. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19698. DsaKey key;
  19699. word32 bytes;
  19700. word32 idx = 0;
  19701. #ifdef USE_CERT_BUFFERS_1024
  19702. byte tmp[ONEK_BUF];
  19703. byte der[ONEK_BUF];
  19704. XMEMSET(tmp, 0, sizeof(tmp));
  19705. XMEMSET(der, 0, sizeof(der));
  19706. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19707. bytes = sizeof_dsa_key_der_1024;
  19708. #elif defined(USE_CERT_BUFFERS_2048)
  19709. byte tmp[TWOK_BUF];
  19710. byte der[TWOK_BUF];
  19711. XMEMSET(tmp, 0, sizeof(tmp));
  19712. XMEMSET(der, 0, sizeof(der));
  19713. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19714. bytes = sizeof_dsa_key_der_2048;
  19715. #else
  19716. byte tmp[TWOK_BUF];
  19717. byte der[TWOK_BUF];
  19718. XFILE fp = XBADFILE;
  19719. XMEMSET(tmp, 0, sizeof(tmp));
  19720. XMEMSET(der, 0, sizeof(der));
  19721. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19722. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19723. if (fp != XBADFILE)
  19724. XFCLOSE(fp);
  19725. #endif /* END USE_CERT_BUFFERS_1024 */
  19726. XMEMSET(&key, 0, sizeof(DsaKey));
  19727. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19728. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19729. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19730. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19731. /* Test bad args. */
  19732. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19733. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19734. wc_FreeDsaKey(&key);
  19735. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19736. return EXPECT_RESULT();
  19737. } /* END test_wc_DsaKeyToDer */
  19738. /*
  19739. * Testing wc_DsaKeyToPublicDer()
  19740. * (indirectly testing setDsaPublicKey())
  19741. */
  19742. static int test_wc_DsaKeyToPublicDer(void)
  19743. {
  19744. EXPECT_DECLS;
  19745. #ifndef HAVE_SELFTEST
  19746. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19747. DsaKey key;
  19748. WC_RNG rng;
  19749. byte* der = NULL;
  19750. word32 sz = 0;
  19751. word32 idx = 0;
  19752. XMEMSET(&key, 0, sizeof(DsaKey));
  19753. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19754. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19755. DYNAMIC_TYPE_TMP_BUFFER));
  19756. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19757. ExpectIntEQ(wc_InitRng(&rng), 0);
  19758. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19759. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19760. ExpectIntGE(sz = (word32)wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19761. wc_FreeDsaKey(&key);
  19762. idx = 0;
  19763. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19764. /* Test without the SubjectPublicKeyInfo header */
  19765. ExpectIntGE(sz = (word32)wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19766. wc_FreeDsaKey(&key);
  19767. idx = 0;
  19768. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19769. /* Test bad args. */
  19770. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19771. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19772. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19773. wc_FreeDsaKey(&key);
  19774. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19775. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19776. #endif /* !HAVE_SELFTEST */
  19777. return EXPECT_RESULT();
  19778. } /* END test_wc_DsaKeyToPublicDer */
  19779. /*
  19780. * Testing wc_DsaImportParamsRaw()
  19781. */
  19782. static int test_wc_DsaImportParamsRaw(void)
  19783. {
  19784. EXPECT_DECLS;
  19785. #if !defined(NO_DSA)
  19786. DsaKey key;
  19787. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19788. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19789. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19790. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19791. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19792. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19793. "80a0093113a8bd73";
  19794. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19795. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19796. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19797. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19798. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19799. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19800. "76341a7e7d9";
  19801. /* invalid p and q parameters */
  19802. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19803. const char* invalidQ = "96c5390a";
  19804. XMEMSET(&key, 0, sizeof(DsaKey));
  19805. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19806. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19807. /* test bad args */
  19808. /* null key struct */
  19809. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19810. /* null param pointers */
  19811. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19812. /* illegal p length */
  19813. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19814. /* illegal q length */
  19815. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19816. wc_FreeDsaKey(&key);
  19817. #endif
  19818. return EXPECT_RESULT();
  19819. } /* END test_wc_DsaImportParamsRaw */
  19820. /*
  19821. * Testing wc_DsaImportParamsRawCheck()
  19822. */
  19823. static int test_wc_DsaImportParamsRawCheck(void)
  19824. {
  19825. EXPECT_DECLS;
  19826. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19827. DsaKey key;
  19828. int trusted = 0;
  19829. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19830. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19831. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19832. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19833. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19834. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19835. "80a0093113a8bd73";
  19836. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19837. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19838. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19839. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19840. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19841. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19842. "76341a7e7d9";
  19843. /* invalid p and q parameters */
  19844. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19845. const char* invalidQ = "96c5390a";
  19846. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19847. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19848. /* test bad args */
  19849. /* null key struct */
  19850. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19851. BAD_FUNC_ARG);
  19852. /* null param pointers */
  19853. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19854. NULL), BAD_FUNC_ARG);
  19855. /* illegal p length */
  19856. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19857. BAD_FUNC_ARG);
  19858. /* illegal q length */
  19859. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19860. BAD_FUNC_ARG);
  19861. wc_FreeDsaKey(&key);
  19862. #endif
  19863. return EXPECT_RESULT();
  19864. } /* END test_wc_DsaImportParamsRawCheck */
  19865. /*
  19866. * Testing wc_DsaExportParamsRaw()
  19867. */
  19868. static int test_wc_DsaExportParamsRaw(void)
  19869. {
  19870. EXPECT_DECLS;
  19871. #if !defined(NO_DSA)
  19872. DsaKey key;
  19873. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19874. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19875. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19876. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19877. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19878. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19879. "80a0093113a8bd73";
  19880. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19881. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19882. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19883. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19884. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19885. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19886. "76341a7e7d9";
  19887. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19888. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19889. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19890. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19891. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19892. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19893. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19894. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19895. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19896. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19897. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19898. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19899. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19900. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19901. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19902. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19903. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19904. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19905. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19906. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19907. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19908. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19909. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19910. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19911. byte pOut[MAX_DSA_PARAM_SIZE];
  19912. byte qOut[MAX_DSA_PARAM_SIZE];
  19913. byte gOut[MAX_DSA_PARAM_SIZE];
  19914. word32 pOutSz;
  19915. word32 qOutSz;
  19916. word32 gOutSz;
  19917. XMEMSET(&key, 0, sizeof(DsaKey));
  19918. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19919. /* first test using imported raw parameters, for expected */
  19920. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19921. pOutSz = sizeof(pOut);
  19922. qOutSz = sizeof(qOut);
  19923. gOutSz = sizeof(gOut);
  19924. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19925. &gOutSz), 0);
  19926. /* validate exported parameters are correct */
  19927. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19928. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19929. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19930. /* test bad args */
  19931. /* null key struct */
  19932. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19933. &gOutSz), BAD_FUNC_ARG);
  19934. /* null output pointers */
  19935. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19936. &gOutSz), LENGTH_ONLY_E);
  19937. /* null output size pointers */
  19938. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19939. NULL), BAD_FUNC_ARG);
  19940. /* p output buffer size too small */
  19941. pOutSz = 1;
  19942. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19943. &gOutSz), BUFFER_E);
  19944. pOutSz = sizeof(pOut);
  19945. /* q output buffer size too small */
  19946. qOutSz = 1;
  19947. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19948. &gOutSz), BUFFER_E);
  19949. qOutSz = sizeof(qOut);
  19950. /* g output buffer size too small */
  19951. gOutSz = 1;
  19952. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19953. &gOutSz), BUFFER_E);
  19954. wc_FreeDsaKey(&key);
  19955. #endif
  19956. return EXPECT_RESULT();
  19957. } /* END test_wc_DsaExportParamsRaw */
  19958. /*
  19959. * Testing wc_DsaExportKeyRaw()
  19960. */
  19961. static int test_wc_DsaExportKeyRaw(void)
  19962. {
  19963. EXPECT_DECLS;
  19964. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19965. DsaKey key;
  19966. WC_RNG rng;
  19967. byte xOut[MAX_DSA_PARAM_SIZE];
  19968. byte yOut[MAX_DSA_PARAM_SIZE];
  19969. word32 xOutSz, yOutSz;
  19970. XMEMSET(&key, 0, sizeof(key));
  19971. XMEMSET(&rng, 0, sizeof(rng));
  19972. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19973. ExpectIntEQ(wc_InitRng(&rng), 0);
  19974. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19975. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19976. /* try successful export */
  19977. xOutSz = sizeof(xOut);
  19978. yOutSz = sizeof(yOut);
  19979. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19980. /* test bad args */
  19981. /* null key struct */
  19982. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19983. BAD_FUNC_ARG);
  19984. /* null output pointers */
  19985. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19986. LENGTH_ONLY_E);
  19987. /* null output size pointers */
  19988. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19989. BAD_FUNC_ARG);
  19990. /* x output buffer size too small */
  19991. xOutSz = 1;
  19992. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19993. BUFFER_E);
  19994. xOutSz = sizeof(xOut);
  19995. /* y output buffer size too small */
  19996. yOutSz = 1;
  19997. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19998. BUFFER_E);
  19999. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20000. wc_FreeDsaKey(&key);
  20001. #endif
  20002. return EXPECT_RESULT();
  20003. } /* END test_wc_DsaExportParamsRaw */
  20004. /*
  20005. * Testing wc_ed25519_make_key().
  20006. */
  20007. static int test_wc_ed25519_make_key(void)
  20008. {
  20009. EXPECT_DECLS;
  20010. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  20011. ed25519_key key;
  20012. WC_RNG rng;
  20013. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  20014. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  20015. XMEMSET(&key, 0, sizeof(ed25519_key));
  20016. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20017. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20018. ExpectIntEQ(wc_InitRng(&rng), 0);
  20019. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, (word32)pubkey_sz),
  20020. ECC_PRIV_KEY_E);
  20021. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, (word32)pubkey_sz),
  20022. ECC_PRIV_KEY_E);
  20023. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20024. /* Test bad args. */
  20025. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  20026. BAD_FUNC_ARG);
  20027. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  20028. BAD_FUNC_ARG);
  20029. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  20030. BAD_FUNC_ARG);
  20031. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  20032. BAD_FUNC_ARG);
  20033. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20034. wc_ed25519_free(&key);
  20035. #endif
  20036. return EXPECT_RESULT();
  20037. } /* END test_wc_ed25519_make_key */
  20038. /*
  20039. * Testing wc_ed25519_init()
  20040. */
  20041. static int test_wc_ed25519_init(void)
  20042. {
  20043. EXPECT_DECLS;
  20044. #if defined(HAVE_ED25519)
  20045. ed25519_key key;
  20046. XMEMSET(&key, 0, sizeof(ed25519_key));
  20047. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20048. /* Test bad args. */
  20049. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  20050. wc_ed25519_free(&key);
  20051. #endif
  20052. return EXPECT_RESULT();
  20053. } /* END test_wc_ed25519_init */
  20054. /*
  20055. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  20056. */
  20057. static int test_wc_ed25519_sign_msg(void)
  20058. {
  20059. EXPECT_DECLS;
  20060. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  20061. WC_RNG rng;
  20062. ed25519_key key;
  20063. byte msg[] = "Everybody gets Friday off.\n";
  20064. byte sig[ED25519_SIG_SIZE+1];
  20065. word32 msglen = sizeof(msg);
  20066. word32 siglen = ED25519_SIG_SIZE;
  20067. word32 badSigLen = ED25519_SIG_SIZE - 1;
  20068. #ifdef HAVE_ED25519_VERIFY
  20069. int verify_ok = 0; /*1 = Verify success.*/
  20070. #endif
  20071. /* Initialize stack variables. */
  20072. XMEMSET(&key, 0, sizeof(ed25519_key));
  20073. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20074. XMEMSET(sig, 0, sizeof(sig));
  20075. /* Initialize key. */
  20076. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20077. ExpectIntEQ(wc_InitRng(&rng), 0);
  20078. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20079. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  20080. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20081. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  20082. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20083. /* Test bad args. */
  20084. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  20085. BAD_FUNC_ARG);
  20086. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  20087. BAD_FUNC_ARG);
  20088. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  20089. BAD_FUNC_ARG);
  20090. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  20091. BAD_FUNC_ARG);
  20092. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  20093. BUFFER_E);
  20094. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  20095. badSigLen -= 1;
  20096. #ifdef HAVE_ED25519_VERIFY
  20097. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20098. &key), 0);
  20099. ExpectIntEQ(verify_ok, 1);
  20100. /* Test bad args. */
  20101. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  20102. &verify_ok, &key), BAD_FUNC_ARG);
  20103. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  20104. &verify_ok, &key), BAD_FUNC_ARG);
  20105. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20106. &key), BAD_FUNC_ARG);
  20107. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  20108. &key), BAD_FUNC_ARG);
  20109. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  20110. BAD_FUNC_ARG);
  20111. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20112. NULL), BAD_FUNC_ARG);
  20113. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  20114. &key), BAD_FUNC_ARG);
  20115. #endif /* Verify. */
  20116. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20117. wc_ed25519_free(&key);
  20118. #endif
  20119. return EXPECT_RESULT();
  20120. } /* END test_wc_ed25519_sign_msg */
  20121. /*
  20122. * Testing wc_ed25519_import_public()
  20123. */
  20124. static int test_wc_ed25519_import_public(void)
  20125. {
  20126. EXPECT_DECLS;
  20127. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20128. ed25519_key pubKey;
  20129. WC_RNG rng;
  20130. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20131. word32 inlen = sizeof(in);
  20132. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  20133. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20134. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  20135. ExpectIntEQ(wc_InitRng(&rng), 0);
  20136. #ifdef HAVE_ED25519_MAKE_KEY
  20137. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  20138. #endif
  20139. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  20140. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20141. /* Test bad args. */
  20142. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20143. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20144. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20145. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20146. wc_ed25519_free(&pubKey);
  20147. #endif
  20148. return EXPECT_RESULT();
  20149. } /* END wc_ed25519_import_public */
  20150. /*
  20151. * Testing wc_ed25519_import_private_key()
  20152. */
  20153. static int test_wc_ed25519_import_private_key(void)
  20154. {
  20155. EXPECT_DECLS;
  20156. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20157. ed25519_key key;
  20158. WC_RNG rng;
  20159. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20160. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20161. word32 privKeySz = sizeof(privKey);
  20162. word32 pubKeySz = sizeof(pubKey);
  20163. #ifdef HAVE_ED25519_KEY_EXPORT
  20164. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20165. word32 bothKeysSz = sizeof(bothKeys);
  20166. #endif
  20167. XMEMSET(&key, 0, sizeof(ed25519_key));
  20168. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20169. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20170. ExpectIntEQ(wc_InitRng(&rng), 0);
  20171. #ifdef HAVE_ED25519_MAKE_KEY
  20172. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20173. #endif
  20174. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20175. pubKeySz, &key, 1), 0);
  20176. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20177. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20178. #ifdef HAVE_ED25519_KEY_EXPORT
  20179. PRIVATE_KEY_UNLOCK();
  20180. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  20181. PRIVATE_KEY_LOCK();
  20182. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20183. &key, 1), 0);
  20184. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20185. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20186. #endif
  20187. /* Test bad args. */
  20188. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20189. &key), BAD_FUNC_ARG);
  20190. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20191. pubKeySz, &key), BAD_FUNC_ARG);
  20192. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20193. pubKeySz, NULL), BAD_FUNC_ARG);
  20194. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20195. pubKeySz, &key), BAD_FUNC_ARG);
  20196. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20197. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20198. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20199. &key), BAD_FUNC_ARG);
  20200. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20201. wc_ed25519_free(&key);
  20202. #endif
  20203. return EXPECT_RESULT();
  20204. } /* END test_wc_ed25519_import_private_key */
  20205. /*
  20206. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20207. */
  20208. static int test_wc_ed25519_export(void)
  20209. {
  20210. EXPECT_DECLS;
  20211. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20212. ed25519_key key;
  20213. WC_RNG rng;
  20214. byte priv[ED25519_PRV_KEY_SIZE];
  20215. byte pub[ED25519_PUB_KEY_SIZE];
  20216. word32 privSz = sizeof(priv);
  20217. word32 pubSz = sizeof(pub);
  20218. #ifndef HAVE_ED25519_MAKE_KEY
  20219. const byte privKey[] = {
  20220. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20221. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20222. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20223. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20224. };
  20225. const byte pubKey[] = {
  20226. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20227. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20228. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20229. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20230. };
  20231. #endif
  20232. XMEMSET(&key, 0, sizeof(ed25519_key));
  20233. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20234. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20235. ExpectIntEQ(wc_InitRng(&rng), 0);
  20236. #ifdef HAVE_ED25519_MAKE_KEY
  20237. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20238. #else
  20239. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20240. pubKey, sizeof(pubKey), &key, 1), 0);
  20241. #endif
  20242. PRIVATE_KEY_UNLOCK();
  20243. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20244. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20245. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20246. /* Test bad args. */
  20247. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20248. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20249. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20250. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20251. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20252. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20253. /* Test bad args. */
  20254. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20255. BAD_FUNC_ARG);
  20256. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20257. BAD_FUNC_ARG);
  20258. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20259. BAD_FUNC_ARG);
  20260. PRIVATE_KEY_LOCK();
  20261. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20262. wc_ed25519_free(&key);
  20263. #endif
  20264. return EXPECT_RESULT();
  20265. } /* END test_wc_ed25519_export */
  20266. /*
  20267. * Testing wc_ed25519_size()
  20268. */
  20269. static int test_wc_ed25519_size(void)
  20270. {
  20271. EXPECT_DECLS;
  20272. #if defined(HAVE_ED25519)
  20273. ed25519_key key;
  20274. WC_RNG rng;
  20275. #ifndef HAVE_ED25519_MAKE_KEY
  20276. const byte privKey[] = {
  20277. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20278. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20279. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20280. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20281. };
  20282. const byte pubKey[] = {
  20283. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20284. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20285. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20286. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20287. };
  20288. #endif
  20289. XMEMSET(&key, 0, sizeof(ed25519_key));
  20290. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20291. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20292. ExpectIntEQ(wc_InitRng(&rng), 0);
  20293. #ifdef HAVE_ED25519_MAKE_KEY
  20294. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20295. #else
  20296. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20297. pubKey, sizeof(pubKey), &key, 1), 0);
  20298. #endif
  20299. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20300. /* Test bad args. */
  20301. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20302. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20303. /* Test bad args. */
  20304. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20305. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20306. /* Test bad args. */
  20307. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20308. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20309. /* Test bad args. */
  20310. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20311. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20312. wc_ed25519_free(&key);
  20313. #endif
  20314. return EXPECT_RESULT();
  20315. } /* END test_wc_ed25519_size */
  20316. /*
  20317. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20318. */
  20319. static int test_wc_ed25519_exportKey(void)
  20320. {
  20321. EXPECT_DECLS;
  20322. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20323. WC_RNG rng;
  20324. ed25519_key key;
  20325. byte priv[ED25519_PRV_KEY_SIZE];
  20326. byte pub[ED25519_PUB_KEY_SIZE];
  20327. byte privOnly[ED25519_PRV_KEY_SIZE];
  20328. word32 privSz = sizeof(priv);
  20329. word32 pubSz = sizeof(pub);
  20330. word32 privOnlySz = sizeof(privOnly);
  20331. #ifndef HAVE_ED25519_MAKE_KEY
  20332. const byte privKey[] = {
  20333. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20334. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20335. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20336. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20337. };
  20338. const byte pubKey[] = {
  20339. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20340. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20341. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20342. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20343. };
  20344. #endif
  20345. XMEMSET(&key, 0, sizeof(ed25519_key));
  20346. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20347. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20348. ExpectIntEQ(wc_InitRng(&rng), 0);
  20349. #ifdef HAVE_ED25519_MAKE_KEY
  20350. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20351. #else
  20352. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20353. pubKey, sizeof(pubKey), &key, 1), 0);
  20354. #endif
  20355. PRIVATE_KEY_UNLOCK();
  20356. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20357. /* Test bad args. */
  20358. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20359. BAD_FUNC_ARG);
  20360. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20361. BAD_FUNC_ARG);
  20362. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20363. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20364. /* Test bad args. */
  20365. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20366. BAD_FUNC_ARG);
  20367. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20368. BAD_FUNC_ARG);
  20369. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20370. BAD_FUNC_ARG);
  20371. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20372. BAD_FUNC_ARG);
  20373. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20374. BAD_FUNC_ARG);
  20375. PRIVATE_KEY_LOCK();
  20376. /* Cross check output. */
  20377. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20378. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20379. wc_ed25519_free(&key);
  20380. #endif
  20381. return EXPECT_RESULT();
  20382. } /* END test_wc_ed25519_exportKey */
  20383. /*
  20384. * Testing wc_Ed25519PublicKeyToDer
  20385. */
  20386. static int test_wc_Ed25519PublicKeyToDer(void)
  20387. {
  20388. EXPECT_DECLS;
  20389. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20390. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20391. ed25519_key key;
  20392. byte derBuf[1024];
  20393. XMEMSET(&key, 0, sizeof(ed25519_key));
  20394. /* Test bad args */
  20395. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20396. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20397. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20398. wc_ed25519_free(&key);
  20399. /* Test good args */
  20400. if (EXPECT_SUCCESS()) {
  20401. WC_RNG rng;
  20402. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20403. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20404. ExpectIntEQ(wc_InitRng(&rng), 0);
  20405. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20406. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20407. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20408. wc_ed25519_free(&key);
  20409. }
  20410. #endif
  20411. return EXPECT_RESULT();
  20412. } /* END testing wc_Ed25519PublicKeyToDer */
  20413. /*
  20414. * Testing wc_curve25519_init and wc_curve25519_free.
  20415. */
  20416. static int test_wc_curve25519_init(void)
  20417. {
  20418. EXPECT_DECLS;
  20419. #if defined(HAVE_CURVE25519)
  20420. curve25519_key key;
  20421. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20422. /* Test bad args for wc_curve25519_init */
  20423. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20424. /* Test good args for wc_curve_25519_free */
  20425. wc_curve25519_free(&key);
  20426. /* Test bad args for wc_curve25519 free. */
  20427. wc_curve25519_free(NULL);
  20428. #endif
  20429. return EXPECT_RESULT();
  20430. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20431. /*
  20432. * Testing test_wc_curve25519_size.
  20433. */
  20434. static int test_wc_curve25519_size(void)
  20435. {
  20436. EXPECT_DECLS;
  20437. #if defined(HAVE_CURVE25519)
  20438. curve25519_key key;
  20439. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20440. /* Test good args for wc_curve25519_size */
  20441. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20442. /* Test bad args for wc_curve25519_size */
  20443. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20444. wc_curve25519_free(&key);
  20445. #endif
  20446. return EXPECT_RESULT();
  20447. } /* END test_wc_curve25519_size*/
  20448. /*
  20449. * Testing test_wc_curve25519_export_key_raw().
  20450. */
  20451. static int test_wc_curve25519_export_key_raw(void)
  20452. {
  20453. EXPECT_DECLS;
  20454. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20455. curve25519_key key;
  20456. WC_RNG rng;
  20457. byte privateKey[CURVE25519_KEYSIZE];
  20458. byte publicKey[CURVE25519_KEYSIZE];
  20459. word32 prvkSz;
  20460. word32 pubkSz;
  20461. byte prik[CURVE25519_KEYSIZE];
  20462. byte pubk[CURVE25519_KEYSIZE];
  20463. word32 prksz;
  20464. word32 pbksz;
  20465. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20466. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20467. ExpectIntEQ(wc_InitRng(&rng), 0);
  20468. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20469. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20470. prvkSz = CURVE25519_KEYSIZE;
  20471. pubkSz = CURVE25519_KEYSIZE;
  20472. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20473. publicKey, &pubkSz), BAD_FUNC_ARG);
  20474. prvkSz = CURVE25519_KEYSIZE;
  20475. pubkSz = CURVE25519_KEYSIZE;
  20476. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20477. &pubkSz), BAD_FUNC_ARG);
  20478. prvkSz = CURVE25519_KEYSIZE;
  20479. pubkSz = CURVE25519_KEYSIZE;
  20480. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20481. publicKey, &pubkSz), BAD_FUNC_ARG);
  20482. /* prvkSz = CURVE25519_KEYSIZE; */
  20483. pubkSz = CURVE25519_KEYSIZE;
  20484. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20485. NULL, &pubkSz), BAD_FUNC_ARG);
  20486. prvkSz = CURVE25519_KEYSIZE;
  20487. pubkSz = CURVE25519_KEYSIZE;
  20488. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20489. publicKey, NULL), BAD_FUNC_ARG);
  20490. /* cross-testing */
  20491. prksz = CURVE25519_KEYSIZE;
  20492. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20493. pbksz = CURVE25519_KEYSIZE;
  20494. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20495. prvkSz = CURVE25519_KEYSIZE;
  20496. /* pubkSz = CURVE25519_KEYSIZE; */
  20497. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20498. publicKey, &pubkSz), 0);
  20499. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20500. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20501. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20502. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20503. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20504. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20505. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20506. wc_curve25519_free(&key);
  20507. #endif
  20508. return EXPECT_RESULT();
  20509. } /* end of test_wc_curve25519_export_key_raw */
  20510. /*
  20511. * Testing test_wc_curve25519_export_key_raw_ex().
  20512. */
  20513. static int test_wc_curve25519_export_key_raw_ex(void)
  20514. {
  20515. EXPECT_DECLS;
  20516. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20517. curve25519_key key;
  20518. WC_RNG rng;
  20519. byte privateKey[CURVE25519_KEYSIZE];
  20520. byte publicKey[CURVE25519_KEYSIZE];
  20521. word32 prvkSz;
  20522. word32 pubkSz;
  20523. byte prik[CURVE25519_KEYSIZE];
  20524. byte pubk[CURVE25519_KEYSIZE];
  20525. word32 prksz;
  20526. word32 pbksz;
  20527. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20528. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20529. ExpectIntEQ(wc_InitRng(&rng), 0);
  20530. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20531. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20532. prvkSz = CURVE25519_KEYSIZE;
  20533. pubkSz = CURVE25519_KEYSIZE;
  20534. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20535. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20536. prvkSz = CURVE25519_KEYSIZE;
  20537. pubkSz = CURVE25519_KEYSIZE;
  20538. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20539. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20540. prvkSz = CURVE25519_KEYSIZE;
  20541. pubkSz = CURVE25519_KEYSIZE;
  20542. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20543. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20544. /* prvkSz = CURVE25519_KEYSIZE; */
  20545. pubkSz = CURVE25519_KEYSIZE;
  20546. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20547. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20548. prvkSz = CURVE25519_KEYSIZE;
  20549. pubkSz = CURVE25519_KEYSIZE;
  20550. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20551. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20552. prvkSz = CURVE25519_KEYSIZE;
  20553. /* pubkSz = CURVE25519_KEYSIZE; */
  20554. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20555. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20556. prvkSz = CURVE25519_KEYSIZE;
  20557. pubkSz = CURVE25519_KEYSIZE;
  20558. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20559. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20560. prvkSz = CURVE25519_KEYSIZE;
  20561. pubkSz = CURVE25519_KEYSIZE;
  20562. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20563. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20564. /* prvkSz = CURVE25519_KEYSIZE; */
  20565. pubkSz = CURVE25519_KEYSIZE;
  20566. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20567. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20568. prvkSz = CURVE25519_KEYSIZE;
  20569. pubkSz = CURVE25519_KEYSIZE;
  20570. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20571. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20572. /* illegal value for endian */
  20573. prvkSz = CURVE25519_KEYSIZE;
  20574. /* pubkSz = CURVE25519_KEYSIZE; */
  20575. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20576. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20577. /* cross-testing */
  20578. prksz = CURVE25519_KEYSIZE;
  20579. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20580. pbksz = CURVE25519_KEYSIZE;
  20581. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20582. prvkSz = CURVE25519_KEYSIZE;
  20583. /* pubkSz = CURVE25519_KEYSIZE; */
  20584. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20585. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20586. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20587. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20588. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20589. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20590. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20591. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20592. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20593. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20594. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20595. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20596. /* try once with another endian */
  20597. prvkSz = CURVE25519_KEYSIZE;
  20598. pubkSz = CURVE25519_KEYSIZE;
  20599. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20600. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20601. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20602. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20603. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20604. wc_curve25519_free(&key);
  20605. #endif
  20606. return EXPECT_RESULT();
  20607. } /* end of test_wc_curve25519_export_key_raw_ex */
  20608. /*
  20609. * Testing wc_curve25519_make_key
  20610. */
  20611. static int test_wc_curve25519_make_key(void)
  20612. {
  20613. EXPECT_DECLS;
  20614. #if defined(HAVE_CURVE25519)
  20615. curve25519_key key;
  20616. WC_RNG rng;
  20617. int keysize;
  20618. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20619. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20620. ExpectIntEQ(wc_InitRng(&rng), 0);
  20621. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20622. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20623. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20624. /* test bad cases*/
  20625. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20626. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20627. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20628. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20629. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20630. wc_curve25519_free(&key);
  20631. #endif
  20632. return EXPECT_RESULT();
  20633. } /* END test_wc_curve25519_make_key*/
  20634. /*
  20635. * Testing wc_curve25519_shared_secret_ex
  20636. */
  20637. static int test_wc_curve25519_shared_secret_ex(void)
  20638. {
  20639. EXPECT_DECLS;
  20640. #if defined(HAVE_CURVE25519)
  20641. curve25519_key private_key;
  20642. curve25519_key public_key;
  20643. WC_RNG rng;
  20644. byte out[CURVE25519_KEYSIZE];
  20645. word32 outLen = sizeof(out);
  20646. int endian = EC25519_BIG_ENDIAN;
  20647. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20648. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20649. ExpectIntEQ(wc_InitRng(&rng), 0);
  20650. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20651. 0);
  20652. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20653. 0);
  20654. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20655. &outLen, endian), 0);
  20656. /* test bad cases*/
  20657. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20658. BAD_FUNC_ARG);
  20659. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20660. endian), BAD_FUNC_ARG);
  20661. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20662. endian), BAD_FUNC_ARG);
  20663. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20664. &outLen, endian), BAD_FUNC_ARG);
  20665. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20666. NULL, endian), BAD_FUNC_ARG);
  20667. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20668. * increasing to 0x8f checks for error being returned*/
  20669. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20670. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20671. &outLen, endian), ECC_BAD_ARG_E);
  20672. outLen = outLen - 2;
  20673. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20674. &outLen, endian), BAD_FUNC_ARG);
  20675. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20676. wc_curve25519_free(&private_key);
  20677. wc_curve25519_free(&public_key);
  20678. #endif
  20679. return EXPECT_RESULT();
  20680. } /* END test_wc_curve25519_shared_secret_ex*/
  20681. /*
  20682. * Testing wc_curve25519_make_pub
  20683. */
  20684. static int test_wc_curve25519_make_pub(void)
  20685. {
  20686. EXPECT_DECLS;
  20687. #ifdef HAVE_CURVE25519
  20688. curve25519_key key;
  20689. WC_RNG rng;
  20690. byte out[CURVE25519_KEYSIZE];
  20691. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20692. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20693. ExpectIntEQ(wc_InitRng(&rng), 0);
  20694. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20695. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20696. (int)sizeof(key.k), key.k), 0);
  20697. /* test bad cases*/
  20698. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20699. (int)sizeof out, out), ECC_BAD_ARG_E);
  20700. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20701. NULL), ECC_BAD_ARG_E);
  20702. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20703. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20704. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20705. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20706. /* verify clamping test */
  20707. key.k[0] |= ~248;
  20708. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20709. key.k), ECC_BAD_ARG_E);
  20710. key.k[0] &= 248;
  20711. /* repeat the expected-to-succeed test. */
  20712. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20713. key.k), 0);
  20714. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20715. wc_curve25519_free(&key);
  20716. #endif
  20717. return EXPECT_RESULT();
  20718. } /* END test_wc_curve25519_make_pub */
  20719. /*
  20720. * Testing test_wc_curve25519_export_public_ex
  20721. */
  20722. static int test_wc_curve25519_export_public_ex(void)
  20723. {
  20724. EXPECT_DECLS;
  20725. #if defined(HAVE_CURVE25519)
  20726. curve25519_key key;
  20727. WC_RNG rng;
  20728. byte out[CURVE25519_KEYSIZE];
  20729. word32 outLen = sizeof(out);
  20730. int endian = EC25519_BIG_ENDIAN;
  20731. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20732. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20733. ExpectIntEQ(wc_InitRng(&rng), 0);
  20734. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20735. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20736. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20737. /* test bad cases*/
  20738. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20739. BAD_FUNC_ARG);
  20740. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20741. BAD_FUNC_ARG);
  20742. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20743. BAD_FUNC_ARG);
  20744. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20745. BAD_FUNC_ARG);
  20746. outLen = outLen - 2;
  20747. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20748. ECC_BAD_ARG_E);
  20749. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20750. wc_curve25519_free(&key);
  20751. #endif
  20752. return EXPECT_RESULT();
  20753. } /* END test_wc_curve25519_export_public_ex*/
  20754. /*
  20755. * Testing test_wc_curve25519_import_private_raw_ex
  20756. */
  20757. static int test_wc_curve25519_import_private_raw_ex(void)
  20758. {
  20759. EXPECT_DECLS;
  20760. #if defined(HAVE_CURVE25519)
  20761. curve25519_key key;
  20762. WC_RNG rng;
  20763. byte priv[CURVE25519_KEYSIZE];
  20764. byte pub[CURVE25519_KEYSIZE];
  20765. word32 privSz = sizeof(priv);
  20766. word32 pubSz = sizeof(pub);
  20767. int endian = EC25519_BIG_ENDIAN;
  20768. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20769. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20770. ExpectIntEQ(wc_InitRng(&rng), 0);
  20771. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20772. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20773. endian), 0);
  20774. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20775. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20776. &key, endian), 0);
  20777. /* test bad cases*/
  20778. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20779. endian), BAD_FUNC_ARG);
  20780. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20781. &key, endian), BAD_FUNC_ARG);
  20782. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20783. &key, endian), BAD_FUNC_ARG);
  20784. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20785. NULL, endian), BAD_FUNC_ARG);
  20786. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20787. &key, endian), ECC_BAD_ARG_E);
  20788. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20789. &key, endian), ECC_BAD_ARG_E);
  20790. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20791. &key, EC25519_LITTLE_ENDIAN), 0);
  20792. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20793. wc_curve25519_free(&key);
  20794. #endif
  20795. return EXPECT_RESULT();
  20796. } /* END test_wc_curve25519_import_private_raw_ex*/
  20797. /*
  20798. * Testing test_wc_curve25519_import_private
  20799. */
  20800. static int test_wc_curve25519_import_private(void)
  20801. {
  20802. EXPECT_DECLS;
  20803. #if defined(HAVE_CURVE25519)
  20804. curve25519_key key;
  20805. WC_RNG rng;
  20806. byte priv[CURVE25519_KEYSIZE];
  20807. word32 privSz = sizeof(priv);
  20808. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20809. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20810. ExpectIntEQ(wc_InitRng(&rng), 0);
  20811. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20812. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20813. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20814. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20815. wc_curve25519_free(&key);
  20816. #endif
  20817. return EXPECT_RESULT();
  20818. } /* END test_wc_curve25519_import*/
  20819. /*
  20820. * Testing test_wc_curve25519_export_private_raw_ex
  20821. */
  20822. static int test_wc_curve25519_export_private_raw_ex(void)
  20823. {
  20824. EXPECT_DECLS;
  20825. #if defined(HAVE_CURVE25519)
  20826. curve25519_key key;
  20827. byte out[CURVE25519_KEYSIZE];
  20828. word32 outLen = sizeof(out);
  20829. int endian = EC25519_BIG_ENDIAN;
  20830. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20831. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20832. 0);
  20833. /* test bad cases*/
  20834. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20835. BAD_FUNC_ARG);
  20836. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20837. BAD_FUNC_ARG);
  20838. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20839. endian), BAD_FUNC_ARG);
  20840. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20841. BAD_FUNC_ARG);
  20842. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20843. EC25519_LITTLE_ENDIAN), 0);
  20844. outLen = outLen - 2;
  20845. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20846. ECC_BAD_ARG_E);
  20847. wc_curve25519_free(&key);
  20848. #endif
  20849. return EXPECT_RESULT();
  20850. } /* END test_wc_curve25519_export_private_raw_ex*/
  20851. /*
  20852. * Testing wc_ed448_make_key().
  20853. */
  20854. static int test_wc_ed448_make_key(void)
  20855. {
  20856. EXPECT_DECLS;
  20857. #if defined(HAVE_ED448)
  20858. ed448_key key;
  20859. WC_RNG rng;
  20860. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20861. XMEMSET(&key, 0, sizeof(ed448_key));
  20862. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20863. ExpectIntEQ(wc_ed448_init(&key), 0);
  20864. ExpectIntEQ(wc_InitRng(&rng), 0);
  20865. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20866. ECC_PRIV_KEY_E);
  20867. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20868. /* Test bad args. */
  20869. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20870. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20871. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20872. BAD_FUNC_ARG);
  20873. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20874. BAD_FUNC_ARG);
  20875. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20876. wc_ed448_free(&key);
  20877. #endif
  20878. return EXPECT_RESULT();
  20879. } /* END test_wc_ed448_make_key */
  20880. /*
  20881. * Testing wc_ed448_init()
  20882. */
  20883. static int test_wc_ed448_init(void)
  20884. {
  20885. EXPECT_DECLS;
  20886. #if defined(HAVE_ED448)
  20887. ed448_key key;
  20888. XMEMSET(&key, 0, sizeof(ed448_key));
  20889. ExpectIntEQ(wc_ed448_init(&key), 0);
  20890. /* Test bad args. */
  20891. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20892. wc_ed448_free(&key);
  20893. #endif
  20894. return EXPECT_RESULT();
  20895. } /* END test_wc_ed448_init */
  20896. /*
  20897. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20898. */
  20899. static int test_wc_ed448_sign_msg(void)
  20900. {
  20901. EXPECT_DECLS;
  20902. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20903. ed448_key key;
  20904. WC_RNG rng;
  20905. byte msg[] = "Everybody gets Friday off.\n";
  20906. byte sig[ED448_SIG_SIZE];
  20907. word32 msglen = sizeof(msg);
  20908. word32 siglen = sizeof(sig);
  20909. word32 badSigLen = sizeof(sig) - 1;
  20910. #ifdef HAVE_ED448_VERIFY
  20911. int verify_ok = 0; /*1 = Verify success.*/
  20912. #endif
  20913. /* Initialize stack variables. */
  20914. XMEMSET(&key, 0, sizeof(ed448_key));
  20915. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20916. XMEMSET(sig, 0, siglen);
  20917. /* Initialize key. */
  20918. ExpectIntEQ(wc_ed448_init(&key), 0);
  20919. ExpectIntEQ(wc_InitRng(&rng), 0);
  20920. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20921. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20922. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20923. /* Test bad args. */
  20924. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20925. BAD_FUNC_ARG);
  20926. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20927. BAD_FUNC_ARG);
  20928. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20929. BAD_FUNC_ARG);
  20930. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20931. BAD_FUNC_ARG);
  20932. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20933. BUFFER_E);
  20934. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20935. badSigLen -= 1;
  20936. #ifdef HAVE_ED448_VERIFY
  20937. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20938. NULL, 0), 0);
  20939. ExpectIntEQ(verify_ok, 1);
  20940. /* Test bad args. */
  20941. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20942. &key, NULL, 0), BAD_FUNC_ARG);
  20943. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20944. &key, NULL, 0), BAD_FUNC_ARG);
  20945. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20946. &key, NULL, 0), BAD_FUNC_ARG);
  20947. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20948. &key, NULL, 0), BAD_FUNC_ARG);
  20949. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20950. &key, NULL, 0), BAD_FUNC_ARG);
  20951. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20952. NULL, NULL, 0), BAD_FUNC_ARG);
  20953. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20954. &key, NULL, 0), BAD_FUNC_ARG);
  20955. #endif /* Verify. */
  20956. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20957. wc_ed448_free(&key);
  20958. #endif
  20959. return EXPECT_RESULT();
  20960. } /* END test_wc_ed448_sign_msg */
  20961. /*
  20962. * Testing wc_ed448_import_public()
  20963. */
  20964. static int test_wc_ed448_import_public(void)
  20965. {
  20966. EXPECT_DECLS;
  20967. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20968. ed448_key pubKey;
  20969. WC_RNG rng;
  20970. const byte in[] =
  20971. "Ed448PublicKeyUnitTest.................................\n";
  20972. word32 inlen = sizeof(in);
  20973. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20974. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20975. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20976. ExpectIntEQ(wc_InitRng(&rng), 0);
  20977. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20978. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20979. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20980. /* Test bad args. */
  20981. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20982. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20983. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20984. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20985. wc_ed448_free(&pubKey);
  20986. #endif
  20987. return EXPECT_RESULT();
  20988. } /* END wc_ed448_import_public */
  20989. /*
  20990. * Testing wc_ed448_import_private_key()
  20991. */
  20992. static int test_wc_ed448_import_private_key(void)
  20993. {
  20994. EXPECT_DECLS;
  20995. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20996. ed448_key key;
  20997. WC_RNG rng;
  20998. const byte privKey[] =
  20999. "Ed448PrivateKeyUnitTest................................\n";
  21000. const byte pubKey[] =
  21001. "Ed448PublicKeyUnitTest.................................\n";
  21002. word32 privKeySz = sizeof(privKey);
  21003. word32 pubKeySz = sizeof(pubKey);
  21004. #ifdef HAVE_ED448_KEY_EXPORT
  21005. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  21006. word32 bothKeysSz = sizeof(bothKeys);
  21007. #endif
  21008. XMEMSET(&key, 0, sizeof(ed448_key));
  21009. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21010. ExpectIntEQ(wc_ed448_init(&key), 0);
  21011. ExpectIntEQ(wc_InitRng(&rng), 0);
  21012. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21013. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  21014. pubKeySz, &key, 1), 0);
  21015. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21016. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21017. #ifdef HAVE_ED448_KEY_EXPORT
  21018. PRIVATE_KEY_UNLOCK();
  21019. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  21020. PRIVATE_KEY_LOCK();
  21021. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  21022. &key, 1), 0);
  21023. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21024. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21025. #endif
  21026. /* Test bad args. */
  21027. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  21028. &key), BAD_FUNC_ARG);
  21029. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  21030. &key), BAD_FUNC_ARG);
  21031. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21032. pubKeySz, NULL), BAD_FUNC_ARG);
  21033. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  21034. pubKeySz, &key), BAD_FUNC_ARG);
  21035. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21036. pubKeySz - 1, &key), BAD_FUNC_ARG);
  21037. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  21038. BAD_FUNC_ARG);
  21039. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21040. wc_ed448_free(&key);
  21041. #endif
  21042. return EXPECT_RESULT();
  21043. } /* END test_wc_ed448_import_private_key */
  21044. /*
  21045. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  21046. */
  21047. static int test_wc_ed448_export(void)
  21048. {
  21049. EXPECT_DECLS;
  21050. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21051. ed448_key key;
  21052. WC_RNG rng;
  21053. byte priv[ED448_PRV_KEY_SIZE];
  21054. byte pub[ED448_PUB_KEY_SIZE];
  21055. word32 privSz = sizeof(priv);
  21056. word32 pubSz = sizeof(pub);
  21057. XMEMSET(&key, 0, sizeof(ed448_key));
  21058. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21059. ExpectIntEQ(wc_ed448_init(&key), 0);
  21060. ExpectIntEQ(wc_InitRng(&rng), 0);
  21061. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21062. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  21063. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  21064. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  21065. /* Test bad args. */
  21066. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  21067. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  21068. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  21069. PRIVATE_KEY_UNLOCK();
  21070. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  21071. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  21072. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  21073. /* Test bad args. */
  21074. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  21075. BAD_FUNC_ARG);
  21076. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  21077. BAD_FUNC_ARG);
  21078. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  21079. PRIVATE_KEY_LOCK();
  21080. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21081. wc_ed448_free(&key);
  21082. #endif
  21083. return EXPECT_RESULT();
  21084. } /* END test_wc_ed448_export */
  21085. /*
  21086. * Testing wc_ed448_size()
  21087. */
  21088. static int test_wc_ed448_size(void)
  21089. {
  21090. EXPECT_DECLS;
  21091. #if defined(HAVE_ED448)
  21092. ed448_key key;
  21093. WC_RNG rng;
  21094. XMEMSET(&key, 0, sizeof(ed448_key));
  21095. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21096. ExpectIntEQ(wc_ed448_init(&key), 0);
  21097. ExpectIntEQ(wc_InitRng(&rng), 0);
  21098. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21099. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  21100. /* Test bad args. */
  21101. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  21102. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  21103. /* Test bad args. */
  21104. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  21105. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  21106. /* Test bad args. */
  21107. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  21108. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  21109. /* Test bad args. */
  21110. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  21111. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21112. wc_ed448_free(&key);
  21113. #endif
  21114. return EXPECT_RESULT();
  21115. } /* END test_wc_ed448_size */
  21116. /*
  21117. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21118. */
  21119. static int test_wc_ed448_exportKey(void)
  21120. {
  21121. EXPECT_DECLS;
  21122. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21123. ed448_key key;
  21124. WC_RNG rng;
  21125. byte priv[ED448_PRV_KEY_SIZE];
  21126. byte pub[ED448_PUB_KEY_SIZE];
  21127. byte privOnly[ED448_PRV_KEY_SIZE];
  21128. word32 privSz = sizeof(priv);
  21129. word32 pubSz = sizeof(pub);
  21130. word32 privOnlySz = sizeof(privOnly);
  21131. XMEMSET(&key, 0, sizeof(ed448_key));
  21132. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21133. ExpectIntEQ(wc_ed448_init(&key), 0);
  21134. ExpectIntEQ(wc_InitRng(&rng), 0);
  21135. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21136. PRIVATE_KEY_UNLOCK();
  21137. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  21138. /* Test bad args. */
  21139. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  21140. BAD_FUNC_ARG);
  21141. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  21142. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  21143. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  21144. /* Test bad args. */
  21145. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  21146. BAD_FUNC_ARG);
  21147. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  21148. BAD_FUNC_ARG);
  21149. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  21150. BAD_FUNC_ARG);
  21151. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  21152. BAD_FUNC_ARG);
  21153. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  21154. BAD_FUNC_ARG);
  21155. PRIVATE_KEY_LOCK();
  21156. /* Cross check output. */
  21157. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  21158. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21159. wc_ed448_free(&key);
  21160. #endif
  21161. return EXPECT_RESULT();
  21162. } /* END test_wc_ed448_exportKey */
  21163. /*
  21164. * Testing wc_Ed448PublicKeyToDer
  21165. */
  21166. static int test_wc_Ed448PublicKeyToDer(void)
  21167. {
  21168. EXPECT_DECLS;
  21169. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21170. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21171. ed448_key key;
  21172. byte derBuf[1024];
  21173. XMEMSET(&key, 0, sizeof(ed448_key));
  21174. /* Test bad args */
  21175. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  21176. ExpectIntEQ(wc_ed448_init(&key), 0);
  21177. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  21178. wc_ed448_free(&key);
  21179. /* Test good args */
  21180. if (EXPECT_SUCCESS()) {
  21181. WC_RNG rng;
  21182. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21183. ExpectIntEQ(wc_ed448_init(&key), 0);
  21184. ExpectIntEQ(wc_InitRng(&rng), 0);
  21185. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21186. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  21187. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21188. wc_ed448_free(&key);
  21189. }
  21190. #endif
  21191. return EXPECT_RESULT();
  21192. } /* END testing wc_Ed448PublicKeyToDer */
  21193. /*
  21194. * Testing wc_curve448_init and wc_curve448_free.
  21195. */
  21196. static int test_wc_curve448_init(void)
  21197. {
  21198. EXPECT_DECLS;
  21199. #if defined(HAVE_CURVE448)
  21200. curve448_key key;
  21201. /* Test bad args for wc_curve448_init */
  21202. ExpectIntEQ(wc_curve448_init(&key), 0);
  21203. /* Test bad args for wc_curve448_init */
  21204. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21205. /* Test good args for wc_curve_448_free */
  21206. wc_curve448_free(&key);
  21207. /* Test bad args for wc_curve448_free */
  21208. wc_curve448_free(NULL);
  21209. #endif
  21210. return EXPECT_RESULT();
  21211. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21212. /*
  21213. * Testing wc_curve448_make_key
  21214. */
  21215. static int test_wc_curve448_make_key(void)
  21216. {
  21217. EXPECT_DECLS;
  21218. #if defined(HAVE_CURVE448)
  21219. curve448_key key;
  21220. WC_RNG rng;
  21221. int keysize;
  21222. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21223. ExpectIntEQ(wc_curve448_init(&key), 0);
  21224. ExpectIntEQ(wc_InitRng(&rng), 0);
  21225. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21226. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21227. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21228. /* test bad cases */
  21229. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21230. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21231. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21232. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21233. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21234. wc_curve448_free(&key);
  21235. #endif
  21236. return EXPECT_RESULT();
  21237. } /* END test_wc_curve448_make_key*/
  21238. /*
  21239. * Testing test_wc_curve448_shared_secret_ex
  21240. */
  21241. static int test_wc_curve448_shared_secret_ex(void)
  21242. {
  21243. EXPECT_DECLS;
  21244. #if defined(HAVE_CURVE448)
  21245. curve448_key private_key;
  21246. curve448_key public_key;
  21247. WC_RNG rng;
  21248. byte out[CURVE448_KEY_SIZE];
  21249. word32 outLen = sizeof(out);
  21250. int endian = EC448_BIG_ENDIAN;
  21251. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21252. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21253. ExpectIntEQ(wc_InitRng(&rng), 0);
  21254. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21255. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21256. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21257. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21258. &outLen, endian), 0);
  21259. /* test bad cases */
  21260. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21261. BAD_FUNC_ARG);
  21262. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21263. endian), BAD_FUNC_ARG);
  21264. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21265. endian), BAD_FUNC_ARG);
  21266. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21267. &outLen, endian), BAD_FUNC_ARG);
  21268. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21269. NULL, endian), BAD_FUNC_ARG);
  21270. outLen = outLen - 2;
  21271. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21272. &outLen, endian), BAD_FUNC_ARG);
  21273. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21274. wc_curve448_free(&private_key);
  21275. wc_curve448_free(&public_key);
  21276. #endif
  21277. return EXPECT_RESULT();
  21278. } /* END test_wc_curve448_shared_secret_ex*/
  21279. /*
  21280. * Testing test_wc_curve448_export_public_ex
  21281. */
  21282. static int test_wc_curve448_export_public_ex(void)
  21283. {
  21284. EXPECT_DECLS;
  21285. #if defined(HAVE_CURVE448)
  21286. WC_RNG rng;
  21287. curve448_key key;
  21288. byte out[CURVE448_KEY_SIZE];
  21289. word32 outLen = sizeof(out);
  21290. int endian = EC448_BIG_ENDIAN;
  21291. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21292. ExpectIntEQ(wc_curve448_init(&key), 0);
  21293. ExpectIntEQ(wc_InitRng(&rng), 0);
  21294. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21295. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21296. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21297. /* test bad cases*/
  21298. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21299. BAD_FUNC_ARG);
  21300. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21301. BAD_FUNC_ARG);
  21302. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21303. BAD_FUNC_ARG);
  21304. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21305. BAD_FUNC_ARG);
  21306. outLen = outLen - 2;
  21307. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21308. ECC_BAD_ARG_E);
  21309. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21310. wc_curve448_free(&key);
  21311. #endif
  21312. return EXPECT_RESULT();
  21313. } /* END test_wc_curve448_export_public_ex*/
  21314. /*
  21315. * Testing test_wc_curve448_export_private_raw_ex
  21316. */
  21317. static int test_wc_curve448_export_private_raw_ex(void)
  21318. {
  21319. EXPECT_DECLS;
  21320. #if defined(HAVE_CURVE448)
  21321. curve448_key key;
  21322. byte out[CURVE448_KEY_SIZE];
  21323. word32 outLen = sizeof(out);
  21324. int endian = EC448_BIG_ENDIAN;
  21325. ExpectIntEQ(wc_curve448_init(&key), 0);
  21326. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21327. 0);
  21328. /* test bad cases*/
  21329. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21330. BAD_FUNC_ARG);
  21331. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21332. BAD_FUNC_ARG);
  21333. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21334. BAD_FUNC_ARG);
  21335. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21336. BAD_FUNC_ARG);
  21337. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21338. EC448_LITTLE_ENDIAN), 0);
  21339. outLen = outLen - 2;
  21340. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21341. ECC_BAD_ARG_E);
  21342. wc_curve448_free(&key);
  21343. #endif
  21344. return EXPECT_RESULT();
  21345. } /* END test_wc_curve448_export_private_raw_ex*/
  21346. /*
  21347. * Testing test_wc_curve448_import_private_raw_ex
  21348. */
  21349. static int test_wc_curve448_import_private_raw_ex(void)
  21350. {
  21351. EXPECT_DECLS;
  21352. #if defined(HAVE_CURVE448)
  21353. curve448_key key;
  21354. WC_RNG rng;
  21355. byte priv[CURVE448_KEY_SIZE];
  21356. byte pub[CURVE448_KEY_SIZE];
  21357. word32 privSz = sizeof(priv);
  21358. word32 pubSz = sizeof(pub);
  21359. int endian = EC448_BIG_ENDIAN;
  21360. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21361. ExpectIntEQ(wc_curve448_init(&key), 0);
  21362. ExpectIntEQ(wc_InitRng(&rng), 0);
  21363. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21364. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21365. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21366. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21367. &key, endian), 0);
  21368. /* test bad cases */
  21369. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21370. BAD_FUNC_ARG);
  21371. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21372. &key, endian), BAD_FUNC_ARG);
  21373. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21374. &key, endian), BAD_FUNC_ARG);
  21375. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21376. NULL, endian), BAD_FUNC_ARG);
  21377. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21378. &key, endian), ECC_BAD_ARG_E);
  21379. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21380. &key, endian), ECC_BAD_ARG_E);
  21381. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21382. &key, EC448_LITTLE_ENDIAN), 0);
  21383. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21384. wc_curve448_free(&key);
  21385. #endif
  21386. return EXPECT_RESULT();
  21387. } /* END test_wc_curve448_import_private_raw_ex*/
  21388. /*
  21389. * Testing test_curve448_export_key_raw
  21390. */
  21391. static int test_wc_curve448_export_key_raw(void)
  21392. {
  21393. EXPECT_DECLS;
  21394. #if defined(HAVE_CURVE448)
  21395. curve448_key key;
  21396. WC_RNG rng;
  21397. byte priv[CURVE448_KEY_SIZE];
  21398. byte pub[CURVE448_KEY_SIZE];
  21399. word32 privSz = sizeof(priv);
  21400. word32 pubSz = sizeof(pub);
  21401. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21402. ExpectIntEQ(wc_curve448_init(&key), 0);
  21403. ExpectIntEQ(wc_InitRng(&rng), 0);
  21404. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21405. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21406. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21407. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21408. 0);
  21409. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21410. wc_curve448_free(&key);
  21411. #endif
  21412. return EXPECT_RESULT();
  21413. } /* END test_wc_curve448_import_private_raw_ex*/
  21414. /*
  21415. * Testing test_wc_curve448_import_private
  21416. */
  21417. static int test_wc_curve448_import_private(void)
  21418. {
  21419. EXPECT_DECLS;
  21420. #if defined(HAVE_CURVE448)
  21421. curve448_key key;
  21422. WC_RNG rng;
  21423. byte priv[CURVE448_KEY_SIZE];
  21424. word32 privSz = sizeof(priv);
  21425. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21426. ExpectIntEQ(wc_curve448_init(&key), 0);
  21427. ExpectIntEQ(wc_InitRng(&rng), 0);
  21428. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21429. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21430. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21431. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21432. wc_curve448_free(&key);
  21433. #endif
  21434. return EXPECT_RESULT();
  21435. } /* END test_wc_curve448_import*/
  21436. /*
  21437. * Testing test_wc_curve448_size.
  21438. */
  21439. static int test_wc_curve448_size(void)
  21440. {
  21441. EXPECT_DECLS;
  21442. #if defined(HAVE_CURVE448)
  21443. curve448_key key;
  21444. ExpectIntEQ(wc_curve448_init(&key), 0);
  21445. /* Test good args for wc_curve448_size */
  21446. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21447. /* Test bad args for wc_curve448_size */
  21448. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21449. wc_curve448_free(&key);
  21450. #endif
  21451. return EXPECT_RESULT();
  21452. } /* END test_wc_curve448_size*/
  21453. /*
  21454. * Testing wc_ecc_make_key.
  21455. */
  21456. static int test_wc_ecc_make_key(void)
  21457. {
  21458. EXPECT_DECLS;
  21459. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21460. ecc_key key;
  21461. WC_RNG rng;
  21462. int ret;
  21463. XMEMSET(&key, 0, sizeof(ecc_key));
  21464. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21465. ExpectIntEQ(wc_ecc_init(&key), 0);
  21466. ExpectIntEQ(wc_InitRng(&rng), 0);
  21467. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21468. #if defined(WOLFSSL_ASYNC_CRYPT)
  21469. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21470. #endif
  21471. ExpectIntEQ(ret, 0);
  21472. /* Pass in bad args. */
  21473. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21474. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21475. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21476. wc_ecc_free(&key);
  21477. #ifdef FP_ECC
  21478. wc_ecc_fp_free();
  21479. #endif
  21480. #endif
  21481. return EXPECT_RESULT();
  21482. } /* END test_wc_ecc_make_key */
  21483. /*
  21484. * Testing wc_ecc_init()
  21485. */
  21486. static int test_wc_ecc_init(void)
  21487. {
  21488. EXPECT_DECLS;
  21489. #ifdef HAVE_ECC
  21490. ecc_key key;
  21491. XMEMSET(&key, 0, sizeof(ecc_key));
  21492. ExpectIntEQ(wc_ecc_init(&key), 0);
  21493. /* Pass in bad args. */
  21494. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21495. wc_ecc_free(&key);
  21496. #endif
  21497. return EXPECT_RESULT();
  21498. } /* END test_wc_ecc_init */
  21499. /*
  21500. * Testing wc_ecc_check_key()
  21501. */
  21502. static int test_wc_ecc_check_key(void)
  21503. {
  21504. EXPECT_DECLS;
  21505. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21506. ecc_key key;
  21507. WC_RNG rng;
  21508. int ret;
  21509. XMEMSET(&rng, 0, sizeof(rng));
  21510. XMEMSET(&key, 0, sizeof(key));
  21511. ExpectIntEQ(wc_ecc_init(&key), 0);
  21512. ExpectIntEQ(wc_InitRng(&rng), 0);
  21513. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21514. #if defined(WOLFSSL_ASYNC_CRYPT)
  21515. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21516. #endif
  21517. ExpectIntEQ(ret, 0);
  21518. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21519. /* Pass in bad args. */
  21520. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21521. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21522. wc_ecc_free(&key);
  21523. #ifdef FP_ECC
  21524. wc_ecc_fp_free();
  21525. #endif
  21526. #endif
  21527. return EXPECT_RESULT();
  21528. } /* END test_wc_ecc_check_key */
  21529. /*
  21530. * Testing wc_ecc_get_generator()
  21531. */
  21532. static int test_wc_ecc_get_generator(void)
  21533. {
  21534. EXPECT_DECLS;
  21535. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21536. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21537. ecc_point* pt = NULL;
  21538. ExpectNotNull(pt = wc_ecc_new_point());
  21539. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21540. MP_OKAY);
  21541. /* Test bad args. */
  21542. /* Returns Zero for bad arg. */
  21543. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21544. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21545. MP_OKAY);
  21546. /* If we ever get to 1000 curves increase this number */
  21547. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21548. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21549. wc_ecc_del_point(pt);
  21550. #endif
  21551. return EXPECT_RESULT();
  21552. } /* END test_wc_ecc_get_generator */
  21553. /*
  21554. * Testing wc_ecc_size()
  21555. */
  21556. static int test_wc_ecc_size(void)
  21557. {
  21558. EXPECT_DECLS;
  21559. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21560. WC_RNG rng;
  21561. ecc_key key;
  21562. int ret;
  21563. XMEMSET(&key, 0, sizeof(ecc_key));
  21564. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21565. ExpectIntEQ(wc_ecc_init(&key), 0);
  21566. ExpectIntEQ(wc_InitRng(&rng), 0);
  21567. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21568. #if defined(WOLFSSL_ASYNC_CRYPT)
  21569. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21570. #endif
  21571. ExpectIntEQ(ret, 0);
  21572. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21573. /* Test bad args. */
  21574. /* Returns Zero for bad arg. */
  21575. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21576. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21577. wc_ecc_free(&key);
  21578. #endif
  21579. return EXPECT_RESULT();
  21580. } /* END test_wc_ecc_size */
  21581. static int test_wc_ecc_params(void)
  21582. {
  21583. EXPECT_DECLS;
  21584. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21585. It was added after certifications */
  21586. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21587. const ecc_set_type* ecc_set;
  21588. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21589. /* Test for SECP256R1 curve */
  21590. int curve_id = ECC_SECP256R1;
  21591. int curve_idx;
  21592. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21593. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21594. ExpectIntEQ(ecc_set->id, curve_id);
  21595. #endif
  21596. /* Test case when SECP256R1 is not enabled */
  21597. /* Test that we get curve params for index 0 */
  21598. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21599. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21600. return EXPECT_RESULT();
  21601. }
  21602. /*
  21603. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21604. */
  21605. static int test_wc_ecc_signVerify_hash(void)
  21606. {
  21607. EXPECT_DECLS;
  21608. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21609. ecc_key key;
  21610. WC_RNG rng;
  21611. int ret;
  21612. #ifdef HAVE_ECC_VERIFY
  21613. int verify = 0;
  21614. #endif
  21615. word32 siglen = ECC_BUFSIZE;
  21616. byte sig[ECC_BUFSIZE];
  21617. byte adjustedSig[ECC_BUFSIZE+1];
  21618. byte digest[] = TEST_STRING;
  21619. word32 digestlen = (word32)TEST_STRING_SZ;
  21620. /* Init stack var */
  21621. XMEMSET(&key, 0, sizeof(ecc_key));
  21622. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21623. XMEMSET(sig, 0, siglen);
  21624. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21625. /* Init structs. */
  21626. ExpectIntEQ(wc_ecc_init(&key), 0);
  21627. ExpectIntEQ(wc_InitRng(&rng), 0);
  21628. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21629. #if defined(WOLFSSL_ASYNC_CRYPT)
  21630. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21631. #endif
  21632. ExpectIntEQ(ret, 0);
  21633. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21634. 0);
  21635. /* Check bad args. */
  21636. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21637. ECC_BAD_ARG_E);
  21638. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21639. ECC_BAD_ARG_E);
  21640. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21641. ECC_BAD_ARG_E);
  21642. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21643. ECC_BAD_ARG_E);
  21644. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21645. ECC_BAD_ARG_E);
  21646. #ifdef HAVE_ECC_VERIFY
  21647. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21648. &key), 0);
  21649. ExpectIntEQ(verify, 1);
  21650. /* test check on length of signature passed in */
  21651. XMEMCPY(adjustedSig, sig, siglen);
  21652. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21653. #ifndef NO_STRICT_ECDSA_LEN
  21654. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21655. &verify, &key), 0);
  21656. #else
  21657. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21658. * is allowed */
  21659. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21660. &verify, &key), 0);
  21661. #endif
  21662. /* Test bad args. */
  21663. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21664. &key), ECC_BAD_ARG_E);
  21665. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21666. ECC_BAD_ARG_E);
  21667. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21668. ECC_BAD_ARG_E);
  21669. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21670. NULL), ECC_BAD_ARG_E);
  21671. #endif /* HAVE_ECC_VERIFY */
  21672. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21673. wc_ecc_free(&key);
  21674. #ifdef FP_ECC
  21675. wc_ecc_fp_free();
  21676. #endif
  21677. #endif
  21678. return EXPECT_RESULT();
  21679. } /* END test_wc_ecc_sign_hash */
  21680. /*
  21681. * Testing wc_ecc_shared_secret()
  21682. */
  21683. static int test_wc_ecc_shared_secret(void)
  21684. {
  21685. EXPECT_DECLS;
  21686. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21687. ecc_key key;
  21688. ecc_key pubKey;
  21689. WC_RNG rng;
  21690. #if defined(NO_ECC256)
  21691. int ret;
  21692. #endif
  21693. byte out[KEY32];
  21694. int keySz = sizeof(out);
  21695. word32 outlen = (word32)sizeof(out);
  21696. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21697. const char* qx =
  21698. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21699. const char* qy =
  21700. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21701. const char* d =
  21702. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21703. const char* curveName = "SECP256R1";
  21704. const byte expected_shared_secret[] =
  21705. {
  21706. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21707. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21708. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21709. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21710. };
  21711. #endif
  21712. PRIVATE_KEY_UNLOCK();
  21713. /* Initialize variables. */
  21714. XMEMSET(&key, 0, sizeof(ecc_key));
  21715. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21716. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21717. XMEMSET(out, 0, keySz);
  21718. ExpectIntEQ(wc_ecc_init(&key), 0);
  21719. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21720. ExpectIntEQ(wc_InitRng(&rng), 0);
  21721. #if !defined(NO_ECC256)
  21722. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21723. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21724. #else
  21725. ret = wc_ecc_make_key(&rng, keySz, &key);
  21726. #if defined(WOLFSSL_ASYNC_CRYPT)
  21727. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21728. #endif
  21729. ExpectIntEQ(ret, 0);
  21730. ret = wc_ecc_make_key(&rng, keySz, &key);
  21731. #if defined(WOLFSSL_ASYNC_CRYPT)
  21732. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21733. #endif
  21734. ExpectIntEQ(ret, 0);
  21735. #endif
  21736. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21737. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21738. !defined(HAVE_SELFTEST)
  21739. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21740. #endif
  21741. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21742. #if !defined(NO_ECC256)
  21743. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21744. #endif
  21745. /* Test bad args. */
  21746. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21747. BAD_FUNC_ARG);
  21748. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21749. BAD_FUNC_ARG);
  21750. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21751. BAD_FUNC_ARG);
  21752. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21753. BAD_FUNC_ARG);
  21754. /* Invalid length */
  21755. outlen = 1;
  21756. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21757. BUFFER_E);
  21758. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21759. wc_ecc_free(&pubKey);
  21760. wc_ecc_free(&key);
  21761. #ifdef FP_ECC
  21762. wc_ecc_fp_free();
  21763. #endif
  21764. PRIVATE_KEY_LOCK();
  21765. #endif
  21766. return EXPECT_RESULT();
  21767. } /* END tests_wc_ecc_shared_secret */
  21768. /*
  21769. * testint wc_ecc_export_x963()
  21770. */
  21771. static int test_wc_ecc_export_x963(void)
  21772. {
  21773. EXPECT_DECLS;
  21774. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21775. ecc_key key;
  21776. WC_RNG rng;
  21777. byte out[ECC_ASN963_MAX_BUF_SZ];
  21778. word32 outlen = sizeof(out);
  21779. int ret;
  21780. PRIVATE_KEY_UNLOCK();
  21781. /* Initialize variables. */
  21782. XMEMSET(&key, 0, sizeof(ecc_key));
  21783. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21784. XMEMSET(out, 0, outlen);
  21785. ExpectIntEQ(wc_ecc_init(&key), 0);
  21786. ExpectIntEQ(wc_InitRng(&rng), 0);
  21787. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21788. #if defined(WOLFSSL_ASYNC_CRYPT)
  21789. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21790. #endif
  21791. ExpectIntEQ(ret, 0);
  21792. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21793. /* Test bad args. */
  21794. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21795. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21796. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21797. key.idx = -4;
  21798. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21799. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21800. wc_ecc_free(&key);
  21801. #ifdef FP_ECC
  21802. wc_ecc_fp_free();
  21803. #endif
  21804. PRIVATE_KEY_LOCK();
  21805. #endif
  21806. return EXPECT_RESULT();
  21807. } /* END test_wc_ecc_export_x963 */
  21808. /*
  21809. * Testing wc_ecc_export_x963_ex()
  21810. * compile with --enable-compkey will use compression.
  21811. */
  21812. static int test_wc_ecc_export_x963_ex(void)
  21813. {
  21814. EXPECT_DECLS;
  21815. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21816. ecc_key key;
  21817. WC_RNG rng;
  21818. int ret;
  21819. byte out[ECC_ASN963_MAX_BUF_SZ];
  21820. word32 outlen = sizeof(out);
  21821. #ifdef HAVE_COMP_KEY
  21822. word32 badOutLen = 5;
  21823. #endif
  21824. /* Init stack variables. */
  21825. XMEMSET(&key, 0, sizeof(ecc_key));
  21826. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21827. XMEMSET(out, 0, outlen);
  21828. PRIVATE_KEY_UNLOCK();
  21829. ExpectIntEQ(wc_ecc_init(&key), 0);
  21830. ExpectIntEQ(wc_InitRng(&rng), 0);
  21831. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21832. #if defined(WOLFSSL_ASYNC_CRYPT)
  21833. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21834. #endif
  21835. ExpectIntEQ(ret, 0);
  21836. #ifdef HAVE_COMP_KEY
  21837. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21838. #else
  21839. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21840. #endif
  21841. /* Test bad args. */
  21842. #ifdef HAVE_COMP_KEY
  21843. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21844. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21845. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21846. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21847. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21848. #else
  21849. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21850. LENGTH_ONLY_E);
  21851. #endif
  21852. key.idx = -4;
  21853. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21854. #else
  21855. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21856. ECC_BAD_ARG_E);
  21857. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21858. LENGTH_ONLY_E);
  21859. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21860. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21861. ECC_BAD_ARG_E);
  21862. key.idx = -4;
  21863. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21864. ECC_BAD_ARG_E);
  21865. #endif
  21866. PRIVATE_KEY_LOCK();
  21867. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21868. wc_ecc_free(&key);
  21869. #ifdef FP_ECC
  21870. wc_ecc_fp_free();
  21871. #endif
  21872. #endif
  21873. return EXPECT_RESULT();
  21874. } /* END test_wc_ecc_export_x963_ex */
  21875. /*
  21876. * testing wc_ecc_import_x963()
  21877. */
  21878. static int test_wc_ecc_import_x963(void)
  21879. {
  21880. EXPECT_DECLS;
  21881. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21882. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21883. ecc_key pubKey;
  21884. ecc_key key;
  21885. WC_RNG rng;
  21886. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21887. word32 x963Len = (word32)sizeof(x963);
  21888. int ret;
  21889. /* Init stack variables. */
  21890. XMEMSET(&key, 0, sizeof(ecc_key));
  21891. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21892. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21893. XMEMSET(x963, 0, x963Len);
  21894. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21895. ExpectIntEQ(wc_ecc_init(&key), 0);
  21896. ExpectIntEQ(wc_InitRng(&rng), 0);
  21897. #if FIPS_VERSION3_GE(6,0,0)
  21898. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21899. #else
  21900. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21901. #endif
  21902. #if defined(WOLFSSL_ASYNC_CRYPT)
  21903. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21904. #endif
  21905. ExpectIntEQ(ret, 0);
  21906. PRIVATE_KEY_UNLOCK();
  21907. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21908. PRIVATE_KEY_LOCK();
  21909. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21910. /* Test bad args. */
  21911. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21912. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21913. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21914. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21915. wc_ecc_free(&key);
  21916. wc_ecc_free(&pubKey);
  21917. #ifdef FP_ECC
  21918. wc_ecc_fp_free();
  21919. #endif
  21920. #endif
  21921. return EXPECT_RESULT();
  21922. } /* END wc_ecc_import_x963 */
  21923. /*
  21924. * testing wc_ecc_import_private_key()
  21925. */
  21926. static int test_wc_ecc_import_private_key(void)
  21927. {
  21928. EXPECT_DECLS;
  21929. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21930. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21931. ecc_key key;
  21932. ecc_key keyImp;
  21933. WC_RNG rng;
  21934. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21935. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21936. word32 privKeySz = (word32)sizeof(privKey);
  21937. word32 x963KeySz = (word32)sizeof(x963Key);
  21938. int ret;
  21939. /* Init stack variables. */
  21940. XMEMSET(&key, 0, sizeof(ecc_key));
  21941. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21942. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21943. XMEMSET(privKey, 0, privKeySz);
  21944. XMEMSET(x963Key, 0, x963KeySz);
  21945. PRIVATE_KEY_UNLOCK();
  21946. ExpectIntEQ(wc_ecc_init(&key), 0);
  21947. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21948. ExpectIntEQ(wc_InitRng(&rng), 0);
  21949. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21950. #if defined(WOLFSSL_ASYNC_CRYPT)
  21951. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21952. #endif
  21953. ExpectIntEQ(ret, 0);
  21954. PRIVATE_KEY_UNLOCK();
  21955. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21956. PRIVATE_KEY_LOCK();
  21957. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21958. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21959. x963KeySz, &keyImp), 0);
  21960. /* Pass in bad args. */
  21961. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21962. x963KeySz, NULL), BAD_FUNC_ARG);
  21963. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21964. &keyImp), BAD_FUNC_ARG);
  21965. PRIVATE_KEY_LOCK();
  21966. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21967. wc_ecc_free(&keyImp);
  21968. wc_ecc_free(&key);
  21969. #ifdef FP_ECC
  21970. wc_ecc_fp_free();
  21971. #endif
  21972. #endif
  21973. return EXPECT_RESULT();
  21974. } /* END test_wc_ecc_import_private_key */
  21975. /*
  21976. * Testing wc_ecc_export_private_only()
  21977. */
  21978. static int test_wc_ecc_export_private_only(void)
  21979. {
  21980. EXPECT_DECLS;
  21981. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21982. ecc_key key;
  21983. WC_RNG rng;
  21984. byte out[ECC_PRIV_KEY_BUF];
  21985. word32 outlen = sizeof(out);
  21986. int ret;
  21987. /* Init stack variables. */
  21988. XMEMSET(&key, 0, sizeof(ecc_key));
  21989. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21990. XMEMSET(out, 0, outlen);
  21991. PRIVATE_KEY_UNLOCK();
  21992. ExpectIntEQ(wc_ecc_init(&key), 0);
  21993. ExpectIntEQ(wc_InitRng(&rng), 0);
  21994. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21995. #if defined(WOLFSSL_ASYNC_CRYPT)
  21996. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21997. #endif
  21998. ExpectIntEQ(ret, 0);
  21999. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  22000. /* Pass in bad args. */
  22001. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  22002. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  22003. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  22004. PRIVATE_KEY_LOCK();
  22005. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22006. wc_ecc_free(&key);
  22007. #ifdef FP_ECC
  22008. wc_ecc_fp_free();
  22009. #endif
  22010. #endif
  22011. return EXPECT_RESULT();
  22012. } /* END test_wc_ecc_export_private_only */
  22013. /*
  22014. * Testing wc_ecc_rs_to_sig()
  22015. */
  22016. static int test_wc_ecc_rs_to_sig(void)
  22017. {
  22018. EXPECT_DECLS;
  22019. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22020. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22021. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22022. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22023. const char* zeroStr = "0";
  22024. byte sig[ECC_MAX_SIG_SIZE];
  22025. word32 siglen = (word32)sizeof(sig);
  22026. /* R and S max size is the order of curve. 2^192.*/
  22027. int keySz = KEY24;
  22028. byte r[KEY24];
  22029. byte s[KEY24];
  22030. word32 rlen = (word32)sizeof(r);
  22031. word32 slen = (word32)sizeof(s);
  22032. /* Init stack variables. */
  22033. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22034. XMEMSET(r, 0, keySz);
  22035. XMEMSET(s, 0, keySz);
  22036. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  22037. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  22038. /* Test bad args. */
  22039. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  22040. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  22041. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  22042. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  22043. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  22044. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  22045. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  22046. ECC_BAD_ARG_E);
  22047. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  22048. ECC_BAD_ARG_E);
  22049. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  22050. ECC_BAD_ARG_E);
  22051. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  22052. ECC_BAD_ARG_E);
  22053. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  22054. ECC_BAD_ARG_E);
  22055. #endif
  22056. return EXPECT_RESULT();
  22057. } /* END test_wc_ecc_rs_to_sig */
  22058. static int test_wc_ecc_import_raw(void)
  22059. {
  22060. EXPECT_DECLS;
  22061. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22062. ecc_key key;
  22063. const char* qx =
  22064. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22065. const char* qy =
  22066. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22067. const char* d =
  22068. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22069. const char* curveName = "SECP256R1";
  22070. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22071. const char* kNullStr = "";
  22072. int ret;
  22073. #endif
  22074. XMEMSET(&key, 0, sizeof(ecc_key));
  22075. ExpectIntEQ(wc_ecc_init(&key), 0);
  22076. /* Test good import */
  22077. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  22078. /* Test bad args. */
  22079. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  22080. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  22081. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  22082. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  22083. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22084. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22085. wc_ecc_free(&key);
  22086. #endif
  22087. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  22088. curveName), 0);
  22089. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22090. #endif
  22091. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22092. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22093. wc_ecc_free(&key);
  22094. #endif
  22095. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22096. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22097. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22098. #else
  22099. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22100. #endif
  22101. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22102. wc_ecc_free(&key);
  22103. #endif
  22104. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22105. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22106. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22107. #else
  22108. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22109. #endif
  22110. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22111. wc_ecc_free(&key);
  22112. #endif
  22113. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  22114. #endif
  22115. wc_ecc_free(&key);
  22116. #endif
  22117. return EXPECT_RESULT();
  22118. } /* END test_wc_ecc_import_raw */
  22119. static int test_wc_ecc_import_unsigned(void)
  22120. {
  22121. EXPECT_DECLS;
  22122. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22123. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  22124. HAVE_FIPS_VERSION >= 2))
  22125. ecc_key key;
  22126. const byte qx[] = {
  22127. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22128. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22129. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22130. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22131. };
  22132. const byte qy[] = {
  22133. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22134. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22135. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22136. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22137. };
  22138. const byte d[] = {
  22139. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22140. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22141. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22142. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22143. };
  22144. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22145. const byte nullBytes[32] = {0};
  22146. int ret;
  22147. #endif
  22148. int curveId = ECC_SECP256R1;
  22149. XMEMSET(&key, 0, sizeof(ecc_key));
  22150. ExpectIntEQ(wc_ecc_init(&key), 0);
  22151. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22152. curveId), 0);
  22153. /* Test bad args. */
  22154. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22155. curveId), BAD_FUNC_ARG);
  22156. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22157. curveId), BAD_FUNC_ARG);
  22158. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22159. curveId), BAD_FUNC_ARG);
  22160. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22161. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  22162. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22163. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22164. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  22165. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22166. #endif
  22167. wc_ecc_free(&key);
  22168. #endif
  22169. return EXPECT_RESULT();
  22170. } /* END test_wc_ecc_import_unsigned */
  22171. /*
  22172. * Testing wc_ecc_sig_size()
  22173. */
  22174. static int test_wc_ecc_sig_size(void)
  22175. {
  22176. EXPECT_DECLS;
  22177. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22178. ecc_key key;
  22179. WC_RNG rng;
  22180. int keySz = KEY16;
  22181. int ret;
  22182. XMEMSET(&rng, 0, sizeof(rng));
  22183. XMEMSET(&key, 0, sizeof(key));
  22184. ExpectIntEQ(wc_ecc_init(&key), 0);
  22185. ExpectIntEQ(wc_InitRng(&rng), 0);
  22186. ret = wc_ecc_make_key(&rng, keySz, &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. ExpectIntLE(wc_ecc_sig_size(&key),
  22192. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  22193. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22194. wc_ecc_free(&key);
  22195. #endif
  22196. return EXPECT_RESULT();
  22197. } /* END test_wc_ecc_sig_size */
  22198. /*
  22199. * Testing wc_ecc_ctx_new()
  22200. */
  22201. static int test_wc_ecc_ctx_new(void)
  22202. {
  22203. EXPECT_DECLS;
  22204. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22205. WC_RNG rng;
  22206. ecEncCtx* cli = NULL;
  22207. ecEncCtx* srv = NULL;
  22208. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22209. ExpectIntEQ(wc_InitRng(&rng), 0);
  22210. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22211. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22212. wc_ecc_ctx_free(cli);
  22213. cli = NULL;
  22214. wc_ecc_ctx_free(srv);
  22215. /* Test bad args. */
  22216. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22217. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22218. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22219. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22220. wc_ecc_ctx_free(cli);
  22221. #endif
  22222. return EXPECT_RESULT();
  22223. } /* END test_wc_ecc_ctx_new */
  22224. /*
  22225. * Tesing wc_ecc_reset()
  22226. */
  22227. static int test_wc_ecc_ctx_reset(void)
  22228. {
  22229. EXPECT_DECLS;
  22230. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22231. ecEncCtx* ctx = NULL;
  22232. WC_RNG rng;
  22233. XMEMSET(&rng, 0, sizeof(rng));
  22234. ExpectIntEQ(wc_InitRng(&rng), 0);
  22235. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22236. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22237. /* Pass in bad args. */
  22238. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22239. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22240. wc_ecc_ctx_free(ctx);
  22241. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22242. #endif
  22243. return EXPECT_RESULT();
  22244. } /* END test_wc_ecc_ctx_reset */
  22245. /*
  22246. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22247. */
  22248. static int test_wc_ecc_ctx_set_peer_salt(void)
  22249. {
  22250. EXPECT_DECLS;
  22251. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22252. WC_RNG rng;
  22253. ecEncCtx* cliCtx = NULL;
  22254. ecEncCtx* servCtx = NULL;
  22255. const byte* cliSalt = NULL;
  22256. const byte* servSalt = NULL;
  22257. XMEMSET(&rng, 0, sizeof(rng));
  22258. ExpectIntEQ(wc_InitRng(&rng), 0);
  22259. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22260. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22261. /* Test bad args. */
  22262. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22263. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22264. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22265. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22266. /* Test bad args. */
  22267. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22268. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22269. wc_ecc_ctx_free(cliCtx);
  22270. wc_ecc_ctx_free(servCtx);
  22271. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22272. #endif
  22273. return EXPECT_RESULT();
  22274. } /* END test_wc_ecc_ctx_set_peer_salt */
  22275. /*
  22276. * Testing wc_ecc_ctx_set_info()
  22277. */
  22278. static int test_wc_ecc_ctx_set_info(void)
  22279. {
  22280. EXPECT_DECLS;
  22281. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22282. ecEncCtx* ctx = NULL;
  22283. WC_RNG rng;
  22284. const char* optInfo = "Optional Test Info.";
  22285. int optInfoSz = (int)XSTRLEN(optInfo);
  22286. const char* badOptInfo = NULL;
  22287. XMEMSET(&rng, 0, sizeof(rng));
  22288. ExpectIntEQ(wc_InitRng(&rng), 0);
  22289. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22290. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22291. /* Test bad args. */
  22292. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22293. BAD_FUNC_ARG);
  22294. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22295. BAD_FUNC_ARG);
  22296. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22297. wc_ecc_ctx_free(ctx);
  22298. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22299. #endif
  22300. return EXPECT_RESULT();
  22301. } /* END test_wc_ecc_ctx_set_info */
  22302. /*
  22303. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22304. */
  22305. static int test_wc_ecc_encryptDecrypt(void)
  22306. {
  22307. EXPECT_DECLS;
  22308. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22309. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22310. ecc_key srvKey;
  22311. ecc_key cliKey;
  22312. ecc_key tmpKey;
  22313. WC_RNG rng;
  22314. int ret;
  22315. const char* msg = "EccBlock Size 16";
  22316. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22317. #ifdef WOLFSSL_ECIES_OLD
  22318. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22319. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22320. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22321. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22322. #else
  22323. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22324. WC_SHA256_DIGEST_SIZE];
  22325. #endif
  22326. word32 outSz = (word32)sizeof(out);
  22327. byte plain[sizeof("EccBlock Size 16")];
  22328. word32 plainSz = (word32)sizeof(plain);
  22329. int keySz = KEY20;
  22330. /* Init stack variables. */
  22331. XMEMSET(out, 0, outSz);
  22332. XMEMSET(plain, 0, plainSz);
  22333. XMEMSET(&rng, 0, sizeof(rng));
  22334. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22335. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22336. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22337. ExpectIntEQ(wc_InitRng(&rng), 0);
  22338. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22339. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22340. #if defined(WOLFSSL_ASYNC_CRYPT)
  22341. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22342. #endif
  22343. ExpectIntEQ(ret, 0);
  22344. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22345. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22346. #if defined(WOLFSSL_ASYNC_CRYPT)
  22347. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22348. #endif
  22349. ExpectIntEQ(ret, 0);
  22350. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22351. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22352. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22353. !defined(HAVE_SELFTEST)
  22354. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22355. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22356. #endif
  22357. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22358. &outSz, NULL), 0);
  22359. /* Test bad args. */
  22360. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22361. NULL), BAD_FUNC_ARG);
  22362. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22363. NULL), BAD_FUNC_ARG);
  22364. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22365. NULL), BAD_FUNC_ARG);
  22366. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22367. &outSz, NULL), BAD_FUNC_ARG);
  22368. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22369. NULL), BAD_FUNC_ARG);
  22370. #ifdef WOLFSSL_ECIES_OLD
  22371. tmpKey.dp = cliKey.dp;
  22372. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22373. #endif
  22374. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22375. NULL), 0);
  22376. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22377. NULL), BAD_FUNC_ARG);
  22378. #ifdef WOLFSSL_ECIES_OLD
  22379. /* NULL parameter allowed in new implementations - public key comes from
  22380. * the message. */
  22381. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22382. NULL), BAD_FUNC_ARG);
  22383. #endif
  22384. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22385. NULL), BAD_FUNC_ARG);
  22386. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22387. NULL), BAD_FUNC_ARG);
  22388. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22389. BAD_FUNC_ARG);
  22390. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22391. wc_ecc_free(&tmpKey);
  22392. wc_ecc_free(&srvKey);
  22393. wc_ecc_free(&cliKey);
  22394. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22395. #endif
  22396. return EXPECT_RESULT();
  22397. } /* END test_wc_ecc_encryptDecrypt */
  22398. /*
  22399. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22400. */
  22401. static int test_wc_ecc_del_point(void)
  22402. {
  22403. EXPECT_DECLS;
  22404. #if defined(HAVE_ECC)
  22405. ecc_point* pt = NULL;
  22406. ExpectNotNull(pt = wc_ecc_new_point());
  22407. wc_ecc_del_point(pt);
  22408. #endif
  22409. return EXPECT_RESULT();
  22410. } /* END test_wc_ecc_del_point */
  22411. /*
  22412. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22413. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22414. * and wc_ecc_cmp_point()
  22415. */
  22416. static int test_wc_ecc_pointFns(void)
  22417. {
  22418. EXPECT_DECLS;
  22419. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22420. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22421. !defined(WOLFSSL_ATECC608A)
  22422. ecc_key key;
  22423. WC_RNG rng;
  22424. int ret;
  22425. ecc_point* point = NULL;
  22426. ecc_point* cpypt = NULL;
  22427. int idx = 0;
  22428. int keySz = KEY32;
  22429. byte der[DER_SZ(KEY32)];
  22430. word32 derlenChk = 0;
  22431. word32 derSz = DER_SZ(KEY32);
  22432. /* Init stack variables. */
  22433. XMEMSET(der, 0, derSz);
  22434. XMEMSET(&key, 0, sizeof(ecc_key));
  22435. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22436. ExpectIntEQ(wc_InitRng(&rng), 0);
  22437. ExpectIntEQ(wc_ecc_init(&key), 0);
  22438. ret = wc_ecc_make_key(&rng, keySz, &key);
  22439. #if defined(WOLFSSL_ASYNC_CRYPT)
  22440. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22441. #endif
  22442. ExpectIntEQ(ret, 0);
  22443. ExpectNotNull(point = wc_ecc_new_point());
  22444. ExpectNotNull(cpypt = wc_ecc_new_point());
  22445. /* Export */
  22446. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22447. &derlenChk), LENGTH_ONLY_E);
  22448. /* Check length value. */
  22449. ExpectIntEQ(derSz, derlenChk);
  22450. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22451. &derSz), 0);
  22452. /* Test bad args. */
  22453. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22454. ECC_BAD_ARG_E);
  22455. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22456. ECC_BAD_ARG_E);
  22457. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22458. NULL), ECC_BAD_ARG_E);
  22459. /* Import */
  22460. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22461. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22462. /* Test bad args. */
  22463. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22464. ECC_BAD_ARG_E);
  22465. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22466. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22467. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22468. ECC_BAD_ARG_E);
  22469. /* Copy */
  22470. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22471. /* Test bad args. */
  22472. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22473. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22474. /* Compare point */
  22475. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22476. /* Test bad args. */
  22477. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22478. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22479. /* At infinity if return == 1, otherwise return == 0. */
  22480. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22481. /* Test bad args. */
  22482. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22483. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22484. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22485. #ifdef USE_ECC_B_PARAM
  22486. /* On curve if ret == 0 */
  22487. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22488. /* Test bad args. */
  22489. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22490. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22491. #endif /* USE_ECC_B_PARAM */
  22492. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22493. /* Free */
  22494. wc_ecc_del_point(point);
  22495. wc_ecc_del_point(cpypt);
  22496. wc_ecc_free(&key);
  22497. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22498. #endif
  22499. return EXPECT_RESULT();
  22500. } /* END test_wc_ecc_pointFns */
  22501. /*
  22502. * Testing wc_ecc_shared_secret_ssh()
  22503. */
  22504. static int test_wc_ecc_shared_secret_ssh(void)
  22505. {
  22506. EXPECT_DECLS;
  22507. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22508. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22509. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22510. !defined(WOLFSSL_CRYPTOCELL)
  22511. ecc_key key;
  22512. ecc_key key2;
  22513. WC_RNG rng;
  22514. int ret;
  22515. int keySz = KEY32;
  22516. #if FIPS_VERSION3_GE(6,0,0)
  22517. int key2Sz = KEY28;
  22518. #else
  22519. int key2Sz = KEY24;
  22520. #endif
  22521. byte secret[KEY32];
  22522. word32 secretLen = (word32)keySz;
  22523. /* Init stack variables. */
  22524. XMEMSET(&key, 0, sizeof(ecc_key));
  22525. XMEMSET(&key2, 0, sizeof(ecc_key));
  22526. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22527. XMEMSET(secret, 0, secretLen);
  22528. PRIVATE_KEY_UNLOCK();
  22529. /* Make keys */
  22530. ExpectIntEQ(wc_ecc_init(&key), 0);
  22531. ExpectIntEQ(wc_InitRng(&rng), 0);
  22532. ret = wc_ecc_make_key(&rng, keySz, &key);
  22533. #if defined(WOLFSSL_ASYNC_CRYPT)
  22534. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22535. #endif
  22536. ExpectIntEQ(ret, 0);
  22537. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22538. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22539. ExpectIntEQ(wc_InitRng(&rng), 0);
  22540. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22541. #if defined(WOLFSSL_ASYNC_CRYPT)
  22542. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22543. #endif
  22544. ExpectIntEQ(ret, 0);
  22545. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22546. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22547. !defined(HAVE_SELFTEST)
  22548. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22549. #endif
  22550. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22551. &secretLen), 0);
  22552. /* Pass in bad args. */
  22553. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22554. &secretLen), BAD_FUNC_ARG);
  22555. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22556. BAD_FUNC_ARG);
  22557. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22558. BAD_FUNC_ARG);
  22559. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22560. BAD_FUNC_ARG);
  22561. key.type = ECC_PUBLICKEY;
  22562. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22563. &secretLen), ECC_BAD_ARG_E);
  22564. PRIVATE_KEY_LOCK();
  22565. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22566. wc_ecc_free(&key);
  22567. wc_ecc_free(&key2);
  22568. #ifdef FP_ECC
  22569. wc_ecc_fp_free();
  22570. #endif
  22571. #endif
  22572. return EXPECT_RESULT();
  22573. } /* END test_wc_ecc_shared_secret_ssh */
  22574. /*
  22575. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22576. */
  22577. static int test_wc_ecc_verify_hash_ex(void)
  22578. {
  22579. EXPECT_DECLS;
  22580. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22581. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22582. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22583. ecc_key key;
  22584. WC_RNG rng;
  22585. int ret;
  22586. mp_int r;
  22587. mp_int s;
  22588. mp_int z;
  22589. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22590. unsigned char iHash[] = "Everyone gets Friday off.......";
  22591. unsigned char shortHash[] = TEST_STRING;
  22592. word32 hashlen = sizeof(hash);
  22593. word32 iHashLen = sizeof(iHash);
  22594. word32 shortHashLen = sizeof(shortHash);
  22595. int keySz = KEY32;
  22596. int verify_ok = 0;
  22597. XMEMSET(&key, 0, sizeof(ecc_key));
  22598. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22599. XMEMSET(&r, 0, sizeof(mp_int));
  22600. XMEMSET(&s, 0, sizeof(mp_int));
  22601. XMEMSET(&z, 0, sizeof(mp_int));
  22602. /* Initialize r, s and z. */
  22603. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22604. ExpectIntEQ(wc_ecc_init(&key), 0);
  22605. ExpectIntEQ(wc_InitRng(&rng), 0);
  22606. ret = wc_ecc_make_key(&rng, keySz, &key);
  22607. #if defined(WOLFSSL_ASYNC_CRYPT)
  22608. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22609. #endif
  22610. ExpectIntEQ(ret, 0);
  22611. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22612. /* verify_ok should be 1. */
  22613. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22614. 0);
  22615. ExpectIntEQ(verify_ok, 1);
  22616. /* verify_ok should be 0 */
  22617. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22618. &key), 0);
  22619. ExpectIntEQ(verify_ok, 0);
  22620. /* verify_ok should be 0. */
  22621. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22622. &verify_ok, &key), 0);
  22623. ExpectIntEQ(verify_ok, 0);
  22624. /* Test bad args. */
  22625. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22626. ECC_BAD_ARG_E);
  22627. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22628. ECC_BAD_ARG_E);
  22629. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22630. ECC_BAD_ARG_E);
  22631. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22632. ECC_BAD_ARG_E);
  22633. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22634. ECC_BAD_ARG_E);
  22635. /* Test bad args. */
  22636. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22637. &verify_ok, &key), ECC_BAD_ARG_E);
  22638. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22639. &verify_ok, &key), ECC_BAD_ARG_E);
  22640. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22641. &verify_ok, &key), MP_ZERO_E);
  22642. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22643. &verify_ok, &key), MP_ZERO_E);
  22644. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22645. &verify_ok, &key), MP_ZERO_E);
  22646. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22647. &key), ECC_BAD_ARG_E);
  22648. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22649. &key), ECC_BAD_ARG_E);
  22650. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22651. &verify_ok, NULL), ECC_BAD_ARG_E);
  22652. wc_ecc_free(&key);
  22653. mp_free(&r);
  22654. mp_free(&s);
  22655. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22656. #endif
  22657. return EXPECT_RESULT();
  22658. } /* END test_wc_ecc_verify_hash_ex */
  22659. /*
  22660. * Testing wc_ecc_mulmod()
  22661. */
  22662. static int test_wc_ecc_mulmod(void)
  22663. {
  22664. EXPECT_DECLS;
  22665. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22666. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22667. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22668. ecc_key key1;
  22669. ecc_key key2;
  22670. ecc_key key3;
  22671. WC_RNG rng;
  22672. int ret;
  22673. XMEMSET(&key1, 0, sizeof(ecc_key));
  22674. XMEMSET(&key2, 0, sizeof(ecc_key));
  22675. XMEMSET(&key3, 0, sizeof(ecc_key));
  22676. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22677. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22678. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22679. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22680. ExpectIntEQ(wc_InitRng(&rng), 0);
  22681. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22682. #if defined(WOLFSSL_ASYNC_CRYPT)
  22683. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22684. #endif
  22685. ExpectIntEQ(ret, 0);
  22686. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22687. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22688. key1.dp->Af, ECC_SECP256R1), 0);
  22689. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22690. key1.dp->prime, ECC_SECP256R1), 0);
  22691. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22692. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22693. 1), 0);
  22694. /* Test bad args. */
  22695. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22696. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22697. ECC_BAD_ARG_E);
  22698. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22699. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22700. ECC_BAD_ARG_E);
  22701. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22702. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22703. ECC_BAD_ARG_E);
  22704. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22705. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22706. wc_ecc_free(&key1);
  22707. wc_ecc_free(&key2);
  22708. wc_ecc_free(&key3);
  22709. #ifdef FP_ECC
  22710. wc_ecc_fp_free();
  22711. #endif
  22712. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22713. return EXPECT_RESULT();
  22714. } /* END test_wc_ecc_mulmod */
  22715. /*
  22716. * Testing wc_ecc_is_valid_idx()
  22717. */
  22718. static int test_wc_ecc_is_valid_idx(void)
  22719. {
  22720. EXPECT_DECLS;
  22721. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22722. ecc_key key;
  22723. WC_RNG rng;
  22724. int ret;
  22725. int iVal = -2;
  22726. int iVal2 = 3000;
  22727. XMEMSET(&key, 0, sizeof(ecc_key));
  22728. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22729. ExpectIntEQ(wc_ecc_init(&key), 0);
  22730. ExpectIntEQ(wc_InitRng(&rng), 0);
  22731. ret = wc_ecc_make_key(&rng, 32, &key);
  22732. #if defined(WOLFSSL_ASYNC_CRYPT)
  22733. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22734. #endif
  22735. ExpectIntEQ(ret, 0);
  22736. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22737. /* Test bad args. */
  22738. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22739. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22740. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22741. wc_ecc_free(&key);
  22742. #ifdef FP_ECC
  22743. wc_ecc_fp_free();
  22744. #endif
  22745. #endif
  22746. return EXPECT_RESULT();
  22747. } /* END test_wc_ecc_is_valid_idx */
  22748. /*
  22749. * Testing wc_ecc_get_curve_id_from_oid()
  22750. */
  22751. static int test_wc_ecc_get_curve_id_from_oid(void)
  22752. {
  22753. EXPECT_DECLS;
  22754. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22755. !defined(HAVE_FIPS)
  22756. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22757. word32 len = sizeof(oid);
  22758. /* Bad Cases */
  22759. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22760. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22761. /* Good Case */
  22762. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22763. #endif
  22764. return EXPECT_RESULT();
  22765. } /* END test_wc_ecc_get_curve_id_from_oid */
  22766. /*
  22767. * Testing wc_ecc_sig_size_calc()
  22768. */
  22769. static int test_wc_ecc_sig_size_calc(void)
  22770. {
  22771. EXPECT_DECLS;
  22772. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22773. ecc_key key;
  22774. WC_RNG rng;
  22775. int sz = 0;
  22776. int ret;
  22777. XMEMSET(&key, 0, sizeof(ecc_key));
  22778. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22779. ExpectIntEQ(wc_ecc_init(&key), 0);
  22780. ExpectIntEQ(wc_InitRng(&rng), 0);
  22781. ret = wc_ecc_make_key(&rng, 16, &key);
  22782. #if defined(WOLFSSL_ASYNC_CRYPT)
  22783. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22784. #endif
  22785. #if FIPS_VERSION3_GE(6,0,0)
  22786. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22787. #else
  22788. ExpectIntEQ(ret, 0);
  22789. #endif
  22790. #if FIPS_VERSION3_LT(6,0,0)
  22791. sz = key.dp->size;
  22792. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22793. #else
  22794. (void) sz;
  22795. #endif
  22796. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22797. wc_ecc_free(&key);
  22798. #endif
  22799. return EXPECT_RESULT();
  22800. } /* END test_wc_ecc_sig_size_calc */
  22801. /*
  22802. * Testing wc_ecc_sm2_make_key()
  22803. */
  22804. static int test_wc_ecc_sm2_make_key(void)
  22805. {
  22806. int res = TEST_SKIPPED;
  22807. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22808. EXPECT_DECLS;
  22809. WC_RNG rng[1];
  22810. ecc_key key[1];
  22811. XMEMSET(rng, 0, sizeof(*rng));
  22812. XMEMSET(key, 0, sizeof(*key));
  22813. ExpectIntEQ(wc_InitRng(rng), 0);
  22814. ExpectIntEQ(wc_ecc_init(key), 0);
  22815. /* Test invalid parameters. */
  22816. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22817. BAD_FUNC_ARG);
  22818. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22819. BAD_FUNC_ARG);
  22820. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22821. BAD_FUNC_ARG);
  22822. /* Test valid parameters. */
  22823. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22824. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22825. wc_ecc_free(key);
  22826. wc_FreeRng(rng);
  22827. #ifdef FP_ECC
  22828. wc_ecc_fp_free();
  22829. #endif
  22830. res = EXPECT_RESULT();
  22831. #endif
  22832. return res;
  22833. }
  22834. /*
  22835. * Testing wc_ecc_sm2_shared_secret()
  22836. */
  22837. static int test_wc_ecc_sm2_shared_secret(void)
  22838. {
  22839. int res = TEST_SKIPPED;
  22840. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22841. EXPECT_DECLS;
  22842. WC_RNG rng[1];
  22843. ecc_key keyA[1];
  22844. ecc_key keyB[1];
  22845. byte outA[32];
  22846. byte outB[32];
  22847. word32 outALen = 32;
  22848. word32 outBLen = 32;
  22849. XMEMSET(rng, 0, sizeof(*rng));
  22850. XMEMSET(keyA, 0, sizeof(*keyA));
  22851. XMEMSET(keyB, 0, sizeof(*keyB));
  22852. ExpectIntEQ(wc_InitRng(rng), 0);
  22853. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22854. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22855. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22856. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22857. #ifdef ECC_TIMING_RESISTANT
  22858. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22859. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22860. #endif
  22861. /* Test invalid parameters. */
  22862. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22863. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22864. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22865. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22866. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22867. BAD_FUNC_ARG);
  22868. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22869. BAD_FUNC_ARG);
  22870. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22871. BAD_FUNC_ARG);
  22872. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22873. BAD_FUNC_ARG);
  22874. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22875. /* Test valid parameters. */
  22876. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22877. ExpectIntLE(outALen, 32);
  22878. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22879. ExpectIntLE(outBLen, 32);
  22880. ExpectIntEQ(outALen, outBLen);
  22881. ExpectBufEQ(outA, outB, outALen);
  22882. wc_ecc_free(keyB);
  22883. wc_ecc_free(keyA);
  22884. wc_FreeRng(rng);
  22885. #ifdef FP_ECC
  22886. wc_ecc_fp_free();
  22887. #endif
  22888. res = EXPECT_RESULT();
  22889. #endif
  22890. return res;
  22891. }
  22892. /*
  22893. * Testing wc_ecc_sm2_create_digest()
  22894. */
  22895. static int test_wc_ecc_sm2_create_digest(void)
  22896. {
  22897. int res = TEST_SKIPPED;
  22898. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22899. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22900. EXPECT_DECLS;
  22901. ecc_key key[1];
  22902. enum wc_HashType hashType;
  22903. unsigned char pub[] = {
  22904. 0x04,
  22905. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22906. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22907. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22908. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22909. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22910. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22911. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22912. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22913. };
  22914. unsigned char id[] = {
  22915. 0x01, 0x02, 0x03,
  22916. };
  22917. unsigned char msg[] = {
  22918. 0x01, 0x02, 0x03,
  22919. };
  22920. unsigned char hash[32];
  22921. #ifdef WOLFSSL_SM3
  22922. unsigned char expHash[32] = {
  22923. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22924. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22925. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22926. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22927. };
  22928. #else
  22929. unsigned char expHash[32] = {
  22930. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22931. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22932. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22933. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22934. };
  22935. #endif
  22936. #ifdef WOLFSSL_SM3
  22937. hashType = WC_HASH_TYPE_SM3;
  22938. #else
  22939. hashType = WC_HASH_TYPE_SHA256;
  22940. #endif
  22941. XMEMSET(key, 0, sizeof(*key));
  22942. ExpectIntEQ(wc_ecc_init(key), 0);
  22943. /* Test with no curve set. */
  22944. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22945. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22946. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22947. /* Test invalid parameters. */
  22948. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22949. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22950. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22951. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22952. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22953. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22954. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22955. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22956. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22957. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22958. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22959. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22960. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22961. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22962. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22963. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22964. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22965. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22966. /* Bad hash type. */
  22967. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  22968. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22969. -1, hash, 0, key), BAD_FUNC_ARG);
  22970. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  22971. /* Bad hash size. */
  22972. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22973. hashType, hash, 0, key), BUFFER_E);
  22974. /* Test valid parameters. */
  22975. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22976. hashType, hash, sizeof(hash), key), 0);
  22977. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22978. wc_ecc_free(key);
  22979. res = EXPECT_RESULT();
  22980. #endif
  22981. return res;
  22982. }
  22983. /*
  22984. * Testing wc_ecc_sm2_verify_hash_ex()
  22985. */
  22986. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22987. {
  22988. int res = TEST_SKIPPED;
  22989. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22990. defined(WOLFSSL_PUBLIC_MP)
  22991. EXPECT_DECLS;
  22992. ecc_key key[1];
  22993. mp_int r[1];
  22994. mp_int s[1];
  22995. int verified;
  22996. unsigned char pub[] = {
  22997. 0x04,
  22998. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22999. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23000. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23001. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23002. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23003. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23004. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23005. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23006. };
  23007. unsigned char hash[] = {
  23008. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23009. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23010. 0x50, 0x69, 0x5B, 0x20
  23011. };
  23012. unsigned char rData[] = {
  23013. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23014. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23015. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23016. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  23017. };
  23018. unsigned char sData[] = {
  23019. 0x1D,
  23020. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23021. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23022. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23023. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23024. };
  23025. unsigned char rBadData[] = {
  23026. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23027. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23028. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23029. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  23030. };
  23031. XMEMSET(key, 0, sizeof(*key));
  23032. XMEMSET(r, 0, sizeof(*r));
  23033. XMEMSET(s, 0, sizeof(*s));
  23034. ExpectIntEQ(mp_init(r), 0);
  23035. ExpectIntEQ(mp_init(s), 0);
  23036. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  23037. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  23038. ExpectIntEQ(wc_ecc_init(key), 0);
  23039. /* Test with no curve set. */
  23040. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23041. &verified, key), BAD_FUNC_ARG);
  23042. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23043. /* Test invalid parameters. */
  23044. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23045. NULL, NULL), BAD_FUNC_ARG);
  23046. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  23047. NULL, NULL), BAD_FUNC_ARG);
  23048. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  23049. NULL, NULL), BAD_FUNC_ARG);
  23050. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  23051. NULL, NULL), BAD_FUNC_ARG);
  23052. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23053. &verified, NULL), BAD_FUNC_ARG);
  23054. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23055. NULL, key), BAD_FUNC_ARG);
  23056. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  23057. &verified, key), BAD_FUNC_ARG);
  23058. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  23059. &verified, key), BAD_FUNC_ARG);
  23060. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  23061. &verified, key), BAD_FUNC_ARG);
  23062. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23063. NULL, key), BAD_FUNC_ARG);
  23064. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23065. &verified, NULL), BAD_FUNC_ARG);
  23066. /* Make key not on the SM2 curve. */
  23067. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23068. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23069. &verified, key), BAD_FUNC_ARG);
  23070. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23071. /* Test valid parameters. */
  23072. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23073. &verified, key), 0);
  23074. ExpectIntEQ(verified, 1);
  23075. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  23076. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23077. &verified, key), 0);
  23078. ExpectIntEQ(verified, 0);
  23079. mp_free(s);
  23080. mp_free(r);
  23081. wc_ecc_free(key);
  23082. #ifdef FP_ECC
  23083. wc_ecc_fp_free();
  23084. #endif
  23085. res = EXPECT_RESULT();
  23086. #endif
  23087. return res;
  23088. }
  23089. /*
  23090. * Testing wc_ecc_sm2_verify_hash()
  23091. */
  23092. static int test_wc_ecc_sm2_verify_hash(void)
  23093. {
  23094. int res = TEST_SKIPPED;
  23095. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  23096. EXPECT_DECLS;
  23097. ecc_key key[1];
  23098. int verified;
  23099. unsigned char pub[] = {
  23100. 0x04,
  23101. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23102. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23103. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23104. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23105. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23106. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23107. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23108. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23109. };
  23110. unsigned char hash[] = {
  23111. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23112. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23113. 0x50, 0x69, 0x5B, 0x20
  23114. };
  23115. unsigned char sig[] = {
  23116. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23117. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23118. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23119. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23120. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23121. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23122. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23123. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23124. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23125. };
  23126. unsigned char sigBad[] = {
  23127. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23128. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23129. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23130. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23131. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23132. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23133. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23134. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23135. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  23136. };
  23137. XMEMSET(key, 0, sizeof(*key));
  23138. ExpectIntEQ(wc_ecc_init(key), 0);
  23139. /* Test with no curve set. */
  23140. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23141. &verified, key), BAD_FUNC_ARG);
  23142. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23143. /* Test invalid parameters. */
  23144. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23145. NULL, NULL), BAD_FUNC_ARG);
  23146. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23147. NULL, NULL), BAD_FUNC_ARG);
  23148. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23149. NULL, NULL), BAD_FUNC_ARG);
  23150. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23151. &verified, NULL), BAD_FUNC_ARG);
  23152. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23153. NULL, key), BAD_FUNC_ARG);
  23154. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23155. &verified, key), BAD_FUNC_ARG);
  23156. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23157. &verified, key), BAD_FUNC_ARG);
  23158. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23159. NULL, key), BAD_FUNC_ARG);
  23160. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23161. &verified, NULL), BAD_FUNC_ARG);
  23162. /* Make key not on the SM2 curve. */
  23163. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23164. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23165. &verified, key), BAD_FUNC_ARG);
  23166. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23167. /* Test valid parameters. */
  23168. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23169. &verified, key), 0);
  23170. ExpectIntEQ(verified, 1);
  23171. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  23172. sizeof(hash), &verified, key), 0);
  23173. ExpectIntEQ(verified, 0);
  23174. wc_ecc_free(key);
  23175. #ifdef FP_ECC
  23176. wc_ecc_fp_free();
  23177. #endif
  23178. res = EXPECT_RESULT();
  23179. #endif
  23180. return res;
  23181. }
  23182. /*
  23183. * Testing wc_ecc_sm2_verify_hash_ex()
  23184. */
  23185. static int test_wc_ecc_sm2_sign_hash_ex(void)
  23186. {
  23187. int res = TEST_SKIPPED;
  23188. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  23189. defined(WOLFSSL_PUBLIC_MP)
  23190. EXPECT_DECLS;
  23191. WC_RNG rng[1];
  23192. ecc_key key[1];
  23193. mp_int r[1];
  23194. mp_int s[1];
  23195. unsigned char hash[32];
  23196. #ifdef HAVE_ECC_VERIFY
  23197. int verified;
  23198. #endif
  23199. XMEMSET(rng, 0, sizeof(*rng));
  23200. XMEMSET(key, 0, sizeof(*key));
  23201. XMEMSET(r, 0, sizeof(*r));
  23202. XMEMSET(s, 0, sizeof(*s));
  23203. ExpectIntEQ(wc_InitRng(rng), 0);
  23204. ExpectIntEQ(mp_init(r), 0);
  23205. ExpectIntEQ(mp_init(s), 0);
  23206. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23207. ExpectIntEQ(wc_ecc_init(key), 0);
  23208. /* Test with no curve set. */
  23209. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23210. BAD_FUNC_ARG);
  23211. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23212. /* Test invalid parameters. */
  23213. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23214. NULL), BAD_FUNC_ARG);
  23215. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23216. NULL), BAD_FUNC_ARG);
  23217. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23218. NULL), BAD_FUNC_ARG);
  23219. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23220. NULL), BAD_FUNC_ARG);
  23221. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23222. NULL), BAD_FUNC_ARG);
  23223. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23224. s), BAD_FUNC_ARG);
  23225. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23226. BAD_FUNC_ARG);
  23227. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23228. BAD_FUNC_ARG);
  23229. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23230. BAD_FUNC_ARG);
  23231. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23232. BAD_FUNC_ARG);
  23233. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23234. BAD_FUNC_ARG);
  23235. /* Make key not on the SM2 curve. */
  23236. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23237. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23238. BAD_FUNC_ARG);
  23239. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23240. #ifdef WOLFSSL_SP_MATH_ALL
  23241. {
  23242. mp_int smallR[1];
  23243. sp_init_size(smallR, 1);
  23244. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23245. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23246. smallR, s), 0);
  23247. }
  23248. #endif
  23249. /* Test valid parameters. */
  23250. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23251. 0);
  23252. #ifdef HAVE_ECC_VERIFY
  23253. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23254. key), 0);
  23255. ExpectIntEQ(verified, 1);
  23256. #endif
  23257. mp_free(s);
  23258. mp_free(r);
  23259. wc_ecc_free(key);
  23260. wc_FreeRng(rng);
  23261. #ifdef FP_ECC
  23262. wc_ecc_fp_free();
  23263. #endif
  23264. res = EXPECT_RESULT();
  23265. #endif
  23266. return res;
  23267. }
  23268. /*
  23269. * Testing wc_ecc_sm2_verify_hash()
  23270. */
  23271. static int test_wc_ecc_sm2_sign_hash(void)
  23272. {
  23273. int res = TEST_SKIPPED;
  23274. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23275. EXPECT_DECLS;
  23276. WC_RNG rng[1];
  23277. ecc_key key[1];
  23278. unsigned char hash[32];
  23279. unsigned char sig[72];
  23280. word32 sigSz = sizeof(sig);
  23281. #ifdef HAVE_ECC_VERIFY
  23282. int verified;
  23283. #endif
  23284. XMEMSET(rng, 0, sizeof(*rng));
  23285. XMEMSET(key, 0, sizeof(*key));
  23286. ExpectIntEQ(wc_InitRng(rng), 0);
  23287. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23288. ExpectIntEQ(wc_ecc_init(key), 0);
  23289. /* Test with no curve set. */
  23290. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23291. BAD_FUNC_ARG);
  23292. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23293. /* Test invalid parameters. */
  23294. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23295. NULL), BAD_FUNC_ARG);
  23296. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23297. NULL), BAD_FUNC_ARG);
  23298. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23299. NULL), BAD_FUNC_ARG);
  23300. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23301. NULL), BAD_FUNC_ARG);
  23302. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23303. NULL), BAD_FUNC_ARG);
  23304. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23305. key), BAD_FUNC_ARG);
  23306. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23307. key), BAD_FUNC_ARG);
  23308. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23309. key), BAD_FUNC_ARG);
  23310. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23311. key), BAD_FUNC_ARG);
  23312. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23313. key), BAD_FUNC_ARG);
  23314. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23315. NULL), BAD_FUNC_ARG);
  23316. /* Make key not on the SM2 curve. */
  23317. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23318. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23319. BAD_FUNC_ARG);
  23320. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23321. /* Test valid parameters. */
  23322. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23323. 0);
  23324. #ifdef HAVE_ECC_VERIFY
  23325. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23326. &verified, key), 0);
  23327. ExpectIntEQ(verified, 1);
  23328. #endif
  23329. wc_ecc_free(key);
  23330. wc_FreeRng(rng);
  23331. #ifdef FP_ECC
  23332. wc_ecc_fp_free();
  23333. #endif
  23334. res = EXPECT_RESULT();
  23335. #endif
  23336. return res;
  23337. }
  23338. /*
  23339. * Testing ToTraditional
  23340. */
  23341. static int test_ToTraditional(void)
  23342. {
  23343. EXPECT_DECLS;
  23344. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23345. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23346. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23347. XFILE f = XBADFILE;
  23348. byte input[TWOK_BUF];
  23349. word32 sz = 0;
  23350. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23351. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23352. if (f != XBADFILE)
  23353. XFCLOSE(f);
  23354. /* Good case */
  23355. ExpectIntGT(ToTraditional(input, sz), 0);
  23356. /* Bad cases */
  23357. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23358. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23359. #ifdef WOLFSSL_ASN_TEMPLATE
  23360. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23361. #else
  23362. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23363. #endif
  23364. #endif
  23365. return EXPECT_RESULT();
  23366. } /* End test_ToTraditional*/
  23367. /*
  23368. * Testing wc_EccPrivateKeyToDer
  23369. */
  23370. static int test_wc_EccPrivateKeyToDer(void)
  23371. {
  23372. EXPECT_DECLS;
  23373. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23374. byte output[ONEK_BUF];
  23375. ecc_key eccKey;
  23376. WC_RNG rng;
  23377. word32 inLen;
  23378. word32 outLen = 0;
  23379. int ret;
  23380. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23381. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23382. PRIVATE_KEY_UNLOCK();
  23383. ExpectIntEQ(wc_InitRng(&rng), 0);
  23384. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23385. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23386. #if defined(WOLFSSL_ASYNC_CRYPT)
  23387. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23388. #endif
  23389. ExpectIntEQ(ret, 0);
  23390. inLen = (word32)sizeof(output);
  23391. /* Bad Cases */
  23392. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23393. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23394. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23395. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23396. /* Good Case */
  23397. ExpectIntGT(outLen = (word32)wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23398. wc_ecc_free(&eccKey);
  23399. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23400. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23401. {
  23402. /* test importing private only into a PKEY struct */
  23403. EC_KEY* ec = NULL;
  23404. EVP_PKEY* pkey = NULL;
  23405. const unsigned char* der;
  23406. der = output;
  23407. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23408. der = output;
  23409. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23410. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23411. if (EXPECT_FAIL()) {
  23412. EC_KEY_free(ec);
  23413. }
  23414. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23415. }
  23416. #endif
  23417. PRIVATE_KEY_LOCK();
  23418. #endif
  23419. return EXPECT_RESULT();
  23420. } /* End test_wc_EccPrivateKeyToDer*/
  23421. /*
  23422. * Testing wc_DhPublicKeyDecode
  23423. */
  23424. static int test_wc_DhPublicKeyDecode(void)
  23425. {
  23426. EXPECT_DECLS;
  23427. #ifndef NO_DH
  23428. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23429. DhKey key;
  23430. word32 inOutIdx;
  23431. XMEMSET(&key, 0, sizeof(DhKey));
  23432. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23433. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23434. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23435. BAD_FUNC_ARG);
  23436. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23437. BAD_FUNC_ARG);
  23438. inOutIdx = 0;
  23439. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23440. BAD_FUNC_ARG);
  23441. inOutIdx = 0;
  23442. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23443. BAD_FUNC_ARG);
  23444. inOutIdx = 0;
  23445. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23446. sizeof_dh_pub_key_der_2048), 0);
  23447. ExpectIntNE(key.p.used, 0);
  23448. ExpectIntNE(key.g.used, 0);
  23449. ExpectIntEQ(key.q.used, 0);
  23450. ExpectIntNE(key.pub.used, 0);
  23451. ExpectIntEQ(key.priv.used, 0);
  23452. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23453. #endif
  23454. #endif /* !NO_DH */
  23455. return EXPECT_RESULT();
  23456. }
  23457. /*
  23458. * Testing wc_Ed25519KeyToDer
  23459. */
  23460. static int test_wc_Ed25519KeyToDer(void)
  23461. {
  23462. EXPECT_DECLS;
  23463. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23464. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23465. byte output[ONEK_BUF];
  23466. ed25519_key ed25519Key;
  23467. WC_RNG rng;
  23468. word32 inLen;
  23469. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23470. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23471. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23472. ExpectIntEQ(wc_InitRng(&rng), 0);
  23473. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23474. inLen = (word32)sizeof(output);
  23475. /* Bad Cases */
  23476. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23477. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23478. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23479. /* Good Cases */
  23480. /* length only */
  23481. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23482. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23483. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23484. wc_ed25519_free(&ed25519Key);
  23485. #endif
  23486. return EXPECT_RESULT();
  23487. } /* End test_wc_Ed25519KeyToDer*/
  23488. /*
  23489. * Testing wc_Ed25519PrivateKeyToDer
  23490. */
  23491. static int test_wc_Ed25519PrivateKeyToDer(void)
  23492. {
  23493. EXPECT_DECLS;
  23494. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23495. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23496. byte output[ONEK_BUF];
  23497. ed25519_key ed25519PrivKey;
  23498. WC_RNG rng;
  23499. word32 inLen;
  23500. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23501. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23502. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23503. ExpectIntEQ(wc_InitRng(&rng), 0);
  23504. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23505. 0);
  23506. inLen = (word32)sizeof(output);
  23507. /* Bad Cases */
  23508. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23509. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23510. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23511. BAD_FUNC_ARG);
  23512. /* Good Cases */
  23513. /* length only */
  23514. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23515. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23516. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23517. wc_ed25519_free(&ed25519PrivKey);
  23518. #endif
  23519. return EXPECT_RESULT();
  23520. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23521. /*
  23522. * Testing wc_Ed448KeyToDer
  23523. */
  23524. static int test_wc_Ed448KeyToDer(void)
  23525. {
  23526. EXPECT_DECLS;
  23527. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23528. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23529. byte output[ONEK_BUF];
  23530. ed448_key ed448Key;
  23531. WC_RNG rng;
  23532. word32 inLen;
  23533. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23534. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23535. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23536. ExpectIntEQ(wc_InitRng(&rng), 0);
  23537. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23538. inLen = (word32)sizeof(output);
  23539. /* Bad Cases */
  23540. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23541. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23542. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23543. /* Good Cases */
  23544. /* length only */
  23545. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23546. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23547. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23548. wc_ed448_free(&ed448Key);
  23549. #endif
  23550. return EXPECT_RESULT();
  23551. } /* End test_wc_Ed448KeyToDer*/
  23552. /*
  23553. * Testing wc_Ed448PrivateKeyToDer
  23554. */
  23555. static int test_wc_Ed448PrivateKeyToDer(void)
  23556. {
  23557. EXPECT_DECLS;
  23558. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23559. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23560. byte output[ONEK_BUF];
  23561. ed448_key ed448PrivKey;
  23562. WC_RNG rng;
  23563. word32 inLen;
  23564. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23565. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23566. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23567. ExpectIntEQ(wc_InitRng(&rng), 0);
  23568. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23569. 0);
  23570. inLen = (word32)sizeof(output);
  23571. /* Bad Cases */
  23572. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23573. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23574. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23575. BAD_FUNC_ARG);
  23576. /* Good cases */
  23577. /* length only */
  23578. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23579. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23580. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23581. wc_ed448_free(&ed448PrivKey);
  23582. #endif
  23583. return EXPECT_RESULT();
  23584. } /* End test_wc_Ed448PrivateKeyToDer*/
  23585. /*
  23586. * Testing wc_Curve448PrivateKeyToDer
  23587. */
  23588. static int test_wc_Curve448PrivateKeyToDer(void)
  23589. {
  23590. EXPECT_DECLS;
  23591. #if defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_EXPORT) && \
  23592. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23593. byte output[ONEK_BUF];
  23594. curve448_key curve448PrivKey;
  23595. WC_RNG rng;
  23596. word32 inLen;
  23597. XMEMSET(&curve448PrivKey, 0, sizeof(curve448PrivKey));
  23598. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23599. ExpectIntEQ(wc_curve448_init(&curve448PrivKey), 0);
  23600. ExpectIntEQ(wc_InitRng(&rng), 0);
  23601. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &curve448PrivKey),
  23602. 0);
  23603. inLen = (word32)sizeof(output);
  23604. /* Bad Cases */
  23605. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23606. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23607. ExpectIntEQ(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, 0),
  23608. BAD_FUNC_ARG);
  23609. /* Good cases */
  23610. /* length only */
  23611. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
  23612. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);
  23613. /* Bad Cases */
  23614. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  23615. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, output, inLen, 0), BAD_FUNC_ARG);
  23616. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 0),
  23617. BUFFER_E);
  23618. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 1),
  23619. BUFFER_E);
  23620. /* Good cases */
  23621. /* length only */
  23622. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 0), 0);
  23623. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 1), 0);
  23624. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 0), 0);
  23625. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);
  23626. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23627. wc_curve448_free(&curve448PrivKey);
  23628. #endif
  23629. return EXPECT_RESULT();
  23630. } /* End wc_Curve448PrivateKeyToDer*/
  23631. /*
  23632. * Testing wc_SetSubjectBuffer
  23633. */
  23634. static int test_wc_SetSubjectBuffer(void)
  23635. {
  23636. EXPECT_DECLS;
  23637. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23638. Cert cert;
  23639. XFILE file = XBADFILE;
  23640. byte* der = NULL;
  23641. word32 derSz;
  23642. derSz = FOURK_BUF;
  23643. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23644. DYNAMIC_TYPE_TMP_BUFFER));
  23645. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23646. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23647. if (file != XBADFILE)
  23648. XFCLOSE(file);
  23649. ExpectIntEQ(wc_InitCert(&cert), 0);
  23650. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, (int)derSz), 0);
  23651. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, (int)derSz), BAD_FUNC_ARG);
  23652. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23653. #endif
  23654. return EXPECT_RESULT();
  23655. } /* End test_wc_SetSubjectBuffer*/
  23656. /*
  23657. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23658. */
  23659. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23660. {
  23661. EXPECT_DECLS;
  23662. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23663. WC_RNG rng;
  23664. Cert cert;
  23665. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23666. RsaKey rsaKey;
  23667. int bits = 2048;
  23668. #endif
  23669. #if defined(HAVE_ECC)
  23670. ecc_key eccKey;
  23671. int ret;
  23672. #endif
  23673. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23674. ed25519_key ed25519Key;
  23675. #endif
  23676. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23677. ed448_key ed448Key;
  23678. #endif
  23679. #ifndef HAVE_FIPS
  23680. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23681. #else
  23682. ExpectIntEQ(wc_InitRng(&rng), 0);
  23683. #endif
  23684. ExpectIntEQ(wc_InitCert(&cert), 0);
  23685. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23686. /* RSA */
  23687. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23688. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23689. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23690. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23691. 0);
  23692. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23693. #endif
  23694. #if defined(HAVE_ECC)
  23695. /* ECC */
  23696. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23697. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23698. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23699. #if defined(WOLFSSL_ASYNC_CRYPT)
  23700. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23701. #endif
  23702. ExpectIntEQ(ret, 0);
  23703. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23704. 0);
  23705. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23706. #endif
  23707. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23708. /* ED25519 */
  23709. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23710. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23711. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23712. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23713. &ed25519Key), 0);
  23714. wc_ed25519_free(&ed25519Key);
  23715. #endif
  23716. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23717. /* ED448 */
  23718. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23719. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23720. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23721. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23722. &ed448Key), 0);
  23723. wc_ed448_free(&ed448Key);
  23724. #endif
  23725. wc_FreeRng(&rng);
  23726. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23727. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23728. return EXPECT_RESULT();
  23729. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23730. /*
  23731. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23732. */
  23733. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23734. {
  23735. EXPECT_DECLS;
  23736. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23737. WC_RNG rng;
  23738. Cert cert;
  23739. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23740. RsaKey rsaKey;
  23741. int bits = 2048;
  23742. #endif
  23743. #if defined(HAVE_ECC)
  23744. ecc_key eccKey;
  23745. int ret;
  23746. #endif
  23747. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23748. ed25519_key ed25519Key;
  23749. #endif
  23750. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23751. ed448_key ed448Key;
  23752. #endif
  23753. #ifndef HAVE_FIPS
  23754. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23755. #else
  23756. ExpectIntEQ(wc_InitRng(&rng), 0);
  23757. #endif
  23758. ExpectIntEQ(wc_InitCert(&cert), 0);
  23759. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23760. /* RSA */
  23761. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23762. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23763. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23764. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23765. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23766. #endif
  23767. #if defined(HAVE_ECC)
  23768. /* ECC */
  23769. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23770. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23771. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23772. #if defined(WOLFSSL_ASYNC_CRYPT)
  23773. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23774. #endif
  23775. ExpectIntEQ(ret, 0);
  23776. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23777. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23778. #endif
  23779. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23780. /* ED25519 */
  23781. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23782. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23783. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23784. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23785. &ed25519Key), 0);
  23786. wc_ed25519_free(&ed25519Key);
  23787. #endif
  23788. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23789. /* ED448 */
  23790. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23791. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23792. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23793. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23794. 0);
  23795. wc_ed448_free(&ed448Key);
  23796. #endif
  23797. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23798. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23799. return EXPECT_RESULT();
  23800. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23801. /*
  23802. * Testing wc_PKCS7_New()
  23803. */
  23804. static int test_wc_PKCS7_New(void)
  23805. {
  23806. EXPECT_DECLS;
  23807. #if defined(HAVE_PKCS7)
  23808. PKCS7* pkcs7 = NULL;
  23809. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23810. wc_PKCS7_Free(pkcs7);
  23811. #endif
  23812. return EXPECT_RESULT();
  23813. } /* END test-wc_PKCS7_New */
  23814. /*
  23815. * Testing wc_PKCS7_Init()
  23816. */
  23817. static int test_wc_PKCS7_Init(void)
  23818. {
  23819. EXPECT_DECLS;
  23820. #if defined(HAVE_PKCS7)
  23821. PKCS7* pkcs7 = NULL;
  23822. void* heap = NULL;
  23823. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23824. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23825. /* Pass in bad args. */
  23826. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23827. wc_PKCS7_Free(pkcs7);
  23828. #endif
  23829. return EXPECT_RESULT();
  23830. } /* END test-wc_PKCS7_Init */
  23831. /*
  23832. * Testing wc_PKCS7_InitWithCert()
  23833. */
  23834. static int test_wc_PKCS7_InitWithCert(void)
  23835. {
  23836. EXPECT_DECLS;
  23837. #if defined(HAVE_PKCS7)
  23838. PKCS7* pkcs7 = NULL;
  23839. #ifndef NO_RSA
  23840. #if defined(USE_CERT_BUFFERS_2048)
  23841. unsigned char cert[sizeof(client_cert_der_2048)];
  23842. int certSz = (int)sizeof(cert);
  23843. XMEMSET(cert, 0, certSz);
  23844. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23845. #elif defined(USE_CERT_BUFFERS_1024)
  23846. unsigned char cert[sizeof(client_cert_der_1024)];
  23847. int certSz = (int)sizeof(cert);
  23848. XMEMSET(cert, 0, certSz);
  23849. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23850. #else
  23851. unsigned char cert[ONEK_BUF];
  23852. XFILE fp = XBADFILE;
  23853. int certSz;
  23854. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23855. XBADFILE);
  23856. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23857. fp), 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. int certSz = (int)sizeof(cert);
  23865. XMEMSET(cert, 0, certSz);
  23866. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23867. #else
  23868. unsigned char cert[ONEK_BUF];
  23869. XFILE fp = XBADFILE;
  23870. int certSz;
  23871. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23872. XBADFILE);
  23873. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23874. fp), 0);
  23875. if (fp != XBADFILE)
  23876. XFCLOSE(fp);
  23877. #endif
  23878. #else
  23879. #error PKCS7 requires ECC or RSA
  23880. #endif
  23881. #ifdef HAVE_ECC
  23882. {
  23883. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23884. static unsigned char certWithInvalidEccKey[] = {
  23885. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23886. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23887. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23888. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23889. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23890. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23891. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23892. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23893. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23894. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23895. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23896. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23897. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23898. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23899. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23900. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23901. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23902. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23903. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23904. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23905. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23906. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23907. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23908. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23909. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23910. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23911. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23912. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23913. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23914. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23915. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23916. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23917. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23918. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23919. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23920. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23921. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23922. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23923. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23924. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23925. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23926. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23927. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23928. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23929. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23930. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23931. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23932. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23933. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23934. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23935. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23936. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23937. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23938. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23939. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23940. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23941. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23942. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23943. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23944. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23945. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23946. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23947. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23948. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23949. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23950. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23951. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23952. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23953. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23954. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23955. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23956. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23957. 0x08, 0xA8, 0xB4
  23958. };
  23959. #endif
  23960. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23961. /* If initialization is not successful, it's free'd in init func. */
  23962. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23963. 0);
  23964. wc_PKCS7_Free(pkcs7);
  23965. pkcs7 = NULL;
  23966. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23967. /* Valid initialization usage. */
  23968. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23969. /* Pass in bad args. No need free for null checks, free at end.*/
  23970. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23971. BAD_FUNC_ARG);
  23972. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23973. BAD_FUNC_ARG);
  23974. #ifdef HAVE_ECC
  23975. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23976. sizeof(certWithInvalidEccKey)), 0);
  23977. }
  23978. #endif
  23979. wc_PKCS7_Free(pkcs7);
  23980. #endif
  23981. return EXPECT_RESULT();
  23982. } /* END test_wc_PKCS7_InitWithCert */
  23983. /*
  23984. * Testing wc_PKCS7_EncodeData()
  23985. */
  23986. static int test_wc_PKCS7_EncodeData(void)
  23987. {
  23988. EXPECT_DECLS;
  23989. #if defined(HAVE_PKCS7)
  23990. PKCS7* pkcs7 = NULL;
  23991. byte output[FOURK_BUF];
  23992. byte data[] = "My encoded DER cert.";
  23993. #ifndef NO_RSA
  23994. #if defined(USE_CERT_BUFFERS_2048)
  23995. unsigned char cert[sizeof(client_cert_der_2048)];
  23996. unsigned char key[sizeof(client_key_der_2048)];
  23997. int certSz = (int)sizeof(cert);
  23998. int keySz = (int)sizeof(key);
  23999. XMEMSET(cert, 0, certSz);
  24000. XMEMSET(key, 0, keySz);
  24001. XMEMCPY(cert, client_cert_der_2048, certSz);
  24002. XMEMCPY(key, client_key_der_2048, keySz);
  24003. #elif defined(USE_CERT_BUFFERS_1024)
  24004. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  24005. unsigned char key[sizeof_client_key_der_1024];
  24006. int certSz = (int)sizeof(cert);
  24007. int keySz = (int)sizeof(key);
  24008. XMEMSET(cert, 0, certSz);
  24009. XMEMSET(key, 0, keySz);
  24010. XMEMCPY(cert, client_cert_der_1024, certSz);
  24011. XMEMCPY(key, client_key_der_1024, keySz);
  24012. #else
  24013. unsigned char cert[ONEK_BUF];
  24014. unsigned char key[ONEK_BUF];
  24015. XFILE fp = XBADFILE;
  24016. int certSz;
  24017. int keySz;
  24018. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24019. XBADFILE);
  24020. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24021. fp), 0);
  24022. if (fp != XBADFILE) {
  24023. XFCLOSE(fp);
  24024. fp = XBADFILE;
  24025. }
  24026. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24027. XBADFILE);
  24028. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24029. 0);
  24030. if (fp != XBADFILE)
  24031. XFCLOSE(fp);
  24032. #endif
  24033. #elif defined(HAVE_ECC)
  24034. #if defined(USE_CERT_BUFFERS_256)
  24035. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24036. unsigned char key[sizeof(ecc_clikey_der_256)];
  24037. int certSz = (int)sizeof(cert);
  24038. int keySz = (int)sizeof(key);
  24039. XMEMSET(cert, 0, certSz);
  24040. XMEMSET(key, 0, keySz);
  24041. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24042. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24043. #else
  24044. unsigned char cert[ONEK_BUF];
  24045. unsigned char key[ONEK_BUF];
  24046. XFILE fp = XBADFILE;
  24047. int certSz, keySz;
  24048. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24049. XBADFILE);
  24050. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24051. fp), 0);
  24052. if (fp != XBADFILE) {
  24053. XFCLOSE(fp);
  24054. fp = XBADFILE;
  24055. }
  24056. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24057. XBADFILE);
  24058. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24059. 0);
  24060. if (fp != XBADFILE)
  24061. XFCLOSE(fp);
  24062. #endif
  24063. #endif
  24064. XMEMSET(output, 0, sizeof(output));
  24065. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24066. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24067. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  24068. if (pkcs7 != NULL) {
  24069. pkcs7->content = data;
  24070. pkcs7->contentSz = sizeof(data);
  24071. pkcs7->privateKey = key;
  24072. pkcs7->privateKeySz = (word32)keySz;
  24073. }
  24074. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  24075. /* Test bad args. */
  24076. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  24077. BAD_FUNC_ARG);
  24078. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  24079. BAD_FUNC_ARG);
  24080. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  24081. wc_PKCS7_Free(pkcs7);
  24082. #endif
  24083. return EXPECT_RESULT();
  24084. } /* END test_wc_PKCS7_EncodeData */
  24085. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24086. !defined(NO_RSA) && !defined(NO_SHA256)
  24087. /* RSA sign raw digest callback */
  24088. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  24089. byte* out, word32 outSz, byte* privateKey,
  24090. word32 privateKeySz, int devid, int hashOID)
  24091. {
  24092. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  24093. byte digInfoEncoding[] = {
  24094. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  24095. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  24096. 0x00, 0x04, 0x20
  24097. };
  24098. int ret;
  24099. byte digestInfo[ONEK_BUF];
  24100. byte sig[FOURK_BUF];
  24101. word32 digestInfoSz = 0;
  24102. word32 idx = 0;
  24103. RsaKey rsa;
  24104. /* SHA-256 required only for this example callback due to above
  24105. * digInfoEncoding[] */
  24106. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24107. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24108. (hashOID != SHA256h)) {
  24109. return -1;
  24110. }
  24111. /* build DigestInfo */
  24112. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24113. digestInfoSz += sizeof(digInfoEncoding);
  24114. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24115. digestInfoSz += digestSz;
  24116. /* set up RSA key */
  24117. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24118. if (ret != 0) {
  24119. return ret;
  24120. }
  24121. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24122. /* sign DigestInfo */
  24123. if (ret == 0) {
  24124. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24125. &rsa, pkcs7->rng);
  24126. if (ret > 0) {
  24127. if (ret > (int)outSz) {
  24128. /* output buffer too small */
  24129. ret = -1;
  24130. }
  24131. else {
  24132. /* success, ret holds sig size */
  24133. XMEMCPY(out, sig, ret);
  24134. }
  24135. }
  24136. }
  24137. wc_FreeRsaKey(&rsa);
  24138. return ret;
  24139. }
  24140. #endif
  24141. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  24142. typedef struct encodeSignedDataStream {
  24143. byte out[FOURK_BUF*3];
  24144. int idx;
  24145. word32 outIdx;
  24146. } encodeSignedDataStream;
  24147. /* content is 8k of partially created bundle */
  24148. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  24149. {
  24150. int ret = 0;
  24151. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  24152. if (strm->outIdx < pkcs7->contentSz) {
  24153. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  24154. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  24155. *content = strm->out + strm->outIdx;
  24156. strm->outIdx += ret;
  24157. }
  24158. (void)pkcs7;
  24159. return ret;
  24160. }
  24161. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  24162. void* ctx)
  24163. {
  24164. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  24165. XMEMCPY(strm->out + strm->idx, output, outputSz);
  24166. strm->idx += outputSz;
  24167. (void)pkcs7;
  24168. return 0;
  24169. }
  24170. #endif
  24171. /*
  24172. * Testing wc_PKCS7_EncodeSignedData()
  24173. */
  24174. static int test_wc_PKCS7_EncodeSignedData(void)
  24175. {
  24176. EXPECT_DECLS;
  24177. #if defined(HAVE_PKCS7)
  24178. PKCS7* pkcs7 = NULL;
  24179. WC_RNG rng;
  24180. byte output[FOURK_BUF];
  24181. byte badOut[1];
  24182. word32 outputSz = (word32)sizeof(output);
  24183. word32 badOutSz = 0;
  24184. byte data[] = "Test data to encode.";
  24185. #ifndef NO_RSA
  24186. #if defined(USE_CERT_BUFFERS_2048)
  24187. byte key[sizeof(client_key_der_2048)];
  24188. byte cert[sizeof(client_cert_der_2048)];
  24189. word32 keySz = (word32)sizeof(key);
  24190. word32 certSz = (word32)sizeof(cert);
  24191. XMEMSET(key, 0, keySz);
  24192. XMEMSET(cert, 0, certSz);
  24193. XMEMCPY(key, client_key_der_2048, keySz);
  24194. XMEMCPY(cert, client_cert_der_2048, certSz);
  24195. #elif defined(USE_CERT_BUFFERS_1024)
  24196. byte key[sizeof_client_key_der_1024];
  24197. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24198. word32 keySz = (word32)sizeof(key);
  24199. word32 certSz = (word32)sizeof(cert);
  24200. XMEMSET(key, 0, keySz);
  24201. XMEMSET(cert, 0, certSz);
  24202. XMEMCPY(key, client_key_der_1024, keySz);
  24203. XMEMCPY(cert, client_cert_der_1024, certSz);
  24204. #else
  24205. unsigned char cert[ONEK_BUF];
  24206. unsigned char key[ONEK_BUF];
  24207. XFILE fp = XBADFILE;
  24208. int certSz;
  24209. int keySz;
  24210. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24211. XBADFILE);
  24212. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24213. fp), 0);
  24214. if (fp != XBADFILE) {
  24215. XFCLOSE(fp);
  24216. fp = XBADFILE;
  24217. }
  24218. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24219. XBADFILE);
  24220. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24221. 0);
  24222. if (fp != XBADFILE)
  24223. XFCLOSE(fp);
  24224. #endif
  24225. #elif defined(HAVE_ECC)
  24226. #if defined(USE_CERT_BUFFERS_256)
  24227. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24228. unsigned char key[sizeof(ecc_clikey_der_256)];
  24229. int certSz = (int)sizeof(cert);
  24230. int keySz = (int)sizeof(key);
  24231. XMEMSET(cert, 0, certSz);
  24232. XMEMSET(key, 0, keySz);
  24233. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24234. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24235. #else
  24236. unsigned char cert[ONEK_BUF];
  24237. unsigned char key[ONEK_BUF];
  24238. XFILE fp = XBADFILE;
  24239. int certSz;
  24240. int keySz;
  24241. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24242. XBADFILE);
  24243. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  24244. if (fp != XBADFILE) {
  24245. XFCLOSE(fp);
  24246. fp = XBADFILE;
  24247. }
  24248. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24249. XBADFILE);
  24250. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  24251. if (fp != XBADFILE)
  24252. XFCLOSE(fp);
  24253. #endif
  24254. #endif
  24255. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24256. XMEMSET(output, 0, outputSz);
  24257. ExpectIntEQ(wc_InitRng(&rng), 0);
  24258. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24259. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24260. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24261. if (pkcs7 != NULL) {
  24262. pkcs7->content = data;
  24263. pkcs7->contentSz = (word32)sizeof(data);
  24264. pkcs7->privateKey = key;
  24265. pkcs7->privateKeySz = (word32)sizeof(key);
  24266. pkcs7->encryptOID = RSAk;
  24267. #ifdef NO_SHA
  24268. pkcs7->hashOID = SHA256h;
  24269. #else
  24270. pkcs7->hashOID = SHAh;
  24271. #endif
  24272. pkcs7->rng = &rng;
  24273. }
  24274. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24275. wc_PKCS7_Free(pkcs7);
  24276. pkcs7 = NULL;
  24277. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24278. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24279. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24280. #ifdef ASN_BER_TO_DER
  24281. wc_PKCS7_Free(pkcs7);
  24282. /* reinitialize and test setting stream mode */
  24283. {
  24284. int signedSz = 0;
  24285. encodeSignedDataStream strm;
  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 (pkcs7 != NULL) {
  24290. pkcs7->content = data;
  24291. pkcs7->contentSz = (word32)sizeof(data);
  24292. pkcs7->privateKey = key;
  24293. pkcs7->privateKeySz = (word32)sizeof(key);
  24294. pkcs7->encryptOID = RSAk;
  24295. #ifdef NO_SHA
  24296. pkcs7->hashOID = SHA256h;
  24297. #else
  24298. pkcs7->hashOID = SHAh;
  24299. #endif
  24300. pkcs7->rng = &rng;
  24301. }
  24302. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  24303. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  24304. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  24305. BAD_FUNC_ARG);
  24306. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  24307. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  24308. outputSz), 0);
  24309. wc_PKCS7_Free(pkcs7);
  24310. pkcs7 = NULL;
  24311. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24312. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24313. /* use exact signed buffer size since BER encoded */
  24314. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)signedSz), 0);
  24315. wc_PKCS7_Free(pkcs7);
  24316. /* now try with using callbacks for IO */
  24317. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24318. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24319. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24320. if (pkcs7 != NULL) {
  24321. pkcs7->contentSz = FOURK_BUF*2;
  24322. pkcs7->privateKey = key;
  24323. pkcs7->privateKeySz = (word32)sizeof(key);
  24324. pkcs7->encryptOID = RSAk;
  24325. #ifdef NO_SHA
  24326. pkcs7->hashOID = SHA256h;
  24327. #else
  24328. pkcs7->hashOID = SHAh;
  24329. #endif
  24330. pkcs7->rng = &rng;
  24331. }
  24332. XMEMSET(&strm, 0, sizeof(strm));
  24333. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  24334. StreamOutputCB, (void*)&strm), 0);
  24335. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  24336. wc_PKCS7_Free(pkcs7);
  24337. pkcs7 = NULL;
  24338. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24339. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24340. /* use exact signed buffer size since BER encoded */
  24341. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, (word32)signedSz), 0);
  24342. }
  24343. #endif
  24344. #ifndef NO_PKCS7_STREAM
  24345. wc_PKCS7_Free(pkcs7);
  24346. pkcs7 = NULL;
  24347. {
  24348. word32 z;
  24349. int ret;
  24350. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24351. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24352. /* test for streaming mode */
  24353. ret = -1;
  24354. for (z = 0; z < outputSz && ret != 0; z++) {
  24355. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24356. if (ret < 0){
  24357. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24358. }
  24359. }
  24360. ExpectIntEQ(ret, 0);
  24361. ExpectIntNE(pkcs7->contentSz, 0);
  24362. ExpectNotNull(pkcs7->contentDynamic);
  24363. }
  24364. #endif /* !NO_PKCS7_STREAM */
  24365. /* Pass in bad args. */
  24366. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  24367. BAD_FUNC_ARG);
  24368. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24369. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24370. badOutSz), BAD_FUNC_ARG);
  24371. if (pkcs7 != NULL) {
  24372. pkcs7->hashOID = 0; /* bad hashOID */
  24373. }
  24374. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  24375. BAD_FUNC_ARG);
  24376. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24377. !defined(NO_RSA) && !defined(NO_SHA256)
  24378. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24379. * Example callback assumes SHA-256, so only run test if compiled in. */
  24380. wc_PKCS7_Free(pkcs7);
  24381. pkcs7 = NULL;
  24382. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24383. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24384. if (pkcs7 != NULL) {
  24385. pkcs7->content = data;
  24386. pkcs7->contentSz = (word32)sizeof(data);
  24387. pkcs7->privateKey = key;
  24388. pkcs7->privateKeySz = (word32)sizeof(key);
  24389. pkcs7->encryptOID = RSAk;
  24390. pkcs7->hashOID = SHA256h;
  24391. pkcs7->rng = &rng;
  24392. }
  24393. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24394. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24395. #endif
  24396. wc_PKCS7_Free(pkcs7);
  24397. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24398. #endif
  24399. return EXPECT_RESULT();
  24400. } /* END test_wc_PKCS7_EncodeSignedData */
  24401. /*
  24402. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24403. */
  24404. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24405. {
  24406. EXPECT_DECLS;
  24407. #if defined(HAVE_PKCS7)
  24408. int i;
  24409. PKCS7* pkcs7 = NULL;
  24410. WC_RNG rng;
  24411. byte outputHead[FOURK_BUF/2];
  24412. byte outputFoot[FOURK_BUF/2];
  24413. word32 outputHeadSz = (word32)sizeof(outputHead);
  24414. word32 outputFootSz = (word32)sizeof(outputFoot);
  24415. byte data[FOURK_BUF];
  24416. wc_HashAlg hash;
  24417. #ifdef NO_SHA
  24418. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24419. #else
  24420. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24421. #endif
  24422. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24423. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  24424. #ifndef NO_RSA
  24425. #if defined(USE_CERT_BUFFERS_2048)
  24426. byte key[sizeof(client_key_der_2048)];
  24427. byte cert[sizeof(client_cert_der_2048)];
  24428. word32 keySz = (word32)sizeof(key);
  24429. word32 certSz = (word32)sizeof(cert);
  24430. XMEMSET(key, 0, keySz);
  24431. XMEMSET(cert, 0, certSz);
  24432. XMEMCPY(key, client_key_der_2048, keySz);
  24433. XMEMCPY(cert, client_cert_der_2048, certSz);
  24434. #elif defined(USE_CERT_BUFFERS_1024)
  24435. byte key[sizeof_client_key_der_1024];
  24436. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24437. word32 keySz = (word32)sizeof(key);
  24438. word32 certSz = (word32)sizeof(cert);
  24439. XMEMSET(key, 0, keySz);
  24440. XMEMSET(cert, 0, certSz);
  24441. XMEMCPY(key, client_key_der_1024, keySz);
  24442. XMEMCPY(cert, client_cert_der_1024, certSz);
  24443. #else
  24444. unsigned char cert[ONEK_BUF];
  24445. unsigned char key[ONEK_BUF];
  24446. XFILE fp = XBADFILE;
  24447. int certSz;
  24448. int keySz;
  24449. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24450. XBADFILE);
  24451. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24452. fp), 0);
  24453. if (fp != XBADFILE) {
  24454. XFCLOSE(fp);
  24455. fp = XBADFILE;
  24456. }
  24457. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24458. XBADFILE);
  24459. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24460. 0);
  24461. if (fp != XBADFILE)
  24462. XFCLOSE(fp);
  24463. #endif
  24464. #elif defined(HAVE_ECC)
  24465. #if defined(USE_CERT_BUFFERS_256)
  24466. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24467. unsigned char key[sizeof(ecc_clikey_der_256)];
  24468. int certSz = (int)sizeof(cert);
  24469. int keySz = (int)sizeof(key);
  24470. XMEMSET(cert, 0, certSz);
  24471. XMEMSET(key, 0, keySz);
  24472. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24473. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24474. #else
  24475. unsigned char cert[ONEK_BUF];
  24476. unsigned char key[ONEK_BUF];
  24477. XFILE fp = XBADFILE;
  24478. int certSz;
  24479. int keySz;
  24480. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24481. XBADFILE);
  24482. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24483. fp), 0);
  24484. if (fp != XBADFILE) {
  24485. XFCLOSE(fp);
  24486. fp = XBADFILE;
  24487. }
  24488. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24489. XBADFILE);
  24490. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24491. 0);
  24492. if (fp != XBADFILE)
  24493. XFCLOSE(fp);
  24494. #endif
  24495. #endif
  24496. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24497. /* initialize large data with sequence */
  24498. for (i=0; i<(int)sizeof(data); i++)
  24499. data[i] = i & 0xff;
  24500. XMEMSET(outputHead, 0, outputHeadSz);
  24501. XMEMSET(outputFoot, 0, outputFootSz);
  24502. ExpectIntEQ(wc_InitRng(&rng), 0);
  24503. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24504. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24505. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24506. if (pkcs7 != NULL) {
  24507. pkcs7->content = NULL; /* not used for ex */
  24508. pkcs7->contentSz = (word32)sizeof(data);
  24509. pkcs7->privateKey = key;
  24510. pkcs7->privateKeySz = (word32)sizeof(key);
  24511. pkcs7->encryptOID = RSAk;
  24512. #ifdef NO_SHA
  24513. pkcs7->hashOID = SHA256h;
  24514. #else
  24515. pkcs7->hashOID = SHAh;
  24516. #endif
  24517. pkcs7->rng = &rng;
  24518. }
  24519. /* calculate hash for content */
  24520. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24521. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24522. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24523. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24524. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24525. /* Perform PKCS7 sign using hash directly */
  24526. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24527. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24528. ExpectIntGT(outputHeadSz, 0);
  24529. ExpectIntGT(outputFootSz, 0);
  24530. wc_PKCS7_Free(pkcs7);
  24531. pkcs7 = NULL;
  24532. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24533. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24534. /* required parameter even on verify when using _ex, if using outputHead
  24535. * and outputFoot */
  24536. if (pkcs7 != NULL) {
  24537. pkcs7->contentSz = (word32)sizeof(data);
  24538. }
  24539. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24540. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24541. wc_PKCS7_Free(pkcs7);
  24542. pkcs7 = NULL;
  24543. /* assembly complete PKCS7 sign and use normal verify */
  24544. {
  24545. byte* output = NULL;
  24546. word32 outputSz = 0;
  24547. #ifndef NO_PKCS7_STREAM
  24548. word32 z;
  24549. int ret;
  24550. #endif /* !NO_PKCS7_STREAM */
  24551. ExpectNotNull(output = (byte*)XMALLOC(
  24552. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  24553. DYNAMIC_TYPE_TMP_BUFFER));
  24554. if (output != NULL) {
  24555. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24556. outputSz += outputHeadSz;
  24557. XMEMCPY(&output[outputSz], data, sizeof(data));
  24558. outputSz += sizeof(data);
  24559. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24560. outputSz += outputFootSz;
  24561. }
  24562. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24563. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24564. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24565. #ifndef NO_PKCS7_STREAM
  24566. wc_PKCS7_Free(pkcs7);
  24567. pkcs7 = NULL;
  24568. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24569. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24570. /* test for streaming mode */
  24571. ret = -1;
  24572. for (z = 0; z < outputSz && ret != 0; z++) {
  24573. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24574. if (ret < 0){
  24575. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24576. }
  24577. }
  24578. ExpectIntEQ(ret, 0);
  24579. ExpectIntNE(pkcs7->contentSz, 0);
  24580. ExpectNotNull(pkcs7->contentDynamic);
  24581. wc_PKCS7_Free(pkcs7);
  24582. pkcs7 = NULL;
  24583. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24584. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24585. #endif /* !NO_PKCS7_STREAM */
  24586. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24587. }
  24588. /* Pass in bad args. */
  24589. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24590. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24591. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24592. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24593. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24594. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24595. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24596. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24597. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24598. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24599. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24600. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24601. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24602. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24603. if (pkcs7 != NULL) {
  24604. pkcs7->hashOID = 0; /* bad hashOID */
  24605. }
  24606. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24607. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24608. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24609. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24610. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24611. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24612. #ifndef NO_PKCS7_STREAM
  24613. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24614. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24615. #else
  24616. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24617. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24618. #endif
  24619. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24620. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24621. #ifndef NO_PKCS7_STREAM
  24622. /* can pass in 0 buffer length with streaming API */
  24623. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24624. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24625. #else
  24626. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24627. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24628. #endif
  24629. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24630. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24631. #ifndef NO_PKCS7_STREAM
  24632. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24633. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24634. #else
  24635. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24636. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24637. #endif
  24638. wc_PKCS7_Free(pkcs7);
  24639. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24640. #endif
  24641. return EXPECT_RESULT();
  24642. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24643. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24644. /**
  24645. * Loads certs/keys from files or buffers into the argument buffers,
  24646. * helper function called by CreatePKCS7SignedData().
  24647. *
  24648. * Returns 0 on success, negative on error.
  24649. */
  24650. static int LoadPKCS7SignedDataCerts(
  24651. int useIntermediateCertChain, int pkAlgoType,
  24652. byte* intCARoot, word32* intCARootSz,
  24653. byte* intCA1, word32* intCA1Sz,
  24654. byte* intCA2, word32* intCA2Sz,
  24655. byte* cert, word32* certSz,
  24656. byte* key, word32* keySz)
  24657. {
  24658. EXPECT_DECLS;
  24659. int ret = 0;
  24660. XFILE fp = XBADFILE;
  24661. #ifndef NO_RSA
  24662. const char* intCARootRSA = "./certs/ca-cert.der";
  24663. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24664. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24665. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24666. const char* intServKeyRSA = "./certs/server-key.der";
  24667. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24668. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24669. const char* cli1024Key = "./certs/1024/client-key.der";
  24670. #endif
  24671. #endif
  24672. #ifdef HAVE_ECC
  24673. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24674. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24675. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24676. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24677. const char* intServKeyECC = "./certs/ecc-key.der";
  24678. #ifndef USE_CERT_BUFFERS_256
  24679. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24680. const char* cliEccKey = "./certs/client-ecc-key.der";
  24681. #endif
  24682. #endif
  24683. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24684. ((useIntermediateCertChain == 1) &&
  24685. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24686. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24687. return BAD_FUNC_ARG;
  24688. }
  24689. /* Read/load certs and keys to use for signing based on PK type and chain */
  24690. switch (pkAlgoType) {
  24691. #ifndef NO_RSA
  24692. case RSA_TYPE:
  24693. if (useIntermediateCertChain == 1) {
  24694. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24695. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24696. if (fp != XBADFILE) {
  24697. XFCLOSE(fp);
  24698. fp = XBADFILE;
  24699. }
  24700. ExpectIntGT(*intCARootSz, 0);
  24701. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24702. if (fp != XBADFILE) {
  24703. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24704. XFCLOSE(fp);
  24705. fp = XBADFILE;
  24706. }
  24707. ExpectIntGT(*intCA1Sz, 0);
  24708. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24709. if (fp != XBADFILE) {
  24710. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24711. XFCLOSE(fp);
  24712. fp = XBADFILE;
  24713. }
  24714. ExpectIntGT(*intCA2Sz, 0);
  24715. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24716. if (fp != XBADFILE) {
  24717. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24718. XFCLOSE(fp);
  24719. fp = XBADFILE;
  24720. }
  24721. ExpectIntGT(*certSz, 0);
  24722. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24723. if (fp != XBADFILE) {
  24724. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24725. XFCLOSE(fp);
  24726. fp = XBADFILE;
  24727. }
  24728. ExpectIntGT(*keySz, 0);
  24729. }
  24730. else {
  24731. #if defined(USE_CERT_BUFFERS_2048)
  24732. *keySz = sizeof_client_key_der_2048;
  24733. *certSz = sizeof_client_cert_der_2048;
  24734. XMEMCPY(key, client_key_der_2048, *keySz);
  24735. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24736. #elif defined(USE_CERT_BUFFERS_1024)
  24737. *keySz = sizeof_client_key_der_1024;
  24738. *certSz = sizeof_client_cert_der_1024;
  24739. XMEMCPY(key, client_key_der_1024, *keySz);
  24740. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24741. #else
  24742. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24743. if (fp != XBADFILE) {
  24744. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24745. XFCLOSE(fp);
  24746. fp = XBADFILE;
  24747. }
  24748. ExpectIntGT(*keySz, 0);
  24749. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24750. if (fp != XBADFILE) {
  24751. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24752. XFCLOSE(fp);
  24753. fp = XBADFILE;
  24754. }
  24755. ExpectIntGT(*certSz, 0);
  24756. #endif /* USE_CERT_BUFFERS_2048 */
  24757. }
  24758. break;
  24759. #endif /* !NO_RSA */
  24760. #ifdef HAVE_ECC
  24761. case ECC_TYPE:
  24762. if (useIntermediateCertChain == 1) {
  24763. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24764. if (fp != XBADFILE) {
  24765. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24766. fp);
  24767. XFCLOSE(fp);
  24768. fp = XBADFILE;
  24769. }
  24770. ExpectIntGT(*intCARootSz, 0);
  24771. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24772. if (fp != XBADFILE) {
  24773. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24774. XFCLOSE(fp);
  24775. fp = XBADFILE;
  24776. }
  24777. ExpectIntGT(*intCA1Sz, 0);
  24778. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24779. if (fp != XBADFILE) {
  24780. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24781. XFCLOSE(fp);
  24782. fp = XBADFILE;
  24783. }
  24784. ExpectIntGT(*intCA2Sz, 0);
  24785. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24786. if (fp != XBADFILE) {
  24787. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24788. XFCLOSE(fp);
  24789. fp = XBADFILE;
  24790. }
  24791. ExpectIntGT(*certSz, 0);
  24792. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24793. if (fp != XBADFILE) {
  24794. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24795. XFCLOSE(fp);
  24796. fp = XBADFILE;
  24797. }
  24798. ExpectIntGT(*keySz, 0);
  24799. }
  24800. else {
  24801. #if defined(USE_CERT_BUFFERS_256)
  24802. *keySz = sizeof_ecc_clikey_der_256;
  24803. *certSz = sizeof_cliecc_cert_der_256;
  24804. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24805. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24806. #else
  24807. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24808. if (fp != XBADFILE) {
  24809. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24810. XFCLOSE(fp);
  24811. fp = XBADFILE;
  24812. }
  24813. ExpectIntGT(*keySz, 0);
  24814. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24815. if (fp != XBADFILE) {
  24816. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24817. XFCLOSE(fp);
  24818. fp = XBADFILE;
  24819. }
  24820. ExpectIntGT(*certSz, 0);
  24821. #endif /* USE_CERT_BUFFERS_256 */
  24822. }
  24823. break;
  24824. #endif /* HAVE_ECC */
  24825. default:
  24826. WOLFSSL_MSG("Unsupported SignedData PK type");
  24827. ret = BAD_FUNC_ARG;
  24828. break;
  24829. }
  24830. if (EXPECT_FAIL() && (ret == 0)) {
  24831. ret = BAD_FUNC_ARG;
  24832. }
  24833. return ret;
  24834. }
  24835. /**
  24836. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24837. *
  24838. * output output buffer to place SignedData
  24839. * outputSz size of output buffer
  24840. * data data buffer to be signed
  24841. * dataSz size of data buffer
  24842. * withAttribs [1/0] include attributes in SignedData message
  24843. * detachedSig [1/0] create detached signature, no content
  24844. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24845. * root CAs in bundle
  24846. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24847. *
  24848. * Return size of bundle created on success, negative on error */
  24849. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24850. byte* data, word32 dataSz,
  24851. int withAttribs, int detachedSig,
  24852. int useIntermediateCertChain,
  24853. int pkAlgoType)
  24854. {
  24855. EXPECT_DECLS;
  24856. int ret = 0;
  24857. WC_RNG rng;
  24858. PKCS7* pkcs7 = NULL;
  24859. static byte messageTypeOid[] =
  24860. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24861. 0x09, 0x02 };
  24862. static byte messageType[] = { 0x13, 2, '1', '9' };
  24863. PKCS7Attrib attribs[] =
  24864. {
  24865. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24866. sizeof(messageType) }
  24867. };
  24868. byte intCARoot[TWOK_BUF];
  24869. byte intCA1[TWOK_BUF];
  24870. byte intCA2[TWOK_BUF];
  24871. byte cert[TWOK_BUF];
  24872. byte key[TWOK_BUF];
  24873. word32 intCARootSz = sizeof(intCARoot);
  24874. word32 intCA1Sz = sizeof(intCA1);
  24875. word32 intCA2Sz = sizeof(intCA2);
  24876. word32 certSz = sizeof(cert);
  24877. word32 keySz = sizeof(key);
  24878. XMEMSET(intCARoot, 0, intCARootSz);
  24879. XMEMSET(intCA1, 0, intCA1Sz);
  24880. XMEMSET(intCA2, 0, intCA2Sz);
  24881. XMEMSET(cert, 0, certSz);
  24882. XMEMSET(key, 0, keySz);
  24883. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24884. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24885. cert, &certSz, key, &keySz);
  24886. ExpectIntEQ(ret, 0);
  24887. XMEMSET(output, 0, outputSz);
  24888. ExpectIntEQ(wc_InitRng(&rng), 0);
  24889. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24890. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24891. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24892. if (useIntermediateCertChain == 1) {
  24893. /* Add intermediate and root CA certs into SignedData Certs SET */
  24894. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24895. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24896. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24897. }
  24898. if (pkcs7 != NULL) {
  24899. pkcs7->content = data;
  24900. pkcs7->contentSz = dataSz;
  24901. pkcs7->privateKey = key;
  24902. pkcs7->privateKeySz = (word32)sizeof(key);
  24903. if (pkAlgoType == RSA_TYPE) {
  24904. pkcs7->encryptOID = RSAk;
  24905. }
  24906. else {
  24907. pkcs7->encryptOID = ECDSAk;
  24908. }
  24909. #ifdef NO_SHA
  24910. pkcs7->hashOID = SHA256h;
  24911. #else
  24912. pkcs7->hashOID = SHAh;
  24913. #endif
  24914. pkcs7->rng = &rng;
  24915. if (withAttribs) {
  24916. /* include a signed attribute */
  24917. pkcs7->signedAttribs = attribs;
  24918. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24919. }
  24920. }
  24921. if (detachedSig) {
  24922. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24923. }
  24924. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, (word32)outputSz);
  24925. ExpectIntGT(outputSz, 0);
  24926. wc_PKCS7_Free(pkcs7);
  24927. pkcs7 = NULL;
  24928. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24929. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24930. if (detachedSig && (pkcs7 != NULL)) {
  24931. pkcs7->content = data;
  24932. pkcs7->contentSz = dataSz;
  24933. }
  24934. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)outputSz), 0);
  24935. wc_PKCS7_Free(pkcs7);
  24936. wc_FreeRng(&rng);
  24937. if (EXPECT_FAIL()) {
  24938. outputSz = 0;
  24939. }
  24940. return outputSz;
  24941. }
  24942. #endif
  24943. /*
  24944. * Testing wc_PKCS_VerifySignedData()
  24945. */
  24946. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24947. {
  24948. EXPECT_DECLS;
  24949. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24950. PKCS7* pkcs7 = NULL;
  24951. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24952. word32 outputSz = sizeof(output);
  24953. byte data[] = "Test data to encode.";
  24954. byte badOut[1];
  24955. word32 badOutSz = 0;
  24956. byte badContent[] = "This is different content than was signed";
  24957. wc_HashAlg hash;
  24958. #ifdef NO_SHA
  24959. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24960. #else
  24961. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24962. #endif
  24963. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24964. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  24965. #ifndef NO_RSA
  24966. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24967. /* contentType OID (1.2.840.113549.1.9.3) */
  24968. static const byte contentTypeOid[] =
  24969. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24970. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24971. static const byte dataType[] =
  24972. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24973. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24974. static const byte messageDigestOid[] =
  24975. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24976. #ifndef NO_ASN_TIME
  24977. /* signingTime OID () */
  24978. static const byte signingTimeOid[] =
  24979. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24980. #endif
  24981. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24982. int dateLength = 0;
  24983. byte dateFormat;
  24984. const byte* datePart = NULL;
  24985. struct tm timearg;
  24986. time_t now;
  24987. struct tm* nowTm = NULL;
  24988. #ifdef NEED_TMP_TIME
  24989. struct tm tmpTimeStorage;
  24990. struct tm* tmpTime = &tmpTimeStorage;
  24991. #endif
  24992. #endif /* !NO_ASN && !NO_ASN_TIME */
  24993. #ifndef NO_PKCS7_STREAM
  24994. word32 z;
  24995. int ret;
  24996. #endif /* !NO_PKCS7_STREAM */
  24997. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24998. /* Success test with RSA certs/key */
  24999. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25000. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  25001. /* calculate hash for content, used later */
  25002. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25003. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25004. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25005. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25006. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25007. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25008. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25009. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25010. #ifndef NO_PKCS7_STREAM
  25011. wc_PKCS7_Free(pkcs7);
  25012. pkcs7 = NULL;
  25013. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25014. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25015. /* test for streaming */
  25016. ret = -1;
  25017. for (z = 0; z < outputSz && ret != 0; z++) {
  25018. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25019. if (ret < 0){
  25020. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25021. }
  25022. }
  25023. ExpectIntEQ(ret, 0);
  25024. ExpectIntNE(pkcs7->contentSz, 0);
  25025. ExpectNotNull(pkcs7->contentDynamic);
  25026. #endif /* !NO_PKCS7_STREAM */
  25027. /* Check that decoded signed attributes are correct */
  25028. /* messageDigest should be first */
  25029. if (pkcs7 != NULL) {
  25030. decodedAttrib = pkcs7->decodedAttrib;
  25031. }
  25032. ExpectNotNull(decodedAttrib);
  25033. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  25034. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  25035. decodedAttrib->oidSz), 0);
  25036. /* + 2 for OCTET STRING and length bytes */
  25037. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  25038. ExpectNotNull(decodedAttrib->value);
  25039. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  25040. #ifndef NO_ASN_TIME
  25041. /* signingTime should be second */
  25042. if (decodedAttrib != NULL) {
  25043. decodedAttrib = decodedAttrib->next;
  25044. }
  25045. ExpectNotNull(decodedAttrib);
  25046. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  25047. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  25048. decodedAttrib->oidSz), 0);
  25049. ExpectIntGT(decodedAttrib->valueSz, 0);
  25050. ExpectNotNull(decodedAttrib->value);
  25051. #endif
  25052. /* Verify signingTime if ASN and time are available */
  25053. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  25054. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  25055. &datePart, &dateFormat, &dateLength), 0);
  25056. ExpectNotNull(datePart);
  25057. ExpectIntGT(dateLength, 0);
  25058. XMEMSET(&timearg, 0, sizeof(timearg));
  25059. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  25060. &timearg), 0);
  25061. /* Get current time and compare year/month/day against attribute value */
  25062. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  25063. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  25064. ExpectNotNull(nowTm);
  25065. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  25066. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  25067. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  25068. #endif /* !NO_ASN && !NO_ASN_TIME */
  25069. /* contentType should be third */
  25070. if (decodedAttrib != NULL) {
  25071. decodedAttrib = decodedAttrib->next;
  25072. }
  25073. ExpectNotNull(decodedAttrib);
  25074. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  25075. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  25076. decodedAttrib->oidSz), 0);
  25077. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  25078. ExpectNotNull(decodedAttrib->value);
  25079. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  25080. 0);
  25081. #endif /* !NO_RSA */
  25082. /* Test bad args. */
  25083. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  25084. BAD_FUNC_ARG);
  25085. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  25086. BAD_FUNC_ARG);
  25087. #ifndef NO_PKCS7_STREAM
  25088. /* can pass in 0 buffer length with streaming API */
  25089. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25090. badOutSz), WC_PKCS7_WANT_READ_E);
  25091. #else
  25092. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25093. badOutSz), BAD_FUNC_ARG);
  25094. #endif
  25095. wc_PKCS7_Free(pkcs7);
  25096. pkcs7 = NULL;
  25097. #ifndef NO_RSA
  25098. /* Try RSA certs/key/sig first */
  25099. outputSz = sizeof(output);
  25100. XMEMSET(output, 0, outputSz);
  25101. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25102. (word32)sizeof(data),
  25103. 1, 1, 0, RSA_TYPE)), 0);
  25104. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25105. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25106. if (pkcs7 != NULL) {
  25107. pkcs7->content = badContent;
  25108. pkcs7->contentSz = sizeof(badContent);
  25109. }
  25110. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25111. SIG_VERIFY_E);
  25112. wc_PKCS7_Free(pkcs7);
  25113. pkcs7 = NULL;
  25114. #ifndef NO_PKCS7_STREAM
  25115. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25116. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25117. if (pkcs7 != NULL) {
  25118. pkcs7->content = badContent;
  25119. pkcs7->contentSz = sizeof(badContent);
  25120. }
  25121. /* test for streaming */
  25122. ret = -1;
  25123. for (z = 0; z < outputSz && ret != 0; z++) {
  25124. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25125. if (ret == WC_PKCS7_WANT_READ_E){
  25126. continue;
  25127. }
  25128. else if (ret < 0) {
  25129. break;
  25130. }
  25131. }
  25132. ExpectIntEQ(ret, SIG_VERIFY_E);
  25133. ExpectIntNE(pkcs7->contentSz, 0);
  25134. ExpectNotNull(pkcs7->contentDynamic);
  25135. wc_PKCS7_Free(pkcs7);
  25136. pkcs7 = NULL;
  25137. #endif /* !NO_PKCS7_STREAM */
  25138. /* Test success case with detached signature and valid content */
  25139. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25140. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25141. if (pkcs7 != NULL) {
  25142. pkcs7->content = data;
  25143. pkcs7->contentSz = sizeof(data);
  25144. }
  25145. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25146. wc_PKCS7_Free(pkcs7);
  25147. pkcs7 = NULL;
  25148. #ifndef NO_PKCS7_STREAM
  25149. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25150. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25151. if (pkcs7 != NULL) {
  25152. pkcs7->content = data;
  25153. pkcs7->contentSz = sizeof(data);
  25154. }
  25155. /* test for streaming */
  25156. ret = -1;
  25157. for (z = 0; z < outputSz && ret != 0; z++) {
  25158. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25159. if (ret < 0){
  25160. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25161. }
  25162. }
  25163. ExpectIntEQ(ret, 0);
  25164. ExpectIntNE(pkcs7->contentSz, 0);
  25165. ExpectNotNull(pkcs7->contentDynamic);
  25166. wc_PKCS7_Free(pkcs7);
  25167. pkcs7 = NULL;
  25168. #endif /* !NO_PKCS7_STREAM */
  25169. /* verify using pre-computed content digest only (no content) */
  25170. {
  25171. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25172. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25173. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25174. output, outputSz, NULL, 0), 0);
  25175. wc_PKCS7_Free(pkcs7);
  25176. pkcs7 = NULL;
  25177. }
  25178. #endif /* !NO_RSA */
  25179. /* Test verify on signedData containing intermediate/root CA certs */
  25180. #ifndef NO_RSA
  25181. outputSz = sizeof(output);
  25182. XMEMSET(output, 0, outputSz);
  25183. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25184. (word32)sizeof(data),
  25185. 0, 0, 1, RSA_TYPE)), 0);
  25186. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25187. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25188. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25189. wc_PKCS7_Free(pkcs7);
  25190. pkcs7 = NULL;
  25191. #ifndef NO_PKCS7_STREAM
  25192. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25193. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25194. /* test for streaming */
  25195. ret = -1;
  25196. for (z = 0; z < outputSz && ret != 0; z++) {
  25197. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25198. if (ret < 0){
  25199. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25200. }
  25201. }
  25202. ExpectIntEQ(ret, 0);
  25203. ExpectIntNE(pkcs7->contentSz, 0);
  25204. ExpectNotNull(pkcs7->contentDynamic);
  25205. wc_PKCS7_Free(pkcs7);
  25206. pkcs7 = NULL;
  25207. #endif /* !NO_PKCS7_STREAM */
  25208. #endif /* !NO_RSA */
  25209. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  25210. !defined(NO_FILESYSTEM)
  25211. {
  25212. XFILE signedBundle = XBADFILE;
  25213. int signedBundleSz = 0;
  25214. int chunkSz = 1;
  25215. int i, rc = 0;
  25216. byte* buf = NULL;
  25217. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  25218. "rb")) != XBADFILE);
  25219. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  25220. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  25221. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  25222. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  25223. DYNAMIC_TYPE_FILE));
  25224. if (buf != NULL) {
  25225. ExpectIntEQ(XFREAD(buf, 1, (size_t)signedBundleSz, signedBundle),
  25226. signedBundleSz);
  25227. }
  25228. if (signedBundle != XBADFILE) {
  25229. XFCLOSE(signedBundle);
  25230. signedBundle = XBADFILE;
  25231. }
  25232. if (buf != NULL) {
  25233. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25234. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25235. for (i = 0; i < signedBundleSz;) {
  25236. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25237. chunkSz;
  25238. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  25239. if (rc < 0 ) {
  25240. if (rc == WC_PKCS7_WANT_READ_E) {
  25241. i += sz;
  25242. continue;
  25243. }
  25244. break;
  25245. }
  25246. else {
  25247. break;
  25248. }
  25249. }
  25250. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  25251. wc_PKCS7_Free(pkcs7);
  25252. pkcs7 = NULL;
  25253. }
  25254. /* now try with malformed bundle */
  25255. if (buf != NULL) {
  25256. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25257. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25258. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  25259. for (i = 0; i < signedBundleSz;) {
  25260. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25261. chunkSz;
  25262. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  25263. if (rc < 0 ) {
  25264. if (rc == WC_PKCS7_WANT_READ_E) {
  25265. i += sz;
  25266. continue;
  25267. }
  25268. break;
  25269. }
  25270. else {
  25271. break;
  25272. }
  25273. }
  25274. ExpectIntEQ(rc, ASN_PARSE_E);
  25275. wc_PKCS7_Free(pkcs7);
  25276. pkcs7 = NULL;
  25277. }
  25278. if (buf != NULL)
  25279. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25280. }
  25281. #endif /* BER and stream */
  25282. #endif
  25283. return EXPECT_RESULT();
  25284. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  25285. /*
  25286. * Testing wc_PKCS_VerifySignedData()
  25287. */
  25288. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  25289. {
  25290. EXPECT_DECLS;
  25291. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  25292. PKCS7* pkcs7 = NULL;
  25293. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25294. word32 outputSz = sizeof(output);
  25295. byte data[] = "Test data to encode.";
  25296. byte badContent[] = "This is different content than was signed";
  25297. wc_HashAlg hash;
  25298. #ifndef NO_PKCS7_STREAM
  25299. word32 z;
  25300. int ret;
  25301. #endif /* !NO_PKCS7_STREAM */
  25302. #ifdef NO_SHA
  25303. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25304. #else
  25305. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25306. #endif
  25307. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25308. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  25309. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25310. /* Success test with ECC certs/key */
  25311. outputSz = sizeof(output);
  25312. XMEMSET(output, 0, outputSz);
  25313. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25314. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  25315. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25316. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25317. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25318. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25319. wc_PKCS7_Free(pkcs7);
  25320. pkcs7 = NULL;
  25321. #ifndef NO_PKCS7_STREAM
  25322. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25323. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25324. /* test for streaming */
  25325. ret = -1;
  25326. for (z = 0; z < outputSz && ret != 0; z++) {
  25327. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25328. if (ret < 0){
  25329. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25330. }
  25331. }
  25332. ExpectIntEQ(ret, 0);
  25333. ExpectIntNE(pkcs7->contentSz, 0);
  25334. ExpectNotNull(pkcs7->contentDynamic);
  25335. wc_PKCS7_Free(pkcs7);
  25336. pkcs7 = NULL;
  25337. #endif /* !NO_PKCS7_STREAM */
  25338. /* Invalid content should error, use detached signature so we can
  25339. * easily change content */
  25340. outputSz = sizeof(output);
  25341. XMEMSET(output, 0, outputSz);
  25342. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25343. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  25344. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25345. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25346. if (pkcs7 != NULL) {
  25347. pkcs7->content = badContent;
  25348. pkcs7->contentSz = sizeof(badContent);
  25349. }
  25350. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25351. SIG_VERIFY_E);
  25352. wc_PKCS7_Free(pkcs7);
  25353. pkcs7 = NULL;
  25354. #ifndef NO_PKCS7_STREAM
  25355. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25356. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25357. if (pkcs7 != NULL) {
  25358. pkcs7->content = badContent;
  25359. pkcs7->contentSz = sizeof(badContent);
  25360. }
  25361. /* test for streaming */
  25362. ret = -1;
  25363. for (z = 0; z < outputSz && ret != 0; z++) {
  25364. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25365. if (ret == WC_PKCS7_WANT_READ_E){
  25366. continue;
  25367. }
  25368. else if (ret < 0) {
  25369. break;
  25370. }
  25371. }
  25372. ExpectIntEQ(ret, SIG_VERIFY_E);
  25373. ExpectIntNE(pkcs7->contentSz, 0);
  25374. ExpectNotNull(pkcs7->contentDynamic);
  25375. wc_PKCS7_Free(pkcs7);
  25376. pkcs7 = NULL;
  25377. #endif /* !NO_PKCS7_STREAM */
  25378. /* Test success case with detached signature and valid content */
  25379. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25380. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25381. if (pkcs7 != NULL) {
  25382. pkcs7->content = data;
  25383. pkcs7->contentSz = sizeof(data);
  25384. }
  25385. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25386. wc_PKCS7_Free(pkcs7);
  25387. pkcs7 = NULL;
  25388. #ifndef NO_PKCS7_STREAM
  25389. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25390. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25391. if (pkcs7 != NULL) {
  25392. pkcs7->content = data;
  25393. pkcs7->contentSz = sizeof(data);
  25394. }
  25395. /* test for streaming */
  25396. ret = -1;
  25397. for (z = 0; z < outputSz && ret != 0; z++) {
  25398. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25399. if (ret < 0){
  25400. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25401. }
  25402. }
  25403. ExpectIntEQ(ret, 0);
  25404. ExpectIntNE(pkcs7->contentSz, 0);
  25405. ExpectNotNull(pkcs7->contentDynamic);
  25406. wc_PKCS7_Free(pkcs7);
  25407. pkcs7 = NULL;
  25408. #endif /* !NO_PKCS7_STREAM */
  25409. /* verify using pre-computed content digest only (no content) */
  25410. {
  25411. /* calculate hash for content */
  25412. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25413. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25414. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25415. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25416. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25417. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25418. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25419. output, outputSz, NULL, 0), 0);
  25420. wc_PKCS7_Free(pkcs7);
  25421. pkcs7 = NULL;
  25422. }
  25423. /* Test verify on signedData containing intermediate/root CA certs */
  25424. outputSz = sizeof(output);
  25425. XMEMSET(output, 0, outputSz);
  25426. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25427. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  25428. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25429. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25430. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25431. wc_PKCS7_Free(pkcs7);
  25432. pkcs7 = NULL;
  25433. #ifndef NO_PKCS7_STREAM
  25434. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25435. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25436. /* test for streaming */
  25437. ret = -1;
  25438. for (z = 0; z < outputSz && ret != 0; z++) {
  25439. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25440. if (ret < 0){
  25441. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25442. }
  25443. }
  25444. ExpectIntEQ(ret, 0);
  25445. ExpectIntNE(pkcs7->contentSz, 0);
  25446. ExpectNotNull(pkcs7->contentDynamic);
  25447. wc_PKCS7_Free(pkcs7);
  25448. pkcs7 = NULL;
  25449. #endif /* !NO_PKCS7_STREAM */
  25450. #endif
  25451. return EXPECT_RESULT();
  25452. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  25453. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25454. !defined(NO_AES_256)
  25455. static const byte defKey[] = {
  25456. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25457. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25458. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25459. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25460. };
  25461. static byte aesHandle[32]; /* simulated hardware key handle */
  25462. /* return 0 on success */
  25463. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25464. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25465. byte* in, int inSz, byte* out, void* usrCtx)
  25466. {
  25467. int ret;
  25468. Aes aes;
  25469. if (usrCtx == NULL) {
  25470. /* no simulated handle passed in */
  25471. return -1;
  25472. }
  25473. switch (encryptOID) {
  25474. case AES256CBCb:
  25475. if (ivSz != AES_BLOCK_SIZE)
  25476. return BAD_FUNC_ARG;
  25477. break;
  25478. default:
  25479. WOLFSSL_MSG("Unsupported content cipher type for test");
  25480. return ALGO_ID_E;
  25481. };
  25482. /* simulate using handle to get key */
  25483. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25484. if (ret == 0) {
  25485. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25486. if (ret == 0)
  25487. ret = wc_AesCbcDecrypt(&aes, out, in, (word32)inSz);
  25488. wc_AesFree(&aes);
  25489. }
  25490. (void)aad;
  25491. (void)aadSz;
  25492. (void)authTag;
  25493. (void)authTagSz;
  25494. (void)pkcs7;
  25495. return ret;
  25496. }
  25497. /* returns key size on success */
  25498. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25499. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25500. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25501. {
  25502. int ret = -1;
  25503. (void)cekSz;
  25504. (void)cek;
  25505. (void)outSz;
  25506. (void)keyIdSz;
  25507. (void)direction;
  25508. (void)orginKey; /* used with KAKRI */
  25509. (void)orginKeySz;
  25510. if (out == NULL)
  25511. return BAD_FUNC_ARG;
  25512. if (keyId[0] != 0x00) {
  25513. return -1;
  25514. }
  25515. if (type != (int)PKCS7_KEKRI) {
  25516. return -1;
  25517. }
  25518. switch (keyWrapAlgo) {
  25519. case AES256_WRAP:
  25520. /* simulate setting a handle for later decryption but use key
  25521. * as handle in the test case here */
  25522. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25523. aesHandle, sizeof(aesHandle), NULL);
  25524. if (ret < 0)
  25525. return ret;
  25526. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25527. if (ret < 0)
  25528. return ret;
  25529. /* return key size on success */
  25530. return sizeof(defKey);
  25531. default:
  25532. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25533. return BAD_KEYWRAP_ALG_E;
  25534. };
  25535. }
  25536. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25537. /*
  25538. * Testing wc_PKCS7_EncodeEnvelopedData()
  25539. */
  25540. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25541. {
  25542. EXPECT_DECLS;
  25543. #if defined(HAVE_PKCS7)
  25544. PKCS7* pkcs7 = NULL;
  25545. #ifdef ASN_BER_TO_DER
  25546. int encodedSz = 0;
  25547. #endif
  25548. #ifdef ECC_TIMING_RESISTANT
  25549. WC_RNG rng;
  25550. #endif
  25551. word32 tempWrd32 = 0;
  25552. byte* tmpBytePtr = NULL;
  25553. const char input[] = "Test data to encode.";
  25554. int i;
  25555. int testSz = 0;
  25556. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25557. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25558. byte* rsaCert = NULL;
  25559. byte* rsaPrivKey = NULL;
  25560. word32 rsaCertSz;
  25561. word32 rsaPrivKeySz;
  25562. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25563. !defined(USE_CERT_BUFFERS_2048) )
  25564. static const char* rsaClientCert = "./certs/client-cert.der";
  25565. static const char* rsaClientKey = "./certs/client-key.der";
  25566. rsaCertSz = (word32)sizeof(rsaClientCert);
  25567. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25568. #endif
  25569. #endif
  25570. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25571. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25572. byte* eccCert = NULL;
  25573. byte* eccPrivKey = NULL;
  25574. word32 eccCertSz;
  25575. word32 eccPrivKeySz;
  25576. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25577. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25578. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25579. #endif
  25580. #endif
  25581. /* Generic buffer size. */
  25582. byte output[ONEK_BUF];
  25583. byte decoded[sizeof(input)/sizeof(char)];
  25584. int decodedSz = 0;
  25585. #ifndef NO_FILESYSTEM
  25586. XFILE certFile = XBADFILE;
  25587. XFILE keyFile = XBADFILE;
  25588. #endif
  25589. #ifdef ECC_TIMING_RESISTANT
  25590. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25591. #endif
  25592. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25593. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25594. /* RSA certs and keys. */
  25595. #if defined(USE_CERT_BUFFERS_1024)
  25596. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25597. /* Allocate buffer space. */
  25598. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25599. DYNAMIC_TYPE_TMP_BUFFER));
  25600. /* Init buffer. */
  25601. if (rsaCert != NULL) {
  25602. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25603. }
  25604. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25605. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25606. DYNAMIC_TYPE_TMP_BUFFER));
  25607. if (rsaPrivKey != NULL) {
  25608. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25609. }
  25610. #elif defined(USE_CERT_BUFFERS_2048)
  25611. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25612. /* Allocate buffer */
  25613. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25614. DYNAMIC_TYPE_TMP_BUFFER));
  25615. /* Init buffer. */
  25616. if (rsaCert != NULL) {
  25617. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25618. }
  25619. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25620. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25621. DYNAMIC_TYPE_TMP_BUFFER));
  25622. if (rsaPrivKey != NULL) {
  25623. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25624. }
  25625. #else
  25626. /* File system. */
  25627. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  25628. rsaCertSz = (word32)FOURK_BUF;
  25629. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25630. DYNAMIC_TYPE_TMP_BUFFER));
  25631. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  25632. certFile)) > 0);
  25633. if (certFile != XBADFILE)
  25634. XFCLOSE(certFile);
  25635. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25636. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25637. DYNAMIC_TYPE_TMP_BUFFER));
  25638. rsaPrivKeySz = (word32)FOURK_BUF;
  25639. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25640. keyFile)) > 0);
  25641. if (keyFile != XBADFILE)
  25642. XFCLOSE(keyFile);
  25643. #endif /* USE_CERT_BUFFERS */
  25644. #endif /* NO_RSA */
  25645. /* ECC */
  25646. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25647. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25648. #ifdef USE_CERT_BUFFERS_256
  25649. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25650. DYNAMIC_TYPE_TMP_BUFFER));
  25651. /* Init buffer. */
  25652. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25653. if (eccCert != NULL) {
  25654. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25655. }
  25656. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25657. DYNAMIC_TYPE_TMP_BUFFER));
  25658. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25659. if (eccPrivKey != NULL) {
  25660. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25661. }
  25662. #else /* File system. */
  25663. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25664. eccCertSz = (word32)FOURK_BUF;
  25665. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25666. DYNAMIC_TYPE_TMP_BUFFER));
  25667. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25668. certFile)) > 0);
  25669. if (certFile != XBADFILE) {
  25670. XFCLOSE(certFile);
  25671. }
  25672. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25673. eccPrivKeySz = (word32)FOURK_BUF;
  25674. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25675. DYNAMIC_TYPE_TMP_BUFFER));
  25676. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25677. keyFile)) > 0);
  25678. if (keyFile != XBADFILE) {
  25679. XFCLOSE(keyFile);
  25680. }
  25681. #endif /* USE_CERT_BUFFERS_256 */
  25682. #endif /* END HAVE_ECC */
  25683. #ifndef NO_FILESYSTEM
  25684. /* Silence. */
  25685. (void)keyFile;
  25686. (void)certFile;
  25687. #endif
  25688. {
  25689. const pkcs7EnvelopedVector testVectors[] = {
  25690. /* DATA is a global variable defined in the makefile. */
  25691. #if !defined(NO_RSA)
  25692. #ifndef NO_DES3
  25693. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25694. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25695. #endif /* NO_DES3 */
  25696. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25697. #ifndef NO_AES_128
  25698. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25699. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25700. #endif
  25701. #ifndef NO_AES_192
  25702. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25703. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25704. #endif
  25705. #ifndef NO_AES_256
  25706. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25707. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25708. #endif
  25709. #endif /* NO_AES && HAVE_AES_CBC */
  25710. #endif /* NO_RSA */
  25711. #if defined(HAVE_ECC)
  25712. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25713. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25714. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25715. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25716. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25717. #endif
  25718. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25719. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25720. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25721. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25722. #endif
  25723. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25724. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25725. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25726. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25727. #endif
  25728. #endif /* NO_AES && HAVE_AES_CBC*/
  25729. #endif /* END HAVE_ECC */
  25730. }; /* END pkcs7EnvelopedVector */
  25731. #ifdef ECC_TIMING_RESISTANT
  25732. ExpectIntEQ(wc_InitRng(&rng), 0);
  25733. #endif
  25734. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25735. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25736. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25737. for (i = 0; i < testSz; i++) {
  25738. #ifdef ASN_BER_TO_DER
  25739. encodeSignedDataStream strm;
  25740. /* test setting stream mode, the first one using IO callbacks */
  25741. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25742. (word32)(testVectors + i)->certSz), 0);
  25743. if (pkcs7 != NULL) {
  25744. #ifdef ECC_TIMING_RESISTANT
  25745. pkcs7->rng = &rng;
  25746. #endif
  25747. if (i != 0)
  25748. pkcs7->content = (byte*)(testVectors + i)->content;
  25749. pkcs7->contentSz = (testVectors + i)->contentSz;
  25750. pkcs7->contentOID = (testVectors + i)->contentOID;
  25751. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25752. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25753. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25754. pkcs7->privateKey = (testVectors + i)->privateKey;
  25755. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25756. }
  25757. if (i == 0) {
  25758. XMEMSET(&strm, 0, sizeof(strm));
  25759. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25760. StreamOutputCB, (void*)&strm), 0);
  25761. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25762. }
  25763. else {
  25764. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25765. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25766. (word32)sizeof(output));
  25767. }
  25768. switch ((testVectors + i)->encryptOID) {
  25769. #ifndef NO_DES3
  25770. case DES3b:
  25771. case DESb:
  25772. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25773. break;
  25774. #endif
  25775. #ifdef HAVE_AESCCM
  25776. #ifdef WOLFSSL_AES_128
  25777. case AES128CCMb:
  25778. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25779. break;
  25780. #endif
  25781. #ifdef WOLFSSL_AES_192
  25782. case AES192CCMb:
  25783. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25784. break;
  25785. #endif
  25786. #ifdef WOLFSSL_AES_256
  25787. case AES256CCMb:
  25788. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25789. break;
  25790. #endif
  25791. #endif
  25792. default:
  25793. ExpectIntGE(encodedSz, 0);
  25794. }
  25795. if (encodedSz > 0) {
  25796. if (i == 0) {
  25797. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25798. strm.out, (word32)encodedSz, decoded,
  25799. (word32)sizeof(decoded));
  25800. }
  25801. else {
  25802. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25803. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25804. }
  25805. ExpectIntGE(decodedSz, 0);
  25806. /* Verify the size of each buffer. */
  25807. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25808. }
  25809. wc_PKCS7_Free(pkcs7);
  25810. pkcs7 = NULL;
  25811. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25812. #endif
  25813. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25814. (word32)(testVectors + i)->certSz), 0);
  25815. if (pkcs7 != NULL) {
  25816. #ifdef ECC_TIMING_RESISTANT
  25817. pkcs7->rng = &rng;
  25818. #endif
  25819. pkcs7->content = (byte*)(testVectors + i)->content;
  25820. pkcs7->contentSz = (testVectors + i)->contentSz;
  25821. pkcs7->contentOID = (testVectors + i)->contentOID;
  25822. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25823. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25824. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25825. pkcs7->privateKey = (testVectors + i)->privateKey;
  25826. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25827. }
  25828. #ifdef ASN_BER_TO_DER
  25829. /* test without setting stream mode */
  25830. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25831. #endif
  25832. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25833. (word32)sizeof(output)), 0);
  25834. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25835. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25836. ExpectIntGE(decodedSz, 0);
  25837. /* Verify the size of each buffer. */
  25838. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25839. /* Don't free the last time through the loop. */
  25840. if (i < testSz - 1) {
  25841. wc_PKCS7_Free(pkcs7);
  25842. pkcs7 = NULL;
  25843. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25844. }
  25845. } /* END test loop. */
  25846. }
  25847. /* Test bad args. */
  25848. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25849. (word32)sizeof(output)), BAD_FUNC_ARG);
  25850. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25851. (word32)sizeof(output)), BAD_FUNC_ARG);
  25852. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25853. /* Decode. */
  25854. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25855. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25856. BAD_FUNC_ARG);
  25857. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25858. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25859. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25860. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25861. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25862. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25863. BAD_FUNC_ARG);
  25864. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25865. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25866. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25867. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25868. /* only a failure for KARI test cases */
  25869. if (pkcs7 != NULL) {
  25870. tempWrd32 = pkcs7->singleCertSz;
  25871. pkcs7->singleCertSz = 0;
  25872. }
  25873. #if defined(WOLFSSL_ASN_TEMPLATE)
  25874. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25875. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25876. BUFFER_E);
  25877. #else
  25878. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25879. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25880. ASN_PARSE_E);
  25881. #endif
  25882. if (pkcs7 != NULL) {
  25883. pkcs7->singleCertSz = tempWrd32;
  25884. tmpBytePtr = pkcs7->singleCert;
  25885. pkcs7->singleCert = NULL;
  25886. }
  25887. #ifndef NO_RSA
  25888. #if defined(NO_PKCS7_STREAM)
  25889. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25890. * is returned from kari parse which gets set to bad func arg */
  25891. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25892. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25893. BAD_FUNC_ARG);
  25894. #else
  25895. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25896. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25897. ASN_PARSE_E);
  25898. #endif
  25899. #endif /* !NO_RSA */
  25900. if (pkcs7 != NULL) {
  25901. pkcs7->singleCert = tmpBytePtr;
  25902. }
  25903. #endif
  25904. if (pkcs7 != NULL) {
  25905. tempWrd32 = pkcs7->privateKeySz;
  25906. pkcs7->privateKeySz = 0;
  25907. }
  25908. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25909. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25910. BAD_FUNC_ARG);
  25911. if (pkcs7 != NULL) {
  25912. pkcs7->privateKeySz = tempWrd32;
  25913. tmpBytePtr = pkcs7->privateKey;
  25914. pkcs7->privateKey = NULL;
  25915. }
  25916. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25917. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25918. BAD_FUNC_ARG);
  25919. if (pkcs7 != NULL) {
  25920. pkcs7->privateKey = tmpBytePtr;
  25921. }
  25922. wc_PKCS7_Free(pkcs7);
  25923. pkcs7 = NULL;
  25924. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25925. /* test of decrypt callback with KEKRI enveloped data */
  25926. {
  25927. int envelopedSz = 0;
  25928. const byte keyId[] = { 0x00 };
  25929. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25930. if (pkcs7 != NULL) {
  25931. pkcs7->content = (byte*)input;
  25932. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25933. pkcs7->contentOID = DATA;
  25934. pkcs7->encryptOID = AES256CBCb;
  25935. }
  25936. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25937. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25938. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25939. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25940. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25941. (word32)sizeof(output))), 0);
  25942. wc_PKCS7_Free(pkcs7);
  25943. pkcs7 = NULL;
  25944. /* decode envelopedData */
  25945. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25946. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25947. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25948. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25949. (word32)envelopedSz, decoded, sizeof(decoded))), 0);
  25950. wc_PKCS7_Free(pkcs7);
  25951. pkcs7 = NULL;
  25952. }
  25953. #endif /* !NO_AES && !NO_AES_256 */
  25954. #ifndef NO_RSA
  25955. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25956. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25957. #endif /* NO_RSA */
  25958. #ifdef HAVE_ECC
  25959. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25960. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25961. #endif /* HAVE_ECC */
  25962. #ifdef ECC_TIMING_RESISTANT
  25963. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25964. #endif
  25965. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25966. !defined(NO_RSA) && !defined(NO_SHA)
  25967. {
  25968. byte out[7];
  25969. byte *cms = NULL;
  25970. word32 cmsSz;
  25971. XFILE cmsFile = XBADFILE;
  25972. XMEMSET(out, 0, sizeof(out));
  25973. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25974. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25975. != XBADFILE);
  25976. cmsSz = (word32)FOURK_BUF;
  25977. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25978. DYNAMIC_TYPE_TMP_BUFFER));
  25979. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25980. if (cmsFile != XBADFILE)
  25981. XFCLOSE(cmsFile);
  25982. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25983. sizeof_client_cert_der_2048), 0);
  25984. if (pkcs7 != NULL) {
  25985. pkcs7->privateKey = (byte*)client_key_der_2048;
  25986. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25987. }
  25988. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25989. 2), 0);
  25990. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25991. sizeof(out)), 0);
  25992. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25993. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25994. wc_PKCS7_Free(pkcs7);
  25995. pkcs7 = NULL;
  25996. }
  25997. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25998. #endif /* HAVE_PKCS7 */
  25999. return EXPECT_RESULT();
  26000. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  26001. /*
  26002. * Testing wc_PKCS7_EncodeEncryptedData()
  26003. */
  26004. static int test_wc_PKCS7_EncodeEncryptedData(void)
  26005. {
  26006. EXPECT_DECLS;
  26007. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26008. PKCS7* pkcs7 = NULL;
  26009. byte* tmpBytePtr = NULL;
  26010. byte encrypted[TWOK_BUF];
  26011. byte decoded[TWOK_BUF];
  26012. word32 tmpWrd32 = 0;
  26013. int tmpInt = 0;
  26014. int decodedSz = 0;
  26015. int encryptedSz = 0;
  26016. int testSz = 0;
  26017. int i = 0;
  26018. const byte data[] = { /* Hello World */
  26019. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26020. 0x72,0x6c,0x64
  26021. };
  26022. #ifndef NO_DES3
  26023. byte desKey[] = {
  26024. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  26025. };
  26026. byte des3Key[] = {
  26027. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  26028. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  26029. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  26030. };
  26031. #endif
  26032. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26033. #ifndef NO_AES_128
  26034. byte aes128Key[] = {
  26035. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26036. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26037. };
  26038. #endif
  26039. #ifndef NO_AES_192
  26040. byte aes192Key[] = {
  26041. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26042. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26043. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26044. };
  26045. #endif
  26046. #ifndef NO_AES_256
  26047. byte aes256Key[] = {
  26048. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26049. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26050. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26051. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26052. };
  26053. #endif
  26054. #endif /* !NO_AES && HAVE_AES_CBC */
  26055. const pkcs7EncryptedVector testVectors[] =
  26056. {
  26057. #ifndef NO_DES3
  26058. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  26059. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  26060. #endif /* !NO_DES3 */
  26061. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26062. #ifndef NO_AES_128
  26063. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  26064. sizeof(aes128Key)},
  26065. #endif
  26066. #ifndef NO_AES_192
  26067. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  26068. sizeof(aes192Key)},
  26069. #endif
  26070. #ifndef NO_AES_256
  26071. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  26072. sizeof(aes256Key)},
  26073. #endif
  26074. #endif /* !NO_AES && HAVE_AES_CBC */
  26075. };
  26076. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  26077. for (i = 0; i < testSz; i++) {
  26078. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26079. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26080. if (pkcs7 != NULL) {
  26081. pkcs7->content = (byte*)testVectors[i].content;
  26082. pkcs7->contentSz = testVectors[i].contentSz;
  26083. pkcs7->contentOID = testVectors[i].contentOID;
  26084. pkcs7->encryptOID = testVectors[i].encryptOID;
  26085. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  26086. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  26087. pkcs7->heap = HEAP_HINT;
  26088. }
  26089. /* encode encryptedData */
  26090. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26091. sizeof(encrypted)), 0);
  26092. /* Decode encryptedData */
  26093. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  26094. (word32)encryptedSz, decoded, sizeof(decoded)), 0);
  26095. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  26096. /* Keep values for last itr. */
  26097. if (i < testSz - 1) {
  26098. wc_PKCS7_Free(pkcs7);
  26099. pkcs7 = NULL;
  26100. }
  26101. }
  26102. if (pkcs7 == NULL || testSz == 0) {
  26103. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26104. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26105. }
  26106. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  26107. sizeof(encrypted)),BAD_FUNC_ARG);
  26108. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  26109. sizeof(encrypted)), BAD_FUNC_ARG);
  26110. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26111. 0), BAD_FUNC_ARG);
  26112. /* Testing the struct. */
  26113. if (pkcs7 != NULL) {
  26114. tmpBytePtr = pkcs7->content;
  26115. pkcs7->content = NULL;
  26116. }
  26117. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26118. sizeof(encrypted)), BAD_FUNC_ARG);
  26119. if (pkcs7 != NULL) {
  26120. pkcs7->content = tmpBytePtr;
  26121. tmpWrd32 = pkcs7->contentSz;
  26122. pkcs7->contentSz = 0;
  26123. }
  26124. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26125. sizeof(encrypted)), BAD_FUNC_ARG);
  26126. if (pkcs7 != NULL) {
  26127. pkcs7->contentSz = tmpWrd32;
  26128. tmpInt = pkcs7->encryptOID;
  26129. pkcs7->encryptOID = 0;
  26130. }
  26131. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26132. sizeof(encrypted)), BAD_FUNC_ARG);
  26133. if (pkcs7 != NULL) {
  26134. pkcs7->encryptOID = tmpInt;
  26135. tmpBytePtr = pkcs7->encryptionKey;
  26136. pkcs7->encryptionKey = NULL;
  26137. }
  26138. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26139. sizeof(encrypted)), BAD_FUNC_ARG);
  26140. if (pkcs7 != NULL) {
  26141. pkcs7->encryptionKey = tmpBytePtr;
  26142. tmpWrd32 = pkcs7->encryptionKeySz;
  26143. pkcs7->encryptionKeySz = 0;
  26144. }
  26145. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26146. sizeof(encrypted)), BAD_FUNC_ARG);
  26147. if (pkcs7 != NULL) {
  26148. pkcs7->encryptionKeySz = tmpWrd32;
  26149. }
  26150. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, (word32)encryptedSz,
  26151. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26152. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, (word32)encryptedSz,
  26153. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26154. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  26155. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26156. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26157. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  26158. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26159. decoded, 0), BAD_FUNC_ARG);
  26160. /* Test struct fields */
  26161. if (pkcs7 != NULL) {
  26162. tmpBytePtr = pkcs7->encryptionKey;
  26163. pkcs7->encryptionKey = NULL;
  26164. }
  26165. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26166. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26167. if (pkcs7 != NULL) {
  26168. pkcs7->encryptionKey = tmpBytePtr;
  26169. pkcs7->encryptionKeySz = 0;
  26170. }
  26171. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26172. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26173. wc_PKCS7_Free(pkcs7);
  26174. #endif
  26175. return EXPECT_RESULT();
  26176. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  26177. /*
  26178. * Testing wc_PKCS7_Degenerate()
  26179. */
  26180. static int test_wc_PKCS7_Degenerate(void)
  26181. {
  26182. EXPECT_DECLS;
  26183. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26184. PKCS7* pkcs7 = NULL;
  26185. char fName[] = "./certs/test-degenerate.p7b";
  26186. XFILE f = XBADFILE;
  26187. byte der[4096];
  26188. word32 derSz = 0;
  26189. #ifndef NO_PKCS7_STREAM
  26190. word32 z;
  26191. int ret;
  26192. #endif /* !NO_PKCS7_STREAM */
  26193. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26194. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26195. if (f != XBADFILE)
  26196. XFCLOSE(f);
  26197. /* test degenerate success */
  26198. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26199. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26200. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26201. #ifndef NO_RSA
  26202. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26203. #ifndef NO_PKCS7_STREAM
  26204. wc_PKCS7_Free(pkcs7);
  26205. pkcs7 = NULL;
  26206. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26207. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26208. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26209. /* test for streaming */
  26210. ret = -1;
  26211. for (z = 0; z < derSz && ret != 0; z++) {
  26212. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26213. if (ret < 0){
  26214. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26215. }
  26216. }
  26217. ExpectIntEQ(ret, 0);
  26218. #endif /* !NO_PKCS7_STREAM */
  26219. #else
  26220. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26221. #endif /* NO_RSA */
  26222. wc_PKCS7_Free(pkcs7);
  26223. pkcs7 = NULL;
  26224. /* test with turning off degenerate cases */
  26225. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26226. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26227. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26228. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26229. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  26230. PKCS7_NO_SIGNER_E);
  26231. #ifndef NO_PKCS7_STREAM
  26232. wc_PKCS7_Free(pkcs7);
  26233. pkcs7 = NULL;
  26234. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26235. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26236. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26237. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26238. /* test for streaming */
  26239. ret = -1;
  26240. for (z = 0; z < derSz && ret != 0; z++) {
  26241. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26242. if (ret == WC_PKCS7_WANT_READ_E){
  26243. continue;
  26244. }
  26245. else
  26246. break;
  26247. }
  26248. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  26249. #endif /* !NO_PKCS7_STREAM */
  26250. wc_PKCS7_Free(pkcs7);
  26251. #endif
  26252. return EXPECT_RESULT();
  26253. } /* END test_wc_PKCS7_Degenerate() */
  26254. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26255. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26256. static byte berContent[] = {
  26257. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26258. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26259. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26260. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26261. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26262. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26263. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26264. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26265. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26266. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26267. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26268. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26269. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26270. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26271. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26272. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26273. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26274. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26275. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26276. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26277. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26278. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26279. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26280. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26281. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26282. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26283. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26284. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26285. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26286. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26287. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26288. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26289. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26290. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26291. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26292. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26293. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26294. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26295. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26296. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26297. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26298. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26299. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26300. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26301. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26302. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26303. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26304. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26305. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26306. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26307. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26308. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26309. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26310. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26311. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26312. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26313. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26314. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26315. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26316. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26317. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26318. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26319. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26320. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26321. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26322. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26323. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26324. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26325. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26326. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26327. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26328. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26329. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26330. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26331. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26332. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26333. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26334. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26335. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26336. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26337. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26338. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26339. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26340. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26341. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26342. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26343. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26344. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26345. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26346. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26347. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26348. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26349. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26350. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26351. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26352. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26353. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26354. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26355. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26356. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26357. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26358. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26359. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26360. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26361. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26362. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26363. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26364. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26365. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26366. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26367. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26368. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26369. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26370. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26371. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26372. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26373. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26374. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26375. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26376. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26377. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26378. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26379. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26380. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26381. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26382. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26383. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26384. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26385. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26386. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26387. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26388. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26389. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26390. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26391. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26392. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26393. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26394. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26395. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26396. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26397. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26398. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26399. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26400. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26401. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26402. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26403. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26404. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26405. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26406. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26407. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26408. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26409. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26410. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26411. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26412. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26413. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26414. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26415. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26416. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26417. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26418. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26419. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26420. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26421. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26422. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26423. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26424. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26425. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26426. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26427. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26428. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26429. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26430. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26431. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26432. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26433. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26434. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26435. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26436. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26437. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26438. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26439. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26440. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26441. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26442. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26443. 0x00, 0x00, 0x00, 0x00, 0x00
  26444. };
  26445. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26446. * !NO_DES3 && !NO_SHA
  26447. */
  26448. /*
  26449. * Testing wc_PKCS7_BER()
  26450. */
  26451. static int test_wc_PKCS7_BER(void)
  26452. {
  26453. EXPECT_DECLS;
  26454. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26455. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26456. PKCS7* pkcs7 = NULL;
  26457. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26458. XFILE f = XBADFILE;
  26459. byte der[4096];
  26460. #ifndef NO_DES3
  26461. byte decoded[2048];
  26462. #endif
  26463. word32 derSz = 0;
  26464. #ifndef NO_PKCS7_STREAM
  26465. word32 z;
  26466. int ret;
  26467. #endif /* !NO_PKCS7_STREAM */
  26468. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26469. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26470. if (f != XBADFILE) {
  26471. XFCLOSE(f);
  26472. f = XBADFILE;
  26473. }
  26474. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26475. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26476. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26477. #ifndef NO_RSA
  26478. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26479. #ifndef NO_PKCS7_STREAM
  26480. wc_PKCS7_Free(pkcs7);
  26481. pkcs7 = NULL;
  26482. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26483. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26484. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26485. /* test for streaming */
  26486. ret = -1;
  26487. for (z = 0; z < derSz && ret != 0; z++) {
  26488. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26489. if (ret < 0){
  26490. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26491. }
  26492. }
  26493. ExpectIntEQ(ret, 0);
  26494. #endif /* !NO_PKCS7_STREAM */
  26495. #else
  26496. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26497. #endif
  26498. wc_PKCS7_Free(pkcs7);
  26499. pkcs7 = NULL;
  26500. #ifndef NO_DES3
  26501. /* decode BER content */
  26502. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26503. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26504. if (f != XBADFILE) {
  26505. XFCLOSE(f);
  26506. f = XBADFILE;
  26507. }
  26508. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26509. #ifndef NO_RSA
  26510. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26511. #else
  26512. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26513. #endif
  26514. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  26515. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26516. if (f != XBADFILE) {
  26517. XFCLOSE(f);
  26518. f = XBADFILE;
  26519. }
  26520. if (pkcs7 != NULL) {
  26521. pkcs7->privateKey = der;
  26522. pkcs7->privateKeySz = derSz;
  26523. }
  26524. #ifndef NO_RSA
  26525. #ifdef WOLFSSL_SP_MATH
  26526. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26527. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26528. #else
  26529. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26530. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26531. #endif
  26532. #else
  26533. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26534. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26535. #endif
  26536. wc_PKCS7_Free(pkcs7);
  26537. #endif /* !NO_DES3 */
  26538. #endif
  26539. return EXPECT_RESULT();
  26540. } /* END test_wc_PKCS7_BER() */
  26541. static int test_wc_PKCS7_signed_enveloped(void)
  26542. {
  26543. EXPECT_DECLS;
  26544. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26545. !defined(NO_FILESYSTEM)
  26546. XFILE f = XBADFILE;
  26547. PKCS7* pkcs7 = NULL;
  26548. #ifdef HAVE_AES_CBC
  26549. PKCS7* inner = NULL;
  26550. #endif
  26551. WC_RNG rng;
  26552. unsigned char key[FOURK_BUF/2];
  26553. unsigned char cert[FOURK_BUF/2];
  26554. unsigned char env[FOURK_BUF];
  26555. int envSz = FOURK_BUF;
  26556. int keySz = 0;
  26557. int certSz = 0;
  26558. unsigned char sig[FOURK_BUF * 2];
  26559. int sigSz = FOURK_BUF * 2;
  26560. #ifdef HAVE_AES_CBC
  26561. unsigned char decoded[FOURK_BUF];
  26562. int decodedSz = FOURK_BUF;
  26563. #endif
  26564. #ifndef NO_PKCS7_STREAM
  26565. int z;
  26566. int ret;
  26567. #endif /* !NO_PKCS7_STREAM */
  26568. XMEMSET(&rng, 0, sizeof(WC_RNG));
  26569. /* load cert */
  26570. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  26571. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26572. if (f != XBADFILE) {
  26573. XFCLOSE(f);
  26574. f = XBADFILE;
  26575. }
  26576. /* load key */
  26577. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  26578. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26579. if (f != XBADFILE) {
  26580. XFCLOSE(f);
  26581. f = XBADFILE;
  26582. }
  26583. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  26584. /* sign cert for envelope */
  26585. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26586. ExpectIntEQ(wc_InitRng(&rng), 0);
  26587. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26588. if (pkcs7 != NULL) {
  26589. pkcs7->content = cert;
  26590. pkcs7->contentSz = (word32)certSz;
  26591. pkcs7->contentOID = DATA;
  26592. pkcs7->privateKey = key;
  26593. pkcs7->privateKeySz = (word32)keySz;
  26594. pkcs7->encryptOID = RSAk;
  26595. pkcs7->hashOID = SHA256h;
  26596. pkcs7->rng = &rng;
  26597. }
  26598. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26599. wc_PKCS7_Free(pkcs7);
  26600. pkcs7 = NULL;
  26601. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26602. #ifdef HAVE_AES_CBC
  26603. /* create envelope */
  26604. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26605. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26606. if (pkcs7 != NULL) {
  26607. pkcs7->content = sig;
  26608. pkcs7->contentSz = (word32)sigSz;
  26609. pkcs7->contentOID = DATA;
  26610. pkcs7->encryptOID = AES256CBCb;
  26611. pkcs7->privateKey = key;
  26612. pkcs7->privateKeySz = (word32)keySz;
  26613. }
  26614. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, (word32)envSz)), 0);
  26615. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26616. wc_PKCS7_Free(pkcs7);
  26617. pkcs7 = NULL;
  26618. #endif
  26619. /* create bad signed enveloped data */
  26620. sigSz = FOURK_BUF * 2;
  26621. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26622. ExpectIntEQ(wc_InitRng(&rng), 0);
  26623. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26624. if (pkcs7 != NULL) {
  26625. pkcs7->content = env;
  26626. pkcs7->contentSz = (word32)envSz;
  26627. pkcs7->contentOID = DATA;
  26628. pkcs7->privateKey = key;
  26629. pkcs7->privateKeySz = (word32)keySz;
  26630. pkcs7->encryptOID = RSAk;
  26631. pkcs7->hashOID = SHA256h;
  26632. pkcs7->rng = &rng;
  26633. }
  26634. /* Set no certs in bundle for this test. */
  26635. if (pkcs7 != NULL) {
  26636. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26637. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26638. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26639. }
  26640. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26641. wc_PKCS7_Free(pkcs7);
  26642. pkcs7 = NULL;
  26643. /* check verify fails */
  26644. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26645. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26646. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz),
  26647. PKCS7_SIGNEEDS_CHECK);
  26648. /* try verifying the signature manually */
  26649. {
  26650. RsaKey rKey;
  26651. word32 idx = 0;
  26652. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26653. WC_MAX_DIGEST_SIZE];
  26654. int digestSz = 0;
  26655. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26656. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, (word32)keySz), 0);
  26657. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26658. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26659. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26660. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26661. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26662. /* verify was success */
  26663. }
  26664. wc_PKCS7_Free(pkcs7);
  26665. pkcs7 = NULL;
  26666. /* initializing the PKCS7 struct with the signing certificate should pass */
  26667. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26668. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26669. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  26670. #ifndef NO_PKCS7_STREAM
  26671. wc_PKCS7_Free(pkcs7);
  26672. pkcs7 = NULL;
  26673. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26674. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26675. /* test for streaming */
  26676. ret = -1;
  26677. for (z = 0; z < sigSz && ret != 0; z++) {
  26678. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26679. if (ret < 0){
  26680. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26681. }
  26682. }
  26683. ExpectIntEQ(ret, 0);
  26684. #endif /* !NO_PKCS7_STREAM */
  26685. wc_PKCS7_Free(pkcs7);
  26686. pkcs7 = NULL;
  26687. /* create valid degenerate bundle */
  26688. sigSz = FOURK_BUF * 2;
  26689. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26690. if (pkcs7 != NULL) {
  26691. pkcs7->content = env;
  26692. pkcs7->contentSz = (word32)envSz;
  26693. pkcs7->contentOID = DATA;
  26694. pkcs7->privateKey = key;
  26695. pkcs7->privateKeySz = (word32)keySz;
  26696. pkcs7->encryptOID = RSAk;
  26697. pkcs7->hashOID = SHA256h;
  26698. pkcs7->rng = &rng;
  26699. }
  26700. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26701. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26702. wc_PKCS7_Free(pkcs7);
  26703. pkcs7 = NULL;
  26704. wc_FreeRng(&rng);
  26705. /* check verify */
  26706. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26707. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26708. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  26709. ExpectNotNull(pkcs7->content);
  26710. #ifndef NO_PKCS7_STREAM
  26711. wc_PKCS7_Free(pkcs7);
  26712. pkcs7 = NULL;
  26713. /* create valid degenerate bundle */
  26714. sigSz = FOURK_BUF * 2;
  26715. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26716. if (pkcs7 != NULL) {
  26717. pkcs7->content = env;
  26718. pkcs7->contentSz = (word32)envSz;
  26719. pkcs7->contentOID = DATA;
  26720. pkcs7->privateKey = key;
  26721. pkcs7->privateKeySz = (word32)keySz;
  26722. pkcs7->encryptOID = RSAk;
  26723. pkcs7->hashOID = SHA256h;
  26724. pkcs7->rng = &rng;
  26725. }
  26726. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26727. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26728. wc_PKCS7_Free(pkcs7);
  26729. pkcs7 = NULL;
  26730. wc_FreeRng(&rng);
  26731. /* check verify */
  26732. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26733. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26734. /* test for streaming */
  26735. ret = -1;
  26736. for (z = 0; z < sigSz && ret != 0; z++) {
  26737. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26738. if (ret < 0){
  26739. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26740. }
  26741. }
  26742. ExpectIntEQ(ret, 0);
  26743. #endif /* !NO_PKCS7_STREAM */
  26744. #ifdef HAVE_AES_CBC
  26745. /* check decode */
  26746. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26747. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, (word32)certSz), 0);
  26748. if (inner != NULL) {
  26749. inner->privateKey = key;
  26750. inner->privateKeySz = (word32)keySz;
  26751. }
  26752. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26753. pkcs7->contentSz, decoded, (word32)decodedSz)), 0);
  26754. wc_PKCS7_Free(inner);
  26755. inner = NULL;
  26756. #endif
  26757. wc_PKCS7_Free(pkcs7);
  26758. pkcs7 = NULL;
  26759. #ifdef HAVE_AES_CBC
  26760. /* check cert set */
  26761. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26762. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26763. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, (word32)decodedSz), 0);
  26764. ExpectNotNull(pkcs7->singleCert);
  26765. ExpectIntNE(pkcs7->singleCertSz, 0);
  26766. wc_PKCS7_Free(pkcs7);
  26767. pkcs7 = NULL;
  26768. #ifndef NO_PKCS7_STREAM
  26769. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26770. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26771. /* test for streaming */
  26772. ret = -1;
  26773. for (z = 0; z < decodedSz && ret != 0; z++) {
  26774. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26775. if (ret < 0){
  26776. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26777. }
  26778. }
  26779. ExpectIntEQ(ret, 0);
  26780. ExpectNotNull(pkcs7->singleCert);
  26781. ExpectIntNE(pkcs7->singleCertSz, 0);
  26782. wc_PKCS7_Free(pkcs7);
  26783. pkcs7 = NULL;
  26784. #endif /* !NO_PKCS7_STREAM */
  26785. #endif
  26786. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26787. return EXPECT_RESULT();
  26788. }
  26789. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26790. {
  26791. EXPECT_DECLS;
  26792. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26793. && !defined(NO_AES)
  26794. PKCS7* pkcs7 = NULL;
  26795. void* heap = NULL;
  26796. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26797. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26798. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26799. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26800. wc_PKCS7_Free(pkcs7);
  26801. #endif
  26802. return EXPECT_RESULT();
  26803. }
  26804. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26805. {
  26806. EXPECT_DECLS;
  26807. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26808. && !defined(NO_AES)
  26809. PKCS7* pkcs7 = NULL;
  26810. void* heap = NULL;
  26811. WOLFSSL_CTX* ctx = NULL;
  26812. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26813. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26814. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26815. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26816. wc_PKCS7_Free(pkcs7);
  26817. #endif
  26818. return EXPECT_RESULT();
  26819. }
  26820. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26821. {
  26822. EXPECT_DECLS;
  26823. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26824. && !defined(NO_AES)
  26825. PKCS7* pkcs7 = NULL;
  26826. void* heap = NULL;
  26827. WOLFSSL_CTX* ctx = NULL;
  26828. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26829. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26830. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26831. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26832. wc_PKCS7_Free(pkcs7);
  26833. #endif
  26834. return EXPECT_RESULT();
  26835. }
  26836. static int test_wc_PKCS7_DecodeCompressedData(void)
  26837. {
  26838. EXPECT_DECLS;
  26839. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26840. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26841. PKCS7* pkcs7 = NULL;
  26842. void* heap = NULL;
  26843. byte out[4096];
  26844. byte* decompressed = NULL;
  26845. int outSz;
  26846. int decompressedSz;
  26847. const char* cert = "./certs/client-cert.pem";
  26848. byte* cert_buf = NULL;
  26849. size_t cert_sz = 0;
  26850. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26851. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26852. DYNAMIC_TYPE_TMP_BUFFER)));
  26853. decompressedSz = (int)cert_sz;
  26854. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26855. if (pkcs7 != NULL) {
  26856. pkcs7->content = (byte*)cert_buf;
  26857. pkcs7->contentSz = (word32)cert_sz;
  26858. pkcs7->contentOID = DATA;
  26859. }
  26860. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26861. sizeof(out))), 0);
  26862. wc_PKCS7_Free(pkcs7);
  26863. pkcs7 = NULL;
  26864. /* compressed key should be smaller than when started */
  26865. ExpectIntLT(outSz, cert_sz);
  26866. /* test decompression */
  26867. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26868. ExpectIntEQ(pkcs7->contentOID, 0);
  26869. /* fail case with out buffer too small */
  26870. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26871. decompressed, outSz), 0);
  26872. /* success case */
  26873. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26874. decompressed, decompressedSz), cert_sz);
  26875. ExpectIntEQ(pkcs7->contentOID, DATA);
  26876. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26877. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26878. decompressed = NULL;
  26879. /* test decompression function with different 'max' inputs */
  26880. outSz = sizeof(out);
  26881. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26882. 0);
  26883. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26884. out, outSz, 0, heap), 0);
  26885. ExpectNull(decompressed);
  26886. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26887. out, outSz, 0, heap), 0);
  26888. ExpectNotNull(decompressed);
  26889. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26890. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26891. decompressed = NULL;
  26892. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26893. out, outSz, 0, heap), 0);
  26894. ExpectNotNull(decompressed);
  26895. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26896. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26897. if (cert_buf != NULL)
  26898. free(cert_buf);
  26899. wc_PKCS7_Free(pkcs7);
  26900. #endif
  26901. return EXPECT_RESULT();
  26902. }
  26903. static int test_wc_i2d_PKCS12(void)
  26904. {
  26905. EXPECT_DECLS;
  26906. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26907. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26908. && !defined(NO_AES) && !defined(NO_SHA)
  26909. WC_PKCS12* pkcs12 = NULL;
  26910. unsigned char der[FOURK_BUF * 2];
  26911. unsigned char* pt;
  26912. int derSz = 0;
  26913. unsigned char out[FOURK_BUF * 2];
  26914. int outSz = FOURK_BUF * 2;
  26915. const char p12_f[] = "./certs/test-servercert.p12";
  26916. XFILE f = XBADFILE;
  26917. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26918. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26919. if (f != XBADFILE)
  26920. XFCLOSE(f);
  26921. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26922. ExpectIntEQ(wc_d2i_PKCS12(der, (word32)derSz, pkcs12), 0);
  26923. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26924. ExpectIntEQ(outSz, derSz);
  26925. outSz = derSz - 1;
  26926. pt = out;
  26927. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26928. outSz = derSz;
  26929. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26930. ExpectIntEQ((pt == out), 0);
  26931. pt = NULL;
  26932. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26933. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26934. wc_PKCS12_free(pkcs12);
  26935. pkcs12 = NULL;
  26936. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26937. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26938. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26939. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26940. ExpectIntEQ(outSz, derSz);
  26941. wc_PKCS12_free(pkcs12);
  26942. pkcs12 = NULL;
  26943. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26944. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26945. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26946. ExpectIntEQ(outSz, derSz);
  26947. wc_PKCS12_free(pkcs12);
  26948. pkcs12 = NULL;
  26949. #endif
  26950. return EXPECT_RESULT();
  26951. }
  26952. /* Testing wc_SignatureGetSize() for signature type ECC */
  26953. static int test_wc_SignatureGetSize_ecc(void)
  26954. {
  26955. EXPECT_DECLS;
  26956. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26957. enum wc_SignatureType sig_type;
  26958. word32 key_len;
  26959. ecc_key ecc;
  26960. const char* qx =
  26961. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26962. const char* qy =
  26963. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26964. const char* d =
  26965. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26966. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26967. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26968. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26969. /* Input for signature type ECC */
  26970. sig_type = WC_SIGNATURE_TYPE_ECC;
  26971. key_len = sizeof(ecc_key);
  26972. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26973. /* Test bad args */
  26974. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26975. sig_type = (enum wc_SignatureType) 100;
  26976. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26977. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26978. sig_type = WC_SIGNATURE_TYPE_ECC;
  26979. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26980. key_len = (word32)0;
  26981. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26982. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26983. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26984. return EXPECT_RESULT();
  26985. } /* END test_wc_SignatureGetSize_ecc() */
  26986. /* Testing wc_SignatureGetSize() for signature type rsa */
  26987. static int test_wc_SignatureGetSize_rsa(void)
  26988. {
  26989. EXPECT_DECLS;
  26990. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26991. enum wc_SignatureType sig_type;
  26992. word32 key_len;
  26993. word32 idx = 0;
  26994. RsaKey rsa_key;
  26995. byte* tmp = NULL;
  26996. size_t bytes;
  26997. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26998. #ifdef USE_CERT_BUFFERS_1024
  26999. bytes = (size_t)sizeof_client_key_der_1024;
  27000. if (bytes < (size_t)sizeof_client_key_der_1024)
  27001. bytes = (size_t)sizeof_client_cert_der_1024;
  27002. #elif defined(USE_CERT_BUFFERS_2048)
  27003. bytes = (size_t)sizeof_client_key_der_2048;
  27004. if (bytes < (size_t)sizeof_client_cert_der_2048)
  27005. bytes = (size_t)sizeof_client_cert_der_2048;
  27006. #else
  27007. bytes = FOURK_BUF;
  27008. #endif
  27009. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  27010. DYNAMIC_TYPE_TMP_BUFFER));
  27011. if (tmp != NULL) {
  27012. #ifdef USE_CERT_BUFFERS_1024
  27013. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  27014. #elif defined(USE_CERT_BUFFERS_2048)
  27015. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  27016. #elif !defined(NO_FILESYSTEM)
  27017. XFILE file = XBADFILE;
  27018. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  27019. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  27020. if (file != XBADFILE)
  27021. XFCLOSE(file);
  27022. }
  27023. #else
  27024. ExpectFail();
  27025. #endif
  27026. }
  27027. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  27028. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  27029. /* Input for signature type RSA */
  27030. sig_type = WC_SIGNATURE_TYPE_RSA;
  27031. key_len = sizeof(RsaKey);
  27032. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  27033. /* Test bad args */
  27034. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  27035. sig_type = (enum wc_SignatureType)100;
  27036. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  27037. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  27038. sig_type = WC_SIGNATURE_TYPE_RSA;
  27039. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  27040. key_len = (word32)0;
  27041. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  27042. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  27043. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27044. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  27045. return EXPECT_RESULT();
  27046. } /* END test_wc_SignatureGetSize_rsa(void) */
  27047. /*----------------------------------------------------------------------------*
  27048. | hash.h Tests
  27049. *----------------------------------------------------------------------------*/
  27050. static int test_wc_HashInit(void)
  27051. {
  27052. EXPECT_DECLS;
  27053. int i; /* 0 indicates tests passed, 1 indicates failure */
  27054. wc_HashAlg hash;
  27055. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27056. enum wc_HashType enumArray[] = {
  27057. #ifndef NO_MD5
  27058. WC_HASH_TYPE_MD5,
  27059. #endif
  27060. #ifndef NO_SHA
  27061. WC_HASH_TYPE_SHA,
  27062. #endif
  27063. #ifdef WOLFSSL_SHA224
  27064. WC_HASH_TYPE_SHA224,
  27065. #endif
  27066. #ifndef NO_SHA256
  27067. WC_HASH_TYPE_SHA256,
  27068. #endif
  27069. #ifdef WOLFSSL_SHA384
  27070. WC_HASH_TYPE_SHA384,
  27071. #endif
  27072. #ifdef WOLFSSL_SHA512
  27073. WC_HASH_TYPE_SHA512,
  27074. #endif
  27075. };
  27076. /* dynamically finds the length */
  27077. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27078. /* For loop to test various arguments... */
  27079. for (i = 0; i < enumlen; i++) {
  27080. /* check for bad args */
  27081. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27082. wc_HashFree(&hash, enumArray[i]);
  27083. /* check for null ptr */
  27084. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  27085. } /* end of for loop */
  27086. return EXPECT_RESULT();
  27087. } /* end of test_wc_HashInit */
  27088. /*
  27089. * Unit test function for wc_HashSetFlags()
  27090. */
  27091. static int test_wc_HashSetFlags(void)
  27092. {
  27093. EXPECT_DECLS;
  27094. #ifdef WOLFSSL_HASH_FLAGS
  27095. wc_HashAlg hash;
  27096. word32 flags = 0;
  27097. int i, j;
  27098. int notSupportedLen;
  27099. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27100. enum wc_HashType enumArray[] = {
  27101. #ifndef NO_MD5
  27102. WC_HASH_TYPE_MD5,
  27103. #endif
  27104. #ifndef NO_SHA
  27105. WC_HASH_TYPE_SHA,
  27106. #endif
  27107. #ifdef WOLFSSL_SHA224
  27108. WC_HASH_TYPE_SHA224,
  27109. #endif
  27110. #ifndef NO_SHA256
  27111. WC_HASH_TYPE_SHA256,
  27112. #endif
  27113. #ifdef WOLFSSL_SHA384
  27114. WC_HASH_TYPE_SHA384,
  27115. #endif
  27116. #ifdef WOLFSSL_SHA512
  27117. WC_HASH_TYPE_SHA512,
  27118. #endif
  27119. #ifdef WOLFSSL_SHA3
  27120. WC_HASH_TYPE_SHA3_224,
  27121. #endif
  27122. };
  27123. enum wc_HashType notSupported[] = {
  27124. WC_HASH_TYPE_MD5_SHA,
  27125. WC_HASH_TYPE_MD2,
  27126. WC_HASH_TYPE_MD4,
  27127. WC_HASH_TYPE_BLAKE2B,
  27128. WC_HASH_TYPE_BLAKE2S,
  27129. WC_HASH_TYPE_NONE,
  27130. };
  27131. /* dynamically finds the length */
  27132. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27133. /* For loop to test various arguments... */
  27134. for (i = 0; i < enumlen; i++) {
  27135. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27136. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  27137. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  27138. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  27139. wc_HashFree(&hash, enumArray[i]);
  27140. }
  27141. /* For loop to test not supported cases */
  27142. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27143. for (j = 0; j < notSupportedLen; j++) {
  27144. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  27145. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  27146. BAD_FUNC_ARG);
  27147. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  27148. }
  27149. #endif
  27150. return EXPECT_RESULT();
  27151. } /* END test_wc_HashSetFlags */
  27152. /*
  27153. * Unit test function for wc_HashGetFlags()
  27154. */
  27155. static int test_wc_HashGetFlags(void)
  27156. {
  27157. EXPECT_DECLS;
  27158. #ifdef WOLFSSL_HASH_FLAGS
  27159. wc_HashAlg hash;
  27160. word32 flags = 0;
  27161. int i, j;
  27162. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27163. enum wc_HashType enumArray[] = {
  27164. #ifndef NO_MD5
  27165. WC_HASH_TYPE_MD5,
  27166. #endif
  27167. #ifndef NO_SHA
  27168. WC_HASH_TYPE_SHA,
  27169. #endif
  27170. #ifdef WOLFSSL_SHA224
  27171. WC_HASH_TYPE_SHA224,
  27172. #endif
  27173. #ifndef NO_SHA256
  27174. WC_HASH_TYPE_SHA256,
  27175. #endif
  27176. #ifdef WOLFSSL_SHA384
  27177. WC_HASH_TYPE_SHA384,
  27178. #endif
  27179. #ifdef WOLFSSL_SHA512
  27180. WC_HASH_TYPE_SHA512,
  27181. #endif
  27182. #ifdef WOLFSSL_SHA3
  27183. WC_HASH_TYPE_SHA3_224,
  27184. #endif
  27185. };
  27186. enum wc_HashType notSupported[] = {
  27187. WC_HASH_TYPE_MD5_SHA,
  27188. WC_HASH_TYPE_MD2,
  27189. WC_HASH_TYPE_MD4,
  27190. WC_HASH_TYPE_BLAKE2B,
  27191. WC_HASH_TYPE_BLAKE2S,
  27192. WC_HASH_TYPE_NONE,
  27193. };
  27194. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27195. int notSupportedLen;
  27196. /* For loop to test various arguments... */
  27197. for (i = 0; i < enumlen; i++) {
  27198. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27199. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  27200. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  27201. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  27202. wc_HashFree(&hash, enumArray[i]);
  27203. }
  27204. /* For loop to test not supported cases */
  27205. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27206. for (j = 0; j < notSupportedLen; j++) {
  27207. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  27208. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  27209. BAD_FUNC_ARG);
  27210. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  27211. }
  27212. #endif
  27213. return EXPECT_RESULT();
  27214. } /* END test_wc_HashGetFlags */
  27215. /*----------------------------------------------------------------------------*
  27216. | Compatibility Tests
  27217. *----------------------------------------------------------------------------*/
  27218. /*----------------------------------------------------------------------------*
  27219. | ASN.1 Tests
  27220. *----------------------------------------------------------------------------*/
  27221. static int test_wolfSSL_ASN1_BIT_STRING(void)
  27222. {
  27223. EXPECT_DECLS;
  27224. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  27225. ASN1_BIT_STRING* str = NULL;
  27226. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27227. /* Empty data testing. */
  27228. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  27229. ASN1_BIT_STRING_free(str);
  27230. str = NULL;
  27231. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27232. /* Invalid parameter testing. */
  27233. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  27234. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  27235. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  27236. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  27237. /* No bit string - bit is always 0. */
  27238. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  27239. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  27240. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27241. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  27242. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  27243. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  27244. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  27245. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27246. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  27247. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  27248. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  27249. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  27250. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  27251. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  27252. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  27253. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  27254. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27255. ASN1_BIT_STRING_free(str);
  27256. ASN1_BIT_STRING_free(NULL);
  27257. #endif
  27258. return EXPECT_RESULT();
  27259. }
  27260. static int test_wolfSSL_ASN1_INTEGER(void)
  27261. {
  27262. EXPECT_DECLS;
  27263. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27264. ASN1_INTEGER* a = NULL;
  27265. ASN1_INTEGER* dup = NULL;
  27266. const unsigned char invalidLenDer[] = {
  27267. 0x02, 0x20, 0x00
  27268. };
  27269. const unsigned char longDer[] = {
  27270. 0x02, 0x20,
  27271. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27272. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27273. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27274. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27275. };
  27276. const unsigned char* p;
  27277. /* Invalid parameter testing. */
  27278. ASN1_INTEGER_free(NULL);
  27279. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27280. ExpectNotNull(a = ASN1_INTEGER_new());
  27281. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27282. ASN1_INTEGER_free(dup);
  27283. dup = NULL;
  27284. ASN1_INTEGER_free(a);
  27285. a = NULL;
  27286. p = longDer;
  27287. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27288. p = longDer;
  27289. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27290. ExpectPtrNE(p, longDer);
  27291. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27292. ASN1_INTEGER_free(dup);
  27293. ASN1_INTEGER_free(a);
  27294. #endif
  27295. return EXPECT_RESULT();
  27296. }
  27297. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27298. {
  27299. EXPECT_DECLS;
  27300. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27301. ASN1_INTEGER* a = NULL;
  27302. ASN1_INTEGER* b = NULL;
  27303. ExpectNotNull(a = ASN1_INTEGER_new());
  27304. ExpectNotNull(b = ASN1_INTEGER_new());
  27305. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27306. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27307. /* Invalid parameter testing. */
  27308. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27309. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27310. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27311. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27312. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27313. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27314. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27315. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27316. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27317. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27318. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27319. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27320. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27321. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27322. ASN1_INTEGER_free(b);
  27323. ASN1_INTEGER_free(a);
  27324. #endif
  27325. return EXPECT_RESULT();
  27326. }
  27327. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27328. {
  27329. EXPECT_DECLS;
  27330. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27331. ASN1_INTEGER* ai = NULL;
  27332. ASN1_INTEGER* ai2 = NULL;
  27333. BIGNUM* bn = NULL;
  27334. BIGNUM* bn2 = NULL;
  27335. ExpectNotNull(ai = ASN1_INTEGER_new());
  27336. ExpectNotNull(bn2 = BN_new());
  27337. /* Invalid parameter testing. */
  27338. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27339. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27340. /* at the moment hard setting since no set function */
  27341. if (ai != NULL) {
  27342. ai->data[0] = 0xff; /* No DER encoding. */
  27343. ai->length = 1;
  27344. }
  27345. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27346. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27347. BN_free(bn);
  27348. bn = NULL;
  27349. #else
  27350. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27351. #endif
  27352. if (ai != NULL) {
  27353. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27354. ai->data[1] = 0x04; /* bad length of integer */
  27355. ai->data[2] = 0x03;
  27356. ai->length = 3;
  27357. }
  27358. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27359. /* Interpreted as a number 0x020403. */
  27360. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27361. BN_free(bn);
  27362. bn = NULL;
  27363. #else
  27364. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27365. #endif
  27366. if (ai != NULL) {
  27367. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27368. ai->data[1] = 0x01; /* length of integer */
  27369. ai->data[2] = 0x03;
  27370. ai->length = 3;
  27371. }
  27372. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27373. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27374. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27375. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27376. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27377. if (ai != NULL) {
  27378. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27379. ai->data[1] = 0x02; /* length of integer */
  27380. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27381. ai->data[3] = 0xff;
  27382. ai->length = 4;
  27383. }
  27384. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27385. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27386. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27387. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27388. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27389. if (ai != NULL) {
  27390. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27391. ai->data[1] = 0x01; /* length of integer */
  27392. ai->data[2] = 0x00;
  27393. ai->length = 3;
  27394. }
  27395. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27396. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27397. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27398. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27399. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27400. if (ai != NULL) {
  27401. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27402. ai->data[1] = 0x01; /* length of integer */
  27403. ai->data[2] = 0x01;
  27404. ai->length = 3;
  27405. ai->negative = 1;
  27406. }
  27407. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27408. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27409. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27410. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27411. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27412. BN_free(bn2);
  27413. BN_free(bn);
  27414. ASN1_INTEGER_free(ai2);
  27415. ASN1_INTEGER_free(ai);
  27416. #endif
  27417. return EXPECT_RESULT();
  27418. }
  27419. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27420. {
  27421. EXPECT_DECLS;
  27422. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27423. ASN1_INTEGER *a = NULL;
  27424. long val;
  27425. ExpectNotNull(a = ASN1_INTEGER_new());
  27426. /* Invalid parameter testing. */
  27427. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  27428. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27429. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  27430. #else
  27431. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  27432. #endif
  27433. ASN1_INTEGER_free(a);
  27434. a = NULL;
  27435. ExpectNotNull(a = ASN1_INTEGER_new());
  27436. val = 0;
  27437. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  27438. ASN1_INTEGER_free(a);
  27439. a = NULL;
  27440. /* 0 */
  27441. ExpectNotNull(a = ASN1_INTEGER_new());
  27442. val = 0;
  27443. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27444. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27445. ASN1_INTEGER_free(a);
  27446. a = NULL;
  27447. /* 40 */
  27448. ExpectNotNull(a = ASN1_INTEGER_new());
  27449. val = 40;
  27450. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27451. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27452. ASN1_INTEGER_free(a);
  27453. a = NULL;
  27454. /* -40 */
  27455. ExpectNotNull(a = ASN1_INTEGER_new());
  27456. val = -40;
  27457. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27458. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27459. ASN1_INTEGER_free(a);
  27460. a = NULL;
  27461. /* 128 */
  27462. ExpectNotNull(a = ASN1_INTEGER_new());
  27463. val = 128;
  27464. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27465. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27466. ASN1_INTEGER_free(a);
  27467. a = NULL;
  27468. /* -128 */
  27469. ExpectNotNull(a = ASN1_INTEGER_new());
  27470. val = -128;
  27471. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27472. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27473. ASN1_INTEGER_free(a);
  27474. a = NULL;
  27475. /* 200 */
  27476. ExpectNotNull(a = ASN1_INTEGER_new());
  27477. val = 200;
  27478. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27479. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27480. ASN1_INTEGER_free(a);
  27481. a = NULL;
  27482. /* int max (2147483647) */
  27483. ExpectNotNull(a = ASN1_INTEGER_new());
  27484. val = 2147483647;
  27485. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27486. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27487. ASN1_INTEGER_free(a);
  27488. a = NULL;
  27489. /* int min (-2147483648) */
  27490. ExpectNotNull(a = ASN1_INTEGER_new());
  27491. val = -2147483647 - 1;
  27492. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27493. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27494. ASN1_INTEGER_free(a);
  27495. a = NULL;
  27496. /* long max positive */
  27497. ExpectNotNull(a = ASN1_INTEGER_new());
  27498. val = (long)(((unsigned long)-1) >> 1);
  27499. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27500. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27501. ASN1_INTEGER_free(a);
  27502. #endif
  27503. return EXPECT_RESULT();
  27504. }
  27505. #if defined(OPENSSL_EXTRA)
  27506. typedef struct ASN1IntTestVector {
  27507. const byte* der;
  27508. const size_t derSz;
  27509. const long value;
  27510. } ASN1IntTestVector;
  27511. #endif
  27512. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27513. {
  27514. EXPECT_DECLS;
  27515. #if defined(OPENSSL_EXTRA)
  27516. size_t i;
  27517. WOLFSSL_ASN1_INTEGER* a = NULL;
  27518. WOLFSSL_ASN1_INTEGER* b = NULL;
  27519. WOLFSSL_ASN1_INTEGER* c = NULL;
  27520. const byte* p = NULL;
  27521. byte* p2 = NULL;
  27522. byte* reEncoded = NULL;
  27523. int reEncodedSz = 0;
  27524. static const byte zeroDer[] = {
  27525. 0x02, 0x01, 0x00
  27526. };
  27527. static const byte oneDer[] = {
  27528. 0x02, 0x01, 0x01
  27529. };
  27530. static const byte negativeDer[] = {
  27531. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27532. };
  27533. static const byte positiveDer[] = {
  27534. 0x02, 0x03, 0x01, 0x00, 0x01
  27535. };
  27536. static const byte primeDer[] = {
  27537. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27538. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27539. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27540. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27541. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27542. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27543. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27544. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27545. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27546. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27547. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27548. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27549. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27550. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27551. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27552. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27553. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27554. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27555. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27556. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27557. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27558. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27559. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27560. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27561. };
  27562. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27563. static const ASN1IntTestVector testVectors[] = {
  27564. {zeroDer, sizeof(zeroDer), 0},
  27565. {oneDer, sizeof(oneDer), 1},
  27566. {negativeDer, sizeof(negativeDer), -4123123},
  27567. {positiveDer, sizeof(positiveDer), 65537},
  27568. {primeDer, sizeof(primeDer), 0}
  27569. };
  27570. static const size_t NUM_TEST_VECTORS =
  27571. sizeof(testVectors)/sizeof(testVectors[0]);
  27572. /* Check d2i error conditions */
  27573. /* NULL pointer to input. */
  27574. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27575. ExpectNull(b);
  27576. /* NULL input. */
  27577. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27578. ExpectNull(b);
  27579. /* 0 length. */
  27580. p = testVectors[0].der;
  27581. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27582. ExpectNull(b);
  27583. /* Negative length. */
  27584. p = testVectors[0].der;
  27585. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27586. ExpectNull(b);
  27587. /* Garbage DER input. */
  27588. p = garbageDer;
  27589. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27590. ExpectNull(b);
  27591. /* Check i2d error conditions */
  27592. /* NULL input. */
  27593. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27594. /* 0 length input data buffer (a->length == 0). */
  27595. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27596. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27597. if (a != NULL)
  27598. a->data = NULL;
  27599. /* NULL input data buffer. */
  27600. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27601. if (a != NULL) {
  27602. /* Reset a->data. */
  27603. a->data = a->intData;
  27604. }
  27605. /* Set a to valid value. */
  27606. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27607. /* NULL output buffer. */
  27608. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27609. wolfSSL_ASN1_INTEGER_free(a);
  27610. a = NULL;
  27611. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27612. p = testVectors[i].der;
  27613. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  27614. testVectors[i].derSz));
  27615. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27616. if (testVectors[i].derSz <= sizeof(long)) {
  27617. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  27618. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  27619. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27620. wolfSSL_ASN1_INTEGER_free(c);
  27621. c = NULL;
  27622. }
  27623. /* Convert to DER without a pre-allocated output buffer. */
  27624. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27625. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27626. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27627. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27628. * output buffer pointer should be incremented just past the end of the
  27629. * encoded data. */
  27630. p2 = reEncoded;
  27631. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  27632. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27633. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27634. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27635. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27636. reEncoded = NULL;
  27637. wolfSSL_ASN1_INTEGER_free(a);
  27638. a = NULL;
  27639. }
  27640. #endif /* OPENSSL_EXTRA */
  27641. return EXPECT_RESULT();
  27642. }
  27643. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27644. {
  27645. EXPECT_DECLS;
  27646. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27647. BIO* bio = NULL;
  27648. BIO* out = NULL;
  27649. BIO* fixed = NULL;
  27650. ASN1_INTEGER* ai = NULL;
  27651. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27652. char tmp[1024];
  27653. int tmpSz;
  27654. const char expected1[] = "123456";
  27655. const char expected2[] = "112345678912345678901234567890";
  27656. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27657. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27658. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27659. ExpectNotNull(ai = ASN1_INTEGER_new());
  27660. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27661. /* Invalid parameter testing. */
  27662. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27663. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27664. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27665. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27666. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27667. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27668. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27669. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27670. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27671. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27672. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27673. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27674. /* No data to read from BIO. */
  27675. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27676. /* read first line */
  27677. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27678. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27679. XMEMSET(tmp, 0, 1024);
  27680. tmpSz = BIO_read(out, tmp, 1024);
  27681. ExpectIntEQ(tmpSz, 6);
  27682. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27683. /* fail on second line (not % 2) */
  27684. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27685. /* read 3rd long line */
  27686. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27687. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27688. XMEMSET(tmp, 0, 1024);
  27689. tmpSz = BIO_read(out, tmp, 1024);
  27690. ExpectIntEQ(tmpSz, 30);
  27691. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27692. /* fail on empty line */
  27693. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27694. BIO_free(bio);
  27695. bio = NULL;
  27696. /* Make long integer, requiring dynamic memory, even longer. */
  27697. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27698. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27699. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27700. XMEMSET(tmp, 0, 1024);
  27701. tmpSz = BIO_read(out, tmp, 1024);
  27702. ExpectIntEQ(tmpSz, 48);
  27703. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27704. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27705. XMEMSET(tmp, 0, 1024);
  27706. tmpSz = BIO_read(out, tmp, 1024);
  27707. ExpectIntEQ(tmpSz, 56);
  27708. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27709. BIO_free(bio);
  27710. BIO_free(out);
  27711. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27712. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27713. /* Ensure there is 0 bytes available to write into. */
  27714. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27715. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27716. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27717. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27718. BIO_free(fixed);
  27719. ASN1_INTEGER_free(ai);
  27720. #endif
  27721. return EXPECT_RESULT();
  27722. }
  27723. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27724. {
  27725. EXPECT_DECLS;
  27726. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27727. ASN1_INTEGER *a = NULL;
  27728. unsigned char *pp = NULL,*tpp = NULL;
  27729. int ret = 0;
  27730. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27731. /* Invalid parameter testing. */
  27732. /* Set pp to an invalid value. */
  27733. pp = NULL;
  27734. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27735. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27736. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27737. /* 40 */
  27738. if (a != NULL) {
  27739. a->intData[0] = ASN_INTEGER;
  27740. a->intData[1] = 1;
  27741. a->intData[2] = 40;
  27742. }
  27743. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27744. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27745. DYNAMIC_TYPE_TMP_BUFFER));
  27746. tpp = pp;
  27747. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27748. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27749. tpp--;
  27750. ExpectIntEQ(*tpp, 40);
  27751. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27752. pp = NULL;
  27753. /* 128 */
  27754. if (a != NULL) {
  27755. a->intData[0] = ASN_INTEGER;
  27756. a->intData[1] = 1;
  27757. a->intData[2] = 128;
  27758. }
  27759. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27760. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27761. DYNAMIC_TYPE_TMP_BUFFER));
  27762. tpp = pp;
  27763. if (tpp != NULL) {
  27764. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27765. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27766. tpp--;
  27767. ExpectIntEQ(*(tpp--), 128);
  27768. ExpectIntEQ(*tpp, 0);
  27769. }
  27770. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27771. pp = NULL;
  27772. /* -40 */
  27773. if (a != NULL) {
  27774. a->intData[0] = ASN_INTEGER;
  27775. a->intData[1] = 1;
  27776. a->intData[2] = 40;
  27777. a->negative = 1;
  27778. }
  27779. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27780. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27781. DYNAMIC_TYPE_TMP_BUFFER));
  27782. tpp = pp;
  27783. if (tpp != NULL) {
  27784. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27785. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27786. tpp--;
  27787. ExpectIntEQ(*tpp, 216);
  27788. }
  27789. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27790. pp = NULL;
  27791. /* -128 */
  27792. if (a != NULL) {
  27793. a->intData[0] = ASN_INTEGER;
  27794. a->intData[1] = 1;
  27795. a->intData[2] = 128;
  27796. a->negative = 1;
  27797. }
  27798. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27799. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27800. DYNAMIC_TYPE_TMP_BUFFER));
  27801. tpp = pp;
  27802. if (tpp != NULL) {
  27803. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27804. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27805. tpp--;
  27806. ExpectIntEQ(*tpp, 128);
  27807. }
  27808. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27809. pp = NULL;
  27810. /* -200 */
  27811. if (a != NULL) {
  27812. a->intData[0] = ASN_INTEGER;
  27813. a->intData[1] = 1;
  27814. a->intData[2] = 200;
  27815. a->negative = 1;
  27816. }
  27817. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27818. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27819. DYNAMIC_TYPE_TMP_BUFFER));
  27820. tpp = pp;
  27821. if (tpp != NULL) {
  27822. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27823. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27824. tpp--;
  27825. ExpectIntEQ(*(tpp--), 56);
  27826. ExpectIntEQ(*tpp, 255);
  27827. }
  27828. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27829. pp = NULL;
  27830. /* Empty */
  27831. if (a != NULL) {
  27832. a->intData[0] = ASN_INTEGER;
  27833. a->intData[1] = 0;
  27834. a->negative = 0;
  27835. }
  27836. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27837. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27838. DYNAMIC_TYPE_TMP_BUFFER));
  27839. tpp = pp;
  27840. if (tpp != NULL) {
  27841. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27842. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27843. tpp--;
  27844. ExpectIntEQ(*tpp, 0);
  27845. }
  27846. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27847. pp = NULL;
  27848. /* 0 */
  27849. if (a != NULL) {
  27850. a->intData[0] = ASN_INTEGER;
  27851. a->intData[1] = 1;
  27852. a->intData[2] = 0;
  27853. a->negative = 1;
  27854. }
  27855. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27856. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27857. DYNAMIC_TYPE_TMP_BUFFER));
  27858. if (tpp != NULL) {
  27859. tpp = pp;
  27860. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27861. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27862. tpp--;
  27863. ExpectIntEQ(*tpp, 0);
  27864. }
  27865. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27866. pp = NULL;
  27867. /* 0x100 */
  27868. if (a != NULL) {
  27869. a->intData[0] = ASN_INTEGER;
  27870. a->intData[1] = 2;
  27871. a->intData[2] = 0x01;
  27872. a->intData[3] = 0x00;
  27873. a->negative = 0;
  27874. }
  27875. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27876. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27877. DYNAMIC_TYPE_TMP_BUFFER));
  27878. if (tpp != NULL) {
  27879. tpp = pp;
  27880. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27881. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27882. tpp -= 2;
  27883. ExpectIntEQ(tpp[0], 0x01);
  27884. ExpectIntEQ(tpp[1], 0x00);
  27885. }
  27886. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27887. pp = NULL;
  27888. /* -0x8000 => 0x8000 */
  27889. if (a != NULL) {
  27890. a->intData[0] = ASN_INTEGER;
  27891. a->intData[1] = 2;
  27892. a->intData[2] = 0x80;
  27893. a->intData[3] = 0x00;
  27894. a->negative = 1;
  27895. }
  27896. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27897. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27898. DYNAMIC_TYPE_TMP_BUFFER));
  27899. tpp = pp;
  27900. if (tpp != NULL) {
  27901. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27902. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27903. tpp -= 2;
  27904. ExpectIntEQ(tpp[0], 0x80);
  27905. ExpectIntEQ(tpp[1], 0x00);
  27906. }
  27907. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27908. pp = NULL;
  27909. /* -0x8001 => 0xFF7FFF */
  27910. if (a != NULL) {
  27911. a->intData[0] = ASN_INTEGER;
  27912. a->intData[1] = 2;
  27913. a->intData[2] = 0x80;
  27914. a->intData[3] = 0x01;
  27915. a->negative = 1;
  27916. }
  27917. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27918. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27919. DYNAMIC_TYPE_TMP_BUFFER));
  27920. tpp = pp;
  27921. if (tpp != NULL) {
  27922. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27923. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27924. tpp -= 3;
  27925. ExpectIntEQ(tpp[0], 0xFF);
  27926. ExpectIntEQ(tpp[1], 0x7F);
  27927. ExpectIntEQ(tpp[2], 0xFF);
  27928. }
  27929. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27930. wolfSSL_ASN1_INTEGER_free(a);
  27931. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27932. return EXPECT_RESULT();
  27933. }
  27934. static int test_wolfSSL_ASN1_OBJECT(void)
  27935. {
  27936. EXPECT_DECLS;
  27937. #if defined(OPENSSL_EXTRA)
  27938. ASN1_OBJECT* a = NULL;
  27939. ASN1_OBJECT s;
  27940. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27941. /* Invalid parameter testing. */
  27942. ASN1_OBJECT_free(NULL);
  27943. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27944. /* Test that a static ASN1_OBJECT can be freed. */
  27945. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27946. ASN1_OBJECT_free(&s);
  27947. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27948. ASN1_OBJECT_free(a);
  27949. a = NULL;
  27950. s.obj = der;
  27951. s.objSz = sizeof(der);
  27952. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27953. ASN1_OBJECT_free(a);
  27954. ASN1_OBJECT_free(&s);
  27955. #endif /* OPENSSL_EXTRA */
  27956. return EXPECT_RESULT();
  27957. }
  27958. static int test_wolfSSL_ASN1_get_object(void)
  27959. {
  27960. EXPECT_DECLS;
  27961. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27962. const unsigned char* derBuf = cliecc_cert_der_256;
  27963. const unsigned char* nullPtr = NULL;
  27964. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27965. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27966. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27967. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27968. const unsigned char* p;
  27969. unsigned char objDer[8];
  27970. unsigned char* der;
  27971. unsigned char* derPtr;
  27972. int len = sizeof_cliecc_cert_der_256;
  27973. long asnLen = 0;
  27974. int tag = 0;
  27975. int cls = 0;
  27976. ASN1_OBJECT* a = NULL;
  27977. ASN1_OBJECT s;
  27978. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27979. /* Invalid encoding at length. */
  27980. p = objDerInvalidLen;
  27981. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27982. 0x80);
  27983. p = objDerBadLen;
  27984. /* Error = 0x80, Constructed = 0x20 */
  27985. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27986. 0x80 | 0x20);
  27987. /* Read a couple TLV triplets and make sure they match the expected values
  27988. */
  27989. /* SEQUENCE */
  27990. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27991. ExpectIntEQ(asnLen, 861);
  27992. ExpectIntEQ(tag, 0x10);
  27993. ExpectIntEQ(cls, 0);
  27994. /* SEQUENCE */
  27995. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27996. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27997. ExpectIntEQ(asnLen, 772);
  27998. ExpectIntEQ(tag, 0x10);
  27999. ExpectIntEQ(cls, 0);
  28000. /* [0] */
  28001. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28002. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28003. ExpectIntEQ(asnLen, 3);
  28004. ExpectIntEQ(tag, 0);
  28005. ExpectIntEQ(cls, 0x80);
  28006. /* INTEGER */
  28007. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28008. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28009. ExpectIntEQ(asnLen, 1);
  28010. ExpectIntEQ(tag, 0x2);
  28011. ExpectIntEQ(cls, 0);
  28012. derBuf += asnLen;
  28013. /* INTEGER */
  28014. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28015. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28016. ExpectIntEQ(asnLen, 20);
  28017. ExpectIntEQ(tag, 0x2);
  28018. ExpectIntEQ(cls, 0);
  28019. derBuf += asnLen;
  28020. /* SEQUENCE */
  28021. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28022. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28023. ExpectIntEQ(asnLen, 10);
  28024. ExpectIntEQ(tag, 0x10);
  28025. ExpectIntEQ(cls, 0);
  28026. /* Found OBJECT_ID. */
  28027. /* Invalid parameter testing. */
  28028. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  28029. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  28030. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  28031. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  28032. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  28033. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  28034. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  28035. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  28036. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  28037. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  28038. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28039. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28040. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  28041. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  28042. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  28043. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  28044. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  28045. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28046. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28047. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  28048. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  28049. /* Invalid encoding at length. */
  28050. p = objDerInvalidLen;
  28051. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  28052. p = objDerBadLen;
  28053. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  28054. p = objDerNotObj;
  28055. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  28056. p = objDerNoData;
  28057. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  28058. /* Create an ASN OBJECT from content */
  28059. p = derBuf + 2;
  28060. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  28061. ASN1_OBJECT_free(a);
  28062. a = NULL;
  28063. /* Create an ASN OBJECT from DER */
  28064. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  28065. /* Invalid parameter testing. */
  28066. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  28067. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  28068. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  28069. der = NULL;
  28070. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  28071. derPtr = objDer;
  28072. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  28073. ExpectPtrNE(derPtr, objDer);
  28074. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  28075. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28076. ASN1_OBJECT_free(a);
  28077. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  28078. return EXPECT_RESULT();
  28079. }
  28080. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  28081. {
  28082. EXPECT_DECLS;
  28083. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28084. ASN1_OBJECT* obj = NULL;
  28085. ASN1_OBJECT* a = NULL;
  28086. BIO *bio = NULL;
  28087. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  28088. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  28089. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  28090. const unsigned char* p;
  28091. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  28092. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  28093. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  28094. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  28095. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  28096. /* No DER encoding in ASN1_OBJECT. */
  28097. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  28098. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28099. ASN1_OBJECT_free(a);
  28100. a = NULL;
  28101. /* DER encoding - not OBJECT_ID */
  28102. p = notObjDer;
  28103. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28104. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28105. ASN1_OBJECT_free(a);
  28106. a = NULL;
  28107. /* Bad length encoding. */
  28108. p = badLenDer;
  28109. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28110. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28111. ASN1_OBJECT_free(a);
  28112. a = NULL;
  28113. /* Good encoding - but unknown. */
  28114. p = goodDer;
  28115. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28116. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28117. ASN1_OBJECT_free(a);
  28118. BIO_free(bio);
  28119. ASN1_OBJECT_free(obj);
  28120. #endif
  28121. return EXPECT_RESULT();
  28122. }
  28123. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  28124. {
  28125. EXPECT_DECLS;
  28126. #if defined(OPENSSL_EXTRA) && \
  28127. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  28128. char buf[50] = {0};
  28129. ASN1_OBJECT* obj;
  28130. const char* oid = "2.5.29.19";
  28131. const char* ln = "X509v3 Basic Constraints";
  28132. obj = NULL;
  28133. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  28134. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  28135. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  28136. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  28137. XMEMSET(buf, 0, sizeof(buf));
  28138. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  28139. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  28140. ASN1_OBJECT_free(obj);
  28141. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  28142. return EXPECT_RESULT();
  28143. }
  28144. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  28145. {
  28146. EXPECT_DECLS;
  28147. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  28148. WOLFSSL_STACK* sk = NULL;
  28149. WOLFSSL_ASN1_OBJECT* obj;
  28150. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28151. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28152. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28153. sk = NULL;
  28154. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28155. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  28156. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  28157. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  28158. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28159. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  28160. sk = NULL;
  28161. /* obj freed in pop_free call. */
  28162. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28163. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28164. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28165. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  28166. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28167. wolfSSL_ASN1_OBJECT_free(obj);
  28168. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  28169. return EXPECT_RESULT();
  28170. }
  28171. static int test_wolfSSL_ASN1_STRING(void)
  28172. {
  28173. EXPECT_DECLS;
  28174. #if defined(OPENSSL_EXTRA)
  28175. ASN1_STRING* str = NULL;
  28176. ASN1_STRING* c = NULL;
  28177. const char data[] = "hello wolfSSL";
  28178. const char data2[] = "Same len data";
  28179. const char longData[] =
  28180. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28181. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28182. ASN1_STRING_free(str);
  28183. str = NULL;
  28184. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28185. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28186. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  28187. /* Check setting to NULL works. */
  28188. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  28189. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28190. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28191. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28192. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  28193. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  28194. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  28195. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  28196. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  28197. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  28198. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  28199. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  28200. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  28201. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28202. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  28203. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  28204. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  28205. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  28206. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28207. /* Check setting back to smaller size frees dynamic data. */
  28208. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28209. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  28210. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  28211. ExpectNull(ASN1_STRING_get0_data(NULL));
  28212. ExpectNotNull(ASN1_STRING_get0_data(str));
  28213. ExpectNull(ASN1_STRING_data(NULL));
  28214. ExpectNotNull(ASN1_STRING_data(str));
  28215. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  28216. ExpectIntGT(ASN1_STRING_length(str), 0);
  28217. ASN1_STRING_free(c);
  28218. ASN1_STRING_free(str);
  28219. ASN1_STRING_free(NULL);
  28220. #ifndef NO_WOLFSSL_STUB
  28221. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  28222. #endif
  28223. #endif
  28224. return EXPECT_RESULT();
  28225. }
  28226. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  28227. {
  28228. EXPECT_DECLS;
  28229. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  28230. !defined(NO_FILESYSTEM)
  28231. WOLFSSL_X509* x509 = NULL;
  28232. WOLFSSL_X509_NAME* subject = NULL;
  28233. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  28234. WOLFSSL_ASN1_STRING* a = NULL;
  28235. FILE* file = XBADFILE;
  28236. int idx = 0;
  28237. char targetOutput[16] = "www.wolfssl.com";
  28238. unsigned char* actual_output = NULL;
  28239. int len = 0;
  28240. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  28241. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  28242. if (file != NULL)
  28243. fclose(file);
  28244. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  28245. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  28246. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  28247. NID_commonName, -1)), 5);
  28248. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  28249. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  28250. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  28251. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, (size_t)len), 0);
  28252. a = NULL;
  28253. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  28254. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28255. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28256. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28257. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28258. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28259. wolfSSL_X509_free(x509);
  28260. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28261. ExpectNotNull(a = ASN1_STRING_new());
  28262. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28263. ASN1_STRING_free(a);
  28264. #endif
  28265. return EXPECT_RESULT();
  28266. }
  28267. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28268. {
  28269. EXPECT_DECLS;
  28270. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28271. WOLFSSL_ASN1_STRING* str = NULL;
  28272. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28273. char* ret = NULL;
  28274. ExpectNotNull(str = ASN1_STRING_new());
  28275. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28276. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28277. ret = NULL;
  28278. /* No data. */
  28279. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28280. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28281. ret = NULL;
  28282. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28283. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28284. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28285. ret = NULL;
  28286. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28287. /* No type. */
  28288. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28289. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28290. ASN1_STRING_free(str);
  28291. #endif
  28292. return EXPECT_RESULT();
  28293. }
  28294. static int test_wolfSSL_ASN1_STRING_canon(void)
  28295. {
  28296. EXPECT_DECLS;
  28297. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28298. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28299. defined(OPENSSL_EXTRA_X509_SMALL))
  28300. WOLFSSL_ASN1_STRING* orig = NULL;
  28301. WOLFSSL_ASN1_STRING* canon = NULL;
  28302. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28303. const char* whitespaceOnly = "\t\r\n";
  28304. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28305. const char* canonData = "\x01 \x02 \xff tt";
  28306. const char longData[] =
  28307. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28308. ExpectNotNull(orig = ASN1_STRING_new());
  28309. ExpectNotNull(canon = ASN1_STRING_new());
  28310. /* Invalid parameter testing. */
  28311. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28312. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28313. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28314. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28315. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28316. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28317. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28318. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28319. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28320. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28321. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28322. ASN1_STRING_free(orig);
  28323. orig = NULL;
  28324. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28325. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28326. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28327. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28328. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28329. ASN1_STRING_free(orig);
  28330. orig = NULL;
  28331. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28332. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28333. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28334. ASN1_STRING_free(orig);
  28335. orig = NULL;
  28336. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28337. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28338. ASN1_STRING_free(orig);
  28339. ASN1_STRING_free(canon);
  28340. #endif
  28341. #endif
  28342. return EXPECT_RESULT();
  28343. }
  28344. static int test_wolfSSL_ASN1_STRING_print(void)
  28345. {
  28346. EXPECT_DECLS;
  28347. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28348. !defined(NO_BIO)
  28349. ASN1_STRING* asnStr = NULL;
  28350. const char HELLO_DATA[]= \
  28351. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28352. #define MAX_UNPRINTABLE_CHAR 32
  28353. #define MAX_BUF 255
  28354. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28355. unsigned char expected[sizeof(unprintableData)+1];
  28356. unsigned char rbuf[MAX_BUF];
  28357. BIO *bio = NULL;
  28358. int p_len;
  28359. int i;
  28360. /* setup */
  28361. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28362. unprintableData[i] = (unsigned char)HELLO_DATA[i];
  28363. expected[i] = (unsigned char)HELLO_DATA[i];
  28364. }
  28365. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28366. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28367. if (i == (int)'\n' || i == (int)'\r')
  28368. expected[sizeof(HELLO_DATA)+i] = i;
  28369. else
  28370. expected[sizeof(HELLO_DATA)+i] = '.';
  28371. }
  28372. unprintableData[sizeof(unprintableData)-1] = '\0';
  28373. expected[sizeof(expected)-1] = '\0';
  28374. XMEMSET(rbuf, 0, MAX_BUF);
  28375. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28376. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  28377. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28378. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28379. (int)sizeof(unprintableData)), 1);
  28380. /* test */
  28381. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28382. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28383. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28384. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28385. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  28386. ExpectStrEQ((char*)rbuf, (const char*)expected);
  28387. BIO_free(bio);
  28388. bio = NULL;
  28389. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28390. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28391. /* Ensure there is 0 bytes available to write into. */
  28392. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  28393. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28394. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28395. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28396. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28397. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28398. BIO_free(bio);
  28399. ASN1_STRING_free(asnStr);
  28400. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28401. return EXPECT_RESULT();
  28402. }
  28403. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28404. {
  28405. EXPECT_DECLS;
  28406. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28407. ASN1_STRING* asn_str = NULL;
  28408. const char data[] = "Hello wolfSSL!";
  28409. ASN1_STRING* esc_str = NULL;
  28410. const char esc_data[] = "a+;<>";
  28411. ASN1_STRING* neg_int = NULL;
  28412. const char neg_int_data[] = "\xff";
  28413. ASN1_STRING* neg_enum = NULL;
  28414. const char neg_enum_data[] = "\xff";
  28415. BIO *bio = NULL;
  28416. BIO *fixed = NULL;
  28417. unsigned long flags;
  28418. int p_len;
  28419. unsigned char rbuf[255];
  28420. /* setup */
  28421. XMEMSET(rbuf, 0, 255);
  28422. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28423. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  28424. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28425. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28426. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  28427. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28428. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  28429. sizeof(esc_data)), 1);
  28430. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  28431. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28432. sizeof(neg_int_data) - 1), 1);
  28433. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  28434. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28435. sizeof(neg_enum_data) - 1), 1);
  28436. /* Invalid parameter testing. */
  28437. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28438. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28439. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28440. /* no flags */
  28441. XMEMSET(rbuf, 0, 255);
  28442. flags = 0;
  28443. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  28444. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  28445. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  28446. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28447. /* Ensure there is 0 bytes available to write into. */
  28448. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28449. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28450. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28451. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28452. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28453. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28454. /* RFC2253 Escape */
  28455. XMEMSET(rbuf, 0, 255);
  28456. flags = ASN1_STRFLGS_ESC_2253;
  28457. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  28458. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  28459. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28460. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28461. /* Ensure there is 0 bytes available to write into. */
  28462. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28463. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28464. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28465. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28466. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28467. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28468. /* Show type */
  28469. XMEMSET(rbuf, 0, 255);
  28470. flags = ASN1_STRFLGS_SHOW_TYPE;
  28471. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  28472. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  28473. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28474. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28475. /* Ensure there is 0 bytes available to write into. */
  28476. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28477. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28478. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28479. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28480. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28481. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28482. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28483. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28484. /* Dump All */
  28485. XMEMSET(rbuf, 0, 255);
  28486. flags = ASN1_STRFLGS_DUMP_ALL;
  28487. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  28488. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  28489. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28490. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28491. /* Ensure there is 0 bytes available to write into. */
  28492. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28493. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28494. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28495. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28496. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28497. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28498. /* Dump Der */
  28499. XMEMSET(rbuf, 0, 255);
  28500. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28501. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  28502. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  28503. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28504. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28505. /* Ensure there is 0 bytes available to write into. */
  28506. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28507. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28508. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28509. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28510. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28511. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28512. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28513. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28514. /* Dump All + Show type */
  28515. XMEMSET(rbuf, 0, 255);
  28516. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28517. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  28518. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  28519. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28520. /* Dump All + Show type - Negative Integer. */
  28521. XMEMSET(rbuf, 0, 255);
  28522. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28523. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  28524. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  28525. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  28526. /* Dump All + Show type - Negative Enumerated. */
  28527. XMEMSET(rbuf, 0, 255);
  28528. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28529. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  28530. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  28531. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28532. BIO_free(fixed);
  28533. BIO_free(bio);
  28534. ASN1_STRING_free(asn_str);
  28535. ASN1_STRING_free(esc_str);
  28536. ASN1_STRING_free(neg_int);
  28537. ASN1_STRING_free(neg_enum);
  28538. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28539. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28540. #endif
  28541. return EXPECT_RESULT();
  28542. }
  28543. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28544. {
  28545. EXPECT_DECLS;
  28546. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28547. ASN1_STRING* asn1str_test = NULL;
  28548. ASN1_STRING* asn1str_answer = NULL;
  28549. /* Each character is encoded using 4 bytes */
  28550. char input[] = {
  28551. 0, 0, 0, 'T',
  28552. 0, 0, 0, 'e',
  28553. 0, 0, 0, 's',
  28554. 0, 0, 0, 't',
  28555. };
  28556. char output[] = "Test";
  28557. char badInput[] = {
  28558. 1, 0, 0, 'T',
  28559. 0, 1, 0, 'e',
  28560. 0, 0, 1, 's',
  28561. };
  28562. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28563. /* Test wrong type. */
  28564. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28565. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28566. ASN1_STRING_free(asn1str_test);
  28567. asn1str_test = NULL;
  28568. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28569. /* Test bad length. */
  28570. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28571. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28572. /* Test bad input. */
  28573. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28574. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28575. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28576. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28577. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28578. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28579. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28580. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28581. ExpectNotNull(
  28582. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28583. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28584. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28585. ASN1_STRING_free(asn1str_test);
  28586. ASN1_STRING_free(asn1str_answer);
  28587. #endif /* OPENSSL_ALL && !NO_ASN */
  28588. return EXPECT_RESULT();
  28589. }
  28590. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28591. {
  28592. EXPECT_DECLS;
  28593. #if defined(OPENSSL_EXTRA)
  28594. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  28595. ExpectNotNull(asn1_gtime = ASN1_GENERALIZEDTIME_new());
  28596. if (asn1_gtime != NULL)
  28597. XMEMCPY(asn1_gtime->data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28598. ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28599. #endif /* OPENSSL_EXTRA */
  28600. return EXPECT_RESULT();
  28601. }
  28602. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28603. {
  28604. EXPECT_DECLS;
  28605. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28606. WOLFSSL_ASN1_GENERALIZEDTIME* gtime = NULL;
  28607. BIO* bio = NULL;
  28608. unsigned char buf[24];
  28609. int i;
  28610. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28611. BIO_set_write_buf_size(bio, 24);
  28612. ExpectNotNull(gtime = ASN1_GENERALIZEDTIME_new());
  28613. /* Type not set. */
  28614. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28615. ExpectIntEQ(wolfSSL_ASN1_TIME_set_string(gtime, "20180504123500Z"), 1);
  28616. /* Invalid parameters testing. */
  28617. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28618. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28619. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, gtime), BAD_FUNC_ARG);
  28620. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 1);
  28621. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28622. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28623. BIO_free(bio);
  28624. bio = NULL;
  28625. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28626. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28627. /* Ensure there is 0 bytes available to write into. */
  28628. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28629. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28630. for (i = 1; i < 20; i++) {
  28631. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28632. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28633. }
  28634. BIO_free(bio);
  28635. wolfSSL_ASN1_GENERALIZEDTIME_free(gtime);
  28636. #endif /* OPENSSL_EXTRA */
  28637. return EXPECT_RESULT();
  28638. }
  28639. static int test_wolfSSL_ASN1_TIME(void)
  28640. {
  28641. EXPECT_DECLS;
  28642. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28643. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28644. unsigned char *data;
  28645. ExpectNotNull(asn_time = ASN1_TIME_new());
  28646. #ifndef NO_WOLFSSL_STUB
  28647. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28648. #endif
  28649. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28650. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28651. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28652. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28653. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28654. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28655. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28656. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28657. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28658. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28659. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28660. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28661. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28662. ASN1_TIME_free(asn_time);
  28663. ASN1_TIME_free(NULL);
  28664. #endif
  28665. return EXPECT_RESULT();
  28666. }
  28667. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28668. {
  28669. EXPECT_DECLS;
  28670. #ifndef NO_ASN_TIME
  28671. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28672. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28673. WOLFSSL_ASN1_TIME* t = NULL;
  28674. char buf[ASN_GENERALIZED_TIME_SIZE];
  28675. ExpectNotNull((t = ASN1_TIME_new()));
  28676. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28677. /* Invalid parameter testing. */
  28678. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28679. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28680. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28681. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28682. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28683. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28684. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28685. /* Buffer needs to be longer than minimum of 5 characters. */
  28686. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28687. ASN1_TIME_free(t);
  28688. #endif
  28689. #endif /* NO_ASN_TIME */
  28690. return EXPECT_RESULT();
  28691. }
  28692. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28693. {
  28694. EXPECT_DECLS;
  28695. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28696. ASN1_TIME* fromTime = NULL;
  28697. ASN1_TIME* closeToTime = NULL;
  28698. ASN1_TIME* toTime = NULL;
  28699. ASN1_TIME* invalidTime = NULL;
  28700. int daysDiff = 0;
  28701. int secsDiff = 0;
  28702. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28703. /* Feb 22, 2003, 21:15:15 */
  28704. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28705. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28706. /* Feb 22, 2003, 21:16:15 */
  28707. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28708. ExpectNotNull((toTime = ASN1_TIME_new()));
  28709. /* Dec 19, 2010, 18:10:11 */
  28710. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28711. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28712. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28713. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28714. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28715. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28716. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28717. /* Error conditions. */
  28718. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28719. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28720. /* If both times are NULL, difference is 0. */
  28721. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28722. ExpectIntEQ(daysDiff, 0);
  28723. ExpectIntEQ(secsDiff, 0);
  28724. /* If one time is NULL, it defaults to the current time. */
  28725. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28726. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28727. /* Normal operation. Both times non-NULL. */
  28728. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28729. ExpectIntEQ(daysDiff, 2856);
  28730. ExpectIntEQ(secsDiff, 75296);
  28731. /* Swapping the times should return negative values. */
  28732. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28733. ExpectIntEQ(daysDiff, -2856);
  28734. ExpectIntEQ(secsDiff, -75296);
  28735. /* Compare with invalid time string. */
  28736. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28737. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28738. /* Compare with days difference of 0. */
  28739. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28740. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28741. /* Days and seconds differences not 0. */
  28742. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28743. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28744. /* Same time. */
  28745. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28746. /* Compare regression test: No seconds difference, just difference in days.
  28747. */
  28748. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28749. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28750. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28751. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28752. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28753. /* Edge case with Unix epoch. */
  28754. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28755. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28756. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28757. ExpectIntEQ(daysDiff, 3652);
  28758. ExpectIntEQ(secsDiff, 0);
  28759. /* Edge case with year > 2038 (year 2038 problem). */
  28760. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28761. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28762. ExpectIntEQ(daysDiff, 2932896);
  28763. ExpectIntEQ(secsDiff, 86399);
  28764. ASN1_TIME_free(fromTime);
  28765. ASN1_TIME_free(closeToTime);
  28766. ASN1_TIME_free(toTime);
  28767. ASN1_TIME_free(invalidTime);
  28768. #endif
  28769. return EXPECT_RESULT();
  28770. }
  28771. static int test_wolfSSL_ASN1_TIME_adj(void)
  28772. {
  28773. EXPECT_DECLS;
  28774. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28775. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28776. const int year = 365*24*60*60;
  28777. const int day = 24*60*60;
  28778. const int hour = 60*60;
  28779. const int mini = 60;
  28780. const byte asn_utc_time = ASN_UTC_TIME;
  28781. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28782. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28783. #endif
  28784. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28785. WOLFSSL_ASN1_TIME* s = NULL;
  28786. int offset_day;
  28787. long offset_sec;
  28788. char date_str[CTC_DATE_SIZE + 1];
  28789. time_t t;
  28790. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28791. /* UTC notation test */
  28792. /* 2000/2/15 20:30:00 */
  28793. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28794. offset_day = 7;
  28795. offset_sec = 45 * mini;
  28796. /* offset_sec = -45 * min;*/
  28797. ExpectNotNull(asn_time =
  28798. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28799. ExpectTrue(asn_time->type == asn_utc_time);
  28800. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28801. CTC_DATE_SIZE));
  28802. date_str[CTC_DATE_SIZE] = '\0';
  28803. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28804. /* negative offset */
  28805. offset_sec = -45 * mini;
  28806. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28807. ExpectNotNull(asn_time);
  28808. ExpectTrue(asn_time->type == asn_utc_time);
  28809. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28810. CTC_DATE_SIZE));
  28811. date_str[CTC_DATE_SIZE] = '\0';
  28812. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28813. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28814. s = NULL;
  28815. XMEMSET(date_str, 0, sizeof(date_str));
  28816. /* Generalized time will overflow time_t if not long */
  28817. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28818. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28819. DYNAMIC_TYPE_OPENSSL);
  28820. /* GeneralizedTime notation test */
  28821. /* 2055/03/01 09:00:00 */
  28822. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28823. offset_day = 12;
  28824. offset_sec = 10 * mini;
  28825. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28826. offset_sec));
  28827. ExpectTrue(asn_time->type == asn_gen_time);
  28828. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28829. CTC_DATE_SIZE));
  28830. date_str[CTC_DATE_SIZE] = '\0';
  28831. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28832. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28833. s = NULL;
  28834. XMEMSET(date_str, 0, sizeof(date_str));
  28835. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28836. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28837. s = NULL;
  28838. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28839. offset_day = 7;
  28840. offset_sec = 45 * mini;
  28841. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28842. offset_sec));
  28843. ExpectTrue(asn_time->type == asn_utc_time);
  28844. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28845. CTC_DATE_SIZE));
  28846. date_str[CTC_DATE_SIZE] = '\0';
  28847. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28848. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28849. asn_time = NULL;
  28850. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28851. offset_sec));
  28852. ExpectTrue(asn_time->type == asn_utc_time);
  28853. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28854. CTC_DATE_SIZE));
  28855. date_str[CTC_DATE_SIZE] = '\0';
  28856. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28857. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28858. #endif
  28859. return EXPECT_RESULT();
  28860. }
  28861. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28862. {
  28863. EXPECT_DECLS;
  28864. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28865. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28866. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28867. ASN1_TIME asnTime;
  28868. struct tm tm;
  28869. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28870. XMEMSET(&tm, 0, sizeof(struct tm));
  28871. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28872. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28873. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28874. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28875. ExpectIntEQ(tm.tm_sec, 15);
  28876. ExpectIntEQ(tm.tm_min, 15);
  28877. ExpectIntEQ(tm.tm_hour, 21);
  28878. ExpectIntEQ(tm.tm_mday, 22);
  28879. ExpectIntEQ(tm.tm_mon, 1);
  28880. ExpectIntEQ(tm.tm_year, 100);
  28881. ExpectIntEQ(tm.tm_isdst, 0);
  28882. #ifdef XMKTIME
  28883. ExpectIntEQ(tm.tm_wday, 2);
  28884. ExpectIntEQ(tm.tm_yday, 52);
  28885. #endif
  28886. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28887. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28888. ExpectIntEQ(tm.tm_year, 50);
  28889. /* Get current time. */
  28890. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28891. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28892. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28893. /* 0 length. */
  28894. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28895. /* No type. */
  28896. asnTime.length = 1;
  28897. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28898. /* Not UTCTIME length. */
  28899. asnTime.type = V_ASN1_UTCTIME;
  28900. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28901. /* Not GENERALIZEDTIME length. */
  28902. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28903. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28904. /* Not Zulu timezone. */
  28905. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28906. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28907. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28908. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28909. #ifdef XMKTIME
  28910. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28911. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28912. ExpectIntEQ(tm.tm_sec, 47);
  28913. ExpectIntEQ(tm.tm_min, 22);
  28914. ExpectIntEQ(tm.tm_hour, 21);
  28915. ExpectIntEQ(tm.tm_mday, 12);
  28916. ExpectIntEQ(tm.tm_mon, 4);
  28917. ExpectIntEQ(tm.tm_year, 123);
  28918. ExpectIntEQ(tm.tm_wday, 5);
  28919. ExpectIntEQ(tm.tm_yday, 131);
  28920. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28921. fields are zeroed out as expected. */
  28922. ExpectIntEQ(tm.tm_isdst, 0);
  28923. #endif
  28924. #endif
  28925. return EXPECT_RESULT();
  28926. }
  28927. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28928. {
  28929. EXPECT_DECLS;
  28930. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28931. WOLFSSL_ASN1_TIME *t = NULL;
  28932. WOLFSSL_ASN1_TIME *out = NULL;
  28933. WOLFSSL_ASN1_TIME *gtime = NULL;
  28934. int tlen = 0;
  28935. unsigned char *data = NULL;
  28936. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28937. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28938. /* type not set. */
  28939. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28940. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28941. t = NULL;
  28942. /* UTC Time test */
  28943. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28944. if (t != NULL) {
  28945. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28946. t->type = ASN_UTC_TIME;
  28947. t->length = ASN_UTC_TIME_SIZE;
  28948. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28949. }
  28950. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28951. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28952. out = NULL;
  28953. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28954. wolfSSL_ASN1_TIME_free(gtime);
  28955. gtime = NULL;
  28956. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28957. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28958. ExpectPtrEq(gtime, out);
  28959. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28960. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28961. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28962. /* Generalized Time test */
  28963. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28964. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28965. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28966. if (t != NULL) {
  28967. t->type = ASN_GENERALIZED_TIME;
  28968. t->length = ASN_GENERALIZED_TIME_SIZE;
  28969. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28970. }
  28971. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28972. ASN_GENERALIZED_TIME_SIZE);
  28973. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28974. "20050727123456Z");
  28975. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28976. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28977. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28978. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28979. /* UTC Time to Generalized Time 1900's test */
  28980. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28981. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28982. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28983. if (t != NULL) {
  28984. t->type = ASN_UTC_TIME;
  28985. t->length = ASN_UTC_TIME_SIZE;
  28986. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28987. }
  28988. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28989. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28990. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28991. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28992. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28993. /* Null parameter test */
  28994. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28995. gtime = NULL;
  28996. out = NULL;
  28997. if (t != NULL) {
  28998. t->type = ASN_UTC_TIME;
  28999. t->length = ASN_UTC_TIME_SIZE;
  29000. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  29001. }
  29002. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  29003. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29004. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29005. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  29006. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29007. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29008. #endif
  29009. return EXPECT_RESULT();
  29010. }
  29011. static int test_wolfSSL_ASN1_TIME_print(void)
  29012. {
  29013. EXPECT_DECLS;
  29014. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  29015. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  29016. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  29017. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  29018. !defined(NO_ASN_TIME)
  29019. BIO* bio = NULL;
  29020. BIO* fixed = NULL;
  29021. X509* x509 = NULL;
  29022. const unsigned char* der = client_cert_der_2048;
  29023. ASN1_TIME* notAfter = NULL;
  29024. ASN1_TIME* notBefore = NULL;
  29025. unsigned char buf[25];
  29026. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29027. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29028. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  29029. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  29030. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  29031. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  29032. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  29033. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  29034. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  29035. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29036. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  29037. /* Test BIO_write fails. */
  29038. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29039. /* Ensure there is 0 bytes available to write into. */
  29040. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  29041. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29042. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29043. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29044. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  29045. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29046. /* create a bad time and test results */
  29047. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  29048. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  29049. if (EXPECT_SUCCESS()) {
  29050. notAfter->data[8] = 0;
  29051. notAfter->data[3] = 0;
  29052. }
  29053. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  29054. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29055. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29056. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  29057. BIO_free(bio);
  29058. BIO_free(fixed);
  29059. X509_free(x509);
  29060. #endif
  29061. return EXPECT_RESULT();
  29062. }
  29063. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  29064. {
  29065. EXPECT_DECLS;
  29066. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  29067. BIO* bio = NULL;
  29068. ASN1_UTCTIME* utc = NULL;
  29069. unsigned char buf[25];
  29070. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  29071. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  29072. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  29073. /* Valid date */
  29074. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29075. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  29076. DYNAMIC_TYPE_ASN1));
  29077. if (utc != NULL) {
  29078. utc->type = ASN_UTC_TIME;
  29079. utc->length = ASN_UTC_TIME_SIZE;
  29080. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  29081. }
  29082. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  29083. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  29084. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  29085. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  29086. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29087. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  29088. XMEMSET(buf, 0, sizeof(buf));
  29089. BIO_free(bio);
  29090. bio = NULL;
  29091. /* Invalid format */
  29092. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29093. if (utc != NULL) {
  29094. utc->type = ASN_UTC_TIME;
  29095. utc->length = ASN_UTC_TIME_SIZE;
  29096. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  29097. }
  29098. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29099. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29100. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29101. /* Invalid type */
  29102. if (utc != NULL) {
  29103. utc->type = ASN_GENERALIZED_TIME;
  29104. utc->length = ASN_GENERALIZED_TIME_SIZE;
  29105. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  29106. }
  29107. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29108. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  29109. BIO_free(bio);
  29110. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  29111. return EXPECT_RESULT();
  29112. }
  29113. static int test_wolfSSL_ASN1_TYPE(void)
  29114. {
  29115. EXPECT_DECLS;
  29116. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  29117. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  29118. WOLFSSL_ASN1_TYPE* t = NULL;
  29119. WOLFSSL_ASN1_OBJECT* obj = NULL;
  29120. #ifndef NO_ASN_TIME
  29121. WOLFSSL_ASN1_TIME* time = NULL;
  29122. #endif
  29123. WOLFSSL_ASN1_STRING* str = NULL;
  29124. unsigned char data[] = { 0x00 };
  29125. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  29126. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29127. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  29128. wolfSSL_ASN1_TYPE_free(t);
  29129. t = NULL;
  29130. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29131. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  29132. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  29133. wolfSSL_ASN1_TYPE_free(t);
  29134. t = NULL;
  29135. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29136. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29137. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  29138. wolfSSL_ASN1_TYPE_free(t);
  29139. t = NULL;
  29140. #ifndef NO_ASN_TIME
  29141. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29142. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  29143. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  29144. wolfSSL_ASN1_TYPE_free(t);
  29145. t = NULL;
  29146. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29147. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  29148. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  29149. wolfSSL_ASN1_TYPE_free(t);
  29150. t = NULL;
  29151. #endif
  29152. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29153. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29154. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  29155. wolfSSL_ASN1_TYPE_free(t);
  29156. t = NULL;
  29157. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29158. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29159. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  29160. wolfSSL_ASN1_TYPE_free(t);
  29161. t = NULL;
  29162. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29163. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29164. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  29165. wolfSSL_ASN1_TYPE_free(t);
  29166. t = NULL;
  29167. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29168. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29169. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  29170. wolfSSL_ASN1_TYPE_free(t);
  29171. t = NULL;
  29172. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29173. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29174. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  29175. wolfSSL_ASN1_TYPE_free(t);
  29176. t = NULL;
  29177. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29178. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29179. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  29180. wolfSSL_ASN1_TYPE_free(t);
  29181. t = NULL;
  29182. #endif
  29183. return EXPECT_RESULT();
  29184. }
  29185. /* Testing code used in dpp.c in hostap */
  29186. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29187. typedef struct {
  29188. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  29189. * as an OID identifying the curve */
  29190. X509_ALGOR *alg;
  29191. /* Compressed format public key per ANSI X9.63 */
  29192. ASN1_BIT_STRING *pub_key;
  29193. } DPP_BOOTSTRAPPING_KEY;
  29194. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  29195. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  29196. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  29197. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  29198. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  29199. typedef struct {
  29200. ASN1_INTEGER *integer;
  29201. } TEST_ASN1;
  29202. ASN1_SEQUENCE(TEST_ASN1) = {
  29203. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  29204. } ASN1_SEQUENCE_END(TEST_ASN1)
  29205. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  29206. typedef struct {
  29207. ASN1_OCTET_STRING *octet_string;
  29208. } TEST_FAIL_ASN1;
  29209. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  29210. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  29211. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  29212. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  29213. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  29214. #endif
  29215. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  29216. {
  29217. EXPECT_DECLS;
  29218. /* Testing code used in dpp.c in hostap */
  29219. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29220. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  29221. EC_KEY *eckey = NULL;
  29222. EVP_PKEY *key = NULL;
  29223. size_t len = 0;
  29224. unsigned char *der = NULL;
  29225. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  29226. const unsigned char *in = ecc_clikey_der_256;
  29227. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  29228. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  29229. const EC_GROUP *group = NULL;
  29230. const EC_POINT *point = NULL;
  29231. int nid;
  29232. TEST_ASN1 *test_asn1 = NULL;
  29233. TEST_FAIL_ASN1 test_fail_asn1;
  29234. const unsigned char badObjDer[] = { 0x06, 0x00 };
  29235. const unsigned char goodObjDer[] = {
  29236. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  29237. };
  29238. WOLFSSL_ASN1_ITEM emptyTemplate;
  29239. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  29240. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29241. der = NULL;
  29242. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  29243. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  29244. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29245. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29246. (long)sizeof_ecc_clikey_der_256));
  29247. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29248. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  29249. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  29250. nid = EC_GROUP_get_curve_name(group);
  29251. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  29252. group_obj = OBJ_nid2obj(nid);
  29253. if ((ec_obj != NULL) && (group_obj != NULL)) {
  29254. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  29255. group_obj), 1);
  29256. if (EXPECT_SUCCESS()) {
  29257. ec_obj = NULL;
  29258. group_obj = NULL;
  29259. }
  29260. }
  29261. wolfSSL_ASN1_OBJECT_free(group_obj);
  29262. wolfSSL_ASN1_OBJECT_free(ec_obj);
  29263. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29264. #ifdef HAVE_COMP_KEY
  29265. ExpectIntGT((len = EC_POINT_point2oct(
  29266. group, point, POINT_CONVERSION_COMPRESSED,
  29267. NULL, 0, NULL)), 0);
  29268. #else
  29269. ExpectIntGT((len = EC_POINT_point2oct(
  29270. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29271. NULL, 0, NULL)), 0);
  29272. #endif
  29273. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29274. #ifdef HAVE_COMP_KEY
  29275. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29276. der, len-1, NULL), 0);
  29277. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29278. der, len, NULL), len);
  29279. #else
  29280. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29281. der, len-1, NULL), 0);
  29282. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29283. der, len, NULL), len);
  29284. #endif
  29285. if (EXPECT_SUCCESS()) {
  29286. bootstrap->pub_key->data = der;
  29287. bootstrap->pub_key->length = (int)len;
  29288. /* Not actually used */
  29289. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29290. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29291. }
  29292. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29293. der = NULL;
  29294. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29295. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29296. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29297. EVP_PKEY_free(key);
  29298. EC_KEY_free(eckey);
  29299. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29300. bootstrap = NULL;
  29301. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29302. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29303. * a NULL and an empty BIT_STRING. */
  29304. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29305. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  29306. if (EXPECT_SUCCESS()) {
  29307. bootstrap->alg->algorithm->obj = badObjDer;
  29308. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29309. }
  29310. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  29311. if (EXPECT_SUCCESS()) {
  29312. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29313. bootstrap->alg->parameter->value.ptr = NULL;
  29314. bootstrap->pub_key->data = NULL;
  29315. bootstrap->pub_key->length = 0;
  29316. /* Not actually used */
  29317. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29318. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29319. }
  29320. /* Encode with bad OBJECT_ID. */
  29321. der = NULL;
  29322. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29323. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29324. if (EXPECT_SUCCESS()) {
  29325. bootstrap->alg->algorithm->obj = goodObjDer;
  29326. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29327. bootstrap->alg->algorithm->grp = 2;
  29328. }
  29329. der = NULL;
  29330. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29331. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29332. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29333. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29334. /* Test integer */
  29335. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  29336. der = NULL;
  29337. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  29338. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  29339. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29340. TEST_ASN1_free(test_asn1);
  29341. /* Test integer cases. */
  29342. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29343. TEST_ASN1_free(NULL);
  29344. /* Test error cases. */
  29345. ExpectNull(TEST_FAIL_ASN1_new());
  29346. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29347. TEST_FAIL_ASN1_free(NULL);
  29348. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  29349. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  29350. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29351. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29352. return EXPECT_RESULT();
  29353. }
  29354. static int test_wolfSSL_lhash(void)
  29355. {
  29356. EXPECT_DECLS;
  29357. #ifdef OPENSSL_ALL
  29358. const char testStr[] = "Like a true nature's child\n"
  29359. "We were born\n"
  29360. "Born to be wild";
  29361. #ifdef NO_SHA
  29362. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29363. #else
  29364. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29365. #endif
  29366. #endif
  29367. return EXPECT_RESULT();
  29368. }
  29369. static int test_wolfSSL_X509_NAME(void)
  29370. {
  29371. EXPECT_DECLS;
  29372. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29373. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29374. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29375. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29376. defined(OPENSSL_EXTRA))
  29377. X509* x509 = NULL;
  29378. const unsigned char* c = NULL;
  29379. unsigned char buf[4096];
  29380. int bytes = 0;
  29381. XFILE f = XBADFILE;
  29382. const X509_NAME* a = NULL;
  29383. const X509_NAME* b = NULL;
  29384. X509_NAME* d2i_name = NULL;
  29385. int sz = 0;
  29386. unsigned char* tmp = NULL;
  29387. char file[] = "./certs/ca-cert.der";
  29388. #ifndef OPENSSL_EXTRA_X509_SMALL
  29389. byte empty[] = { /* CN=empty emailAddress= */
  29390. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29391. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29392. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29393. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29394. 0x01, 0x16, 0x00
  29395. };
  29396. #endif
  29397. #ifndef OPENSSL_EXTRA_X509_SMALL
  29398. /* test compile of deprecated function, returns 0 */
  29399. ExpectIntEQ(CRYPTO_thread_id(), 0);
  29400. #endif
  29401. ExpectNotNull(a = X509_NAME_new());
  29402. X509_NAME_free((X509_NAME*)a);
  29403. a = NULL;
  29404. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29405. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  29406. if (f != XBADFILE)
  29407. XFCLOSE(f);
  29408. c = buf;
  29409. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  29410. /* test cmp function */
  29411. ExpectNotNull(a = X509_get_issuer_name(x509));
  29412. ExpectNotNull(b = X509_get_subject_name(x509));
  29413. #ifndef OPENSSL_EXTRA_X509_SMALL
  29414. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29415. #endif
  29416. tmp = buf;
  29417. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29418. if (sz > 0 && tmp == buf) {
  29419. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29420. __LINE__);
  29421. fprintf(stderr, " Expected pointer to be incremented\n");
  29422. abort();
  29423. }
  29424. #ifndef OPENSSL_EXTRA_X509_SMALL
  29425. tmp = buf;
  29426. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29427. #endif
  29428. /* if output parameter is NULL, should still return required size. */
  29429. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29430. /* retry but with the function creating a buffer */
  29431. tmp = NULL;
  29432. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29433. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29434. tmp = NULL;
  29435. #ifdef WOLFSSL_CERT_NAME_ALL
  29436. /* test for givenName and name */
  29437. {
  29438. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  29439. const byte gName[] = "test-given-name";
  29440. const byte name[] = "test-name";
  29441. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29442. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  29443. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29444. 1);
  29445. wolfSSL_X509_NAME_ENTRY_free(entry);
  29446. entry = NULL;
  29447. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29448. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  29449. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29450. 1);
  29451. wolfSSL_X509_NAME_ENTRY_free(entry);
  29452. tmp = NULL;
  29453. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29454. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29455. }
  29456. #endif
  29457. b = NULL;
  29458. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29459. #ifndef OPENSSL_EXTRA_X509_SMALL
  29460. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  29461. #endif
  29462. X509_NAME_free((X509_NAME*)b);
  29463. X509_NAME_free(d2i_name);
  29464. d2i_name = NULL;
  29465. X509_free(x509);
  29466. #ifndef OPENSSL_EXTRA_X509_SMALL
  29467. /* test with an empty domain component */
  29468. tmp = empty;
  29469. sz = sizeof(empty);
  29470. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29471. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29472. /* size of empty emailAddress will be 0 */
  29473. tmp = buf;
  29474. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29475. (char*)tmp, sizeof(buf)), 0);
  29476. /* should contain no organization name */
  29477. tmp = buf;
  29478. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29479. (char*)tmp, sizeof(buf)), -1);
  29480. X509_NAME_free(d2i_name);
  29481. #endif
  29482. #endif
  29483. return EXPECT_RESULT();
  29484. }
  29485. static int test_wolfSSL_X509_NAME_hash(void)
  29486. {
  29487. EXPECT_DECLS;
  29488. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29489. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29490. BIO* bio = NULL;
  29491. X509* x509 = NULL;
  29492. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29493. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29494. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29495. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29496. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29497. X509_free(x509);
  29498. BIO_free(bio);
  29499. #endif
  29500. return EXPECT_RESULT();
  29501. }
  29502. static int test_wolfSSL_X509_NAME_print_ex(void)
  29503. {
  29504. EXPECT_DECLS;
  29505. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29506. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29507. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29508. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29509. !defined(NO_BIO) && !defined(NO_RSA)
  29510. int memSz = 0;
  29511. byte* mem = NULL;
  29512. BIO* bio = NULL;
  29513. BIO* membio = NULL;
  29514. X509* x509 = NULL;
  29515. X509_NAME* name = NULL;
  29516. const char* expNormal = "C=US, CN=wolfssl.com";
  29517. const char* expReverse = "CN=wolfssl.com, C=US";
  29518. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29519. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29520. const char* expRFC5523 =
  29521. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29522. /* Test with real cert (svrCertFile) first */
  29523. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29524. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29525. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29526. ExpectNotNull(name = X509_get_subject_name(x509));
  29527. /* Test without flags */
  29528. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29529. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29530. BIO_free(membio);
  29531. membio = NULL;
  29532. /* Test flag: XN_FLAG_RFC2253 */
  29533. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29534. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29535. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29536. BIO_free(membio);
  29537. membio = NULL;
  29538. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29539. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29540. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29541. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29542. BIO_free(membio);
  29543. membio = NULL;
  29544. X509_free(x509);
  29545. BIO_free(bio);
  29546. name = NULL;
  29547. /* Test normal case without escaped characters */
  29548. {
  29549. /* Create name: "/C=US/CN=wolfssl.com" */
  29550. ExpectNotNull(name = X509_NAME_new());
  29551. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29552. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29553. WOLFSSL_SUCCESS);
  29554. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29555. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29556. WOLFSSL_SUCCESS);
  29557. /* Test without flags */
  29558. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29559. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29560. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29561. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  29562. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29563. BIO_free(membio);
  29564. membio = NULL;
  29565. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29566. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29567. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29568. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29569. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29570. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29571. BIO_free(membio);
  29572. membio = NULL;
  29573. /* Test flags: XN_FLAG_DN_REV - reversed */
  29574. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29575. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29576. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29577. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29578. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29579. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29580. BIO_free(membio);
  29581. membio = NULL;
  29582. X509_NAME_free(name);
  29583. name = NULL;
  29584. }
  29585. /* Test RFC2253 characters are escaped with backslashes */
  29586. {
  29587. ExpectNotNull(name = X509_NAME_new());
  29588. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29589. /* space at beginning and end, and: ,+"\ */
  29590. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29591. WOLFSSL_SUCCESS);
  29592. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29593. /* # at beginning, and: <>;*/
  29594. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29595. WOLFSSL_SUCCESS);
  29596. /* Test without flags */
  29597. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29598. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29599. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29600. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  29601. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29602. XSTRLEN(expNotEscaped)), 0);
  29603. BIO_free(membio);
  29604. membio = NULL;
  29605. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29606. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29607. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29608. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29609. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29610. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  29611. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29612. BIO_free(membio);
  29613. membio = NULL;
  29614. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29615. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29616. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29617. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29618. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29619. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29620. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29621. XSTRLEN(expNotEscapedRev)), 0);
  29622. BIO_free(membio);
  29623. X509_NAME_free(name);
  29624. }
  29625. #endif
  29626. return EXPECT_RESULT();
  29627. }
  29628. #ifndef NO_BIO
  29629. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29630. {
  29631. EXPECT_DECLS;
  29632. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29633. STACK_OF(X509_INFO) *info_stack = NULL;
  29634. X509_INFO *info = NULL;
  29635. int len;
  29636. int i;
  29637. const char* files[] = {
  29638. cliCertFile,
  29639. cliKeyFile,
  29640. /* This needs to be the order as svrCertFile contains the
  29641. * intermediate cert as well. */
  29642. svrKeyFile,
  29643. svrCertFile,
  29644. NULL,
  29645. };
  29646. const char** curFile;
  29647. BIO *fileBIO = NULL;
  29648. BIO *concatBIO = NULL;
  29649. byte tmp[FOURK_BUF];
  29650. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29651. * to group objects together. */
  29652. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29653. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29654. int fileLen = 0;
  29655. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29656. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29657. if (EXPECT_SUCCESS()) {
  29658. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29659. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29660. fileLen -= len;
  29661. if (EXPECT_FAIL())
  29662. break;
  29663. }
  29664. /* Make sure we read the entire file */
  29665. ExpectIntEQ(fileLen, 0);
  29666. }
  29667. BIO_free(fileBIO);
  29668. fileBIO = NULL;
  29669. }
  29670. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29671. NULL));
  29672. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  29673. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29674. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29675. ExpectNotNull(info->x509);
  29676. ExpectNull(info->crl);
  29677. if (i != 0) {
  29678. ExpectNotNull(info->x_pkey);
  29679. ExpectIntEQ(X509_check_private_key(info->x509,
  29680. info->x_pkey->dec_pkey), 1);
  29681. }
  29682. else {
  29683. ExpectNull(info->x_pkey);
  29684. }
  29685. }
  29686. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29687. BIO_free(concatBIO);
  29688. #endif
  29689. return EXPECT_RESULT();
  29690. }
  29691. #endif
  29692. #ifndef NO_BIO
  29693. static int test_wolfSSL_X509_INFO(void)
  29694. {
  29695. EXPECT_DECLS;
  29696. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29697. STACK_OF(X509_INFO) *info_stack = NULL;
  29698. X509_INFO *info = NULL;
  29699. BIO *cert = NULL;
  29700. int i;
  29701. /* PEM in hex format to avoid null terminator */
  29702. byte data[] = {
  29703. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29704. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29705. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29706. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29707. 0x2d, 0x2d, 0x2d
  29708. };
  29709. /* PEM in hex format to avoid null terminator */
  29710. byte data2[] = {
  29711. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29712. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29713. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29714. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29715. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29716. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29717. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29718. };
  29719. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29720. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29721. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29722. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29723. ExpectNotNull(info->x509);
  29724. ExpectNull(info->crl);
  29725. ExpectNull(info->x_pkey);
  29726. }
  29727. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29728. info_stack = NULL;
  29729. BIO_free(cert);
  29730. cert = NULL;
  29731. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29732. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29733. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29734. info_stack = NULL;
  29735. BIO_free(cert);
  29736. cert = NULL;
  29737. /* This case should fail due to invalid input. */
  29738. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29739. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29740. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29741. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29742. info_stack = NULL;
  29743. BIO_free(cert);
  29744. cert = NULL;
  29745. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29746. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29747. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29748. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29749. BIO_free(cert);
  29750. #endif
  29751. return EXPECT_RESULT();
  29752. }
  29753. #endif
  29754. static int test_wolfSSL_X509_subject_name_hash(void)
  29755. {
  29756. EXPECT_DECLS;
  29757. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29758. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29759. X509* x509 = NULL;
  29760. X509_NAME* subjectName = NULL;
  29761. unsigned long ret1 = 0;
  29762. unsigned long ret2 = 0;
  29763. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29764. SSL_FILETYPE_PEM));
  29765. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29766. /* These two
  29767. * - X509_subject_name_hash(x509)
  29768. * - X509_NAME_hash(X509_get_subject_name(x509))
  29769. * should give the same hash, if !defined(NO_SHA) is true. */
  29770. ret1 = X509_subject_name_hash(x509);
  29771. ExpectIntNE(ret1, 0);
  29772. #if !defined(NO_SHA)
  29773. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29774. ExpectIntNE(ret2, 0);
  29775. ExpectIntEQ(ret1, ret2);
  29776. #else
  29777. (void) ret2;
  29778. #endif
  29779. X509_free(x509);
  29780. #endif
  29781. return EXPECT_RESULT();
  29782. }
  29783. static int test_wolfSSL_X509_issuer_name_hash(void)
  29784. {
  29785. EXPECT_DECLS;
  29786. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29787. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29788. X509* x509 = NULL;
  29789. X509_NAME* issuertName = NULL;
  29790. unsigned long ret1 = 0;
  29791. unsigned long ret2 = 0;
  29792. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29793. SSL_FILETYPE_PEM));
  29794. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29795. /* These two
  29796. * - X509_issuer_name_hash(x509)
  29797. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29798. * should give the same hash, if !defined(NO_SHA) is true. */
  29799. ret1 = X509_issuer_name_hash(x509);
  29800. ExpectIntNE(ret1, 0);
  29801. #if !defined(NO_SHA)
  29802. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29803. ExpectIntNE(ret2, 0);
  29804. ExpectIntEQ(ret1, ret2);
  29805. #else
  29806. (void) ret2;
  29807. #endif
  29808. X509_free(x509);
  29809. #endif
  29810. return EXPECT_RESULT();
  29811. }
  29812. static int test_wolfSSL_X509_check_host(void)
  29813. {
  29814. EXPECT_DECLS;
  29815. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29816. && !defined(NO_SHA) && !defined(NO_RSA)
  29817. X509* x509 = NULL;
  29818. const char altName[] = "example.com";
  29819. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29820. SSL_FILETYPE_PEM));
  29821. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29822. WOLFSSL_SUCCESS);
  29823. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29824. WOLFSSL_FAILURE);
  29825. X509_free(x509);
  29826. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29827. WOLFSSL_FAILURE);
  29828. #endif
  29829. return EXPECT_RESULT();
  29830. }
  29831. static int test_wolfSSL_X509_check_email(void)
  29832. {
  29833. EXPECT_DECLS;
  29834. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29835. X509* x509 = NULL;
  29836. const char goodEmail[] = "info@wolfssl.com";
  29837. const char badEmail[] = "disinfo@wolfssl.com";
  29838. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29839. SSL_FILETYPE_PEM));
  29840. /* Should fail on non-matching email address */
  29841. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29842. WOLFSSL_FAILURE);
  29843. /* Should succeed on matching email address */
  29844. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29845. WOLFSSL_SUCCESS);
  29846. /* Should compute length internally when not provided */
  29847. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29848. WOLFSSL_SUCCESS);
  29849. /* Should fail when email address is NULL */
  29850. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29851. WOLFSSL_FAILURE);
  29852. X509_free(x509);
  29853. /* Should fail when x509 is NULL */
  29854. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29855. WOLFSSL_FAILURE);
  29856. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29857. return EXPECT_RESULT();
  29858. }
  29859. static int test_wc_PemToDer(void)
  29860. {
  29861. EXPECT_DECLS;
  29862. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29863. int ret;
  29864. DerBuffer* pDer = NULL;
  29865. const char* ca_cert = "./certs/server-cert.pem";
  29866. byte* cert_buf = NULL;
  29867. size_t cert_sz = 0;
  29868. int eccKey = 0;
  29869. EncryptedInfo info;
  29870. XMEMSET(&info, 0, sizeof(info));
  29871. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29872. ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
  29873. &info, &eccKey), 0);
  29874. wc_FreeDer(&pDer);
  29875. pDer = NULL;
  29876. if (cert_buf != NULL) {
  29877. free(cert_buf);
  29878. cert_buf = NULL;
  29879. }
  29880. #ifdef HAVE_ECC
  29881. {
  29882. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29883. byte key_buf[256] = {0};
  29884. /* Test fail of loading a key with cert type */
  29885. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29886. key_buf[0] = '\n';
  29887. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29888. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29889. &pDer, NULL, &info, &eccKey)), 0);
  29890. #ifdef OPENSSL_EXTRA
  29891. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29892. &pDer, NULL, &info, &eccKey)), 0);
  29893. #endif
  29894. wc_FreeDer(&pDer);
  29895. if (cert_buf != NULL)
  29896. free(cert_buf);
  29897. }
  29898. #endif
  29899. #endif
  29900. return EXPECT_RESULT();
  29901. }
  29902. static int test_wc_AllocDer(void)
  29903. {
  29904. EXPECT_DECLS;
  29905. #if !defined(NO_CERTS)
  29906. DerBuffer* pDer = NULL;
  29907. word32 testSize = 1024;
  29908. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29909. BAD_FUNC_ARG);
  29910. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29911. ExpectNotNull(pDer);
  29912. wc_FreeDer(&pDer);
  29913. #endif
  29914. return EXPECT_RESULT();
  29915. }
  29916. static int test_wc_CertPemToDer(void)
  29917. {
  29918. EXPECT_DECLS;
  29919. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29920. const char* ca_cert = "./certs/ca-cert.pem";
  29921. byte* cert_buf = NULL;
  29922. size_t cert_sz = 0;
  29923. size_t cert_dersz = 0;
  29924. byte* cert_der = NULL;
  29925. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29926. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29927. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29928. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29929. (int)cert_dersz, CERT_TYPE), 0);
  29930. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29931. BAD_FUNC_ARG);
  29932. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29933. BAD_FUNC_ARG);
  29934. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29935. BAD_FUNC_ARG);
  29936. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29937. CERT_TYPE), BAD_FUNC_ARG);
  29938. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29939. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29940. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29941. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29942. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29943. CERT_TYPE), BAD_FUNC_ARG);
  29944. if (cert_der != NULL)
  29945. free(cert_der);
  29946. if (cert_buf != NULL)
  29947. free(cert_buf);
  29948. #endif
  29949. return EXPECT_RESULT();
  29950. }
  29951. static int test_wc_KeyPemToDer(void)
  29952. {
  29953. EXPECT_DECLS;
  29954. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29955. int ret = 0;
  29956. const byte cert_buf[] = \
  29957. "-----BEGIN PRIVATE KEY-----\n"
  29958. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29959. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29960. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29961. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29962. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29963. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29964. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29965. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29966. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29967. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29968. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29969. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29970. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29971. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29972. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29973. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29974. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29975. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29976. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29977. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29978. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29979. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29980. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29981. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29982. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29983. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29984. "-----END PRIVATE KEY-----\n";
  29985. const int cert_sz = sizeof(cert_buf);
  29986. const char cert_pw[] = "password";
  29987. int cert_dersz = 0;
  29988. byte* cert_der = NULL;
  29989. /* Bad arg: Cert buffer is NULL */
  29990. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29991. BAD_FUNC_ARG);
  29992. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29993. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29994. BAD_FUNC_ARG);
  29995. /* Test normal operation */
  29996. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29997. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  29998. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29999. cert_pw), 0);
  30000. ExpectIntLE(ret, cert_sz);
  30001. if (cert_der != NULL) {
  30002. free(cert_der);
  30003. cert_der = NULL;
  30004. }
  30005. /* Test NULL for DER buffer to return needed DER buffer size */
  30006. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  30007. ExpectIntLE(ret, cert_sz);
  30008. if (EXPECT_SUCCESS())
  30009. cert_dersz = ret;
  30010. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  30011. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  30012. cert_pw), 0);
  30013. ExpectIntLE(ret, cert_sz);
  30014. if (cert_der != NULL)
  30015. free(cert_der);
  30016. #endif
  30017. return EXPECT_RESULT();
  30018. }
  30019. static int test_wc_PubKeyPemToDer(void)
  30020. {
  30021. EXPECT_DECLS;
  30022. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  30023. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30024. int ret = 0;
  30025. const char* key = "./certs/ecc-client-keyPub.pem";
  30026. byte* cert_buf = NULL;
  30027. size_t cert_sz = 0, cert_dersz = 0;
  30028. byte* cert_der = NULL;
  30029. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  30030. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  30031. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  30032. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30033. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30034. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30035. (int)cert_dersz), 0);
  30036. if (cert_der != NULL) {
  30037. free(cert_der);
  30038. cert_der = NULL;
  30039. }
  30040. /* Test NULL for DER buffer to return needed DER buffer size */
  30041. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  30042. ExpectIntLE(ret, cert_sz);
  30043. cert_dersz = (size_t)ret;
  30044. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30045. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30046. (int)cert_dersz), 0);
  30047. if (cert_der != NULL) {
  30048. free(cert_der);
  30049. }
  30050. if (cert_buf != NULL) {
  30051. free(cert_buf);
  30052. }
  30053. #endif
  30054. return EXPECT_RESULT();
  30055. }
  30056. static int test_wc_PemPubKeyToDer(void)
  30057. {
  30058. EXPECT_DECLS;
  30059. #if !defined(NO_FILESYSTEM) && \
  30060. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30061. const char* key = "./certs/ecc-client-keyPub.pem";
  30062. size_t cert_dersz = 1024;
  30063. byte* cert_der = NULL;
  30064. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  30065. BAD_FUNC_ARG);
  30066. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30067. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  30068. if (cert_der != NULL) {
  30069. free(cert_der);
  30070. }
  30071. #endif
  30072. return EXPECT_RESULT();
  30073. }
  30074. static int test_wc_GetPubKeyDerFromCert(void)
  30075. {
  30076. EXPECT_DECLS;
  30077. #if !defined(NO_RSA) || defined(HAVE_ECC)
  30078. int ret;
  30079. word32 idx = 0;
  30080. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30081. word32 keyDerSz = (word32)sizeof(keyDer);
  30082. DecodedCert decoded;
  30083. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30084. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  30085. word32 certBufSz = sizeof(certBuf);
  30086. #endif
  30087. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  30088. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30089. XFILE fp = XBADFILE;
  30090. #endif
  30091. #ifndef NO_RSA
  30092. RsaKey rsaKey;
  30093. #if defined(USE_CERT_BUFFERS_2048)
  30094. byte* rsaCertDer = (byte*)client_cert_der_2048;
  30095. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  30096. #elif defined(USE_CERT_BUFFERS_1024)
  30097. byte* rsaCertDer = (byte*)client_cert_der_1024;
  30098. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  30099. #else
  30100. unsigned char rsaCertDer[TWOK_BUF];
  30101. word32 rsaCertDerSz;
  30102. #endif
  30103. #endif
  30104. #ifdef HAVE_ECC
  30105. ecc_key eccKey;
  30106. #if defined(USE_CERT_BUFFERS_256)
  30107. byte* eccCert = (byte*)cliecc_cert_der_256;
  30108. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  30109. #else
  30110. unsigned char eccCert[ONEK_BUF];
  30111. word32 eccCertSz;
  30112. XFILE fp2 = XBADFILE;
  30113. #endif
  30114. #endif
  30115. #ifndef NO_RSA
  30116. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  30117. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  30118. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  30119. fp), 0);
  30120. if (fp != XBADFILE) {
  30121. XFCLOSE(fp);
  30122. fp = XBADFILE;
  30123. }
  30124. #endif
  30125. /* good test case - RSA DER cert */
  30126. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  30127. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30128. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30129. ExpectIntGT(keyDerSz, 0);
  30130. /* sanity check, verify we can import DER public key */
  30131. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30132. ExpectIntEQ(ret, 0);
  30133. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  30134. if (ret == 0) {
  30135. wc_FreeRsaKey(&rsaKey);
  30136. }
  30137. /* test LENGTH_ONLY_E case */
  30138. keyDerSz = 0;
  30139. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  30140. LENGTH_ONLY_E);
  30141. ExpectIntGT(keyDerSz, 0);
  30142. /* bad args: DecodedCert NULL */
  30143. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  30144. /* bad args: output key buff size */
  30145. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  30146. /* bad args: zero size output key buffer */
  30147. keyDerSz = 0;
  30148. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  30149. BAD_FUNC_ARG);
  30150. wc_FreeDecodedCert(&decoded);
  30151. /* Certificate Request Tests */
  30152. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30153. {
  30154. XMEMSET(certBuf, 0, sizeof(certBuf));
  30155. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  30156. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  30157. if (fp != XBADFILE) {
  30158. XFCLOSE(fp);
  30159. }
  30160. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  30161. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  30162. /* good test case - RSA DER certificate request */
  30163. keyDerSz = sizeof(keyDer);
  30164. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  30165. 0);
  30166. ExpectIntGT(keyDerSz, 0);
  30167. /* sanity check, verify we can import DER public key */
  30168. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30169. ExpectIntEQ(ret, 0);
  30170. idx = 0;
  30171. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  30172. if (ret == 0) {
  30173. wc_FreeRsaKey(&rsaKey);
  30174. }
  30175. wc_FreeDecodedCert(&decoded);
  30176. }
  30177. #endif /* WOLFSSL_CERT_REQ */
  30178. #endif /* NO_RSA */
  30179. #ifdef HAVE_ECC
  30180. #ifndef USE_CERT_BUFFERS_256
  30181. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  30182. XBADFILE);
  30183. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  30184. if (fp2 != XBADFILE) {
  30185. XFCLOSE(fp2);
  30186. }
  30187. #endif
  30188. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  30189. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30190. /* good test case - ECC */
  30191. XMEMSET(keyDer, 0, sizeof(keyDer));
  30192. keyDerSz = sizeof(keyDer);
  30193. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30194. ExpectIntGT(keyDerSz, 0);
  30195. /* sanity check, verify we can import DER public key */
  30196. ret = wc_ecc_init(&eccKey);
  30197. ExpectIntEQ(ret, 0);
  30198. idx = 0; /* reset idx to 0, used above in RSA case */
  30199. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  30200. if (ret == 0) {
  30201. wc_ecc_free(&eccKey);
  30202. }
  30203. /* test LENGTH_ONLY_E case */
  30204. keyDerSz = 0;
  30205. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  30206. LENGTH_ONLY_E);
  30207. ExpectIntGT(keyDerSz, 0);
  30208. wc_FreeDecodedCert(&decoded);
  30209. #endif
  30210. #endif /* !NO_RSA || HAVE_ECC */
  30211. return EXPECT_RESULT();
  30212. }
  30213. static int test_wc_CheckCertSigPubKey(void)
  30214. {
  30215. EXPECT_DECLS;
  30216. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30217. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  30218. int ret = 0;
  30219. const char* ca_cert = "./certs/ca-cert.pem";
  30220. byte* cert_buf = NULL;
  30221. size_t cert_sz = 0;
  30222. byte* cert_der = NULL;
  30223. word32 cert_dersz = 0;
  30224. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30225. word32 keyDerSz = (word32)sizeof(keyDer);
  30226. DecodedCert decoded;
  30227. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  30228. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  30229. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30230. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  30231. (int)cert_dersz, CERT_TYPE), 0);
  30232. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  30233. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30234. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30235. ExpectIntGT(keyDerSz, 0);
  30236. /* Good test case. */
  30237. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30238. keyDerSz, RSAk), 0);
  30239. /* No certificate. */
  30240. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  30241. ECDSAk), BAD_FUNC_ARG);
  30242. /* Bad cert size. */
  30243. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  30244. RSAk), 0);
  30245. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30246. /* No public key. */
  30247. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  30248. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  30249. /* Bad public key size. */
  30250. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30251. RSAk), BAD_FUNC_ARG);
  30252. /* Wrong aglo. */
  30253. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30254. keyDerSz, ECDSAk), ASN_PARSE_E);
  30255. wc_FreeDecodedCert(&decoded);
  30256. if (cert_der != NULL)
  30257. free(cert_der);
  30258. if (cert_buf != NULL)
  30259. free(cert_buf);
  30260. #endif
  30261. return EXPECT_RESULT();
  30262. }
  30263. static int test_wolfSSL_certs(void)
  30264. {
  30265. EXPECT_DECLS;
  30266. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30267. !defined(NO_RSA)
  30268. X509* x509ext = NULL;
  30269. #ifdef OPENSSL_ALL
  30270. X509* x509 = NULL;
  30271. WOLFSSL_X509_EXTENSION* ext = NULL;
  30272. ASN1_OBJECT* obj = NULL;
  30273. #endif
  30274. WOLFSSL* ssl = NULL;
  30275. WOLFSSL_CTX* ctx = NULL;
  30276. STACK_OF(ASN1_OBJECT)* sk = NULL;
  30277. ASN1_STRING* asn1_str = NULL;
  30278. AUTHORITY_KEYID* akey = NULL;
  30279. BASIC_CONSTRAINTS* bc = NULL;
  30280. int crit = 0;
  30281. #ifndef NO_WOLFSSL_SERVER
  30282. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30283. #else
  30284. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30285. #endif
  30286. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30287. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30288. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30289. #if !defined(NO_CHECK_PRIVATE_KEY)
  30290. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30291. #endif
  30292. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30293. #if !defined(NO_CHECK_PRIVATE_KEY)
  30294. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30295. #endif
  30296. ExpectNotNull(ssl = SSL_new(ctx));
  30297. #if !defined(NO_CHECK_PRIVATE_KEY)
  30298. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30299. #endif
  30300. #ifdef HAVE_PK_CALLBACKS
  30301. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30302. #endif /* HAVE_PK_CALLBACKS */
  30303. /* create and use x509 */
  30304. #ifdef OPENSSL_ALL
  30305. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30306. WOLFSSL_FILETYPE_PEM));
  30307. #endif
  30308. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  30309. WOLFSSL_FILETYPE_PEM));
  30310. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30311. #if !defined(NO_CHECK_PRIVATE_KEY)
  30312. /* with loading in a new cert the check on private key should now fail */
  30313. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30314. #endif
  30315. #if defined(USE_CERT_BUFFERS_2048)
  30316. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  30317. (unsigned char*)server_cert_der_2048,
  30318. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30319. #endif
  30320. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30321. /************* Get Digest of Certificate ******************/
  30322. {
  30323. byte digest[64]; /* max digest size */
  30324. word32 digestSz;
  30325. XMEMSET(digest, 0, sizeof(digest));
  30326. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30327. WOLFSSL_SUCCESS);
  30328. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30329. WOLFSSL_SUCCESS);
  30330. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30331. WOLFSSL_FAILURE);
  30332. }
  30333. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30334. /* test and checkout X509 extensions */
  30335. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  30336. NID_basic_constraints, &crit, NULL));
  30337. ExpectIntEQ(crit, 0);
  30338. #ifdef OPENSSL_ALL
  30339. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  30340. X509_EXTENSION_free(ext);
  30341. ext = NULL;
  30342. ExpectNotNull(ext = X509_EXTENSION_new());
  30343. X509_EXTENSION_set_critical(ext, 1);
  30344. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30345. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30346. ASN1_OBJECT_free(obj);
  30347. obj = NULL;
  30348. X509_EXTENSION_free(ext);
  30349. ext = NULL;
  30350. ExpectNotNull(ext = X509_EXTENSION_new());
  30351. X509_EXTENSION_set_critical(ext, 0);
  30352. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30353. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30354. NULL);
  30355. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30356. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30357. * and X509_get_ext_d2i has created new */
  30358. asn1_str = NULL;
  30359. X509_EXTENSION_free(ext);
  30360. ext = NULL;
  30361. #endif
  30362. BASIC_CONSTRAINTS_free(bc);
  30363. bc = NULL;
  30364. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  30365. NID_key_usage, &crit, NULL));
  30366. ExpectIntEQ(crit, 1);
  30367. ExpectIntEQ(asn1_str->type, NID_key_usage);
  30368. #ifdef OPENSSL_ALL
  30369. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  30370. X509_EXTENSION_free(ext);
  30371. ext = NULL;
  30372. #endif
  30373. ASN1_STRING_free(asn1_str);
  30374. asn1_str = NULL;
  30375. #ifdef OPENSSL_ALL
  30376. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  30377. NID_ext_key_usage, &crit, NULL));
  30378. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  30379. X509_EXTENSION_free(ext);
  30380. ext = NULL;
  30381. EXTENDED_KEY_USAGE_free(sk);
  30382. sk = NULL;
  30383. #else
  30384. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30385. &crit, NULL);
  30386. ExpectNull(sk);
  30387. #endif
  30388. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30389. NID_authority_key_identifier, &crit, NULL));
  30390. #ifdef OPENSSL_ALL
  30391. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  30392. akey));
  30393. X509_EXTENSION_free(ext);
  30394. ext = NULL;
  30395. #endif
  30396. wolfSSL_AUTHORITY_KEYID_free(akey);
  30397. akey = NULL;
  30398. /* NID not yet supported */
  30399. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30400. NID_private_key_usage_period, &crit, NULL));
  30401. ExpectIntEQ(crit, -1);
  30402. sk_ASN1_OBJECT_free(sk);
  30403. sk = NULL;
  30404. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  30405. NID_subject_alt_name, &crit, NULL));
  30406. {
  30407. int i;
  30408. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30409. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30410. ExpectIntEQ(gen->type, GEN_DNS);
  30411. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30412. }
  30413. }
  30414. sk_GENERAL_NAME_free(sk);
  30415. sk = NULL;
  30416. /* NID not yet supported */
  30417. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30418. NID_issuer_alt_name, &crit, NULL));
  30419. ExpectIntEQ(crit, -1);
  30420. sk_ASN1_OBJECT_free(sk);
  30421. sk = NULL;
  30422. /* NID not yet supported */
  30423. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30424. NID_info_access, &crit, NULL));
  30425. sk_ASN1_OBJECT_free(sk);
  30426. sk = NULL;
  30427. /* NID not yet supported */
  30428. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30429. NID_sinfo_access, &crit, NULL));
  30430. ExpectIntEQ(crit, -1);
  30431. sk_ASN1_OBJECT_free(sk);
  30432. sk = NULL;
  30433. /* NID not yet supported */
  30434. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30435. NID_name_constraints, &crit, NULL));
  30436. ExpectIntEQ(crit, -1);
  30437. sk_ASN1_OBJECT_free(sk);
  30438. sk = NULL;
  30439. /* no cert policy set */
  30440. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30441. NID_certificate_policies, &crit, NULL));
  30442. sk_ASN1_OBJECT_free(sk);
  30443. sk = NULL;
  30444. /* NID not yet supported */
  30445. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30446. NID_policy_mappings, &crit, NULL));
  30447. ExpectIntEQ(crit, -1);
  30448. sk_ASN1_OBJECT_free(sk);
  30449. sk = NULL;
  30450. /* NID not yet supported */
  30451. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30452. NID_policy_constraints, &crit, NULL));
  30453. ExpectIntEQ(crit, -1);
  30454. sk_ASN1_OBJECT_free(sk);
  30455. sk = NULL;
  30456. /* NID not yet supported */
  30457. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30458. NID_inhibit_any_policy, &crit, NULL));
  30459. ExpectIntEQ(crit, -1);
  30460. sk_ASN1_OBJECT_free(sk);
  30461. sk = NULL;
  30462. /* NID not yet supported */
  30463. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30464. NID_tlsfeature, &crit, NULL));
  30465. ExpectIntEQ(crit, -1);
  30466. sk_ASN1_OBJECT_free(sk);
  30467. sk = NULL;
  30468. /* test invalid cases */
  30469. crit = 0;
  30470. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  30471. NULL));
  30472. ExpectIntEQ(crit, -1);
  30473. /* NULL passed for criticality. */
  30474. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  30475. NID_tlsfeature, NULL, NULL));
  30476. ExpectIntEQ(SSL_get_hit(ssl), 0);
  30477. #ifdef OPENSSL_ALL
  30478. X509_free(x509);
  30479. #endif
  30480. X509_free(x509ext);
  30481. SSL_free(ssl);
  30482. SSL_CTX_free(ctx);
  30483. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30484. return EXPECT_RESULT();
  30485. }
  30486. static int test_wolfSSL_X509_check_private_key(void)
  30487. {
  30488. EXPECT_DECLS;
  30489. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30490. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30491. X509* x509 = NULL;
  30492. EVP_PKEY* pkey = NULL;
  30493. const byte* key;
  30494. /* Check with correct key */
  30495. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30496. SSL_FILETYPE_PEM)));
  30497. key = client_key_der_2048;
  30498. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30499. (long)sizeof_client_key_der_2048));
  30500. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  30501. EVP_PKEY_free(pkey);
  30502. pkey = NULL;
  30503. /* Check with wrong key */
  30504. key = server_key_der_2048;
  30505. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30506. (long)sizeof_server_key_der_2048));
  30507. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  30508. /* test for incorrect parameter */
  30509. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  30510. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  30511. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  30512. EVP_PKEY_free(pkey);
  30513. X509_free(x509);
  30514. #endif
  30515. return EXPECT_RESULT();
  30516. }
  30517. static int test_wolfSSL_private_keys(void)
  30518. {
  30519. EXPECT_DECLS;
  30520. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30521. !defined(NO_FILESYSTEM)
  30522. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30523. WOLFSSL* ssl = NULL;
  30524. WOLFSSL_CTX* ctx = NULL;
  30525. EVP_PKEY* pkey = NULL;
  30526. OpenSSL_add_all_digests();
  30527. OpenSSL_add_all_algorithms();
  30528. #ifndef NO_RSA
  30529. #ifndef NO_WOLFSSL_SERVER
  30530. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30531. #else
  30532. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30533. #endif
  30534. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30535. /* Have to load a cert before you can check the private key against that
  30536. * certificates public key! */
  30537. #if !defined(NO_CHECK_PRIVATE_KEY)
  30538. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30539. #endif
  30540. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30541. #if !defined(NO_CHECK_PRIVATE_KEY)
  30542. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30543. #endif
  30544. ExpectNotNull(ssl = SSL_new(ctx));
  30545. #if !defined(NO_CHECK_PRIVATE_KEY)
  30546. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30547. #endif
  30548. #ifdef USE_CERT_BUFFERS_2048
  30549. {
  30550. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30551. unsigned char buf[FOURK_BUF];
  30552. word32 bufSz;
  30553. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30554. (unsigned char*)client_key_der_2048,
  30555. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30556. #if !defined(NO_CHECK_PRIVATE_KEY)
  30557. /* Should mismatch now that a different private key loaded */
  30558. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30559. #endif
  30560. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30561. (unsigned char*)server_key,
  30562. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30563. #if !defined(NO_CHECK_PRIVATE_KEY)
  30564. /* After loading back in DER format of original key, should match */
  30565. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30566. #endif
  30567. /* test loading private key to the WOLFSSL_CTX */
  30568. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30569. (unsigned char*)client_key_der_2048,
  30570. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30571. #if !defined(NO_CHECK_PRIVATE_KEY)
  30572. /* Should mismatch now that a different private key loaded */
  30573. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30574. #endif
  30575. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30576. (unsigned char*)server_key,
  30577. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30578. #if !defined(NO_CHECK_PRIVATE_KEY)
  30579. /* After loading back in DER format of original key, should match */
  30580. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30581. #endif
  30582. /* pkey not set yet, expecting to fail */
  30583. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30584. /* set PKEY and test again */
  30585. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30586. &server_key, (long)sizeof_server_key_der_2048));
  30587. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30588. /* reuse PKEY structure and test
  30589. * this should be checked with a memory management sanity checker */
  30590. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  30591. server_key = (const unsigned char*)server_key_der_2048;
  30592. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30593. &server_key, (long)sizeof_server_key_der_2048));
  30594. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30595. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30596. bufSz = FOURK_BUF;
  30597. ExpectIntGT((bufSz = (word32)wc_CreatePKCS8Key(buf, &bufSz,
  30598. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30599. RSAk, NULL, 0)), 0);
  30600. server_key = (const unsigned char*)buf;
  30601. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30602. (long)bufSz));
  30603. }
  30604. #endif
  30605. EVP_PKEY_free(pkey);
  30606. pkey = NULL;
  30607. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30608. ssl = NULL;
  30609. SSL_CTX_free(ctx);
  30610. ctx = NULL;
  30611. #endif /* end of RSA private key match tests */
  30612. #ifdef HAVE_ECC
  30613. #ifndef NO_WOLFSSL_SERVER
  30614. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30615. #else
  30616. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30617. #endif
  30618. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30619. WOLFSSL_FILETYPE_PEM));
  30620. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30621. WOLFSSL_FILETYPE_PEM));
  30622. ExpectNotNull(ssl = SSL_new(ctx));
  30623. #if !defined(NO_CHECK_PRIVATE_KEY)
  30624. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30625. #endif
  30626. SSL_free(ssl);
  30627. ssl = NULL;
  30628. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30629. WOLFSSL_FILETYPE_PEM));
  30630. ExpectNotNull(ssl = SSL_new(ctx));
  30631. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30632. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30633. #endif
  30634. SSL_free(ssl);
  30635. ssl = NULL;
  30636. SSL_CTX_free(ctx);
  30637. ctx = NULL;
  30638. #endif /* end of ECC private key match tests */
  30639. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30640. #ifndef NO_WOLFSSL_SERVER
  30641. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30642. #else
  30643. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30644. #endif
  30645. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30646. WOLFSSL_FILETYPE_PEM));
  30647. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30648. WOLFSSL_FILETYPE_PEM));
  30649. ExpectNotNull(ssl = SSL_new(ctx));
  30650. #if !defined(NO_CHECK_PRIVATE_KEY)
  30651. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30652. #endif
  30653. SSL_free(ssl);
  30654. ssl = NULL;
  30655. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30656. WOLFSSL_FILETYPE_PEM));
  30657. ExpectNotNull(ssl = SSL_new(ctx));
  30658. #if !defined(NO_CHECK_PRIVATE_KEY)
  30659. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30660. #endif
  30661. SSL_free(ssl);
  30662. ssl = NULL;
  30663. SSL_CTX_free(ctx);
  30664. ctx = NULL;
  30665. #endif /* end of Ed25519 private key match tests */
  30666. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30667. #ifndef NO_WOLFSSL_SERVER
  30668. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30669. #else
  30670. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30671. #endif
  30672. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30673. WOLFSSL_FILETYPE_PEM));
  30674. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30675. WOLFSSL_FILETYPE_PEM));
  30676. ExpectNotNull(ssl = SSL_new(ctx));
  30677. #if !defined(NO_CHECK_PRIVATE_KEY)
  30678. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30679. #endif
  30680. SSL_free(ssl);
  30681. ssl = NULL;
  30682. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30683. WOLFSSL_FILETYPE_PEM));
  30684. ExpectNotNull(ssl = SSL_new(ctx));
  30685. #if !defined(NO_CHECK_PRIVATE_KEY)
  30686. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30687. #endif
  30688. SSL_free(ssl);
  30689. ssl = NULL;
  30690. SSL_CTX_free(ctx);
  30691. ctx = NULL;
  30692. #endif /* end of Ed448 private key match tests */
  30693. EVP_cleanup();
  30694. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30695. CONF_modules_free();
  30696. ENGINE_cleanup();
  30697. CONF_modules_unload();
  30698. (void)ssl;
  30699. (void)ctx;
  30700. (void)pkey;
  30701. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30702. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30703. return EXPECT_RESULT();
  30704. }
  30705. static int test_wolfSSL_PEM_def_callback(void)
  30706. {
  30707. EXPECT_DECLS;
  30708. #ifdef OPENSSL_EXTRA
  30709. char buf[10];
  30710. const char* defpwd = "DEF PWD";
  30711. int defpwdLen = (int)XSTRLEN(defpwd);
  30712. int smallLen = 1;
  30713. /* Bad parameters. */
  30714. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  30715. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  30716. 0);
  30717. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  30718. XMEMSET(buf, 0, sizeof(buf));
  30719. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  30720. defpwdLen);
  30721. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  30722. ExpectIntEQ(buf[defpwdLen], 0);
  30723. /* Size of buffer is smaller than default password. */
  30724. XMEMSET(buf, 0, sizeof(buf));
  30725. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  30726. smallLen);
  30727. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  30728. ExpectIntEQ(buf[smallLen], 0);
  30729. #endif /* OPENSSL_EXTRA */
  30730. return EXPECT_RESULT();
  30731. }
  30732. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30733. {
  30734. EXPECT_DECLS;
  30735. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  30736. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  30737. XFILE file = XBADFILE;
  30738. #if !defined(NO_RSA)
  30739. const char* fname_rsa = "./certs/server-key.pem";
  30740. RSA* rsa = NULL;
  30741. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30742. unsigned char* sig = NULL;
  30743. size_t sigLen = 0;
  30744. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30745. size_t tbsLen = sizeof(tbs);
  30746. #endif
  30747. #if !defined(NO_DSA)
  30748. const char* fname_dsa = "./certs/dsa2048.pem";
  30749. #endif
  30750. #if defined(HAVE_ECC)
  30751. const char* fname_ec = "./certs/ecc-key.pem";
  30752. #endif
  30753. #if !defined(NO_DH)
  30754. const char* fname_dh = "./certs/dh-priv-2048.pem";
  30755. #endif
  30756. EVP_PKEY* pkey = NULL;
  30757. /* Check error case. */
  30758. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30759. /* not a PEM key. */
  30760. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30761. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30762. if (file != XBADFILE)
  30763. XFCLOSE(file);
  30764. file = XBADFILE;
  30765. #ifndef NO_RSA
  30766. /* Read in an RSA key. */
  30767. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  30768. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30769. if (file != XBADFILE)
  30770. XFCLOSE(file);
  30771. file = XBADFILE;
  30772. /* Make sure the key is usable by signing some data with it. */
  30773. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30774. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30775. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30776. DYNAMIC_TYPE_TMP_BUFFER));
  30777. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30778. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30779. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30780. WOLFSSL_SUCCESS);
  30781. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30782. EVP_PKEY_CTX_free(ctx);
  30783. EVP_PKEY_free(pkey);
  30784. pkey = NULL;
  30785. #endif
  30786. #ifndef NO_DSA
  30787. /* Read in a DSA key. */
  30788. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  30789. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  30790. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30791. EVP_PKEY_free(pkey);
  30792. pkey = NULL;
  30793. #else
  30794. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30795. #endif
  30796. if (file != XBADFILE)
  30797. XFCLOSE(file);
  30798. file = XBADFILE;
  30799. #endif
  30800. #ifdef HAVE_ECC
  30801. /* Read in an EC key. */
  30802. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  30803. ExpectNotNull(pkey = EVP_PKEY_new());
  30804. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  30805. if (file != XBADFILE)
  30806. XFCLOSE(file);
  30807. file = XBADFILE;
  30808. EVP_PKEY_free(pkey);
  30809. pkey = NULL;
  30810. #endif
  30811. #ifndef NO_DH
  30812. /* Read in a DH key. */
  30813. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  30814. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30815. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30816. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30817. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30818. EVP_PKEY_free(pkey);
  30819. pkey = NULL;
  30820. #else
  30821. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30822. #endif
  30823. if (file != XBADFILE)
  30824. XFCLOSE(file);
  30825. file = XBADFILE;
  30826. #endif
  30827. #endif
  30828. return EXPECT_RESULT();
  30829. }
  30830. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30831. {
  30832. EXPECT_DECLS;
  30833. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30834. && !defined(NO_FILESYSTEM)
  30835. XFILE file = XBADFILE;
  30836. const char* fname = "./certs/client-keyPub.pem";
  30837. EVP_PKEY* pkey = NULL;
  30838. /* Check error case. */
  30839. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30840. /* Read in an RSA key. */
  30841. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30842. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30843. EVP_PKEY_free(pkey);
  30844. pkey = NULL;
  30845. if (file != XBADFILE)
  30846. XFCLOSE(file);
  30847. file = XBADFILE;
  30848. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30849. ExpectNotNull(pkey = EVP_PKEY_new());
  30850. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  30851. EVP_PKEY_free(pkey);
  30852. if (file != XBADFILE)
  30853. XFCLOSE(file);
  30854. #endif
  30855. return EXPECT_RESULT();
  30856. }
  30857. /* test loading RSA key using BIO */
  30858. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  30859. {
  30860. EXPECT_DECLS;
  30861. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30862. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  30863. !defined(NO_BIO)
  30864. BIO* bio = NULL;
  30865. XFILE file = XBADFILE;
  30866. const char* fname = "./certs/server-key.pem";
  30867. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30868. EVP_PKEY* pkey = NULL;
  30869. size_t sz = 0;
  30870. byte* buf = NULL;
  30871. EVP_PKEY* pkey2 = NULL;
  30872. EVP_PKEY* pkey3 = NULL;
  30873. RSA* rsa_key = NULL;
  30874. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30875. unsigned char extra[10];
  30876. int i;
  30877. BIO* pub_bio = NULL;
  30878. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30879. #endif
  30880. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30881. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30882. ExpectIntGT(sz = XFTELL(file), 0);
  30883. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30884. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30885. if (buf != NULL) {
  30886. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30887. }
  30888. if (file != XBADFILE) {
  30889. XFCLOSE(file);
  30890. file = XBADFILE;
  30891. }
  30892. /* Test using BIO new mem and loading PEM private key */
  30893. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30894. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30895. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30896. buf = NULL;
  30897. BIO_free(bio);
  30898. bio = NULL;
  30899. /* New empty EVP_PKEY */
  30900. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30901. if (pkey2 != NULL) {
  30902. pkey2->type = EVP_PKEY_RSA;
  30903. }
  30904. /* Test parameter copy */
  30905. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30906. EVP_PKEY_free(pkey2);
  30907. EVP_PKEY_free(pkey);
  30908. pkey = NULL;
  30909. /* Qt unit test case : rsa pkcs8 key */
  30910. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30911. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30912. ExpectIntGT(sz = XFTELL(file), 0);
  30913. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30914. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30915. if (buf) {
  30916. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30917. }
  30918. if (file != XBADFILE) {
  30919. XFCLOSE(file);
  30920. file = XBADFILE;
  30921. }
  30922. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30923. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30924. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30925. buf = NULL;
  30926. BIO_free(bio);
  30927. bio = NULL;
  30928. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30929. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30930. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30931. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30932. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30933. #else
  30934. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30935. #endif
  30936. RSA_free(rsa_key);
  30937. EVP_PKEY_free(pkey3);
  30938. EVP_PKEY_free(pkey);
  30939. pkey = NULL;
  30940. pkey2 = NULL;
  30941. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30942. #define BIO_PEM_TEST_CHAR 'a'
  30943. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30944. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30945. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30946. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30947. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30948. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  30949. (long)sizeof_server_key_der_2048));
  30950. ExpectNull(pkey);
  30951. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30952. (long)sizeof_server_key_der_2048));
  30953. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  30954. WOLFSSL_FAILURE);
  30955. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  30956. WOLFSSL_FAILURE);
  30957. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30958. WOLFSSL_SUCCESS);
  30959. ExpectIntGT(BIO_pending(bio), 0);
  30960. ExpectIntEQ(BIO_pending(bio), 1679);
  30961. /* Check if the pubkey API writes only the public key */
  30962. #ifdef WOLFSSL_KEY_GEN
  30963. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30964. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30965. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30966. ExpectIntGT(BIO_pending(pub_bio), 0);
  30967. /* Previously both the private key and the pubkey calls would write
  30968. * out the private key and the PEM header was the only difference.
  30969. * The public PEM should be significantly shorter than the
  30970. * private key versison. */
  30971. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30972. #else
  30973. /* Not supported. */
  30974. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  30975. #endif
  30976. /* test creating new EVP_PKEY with good args */
  30977. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30978. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30979. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30980. pkey->pkey_sz), 0);
  30981. }
  30982. /* test of reuse of EVP_PKEY */
  30983. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30984. ExpectIntEQ(BIO_pending(bio), 0);
  30985. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30986. SSL_SUCCESS);
  30987. /* add 10 extra bytes after PEM */
  30988. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  30989. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30990. ExpectNotNull(pkey);
  30991. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30992. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30993. pkey->pkey_sz), 0);
  30994. }
  30995. /* check 10 extra bytes still there */
  30996. ExpectIntEQ(BIO_pending(bio), 10);
  30997. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30998. for (i = 0; i < 10; i++) {
  30999. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  31000. }
  31001. BIO_free(pub_bio);
  31002. BIO_free(bio);
  31003. bio = NULL;
  31004. EVP_PKEY_free(pkey);
  31005. pkey = NULL;
  31006. EVP_PKEY_free(pkey2);
  31007. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  31008. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  31009. * !NO_FILESYSTEM && !NO_BIO */
  31010. return EXPECT_RESULT();
  31011. }
  31012. /* test loading ECC key using BIO */
  31013. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  31014. {
  31015. EXPECT_DECLS;
  31016. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  31017. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31018. BIO* bio = NULL;
  31019. EVP_PKEY* pkey = NULL;
  31020. XFILE file = XBADFILE;
  31021. const char* fname = "./certs/ecc-key.pem";
  31022. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  31023. size_t sz = 0;
  31024. byte* buf = NULL;
  31025. EVP_PKEY* pkey2 = NULL;
  31026. EVP_PKEY* pkey3 = NULL;
  31027. EC_KEY* ec_key = NULL;
  31028. int nid = 0;
  31029. BIO* pub_bio = NULL;
  31030. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31031. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31032. ExpectIntGT(sz = XFTELL(file), 0);
  31033. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31034. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31035. if (buf) {
  31036. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31037. }
  31038. if (file != XBADFILE) {
  31039. XFCLOSE(file);
  31040. file = XBADFILE;
  31041. }
  31042. /* Test using BIO new mem and loading PEM private key */
  31043. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31044. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31045. BIO_free(bio);
  31046. bio = NULL;
  31047. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31048. buf = NULL;
  31049. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31050. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31051. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31052. WOLFSSL_SUCCESS);
  31053. ExpectIntGT(BIO_pending(bio), 0);
  31054. /* No parmeters. */
  31055. ExpectIntEQ(BIO_pending(bio), 227);
  31056. /* Check if the pubkey API writes only the public key */
  31057. #ifdef WOLFSSL_KEY_GEN
  31058. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  31059. ExpectIntGT(BIO_pending(pub_bio), 0);
  31060. /* Previously both the private key and the pubkey calls would write
  31061. * out the private key and the PEM header was the only difference.
  31062. * The public PEM should be significantly shorter than the
  31063. * private key versison. */
  31064. ExpectIntEQ(BIO_pending(pub_bio), 178);
  31065. #endif
  31066. BIO_free(pub_bio);
  31067. BIO_free(bio);
  31068. bio = NULL;
  31069. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31070. ExpectNotNull(pkey3 = EVP_PKEY_new());
  31071. if (pkey2 != NULL) {
  31072. pkey2->type = EVP_PKEY_EC;
  31073. }
  31074. /* Test parameter copy */
  31075. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  31076. /* Qt unit test case 1*/
  31077. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31078. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31079. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31080. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31081. #else
  31082. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31083. #endif
  31084. /* Test default digest */
  31085. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  31086. ExpectIntEQ(nid, NID_sha256);
  31087. EC_KEY_free(ec_key);
  31088. ec_key = NULL;
  31089. EVP_PKEY_free(pkey3);
  31090. pkey3 = NULL;
  31091. EVP_PKEY_free(pkey2);
  31092. pkey2 = NULL;
  31093. EVP_PKEY_free(pkey);
  31094. pkey = NULL;
  31095. /* Qt unit test case ec pkcs8 key */
  31096. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  31097. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31098. ExpectIntGT(sz = XFTELL(file), 0);
  31099. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31100. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31101. if (buf) {
  31102. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31103. }
  31104. if (file != XBADFILE) {
  31105. XFCLOSE(file);
  31106. file = XBADFILE;
  31107. }
  31108. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31109. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31110. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31111. buf = NULL;
  31112. BIO_free(bio);
  31113. bio = NULL;
  31114. ExpectNotNull(pkey3 = EVP_PKEY_new());
  31115. /* Qt unit test case */
  31116. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31117. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31118. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31119. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31120. #else
  31121. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31122. #endif
  31123. EC_KEY_free(ec_key);
  31124. EVP_PKEY_free(pkey3);
  31125. EVP_PKEY_free(pkey);
  31126. pkey = NULL;
  31127. #endif
  31128. return EXPECT_RESULT();
  31129. }
  31130. /* test loading DSA key using BIO */
  31131. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  31132. {
  31133. EXPECT_DECLS;
  31134. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  31135. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31136. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  31137. BIO* bio = NULL;
  31138. EVP_PKEY* pkey = NULL;
  31139. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  31140. /* Private DSA EVP_PKEY */
  31141. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31142. NULL));
  31143. BIO_free(bio);
  31144. bio = NULL;
  31145. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31146. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31147. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31148. NULL), 0);
  31149. #endif
  31150. #ifdef WOLFSSL_KEY_GEN
  31151. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  31152. ExpectIntEQ(BIO_pending(bio), 1178);
  31153. BIO_reset(bio);
  31154. #endif
  31155. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31156. 1);
  31157. ExpectIntEQ(BIO_pending(bio), 1196);
  31158. BIO_free(bio);
  31159. bio = NULL;
  31160. EVP_PKEY_free(pkey);
  31161. pkey = NULL;
  31162. #endif
  31163. #endif
  31164. return EXPECT_RESULT();
  31165. }
  31166. /* test loading DH key using BIO */
  31167. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  31168. {
  31169. EXPECT_DECLS;
  31170. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  31171. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31172. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  31173. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  31174. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  31175. BIO* bio = NULL;
  31176. EVP_PKEY* pkey = NULL;
  31177. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  31178. /* Private DH EVP_PKEY */
  31179. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31180. NULL));
  31181. BIO_free(bio);
  31182. bio = NULL;
  31183. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31184. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31185. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31186. NULL), 0);
  31187. #endif
  31188. #ifdef WOLFSSL_KEY_GEN
  31189. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  31190. #endif
  31191. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31192. 1);
  31193. ExpectIntEQ(BIO_pending(bio), 806);
  31194. BIO_free(bio);
  31195. bio = NULL;
  31196. EVP_PKEY_free(pkey);
  31197. pkey = NULL;
  31198. #endif
  31199. #endif
  31200. return EXPECT_RESULT();
  31201. }
  31202. static int test_wolfSSL_PEM_PrivateKey(void)
  31203. {
  31204. EXPECT_DECLS;
  31205. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31206. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  31207. #ifndef NO_BIO
  31208. BIO* bio = NULL;
  31209. #endif
  31210. EVP_PKEY* pkey = NULL;
  31211. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31212. #ifndef NO_BIO
  31213. /* test creating new EVP_PKEY with bad arg */
  31214. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  31215. /* Test bad EVP_PKEY type. */
  31216. /* New HMAC EVP_PKEY */
  31217. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  31218. ExpectNotNull(pkey = EVP_PKEY_new());
  31219. if (pkey != NULL) {
  31220. pkey->type = EVP_PKEY_HMAC;
  31221. }
  31222. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31223. 0);
  31224. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31225. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31226. NULL), 0);
  31227. #endif
  31228. #ifdef WOLFSSL_KEY_GEN
  31229. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  31230. #endif
  31231. EVP_PKEY_free(pkey);
  31232. pkey = NULL;
  31233. BIO_free(bio);
  31234. bio = NULL;
  31235. /* key is DES encrypted */
  31236. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  31237. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  31238. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31239. {
  31240. XFILE f = XBADFILE;
  31241. wc_pem_password_cb* passwd_cb = NULL;
  31242. void* passwd_cb_userdata;
  31243. SSL_CTX* ctx = NULL;
  31244. char passwd[] = "bad password";
  31245. #ifndef WOLFSSL_NO_TLS12
  31246. #ifndef NO_WOLFSSL_SERVER
  31247. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31248. #else
  31249. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31250. #endif
  31251. #else
  31252. #ifndef NO_WOLFSSL_SERVER
  31253. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31254. #else
  31255. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31256. #endif
  31257. #endif
  31258. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31259. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31260. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31261. ExpectNull(passwd_cb_userdata =
  31262. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31263. /* fail case with password call back */
  31264. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31265. (void*)passwd));
  31266. BIO_free(bio);
  31267. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31268. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31269. (void*)passwd));
  31270. BIO_free(bio);
  31271. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  31272. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31273. if ((bio == NULL) && (f != XBADFILE)) {
  31274. XFCLOSE(f);
  31275. }
  31276. /* use callback that works */
  31277. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31278. (void*)"yassl123"));
  31279. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31280. EVP_PKEY_free(pkey);
  31281. pkey = NULL;
  31282. BIO_free(bio);
  31283. bio = NULL;
  31284. SSL_CTX_free(ctx);
  31285. }
  31286. #endif /* !defined(NO_DES3) */
  31287. #endif /* !NO_BIO */
  31288. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31289. {
  31290. unsigned char buf[2048];
  31291. size_t bytes = 0;
  31292. XFILE f = XBADFILE;
  31293. SSL_CTX* ctx = NULL;
  31294. #ifndef WOLFSSL_NO_TLS12
  31295. #ifndef NO_WOLFSSL_SERVER
  31296. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31297. #else
  31298. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31299. #endif
  31300. #else
  31301. #ifndef NO_WOLFSSL_SERVER
  31302. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31303. #else
  31304. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31305. #endif
  31306. #endif
  31307. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  31308. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  31309. if (f != XBADFILE)
  31310. XFCLOSE(f);
  31311. server_key = buf;
  31312. pkey = NULL;
  31313. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, (long int)bytes));
  31314. ExpectNull(pkey);
  31315. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, (long int)bytes));
  31316. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31317. EVP_PKEY_free(pkey);
  31318. pkey = NULL;
  31319. SSL_CTX_free(ctx);
  31320. server_key = NULL;
  31321. }
  31322. #endif
  31323. #ifndef NO_BIO
  31324. (void)bio;
  31325. #endif
  31326. (void)pkey;
  31327. (void)server_key;
  31328. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31329. return EXPECT_RESULT();
  31330. }
  31331. static int test_wolfSSL_PEM_file_RSAKey(void)
  31332. {
  31333. EXPECT_DECLS;
  31334. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31335. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31336. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31337. RSA* rsa = NULL;
  31338. XFILE fp = XBADFILE;
  31339. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31340. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31341. if (fp != XBADFILE)
  31342. XFCLOSE(fp);
  31343. ExpectIntEQ(RSA_size(rsa), 256);
  31344. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31345. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31346. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31347. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31348. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31349. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31350. RSA_free(rsa);
  31351. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31352. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31353. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31354. return EXPECT_RESULT();
  31355. }
  31356. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31357. {
  31358. EXPECT_DECLS;
  31359. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31360. !defined(NO_FILESYSTEM) && \
  31361. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31362. RSA* rsa = NULL;
  31363. XFILE f = NULL;
  31364. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  31365. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31366. ExpectIntEQ(RSA_size(rsa), 256);
  31367. if (f != XBADFILE) {
  31368. XFCLOSE(f);
  31369. f = XBADFILE;
  31370. }
  31371. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31372. NULL), WOLFSSL_FAILURE);
  31373. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31374. NULL), WOLFSSL_FAILURE);
  31375. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31376. WOLFSSL_SUCCESS);
  31377. RSA_free(rsa);
  31378. #ifdef HAVE_ECC
  31379. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  31380. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31381. if (f != XBADFILE)
  31382. XFCLOSE(f);
  31383. #endif /* HAVE_ECC */
  31384. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31385. return EXPECT_RESULT();
  31386. }
  31387. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31388. {
  31389. EXPECT_DECLS;
  31390. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31391. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31392. XFILE file = XBADFILE;
  31393. const char* fname = "./certs/client-keyPub.pem";
  31394. RSA *rsa = NULL;
  31395. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31396. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31397. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31398. ExpectIntEQ(RSA_size(rsa), 256);
  31399. RSA_free(rsa);
  31400. if (file != XBADFILE)
  31401. XFCLOSE(file);
  31402. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31403. return EXPECT_RESULT();
  31404. }
  31405. #ifndef NO_BIO
  31406. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31407. {
  31408. EXPECT_DECLS;
  31409. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31410. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31411. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31412. RSA* rsa = NULL;
  31413. BIO* bio = NULL;
  31414. /* PrivateKey */
  31415. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31416. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31417. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31418. ExpectNotNull(rsa);
  31419. ExpectIntEQ(RSA_size(rsa), 256);
  31420. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31421. NULL), WOLFSSL_FAILURE);
  31422. BIO_free(bio);
  31423. bio = NULL;
  31424. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31425. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31426. NULL), WOLFSSL_SUCCESS);
  31427. BIO_free(bio);
  31428. bio = NULL;
  31429. RSA_free(rsa);
  31430. rsa = NULL;
  31431. /* PUBKEY */
  31432. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31433. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31434. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31435. ExpectIntEQ(RSA_size(rsa), 256);
  31436. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31437. BIO_free(bio);
  31438. bio = NULL;
  31439. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31440. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31441. BIO_free(bio);
  31442. bio = NULL;
  31443. RSA_free(rsa);
  31444. rsa = NULL;
  31445. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31446. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31447. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31448. BIO_free(bio);
  31449. bio = NULL;
  31450. RSA_free(rsa);
  31451. rsa = NULL;
  31452. #ifdef HAVE_ECC
  31453. /* ensure that non-rsa keys do not work */
  31454. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31455. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31456. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31457. BIO_free(bio);
  31458. bio = NULL;
  31459. RSA_free(rsa);
  31460. rsa = NULL;
  31461. #endif /* HAVE_ECC */
  31462. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31463. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31464. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31465. return EXPECT_RESULT();
  31466. }
  31467. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31468. {
  31469. EXPECT_DECLS;
  31470. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31471. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31472. RSA* rsa = NULL;
  31473. RSA* rsa_dup = NULL;
  31474. BIO* bio = NULL;
  31475. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31476. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31477. ExpectIntEQ(RSA_size(rsa), 256);
  31478. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31479. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  31480. /* Test duplicating empty key. */
  31481. ExpectNotNull(rsa_dup = RSA_new());
  31482. ExpectNull(RSAPublicKey_dup(rsa_dup));
  31483. RSA_free(rsa_dup);
  31484. rsa_dup = NULL;
  31485. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31486. ExpectPtrNE(rsa_dup, rsa);
  31487. #endif
  31488. /* test if valgrind complains about unreleased memory */
  31489. RSA_up_ref(rsa);
  31490. RSA_free(rsa);
  31491. BIO_free(bio);
  31492. bio = NULL;
  31493. RSA_free(rsa);
  31494. rsa = NULL;
  31495. RSA_free(rsa_dup);
  31496. rsa_dup = NULL;
  31497. #ifdef HAVE_ECC
  31498. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31499. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31500. BIO_free(bio);
  31501. #endif /* HAVE_ECC */
  31502. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31503. return EXPECT_RESULT();
  31504. }
  31505. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31506. {
  31507. EXPECT_DECLS;
  31508. #ifndef HAVE_SELFTEST
  31509. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31510. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31511. DSA* dsa = NULL;
  31512. BIO* bio = NULL;
  31513. /* PrivateKey */
  31514. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31515. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31516. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31517. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31518. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31519. NULL), WOLFSSL_FAILURE);
  31520. BIO_free(bio);
  31521. bio = NULL;
  31522. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31523. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  31524. NULL), WOLFSSL_SUCCESS);
  31525. BIO_free(bio);
  31526. bio = NULL;
  31527. DSA_free(dsa);
  31528. dsa = NULL;
  31529. /* PUBKEY */
  31530. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31531. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31532. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31533. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31534. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31535. BIO_free(bio);
  31536. bio = NULL;
  31537. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31538. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31539. BIO_free(bio);
  31540. bio = NULL;
  31541. DSA_free(dsa);
  31542. dsa = NULL;
  31543. #ifdef HAVE_ECC
  31544. /* ensure that non-dsa keys do not work */
  31545. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31546. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31547. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31548. BIO_free(bio);
  31549. bio = NULL;
  31550. DSA_free(dsa);
  31551. dsa = NULL;
  31552. #endif /* HAVE_ECC */
  31553. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31554. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31555. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31556. #endif /* HAVE_SELFTEST */
  31557. return EXPECT_RESULT();
  31558. }
  31559. static int test_wolfSSL_PEM_bio_ECKey(void)
  31560. {
  31561. EXPECT_DECLS;
  31562. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31563. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31564. EC_KEY* ec = NULL;
  31565. EC_KEY* ec2;
  31566. BIO* bio = NULL;
  31567. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31568. unsigned char* pem = NULL;
  31569. int pLen;
  31570. #endif
  31571. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31572. "MAA=\n"
  31573. "-----END PUBLIC KEY-----";
  31574. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31575. "MAA=\n"
  31576. "-----END EC PRIVATE KEY-----";
  31577. /* PrivateKey */
  31578. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31579. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31580. ec2 = NULL;
  31581. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31582. ExpectIntEQ(ec == ec2, 1);
  31583. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31584. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31585. NULL), WOLFSSL_FAILURE);
  31586. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31587. NULL), WOLFSSL_FAILURE);
  31588. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31589. WOLFSSL_FAILURE);
  31590. BIO_free(bio);
  31591. bio = NULL;
  31592. /* Public key data - fail. */
  31593. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31594. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31595. BIO_free(bio);
  31596. bio = NULL;
  31597. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31598. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31599. NULL), WOLFSSL_SUCCESS);
  31600. BIO_free(bio);
  31601. bio = NULL;
  31602. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31603. NULL),WOLFSSL_FAILURE);
  31604. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31605. WOLFSSL_FAILURE);
  31606. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31607. WOLFSSL_FAILURE);
  31608. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31609. WOLFSSL_SUCCESS);
  31610. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31611. NULL), 0);
  31612. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31613. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31614. NULL), 0);
  31615. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31616. NULL), 0);
  31617. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31618. &pLen), 0);
  31619. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31620. &pLen), 0);
  31621. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31622. &pLen), 0);
  31623. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31624. NULL), 0);
  31625. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31626. &pLen), 1);
  31627. ExpectIntGT(pLen, 0);
  31628. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31629. #endif
  31630. EC_KEY_free(ec);
  31631. ec = NULL;
  31632. /* PUBKEY */
  31633. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31634. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31635. ec2 = NULL;
  31636. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31637. ExpectIntEQ(ec == ec2, 1);
  31638. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31639. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31640. BIO_free(bio);
  31641. bio = NULL;
  31642. /* Test 0x30, 0x00 fails. */
  31643. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31644. sizeof(ec_key_bad_1)));
  31645. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31646. BIO_free(bio);
  31647. bio = NULL;
  31648. /* Private key data - fail. */
  31649. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31650. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31651. BIO_free(bio);
  31652. bio = NULL;
  31653. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31654. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31655. BIO_free(bio);
  31656. bio = NULL;
  31657. /* Same test as above, but with a file pointer rather than a BIO. */
  31658. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31659. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31660. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31661. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31662. EC_KEY_free(ec);
  31663. ec = NULL;
  31664. #ifndef NO_RSA
  31665. /* ensure that non-ec keys do not work */
  31666. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31667. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31668. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31669. BIO_free(bio);
  31670. bio = NULL;
  31671. EC_KEY_free(ec);
  31672. ec = NULL;
  31673. #endif /* !NO_RSA */
  31674. /* Test 0x30, 0x00 fails. */
  31675. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31676. sizeof(ec_priv_key_bad_1)));
  31677. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31678. BIO_free(bio);
  31679. bio = NULL;
  31680. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31681. return EXPECT_RESULT();
  31682. }
  31683. static int test_wolfSSL_PEM_PUBKEY(void)
  31684. {
  31685. EXPECT_DECLS;
  31686. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31687. BIO* bio = NULL;
  31688. EVP_PKEY* pkey = NULL;
  31689. /* test creating new EVP_PKEY with bad arg */
  31690. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31691. /* test loading ECC key using BIO */
  31692. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31693. {
  31694. XFILE file = XBADFILE;
  31695. const char* fname = "./certs/ecc-client-keyPub.pem";
  31696. size_t sz = 0;
  31697. byte* buf = NULL;
  31698. EVP_PKEY* pkey2 = NULL;
  31699. EC_KEY* ec_key = NULL;
  31700. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31701. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31702. ExpectIntGT(sz = XFTELL(file), 0);
  31703. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31704. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31705. if (buf != NULL) {
  31706. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31707. }
  31708. if (file != XBADFILE) {
  31709. XFCLOSE(file);
  31710. }
  31711. /* Test using BIO new mem and loading PEM private key */
  31712. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31713. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31714. BIO_free(bio);
  31715. bio = NULL;
  31716. EVP_PKEY_free(pkey);
  31717. pkey = NULL;
  31718. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31719. ExpectNotNull(pkey = EVP_PKEY_new());
  31720. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  31721. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31722. BIO_free(bio);
  31723. bio = NULL;
  31724. /* Qt unit test case*/
  31725. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31726. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31727. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31728. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31729. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31730. #else
  31731. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31732. #endif
  31733. EC_KEY_free(ec_key);
  31734. EVP_PKEY_free(pkey2);
  31735. EVP_PKEY_free(pkey);
  31736. pkey = NULL;
  31737. }
  31738. #endif
  31739. (void)bio;
  31740. (void)pkey;
  31741. #endif
  31742. return EXPECT_RESULT();
  31743. }
  31744. #endif /* !NO_BIO */
  31745. static int test_DSA_do_sign_verify(void)
  31746. {
  31747. EXPECT_DECLS;
  31748. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31749. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31750. !defined(NO_DSA)
  31751. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31752. DSA_SIG* sig = NULL;
  31753. DSA* dsa = NULL;
  31754. word32 bytes;
  31755. byte sigBin[DSA_SIG_SIZE];
  31756. int dsacheck;
  31757. #ifdef USE_CERT_BUFFERS_1024
  31758. byte tmp[ONEK_BUF];
  31759. XMEMSET(tmp, 0, sizeof(tmp));
  31760. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31761. bytes = sizeof_dsa_key_der_1024;
  31762. #elif defined(USE_CERT_BUFFERS_2048)
  31763. byte tmp[TWOK_BUF];
  31764. XMEMSET(tmp, 0, sizeof(tmp));
  31765. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31766. bytes = sizeof_dsa_key_der_2048;
  31767. #else
  31768. byte tmp[TWOK_BUF];
  31769. XFILE fp = XBADFILE;
  31770. XMEMSET(tmp, 0, sizeof(tmp));
  31771. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  31772. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  31773. if (fp != XBADFILE)
  31774. XFCLOSE(fp);
  31775. #endif /* END USE_CERT_BUFFERS_1024 */
  31776. XMEMSET(digest, 202, sizeof(digest));
  31777. ExpectNotNull(dsa = DSA_new());
  31778. ExpectIntEQ(DSA_LoadDer(dsa, tmp, (int)bytes), 1);
  31779. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31780. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31781. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31782. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31783. DSA_SIG_free(sig);
  31784. DSA_free(dsa);
  31785. #endif
  31786. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31787. return EXPECT_RESULT();
  31788. }
  31789. static int test_wolfSSL_tmp_dh(void)
  31790. {
  31791. EXPECT_DECLS;
  31792. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31793. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31794. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31795. byte buff[6000];
  31796. char file[] = "./certs/dsaparams.pem";
  31797. XFILE f = XBADFILE;
  31798. int bytes = 0;
  31799. DSA* dsa = NULL;
  31800. DH* dh = NULL;
  31801. #if defined(WOLFSSL_DH_EXTRA) && \
  31802. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31803. DH* dh2 = NULL;
  31804. #endif
  31805. BIO* bio = NULL;
  31806. SSL* ssl = NULL;
  31807. SSL_CTX* ctx = NULL;
  31808. #ifndef NO_WOLFSSL_SERVER
  31809. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31810. #else
  31811. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31812. #endif
  31813. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31814. WOLFSSL_FILETYPE_PEM));
  31815. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31816. WOLFSSL_FILETYPE_PEM));
  31817. ExpectNotNull(ssl = SSL_new(ctx));
  31818. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  31819. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  31820. if (f != XBADFILE)
  31821. XFCLOSE(f);
  31822. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31823. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31824. ExpectNotNull(dsa);
  31825. dh = wolfSSL_DSA_dup_DH(dsa);
  31826. ExpectNotNull(dh);
  31827. #if defined(WOLFSSL_DH_EXTRA) && \
  31828. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31829. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  31830. #endif
  31831. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31832. #ifndef NO_WOLFSSL_SERVER
  31833. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31834. #else
  31835. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31836. #endif
  31837. BIO_free(bio);
  31838. DSA_free(dsa);
  31839. DH_free(dh);
  31840. dh = NULL;
  31841. #if defined(WOLFSSL_DH_EXTRA) && \
  31842. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31843. DH_free(dh2);
  31844. dh2 = NULL;
  31845. #endif
  31846. SSL_free(ssl);
  31847. SSL_CTX_free(ctx);
  31848. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31849. #endif
  31850. return EXPECT_RESULT();
  31851. }
  31852. static int test_wolfSSL_ctrl(void)
  31853. {
  31854. EXPECT_DECLS;
  31855. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31856. byte buff[6000];
  31857. BIO* bio = NULL;
  31858. int bytes;
  31859. BUF_MEM* ptr = NULL;
  31860. XMEMSET(buff, 0, sizeof(buff));
  31861. bytes = sizeof(buff);
  31862. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31863. ExpectNotNull(BIO_s_socket());
  31864. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31865. /* needs tested after stubs filled out @TODO
  31866. SSL_ctrl
  31867. SSL_CTX_ctrl
  31868. */
  31869. BIO_free(bio);
  31870. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31871. return EXPECT_RESULT();
  31872. }
  31873. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31874. {
  31875. EXPECT_DECLS;
  31876. #ifdef OPENSSL_EXTRA
  31877. static const unsigned char pw[] = "password";
  31878. static const int pwSz = sizeof(pw) - 1;
  31879. size_t checkPwSz = 0;
  31880. const unsigned char* checkPw = NULL;
  31881. WOLFSSL_EVP_PKEY* key = NULL;
  31882. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31883. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31884. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31885. pwSz));
  31886. if (key != NULL) {
  31887. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31888. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31889. ExpectIntEQ(key->pkey_sz, pwSz);
  31890. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31891. }
  31892. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31893. ExpectIntEQ((int)checkPwSz, pwSz);
  31894. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31895. wolfSSL_EVP_PKEY_free(key);
  31896. key = NULL;
  31897. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31898. 0));
  31899. ExpectIntEQ(key->pkey_sz, 0);
  31900. if (EXPECT_SUCCESS()) {
  31901. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31902. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31903. }
  31904. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31905. ExpectIntEQ((int)checkPwSz, 0);
  31906. wolfSSL_EVP_PKEY_free(key);
  31907. key = NULL;
  31908. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31909. 0));
  31910. ExpectIntEQ(key->pkey_sz, 0);
  31911. if (EXPECT_SUCCESS()) {
  31912. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31913. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31914. }
  31915. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31916. ExpectIntEQ((int)checkPwSz, 0);
  31917. wolfSSL_EVP_PKEY_free(key);
  31918. key = NULL;
  31919. #endif /* OPENSSL_EXTRA */
  31920. return EXPECT_RESULT();
  31921. }
  31922. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31923. {
  31924. EXPECT_DECLS;
  31925. #ifdef OPENSSL_EXTRA
  31926. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31927. const char *priv = "ABCDEFGHIJKLMNOP";
  31928. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31929. WOLFSSL_EVP_PKEY* key = NULL;
  31930. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31931. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31932. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31933. NULL, (const unsigned char *)priv, 0, cipher));
  31934. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31935. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31936. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31937. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31938. wolfSSL_EVP_PKEY_free(key);
  31939. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31940. #endif /* OPENSSL_EXTRA */
  31941. return EXPECT_RESULT();
  31942. }
  31943. static int test_wolfSSL_EVP_Digest(void)
  31944. {
  31945. EXPECT_DECLS;
  31946. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31947. const char* in = "abc";
  31948. int inLen = (int)XSTRLEN(in);
  31949. byte out[WC_SHA256_DIGEST_SIZE];
  31950. unsigned int outLen;
  31951. const char* expOut =
  31952. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31953. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31954. "\x15\xAD";
  31955. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31956. "SHA256", NULL), 1);
  31957. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31958. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31959. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31960. return EXPECT_RESULT();
  31961. }
  31962. static int test_wolfSSL_EVP_Digest_all(void)
  31963. {
  31964. EXPECT_DECLS;
  31965. #ifdef OPENSSL_EXTRA
  31966. const char* digests[] = {
  31967. #ifndef NO_MD5
  31968. "MD5",
  31969. #endif
  31970. #ifndef NO_SHA
  31971. "SHA",
  31972. #endif
  31973. #ifdef WOLFSSL_SHA224
  31974. "SHA224",
  31975. #endif
  31976. #ifndef NO_SHA256
  31977. "SHA256",
  31978. #endif
  31979. #ifdef WOLFSSL_SHA384
  31980. "SHA384",
  31981. #endif
  31982. #ifdef WOLFSSL_SHA512
  31983. "SHA512",
  31984. #endif
  31985. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31986. "SHA512_224",
  31987. #endif
  31988. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31989. "SHA512_256",
  31990. #endif
  31991. #ifdef WOLFSSL_SHA3
  31992. #ifndef WOLFSSL_NOSHA3_224
  31993. "SHA3_224",
  31994. #endif
  31995. #ifndef WOLFSSL_NOSHA3_256
  31996. "SHA3_256",
  31997. #endif
  31998. "SHA3_384",
  31999. #ifndef WOLFSSL_NOSHA3_512
  32000. "SHA3_512",
  32001. #endif
  32002. #endif /* WOLFSSL_SHA3 */
  32003. NULL
  32004. };
  32005. const char** d;
  32006. const unsigned char in[] = "abc";
  32007. int inLen = XSTR_SIZEOF(in);
  32008. byte out[WC_MAX_DIGEST_SIZE];
  32009. unsigned int outLen;
  32010. for (d = digests; *d != NULL; d++) {
  32011. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  32012. ExpectIntGT(outLen, 0);
  32013. ExpectIntEQ(EVP_MD_size(*d), outLen);
  32014. }
  32015. #endif
  32016. return EXPECT_RESULT();
  32017. }
  32018. static int test_wolfSSL_EVP_MD_size(void)
  32019. {
  32020. EXPECT_DECLS;
  32021. #ifdef OPENSSL_EXTRA
  32022. WOLFSSL_EVP_MD_CTX mdCtx;
  32023. #ifdef WOLFSSL_SHA3
  32024. #ifndef WOLFSSL_NOSHA3_224
  32025. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32026. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  32027. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  32028. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  32029. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32030. #endif
  32031. #ifndef WOLFSSL_NOSHA3_256
  32032. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32033. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  32034. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  32035. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  32036. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32037. #endif
  32038. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32039. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  32040. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  32041. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  32042. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32043. #ifndef WOLFSSL_NOSHA3_512
  32044. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32045. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  32046. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  32047. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  32048. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32049. #endif
  32050. #endif /* WOLFSSL_SHA3 */
  32051. #ifndef NO_SHA256
  32052. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32053. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  32054. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32055. WC_SHA256_DIGEST_SIZE);
  32056. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32057. WC_SHA256_BLOCK_SIZE);
  32058. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  32059. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  32060. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32061. #endif
  32062. #ifndef NO_MD5
  32063. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32064. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  32065. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32066. WC_MD5_DIGEST_SIZE);
  32067. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32068. WC_MD5_BLOCK_SIZE);
  32069. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  32070. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  32071. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32072. #endif
  32073. #ifdef WOLFSSL_SHA224
  32074. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32075. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  32076. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32077. WC_SHA224_DIGEST_SIZE);
  32078. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32079. WC_SHA224_BLOCK_SIZE);
  32080. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  32081. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  32082. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32083. #endif
  32084. #ifdef WOLFSSL_SHA384
  32085. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32086. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  32087. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32088. WC_SHA384_DIGEST_SIZE);
  32089. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32090. WC_SHA384_BLOCK_SIZE);
  32091. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  32092. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  32093. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32094. #endif
  32095. #ifdef WOLFSSL_SHA512
  32096. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32097. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  32098. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32099. WC_SHA512_DIGEST_SIZE);
  32100. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32101. WC_SHA512_BLOCK_SIZE);
  32102. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  32103. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  32104. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32105. #endif
  32106. #ifndef NO_SHA
  32107. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32108. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  32109. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32110. WC_SHA_DIGEST_SIZE);
  32111. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32112. WC_SHA_BLOCK_SIZE);
  32113. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  32114. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  32115. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32116. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32117. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  32118. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32119. WC_SHA_DIGEST_SIZE);
  32120. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32121. WC_SHA_BLOCK_SIZE);
  32122. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  32123. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  32124. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32125. #endif
  32126. /* error case */
  32127. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32128. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  32129. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32130. BAD_FUNC_ARG);
  32131. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  32132. /* Cleanup is valid on uninit'ed struct */
  32133. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32134. #endif /* OPENSSL_EXTRA */
  32135. return EXPECT_RESULT();
  32136. }
  32137. static int test_wolfSSL_EVP_MD_pkey_type(void)
  32138. {
  32139. EXPECT_DECLS;
  32140. #ifdef OPENSSL_EXTRA
  32141. const WOLFSSL_EVP_MD* md;
  32142. #ifndef NO_MD5
  32143. ExpectNotNull(md = EVP_md5());
  32144. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  32145. #endif
  32146. #ifndef NO_SHA
  32147. ExpectNotNull(md = EVP_sha1());
  32148. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  32149. #endif
  32150. #ifdef WOLFSSL_SHA224
  32151. ExpectNotNull(md = EVP_sha224());
  32152. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  32153. #endif
  32154. ExpectNotNull(md = EVP_sha256());
  32155. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  32156. #ifdef WOLFSSL_SHA384
  32157. ExpectNotNull(md = EVP_sha384());
  32158. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  32159. #endif
  32160. #ifdef WOLFSSL_SHA512
  32161. ExpectNotNull(md = EVP_sha512());
  32162. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  32163. #endif
  32164. #endif
  32165. return EXPECT_RESULT();
  32166. }
  32167. #ifdef OPENSSL_EXTRA
  32168. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  32169. size_t testKeySz, const char* testData, size_t testDataSz,
  32170. const byte* testResult, size_t testResultSz)
  32171. {
  32172. EXPECT_DECLS;
  32173. unsigned char check[WC_MAX_DIGEST_SIZE];
  32174. size_t checkSz = -1;
  32175. WOLFSSL_EVP_PKEY* key = NULL;
  32176. WOLFSSL_EVP_MD_CTX mdCtx;
  32177. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  32178. testKey, (int)testKeySz));
  32179. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32180. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32181. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32182. (unsigned int)testDataSz), 1);
  32183. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32184. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32185. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32186. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32187. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32188. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32189. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32190. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32191. (unsigned int)testDataSz), 1);
  32192. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32193. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32194. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32195. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32196. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32197. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32198. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32199. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32200. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32201. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32202. (unsigned int)testDataSz - 4), 1);
  32203. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32204. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32205. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32206. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32207. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32208. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32209. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32210. (unsigned int)testDataSz - 4), 1);
  32211. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32212. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32213. wolfSSL_EVP_PKEY_free(key);
  32214. return EXPECT_RESULT();
  32215. }
  32216. #endif
  32217. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  32218. {
  32219. EXPECT_DECLS;
  32220. #ifdef OPENSSL_EXTRA
  32221. static const unsigned char testKey[] =
  32222. {
  32223. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32224. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32225. 0x0b, 0x0b, 0x0b, 0x0b
  32226. };
  32227. static const char testData[] = "Hi There";
  32228. #ifdef WOLFSSL_SHA224
  32229. static const unsigned char testResultSha224[] =
  32230. {
  32231. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  32232. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  32233. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  32234. 0x53, 0x68, 0x4b, 0x22
  32235. };
  32236. #endif
  32237. #ifndef NO_SHA256
  32238. static const unsigned char testResultSha256[] =
  32239. {
  32240. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  32241. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  32242. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  32243. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  32244. };
  32245. #endif
  32246. #ifdef WOLFSSL_SHA384
  32247. static const unsigned char testResultSha384[] =
  32248. {
  32249. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32250. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32251. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32252. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32253. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32254. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32255. };
  32256. #endif
  32257. #ifdef WOLFSSL_SHA512
  32258. static const unsigned char testResultSha512[] =
  32259. {
  32260. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32261. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32262. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32263. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32264. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32265. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32266. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32267. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32268. };
  32269. #endif
  32270. #ifdef WOLFSSL_SHA3
  32271. #ifndef WOLFSSL_NOSHA3_224
  32272. static const unsigned char testResultSha3_224[] =
  32273. {
  32274. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32275. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32276. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32277. 0xf3, 0xc8, 0x60, 0xf7
  32278. };
  32279. #endif
  32280. #ifndef WOLFSSL_NOSHA3_256
  32281. static const unsigned char testResultSha3_256[] =
  32282. {
  32283. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32284. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32285. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32286. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32287. };
  32288. #endif
  32289. #ifndef WOLFSSL_NOSHA3_384
  32290. static const unsigned char testResultSha3_384[] =
  32291. {
  32292. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32293. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32294. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32295. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32296. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32297. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32298. };
  32299. #endif
  32300. #ifndef WOLFSSL_NOSHA3_512
  32301. static const unsigned char testResultSha3_512[] =
  32302. {
  32303. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32304. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32305. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32306. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32307. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32308. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32309. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32310. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32311. };
  32312. #endif
  32313. #endif
  32314. #ifndef NO_SHA256
  32315. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  32316. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  32317. sizeof(testResultSha256)), TEST_SUCCESS);
  32318. #endif
  32319. #ifdef WOLFSSL_SHA224
  32320. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  32321. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  32322. sizeof(testResultSha224)), TEST_SUCCESS);
  32323. #endif
  32324. #ifdef WOLFSSL_SHA384
  32325. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  32326. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  32327. sizeof(testResultSha384)), TEST_SUCCESS);
  32328. #endif
  32329. #ifdef WOLFSSL_SHA512
  32330. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  32331. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  32332. sizeof(testResultSha512)), TEST_SUCCESS);
  32333. #endif
  32334. #ifdef WOLFSSL_SHA3
  32335. #ifndef WOLFSSL_NOSHA3_224
  32336. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  32337. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  32338. sizeof(testResultSha3_224)), TEST_SUCCESS);
  32339. #endif
  32340. #ifndef WOLFSSL_NOSHA3_256
  32341. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  32342. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  32343. sizeof(testResultSha3_256)), TEST_SUCCESS);
  32344. #endif
  32345. #ifndef WOLFSSL_NOSHA3_384
  32346. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  32347. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  32348. sizeof(testResultSha3_384)), TEST_SUCCESS);
  32349. #endif
  32350. #ifndef WOLFSSL_NOSHA3_512
  32351. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  32352. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  32353. sizeof(testResultSha3_512)), TEST_SUCCESS);
  32354. #endif
  32355. #endif
  32356. #endif /* OPENSSL_EXTRA */
  32357. return EXPECT_RESULT();
  32358. }
  32359. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32360. {
  32361. EXPECT_DECLS;
  32362. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  32363. WOLFSSL_EVP_PKEY* privKey = NULL;
  32364. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32365. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  32366. const char testData[] = "Hi There";
  32367. WOLFSSL_EVP_MD_CTX mdCtx;
  32368. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32369. int ret;
  32370. size_t checkSz = -1;
  32371. int sz = 2048 / 8;
  32372. const unsigned char* cp;
  32373. const unsigned char* p;
  32374. unsigned char check[2048/8];
  32375. size_t i;
  32376. int paddings[] = {
  32377. RSA_PKCS1_PADDING,
  32378. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32379. RSA_PKCS1_PSS_PADDING,
  32380. #endif
  32381. };
  32382. cp = client_key_der_2048;
  32383. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32384. sizeof_client_key_der_2048)));
  32385. p = client_keypub_der_2048;
  32386. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32387. sizeof_client_keypub_der_2048)));
  32388. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32389. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32390. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32391. NULL, privKey), 1);
  32392. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32393. (unsigned int)XSTRLEN(testData)), 1);
  32394. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32395. ExpectIntEQ((int)checkSz, sz);
  32396. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32397. ExpectIntEQ((int)checkSz,sz);
  32398. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32399. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32400. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  32401. ExpectIntEQ(ret, 1);
  32402. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32403. ExpectIntEQ(ret, 1);
  32404. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32405. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32406. NULL, pubKey), 1);
  32407. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32408. (unsigned int)XSTRLEN(testData)),
  32409. 1);
  32410. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32411. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32412. ExpectIntEQ(ret, 1);
  32413. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32414. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32415. NULL, privKey), 1);
  32416. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32417. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32418. ExpectIntEQ((int)checkSz, sz);
  32419. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32420. ExpectIntEQ((int)checkSz, sz);
  32421. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32422. (unsigned int)XSTRLEN(testData) - 4), 1);
  32423. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32424. ExpectIntEQ((int)checkSz, sz);
  32425. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32426. ExpectIntEQ(ret, 1);
  32427. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32428. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32429. NULL, pubKey), 1);
  32430. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32431. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32432. (unsigned int)XSTRLEN(testData) - 4),
  32433. 1);
  32434. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32435. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32436. ExpectIntEQ(ret, 1);
  32437. /* Check all signing padding types */
  32438. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32439. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32440. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32441. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32442. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32443. paddings[i]), 1);
  32444. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32445. (unsigned int)XSTRLEN(testData)), 1);
  32446. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32447. ExpectIntEQ((int)checkSz, sz);
  32448. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32449. ExpectIntEQ((int)checkSz,sz);
  32450. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32451. ExpectIntEQ(ret, 1);
  32452. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32453. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32454. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32455. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32456. paddings[i]), 1);
  32457. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32458. (unsigned int)XSTRLEN(testData)), 1);
  32459. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32460. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32461. ExpectIntEQ(ret, 1);
  32462. }
  32463. wolfSSL_EVP_PKEY_free(pubKey);
  32464. wolfSSL_EVP_PKEY_free(privKey);
  32465. #endif
  32466. return EXPECT_RESULT();
  32467. }
  32468. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32469. {
  32470. EXPECT_DECLS;
  32471. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32472. WOLFSSL_EVP_PKEY* privKey = NULL;
  32473. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32474. const char testData[] = "Hi There";
  32475. WOLFSSL_EVP_MD_CTX mdCtx;
  32476. int ret;
  32477. size_t checkSz = -1;
  32478. const unsigned char* cp;
  32479. const unsigned char* p;
  32480. unsigned char check[2048/8];
  32481. cp = ecc_clikey_der_256;
  32482. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32483. sizeof_ecc_clikey_der_256));
  32484. p = ecc_clikeypub_der_256;
  32485. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32486. sizeof_ecc_clikeypub_der_256)));
  32487. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32488. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32489. NULL, privKey), 1);
  32490. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32491. (unsigned int)XSTRLEN(testData)), 1);
  32492. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32493. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32494. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32495. ExpectIntEQ(ret, 1);
  32496. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32497. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32498. NULL, pubKey), 1);
  32499. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32500. (unsigned int)XSTRLEN(testData)),
  32501. 1);
  32502. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32503. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32504. ExpectIntEQ(ret, 1);
  32505. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32506. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32507. NULL, privKey), 1);
  32508. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32509. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32510. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32511. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32512. (unsigned int)XSTRLEN(testData) - 4), 1);
  32513. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32514. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32515. ExpectIntEQ(ret, 1);
  32516. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32517. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32518. NULL, pubKey), 1);
  32519. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32520. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32521. (unsigned int)XSTRLEN(testData) - 4),
  32522. 1);
  32523. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32524. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32525. ExpectIntEQ(ret, 1);
  32526. wolfSSL_EVP_PKEY_free(pubKey);
  32527. wolfSSL_EVP_PKEY_free(privKey);
  32528. #endif
  32529. return EXPECT_RESULT();
  32530. }
  32531. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32532. {
  32533. EXPECT_DECLS;
  32534. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32535. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32536. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32537. char caFile[] = "./certs/client-ca.pem";
  32538. char clientFile[] = "./certs/client-cert.pem";
  32539. SSL_CTX* ctx = NULL;
  32540. X509* x509 = NULL;
  32541. BIO *bio = NULL;
  32542. X509 *cert = NULL;
  32543. X509 *ca = NULL;
  32544. STACK_OF(X509) *chain = NULL;
  32545. STACK_OF(X509) *chain2 = NULL;
  32546. #ifndef NO_WOLFSSL_SERVER
  32547. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32548. #else
  32549. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32550. #endif
  32551. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  32552. WOLFSSL_FILETYPE_PEM));
  32553. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32554. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  32555. WOLFSSL_FILETYPE_PEM));
  32556. /* additional test of getting EVP_PKEY key size from X509
  32557. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32558. * allowed with user RSA */
  32559. {
  32560. EVP_PKEY* pkey = NULL;
  32561. #if defined(HAVE_ECC)
  32562. X509* ecX509 = NULL;
  32563. #endif /* HAVE_ECC */
  32564. ExpectNotNull(pkey = X509_get_pubkey(x509));
  32565. /* current RSA key is 2048 bit (256 bytes) */
  32566. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  32567. EVP_PKEY_free(pkey);
  32568. pkey = NULL;
  32569. #if defined(HAVE_ECC)
  32570. #if defined(USE_CERT_BUFFERS_256)
  32571. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32572. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32573. SSL_FILETYPE_ASN1));
  32574. #else
  32575. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  32576. cliEccCertFile, SSL_FILETYPE_PEM));
  32577. #endif
  32578. pkey = X509_get_pubkey(ecX509);
  32579. ExpectNotNull(pkey);
  32580. /* current ECC key is 256 bit (32 bytes) */
  32581. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  32582. X509_free(ecX509);
  32583. ecX509 = NULL;
  32584. EVP_PKEY_free(pkey);
  32585. pkey = NULL;
  32586. #endif /* HAVE_ECC */
  32587. }
  32588. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32589. if (EXPECT_SUCCESS()) {
  32590. x509 = NULL;
  32591. }
  32592. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32593. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  32594. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32595. #endif
  32596. SSL_CTX_free(ctx);
  32597. ctx = NULL;
  32598. #ifndef NO_WOLFSSL_SERVER
  32599. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32600. #else
  32601. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32602. #endif
  32603. /* Test haproxy use case */
  32604. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32605. /* Read Certificate */
  32606. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32607. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32608. ExpectNotNull(chain = sk_X509_new_null());
  32609. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  32610. if (EXPECT_SUCCESS()) {
  32611. ca = NULL;
  32612. }
  32613. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  32614. ExpectNotNull(ca = sk_X509_shift(chain2));
  32615. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32616. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32617. if (EXPECT_SUCCESS()) {
  32618. ca = NULL;
  32619. }
  32620. BIO_free(bio);
  32621. X509_free(cert);
  32622. X509_free(ca);
  32623. X509_free(x509);
  32624. sk_X509_pop_free(chain, X509_free);
  32625. sk_X509_pop_free(chain2, X509_free);
  32626. SSL_CTX_free(ctx);
  32627. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32628. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32629. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32630. return EXPECT_RESULT();
  32631. }
  32632. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32633. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32634. {
  32635. EXPECT_DECLS;
  32636. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32637. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32638. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32639. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32640. callback_functions client_cb;
  32641. callback_functions server_cb;
  32642. int line = 0;
  32643. int flag = ERR_TXT_STRING;
  32644. const char* file = NULL;
  32645. const char* data = NULL;
  32646. /* create a failed connection and inspect the error */
  32647. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32648. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32649. client_cb.method = wolfTLSv1_1_client_method;
  32650. server_cb.method = wolfTLSv1_2_server_method;
  32651. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32652. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32653. ExpectNotNull(data);
  32654. /* check clearing error state */
  32655. ERR_remove_state(0);
  32656. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32657. ERR_peek_last_error_line(NULL, &line);
  32658. ExpectIntEQ(line, 0);
  32659. ERR_peek_last_error_line(&file, NULL);
  32660. ExpectNull(file);
  32661. /* retry connection to fill error queue */
  32662. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32663. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32664. client_cb.method = wolfTLSv1_1_client_method;
  32665. server_cb.method = wolfTLSv1_2_server_method;
  32666. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32667. /* check that error code was stored */
  32668. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32669. ERR_peek_last_error_line(NULL, &line);
  32670. ExpectIntNE(line, 0);
  32671. ERR_peek_last_error_line(&file, NULL);
  32672. ExpectNotNull(file);
  32673. fprintf(stderr, "\nTesting error print out\n");
  32674. ERR_print_errors_fp(stderr);
  32675. fprintf(stderr, "Done testing print out\n\n");
  32676. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32677. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32678. return EXPECT_RESULT();
  32679. }
  32680. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  32681. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32682. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32683. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32684. {
  32685. (void) ok;
  32686. (void) ctx;
  32687. fprintf(stderr, "ENTER verify_cb\n");
  32688. return SSL_SUCCESS;
  32689. }
  32690. #endif
  32691. static int test_wolfSSL_X509_Name_canon(void)
  32692. {
  32693. EXPECT_DECLS;
  32694. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32695. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32696. defined(WOLFSSL_CERT_GEN) && \
  32697. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32698. const long ex_hash1 = 0x0fdb2da4;
  32699. const long ex_hash2 = 0x9f3e8c9e;
  32700. X509_NAME *name = NULL;
  32701. X509 *x509 = NULL;
  32702. XFILE file = XBADFILE;
  32703. unsigned long hash = 0;
  32704. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32705. byte *pbuf = NULL;
  32706. word32 len = 0;
  32707. (void) ex_hash2;
  32708. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  32709. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32710. ExpectNotNull(name = X509_get_issuer_name(x509));
  32711. /* When output buffer is NULL, should return necessary output buffer
  32712. * length.*/
  32713. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32714. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32715. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32716. hash = (((unsigned long)digest[3] << 24) |
  32717. ((unsigned long)digest[2] << 16) |
  32718. ((unsigned long)digest[1] << 8) |
  32719. ((unsigned long)digest[0]));
  32720. ExpectIntEQ(hash, ex_hash1);
  32721. if (file != XBADFILE) {
  32722. XFCLOSE(file);
  32723. file = XBADFILE;
  32724. }
  32725. X509_free(x509);
  32726. x509 = NULL;
  32727. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32728. pbuf = NULL;
  32729. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32730. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32731. ExpectNotNull(name = X509_get_issuer_name(x509));
  32732. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32733. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32734. hash = (((unsigned long)digest[3] << 24) |
  32735. ((unsigned long)digest[2] << 16) |
  32736. ((unsigned long)digest[1] << 8) |
  32737. ((unsigned long)digest[0]));
  32738. ExpectIntEQ(hash, ex_hash2);
  32739. if (file != XBADFILE)
  32740. XFCLOSE(file);
  32741. X509_free(x509);
  32742. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32743. #endif
  32744. return EXPECT_RESULT();
  32745. }
  32746. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32747. {
  32748. EXPECT_DECLS;
  32749. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32750. const int MAX_DIR = 4;
  32751. const char paths[][32] = {
  32752. "./certs/ed25519",
  32753. "./certs/ecc",
  32754. "./certs/crl",
  32755. "./certs/",
  32756. };
  32757. char CertCrl_path[MAX_FILENAME_SZ];
  32758. char *p;
  32759. X509_STORE* str = NULL;
  32760. X509_LOOKUP* lookup = NULL;
  32761. WOLFSSL_STACK* sk = NULL;
  32762. int len, total_len, i;
  32763. (void)sk;
  32764. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32765. /* illegal string */
  32766. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32767. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32768. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32769. SSL_FILETYPE_PEM,NULL), 0);
  32770. /* free store */
  32771. X509_STORE_free(str);
  32772. str = NULL;
  32773. /* short folder string */
  32774. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32775. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32776. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32777. SSL_FILETYPE_PEM,NULL), 1);
  32778. #if defined(WOLFSSL_INT_H)
  32779. /* only available when including internal.h */
  32780. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32781. #endif
  32782. /* free store */
  32783. X509_STORE_free(str);
  32784. str = NULL;
  32785. /* typical function check */
  32786. p = &CertCrl_path[0];
  32787. total_len = 0;
  32788. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32789. len = (int)XSTRLEN((const char*)&paths[i]);
  32790. total_len += len;
  32791. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32792. p += len;
  32793. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32794. }
  32795. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32796. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32797. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  32798. SSL_FILETYPE_PEM,NULL), 1);
  32799. #if defined(WOLFSSL_INT_H)
  32800. /* only available when including internal.h */
  32801. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32802. #endif
  32803. X509_STORE_free(str);
  32804. #endif
  32805. return EXPECT_RESULT();
  32806. }
  32807. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32808. {
  32809. EXPECT_DECLS;
  32810. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32811. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32812. defined(WOLFSSL_SIGNER_DER_CERT)
  32813. X509_STORE_CTX* ctx = NULL;
  32814. X509_STORE* str = NULL;
  32815. X509_LOOKUP* lookup = NULL;
  32816. X509* cert1 = NULL;
  32817. X509* x509Ca = NULL;
  32818. X509* x509Svr = NULL;
  32819. X509* issuer = NULL;
  32820. WOLFSSL_STACK* sk = NULL;
  32821. X509_NAME* caName = NULL;
  32822. X509_NAME* issuerName = NULL;
  32823. XFILE file1 = XBADFILE;
  32824. int i;
  32825. int cert_count = 0;
  32826. int cmp;
  32827. char der[] = "certs/ca-cert.der";
  32828. #ifdef HAVE_CRL
  32829. char pem[][100] = {
  32830. "./certs/crl/crl.pem",
  32831. "./certs/crl/crl2.pem",
  32832. "./certs/crl/caEccCrl.pem",
  32833. "./certs/crl/eccCliCRL.pem",
  32834. "./certs/crl/eccSrvCRL.pem",
  32835. ""
  32836. };
  32837. #endif
  32838. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  32839. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32840. if (file1 != XBADFILE)
  32841. XFCLOSE(file1);
  32842. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32843. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32844. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32845. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32846. SSL_FILETYPE_PEM,NULL), 1);
  32847. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32848. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32849. /* check if CA cert is loaded into the store */
  32850. for (i = 0; i < cert_count; i++) {
  32851. x509Ca = sk_X509_value(sk, i);
  32852. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32853. }
  32854. ExpectNotNull((x509Svr =
  32855. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32856. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32857. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32858. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32859. ExpectNotNull(issuer);
  32860. caName = X509_get_subject_name(x509Ca);
  32861. ExpectNotNull(caName);
  32862. issuerName = X509_get_subject_name(issuer);
  32863. ExpectNotNull(issuerName);
  32864. cmp = X509_NAME_cmp(caName, issuerName);
  32865. ExpectIntEQ(cmp, 0);
  32866. /* load der format */
  32867. X509_free(issuer);
  32868. issuer = NULL;
  32869. X509_STORE_CTX_free(ctx);
  32870. ctx = NULL;
  32871. X509_STORE_free(str);
  32872. str = NULL;
  32873. sk_X509_pop_free(sk, NULL);
  32874. sk = NULL;
  32875. X509_free(x509Svr);
  32876. x509Svr = NULL;
  32877. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32878. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32879. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32880. SSL_FILETYPE_ASN1,NULL), 1);
  32881. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32882. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32883. /* check if CA cert is loaded into the store */
  32884. for (i = 0; i < cert_count; i++) {
  32885. x509Ca = sk_X509_value(sk, i);
  32886. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32887. }
  32888. X509_STORE_free(str);
  32889. str = NULL;
  32890. sk_X509_pop_free(sk, NULL);
  32891. sk = NULL;
  32892. X509_free(cert1);
  32893. cert1 = NULL;
  32894. #ifdef HAVE_CRL
  32895. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32896. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32897. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32898. SSL_FILETYPE_PEM,NULL), 1);
  32899. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32900. "certs/server-revoked-cert.pem",
  32901. SSL_FILETYPE_PEM,NULL), 1);
  32902. if (str) {
  32903. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32904. WOLFSSL_FILETYPE_PEM), 1);
  32905. /* since store hasn't yet known the revoked cert*/
  32906. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32907. "certs/server-revoked-cert.pem",
  32908. WOLFSSL_FILETYPE_PEM), 1);
  32909. }
  32910. for (i = 0; pem[i][0] != '\0'; i++)
  32911. {
  32912. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32913. SSL_FILETYPE_PEM, NULL), 1);
  32914. }
  32915. if (str) {
  32916. /* since store knows crl list */
  32917. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32918. "certs/server-revoked-cert.pem",
  32919. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32920. }
  32921. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32922. X509_STORE_free(str);
  32923. #endif
  32924. #endif
  32925. return EXPECT_RESULT();
  32926. }
  32927. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32928. {
  32929. int res = TEST_SKIPPED;
  32930. #if defined(OPENSSL_EXTRA)
  32931. X509_STORE_CTX_cleanup(NULL);
  32932. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32933. res = TEST_SUCCESS;
  32934. #endif
  32935. return res;
  32936. }
  32937. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32938. {
  32939. EXPECT_DECLS;
  32940. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32941. X509_STORE_CTX* ctx = NULL;
  32942. X509_STORE* str = NULL;
  32943. X509* x509Ca = NULL;
  32944. X509* x509Svr = NULL;
  32945. X509* issuer = NULL;
  32946. X509_NAME* caName = NULL;
  32947. X509_NAME* issuerName = NULL;
  32948. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32949. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32950. ExpectNotNull((x509Ca =
  32951. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32952. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32953. ExpectNotNull((x509Svr =
  32954. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32955. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32956. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32957. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32958. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32959. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32960. #ifdef WOLFSSL_SIGNER_DER_CERT
  32961. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32962. #endif
  32963. X509_free(issuer);
  32964. X509_STORE_CTX_free(ctx);
  32965. X509_free(x509Svr);
  32966. X509_STORE_free(str);
  32967. X509_free(x509Ca);
  32968. #endif
  32969. return EXPECT_RESULT();
  32970. }
  32971. static int test_wolfSSL_PKCS7_certs(void)
  32972. {
  32973. EXPECT_DECLS;
  32974. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32975. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32976. STACK_OF(X509)* sk = NULL;
  32977. STACK_OF(X509_INFO)* info_sk = NULL;
  32978. PKCS7 *p7 = NULL;
  32979. BIO* bio = NULL;
  32980. const byte* p = NULL;
  32981. int buflen = 0;
  32982. int i;
  32983. /* Test twice. Once with d2i and once without to test
  32984. * that everything is free'd correctly. */
  32985. for (i = 0; i < 2; i++) {
  32986. ExpectNotNull(p7 = PKCS7_new());
  32987. if (p7 != NULL) {
  32988. p7->version = 1;
  32989. #ifdef NO_SHA
  32990. p7->hashOID = SHA256h;
  32991. #else
  32992. p7->hashOID = SHAh;
  32993. #endif
  32994. }
  32995. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32996. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32997. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32998. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32999. ExpectNotNull(sk = sk_X509_new_null());
  33000. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  33001. X509_INFO* info = NULL;
  33002. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  33003. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  33004. if (EXPECT_SUCCESS() && (info != NULL)) {
  33005. info->x509 = NULL;
  33006. }
  33007. X509_INFO_free(info);
  33008. }
  33009. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  33010. info_sk = NULL;
  33011. BIO_free(bio);
  33012. bio = NULL;
  33013. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33014. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  33015. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  33016. sk_X509_pop_free(sk, X509_free);
  33017. }
  33018. sk = NULL;
  33019. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  33020. if (i == 0) {
  33021. PKCS7_free(p7);
  33022. p7 = NULL;
  33023. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  33024. if (p7 != NULL) {
  33025. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  33026. * them */
  33027. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  33028. }
  33029. /* PKCS7_free free's the certs */
  33030. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  33031. }
  33032. BIO_free(bio);
  33033. bio = NULL;
  33034. PKCS7_free(p7);
  33035. p7 = NULL;
  33036. }
  33037. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33038. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  33039. return EXPECT_RESULT();
  33040. }
  33041. static int test_wolfSSL_X509_STORE_CTX(void)
  33042. {
  33043. EXPECT_DECLS;
  33044. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33045. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33046. X509_STORE_CTX* ctx = NULL;
  33047. X509_STORE* str = NULL;
  33048. X509* x509 = NULL;
  33049. #ifdef OPENSSL_ALL
  33050. X509* x5092 = NULL;
  33051. STACK_OF(X509) *sk = NULL;
  33052. STACK_OF(X509) *sk2 = NULL;
  33053. STACK_OF(X509) *sk3 = NULL;
  33054. #endif
  33055. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33056. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33057. ExpectNotNull((x509 =
  33058. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  33059. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  33060. #ifdef OPENSSL_ALL
  33061. /* sk_X509_new only in OPENSSL_ALL */
  33062. sk = sk_X509_new_null();
  33063. ExpectNotNull(sk);
  33064. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  33065. #else
  33066. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  33067. #endif
  33068. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  33069. X509_STORE_CTX_set_error(ctx, -5);
  33070. X509_STORE_CTX_set_error(NULL, -5);
  33071. X509_STORE_CTX_free(ctx);
  33072. ctx = NULL;
  33073. #ifdef OPENSSL_ALL
  33074. sk_X509_pop_free(sk, NULL);
  33075. sk = NULL;
  33076. #endif
  33077. X509_STORE_free(str);
  33078. str = NULL;
  33079. X509_free(x509);
  33080. x509 = NULL;
  33081. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33082. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  33083. X509_STORE_CTX_free(ctx);
  33084. ctx = NULL;
  33085. #ifdef OPENSSL_ALL
  33086. /* test X509_STORE_CTX_get(1)_chain */
  33087. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  33088. SSL_FILETYPE_PEM)));
  33089. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  33090. SSL_FILETYPE_PEM)));
  33091. ExpectNotNull((sk = sk_X509_new_null()));
  33092. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  33093. if (EXPECT_FAIL()) {
  33094. X509_free(x509);
  33095. x509 = NULL;
  33096. }
  33097. ExpectNotNull((str = X509_STORE_new()));
  33098. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  33099. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  33100. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  33101. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  33102. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  33103. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  33104. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  33105. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  33106. X509_STORE_CTX_free(ctx);
  33107. ctx = NULL;
  33108. X509_STORE_free(str);
  33109. str = NULL;
  33110. /* CTX certs not freed yet */
  33111. X509_free(x5092);
  33112. x5092 = NULL;
  33113. sk_X509_pop_free(sk, NULL);
  33114. sk = NULL;
  33115. /* sk3 is dup so free here */
  33116. sk_X509_pop_free(sk3, NULL);
  33117. sk3 = NULL;
  33118. #endif
  33119. /* test X509_STORE_CTX_get/set_ex_data */
  33120. {
  33121. int i = 0, tmpData = 5;
  33122. void* tmpDataRet;
  33123. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33124. #ifdef HAVE_EX_DATA
  33125. for (i = 0; i < MAX_EX_DATA; i++) {
  33126. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  33127. WOLFSSL_SUCCESS);
  33128. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  33129. ExpectNotNull(tmpDataRet);
  33130. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33131. }
  33132. #else
  33133. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  33134. WOLFSSL_FAILURE);
  33135. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  33136. ExpectNull(tmpDataRet);
  33137. #endif
  33138. X509_STORE_CTX_free(ctx);
  33139. ctx = NULL;
  33140. }
  33141. /* test X509_STORE_get/set_ex_data */
  33142. {
  33143. int i = 0, tmpData = 99;
  33144. void* tmpDataRet;
  33145. ExpectNotNull(str = X509_STORE_new());
  33146. #ifdef HAVE_EX_DATA
  33147. for (i = 0; i < MAX_EX_DATA; i++) {
  33148. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33149. WOLFSSL_SUCCESS);
  33150. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33151. ExpectNotNull(tmpDataRet);
  33152. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33153. }
  33154. #else
  33155. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33156. WOLFSSL_FAILURE);
  33157. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33158. ExpectNull(tmpDataRet);
  33159. #endif
  33160. X509_STORE_free(str);
  33161. str = NULL;
  33162. }
  33163. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33164. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33165. return EXPECT_RESULT();
  33166. }
  33167. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33168. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  33169. STACK_OF(X509)* chain)
  33170. {
  33171. EXPECT_DECLS;
  33172. XFILE fp = XBADFILE;
  33173. X509* cert = NULL;
  33174. ExpectTrue((fp = XFOPEN(filename, "rb"))
  33175. != XBADFILE);
  33176. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33177. if (fp != XBADFILE) {
  33178. XFCLOSE(fp);
  33179. fp = XBADFILE;
  33180. }
  33181. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  33182. if (EXPECT_FAIL())
  33183. X509_free(cert);
  33184. return EXPECT_RESULT();
  33185. }
  33186. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  33187. int err, int loadCA)
  33188. {
  33189. EXPECT_DECLS;
  33190. X509_STORE_CTX* ctx = NULL;
  33191. X509_STORE* str = NULL;
  33192. XFILE fp = XBADFILE;
  33193. X509* cert = NULL;
  33194. STACK_OF(X509)* untrusted = NULL;
  33195. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  33196. != XBADFILE);
  33197. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33198. if (fp != XBADFILE) {
  33199. XFCLOSE(fp);
  33200. fp = XBADFILE;
  33201. }
  33202. ExpectNotNull(str = X509_STORE_new());
  33203. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33204. ExpectNotNull(untrusted = sk_X509_new_null());
  33205. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  33206. if (loadCA) {
  33207. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  33208. 1);
  33209. }
  33210. for (; *filenames; filenames++) {
  33211. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  33212. untrusted), TEST_SUCCESS);
  33213. }
  33214. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  33215. ExpectIntEQ(X509_verify_cert(ctx), ret);
  33216. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  33217. X509_free(cert);
  33218. X509_STORE_free(str);
  33219. X509_STORE_CTX_free(ctx);
  33220. sk_X509_pop_free(untrusted, NULL);
  33221. return EXPECT_RESULT();
  33222. }
  33223. #endif
  33224. static int test_X509_STORE_untrusted(void)
  33225. {
  33226. EXPECT_DECLS;
  33227. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33228. const char* untrusted1[] = {
  33229. "./certs/intermediate/ca-int2-cert.pem",
  33230. NULL
  33231. };
  33232. const char* untrusted2[] = {
  33233. "./certs/intermediate/ca-int-cert.pem",
  33234. "./certs/intermediate/ca-int2-cert.pem",
  33235. NULL
  33236. };
  33237. const char* untrusted3[] = {
  33238. "./certs/intermediate/ca-int-cert.pem",
  33239. "./certs/intermediate/ca-int2-cert.pem",
  33240. "./certs/ca-cert.pem",
  33241. NULL
  33242. };
  33243. /* Adding unrelated certs that should be ignored */
  33244. const char* untrusted4[] = {
  33245. "./certs/client-ca.pem",
  33246. "./certs/intermediate/ca-int-cert.pem",
  33247. "./certs/server-cert.pem",
  33248. "./certs/intermediate/ca-int2-cert.pem",
  33249. NULL
  33250. };
  33251. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  33252. * to loaded CA. */
  33253. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  33254. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  33255. /* Succeeds because path to loaded CA is available. */
  33256. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  33257. TEST_SUCCESS);
  33258. /* Fails because root CA is in the untrusted stack */
  33259. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  33260. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  33261. /* Succeeds because path to loaded CA is available. */
  33262. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  33263. TEST_SUCCESS);
  33264. #endif
  33265. return EXPECT_RESULT();
  33266. }
  33267. static int test_wolfSSL_X509_STORE_set_flags(void)
  33268. {
  33269. EXPECT_DECLS;
  33270. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33271. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33272. X509_STORE* store = NULL;
  33273. X509* x509 = NULL;
  33274. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  33275. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33276. WOLFSSL_FILETYPE_PEM)));
  33277. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  33278. #ifdef HAVE_CRL
  33279. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33280. WOLFSSL_SUCCESS);
  33281. #else
  33282. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33283. NOT_COMPILED_IN);
  33284. #endif
  33285. wolfSSL_X509_free(x509);
  33286. wolfSSL_X509_STORE_free(store);
  33287. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33288. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33289. return EXPECT_RESULT();
  33290. }
  33291. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  33292. {
  33293. EXPECT_DECLS;
  33294. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  33295. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33296. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  33297. WOLFSSL_X509_STORE* store = NULL;
  33298. WOLFSSL_X509_LOOKUP* lookup = NULL;
  33299. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  33300. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  33301. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  33302. X509_FILETYPE_PEM), 1);
  33303. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  33304. X509_FILETYPE_PEM), 1);
  33305. if (store != NULL) {
  33306. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  33307. WOLFSSL_FILETYPE_PEM), 1);
  33308. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33309. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  33310. }
  33311. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  33312. X509_FILETYPE_PEM), 1);
  33313. if (store != NULL) {
  33314. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33315. WOLFSSL_FILETYPE_PEM), 1);
  33316. }
  33317. wolfSSL_X509_STORE_free(store);
  33318. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  33319. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33320. return EXPECT_RESULT();
  33321. }
  33322. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  33323. {
  33324. EXPECT_DECLS;
  33325. #if defined(OPENSSL_EXTRA)
  33326. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  33327. time_t c_time;
  33328. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33329. c_time = 365*24*60*60;
  33330. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33331. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  33332. WOLFSSL_USE_CHECK_TIME);
  33333. ExpectTrue(ctx->param->check_time == c_time);
  33334. wolfSSL_X509_STORE_CTX_free(ctx);
  33335. #endif /* OPENSSL_EXTRA */
  33336. return EXPECT_RESULT();
  33337. }
  33338. static int test_wolfSSL_CTX_get0_set1_param(void)
  33339. {
  33340. EXPECT_DECLS;
  33341. #if defined(OPENSSL_EXTRA)
  33342. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33343. SSL_CTX* ctx = NULL;
  33344. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33345. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  33346. char testIPv4[] = "127.0.0.1";
  33347. char testhostName[] = "foo.hoge.com";
  33348. #ifndef NO_WOLFSSL_SERVER
  33349. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33350. #else
  33351. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33352. #endif
  33353. ExpectNull(SSL_CTX_get0_param(NULL));
  33354. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  33355. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33356. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  33357. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  33358. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33359. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  33360. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  33361. WOLFSSL_SUCCESS);
  33362. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33363. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  33364. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33365. (int)XSTRLEN(testhostName)));
  33366. ExpectIntEQ(0x01, pParam->hostFlags);
  33367. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33368. /* test for incorrect parameter */
  33369. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33370. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33371. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33372. SSL_CTX_free(ctx);
  33373. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33374. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33375. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33376. return EXPECT_RESULT();
  33377. }
  33378. static int test_wolfSSL_get0_param(void)
  33379. {
  33380. EXPECT_DECLS;
  33381. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33382. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33383. SSL_CTX* ctx = NULL;
  33384. SSL* ssl = NULL;
  33385. #ifndef NO_WOLFSSL_SERVER
  33386. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33387. #else
  33388. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33389. #endif
  33390. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33391. SSL_FILETYPE_PEM));
  33392. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33393. ExpectNotNull(ssl = SSL_new(ctx));
  33394. ExpectNotNull(SSL_get0_param(ssl));
  33395. SSL_free(ssl);
  33396. SSL_CTX_free(ctx);
  33397. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33398. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33399. return EXPECT_RESULT();
  33400. }
  33401. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33402. {
  33403. EXPECT_DECLS;
  33404. #if defined(OPENSSL_EXTRA)
  33405. const char host[] = "www.example.com";
  33406. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33407. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33408. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33409. if (pParam != NULL) {
  33410. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33411. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33412. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33413. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33414. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33415. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33416. }
  33417. #endif /* OPENSSL_EXTRA */
  33418. return EXPECT_RESULT();
  33419. }
  33420. static int test_wolfSSL_set1_host(void)
  33421. {
  33422. EXPECT_DECLS;
  33423. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33424. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33425. const char host[] = "www.test_wolfSSL_set1_host.com";
  33426. const char emptyStr[] = "";
  33427. SSL_CTX* ctx = NULL;
  33428. SSL* ssl = NULL;
  33429. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33430. #ifndef NO_WOLFSSL_SERVER
  33431. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33432. #else
  33433. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33434. #endif
  33435. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33436. SSL_FILETYPE_PEM));
  33437. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33438. ExpectNotNull(ssl = SSL_new(ctx));
  33439. pParam = SSL_get0_param(ssl);
  33440. /* we should get back host string */
  33441. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33442. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33443. /* we should get back empty string */
  33444. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  33445. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33446. /* we should get back host string */
  33447. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33448. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33449. /* we should get back empty string */
  33450. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  33451. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33452. SSL_free(ssl);
  33453. SSL_CTX_free(ctx);
  33454. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33455. #endif /* OPENSSL_EXTRA */
  33456. return EXPECT_RESULT();
  33457. }
  33458. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33459. {
  33460. EXPECT_DECLS;
  33461. #if defined(OPENSSL_EXTRA)
  33462. unsigned char buf[16] = {0};
  33463. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  33464. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  33465. /* test 127.0.0.1 */
  33466. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33467. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33468. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33469. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33470. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33471. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33472. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33473. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33474. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33475. ExpectIntEQ(XSTRNCMP(param->ipasc,
  33476. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33477. /* test 2001:db8:: */
  33478. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33479. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33480. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33481. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33482. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33483. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33484. /* test ::1234:5678 */
  33485. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33486. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33487. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33488. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33489. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33490. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33491. /* test 2001:db8::1234:5678 */
  33492. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33493. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33494. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33495. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33496. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33497. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33498. sizeof(param->ipasc)), 0);
  33499. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33500. /* 2001:db8:1::ab9:c0a8:102 */
  33501. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33502. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33503. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33504. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33505. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33506. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33507. sizeof(param->ipasc)), 0);
  33508. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33509. #endif /* OPENSSL_EXTRA */
  33510. return EXPECT_RESULT();
  33511. }
  33512. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33513. {
  33514. EXPECT_DECLS;
  33515. #if defined(OPENSSL_EXTRA)
  33516. X509_STORE* store = NULL;
  33517. X509_STORE_CTX* ctx = NULL;
  33518. X509_STORE_CTX* ctx_no_init = NULL;
  33519. ExpectNotNull((store = X509_STORE_new()));
  33520. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33521. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  33522. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33523. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  33524. /* should return NULL if ctx has not bee initialized */
  33525. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33526. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  33527. wolfSSL_X509_STORE_CTX_free(ctx);
  33528. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33529. X509_STORE_free(store);
  33530. #endif /* OPENSSL_EXTRA */
  33531. return EXPECT_RESULT();
  33532. }
  33533. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33534. {
  33535. EXPECT_DECLS;
  33536. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33537. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33538. WOLFSSL_CTX* ctx = NULL;
  33539. WOLFSSL* ssl = NULL;
  33540. X509_NAME* name = NULL;
  33541. STACK_OF(X509_NAME)* names = NULL;
  33542. STACK_OF(X509_NAME)* ca_list = NULL;
  33543. int names_len = 0;
  33544. int i;
  33545. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33546. /* Send two X501 names in cert request */
  33547. names = SSL_load_client_CA_file(cliCertFile);
  33548. ExpectNotNull(names);
  33549. ca_list = SSL_load_client_CA_file(caCertFile);
  33550. ExpectNotNull(ca_list);
  33551. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  33552. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  33553. if (EXPECT_FAIL()) {
  33554. wolfSSL_X509_NAME_free(name);
  33555. name = NULL;
  33556. }
  33557. SSL_CTX_set_client_CA_list(ctx, names);
  33558. /* This should only free the stack structure */
  33559. sk_X509_NAME_free(ca_list);
  33560. ca_list = NULL;
  33561. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33562. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33563. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33564. for (i = 0; i < names_len; i++) {
  33565. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33566. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33567. }
  33568. /* Needed to be able to create ssl object */
  33569. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33570. SSL_FILETYPE_PEM));
  33571. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33572. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33573. /* load again as old names are responsibility of ctx to free*/
  33574. names = SSL_load_client_CA_file(cliCertFile);
  33575. ExpectNotNull(names);
  33576. SSL_set_client_CA_list(ssl, names);
  33577. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33578. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33579. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33580. for (i = 0; i < names_len; i++) {
  33581. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33582. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33583. }
  33584. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33585. {
  33586. tcp_ready ready;
  33587. func_args server_args;
  33588. callback_functions server_cb;
  33589. THREAD_TYPE serverThread;
  33590. WOLFSSL* ssl_client = NULL;
  33591. WOLFSSL_CTX* ctx_client = NULL;
  33592. SOCKET_T sockfd = 0;
  33593. /* wolfSSL_get_client_CA_list() with handshake */
  33594. StartTCP();
  33595. InitTcpReady(&ready);
  33596. XMEMSET(&server_args, 0, sizeof(func_args));
  33597. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33598. server_args.signal = &ready;
  33599. server_args.callbacks = &server_cb;
  33600. /* we are responsible for free'ing WOLFSSL_CTX */
  33601. server_cb.ctx = ctx;
  33602. server_cb.isSharedCtx = 1;
  33603. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  33604. cliCertFile, 0));
  33605. start_thread(test_server_nofail, &server_args, &serverThread);
  33606. wait_tcp_ready(&server_args);
  33607. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33608. ExpectNotNull(ctx_client =
  33609. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33610. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  33611. ctx_client, caCertFile, 0));
  33612. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  33613. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33614. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  33615. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33616. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  33617. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33618. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33619. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33620. /* We are expecting two cert names to be sent */
  33621. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  33622. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33623. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33624. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  33625. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  33626. }
  33627. wolfSSL_shutdown(ssl_client);
  33628. wolfSSL_free(ssl_client);
  33629. wolfSSL_CTX_free(ctx_client);
  33630. CloseSocket(sockfd);
  33631. join_thread(serverThread);
  33632. FreeTcpReady(&ready);
  33633. }
  33634. #endif
  33635. wolfSSL_free(ssl);
  33636. wolfSSL_CTX_free(ctx);
  33637. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33638. * !NO_BIO */
  33639. return EXPECT_RESULT();
  33640. }
  33641. static int test_wolfSSL_CTX_add_client_CA(void)
  33642. {
  33643. EXPECT_DECLS;
  33644. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33645. !defined(NO_WOLFSSL_CLIENT)
  33646. WOLFSSL_CTX* ctx = NULL;
  33647. WOLFSSL_X509* x509 = NULL;
  33648. WOLFSSL_X509* x509_a = NULL;
  33649. STACK_OF(X509_NAME)* ca_list = NULL;
  33650. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33651. /* Add client cert */
  33652. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33653. SSL_FILETYPE_PEM));
  33654. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  33655. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33656. /* Add another client cert */
  33657. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33658. SSL_FILETYPE_PEM));
  33659. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33660. /* test for incorrect parameter */
  33661. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33662. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33663. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33664. X509_free(x509);
  33665. X509_free(x509_a);
  33666. SSL_CTX_free(ctx);
  33667. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33668. return EXPECT_RESULT();
  33669. }
  33670. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33671. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33672. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33673. {
  33674. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33675. WOLFSSL_CTX* ctx = callbacks->ctx;
  33676. WOLFSSL* ssl = NULL;
  33677. SOCKET_T sfd = 0;
  33678. SOCKET_T cfd = 0;
  33679. word16 port;
  33680. char input[1024];
  33681. int idx;
  33682. int ret, err = 0;
  33683. const char* privateName = "ech-private-name.com";
  33684. int privateNameLen = (int)XSTRLEN(privateName);
  33685. ((func_args*)args)->return_code = TEST_FAIL;
  33686. port = ((func_args*)args)->signal->port;
  33687. AssertIntEQ(WOLFSSL_SUCCESS,
  33688. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33689. AssertIntEQ(WOLFSSL_SUCCESS,
  33690. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33691. WOLFSSL_FILETYPE_PEM));
  33692. AssertIntEQ(WOLFSSL_SUCCESS,
  33693. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33694. WOLFSSL_FILETYPE_PEM));
  33695. if (callbacks->ctx_ready)
  33696. callbacks->ctx_ready(ctx);
  33697. ssl = wolfSSL_new(ctx);
  33698. /* set the sni for the server */
  33699. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33700. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33701. CloseSocket(sfd);
  33702. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33703. if (callbacks->ssl_ready)
  33704. callbacks->ssl_ready(ssl);
  33705. do {
  33706. err = 0; /* Reset error */
  33707. ret = wolfSSL_accept(ssl);
  33708. if (ret != WOLFSSL_SUCCESS) {
  33709. err = wolfSSL_get_error(ssl, 0);
  33710. }
  33711. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33712. if (ret != WOLFSSL_SUCCESS) {
  33713. char buff[WOLFSSL_MAX_ERROR_SZ];
  33714. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33715. }
  33716. else {
  33717. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33718. input[idx] = 0;
  33719. fprintf(stderr, "Client message: %s\n", input);
  33720. }
  33721. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33722. privateNameLen));
  33723. ((func_args*)args)->return_code = TEST_SUCCESS;
  33724. }
  33725. if (callbacks->on_result)
  33726. callbacks->on_result(ssl);
  33727. wolfSSL_shutdown(ssl);
  33728. wolfSSL_free(ssl);
  33729. wolfSSL_CTX_free(ctx);
  33730. CloseSocket(cfd);
  33731. #ifdef FP_ECC
  33732. wc_ecc_fp_free();
  33733. #endif
  33734. WOLFSSL_RETURN_FROM_THREAD(0);
  33735. }
  33736. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33737. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33738. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33739. {
  33740. AssertNotNull(ssl);
  33741. AssertNotNull(line);
  33742. XFILE fp;
  33743. const byte lf = '\n';
  33744. fp = XFOPEN("./MyKeyLog.txt", "a");
  33745. XFWRITE( line, 1, strlen(line),fp);
  33746. XFWRITE( (void*)&lf,1,1,fp);
  33747. XFFLUSH(fp);
  33748. XFCLOSE(fp);
  33749. }
  33750. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33751. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33752. {
  33753. EXPECT_DECLS;
  33754. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33755. !defined(NO_WOLFSSL_CLIENT)
  33756. SSL_CTX* ctx = NULL;
  33757. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33758. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33759. SSL_CTX_free(ctx);
  33760. SSL_CTX_set_keylog_callback(NULL, NULL);
  33761. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33762. return EXPECT_RESULT();
  33763. }
  33764. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33765. {
  33766. EXPECT_DECLS;
  33767. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33768. !defined(NO_WOLFSSL_CLIENT)
  33769. SSL_CTX* ctx = NULL;
  33770. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33771. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33772. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33773. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33774. SSL_CTX_set_keylog_callback(ctx, NULL );
  33775. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33776. SSL_CTX_free(ctx);
  33777. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33778. return EXPECT_RESULT();
  33779. }
  33780. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33781. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33782. {
  33783. /* set keylog callback */
  33784. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33785. return TEST_SUCCESS;
  33786. }
  33787. #endif
  33788. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33789. {
  33790. EXPECT_DECLS;
  33791. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33792. /* This test is intended for checking whether keylog callback is called
  33793. * in client during TLS handshake between the client and a server.
  33794. */
  33795. test_ssl_cbf server_cbf;
  33796. test_ssl_cbf client_cbf;
  33797. XFILE fp = XBADFILE;
  33798. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33799. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33800. server_cbf.method = wolfTLSv1_2_server_method;
  33801. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  33802. /* clean up keylog file */
  33803. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33804. if (fp != XBADFILE) {
  33805. XFFLUSH(fp);
  33806. XFCLOSE(fp);
  33807. fp = XBADFILE;
  33808. }
  33809. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33810. &server_cbf, NULL), TEST_SUCCESS);
  33811. XSLEEP_MS(100);
  33812. /* check if the keylog file exists */
  33813. char buff[300] = {0};
  33814. int found = 0;
  33815. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33816. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  33817. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33818. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  33819. found = 1;
  33820. break;
  33821. }
  33822. }
  33823. if (fp != XBADFILE) {
  33824. XFCLOSE(fp);
  33825. }
  33826. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33827. ExpectIntEQ(found, 1);
  33828. /* clean up */
  33829. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  33830. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33831. return EXPECT_RESULT();
  33832. }
  33833. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33834. defined(HAVE_SECRET_CALLBACK)
  33835. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33836. {
  33837. /* set keylog callback */
  33838. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33839. return TEST_SUCCESS;
  33840. }
  33841. #endif
  33842. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33843. {
  33844. EXPECT_DECLS;
  33845. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33846. defined(HAVE_SECRET_CALLBACK)
  33847. /* This test is intended for checking whether keylog callback is called
  33848. * in client during TLS handshake between the client and a server.
  33849. */
  33850. test_ssl_cbf server_cbf;
  33851. test_ssl_cbf client_cbf;
  33852. XFILE fp = XBADFILE;
  33853. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33854. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33855. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33856. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  33857. /* clean up keylog file */
  33858. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33859. if (fp != XBADFILE) {
  33860. XFCLOSE(fp);
  33861. fp = XBADFILE;
  33862. }
  33863. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33864. &server_cbf, NULL), TEST_SUCCESS);
  33865. /* check if the keylog file exists */
  33866. {
  33867. char buff[300] = {0};
  33868. int found[4] = {0};
  33869. int numfnd = 0;
  33870. int i;
  33871. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33872. while (EXPECT_SUCCESS() &&
  33873. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33874. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33875. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33876. found[0] = 1;
  33877. continue;
  33878. }
  33879. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33880. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33881. found[1] = 1;
  33882. continue;
  33883. }
  33884. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33885. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33886. found[2] = 1;
  33887. continue;
  33888. }
  33889. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33890. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33891. found[3] = 1;
  33892. continue;
  33893. }
  33894. }
  33895. if (fp != XBADFILE)
  33896. XFCLOSE(fp);
  33897. for (i = 0; i < 4; i++) {
  33898. if (found[i] != 0)
  33899. numfnd++;
  33900. }
  33901. ExpectIntEQ(numfnd, 4);
  33902. }
  33903. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33904. return EXPECT_RESULT();
  33905. }
  33906. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33907. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33908. static int test_wolfSSL_Tls13_ECH_params(void)
  33909. {
  33910. EXPECT_DECLS;
  33911. #if !defined(NO_WOLFSSL_CLIENT)
  33912. word32 outputLen = 0;
  33913. byte testBuf[72];
  33914. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33915. WOLFSSL *ssl = wolfSSL_new(ctx);
  33916. ExpectNotNull(ctx);
  33917. ExpectNotNull(ssl);
  33918. /* invalid ctx */
  33919. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33920. "ech-public-name.com", 0, 0, 0));
  33921. /* invalid public name */
  33922. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33923. 0, 0));
  33924. /* invalid algorithms */
  33925. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33926. "ech-public-name.com", 1000, 1000, 1000));
  33927. /* invalid ctx */
  33928. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33929. &outputLen));
  33930. /* invalid output len */
  33931. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33932. /* invalid ssl */
  33933. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33934. (char*)testBuf, sizeof(testBuf)));
  33935. /* invalid configs64 */
  33936. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33937. sizeof(testBuf)));
  33938. /* invalid size */
  33939. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33940. (char*)testBuf, 0));
  33941. /* invalid ssl */
  33942. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33943. sizeof(testBuf)));
  33944. /* invalid configs */
  33945. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33946. sizeof(testBuf)));
  33947. /* invalid size */
  33948. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33949. /* invalid ssl */
  33950. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33951. /* invalid size */
  33952. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33953. wolfSSL_free(ssl);
  33954. wolfSSL_CTX_free(ctx);
  33955. #endif /* !NO_WOLFSSL_CLIENT */
  33956. return EXPECT_RESULT();
  33957. }
  33958. static int test_wolfSSL_Tls13_ECH(void)
  33959. {
  33960. EXPECT_DECLS;
  33961. tcp_ready ready;
  33962. func_args client_args;
  33963. func_args server_args;
  33964. THREAD_TYPE serverThread;
  33965. callback_functions server_cbf;
  33966. callback_functions client_cbf;
  33967. SOCKET_T sockfd = 0;
  33968. WOLFSSL_CTX* ctx = NULL;
  33969. WOLFSSL* ssl = NULL;
  33970. const char* publicName = "ech-public-name.com";
  33971. const char* privateName = "ech-private-name.com";
  33972. int privateNameLen = 20;
  33973. char reply[1024];
  33974. int replyLen = 0;
  33975. byte rawEchConfig[128];
  33976. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33977. InitTcpReady(&ready);
  33978. ready.port = 22222;
  33979. XMEMSET(&client_args, 0, sizeof(func_args));
  33980. XMEMSET(&server_args, 0, sizeof(func_args));
  33981. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33982. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33983. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33984. /* create the server context here so we can get the ech config */
  33985. ExpectNotNull(server_cbf.ctx =
  33986. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33987. /* generate ech config */
  33988. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33989. publicName, 0, 0, 0));
  33990. /* get the config for the client to use */
  33991. ExpectIntEQ(WOLFSSL_SUCCESS,
  33992. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33993. &rawEchConfigLen));
  33994. server_args.callbacks = &server_cbf;
  33995. server_args.signal = &ready;
  33996. /* start server task */
  33997. start_thread(server_task_ech, &server_args, &serverThread);
  33998. wait_tcp_ready(&server_args);
  33999. /* run as a TLS1.3 client */
  34000. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  34001. ExpectIntEQ(WOLFSSL_SUCCESS,
  34002. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  34003. ExpectIntEQ(WOLFSSL_SUCCESS,
  34004. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34005. ExpectIntEQ(WOLFSSL_SUCCESS,
  34006. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34007. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  34008. /* get connected the server task */
  34009. ExpectNotNull(ssl = wolfSSL_new(ctx));
  34010. /* set the ech configs for the client */
  34011. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  34012. rawEchConfigLen));
  34013. /* set the sni for the client */
  34014. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  34015. privateName, privateNameLen));
  34016. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  34017. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  34018. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  34019. privateNameLen);
  34020. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  34021. /* add th null terminator for string compare */
  34022. reply[replyLen] = 0;
  34023. /* check that the server replied with the private name */
  34024. ExpectStrEQ(privateName, reply);
  34025. wolfSSL_free(ssl);
  34026. wolfSSL_CTX_free(ctx);
  34027. CloseSocket(sockfd);
  34028. join_thread(serverThread);
  34029. FreeTcpReady(&ready);
  34030. return EXPECT_RESULT();
  34031. }
  34032. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  34033. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34034. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34035. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  34036. static int post_auth_version_cb(WOLFSSL* ssl)
  34037. {
  34038. EXPECT_DECLS;
  34039. /* do handshake and then test version error */
  34040. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  34041. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  34042. return EXPECT_RESULT();
  34043. }
  34044. static int post_auth_version_client_cb(WOLFSSL* ssl)
  34045. {
  34046. EXPECT_DECLS;
  34047. /* do handshake and then test version error */
  34048. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  34049. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  34050. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  34051. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  34052. /* check was added to error queue */
  34053. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  34054. /* check the string matches expected string */
  34055. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  34056. "WRONG_SSL_VERSION");
  34057. #endif
  34058. return EXPECT_RESULT();
  34059. }
  34060. static int post_auth_cb(WOLFSSL* ssl)
  34061. {
  34062. EXPECT_DECLS;
  34063. WOLFSSL_X509* x509 = NULL;
  34064. /* do handshake and then test version error */
  34065. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  34066. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  34067. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  34068. wolfSSL_X509_free(x509);
  34069. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  34070. return EXPECT_RESULT();
  34071. }
  34072. static int set_post_auth_cb(WOLFSSL* ssl)
  34073. {
  34074. if (!wolfSSL_is_server(ssl)) {
  34075. EXPECT_DECLS;
  34076. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  34077. return EXPECT_RESULT();
  34078. }
  34079. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  34080. return TEST_SUCCESS;
  34081. }
  34082. #endif
  34083. static int test_wolfSSL_Tls13_postauth(void)
  34084. {
  34085. EXPECT_DECLS;
  34086. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34087. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34088. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  34089. test_ssl_cbf server_cbf;
  34090. test_ssl_cbf client_cbf;
  34091. /* test version failure doing post auth with TLS 1.2 connection */
  34092. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  34093. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  34094. server_cbf.method = wolfTLSv1_2_server_method;
  34095. server_cbf.ssl_ready = set_post_auth_cb;
  34096. server_cbf.on_result = post_auth_version_cb;
  34097. client_cbf.ssl_ready = set_post_auth_cb;
  34098. client_cbf.on_result = post_auth_version_client_cb;
  34099. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34100. &server_cbf, NULL), TEST_SUCCESS);
  34101. /* tests on post auth with TLS 1.3 */
  34102. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  34103. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  34104. server_cbf.method = wolfTLSv1_3_server_method;
  34105. server_cbf.ssl_ready = set_post_auth_cb;
  34106. client_cbf.ssl_ready = set_post_auth_cb;
  34107. server_cbf.on_result = post_auth_cb;
  34108. client_cbf.on_result = NULL;
  34109. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34110. &server_cbf, NULL), TEST_SUCCESS);
  34111. #endif
  34112. return EXPECT_RESULT();
  34113. }
  34114. static int test_wolfSSL_X509_NID(void)
  34115. {
  34116. EXPECT_DECLS;
  34117. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  34118. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  34119. int sigType;
  34120. int nameSz;
  34121. X509* cert = NULL;
  34122. EVP_PKEY* pubKeyTmp = NULL;
  34123. X509_NAME* name = NULL;
  34124. char commonName[80];
  34125. char countryName[80];
  34126. char localityName[80];
  34127. char stateName[80];
  34128. char orgName[80];
  34129. char orgUnit[80];
  34130. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  34131. /* convert cert from DER to internal WOLFSSL_X509 struct */
  34132. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  34133. sizeof_client_cert_der_2048, HEAP_HINT));
  34134. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  34135. /* extract PUBLIC KEY from cert */
  34136. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  34137. /* extract signatureType */
  34138. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  34139. /* extract subjectName info */
  34140. ExpectNotNull(name = X509_get_subject_name(cert));
  34141. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  34142. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34143. NULL, 0)), 0);
  34144. ExpectIntEQ(nameSz, 15);
  34145. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34146. commonName, sizeof(commonName))), 0);
  34147. ExpectIntEQ(nameSz, 15);
  34148. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  34149. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34150. commonName, 9)), 0);
  34151. ExpectIntEQ(nameSz, 8);
  34152. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  34153. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  34154. countryName, sizeof(countryName))), 0);
  34155. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  34156. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  34157. localityName, sizeof(localityName))), 0);
  34158. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  34159. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34160. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  34161. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  34162. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  34163. orgName, sizeof(orgName))), 0);
  34164. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  34165. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34166. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  34167. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  34168. EVP_PKEY_free(pubKeyTmp);
  34169. X509_free(cert);
  34170. #endif
  34171. return EXPECT_RESULT();
  34172. }
  34173. static int test_wolfSSL_CTX_set_srp_username(void)
  34174. {
  34175. EXPECT_DECLS;
  34176. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34177. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34178. WOLFSSL_CTX* ctx = NULL;
  34179. WOLFSSL* ssl = NULL;
  34180. const char *username = "TESTUSER";
  34181. const char *password = "TESTPASSWORD";
  34182. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34183. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34184. SSL_SUCCESS);
  34185. wolfSSL_CTX_free(ctx);
  34186. ctx = NULL;
  34187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34188. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34189. SSL_SUCCESS);
  34190. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34191. SSL_SUCCESS);
  34192. ExpectNotNull(ssl = SSL_new(ctx));
  34193. ExpectNotNull(SSL_get_srp_username(ssl));
  34194. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  34195. wolfSSL_free(ssl);
  34196. wolfSSL_CTX_free(ctx);
  34197. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34198. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34199. return EXPECT_RESULT();
  34200. }
  34201. static int test_wolfSSL_CTX_set_srp_password(void)
  34202. {
  34203. EXPECT_DECLS;
  34204. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  34205. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34206. WOLFSSL_CTX* ctx = NULL;
  34207. const char *username = "TESTUSER";
  34208. const char *password = "TESTPASSWORD";
  34209. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34210. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34211. SSL_SUCCESS);
  34212. wolfSSL_CTX_free(ctx);
  34213. ctx = NULL;
  34214. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34215. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34216. SSL_SUCCESS);
  34217. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34218. SSL_SUCCESS);
  34219. wolfSSL_CTX_free(ctx);
  34220. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34221. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34222. return EXPECT_RESULT();
  34223. }
  34224. static int test_wolfSSL_X509_STORE(void)
  34225. {
  34226. EXPECT_DECLS;
  34227. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34228. X509_STORE *store = NULL;
  34229. #ifdef HAVE_CRL
  34230. X509_STORE_CTX *storeCtx = NULL;
  34231. X509_CRL *crl = NULL;
  34232. X509 *ca = NULL;
  34233. X509 *cert = NULL;
  34234. const char crlPem[] = "./certs/crl/crl.revoked";
  34235. const char srvCert[] = "./certs/server-revoked-cert.pem";
  34236. const char caCert[] = "./certs/ca-cert.pem";
  34237. XFILE fp = XBADFILE;
  34238. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34239. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34240. SSL_FILETYPE_PEM)));
  34241. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34242. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34243. SSL_FILETYPE_PEM)));
  34244. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34245. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34246. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34247. X509_STORE_free(store);
  34248. store = NULL;
  34249. X509_STORE_CTX_free(storeCtx);
  34250. storeCtx = NULL;
  34251. X509_free(cert);
  34252. cert = NULL;
  34253. X509_free(ca);
  34254. ca = NULL;
  34255. /* should fail to verify now after adding in CRL */
  34256. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34257. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34258. SSL_FILETYPE_PEM)));
  34259. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34260. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  34261. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34262. NULL, NULL));
  34263. if (fp != XBADFILE)
  34264. XFCLOSE(fp);
  34265. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34266. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34267. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34268. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34269. SSL_FILETYPE_PEM)));
  34270. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34271. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34272. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  34273. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  34274. X509_CRL_free(crl);
  34275. crl = NULL;
  34276. X509_STORE_free(store);
  34277. store = NULL;
  34278. X509_STORE_CTX_free(storeCtx);
  34279. storeCtx = NULL;
  34280. X509_free(cert);
  34281. cert = NULL;
  34282. X509_free(ca);
  34283. ca = NULL;
  34284. #endif /* HAVE_CRL */
  34285. #ifndef WOLFCRYPT_ONLY
  34286. {
  34287. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34288. SSL_CTX* ctx = NULL;
  34289. SSL* ssl = NULL;
  34290. int i;
  34291. for (i = 0; i < 2; i++) {
  34292. #ifndef NO_WOLFSSL_SERVER
  34293. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34294. #else
  34295. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34296. #endif
  34297. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34298. SSL_CTX_set_cert_store(ctx, store);
  34299. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34300. SSL_CTX_set_cert_store(ctx, store);
  34301. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34302. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34303. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34304. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34305. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34306. ExpectNotNull(ssl = SSL_new(ctx));
  34307. if (i == 0) {
  34308. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  34309. SSL_SUCCESS);
  34310. }
  34311. else {
  34312. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34313. #ifdef OPENSSL_ALL
  34314. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  34315. #endif
  34316. }
  34317. if (EXPECT_FAIL() || (i == 1)) {
  34318. X509_STORE_free(store);
  34319. store = NULL;
  34320. }
  34321. SSL_free(ssl);
  34322. ssl = NULL;
  34323. SSL_CTX_free(ctx);
  34324. ctx = NULL;
  34325. }
  34326. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34327. }
  34328. #endif
  34329. #endif
  34330. return EXPECT_RESULT();
  34331. }
  34332. static int test_wolfSSL_X509_STORE_load_locations(void)
  34333. {
  34334. EXPECT_DECLS;
  34335. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34336. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34337. SSL_CTX *ctx = NULL;
  34338. X509_STORE *store = NULL;
  34339. const char ca_file[] = "./certs/ca-cert.pem";
  34340. const char client_pem_file[] = "./certs/client-cert.pem";
  34341. const char client_der_file[] = "./certs/client-cert.der";
  34342. const char ecc_file[] = "./certs/ecc-key.pem";
  34343. const char certs_path[] = "./certs/";
  34344. const char bad_path[] = "./bad-path/";
  34345. #ifdef HAVE_CRL
  34346. const char crl_path[] = "./certs/crl/";
  34347. const char crl_file[] = "./certs/crl/crl.pem";
  34348. #endif
  34349. #ifndef NO_WOLFSSL_SERVER
  34350. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34351. #else
  34352. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34353. #endif
  34354. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34355. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  34356. WOLFSSL_SUCCESS);
  34357. /* Test bad arguments */
  34358. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  34359. WOLFSSL_FAILURE);
  34360. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34361. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  34362. WOLFSSL_FAILURE);
  34363. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  34364. WOLFSSL_FAILURE);
  34365. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  34366. WOLFSSL_FAILURE);
  34367. #ifdef HAVE_CRL
  34368. /* Test with CRL */
  34369. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  34370. WOLFSSL_SUCCESS);
  34371. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  34372. WOLFSSL_SUCCESS);
  34373. #endif
  34374. /* Test with CA */
  34375. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  34376. WOLFSSL_SUCCESS);
  34377. /* Test with client_cert and certs path */
  34378. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  34379. WOLFSSL_SUCCESS);
  34380. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  34381. WOLFSSL_SUCCESS);
  34382. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34383. /* Clear nodes */
  34384. ERR_clear_error();
  34385. #endif
  34386. SSL_CTX_free(ctx);
  34387. #endif
  34388. return EXPECT_RESULT();
  34389. }
  34390. static int test_X509_STORE_get0_objects(void)
  34391. {
  34392. EXPECT_DECLS;
  34393. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34394. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34395. X509_STORE *store = NULL;
  34396. X509_STORE *store_cpy = NULL;
  34397. SSL_CTX *ctx = NULL;
  34398. X509_OBJECT *obj = NULL;
  34399. STACK_OF(X509_OBJECT) *objs = NULL;
  34400. int i;
  34401. /* Setup store */
  34402. #ifndef NO_WOLFSSL_SERVER
  34403. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34404. #else
  34405. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34406. #endif
  34407. ExpectNotNull(store_cpy = X509_STORE_new());
  34408. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34409. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  34410. WOLFSSL_SUCCESS);
  34411. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  34412. WOLFSSL_SUCCESS);
  34413. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  34414. WOLFSSL_SUCCESS);
  34415. #ifdef HAVE_CRL
  34416. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  34417. WOLFSSL_SUCCESS);
  34418. #endif
  34419. /* Store ready */
  34420. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34421. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  34422. #ifdef HAVE_CRL
  34423. #ifdef WOLFSSL_SIGNER_DER_CERT
  34424. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  34425. #else
  34426. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  34427. #endif
  34428. #else
  34429. #ifdef WOLFSSL_SIGNER_DER_CERT
  34430. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  34431. #else
  34432. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  34433. #endif
  34434. #endif
  34435. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34436. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34437. switch (X509_OBJECT_get_type(obj)) {
  34438. case X509_LU_X509:
  34439. {
  34440. WOLFSSL_X509* x509;
  34441. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  34442. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  34443. break;
  34444. }
  34445. case X509_LU_CRL:
  34446. #ifdef HAVE_CRL
  34447. {
  34448. WOLFSSL_CRL* crl = NULL;
  34449. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  34450. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  34451. break;
  34452. }
  34453. #endif
  34454. case X509_LU_NONE:
  34455. default:
  34456. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34457. "(when built with crl support)"),
  34458. ("Unrecognized X509_OBJECT type or none"));
  34459. }
  34460. }
  34461. X509_STORE_free(store_cpy);
  34462. SSL_CTX_free(ctx);
  34463. #endif
  34464. return EXPECT_RESULT();
  34465. }
  34466. static int test_wolfSSL_BN_CTX(void)
  34467. {
  34468. EXPECT_DECLS;
  34469. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34470. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34471. WOLFSSL_BN_CTX* bn_ctx = NULL;
  34472. WOLFSSL_BIGNUM* t = NULL;
  34473. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34474. /* No implementation. */
  34475. BN_CTX_init(NULL);
  34476. ExpectNotNull(t = BN_CTX_get(NULL));
  34477. BN_free(t);
  34478. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  34479. BN_free(t);
  34480. #ifndef NO_WOLFSSL_STUB
  34481. /* No implementation. */
  34482. BN_CTX_start(NULL);
  34483. BN_CTX_start(bn_ctx);
  34484. #endif
  34485. BN_CTX_free(NULL);
  34486. BN_CTX_free(bn_ctx);
  34487. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34488. return EXPECT_RESULT();
  34489. }
  34490. static int test_wolfSSL_BN(void)
  34491. {
  34492. EXPECT_DECLS;
  34493. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34494. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34495. BIGNUM* a = NULL;
  34496. BIGNUM* b = NULL;
  34497. BIGNUM* c = NULL;
  34498. BIGNUM* d = NULL;
  34499. BIGNUM emptyBN;
  34500. /* Setup */
  34501. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34502. /* internal not set emptyBN. */
  34503. ExpectNotNull(a = BN_new());
  34504. ExpectNotNull(b = BN_new());
  34505. ExpectNotNull(c = BN_dup(b));
  34506. ExpectNotNull(d = BN_new());
  34507. /* Invalid parameter testing. */
  34508. BN_free(NULL);
  34509. ExpectNull(BN_dup(NULL));
  34510. ExpectNull(BN_dup(&emptyBN));
  34511. ExpectNull(BN_copy(NULL, NULL));
  34512. ExpectNull(BN_copy(b, NULL));
  34513. ExpectNull(BN_copy(NULL, c));
  34514. ExpectNull(BN_copy(b, &emptyBN));
  34515. ExpectNull(BN_copy(&emptyBN, c));
  34516. BN_clear(NULL);
  34517. BN_clear(&emptyBN);
  34518. ExpectIntEQ(BN_num_bytes(NULL), 0);
  34519. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  34520. ExpectIntEQ(BN_num_bits(NULL), 0);
  34521. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  34522. ExpectIntEQ(BN_is_negative(NULL), 0);
  34523. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  34524. /* END Invalid Parameters */
  34525. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34526. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34527. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34528. ExpectIntEQ(BN_num_bits(a), 2);
  34529. ExpectIntEQ(BN_num_bytes(a), 1);
  34530. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34531. defined(WOLFSSL_SP_INT_NEGATIVE))
  34532. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34533. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34534. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34535. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34536. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34537. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34538. {
  34539. /* Do additional tests on negative BN conversions. */
  34540. char* ret = NULL;
  34541. ASN1_INTEGER* asn1 = NULL;
  34542. BIGNUM* tmp = NULL;
  34543. /* Sanity check we have a negative BN. */
  34544. ExpectIntEQ(BN_is_negative(c), 1);
  34545. ExpectNotNull(ret = BN_bn2dec(c));
  34546. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34547. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34548. ret = NULL;
  34549. /* Convert to ASN1_INTEGER and back to BN. */
  34550. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34551. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34552. /* After converting back BN should be negative and correct. */
  34553. ExpectIntEQ(BN_is_negative(tmp), 1);
  34554. ExpectNotNull(ret = BN_bn2dec(tmp));
  34555. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34556. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34557. ASN1_INTEGER_free(asn1);
  34558. BN_free(tmp);
  34559. }
  34560. #endif
  34561. ExpectIntEQ(BN_get_word(c), 4);
  34562. #endif
  34563. ExpectIntEQ(BN_set_word(a, 3), 1);
  34564. ExpectIntEQ(BN_set_word(b, 3), 1);
  34565. ExpectIntEQ(BN_set_word(c, 4), 1);
  34566. /* NULL == NULL, NULL < num, num > NULL */
  34567. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  34568. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34569. ExpectIntLT(BN_cmp(NULL, b), 0);
  34570. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  34571. ExpectIntGT(BN_cmp(a, NULL), 0);
  34572. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  34573. ExpectIntEQ(BN_cmp(a, b), 0);
  34574. ExpectIntLT(BN_cmp(a, c), 0);
  34575. ExpectIntGT(BN_cmp(c, b), 0);
  34576. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  34577. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34578. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34579. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  34580. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34581. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  34582. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  34583. #endif
  34584. BN_clear(a);
  34585. BN_free(a);
  34586. BN_free(b);
  34587. BN_free(c);
  34588. BN_clear_free(d);
  34589. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34590. return EXPECT_RESULT();
  34591. }
  34592. static int test_wolfSSL_BN_init(void)
  34593. {
  34594. EXPECT_DECLS;
  34595. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34596. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34597. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34598. BIGNUM* ap = NULL;
  34599. BIGNUM bv;
  34600. BIGNUM cv;
  34601. BIGNUM dv;
  34602. ExpectNotNull(ap = BN_new());
  34603. BN_init(NULL);
  34604. XMEMSET(&bv, 0, sizeof(bv));
  34605. ExpectNull(BN_dup(&bv));
  34606. BN_init(&bv);
  34607. BN_init(&cv);
  34608. BN_init(&dv);
  34609. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34610. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34611. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34612. /* a^b mod c = */
  34613. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34614. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34615. /* check result 3^2 mod 5 */
  34616. ExpectIntEQ(BN_get_word(&dv), 4);
  34617. /* a*b mod c = */
  34618. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34619. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34620. /* check result 3*2 mod 5 */
  34621. ExpectIntEQ(BN_get_word(&dv), 1);
  34622. BN_free(ap);
  34623. #endif
  34624. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34625. return EXPECT_RESULT();
  34626. }
  34627. static int test_wolfSSL_BN_enc_dec(void)
  34628. {
  34629. EXPECT_DECLS;
  34630. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34631. BIGNUM* a = NULL;
  34632. BIGNUM* b = NULL;
  34633. BIGNUM* c = NULL;
  34634. BIGNUM emptyBN;
  34635. char* str = NULL;
  34636. const char* emptyStr = "";
  34637. const char* numberStr = "12345";
  34638. const char* badStr = "g12345";
  34639. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34640. const char* twoStr = "2";
  34641. #endif
  34642. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34643. unsigned char outNum[5];
  34644. /* Setup */
  34645. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34646. ExpectNotNull(a = BN_new());
  34647. ExpectNotNull(b = BN_new());
  34648. ExpectIntEQ(BN_set_word(a, 2), 1);
  34649. /* Invalid parameters */
  34650. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  34651. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34652. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  34653. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34654. ExpectNull(BN_bn2hex(NULL));
  34655. ExpectNull(BN_bn2hex(&emptyBN));
  34656. ExpectNull(BN_bn2dec(NULL));
  34657. ExpectNull(BN_bn2dec(&emptyBN));
  34658. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34659. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34660. ExpectNull(BN_bin2bn(binNum, -1, a));
  34661. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  34662. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34663. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  34664. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34665. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  34666. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34667. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  34668. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  34669. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  34670. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34671. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  34672. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34673. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  34674. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  34675. ExpectIntEQ(BN_set_word(a, 2), 1);
  34676. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  34677. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  34678. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  34679. ExpectIntEQ(BN_cmp(a, b), 0);
  34680. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34681. ExpectIntEQ(BN_cmp(a, b), -1);
  34682. ExpectNotNull(str = BN_bn2hex(a));
  34683. ExpectNotNull(BN_hex2bn(&b, str));
  34684. ExpectIntEQ(BN_cmp(a, b), 0);
  34685. ExpectNotNull(BN_hex2bn(&b, numberStr));
  34686. ExpectIntEQ(BN_cmp(a, b), -1);
  34687. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34688. str = NULL;
  34689. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34690. ExpectNotNull(str = BN_bn2dec(a));
  34691. ExpectStrEQ(str, twoStr);
  34692. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34693. str = NULL;
  34694. #ifndef NO_RSA
  34695. ExpectNotNull(str = BN_bn2dec(a));
  34696. ExpectNotNull(BN_dec2bn(&b, str));
  34697. ExpectIntEQ(BN_cmp(a, b), 0);
  34698. ExpectNotNull(BN_dec2bn(&b, numberStr));
  34699. ExpectIntEQ(BN_cmp(a, b), -1);
  34700. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34701. str = NULL;
  34702. #else
  34703. /* No implementation - fail with good parameters. */
  34704. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  34705. #endif
  34706. #endif
  34707. BN_free(b);
  34708. BN_free(a);
  34709. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34710. return EXPECT_RESULT();
  34711. }
  34712. static int test_wolfSSL_BN_word(void)
  34713. {
  34714. EXPECT_DECLS;
  34715. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34716. BIGNUM* a = NULL;
  34717. BIGNUM* b = NULL;
  34718. BIGNUM* c = NULL;
  34719. BIGNUM av;
  34720. ExpectNotNull(a = BN_new());
  34721. ExpectNotNull(b = BN_new());
  34722. ExpectNotNull(c = BN_new());
  34723. XMEMSET(&av, 0, sizeof(av));
  34724. /* Invalid parameter. */
  34725. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  34726. ExpectIntEQ(BN_add_word(&av, 3), 0);
  34727. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  34728. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  34729. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  34730. ExpectIntEQ(BN_set_word(&av, 3), 0);
  34731. ExpectIntEQ(BN_get_word(NULL), 0);
  34732. ExpectIntEQ(BN_get_word(&av), 0);
  34733. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  34734. ExpectIntEQ(BN_is_word(&av, 3), 0);
  34735. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34736. !defined(NO_DSA))
  34737. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  34738. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  34739. #endif
  34740. ExpectIntEQ(BN_one(NULL), 0);
  34741. ExpectIntEQ(BN_one(&av), 0);
  34742. BN_zero(NULL);
  34743. BN_zero(&av);
  34744. ExpectIntEQ(BN_is_one(NULL), 0);
  34745. ExpectIntEQ(BN_is_one(&av), 0);
  34746. ExpectIntEQ(BN_is_zero(NULL), 0);
  34747. ExpectIntEQ(BN_is_zero(&av), 0);
  34748. ExpectIntEQ(BN_set_word(a, 3), 1);
  34749. ExpectIntEQ(BN_set_word(b, 2), 1);
  34750. ExpectIntEQ(BN_set_word(c, 5), 1);
  34751. /* a + 3 = */
  34752. ExpectIntEQ(BN_add_word(a, 3), 1);
  34753. /* check result 3 + 3*/
  34754. ExpectIntEQ(BN_get_word(a), 6);
  34755. ExpectIntEQ(BN_is_word(a, 6), 1);
  34756. ExpectIntEQ(BN_is_word(a, 5), 0);
  34757. /* set a back to 3 */
  34758. ExpectIntEQ(BN_set_word(a, 3), 1);
  34759. /* a - 3 = */
  34760. ExpectIntEQ(BN_sub_word(a, 3), 1);
  34761. /* check result 3 - 3*/
  34762. ExpectIntEQ(BN_get_word(a), 0);
  34763. ExpectIntEQ(BN_one(a), 1);
  34764. ExpectIntEQ(BN_is_word(a, 1), 1);
  34765. ExpectIntEQ(BN_is_word(a, 0), 0);
  34766. ExpectIntEQ(BN_is_one(a), 1);
  34767. ExpectIntEQ(BN_is_zero(a), 0);
  34768. BN_zero(a);
  34769. ExpectIntEQ(BN_is_word(a, 0), 1);
  34770. ExpectIntEQ(BN_is_word(a, 1), 0);
  34771. ExpectIntEQ(BN_is_zero(a), 1);
  34772. ExpectIntEQ(BN_is_one(a), 0);
  34773. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34774. !defined(NO_DSA))
  34775. ExpectIntEQ(BN_set_word(a, 5), 1);
  34776. ExpectIntEQ(BN_mod_word(a, 3), 2);
  34777. ExpectIntEQ(BN_mod_word(a, 0), -1);
  34778. #endif
  34779. BN_free(c);
  34780. BN_free(b);
  34781. BN_free(a);
  34782. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34783. return EXPECT_RESULT();
  34784. }
  34785. static int test_wolfSSL_BN_bits(void)
  34786. {
  34787. EXPECT_DECLS;
  34788. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34789. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34790. BIGNUM* a = NULL;
  34791. BIGNUM emptyBN;
  34792. /* Setup */
  34793. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34794. ExpectNotNull(a = BN_new());
  34795. /* Invalid parameters. */
  34796. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  34797. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34798. ExpectIntEQ(BN_set_bit(a, -1), 0);
  34799. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  34800. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34801. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  34802. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  34803. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34804. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  34805. ExpectIntEQ(BN_is_odd(NULL), 0);
  34806. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  34807. ExpectIntEQ(BN_set_word(a, 0), 1);
  34808. ExpectIntEQ(BN_is_zero(a), 1);
  34809. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  34810. ExpectIntEQ(BN_is_zero(a), 0);
  34811. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  34812. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  34813. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  34814. ExpectIntEQ(BN_is_zero(a), 1);
  34815. ExpectIntEQ(BN_set_bit(a, 0), 1);
  34816. ExpectIntEQ(BN_is_odd(a), 1);
  34817. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34818. ExpectIntEQ(BN_is_odd(a), 0);
  34819. ExpectIntEQ(BN_set_bit(a, 1), 1);
  34820. ExpectIntEQ(BN_is_odd(a), 0);
  34821. ExpectIntEQ(BN_set_bit(a, 129), 1);
  34822. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34823. #ifndef NO_WOLFSSL_STUB
  34824. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  34825. #endif
  34826. BN_free(a);
  34827. #endif
  34828. return EXPECT_RESULT();
  34829. }
  34830. static int test_wolfSSL_BN_shift(void)
  34831. {
  34832. EXPECT_DECLS;
  34833. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34834. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34835. BIGNUM* a = NULL;
  34836. BIGNUM* b = NULL;
  34837. BIGNUM emptyBN;
  34838. /* Setup */
  34839. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34840. ExpectNotNull(a = BN_new());
  34841. ExpectNotNull(b = BN_new());
  34842. /* Invalid parameters. */
  34843. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34844. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34845. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34846. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  34847. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34848. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  34849. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34850. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  34851. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34852. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34853. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34854. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  34855. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34856. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  34857. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34858. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  34859. ExpectIntEQ(BN_set_word(a, 1), 1);
  34860. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  34861. ExpectIntEQ(BN_is_word(b, 2), 1);
  34862. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  34863. ExpectIntEQ(BN_is_word(a, 2), 1);
  34864. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34865. ExpectIntEQ(BN_is_word(b, 1), 1);
  34866. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34867. ExpectIntEQ(BN_is_word(a, 1), 1);
  34868. BN_free(b);
  34869. BN_free(a);
  34870. #endif
  34871. return EXPECT_RESULT();
  34872. }
  34873. static int test_wolfSSL_BN_math(void)
  34874. {
  34875. EXPECT_DECLS;
  34876. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34877. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34878. BIGNUM* a = NULL;
  34879. BIGNUM* b = NULL;
  34880. BIGNUM* r = NULL;
  34881. BIGNUM* rem = NULL;
  34882. BIGNUM emptyBN;
  34883. BN_ULONG val1;
  34884. BN_ULONG val2;
  34885. /* Setup */
  34886. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34887. ExpectNotNull(a = BN_new());
  34888. ExpectNotNull(b = BN_new());
  34889. ExpectNotNull(r = BN_new());
  34890. ExpectNotNull(rem = BN_new());
  34891. /* Invalid parameters. */
  34892. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34893. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34894. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34895. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34896. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34897. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34898. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34899. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34900. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34901. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34902. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34903. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34904. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34905. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34906. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34907. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34908. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34909. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34910. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34911. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34912. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34913. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34914. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34915. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34916. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34917. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34918. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34919. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34920. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34921. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34922. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34923. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34924. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34925. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34926. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34927. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34928. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34929. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34930. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34931. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34932. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34933. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34934. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34935. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34936. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34937. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34938. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34939. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34940. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34941. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34942. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34943. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34944. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34945. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34946. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34947. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34948. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34949. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34950. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34951. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34952. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34953. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34954. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34955. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34956. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34957. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34958. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34959. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34960. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34961. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34962. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34963. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34964. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34965. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34966. /* END Invalid parameters. */
  34967. val1 = 8;
  34968. val2 = 3;
  34969. ExpectIntEQ(BN_set_word(a, val1), 1);
  34970. ExpectIntEQ(BN_set_word(b, val2), 1);
  34971. ExpectIntEQ(BN_add(r, a, b), 1);
  34972. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34973. ExpectIntEQ(BN_sub(r, a, b), 1);
  34974. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34975. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34976. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34977. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34978. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34979. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34980. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34981. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34982. BN_free(rem);
  34983. BN_free(r);
  34984. BN_free(b);
  34985. BN_free(a);
  34986. #endif
  34987. return EXPECT_RESULT();
  34988. }
  34989. static int test_wolfSSL_BN_math_mod(void)
  34990. {
  34991. EXPECT_DECLS;
  34992. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34993. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34994. BIGNUM* a = NULL;
  34995. BIGNUM* b = NULL;
  34996. BIGNUM* m = NULL;
  34997. BIGNUM* r = NULL;
  34998. BIGNUM* t = NULL;
  34999. BIGNUM emptyBN;
  35000. BN_ULONG val1;
  35001. BN_ULONG val2;
  35002. BN_ULONG val3;
  35003. /* Setup */
  35004. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35005. ExpectNotNull(a = BN_new());
  35006. ExpectNotNull(b = BN_new());
  35007. ExpectNotNull(m = BN_new());
  35008. ExpectNotNull(r = BN_new());
  35009. /* Invalid parameters. */
  35010. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  35011. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  35012. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  35013. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  35014. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  35015. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  35016. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  35017. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  35018. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  35019. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35020. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35021. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35022. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35023. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35024. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  35025. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  35026. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  35027. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  35028. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  35029. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  35030. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  35031. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  35032. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  35033. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  35034. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  35035. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  35036. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  35037. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35038. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35039. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35040. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35041. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35042. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  35043. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  35044. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  35045. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  35046. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  35047. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  35048. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  35049. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  35050. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  35051. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  35052. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  35053. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  35054. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  35055. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35056. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35057. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35058. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35059. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35060. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  35061. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  35062. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  35063. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  35064. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  35065. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  35066. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  35067. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  35068. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  35069. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  35070. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  35071. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  35072. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  35073. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  35074. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  35075. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  35076. /* END Invalid parameters. */
  35077. val1 = 9;
  35078. val2 = 13;
  35079. val3 = 5;
  35080. ExpectIntEQ(BN_set_word(a, val1), 1);
  35081. ExpectIntEQ(BN_set_word(b, val2), 1);
  35082. ExpectIntEQ(BN_set_word(m, val3), 1);
  35083. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  35084. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  35085. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  35086. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  35087. ExpectIntEQ(BN_set_word(a, 2), 1);
  35088. ExpectIntEQ(BN_set_word(b, 3), 1);
  35089. ExpectIntEQ(BN_set_word(m, 5), 1);
  35090. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  35091. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  35092. ExpectIntEQ(BN_is_word(r, 3), 1);
  35093. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  35094. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  35095. ExpectIntEQ(BN_is_word(r, 3), 1);
  35096. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  35097. ExpectIntEQ(BN_is_word(t, 3), 1);
  35098. BN_free(t);
  35099. /* No inverse case. No inverse when a divides b. */
  35100. ExpectIntEQ(BN_set_word(a, 3), 1);
  35101. ExpectIntEQ(BN_set_word(m, 9), 1);
  35102. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  35103. BN_free(r);
  35104. BN_free(m);
  35105. BN_free(b);
  35106. BN_free(a);
  35107. #endif
  35108. return EXPECT_RESULT();
  35109. }
  35110. static int test_wolfSSL_BN_math_other(void)
  35111. {
  35112. EXPECT_DECLS;
  35113. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35114. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35115. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  35116. BIGNUM* a = NULL;
  35117. BIGNUM* b = NULL;
  35118. BIGNUM* r = NULL;
  35119. BIGNUM emptyBN;
  35120. /* Setup */
  35121. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35122. ExpectNotNull(a = BN_new());
  35123. ExpectNotNull(b = BN_new());
  35124. ExpectNotNull(r = BN_new());
  35125. /* Invalid parameters. */
  35126. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  35127. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  35128. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  35129. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  35130. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  35131. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  35132. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  35133. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35134. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  35135. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  35136. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  35137. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  35138. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  35139. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  35140. /* END Invalid parameters. */
  35141. /* No common factors between 2 and 3. */
  35142. ExpectIntEQ(BN_set_word(a, 2), 1);
  35143. ExpectIntEQ(BN_set_word(b, 3), 1);
  35144. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  35145. ExpectIntEQ(BN_is_word(r, 1), 1);
  35146. /* 3 is largest value that divides both 6 and 9. */
  35147. ExpectIntEQ(BN_set_word(a, 6), 1);
  35148. ExpectIntEQ(BN_set_word(b, 9), 1);
  35149. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  35150. ExpectIntEQ(BN_is_word(r, 3), 1);
  35151. /* GCD of 0 and 0 is undefined. */
  35152. ExpectIntEQ(BN_set_word(a, 0), 1);
  35153. ExpectIntEQ(BN_set_word(b, 0), 1);
  35154. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  35155. /* Teardown */
  35156. BN_free(r);
  35157. BN_free(b);
  35158. BN_free(a);
  35159. #endif
  35160. #endif
  35161. return EXPECT_RESULT();
  35162. }
  35163. static int test_wolfSSL_BN_rand(void)
  35164. {
  35165. EXPECT_DECLS;
  35166. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  35167. BIGNUM* bn = NULL;
  35168. BIGNUM* range = NULL;
  35169. BIGNUM emptyBN;
  35170. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35171. ExpectNotNull(bn = BN_new());
  35172. ExpectNotNull(range = BN_new());
  35173. /* Invalid parameters. */
  35174. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  35175. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35176. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  35177. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  35178. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35179. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  35180. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  35181. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35182. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  35183. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  35184. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35185. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  35186. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  35187. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  35188. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  35189. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  35190. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  35191. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  35192. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  35193. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35194. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35195. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35196. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35197. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35198. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35199. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35200. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35201. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35202. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35203. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35204. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35205. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35206. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35207. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35208. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35209. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35210. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35211. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35212. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35213. /* 1 bit random value must have no more than one top bit set. */
  35214. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35215. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35216. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35217. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35218. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35219. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35220. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35221. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35222. /* END Invalid parameters. */
  35223. /* 0 bit random: 0. */
  35224. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35225. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35226. ExpectIntEQ(BN_is_zero(bn), 1);
  35227. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  35228. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35229. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35230. ExpectIntEQ(BN_is_zero(bn), 1);
  35231. /* 1 bit random: 0 or 1. */
  35232. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35233. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35234. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35235. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35236. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35237. ExpectIntEQ(BN_get_word(bn), 1);
  35238. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35239. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35240. ExpectIntEQ(BN_get_word(bn), 1);
  35241. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35242. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35243. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35244. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35245. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35246. ExpectIntEQ(BN_get_word(bn), 1);
  35247. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35248. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35249. ExpectIntEQ(BN_get_word(bn), 1);
  35250. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35251. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35252. ExpectIntEQ(BN_num_bits(bn), 8);
  35253. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35254. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35255. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35256. ExpectIntEQ(BN_num_bits(bn), 8);
  35257. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35258. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35259. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35260. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35261. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35262. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35263. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35264. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35265. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35266. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35267. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35268. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35269. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35270. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35271. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35272. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35273. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35274. * case, requesting a 13-bit random number would actually return a 16-bit
  35275. * random number. */
  35276. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35277. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35278. ExpectIntEQ(BN_num_bits(bn), 13);
  35279. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35280. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35281. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35282. ExpectIntEQ(BN_set_word(range, 0), 1);
  35283. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35284. ExpectIntEQ(BN_set_word(range, 1), 1);
  35285. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35286. BN_free(bn);
  35287. BN_free(range);
  35288. #endif
  35289. return EXPECT_RESULT();
  35290. }
  35291. static int test_wolfSSL_BN_prime(void)
  35292. {
  35293. EXPECT_DECLS;
  35294. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35295. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35296. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35297. BIGNUM* a = NULL;
  35298. BIGNUM* add = NULL;
  35299. BIGNUM* rem = NULL;
  35300. BIGNUM emptyBN;
  35301. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35302. ExpectNotNull(a = BN_new());
  35303. ExpectNotNull(add = BN_new());
  35304. ExpectNotNull(rem = BN_new());
  35305. /* Invalid parameters. */
  35306. /* BN_generate_prime_ex()
  35307. * prime - must have valid BIGNUM
  35308. * bits - Greater then 0
  35309. * safe - not supported, must be 0
  35310. * add - not supported, must be NULL
  35311. * rem - not supported, must be NULL
  35312. * cb - anything
  35313. */
  35314. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35315. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35316. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35317. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35318. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35319. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35320. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35321. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35322. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35323. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35324. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35325. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35326. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35327. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35328. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35329. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35330. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35331. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35332. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35333. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35334. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35335. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35336. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35337. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35338. /* END Invalid parameters. */
  35339. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35340. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35341. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  35342. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35343. BN_free(rem);
  35344. BN_free(add);
  35345. BN_free(a);
  35346. #endif
  35347. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35348. return EXPECT_RESULT();
  35349. }
  35350. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35351. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35352. #define TEST_ARG 0x1234
  35353. static void msg_cb(int write_p, int version, int content_type,
  35354. const void *buf, size_t len, SSL *ssl, void *arg)
  35355. {
  35356. (void)write_p;
  35357. (void)version;
  35358. (void)content_type;
  35359. (void)buf;
  35360. (void)len;
  35361. (void)ssl;
  35362. AssertTrue(arg == (void*)TEST_ARG);
  35363. }
  35364. #endif
  35365. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35366. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35367. #if defined(SESSION_CERTS)
  35368. #include "wolfssl/internal.h"
  35369. #endif
  35370. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35371. {
  35372. EXPECT_DECLS;
  35373. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35374. STACK_OF(X509)* sk = NULL;
  35375. X509* x509 = NULL;
  35376. int i, num;
  35377. BIO* bio = NULL;
  35378. #endif
  35379. ExpectNotNull(ctx);
  35380. ExpectNotNull(ssl);
  35381. fprintf(stderr, "\n===== msgcb called ====\n");
  35382. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35383. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35384. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35385. ExpectNotNull(SSL_get0_verified_chain(ssl));
  35386. #endif
  35387. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35388. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  35389. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  35390. if (sk == NULL) {
  35391. BIO_free(bio);
  35392. return TEST_FAIL;
  35393. }
  35394. num = sk_X509_num(sk);
  35395. ExpectTrue(num > 0);
  35396. for (i = 0; i < num; i++) {
  35397. ExpectNotNull(x509 = sk_X509_value(sk,i));
  35398. if (x509 == NULL)
  35399. break;
  35400. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35401. X509_print(bio,x509);
  35402. fprintf(stderr, "\n\n");
  35403. }
  35404. BIO_free(bio);
  35405. #endif
  35406. return EXPECT_RESULT();
  35407. }
  35408. #endif
  35409. static int test_wolfSSL_msgCb(void)
  35410. {
  35411. EXPECT_DECLS;
  35412. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35413. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35414. test_ssl_cbf client_cb;
  35415. test_ssl_cbf server_cb;
  35416. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35417. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35418. #ifndef WOLFSSL_NO_TLS12
  35419. client_cb.method = wolfTLSv1_2_client_method;
  35420. server_cb.method = wolfTLSv1_2_server_method;
  35421. #else
  35422. client_cb.method = wolfTLSv1_3_client_method;
  35423. server_cb.method = wolfTLSv1_3_server_method;
  35424. #endif
  35425. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35426. &server_cb, msgCb), TEST_SUCCESS);
  35427. #endif
  35428. return EXPECT_RESULT();
  35429. }
  35430. static int test_wolfSSL_either_side(void)
  35431. {
  35432. EXPECT_DECLS;
  35433. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35434. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35435. test_ssl_cbf client_cb;
  35436. test_ssl_cbf server_cb;
  35437. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35438. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35439. /* Use different CTX for client and server */
  35440. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35441. ExpectNotNull(client_cb.ctx);
  35442. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35443. ExpectNotNull(server_cb.ctx);
  35444. /* we are responsible for free'ing WOLFSSL_CTX */
  35445. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35446. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35447. &server_cb, NULL), TEST_SUCCESS);
  35448. wolfSSL_CTX_free(client_cb.ctx);
  35449. wolfSSL_CTX_free(server_cb.ctx);
  35450. #endif
  35451. return EXPECT_RESULT();
  35452. }
  35453. static int test_wolfSSL_DTLS_either_side(void)
  35454. {
  35455. EXPECT_DECLS;
  35456. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35457. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  35458. test_ssl_cbf client_cb;
  35459. test_ssl_cbf server_cb;
  35460. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35461. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35462. /* Use different CTX for client and server */
  35463. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35464. ExpectNotNull(client_cb.ctx);
  35465. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35466. ExpectNotNull(server_cb.ctx);
  35467. /* we are responsible for free'ing WOLFSSL_CTX */
  35468. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35469. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35470. &server_cb, NULL), TEST_SUCCESS);
  35471. wolfSSL_CTX_free(client_cb.ctx);
  35472. wolfSSL_CTX_free(server_cb.ctx);
  35473. #endif
  35474. return EXPECT_RESULT();
  35475. }
  35476. static int test_generate_cookie(void)
  35477. {
  35478. EXPECT_DECLS;
  35479. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35480. SSL_CTX* ctx = NULL;
  35481. SSL* ssl = NULL;
  35482. byte buf[FOURK_BUF] = {0};
  35483. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35484. ExpectNotNull(ssl = SSL_new(ctx));
  35485. /* Test unconnected */
  35486. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35487. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35488. wolfSSL_SetCookieCtx(ssl, ctx);
  35489. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  35490. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  35491. SSL_free(ssl);
  35492. SSL_CTX_free(ctx);
  35493. #endif
  35494. return EXPECT_RESULT();
  35495. }
  35496. static int test_wolfSSL_set_options(void)
  35497. {
  35498. EXPECT_DECLS;
  35499. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35500. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35501. WOLFSSL* ssl = NULL;
  35502. WOLFSSL_CTX* ctx = NULL;
  35503. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35504. char appData[] = "extra msg";
  35505. #endif
  35506. #ifdef OPENSSL_EXTRA
  35507. unsigned char protos[] = {
  35508. 7, 't', 'l', 's', '/', '1', '.', '2',
  35509. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35510. };
  35511. unsigned int len = sizeof(protos);
  35512. void *arg = (void *)TEST_ARG;
  35513. #endif
  35514. #ifndef NO_WOLFSSL_SERVER
  35515. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35516. #else
  35517. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35518. #endif
  35519. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35520. WOLFSSL_FILETYPE_PEM));
  35521. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35522. WOLFSSL_FILETYPE_PEM));
  35523. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35524. == WOLFSSL_OP_NO_TLSv1);
  35525. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35526. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35527. WOLFSSL_OP_NO_SSLv2)), 0);
  35528. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35529. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35530. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35531. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35532. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35533. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35534. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35535. WOLFSSL_OP_NO_COMPRESSION));
  35536. wolfSSL_CTX_free(ctx);
  35537. ctx = NULL;
  35538. #ifndef NO_WOLFSSL_SERVER
  35539. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35540. #else
  35541. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35542. #endif
  35543. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35544. WOLFSSL_FILETYPE_PEM));
  35545. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35546. WOLFSSL_FILETYPE_PEM));
  35547. #ifdef OPENSSL_EXTRA
  35548. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35549. #endif
  35550. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35551. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35552. #ifdef HAVE_EX_DATA
  35553. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35554. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35555. if (ssl != NULL) {
  35556. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35557. appData, sizeof(appData)), 0);
  35558. }
  35559. #else
  35560. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35561. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35562. #endif
  35563. #endif
  35564. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35565. WOLFSSL_OP_NO_TLSv1);
  35566. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35567. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35568. WOLFSSL_OP_NO_SSLv2)), 0);
  35569. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35570. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35571. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35572. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35573. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35574. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35575. #ifdef OPENSSL_EXTRA
  35576. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35577. WOLFSSL_OP_NO_COMPRESSION));
  35578. #endif
  35579. #ifdef OPENSSL_EXTRA
  35580. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35581. wolfSSL_set_msg_callback_arg(ssl, arg);
  35582. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35583. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35584. #else
  35585. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35586. #endif
  35587. #endif
  35588. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35589. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35590. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35591. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35592. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35593. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35594. #else
  35595. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35596. #endif
  35597. #endif /* HAVE_ALPN && !NO_BIO */
  35598. #endif
  35599. wolfSSL_free(ssl);
  35600. wolfSSL_CTX_free(ctx);
  35601. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35602. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35603. return EXPECT_RESULT();
  35604. }
  35605. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35606. {
  35607. EXPECT_DECLS;
  35608. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35609. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35610. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35611. SSL* ssl = NULL;
  35612. SSL_CTX* ctx = NULL;
  35613. STACK_OF(SSL_CIPHER) *sk = NULL;
  35614. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  35615. #ifndef NO_WOLFSSL_SERVER
  35616. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35617. #else
  35618. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35619. #endif
  35620. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35621. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35622. ExpectNotNull(ssl = SSL_new(ctx));
  35623. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  35624. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35625. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  35626. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35627. /* error case because connection has not been established yet */
  35628. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35629. sk_SSL_CIPHER_free(dupSk);
  35630. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35631. SSL_free(ssl);
  35632. SSL_CTX_free(ctx);
  35633. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35634. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35635. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35636. return EXPECT_RESULT();
  35637. }
  35638. static int test_wolfSSL_set1_curves_list(void)
  35639. {
  35640. EXPECT_DECLS;
  35641. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35642. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35643. SSL* ssl = NULL;
  35644. SSL_CTX* ctx = NULL;
  35645. #ifndef NO_WOLFSSL_SERVER
  35646. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35647. #else
  35648. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35649. #endif
  35650. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35651. SSL_FILETYPE_PEM));
  35652. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35653. ExpectNotNull(ssl = SSL_new(ctx));
  35654. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35655. #ifdef HAVE_ECC
  35656. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35657. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35658. #endif
  35659. #ifdef HAVE_CURVE25519
  35660. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35661. #else
  35662. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35663. #endif
  35664. #ifdef HAVE_CURVE448
  35665. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35666. #else
  35667. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35668. #endif
  35669. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35670. #ifdef HAVE_ECC
  35671. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35672. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35673. #endif
  35674. #ifdef HAVE_CURVE25519
  35675. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35676. #else
  35677. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35678. #endif
  35679. #ifdef HAVE_CURVE448
  35680. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35681. #else
  35682. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35683. #endif
  35684. SSL_free(ssl);
  35685. SSL_CTX_free(ctx);
  35686. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35687. #endif
  35688. return EXPECT_RESULT();
  35689. }
  35690. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  35691. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  35692. static int test_wolfSSL_curves_mismatch_ctx_ready(WOLFSSL_CTX* ctx)
  35693. {
  35694. static int counter = 0;
  35695. EXPECT_DECLS;
  35696. if (counter % 2) {
  35697. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-256"),
  35698. WOLFSSL_SUCCESS);
  35699. }
  35700. else {
  35701. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-384"),
  35702. WOLFSSL_SUCCESS);
  35703. }
  35704. /* Ciphersuites that require curves */
  35705. wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384:"
  35706. "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-GCM-SHA256:"
  35707. "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
  35708. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  35709. "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-CHACHA20-POLY1305:"
  35710. "ECDHE-ECDSA-CHACHA20-POLY1305");
  35711. counter++;
  35712. return EXPECT_RESULT();
  35713. }
  35714. #endif
  35715. static int test_wolfSSL_curves_mismatch(void)
  35716. {
  35717. EXPECT_DECLS;
  35718. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  35719. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  35720. test_ssl_cbf func_cb_client;
  35721. test_ssl_cbf func_cb_server;
  35722. size_t i;
  35723. struct {
  35724. method_provider client_meth;
  35725. method_provider server_meth;
  35726. const char* desc;
  35727. int client_last_err;
  35728. int server_last_err;
  35729. } test_params[] = {
  35730. #ifdef WOLFSSL_TLS13
  35731. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3",
  35732. FATAL_ERROR, BAD_KEY_SHARE_DATA},
  35733. #endif
  35734. #ifndef WOLFSSL_NO_TLS12
  35735. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2",
  35736. FATAL_ERROR, MATCH_SUITE_ERROR},
  35737. #endif
  35738. #ifndef NO_OLD_TLS
  35739. {wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLS 1.1",
  35740. FATAL_ERROR, MATCH_SUITE_ERROR},
  35741. #endif
  35742. };
  35743. for (i = 0; i < XELEM_CNT(test_params) && !EXPECT_FAIL(); i++) {
  35744. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35745. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35746. printf("\tTesting with %s...\n", test_params[i].desc);
  35747. func_cb_client.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  35748. func_cb_server.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  35749. func_cb_client.method = test_params[i].client_meth;
  35750. func_cb_server.method = test_params[i].server_meth;
  35751. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35752. &func_cb_server, NULL), TEST_FAIL);
  35753. ExpectIntEQ(func_cb_client.last_err, test_params[i].client_last_err);
  35754. ExpectIntEQ(func_cb_server.last_err, test_params[i].server_last_err);
  35755. if (!EXPECT_SUCCESS())
  35756. break;
  35757. printf("\t%s passed\n", test_params[i].desc);
  35758. }
  35759. #endif
  35760. return EXPECT_RESULT();
  35761. }
  35762. static int test_wolfSSL_set1_sigalgs_list(void)
  35763. {
  35764. EXPECT_DECLS;
  35765. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35766. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35767. SSL* ssl = NULL;
  35768. SSL_CTX* ctx = NULL;
  35769. #ifndef NO_WOLFSSL_SERVER
  35770. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35771. #else
  35772. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35773. #endif
  35774. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35775. SSL_FILETYPE_PEM));
  35776. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35777. ExpectNotNull(ssl = SSL_new(ctx));
  35778. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35779. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35780. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35781. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35782. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35783. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35784. #ifndef NO_RSA
  35785. #ifndef NO_SHA256
  35786. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35787. WOLFSSL_FAILURE);
  35788. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35789. WOLFSSL_FAILURE);
  35790. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35791. WOLFSSL_SUCCESS);
  35792. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35793. WOLFSSL_SUCCESS);
  35794. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35795. WOLFSSL_FAILURE);
  35796. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35797. WOLFSSL_FAILURE);
  35798. #ifdef WC_RSA_PSS
  35799. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35800. WOLFSSL_SUCCESS);
  35801. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35802. WOLFSSL_SUCCESS);
  35803. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35804. WOLFSSL_SUCCESS);
  35805. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35806. WOLFSSL_SUCCESS);
  35807. #endif
  35808. #ifdef WOLFSSL_SHA512
  35809. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35810. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35811. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35812. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35813. #elif defined(WOLFSSL_SHA384)
  35814. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35815. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35816. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35817. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35818. #endif
  35819. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35820. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35821. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35822. WOLFSSL_FAILURE);
  35823. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35824. WOLFSSL_FAILURE);
  35825. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35826. WOLFSSL_FAILURE);
  35827. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35828. WOLFSSL_FAILURE);
  35829. #endif
  35830. #endif
  35831. #ifdef HAVE_ECC
  35832. #ifndef NO_SHA256
  35833. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35834. WOLFSSL_SUCCESS);
  35835. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  35836. WOLFSSL_SUCCESS);
  35837. #ifdef WOLFSSL_SHA512
  35838. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35839. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35840. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35841. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35842. #elif defined(WOLFSSL_SHA384)
  35843. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35844. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35845. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35846. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35847. #endif
  35848. #endif
  35849. #endif
  35850. #ifdef HAVE_ED25519
  35851. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35852. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35853. #endif
  35854. #ifdef HAVE_ED448
  35855. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35856. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35857. #endif
  35858. #ifndef NO_DSA
  35859. #ifndef NO_SHA256
  35860. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35861. WOLFSSL_SUCCESS);
  35862. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35863. WOLFSSL_SUCCESS);
  35864. #endif
  35865. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35866. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35867. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35868. WOLFSSL_SUCCESS);
  35869. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35870. WOLFSSL_SUCCESS);
  35871. #endif
  35872. #endif
  35873. SSL_free(ssl);
  35874. SSL_CTX_free(ctx);
  35875. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35876. #endif
  35877. return EXPECT_RESULT();
  35878. }
  35879. /* Testing wolfSSL_set_tlsext_status_type function.
  35880. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35881. */
  35882. static int test_wolfSSL_set_tlsext_status_type(void)
  35883. {
  35884. EXPECT_DECLS;
  35885. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35886. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35887. SSL* ssl = NULL;
  35888. SSL_CTX* ctx = NULL;
  35889. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35890. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35891. SSL_FILETYPE_PEM));
  35892. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35893. ExpectNotNull(ssl = SSL_new(ctx));
  35894. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35895. SSL_SUCCESS);
  35896. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35897. SSL_free(ssl);
  35898. SSL_CTX_free(ctx);
  35899. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35900. return EXPECT_RESULT();
  35901. }
  35902. #ifndef NO_BIO
  35903. static int test_wolfSSL_PEM_read_bio(void)
  35904. {
  35905. EXPECT_DECLS;
  35906. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35907. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35908. byte buff[6000];
  35909. XFILE f = XBADFILE;
  35910. int bytes;
  35911. X509* x509 = NULL;
  35912. BIO* bio = NULL;
  35913. BUF_MEM* buf = NULL;
  35914. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  35915. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  35916. if (f != XBADFILE)
  35917. XFCLOSE(f);
  35918. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35919. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35920. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35921. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35922. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35923. /* BIO should return the set EOF value */
  35924. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35925. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35926. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35927. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35928. BIO_free(bio);
  35929. BUF_MEM_free(buf);
  35930. X509_free(x509);
  35931. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  35932. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35933. return EXPECT_RESULT();
  35934. }
  35935. #if defined(OPENSSL_EXTRA)
  35936. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35937. long argl, long ret)
  35938. {
  35939. (void)bio;
  35940. (void)cmd;
  35941. (void)argp;
  35942. (void)argi;
  35943. (void)argl;
  35944. return ret;
  35945. }
  35946. #endif
  35947. static int test_wolfSSL_BIO(void)
  35948. {
  35949. EXPECT_DECLS;
  35950. #if defined(OPENSSL_EXTRA)
  35951. const unsigned char* p = NULL;
  35952. byte buff[20];
  35953. BIO* bio1 = NULL;
  35954. BIO* bio2 = NULL;
  35955. BIO* bio3 = NULL;
  35956. char* bufPt = NULL;
  35957. int i;
  35958. for (i = 0; i < 20; i++) {
  35959. buff[i] = i;
  35960. }
  35961. /* test BIO_free with NULL */
  35962. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35963. /* Creating and testing type BIO_s_bio */
  35964. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35965. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35966. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35967. /* read/write before set up */
  35968. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35969. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35970. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35971. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35972. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35973. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35974. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35975. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35976. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35977. /* write buffer full */
  35978. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35979. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35980. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35981. /* write the other direction with pair */
  35982. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35983. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35984. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35985. /* try read */
  35986. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35987. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35988. /* try read using ctrl function */
  35989. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35990. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35991. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35992. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35993. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35994. for (i = 0; i < 20; i++) {
  35995. ExpectIntEQ((int)bufPt[i], i);
  35996. }
  35997. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35998. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35999. for (i = 0; i < 8; i++) {
  36000. ExpectIntEQ((int)bufPt[i], i);
  36001. }
  36002. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  36003. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  36004. /* new pair */
  36005. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  36006. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  36007. bio2 = NULL;
  36008. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  36009. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  36010. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  36011. /* test wrap around... */
  36012. ExpectIntEQ(BIO_reset(bio1), 0);
  36013. ExpectIntEQ(BIO_reset(bio3), 0);
  36014. /* fill write buffer, read only small amount then write again */
  36015. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36016. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36017. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  36018. for (i = 0; i < 4; i++) {
  36019. ExpectIntEQ(bufPt[i], i);
  36020. }
  36021. /* try writing over read index */
  36022. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  36023. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  36024. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  36025. /* read and write 0 bytes */
  36026. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  36027. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  36028. /* should read only to end of write buffer then need to read again */
  36029. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  36030. for (i = 0; i < 16; i++) {
  36031. ExpectIntEQ(bufPt[i], buff[4 + i]);
  36032. }
  36033. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  36034. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  36035. for (i = 0; i < 4; i++) {
  36036. ExpectIntEQ(bufPt[i], 0);
  36037. }
  36038. /* read index should not have advanced with nread0 */
  36039. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  36040. for (i = 0; i < 4; i++) {
  36041. ExpectIntEQ(bufPt[i], 0);
  36042. }
  36043. /* write and fill up buffer checking reset of index state */
  36044. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36045. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36046. /* test reset on data in bio1 write buffer */
  36047. ExpectIntEQ(BIO_reset(bio1), 0);
  36048. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  36049. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  36050. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36051. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  36052. ExpectNotNull(p);
  36053. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36054. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  36055. for (i = 0; i < 6; i++) {
  36056. ExpectIntEQ(bufPt[i], i);
  36057. }
  36058. /* test case of writing twice with offset read index */
  36059. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  36060. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  36061. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36062. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  36063. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36064. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  36065. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  36066. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36067. BIO_free(bio1);
  36068. bio1 = NULL;
  36069. BIO_free(bio3);
  36070. bio3 = NULL;
  36071. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  36072. {
  36073. BIO* bioA = NULL;
  36074. BIO* bioB = NULL;
  36075. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  36076. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  36077. BIO_free(bioA);
  36078. bioA = NULL;
  36079. BIO_free(bioB);
  36080. bioB = NULL;
  36081. }
  36082. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  36083. /* BIOs with file pointers */
  36084. #if !defined(NO_FILESYSTEM)
  36085. {
  36086. XFILE f1 = XBADFILE;
  36087. XFILE f2 = XBADFILE;
  36088. BIO* f_bio1 = NULL;
  36089. BIO* f_bio2 = NULL;
  36090. unsigned char cert[300];
  36091. char testFile[] = "tests/bio_write_test.txt";
  36092. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  36093. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  36094. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  36095. /* Failure due to wrong BIO type */
  36096. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  36097. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  36098. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  36099. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  36100. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  36101. WOLFSSL_SUCCESS);
  36102. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  36103. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  36104. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  36105. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  36106. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  36107. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  36108. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  36109. ExpectIntEQ(BIO_reset(f_bio2), 0);
  36110. ExpectIntEQ(BIO_tell(NULL),-1);
  36111. ExpectIntEQ(BIO_tell(f_bio2),0);
  36112. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  36113. ExpectIntEQ(BIO_tell(f_bio2),4);
  36114. BIO_free(f_bio1);
  36115. f_bio1 = NULL;
  36116. BIO_free(f_bio2);
  36117. f_bio2 = NULL;
  36118. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  36119. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  36120. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  36121. BIO_free(f_bio1);
  36122. f_bio1 = NULL;
  36123. }
  36124. #endif /* !defined(NO_FILESYSTEM) */
  36125. /* BIO info callback */
  36126. {
  36127. const char* testArg = "test";
  36128. BIO* cb_bio = NULL;
  36129. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  36130. BIO_set_callback(cb_bio, bioCallback);
  36131. ExpectNotNull(BIO_get_callback(cb_bio));
  36132. BIO_set_callback(cb_bio, NULL);
  36133. ExpectNull(BIO_get_callback(cb_bio));
  36134. BIO_set_callback_arg(cb_bio, (char*)testArg);
  36135. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  36136. ExpectNull(BIO_get_callback_arg(NULL));
  36137. BIO_free(cb_bio);
  36138. cb_bio = NULL;
  36139. }
  36140. /* BIO_vfree */
  36141. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  36142. BIO_vfree(NULL);
  36143. BIO_vfree(bio1);
  36144. #endif
  36145. return EXPECT_RESULT();
  36146. }
  36147. static int test_wolfSSL_BIO_BIO_ring_read(void)
  36148. {
  36149. EXPECT_DECLS;
  36150. #if defined(OPENSSL_ALL)
  36151. BIO* bio1 = NULL;
  36152. BIO* bio2 = NULL;
  36153. byte data[50];
  36154. byte tmp[50];
  36155. XMEMSET(data, 42, sizeof(data));
  36156. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  36157. SSL_SUCCESS);
  36158. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  36159. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  36160. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  36161. ExpectBufEQ(tmp, data, 20);
  36162. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  36163. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  36164. ExpectBufEQ(tmp, data, 40);
  36165. BIO_free(bio1);
  36166. BIO_free(bio2);
  36167. #endif
  36168. return EXPECT_RESULT();
  36169. }
  36170. #endif /* !NO_BIO */
  36171. static int test_wolfSSL_a2i_IPADDRESS(void)
  36172. {
  36173. EXPECT_DECLS;
  36174. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  36175. const unsigned char* data = NULL;
  36176. int dataSz = 0;
  36177. ASN1_OCTET_STRING *st = NULL;
  36178. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  36179. const unsigned char ipv6_exp[] = {
  36180. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36181. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  36182. };
  36183. const unsigned char ipv6_home[] = {
  36184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  36186. };
  36187. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  36188. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  36189. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36190. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  36191. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  36192. ASN1_STRING_free(st);
  36193. st = NULL;
  36194. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  36195. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36196. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  36197. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  36198. ASN1_STRING_free(st);
  36199. st = NULL;
  36200. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  36201. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36202. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  36203. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  36204. ASN1_STRING_free(st);
  36205. #endif
  36206. return EXPECT_RESULT();
  36207. }
  36208. static int test_wolfSSL_X509_cmp_time(void)
  36209. {
  36210. EXPECT_DECLS;
  36211. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  36212. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  36213. WOLFSSL_ASN1_TIME asn_time;
  36214. time_t t;
  36215. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  36216. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  36217. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  36218. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  36219. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  36220. #endif
  36221. return EXPECT_RESULT();
  36222. }
  36223. static int test_wolfSSL_X509_time_adj(void)
  36224. {
  36225. EXPECT_DECLS;
  36226. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  36227. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  36228. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  36229. !defined(NO_ASN_TIME)
  36230. X509* x509 = NULL;
  36231. time_t t;
  36232. time_t not_before;
  36233. time_t not_after;
  36234. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36235. client_cert_der_2048, sizeof_client_cert_der_2048,
  36236. WOLFSSL_FILETYPE_ASN1));
  36237. t = 0;
  36238. not_before = wc_Time(0);
  36239. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  36240. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  36241. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  36242. /* Check X509_gmtime_adj, too. */
  36243. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  36244. X509_free(x509);
  36245. #endif
  36246. return EXPECT_RESULT();
  36247. }
  36248. static int test_wolfSSL_X509_bad_altname(void)
  36249. {
  36250. EXPECT_DECLS;
  36251. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36252. const unsigned char malformed_alt_name_cert[] = {
  36253. 0x30, 0x82, 0x02, 0xf9, 0x30, 0x82, 0x01, 0xe1, 0xa0, 0x03, 0x02, 0x01,
  36254. 0x02, 0x02, 0x02, 0x10, 0x21, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  36255. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d,
  36256. 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61, 0x31,
  36257. 0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x37, 0x31,
  36258. 0x37, 0x32, 0x34, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34, 0x30, 0x32,
  36259. 0x31, 0x34, 0x30, 0x36, 0x32, 0x36, 0x35, 0x33, 0x5a, 0x30, 0x0f, 0x31,
  36260. 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61,
  36261. 0x61, 0x61, 0x30, 0x82, 0x01, 0x20, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  36262. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
  36263. 0x0d, 0x00, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa8,
  36264. 0x8a, 0x5e, 0x26, 0x23, 0x1b, 0x31, 0xd3, 0x37, 0x1a, 0x70, 0xb2, 0xec,
  36265. 0x3f, 0x74, 0xd4, 0xb4, 0x44, 0xe3, 0x7a, 0xa5, 0xc0, 0xf5, 0xaa, 0x97,
  36266. 0x26, 0x9a, 0x04, 0xff, 0xda, 0xbe, 0xe5, 0x09, 0x03, 0x98, 0x3d, 0xb5,
  36267. 0xbf, 0x01, 0x2c, 0x9a, 0x0a, 0x3a, 0xfb, 0xbc, 0x3c, 0xe7, 0xbe, 0x83,
  36268. 0x5c, 0xb3, 0x70, 0xe8, 0x5c, 0xe3, 0xd1, 0x83, 0xc3, 0x94, 0x08, 0xcd,
  36269. 0x1a, 0x87, 0xe5, 0xe0, 0x5b, 0x9c, 0x5c, 0x6e, 0xb0, 0x7d, 0xe2, 0x58,
  36270. 0x6c, 0xc3, 0xb5, 0xc8, 0x9d, 0x11, 0xf1, 0x5d, 0x96, 0x0d, 0x66, 0x1e,
  36271. 0x56, 0x7f, 0x8f, 0x59, 0xa7, 0xa5, 0xe1, 0xc5, 0xe7, 0x81, 0x4c, 0x09,
  36272. 0x9d, 0x5e, 0x96, 0xf0, 0x9a, 0xc2, 0x8b, 0x70, 0xd5, 0xab, 0x79, 0x58,
  36273. 0x5d, 0xb7, 0x58, 0xaa, 0xfd, 0x75, 0x52, 0xaa, 0x4b, 0xa7, 0x25, 0x68,
  36274. 0x76, 0x59, 0x00, 0xee, 0x78, 0x2b, 0x91, 0xc6, 0x59, 0x91, 0x99, 0x38,
  36275. 0x3e, 0xa1, 0x76, 0xc3, 0xf5, 0x23, 0x6b, 0xe6, 0x07, 0xea, 0x63, 0x1c,
  36276. 0x97, 0x49, 0xef, 0xa0, 0xfe, 0xfd, 0x13, 0xc9, 0xa9, 0x9f, 0xc2, 0x0b,
  36277. 0xe6, 0x87, 0x92, 0x5b, 0xcc, 0xf5, 0x42, 0x95, 0x4a, 0xa4, 0x6d, 0x64,
  36278. 0xba, 0x7d, 0xce, 0xcb, 0x04, 0xd0, 0xf8, 0xe7, 0xe3, 0xda, 0x75, 0x60,
  36279. 0xd3, 0x8b, 0x6a, 0x64, 0xfc, 0x78, 0x56, 0x21, 0x69, 0x5a, 0xe8, 0xa7,
  36280. 0x8f, 0xfb, 0x8f, 0x82, 0xe3, 0xae, 0x36, 0xa2, 0x93, 0x66, 0x92, 0xcb,
  36281. 0x82, 0xa3, 0xbe, 0x84, 0x00, 0x86, 0xdc, 0x7e, 0x6d, 0x53, 0x77, 0x84,
  36282. 0x17, 0xb9, 0x55, 0x43, 0x0d, 0xf1, 0x16, 0x1f, 0xd5, 0x43, 0x75, 0x99,
  36283. 0x66, 0x19, 0x52, 0xd0, 0xac, 0x5f, 0x74, 0xad, 0xb2, 0x90, 0x15, 0x50,
  36284. 0x04, 0x74, 0x43, 0xdf, 0x6c, 0x35, 0xd0, 0xfd, 0x32, 0x37, 0xb3, 0x8d,
  36285. 0xf5, 0xe5, 0x09, 0x02, 0x01, 0x03, 0xa3, 0x61, 0x30, 0x5f, 0x30, 0x0c,
  36286. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
  36287. 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82,
  36288. 0x04, 0x61, 0x2a, 0x00, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
  36289. 0x04, 0x16, 0x04, 0x14, 0x92, 0x6a, 0x1e, 0x52, 0x3a, 0x1a, 0x57, 0x9f,
  36290. 0xc9, 0x82, 0x9a, 0xce, 0xc8, 0xc0, 0xa9, 0x51, 0x9d, 0x2f, 0xc7, 0x72,
  36291. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  36292. 0x14, 0x6b, 0xf9, 0xa4, 0x2d, 0xa5, 0xe9, 0x39, 0x89, 0xa8, 0x24, 0x58,
  36293. 0x79, 0x87, 0x11, 0xfc, 0x6f, 0x07, 0x91, 0xef, 0xa6, 0x30, 0x0d, 0x06,
  36294. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
  36295. 0x03, 0x82, 0x01, 0x01, 0x00, 0x3f, 0xd5, 0x37, 0x2f, 0xc7, 0xf8, 0x8b,
  36296. 0x39, 0x1c, 0xe3, 0xdf, 0x77, 0xee, 0xc6, 0x4b, 0x5f, 0x84, 0xcf, 0xfa,
  36297. 0x33, 0x2c, 0xb2, 0xb5, 0x4b, 0x09, 0xee, 0x56, 0xc0, 0xf2, 0xf0, 0xeb,
  36298. 0xad, 0x1c, 0x02, 0xef, 0xae, 0x09, 0x53, 0xc0, 0x06, 0xad, 0x4e, 0xfd,
  36299. 0x3e, 0x8c, 0x13, 0xb3, 0xbf, 0x80, 0x05, 0x36, 0xb5, 0x3f, 0x2b, 0xc7,
  36300. 0x60, 0x53, 0x14, 0xbf, 0x33, 0x63, 0x47, 0xc3, 0xc6, 0x28, 0xda, 0x10,
  36301. 0x12, 0xe2, 0xc4, 0xeb, 0xc5, 0x64, 0x66, 0xc0, 0xcc, 0x6b, 0x84, 0xda,
  36302. 0x0c, 0xe9, 0xf6, 0xe3, 0xf8, 0x8e, 0x3d, 0x95, 0x5f, 0xba, 0x9f, 0xe1,
  36303. 0xc7, 0xed, 0x6e, 0x97, 0xcc, 0xbd, 0x7d, 0xe5, 0x4e, 0xab, 0xbc, 0x1b,
  36304. 0xf1, 0x3a, 0x09, 0x33, 0x09, 0xe1, 0xcc, 0xec, 0x21, 0x16, 0x8e, 0xb1,
  36305. 0x74, 0x9e, 0xc8, 0x13, 0x7c, 0xdf, 0x07, 0xaa, 0xeb, 0x70, 0xd7, 0x91,
  36306. 0x5c, 0xc4, 0xef, 0x83, 0x88, 0xc3, 0xe4, 0x97, 0xfa, 0xe4, 0xdf, 0xd7,
  36307. 0x0d, 0xff, 0xba, 0x78, 0x22, 0xfc, 0x3f, 0xdc, 0xd8, 0x02, 0x8d, 0x93,
  36308. 0x57, 0xf9, 0x9e, 0x39, 0x3a, 0x77, 0x00, 0xd9, 0x19, 0xaa, 0x68, 0xa1,
  36309. 0xe6, 0x9e, 0x13, 0xeb, 0x37, 0x16, 0xf5, 0x77, 0xa4, 0x0b, 0x40, 0x04,
  36310. 0xd3, 0xa5, 0x49, 0x78, 0x35, 0xfa, 0x3b, 0xf6, 0x02, 0xab, 0x85, 0xee,
  36311. 0xcb, 0x9b, 0x62, 0xda, 0x05, 0x00, 0x22, 0x2f, 0xf8, 0xbd, 0x0b, 0xe5,
  36312. 0x2c, 0xb2, 0x53, 0x78, 0x0a, 0xcb, 0x69, 0xc0, 0xb6, 0x9f, 0x96, 0xff,
  36313. 0x58, 0x22, 0x70, 0x9c, 0x01, 0x2e, 0x56, 0x60, 0x5d, 0x37, 0xe3, 0x40,
  36314. 0x25, 0xc9, 0x90, 0xc8, 0x0f, 0x41, 0x68, 0xb4, 0xfd, 0x10, 0xe2, 0x09,
  36315. 0x99, 0x08, 0x5d, 0x7b, 0xc9, 0xe3, 0x29, 0xd4, 0x5a, 0xcf, 0xc9, 0x34,
  36316. 0x55, 0xa1, 0x40, 0x44, 0xd6, 0x88, 0x16, 0xbb, 0xdd
  36317. };
  36318. X509* x509 = NULL;
  36319. int certSize = (int)sizeof(malformed_alt_name_cert) / sizeof(unsigned char);
  36320. const char *name = "aaaaa";
  36321. int nameLen = (int)XSTRLEN(name);
  36322. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36323. malformed_alt_name_cert, certSize, SSL_FILETYPE_ASN1));
  36324. /* malformed_alt_name_cert has a malformed alternative
  36325. * name of "a*\0*". Ensure that it does not match "aaaaa" */
  36326. ExpectIntNE(wolfSSL_X509_check_host(x509, name, nameLen,
  36327. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  36328. X509_free(x509);
  36329. #endif
  36330. return EXPECT_RESULT();
  36331. }
  36332. static int test_wolfSSL_X509_name_match(void)
  36333. {
  36334. EXPECT_DECLS;
  36335. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36336. /* A certificate with the subject alternative name a* */
  36337. const unsigned char cert_der[] = {
  36338. 0x30, 0x82, 0x03, 0xac, 0x30, 0x82, 0x02, 0x94, 0xa0, 0x03, 0x02, 0x01,
  36339. 0x02, 0x02, 0x14, 0x0f, 0xa5, 0x10, 0x85, 0xef, 0x58, 0x10, 0x59, 0xfc,
  36340. 0x0f, 0x20, 0x1f, 0x53, 0xf5, 0x30, 0x39, 0x34, 0x49, 0x54, 0x05, 0x30,
  36341. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  36342. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  36343. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  36344. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  36345. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  36346. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  36347. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  36348. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  36349. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  36350. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  36351. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  36352. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  36353. 0x30, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  36354. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x30,
  36355. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  36356. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  36357. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  36358. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  36359. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  36360. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  36361. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  36362. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  36363. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  36364. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  36365. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  36366. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  36367. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xf4, 0xca, 0x3d,
  36368. 0xd4, 0xbc, 0x9b, 0xea, 0x74, 0xfe, 0x73, 0xf4, 0x16, 0x23, 0x0b, 0x4a,
  36369. 0x09, 0x54, 0xf6, 0x7b, 0x10, 0x99, 0x11, 0x93, 0xb2, 0xdb, 0x4d, 0x7d,
  36370. 0x23, 0xab, 0xf9, 0xcd, 0xf6, 0x54, 0xd4, 0xf6, 0x39, 0x57, 0xee, 0x97,
  36371. 0xb2, 0xb9, 0xfc, 0x7e, 0x9c, 0xb3, 0xfb, 0x56, 0xb6, 0x84, 0xd6, 0x2d,
  36372. 0x59, 0x1c, 0xed, 0xda, 0x9b, 0x19, 0xf5, 0x8a, 0xa7, 0x8a, 0x89, 0xd6,
  36373. 0xa1, 0xc0, 0xe6, 0x16, 0xad, 0x04, 0xcf, 0x5a, 0x1f, 0xdf, 0x62, 0x6c,
  36374. 0x68, 0x45, 0xe9, 0x55, 0x2e, 0x42, 0xa3, 0x1b, 0x3b, 0x86, 0x23, 0x22,
  36375. 0xa1, 0x20, 0x48, 0xd1, 0x52, 0xc0, 0x8b, 0xab, 0xe2, 0x8a, 0x15, 0x68,
  36376. 0xbd, 0x89, 0x6f, 0x9f, 0x45, 0x75, 0xb4, 0x27, 0xc1, 0x72, 0x41, 0xfd,
  36377. 0x79, 0x89, 0xb0, 0x74, 0xa2, 0xe9, 0x61, 0x48, 0x4c, 0x54, 0xad, 0x6b,
  36378. 0x61, 0xbf, 0x0e, 0x27, 0x58, 0xb4, 0xf6, 0x9c, 0x2c, 0x9f, 0xc2, 0x3e,
  36379. 0x3b, 0xb3, 0x90, 0x41, 0xbc, 0x61, 0xcd, 0x01, 0x57, 0x90, 0x82, 0xec,
  36380. 0x46, 0xba, 0x4f, 0x89, 0x8e, 0x7f, 0x49, 0x4f, 0x46, 0x69, 0x37, 0x8b,
  36381. 0xa0, 0xba, 0x85, 0xe8, 0x42, 0xff, 0x9a, 0xa1, 0x53, 0x81, 0x5c, 0xf3,
  36382. 0x8e, 0x85, 0x1c, 0xd4, 0x90, 0x60, 0xa0, 0x37, 0x59, 0x04, 0x65, 0xa6,
  36383. 0xb5, 0x12, 0x00, 0xc3, 0x04, 0x51, 0xa7, 0x83, 0x96, 0x62, 0x3d, 0x49,
  36384. 0x97, 0xe8, 0x6b, 0x9a, 0x5d, 0x51, 0x24, 0xee, 0xad, 0x45, 0x18, 0x0f,
  36385. 0x3f, 0x97, 0xec, 0xdf, 0xcf, 0x42, 0x8a, 0x96, 0xc7, 0xd8, 0x82, 0x87,
  36386. 0x7f, 0x57, 0x70, 0x22, 0xfb, 0x29, 0x3e, 0x3c, 0xa3, 0xc1, 0xd5, 0x71,
  36387. 0xb3, 0x84, 0x06, 0x53, 0xa3, 0x86, 0x20, 0x35, 0xe3, 0x41, 0xb9, 0xd8,
  36388. 0x00, 0x22, 0x4f, 0x6d, 0xe6, 0xfd, 0xf0, 0xf4, 0xa2, 0x39, 0x0a, 0x1a,
  36389. 0x23, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x30, 0x30, 0x2e, 0x30, 0x0d,
  36390. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x06, 0x30, 0x04, 0x82, 0x02, 0x61,
  36391. 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  36392. 0x45, 0x05, 0xf3, 0x4d, 0x3e, 0x7e, 0x9c, 0xf5, 0x08, 0xee, 0x2c, 0x13,
  36393. 0x32, 0xe3, 0xf2, 0x14, 0xe8, 0x0e, 0x71, 0x21, 0x30, 0x0d, 0x06, 0x09,
  36394. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  36395. 0x82, 0x01, 0x01, 0x00, 0xa8, 0x28, 0xe5, 0x22, 0x65, 0xcf, 0x47, 0xfe,
  36396. 0x82, 0x17, 0x99, 0x20, 0xdb, 0xb1, 0x57, 0xd4, 0x91, 0x1a, 0x83, 0xde,
  36397. 0xc1, 0xaf, 0xc4, 0x1f, 0xfb, 0xa4, 0x6a, 0xad, 0xdc, 0x58, 0x72, 0xd9,
  36398. 0x9b, 0xab, 0xa5, 0xbb, 0xf4, 0x98, 0xd4, 0xdf, 0x36, 0xcb, 0xb5, 0x78,
  36399. 0xce, 0x4b, 0x25, 0x5b, 0x24, 0x92, 0xfe, 0xe8, 0xd4, 0xe4, 0xbd, 0x6f,
  36400. 0x71, 0x1a, 0x81, 0x2a, 0x6f, 0x35, 0x93, 0xf7, 0xcc, 0xed, 0xe5, 0x06,
  36401. 0xd2, 0x96, 0x41, 0xb5, 0xa9, 0x8a, 0xc0, 0xc9, 0x17, 0xe3, 0x13, 0x5e,
  36402. 0x94, 0x5e, 0xfa, 0xfc, 0xf0, 0x00, 0x2e, 0xe1, 0xd8, 0x1b, 0x23, 0x3f,
  36403. 0x7c, 0x4d, 0x9f, 0xfb, 0xb7, 0x95, 0xc1, 0x94, 0x7f, 0x7f, 0xb5, 0x4f,
  36404. 0x93, 0x6d, 0xc3, 0x2b, 0xb2, 0x28, 0x36, 0xd2, 0x7c, 0x01, 0x3c, 0xae,
  36405. 0x35, 0xdb, 0xc8, 0x95, 0x1b, 0x5f, 0x6c, 0x0f, 0x57, 0xb3, 0xcc, 0x97,
  36406. 0x98, 0x80, 0x06, 0xaa, 0xe4, 0x93, 0x1f, 0xb7, 0xa0, 0x54, 0xf1, 0x4f,
  36407. 0x6f, 0x11, 0xdf, 0xab, 0xd3, 0xbf, 0xf0, 0x3a, 0x81, 0x60, 0xaf, 0x7a,
  36408. 0xf7, 0x09, 0xd5, 0xae, 0x0c, 0x7d, 0xae, 0x8d, 0x47, 0x06, 0xbe, 0x11,
  36409. 0x6e, 0xf8, 0x7e, 0x49, 0xf8, 0xac, 0x24, 0x0a, 0x4b, 0xc2, 0xf6, 0xe8,
  36410. 0x2c, 0xec, 0x35, 0xef, 0xa9, 0x13, 0xb8, 0xd2, 0x9c, 0x92, 0x61, 0x91,
  36411. 0xec, 0x7b, 0x0c, 0xea, 0x9a, 0x71, 0x36, 0x15, 0x34, 0x2b, 0x7a, 0x25,
  36412. 0xac, 0xfe, 0xc7, 0x26, 0x89, 0x70, 0x3e, 0x64, 0x68, 0x97, 0x4b, 0xaa,
  36413. 0xc1, 0x24, 0x14, 0xbd, 0x45, 0x2f, 0xe0, 0xfe, 0xf4, 0x2b, 0x8e, 0x08,
  36414. 0x3e, 0xe4, 0xb5, 0x3d, 0x5d, 0xf4, 0xc3, 0xd6, 0x9c, 0xb5, 0x33, 0x1b,
  36415. 0x3b, 0xda, 0x6e, 0x99, 0x7b, 0x09, 0xd1, 0x30, 0x97, 0x23, 0x52, 0x6d,
  36416. 0x1b, 0x71, 0x3a, 0xf4, 0x54, 0xf0, 0xe5, 0x9e
  36417. };
  36418. WOLFSSL_X509* x509 = NULL;
  36419. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  36420. const char *name1 = "aaaaa";
  36421. int nameLen1 = (int)(XSTRLEN(name1));
  36422. const char *name2 = "a";
  36423. int nameLen2 = (int)(XSTRLEN(name2));
  36424. const char *name3 = "abbbb";
  36425. int nameLen3 = (int)(XSTRLEN(name3));
  36426. const char *name4 = "bbb";
  36427. int nameLen4 = (int)(XSTRLEN(name4));
  36428. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36429. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  36430. /* Ensure that "a*" matches "aaaaa" */
  36431. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  36432. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36433. /* Ensure that "a*" matches "a" */
  36434. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  36435. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36436. /* Ensure that "a*" matches "abbbb" */
  36437. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  36438. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36439. /* Ensure that "a*" does not match "bbb" */
  36440. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  36441. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  36442. wolfSSL_X509_free(x509);
  36443. #endif
  36444. return EXPECT_RESULT();
  36445. }
  36446. static int test_wolfSSL_X509_name_match2(void)
  36447. {
  36448. EXPECT_DECLS;
  36449. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36450. /* A certificate with the subject alternative name a*b* */
  36451. const unsigned char cert_der[] = {
  36452. 0x30, 0x82, 0x03, 0xae, 0x30, 0x82, 0x02, 0x96, 0xa0, 0x03, 0x02, 0x01,
  36453. 0x02, 0x02, 0x14, 0x41, 0x8c, 0x8b, 0xaa, 0x0e, 0xd8, 0x5a, 0xc0, 0x52,
  36454. 0x46, 0x0e, 0xe5, 0xd8, 0xb9, 0x48, 0x93, 0x7e, 0x8a, 0x7c, 0x65, 0x30,
  36455. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  36456. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  36457. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  36458. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  36459. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  36460. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  36461. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  36462. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  36463. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  36464. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  36465. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  36466. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  36467. 0x30, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  36468. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x30,
  36469. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  36470. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  36471. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  36472. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  36473. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  36474. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  36475. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  36476. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  36477. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  36478. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  36479. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  36480. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  36481. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa5, 0x60, 0x80,
  36482. 0xf3, 0xee, 0x19, 0xd2, 0xe4, 0x15, 0x94, 0x54, 0x12, 0x88, 0xee, 0xda,
  36483. 0x11, 0x11, 0x87, 0x99, 0x88, 0xb3, 0x71, 0xc7, 0x97, 0x78, 0x1b, 0x57,
  36484. 0x37, 0x1d, 0x0b, 0x1f, 0x2f, 0x2c, 0x35, 0x13, 0x75, 0xd3, 0x31, 0x3e,
  36485. 0x6f, 0x80, 0x21, 0xa5, 0xa3, 0xad, 0x10, 0x81, 0xb6, 0x37, 0xd4, 0x55,
  36486. 0x2e, 0xc1, 0xb8, 0x37, 0xa3, 0x3c, 0xe8, 0x81, 0x03, 0x3c, 0xda, 0x5f,
  36487. 0x6f, 0x45, 0x32, 0x2b, 0x0e, 0x99, 0x27, 0xfd, 0xe5, 0x6c, 0x07, 0xd9,
  36488. 0x4e, 0x0a, 0x8b, 0x23, 0x74, 0x96, 0x25, 0x97, 0xae, 0x6d, 0x19, 0xba,
  36489. 0xbf, 0x0f, 0xc8, 0xa1, 0xe5, 0xea, 0xa8, 0x00, 0x09, 0xc3, 0x9a, 0xef,
  36490. 0x09, 0x33, 0xc1, 0x33, 0x2e, 0x7b, 0x6d, 0xa7, 0x66, 0x87, 0xb6, 0x3a,
  36491. 0xb9, 0xdb, 0x4c, 0x5e, 0xb5, 0x55, 0x69, 0x37, 0x17, 0x92, 0x1f, 0xe3,
  36492. 0x53, 0x1a, 0x2d, 0x25, 0xd0, 0xcf, 0x72, 0x37, 0xc2, 0x89, 0x83, 0x78,
  36493. 0xcf, 0xac, 0x2e, 0x46, 0x92, 0x5c, 0x4a, 0xba, 0x7d, 0xa0, 0x22, 0x34,
  36494. 0xb1, 0x22, 0x26, 0x99, 0xda, 0xe8, 0x97, 0xe2, 0x0c, 0xd3, 0xbc, 0x97,
  36495. 0x7e, 0xa8, 0xb9, 0xe3, 0xe2, 0x7f, 0x56, 0xef, 0x22, 0xee, 0x15, 0x95,
  36496. 0xa6, 0xd1, 0xf4, 0xa7, 0xac, 0x4a, 0xab, 0xc1, 0x1a, 0xda, 0xc5, 0x5f,
  36497. 0xa5, 0x5e, 0x2f, 0x15, 0x9c, 0x36, 0xbe, 0xd3, 0x47, 0xb6, 0x86, 0xb9,
  36498. 0xc6, 0x59, 0x39, 0x36, 0xad, 0x84, 0x53, 0x95, 0x72, 0x91, 0x89, 0x51,
  36499. 0x32, 0x77, 0xf1, 0xa5, 0x93, 0xfe, 0xf0, 0x41, 0x7c, 0x64, 0xf1, 0xb0,
  36500. 0x8b, 0x81, 0x8d, 0x3a, 0x2c, 0x9e, 0xbe, 0x2e, 0x8b, 0xf7, 0x80, 0x63,
  36501. 0x35, 0x32, 0xfa, 0x26, 0xe0, 0x63, 0xbf, 0x5e, 0xaf, 0xf0, 0x08, 0xe0,
  36502. 0x80, 0x65, 0x38, 0xfa, 0x21, 0xaa, 0x91, 0x34, 0x48, 0x3d, 0x32, 0x5c,
  36503. 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x32, 0x30, 0x30, 0x30, 0x0f,
  36504. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82, 0x04, 0x61,
  36505. 0x2a, 0x62, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16,
  36506. 0x04, 0x14, 0x3d, 0x55, 0x74, 0xf8, 0x3a, 0x26, 0x03, 0x8c, 0x6a, 0x2e,
  36507. 0x91, 0x0e, 0x18, 0x70, 0xb4, 0xa4, 0xcc, 0x04, 0x00, 0xd3, 0x30, 0x0d,
  36508. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  36509. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x8f, 0x3b, 0xff, 0x46, 0x0c, 0xb5,
  36510. 0x21, 0xdc, 0xcf, 0x61, 0x9a, 0x25, 0x93, 0x99, 0x68, 0x2f, 0x16, 0x71,
  36511. 0x15, 0x00, 0x5f, 0xb0, 0x9b, 0x43, 0x5c, 0x47, 0xe2, 0x8e, 0xc8, 0xea,
  36512. 0xb3, 0x30, 0x4d, 0x87, 0x90, 0xcf, 0x24, 0x37, 0x5c, 0xfd, 0xc8, 0xc6,
  36513. 0x09, 0x36, 0xb2, 0xfb, 0xfd, 0xc1, 0x82, 0x92, 0x77, 0x5b, 0x9d, 0xeb,
  36514. 0xac, 0x47, 0xbc, 0xda, 0x7c, 0x89, 0x19, 0x03, 0x9e, 0xcd, 0x96, 0x2a,
  36515. 0x90, 0x55, 0x23, 0x19, 0xac, 0x9d, 0x49, 0xfb, 0xa0, 0x31, 0x7d, 0x6b,
  36516. 0x1a, 0x16, 0x13, 0xb1, 0xa9, 0xc9, 0xc4, 0xaf, 0xf1, 0xb4, 0xa7, 0x9b,
  36517. 0x08, 0x64, 0x6a, 0x09, 0xcd, 0x4a, 0x03, 0x4c, 0x93, 0xb6, 0xcf, 0x29,
  36518. 0xdb, 0x56, 0x88, 0x8e, 0xed, 0x08, 0x6d, 0x8d, 0x76, 0xa3, 0xd7, 0xc6,
  36519. 0x69, 0xa1, 0xf5, 0xd2, 0xd0, 0x0a, 0x4b, 0xfa, 0x88, 0x66, 0x6c, 0xe5,
  36520. 0x4a, 0xee, 0x13, 0xad, 0xad, 0x22, 0x25, 0x73, 0x39, 0x56, 0x74, 0x0e,
  36521. 0xda, 0xcd, 0x35, 0x67, 0xe3, 0x81, 0x5c, 0xc5, 0xae, 0x3c, 0x4f, 0x47,
  36522. 0x3e, 0x97, 0xde, 0xac, 0xf6, 0xe1, 0x26, 0xe2, 0xe0, 0x66, 0x48, 0x20,
  36523. 0x7c, 0x02, 0x81, 0x3e, 0x7d, 0x34, 0xb7, 0x73, 0x3e, 0x2e, 0xd6, 0x20,
  36524. 0x1c, 0xdf, 0xf1, 0xae, 0x86, 0x8b, 0xb2, 0xc2, 0x9b, 0x68, 0x9c, 0xf6,
  36525. 0x1a, 0x5e, 0x30, 0x06, 0x39, 0x0a, 0x1f, 0x7b, 0xd7, 0x18, 0x4b, 0x06,
  36526. 0x9d, 0xff, 0x84, 0x57, 0xcc, 0x92, 0xad, 0x81, 0x0a, 0x19, 0x11, 0xc4,
  36527. 0xac, 0x59, 0x00, 0xe8, 0x5a, 0x70, 0x78, 0xd6, 0x9f, 0xe0, 0x82, 0x2a,
  36528. 0x1f, 0x09, 0x36, 0x1c, 0x52, 0x98, 0xf7, 0x95, 0x8f, 0xf9, 0x48, 0x4f,
  36529. 0x30, 0x52, 0xb5, 0xf3, 0x8d, 0x13, 0x93, 0x27, 0xbe, 0xb4, 0x75, 0x39,
  36530. 0x65, 0xc6, 0x48, 0x4e, 0x32, 0xd7, 0xf4, 0xc3, 0x26, 0x8d
  36531. };
  36532. WOLFSSL_X509* x509 = NULL;
  36533. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  36534. const char *name1 = "ab";
  36535. int nameLen1 = (int)(XSTRLEN(name1));
  36536. const char *name2 = "acccbccc";
  36537. int nameLen2 = (int)(XSTRLEN(name2));
  36538. const char *name3 = "accb";
  36539. int nameLen3 = (int)(XSTRLEN(name3));
  36540. const char *name4 = "accda";
  36541. int nameLen4 = (int)(XSTRLEN(name4));
  36542. const char *name5 = "acc\0bcc";
  36543. int nameLen5 = 7;
  36544. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36545. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  36546. /* Ensure that "a*b*" matches "ab" */
  36547. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  36548. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36549. /* Ensure that "a*b*" matches "acccbccc" */
  36550. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  36551. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36552. /* Ensure that "a*b*" matches "accb" */
  36553. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  36554. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36555. /* Ensure that "a*b*" does not match "accda" */
  36556. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  36557. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36558. /* Ensure that "a*b*" matches "ab", testing openssl behavior replication
  36559. * on check len input handling, 0 for len is OK as it should then use
  36560. * strlen(name1) */
  36561. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, 0,
  36562. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36563. /* Openssl also allows for len to include NULL terminator */
  36564. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1 + 1,
  36565. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36566. /* Ensure that check string with NULL terminator in middle is
  36567. * rejected */
  36568. ExpectIntNE(wolfSSL_X509_check_host(x509, name5, nameLen5,
  36569. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36570. wolfSSL_X509_free(x509);
  36571. #endif
  36572. return EXPECT_RESULT();
  36573. }
  36574. static int test_wolfSSL_X509_name_match3(void)
  36575. {
  36576. EXPECT_DECLS;
  36577. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36578. /* A certificate with the subject alternative name *.example.com */
  36579. const unsigned char cert_der[] = {
  36580. 0x30, 0x82, 0x03, 0xb7, 0x30, 0x82, 0x02, 0x9f, 0xa0, 0x03, 0x02, 0x01,
  36581. 0x02, 0x02, 0x14, 0x59, 0xbb, 0xf6, 0xde, 0xb8, 0x3d, 0x0e, 0x8c, 0xe4,
  36582. 0xbd, 0x98, 0xa3, 0xbe, 0x3e, 0x8f, 0xdc, 0xbd, 0x7f, 0xcc, 0xae, 0x30,
  36583. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  36584. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  36585. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  36586. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  36587. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  36588. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  36589. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  36590. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  36591. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  36592. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  36593. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  36594. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  36595. 0x31, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  36596. 0x30, 0x35, 0x32, 0x39, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x30,
  36597. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  36598. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  36599. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  36600. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  36601. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  36602. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  36603. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  36604. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  36605. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  36606. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  36607. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  36608. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  36609. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0x78, 0x16,
  36610. 0x05, 0x65, 0xf2, 0x85, 0xf2, 0x61, 0x7f, 0xb1, 0x4d, 0x73, 0xe2, 0x82,
  36611. 0xb5, 0x3d, 0xf7, 0x9d, 0x05, 0x65, 0xed, 0x9d, 0xc3, 0x29, 0x7a, 0x92,
  36612. 0x2c, 0x06, 0x5f, 0xc8, 0x13, 0x55, 0x42, 0x4e, 0xbd, 0xe2, 0x56, 0x2a,
  36613. 0x4b, 0xac, 0xe6, 0x1b, 0x10, 0xc9, 0xdb, 0x9a, 0x45, 0x36, 0xed, 0xf3,
  36614. 0x26, 0x8c, 0x22, 0x88, 0x1e, 0x6d, 0x2b, 0x41, 0xfa, 0x0d, 0x43, 0x88,
  36615. 0x88, 0xde, 0x8d, 0x2e, 0xca, 0x6e, 0x7c, 0x62, 0x66, 0x3e, 0xfa, 0x4e,
  36616. 0x71, 0xea, 0x7d, 0x3b, 0x32, 0x33, 0x5c, 0x7a, 0x7e, 0xea, 0x74, 0xbd,
  36617. 0xb6, 0x8f, 0x4c, 0x1c, 0x7a, 0x79, 0x94, 0xf1, 0xe8, 0x02, 0x67, 0x98,
  36618. 0x25, 0xb4, 0x31, 0x80, 0xc1, 0xae, 0xbf, 0xef, 0xf2, 0x6c, 0x78, 0x42,
  36619. 0xef, 0xb5, 0xc6, 0x01, 0x47, 0x79, 0x8d, 0x92, 0xce, 0xc1, 0xb5, 0x98,
  36620. 0x76, 0xf0, 0x84, 0xa2, 0x53, 0x90, 0xe5, 0x39, 0xc7, 0xbd, 0xf2, 0xbb,
  36621. 0xe3, 0x3f, 0x00, 0xf6, 0xf0, 0x46, 0x86, 0xee, 0x55, 0xbd, 0x2c, 0x1f,
  36622. 0x97, 0x24, 0x7c, 0xbc, 0xda, 0x2f, 0x1b, 0x53, 0xef, 0x26, 0x56, 0xcc,
  36623. 0xb7, 0xd8, 0xca, 0x17, 0x20, 0x4e, 0x62, 0x03, 0x66, 0x32, 0xb3, 0xd1,
  36624. 0x71, 0x26, 0x6c, 0xff, 0xd1, 0x9e, 0x44, 0x86, 0x2a, 0xae, 0xba, 0x43,
  36625. 0x00, 0x13, 0x7e, 0x50, 0xdd, 0x3e, 0x27, 0x39, 0x70, 0x1c, 0x0c, 0x0b,
  36626. 0xe8, 0xa2, 0xae, 0x03, 0x09, 0x2e, 0xd8, 0x71, 0xee, 0x7b, 0x1a, 0x09,
  36627. 0x2d, 0xe1, 0xd5, 0xde, 0xf5, 0xa3, 0x36, 0x77, 0x90, 0x97, 0x99, 0xd7,
  36628. 0x6c, 0xb7, 0x5c, 0x9d, 0xf7, 0x7e, 0x41, 0x89, 0xfe, 0xe4, 0x08, 0xc6,
  36629. 0x0b, 0xe4, 0x9b, 0x5f, 0x51, 0xa6, 0x08, 0xb8, 0x99, 0x81, 0xe9, 0xce,
  36630. 0xb4, 0x2d, 0xb2, 0x92, 0x9f, 0xe5, 0x1a, 0x98, 0x76, 0x20, 0x70, 0x54,
  36631. 0x93, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x18,
  36632. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x11, 0x30, 0x0f, 0x82, 0x0d, 0x2a,
  36633. 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
  36634. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x60,
  36635. 0xd4, 0x26, 0xbb, 0xcc, 0x7c, 0x29, 0xa2, 0x88, 0x3c, 0x76, 0x7d, 0xb4,
  36636. 0x86, 0x8b, 0x47, 0x64, 0x5b, 0x87, 0xe0, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  36637. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  36638. 0x01, 0x01, 0x00, 0xc3, 0x0d, 0x03, 0x67, 0xbb, 0x47, 0x8b, 0xf3, 0x20,
  36639. 0xdc, 0x7d, 0x2e, 0xe1, 0xd9, 0xf0, 0x01, 0xc4, 0x66, 0xc2, 0xe1, 0xcd,
  36640. 0xc3, 0x4a, 0x72, 0xf0, 0x6e, 0x38, 0xcf, 0x63, 0x01, 0x96, 0x9e, 0x84,
  36641. 0xb9, 0xce, 0x1d, 0xba, 0x4b, 0xe0, 0x70, 0x86, 0x2b, 0x5a, 0xab, 0xec,
  36642. 0xbf, 0xc2, 0xaa, 0x64, 0xa2, 0x6c, 0xd2, 0x42, 0x52, 0xd4, 0xbe, 0x8a,
  36643. 0xca, 0x9c, 0x03, 0xf3, 0xd6, 0x5f, 0xcd, 0x23, 0x9f, 0xf5, 0xa9, 0x04,
  36644. 0x40, 0x5b, 0x66, 0x78, 0xc0, 0xac, 0xa1, 0xdb, 0x5d, 0xd1, 0x94, 0xfc,
  36645. 0x47, 0x94, 0xf5, 0x45, 0xe3, 0x70, 0x13, 0x3f, 0x66, 0x6d, 0xdd, 0x73,
  36646. 0x68, 0x68, 0xe2, 0xd2, 0x89, 0xcb, 0x7f, 0xc6, 0xca, 0xd6, 0x96, 0x0b,
  36647. 0xcc, 0xdd, 0xa1, 0x74, 0xda, 0x33, 0xe8, 0x9e, 0xda, 0xb7, 0xd9, 0x12,
  36648. 0xab, 0x85, 0x9d, 0x0c, 0xde, 0xa0, 0x7d, 0x7e, 0xa1, 0x91, 0xed, 0xe5,
  36649. 0x32, 0x7c, 0xc5, 0xea, 0x1d, 0x4a, 0xb5, 0x38, 0x63, 0x17, 0xf3, 0x4f,
  36650. 0x2c, 0x4a, 0x58, 0x86, 0x09, 0x33, 0x86, 0xc4, 0xe7, 0x56, 0x6f, 0x32,
  36651. 0x71, 0xb7, 0xd0, 0x83, 0x12, 0x9e, 0x26, 0x0a, 0x3a, 0x45, 0xcb, 0xd7,
  36652. 0x4e, 0xab, 0xa4, 0xc3, 0xee, 0x4c, 0xc0, 0x38, 0xa1, 0xfa, 0xba, 0xfa,
  36653. 0xb7, 0x80, 0x69, 0x67, 0xa3, 0xef, 0x89, 0xba, 0xce, 0x89, 0x91, 0x3d,
  36654. 0x6a, 0x76, 0xe9, 0x3b, 0x32, 0x86, 0x76, 0x85, 0x6b, 0x4f, 0x7f, 0xbc,
  36655. 0x7a, 0x5b, 0x31, 0x92, 0x79, 0x35, 0xf8, 0xb9, 0xb1, 0xd7, 0xdb, 0xa9,
  36656. 0x6a, 0x8a, 0x91, 0x60, 0x65, 0xd4, 0x76, 0x54, 0x55, 0x57, 0xb9, 0x35,
  36657. 0xe0, 0xf5, 0xbb, 0x8f, 0xd4, 0x40, 0x75, 0xbb, 0x47, 0xa8, 0xf9, 0x0f,
  36658. 0xea, 0xc9, 0x6e, 0x84, 0xd5, 0xf5, 0x58, 0x2d, 0xe5, 0x76, 0x7b, 0xdf,
  36659. 0x97, 0x05, 0x5e, 0xaf, 0x50, 0xf5, 0x48
  36660. };
  36661. WOLFSSL_X509* x509 = NULL;
  36662. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  36663. const char *name1 = "foo.example.com";
  36664. int nameLen1 = (int)(XSTRLEN(name1));
  36665. const char *name2 = "x.y.example.com";
  36666. int nameLen2 = (int)(XSTRLEN(name2));
  36667. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36668. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  36669. /* Ensure that "*.example.com" matches "foo.example.com" */
  36670. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  36671. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36672. /* Ensure that "*.example.com" does NOT match "x.y.example.com" */
  36673. ExpectIntNE(wolfSSL_X509_check_host(x509, name2, nameLen2,
  36674. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36675. wolfSSL_X509_free(x509);
  36676. #endif
  36677. return EXPECT_RESULT();
  36678. }
  36679. static int test_wolfSSL_X509_max_altnames(void)
  36680. {
  36681. EXPECT_DECLS;
  36682. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  36683. /* Only test if max alt names has not been modified */
  36684. #if WOLFSSL_MAX_ALT_NAMES == 128
  36685. WOLFSSL_CTX* ctx = NULL;
  36686. /* File contains a certificate encoded with 130 subject alternative names */
  36687. const char* over_max_altnames_cert = \
  36688. "./certs/test/cert-over-max-altnames.pem";
  36689. #ifndef NO_WOLFSSL_SERVER
  36690. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36691. #else
  36692. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36693. #endif
  36694. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx,
  36695. over_max_altnames_cert, NULL, WOLFSSL_LOAD_FLAG_NONE),
  36696. WOLFSSL_SUCCESS);
  36697. wolfSSL_CTX_free(ctx);
  36698. #endif
  36699. #endif
  36700. return EXPECT_RESULT();
  36701. }
  36702. static int test_wolfSSL_X509_max_name_constraints(void)
  36703. {
  36704. EXPECT_DECLS;
  36705. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  36706. !defined(IGNORE_NAME_CONSTRAINTS)
  36707. /* Only test if max name constraints has not been modified */
  36708. #if WOLFSSL_MAX_NAME_CONSTRAINTS == 128
  36709. WOLFSSL_CTX* ctx = NULL;
  36710. /* File contains a certificate with 130 name constraints */
  36711. const char* over_max_nc = "./certs/test/cert-over-max-nc.pem";
  36712. #ifndef NO_WOLFSSL_SERVER
  36713. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36714. #else
  36715. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36716. #endif
  36717. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, over_max_nc,
  36718. NULL, WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  36719. wolfSSL_CTX_free(ctx);
  36720. #endif
  36721. #endif
  36722. return EXPECT_RESULT();
  36723. }
  36724. static int test_wolfSSL_X509(void)
  36725. {
  36726. EXPECT_DECLS;
  36727. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36728. !defined(NO_RSA)
  36729. X509* x509 = NULL;
  36730. #ifndef NO_BIO
  36731. BIO* bio = NULL;
  36732. X509_STORE_CTX* ctx = NULL;
  36733. X509_STORE* store = NULL;
  36734. #endif
  36735. char der[] = "certs/ca-cert.der";
  36736. XFILE fp = XBADFILE;
  36737. ExpectNotNull(x509 = X509_new());
  36738. X509_free(x509);
  36739. x509 = NULL;
  36740. #ifndef NO_BIO
  36741. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36742. SSL_FILETYPE_PEM));
  36743. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36744. #ifdef WOLFSSL_CERT_GEN
  36745. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  36746. #endif
  36747. ExpectNotNull(ctx = X509_STORE_CTX_new());
  36748. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  36749. ExpectNotNull(store = X509_STORE_new());
  36750. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  36751. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  36752. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  36753. X509_STORE_CTX_free(ctx);
  36754. X509_STORE_free(store);
  36755. X509_free(x509);
  36756. x509 = NULL;
  36757. BIO_free(bio);
  36758. #endif
  36759. /** d2i_X509_fp test **/
  36760. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36761. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  36762. ExpectNotNull(x509);
  36763. X509_free(x509);
  36764. x509 = NULL;
  36765. if (fp != XBADFILE) {
  36766. XFCLOSE(fp);
  36767. fp = XBADFILE;
  36768. }
  36769. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36770. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  36771. ExpectNotNull(x509);
  36772. X509_free(x509);
  36773. if (fp != XBADFILE)
  36774. XFCLOSE(fp);
  36775. /* X509_up_ref test */
  36776. ExpectIntEQ(X509_up_ref(NULL), 0);
  36777. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  36778. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  36779. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  36780. X509_free(x509); /* refCount = 2 */
  36781. X509_free(x509); /* refCount = 1 */
  36782. X509_free(x509); /* refCount = 0, free */
  36783. #endif
  36784. return EXPECT_RESULT();
  36785. }
  36786. static int test_wolfSSL_X509_get_ext_count(void)
  36787. {
  36788. EXPECT_DECLS;
  36789. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36790. !defined(NO_RSA)
  36791. int ret = 0;
  36792. WOLFSSL_X509* x509 = NULL;
  36793. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36794. XFILE f = XBADFILE;
  36795. /* NULL parameter check */
  36796. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36797. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36798. SSL_FILETYPE_PEM));
  36799. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36800. wolfSSL_X509_free(x509);
  36801. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36802. SSL_FILETYPE_PEM));
  36803. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36804. wolfSSL_X509_free(x509);
  36805. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  36806. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36807. if (f != XBADFILE)
  36808. XFCLOSE(f);
  36809. /* wolfSSL_X509_get_ext_count() valid input */
  36810. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36811. /* wolfSSL_X509_get_ext_count() NULL argument */
  36812. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36813. wolfSSL_X509_free(x509);
  36814. #endif
  36815. return EXPECT_RESULT();
  36816. }
  36817. static int test_wolfSSL_X509_sign2(void)
  36818. {
  36819. EXPECT_DECLS;
  36820. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36821. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36822. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36823. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36824. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  36825. defined(WOLFSSL_IP_ALT_NAME))
  36826. WOLFSSL_X509 *x509 = NULL;
  36827. WOLFSSL_X509 *ca = NULL;
  36828. const unsigned char *der = NULL;
  36829. const unsigned char *pt = NULL;
  36830. WOLFSSL_EVP_PKEY *priv = NULL;
  36831. WOLFSSL_X509_NAME *name = NULL;
  36832. int derSz;
  36833. #ifndef NO_ASN_TIME
  36834. WOLFSSL_ASN1_TIME *notBefore = NULL;
  36835. WOLFSSL_ASN1_TIME *notAfter = NULL;
  36836. const int year = 365*24*60*60;
  36837. const int day = 24*60*60;
  36838. const int hour = 60*60;
  36839. const int mini = 60;
  36840. time_t t;
  36841. #endif
  36842. const unsigned char expected[] = {
  36843. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36844. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  36845. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  36846. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36847. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36848. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36849. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36850. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36851. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36852. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36853. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36854. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36855. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36856. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36857. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36858. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36859. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36860. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36861. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36862. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36863. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36864. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36865. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36866. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36867. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36868. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36869. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36870. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36871. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36872. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36873. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36874. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36875. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36876. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36877. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36878. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36879. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36880. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36881. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36882. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36883. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36884. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36885. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36886. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36887. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36888. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36889. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36890. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36891. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36892. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36893. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36894. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36895. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36896. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36897. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36898. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36899. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36900. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36901. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36902. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36903. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36904. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36905. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36906. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36907. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36908. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36909. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36910. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36911. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36912. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36913. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36914. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36915. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36916. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36917. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36918. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36919. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36920. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36921. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36922. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36923. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36924. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  36925. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  36926. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36927. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36928. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36929. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36930. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  36931. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  36932. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  36933. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  36934. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  36935. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  36936. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  36937. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  36938. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  36939. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  36940. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  36941. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  36942. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  36943. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  36944. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  36945. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  36946. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  36947. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  36948. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  36949. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  36950. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  36951. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  36952. };
  36953. pt = ca_key_der_2048;
  36954. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36955. sizeof_ca_key_der_2048));
  36956. pt = client_cert_der_2048;
  36957. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36958. sizeof_client_cert_der_2048));
  36959. pt = ca_cert_der_2048;
  36960. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36961. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36962. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36963. #ifndef NO_ASN_TIME
  36964. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36965. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36966. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36967. ExpectIntEQ(notAfter->length, 13);
  36968. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36969. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36970. #endif
  36971. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  36972. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36973. ExpectIntEQ(derSz, sizeof(expected));
  36974. #ifndef NO_ASN_TIME
  36975. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  36976. #endif
  36977. wolfSSL_X509_free(ca);
  36978. wolfSSL_X509_free(x509);
  36979. wolfSSL_EVP_PKEY_free(priv);
  36980. #ifndef NO_ASN_TIME
  36981. wolfSSL_ASN1_TIME_free(notBefore);
  36982. wolfSSL_ASN1_TIME_free(notAfter);
  36983. #endif
  36984. #endif
  36985. return EXPECT_RESULT();
  36986. }
  36987. static int test_wolfSSL_X509_sign(void)
  36988. {
  36989. EXPECT_DECLS;
  36990. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36991. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36992. int ret = 0;
  36993. char *cn = NULL;
  36994. word32 cnSz = 0;
  36995. X509_NAME *name = NULL;
  36996. X509 *x509 = NULL;
  36997. X509 *ca = NULL;
  36998. DecodedCert dCert;
  36999. EVP_PKEY *pub = NULL;
  37000. EVP_PKEY *priv = NULL;
  37001. EVP_MD_CTX *mctx = NULL;
  37002. #if defined(USE_CERT_BUFFERS_1024)
  37003. const unsigned char* rsaPriv = client_key_der_1024;
  37004. const unsigned char* rsaPub = client_keypub_der_1024;
  37005. const unsigned char* certIssuer = client_cert_der_1024;
  37006. long clientKeySz = (long)sizeof_client_key_der_1024;
  37007. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  37008. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  37009. #elif defined(USE_CERT_BUFFERS_2048)
  37010. const unsigned char* rsaPriv = client_key_der_2048;
  37011. const unsigned char* rsaPub = client_keypub_der_2048;
  37012. const unsigned char* certIssuer = client_cert_der_2048;
  37013. long clientKeySz = (long)sizeof_client_key_der_2048;
  37014. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  37015. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  37016. #endif
  37017. byte sn[16];
  37018. int snSz = sizeof(sn);
  37019. /* Set X509_NAME fields */
  37020. ExpectNotNull(name = X509_NAME_new());
  37021. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  37022. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  37023. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  37024. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  37025. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  37026. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  37027. /* Get private and public keys */
  37028. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  37029. clientKeySz));
  37030. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  37031. ExpectNotNull(x509 = X509_new());
  37032. /* Set version 3 */
  37033. ExpectIntNE(X509_set_version(x509, 2L), 0);
  37034. /* Set subject name, add pubkey, and sign certificate */
  37035. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  37036. X509_NAME_free(name);
  37037. name = NULL;
  37038. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  37039. #ifdef WOLFSSL_ALT_NAMES
  37040. /* Add some subject alt names */
  37041. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  37042. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  37043. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37044. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  37045. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37046. "sphygmomanometer",
  37047. ASN_DNS_TYPE), SSL_SUCCESS);
  37048. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37049. "supercalifragilisticexpialidocious",
  37050. ASN_DNS_TYPE), SSL_SUCCESS);
  37051. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37052. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  37053. ASN_DNS_TYPE), SSL_SUCCESS);
  37054. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37055. {
  37056. unsigned char ip4_type[] = {127,128,0,255};
  37057. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  37058. 0xff, 0xee, 0x99, 0x88,
  37059. 0x77, 0x66, 0x55, 0x44,
  37060. 0x00, 0x33, 0x22, 0x11};
  37061. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  37062. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  37063. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  37064. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  37065. }
  37066. #endif
  37067. #endif /* WOLFSSL_ALT_NAMES */
  37068. {
  37069. ASN1_UTCTIME* infinite_past = NULL;
  37070. ExpectNotNull(infinite_past = ASN1_UTCTIME_set(NULL, 0));
  37071. ExpectIntEQ(X509_set1_notBefore(x509, infinite_past), 1);
  37072. ASN1_UTCTIME_free(infinite_past);
  37073. }
  37074. /* test valid sign case */
  37075. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  37076. /* test valid X509_sign_ctx case */
  37077. ExpectNotNull(mctx = EVP_MD_CTX_new());
  37078. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  37079. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  37080. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  37081. ExpectIntEQ(X509_get_ext_count(x509), 1);
  37082. #endif
  37083. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  37084. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  37085. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  37086. #endif
  37087. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  37088. WOLFSSL_SUCCESS);
  37089. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  37090. /* Variation in size depends on ASN.1 encoding when MSB is set.
  37091. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  37092. * with the MSB set. See GenerateInteger in asn.c */
  37093. #ifndef USE_CERT_BUFFERS_1024
  37094. #ifndef WOLFSSL_ALT_NAMES
  37095. /* Valid case - size should be 781-786 with 16 byte serial number */
  37096. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  37097. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37098. /* Valid case - size should be 955-960 with 16 byte serial number */
  37099. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  37100. #else
  37101. /* Valid case - size should be 926-931 with 16 byte serial number */
  37102. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  37103. #endif
  37104. #else
  37105. #ifndef WOLFSSL_ALT_NAMES
  37106. /* Valid case - size should be 537-542 with 16 byte serial number */
  37107. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  37108. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37109. /* Valid case - size should be 695-670 with 16 byte serial number */
  37110. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  37111. #else
  37112. /* Valid case - size should be 666-671 with 16 byte serial number */
  37113. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  37114. #endif
  37115. #endif
  37116. /* check that issuer name is as expected after signature */
  37117. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  37118. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  37119. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  37120. ExpectNotNull(name = X509_get_subject_name(ca));
  37121. cnSz = X509_NAME_get_sz(name);
  37122. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  37123. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  37124. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  37125. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  37126. cn = NULL;
  37127. #ifdef WOLFSSL_MULTI_ATTRIB
  37128. /* test adding multiple OU's to the signer */
  37129. ExpectNotNull(name = X509_get_subject_name(ca));
  37130. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  37131. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  37132. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  37133. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  37134. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  37135. #endif
  37136. ExpectNotNull(name = X509_get_subject_name(ca));
  37137. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  37138. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  37139. ExpectNotNull(name = X509_get_issuer_name(x509));
  37140. cnSz = X509_NAME_get_sz(name);
  37141. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  37142. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  37143. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  37144. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  37145. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  37146. cn = NULL;
  37147. FreeDecodedCert(&dCert);
  37148. /* Test invalid parameters */
  37149. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  37150. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  37151. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  37152. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  37153. EVP_MD_CTX_free(mctx);
  37154. mctx = NULL;
  37155. ExpectNotNull(mctx = EVP_MD_CTX_new());
  37156. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  37157. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  37158. /* test invalid version number */
  37159. #if defined(OPENSSL_ALL)
  37160. ExpectIntNE(X509_set_version(x509, 6L), 0);
  37161. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  37162. /* uses ParseCert which fails on bad version number */
  37163. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  37164. #endif
  37165. EVP_MD_CTX_free(mctx);
  37166. EVP_PKEY_free(priv);
  37167. EVP_PKEY_free(pub);
  37168. X509_free(x509);
  37169. X509_free(ca);
  37170. #endif
  37171. return EXPECT_RESULT();
  37172. }
  37173. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  37174. {
  37175. EXPECT_DECLS;
  37176. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  37177. X509* x509 = NULL;
  37178. const X509_ALGOR* alg;
  37179. ExpectNotNull(x509 = X509_new());
  37180. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  37181. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  37182. X509_free(x509);
  37183. #endif
  37184. return EXPECT_RESULT();
  37185. }
  37186. static int test_wolfSSL_X509_ALGOR_get0(void)
  37187. {
  37188. EXPECT_DECLS;
  37189. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  37190. !defined(NO_SHA256) && !defined(NO_RSA)
  37191. X509* x509 = NULL;
  37192. const ASN1_OBJECT* obj = NULL;
  37193. const X509_ALGOR* alg = NULL;
  37194. int pptype = 0;
  37195. const void *ppval = NULL;
  37196. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  37197. SSL_FILETYPE_PEM));
  37198. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  37199. /* Invalid case */
  37200. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  37201. ExpectNull(obj);
  37202. /* Valid case */
  37203. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  37204. ExpectNotNull(obj);
  37205. ExpectNull(ppval);
  37206. ExpectIntNE(pptype, 0);
  37207. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  37208. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  37209. X509_free(x509);
  37210. #endif
  37211. return EXPECT_RESULT();
  37212. }
  37213. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  37214. {
  37215. EXPECT_DECLS;
  37216. #if defined(OPENSSL_EXTRA)
  37217. X509_VERIFY_PARAM *paramTo = NULL;
  37218. X509_VERIFY_PARAM *paramFrom = NULL;
  37219. char testIPv4[] = "127.0.0.1";
  37220. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  37221. char testhostName1[] = "foo.hoge.com";
  37222. char testhostName2[] = "foobar.hoge.com";
  37223. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  37224. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  37225. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  37226. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  37227. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  37228. (int)XSTRLEN(testhostName1)), 1);
  37229. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  37230. (int)XSTRLEN(testhostName1)));
  37231. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  37232. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  37233. ExpectIntEQ(0x01, paramFrom->hostFlags);
  37234. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  37235. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  37236. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  37237. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  37238. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  37239. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37240. /* null pointer */
  37241. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  37242. /* in the case of "from" null, returns success */
  37243. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  37244. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  37245. /* inherit flags test : VPARAM_DEFAULT */
  37246. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37247. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37248. (int)XSTRLEN(testhostName1)));
  37249. ExpectIntEQ(0x01, paramTo->hostFlags);
  37250. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37251. /* inherit flags test : VPARAM OVERWRITE */
  37252. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37253. (int)XSTRLEN(testhostName2)), 1);
  37254. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37255. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  37256. if (paramTo != NULL) {
  37257. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  37258. }
  37259. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37260. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37261. (int)XSTRLEN(testhostName1)));
  37262. ExpectIntEQ(0x01, paramTo->hostFlags);
  37263. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37264. /* inherit flags test : VPARAM_RESET_FLAGS */
  37265. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37266. (int)XSTRLEN(testhostName2)), 1);
  37267. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37268. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  37269. if (paramTo != NULL) {
  37270. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  37271. }
  37272. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37273. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37274. (int)XSTRLEN(testhostName1)));
  37275. ExpectIntEQ(0x01, paramTo->hostFlags);
  37276. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37277. /* inherit flags test : VPARAM_LOCKED */
  37278. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37279. (int)XSTRLEN(testhostName2)), 1);
  37280. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37281. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  37282. if (paramTo != NULL) {
  37283. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  37284. }
  37285. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37286. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  37287. (int)XSTRLEN(testhostName2)));
  37288. ExpectIntEQ(0x00, paramTo->hostFlags);
  37289. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  37290. /* test for incorrect parameters */
  37291. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  37292. 0);
  37293. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  37294. /* inherit flags test : VPARAM_ONCE, not testable yet */
  37295. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  37296. 1);
  37297. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  37298. X509_V_FLAG_CRL_CHECK_ALL);
  37299. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  37300. X509_V_FLAG_CRL_CHECK_ALL), 1);
  37301. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  37302. X509_VERIFY_PARAM_free(paramTo);
  37303. X509_VERIFY_PARAM_free(paramFrom);
  37304. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  37305. #endif
  37306. return EXPECT_RESULT();
  37307. }
  37308. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  37309. !defined(WOLFSSL_HOSTNAME_VERIFY_ALT_NAME_ONLY)
  37310. static int test_wolfSSL_check_domain_verify_count = 0;
  37311. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  37312. WOLFSSL_X509_STORE_CTX* store)
  37313. {
  37314. EXPECT_DECLS;
  37315. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  37316. ExpectIntEQ(preverify, 1);
  37317. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  37318. return EXPECT_SUCCESS();
  37319. }
  37320. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  37321. {
  37322. EXPECT_DECLS;
  37323. X509_VERIFY_PARAM *param = NULL;
  37324. ExpectNotNull(param = SSL_get0_param(ssl));
  37325. /* Domain check should only be done on the leaf cert */
  37326. X509_VERIFY_PARAM_set_hostflags(param,
  37327. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  37328. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  37329. "wolfSSL Server Chain", 0), 1);
  37330. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  37331. test_wolfSSL_check_domain_verify_cb);
  37332. return EXPECT_RESULT();
  37333. }
  37334. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  37335. {
  37336. EXPECT_DECLS;
  37337. /* Use a cert with different domains in chain */
  37338. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  37339. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  37340. return EXPECT_RESULT();
  37341. }
  37342. static int test_wolfSSL_check_domain(void)
  37343. {
  37344. EXPECT_DECLS;
  37345. test_ssl_cbf func_cb_client;
  37346. test_ssl_cbf func_cb_server;
  37347. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  37348. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  37349. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  37350. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  37351. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  37352. &func_cb_server, NULL), TEST_SUCCESS);
  37353. /* Should have been called once for each cert in sent chain */
  37354. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  37355. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  37356. #else
  37357. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  37358. #endif
  37359. return EXPECT_RESULT();
  37360. }
  37361. #else
  37362. static int test_wolfSSL_check_domain(void)
  37363. {
  37364. EXPECT_DECLS;
  37365. return EXPECT_RESULT();
  37366. }
  37367. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  37368. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  37369. {
  37370. EXPECT_DECLS;
  37371. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  37372. X509* x509 = NULL;
  37373. X509_PUBKEY* pubKey;
  37374. ExpectNotNull(x509 = X509_new());
  37375. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  37376. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  37377. X509_free(x509);
  37378. #endif
  37379. return EXPECT_RESULT();
  37380. }
  37381. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  37382. {
  37383. EXPECT_DECLS;
  37384. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  37385. !defined(NO_SHA256) && !defined(NO_RSA)
  37386. X509* x509 = NULL;
  37387. ASN1_OBJECT* obj = NULL;
  37388. const ASN1_OBJECT* pa_oid = NULL;
  37389. X509_PUBKEY* pubKey = NULL;
  37390. X509_PUBKEY* pubKey2 = NULL;
  37391. EVP_PKEY* evpKey = NULL;
  37392. const unsigned char *pk = NULL;
  37393. int ppklen;
  37394. int pptype;
  37395. X509_ALGOR *pa = NULL;
  37396. const void *pval;
  37397. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  37398. SSL_FILETYPE_PEM));
  37399. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  37400. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  37401. ExpectNotNull(pk);
  37402. ExpectNotNull(pa);
  37403. ExpectNotNull(pubKey);
  37404. ExpectIntGT(ppklen, 0);
  37405. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  37406. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  37407. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  37408. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  37409. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  37410. ExpectNotNull(pk);
  37411. ExpectNotNull(pa);
  37412. ExpectIntGT(ppklen, 0);
  37413. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37414. ExpectNotNull(pa_oid);
  37415. ExpectNull(pval);
  37416. ExpectIntEQ(pptype, V_ASN1_NULL);
  37417. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  37418. X509_PUBKEY_free(pubKey2);
  37419. X509_free(x509);
  37420. EVP_PKEY_free(evpKey);
  37421. #endif
  37422. return EXPECT_RESULT();
  37423. }
  37424. static int test_wolfSSL_X509_PUBKEY_EC(void)
  37425. {
  37426. EXPECT_DECLS;
  37427. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  37428. X509* x509 = NULL;
  37429. ASN1_OBJECT* obj = NULL;
  37430. ASN1_OBJECT* poid = NULL;
  37431. const ASN1_OBJECT* pa_oid = NULL;
  37432. X509_PUBKEY* pubKey = NULL;
  37433. X509_PUBKEY* pubKey2 = NULL;
  37434. EVP_PKEY* evpKey = NULL;
  37435. const unsigned char *pk = NULL;
  37436. int ppklen;
  37437. int pptype;
  37438. X509_ALGOR *pa = NULL;
  37439. const void *pval;
  37440. char buf[50];
  37441. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  37442. SSL_FILETYPE_PEM));
  37443. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  37444. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  37445. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  37446. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  37447. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  37448. ExpectNotNull(pk);
  37449. ExpectNotNull(pa);
  37450. ExpectIntGT(ppklen, 0);
  37451. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37452. ExpectNotNull(pa_oid);
  37453. ExpectNotNull(pval);
  37454. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  37455. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  37456. poid = (ASN1_OBJECT *)pval;
  37457. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  37458. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  37459. X509_PUBKEY_free(pubKey2);
  37460. X509_free(x509);
  37461. EVP_PKEY_free(evpKey);
  37462. #endif
  37463. return EXPECT_RESULT();
  37464. }
  37465. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  37466. {
  37467. EXPECT_DECLS;
  37468. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  37469. word32 bytes;
  37470. #ifdef USE_CERT_BUFFERS_1024
  37471. byte tmp[ONEK_BUF];
  37472. #elif defined(USE_CERT_BUFFERS_2048)
  37473. byte tmp[TWOK_BUF];
  37474. #else
  37475. byte tmp[TWOK_BUF];
  37476. #endif /* END USE_CERT_BUFFERS_1024 */
  37477. const unsigned char* dsaKeyDer = tmp;
  37478. ASN1_OBJECT* obj = NULL;
  37479. ASN1_STRING* str;
  37480. const ASN1_OBJECT* pa_oid = NULL;
  37481. X509_PUBKEY* pubKey = NULL;
  37482. EVP_PKEY* evpKey = NULL;
  37483. const unsigned char *pk = NULL;
  37484. int ppklen, pptype;
  37485. X509_ALGOR *pa = NULL;
  37486. const void *pval;
  37487. #ifdef USE_CERT_BUFFERS_1024
  37488. XMEMSET(tmp, 0, sizeof(tmp));
  37489. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  37490. bytes = sizeof_dsa_key_der_1024;
  37491. #elif defined(USE_CERT_BUFFERS_2048)
  37492. XMEMSET(tmp, 0, sizeof(tmp));
  37493. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  37494. bytes = sizeof_dsa_key_der_2048;
  37495. #else
  37496. {
  37497. XFILE fp = XBADFILE;
  37498. XMEMSET(tmp, 0, sizeof(tmp));
  37499. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  37500. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  37501. if (fp != XBADFILE)
  37502. XFCLOSE(fp);
  37503. }
  37504. #endif
  37505. /* Initialize pkey with der format dsa key */
  37506. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  37507. ExpectNotNull(pubKey = X509_PUBKEY_new());
  37508. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  37509. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  37510. ExpectNotNull(pk);
  37511. ExpectNotNull(pa);
  37512. ExpectIntGT(ppklen, 0);
  37513. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37514. ExpectNotNull(pa_oid);
  37515. ExpectNotNull(pval);
  37516. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  37517. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  37518. str = (ASN1_STRING *)pval;
  37519. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  37520. #ifdef USE_CERT_BUFFERS_1024
  37521. ExpectIntEQ(ASN1_STRING_length(str), 291);
  37522. #else
  37523. ExpectIntEQ(ASN1_STRING_length(str), 549);
  37524. #endif /* END USE_CERT_BUFFERS_1024 */
  37525. X509_PUBKEY_free(pubKey);
  37526. EVP_PKEY_free(evpKey);
  37527. #endif
  37528. return EXPECT_RESULT();
  37529. }
  37530. static int test_wolfSSL_BUF(void)
  37531. {
  37532. EXPECT_DECLS;
  37533. #if defined(OPENSSL_EXTRA)
  37534. BUF_MEM* buf = NULL;
  37535. ExpectNotNull(buf = BUF_MEM_new());
  37536. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  37537. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  37538. BUF_MEM_free(buf);
  37539. #endif
  37540. return EXPECT_RESULT();
  37541. }
  37542. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  37543. static int stub_rand_seed(const void *buf, int num)
  37544. {
  37545. (void)buf;
  37546. (void)num;
  37547. return 123;
  37548. }
  37549. static int stub_rand_bytes(unsigned char *buf, int num)
  37550. {
  37551. (void)buf;
  37552. (void)num;
  37553. return 456;
  37554. }
  37555. static byte* was_stub_rand_cleanup_called(void)
  37556. {
  37557. static byte was_called = 0;
  37558. return &was_called;
  37559. }
  37560. static void stub_rand_cleanup(void)
  37561. {
  37562. byte* was_called = was_stub_rand_cleanup_called();
  37563. *was_called = 1;
  37564. return;
  37565. }
  37566. static byte* was_stub_rand_add_called(void)
  37567. {
  37568. static byte was_called = 0;
  37569. return &was_called;
  37570. }
  37571. static int stub_rand_add(const void *buf, int num, double entropy)
  37572. {
  37573. byte* was_called = was_stub_rand_add_called();
  37574. (void)buf;
  37575. (void)num;
  37576. (void)entropy;
  37577. *was_called = 1;
  37578. return 0;
  37579. }
  37580. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  37581. {
  37582. (void)buf;
  37583. (void)num;
  37584. return 9876;
  37585. }
  37586. static int stub_rand_status(void)
  37587. {
  37588. return 5432;
  37589. }
  37590. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37591. static int test_wolfSSL_RAND_set_rand_method(void)
  37592. {
  37593. EXPECT_DECLS;
  37594. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  37595. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  37596. unsigned char* buf = NULL;
  37597. int num = 0;
  37598. double entropy = 0;
  37599. int ret;
  37600. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  37601. byte* was_add_called = was_stub_rand_add_called();
  37602. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  37603. DYNAMIC_TYPE_TMP_BUFFER));
  37604. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  37605. ExpectIntEQ(*was_cleanup_called, 0);
  37606. RAND_cleanup();
  37607. ExpectIntEQ(*was_cleanup_called, 0);
  37608. rand_methods.seed = &stub_rand_seed;
  37609. rand_methods.bytes = &stub_rand_bytes;
  37610. rand_methods.cleanup = &stub_rand_cleanup;
  37611. rand_methods.add = &stub_rand_add;
  37612. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  37613. rand_methods.status = &stub_rand_status;
  37614. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  37615. ExpectIntEQ(RAND_seed(buf, num), 123);
  37616. ExpectIntEQ(RAND_bytes(buf, num), 456);
  37617. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  37618. ExpectIntEQ(RAND_status(), 5432);
  37619. ExpectIntEQ(*was_add_called, 0);
  37620. /* The function pointer for RAND_add returns int, but RAND_add itself
  37621. * returns void. */
  37622. RAND_add(buf, num, entropy);
  37623. ExpectIntEQ(*was_add_called, 1);
  37624. was_add_called = 0;
  37625. ExpectIntEQ(*was_cleanup_called, 0);
  37626. RAND_cleanup();
  37627. ExpectIntEQ(*was_cleanup_called, 1);
  37628. *was_cleanup_called = 0;
  37629. ret = RAND_set_rand_method(NULL);
  37630. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  37631. ExpectIntNE(RAND_status(), 5432);
  37632. ExpectIntEQ(*was_cleanup_called, 0);
  37633. RAND_cleanup();
  37634. ExpectIntEQ(*was_cleanup_called, 0);
  37635. RAND_set_rand_method(NULL);
  37636. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37637. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37638. return EXPECT_RESULT();
  37639. }
  37640. static int test_wolfSSL_RAND_bytes(void)
  37641. {
  37642. EXPECT_DECLS;
  37643. #if defined(OPENSSL_EXTRA)
  37644. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  37645. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  37646. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  37647. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  37648. int max_bufsize;
  37649. byte *my_buf = NULL;
  37650. /* sanity check */
  37651. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  37652. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  37653. max_bufsize = size4;
  37654. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  37655. DYNAMIC_TYPE_TMP_BUFFER));
  37656. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  37657. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  37658. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  37659. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  37660. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  37661. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  37662. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  37663. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37664. #endif
  37665. return EXPECT_RESULT();
  37666. }
  37667. static int test_wolfSSL_RAND(void)
  37668. {
  37669. EXPECT_DECLS;
  37670. #if defined(OPENSSL_EXTRA)
  37671. byte seed[16];
  37672. XMEMSET(seed, 0, sizeof(seed));
  37673. /* No global methods set. */
  37674. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  37675. ExpectIntEQ(RAND_poll(), 1);
  37676. RAND_cleanup();
  37677. ExpectIntEQ(RAND_egd(NULL), -1);
  37678. #ifndef NO_FILESYSTEM
  37679. {
  37680. char fname[100];
  37681. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  37682. ExpectIntEQ(RAND_write_file(NULL), 0);
  37683. }
  37684. #endif
  37685. #endif
  37686. return EXPECT_RESULT();
  37687. }
  37688. static int test_wolfSSL_PKCS8_Compat(void)
  37689. {
  37690. EXPECT_DECLS;
  37691. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  37692. !defined(NO_BIO)
  37693. PKCS8_PRIV_KEY_INFO* pt = NULL;
  37694. BIO* bio = NULL;
  37695. XFILE f = XBADFILE;
  37696. int bytes;
  37697. char pkcs8_buffer[512];
  37698. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37699. EVP_PKEY *pkey = NULL;
  37700. #endif
  37701. /* file from wolfssl/certs/ directory */
  37702. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  37703. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  37704. 0);
  37705. if (f != XBADFILE)
  37706. XFCLOSE(f);
  37707. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37708. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  37709. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37710. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  37711. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  37712. /* gets PKCS8 pointer to pkey */
  37713. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  37714. EVP_PKEY_free(pkey);
  37715. #endif
  37716. BIO_free(bio);
  37717. PKCS8_PRIV_KEY_INFO_free(pt);
  37718. #endif
  37719. return EXPECT_RESULT();
  37720. }
  37721. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  37722. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  37723. {
  37724. (void)passwd;
  37725. (void)sz;
  37726. (void)rw;
  37727. (void)userdata;
  37728. return -1;
  37729. }
  37730. #endif
  37731. static int test_wolfSSL_PKCS8_d2i(void)
  37732. {
  37733. EXPECT_DECLS;
  37734. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  37735. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  37736. * requires a 12 byte password in FIPS mode. This test ends up
  37737. * trying to use an 8 byte password. */
  37738. #ifndef NO_FILESYSTEM
  37739. unsigned char pkcs8_buffer[2048];
  37740. const unsigned char* p = NULL;
  37741. int bytes = 0;
  37742. XFILE file = XBADFILE;
  37743. WOLFSSL_EVP_PKEY* pkey = NULL;
  37744. #ifndef NO_BIO
  37745. BIO* bio = NULL;
  37746. #if defined(OPENSSL_ALL) && \
  37747. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  37748. defined(HAVE_ECC)) && \
  37749. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37750. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  37751. #endif
  37752. #endif
  37753. #ifndef NO_RSA
  37754. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  37755. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  37756. #ifndef NO_DES3
  37757. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  37758. #endif
  37759. #endif /* NO_RSA */
  37760. #ifdef HAVE_ECC
  37761. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  37762. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  37763. #ifndef NO_DES3
  37764. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  37765. #endif
  37766. #endif /* HAVE_ECC */
  37767. #endif /* !NO_FILESYSTEM */
  37768. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  37769. #ifndef NO_RSA
  37770. #ifdef USE_CERT_BUFFERS_1024
  37771. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  37772. int rsaSz = sizeof_server_key_der_1024;
  37773. #else
  37774. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  37775. int rsaSz = sizeof_server_key_der_2048;
  37776. #endif
  37777. #endif
  37778. #ifdef HAVE_ECC
  37779. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  37780. int ecSz = sizeof_ecc_key_der_256;
  37781. #endif
  37782. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  37783. #ifndef NO_FILESYSTEM
  37784. (void)pkcs8_buffer;
  37785. (void)p;
  37786. (void)bytes;
  37787. (void)file;
  37788. #ifndef NO_BIO
  37789. (void)bio;
  37790. #endif
  37791. #endif
  37792. #ifdef OPENSSL_ALL
  37793. #ifndef NO_RSA
  37794. /* Try to auto-detect normal RSA private key */
  37795. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  37796. EVP_PKEY_free(pkey);
  37797. pkey = NULL;
  37798. #endif
  37799. #ifdef HAVE_ECC
  37800. /* Try to auto-detect normal EC private key */
  37801. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  37802. EVP_PKEY_free(pkey);
  37803. pkey = NULL;
  37804. #endif
  37805. #endif /* OPENSSL_ALL */
  37806. #ifndef NO_FILESYSTEM
  37807. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37808. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  37809. NULL), 0);
  37810. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  37811. NULL), 0);
  37812. #endif
  37813. #ifndef NO_RSA
  37814. /* Get DER encoded RSA PKCS#8 data. */
  37815. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  37816. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37817. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37818. file)), 0);
  37819. if (file != XBADFILE) {
  37820. XFCLOSE(file);
  37821. file = XBADFILE;
  37822. }
  37823. p = pkcs8_buffer;
  37824. #ifdef OPENSSL_ALL
  37825. /* Try to decode - auto-detect key type. */
  37826. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37827. #else
  37828. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37829. #endif
  37830. /* Get PEM encoded RSA PKCS#8 data. */
  37831. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  37832. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37833. file)), 0);
  37834. if (file != XBADFILE) {
  37835. XFCLOSE(file);
  37836. file = XBADFILE;
  37837. }
  37838. #if defined(OPENSSL_ALL) && \
  37839. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37840. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37841. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  37842. NULL), 0);
  37843. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  37844. NULL), 0);
  37845. /* Write PKCS#8 PEM to BIO. */
  37846. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37847. NULL), bytes);
  37848. /* Write PKCS#8 PEM to stderr. */
  37849. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37850. NULL), bytes);
  37851. /* Compare file and written data */
  37852. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37853. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37854. BIO_free(bio);
  37855. bio = NULL;
  37856. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37857. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  37858. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  37859. #endif
  37860. #if !defined(NO_DES3) && !defined(NO_SHA)
  37861. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37862. /* Write Encrypted PKCS#8 PEM to BIO. */
  37863. bytes = 1834;
  37864. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37865. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37866. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  37867. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37868. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37869. (void*)"yassl123"));
  37870. EVP_PKEY_free(evpPkey);
  37871. evpPkey = NULL;
  37872. BIO_free(bio);
  37873. bio = NULL;
  37874. #endif /* !NO_DES3 && !NO_SHA */
  37875. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37876. EVP_PKEY_free(pkey);
  37877. pkey = NULL;
  37878. /* PKCS#8 encrypted RSA key */
  37879. #ifndef NO_DES3
  37880. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  37881. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37882. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37883. file)), 0);
  37884. if (file != XBADFILE) {
  37885. XFCLOSE(file);
  37886. file = XBADFILE;
  37887. }
  37888. #if defined(OPENSSL_ALL) && \
  37889. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37890. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37891. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37892. (void*)"yassl123"));
  37893. EVP_PKEY_free(pkey);
  37894. pkey = NULL;
  37895. BIO_free(bio);
  37896. bio = NULL;
  37897. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37898. #endif /* !NO_DES3 */
  37899. #endif /* NO_RSA */
  37900. #ifdef HAVE_ECC
  37901. /* PKCS#8 encode EC key */
  37902. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  37903. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37904. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37905. file)), 0);
  37906. if (file != XBADFILE) {
  37907. XFCLOSE(file);
  37908. file = XBADFILE;
  37909. }
  37910. p = pkcs8_buffer;
  37911. #ifdef OPENSSL_ALL
  37912. /* Try to decode - auto-detect key type. */
  37913. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37914. #else
  37915. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37916. #endif
  37917. /* Get PEM encoded RSA PKCS#8 data. */
  37918. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  37919. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37920. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37921. file)), 0);
  37922. if (file != XBADFILE) {
  37923. XFCLOSE(file);
  37924. file = XBADFILE;
  37925. }
  37926. #if defined(OPENSSL_ALL) && \
  37927. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37928. defined(HAVE_AES_CBC)
  37929. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37930. /* Write PKCS#8 PEM to BIO. */
  37931. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37932. NULL), bytes);
  37933. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37934. NULL), bytes);
  37935. /* Compare file and written data */
  37936. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37937. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37938. BIO_free(bio);
  37939. bio = NULL;
  37940. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37941. /* Write Encrypted PKCS#8 PEM to BIO. */
  37942. bytes = 379;
  37943. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37944. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  37945. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37946. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37947. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37948. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37949. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37950. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  37951. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  37952. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37953. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37954. (void*)"yassl123"));
  37955. EVP_PKEY_free(evpPkey);
  37956. evpPkey = NULL;
  37957. BIO_free(bio);
  37958. bio = NULL;
  37959. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37960. EVP_PKEY_free(pkey);
  37961. pkey = NULL;
  37962. /* PKCS#8 encrypted EC key */
  37963. #ifndef NO_DES3
  37964. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  37965. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37966. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37967. file)), 0);
  37968. if (file != XBADFILE) {
  37969. XFCLOSE(file);
  37970. file = XBADFILE;
  37971. }
  37972. #if defined(OPENSSL_ALL) && \
  37973. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37974. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37975. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37976. (void*)"yassl123"));
  37977. EVP_PKEY_free(pkey);
  37978. pkey = NULL;
  37979. BIO_free(bio);
  37980. bio = NULL;
  37981. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37982. #endif /* !NO_DES3 */
  37983. #endif /* HAVE_ECC */
  37984. #endif /* !NO_FILESYSTEM */
  37985. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37986. return EXPECT_RESULT();
  37987. }
  37988. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37989. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37990. #define LOGGING_THREADS 5
  37991. #define ERROR_COUNT 10
  37992. /* copied from logging.c since this is not exposed otherwise */
  37993. #ifndef ERROR_QUEUE_MAX
  37994. #ifdef ERROR_QUEUE_PER_THREAD
  37995. #define ERROR_QUEUE_MAX 16
  37996. #else
  37997. /* this breaks from compat of unlimited error queue size */
  37998. #define ERROR_QUEUE_MAX 100
  37999. #endif
  38000. #endif
  38001. static volatile int loggingThreadsReady;
  38002. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  38003. {
  38004. const char* file;
  38005. int line;
  38006. unsigned long err;
  38007. int errorCount = 0;
  38008. int i;
  38009. (void)args;
  38010. while (!loggingThreadsReady);
  38011. for (i = 0; i < ERROR_COUNT; i++)
  38012. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  38013. while ((err = ERR_get_error_line(&file, &line))) {
  38014. AssertIntEQ(err, 990 + errorCount);
  38015. errorCount++;
  38016. }
  38017. AssertIntEQ(errorCount, ERROR_COUNT);
  38018. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  38019. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  38020. errorCount = 0;
  38021. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  38022. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  38023. while ((err = ERR_get_error_line(&file, &line))) {
  38024. AssertIntEQ(err, 990 + errorCount);
  38025. errorCount++;
  38026. }
  38027. /* test that the 3 errors over the max were dropped */
  38028. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  38029. WOLFSSL_RETURN_FROM_THREAD(0);
  38030. }
  38031. #endif
  38032. static int test_error_queue_per_thread(void)
  38033. {
  38034. int res = TEST_SKIPPED;
  38035. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  38036. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  38037. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  38038. int i;
  38039. ERR_clear_error(); /* clear out any error nodes */
  38040. loggingThreadsReady = 0;
  38041. for (i = 0; i < LOGGING_THREADS; i++)
  38042. start_thread(test_logging, NULL, &loggingThreads[i]);
  38043. loggingThreadsReady = 1;
  38044. for (i = 0; i < LOGGING_THREADS; i++)
  38045. join_thread(loggingThreads[i]);
  38046. res = TEST_SUCCESS;
  38047. #endif
  38048. return res;
  38049. }
  38050. static int test_wolfSSL_ERR_put_error(void)
  38051. {
  38052. EXPECT_DECLS;
  38053. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38054. defined(DEBUG_WOLFSSL)
  38055. const char* file;
  38056. int line;
  38057. ERR_clear_error(); /* clear out any error nodes */
  38058. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  38059. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38060. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  38061. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  38062. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  38063. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  38064. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  38065. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  38066. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  38067. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  38068. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  38069. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  38070. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  38071. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  38072. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  38073. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  38074. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  38075. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  38076. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  38077. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  38078. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  38079. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  38080. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  38081. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  38082. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  38083. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  38084. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  38085. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  38086. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  38087. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  38088. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  38089. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  38090. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  38091. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  38092. "this file", 100);
  38093. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  38094. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  38095. ExpectIntEQ(line, 100);
  38096. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  38097. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  38098. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  38099. #endif
  38100. /* try reading past end of error queue */
  38101. file = NULL;
  38102. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38103. ExpectNull(file);
  38104. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  38105. PEMerr(4,4);
  38106. ExpectIntEQ(ERR_get_error(), 4);
  38107. /* Empty and free up all error nodes */
  38108. ERR_clear_error();
  38109. /* Verify all nodes are cleared */
  38110. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  38111. ERR_clear_error();
  38112. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38113. #endif
  38114. return EXPECT_RESULT();
  38115. }
  38116. /*
  38117. * This is a regression test for a bug where the peek/get error functions were
  38118. * drawing from the end of the queue rather than the front.
  38119. */
  38120. static int test_wolfSSL_ERR_get_error_order(void)
  38121. {
  38122. EXPECT_DECLS;
  38123. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  38124. /* Empty the queue. */
  38125. wolfSSL_ERR_clear_error();
  38126. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  38127. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  38128. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  38129. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  38130. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  38131. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  38132. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  38133. return EXPECT_RESULT();
  38134. }
  38135. #ifndef NO_BIO
  38136. static int test_wolfSSL_ERR_print_errors(void)
  38137. {
  38138. EXPECT_DECLS;
  38139. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38140. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  38141. BIO* bio = NULL;
  38142. char buf[1024];
  38143. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38144. ERR_clear_error(); /* clear out any error nodes */
  38145. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  38146. /* Choosing -600 as an unused errno. */
  38147. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  38148. ERR_print_errors(bio);
  38149. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  38150. ExpectIntEQ(XSTRNCMP(
  38151. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  38152. buf, 55), 0);
  38153. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  38154. ExpectIntEQ(XSTRNCMP(
  38155. "error:600:wolfSSL library:unknown error number:asn.c:100",
  38156. buf, 56), 0);
  38157. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  38158. ExpectIntEQ(buf[0], '\0');
  38159. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  38160. BIO_free(bio);
  38161. #endif
  38162. return EXPECT_RESULT();
  38163. }
  38164. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38165. defined(DEBUG_WOLFSSL)
  38166. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  38167. {
  38168. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  38169. return 0;
  38170. }
  38171. #endif
  38172. static int test_wolfSSL_ERR_print_errors_cb(void)
  38173. {
  38174. EXPECT_DECLS;
  38175. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38176. defined(DEBUG_WOLFSSL)
  38177. BIO* bio = NULL;
  38178. char buf[1024];
  38179. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38180. ERR_clear_error(); /* clear out any error nodes */
  38181. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  38182. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  38183. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  38184. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  38185. ExpectIntEQ(XSTRNCMP(
  38186. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  38187. buf, 53), 0);
  38188. ExpectIntEQ(XSTRNCMP(
  38189. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  38190. buf + 53, 55), 0);
  38191. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  38192. BIO_free(bio);
  38193. #endif
  38194. return EXPECT_RESULT();
  38195. }
  38196. /*
  38197. * Testing WOLFSSL_ERROR_MSG
  38198. */
  38199. static int test_WOLFSSL_ERROR_MSG(void)
  38200. {
  38201. int res = TEST_SKIPPED;
  38202. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  38203. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  38204. const char* msg = TEST_STRING;
  38205. WOLFSSL_ERROR_MSG(msg);
  38206. res = TEST_SUCCESS;
  38207. #endif
  38208. return res;
  38209. } /* End test_WOLFSSL_ERROR_MSG */
  38210. /*
  38211. * Testing wc_ERR_remove_state
  38212. */
  38213. static int test_wc_ERR_remove_state(void)
  38214. {
  38215. int res = TEST_SKIPPED;
  38216. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  38217. wc_ERR_remove_state();
  38218. res = TEST_SUCCESS;
  38219. #endif
  38220. return res;
  38221. } /* End test_wc_ERR_remove_state */
  38222. /*
  38223. * Testing wc_ERR_print_errors_fp
  38224. */
  38225. static int test_wc_ERR_print_errors_fp(void)
  38226. {
  38227. EXPECT_DECLS;
  38228. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  38229. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  38230. long sz;
  38231. XFILE fp = XBADFILE;
  38232. WOLFSSL_ERROR(BAD_FUNC_ARG);
  38233. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  38234. XBADFILE);
  38235. wc_ERR_print_errors_fp(fp);
  38236. #if defined(DEBUG_WOLFSSL)
  38237. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  38238. #ifdef NO_ERROR_QUEUE
  38239. ExpectIntEQ(sz = XFTELL(fp), 0);
  38240. #else
  38241. ExpectIntNE(sz = XFTELL(fp), 0);
  38242. #endif
  38243. #endif
  38244. if (fp != XBADFILE)
  38245. XFCLOSE(fp);
  38246. (void)sz;
  38247. #endif
  38248. return EXPECT_RESULT();
  38249. } /* End test_wc_ERR_print_errors_fp */
  38250. #ifdef DEBUG_WOLFSSL
  38251. static void Logging_cb(const int logLevel, const char *const logMessage)
  38252. {
  38253. (void)logLevel;
  38254. (void)logMessage;
  38255. }
  38256. #endif
  38257. /*
  38258. * Testing wolfSSL_GetLoggingCb
  38259. */
  38260. static int test_wolfSSL_GetLoggingCb(void)
  38261. {
  38262. EXPECT_DECLS;
  38263. #ifdef DEBUG_WOLFSSL
  38264. /* Testing without wolfSSL_SetLoggingCb() */
  38265. ExpectNull(wolfSSL_GetLoggingCb());
  38266. /* Testing with wolfSSL_SetLoggingCb() */
  38267. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  38268. ExpectNotNull(wolfSSL_GetLoggingCb());
  38269. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  38270. #endif
  38271. ExpectNull(wolfSSL_GetLoggingCb());
  38272. return EXPECT_RESULT();
  38273. } /* End test_wolfSSL_GetLoggingCb */
  38274. #endif /* !NO_BIO */
  38275. static int test_wolfSSL_MD4(void)
  38276. {
  38277. EXPECT_DECLS;
  38278. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  38279. MD4_CTX md4;
  38280. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  38281. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  38282. "789012345678901234567890";
  38283. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  38284. "\xcc\x05\x36";
  38285. int msgSz = (int)XSTRLEN(msg);
  38286. XMEMSET(out, 0, sizeof(out));
  38287. MD4_Init(&md4);
  38288. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  38289. MD4_Final(out, &md4);
  38290. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  38291. #endif
  38292. return EXPECT_RESULT();
  38293. }
  38294. static int test_wolfSSL_MD5(void)
  38295. {
  38296. EXPECT_DECLS;
  38297. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  38298. byte input1[] = "";
  38299. byte input2[] = "message digest";
  38300. byte hash[WC_MD5_DIGEST_SIZE];
  38301. unsigned char output1[] =
  38302. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  38303. unsigned char output2[] =
  38304. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  38305. WOLFSSL_MD5_CTX md5;
  38306. XMEMSET(&md5, 0, sizeof(md5));
  38307. /* Test cases for illegal parameters */
  38308. ExpectIntEQ(MD5_Init(NULL), 0);
  38309. ExpectIntEQ(MD5_Init(&md5), 1);
  38310. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  38311. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  38312. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  38313. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  38314. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  38315. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  38316. /* Init MD5 CTX */
  38317. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  38318. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  38319. 1);
  38320. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  38321. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  38322. /* Init MD5 CTX */
  38323. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  38324. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  38325. (int)XSTRLEN((const char*)input2)), 1);
  38326. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  38327. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  38328. #if !defined(NO_OLD_NAMES) && \
  38329. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  38330. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  38331. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  38332. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  38333. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  38334. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  38335. &hash);
  38336. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  38337. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  38338. &hash);
  38339. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  38340. {
  38341. byte data[] = "Data to be hashed.";
  38342. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  38343. ExpectNotNull(MD5(data, sizeof(data), NULL));
  38344. ExpectNotNull(MD5(data, sizeof(data), hash));
  38345. ExpectNotNull(MD5(NULL, 0, hash));
  38346. ExpectNull(MD5(NULL, sizeof(data), hash));
  38347. }
  38348. #endif
  38349. #endif
  38350. return EXPECT_RESULT();
  38351. }
  38352. static int test_wolfSSL_MD5_Transform(void)
  38353. {
  38354. EXPECT_DECLS;
  38355. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  38356. byte input1[] = "";
  38357. byte input2[] = "abc";
  38358. byte local[WC_MD5_BLOCK_SIZE];
  38359. word32 sLen = 0;
  38360. #ifdef BIG_ENDIAN_ORDER
  38361. unsigned char output1[] =
  38362. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  38363. unsigned char output2[] =
  38364. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  38365. #else
  38366. unsigned char output1[] =
  38367. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  38368. unsigned char output2[] =
  38369. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  38370. #endif
  38371. union {
  38372. wc_Md5 native;
  38373. MD5_CTX compat;
  38374. } md5;
  38375. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  38376. XMEMSET(&local, 0, sizeof(local));
  38377. /* sanity check */
  38378. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  38379. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  38380. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  38381. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  38382. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38383. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  38384. /* Init MD5 CTX */
  38385. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  38386. /* Do Transform*/
  38387. sLen = (word32)XSTRLEN((char*)input1);
  38388. XMEMCPY(local, input1, sLen);
  38389. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  38390. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  38391. /* Init MD5 CTX */
  38392. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  38393. sLen = (word32)XSTRLEN((char*)input2);
  38394. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  38395. XMEMCPY(local, input2, sLen);
  38396. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  38397. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  38398. #endif
  38399. return EXPECT_RESULT();
  38400. }
  38401. static int test_wolfSSL_SHA(void)
  38402. {
  38403. EXPECT_DECLS;
  38404. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  38405. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  38406. (!defined(HAVE_FIPS) || \
  38407. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  38408. {
  38409. const unsigned char in[] = "abc";
  38410. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  38411. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  38412. unsigned char out[WC_SHA_DIGEST_SIZE];
  38413. unsigned char* p = NULL;
  38414. WOLFSSL_SHA_CTX sha;
  38415. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  38416. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  38417. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38418. /* SHA interface test */
  38419. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  38420. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  38421. ExpectNotNull(SHA(in, 0, out));
  38422. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  38423. ExpectNotNull(SHA(NULL, 0, out));
  38424. ExpectNotNull(SHA(NULL, 0, NULL));
  38425. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  38426. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38427. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  38428. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  38429. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  38430. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  38431. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  38432. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38433. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  38434. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  38435. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  38436. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38437. }
  38438. #endif
  38439. #if !defined(NO_SHA256)
  38440. {
  38441. const unsigned char in[] = "abc";
  38442. unsigned char expected[] =
  38443. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  38444. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  38445. "\x15\xAD";
  38446. unsigned char out[WC_SHA256_DIGEST_SIZE];
  38447. unsigned char* p = NULL;
  38448. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  38449. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38450. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  38451. #else
  38452. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  38453. #endif
  38454. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  38455. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38456. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  38457. #else
  38458. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  38459. #endif
  38460. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  38461. }
  38462. #endif
  38463. #if defined(WOLFSSL_SHA384)
  38464. {
  38465. const unsigned char in[] = "abc";
  38466. unsigned char expected[] =
  38467. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  38468. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  38469. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  38470. "\xc8\x25\xa7";
  38471. unsigned char out[WC_SHA384_DIGEST_SIZE];
  38472. unsigned char* p = NULL;
  38473. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  38474. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38475. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  38476. #else
  38477. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  38478. #endif
  38479. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  38480. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38481. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  38482. #else
  38483. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  38484. #endif
  38485. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  38486. }
  38487. #endif
  38488. #if defined(WOLFSSL_SHA512)
  38489. {
  38490. const unsigned char in[] = "abc";
  38491. unsigned char expected[] =
  38492. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  38493. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  38494. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  38495. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  38496. "\xa5\x4c\xa4\x9f";
  38497. unsigned char out[WC_SHA512_DIGEST_SIZE];
  38498. unsigned char* p = NULL;
  38499. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  38500. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38501. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  38502. #else
  38503. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  38504. #endif
  38505. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  38506. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38507. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  38508. #else
  38509. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  38510. #endif
  38511. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  38512. }
  38513. #endif
  38514. #endif
  38515. return EXPECT_RESULT();
  38516. }
  38517. static int test_wolfSSL_SHA_Transform(void)
  38518. {
  38519. EXPECT_DECLS;
  38520. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  38521. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38522. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  38523. byte input1[] = "";
  38524. byte input2[] = "abc";
  38525. byte local[WC_SHA_BLOCK_SIZE];
  38526. word32 sLen = 0;
  38527. #ifdef BIG_ENDIAN_ORDER
  38528. unsigned char output1[] =
  38529. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  38530. "\x09\xf7\x3a\x93";
  38531. unsigned char output2[] =
  38532. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  38533. "\x7c\x6e\x08\xb8";
  38534. #else
  38535. unsigned char output1[] =
  38536. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  38537. "\x93\x3a\xf7\x09";
  38538. unsigned char output2[] =
  38539. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  38540. "\xb8\x08\x6e\x7c";
  38541. #endif
  38542. union {
  38543. wc_Sha native;
  38544. SHA_CTX compat;
  38545. } sha;
  38546. union {
  38547. wc_Sha native;
  38548. SHA_CTX compat;
  38549. } sha1;
  38550. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  38551. XMEMSET(&local, 0, sizeof(local));
  38552. /* sanity check */
  38553. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  38554. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  38555. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  38556. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  38557. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  38558. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  38559. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  38560. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38561. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  38562. /* Init SHA CTX */
  38563. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  38564. /* Do Transform*/
  38565. sLen = (word32)XSTRLEN((char*)input1);
  38566. XMEMCPY(local, input1, sLen);
  38567. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  38568. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  38569. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  38570. /* Init SHA CTX */
  38571. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  38572. sLen = (word32)XSTRLEN((char*)input2);
  38573. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38574. XMEMCPY(local, input2, sLen);
  38575. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  38576. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  38577. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  38578. /* SHA1 */
  38579. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38580. /* Init SHA CTX */
  38581. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  38582. /* Do Transform*/
  38583. sLen = (word32)XSTRLEN((char*)input1);
  38584. XMEMCPY(local, input1, sLen);
  38585. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  38586. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  38587. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  38588. /* Init SHA CTX */
  38589. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  38590. sLen = (word32)XSTRLEN((char*)input2);
  38591. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38592. XMEMCPY(local, input2, sLen);
  38593. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  38594. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  38595. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  38596. #endif
  38597. #endif
  38598. return EXPECT_RESULT();
  38599. }
  38600. static int test_wolfSSL_SHA224(void)
  38601. {
  38602. EXPECT_DECLS;
  38603. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  38604. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38605. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  38606. unsigned char input[] =
  38607. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  38608. unsigned char output[] =
  38609. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  38610. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  38611. size_t inLen;
  38612. byte hash[WC_SHA224_DIGEST_SIZE];
  38613. unsigned char* p;
  38614. inLen = XSTRLEN((char*)input);
  38615. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  38616. ExpectNull(SHA224(NULL, inLen, hash));
  38617. ExpectNotNull(SHA224(input, 0, hash));
  38618. ExpectNotNull(SHA224(input, inLen, NULL));
  38619. ExpectNotNull(SHA224(NULL, 0, hash));
  38620. ExpectNotNull(SHA224(NULL, 0, NULL));
  38621. ExpectNotNull(SHA224(input, inLen, hash));
  38622. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  38623. ExpectNotNull(p = SHA224(input, inLen, NULL));
  38624. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  38625. #endif
  38626. return EXPECT_RESULT();
  38627. }
  38628. static int test_wolfSSL_SHA256(void)
  38629. {
  38630. EXPECT_DECLS;
  38631. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  38632. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38633. unsigned char input[] =
  38634. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  38635. unsigned char output[] =
  38636. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  38637. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  38638. "\x06\xC1";
  38639. size_t inLen;
  38640. byte hash[WC_SHA256_DIGEST_SIZE];
  38641. inLen = XSTRLEN((char*)input);
  38642. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  38643. ExpectNotNull(SHA256(input, inLen, hash));
  38644. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  38645. #endif
  38646. return EXPECT_RESULT();
  38647. }
  38648. static int test_wolfSSL_SHA256_Transform(void)
  38649. {
  38650. EXPECT_DECLS;
  38651. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  38652. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38653. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38654. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  38655. !defined(WOLFSSL_KCAPI_HASH)
  38656. byte input1[] = "";
  38657. byte input2[] = "abc";
  38658. byte local[WC_SHA256_BLOCK_SIZE];
  38659. word32 sLen = 0;
  38660. #ifdef BIG_ENDIAN_ORDER
  38661. unsigned char output1[] =
  38662. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  38663. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  38664. unsigned char output2[] =
  38665. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  38666. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  38667. #else
  38668. unsigned char output1[] =
  38669. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  38670. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  38671. unsigned char output2[] =
  38672. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  38673. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  38674. #endif
  38675. union {
  38676. wc_Sha256 native;
  38677. SHA256_CTX compat;
  38678. } sha256;
  38679. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  38680. XMEMSET(&local, 0, sizeof(local));
  38681. /* sanity check */
  38682. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  38683. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  38684. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  38685. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  38686. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38687. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  38688. /* Init SHA256 CTX */
  38689. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  38690. /* Do Transform*/
  38691. sLen = (word32)XSTRLEN((char*)input1);
  38692. XMEMCPY(local, input1, sLen);
  38693. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  38694. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  38695. 0);
  38696. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  38697. /* Init SHA256 CTX */
  38698. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  38699. sLen = (word32)XSTRLEN((char*)input2);
  38700. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  38701. XMEMCPY(local, input2, sLen);
  38702. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  38703. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  38704. 0);
  38705. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  38706. #endif
  38707. #endif
  38708. return EXPECT_RESULT();
  38709. }
  38710. static int test_wolfSSL_SHA512_Transform(void)
  38711. {
  38712. EXPECT_DECLS;
  38713. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  38714. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38715. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38716. !defined(WOLFSSL_KCAPI_HASH)
  38717. byte input1[] = "";
  38718. byte input2[] = "abc";
  38719. byte local[WC_SHA512_BLOCK_SIZE];
  38720. word32 sLen = 0;
  38721. #ifdef BIG_ENDIAN_ORDER
  38722. unsigned char output1[] =
  38723. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  38724. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  38725. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  38726. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  38727. unsigned char output2[] =
  38728. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  38729. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  38730. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  38731. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  38732. #else
  38733. unsigned char output1[] =
  38734. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  38735. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  38736. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  38737. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  38738. unsigned char output2[] =
  38739. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  38740. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  38741. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  38742. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  38743. #endif
  38744. union {
  38745. wc_Sha512 native;
  38746. SHA512_CTX compat;
  38747. } sha512;
  38748. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38749. XMEMSET(&local, 0, sizeof(local));
  38750. /* sanity check */
  38751. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  38752. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  38753. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  38754. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  38755. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38756. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38757. /* Init SHA512 CTX */
  38758. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  38759. /* Do Transform*/
  38760. sLen = (word32)XSTRLEN((char*)input1);
  38761. XMEMCPY(local, input1, sLen);
  38762. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38763. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38764. WC_SHA512_DIGEST_SIZE), 0);
  38765. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38766. /* Init SHA512 CTX */
  38767. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  38768. sLen = (word32)XSTRLEN((char*)input2);
  38769. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38770. XMEMCPY(local, input2, sLen);
  38771. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38772. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38773. WC_SHA512_DIGEST_SIZE), 0);
  38774. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38775. (void)input1;
  38776. #endif
  38777. #endif
  38778. return EXPECT_RESULT();
  38779. }
  38780. static int test_wolfSSL_SHA512_224_Transform(void)
  38781. {
  38782. EXPECT_DECLS;
  38783. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38784. !defined(WOLFSSL_NOSHA512_224)
  38785. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38786. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38787. !defined(WOLFSSL_KCAPI_HASH)
  38788. byte input1[] = "";
  38789. byte input2[] = "abc";
  38790. byte local[WC_SHA512_BLOCK_SIZE];
  38791. word32 sLen = 0;
  38792. unsigned char output1[] =
  38793. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  38794. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  38795. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  38796. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  38797. unsigned char output2[] =
  38798. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  38799. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  38800. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  38801. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  38802. union {
  38803. wc_Sha512 native;
  38804. SHA512_CTX compat;
  38805. } sha512;
  38806. #ifdef BIG_ENDIAN_ORDER
  38807. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38808. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38809. #endif
  38810. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38811. XMEMSET(&local, 0, sizeof(local));
  38812. /* sanity check */
  38813. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  38814. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  38815. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  38816. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  38817. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  38818. BAD_FUNC_ARG);
  38819. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38820. /* Init SHA512 CTX */
  38821. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  38822. /* Do Transform*/
  38823. sLen = (word32)XSTRLEN((char*)input1);
  38824. XMEMCPY(local, input1, sLen);
  38825. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38826. 1);
  38827. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38828. WC_SHA512_DIGEST_SIZE), 0);
  38829. /* frees resources */
  38830. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38831. /* Init SHA512 CTX */
  38832. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  38833. sLen = (word32)XSTRLEN((char*)input2);
  38834. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38835. XMEMCPY(local, input2, sLen);
  38836. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38837. 1);
  38838. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38839. WC_SHA512_DIGEST_SIZE), 0);
  38840. /* frees resources */
  38841. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38842. #endif
  38843. #endif
  38844. return EXPECT_RESULT();
  38845. }
  38846. static int test_wolfSSL_SHA512_256_Transform(void)
  38847. {
  38848. EXPECT_DECLS;
  38849. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38850. !defined(WOLFSSL_NOSHA512_256)
  38851. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38852. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38853. !defined(WOLFSSL_KCAPI_HASH)
  38854. byte input1[] = "";
  38855. byte input2[] = "abc";
  38856. byte local[WC_SHA512_BLOCK_SIZE];
  38857. word32 sLen = 0;
  38858. unsigned char output1[] =
  38859. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  38860. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  38861. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  38862. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  38863. unsigned char output2[] =
  38864. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  38865. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  38866. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  38867. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  38868. union {
  38869. wc_Sha512 native;
  38870. SHA512_CTX compat;
  38871. } sha512;
  38872. #ifdef BIG_ENDIAN_ORDER
  38873. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38874. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38875. #endif
  38876. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38877. XMEMSET(&local, 0, sizeof(local));
  38878. /* sanity check */
  38879. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  38880. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  38881. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  38882. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  38883. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  38884. BAD_FUNC_ARG);
  38885. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38886. /* Init SHA512 CTX */
  38887. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  38888. /* Do Transform*/
  38889. sLen = (word32)XSTRLEN((char*)input1);
  38890. XMEMCPY(local, input1, sLen);
  38891. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38892. 1);
  38893. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38894. WC_SHA512_DIGEST_SIZE), 0);
  38895. /* frees resources */
  38896. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38897. /* Init SHA512 CTX */
  38898. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  38899. sLen = (word32)XSTRLEN((char*)input2);
  38900. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38901. XMEMCPY(local, input2, sLen);
  38902. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38903. 1);
  38904. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38905. WC_SHA512_DIGEST_SIZE), 0);
  38906. /* frees resources */
  38907. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38908. #endif
  38909. #endif
  38910. return EXPECT_RESULT();
  38911. }
  38912. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38913. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  38914. * buffer of 64 bytes.
  38915. *
  38916. * returns the size of the digest buffer on success and a negative value on
  38917. * failure.
  38918. */
  38919. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  38920. int* sz)
  38921. {
  38922. EXPECT_DECLS;
  38923. HMAC_CTX ctx1;
  38924. HMAC_CTX ctx2;
  38925. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  38926. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  38927. unsigned char long_key[] =
  38928. "0123456789012345678901234567890123456789"
  38929. "0123456789012345678901234567890123456789"
  38930. "0123456789012345678901234567890123456789"
  38931. "0123456789012345678901234567890123456789";
  38932. unsigned char msg[] = "message to hash";
  38933. unsigned int digestSz = 64;
  38934. int keySz = sizeof(key);
  38935. int long_keySz = sizeof(long_key);
  38936. int msgSz = sizeof(msg);
  38937. unsigned char digest2[64];
  38938. unsigned int digestSz2 = 64;
  38939. HMAC_CTX_init(&ctx1);
  38940. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38941. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38942. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38943. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38944. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38945. HMAC_CTX_cleanup(&ctx1);
  38946. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38947. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  38948. HMAC_CTX_cleanup(&ctx2);
  38949. ExpectIntEQ(digestSz, digestSz2);
  38950. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38951. /* test HMAC_Init with NULL key */
  38952. /* init after copy */
  38953. HMAC_CTX_init(&ctx1);
  38954. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38955. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38956. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38957. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38958. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38959. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38960. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38961. HMAC_CTX_cleanup(&ctx1);
  38962. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38963. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38964. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38965. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38966. HMAC_CTX_cleanup(&ctx2);
  38967. ExpectIntEQ(digestSz, digestSz2);
  38968. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38969. /* long key */
  38970. HMAC_CTX_init(&ctx1);
  38971. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  38972. SSL_SUCCESS);
  38973. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38974. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38975. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38976. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38977. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38978. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38979. HMAC_CTX_cleanup(&ctx1);
  38980. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38981. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38982. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38983. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38984. HMAC_CTX_cleanup(&ctx2);
  38985. ExpectIntEQ(digestSz, digestSz2);
  38986. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38987. /* init before copy */
  38988. HMAC_CTX_init(&ctx1);
  38989. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38990. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38991. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38992. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38993. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38994. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38995. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38996. HMAC_CTX_cleanup(&ctx1);
  38997. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38998. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38999. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  39000. HMAC_CTX_cleanup(&ctx2);
  39001. ExpectIntEQ(digestSz, digestSz2);
  39002. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  39003. *sz = (int)digestSz;
  39004. return EXPECT_RESULT();
  39005. }
  39006. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  39007. static int test_wolfSSL_HMAC_CTX(void)
  39008. {
  39009. EXPECT_DECLS;
  39010. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  39011. unsigned char digest[64];
  39012. int digestSz;
  39013. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  39014. WOLFSSL_HMAC_CTX ctx1;
  39015. WOLFSSL_HMAC_CTX ctx2;
  39016. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  39017. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  39018. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  39019. wolfSSL_HMAC_CTX_free(NULL);
  39020. wolfSSL_HMAC_CTX_free(hmac_ctx);
  39021. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  39022. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  39023. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  39024. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  39025. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  39026. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  39027. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  39028. ((! defined(HAVE_FIPS_VERSION)) || \
  39029. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  39030. /* Copy object that hasn't had a digest set - MD5. */
  39031. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  39032. #else
  39033. /* Copy object that hasn't had a digest set. */
  39034. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  39035. #endif
  39036. HMAC_CTX_cleanup(NULL);
  39037. HMAC_CTX_cleanup(&ctx2);
  39038. ExpectNull(HMAC_CTX_get_md(NULL));
  39039. #ifndef NO_SHA
  39040. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  39041. TEST_SUCCESS);
  39042. ExpectIntEQ(digestSz, 20);
  39043. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  39044. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  39045. #endif /* !NO_SHA */
  39046. #ifdef WOLFSSL_SHA224
  39047. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  39048. TEST_SUCCESS);
  39049. ExpectIntEQ(digestSz, 28);
  39050. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  39051. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  39052. "\x02\x0E", digest, digestSz), 0);
  39053. #endif /* WOLFSSL_SHA224 */
  39054. #ifndef NO_SHA256
  39055. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  39056. TEST_SUCCESS);
  39057. ExpectIntEQ(digestSz, 32);
  39058. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  39059. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  39060. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  39061. #endif /* !NO_SHA256 */
  39062. #ifdef WOLFSSL_SHA384
  39063. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  39064. TEST_SUCCESS);
  39065. ExpectIntEQ(digestSz, 48);
  39066. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  39067. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  39068. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  39069. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  39070. digest, digestSz), 0);
  39071. #endif /* WOLFSSL_SHA384 */
  39072. #ifdef WOLFSSL_SHA512
  39073. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  39074. TEST_SUCCESS);
  39075. ExpectIntEQ(digestSz, 64);
  39076. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  39077. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  39078. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  39079. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  39080. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  39081. digest, digestSz), 0);
  39082. #endif /* WOLFSSL_SHA512 */
  39083. #ifdef WOLFSSL_SHA3
  39084. #ifndef WOLFSSL_NOSHA3_224
  39085. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  39086. TEST_SUCCESS);
  39087. ExpectIntEQ(digestSz, 28);
  39088. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  39089. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  39090. "\x29\x0f", digest, digestSz), 0);
  39091. #endif
  39092. #ifndef WOLFSSL_NOSHA3_256
  39093. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  39094. TEST_SUCCESS);
  39095. ExpectIntEQ(digestSz, 32);
  39096. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  39097. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  39098. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  39099. #endif
  39100. #ifndef WOLFSSL_NOSHA3_384
  39101. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  39102. TEST_SUCCESS);
  39103. ExpectIntEQ(digestSz, 48);
  39104. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  39105. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  39106. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  39107. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  39108. digest, digestSz), 0);
  39109. #endif
  39110. #ifndef WOLFSSL_NOSHA3_512
  39111. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  39112. TEST_SUCCESS);
  39113. ExpectIntEQ(digestSz, 64);
  39114. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  39115. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  39116. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  39117. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  39118. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  39119. digest, digestSz), 0);
  39120. #endif
  39121. #endif
  39122. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  39123. HAVE_FIPS_VERSION <= 2)
  39124. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  39125. TEST_SUCCESS);
  39126. ExpectIntEQ(digestSz, 16);
  39127. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  39128. "\xE4\x98\xDD", digest, digestSz), 0);
  39129. #endif /* !NO_MD5 */
  39130. #endif
  39131. return EXPECT_RESULT();
  39132. }
  39133. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  39134. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  39135. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  39136. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  39137. {
  39138. EXPECT_DECLS;
  39139. static const unsigned char key[] = "simple test key";
  39140. HMAC_CTX* hmac = NULL;
  39141. ENGINE* e = NULL;
  39142. unsigned char hash[WC_MAX_DIGEST_SIZE];
  39143. unsigned int len;
  39144. ExpectNotNull(hmac = HMAC_CTX_new());
  39145. HMAC_CTX_init(hmac);
  39146. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  39147. ((! defined(HAVE_FIPS_VERSION)) || \
  39148. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  39149. /* Get size on object that hasn't had a digest set - MD5. */
  39150. ExpectIntEQ(HMAC_size(hmac), 16);
  39151. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  39152. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  39153. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  39154. #else
  39155. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  39156. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  39157. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  39158. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  39159. #endif
  39160. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  39161. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  39162. /* re-using test key as data to hash */
  39163. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  39164. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  39165. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  39166. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  39167. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  39168. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  39169. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  39170. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  39171. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  39172. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  39173. ExpectIntEQ(len, md_len);
  39174. ExpectIntEQ(HMAC_size(NULL), 0);
  39175. ExpectIntEQ(HMAC_size(hmac), md_len);
  39176. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  39177. HMAC_cleanup(NULL);
  39178. HMAC_cleanup(hmac);
  39179. HMAC_CTX_free(hmac);
  39180. len = 0;
  39181. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  39182. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  39183. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  39184. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  39185. ExpectIntEQ(len, md_len);
  39186. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  39187. /* With data. */
  39188. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  39189. &len));
  39190. /* With NULL data. */
  39191. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  39192. &len));
  39193. /* With zero length data. */
  39194. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  39195. return EXPECT_RESULT();
  39196. }
  39197. #endif
  39198. static int test_wolfSSL_HMAC(void)
  39199. {
  39200. EXPECT_DECLS;
  39201. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  39202. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  39203. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  39204. #ifndef NO_SHA256
  39205. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  39206. TEST_SUCCESS);
  39207. #endif
  39208. #ifdef WOLFSSL_SHA224
  39209. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  39210. TEST_SUCCESS);
  39211. #endif
  39212. #ifdef WOLFSSL_SHA384
  39213. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  39214. TEST_SUCCESS);
  39215. #endif
  39216. #ifdef WOLFSSL_SHA512
  39217. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  39218. TEST_SUCCESS);
  39219. #endif
  39220. #ifdef WOLFSSL_SHA3
  39221. #ifndef WOLFSSL_NOSHA3_224
  39222. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  39223. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  39224. #endif
  39225. #ifndef WOLFSSL_NOSHA3_256
  39226. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  39227. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  39228. #endif
  39229. #ifndef WOLFSSL_NOSHA3_384
  39230. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  39231. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  39232. #endif
  39233. #ifndef WOLFSSL_NOSHA3_512
  39234. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  39235. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  39236. #endif
  39237. #endif
  39238. #ifndef NO_SHA
  39239. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  39240. TEST_SUCCESS);
  39241. #endif
  39242. #endif
  39243. return EXPECT_RESULT();
  39244. }
  39245. static int test_wolfSSL_CMAC(void)
  39246. {
  39247. EXPECT_DECLS;
  39248. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  39249. defined(WOLFSSL_AES_DIRECT)
  39250. int i;
  39251. byte key[AES_256_KEY_SIZE];
  39252. CMAC_CTX* cmacCtx = NULL;
  39253. byte out[AES_BLOCK_SIZE];
  39254. size_t outLen = AES_BLOCK_SIZE;
  39255. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  39256. key[i] = i;
  39257. }
  39258. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39259. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  39260. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  39261. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  39262. NULL), 1);
  39263. /* re-using test key as data to hash */
  39264. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  39265. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  39266. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  39267. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  39268. /* No Update works. */
  39269. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  39270. NULL), 1);
  39271. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  39272. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  39273. NULL), 1);
  39274. /* Test parameters with CMAC_Update. */
  39275. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  39276. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  39277. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  39278. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  39279. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  39280. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  39281. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  39282. /* Test parameters with CMAC_Final. */
  39283. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  39284. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  39285. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  39286. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  39287. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  39288. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  39289. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  39290. CMAC_CTX_free(cmacCtx);
  39291. /* Test parameters with CMAC Init. */
  39292. cmacCtx = NULL;
  39293. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39294. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  39295. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  39296. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  39297. NULL), 0);
  39298. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  39299. NULL), 0);
  39300. /* give a key too small for the cipher, verify we get failure */
  39301. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  39302. NULL), 0);
  39303. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  39304. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  39305. /* Only AES-CBC supported. */
  39306. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  39307. NULL), 0);
  39308. #endif
  39309. CMAC_CTX_free(cmacCtx);
  39310. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  39311. cmacCtx = NULL;
  39312. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39313. /* No Init. */
  39314. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  39315. CMAC_CTX_free(cmacCtx);
  39316. /* Test AES-256-CBC */
  39317. cmacCtx = NULL;
  39318. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39319. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  39320. NULL), 1);
  39321. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  39322. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  39323. CMAC_CTX_free(cmacCtx);
  39324. /* Test AES-192-CBC */
  39325. cmacCtx = NULL;
  39326. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39327. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  39328. NULL), 1);
  39329. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  39330. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  39331. CMAC_CTX_free(cmacCtx);
  39332. cmacCtx = NULL;
  39333. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39334. CMAC_CTX_free(cmacCtx);
  39335. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  39336. return EXPECT_RESULT();
  39337. }
  39338. static int test_wolfSSL_DES(void)
  39339. {
  39340. EXPECT_DECLS;
  39341. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  39342. const_DES_cblock myDes;
  39343. DES_cblock iv;
  39344. DES_key_schedule key;
  39345. word32 i = 0;
  39346. DES_LONG dl = 0;
  39347. unsigned char msg[] = "hello wolfssl";
  39348. unsigned char weakKey[][8] = {
  39349. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  39350. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  39351. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  39352. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  39353. };
  39354. unsigned char semiWeakKey[][8] = {
  39355. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  39356. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  39357. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  39358. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  39359. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  39360. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  39361. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  39362. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  39363. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  39364. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  39365. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  39366. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  39367. };
  39368. DES_check_key(1);
  39369. DES_set_key(&myDes, &key);
  39370. /* check, check of odd parity */
  39371. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  39372. myDes[0] = 6; /* set even parity */
  39373. XMEMSET(key, 5, sizeof(DES_key_schedule));
  39374. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  39375. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  39376. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  39377. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  39378. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  39379. /* set odd parity for success case */
  39380. DES_set_odd_parity(&myDes);
  39381. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  39382. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  39383. myDes[3]);
  39384. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  39385. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  39386. ExpectIntEQ(key[i], myDes[i]);
  39387. }
  39388. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  39389. /* check weak key */
  39390. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  39391. XMEMSET(key, 5, sizeof(DES_key_schedule));
  39392. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  39393. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  39394. DES_set_key_unchecked(NULL, NULL);
  39395. DES_set_key_unchecked(&myDes, NULL);
  39396. DES_set_key_unchecked(NULL, &key);
  39397. /* compare arrays, should be the same */
  39398. /* now do unchecked copy of a weak key over */
  39399. DES_set_key_unchecked(&myDes, &key);
  39400. /* compare arrays, should be the same */
  39401. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  39402. ExpectIntEQ(key[i], myDes[i]);
  39403. }
  39404. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  39405. myDes[7] = 2;
  39406. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  39407. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  39408. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  39409. /* Test all weak keys. */
  39410. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  39411. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  39412. }
  39413. /* Test all semi-weak keys. */
  39414. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  39415. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  39416. }
  39417. /* check DES_key_sched API */
  39418. XMEMSET(key, 1, sizeof(DES_key_schedule));
  39419. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  39420. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  39421. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  39422. /* compare arrays, should be the same */
  39423. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  39424. ExpectIntEQ(key[i], myDes[i]);
  39425. }
  39426. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  39427. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  39428. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  39429. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  39430. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  39431. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  39432. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  39433. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  39434. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  39435. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  39436. * DES_cbc_encrypt on the input */
  39437. XMEMSET(iv, 0, sizeof(DES_cblock));
  39438. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  39439. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  39440. ExpectIntEQ(dl, 480052723);
  39441. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  39442. return EXPECT_RESULT();
  39443. }
  39444. static int test_wolfSSL_DES_ncbc(void)
  39445. {
  39446. EXPECT_DECLS;
  39447. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  39448. const_DES_cblock myDes;
  39449. DES_cblock iv = {1};
  39450. DES_key_schedule key = {0};
  39451. unsigned char msg[] = "hello wolfssl";
  39452. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  39453. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  39454. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  39455. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  39456. /* partial block test */
  39457. DES_set_key(&key, &myDes);
  39458. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  39459. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  39460. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  39461. DES_set_key(&key, &myDes);
  39462. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  39463. *((byte*)&iv) = 1;
  39464. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  39465. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  39466. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  39467. /* full block test */
  39468. DES_set_key(&key, &myDes);
  39469. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  39470. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  39471. *((byte*)&iv) = 1;
  39472. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  39473. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  39474. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  39475. DES_set_key(&key, &myDes);
  39476. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  39477. *((byte*)&iv) = 1;
  39478. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  39479. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  39480. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  39481. #endif
  39482. return EXPECT_RESULT();
  39483. }
  39484. static int test_wolfSSL_DES_ecb_encrypt(void)
  39485. {
  39486. EXPECT_DECLS;
  39487. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  39488. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  39489. WOLFSSL_DES_key_schedule key;
  39490. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  39491. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  39492. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  39493. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  39494. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  39495. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  39496. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  39497. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  39498. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  39499. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  39500. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  39501. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  39502. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  39503. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  39504. /* Encrypt messages */
  39505. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  39506. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  39507. {
  39508. /* Decrypt messages */
  39509. int ret1 = 0;
  39510. int ret2 = 0;
  39511. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  39512. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  39513. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  39514. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  39515. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  39516. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  39517. }
  39518. #endif
  39519. return EXPECT_RESULT();
  39520. }
  39521. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  39522. {
  39523. EXPECT_DECLS;
  39524. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  39525. unsigned char input1[8], input2[8];
  39526. unsigned char output1[8], output2[8];
  39527. unsigned char back1[8], back2[8];
  39528. WOLFSSL_DES_cblock iv1, iv2;
  39529. WOLFSSL_DES_key_schedule key1, key2, key3;
  39530. int i;
  39531. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  39532. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  39533. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  39534. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  39535. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  39536. XMEMSET(output1, 0, sizeof(output1));
  39537. XMEMSET(output2, 0, sizeof(output2));
  39538. XMEMSET(back1, 0, sizeof(back1));
  39539. XMEMSET(back2, 0, sizeof(back2));
  39540. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39541. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39542. /* Encrypt messages */
  39543. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  39544. DES_ENCRYPT);
  39545. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  39546. DES_ENCRYPT);
  39547. {
  39548. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39549. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39550. /* Decrypt messages */
  39551. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  39552. &iv1, DES_DECRYPT);
  39553. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  39554. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  39555. &iv2, DES_DECRYPT);
  39556. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  39557. }
  39558. for (i = 0; i < 8; i++) {
  39559. XMEMSET(output1, 0, sizeof(output1));
  39560. XMEMSET(output2, 0, sizeof(output2));
  39561. XMEMSET(back1, 0, sizeof(back1));
  39562. XMEMSET(back2, 0, sizeof(back2));
  39563. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39564. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39565. /* Encrypt partial messages */
  39566. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  39567. &iv1, DES_ENCRYPT);
  39568. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  39569. &iv2, DES_ENCRYPT);
  39570. {
  39571. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39572. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39573. /* Decrypt messages */
  39574. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  39575. &key3, &iv1, DES_DECRYPT);
  39576. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  39577. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  39578. &key3, &iv2, DES_DECRYPT);
  39579. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  39580. }
  39581. }
  39582. #endif
  39583. return EXPECT_RESULT();
  39584. }
  39585. static int test_wolfSSL_AES_encrypt(void)
  39586. {
  39587. EXPECT_DECLS;
  39588. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  39589. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39590. AES_KEY enc;
  39591. AES_KEY dec;
  39592. const byte msg[] = {
  39593. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39594. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39595. };
  39596. const byte exp[] = {
  39597. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  39598. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  39599. };
  39600. const byte key[] = {
  39601. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39602. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39603. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39604. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39605. };
  39606. byte eout[sizeof(msg)];
  39607. byte dout[sizeof(msg)];
  39608. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  39609. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  39610. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  39611. wolfSSL_AES_encrypt(msg, NULL, NULL);
  39612. wolfSSL_AES_encrypt(NULL, eout, NULL);
  39613. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  39614. wolfSSL_AES_encrypt(msg, eout, NULL);
  39615. wolfSSL_AES_encrypt(msg, NULL, &enc);
  39616. wolfSSL_AES_encrypt(NULL, eout, &enc);
  39617. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  39618. wolfSSL_AES_decrypt(eout, NULL, NULL);
  39619. wolfSSL_AES_decrypt(NULL, dout, NULL);
  39620. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  39621. wolfSSL_AES_decrypt(eout, dout, NULL);
  39622. wolfSSL_AES_decrypt(eout, NULL, &dec);
  39623. wolfSSL_AES_decrypt(NULL, dout, &dec);
  39624. wolfSSL_AES_encrypt(msg, eout, &enc);
  39625. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  39626. wolfSSL_AES_decrypt(eout, dout, &dec);
  39627. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  39628. #endif
  39629. return EXPECT_RESULT();
  39630. }
  39631. static int test_wolfSSL_AES_ecb_encrypt(void)
  39632. {
  39633. EXPECT_DECLS;
  39634. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  39635. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39636. AES_KEY aes;
  39637. const byte msg[] =
  39638. {
  39639. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39640. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  39641. };
  39642. const byte verify[] =
  39643. {
  39644. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  39645. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  39646. };
  39647. const byte key[] =
  39648. {
  39649. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39650. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39651. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39652. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  39653. };
  39654. byte out[AES_BLOCK_SIZE];
  39655. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  39656. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39657. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  39658. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  39659. #ifdef HAVE_AES_DECRYPT
  39660. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  39661. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39662. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  39663. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  39664. #endif
  39665. /* test bad arguments */
  39666. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  39667. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  39668. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  39669. #endif
  39670. return EXPECT_RESULT();
  39671. }
  39672. static int test_wolfSSL_AES_cbc_encrypt(void)
  39673. {
  39674. EXPECT_DECLS;
  39675. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39676. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39677. AES_KEY aes;
  39678. AES_KEY* aesN = NULL;
  39679. size_t len = 0;
  39680. size_t lenB = 0;
  39681. int keySz0 = 0;
  39682. int keySzN = -1;
  39683. byte out[AES_BLOCK_SIZE] = {0};
  39684. byte* outN = NULL;
  39685. /* Test vectors retrieved from:
  39686. * <begin URL>
  39687. * https://csrc.nist.gov/
  39688. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  39689. * documents/aes/KAT_AES.zip
  39690. * </end URL>
  39691. */
  39692. const byte* pt128N = NULL;
  39693. byte* key128N = NULL;
  39694. byte* iv128N = NULL;
  39695. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  39696. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39697. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  39698. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  39699. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  39700. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39701. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  39702. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  39703. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  39704. len = sizeof(pt128);
  39705. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  39706. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  39707. ExpectIntNE(XMEMCMP(b, g, h), i)
  39708. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  39709. /* Stressing wolfSSL_AES_cbc_encrypt() */
  39710. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  39711. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  39712. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  39713. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39714. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  39715. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39716. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  39717. /* Stressing wolfSSL_AES_set_encrypt_key */
  39718. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  39719. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  39720. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  39721. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  39722. /* Stressing wolfSSL_AES_set_decrypt_key */
  39723. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  39724. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  39725. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  39726. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  39727. #ifdef WOLFSSL_AES_128
  39728. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  39729. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39730. RESET_IV(iv128tmp, iv128);
  39731. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39732. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  39733. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39734. wc_AesFree((Aes*)&aes);
  39735. #ifdef HAVE_AES_DECRYPT
  39736. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  39737. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39738. RESET_IV(iv128tmp, iv128);
  39739. len = sizeof(ct128);
  39740. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39741. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  39742. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  39743. wc_AesFree((Aes*)&aes);
  39744. #endif
  39745. #endif /* WOLFSSL_AES_128 */
  39746. #ifdef WOLFSSL_AES_192
  39747. {
  39748. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  39749. * Appendix F.2.3 */
  39750. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  39751. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39752. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39753. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  39754. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  39755. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39756. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39757. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  39758. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  39759. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  39760. len = sizeof(pt192);
  39761. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  39762. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39763. RESET_IV(iv192tmp, iv192);
  39764. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39765. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  39766. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  39767. wc_AesFree((Aes*)&aes);
  39768. #ifdef HAVE_AES_DECRYPT
  39769. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  39770. len = sizeof(ct192);
  39771. RESET_IV(iv192tmp, iv192);
  39772. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39773. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39774. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  39775. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  39776. wc_AesFree((Aes*)&aes);
  39777. #endif
  39778. }
  39779. #endif /* WOLFSSL_AES_192 */
  39780. #ifdef WOLFSSL_AES_256
  39781. {
  39782. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  39783. * Appendix F.2.5 */
  39784. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  39785. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39786. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39787. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  39788. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  39789. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39790. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39791. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39792. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39793. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39794. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  39795. len = sizeof(pt256);
  39796. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  39797. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39798. RESET_IV(iv256tmp, iv256);
  39799. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39800. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  39801. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  39802. wc_AesFree((Aes*)&aes);
  39803. #ifdef HAVE_AES_DECRYPT
  39804. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  39805. len = sizeof(ct256);
  39806. RESET_IV(iv256tmp, iv256);
  39807. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39808. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39809. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  39810. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  39811. wc_AesFree((Aes*)&aes);
  39812. #endif
  39813. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  39814. !defined(HAVE_SELFTEST)
  39815. {
  39816. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  39817. byte wrapPlain[sizeof(key256)] = { 0 };
  39818. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  39819. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  39820. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39821. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39822. 15), WOLFSSL_FAILURE);
  39823. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39824. sizeof(key256)), sizeof(wrapCipher));
  39825. wc_AesFree((Aes*)&aes);
  39826. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  39827. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39828. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39829. 23), WOLFSSL_FAILURE);
  39830. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39831. sizeof(wrapCipher)), sizeof(wrapPlain));
  39832. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39833. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  39834. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  39835. wc_AesFree((Aes*)&aes);
  39836. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  39837. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39838. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  39839. sizeof(key256)), sizeof(wrapCipher));
  39840. wc_AesFree((Aes*)&aes);
  39841. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  39842. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39843. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  39844. sizeof(wrapCipher)), sizeof(wrapPlain));
  39845. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39846. wc_AesFree((Aes*)&aes);
  39847. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39848. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39849. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39850. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  39851. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  39852. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  39853. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  39854. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  39855. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  39856. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39857. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39858. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39859. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  39860. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  39861. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  39862. 0);
  39863. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  39864. 0);
  39865. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  39866. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  39867. }
  39868. #endif /* HAVE_AES_KEYWRAP */
  39869. }
  39870. #endif /* WOLFSSL_AES_256 */
  39871. #endif
  39872. return EXPECT_RESULT();
  39873. }
  39874. static int test_wolfSSL_AES_cfb128_encrypt(void)
  39875. {
  39876. EXPECT_DECLS;
  39877. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  39878. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39879. AES_KEY aesEnc;
  39880. AES_KEY aesDec;
  39881. const byte msg[] = {
  39882. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39883. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39884. };
  39885. const byte exp[] = {
  39886. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  39887. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  39888. };
  39889. const byte key[] = {
  39890. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39891. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39892. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39893. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39894. };
  39895. const byte ivData[] = {
  39896. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39897. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39898. };
  39899. byte out[AES_BLOCK_SIZE];
  39900. byte iv[AES_BLOCK_SIZE];
  39901. word32 i;
  39902. int num;
  39903. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39904. XMEMCPY(iv, ivData, sizeof(iv));
  39905. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39906. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  39907. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  39908. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39909. #ifdef HAVE_AES_DECRYPT
  39910. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39911. XMEMCPY(iv, ivData, sizeof(iv));
  39912. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39913. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  39914. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39915. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39916. #endif
  39917. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  39918. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39919. XMEMCPY(iv, ivData, sizeof(iv));
  39920. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39921. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  39922. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39923. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  39924. if (i == 0) {
  39925. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39926. }
  39927. else {
  39928. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39929. }
  39930. #ifdef HAVE_AES_DECRYPT
  39931. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39932. XMEMCPY(iv, ivData, sizeof(iv));
  39933. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39934. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  39935. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39936. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  39937. if (i == 0) {
  39938. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39939. }
  39940. else {
  39941. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39942. }
  39943. #endif
  39944. }
  39945. if (EXPECT_SUCCESS()) {
  39946. /* test bad arguments */
  39947. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39948. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39949. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  39950. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39951. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  39952. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39953. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39954. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  39955. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39956. }
  39957. #endif
  39958. return EXPECT_RESULT();
  39959. }
  39960. static int test_wolfSSL_CRYPTO_cts128(void)
  39961. {
  39962. EXPECT_DECLS;
  39963. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39964. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39965. byte tmp[64]; /* Largest vector size */
  39966. /* Test vectors taken form RFC3962 Appendix B */
  39967. const testVector vects[] = {
  39968. {
  39969. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39970. "\x20",
  39971. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  39972. "\x97",
  39973. 17, 17
  39974. },
  39975. {
  39976. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39977. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  39978. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  39979. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  39980. 31, 31
  39981. },
  39982. {
  39983. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39984. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  39985. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39986. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  39987. 32, 32
  39988. },
  39989. {
  39990. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39991. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39992. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  39993. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39994. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  39995. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  39996. 47, 47
  39997. },
  39998. {
  39999. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40000. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  40001. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  40002. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  40003. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  40004. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  40005. 48, 48
  40006. },
  40007. {
  40008. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40009. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  40010. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  40011. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  40012. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  40013. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  40014. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  40015. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  40016. 64, 64
  40017. }
  40018. };
  40019. byte keyBytes[AES_128_KEY_SIZE] = {
  40020. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  40021. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  40022. };
  40023. size_t i;
  40024. AES_KEY encKey;
  40025. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  40026. XMEMSET(tmp, 0, sizeof(tmp));
  40027. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  40028. AES_KEY decKey;
  40029. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  40030. &encKey), 0);
  40031. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  40032. &decKey), 0);
  40033. XMEMSET(iv, 0, sizeof(iv));
  40034. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  40035. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  40036. vects[i].outLen);
  40037. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  40038. XMEMSET(iv, 0, sizeof(iv));
  40039. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  40040. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  40041. vects[i].inLen);
  40042. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  40043. }
  40044. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  40045. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  40046. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  40047. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  40048. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  40049. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  40050. (cbc128_f)AES_cbc_encrypt), 0);
  40051. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  40052. (cbc128_f)AES_cbc_encrypt), 0);
  40053. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  40054. (cbc128_f)AES_cbc_encrypt), 0);
  40055. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  40056. (cbc128_f)AES_cbc_encrypt), 0);
  40057. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  40058. (cbc128_f)AES_cbc_encrypt), 0);
  40059. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  40060. /* Length too small. */
  40061. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  40062. (cbc128_f)AES_cbc_encrypt), 0);
  40063. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  40064. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  40065. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  40066. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  40067. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  40068. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  40069. (cbc128_f)AES_cbc_encrypt), 0);
  40070. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  40071. (cbc128_f)AES_cbc_encrypt), 0);
  40072. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  40073. (cbc128_f)AES_cbc_encrypt), 0);
  40074. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  40075. (cbc128_f)AES_cbc_encrypt), 0);
  40076. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  40077. (cbc128_f)AES_cbc_encrypt), 0);
  40078. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  40079. /* Length too small. */
  40080. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  40081. (cbc128_f)AES_cbc_encrypt), 0);
  40082. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  40083. return EXPECT_RESULT();
  40084. }
  40085. static int test_wolfSSL_RC4(void)
  40086. {
  40087. EXPECT_DECLS;
  40088. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  40089. WOLFSSL_RC4_KEY rc4Key;
  40090. unsigned char key[] = {
  40091. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  40092. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  40093. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  40094. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  40095. };
  40096. unsigned char data[] = {
  40097. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40098. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40099. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40100. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40101. };
  40102. unsigned char enc[sizeof(data)];
  40103. unsigned char dec[sizeof(data)];
  40104. word32 i;
  40105. word32 j;
  40106. wolfSSL_RC4_set_key(NULL, -1, NULL);
  40107. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  40108. wolfSSL_RC4_set_key(NULL, 0, NULL);
  40109. wolfSSL_RC4_set_key(NULL, -1, key);
  40110. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  40111. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  40112. wolfSSL_RC4_set_key(NULL, 0, key);
  40113. wolfSSL_RC4(NULL, 0, NULL, NULL);
  40114. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  40115. wolfSSL_RC4(NULL, 0, data, NULL);
  40116. wolfSSL_RC4(NULL, 0, NULL, enc);
  40117. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  40118. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  40119. wolfSSL_RC4(NULL, 0, data, enc);
  40120. ExpectIntEQ(1, 1);
  40121. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  40122. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  40123. XMEMSET(enc, 0, sizeof(enc));
  40124. XMEMSET(dec, 0, sizeof(dec));
  40125. /* Encrypt */
  40126. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  40127. wolfSSL_RC4(&rc4Key, j, data, enc);
  40128. /* Decrypt */
  40129. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  40130. wolfSSL_RC4(&rc4Key, j, enc, dec);
  40131. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  40132. }
  40133. }
  40134. #endif
  40135. return EXPECT_RESULT();
  40136. }
  40137. static int test_wolfSSL_OBJ(void)
  40138. {
  40139. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  40140. * mode
  40141. */
  40142. EXPECT_DECLS;
  40143. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  40144. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  40145. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  40146. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  40147. ASN1_OBJECT *obj = NULL;
  40148. ASN1_OBJECT *obj2 = NULL;
  40149. char buf[50];
  40150. XFILE fp = XBADFILE;
  40151. X509 *x509 = NULL;
  40152. X509_NAME *x509Name = NULL;
  40153. X509_NAME_ENTRY *x509NameEntry = NULL;
  40154. ASN1_OBJECT *asn1Name = NULL;
  40155. int numNames = 0;
  40156. BIO *bio = NULL;
  40157. int nid;
  40158. int i, j;
  40159. const char *f[] = {
  40160. #ifndef NO_RSA
  40161. "./certs/ca-cert.der",
  40162. #endif
  40163. #ifdef HAVE_ECC
  40164. "./certs/ca-ecc-cert.der",
  40165. "./certs/ca-ecc384-cert.der",
  40166. #endif
  40167. NULL};
  40168. ASN1_OBJECT *field_name_obj = NULL;
  40169. int lastpos = -1;
  40170. int tmp = -1;
  40171. ASN1_STRING *asn1 = NULL;
  40172. unsigned char *buf_dyn = NULL;
  40173. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  40174. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  40175. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  40176. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  40177. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  40178. ASN1_OBJECT_free(obj);
  40179. obj = NULL;
  40180. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  40181. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  40182. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  40183. #ifdef WOLFSSL_CERT_EXT
  40184. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  40185. #endif
  40186. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  40187. ExpectNotNull(obj2 = OBJ_dup(obj));
  40188. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  40189. ASN1_OBJECT_free(obj);
  40190. obj = NULL;
  40191. ASN1_OBJECT_free(obj2);
  40192. obj2 = NULL;
  40193. for (i = 0; f[i] != NULL; i++)
  40194. {
  40195. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  40196. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  40197. if (fp != XBADFILE) {
  40198. XFCLOSE(fp);
  40199. fp = XBADFILE;
  40200. }
  40201. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  40202. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  40203. /* Get the Common Name by using OBJ_txt2obj */
  40204. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  40205. do
  40206. {
  40207. lastpos = tmp;
  40208. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  40209. } while (tmp > -1);
  40210. ExpectIntNE(lastpos, -1);
  40211. ASN1_OBJECT_free(field_name_obj);
  40212. field_name_obj = NULL;
  40213. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  40214. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  40215. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  40216. /*
  40217. * All Common Names should be www.wolfssl.com
  40218. * This makes testing easier as we can test for the expected value.
  40219. */
  40220. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  40221. OPENSSL_free(buf_dyn);
  40222. buf_dyn = NULL;
  40223. bio = BIO_new(BIO_s_mem());
  40224. ExpectTrue(bio != NULL);
  40225. for (j = 0; j < numNames; j++)
  40226. {
  40227. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  40228. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  40229. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  40230. }
  40231. BIO_free(bio);
  40232. bio = NULL;
  40233. X509_free(x509);
  40234. x509 = NULL;
  40235. }
  40236. #ifdef HAVE_PKCS12
  40237. {
  40238. PKCS12 *p12 = NULL;
  40239. int boolRet;
  40240. EVP_PKEY *pkey = NULL;
  40241. const char *p12_f[] = {
  40242. #if !defined(NO_DES3) && !defined(NO_RSA)
  40243. "./certs/test-servercert.p12",
  40244. #endif
  40245. NULL};
  40246. for (i = 0; p12_f[i] != NULL; i++)
  40247. {
  40248. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  40249. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  40250. if (fp != XBADFILE) {
  40251. XFCLOSE(fp);
  40252. fp = XBADFILE;
  40253. }
  40254. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  40255. &pkey, &x509, NULL)) > 0);
  40256. wc_PKCS12_free(p12);
  40257. p12 = NULL;
  40258. EVP_PKEY_free(pkey);
  40259. x509Name = X509_get_issuer_name(x509);
  40260. ExpectNotNull(x509Name);
  40261. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  40262. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  40263. for (j = 0; j < numNames; j++)
  40264. {
  40265. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  40266. ExpectNotNull(asn1Name =
  40267. X509_NAME_ENTRY_get_object(x509NameEntry));
  40268. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  40269. }
  40270. BIO_free(bio);
  40271. bio = NULL;
  40272. X509_free(x509);
  40273. x509 = NULL;
  40274. }
  40275. }
  40276. #endif /* HAVE_PKCS12 */
  40277. #endif
  40278. return EXPECT_RESULT();
  40279. }
  40280. static int test_wolfSSL_OBJ_cmp(void)
  40281. {
  40282. EXPECT_DECLS;
  40283. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  40284. ASN1_OBJECT *obj = NULL;
  40285. ASN1_OBJECT *obj2 = NULL;
  40286. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  40287. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  40288. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  40289. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  40290. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  40291. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  40292. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  40293. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  40294. ASN1_OBJECT_free(obj);
  40295. ASN1_OBJECT_free(obj2);
  40296. #endif
  40297. return EXPECT_RESULT();
  40298. }
  40299. static int test_wolfSSL_OBJ_txt2nid(void)
  40300. {
  40301. EXPECT_DECLS;
  40302. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  40303. defined(WOLFSSL_APACHE_HTTPD)
  40304. int i;
  40305. static const struct {
  40306. const char* sn;
  40307. const char* ln;
  40308. const char* oid;
  40309. int nid;
  40310. } testVals[] = {
  40311. #ifdef WOLFSSL_APACHE_HTTPD
  40312. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  40313. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  40314. NID_id_on_dnsSRV },
  40315. { "msUPN", "Microsoft User Principal Name",
  40316. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  40317. #endif
  40318. { NULL, NULL, NULL, NID_undef }
  40319. };
  40320. /* Invalid cases */
  40321. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  40322. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  40323. /* Valid cases */
  40324. for (i = 0; testVals[i].sn != NULL; i++) {
  40325. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  40326. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  40327. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  40328. }
  40329. #endif
  40330. return EXPECT_RESULT();
  40331. }
  40332. static int test_wolfSSL_OBJ_txt2obj(void)
  40333. {
  40334. EXPECT_DECLS;
  40335. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  40336. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  40337. int i;
  40338. char buf[50];
  40339. ASN1_OBJECT* obj = NULL;
  40340. static const struct {
  40341. const char* oidStr;
  40342. const char* sn;
  40343. const char* ln;
  40344. } objs_list[] = {
  40345. #if defined(WOLFSSL_APACHE_HTTPD)
  40346. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  40347. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  40348. #endif
  40349. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  40350. { NULL, NULL, NULL }
  40351. };
  40352. static const struct {
  40353. const char* numeric;
  40354. const char* name;
  40355. } objs_named[] = {
  40356. /* In dictionary but not in normal list. */
  40357. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  40358. /* Made up OID. */
  40359. { "1.3.5.7", "1.3.5.7" },
  40360. { NULL, NULL }
  40361. };
  40362. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  40363. ASN1_OBJECT_free(obj);
  40364. obj = NULL;
  40365. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  40366. ASN1_OBJECT_free(obj);
  40367. obj = NULL;
  40368. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  40369. /* Test numerical value of oid (oidStr) */
  40370. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  40371. /* Convert object back to text to confirm oid is correct */
  40372. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  40373. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  40374. ASN1_OBJECT_free(obj);
  40375. obj = NULL;
  40376. XMEMSET(buf, 0, sizeof(buf));
  40377. /* Test short name (sn) */
  40378. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  40379. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  40380. /* Convert object back to text to confirm oid is correct */
  40381. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  40382. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  40383. ASN1_OBJECT_free(obj);
  40384. obj = NULL;
  40385. XMEMSET(buf, 0, sizeof(buf));
  40386. /* Test long name (ln) - should fail when no_name = 1 */
  40387. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  40388. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  40389. /* Convert object back to text to confirm oid is correct */
  40390. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  40391. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  40392. ASN1_OBJECT_free(obj);
  40393. obj = NULL;
  40394. XMEMSET(buf, 0, sizeof(buf));
  40395. }
  40396. for (i = 0; objs_named[i].numeric != NULL; i++) {
  40397. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  40398. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  40399. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  40400. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  40401. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  40402. ASN1_OBJECT_free(obj);
  40403. obj = NULL;
  40404. }
  40405. #endif
  40406. return EXPECT_RESULT();
  40407. }
  40408. static int test_wolfSSL_PEM_write_bio_X509(void)
  40409. {
  40410. EXPECT_DECLS;
  40411. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  40412. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  40413. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  40414. !defined(NO_FILESYSTEM)
  40415. /* This test contains the hard coded expected
  40416. * lengths. Update if necessary */
  40417. XFILE fp = XBADFILE;
  40418. WOLFSSL_EVP_PKEY *priv = NULL;
  40419. BIO* input = NULL;
  40420. BIO* output = NULL;
  40421. X509* x509a = NULL;
  40422. X509* x509b = NULL;
  40423. ASN1_TIME* notBeforeA = NULL;
  40424. ASN1_TIME* notAfterA = NULL;
  40425. #ifndef NO_ASN_TIME
  40426. ASN1_TIME* notBeforeB = NULL;
  40427. ASN1_TIME* notAfterB = NULL;
  40428. #endif
  40429. int expectedLen;
  40430. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  40431. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  40432. if (fp != XBADFILE) {
  40433. XFCLOSE(fp);
  40434. fp = XBADFILE;
  40435. }
  40436. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  40437. "rb"));
  40438. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  40439. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  40440. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  40441. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  40442. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  40443. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  40444. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40445. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40446. /* compare length against expected */
  40447. expectedLen = 2000;
  40448. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  40449. #ifndef NO_ASN_TIME
  40450. /* read exported X509 PEM back into struct, sanity check on export,
  40451. * make sure notBefore/notAfter are the same and certs are identical. */
  40452. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  40453. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  40454. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  40455. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  40456. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  40457. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  40458. X509_free(x509b);
  40459. x509b = NULL;
  40460. #endif
  40461. /* Reset output buffer */
  40462. BIO_free(output);
  40463. output = NULL;
  40464. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40465. /* Test forcing the AKID to be generated just from KeyIdentifier */
  40466. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  40467. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  40468. x509a->authKeyId = x509a->authKeyIdSrc;
  40469. x509a->authKeyIdSrc = NULL;
  40470. x509a->authKeyIdSrcSz = 0;
  40471. }
  40472. /* Resign to re-generate the der */
  40473. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  40474. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40475. /* Check that we generate a smaller output since the AKID will
  40476. * only contain the KeyIdentifier without any additional
  40477. * information */
  40478. /* Here we copy the validity struct from the original */
  40479. expectedLen = 1688;
  40480. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  40481. /* Reset buffers and x509 */
  40482. BIO_free(input);
  40483. input = NULL;
  40484. BIO_free(output);
  40485. output = NULL;
  40486. X509_free(x509a);
  40487. x509a = NULL;
  40488. /* test CA and basicConstSet values are encoded when
  40489. * the cert is a CA */
  40490. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  40491. /* read PEM into X509 struct */
  40492. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  40493. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  40494. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40495. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40496. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  40497. * values are maintained and certs are identical.*/
  40498. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  40499. ExpectIntEQ(x509b->isCa, 1);
  40500. ExpectIntEQ(x509b->basicConstSet, 1);
  40501. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  40502. X509_free(x509a);
  40503. x509a = NULL;
  40504. X509_free(x509b);
  40505. x509b = NULL;
  40506. BIO_free(input);
  40507. input = NULL;
  40508. BIO_free(output);
  40509. output = NULL;
  40510. /* test CA and basicConstSet values are encoded when
  40511. * the cert is not CA */
  40512. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  40513. /* read PEM into X509 struct */
  40514. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  40515. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  40516. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40517. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40518. /* read exported X509 PEM back into struct, ensure isCa and
  40519. * basicConstSet values are maintained and certs are identical */
  40520. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  40521. ExpectIntEQ(x509b->isCa, 0);
  40522. ExpectIntEQ(x509b->basicConstSet, 1);
  40523. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  40524. wolfSSL_EVP_PKEY_free(priv);
  40525. X509_free(x509a);
  40526. X509_free(x509b);
  40527. BIO_free(input);
  40528. BIO_free(output);
  40529. #endif
  40530. return EXPECT_RESULT();
  40531. }
  40532. static int test_wolfSSL_X509_NAME_ENTRY(void)
  40533. {
  40534. EXPECT_DECLS;
  40535. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  40536. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  40537. X509* x509 = NULL;
  40538. #ifndef NO_BIO
  40539. BIO* bio = NULL;
  40540. #endif
  40541. X509_NAME* nm = NULL;
  40542. X509_NAME_ENTRY* entry = NULL;
  40543. unsigned char cn[] = "another name to add";
  40544. #ifdef OPENSSL_ALL
  40545. int i;
  40546. int names_len = 0;
  40547. #endif
  40548. ExpectNotNull(x509 =
  40549. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  40550. #ifndef NO_BIO
  40551. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40552. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  40553. #endif
  40554. #ifdef WOLFSSL_CERT_REQ
  40555. {
  40556. X509_REQ* req = NULL;
  40557. #ifndef NO_BIO
  40558. BIO* bReq = NULL;
  40559. #endif
  40560. ExpectNotNull(req =
  40561. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  40562. #ifndef NO_BIO
  40563. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  40564. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  40565. BIO_free(bReq);
  40566. #endif
  40567. X509_free(req);
  40568. }
  40569. #endif
  40570. ExpectNotNull(nm = X509_get_subject_name(x509));
  40571. /* Test add entry */
  40572. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  40573. 0x0c, cn, (int)sizeof(cn)));
  40574. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  40575. #ifdef WOLFSSL_CERT_EXT
  40576. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  40577. (byte*)"support@wolfssl.com", 19, -1,
  40578. 1), WOLFSSL_SUCCESS);
  40579. #endif
  40580. X509_NAME_ENTRY_free(entry);
  40581. entry = NULL;
  40582. #ifdef WOLFSSL_CERT_REQ
  40583. {
  40584. unsigned char srv_pkcs9p[] = "Server";
  40585. unsigned char fvrtDrnk[] = "tequila";
  40586. unsigned char* der = NULL;
  40587. char* subject = NULL;
  40588. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  40589. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  40590. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  40591. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  40592. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  40593. ExpectNotNull(der);
  40594. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  40595. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  40596. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  40597. #ifdef DEBUG_WOLFSSL
  40598. if (subject != NULL) {
  40599. fprintf(stderr, "\n\t%s\n", subject);
  40600. }
  40601. #endif
  40602. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  40603. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  40604. }
  40605. #endif
  40606. /* Test add entry by text */
  40607. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  40608. 0x0c, cn, (int)sizeof(cn)));
  40609. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  40610. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  40611. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  40612. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  40613. #endif
  40614. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  40615. X509_NAME_ENTRY_free(entry);
  40616. entry = NULL;
  40617. /* Test add entry by NID */
  40618. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  40619. cn, -1, -1, 0), SSL_SUCCESS);
  40620. #ifdef OPENSSL_ALL
  40621. /* stack of name entry */
  40622. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  40623. for (i = 0; i < names_len; i++) {
  40624. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  40625. }
  40626. #endif
  40627. #ifndef NO_BIO
  40628. BIO_free(bio);
  40629. #endif
  40630. X509_free(x509); /* free's nm */
  40631. #endif
  40632. return EXPECT_RESULT();
  40633. }
  40634. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  40635. static int test_GENERAL_NAME_set0_othername(void) {
  40636. EXPECT_DECLS;
  40637. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40638. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  40639. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  40640. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  40641. defined(WOLFSSL_FPKI)
  40642. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  40643. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  40644. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  40645. const char * cert_fname = "./certs/server-cert.der";
  40646. const char * key_fname = "./certs/server-key.der";
  40647. X509* x509 = NULL;
  40648. GENERAL_NAME* gn = NULL;
  40649. GENERAL_NAMES* gns = NULL;
  40650. ASN1_OBJECT* upn_oid = NULL;
  40651. ASN1_UTF8STRING *utf8str = NULL;
  40652. ASN1_TYPE *value = NULL;
  40653. X509_EXTENSION * ext = NULL;
  40654. byte* pt = NULL;
  40655. byte der[4096];
  40656. int derSz = 0;
  40657. EVP_PKEY* priv = NULL;
  40658. XFILE f = XBADFILE;
  40659. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  40660. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  40661. if (f != XBADFILE) {
  40662. XFCLOSE(f);
  40663. f = XBADFILE;
  40664. }
  40665. ExpectNotNull(gn = GENERAL_NAME_new());
  40666. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  40667. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  40668. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  40669. ExpectNotNull(value = ASN1_TYPE_new());
  40670. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  40671. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  40672. wolfSSL_ASN1_STRING_free(utf8str);
  40673. }
  40674. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  40675. if (EXPECT_FAIL()) {
  40676. ASN1_TYPE_free(value);
  40677. }
  40678. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  40679. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  40680. if (EXPECT_FAIL()) {
  40681. GENERAL_NAME_free(gn);
  40682. gn = NULL;
  40683. }
  40684. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  40685. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  40686. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  40687. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  40688. if (f != XBADFILE) {
  40689. XFCLOSE(f);
  40690. f = XBADFILE;
  40691. }
  40692. pt = der;
  40693. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40694. (const unsigned char**)&pt, derSz));
  40695. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  40696. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40697. gns = NULL;
  40698. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40699. NULL));
  40700. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  40701. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  40702. ExpectIntEQ(gn->type, 0);
  40703. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40704. ASN1_OBJECT_free(upn_oid);
  40705. X509_EXTENSION_free(ext);
  40706. X509_free(x509);
  40707. EVP_PKEY_free(priv);
  40708. #endif
  40709. return EXPECT_RESULT();
  40710. }
  40711. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  40712. static int test_othername_and_SID_ext(void) {
  40713. EXPECT_DECLS;
  40714. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40715. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  40716. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  40717. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  40718. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  40719. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  40720. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  40721. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  40722. const char* csr_fname = "./certs/csr.signed.der";
  40723. const char* key_fname = "./certs/server-key.der";
  40724. byte der[4096];
  40725. int derSz = 0;
  40726. X509_REQ* x509 = NULL;
  40727. STACK_OF(X509_EXTENSION) *exts = NULL;
  40728. X509_EXTENSION * san_ext = NULL;
  40729. X509_EXTENSION * ext = NULL;
  40730. GENERAL_NAME* gn = NULL;
  40731. GENERAL_NAMES* gns = NULL;
  40732. ASN1_OBJECT* upn_oid = NULL;
  40733. ASN1_UTF8STRING *utf8str = NULL;
  40734. ASN1_TYPE *value = NULL;
  40735. ASN1_STRING *extval = NULL;
  40736. /* SID extension. SID data format explained here:
  40737. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  40738. */
  40739. byte SidExtension[] = {
  40740. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  40741. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  40742. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  40743. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  40744. byte expectedAltName[] = {
  40745. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  40746. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  40747. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  40748. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  40749. X509_EXTENSION *sid_ext = NULL;
  40750. ASN1_OBJECT* sid_oid = NULL;
  40751. ASN1_OCTET_STRING *sid_data = NULL;
  40752. EVP_PKEY* priv = NULL;
  40753. XFILE f = XBADFILE;
  40754. byte* pt = NULL;
  40755. BIO* bio = NULL;
  40756. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  40757. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  40758. if (f != XBADFILE) {
  40759. XFCLOSE(f);
  40760. f = XBADFILE;
  40761. }
  40762. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  40763. ExpectNotNull(gn = GENERAL_NAME_new());
  40764. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  40765. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  40766. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  40767. ExpectNotNull(value = ASN1_TYPE_new());
  40768. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  40769. if (EXPECT_FAIL()) {
  40770. ASN1_UTF8STRING_free(utf8str);
  40771. }
  40772. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  40773. if (EXPECT_FAIL()) {
  40774. ASN1_TYPE_free(value);
  40775. GENERAL_NAME_free(gn);
  40776. gn = NULL;
  40777. }
  40778. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  40779. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  40780. if (EXPECT_FAIL()) {
  40781. GENERAL_NAME_free(gn);
  40782. }
  40783. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  40784. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  40785. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  40786. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  40787. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  40788. sid_data));
  40789. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  40790. /* Ensure an empty stack doesn't raise an error. */
  40791. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40792. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  40793. if (EXPECT_FAIL()) {
  40794. X509_EXTENSION_free(san_ext);
  40795. }
  40796. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  40797. if (EXPECT_FAIL()) {
  40798. X509_EXTENSION_free(sid_ext);
  40799. }
  40800. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40801. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  40802. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  40803. if (f != XBADFILE)
  40804. XFCLOSE(f);
  40805. pt = der;
  40806. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40807. (const unsigned char**)&pt, derSz));
  40808. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  40809. pt = der;
  40810. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  40811. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40812. gns = NULL;
  40813. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40814. exts = NULL;
  40815. ASN1_OBJECT_free(upn_oid);
  40816. ASN1_OBJECT_free(sid_oid);
  40817. ASN1_OCTET_STRING_free(sid_data);
  40818. X509_REQ_free(x509);
  40819. x509 = NULL;
  40820. EVP_PKEY_free(priv);
  40821. /* At this point everything used to generate what is in der is cleaned up.
  40822. * We now read back from der to confirm the extensions were inserted
  40823. * correctly. */
  40824. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  40825. ExpectNotNull(bio);
  40826. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  40827. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  40828. ExpectNotNull(x509);
  40829. BIO_free(bio);
  40830. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  40831. x509));
  40832. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  40833. /* Check the SID extension. */
  40834. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  40835. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40836. ExpectIntEQ(extval->length, sizeof(SidExtension));
  40837. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  40838. /* Check the AltNames extension. */
  40839. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  40840. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40841. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  40842. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  40843. 0);
  40844. /* Cleanup */
  40845. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40846. NULL));
  40847. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40848. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  40849. ExpectIntEQ(gn->type, 0);
  40850. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40851. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40852. X509_REQ_free(x509);
  40853. #endif
  40854. return EXPECT_RESULT();
  40855. }
  40856. static int test_wolfSSL_X509_set_name(void)
  40857. {
  40858. EXPECT_DECLS;
  40859. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40860. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40861. X509* x509 = NULL;
  40862. X509_NAME* name = NULL;
  40863. ExpectNotNull(name = X509_NAME_new());
  40864. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  40865. (byte*)"wolfssl.com", 11, 0, 1),
  40866. WOLFSSL_SUCCESS);
  40867. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  40868. (byte*)"support@wolfssl.com", 19, -1,
  40869. 1), WOLFSSL_SUCCESS);
  40870. ExpectNotNull(x509 = X509_new());
  40871. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  40872. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  40873. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  40874. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  40875. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  40876. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  40877. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  40878. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  40879. X509_free(x509);
  40880. X509_NAME_free(name);
  40881. #endif /* OPENSSL_ALL && !NO_CERTS */
  40882. return EXPECT_RESULT();
  40883. }
  40884. static int test_wolfSSL_X509_set_notAfter(void)
  40885. {
  40886. EXPECT_DECLS;
  40887. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40888. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40889. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40890. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  40891. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  40892. /* Generalized time will overflow time_t if not long */
  40893. X509* x = NULL;
  40894. BIO* bio = NULL;
  40895. ASN1_TIME *asn_time = NULL;
  40896. ASN1_TIME *time_check = NULL;
  40897. const int year = 365*24*60*60;
  40898. const int day = 24*60*60;
  40899. const int hour = 60*60;
  40900. const int mini = 60;
  40901. int offset_day;
  40902. unsigned char buf[25];
  40903. time_t t;
  40904. /*
  40905. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40906. */
  40907. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  40908. offset_day = 7;
  40909. /*
  40910. * Free these.
  40911. */
  40912. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40913. ExpectNotNull(asn_time);
  40914. ExpectNotNull(x = X509_new());
  40915. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40916. /*
  40917. * Tests
  40918. */
  40919. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  40920. /* time_check is simply (ANS1_TIME*)x->notAfter */
  40921. ExpectNotNull(time_check = X509_get_notAfter(x));
  40922. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40923. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40924. /* Convert to human readable format and compare to intended date */
  40925. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40926. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40927. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  40928. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  40929. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  40930. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  40931. /*
  40932. * Cleanup
  40933. */
  40934. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40935. X509_free(x);
  40936. BIO_free(bio);
  40937. #endif
  40938. return EXPECT_RESULT();
  40939. }
  40940. static int test_wolfSSL_X509_set_notBefore(void)
  40941. {
  40942. EXPECT_DECLS;
  40943. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40944. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40945. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40946. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  40947. X509* x = NULL;
  40948. BIO* bio = NULL;
  40949. ASN1_TIME *asn_time = NULL;
  40950. ASN1_TIME *time_check = NULL;
  40951. const int year = 365*24*60*60;
  40952. const int day = 24*60*60;
  40953. const int hour = 60*60;
  40954. const int mini = 60;
  40955. int offset_day;
  40956. unsigned char buf[25];
  40957. time_t t;
  40958. /*
  40959. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40960. */
  40961. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  40962. offset_day = 7;
  40963. /*
  40964. * Free these.
  40965. */
  40966. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40967. ExpectNotNull(asn_time);
  40968. ExpectNotNull(x = X509_new());
  40969. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40970. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  40971. /*
  40972. * Main Tests
  40973. */
  40974. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  40975. /* time_check == (ANS1_TIME*)x->notBefore */
  40976. ExpectNotNull(time_check = X509_get_notBefore(x));
  40977. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40978. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40979. /* Convert to human readable format and compare to intended date */
  40980. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40981. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40982. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  40983. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  40984. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  40985. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  40986. /*
  40987. * Cleanup
  40988. */
  40989. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40990. X509_free(x);
  40991. BIO_free(bio);
  40992. #endif
  40993. return EXPECT_RESULT();
  40994. }
  40995. static int test_wolfSSL_X509_set_version(void)
  40996. {
  40997. EXPECT_DECLS;
  40998. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  40999. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  41000. X509* x509 = NULL;
  41001. long v = 2L;
  41002. long maxInt = INT_MAX;
  41003. ExpectNotNull(x509 = X509_new());
  41004. /* These should pass. */
  41005. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  41006. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  41007. /* Fail Case: When v(long) is greater than x509->version(int). */
  41008. v = maxInt+1;
  41009. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  41010. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  41011. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  41012. /* Cleanup */
  41013. X509_free(x509);
  41014. #endif
  41015. return EXPECT_RESULT();
  41016. }
  41017. #ifndef NO_BIO
  41018. static int test_wolfSSL_BIO_gets(void)
  41019. {
  41020. EXPECT_DECLS;
  41021. #if defined(OPENSSL_EXTRA)
  41022. BIO* bio = NULL;
  41023. BIO* bio2 = NULL;
  41024. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  41025. char emp[] = "";
  41026. char bio_buffer[20];
  41027. int bufferSz = 20;
  41028. #ifdef OPENSSL_ALL
  41029. BUF_MEM* emp_bm = NULL;
  41030. BUF_MEM* msg_bm = NULL;
  41031. #endif
  41032. /* try with bad args */
  41033. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  41034. #ifdef OPENSSL_ALL
  41035. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  41036. #endif
  41037. /* try with real msg */
  41038. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  41039. XMEMSET(bio_buffer, 0, bufferSz);
  41040. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  41041. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  41042. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  41043. ExpectFalse(bio2 != BIO_next(bio));
  41044. /* make buffer filled with no terminating characters */
  41045. XMEMSET(bio_buffer, 1, bufferSz);
  41046. /* BIO_gets reads a line of data */
  41047. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  41048. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  41049. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  41050. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  41051. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  41052. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  41053. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  41054. #ifdef OPENSSL_ALL
  41055. /* test setting the mem_buf manually */
  41056. BIO_free(bio);
  41057. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  41058. ExpectNotNull(emp_bm = BUF_MEM_new());
  41059. ExpectNotNull(msg_bm = BUF_MEM_new());
  41060. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  41061. if (EXPECT_SUCCESS()) {
  41062. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  41063. msg_bm->data = NULL;
  41064. }
  41065. /* emp size is 1 for terminator */
  41066. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  41067. if (EXPECT_SUCCESS()) {
  41068. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  41069. emp_bm->data = emp;
  41070. msg_bm->data = msg;
  41071. }
  41072. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  41073. /* check reading an empty string */
  41074. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  41075. ExpectStrEQ(emp, bio_buffer);
  41076. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  41077. /* BIO_gets reads a line of data */
  41078. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  41079. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  41080. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  41081. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  41082. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  41083. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  41084. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  41085. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  41086. if (EXPECT_SUCCESS())
  41087. emp_bm->data = NULL;
  41088. BUF_MEM_free(emp_bm);
  41089. if (EXPECT_SUCCESS())
  41090. msg_bm->data = NULL;
  41091. BUF_MEM_free(msg_bm);
  41092. #endif
  41093. /* check not null terminated string */
  41094. BIO_free(bio);
  41095. bio = NULL;
  41096. msg[0] = 0x33;
  41097. msg[1] = 0x33;
  41098. msg[2] = 0x33;
  41099. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  41100. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  41101. ExpectIntEQ(bio_buffer[0], msg[0]);
  41102. ExpectIntEQ(bio_buffer[1], msg[1]);
  41103. ExpectIntNE(bio_buffer[2], msg[2]);
  41104. BIO_free(bio);
  41105. bio = NULL;
  41106. msg[3] = 0x33;
  41107. bio_buffer[3] = 0x33;
  41108. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  41109. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  41110. ExpectIntEQ(bio_buffer[0], msg[0]);
  41111. ExpectIntEQ(bio_buffer[1], msg[1]);
  41112. ExpectIntEQ(bio_buffer[2], msg[2]);
  41113. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  41114. /* check reading an empty string */
  41115. BIO_free(bio);
  41116. bio = NULL;
  41117. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  41118. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  41119. ExpectStrEQ(emp, bio_buffer);
  41120. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  41121. /* check error cases */
  41122. BIO_free(bio);
  41123. bio = NULL;
  41124. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  41125. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41126. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  41127. #if !defined(NO_FILESYSTEM)
  41128. {
  41129. BIO* f_bio = NULL;
  41130. XFILE f = XBADFILE;
  41131. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  41132. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  41133. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  41134. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  41135. if (EXPECT_FAIL() && (f != XBADFILE)) {
  41136. XFCLOSE(f);
  41137. }
  41138. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  41139. BIO_free(f_bio);
  41140. f_bio = NULL;
  41141. }
  41142. #endif /* NO_FILESYSTEM */
  41143. BIO_free(bio);
  41144. bio = NULL;
  41145. BIO_free(bio2);
  41146. bio2 = NULL;
  41147. /* try with type BIO */
  41148. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  41149. sizeof(msg));
  41150. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  41151. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  41152. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  41153. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  41154. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  41155. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  41156. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  41157. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  41158. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  41159. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  41160. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  41161. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  41162. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  41163. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  41164. BIO_free(bio);
  41165. bio = NULL;
  41166. BIO_free(bio2);
  41167. bio2 = NULL;
  41168. /* check reading an empty string */
  41169. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  41170. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  41171. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  41172. ExpectStrEQ(emp, bio_buffer);
  41173. BIO_free(bio);
  41174. bio = NULL;
  41175. #endif
  41176. return EXPECT_RESULT();
  41177. }
  41178. static int test_wolfSSL_BIO_puts(void)
  41179. {
  41180. EXPECT_DECLS;
  41181. #if defined(OPENSSL_EXTRA)
  41182. BIO* bio = NULL;
  41183. char input[] = "hello\0world\n.....ok\n\0";
  41184. char output[128];
  41185. XMEMSET(output, 0, sizeof(output));
  41186. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41187. ExpectIntEQ(BIO_puts(bio, input), 5);
  41188. ExpectIntEQ(BIO_pending(bio), 5);
  41189. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  41190. ExpectIntEQ(BIO_pending(bio), 19);
  41191. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  41192. ExpectStrEQ(output, "helloworld\n");
  41193. ExpectIntEQ(BIO_pending(bio), 8);
  41194. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  41195. ExpectStrEQ(output, ".....ok\n");
  41196. ExpectIntEQ(BIO_pending(bio), 0);
  41197. ExpectIntEQ(BIO_puts(bio, ""), -1);
  41198. BIO_free(bio);
  41199. #endif
  41200. return EXPECT_RESULT();
  41201. }
  41202. static int test_wolfSSL_BIO_dump(void)
  41203. {
  41204. EXPECT_DECLS;
  41205. #if defined(OPENSSL_EXTRA)
  41206. BIO* bio;
  41207. static const unsigned char data[] = {
  41208. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  41209. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  41210. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  41211. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  41212. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  41213. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  41214. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  41215. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  41216. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  41217. 0xB4
  41218. };
  41219. /* Generated with OpenSSL. */
  41220. static const char expected[] =
  41221. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  41222. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  41223. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  41224. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  41225. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  41226. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  41227. static const char expectedAll[] =
  41228. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  41229. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  41230. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  41231. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  41232. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  41233. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  41234. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  41235. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  41236. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  41237. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  41238. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  41239. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  41240. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  41241. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  41242. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  41243. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  41244. char output[16 * 80];
  41245. int i;
  41246. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41247. /* Example key dumped. */
  41248. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  41249. sizeof(expected) - 1);
  41250. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  41251. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  41252. /* Try every possible value for a character. */
  41253. for (i = 0; i < 256; i++)
  41254. output[i] = i;
  41255. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  41256. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  41257. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  41258. BIO_free(bio);
  41259. #endif
  41260. return EXPECT_RESULT();
  41261. }
  41262. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41263. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  41264. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  41265. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  41266. {
  41267. (void)ssl;
  41268. (void)buf;
  41269. (void)sz;
  41270. (void)ctx;
  41271. return WOLFSSL_CBIO_ERR_WANT_READ;
  41272. }
  41273. #endif
  41274. static int test_wolfSSL_BIO_should_retry(void)
  41275. {
  41276. EXPECT_DECLS;
  41277. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41278. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  41279. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  41280. tcp_ready ready;
  41281. func_args server_args;
  41282. THREAD_TYPE serverThread;
  41283. SOCKET_T sockfd = 0;
  41284. WOLFSSL_CTX* ctx = NULL;
  41285. WOLFSSL* ssl = NULL;
  41286. char msg[64] = "hello wolfssl!";
  41287. char reply[1024];
  41288. int msgSz = (int)XSTRLEN(msg);
  41289. int ret;
  41290. BIO* bio = NULL;
  41291. XMEMSET(&server_args, 0, sizeof(func_args));
  41292. #ifdef WOLFSSL_TIRTOS
  41293. fdOpenSession(Task_self());
  41294. #endif
  41295. StartTCP();
  41296. InitTcpReady(&ready);
  41297. #if defined(USE_WINDOWS_API)
  41298. /* use RNG to get random port if using windows */
  41299. ready.port = GetRandomPort();
  41300. #endif
  41301. server_args.signal = &ready;
  41302. start_thread(test_server_nofail, &server_args, &serverThread);
  41303. wait_tcp_ready(&server_args);
  41304. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41305. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  41306. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  41307. #endif
  41308. ExpectIntEQ(WOLFSSL_SUCCESS,
  41309. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  41310. ExpectIntEQ(WOLFSSL_SUCCESS,
  41311. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  41312. ExpectIntEQ(WOLFSSL_SUCCESS,
  41313. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  41314. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  41315. /* force retry */
  41316. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  41317. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  41318. ExpectNotNull(ssl);
  41319. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  41320. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  41321. if (EXPECT_FAIL()) {
  41322. wolfSSL_free(ssl);
  41323. ssl = NULL;
  41324. }
  41325. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  41326. ExpectIntNE(BIO_should_retry(bio), 0);
  41327. ExpectIntEQ(BIO_should_read(bio), 0);
  41328. ExpectIntEQ(BIO_should_write(bio), 0);
  41329. /* now perform successful connection */
  41330. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  41331. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  41332. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  41333. ret = wolfSSL_get_error(ssl, -1);
  41334. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  41335. ExpectIntNE(BIO_should_retry(bio), 0);
  41336. if (ret == WOLFSSL_ERROR_WANT_READ)
  41337. ExpectIntEQ(BIO_should_read(bio), 1);
  41338. else
  41339. ExpectIntEQ(BIO_should_read(bio), 0);
  41340. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  41341. ExpectIntEQ(BIO_should_write(bio), 1);
  41342. else
  41343. ExpectIntEQ(BIO_should_write(bio), 0);
  41344. }
  41345. else {
  41346. ExpectIntEQ(BIO_should_retry(bio), 0);
  41347. ExpectIntEQ(BIO_should_read(bio), 0);
  41348. ExpectIntEQ(BIO_should_write(bio), 0);
  41349. }
  41350. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  41351. XSTRLEN("I hear you fa shizzle!")), 0);
  41352. BIO_free(bio);
  41353. wolfSSL_CTX_free(ctx);
  41354. CloseSocket(sockfd);
  41355. join_thread(serverThread);
  41356. FreeTcpReady(&ready);
  41357. #ifdef WOLFSSL_TIRTOS
  41358. fdOpenSession(Task_self());
  41359. #endif
  41360. #endif
  41361. return EXPECT_RESULT();
  41362. }
  41363. static int test_wolfSSL_BIO_connect(void)
  41364. {
  41365. EXPECT_DECLS;
  41366. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41367. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  41368. tcp_ready ready;
  41369. func_args server_args;
  41370. THREAD_TYPE serverThread;
  41371. BIO *tcpBio = NULL;
  41372. BIO *sslBio = NULL;
  41373. SSL_CTX* ctx = NULL;
  41374. SSL *ssl = NULL;
  41375. SSL *sslPtr;
  41376. char msg[] = "hello wolfssl!";
  41377. char reply[30];
  41378. char buff[10] = {0};
  41379. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41380. ExpectIntEQ(WOLFSSL_SUCCESS,
  41381. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  41382. ExpectIntEQ(WOLFSSL_SUCCESS,
  41383. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  41384. ExpectIntEQ(WOLFSSL_SUCCESS,
  41385. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  41386. /* Setup server */
  41387. XMEMSET(&server_args, 0, sizeof(func_args));
  41388. StartTCP();
  41389. InitTcpReady(&ready);
  41390. #if defined(USE_WINDOWS_API)
  41391. /* use RNG to get random port if using windows */
  41392. ready.port = GetRandomPort();
  41393. #endif
  41394. server_args.signal = &ready;
  41395. start_thread(test_server_nofail, &server_args, &serverThread);
  41396. wait_tcp_ready(&server_args);
  41397. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  41398. /* Start the test proper */
  41399. /* Setup the TCP BIO */
  41400. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  41401. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  41402. /* Setup the SSL object */
  41403. ExpectNotNull(ssl = SSL_new(ctx));
  41404. SSL_set_connect_state(ssl);
  41405. /* Setup the SSL BIO */
  41406. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  41407. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  41408. if (EXPECT_FAIL()) {
  41409. wolfSSL_free(ssl);
  41410. }
  41411. /* Verify that BIO_get_ssl works. */
  41412. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  41413. ExpectPtrEq(ssl, sslPtr);
  41414. /* Link BIO's so that sslBio uses tcpBio for IO */
  41415. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  41416. /* Do TCP connect */
  41417. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  41418. /* Do TLS handshake */
  41419. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  41420. /* Test writing */
  41421. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  41422. /* Expect length of default wolfSSL reply */
  41423. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  41424. /* Clean it all up */
  41425. BIO_free_all(sslBio);
  41426. /* Server clean up */
  41427. join_thread(serverThread);
  41428. FreeTcpReady(&ready);
  41429. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  41430. * after. */
  41431. XMEMSET(&server_args, 0, sizeof(func_args));
  41432. StartTCP();
  41433. InitTcpReady(&ready);
  41434. #if defined(USE_WINDOWS_API)
  41435. /* use RNG to get random port if using windows */
  41436. ready.port = GetRandomPort();
  41437. #endif
  41438. server_args.signal = &ready;
  41439. start_thread(test_server_nofail, &server_args, &serverThread);
  41440. wait_tcp_ready(&server_args);
  41441. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  41442. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  41443. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  41444. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  41445. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  41446. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  41447. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  41448. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  41449. /* Attempt to close the TLS connection gracefully. */
  41450. BIO_ssl_shutdown(sslBio);
  41451. BIO_free_all(sslBio);
  41452. join_thread(serverThread);
  41453. FreeTcpReady(&ready);
  41454. SSL_CTX_free(ctx);
  41455. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  41456. wc_ecc_fp_free(); /* free per thread cache */
  41457. #endif
  41458. #endif
  41459. return EXPECT_RESULT();
  41460. }
  41461. static int test_wolfSSL_BIO_tls(void)
  41462. {
  41463. EXPECT_DECLS;
  41464. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  41465. SSL_CTX* ctx = NULL;
  41466. SSL *ssl = NULL;
  41467. BIO *readBio = NULL;
  41468. BIO *writeBio = NULL;
  41469. int ret;
  41470. int err = 0;
  41471. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  41472. ExpectNotNull(ssl = SSL_new(ctx));
  41473. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  41474. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  41475. /* Qt reads data from write-bio,
  41476. * then writes the read data into plain packet.
  41477. * Qt reads data from plain packet,
  41478. * then writes the read data into read-bio.
  41479. */
  41480. SSL_set_bio(ssl, readBio, writeBio);
  41481. do {
  41482. #ifdef WOLFSSL_ASYNC_CRYPT
  41483. if (err == WC_PENDING_E) {
  41484. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41485. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41486. }
  41487. #endif
  41488. ret = SSL_connect(ssl);
  41489. err = SSL_get_error(ssl, 0);
  41490. } while (err == WC_PENDING_E);
  41491. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  41492. /* in this use case, should return WANT READ
  41493. * so that Qt will read the data from plain packet for next state.
  41494. */
  41495. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  41496. SSL_free(ssl);
  41497. SSL_CTX_free(ctx);
  41498. #endif
  41499. return EXPECT_RESULT();
  41500. }
  41501. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41502. defined(HAVE_HTTP_CLIENT)
  41503. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  41504. {
  41505. BIO* clientBio;
  41506. SSL* sslClient;
  41507. SSL_CTX* ctx;
  41508. char connectAddr[20]; /* IP + port */;
  41509. (void)args;
  41510. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  41511. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  41512. AssertIntEQ(BIO_do_connect(clientBio), 1);
  41513. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  41514. AssertNotNull(sslClient = SSL_new(ctx));
  41515. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  41516. SSL_set_bio(sslClient, clientBio, clientBio);
  41517. AssertIntEQ(SSL_connect(sslClient), 1);
  41518. SSL_free(sslClient);
  41519. SSL_CTX_free(ctx);
  41520. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  41521. wc_ecc_fp_free(); /* free per thread cache */
  41522. #endif
  41523. WOLFSSL_RETURN_FROM_THREAD(0);
  41524. }
  41525. #endif
  41526. static int test_wolfSSL_BIO_accept(void)
  41527. {
  41528. EXPECT_DECLS;
  41529. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41530. defined(HAVE_HTTP_CLIENT)
  41531. BIO* serverBindBio = NULL;
  41532. BIO* serverAcceptBio = NULL;
  41533. SSL* sslServer = NULL;
  41534. SSL_CTX* ctx = NULL;
  41535. func_args args;
  41536. THREAD_TYPE thread;
  41537. char port[10]; /* 10 bytes should be enough to store the string
  41538. * representation of the port */
  41539. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  41540. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  41541. /* First BIO_do_accept binds the port */
  41542. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  41543. XMEMSET(&args, 0, sizeof(func_args));
  41544. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  41545. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  41546. /* Let's plug it into SSL to test */
  41547. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  41548. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  41549. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  41550. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  41551. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  41552. ExpectNotNull(sslServer = SSL_new(ctx));
  41553. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  41554. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  41555. ExpectIntEQ(SSL_accept(sslServer), 1);
  41556. join_thread(thread);
  41557. BIO_free(serverBindBio);
  41558. SSL_free(sslServer);
  41559. SSL_CTX_free(ctx);
  41560. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  41561. wc_ecc_fp_free(); /* free per thread cache */
  41562. #endif
  41563. #endif
  41564. return EXPECT_RESULT();
  41565. }
  41566. static int test_wolfSSL_BIO_write(void)
  41567. {
  41568. EXPECT_DECLS;
  41569. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  41570. BIO* bio = NULL;
  41571. BIO* bio64 = NULL;
  41572. BIO* bio_mem = NULL;
  41573. BIO* ptr = NULL;
  41574. int sz;
  41575. char msg[] = "conversion test";
  41576. char out[40];
  41577. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  41578. void* bufPtr = NULL;
  41579. BUF_MEM* buf = NULL;
  41580. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  41581. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  41582. if (EXPECT_FAIL()) {
  41583. BIO_free(bio64);
  41584. }
  41585. /* now should convert to base64 then write to memory */
  41586. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41587. BIO_flush(bio);
  41588. /* test BIO chain */
  41589. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  41590. ExpectNotNull(buf);
  41591. ExpectIntEQ(buf->length, 25);
  41592. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  41593. ExpectPtrEq(buf->data, bufPtr);
  41594. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  41595. sz = sizeof(out);
  41596. XMEMSET(out, 0, sz);
  41597. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  41598. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  41599. /* write then read should return the same message */
  41600. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41601. sz = sizeof(out);
  41602. XMEMSET(out, 0, sz);
  41603. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  41604. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41605. /* now try encoding with no line ending */
  41606. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  41607. #ifdef HAVE_EX_DATA
  41608. BIO_set_ex_data(bio64, 0, (void*) "data");
  41609. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  41610. #endif
  41611. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41612. BIO_flush(bio);
  41613. sz = sizeof(out);
  41614. XMEMSET(out, 0, sz);
  41615. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  41616. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  41617. BIO_free_all(bio); /* frees bio64 also */
  41618. bio = NULL;
  41619. /* test with more than one bio64 in list */
  41620. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  41621. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  41622. if (EXPECT_FAIL()) {
  41623. BIO_free(bio64);
  41624. bio64 = NULL;
  41625. }
  41626. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  41627. ExpectNotNull(BIO_push(bio64, bio_mem));
  41628. if (EXPECT_FAIL()) {
  41629. BIO_free(bio_mem);
  41630. }
  41631. /* now should convert to base64 when stored and then decode with read */
  41632. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  41633. BIO_flush(bio);
  41634. sz = sizeof(out);
  41635. XMEMSET(out, 0, sz);
  41636. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  41637. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  41638. BIO_clear_flags(bio64, ~0);
  41639. BIO_set_retry_read(bio);
  41640. BIO_free_all(bio); /* frees bio64s also */
  41641. bio = NULL;
  41642. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  41643. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41644. BIO_free(bio);
  41645. #endif
  41646. return EXPECT_RESULT();
  41647. }
  41648. static int test_wolfSSL_BIO_printf(void)
  41649. {
  41650. EXPECT_DECLS;
  41651. #if defined(OPENSSL_ALL)
  41652. BIO* bio = NULL;
  41653. int sz = 7;
  41654. char msg[] = "TLS 1.3 for the world";
  41655. char out[60];
  41656. char expected[] = "TLS 1.3 for the world : sz = 7";
  41657. XMEMSET(out, 0, sizeof(out));
  41658. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41659. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  41660. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  41661. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  41662. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  41663. BIO_free(bio);
  41664. #endif
  41665. return EXPECT_RESULT();
  41666. }
  41667. static int test_wolfSSL_BIO_f_md(void)
  41668. {
  41669. EXPECT_DECLS;
  41670. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  41671. BIO* bio = NULL;
  41672. BIO* mem = NULL;
  41673. char msg[] = "message to hash";
  41674. char out[60];
  41675. EVP_MD_CTX* ctx = NULL;
  41676. const unsigned char testKey[] =
  41677. {
  41678. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  41679. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  41680. 0x0b, 0x0b, 0x0b, 0x0b
  41681. };
  41682. const char testData[] = "Hi There";
  41683. const unsigned char testResult[] =
  41684. {
  41685. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  41686. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  41687. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  41688. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  41689. };
  41690. const unsigned char expectedHash[] =
  41691. {
  41692. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  41693. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  41694. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  41695. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  41696. };
  41697. const unsigned char emptyHash[] =
  41698. {
  41699. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  41700. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  41701. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  41702. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  41703. };
  41704. unsigned char check[sizeof(testResult) + 1];
  41705. size_t checkSz = -1;
  41706. EVP_PKEY* key = NULL;
  41707. XMEMSET(out, 0, sizeof(out));
  41708. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41709. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  41710. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  41711. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  41712. /* should not be able to write/read yet since just digest wrapper and no
  41713. * data is passing through the bio */
  41714. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  41715. ExpectIntEQ(BIO_pending(bio), 0);
  41716. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  41717. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  41718. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41719. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  41720. BIO_reset(bio);
  41721. /* append BIO mem to bio in order to read/write */
  41722. ExpectNotNull(bio = BIO_push(bio, mem));
  41723. XMEMSET(out, 0, sizeof(out));
  41724. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  41725. ExpectIntEQ(BIO_pending(bio), 16);
  41726. /* this just reads the message and does not hash it (gets calls final) */
  41727. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  41728. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41729. /* create a message digest using BIO */
  41730. XMEMSET(out, 0, sizeof(out));
  41731. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  41732. ExpectIntEQ(BIO_pending(mem), 16);
  41733. ExpectIntEQ(BIO_pending(bio), 16);
  41734. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41735. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  41736. BIO_free(bio);
  41737. bio = NULL;
  41738. BIO_free(mem);
  41739. mem = NULL;
  41740. /* test with HMAC */
  41741. XMEMSET(out, 0, sizeof(out));
  41742. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41743. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  41744. BIO_get_md_ctx(bio, &ctx);
  41745. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  41746. (int)sizeof(testKey)));
  41747. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  41748. ExpectNotNull(bio = BIO_push(bio, mem));
  41749. BIO_write(bio, testData, (int)strlen(testData));
  41750. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  41751. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  41752. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  41753. EVP_PKEY_free(key);
  41754. BIO_free(bio);
  41755. BIO_free(mem);
  41756. #endif
  41757. return EXPECT_RESULT();
  41758. }
  41759. static int test_wolfSSL_BIO_up_ref(void)
  41760. {
  41761. EXPECT_DECLS;
  41762. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41763. BIO* bio = NULL;
  41764. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41765. ExpectIntEQ(BIO_up_ref(NULL), 0);
  41766. ExpectIntEQ(BIO_up_ref(bio), 1);
  41767. BIO_free(bio);
  41768. ExpectIntEQ(BIO_up_ref(bio), 1);
  41769. BIO_free(bio);
  41770. BIO_free(bio);
  41771. #endif
  41772. return EXPECT_RESULT();
  41773. }
  41774. static int test_wolfSSL_BIO_reset(void)
  41775. {
  41776. EXPECT_DECLS;
  41777. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41778. BIO* bio = NULL;
  41779. byte buf[16];
  41780. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  41781. (word32)XSTRLEN("secure your data")));
  41782. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  41783. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  41784. XMEMSET(buf, 0, 16);
  41785. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  41786. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  41787. /* You cannot write to MEM BIO with read-only mode. */
  41788. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  41789. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  41790. XMEMSET(buf, 0, 16);
  41791. ExpectIntEQ(BIO_reset(bio), 0);
  41792. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  41793. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  41794. BIO_free(bio);
  41795. #endif
  41796. return EXPECT_RESULT();
  41797. }
  41798. #endif /* !NO_BIO */
  41799. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41800. /* test that the callback arg is correct */
  41801. static int certCbArg = 0;
  41802. static int certCb(WOLFSSL* ssl, void* arg)
  41803. {
  41804. if (ssl == NULL || arg != &certCbArg)
  41805. return 0;
  41806. if (wolfSSL_is_server(ssl)) {
  41807. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  41808. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41809. return 0;
  41810. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  41811. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41812. return 0;
  41813. }
  41814. else {
  41815. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  41816. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41817. return 0;
  41818. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  41819. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41820. return 0;
  41821. }
  41822. return 1;
  41823. }
  41824. static int certSetupCb(WOLFSSL_CTX* ctx)
  41825. {
  41826. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  41827. return TEST_SUCCESS;
  41828. }
  41829. /**
  41830. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  41831. * to stop certificate and key loading
  41832. */
  41833. static int certClearCb(WOLFSSL* ssl)
  41834. {
  41835. /* Clear the loaded certs to force the callbacks to set them up */
  41836. SSL_certs_clear(ssl);
  41837. return TEST_SUCCESS;
  41838. }
  41839. #endif
  41840. static int test_wolfSSL_cert_cb(void)
  41841. {
  41842. EXPECT_DECLS;
  41843. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41844. test_ssl_cbf func_cb_client;
  41845. test_ssl_cbf func_cb_server;
  41846. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41847. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41848. func_cb_client.ctx_ready = certSetupCb;
  41849. func_cb_client.ssl_ready = certClearCb;
  41850. func_cb_server.ctx_ready = certSetupCb;
  41851. func_cb_server.ssl_ready = certClearCb;
  41852. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41853. &func_cb_server, NULL), TEST_SUCCESS);
  41854. #endif
  41855. return EXPECT_RESULT();
  41856. }
  41857. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41858. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  41859. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  41860. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  41861. {
  41862. EXPECT_DECLS;
  41863. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  41864. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  41865. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  41866. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  41867. return EXPECT_RESULT();
  41868. }
  41869. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  41870. {
  41871. const byte* suites = NULL;
  41872. word16 suiteSz = 0;
  41873. const byte* hashSigAlgo = NULL;
  41874. word16 hashSigAlgoSz = 0;
  41875. word16 idx = 0;
  41876. int haveRSA = 0;
  41877. int haveECC = 0;
  41878. (void)arg;
  41879. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  41880. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  41881. return 0;
  41882. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  41883. hashSigAlgoSz == 0)
  41884. return 0;
  41885. for (idx = 0; idx < suiteSz; idx += 2) {
  41886. WOLFSSL_CIPHERSUITE_INFO info =
  41887. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  41888. if (info.rsaAuth)
  41889. haveRSA = 1;
  41890. else if (info.eccAuth)
  41891. haveECC = 1;
  41892. }
  41893. if (hashSigAlgoSz > 0) {
  41894. /* sigalgs extension takes precedence over ciphersuites */
  41895. haveRSA = 0;
  41896. haveECC = 0;
  41897. }
  41898. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  41899. int hashAlgo = 0;
  41900. int sigAlgo = 0;
  41901. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  41902. &hashAlgo, &sigAlgo) != 0)
  41903. return 0;
  41904. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  41905. haveRSA = 1;
  41906. else if (sigAlgo == ECDSAk)
  41907. haveECC = 1;
  41908. }
  41909. if (haveRSA) {
  41910. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  41911. != WOLFSSL_SUCCESS)
  41912. return 0;
  41913. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  41914. != WOLFSSL_SUCCESS)
  41915. return 0;
  41916. }
  41917. else if (haveECC) {
  41918. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  41919. != WOLFSSL_SUCCESS)
  41920. return 0;
  41921. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  41922. != WOLFSSL_SUCCESS)
  41923. return 0;
  41924. }
  41925. return 1;
  41926. }
  41927. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  41928. {
  41929. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  41930. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  41931. return TEST_SUCCESS;
  41932. }
  41933. #endif
  41934. /* Testing dynamic ciphers offered by client */
  41935. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  41936. {
  41937. EXPECT_DECLS;
  41938. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41939. test_ssl_cbf func_cb_client;
  41940. test_ssl_cbf func_cb_server;
  41941. struct {
  41942. method_provider client_meth;
  41943. const char* client_ciphers;
  41944. const char* client_sigalgs;
  41945. const char* client_ca;
  41946. method_provider server_meth;
  41947. } test_params[] = {
  41948. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  41949. #ifdef WOLFSSL_TLS13
  41950. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  41951. {wolfTLSv1_3_client_method,
  41952. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41953. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  41954. #endif
  41955. #ifdef HAVE_ECC
  41956. {wolfTLSv1_3_client_method,
  41957. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41958. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  41959. #endif
  41960. #endif
  41961. #ifndef WOLFSSL_NO_TLS12
  41962. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  41963. {wolfTLSv1_2_client_method,
  41964. "DHE-RSA-AES128-GCM-SHA256",
  41965. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  41966. #endif
  41967. #ifdef HAVE_ECC
  41968. {wolfTLSv1_2_client_method,
  41969. "ECDHE-ECDSA-AES128-GCM-SHA256",
  41970. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  41971. #endif
  41972. #endif
  41973. #endif
  41974. };
  41975. size_t i;
  41976. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  41977. if (testCount > 0) {
  41978. for (i = 0; i < testCount; i++) {
  41979. printf("\tTesting %s ciphers with %s sigalgs\n",
  41980. test_params[i].client_ciphers,
  41981. test_params[i].client_sigalgs);
  41982. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41983. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41984. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  41985. test_params[i].client_ciphers;
  41986. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  41987. test_params[i].client_sigalgs;
  41988. func_cb_client.method = test_params[i].client_meth;
  41989. func_cb_client.caPemFile = test_params[i].client_ca;
  41990. func_cb_client.ctx_ready =
  41991. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  41992. func_cb_server.ctx_ready =
  41993. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  41994. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  41995. func_cb_server.method = test_params[i].server_meth;
  41996. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41997. &func_cb_server, NULL), TEST_SUCCESS);
  41998. }
  41999. }
  42000. #endif
  42001. return EXPECT_RESULT();
  42002. }
  42003. static int test_wolfSSL_ciphersuite_auth(void)
  42004. {
  42005. EXPECT_DECLS;
  42006. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  42007. WOLFSSL_CIPHERSUITE_INFO info;
  42008. (void)info;
  42009. #ifndef WOLFSSL_NO_TLS12
  42010. #ifdef HAVE_CHACHA
  42011. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  42012. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  42013. ExpectIntEQ(info.rsaAuth, 1);
  42014. ExpectIntEQ(info.eccAuth, 0);
  42015. ExpectIntEQ(info.eccStatic, 0);
  42016. ExpectIntEQ(info.psk, 0);
  42017. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  42018. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  42019. ExpectIntEQ(info.rsaAuth, 0);
  42020. ExpectIntEQ(info.eccAuth, 1);
  42021. ExpectIntEQ(info.eccStatic, 0);
  42022. ExpectIntEQ(info.psk, 0);
  42023. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  42024. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  42025. ExpectIntEQ(info.rsaAuth, 0);
  42026. ExpectIntEQ(info.eccAuth, 0);
  42027. ExpectIntEQ(info.eccStatic, 0);
  42028. ExpectIntEQ(info.psk, 1);
  42029. #endif
  42030. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  42031. #ifndef NO_RSA
  42032. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42033. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  42034. ExpectIntEQ(info.rsaAuth, 1);
  42035. ExpectIntEQ(info.eccAuth, 0);
  42036. ExpectIntEQ(info.eccStatic, 0);
  42037. ExpectIntEQ(info.psk, 0);
  42038. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42039. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  42040. ExpectIntEQ(info.rsaAuth, 1);
  42041. ExpectIntEQ(info.eccAuth, 0);
  42042. ExpectIntEQ(info.eccStatic, 1);
  42043. ExpectIntEQ(info.psk, 0);
  42044. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42045. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  42046. ExpectIntEQ(info.rsaAuth, 1);
  42047. ExpectIntEQ(info.eccAuth, 0);
  42048. ExpectIntEQ(info.eccStatic, 1);
  42049. ExpectIntEQ(info.psk, 0);
  42050. #endif
  42051. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42052. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  42053. ExpectIntEQ(info.rsaAuth, 0);
  42054. ExpectIntEQ(info.eccAuth, 1);
  42055. ExpectIntEQ(info.eccStatic, 0);
  42056. ExpectIntEQ(info.psk, 0);
  42057. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42058. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  42059. ExpectIntEQ(info.rsaAuth, 0);
  42060. ExpectIntEQ(info.eccAuth, 1);
  42061. ExpectIntEQ(info.eccStatic, 1);
  42062. ExpectIntEQ(info.psk, 0);
  42063. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  42064. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  42065. ExpectIntEQ(info.rsaAuth, 0);
  42066. ExpectIntEQ(info.eccAuth, 0);
  42067. ExpectIntEQ(info.eccStatic, 0);
  42068. ExpectIntEQ(info.psk, 1);
  42069. #endif
  42070. #endif
  42071. #ifdef WOLFSSL_TLS13
  42072. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  42073. TLS_AES_128_GCM_SHA256);
  42074. ExpectIntEQ(info.rsaAuth, 0);
  42075. ExpectIntEQ(info.eccAuth, 0);
  42076. ExpectIntEQ(info.eccStatic, 0);
  42077. ExpectIntEQ(info.psk, 0);
  42078. #endif
  42079. #endif
  42080. return EXPECT_RESULT();
  42081. }
  42082. static int test_wolfSSL_sigalg_info(void)
  42083. {
  42084. EXPECT_DECLS;
  42085. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  42086. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  42087. word16 len = 0;
  42088. word16 idx = 0;
  42089. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  42090. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  42091. for (idx = 0; idx < len; idx += 2) {
  42092. int hashAlgo = 0;
  42093. int sigAlgo = 0;
  42094. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  42095. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  42096. ExpectIntNE(hashAlgo, 0);
  42097. ExpectIntNE(sigAlgo, 0);
  42098. }
  42099. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  42100. 0xFFFFFFFF, &len);
  42101. for (idx = 0; idx < len; idx += 2) {
  42102. int hashAlgo = 0;
  42103. int sigAlgo = 0;
  42104. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  42105. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  42106. ExpectIntNE(hashAlgo, 0);
  42107. }
  42108. #endif
  42109. return EXPECT_RESULT();
  42110. }
  42111. static int test_wolfSSL_SESSION(void)
  42112. {
  42113. EXPECT_DECLS;
  42114. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42115. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42116. !defined(NO_SESSION_CACHE)
  42117. WOLFSSL* ssl = NULL;
  42118. WOLFSSL_CTX* ctx = NULL;
  42119. WOLFSSL_SESSION* sess = NULL;
  42120. WOLFSSL_SESSION* sess_copy = NULL;
  42121. #ifdef OPENSSL_EXTRA
  42122. #ifdef HAVE_EXT_CACHE
  42123. unsigned char* sessDer = NULL;
  42124. unsigned char* ptr = NULL;
  42125. int sz = 0;
  42126. #endif
  42127. const unsigned char context[] = "user app context";
  42128. unsigned int contextSz = (unsigned int)sizeof(context);
  42129. #endif
  42130. int ret = 0, err = 0;
  42131. SOCKET_T sockfd;
  42132. tcp_ready ready;
  42133. func_args server_args;
  42134. THREAD_TYPE serverThread;
  42135. char msg[80];
  42136. const char* sendGET = "GET";
  42137. /* TLS v1.3 requires session tickets */
  42138. /* CHACHA and POLY1305 required for myTicketEncCb */
  42139. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  42140. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  42141. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  42142. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  42143. #else
  42144. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42145. #endif
  42146. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  42147. WOLFSSL_FILETYPE_PEM));
  42148. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  42149. WOLFSSL_FILETYPE_PEM));
  42150. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  42151. WOLFSSL_SUCCESS);
  42152. #ifdef WOLFSSL_ENCRYPTED_KEYS
  42153. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  42154. #endif
  42155. #ifdef HAVE_SESSION_TICKET
  42156. /* Use session tickets, for ticket tests below */
  42157. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  42158. #endif
  42159. XMEMSET(&server_args, 0, sizeof(func_args));
  42160. #ifdef WOLFSSL_TIRTOS
  42161. fdOpenSession(Task_self());
  42162. #endif
  42163. StartTCP();
  42164. InitTcpReady(&ready);
  42165. #if defined(USE_WINDOWS_API)
  42166. /* use RNG to get random port if using windows */
  42167. ready.port = GetRandomPort();
  42168. #endif
  42169. server_args.signal = &ready;
  42170. start_thread(test_server_nofail, &server_args, &serverThread);
  42171. wait_tcp_ready(&server_args);
  42172. /* client connection */
  42173. ExpectNotNull(ssl = wolfSSL_new(ctx));
  42174. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  42175. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  42176. #ifdef WOLFSSL_ASYNC_CRYPT
  42177. err = 0; /* Reset error */
  42178. #endif
  42179. do {
  42180. #ifdef WOLFSSL_ASYNC_CRYPT
  42181. if (err == WC_PENDING_E) {
  42182. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  42183. if (ret < 0) { break; } else if (ret == 0) { continue; }
  42184. }
  42185. #endif
  42186. ret = wolfSSL_connect(ssl);
  42187. err = wolfSSL_get_error(ssl, 0);
  42188. } while (err == WC_PENDING_E);
  42189. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  42190. #ifdef WOLFSSL_ASYNC_CRYPT
  42191. err = 0; /* Reset error */
  42192. #endif
  42193. do {
  42194. #ifdef WOLFSSL_ASYNC_CRYPT
  42195. if (err == WC_PENDING_E) {
  42196. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  42197. if (ret < 0) { break; } else if (ret == 0) { continue; }
  42198. }
  42199. #endif
  42200. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  42201. err = wolfSSL_get_error(ssl, 0);
  42202. } while (err == WC_PENDING_E);
  42203. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  42204. #ifdef WOLFSSL_ASYNC_CRYPT
  42205. err = 0; /* Reset error */
  42206. #endif
  42207. do {
  42208. #ifdef WOLFSSL_ASYNC_CRYPT
  42209. if (err == WC_PENDING_E) {
  42210. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  42211. if (ret < 0) { break; } else if (ret == 0) { continue; }
  42212. }
  42213. #endif
  42214. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  42215. err = wolfSSL_get_error(ssl, 0);
  42216. } while (err == WC_PENDING_E);
  42217. ExpectIntEQ(ret, 23);
  42218. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  42219. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  42220. ExpectIntEQ(wolfSSL_SessionIsSetup(sess), 1);
  42221. #ifdef HAVE_EXT_CACHE
  42222. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  42223. * HAVE_EXT_CACHE we get new objects each time */
  42224. #endif
  42225. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  42226. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  42227. sess = wolfSSL_get_session(ssl);
  42228. #ifdef OPENSSL_EXTRA
  42229. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  42230. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  42231. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  42232. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  42233. #ifdef HAVE_SESSION_TICKET
  42234. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  42235. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  42236. SESSION_TICKET_HINT_DEFAULT);
  42237. #else
  42238. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  42239. #endif
  42240. #else
  42241. (void)sess;
  42242. #endif /* OPENSSL_EXTRA */
  42243. /* Retain copy of the session for later testing */
  42244. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  42245. wolfSSL_shutdown(ssl);
  42246. wolfSSL_free(ssl); ssl = NULL;
  42247. CloseSocket(sockfd);
  42248. join_thread(serverThread);
  42249. FreeTcpReady(&ready);
  42250. #ifdef WOLFSSL_TIRTOS
  42251. fdOpenSession(Task_self());
  42252. #endif
  42253. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  42254. {
  42255. X509 *x509 = NULL;
  42256. char buf[30];
  42257. int bufSz = 0;
  42258. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  42259. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  42260. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  42261. sizeof(buf))), 0);
  42262. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  42263. if (bufSz == 7) {
  42264. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  42265. }
  42266. if (bufSz == 16) {
  42267. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  42268. }
  42269. }
  42270. #endif
  42271. #ifdef HAVE_EXT_CACHE
  42272. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  42273. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  42274. sess_copy = NULL;
  42275. #endif
  42276. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  42277. /* get session from DER and update the timeout */
  42278. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  42279. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  42280. wolfSSL_SESSION_free(sess); sess = NULL;
  42281. sess = NULL;
  42282. ptr = sessDer;
  42283. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  42284. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  42285. (const unsigned char**)&ptr, sz));
  42286. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  42287. sessDer = NULL;
  42288. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  42289. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  42290. #endif
  42291. /* successful set session test */
  42292. ExpectNotNull(ssl = wolfSSL_new(ctx));
  42293. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  42294. #ifdef HAVE_SESSION_TICKET
  42295. /* Test set/get session ticket */
  42296. {
  42297. const char* ticket = "This is a session ticket";
  42298. char buf[64] = {0};
  42299. word32 bufSz = (word32)sizeof(buf);
  42300. ExpectIntEQ(SSL_SUCCESS,
  42301. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  42302. (word32)XSTRLEN(ticket)));
  42303. ExpectIntEQ(SSL_SUCCESS,
  42304. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  42305. ExpectStrEQ(ticket, buf);
  42306. }
  42307. #endif
  42308. #ifdef OPENSSL_EXTRA
  42309. /* session timeout case */
  42310. /* make the session to be expired */
  42311. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  42312. XSLEEP_MS(1200);
  42313. /* SSL_set_session should reject specified session but return success
  42314. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  42315. */
  42316. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42317. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  42318. #else
  42319. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  42320. #endif
  42321. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  42322. #ifdef WOLFSSL_SESSION_ID_CTX
  42323. /* fail case with miss match session context IDs (use compatibility API) */
  42324. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  42325. SSL_SUCCESS);
  42326. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  42327. wolfSSL_free(ssl); ssl = NULL;
  42328. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  42329. SSL_FAILURE);
  42330. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  42331. SSL_SUCCESS);
  42332. ExpectNotNull(ssl = wolfSSL_new(ctx));
  42333. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  42334. #endif
  42335. #endif /* OPENSSL_EXTRA */
  42336. wolfSSL_free(ssl);
  42337. wolfSSL_SESSION_free(sess);
  42338. wolfSSL_CTX_free(ctx);
  42339. #endif
  42340. return EXPECT_RESULT();
  42341. }
  42342. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42343. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42344. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  42345. !defined(WOLFSSL_NO_TLS12)
  42346. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  42347. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  42348. {
  42349. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  42350. /* returns previous timeout value */
  42351. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  42352. #else
  42353. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  42354. #endif
  42355. }
  42356. /* set the session to timeout in a second */
  42357. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  42358. {
  42359. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  42360. }
  42361. /* store the client side session from the first successful connection */
  42362. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  42363. {
  42364. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  42365. NULL); /* ref count 1 */
  42366. }
  42367. /* wait till session is expired then set it in the WOLFSSL struct for use */
  42368. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  42369. {
  42370. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  42371. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  42372. WOLFSSL_SUCCESS);
  42373. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  42374. }
  42375. /* set expired session in the WOLFSSL struct for use */
  42376. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  42377. {
  42378. XSLEEP_MS(1200); /* wait a second for session to expire */
  42379. /* set the expired session, call to set session fails but continuing on
  42380. after failure should be handled here */
  42381. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42382. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  42383. WOLFSSL_SUCCESS);
  42384. #else
  42385. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  42386. WOLFSSL_SUCCESS);
  42387. #endif
  42388. }
  42389. /* check that the expired session was not reused */
  42390. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  42391. {
  42392. /* since the session has expired it should not have been reused */
  42393. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  42394. }
  42395. #endif
  42396. static int test_wolfSSL_SESSION_expire_downgrade(void)
  42397. {
  42398. EXPECT_DECLS;
  42399. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42400. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42401. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  42402. !defined(WOLFSSL_NO_TLS12)
  42403. WOLFSSL_CTX* ctx = NULL;
  42404. callback_functions server_cbf, client_cbf;
  42405. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  42406. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  42407. /* force server side to use TLS 1.2 */
  42408. server_cbf.ctx = ctx;
  42409. server_cbf.method = wolfTLSv1_2_server_method;
  42410. client_cbf.method = wolfSSLv23_client_method;
  42411. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  42412. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  42413. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  42414. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  42415. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  42416. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  42417. /* set the previously created session and wait till expired */
  42418. server_cbf.ctx = ctx;
  42419. client_cbf.method = wolfSSLv23_client_method;
  42420. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  42421. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  42422. client_cbf.on_result =
  42423. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  42424. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  42425. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  42426. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  42427. /* set the previously created expired session */
  42428. server_cbf.ctx = ctx;
  42429. client_cbf.method = wolfSSLv23_client_method;
  42430. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  42431. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  42432. client_cbf.on_result =
  42433. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  42434. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  42435. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  42436. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  42437. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  42438. wolfSSL_CTX_free(ctx);
  42439. #endif
  42440. return EXPECT_RESULT();
  42441. }
  42442. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  42443. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  42444. static int clientSessRemCountMalloc = 0;
  42445. static int serverSessRemCountMalloc = 0;
  42446. static int clientSessRemCountFree = 0;
  42447. static int serverSessRemCountFree = 0;
  42448. static WOLFSSL_CTX* serverSessCtx = NULL;
  42449. static WOLFSSL_SESSION* serverSess = NULL;
  42450. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42451. !defined(NO_SESSION_CACHE_REF)
  42452. static WOLFSSL_CTX* clientSessCtx = NULL;
  42453. static WOLFSSL_SESSION* clientSess = NULL;
  42454. #endif
  42455. static int serverSessRemIdx = 3;
  42456. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  42457. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  42458. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  42459. {
  42460. int* side;
  42461. (void)ctx;
  42462. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  42463. if (side != NULL) {
  42464. if (*side == WOLFSSL_CLIENT_END)
  42465. clientSessRemCountFree++;
  42466. else
  42467. serverSessRemCountFree++;
  42468. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  42469. }
  42470. }
  42471. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  42472. {
  42473. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  42474. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  42475. !defined(NO_SESSION_CACHE_REF)
  42476. {
  42477. EXPECT_DECLS;
  42478. /* Allow downgrade, set min version, and disable TLS 1.3.
  42479. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  42480. * reference to the session cache. But with WOLFSSL_TLS13 and without
  42481. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  42482. * the session in the cache. In this case we need to downgrade to
  42483. * previous versions to just use the legacy session ID field. */
  42484. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  42485. SSL_SUCCESS);
  42486. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  42487. SSL_SUCCESS);
  42488. return EXPECT_RESULT();
  42489. }
  42490. #else
  42491. return TEST_SUCCESS;
  42492. #endif
  42493. }
  42494. static int SessRemSslSetupCb(WOLFSSL* ssl)
  42495. {
  42496. EXPECT_DECLS;
  42497. int* side;
  42498. if (SSL_is_server(ssl)) {
  42499. side = &sessRemCtx_Server;
  42500. serverSessRemCountMalloc++;
  42501. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  42502. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  42503. SSL_SUCCESS);
  42504. }
  42505. else {
  42506. side = &sessRemCtx_Client;
  42507. clientSessRemCountMalloc++;
  42508. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42509. !defined(NO_SESSION_CACHE_REF)
  42510. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  42511. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  42512. SSL_SUCCESS);
  42513. #endif
  42514. }
  42515. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  42516. serverSessRemIdx, side), SSL_SUCCESS);
  42517. return EXPECT_RESULT();
  42518. }
  42519. #endif
  42520. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  42521. {
  42522. EXPECT_DECLS;
  42523. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  42524. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  42525. /* Check that the remove callback gets called for external data in a
  42526. * session object */
  42527. test_ssl_cbf func_cb;
  42528. XMEMSET(&func_cb, 0, sizeof(func_cb));
  42529. func_cb.ctx_ready = SessRemCtxSetupCb;
  42530. func_cb.on_result = SessRemSslSetupCb;
  42531. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  42532. NULL), TEST_SUCCESS);
  42533. /* Both should have been allocated */
  42534. ExpectIntEQ(clientSessRemCountMalloc, 1);
  42535. ExpectIntEQ(serverSessRemCountMalloc, 1);
  42536. /* This should not be called yet. Session wasn't evicted from cache yet. */
  42537. ExpectIntEQ(clientSessRemCountFree, 0);
  42538. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42539. !defined(NO_SESSION_CACHE_REF)
  42540. /* Force a cache lookup */
  42541. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  42542. /* Force a cache update */
  42543. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  42544. /* This should set the timeout to 0 and call the remove callback from within
  42545. * the session cache. */
  42546. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  42547. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  42548. ExpectIntEQ(clientSessRemCountFree, 1);
  42549. #endif
  42550. /* Server session is in the cache so ex_data isn't free'd with the SSL
  42551. * object */
  42552. ExpectIntEQ(serverSessRemCountFree, 0);
  42553. /* Force a cache lookup */
  42554. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  42555. /* Force a cache update */
  42556. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  42557. /* This should set the timeout to 0 and call the remove callback from within
  42558. * the session cache. */
  42559. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  42560. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  42561. ExpectIntEQ(serverSessRemCountFree, 1);
  42562. /* Need to free the references that we kept */
  42563. SSL_CTX_free(serverSessCtx);
  42564. SSL_SESSION_free(serverSess);
  42565. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42566. !defined(NO_SESSION_CACHE_REF)
  42567. SSL_CTX_free(clientSessCtx);
  42568. SSL_SESSION_free(clientSess);
  42569. #endif
  42570. #endif
  42571. return EXPECT_RESULT();
  42572. }
  42573. static int test_wolfSSL_ticket_keys(void)
  42574. {
  42575. EXPECT_DECLS;
  42576. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  42577. !defined(NO_WOLFSSL_SERVER)
  42578. WOLFSSL_CTX* ctx = NULL;
  42579. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  42580. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  42581. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  42582. WOLFSSL_FAILURE);
  42583. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  42584. WOLFSSL_FAILURE);
  42585. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  42586. WOLFSSL_FAILURE);
  42587. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  42588. WOLFSSL_FAILURE);
  42589. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  42590. WOLFSSL_FAILURE);
  42591. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  42592. WOLFSSL_FAILURE);
  42593. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  42594. WOLFSSL_FAILURE);
  42595. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  42596. WOLFSSL_FAILURE);
  42597. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  42598. WOLFSSL_FAILURE);
  42599. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  42600. WOLFSSL_FAILURE);
  42601. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  42602. WOLFSSL_FAILURE);
  42603. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  42604. WOLFSSL_FAILURE);
  42605. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  42606. WOLFSSL_FAILURE);
  42607. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  42608. WOLFSSL_FAILURE);
  42609. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  42610. WOLFSSL_SUCCESS);
  42611. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  42612. WOLFSSL_SUCCESS);
  42613. wolfSSL_CTX_free(ctx);
  42614. #endif
  42615. return EXPECT_RESULT();
  42616. }
  42617. #ifndef NO_BIO
  42618. static int test_wolfSSL_d2i_PUBKEY(void)
  42619. {
  42620. EXPECT_DECLS;
  42621. #if defined(OPENSSL_EXTRA)
  42622. BIO* bio = NULL;
  42623. EVP_PKEY* pkey = NULL;
  42624. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42625. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  42626. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  42627. /* RSA PUBKEY test */
  42628. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  42629. sizeof_client_keypub_der_2048), 0);
  42630. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42631. EVP_PKEY_free(pkey);
  42632. pkey = NULL;
  42633. #endif
  42634. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  42635. /* ECC PUBKEY test */
  42636. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  42637. sizeof_ecc_clikeypub_der_256), 0);
  42638. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42639. EVP_PKEY_free(pkey);
  42640. pkey = NULL;
  42641. #endif
  42642. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  42643. /* DSA PUBKEY test */
  42644. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  42645. sizeof_dsa_pub_key_der_2048), 0);
  42646. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42647. EVP_PKEY_free(pkey);
  42648. pkey = NULL;
  42649. #endif
  42650. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  42651. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  42652. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  42653. (HAVE_FIPS_VERSION > 2))
  42654. /* DH PUBKEY test */
  42655. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  42656. sizeof_dh_pub_key_der_2048), 0);
  42657. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42658. EVP_PKEY_free(pkey);
  42659. pkey = NULL;
  42660. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  42661. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  42662. BIO_free(bio);
  42663. (void)pkey;
  42664. #endif
  42665. return EXPECT_RESULT();
  42666. }
  42667. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  42668. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  42669. {
  42670. EXPECT_DECLS;
  42671. BIO* bio = NULL;
  42672. EVP_PKEY* pkey = NULL;
  42673. WOLFSSL_CTX* ctx = NULL;
  42674. #if defined(WOLFSSL_KEY_GEN)
  42675. unsigned char buff[4096];
  42676. unsigned char* bufPtr = buff;
  42677. #endif
  42678. /* test creating new EVP_PKEY with bad arg */
  42679. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  42680. /* test loading RSA key using BIO */
  42681. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42682. {
  42683. XFILE file = XBADFILE;
  42684. const char* fname = "./certs/server-key.der";
  42685. size_t sz = 0;
  42686. byte* buf = NULL;
  42687. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42688. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  42689. ExpectTrue((sz = XFTELL(file)) != 0);
  42690. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  42691. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  42692. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  42693. if (file != XBADFILE) {
  42694. XFCLOSE(file);
  42695. }
  42696. /* Test using BIO new mem and loading DER private key */
  42697. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42698. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  42699. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  42700. BIO_free(bio);
  42701. bio = NULL;
  42702. EVP_PKEY_free(pkey);
  42703. pkey = NULL;
  42704. }
  42705. #endif
  42706. /* test loading ECC key using BIO */
  42707. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  42708. {
  42709. XFILE file = XBADFILE;
  42710. const char* fname = "./certs/ecc-key.der";
  42711. size_t sz = 0;
  42712. byte* buf = NULL;
  42713. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42714. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  42715. ExpectTrue((sz = XFTELL(file)) != 0);
  42716. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  42717. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  42718. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  42719. if (file != XBADFILE)
  42720. XFCLOSE(file);
  42721. /* Test using BIO new mem and loading DER private key */
  42722. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42723. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  42724. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  42725. BIO_free(bio);
  42726. bio = NULL;
  42727. EVP_PKEY_free(pkey);
  42728. pkey = NULL;
  42729. }
  42730. #endif
  42731. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42732. #ifndef NO_WOLFSSL_SERVER
  42733. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42734. #else
  42735. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  42736. #endif
  42737. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  42738. {
  42739. RSA* rsa = NULL;
  42740. /* Tests bad parameters */
  42741. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  42742. /* RSA not set yet, expecting to fail*/
  42743. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  42744. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  42745. /* set RSA using bio*/
  42746. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  42747. sizeof_client_key_der_2048), 0);
  42748. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  42749. ExpectNotNull(rsa);
  42750. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  42751. /* i2d RSAprivate key tests */
  42752. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  42753. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  42754. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42755. sizeof_client_key_der_2048);
  42756. bufPtr -= sizeof_client_key_der_2048;
  42757. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42758. sizeof_client_key_der_2048), 0);
  42759. bufPtr = NULL;
  42760. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42761. sizeof_client_key_der_2048);
  42762. ExpectNotNull(bufPtr);
  42763. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42764. sizeof_client_key_der_2048), 0);
  42765. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42766. RSA_free(rsa);
  42767. rsa = RSA_new();
  42768. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  42769. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  42770. RSA_free(rsa);
  42771. }
  42772. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  42773. SSL_CTX_free(ctx);
  42774. ctx = NULL;
  42775. BIO_free(bio);
  42776. bio = NULL;
  42777. return EXPECT_RESULT();
  42778. }
  42779. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  42780. #endif /* !NO_BIO */
  42781. static int test_wolfSSL_sk_GENERAL_NAME(void)
  42782. {
  42783. EXPECT_DECLS;
  42784. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42785. !defined(NO_RSA)
  42786. X509* x509 = NULL;
  42787. GENERAL_NAME* gn = NULL;
  42788. unsigned char buf[4096];
  42789. const unsigned char* bufPt = NULL;
  42790. int bytes = 0;
  42791. int i;
  42792. int j;
  42793. XFILE f = XBADFILE;
  42794. STACK_OF(GENERAL_NAME)* sk = NULL;
  42795. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42796. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42797. if (f != XBADFILE)
  42798. XFCLOSE(f);
  42799. for (j = 0; j < 2; ++j) {
  42800. bufPt = buf;
  42801. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42802. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42803. NID_subject_alt_name, NULL, NULL));
  42804. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  42805. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  42806. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  42807. if (gn != NULL) {
  42808. switch (gn->type) {
  42809. case GEN_DNS:
  42810. fprintf(stderr, "found type GEN_DNS\n");
  42811. break;
  42812. case GEN_EMAIL:
  42813. fprintf(stderr, "found type GEN_EMAIL\n");
  42814. break;
  42815. case GEN_URI:
  42816. fprintf(stderr, "found type GEN_URI\n");
  42817. break;
  42818. }
  42819. }
  42820. }
  42821. X509_free(x509);
  42822. x509 = NULL;
  42823. if (j == 0) {
  42824. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42825. }
  42826. else {
  42827. /*
  42828. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  42829. * it was supposed to. This is a regression test for that bug.
  42830. */
  42831. GENERAL_NAMES_free(sk);
  42832. }
  42833. sk = NULL;
  42834. }
  42835. #endif
  42836. return EXPECT_RESULT();
  42837. }
  42838. static int test_wolfSSL_GENERAL_NAME_print(void)
  42839. {
  42840. EXPECT_DECLS;
  42841. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  42842. X509* x509 = NULL;
  42843. GENERAL_NAME* gn = NULL;
  42844. unsigned char buf[4096];
  42845. const unsigned char* bufPt = NULL;
  42846. int bytes;
  42847. XFILE f = XBADFILE;
  42848. STACK_OF(GENERAL_NAME)* sk = NULL;
  42849. BIO* out = NULL;
  42850. unsigned char outbuf[128];
  42851. X509_EXTENSION* ext = NULL;
  42852. AUTHORITY_INFO_ACCESS* aia = NULL;
  42853. ACCESS_DESCRIPTION* ad = NULL;
  42854. ASN1_IA5STRING *dnsname = NULL;
  42855. const unsigned char v4Addr[] = {192,168,53,1};
  42856. const unsigned char v6Addr[] =
  42857. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  42858. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  42859. const unsigned char email[] =
  42860. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  42861. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  42862. const char* dnsStr = "DNS:example.com";
  42863. const char* uriStr = "URI:http://127.0.0.1:22220";
  42864. const char* v4addStr = "IP Address:192.168.53.1";
  42865. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  42866. const char* emailStr = "email:info@wolfssl.com";
  42867. const char* othrStr = "othername:<unsupported>";
  42868. const char* x400Str = "X400Name:<unsupported>";
  42869. const char* ediStr = "EdiPartyName:<unsupported>";
  42870. /* BIO to output */
  42871. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  42872. /* test for NULL param */
  42873. gn = NULL;
  42874. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  42875. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  42876. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  42877. /* test for GEN_DNS */
  42878. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42879. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42880. if (f != XBADFILE) {
  42881. XFCLOSE(f);
  42882. f = XBADFILE;
  42883. }
  42884. bufPt = buf;
  42885. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42886. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42887. NID_subject_alt_name, NULL, NULL));
  42888. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  42889. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42890. XMEMSET(outbuf, 0, sizeof(outbuf));
  42891. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42892. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42893. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42894. gn = NULL;
  42895. sk = NULL;
  42896. X509_free(x509);
  42897. x509 = NULL;
  42898. /* Lets test for setting as well. */
  42899. ExpectNotNull(gn = GENERAL_NAME_new());
  42900. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  42901. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  42902. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  42903. dnsname = NULL;
  42904. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42905. XMEMSET(outbuf, 0, sizeof(outbuf));
  42906. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42907. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42908. GENERAL_NAME_free(gn);
  42909. /* test for GEN_URI */
  42910. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42911. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42912. if (f != XBADFILE) {
  42913. XFCLOSE(f);
  42914. f = XBADFILE;
  42915. }
  42916. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  42917. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  42918. ext));
  42919. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  42920. if (ad != NULL) {
  42921. gn = ad->location;
  42922. }
  42923. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42924. gn = NULL;
  42925. XMEMSET(outbuf,0,sizeof(outbuf));
  42926. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42927. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  42928. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  42929. aia = NULL;
  42930. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  42931. ExpectNotNull(aia);
  42932. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  42933. aia = NULL;
  42934. X509_free(x509);
  42935. x509 = NULL;
  42936. /* test for GEN_IPADD */
  42937. /* ip v4 address */
  42938. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42939. if (gn != NULL) {
  42940. gn->type = GEN_IPADD;
  42941. if (gn->d.iPAddress != NULL) {
  42942. gn->d.iPAddress->length = sizeof(v4Addr);
  42943. }
  42944. }
  42945. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  42946. sizeof(v4Addr)), 1);
  42947. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42948. XMEMSET(outbuf,0,sizeof(outbuf));
  42949. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42950. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  42951. GENERAL_NAME_free(gn);
  42952. gn = NULL;
  42953. /* ip v6 address */
  42954. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42955. if (gn != NULL) {
  42956. gn->type = GEN_IPADD;
  42957. if (gn->d.iPAddress != NULL) {
  42958. gn->d.iPAddress->length = sizeof(v6Addr);
  42959. }
  42960. }
  42961. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  42962. sizeof(v6Addr)), 1);
  42963. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42964. XMEMSET(outbuf,0,sizeof(outbuf));
  42965. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42966. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  42967. GENERAL_NAME_free(gn);
  42968. gn = NULL;
  42969. /* test for GEN_EMAIL */
  42970. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42971. if (gn != NULL) {
  42972. gn->type = GEN_EMAIL;
  42973. if (gn->d.rfc822Name != NULL) {
  42974. gn->d.rfc822Name->length = sizeof(email);
  42975. }
  42976. }
  42977. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  42978. 1);
  42979. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42980. XMEMSET(outbuf,0,sizeof(outbuf));
  42981. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42982. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  42983. GENERAL_NAME_free(gn);
  42984. gn = NULL;
  42985. /* test for GEN_OTHERNAME */
  42986. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42987. if (gn != NULL) {
  42988. gn->type = GEN_OTHERNAME;
  42989. }
  42990. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42991. XMEMSET(outbuf,0,sizeof(outbuf));
  42992. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42993. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  42994. GENERAL_NAME_free(gn);
  42995. gn = NULL;
  42996. /* test for GEN_X400 */
  42997. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42998. if (gn != NULL) {
  42999. gn->type = GEN_X400;
  43000. }
  43001. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  43002. XMEMSET(outbuf,0,sizeof(outbuf));
  43003. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  43004. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  43005. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  43006. if (gn != NULL) {
  43007. gn->type = GEN_IA5;
  43008. }
  43009. GENERAL_NAME_free(gn);
  43010. gn = NULL;
  43011. /* test for GEN_EDIPARTY */
  43012. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  43013. if (gn != NULL) {
  43014. gn->type = GEN_EDIPARTY;
  43015. }
  43016. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  43017. XMEMSET(outbuf,0,sizeof(outbuf));
  43018. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  43019. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  43020. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  43021. if (gn != NULL) {
  43022. gn->type = GEN_IA5;
  43023. }
  43024. GENERAL_NAME_free(gn);
  43025. gn = NULL;
  43026. BIO_free(out);
  43027. #endif /* OPENSSL_ALL */
  43028. return EXPECT_RESULT();
  43029. }
  43030. static int test_wolfSSL_sk_DIST_POINT(void)
  43031. {
  43032. EXPECT_DECLS;
  43033. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  43034. !defined(NO_RSA)
  43035. X509* x509 = NULL;
  43036. unsigned char buf[4096];
  43037. const unsigned char* bufPt;
  43038. int bytes = 0;
  43039. int i = 0;
  43040. int j = 0;
  43041. XFILE f = XBADFILE;
  43042. DIST_POINT* dp = NULL;
  43043. DIST_POINT_NAME* dpn = NULL;
  43044. GENERAL_NAME* gn = NULL;
  43045. ASN1_IA5STRING* uri = NULL;
  43046. STACK_OF(DIST_POINT)* dps = NULL;
  43047. STACK_OF(GENERAL_NAME)* gns = NULL;
  43048. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  43049. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  43050. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  43051. if (f != XBADFILE)
  43052. XFCLOSE(f);
  43053. bufPt = buf;
  43054. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  43055. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  43056. NID_crl_distribution_points, NULL, NULL));
  43057. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  43058. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  43059. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  43060. ExpectNotNull(dpn = dp->distpoint);
  43061. /* this should be type 0, fullname */
  43062. ExpectIntEQ(dpn->type, 0);
  43063. ExpectNotNull(gns = dp->distpoint->name.fullname);
  43064. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  43065. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  43066. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  43067. ExpectIntEQ(gn->type, GEN_URI);
  43068. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  43069. ExpectNotNull(uri->data);
  43070. ExpectIntGT(uri->length, 0);
  43071. }
  43072. }
  43073. X509_free(x509);
  43074. CRL_DIST_POINTS_free(dps);
  43075. #endif
  43076. return EXPECT_RESULT();
  43077. }
  43078. static int test_wolfSSL_verify_mode(void)
  43079. {
  43080. EXPECT_DECLS;
  43081. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43082. WOLFSSL* ssl = NULL;
  43083. WOLFSSL_CTX* ctx = NULL;
  43084. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43085. ExpectNotNull(ssl = SSL_new(ctx));
  43086. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  43087. SSL_free(ssl);
  43088. ssl = NULL;
  43089. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43090. ExpectNotNull(ssl = SSL_new(ctx));
  43091. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  43092. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  43093. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  43094. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  43095. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  43096. SSL_free(ssl);
  43097. ssl = NULL;
  43098. wolfSSL_CTX_set_verify(ctx,
  43099. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  43100. ExpectNotNull(ssl = SSL_new(ctx));
  43101. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  43102. ExpectIntEQ(SSL_get_verify_mode(ssl),
  43103. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  43104. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  43105. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  43106. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  43107. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  43108. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  43109. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  43110. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  43111. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  43112. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  43113. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  43114. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  43115. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  43116. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  43117. #endif
  43118. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  43119. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  43120. SSL_free(ssl);
  43121. SSL_CTX_free(ctx);
  43122. #endif
  43123. return EXPECT_RESULT();
  43124. }
  43125. static int test_wolfSSL_verify_depth(void)
  43126. {
  43127. EXPECT_DECLS;
  43128. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  43129. WOLFSSL* ssl = NULL;
  43130. WOLFSSL_CTX* ctx = NULL;
  43131. long depth;
  43132. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43133. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  43134. ExpectNotNull(ssl = SSL_new(ctx));
  43135. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  43136. SSL_free(ssl);
  43137. ssl = NULL;
  43138. SSL_CTX_set_verify_depth(ctx, -1);
  43139. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  43140. SSL_CTX_set_verify_depth(ctx, 2);
  43141. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  43142. ExpectNotNull(ssl = SSL_new(ctx));
  43143. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  43144. SSL_free(ssl);
  43145. SSL_CTX_free(ctx);
  43146. #endif
  43147. return EXPECT_RESULT();
  43148. }
  43149. static int test_wolfSSL_verify_result(void)
  43150. {
  43151. EXPECT_DECLS;
  43152. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  43153. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  43154. WOLFSSL* ssl = NULL;
  43155. WOLFSSL_CTX* ctx = NULL;
  43156. long result = 0xDEADBEEF;
  43157. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  43158. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43159. ExpectNotNull(ssl = SSL_new(ctx));
  43160. wolfSSL_set_verify_result(ssl, result);
  43161. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  43162. SSL_free(ssl);
  43163. SSL_CTX_free(ctx);
  43164. #endif
  43165. return EXPECT_RESULT();
  43166. }
  43167. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  43168. static void sslMsgCb(int w, int version, int type, const void* buf,
  43169. size_t sz, SSL* ssl, void* arg)
  43170. {
  43171. int i;
  43172. unsigned char* pt = (unsigned char*)buf;
  43173. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  43174. (w)?"Writing":"Reading", (int)sz, version, type);
  43175. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  43176. fprintf(stderr, "\n");
  43177. (void)ssl;
  43178. (void)arg;
  43179. }
  43180. #endif /* OPENSSL_EXTRA */
  43181. static int test_wolfSSL_msg_callback(void)
  43182. {
  43183. EXPECT_DECLS;
  43184. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  43185. WOLFSSL* ssl = NULL;
  43186. WOLFSSL_CTX* ctx = NULL;
  43187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43188. ExpectNotNull(ssl = SSL_new(ctx));
  43189. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  43190. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  43191. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  43192. SSL_free(ssl);
  43193. SSL_CTX_free(ctx);
  43194. #endif
  43195. return EXPECT_RESULT();
  43196. }
  43197. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  43198. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  43199. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  43200. static void binary_dump(void *ptr, int size)
  43201. {
  43202. #ifdef WOLFSSL_EVP_PRINT
  43203. int i = 0;
  43204. unsigned char *p = (unsigned char *) ptr;
  43205. fprintf(stderr, "{");
  43206. while ((p != NULL) && (i < size)) {
  43207. if ((i % 8) == 0) {
  43208. fprintf(stderr, "\n");
  43209. fprintf(stderr, " ");
  43210. }
  43211. fprintf(stderr, "0x%02x, ", p[i]);
  43212. i++;
  43213. }
  43214. fprintf(stderr, "\n};\n");
  43215. #else
  43216. (void) ptr;
  43217. (void) size;
  43218. #endif
  43219. }
  43220. static int last_val = 0x0f;
  43221. static int check_result(unsigned char *data, int len)
  43222. {
  43223. int i;
  43224. for ( ; len; ) {
  43225. last_val = (last_val + 1) % 16;
  43226. for (i = 0; i < 16; len--, i++, data++)
  43227. if (*data != last_val) {
  43228. return -1;
  43229. }
  43230. }
  43231. return 0;
  43232. }
  43233. static int r_offset;
  43234. static int w_offset;
  43235. static void init_offset(void)
  43236. {
  43237. r_offset = 0;
  43238. w_offset = 0;
  43239. }
  43240. static void get_record(unsigned char *data, unsigned char *buf, int len)
  43241. {
  43242. XMEMCPY(buf, data+r_offset, len);
  43243. r_offset += len;
  43244. }
  43245. static void set_record(unsigned char *data, unsigned char *buf, int len)
  43246. {
  43247. XMEMCPY(data+w_offset, buf, len);
  43248. w_offset += len;
  43249. }
  43250. static void set_plain(unsigned char *plain, int rec)
  43251. {
  43252. int i, j;
  43253. unsigned char *p = plain;
  43254. #define BLOCKSZ 16
  43255. for (i=0; i<(rec/BLOCKSZ); i++) {
  43256. for (j=0; j<BLOCKSZ; j++)
  43257. *p++ = (i % 16);
  43258. }
  43259. }
  43260. #endif
  43261. static int test_wolfSSL_EVP_Cipher_extra(void)
  43262. {
  43263. EXPECT_DECLS;
  43264. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  43265. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  43266. /* aes128-cbc, keylen=16, ivlen=16 */
  43267. byte aes128_cbc_key[] = {
  43268. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  43269. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  43270. };
  43271. byte aes128_cbc_iv[] = {
  43272. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  43273. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  43274. };
  43275. /* teset data size table */
  43276. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  43277. int test_drive2[] = {8, 3, 8, 512, 0};
  43278. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  43279. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  43280. int test_drive_len[100];
  43281. int ret = 0;
  43282. EVP_CIPHER_CTX *evp = NULL;
  43283. int ilen = 0;
  43284. int klen = 0;
  43285. int i, j;
  43286. const EVP_CIPHER *type;
  43287. byte *iv;
  43288. byte *key;
  43289. int ivlen;
  43290. int keylen;
  43291. #define RECORDS 16
  43292. #define BUFFSZ 512
  43293. byte plain [BUFFSZ * RECORDS];
  43294. byte cipher[BUFFSZ * RECORDS];
  43295. byte inb[BUFFSZ];
  43296. byte outb[BUFFSZ+16];
  43297. int outl = 0;
  43298. int inl;
  43299. iv = aes128_cbc_iv;
  43300. ivlen = sizeof(aes128_cbc_iv);
  43301. key = aes128_cbc_key;
  43302. keylen = sizeof(aes128_cbc_key);
  43303. type = EVP_aes_128_cbc();
  43304. set_plain(plain, BUFFSZ * RECORDS);
  43305. SSL_library_init();
  43306. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  43307. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  43308. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  43309. klen = EVP_CIPHER_CTX_key_length(evp);
  43310. if (klen > 0 && keylen != klen) {
  43311. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  43312. }
  43313. ilen = EVP_CIPHER_CTX_iv_length(evp);
  43314. if (ilen > 0 && ivlen != ilen) {
  43315. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  43316. }
  43317. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  43318. for (j = 0; j<RECORDS; j++)
  43319. {
  43320. inl = BUFFSZ;
  43321. get_record(plain, inb, inl);
  43322. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  43323. set_record(cipher, outb, outl);
  43324. }
  43325. for (i = 0; test_drive[i]; i++) {
  43326. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  43327. init_offset();
  43328. test_drive_len[i] = 0;
  43329. for (j = 0; test_drive[i][j]; j++)
  43330. {
  43331. inl = test_drive[i][j];
  43332. test_drive_len[i] += inl;
  43333. get_record(plain, inb, inl);
  43334. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  43335. 0);
  43336. /* output to cipher buffer, so that following Dec test can detect
  43337. if any error */
  43338. set_record(cipher, outb, outl);
  43339. }
  43340. EVP_CipherFinal(evp, outb, &outl);
  43341. if (outl > 0)
  43342. set_record(cipher, outb, outl);
  43343. }
  43344. for (i = 0; test_drive[i]; i++) {
  43345. last_val = 0x0f;
  43346. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  43347. init_offset();
  43348. for (j = 0; test_drive[i][j]; j++) {
  43349. inl = test_drive[i][j];
  43350. get_record(cipher, inb, inl);
  43351. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  43352. 0);
  43353. binary_dump(outb, outl);
  43354. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  43355. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  43356. }
  43357. ret = EVP_CipherFinal(evp, outb, &outl);
  43358. binary_dump(outb, outl);
  43359. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  43360. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  43361. ExpectTrue(ret);
  43362. }
  43363. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  43364. EVP_CIPHER_CTX_free(evp);
  43365. evp = NULL;
  43366. /* Do an extra test to verify correct behavior with empty input. */
  43367. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  43368. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  43369. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  43370. klen = EVP_CIPHER_CTX_key_length(evp);
  43371. if (klen > 0 && keylen != klen) {
  43372. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  43373. }
  43374. ilen = EVP_CIPHER_CTX_iv_length(evp);
  43375. if (ilen > 0 && ivlen != ilen) {
  43376. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  43377. }
  43378. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  43379. /* outl should be set to 0 after passing NULL, 0 for input args. */
  43380. outl = -1;
  43381. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  43382. ExpectIntEQ(outl, 0);
  43383. EVP_CIPHER_CTX_free(evp);
  43384. #endif /* test_EVP_Cipher */
  43385. return EXPECT_RESULT();
  43386. }
  43387. static int test_wolfSSL_PEM_read_DHparams(void)
  43388. {
  43389. EXPECT_DECLS;
  43390. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  43391. !defined(NO_FILESYSTEM)
  43392. DH* dh = NULL;
  43393. XFILE fp = XBADFILE;
  43394. unsigned char derOut[300];
  43395. unsigned char* derOutBuf = derOut;
  43396. int derOutSz = 0;
  43397. unsigned char derExpected[300];
  43398. int derExpectedSz = 0;
  43399. XMEMSET(derOut, 0, sizeof(derOut));
  43400. XMEMSET(derExpected, 0, sizeof(derExpected));
  43401. /* open DH param file, read into DH struct */
  43402. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  43403. /* bad args */
  43404. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  43405. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  43406. /* good args */
  43407. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  43408. if (fp != XBADFILE) {
  43409. XFCLOSE(fp);
  43410. fp = XBADFILE;
  43411. }
  43412. /* read in certs/dh2048.der for comparison against exported params */
  43413. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  43414. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  43415. fp), 0);
  43416. if (fp != XBADFILE) {
  43417. XFCLOSE(fp);
  43418. fp = XBADFILE;
  43419. }
  43420. /* export DH back to DER and compare */
  43421. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  43422. ExpectIntEQ(derOutSz, derExpectedSz);
  43423. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  43424. DH_free(dh);
  43425. dh = NULL;
  43426. /* Test parsing with X9.42 header */
  43427. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  43428. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  43429. if (fp != XBADFILE)
  43430. XFCLOSE(fp);
  43431. DH_free(dh);
  43432. dh = NULL;
  43433. #endif
  43434. return EXPECT_RESULT();
  43435. }
  43436. static int test_wolfSSL_X509_get_serialNumber(void)
  43437. {
  43438. EXPECT_DECLS;
  43439. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  43440. ASN1_INTEGER* a = NULL;
  43441. BIGNUM* bn = NULL;
  43442. X509* x509 = NULL;
  43443. char *serialHex = NULL;
  43444. byte serial[3];
  43445. int serialSz;
  43446. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43447. SSL_FILETYPE_PEM));
  43448. ExpectNotNull(a = X509_get_serialNumber(x509));
  43449. /* check on value of ASN1 Integer */
  43450. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  43451. a = NULL;
  43452. /* test setting serial number and then retrieving it */
  43453. ExpectNotNull(a = ASN1_INTEGER_new());
  43454. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  43455. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  43456. serialSz = sizeof(serial);
  43457. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  43458. WOLFSSL_SUCCESS);
  43459. ExpectIntEQ(serialSz, 1);
  43460. ExpectIntEQ(serial[0], 3);
  43461. ASN1_INTEGER_free(a);
  43462. a = NULL;
  43463. /* test setting serial number with 0's in it */
  43464. serial[0] = 0x01;
  43465. serial[1] = 0x00;
  43466. serial[2] = 0x02;
  43467. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  43468. if (a != NULL) {
  43469. a->data[0] = ASN_INTEGER;
  43470. a->data[1] = sizeof(serial);
  43471. XMEMCPY(&a->data[2], serial, sizeof(serial));
  43472. a->length = sizeof(serial) + 2;
  43473. }
  43474. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  43475. XMEMSET(serial, 0, sizeof(serial));
  43476. serialSz = sizeof(serial);
  43477. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  43478. WOLFSSL_SUCCESS);
  43479. ExpectIntEQ(serialSz, 3);
  43480. ExpectIntEQ(serial[0], 0x01);
  43481. ExpectIntEQ(serial[1], 0x00);
  43482. ExpectIntEQ(serial[2], 0x02);
  43483. ASN1_INTEGER_free(a);
  43484. a = NULL;
  43485. X509_free(x509); /* free's a */
  43486. ExpectNotNull(serialHex = BN_bn2hex(bn));
  43487. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  43488. ExpectStrEQ(serialHex, "01");
  43489. #else
  43490. ExpectStrEQ(serialHex, "1");
  43491. #endif
  43492. OPENSSL_free(serialHex);
  43493. ExpectIntEQ(BN_get_word(bn), 1);
  43494. BN_free(bn);
  43495. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  43496. ExpectNotNull(a = ASN1_INTEGER_new());
  43497. if (a != NULL) {
  43498. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  43499. DYNAMIC_TYPE_OPENSSL));
  43500. a->isDynamic = 1;
  43501. ASN1_INTEGER_free(a);
  43502. }
  43503. #endif
  43504. return EXPECT_RESULT();
  43505. }
  43506. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  43507. {
  43508. EXPECT_DECLS;
  43509. #if defined(OPENSSL_EXTRA)
  43510. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  43511. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  43512. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  43513. #endif
  43514. return EXPECT_RESULT();
  43515. }
  43516. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  43517. {
  43518. EXPECT_DECLS;
  43519. #if defined(OPENSSL_EXTRA)
  43520. #define MAX_HEXSTR_BUFSZ 9
  43521. #define NUM_CASES 5
  43522. struct Output {
  43523. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  43524. long ret;
  43525. };
  43526. int i;
  43527. int j;
  43528. const char* inputs[NUM_CASES] = {
  43529. "aabcd1357e",
  43530. "01:12:23:34:a5:b6:c7:d8:e9",
  43531. ":01:02",
  43532. "012",
  43533. ":ab:ac:d"
  43534. };
  43535. struct Output expectedOutputs[NUM_CASES] = {
  43536. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  43537. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  43538. {{0x01, 0x02}, 2},
  43539. {{0x00}, 0},
  43540. {{0x00}, 0}
  43541. };
  43542. long len = 0;
  43543. unsigned char* returnedBuf = NULL;
  43544. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  43545. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  43546. if (returnedBuf == NULL) {
  43547. ExpectIntEQ(expectedOutputs[i].ret, 0);
  43548. continue;
  43549. }
  43550. ExpectIntEQ(expectedOutputs[i].ret, len);
  43551. for (j = 0; j < len; ++j) {
  43552. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  43553. }
  43554. OPENSSL_free(returnedBuf);
  43555. returnedBuf = NULL;
  43556. }
  43557. #endif
  43558. return EXPECT_RESULT();
  43559. }
  43560. static int test_wolfSSL_X509_CA_num(void)
  43561. {
  43562. EXPECT_DECLS;
  43563. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  43564. defined(HAVE_ECC) && !defined(NO_RSA)
  43565. WOLFSSL_X509_STORE *store = NULL;
  43566. WOLFSSL_X509 *x509_1 = NULL;
  43567. WOLFSSL_X509 *x509_2 = NULL;
  43568. int ca_num = 0;
  43569. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  43570. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43571. WOLFSSL_FILETYPE_PEM));
  43572. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  43573. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  43574. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  43575. WOLFSSL_FILETYPE_PEM));
  43576. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  43577. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  43578. wolfSSL_X509_free(x509_1);
  43579. wolfSSL_X509_free(x509_2);
  43580. wolfSSL_X509_STORE_free(store);
  43581. #endif
  43582. return EXPECT_RESULT();
  43583. }
  43584. static int test_wolfSSL_X509_check_ca(void)
  43585. {
  43586. EXPECT_DECLS;
  43587. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  43588. WOLFSSL_X509 *x509 = NULL;
  43589. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43590. WOLFSSL_FILETYPE_PEM));
  43591. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  43592. wolfSSL_X509_free(x509);
  43593. #endif
  43594. return EXPECT_RESULT();
  43595. }
  43596. static int test_wolfSSL_X509_check_ip_asc(void)
  43597. {
  43598. EXPECT_DECLS;
  43599. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  43600. WOLFSSL_X509 *x509 = NULL;
  43601. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  43602. WOLFSSL_FILETYPE_PEM));
  43603. #if 0
  43604. /* TODO: add cert gen for testing positive case */
  43605. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  43606. #endif
  43607. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  43608. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  43609. wolfSSL_X509_free(x509);
  43610. #endif
  43611. return EXPECT_RESULT();
  43612. }
  43613. static int test_wolfSSL_make_cert(void)
  43614. {
  43615. EXPECT_DECLS;
  43616. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  43617. defined(WOLFSSL_CERT_EXT)
  43618. int ret = 0;
  43619. Cert cert;
  43620. CertName name;
  43621. RsaKey key;
  43622. WC_RNG rng;
  43623. byte der[FOURK_BUF];
  43624. word32 idx = 0;
  43625. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  43626. #ifdef OPENSSL_EXTRA
  43627. const unsigned char* pt = NULL;
  43628. int certSz = 0;
  43629. X509* x509 = NULL;
  43630. X509_NAME* x509name = NULL;
  43631. X509_NAME_ENTRY* entry = NULL;
  43632. ASN1_STRING* entryValue = NULL;
  43633. #endif
  43634. XMEMSET(&name, 0, sizeof(CertName));
  43635. /* set up cert name */
  43636. XMEMCPY(name.country, "US", sizeof("US"));
  43637. name.countryEnc = CTC_PRINTABLE;
  43638. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  43639. name.stateEnc = CTC_UTF8;
  43640. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  43641. name.localityEnc = CTC_UTF8;
  43642. XMEMCPY(name.sur, "Test", sizeof("Test"));
  43643. name.surEnc = CTC_UTF8;
  43644. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  43645. name.orgEnc = CTC_UTF8;
  43646. XMEMCPY(name.unit, "Development", sizeof("Development"));
  43647. name.unitEnc = CTC_UTF8;
  43648. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  43649. name.commonNameEnc = CTC_UTF8;
  43650. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  43651. name.serialDevEnc = CTC_PRINTABLE;
  43652. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  43653. name.userIdEnc = CTC_PRINTABLE;
  43654. #ifdef WOLFSSL_MULTI_ATTRIB
  43655. #if CTC_MAX_ATTRIB > 2
  43656. {
  43657. NameAttrib* n;
  43658. n = &name.name[0];
  43659. n->id = ASN_DOMAIN_COMPONENT;
  43660. n->type = CTC_UTF8;
  43661. n->sz = sizeof("com");
  43662. XMEMCPY(n->value, "com", sizeof("com"));
  43663. n = &name.name[1];
  43664. n->id = ASN_DOMAIN_COMPONENT;
  43665. n->type = CTC_UTF8;
  43666. n->sz = sizeof("wolfssl");
  43667. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  43668. }
  43669. #endif
  43670. #endif /* WOLFSSL_MULTI_ATTRIB */
  43671. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  43672. #ifndef HAVE_FIPS
  43673. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  43674. #else
  43675. ExpectIntEQ(wc_InitRng(&rng), 0);
  43676. #endif
  43677. /* load test RSA key */
  43678. idx = 0;
  43679. #if defined(USE_CERT_BUFFERS_1024)
  43680. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  43681. sizeof_server_key_der_1024), 0);
  43682. #elif defined(USE_CERT_BUFFERS_2048)
  43683. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  43684. sizeof_server_key_der_2048), 0);
  43685. #else
  43686. /* error case, no RSA key loaded, happens later */
  43687. (void)idx;
  43688. #endif
  43689. XMEMSET(&cert, 0 , sizeof(Cert));
  43690. ExpectIntEQ(wc_InitCert(&cert), 0);
  43691. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  43692. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  43693. cert.serialSz = (int)sizeof(mySerial);
  43694. cert.isCA = 1;
  43695. #ifndef NO_SHA256
  43696. cert.sigType = CTC_SHA256wRSA;
  43697. #else
  43698. cert.sigType = CTC_SHAwRSA;
  43699. #endif
  43700. /* add SKID from the Public Key */
  43701. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  43702. /* add AKID from the Public Key */
  43703. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  43704. ret = 0;
  43705. do {
  43706. #if defined(WOLFSSL_ASYNC_CRYPT)
  43707. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  43708. #endif
  43709. if (ret >= 0) {
  43710. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  43711. }
  43712. } while (ret == WC_PENDING_E);
  43713. ExpectIntGT(ret, 0);
  43714. #ifdef OPENSSL_EXTRA
  43715. /* der holds a certificate with DC's now check X509 parsing of it */
  43716. certSz = ret;
  43717. pt = der;
  43718. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  43719. ExpectNotNull(x509name = X509_get_subject_name(x509));
  43720. #ifdef WOLFSSL_MULTI_ATTRIB
  43721. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43722. -1)), 5);
  43723. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43724. (int)idx)), 6);
  43725. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43726. (int)idx)), -1);
  43727. #endif /* WOLFSSL_MULTI_ATTRIB */
  43728. /* compare DN at index 0 */
  43729. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  43730. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43731. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  43732. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  43733. #ifndef WOLFSSL_MULTI_ATTRIB
  43734. /* compare Serial Number */
  43735. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  43736. -1)), 7);
  43737. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43738. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43739. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  43740. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  43741. #endif
  43742. #ifdef WOLFSSL_MULTI_ATTRIB
  43743. /* get first and second DC and compare result */
  43744. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43745. -1)), 5);
  43746. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  43747. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43748. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  43749. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43750. (int)idx)), 6);
  43751. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  43752. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43753. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  43754. #endif /* WOLFSSL_MULTI_ATTRIB */
  43755. /* try invalid index locations for regression test and sanity check */
  43756. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  43757. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  43758. X509_free(x509);
  43759. #endif /* OPENSSL_EXTRA */
  43760. wc_FreeRsaKey(&key);
  43761. wc_FreeRng(&rng);
  43762. #endif
  43763. return EXPECT_RESULT();
  43764. }
  43765. static int test_wolfSSL_X509_get_version(void)
  43766. {
  43767. EXPECT_DECLS;
  43768. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43769. WOLFSSL_X509 *x509 = NULL;
  43770. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43771. WOLFSSL_FILETYPE_PEM));
  43772. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  43773. wolfSSL_X509_free(x509);
  43774. #endif
  43775. return EXPECT_RESULT();
  43776. }
  43777. #if defined(OPENSSL_ALL)
  43778. static int test_wolfSSL_sk_CIPHER_description(void)
  43779. {
  43780. EXPECT_DECLS;
  43781. #if !defined(NO_RSA)
  43782. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43783. int i;
  43784. int numCiphers = 0;
  43785. const SSL_METHOD *method = NULL;
  43786. const SSL_CIPHER *cipher = NULL;
  43787. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43788. SSL_CTX *ctx = NULL;
  43789. SSL *ssl = NULL;
  43790. char buf[256];
  43791. char test_str[9] = "0000000";
  43792. const char badStr[] = "unknown";
  43793. const char certPath[] = "./certs/client-cert.pem";
  43794. XMEMSET(buf, 0, sizeof(buf));
  43795. ExpectNotNull(method = TLSv1_2_client_method());
  43796. ExpectNotNull(ctx = SSL_CTX_new(method));
  43797. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43798. SSL_CTX_set_verify_depth(ctx, 4);
  43799. SSL_CTX_set_options(ctx, flags);
  43800. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43801. WOLFSSL_SUCCESS);
  43802. ExpectNotNull(ssl = SSL_new(ctx));
  43803. /* SSL_get_ciphers returns a stack of all configured ciphers
  43804. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  43805. */
  43806. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43807. /* loop through the amount of supportedCiphers */
  43808. numCiphers = sk_num(supportedCiphers);
  43809. for (i = 0; i < numCiphers; ++i) {
  43810. int j;
  43811. /* sk_value increments "sk->data.cipher->cipherOffset".
  43812. * wolfSSL_sk_CIPHER_description sets the description for
  43813. * the cipher based on the provided offset.
  43814. */
  43815. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43816. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43817. }
  43818. /* Search cipher description string for "unknown" descriptor */
  43819. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  43820. int k = 0;
  43821. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  43822. test_str[k] = badStr[k];
  43823. j++;
  43824. k++;
  43825. }
  43826. }
  43827. /* Fail if test_str == badStr == "unknown" */
  43828. ExpectStrNE(test_str,badStr);
  43829. }
  43830. SSL_free(ssl);
  43831. SSL_CTX_free(ctx);
  43832. #endif
  43833. return EXPECT_RESULT();
  43834. }
  43835. static int test_wolfSSL_get_ciphers_compat(void)
  43836. {
  43837. EXPECT_DECLS;
  43838. #if !defined(NO_RSA)
  43839. const SSL_METHOD *method = NULL;
  43840. const char certPath[] = "./certs/client-cert.pem";
  43841. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43842. SSL_CTX *ctx = NULL;
  43843. WOLFSSL *ssl = NULL;
  43844. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43845. ExpectNotNull(method = SSLv23_client_method());
  43846. ExpectNotNull(ctx = SSL_CTX_new(method));
  43847. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43848. SSL_CTX_set_verify_depth(ctx, 4);
  43849. SSL_CTX_set_options(ctx, flags);
  43850. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43851. WOLFSSL_SUCCESS);
  43852. ExpectNotNull(ssl = SSL_new(ctx));
  43853. /* Test Bad NULL input */
  43854. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  43855. /* Test for Good input */
  43856. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43857. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  43858. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  43859. SSL_free(ssl);
  43860. SSL_CTX_free(ctx);
  43861. #endif
  43862. return EXPECT_RESULT();
  43863. }
  43864. static int test_wolfSSL_X509_PUBKEY_get(void)
  43865. {
  43866. EXPECT_DECLS;
  43867. WOLFSSL_X509_PUBKEY pubkey;
  43868. WOLFSSL_X509_PUBKEY* key;
  43869. WOLFSSL_EVP_PKEY evpkey ;
  43870. WOLFSSL_EVP_PKEY* evpPkey;
  43871. WOLFSSL_EVP_PKEY* retEvpPkey;
  43872. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  43873. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  43874. key = &pubkey;
  43875. evpPkey = &evpkey;
  43876. evpPkey->type = WOLFSSL_SUCCESS;
  43877. key->pkey = evpPkey;
  43878. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43879. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  43880. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  43881. key->pkey = NULL;
  43882. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43883. return EXPECT_RESULT();
  43884. }
  43885. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  43886. {
  43887. EXPECT_DECLS;
  43888. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43889. DSA *dsa = NULL;
  43890. DSA *setDsa = NULL;
  43891. EVP_PKEY *pkey = NULL;
  43892. EVP_PKEY *set1Pkey = NULL;
  43893. SHA_CTX sha;
  43894. byte signature[DSA_SIG_SIZE];
  43895. byte hash[WC_SHA_DIGEST_SIZE];
  43896. word32 bytes;
  43897. int answer;
  43898. #ifdef USE_CERT_BUFFERS_1024
  43899. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  43900. int dsaKeySz = sizeof_dsa_key_der_1024;
  43901. byte tmp[ONEK_BUF];
  43902. XMEMSET(tmp, 0, sizeof(tmp));
  43903. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43904. bytes = dsaKeySz;
  43905. #elif defined(USE_CERT_BUFFERS_2048)
  43906. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  43907. int dsaKeySz = sizeof_dsa_key_der_2048;
  43908. byte tmp[TWOK_BUF];
  43909. XMEMSET(tmp, 0, sizeof(tmp));
  43910. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43911. bytes = (word32)dsaKeySz;
  43912. #else
  43913. byte tmp[TWOK_BUF];
  43914. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  43915. int dsaKeySz;
  43916. XFILE fp = XBADFILE;
  43917. XMEMSET(tmp, 0, sizeof(tmp));
  43918. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  43919. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  43920. if (fp != XBADFILE)
  43921. XFCLOSE(fp);
  43922. #endif /* END USE_CERT_BUFFERS_1024 */
  43923. /* Create hash to later Sign and Verify */
  43924. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  43925. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  43926. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  43927. /* Initialize pkey with der format dsa key */
  43928. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  43929. (long)dsaKeySz));
  43930. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  43931. /* Should Fail: NULL argument */
  43932. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  43933. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  43934. /* Should Pass: Initialized pkey argument */
  43935. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  43936. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  43937. #ifdef USE_CERT_BUFFERS_1024
  43938. ExpectIntEQ(DSA_bits(dsa), 1024);
  43939. #else
  43940. ExpectIntEQ(DSA_bits(dsa), 2048);
  43941. #endif
  43942. /* Sign */
  43943. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  43944. /* Verify. */
  43945. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  43946. WOLFSSL_SUCCESS);
  43947. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  43948. /* Should Fail: set1Pkey not initialized */
  43949. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43950. /* Initialize set1Pkey */
  43951. set1Pkey = EVP_PKEY_new();
  43952. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  43953. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  43954. WOLFSSL_SUCCESS);
  43955. /* Should Pass: set dsa into set1Pkey */
  43956. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43957. DSA_free(dsa);
  43958. DSA_free(setDsa);
  43959. EVP_PKEY_free(pkey);
  43960. EVP_PKEY_free(set1Pkey);
  43961. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43962. return EXPECT_RESULT();
  43963. } /* END test_EVP_PKEY_set1_get1_DSA */
  43964. static int test_wolfSSL_DSA_generate_parameters(void)
  43965. {
  43966. EXPECT_DECLS;
  43967. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43968. !defined(HAVE_FIPS)
  43969. DSA *dsa = NULL;
  43970. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  43971. NULL));
  43972. DSA_free(dsa);
  43973. #endif
  43974. return EXPECT_RESULT();
  43975. }
  43976. static int test_wolfSSL_DSA_SIG(void)
  43977. {
  43978. EXPECT_DECLS;
  43979. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43980. !defined(HAVE_FIPS)
  43981. DSA *dsa = NULL;
  43982. DSA *dsa2 = NULL;
  43983. DSA_SIG *sig = NULL;
  43984. const BIGNUM *p = NULL;
  43985. const BIGNUM *q = NULL;
  43986. const BIGNUM *g = NULL;
  43987. const BIGNUM *pub = NULL;
  43988. const BIGNUM *priv = NULL;
  43989. BIGNUM *dup_p = NULL;
  43990. BIGNUM *dup_q = NULL;
  43991. BIGNUM *dup_g = NULL;
  43992. BIGNUM *dup_pub = NULL;
  43993. BIGNUM *dup_priv = NULL;
  43994. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  43995. ExpectNotNull(dsa = DSA_new());
  43996. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  43997. NULL), 1);
  43998. ExpectIntEQ(DSA_generate_key(dsa), 1);
  43999. DSA_get0_pqg(dsa, &p, &q, &g);
  44000. DSA_get0_key(dsa, &pub, &priv);
  44001. ExpectNotNull(dup_p = BN_dup(p));
  44002. ExpectNotNull(dup_q = BN_dup(q));
  44003. ExpectNotNull(dup_g = BN_dup(g));
  44004. ExpectNotNull(dup_pub = BN_dup(pub));
  44005. ExpectNotNull(dup_priv = BN_dup(priv));
  44006. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  44007. ExpectNotNull(dsa2 = DSA_new());
  44008. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  44009. if (EXPECT_FAIL()) {
  44010. BN_free(dup_p);
  44011. BN_free(dup_q);
  44012. BN_free(dup_g);
  44013. }
  44014. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  44015. if (EXPECT_FAIL()) {
  44016. BN_free(dup_pub);
  44017. BN_free(dup_priv);
  44018. }
  44019. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  44020. DSA_free(dsa);
  44021. DSA_free(dsa2);
  44022. DSA_SIG_free(sig);
  44023. #endif
  44024. return EXPECT_RESULT();
  44025. }
  44026. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  44027. {
  44028. EXPECT_DECLS;
  44029. #ifdef HAVE_ECC
  44030. WOLFSSL_EC_KEY* ecKey = NULL;
  44031. WOLFSSL_EC_KEY* ecGet1 = NULL;
  44032. EVP_PKEY* pkey = NULL;
  44033. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44034. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44035. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  44036. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  44037. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  44038. /* Should fail since ecKey is empty */
  44039. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  44040. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44041. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  44042. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  44043. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  44044. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  44045. wolfSSL_EC_KEY_free(ecKey);
  44046. wolfSSL_EC_KEY_free(ecGet1);
  44047. EVP_PKEY_free(pkey);
  44048. #endif /* HAVE_ECC */
  44049. return EXPECT_RESULT();
  44050. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  44051. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  44052. {
  44053. EXPECT_DECLS;
  44054. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  44055. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44056. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  44057. DH *dh = NULL;
  44058. DH *setDh = NULL;
  44059. EVP_PKEY *pkey = NULL;
  44060. XFILE f = XBADFILE;
  44061. unsigned char buf[4096];
  44062. const unsigned char* pt = buf;
  44063. const char* dh2048 = "./certs/dh2048.der";
  44064. long len = 0;
  44065. int code = -1;
  44066. XMEMSET(buf, 0, sizeof(buf));
  44067. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  44068. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44069. if (f != XBADFILE)
  44070. XFCLOSE(f);
  44071. /* Load dh2048.der into DH with internal format */
  44072. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  44073. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  44074. ExpectIntEQ(code, 0);
  44075. code = -1;
  44076. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44077. /* Set DH into PKEY */
  44078. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  44079. /* Get DH from PKEY */
  44080. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  44081. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  44082. ExpectIntEQ(code, 0);
  44083. EVP_PKEY_free(pkey);
  44084. DH_free(setDh);
  44085. setDh = NULL;
  44086. DH_free(dh);
  44087. dh = NULL;
  44088. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  44089. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  44090. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  44091. return EXPECT_RESULT();
  44092. } /* END test_EVP_PKEY_set1_get1_DH */
  44093. static int test_wolfSSL_CTX_ctrl(void)
  44094. {
  44095. EXPECT_DECLS;
  44096. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  44097. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44098. char caFile[] = "./certs/client-ca.pem";
  44099. char clientFile[] = "./certs/client-cert.pem";
  44100. SSL_CTX* ctx = NULL;
  44101. X509* x509 = NULL;
  44102. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  44103. byte buf[6000];
  44104. char file[] = "./certs/dsaparams.pem";
  44105. XFILE f = XBADFILE;
  44106. int bytes = 0;
  44107. BIO* bio = NULL;
  44108. DSA* dsa = NULL;
  44109. DH* dh = NULL;
  44110. #endif
  44111. #ifdef HAVE_ECC
  44112. WOLFSSL_EC_KEY* ecKey = NULL;
  44113. #endif
  44114. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  44115. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  44116. WOLFSSL_FILETYPE_PEM));
  44117. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  44118. if (EXPECT_FAIL()) {
  44119. wolfSSL_X509_free(x509);
  44120. }
  44121. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  44122. WOLFSSL_FILETYPE_PEM));
  44123. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  44124. /* Initialize DH */
  44125. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  44126. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  44127. if (f != XBADFILE)
  44128. XFCLOSE(f);
  44129. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  44130. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  44131. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  44132. #endif
  44133. #ifdef HAVE_ECC
  44134. /* Initialize WOLFSSL_EC_KEY */
  44135. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44136. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44137. #endif
  44138. /* additional test of getting EVP_PKEY key size from X509
  44139. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  44140. * allowed with user RSA */
  44141. {
  44142. EVP_PKEY* pkey = NULL;
  44143. #if defined(HAVE_ECC)
  44144. X509* ecX509 = NULL;
  44145. #endif /* HAVE_ECC */
  44146. ExpectNotNull(pkey = X509_get_pubkey(x509));
  44147. /* current RSA key is 2048 bit (256 bytes) */
  44148. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  44149. EVP_PKEY_free(pkey);
  44150. pkey = NULL;
  44151. #if defined(HAVE_ECC)
  44152. #if defined(USE_CERT_BUFFERS_256)
  44153. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  44154. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  44155. SSL_FILETYPE_ASN1));
  44156. #else
  44157. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  44158. cliEccCertFile, SSL_FILETYPE_PEM));
  44159. #endif
  44160. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  44161. /* current ECC key is 256 bit (32 bytes) */
  44162. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  44163. X509_free(ecX509);
  44164. EVP_PKEY_free(pkey);
  44165. #endif /* HAVE_ECC */
  44166. }
  44167. /* Tests should fail with passed in NULL pointer */
  44168. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  44169. SSL_FAILURE);
  44170. #if !defined(NO_DH) && !defined(NO_DSA)
  44171. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  44172. SSL_FAILURE);
  44173. #endif
  44174. #ifdef HAVE_ECC
  44175. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  44176. SSL_FAILURE);
  44177. #endif
  44178. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  44179. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  44180. */
  44181. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  44182. SSL_SUCCESS);
  44183. if (EXPECT_FAIL()) {
  44184. wolfSSL_X509_free(x509);
  44185. }
  44186. /* Test with SSL_CTRL_OPTIONS
  44187. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  44188. */
  44189. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  44190. NULL) == SSL_OP_NO_TLSv1);
  44191. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  44192. /* Test with SSL_CTRL_SET_TMP_DH
  44193. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  44194. */
  44195. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  44196. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  44197. SSL_SUCCESS);
  44198. #endif
  44199. /* Test with SSL_CTRL_SET_TMP_ECDH
  44200. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  44201. */
  44202. #ifdef HAVE_ECC
  44203. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  44204. SSL_SUCCESS);
  44205. #endif
  44206. #ifdef WOLFSSL_ENCRYPTED_KEYS
  44207. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  44208. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  44209. #endif
  44210. /* Test for min/max proto */
  44211. #ifndef WOLFSSL_NO_TLS12
  44212. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  44213. 0, NULL), SSL_SUCCESS);
  44214. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  44215. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  44216. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  44217. #endif
  44218. #ifdef WOLFSSL_TLS13
  44219. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  44220. 0, NULL), SSL_SUCCESS);
  44221. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  44222. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  44223. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  44224. #ifndef WOLFSSL_NO_TLS12
  44225. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  44226. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  44227. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  44228. #endif
  44229. #endif
  44230. /* Cleanup and Pass */
  44231. #if !defined(NO_DH) && !defined(NO_DSA)
  44232. #ifndef NO_BIO
  44233. BIO_free(bio);
  44234. DSA_free(dsa);
  44235. DH_free(dh);
  44236. dh = NULL;
  44237. #endif
  44238. #endif
  44239. #ifdef HAVE_ECC
  44240. wolfSSL_EC_KEY_free(ecKey);
  44241. #endif
  44242. SSL_CTX_free(ctx);
  44243. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  44244. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  44245. return EXPECT_RESULT();
  44246. }
  44247. static int test_wolfSSL_EVP_PKEY_assign(void)
  44248. {
  44249. EXPECT_DECLS;
  44250. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  44251. int type;
  44252. WOLFSSL_EVP_PKEY* pkey = NULL;
  44253. #ifndef NO_RSA
  44254. WOLFSSL_RSA* rsa = NULL;
  44255. #endif
  44256. #ifndef NO_DSA
  44257. WOLFSSL_DSA* dsa = NULL;
  44258. #endif
  44259. #ifdef HAVE_ECC
  44260. WOLFSSL_EC_KEY* ecKey = NULL;
  44261. #endif
  44262. #ifndef NO_RSA
  44263. type = EVP_PKEY_RSA;
  44264. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44265. ExpectNotNull(rsa = wolfSSL_RSA_new());
  44266. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  44267. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  44268. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  44269. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  44270. if (EXPECT_FAIL()) {
  44271. wolfSSL_RSA_free(rsa);
  44272. }
  44273. wolfSSL_EVP_PKEY_free(pkey);
  44274. pkey = NULL;
  44275. #endif /* NO_RSA */
  44276. #ifndef NO_DSA
  44277. type = EVP_PKEY_DSA;
  44278. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44279. ExpectNotNull(dsa = wolfSSL_DSA_new());
  44280. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  44281. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  44282. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  44283. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  44284. if (EXPECT_FAIL()) {
  44285. wolfSSL_DSA_free(dsa);
  44286. }
  44287. wolfSSL_EVP_PKEY_free(pkey);
  44288. pkey = NULL;
  44289. #endif /* NO_DSA */
  44290. #ifdef HAVE_ECC
  44291. type = EVP_PKEY_EC;
  44292. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44293. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44294. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  44295. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  44296. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  44297. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  44298. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44299. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  44300. if (EXPECT_FAIL()) {
  44301. wolfSSL_EC_KEY_free(ecKey);
  44302. }
  44303. wolfSSL_EVP_PKEY_free(pkey);
  44304. pkey = NULL;
  44305. #endif /* HAVE_ECC */
  44306. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  44307. return EXPECT_RESULT();
  44308. }
  44309. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  44310. {
  44311. EXPECT_DECLS;
  44312. #if !defined(NO_DH) && \
  44313. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44314. XFILE f = XBADFILE;
  44315. unsigned char buf[4096];
  44316. const unsigned char* pt = buf;
  44317. const char* params1 = "./certs/dh2048.der";
  44318. long len = 0;
  44319. WOLFSSL_DH* dh = NULL;
  44320. WOLFSSL_EVP_PKEY* pkey = NULL;
  44321. XMEMSET(buf, 0, sizeof(buf));
  44322. /* Load DH parameters DER. */
  44323. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  44324. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44325. if (f != XBADFILE)
  44326. XFCLOSE(f);
  44327. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  44328. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  44329. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44330. /* Bad cases */
  44331. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  44332. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  44333. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  44334. /* Good case */
  44335. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  44336. if (EXPECT_FAIL()) {
  44337. wolfSSL_DH_free(dh);
  44338. }
  44339. EVP_PKEY_free(pkey);
  44340. #endif
  44341. return EXPECT_RESULT();
  44342. }
  44343. static int test_wolfSSL_EVP_PKEY_base_id(void)
  44344. {
  44345. EXPECT_DECLS;
  44346. WOLFSSL_EVP_PKEY* pkey = NULL;
  44347. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44348. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  44349. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  44350. EVP_PKEY_free(pkey);
  44351. return EXPECT_RESULT();
  44352. }
  44353. static int test_wolfSSL_EVP_PKEY_id(void)
  44354. {
  44355. EXPECT_DECLS;
  44356. WOLFSSL_EVP_PKEY* pkey = NULL;
  44357. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44358. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  44359. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  44360. EVP_PKEY_free(pkey);
  44361. return EXPECT_RESULT();
  44362. }
  44363. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  44364. {
  44365. EXPECT_DECLS;
  44366. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  44367. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  44368. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  44369. ECC_MIN_KEY_SZ <= 256)
  44370. EVP_PKEY_CTX* ctx = NULL;
  44371. EVP_PKEY* pkey = NULL;
  44372. /* Test error conditions. */
  44373. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  44374. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  44375. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  44376. #ifndef NO_RSA
  44377. EVP_PKEY_CTX_free(ctx);
  44378. /* Parameter generation for RSA not supported yet. */
  44379. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  44380. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  44381. #endif
  44382. #ifdef HAVE_ECC
  44383. EVP_PKEY_CTX_free(ctx);
  44384. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  44385. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  44386. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  44387. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  44388. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  44389. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  44390. WOLFSSL_SUCCESS);
  44391. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  44392. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  44393. #endif
  44394. EVP_PKEY_CTX_free(ctx);
  44395. EVP_PKEY_free(pkey);
  44396. #endif
  44397. return EXPECT_RESULT();
  44398. }
  44399. static int test_wolfSSL_EVP_PKEY_keygen(void)
  44400. {
  44401. EXPECT_DECLS;
  44402. WOLFSSL_EVP_PKEY* pkey = NULL;
  44403. EVP_PKEY_CTX* ctx = NULL;
  44404. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  44405. WOLFSSL_EVP_PKEY* params = NULL;
  44406. DH* dh = NULL;
  44407. const BIGNUM* pubkey = NULL;
  44408. const BIGNUM* privkey = NULL;
  44409. ASN1_INTEGER* asn1int = NULL;
  44410. unsigned int length = 0;
  44411. byte* derBuffer = NULL;
  44412. #endif
  44413. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44414. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44415. /* Bad cases */
  44416. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  44417. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  44418. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  44419. /* Good case */
  44420. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  44421. EVP_PKEY_CTX_free(ctx);
  44422. ctx = NULL;
  44423. EVP_PKEY_free(pkey);
  44424. pkey = NULL;
  44425. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  44426. /* Test DH keygen */
  44427. {
  44428. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  44429. ExpectNotNull(dh = DH_get_2048_256());
  44430. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  44431. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  44432. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  44433. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  44434. DH_free(dh);
  44435. dh = NULL;
  44436. EVP_PKEY_CTX_free(ctx);
  44437. EVP_PKEY_free(params);
  44438. /* try exporting generated key to DER, to verify */
  44439. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  44440. DH_get0_key(dh, &pubkey, &privkey);
  44441. ExpectNotNull(pubkey);
  44442. ExpectNotNull(privkey);
  44443. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  44444. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  44445. ASN1_INTEGER_free(asn1int);
  44446. DH_free(dh);
  44447. dh = NULL;
  44448. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44449. EVP_PKEY_free(pkey);
  44450. }
  44451. #endif
  44452. return EXPECT_RESULT();
  44453. }
  44454. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  44455. {
  44456. EXPECT_DECLS;
  44457. WOLFSSL_EVP_PKEY* pkey = NULL;
  44458. EVP_PKEY_CTX *ctx = NULL;
  44459. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44460. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44461. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  44462. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  44463. EVP_PKEY_CTX_free(ctx);
  44464. EVP_PKEY_free(pkey);
  44465. return EXPECT_RESULT();
  44466. }
  44467. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  44468. {
  44469. EXPECT_DECLS;
  44470. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  44471. WOLFSSL_EVP_PKEY* pkey = NULL;
  44472. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44473. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  44474. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  44475. EVP_PKEY_free(pkey);
  44476. #endif
  44477. return EXPECT_RESULT();
  44478. }
  44479. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  44480. {
  44481. EXPECT_DECLS;
  44482. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  44483. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  44484. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  44485. !defined(NO_FILESYSTEM)
  44486. WOLFSSL_EVP_PKEY* params = NULL;
  44487. WOLFSSL_EVP_PKEY* copy = NULL;
  44488. DH* dh = NULL;
  44489. BIGNUM* p1;
  44490. BIGNUM* g1;
  44491. BIGNUM* q1;
  44492. BIGNUM* p2;
  44493. BIGNUM* g2;
  44494. BIGNUM* q2;
  44495. /* create DH with DH_get_2048_256 params */
  44496. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  44497. ExpectNotNull(dh = DH_get_2048_256());
  44498. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  44499. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  44500. (const BIGNUM**)&q1,
  44501. (const BIGNUM**)&g1);
  44502. DH_free(dh);
  44503. dh = NULL;
  44504. /* create DH with random generated DH params */
  44505. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  44506. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  44507. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  44508. DH_free(dh);
  44509. dh = NULL;
  44510. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  44511. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  44512. ExpectNotNull(dh->p);
  44513. ExpectNotNull(dh->g);
  44514. ExpectNotNull(dh->q);
  44515. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  44516. (const BIGNUM**)&q2,
  44517. (const BIGNUM**)&g2);
  44518. ExpectIntEQ(BN_cmp(p1, p2), 0);
  44519. ExpectIntEQ(BN_cmp(q1, q2), 0);
  44520. ExpectIntEQ(BN_cmp(g1, g2), 0);
  44521. DH_free(dh);
  44522. dh = NULL;
  44523. EVP_PKEY_free(copy);
  44524. EVP_PKEY_free(params);
  44525. #endif
  44526. return EXPECT_RESULT();
  44527. }
  44528. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  44529. {
  44530. EXPECT_DECLS;
  44531. WOLFSSL_EVP_PKEY* pkey = NULL;
  44532. EVP_PKEY_CTX* ctx = NULL;
  44533. int bits = 2048;
  44534. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44535. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44536. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  44537. WOLFSSL_SUCCESS);
  44538. EVP_PKEY_CTX_free(ctx);
  44539. EVP_PKEY_free(pkey);
  44540. return EXPECT_RESULT();
  44541. }
  44542. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  44543. {
  44544. EXPECT_DECLS;
  44545. /* This is large enough to be used for all key sizes */
  44546. byte key[AES_256_KEY_SIZE] = {0};
  44547. byte iv[AES_BLOCK_SIZE] = {0};
  44548. int i;
  44549. int nids[] = {
  44550. #ifdef HAVE_AES_CBC
  44551. NID_aes_128_cbc,
  44552. #endif
  44553. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44554. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44555. #ifdef HAVE_AESGCM
  44556. NID_aes_128_gcm,
  44557. #endif
  44558. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44559. #ifdef WOLFSSL_AES_COUNTER
  44560. NID_aes_128_ctr,
  44561. #endif
  44562. #ifndef NO_DES3
  44563. NID_des_cbc,
  44564. NID_des_ede3_cbc,
  44565. #endif
  44566. };
  44567. int iv_lengths[] = {
  44568. #ifdef HAVE_AES_CBC
  44569. AES_BLOCK_SIZE,
  44570. #endif
  44571. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44572. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44573. #ifdef HAVE_AESGCM
  44574. GCM_NONCE_MID_SZ,
  44575. #endif
  44576. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44577. #ifdef WOLFSSL_AES_COUNTER
  44578. AES_BLOCK_SIZE,
  44579. #endif
  44580. #ifndef NO_DES3
  44581. DES_BLOCK_SIZE,
  44582. DES_BLOCK_SIZE,
  44583. #endif
  44584. };
  44585. int nidsLen = (sizeof(nids)/sizeof(int));
  44586. for (i = 0; i < nidsLen; i++) {
  44587. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  44588. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  44589. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44590. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44591. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  44592. EVP_CIPHER_CTX_free(ctx);
  44593. }
  44594. return EXPECT_RESULT();
  44595. }
  44596. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  44597. {
  44598. EXPECT_DECLS;
  44599. byte key[AES_256_KEY_SIZE] = {0};
  44600. byte iv[AES_BLOCK_SIZE] = {0};
  44601. int i;
  44602. int nids[] = {
  44603. #ifdef HAVE_AES_CBC
  44604. NID_aes_128_cbc,
  44605. NID_aes_256_cbc,
  44606. #endif
  44607. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44608. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44609. #ifdef HAVE_AESGCM
  44610. NID_aes_128_gcm,
  44611. NID_aes_256_gcm,
  44612. #endif
  44613. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44614. #ifdef WOLFSSL_AES_COUNTER
  44615. NID_aes_128_ctr,
  44616. NID_aes_256_ctr,
  44617. #endif
  44618. #ifndef NO_DES3
  44619. NID_des_cbc,
  44620. NID_des_ede3_cbc,
  44621. #endif
  44622. };
  44623. int key_lengths[] = {
  44624. #ifdef HAVE_AES_CBC
  44625. AES_128_KEY_SIZE,
  44626. AES_256_KEY_SIZE,
  44627. #endif
  44628. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44629. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44630. #ifdef HAVE_AESGCM
  44631. AES_128_KEY_SIZE,
  44632. AES_256_KEY_SIZE,
  44633. #endif
  44634. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44635. #ifdef WOLFSSL_AES_COUNTER
  44636. AES_128_KEY_SIZE,
  44637. AES_256_KEY_SIZE,
  44638. #endif
  44639. #ifndef NO_DES3
  44640. DES_KEY_SIZE,
  44641. DES3_KEY_SIZE,
  44642. #endif
  44643. };
  44644. int nidsLen = (sizeof(nids)/sizeof(int));
  44645. for (i = 0; i < nidsLen; i++) {
  44646. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  44647. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  44648. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44649. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44650. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  44651. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  44652. WOLFSSL_SUCCESS);
  44653. EVP_CIPHER_CTX_free(ctx);
  44654. }
  44655. return EXPECT_RESULT();
  44656. }
  44657. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  44658. {
  44659. EXPECT_DECLS;
  44660. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  44661. int ivLen, keyLen;
  44662. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  44663. #ifdef HAVE_AESGCM
  44664. byte key[AES_128_KEY_SIZE] = {0};
  44665. byte iv[AES_BLOCK_SIZE] = {0};
  44666. const EVP_CIPHER *init = EVP_aes_128_gcm();
  44667. #else
  44668. byte key[DES3_KEY_SIZE] = {0};
  44669. byte iv[DES_BLOCK_SIZE] = {0};
  44670. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  44671. #endif
  44672. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44673. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44674. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  44675. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  44676. /* Bad cases */
  44677. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  44678. WOLFSSL_FAILURE);
  44679. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  44680. WOLFSSL_FAILURE);
  44681. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  44682. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  44683. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  44684. WOLFSSL_FAILURE);
  44685. /* Good case */
  44686. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  44687. EVP_CIPHER_CTX_free(ctx);
  44688. #endif
  44689. return EXPECT_RESULT();
  44690. }
  44691. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  44692. {
  44693. EXPECT_DECLS;
  44694. WOLFSSL_ENGINE* e = NULL;
  44695. int id = 0;
  44696. EVP_PKEY_CTX *ctx = NULL;
  44697. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  44698. EVP_PKEY_CTX_free(ctx);
  44699. return EXPECT_RESULT();
  44700. }
  44701. static int test_wolfSSL_EVP_rc4(void)
  44702. {
  44703. EXPECT_DECLS;
  44704. #if !defined(NO_RC4)
  44705. ExpectNotNull(wolfSSL_EVP_rc4());
  44706. #endif
  44707. return EXPECT_RESULT();
  44708. }
  44709. static int test_wolfSSL_EVP_enc_null(void)
  44710. {
  44711. EXPECT_DECLS;
  44712. ExpectNotNull(wolfSSL_EVP_enc_null());
  44713. return EXPECT_RESULT();
  44714. }
  44715. static int test_wolfSSL_EVP_rc2_cbc(void)
  44716. {
  44717. EXPECT_DECLS;
  44718. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  44719. ExpectNull(wolfSSL_EVP_rc2_cbc());
  44720. #endif
  44721. return EXPECT_RESULT();
  44722. }
  44723. static int test_wolfSSL_EVP_mdc2(void)
  44724. {
  44725. EXPECT_DECLS;
  44726. #if !defined(NO_WOLFSSL_STUB)
  44727. ExpectNull(wolfSSL_EVP_mdc2());
  44728. #endif
  44729. return EXPECT_RESULT();
  44730. }
  44731. static int test_wolfSSL_EVP_md4(void)
  44732. {
  44733. EXPECT_DECLS;
  44734. #if !defined(NO_MD4)
  44735. ExpectNotNull(wolfSSL_EVP_md4());
  44736. #endif
  44737. return EXPECT_RESULT();
  44738. }
  44739. static int test_wolfSSL_EVP_aes_256_gcm(void)
  44740. {
  44741. EXPECT_DECLS;
  44742. #ifdef HAVE_AESGCM
  44743. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  44744. #endif
  44745. return EXPECT_RESULT();
  44746. }
  44747. static int test_wolfSSL_EVP_aes_192_gcm(void)
  44748. {
  44749. EXPECT_DECLS;
  44750. #ifdef HAVE_AESGCM
  44751. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  44752. #endif
  44753. return EXPECT_RESULT();
  44754. }
  44755. static int test_wolfSSL_EVP_aes_256_ccm(void)
  44756. {
  44757. EXPECT_DECLS;
  44758. #ifdef HAVE_AESCCM
  44759. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  44760. #endif
  44761. return EXPECT_RESULT();
  44762. }
  44763. static int test_wolfSSL_EVP_aes_192_ccm(void)
  44764. {
  44765. EXPECT_DECLS;
  44766. #ifdef HAVE_AESCCM
  44767. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  44768. #endif
  44769. return EXPECT_RESULT();
  44770. }
  44771. static int test_wolfSSL_EVP_aes_128_ccm(void)
  44772. {
  44773. EXPECT_DECLS;
  44774. #ifdef HAVE_AESCCM
  44775. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  44776. #endif
  44777. return EXPECT_RESULT();
  44778. }
  44779. static int test_wolfSSL_EVP_ripemd160(void)
  44780. {
  44781. EXPECT_DECLS;
  44782. #if !defined(NO_WOLFSSL_STUB)
  44783. ExpectNull(wolfSSL_EVP_ripemd160());
  44784. #endif
  44785. return EXPECT_RESULT();
  44786. }
  44787. static int test_wolfSSL_EVP_get_digestbynid(void)
  44788. {
  44789. EXPECT_DECLS;
  44790. #ifndef NO_MD5
  44791. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  44792. #endif
  44793. #ifndef NO_SHA
  44794. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  44795. #endif
  44796. #ifndef NO_SHA256
  44797. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  44798. #endif
  44799. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  44800. return EXPECT_RESULT();
  44801. }
  44802. static int test_wolfSSL_EVP_MD_nid(void)
  44803. {
  44804. EXPECT_DECLS;
  44805. #ifndef NO_MD5
  44806. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  44807. #endif
  44808. #ifndef NO_SHA
  44809. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  44810. #endif
  44811. #ifndef NO_SHA256
  44812. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  44813. #endif
  44814. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  44815. return EXPECT_RESULT();
  44816. }
  44817. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  44818. {
  44819. EXPECT_DECLS;
  44820. #if defined(HAVE_ECC)
  44821. WOLFSSL_EVP_PKEY* pkey = NULL;
  44822. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  44823. ExpectNotNull(pkey = EVP_PKEY_new());
  44824. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  44825. EVP_PKEY_free(pkey);
  44826. #endif
  44827. return EXPECT_RESULT();
  44828. }
  44829. static int test_wolfSSL_EVP_X_STATE(void)
  44830. {
  44831. EXPECT_DECLS;
  44832. #if !defined(NO_DES3) && !defined(NO_RC4)
  44833. byte key[DES3_KEY_SIZE] = {0};
  44834. byte iv[DES_IV_SIZE] = {0};
  44835. EVP_CIPHER_CTX *ctx = NULL;
  44836. const EVP_CIPHER *init = NULL;
  44837. /* Bad test cases */
  44838. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44839. ExpectNotNull(init = EVP_des_ede3_cbc());
  44840. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44841. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44842. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  44843. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  44844. EVP_CIPHER_CTX_free(ctx);
  44845. ctx = NULL;
  44846. /* Good test case */
  44847. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44848. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44849. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44850. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44851. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  44852. EVP_CIPHER_CTX_free(ctx);
  44853. #endif
  44854. return EXPECT_RESULT();
  44855. }
  44856. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  44857. {
  44858. EXPECT_DECLS;
  44859. #if !defined(NO_DES3) && !defined(NO_RC4)
  44860. byte key[DES3_KEY_SIZE] = {0};
  44861. byte iv[DES_IV_SIZE] = {0};
  44862. EVP_CIPHER_CTX *ctx = NULL;
  44863. const EVP_CIPHER *init = NULL;
  44864. /* Bad test cases */
  44865. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44866. ExpectNotNull(init = EVP_des_ede3_cbc());
  44867. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44868. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44869. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  44870. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  44871. EVP_CIPHER_CTX_free(ctx);
  44872. ctx = NULL;
  44873. /* Good test case */
  44874. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44875. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44876. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44877. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44878. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  44879. EVP_CIPHER_CTX_free(ctx);
  44880. #endif
  44881. return EXPECT_RESULT();
  44882. }
  44883. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  44884. {
  44885. EXPECT_DECLS;
  44886. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  44887. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  44888. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  44889. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  44890. #ifdef HAVE_AES_CBC
  44891. #ifdef WOLFSSL_AES_128
  44892. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  44893. #endif
  44894. #ifdef WOLFSSL_AES_192
  44895. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  44896. #endif
  44897. #ifdef WOLFSSL_AES_256
  44898. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  44899. #endif
  44900. #endif
  44901. #ifdef HAVE_AESGCM
  44902. #ifdef WOLFSSL_AES_128
  44903. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  44904. #endif
  44905. #ifdef WOLFSSL_AES_192
  44906. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  44907. #endif
  44908. #ifdef WOLFSSL_AES_256
  44909. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  44910. #endif
  44911. #endif
  44912. #ifdef HAVE_AESCCM
  44913. #ifdef WOLFSSL_AES_128
  44914. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  44915. #endif
  44916. #ifdef WOLFSSL_AES_192
  44917. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  44918. #endif
  44919. #ifdef WOLFSSL_AES_256
  44920. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  44921. #endif
  44922. #endif
  44923. #ifdef WOLFSSL_AES_COUNTER
  44924. #ifdef WOLFSSL_AES_128
  44925. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  44926. #endif
  44927. #ifdef WOLFSSL_AES_192
  44928. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  44929. #endif
  44930. #ifdef WOLFSSL_AES_256
  44931. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  44932. #endif
  44933. #endif
  44934. #ifdef HAVE_AES_ECB
  44935. #ifdef WOLFSSL_AES_128
  44936. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  44937. #endif
  44938. #ifdef WOLFSSL_AES_192
  44939. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  44940. #endif
  44941. #ifdef WOLFSSL_AES_256
  44942. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  44943. #endif
  44944. #endif
  44945. #ifdef WOLFSSL_AES_OFB
  44946. #ifdef WOLFSSL_AES_128
  44947. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  44948. #endif
  44949. #ifdef WOLFSSL_AES_192
  44950. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  44951. #endif
  44952. #ifdef WOLFSSL_AES_256
  44953. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  44954. #endif
  44955. #endif
  44956. #ifndef NO_RC4
  44957. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  44958. #endif
  44959. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44960. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  44961. #endif
  44962. #endif
  44963. #ifdef WOLFSSL_SM4_ECB
  44964. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  44965. #endif
  44966. #ifdef WOLFSSL_SM4_CBC
  44967. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  44968. #endif
  44969. #ifdef WOLFSSL_SM4_CTR
  44970. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  44971. #endif
  44972. #ifdef WOLFSSL_SM4_GCM
  44973. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  44974. #endif
  44975. #ifdef WOLFSSL_SM4_CCM
  44976. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  44977. #endif
  44978. return EXPECT_RESULT();
  44979. }
  44980. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  44981. {
  44982. EXPECT_DECLS;
  44983. int nids[] = {
  44984. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44985. #ifdef WOLFSSL_AES_128
  44986. NID_aes_128_cbc,
  44987. #endif
  44988. #ifdef WOLFSSL_AES_192
  44989. NID_aes_192_cbc,
  44990. #endif
  44991. #ifdef WOLFSSL_AES_256
  44992. NID_aes_256_cbc,
  44993. #endif
  44994. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44995. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44996. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44997. #ifdef HAVE_AESGCM
  44998. #ifdef WOLFSSL_AES_128
  44999. NID_aes_128_gcm,
  45000. #endif
  45001. #ifdef WOLFSSL_AES_192
  45002. NID_aes_192_gcm,
  45003. #endif
  45004. #ifdef WOLFSSL_AES_256
  45005. NID_aes_256_gcm,
  45006. #endif
  45007. #endif /* HAVE_AESGCM */
  45008. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  45009. #ifdef WOLFSSL_AES_COUNTER
  45010. #ifdef WOLFSSL_AES_128
  45011. NID_aes_128_ctr,
  45012. #endif
  45013. #ifdef WOLFSSL_AES_192
  45014. NID_aes_192_ctr,
  45015. #endif
  45016. #ifdef WOLFSSL_AES_256
  45017. NID_aes_256_ctr,
  45018. #endif
  45019. #endif
  45020. #ifndef NO_DES3
  45021. NID_des_cbc,
  45022. NID_des_ede3_cbc,
  45023. #endif
  45024. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45025. NID_chacha20_poly1305,
  45026. #endif
  45027. };
  45028. int iv_lengths[] = {
  45029. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  45030. #ifdef WOLFSSL_AES_128
  45031. AES_BLOCK_SIZE,
  45032. #endif
  45033. #ifdef WOLFSSL_AES_192
  45034. AES_BLOCK_SIZE,
  45035. #endif
  45036. #ifdef WOLFSSL_AES_256
  45037. AES_BLOCK_SIZE,
  45038. #endif
  45039. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  45040. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  45041. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  45042. #ifdef HAVE_AESGCM
  45043. #ifdef WOLFSSL_AES_128
  45044. GCM_NONCE_MID_SZ,
  45045. #endif
  45046. #ifdef WOLFSSL_AES_192
  45047. GCM_NONCE_MID_SZ,
  45048. #endif
  45049. #ifdef WOLFSSL_AES_256
  45050. GCM_NONCE_MID_SZ,
  45051. #endif
  45052. #endif /* HAVE_AESGCM */
  45053. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  45054. #ifdef WOLFSSL_AES_COUNTER
  45055. #ifdef WOLFSSL_AES_128
  45056. AES_BLOCK_SIZE,
  45057. #endif
  45058. #ifdef WOLFSSL_AES_192
  45059. AES_BLOCK_SIZE,
  45060. #endif
  45061. #ifdef WOLFSSL_AES_256
  45062. AES_BLOCK_SIZE,
  45063. #endif
  45064. #endif
  45065. #ifndef NO_DES3
  45066. DES_BLOCK_SIZE,
  45067. DES_BLOCK_SIZE,
  45068. #endif
  45069. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45070. CHACHA20_POLY1305_AEAD_IV_SIZE,
  45071. #endif
  45072. };
  45073. int i;
  45074. int nidsLen = (sizeof(nids)/sizeof(int));
  45075. for (i = 0; i < nidsLen; i++) {
  45076. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  45077. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  45078. }
  45079. return EXPECT_RESULT();
  45080. }
  45081. static int test_wolfSSL_EVP_SignInit_ex(void)
  45082. {
  45083. EXPECT_DECLS;
  45084. WOLFSSL_EVP_MD_CTX mdCtx;
  45085. WOLFSSL_ENGINE* e = 0;
  45086. const EVP_MD* md = EVP_sha256();
  45087. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  45088. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  45089. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  45090. return EXPECT_RESULT();
  45091. }
  45092. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  45093. {
  45094. EXPECT_DECLS;
  45095. #if !defined(NO_SHA256)
  45096. WOLFSSL_EVP_MD_CTX mdCtx;
  45097. unsigned int s = 0;
  45098. unsigned char md[WC_SHA256_DIGEST_SIZE];
  45099. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  45100. /* Bad Case */
  45101. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  45102. (HAVE_FIPS_VERSION > 2))
  45103. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  45104. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  45105. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  45106. #else
  45107. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  45108. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  45109. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  45110. #endif
  45111. /* Good Case */
  45112. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  45113. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  45114. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  45115. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  45116. #endif
  45117. return EXPECT_RESULT();
  45118. }
  45119. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  45120. {
  45121. EXPECT_DECLS;
  45122. #if defined(OPENSSL_EXTRA)
  45123. EVP_PKEY* pkey = NULL;
  45124. EVP_PKEY_CTX* ctx = NULL;
  45125. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45126. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45127. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  45128. /* void */
  45129. EVP_PKEY_CTX_free(ctx);
  45130. #else
  45131. /* int */
  45132. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  45133. #endif
  45134. EVP_PKEY_free(pkey);
  45135. #endif
  45136. return EXPECT_RESULT();
  45137. }
  45138. static int test_wolfSSL_EVP_PKEY_param_check(void)
  45139. {
  45140. EXPECT_DECLS;
  45141. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  45142. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  45143. DH *dh = NULL;
  45144. DH *setDh = NULL;
  45145. EVP_PKEY *pkey = NULL;
  45146. EVP_PKEY_CTX* ctx = NULL;
  45147. FILE* f = NULL;
  45148. unsigned char buf[512];
  45149. const unsigned char* pt = buf;
  45150. const char* dh2048 = "./certs/dh2048.der";
  45151. long len = 0;
  45152. int code = -1;
  45153. XMEMSET(buf, 0, sizeof(buf));
  45154. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  45155. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  45156. if (f != XBADFILE)
  45157. XFCLOSE(f);
  45158. /* Load dh2048.der into DH with internal format */
  45159. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  45160. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  45161. ExpectIntEQ(code, 0);
  45162. code = -1;
  45163. pkey = wolfSSL_EVP_PKEY_new();
  45164. /* Set DH into PKEY */
  45165. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  45166. /* create ctx from pkey */
  45167. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45168. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  45169. /* TODO: more invalid cases */
  45170. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  45171. EVP_PKEY_CTX_free(ctx);
  45172. EVP_PKEY_free(pkey);
  45173. DH_free(setDh);
  45174. setDh = NULL;
  45175. DH_free(dh);
  45176. dh = NULL;
  45177. #endif
  45178. #endif
  45179. return EXPECT_RESULT();
  45180. }
  45181. static int test_wolfSSL_EVP_BytesToKey(void)
  45182. {
  45183. EXPECT_DECLS;
  45184. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  45185. byte key[AES_BLOCK_SIZE] = {0};
  45186. byte iv[AES_BLOCK_SIZE] = {0};
  45187. int count = 0;
  45188. const EVP_MD* md = EVP_sha256();
  45189. const EVP_CIPHER *type;
  45190. const unsigned char *salt = (unsigned char *)"salt1234";
  45191. int sz = 5;
  45192. const byte data[] = {
  45193. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  45194. 0x72,0x6c,0x64
  45195. };
  45196. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  45197. /* Bad cases */
  45198. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  45199. 0);
  45200. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  45201. 16);
  45202. md = "2";
  45203. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  45204. WOLFSSL_FAILURE);
  45205. /* Good case */
  45206. md = EVP_sha256();
  45207. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  45208. 16);
  45209. #endif
  45210. return EXPECT_RESULT();
  45211. }
  45212. static int test_evp_cipher_aes_gcm(void)
  45213. {
  45214. EXPECT_DECLS;
  45215. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  45216. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  45217. (HAVE_FIPS_VERSION >= 2)))
  45218. /*
  45219. * This test checks data at various points in the encrypt/decrypt process
  45220. * against known values produced using the same test with OpenSSL. This
  45221. * interop testing is critical for verifying the correctness of our
  45222. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  45223. * a flow supported by OpenSSL that uses the control command
  45224. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  45225. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  45226. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  45227. * wolfSSL OpenSSH clients because there was a bug in this flow that
  45228. * happened to "cancel out" if both sides of the connection had the bug.
  45229. */
  45230. enum {
  45231. NUM_ENCRYPTIONS = 3,
  45232. AAD_SIZE = 4
  45233. };
  45234. byte plainText1[] = {
  45235. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  45236. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  45237. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  45238. };
  45239. byte plainText2[] = {
  45240. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  45241. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  45242. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  45243. };
  45244. byte plainText3[] = {
  45245. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  45246. 0x5b, 0x57, 0x10
  45247. };
  45248. byte* plainTexts[NUM_ENCRYPTIONS] = {
  45249. plainText1,
  45250. plainText2,
  45251. plainText3
  45252. };
  45253. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  45254. sizeof(plainText1),
  45255. sizeof(plainText2),
  45256. sizeof(plainText3)
  45257. };
  45258. byte aad1[AAD_SIZE] = {
  45259. 0x00, 0x00, 0x00, 0x01
  45260. };
  45261. byte aad2[AAD_SIZE] = {
  45262. 0x00, 0x00, 0x00, 0x10
  45263. };
  45264. byte aad3[AAD_SIZE] = {
  45265. 0x00, 0x00, 0x01, 0x00
  45266. };
  45267. byte* aads[NUM_ENCRYPTIONS] = {
  45268. aad1,
  45269. aad2,
  45270. aad3
  45271. };
  45272. const byte iv[GCM_NONCE_MID_SZ] = {
  45273. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  45274. };
  45275. byte currentIv[GCM_NONCE_MID_SZ];
  45276. const byte key[] = {
  45277. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  45278. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  45279. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  45280. };
  45281. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  45282. {
  45283. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  45284. 0xEF
  45285. },
  45286. {
  45287. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  45288. 0xF0
  45289. },
  45290. {
  45291. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  45292. 0xF1
  45293. }
  45294. };
  45295. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  45296. {
  45297. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  45298. 0x3D, 0x32, 0x18, 0x34, 0xA9
  45299. },
  45300. {
  45301. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  45302. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  45303. },
  45304. {
  45305. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  45306. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  45307. }
  45308. };
  45309. const byte expCipherText1[] = {
  45310. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  45311. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  45312. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  45313. };
  45314. const byte expCipherText2[] = {
  45315. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  45316. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  45317. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  45318. };
  45319. const byte expCipherText3[] = {
  45320. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  45321. 0x80, 0x5E, 0x6B,
  45322. };
  45323. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  45324. expCipherText1,
  45325. expCipherText2,
  45326. expCipherText3
  45327. };
  45328. byte* cipherText = NULL;
  45329. byte* calcPlainText = NULL;
  45330. byte tag[AES_BLOCK_SIZE];
  45331. EVP_CIPHER_CTX* encCtx = NULL;
  45332. EVP_CIPHER_CTX* decCtx = NULL;
  45333. int i, j, outl;
  45334. /****************************************************/
  45335. for (i = 0; i < 3; ++i) {
  45336. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  45337. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  45338. /* First iteration, set key before IV. */
  45339. if (i == 0) {
  45340. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  45341. SSL_SUCCESS);
  45342. /*
  45343. * The call to EVP_CipherInit below (with NULL key) should clear the
  45344. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  45345. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  45346. * behavior.
  45347. */
  45348. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  45349. (void*)iv), SSL_SUCCESS);
  45350. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  45351. SSL_SUCCESS);
  45352. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  45353. currentIv), SSL_FAILURE);
  45354. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  45355. SSL_SUCCESS);
  45356. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  45357. SSL_SUCCESS);
  45358. }
  45359. /* Second iteration, IV before key. */
  45360. else {
  45361. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  45362. SSL_SUCCESS);
  45363. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  45364. SSL_SUCCESS);
  45365. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  45366. SSL_SUCCESS);
  45367. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  45368. SSL_SUCCESS);
  45369. }
  45370. /*
  45371. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  45372. * been issued first.
  45373. */
  45374. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  45375. currentIv), SSL_FAILURE);
  45376. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  45377. (void*)iv), SSL_SUCCESS);
  45378. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  45379. (void*)iv), SSL_SUCCESS);
  45380. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  45381. /*************** Encrypt ***************/
  45382. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  45383. currentIv), SSL_SUCCESS);
  45384. /* Check current IV against expected. */
  45385. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  45386. /* Add AAD. */
  45387. if (i == 2) {
  45388. /* Test streaming API. */
  45389. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  45390. AAD_SIZE), SSL_SUCCESS);
  45391. }
  45392. else {
  45393. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  45394. AAD_SIZE);
  45395. }
  45396. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  45397. DYNAMIC_TYPE_TMP_BUFFER));
  45398. /* Encrypt plaintext. */
  45399. if (i == 2) {
  45400. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  45401. plainTexts[j], plainTextSzs[j]),
  45402. SSL_SUCCESS);
  45403. }
  45404. else {
  45405. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  45406. plainTextSzs[j]), plainTextSzs[j]);
  45407. }
  45408. if (i == 2) {
  45409. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  45410. SSL_SUCCESS);
  45411. }
  45412. else {
  45413. /*
  45414. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  45415. * akin to calling EVP_CipherFinal.
  45416. */
  45417. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  45418. }
  45419. /* Check ciphertext against expected. */
  45420. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  45421. 0);
  45422. /* Get and check tag against expected. */
  45423. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  45424. sizeof(tag), tag), SSL_SUCCESS);
  45425. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  45426. /*************** Decrypt ***************/
  45427. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  45428. currentIv), SSL_SUCCESS);
  45429. /* Check current IV against expected. */
  45430. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  45431. /* Add AAD. */
  45432. if (i == 2) {
  45433. /* Test streaming API. */
  45434. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  45435. AAD_SIZE), SSL_SUCCESS);
  45436. }
  45437. else {
  45438. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  45439. AAD_SIZE);
  45440. }
  45441. /* Set expected tag. */
  45442. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  45443. sizeof(tag), tag), SSL_SUCCESS);
  45444. /* Decrypt ciphertext. */
  45445. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  45446. DYNAMIC_TYPE_TMP_BUFFER));
  45447. if (i == 2) {
  45448. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  45449. cipherText, plainTextSzs[j]),
  45450. SSL_SUCCESS);
  45451. }
  45452. else {
  45453. /* This first EVP_Cipher call will check the tag, too. */
  45454. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  45455. plainTextSzs[j]), plainTextSzs[j]);
  45456. }
  45457. if (i == 2) {
  45458. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  45459. SSL_SUCCESS);
  45460. }
  45461. else {
  45462. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  45463. }
  45464. /* Check plaintext against expected. */
  45465. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  45466. 0);
  45467. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45468. cipherText = NULL;
  45469. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45470. calcPlainText = NULL;
  45471. }
  45472. EVP_CIPHER_CTX_free(encCtx);
  45473. encCtx = NULL;
  45474. EVP_CIPHER_CTX_free(decCtx);
  45475. decCtx = NULL;
  45476. }
  45477. #endif
  45478. return EXPECT_RESULT();
  45479. }
  45480. static int test_wolfSSL_OBJ_ln(void)
  45481. {
  45482. EXPECT_DECLS;
  45483. const int nid_set[] = {
  45484. NID_commonName,
  45485. NID_serialNumber,
  45486. NID_countryName,
  45487. NID_localityName,
  45488. NID_stateOrProvinceName,
  45489. NID_organizationName,
  45490. NID_organizationalUnitName,
  45491. NID_domainComponent,
  45492. NID_businessCategory,
  45493. NID_jurisdictionCountryName,
  45494. NID_jurisdictionStateOrProvinceName,
  45495. NID_emailAddress
  45496. };
  45497. const char* ln_set[] = {
  45498. "commonName",
  45499. "serialNumber",
  45500. "countryName",
  45501. "localityName",
  45502. "stateOrProvinceName",
  45503. "organizationName",
  45504. "organizationalUnitName",
  45505. "domainComponent",
  45506. "businessCategory",
  45507. "jurisdictionCountryName",
  45508. "jurisdictionStateOrProvinceName",
  45509. "emailAddress",
  45510. };
  45511. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  45512. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  45513. #ifdef HAVE_ECC
  45514. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45515. {
  45516. EC_builtin_curve r[27];
  45517. size_t nCurves = sizeof(r) / sizeof(r[0]);
  45518. nCurves = EC_get_builtin_curves(r, nCurves);
  45519. for (i = 0; i < nCurves; i++) {
  45520. /* skip ECC_CURVE_INVALID */
  45521. if (r[i].nid != ECC_CURVE_INVALID) {
  45522. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  45523. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  45524. }
  45525. }
  45526. }
  45527. #endif
  45528. #endif
  45529. for (i = 0; i < maxIdx; i++) {
  45530. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  45531. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  45532. }
  45533. return EXPECT_RESULT();
  45534. }
  45535. static int test_wolfSSL_OBJ_sn(void)
  45536. {
  45537. EXPECT_DECLS;
  45538. int i = 0, maxIdx = 7;
  45539. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  45540. NID_stateOrProvinceName,NID_organizationName,
  45541. NID_organizationalUnitName,NID_emailAddress};
  45542. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  45543. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  45544. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  45545. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  45546. WOLFSSL_EMAIL_ADDR};
  45547. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  45548. for (i = 0; i < maxIdx; i++) {
  45549. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  45550. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  45551. }
  45552. return EXPECT_RESULT();
  45553. }
  45554. #if !defined(NO_BIO)
  45555. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  45556. {
  45557. return lh_strhash(s[3]);
  45558. }
  45559. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  45560. {
  45561. return XSTRCMP(a[3], b[3]);
  45562. }
  45563. #endif
  45564. static int test_wolfSSL_TXT_DB(void)
  45565. {
  45566. EXPECT_DECLS;
  45567. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  45568. BIO *bio = NULL;
  45569. TXT_DB *db = NULL;
  45570. const int columns = 6;
  45571. const char *fields[6] = {
  45572. "V",
  45573. "320926161116Z",
  45574. "",
  45575. "12BD",
  45576. "unknown",
  45577. "/CN=rsa doe",
  45578. };
  45579. char** fields_copy = NULL;
  45580. /* Test read */
  45581. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  45582. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  45583. ExpectNotNull(db = TXT_DB_read(bio, columns));
  45584. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  45585. DYNAMIC_TYPE_OPENSSL));
  45586. if (fields_copy != NULL) {
  45587. XMEMCPY(fields_copy, fields, sizeof(fields));
  45588. }
  45589. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  45590. if (EXPECT_FAIL()) {
  45591. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  45592. }
  45593. BIO_free(bio);
  45594. bio = NULL;
  45595. /* Test write */
  45596. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  45597. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  45598. BIO_free(bio);
  45599. /* Test index */
  45600. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  45601. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  45602. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45603. fields[3] = "12DA";
  45604. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45605. fields[3] = "FFFF";
  45606. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45607. fields[3] = "";
  45608. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45609. TXT_DB_free(db);
  45610. #endif
  45611. return EXPECT_RESULT();
  45612. }
  45613. static int test_wolfSSL_NCONF(void)
  45614. {
  45615. EXPECT_DECLS;
  45616. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  45617. const char* confFile = "./tests/NCONF_test.cnf";
  45618. CONF* conf = NULL;
  45619. long eline = 0;
  45620. long num = 0;
  45621. ExpectNotNull(conf = NCONF_new(NULL));
  45622. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  45623. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  45624. ExpectIntEQ(num, 1234);
  45625. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  45626. ExpectIntEQ(num, 4321);
  45627. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  45628. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  45629. "./test-dir/file1");
  45630. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  45631. "./test-dir/file1:./test-dir/file2:./section1:file2");
  45632. NCONF_free(conf);
  45633. #endif
  45634. return EXPECT_RESULT();
  45635. }
  45636. #endif /* OPENSSL_ALL */
  45637. static int test_wolfSSL_X509V3_EXT_get(void) {
  45638. EXPECT_DECLS;
  45639. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45640. XFILE f = XBADFILE;
  45641. int numOfExt =0;
  45642. int extNid = 0;
  45643. int i = 0;
  45644. WOLFSSL_X509* x509 = NULL;
  45645. WOLFSSL_X509_EXTENSION* ext = NULL;
  45646. const WOLFSSL_v3_ext_method* method = NULL;
  45647. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45648. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45649. if (f != XBADFILE)
  45650. XFCLOSE(f);
  45651. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  45652. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  45653. for (i = 0; i < numOfExt; i++) {
  45654. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45655. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  45656. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45657. ExpectIntEQ(method->ext_nid, extNid);
  45658. }
  45659. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  45660. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  45661. wolfSSL_X509_free(x509);
  45662. #endif
  45663. return EXPECT_RESULT();
  45664. }
  45665. static int test_wolfSSL_X509V3_EXT_nconf(void)
  45666. {
  45667. EXPECT_DECLS;
  45668. #ifdef OPENSSL_ALL
  45669. const char *ext_names[] = {
  45670. "subjectKeyIdentifier",
  45671. "authorityKeyIdentifier",
  45672. "subjectAltName",
  45673. "keyUsage",
  45674. "extendedKeyUsage",
  45675. };
  45676. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  45677. int ext_nids[] = {
  45678. NID_subject_key_identifier,
  45679. NID_authority_key_identifier,
  45680. NID_subject_alt_name,
  45681. NID_key_usage,
  45682. NID_ext_key_usage,
  45683. };
  45684. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  45685. const char *ext_values[] = {
  45686. "hash",
  45687. "hash",
  45688. "DNS:example.com, IP:127.0.0.1",
  45689. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  45690. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  45691. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  45692. "OCSPSigning",
  45693. };
  45694. size_t i;
  45695. X509_EXTENSION* ext = NULL;
  45696. X509* x509 = NULL;
  45697. unsigned int keyUsageFlags;
  45698. unsigned int extKeyUsageFlags;
  45699. ExpectNotNull(x509 = X509_new());
  45700. /* keyUsage / extKeyUsage should match string above */
  45701. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45702. | KU_NON_REPUDIATION
  45703. | KU_KEY_ENCIPHERMENT
  45704. | KU_DATA_ENCIPHERMENT
  45705. | KU_KEY_AGREEMENT
  45706. | KU_KEY_CERT_SIGN
  45707. | KU_CRL_SIGN
  45708. | KU_ENCIPHER_ONLY
  45709. | KU_DECIPHER_ONLY;
  45710. extKeyUsageFlags = XKU_SSL_CLIENT
  45711. | XKU_SSL_SERVER
  45712. | XKU_CODE_SIGN
  45713. | XKU_SMIME
  45714. | XKU_TIMESTAMP
  45715. | XKU_OCSP_SIGN;
  45716. for (i = 0; i < ext_names_count; i++) {
  45717. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45718. ext_values[i]));
  45719. X509_EXTENSION_free(ext);
  45720. ext = NULL;
  45721. }
  45722. for (i = 0; i < ext_nids_count; i++) {
  45723. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  45724. ext_values[i]));
  45725. X509_EXTENSION_free(ext);
  45726. ext = NULL;
  45727. }
  45728. /* Test adding extension to X509 */
  45729. for (i = 0; i < ext_nids_count; i++) {
  45730. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45731. ext_values[i]));
  45732. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  45733. if (ext_nids[i] == NID_key_usage) {
  45734. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45735. }
  45736. else if (ext_nids[i] == NID_ext_key_usage) {
  45737. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45738. }
  45739. X509_EXTENSION_free(ext);
  45740. ext = NULL;
  45741. }
  45742. X509_free(x509);
  45743. #endif
  45744. return EXPECT_RESULT();
  45745. }
  45746. static int test_wolfSSL_X509V3_EXT(void) {
  45747. EXPECT_DECLS;
  45748. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45749. XFILE f = XBADFILE;
  45750. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  45751. char* str = NULL;
  45752. unsigned char* data = NULL;
  45753. const WOLFSSL_v3_ext_method* method = NULL;
  45754. WOLFSSL_X509* x509 = NULL;
  45755. WOLFSSL_X509_EXTENSION* ext = NULL;
  45756. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  45757. WOLFSSL_ASN1_OBJECT *obj = NULL;
  45758. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  45759. WOLFSSL_ASN1_STRING* asn1str = NULL;
  45760. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  45761. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  45762. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  45763. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  45764. WOLFSSL_GENERAL_NAME* gn = NULL;
  45765. /* Check NULL argument */
  45766. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  45767. /* Using OCSP cert with X509V3 extensions */
  45768. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  45769. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45770. if (f != XBADFILE)
  45771. XFCLOSE(f);
  45772. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  45773. /* Basic Constraints */
  45774. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45775. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45776. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  45777. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  45778. ExpectIntEQ(bc->ca, 1);
  45779. ExpectNull(bc->pathlen);
  45780. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  45781. bc = NULL;
  45782. i++;
  45783. /* Subject Key Identifier */
  45784. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45785. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45786. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  45787. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45788. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  45789. asn1str));
  45790. X509_EXTENSION_free(ext2);
  45791. ext2 = NULL;
  45792. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45793. ExpectNotNull(method->i2s);
  45794. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  45795. wolfSSL_ASN1_STRING_free(asn1str);
  45796. asn1str = NULL;
  45797. if (str != NULL) {
  45798. actual = strcmp(str,
  45799. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45800. }
  45801. ExpectIntEQ(actual, 0);
  45802. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45803. str = NULL;
  45804. i++;
  45805. /* Authority Key Identifier */
  45806. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45807. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45808. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  45809. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  45810. ext));
  45811. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45812. ExpectNotNull(asn1str = aKeyId->keyid);
  45813. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  45814. asn1str));
  45815. asn1str = NULL;
  45816. if (str != NULL) {
  45817. actual = strcmp(str,
  45818. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45819. }
  45820. ExpectIntEQ(actual, 0);
  45821. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45822. str = NULL;
  45823. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  45824. aKeyId = NULL;
  45825. i++;
  45826. /* Key Usage */
  45827. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45828. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45829. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  45830. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45831. #if defined(WOLFSSL_QT)
  45832. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  45833. #else
  45834. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  45835. #endif
  45836. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  45837. if (data != NULL) {
  45838. #ifdef BIG_ENDIAN_ORDER
  45839. actual = data[1];
  45840. #else
  45841. actual = data[0];
  45842. #endif
  45843. }
  45844. ExpectIntEQ(actual, expected);
  45845. wolfSSL_ASN1_STRING_free(asn1str);
  45846. asn1str = NULL;
  45847. #if 1
  45848. i++;
  45849. /* Authority Info Access */
  45850. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45851. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45852. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  45853. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  45854. ext));
  45855. #if defined(WOLFSSL_QT)
  45856. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45857. #else
  45858. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45859. #endif
  45860. /* URI entry is an ACCESS_DESCRIPTION type */
  45861. #if defined(WOLFSSL_QT)
  45862. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  45863. #else
  45864. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  45865. #endif
  45866. ExpectNotNull(adObj = ad->method);
  45867. /* Make sure nid is OCSP */
  45868. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  45869. /* GENERAL_NAME stores URI as an ASN1_STRING */
  45870. ExpectNotNull(gn = ad->location);
  45871. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  45872. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  45873. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  45874. #if defined(WOLFSSL_QT)
  45875. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  45876. #else
  45877. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  45878. #endif
  45879. if (str != NULL) {
  45880. actual = strcmp(str, "http://127.0.0.1:22220");
  45881. }
  45882. ExpectIntEQ(actual, 0);
  45883. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  45884. aia = NULL;
  45885. #else
  45886. (void) aia; (void) ad; (void) adObj; (void) gn;
  45887. #endif
  45888. wolfSSL_X509_free(x509);
  45889. #endif
  45890. return EXPECT_RESULT();
  45891. }
  45892. static int test_wolfSSL_X509_get_extension_flags(void)
  45893. {
  45894. EXPECT_DECLS;
  45895. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45896. XFILE f = XBADFILE;
  45897. X509* x509 = NULL;
  45898. unsigned int extFlags;
  45899. unsigned int keyUsageFlags;
  45900. unsigned int extKeyUsageFlags;
  45901. /* client-int-cert.pem has the following extension flags. */
  45902. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  45903. /* and the following key usage flags. */
  45904. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45905. | KU_NON_REPUDIATION
  45906. | KU_KEY_ENCIPHERMENT;
  45907. /* and the following extended key usage flags. */
  45908. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  45909. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  45910. XBADFILE);
  45911. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45912. if (f != XBADFILE) {
  45913. XFCLOSE(f);
  45914. f = XBADFILE;
  45915. }
  45916. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45917. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45918. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45919. X509_free(x509);
  45920. x509 = NULL;
  45921. /* client-cert-ext.pem has the following extension flags. */
  45922. extFlags = EXFLAG_KUSAGE;
  45923. /* and the following key usage flags. */
  45924. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45925. | KU_KEY_CERT_SIGN
  45926. | KU_CRL_SIGN;
  45927. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  45928. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45929. if (f != XBADFILE)
  45930. XFCLOSE(f);
  45931. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45932. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45933. X509_free(x509);
  45934. #endif /* OPENSSL_ALL */
  45935. return EXPECT_RESULT();
  45936. }
  45937. static int test_wolfSSL_X509_get_ext(void)
  45938. {
  45939. EXPECT_DECLS;
  45940. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45941. int ret = 0;
  45942. XFILE f = XBADFILE;
  45943. WOLFSSL_X509* x509 = NULL;
  45944. WOLFSSL_X509_EXTENSION* foundExtension;
  45945. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45946. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45947. if (f != XBADFILE)
  45948. XFCLOSE(f);
  45949. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  45950. /* wolfSSL_X509_get_ext() valid input */
  45951. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  45952. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  45953. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  45954. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  45955. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  45956. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  45957. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  45958. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  45959. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  45960. wolfSSL_X509_free(x509);
  45961. #endif
  45962. return EXPECT_RESULT();
  45963. }
  45964. static int test_wolfSSL_X509_get_ext_by_NID(void)
  45965. {
  45966. EXPECT_DECLS;
  45967. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45968. int rc = 0;
  45969. XFILE f = XBADFILE;
  45970. WOLFSSL_X509* x509 = NULL;
  45971. ASN1_OBJECT* obj = NULL;
  45972. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45973. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45974. if (f != XBADFILE)
  45975. XFCLOSE(f);
  45976. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45977. -1), 0);
  45978. /* Start search from last location (should fail) */
  45979. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45980. rc), -1);
  45981. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45982. -2), -1);
  45983. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  45984. -1), -1);
  45985. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  45986. /* NID_ext_key_usage, check also its nid and oid */
  45987. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  45988. -1);
  45989. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  45990. x509, rc)));
  45991. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  45992. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  45993. wolfSSL_X509_free(x509);
  45994. #endif
  45995. return EXPECT_RESULT();
  45996. }
  45997. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  45998. {
  45999. EXPECT_DECLS;
  46000. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  46001. int rc = 0;
  46002. XFILE f = XBADFILE;
  46003. WOLFSSL_X509* x509 = NULL;
  46004. WOLFSSL_X509_EXTENSION* ext = NULL;
  46005. WOLFSSL_ASN1_STRING* sanString = NULL;
  46006. byte* sanDer = NULL;
  46007. const byte expectedDer[] = {
  46008. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  46009. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  46010. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46011. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  46012. if (f != XBADFILE)
  46013. XFCLOSE(f);
  46014. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  46015. ExpectNotNull(ext = X509_get_ext(x509, rc));
  46016. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  46017. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  46018. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  46019. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  46020. X509_free(x509);
  46021. #endif
  46022. return EXPECT_RESULT();
  46023. }
  46024. static int test_wolfSSL_X509_EXTENSION_new(void)
  46025. {
  46026. EXPECT_DECLS;
  46027. #if defined (OPENSSL_ALL)
  46028. WOLFSSL_X509_EXTENSION* ext = NULL;
  46029. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  46030. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  46031. wolfSSL_X509_EXTENSION_free(ext);
  46032. #endif
  46033. return EXPECT_RESULT();
  46034. }
  46035. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  46036. {
  46037. EXPECT_DECLS;
  46038. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  46039. WOLFSSL_X509* x509 = NULL;
  46040. WOLFSSL_X509_EXTENSION* ext = NULL;
  46041. WOLFSSL_ASN1_OBJECT* o = NULL;
  46042. XFILE file = XBADFILE;
  46043. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46044. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  46045. if (file != XBADFILE)
  46046. XFCLOSE(file);
  46047. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  46048. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  46049. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  46050. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  46051. ExpectIntEQ(o->nid, 128);
  46052. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  46053. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  46054. wolfSSL_X509_free(x509);
  46055. #endif
  46056. return EXPECT_RESULT();
  46057. }
  46058. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  46059. {
  46060. EXPECT_DECLS;
  46061. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  46062. WOLFSSL_X509* x509 = NULL;
  46063. WOLFSSL_X509_EXTENSION* ext = NULL;
  46064. WOLFSSL_ASN1_STRING* str = NULL;
  46065. XFILE file = XBADFILE;
  46066. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46067. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  46068. if (file != XBADFILE)
  46069. XFCLOSE(file);
  46070. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  46071. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  46072. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  46073. wolfSSL_X509_free(x509);
  46074. #endif
  46075. return EXPECT_RESULT();
  46076. }
  46077. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  46078. {
  46079. EXPECT_DECLS;
  46080. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  46081. WOLFSSL_X509* x509 = NULL;
  46082. WOLFSSL_X509_EXTENSION* ext = NULL;
  46083. XFILE file = XBADFILE;
  46084. int crit = 0;
  46085. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46086. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  46087. if (file != XBADFILE)
  46088. XFCLOSE(file);
  46089. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  46090. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  46091. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  46092. wolfSSL_X509_free(x509);
  46093. #endif
  46094. return EXPECT_RESULT();
  46095. }
  46096. static int test_wolfSSL_X509V3_EXT_print(void)
  46097. {
  46098. EXPECT_DECLS;
  46099. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  46100. !defined(NO_RSA)
  46101. {
  46102. XFILE f = XBADFILE;
  46103. WOLFSSL_X509* x509 = NULL;
  46104. X509_EXTENSION * ext = NULL;
  46105. int loc = 0;
  46106. BIO *bio = NULL;
  46107. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  46108. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  46109. if (f != XBADFILE)
  46110. fclose(f);
  46111. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  46112. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  46113. NID_basic_constraints, -1), -1);
  46114. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  46115. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  46116. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  46117. NID_subject_key_identifier, -1), -1);
  46118. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  46119. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  46120. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  46121. NID_authority_key_identifier, -1), -1);
  46122. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  46123. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  46124. wolfSSL_BIO_free(bio);
  46125. wolfSSL_X509_free(x509);
  46126. }
  46127. {
  46128. X509 *x509 = NULL;
  46129. BIO *bio = NULL;
  46130. X509_EXTENSION *ext = NULL;
  46131. unsigned int i = 0;
  46132. unsigned int idx = 0;
  46133. /* Some NIDs to test with */
  46134. int nids[] = {
  46135. /* NID_key_usage, currently X509_get_ext returns this as a bit
  46136. * string, which messes up X509V3_EXT_print */
  46137. /* NID_ext_key_usage, */
  46138. NID_subject_alt_name,
  46139. };
  46140. int* n = NULL;
  46141. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  46142. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  46143. WOLFSSL_FILETYPE_PEM));
  46144. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  46145. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  46146. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  46147. * update the index */
  46148. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  46149. ExpectNotNull(ext = X509_get_ext(x509, (int)idx));
  46150. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  46151. ExpectIntGT(fprintf(stderr, "\n"), 0);
  46152. }
  46153. BIO_free(bio);
  46154. X509_free(x509);
  46155. }
  46156. #endif
  46157. return EXPECT_RESULT();
  46158. }
  46159. static int test_wolfSSL_X509_cmp(void)
  46160. {
  46161. EXPECT_DECLS;
  46162. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  46163. XFILE file1 = XBADFILE;
  46164. XFILE file2 = XBADFILE;
  46165. WOLFSSL_X509* cert1 = NULL;
  46166. WOLFSSL_X509* cert2 = NULL;
  46167. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46168. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  46169. XBADFILE);
  46170. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  46171. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  46172. if (file1 != XBADFILE)
  46173. fclose(file1);
  46174. if (file2 != XBADFILE)
  46175. fclose(file2);
  46176. /* wolfSSL_X509_cmp() testing matching certs */
  46177. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  46178. /* wolfSSL_X509_cmp() testing mismatched certs */
  46179. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  46180. /* wolfSSL_X509_cmp() testing NULL, valid args */
  46181. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  46182. /* wolfSSL_X509_cmp() testing valid, NULL args */
  46183. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  46184. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  46185. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  46186. wolfSSL_X509_free(cert1);
  46187. wolfSSL_X509_free(cert2);
  46188. #endif
  46189. return EXPECT_RESULT();
  46190. }
  46191. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  46192. {
  46193. EXPECT_DECLS;
  46194. #if defined(OPENSSL_ALL)
  46195. EVP_PKEY* pkey;
  46196. pkey = EVP_PKEY_new();
  46197. ExpectNotNull(pkey);
  46198. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  46199. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  46200. EVP_PKEY_free(pkey);
  46201. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  46202. EVP_PKEY_free(pkey);
  46203. EVP_PKEY_free(pkey);
  46204. #endif
  46205. return EXPECT_RESULT();
  46206. }
  46207. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  46208. {
  46209. EXPECT_DECLS;
  46210. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46211. EVP_PKEY* pkey = NULL;
  46212. const unsigned char* p;
  46213. unsigned char *der = NULL;
  46214. unsigned char *tmp = NULL;
  46215. int derLen;
  46216. p = client_keypub_der_2048;
  46217. /* Check that key can be successfully decoded. */
  46218. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  46219. sizeof_client_keypub_der_2048));
  46220. /* Check that key can be successfully encoded. */
  46221. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  46222. /* Ensure that the encoded version matches the original. */
  46223. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  46224. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  46225. /* Do same test except with pre-allocated buffer to ensure the der pointer
  46226. * is advanced. */
  46227. tmp = der;
  46228. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  46229. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  46230. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  46231. ExpectTrue(der + derLen == tmp);
  46232. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  46233. EVP_PKEY_free(pkey);
  46234. #endif
  46235. return EXPECT_RESULT();
  46236. }
  46237. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  46238. {
  46239. EXPECT_DECLS;
  46240. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  46241. !defined(NO_ASN) && !defined(NO_PWDBASED)
  46242. EVP_PKEY* pkey = NULL;
  46243. const unsigned char* p;
  46244. unsigned char *der = NULL;
  46245. unsigned char *tmp = NULL;
  46246. int derLen;
  46247. unsigned char pub_buf[65];
  46248. const int pub_len = 65;
  46249. BN_CTX* ctx;
  46250. EC_GROUP* curve = NULL;
  46251. EC_KEY* ephemeral_key = NULL;
  46252. const EC_POINT* h;
  46253. /* Generate an x963 key pair and get public part into pub_buf */
  46254. ExpectNotNull(ctx = BN_CTX_new());
  46255. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  46256. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  46257. NID_X9_62_prime256v1));
  46258. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  46259. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  46260. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  46261. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  46262. /* Prepare the EVP_PKEY */
  46263. ExpectNotNull(pkey = EVP_PKEY_new());
  46264. p = pub_buf;
  46265. /* Check that key can be successfully decoded. */
  46266. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  46267. pub_len));
  46268. /* Check that key can be successfully encoded. */
  46269. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  46270. /* Ensure that the encoded version matches the original. */
  46271. ExpectIntEQ(derLen, pub_len);
  46272. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  46273. /* Do same test except with pre-allocated buffer to ensure the der pointer
  46274. * is advanced. */
  46275. tmp = der;
  46276. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  46277. ExpectIntEQ(derLen, pub_len);
  46278. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  46279. ExpectTrue(der + derLen == tmp);
  46280. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  46281. EVP_PKEY_free(pkey);
  46282. EC_KEY_free(ephemeral_key);
  46283. EC_GROUP_free(curve);
  46284. #endif
  46285. return EXPECT_RESULT();
  46286. }
  46287. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  46288. {
  46289. EXPECT_DECLS;
  46290. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  46291. DSA* dsa = NULL;
  46292. byte derIn[] = {
  46293. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  46294. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  46295. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  46296. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  46297. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  46298. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  46299. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  46300. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  46301. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  46302. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  46303. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  46304. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  46305. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  46306. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  46307. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  46308. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  46309. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  46310. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  46311. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  46312. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  46313. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  46314. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  46315. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  46316. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  46317. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  46318. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  46319. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  46320. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  46321. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  46322. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  46323. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  46324. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  46325. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  46326. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  46327. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  46328. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  46329. 0x1e, 0xde, 0xc4
  46330. };
  46331. int derInLen = sizeof(derIn);
  46332. byte* derOut = NULL;
  46333. int derOutLen;
  46334. byte* p = derIn;
  46335. /* Check that params can be successfully decoded. */
  46336. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  46337. /* Check that params can be successfully encoded. */
  46338. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  46339. /* Ensure that the encoded version matches the original. */
  46340. ExpectIntEQ(derInLen, derOutLen);
  46341. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  46342. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  46343. DSA_free(dsa);
  46344. #endif
  46345. return EXPECT_RESULT();
  46346. }
  46347. static int test_wolfSSL_i2d_PrivateKey(void)
  46348. {
  46349. EXPECT_DECLS;
  46350. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  46351. !defined(NO_ASN) && !defined(NO_PWDBASED)
  46352. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  46353. {
  46354. EVP_PKEY* pkey = NULL;
  46355. const unsigned char* server_key =
  46356. (const unsigned char*)server_key_der_2048;
  46357. unsigned char buf[FOURK_BUF];
  46358. unsigned char* pt = NULL;
  46359. int bufSz = 0;
  46360. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  46361. (long)sizeof_server_key_der_2048));
  46362. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  46363. pt = buf;
  46364. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  46365. ExpectIntNE((pt - buf), 0);
  46366. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  46367. EVP_PKEY_free(pkey);
  46368. }
  46369. #endif
  46370. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46371. {
  46372. EVP_PKEY* pkey = NULL;
  46373. const unsigned char* client_key =
  46374. (const unsigned char*)ecc_clikey_der_256;
  46375. unsigned char buf[FOURK_BUF];
  46376. unsigned char* pt = NULL;
  46377. int bufSz = 0;
  46378. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  46379. (long)sizeof_ecc_clikey_der_256)));
  46380. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  46381. pt = buf;
  46382. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  46383. ExpectIntNE((pt - buf), 0);
  46384. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  46385. EVP_PKEY_free(pkey);
  46386. }
  46387. #endif
  46388. #endif
  46389. return EXPECT_RESULT();
  46390. }
  46391. static int test_wolfSSL_OCSP_id_get0_info(void)
  46392. {
  46393. EXPECT_DECLS;
  46394. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  46395. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46396. X509* cert = NULL;
  46397. X509* issuer = NULL;
  46398. OCSP_CERTID* id = NULL;
  46399. OCSP_CERTID* id2 = NULL;
  46400. ASN1_STRING* name = NULL;
  46401. ASN1_OBJECT* pmd = NULL;
  46402. ASN1_STRING* keyHash = NULL;
  46403. ASN1_INTEGER* serial = NULL;
  46404. ASN1_INTEGER* x509Int = NULL;
  46405. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  46406. SSL_FILETYPE_PEM));
  46407. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  46408. SSL_FILETYPE_PEM));
  46409. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  46410. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  46411. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  46412. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  46413. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  46414. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  46415. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  46416. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  46417. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  46418. ExpectNotNull(serial);
  46419. /* compare serial number to one in cert, should be equal */
  46420. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  46421. ExpectIntEQ(x509Int->length, serial->length);
  46422. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  46423. /* test OCSP_id_cmp */
  46424. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  46425. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  46426. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  46427. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  46428. if (id != NULL) {
  46429. id->issuerHash[0] = ~id->issuerHash[0];
  46430. }
  46431. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  46432. OCSP_CERTID_free(id);
  46433. OCSP_CERTID_free(id2);
  46434. X509_free(cert); /* free's x509Int */
  46435. X509_free(issuer);
  46436. #endif
  46437. return EXPECT_RESULT();
  46438. }
  46439. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  46440. {
  46441. EXPECT_DECLS;
  46442. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  46443. WOLFSSL_OCSP_CERTID certId;
  46444. byte* targetBuffer = NULL;
  46445. byte* p;
  46446. /* OCSP CertID bytes taken from PCAP */
  46447. byte rawCertId[] = {
  46448. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  46449. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  46450. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  46451. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  46452. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  46453. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  46454. 0xcf, 0xbc, 0x91
  46455. };
  46456. int ret = 0;
  46457. int i;
  46458. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  46459. certId.rawCertId = rawCertId;
  46460. certId.rawCertIdSize = sizeof(rawCertId);
  46461. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  46462. DYNAMIC_TYPE_TMP_BUFFER));
  46463. p = targetBuffer;
  46464. /* Function returns the size of the encoded data. */
  46465. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  46466. /* If target buffer is not null, function increments targetBuffer to point
  46467. * just past the end of the encoded data. */
  46468. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  46469. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  46470. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  46471. }
  46472. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46473. targetBuffer = NULL;
  46474. /* If target buffer is null, function allocates memory for a buffer and
  46475. * copies the encoded data into it. targetBuffer then points to the start of
  46476. * this newly allocate buffer. */
  46477. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  46478. sizeof(rawCertId));
  46479. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  46480. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  46481. }
  46482. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  46483. #endif
  46484. return EXPECT_RESULT();
  46485. }
  46486. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  46487. {
  46488. EXPECT_DECLS;
  46489. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  46490. WOLFSSL_OCSP_CERTID* certId;
  46491. WOLFSSL_OCSP_CERTID* certIdGood;
  46492. WOLFSSL_OCSP_CERTID* certIdBad;
  46493. const unsigned char* rawCertIdPtr;
  46494. const unsigned char rawCertId[] = {
  46495. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  46496. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  46497. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  46498. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  46499. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  46500. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  46501. 0xcf, 0xbc, 0x91
  46502. };
  46503. rawCertIdPtr = &rawCertId[0];
  46504. /* If the cert ID is NULL the function should allocate it and copy the
  46505. * data to it. */
  46506. certId = NULL;
  46507. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  46508. sizeof(rawCertId)));
  46509. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  46510. if (certId != NULL) {
  46511. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  46512. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  46513. }
  46514. /* If the cert ID is not NULL the function will just copy the data to it. */
  46515. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  46516. DYNAMIC_TYPE_TMP_BUFFER));
  46517. ExpectNotNull(certId);
  46518. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  46519. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  46520. rawCertIdPtr = &rawCertId[0];
  46521. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  46522. sizeof(rawCertId)));
  46523. ExpectPtrEq(certIdGood, certId);
  46524. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  46525. if (certId != NULL) {
  46526. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  46527. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46528. certId = NULL;
  46529. }
  46530. /* The below tests should fail when passed bad parameters. NULL should
  46531. * always be returned. */
  46532. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  46533. sizeof(rawCertId)));
  46534. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  46535. sizeof(rawCertId)));
  46536. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  46537. #endif
  46538. return EXPECT_RESULT();
  46539. }
  46540. static int test_wolfSSL_OCSP_id_cmp(void)
  46541. {
  46542. EXPECT_DECLS;
  46543. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46544. OCSP_CERTID id1;
  46545. OCSP_CERTID id2;
  46546. XMEMSET(&id1, 0, sizeof(id1));
  46547. XMEMSET(&id2, 0, sizeof(id2));
  46548. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  46549. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  46550. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  46551. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  46552. #endif
  46553. return EXPECT_RESULT();
  46554. }
  46555. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  46556. {
  46557. EXPECT_DECLS;
  46558. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46559. WOLFSSL_OCSP_SINGLERESP single;
  46560. const WOLFSSL_OCSP_CERTID* certId;
  46561. XMEMSET(&single, 0, sizeof(single));
  46562. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  46563. ExpectPtrEq(&single, certId);
  46564. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  46565. #endif
  46566. return EXPECT_RESULT();
  46567. }
  46568. static int test_wolfSSL_OCSP_single_get0_status(void)
  46569. {
  46570. EXPECT_DECLS;
  46571. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46572. WOLFSSL_OCSP_SINGLERESP single;
  46573. CertStatus certStatus;
  46574. WOLFSSL_ASN1_TIME* thisDate;
  46575. WOLFSSL_ASN1_TIME* nextDate;
  46576. int ret, i;
  46577. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46578. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  46579. /* Fill the date fields with some dummy data. */
  46580. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  46581. certStatus.thisDateParsed.data[i] = i;
  46582. certStatus.nextDateParsed.data[i] = i;
  46583. }
  46584. certStatus.status = CERT_GOOD;
  46585. single.status = &certStatus;
  46586. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  46587. &nextDate);
  46588. ExpectIntEQ(ret, CERT_GOOD);
  46589. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  46590. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  46591. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  46592. CERT_GOOD);
  46593. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  46594. NULL), CERT_GOOD);
  46595. #endif
  46596. return EXPECT_RESULT();
  46597. }
  46598. static int test_wolfSSL_OCSP_resp_count(void)
  46599. {
  46600. EXPECT_DECLS;
  46601. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46602. WOLFSSL_OCSP_BASICRESP basicResp;
  46603. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  46604. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  46605. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  46606. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46607. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46608. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  46609. basicResp.single = &singleRespOne;
  46610. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  46611. singleRespOne.next = &singleRespTwo;
  46612. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  46613. #endif
  46614. return EXPECT_RESULT();
  46615. }
  46616. static int test_wolfSSL_OCSP_resp_get0(void)
  46617. {
  46618. EXPECT_DECLS;
  46619. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46620. WOLFSSL_OCSP_BASICRESP basicResp;
  46621. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  46622. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  46623. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  46624. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46625. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46626. basicResp.single = &singleRespOne;
  46627. singleRespOne.next = &singleRespTwo;
  46628. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  46629. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  46630. #endif
  46631. return EXPECT_RESULT();
  46632. }
  46633. static int test_wolfSSL_EVP_PKEY_derive(void)
  46634. {
  46635. EXPECT_DECLS;
  46636. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  46637. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  46638. EVP_PKEY_CTX *ctx = NULL;
  46639. unsigned char *skey = NULL;
  46640. size_t skeylen;
  46641. EVP_PKEY *pkey = NULL;
  46642. EVP_PKEY *peerkey = NULL;
  46643. const unsigned char* key;
  46644. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  46645. /* DH */
  46646. key = dh_key_der_2048;
  46647. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  46648. sizeof_dh_key_der_2048)));
  46649. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  46650. key = dh_key_der_2048;
  46651. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  46652. sizeof_dh_key_der_2048)));
  46653. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  46654. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46655. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  46656. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  46657. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  46658. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  46659. DYNAMIC_TYPE_OPENSSL));
  46660. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  46661. EVP_PKEY_CTX_free(ctx);
  46662. ctx = NULL;
  46663. EVP_PKEY_free(peerkey);
  46664. peerkey = NULL;
  46665. EVP_PKEY_free(pkey);
  46666. pkey = NULL;
  46667. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  46668. skey = NULL;
  46669. #endif
  46670. #ifdef HAVE_ECC
  46671. /* ECDH */
  46672. key = ecc_clikey_der_256;
  46673. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  46674. sizeof_ecc_clikey_der_256)));
  46675. key = ecc_clikeypub_der_256;
  46676. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  46677. sizeof_ecc_clikeypub_der_256)));
  46678. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46679. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  46680. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  46681. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  46682. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  46683. DYNAMIC_TYPE_OPENSSL));
  46684. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  46685. EVP_PKEY_CTX_free(ctx);
  46686. EVP_PKEY_free(peerkey);
  46687. EVP_PKEY_free(pkey);
  46688. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  46689. #endif /* HAVE_ECC */
  46690. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  46691. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  46692. return EXPECT_RESULT();
  46693. }
  46694. static int test_wolfSSL_EVP_PBE_scrypt(void)
  46695. {
  46696. EXPECT_DECLS;
  46697. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  46698. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  46699. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  46700. int ret;
  46701. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  46702. int pwdlen = sizeof(pwd);
  46703. const byte salt[] = {'N','a','C','l'};
  46704. int saltlen = sizeof(salt);
  46705. byte key[80];
  46706. word64 numOvr32 = (word64)INT32_MAX + 1;
  46707. /* expected derived key for N:16, r:1, p:1 */
  46708. const byte expectedKey[] = {
  46709. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  46710. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  46711. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  46712. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  46713. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  46714. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  46715. 0xE7, 0xE9, 0xC0, 0x9A};
  46716. /* N r p mx key keylen */
  46717. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  46718. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  46719. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  46720. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  46721. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  46722. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  46723. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  46724. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  46725. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  46726. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  46727. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  46728. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  46729. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  46730. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  46731. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  46732. ExpectIntEQ(ret, 1); /* should succeed */
  46733. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  46734. key, 64);
  46735. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  46736. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  46737. key, 64);
  46738. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  46739. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  46740. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  46741. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  46742. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  46743. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  46744. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  46745. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  46746. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  46747. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  46748. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  46749. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  46750. ExpectIntEQ(ret, 1);
  46751. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  46752. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  46753. #endif /* !NO_PWDBASED && !NO_SHA256 */
  46754. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  46755. return EXPECT_RESULT();
  46756. }
  46757. static int test_no_op_functions(void)
  46758. {
  46759. EXPECT_DECLS;
  46760. #if defined(OPENSSL_EXTRA)
  46761. /* this makes sure wolfSSL can compile and run these no-op functions */
  46762. SSL_load_error_strings();
  46763. ENGINE_load_builtin_engines();
  46764. OpenSSL_add_all_ciphers();
  46765. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  46766. #endif
  46767. return EXPECT_RESULT();
  46768. }
  46769. static int test_wolfSSL_CRYPTO_memcmp(void)
  46770. {
  46771. EXPECT_DECLS;
  46772. #ifdef OPENSSL_EXTRA
  46773. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46774. "implementation of TLS/SSL for embedded devices to the cloud.";
  46775. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46776. "implementation of TLS/SSL for embedded devices to the cloud.";
  46777. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46778. "implementation of TLS/SSL for embedded devices to the cloud!";
  46779. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  46780. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  46781. #endif
  46782. return EXPECT_RESULT();
  46783. }
  46784. /*----------------------------------------------------------------------------*
  46785. | wolfCrypt ASN
  46786. *----------------------------------------------------------------------------*/
  46787. static int test_wc_CreateEncryptedPKCS8Key(void)
  46788. {
  46789. EXPECT_DECLS;
  46790. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  46791. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  46792. WC_RNG rng;
  46793. byte* encKey = NULL;
  46794. word32 encKeySz = 0;
  46795. word32 decKeySz = 0;
  46796. const char password[] = "Lorem ipsum dolor sit amet";
  46797. word32 passwordSz = (word32)XSTRLEN(password);
  46798. word32 tradIdx = 0;
  46799. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46800. ExpectIntEQ(wc_InitRng(&rng), 0);
  46801. PRIVATE_KEY_UNLOCK();
  46802. /* Call with NULL for out buffer to get necessary length. */
  46803. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46804. sizeof_server_key_der_2048, NULL, &encKeySz, password, (int)passwordSz,
  46805. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46806. LENGTH_ONLY_E);
  46807. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  46808. DYNAMIC_TYPE_TMP_BUFFER));
  46809. /* Call with the allocated out buffer. */
  46810. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46811. sizeof_server_key_der_2048, encKey, &encKeySz, password, (int)passwordSz,
  46812. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46813. 0);
  46814. /* Decrypt the encrypted PKCS8 key we just made. */
  46815. ExpectIntGT((decKeySz = (word32)wc_DecryptPKCS8Key(encKey, encKeySz, password,
  46816. (int)passwordSz)), 0);
  46817. /* encKey now holds the decrypted key (decrypted in place). */
  46818. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  46819. /* Check that the decrypted key matches the key prior to encryption. */
  46820. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  46821. sizeof_server_key_der_2048), 0);
  46822. PRIVATE_KEY_LOCK();
  46823. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46824. wc_FreeRng(&rng);
  46825. #endif
  46826. return EXPECT_RESULT();
  46827. }
  46828. static int test_wc_GetPkcs8TraditionalOffset(void)
  46829. {
  46830. EXPECT_DECLS;
  46831. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  46832. int length;
  46833. int derSz = 0;
  46834. word32 inOutIdx;
  46835. const char* path = "./certs/server-keyPkcs8.der";
  46836. XFILE file = XBADFILE;
  46837. byte der[2048];
  46838. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  46839. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  46840. if (file != XBADFILE)
  46841. XFCLOSE(file);
  46842. /* valid case */
  46843. inOutIdx = 0;
  46844. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46845. 0);
  46846. /* inOutIdx > sz */
  46847. inOutIdx = 4000;
  46848. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46849. BAD_FUNC_ARG);
  46850. /* null input */
  46851. inOutIdx = 0;
  46852. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  46853. BAD_FUNC_ARG);
  46854. /* invalid input, fill buffer with 1's */
  46855. XMEMSET(der, 1, sizeof(der));
  46856. inOutIdx = 0;
  46857. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46858. ASN_PARSE_E);
  46859. #endif /* NO_ASN */
  46860. return EXPECT_RESULT();
  46861. }
  46862. static int test_wc_SetSubjectRaw(void)
  46863. {
  46864. EXPECT_DECLS;
  46865. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46866. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46867. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46868. WOLFSSL_X509* x509 = NULL;
  46869. int peerCertSz;
  46870. const byte* peerCertBuf = NULL;
  46871. Cert forgedCert;
  46872. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46873. WOLFSSL_FILETYPE_ASN1));
  46874. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46875. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46876. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  46877. wolfSSL_FreeX509(x509);
  46878. #endif
  46879. return EXPECT_RESULT();
  46880. }
  46881. static int test_wc_GetSubjectRaw(void)
  46882. {
  46883. EXPECT_DECLS;
  46884. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46885. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  46886. Cert cert;
  46887. byte *subjectRaw;
  46888. ExpectIntEQ(0, wc_InitCert(&cert));
  46889. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  46890. #endif
  46891. return EXPECT_RESULT();
  46892. }
  46893. static int test_wc_SetIssuerRaw(void)
  46894. {
  46895. EXPECT_DECLS;
  46896. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46897. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46898. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46899. WOLFSSL_X509* x509 = NULL;
  46900. int peerCertSz;
  46901. const byte* peerCertBuf;
  46902. Cert forgedCert;
  46903. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46904. WOLFSSL_FILETYPE_ASN1));
  46905. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46906. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46907. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  46908. wolfSSL_FreeX509(x509);
  46909. #endif
  46910. return EXPECT_RESULT();
  46911. }
  46912. static int test_wc_SetIssueBuffer(void)
  46913. {
  46914. EXPECT_DECLS;
  46915. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46916. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46917. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46918. WOLFSSL_X509* x509 = NULL;
  46919. int peerCertSz;
  46920. const byte* peerCertBuf;
  46921. Cert forgedCert;
  46922. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46923. WOLFSSL_FILETYPE_ASN1));
  46924. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46925. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46926. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  46927. wolfSSL_FreeX509(x509);
  46928. #endif
  46929. return EXPECT_RESULT();
  46930. }
  46931. /*
  46932. * Testing wc_SetSubjectKeyId
  46933. */
  46934. static int test_wc_SetSubjectKeyId(void)
  46935. {
  46936. EXPECT_DECLS;
  46937. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46938. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46939. Cert cert;
  46940. const char* file = "certs/ecc-client-keyPub.pem";
  46941. ExpectIntEQ(0, wc_InitCert(&cert));
  46942. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  46943. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  46944. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  46945. #endif
  46946. return EXPECT_RESULT();
  46947. } /* END test_wc_SetSubjectKeyId */
  46948. /*
  46949. * Testing wc_SetSubject
  46950. */
  46951. static int test_wc_SetSubject(void)
  46952. {
  46953. EXPECT_DECLS;
  46954. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46955. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46956. Cert cert;
  46957. const char* file = "./certs/ca-ecc-cert.pem";
  46958. ExpectIntEQ(0, wc_InitCert(&cert));
  46959. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  46960. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  46961. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  46962. #endif
  46963. return EXPECT_RESULT();
  46964. } /* END test_wc_SetSubject */
  46965. static int test_CheckCertSignature(void)
  46966. {
  46967. EXPECT_DECLS;
  46968. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  46969. WOLFSSL_CERT_MANAGER* cm = NULL;
  46970. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46971. XFILE fp = XBADFILE;
  46972. byte cert[4096];
  46973. int certSz;
  46974. #endif
  46975. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, NULL));
  46976. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46977. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, cm));
  46978. #ifndef NO_RSA
  46979. #ifdef USE_CERT_BUFFERS_1024
  46980. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_1024,
  46981. sizeof_server_cert_der_1024, NULL, cm));
  46982. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46983. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  46984. WOLFSSL_FILETYPE_ASN1));
  46985. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_1024,
  46986. sizeof_server_cert_der_1024, NULL, cm));
  46987. #elif defined(USE_CERT_BUFFERS_2048)
  46988. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_2048,
  46989. sizeof_server_cert_der_2048, NULL, cm));
  46990. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46991. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  46992. WOLFSSL_FILETYPE_ASN1));
  46993. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_2048,
  46994. sizeof_server_cert_der_2048, NULL, cm));
  46995. #endif
  46996. #endif
  46997. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46998. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(serv_ecc_der_256,
  46999. sizeof_serv_ecc_der_256, NULL, cm));
  47000. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  47001. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  47002. WOLFSSL_FILETYPE_ASN1));
  47003. ExpectIntEQ(0, wc_CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  47004. NULL, cm));
  47005. #endif
  47006. #if !defined(NO_FILESYSTEM)
  47007. wolfSSL_CertManagerFree(cm);
  47008. cm = NULL;
  47009. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  47010. #ifndef NO_RSA
  47011. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  47012. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  47013. if (fp != XBADFILE) {
  47014. XFCLOSE(fp);
  47015. fp = XBADFILE;
  47016. }
  47017. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  47018. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  47019. "./certs/ca-cert.pem", NULL));
  47020. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  47021. #endif
  47022. #ifdef HAVE_ECC
  47023. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  47024. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  47025. if (fp != XBADFILE) {
  47026. XFCLOSE(fp);
  47027. fp = XBADFILE;
  47028. }
  47029. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  47030. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  47031. "./certs/ca-ecc-cert.pem", NULL));
  47032. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  47033. #endif
  47034. #endif
  47035. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  47036. (void)fp;
  47037. (void)cert;
  47038. (void)certSz;
  47039. #endif
  47040. wolfSSL_CertManagerFree(cm);
  47041. #endif
  47042. return EXPECT_RESULT();
  47043. }
  47044. static int test_wc_ParseCert(void)
  47045. {
  47046. EXPECT_DECLS;
  47047. #if !defined(NO_CERTS) && !defined(NO_RSA)
  47048. DecodedCert decodedCert;
  47049. const byte* rawCert = client_cert_der_2048;
  47050. const int rawCertSize = sizeof_client_cert_der_2048;
  47051. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  47052. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  47053. #ifndef IGNORE_NAME_CONSTRAINTS
  47054. /* check that the subjects emailAddress was not put in the alt name list */
  47055. ExpectNotNull(decodedCert.subjectEmail);
  47056. ExpectNull(decodedCert.altEmailNames);
  47057. #endif
  47058. wc_FreeDecodedCert(&decodedCert);
  47059. #endif
  47060. return EXPECT_RESULT();
  47061. }
  47062. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  47063. * the API safely errors out on badly-formed ASN input.
  47064. * NOTE: Test not compatible with released FIPS implementations!
  47065. */
  47066. static int test_wc_ParseCert_Error(void)
  47067. {
  47068. EXPECT_DECLS;
  47069. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  47070. (!defined(HAVE_FIPS) || \
  47071. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  47072. DecodedCert decodedCert;
  47073. int i;
  47074. /* Certificate data */
  47075. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  47076. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  47077. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  47078. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  47079. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  47080. /* Test data */
  47081. const struct testStruct {
  47082. const byte* c;
  47083. const int cSz;
  47084. const int expRet;
  47085. } t[] = {
  47086. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  47087. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  47088. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  47089. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  47090. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  47091. };
  47092. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  47093. for (i = 0; i < tSz; i++) {
  47094. WOLFSSL_MSG_EX("i == %d", i);
  47095. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  47096. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  47097. wc_FreeDecodedCert(&decodedCert);
  47098. }
  47099. #endif
  47100. return EXPECT_RESULT();
  47101. }
  47102. static int test_MakeCertWithPathLen(void)
  47103. {
  47104. EXPECT_DECLS;
  47105. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  47106. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  47107. const byte expectedPathLen = 7;
  47108. Cert cert;
  47109. DecodedCert decodedCert;
  47110. byte der[FOURK_BUF];
  47111. int derSize = 0;
  47112. WC_RNG rng;
  47113. ecc_key key;
  47114. int ret;
  47115. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47116. XMEMSET(&key, 0, sizeof(ecc_key));
  47117. XMEMSET(&cert, 0, sizeof(Cert));
  47118. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  47119. ExpectIntEQ(wc_InitRng(&rng), 0);
  47120. ExpectIntEQ(wc_ecc_init(&key), 0);
  47121. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  47122. ExpectIntEQ(wc_InitCert(&cert), 0);
  47123. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  47124. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  47125. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  47126. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  47127. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  47128. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  47129. CTC_NAME_SIZE);
  47130. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  47131. CTC_NAME_SIZE);
  47132. cert.selfSigned = 1;
  47133. cert.isCA = 1;
  47134. cert.pathLen = expectedPathLen;
  47135. cert.pathLenSet = 1;
  47136. cert.sigType = CTC_SHA256wECDSA;
  47137. #ifdef WOLFSSL_CERT_EXT
  47138. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  47139. #endif
  47140. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  47141. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  47142. FOURK_BUF, NULL, &key, &rng), 0);
  47143. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  47144. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  47145. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  47146. wc_FreeDecodedCert(&decodedCert);
  47147. ret = wc_ecc_free(&key);
  47148. ExpectIntEQ(ret, 0);
  47149. ret = wc_FreeRng(&rng);
  47150. ExpectIntEQ(ret, 0);
  47151. #endif
  47152. return EXPECT_RESULT();
  47153. }
  47154. static int test_MakeCertWith0Ser(void)
  47155. {
  47156. EXPECT_DECLS;
  47157. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  47158. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC) && \
  47159. defined(WOLFSSL_ASN_TEMPLATE)
  47160. Cert cert;
  47161. DecodedCert decodedCert;
  47162. byte der[FOURK_BUF];
  47163. int derSize = 0;
  47164. WC_RNG rng;
  47165. ecc_key key;
  47166. int ret;
  47167. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47168. XMEMSET(&key, 0, sizeof(ecc_key));
  47169. XMEMSET(&cert, 0, sizeof(Cert));
  47170. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  47171. ExpectIntEQ(wc_InitRng(&rng), 0);
  47172. ExpectIntEQ(wc_ecc_init(&key), 0);
  47173. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  47174. ExpectIntEQ(wc_InitCert(&cert), 0);
  47175. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  47176. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  47177. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  47178. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  47179. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  47180. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  47181. CTC_NAME_SIZE);
  47182. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  47183. CTC_NAME_SIZE);
  47184. cert.selfSigned = 1;
  47185. cert.isCA = 1;
  47186. cert.sigType = CTC_SHA256wECDSA;
  47187. #ifdef WOLFSSL_CERT_EXT
  47188. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  47189. #endif
  47190. /* set serial number to 0 */
  47191. cert.serialSz = 1;
  47192. cert.serial[0] = 0;
  47193. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  47194. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  47195. FOURK_BUF, NULL, &key, &rng), 0);
  47196. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  47197. #if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON)
  47198. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL),
  47199. ASN_PARSE_E);
  47200. #else
  47201. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  47202. #endif
  47203. wc_FreeDecodedCert(&decodedCert);
  47204. ret = wc_ecc_free(&key);
  47205. ExpectIntEQ(ret, 0);
  47206. ret = wc_FreeRng(&rng);
  47207. ExpectIntEQ(ret, 0);
  47208. #endif
  47209. return EXPECT_RESULT();
  47210. }
  47211. static int test_MakeCertWithCaFalse(void)
  47212. {
  47213. EXPECT_DECLS;
  47214. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  47215. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  47216. const byte expectedIsCa = 0;
  47217. Cert cert;
  47218. DecodedCert decodedCert;
  47219. byte der[FOURK_BUF];
  47220. int derSize = 0;
  47221. WC_RNG rng;
  47222. ecc_key key;
  47223. int ret;
  47224. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47225. XMEMSET(&key, 0, sizeof(ecc_key));
  47226. XMEMSET(&cert, 0, sizeof(Cert));
  47227. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  47228. ExpectIntEQ(wc_InitRng(&rng), 0);
  47229. ExpectIntEQ(wc_ecc_init(&key), 0);
  47230. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  47231. ExpectIntEQ(wc_InitCert(&cert), 0);
  47232. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  47233. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  47234. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  47235. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  47236. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  47237. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  47238. CTC_NAME_SIZE);
  47239. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  47240. CTC_NAME_SIZE);
  47241. cert.selfSigned = 1;
  47242. cert.isCA = expectedIsCa;
  47243. cert.isCaSet = 1;
  47244. cert.sigType = CTC_SHA256wECDSA;
  47245. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  47246. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  47247. FOURK_BUF, NULL, &key, &rng), 0);
  47248. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  47249. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  47250. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  47251. wc_FreeDecodedCert(&decodedCert);
  47252. ret = wc_ecc_free(&key);
  47253. ExpectIntEQ(ret, 0);
  47254. ret = wc_FreeRng(&rng);
  47255. ExpectIntEQ(ret, 0);
  47256. #endif
  47257. return EXPECT_RESULT();
  47258. }
  47259. /*----------------------------------------------------------------------------*
  47260. | wolfCrypt ECC
  47261. *----------------------------------------------------------------------------*/
  47262. static int test_wc_ecc_get_curve_size_from_name(void)
  47263. {
  47264. EXPECT_DECLS;
  47265. #ifdef HAVE_ECC
  47266. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47267. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  47268. #endif
  47269. /* invalid case */
  47270. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  47271. /* NULL input */
  47272. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  47273. #endif /* HAVE_ECC */
  47274. return EXPECT_RESULT();
  47275. }
  47276. static int test_wc_ecc_get_curve_id_from_name(void)
  47277. {
  47278. EXPECT_DECLS;
  47279. #ifdef HAVE_ECC
  47280. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47281. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  47282. ECC_SECP256R1);
  47283. #endif
  47284. /* invalid case */
  47285. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  47286. /* NULL input */
  47287. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  47288. #endif /* HAVE_ECC */
  47289. return EXPECT_RESULT();
  47290. }
  47291. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  47292. !defined(HAVE_SELFTEST) && \
  47293. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  47294. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  47295. {
  47296. EXPECT_DECLS;
  47297. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47298. ecc_key* key;
  47299. const ecc_set_type* params = NULL;
  47300. int ret;
  47301. #endif
  47302. WOLFSSL_EC_KEY *ecKey = NULL;
  47303. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47304. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  47305. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  47306. if (EXPECT_SUCCESS()) {
  47307. ret = EC_KEY_generate_key(ecKey);
  47308. } else
  47309. ret = 0;
  47310. if (ret == 1) {
  47311. /* normal test */
  47312. key = (ecc_key*)ecKey->internal;
  47313. if (key != NULL) {
  47314. params = key->dp;
  47315. }
  47316. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  47317. ECC_SECP256R1);
  47318. }
  47319. #endif
  47320. /* invalid case, NULL input*/
  47321. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  47322. wolfSSL_EC_KEY_free(ecKey);
  47323. return EXPECT_RESULT();
  47324. }
  47325. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  47326. static int test_wc_ecc_get_curve_id_from_params(void)
  47327. {
  47328. EXPECT_DECLS;
  47329. #ifdef HAVE_ECC
  47330. const byte prime[] =
  47331. {
  47332. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  47333. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47334. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  47335. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  47336. };
  47337. const byte primeInvalid[] =
  47338. {
  47339. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  47340. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47341. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  47342. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  47343. };
  47344. const byte Af[] =
  47345. {
  47346. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  47347. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47348. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  47349. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  47350. };
  47351. const byte Bf[] =
  47352. {
  47353. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  47354. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  47355. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  47356. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  47357. };
  47358. const byte order[] =
  47359. {
  47360. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  47361. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47362. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  47363. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  47364. };
  47365. const byte Gx[] =
  47366. {
  47367. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  47368. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  47369. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  47370. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  47371. };
  47372. const byte Gy[] =
  47373. {
  47374. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  47375. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  47376. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  47377. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  47378. };
  47379. int cofactor = 1;
  47380. int fieldSize = 256;
  47381. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47382. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  47383. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  47384. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  47385. ECC_SECP256R1);
  47386. #endif
  47387. /* invalid case, fieldSize = 0 */
  47388. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  47389. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  47390. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  47391. /* invalid case, NULL prime */
  47392. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  47393. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  47394. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  47395. /* invalid case, invalid prime */
  47396. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  47397. primeInvalid, sizeof(primeInvalid),
  47398. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  47399. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  47400. #endif
  47401. return EXPECT_RESULT();
  47402. }
  47403. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  47404. {
  47405. EXPECT_DECLS;
  47406. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  47407. WOLFSSL_RSA* rsa = NULL;
  47408. WOLFSSL_EVP_PKEY* pkey = NULL;
  47409. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  47410. const char* in = "What is easy to do is easy not to do.";
  47411. size_t inlen = XSTRLEN(in);
  47412. size_t outEncLen = 0;
  47413. byte* outEnc = NULL;
  47414. byte* outDec = NULL;
  47415. size_t outDecLen = 0;
  47416. size_t rsaKeySz = 2048/8; /* Bytes */
  47417. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  47418. byte* inTmp = NULL;
  47419. byte* outEncTmp = NULL;
  47420. byte* outDecTmp = NULL;
  47421. #endif
  47422. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47423. DYNAMIC_TYPE_TMP_BUFFER));
  47424. if (outEnc != NULL) {
  47425. XMEMSET(outEnc, 0, rsaKeySz);
  47426. }
  47427. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47428. DYNAMIC_TYPE_TMP_BUFFER));
  47429. if (outDec != NULL) {
  47430. XMEMSET(outDec, 0, rsaKeySz);
  47431. }
  47432. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47433. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  47434. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  47435. if (EXPECT_FAIL()) {
  47436. RSA_free(rsa);
  47437. }
  47438. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  47439. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  47440. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  47441. WOLFSSL_SUCCESS);
  47442. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  47443. since ctx uses it.*/
  47444. ExpectIntEQ(pkey->ref.count, 2);
  47445. EVP_PKEY_free(pkey);
  47446. ExpectIntEQ(pkey->ref.count, 1);
  47447. /* Encrypt data */
  47448. /* Check that we can get the required output buffer length by passing in a
  47449. * NULL output buffer. */
  47450. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  47451. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  47452. ExpectIntEQ(rsaKeySz, outEncLen);
  47453. /* Now do the actual encryption. */
  47454. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  47455. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  47456. /* Decrypt data */
  47457. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  47458. /* Check that we can get the required output buffer length by passing in a
  47459. * NULL output buffer. */
  47460. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  47461. WOLFSSL_SUCCESS);
  47462. ExpectIntEQ(rsaKeySz, outDecLen);
  47463. /* Now do the actual decryption. */
  47464. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  47465. WOLFSSL_SUCCESS);
  47466. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  47467. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  47468. /* The input length must be the same size as the RSA key.*/
  47469. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47470. DYNAMIC_TYPE_TMP_BUFFER));
  47471. if (inTmp != NULL) {
  47472. XMEMSET(inTmp, 9, rsaKeySz);
  47473. }
  47474. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47475. DYNAMIC_TYPE_TMP_BUFFER));
  47476. if (outEncTmp != NULL) {
  47477. XMEMSET(outEncTmp, 0, rsaKeySz);
  47478. }
  47479. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47480. DYNAMIC_TYPE_TMP_BUFFER));
  47481. if (outDecTmp != NULL) {
  47482. XMEMSET(outDecTmp, 0, rsaKeySz);
  47483. }
  47484. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  47485. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  47486. WOLFSSL_SUCCESS);
  47487. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  47488. WOLFSSL_SUCCESS);
  47489. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  47490. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  47491. outEncLen), WOLFSSL_SUCCESS);
  47492. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  47493. #endif
  47494. EVP_PKEY_CTX_free(ctx);
  47495. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47496. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47497. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  47498. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47499. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47500. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47501. #endif
  47502. #endif
  47503. return EXPECT_RESULT();
  47504. }
  47505. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47506. !defined(HAVE_SELFTEST)
  47507. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47508. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47509. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47510. #endif
  47511. #endif
  47512. #endif
  47513. #if defined(OPENSSL_EXTRA)
  47514. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47515. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47516. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47517. #endif
  47518. #endif
  47519. #endif
  47520. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47521. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47522. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47523. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47524. #endif
  47525. #endif
  47526. #endif
  47527. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47528. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  47529. {
  47530. EXPECT_DECLS;
  47531. #if defined(OPENSSL_EXTRA)
  47532. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47533. !defined(HAVE_SELFTEST)
  47534. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47535. WOLFSSL_RSA* rsa = NULL;
  47536. #endif
  47537. #endif
  47538. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47539. WOLFSSL_DSA* dsa = NULL;
  47540. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  47541. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47542. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47543. WOLFSSL_EC_KEY* ecKey = NULL;
  47544. #endif
  47545. #endif
  47546. WOLFSSL_EVP_PKEY* pkey = NULL;
  47547. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  47548. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  47549. const char* in = "What is easy to do is easy not to do.";
  47550. size_t inlen = XSTRLEN(in);
  47551. byte hash[SHA256_DIGEST_LENGTH] = {0};
  47552. byte zero[SHA256_DIGEST_LENGTH] = {0};
  47553. SHA256_CTX c;
  47554. byte* sig = NULL;
  47555. byte* sigVerify = NULL;
  47556. size_t siglen;
  47557. size_t siglenOnlyLen;
  47558. size_t keySz = 2048/8; /* Bytes */
  47559. ExpectNotNull(sig =
  47560. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  47561. ExpectNotNull(sigVerify =
  47562. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  47563. siglen = keySz;
  47564. ExpectNotNull(XMEMSET(sig, 0, keySz));
  47565. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  47566. /* Generate hash */
  47567. SHA256_Init(&c);
  47568. SHA256_Update(&c, in, inlen);
  47569. SHA256_Final(hash, &c);
  47570. #ifdef WOLFSSL_SMALL_STACK_CACHE
  47571. /* workaround for small stack cache case */
  47572. wc_Sha256Free((wc_Sha256*)&c);
  47573. #endif
  47574. /* Generate key */
  47575. ExpectNotNull(pkey = EVP_PKEY_new());
  47576. switch (keyType) {
  47577. case EVP_PKEY_RSA:
  47578. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47579. !defined(HAVE_SELFTEST)
  47580. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47581. {
  47582. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47583. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  47584. }
  47585. #endif
  47586. #endif
  47587. break;
  47588. case EVP_PKEY_DSA:
  47589. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47590. ExpectNotNull(dsa = DSA_new());
  47591. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  47592. NULL, 0, NULL, NULL, NULL), 1);
  47593. ExpectIntEQ(DSA_generate_key(dsa), 1);
  47594. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  47595. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  47596. break;
  47597. case EVP_PKEY_EC:
  47598. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47599. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47600. {
  47601. ExpectNotNull(ecKey = EC_KEY_new());
  47602. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  47603. ExpectIntEQ(
  47604. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  47605. if (EXPECT_FAIL()) {
  47606. EC_KEY_free(ecKey);
  47607. }
  47608. }
  47609. #endif
  47610. #endif
  47611. break;
  47612. }
  47613. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  47614. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47615. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47616. !defined(HAVE_SELFTEST)
  47617. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47618. if (keyType == EVP_PKEY_RSA)
  47619. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  47620. WOLFSSL_SUCCESS);
  47621. #endif
  47622. #endif
  47623. /* Check returning only length */
  47624. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  47625. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  47626. ExpectIntGT(siglenOnlyLen, 0);
  47627. /* Sign data */
  47628. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  47629. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  47630. ExpectIntGE(siglenOnlyLen, siglen);
  47631. /* Verify signature */
  47632. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  47633. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47634. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47635. !defined(HAVE_SELFTEST)
  47636. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47637. if (keyType == EVP_PKEY_RSA)
  47638. ExpectIntEQ(
  47639. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  47640. WOLFSSL_SUCCESS);
  47641. #endif
  47642. #endif
  47643. ExpectIntEQ(EVP_PKEY_verify(
  47644. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  47645. WOLFSSL_SUCCESS);
  47646. ExpectIntEQ(EVP_PKEY_verify(
  47647. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  47648. WOLFSSL_FAILURE);
  47649. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47650. !defined(HAVE_SELFTEST)
  47651. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47652. if (keyType == EVP_PKEY_RSA) {
  47653. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  47654. /* Try RSA sign/verify with no padding. */
  47655. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47656. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  47657. WOLFSSL_SUCCESS);
  47658. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  47659. siglen), WOLFSSL_SUCCESS);
  47660. ExpectIntGE(siglenOnlyLen, siglen);
  47661. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47662. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47663. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  47664. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  47665. siglen), WOLFSSL_SUCCESS);
  47666. #endif
  47667. /* Wrong padding schemes. */
  47668. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47669. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  47670. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  47671. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  47672. siglen), WOLFSSL_SUCCESS);
  47673. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47674. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47675. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  47676. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  47677. siglen), WOLFSSL_SUCCESS);
  47678. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  47679. WOLFSSL_SUCCESS);
  47680. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47681. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  47682. }
  47683. #endif
  47684. #endif
  47685. /* error cases */
  47686. siglen = keySz; /* Reset because sig size may vary slightly */
  47687. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  47688. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47689. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  47690. WOLFSSL_SUCCESS);
  47691. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  47692. WOLFSSL_SUCCESS);
  47693. EVP_PKEY_free(pkey);
  47694. pkey = NULL;
  47695. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47696. DSA_free(dsa);
  47697. dsa = NULL;
  47698. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  47699. EVP_PKEY_CTX_free(ctx_verify);
  47700. ctx_verify = NULL;
  47701. EVP_PKEY_CTX_free(ctx);
  47702. ctx = NULL;
  47703. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47704. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47705. #endif /* OPENSSL_EXTRA */
  47706. return EXPECT_RESULT();
  47707. }
  47708. #endif
  47709. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  47710. {
  47711. EXPECT_DECLS;
  47712. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47713. !defined(HAVE_SELFTEST)
  47714. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47715. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  47716. #endif
  47717. #endif
  47718. return EXPECT_RESULT();
  47719. }
  47720. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  47721. {
  47722. EXPECT_DECLS;
  47723. #if defined(OPENSSL_EXTRA)
  47724. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47725. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  47726. #endif
  47727. #endif
  47728. return EXPECT_RESULT();
  47729. }
  47730. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  47731. {
  47732. EXPECT_DECLS;
  47733. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47734. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47735. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  47736. #endif
  47737. #endif
  47738. return EXPECT_RESULT();
  47739. }
  47740. static int test_EVP_PKEY_rsa(void)
  47741. {
  47742. EXPECT_DECLS;
  47743. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47744. WOLFSSL_RSA* rsa = NULL;
  47745. WOLFSSL_EVP_PKEY* pkey = NULL;
  47746. ExpectNotNull(rsa = wolfSSL_RSA_new());
  47747. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  47748. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  47749. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  47750. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  47751. if (EXPECT_FAIL()) {
  47752. wolfSSL_RSA_free(rsa);
  47753. }
  47754. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  47755. wolfSSL_EVP_PKEY_free(pkey);
  47756. #endif
  47757. return EXPECT_RESULT();
  47758. }
  47759. static int test_EVP_PKEY_ec(void)
  47760. {
  47761. EXPECT_DECLS;
  47762. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47763. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47764. WOLFSSL_EC_KEY* ecKey = NULL;
  47765. WOLFSSL_EVP_PKEY* pkey = NULL;
  47766. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  47767. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  47768. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  47769. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  47770. /* Should fail since ecKey is empty */
  47771. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  47772. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  47773. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  47774. if (EXPECT_FAIL()) {
  47775. wolfSSL_EC_KEY_free(ecKey);
  47776. }
  47777. wolfSSL_EVP_PKEY_free(pkey);
  47778. #endif
  47779. #endif
  47780. return EXPECT_RESULT();
  47781. }
  47782. static int test_EVP_PKEY_cmp(void)
  47783. {
  47784. EXPECT_DECLS;
  47785. #if defined(OPENSSL_EXTRA)
  47786. EVP_PKEY *a = NULL;
  47787. EVP_PKEY *b = NULL;
  47788. const unsigned char *in;
  47789. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  47790. in = client_key_der_2048;
  47791. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47792. &in, (long)sizeof_client_key_der_2048));
  47793. in = client_key_der_2048;
  47794. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47795. &in, (long)sizeof_client_key_der_2048));
  47796. /* Test success case RSA */
  47797. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47798. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47799. #else
  47800. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47801. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47802. EVP_PKEY_free(b);
  47803. b = NULL;
  47804. EVP_PKEY_free(a);
  47805. a = NULL;
  47806. #endif
  47807. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47808. in = ecc_clikey_der_256;
  47809. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47810. &in, (long)sizeof_ecc_clikey_der_256));
  47811. in = ecc_clikey_der_256;
  47812. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47813. &in, (long)sizeof_ecc_clikey_der_256));
  47814. /* Test success case ECC */
  47815. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47816. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47817. #else
  47818. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47819. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47820. EVP_PKEY_free(b);
  47821. b = NULL;
  47822. EVP_PKEY_free(a);
  47823. a = NULL;
  47824. #endif
  47825. /* Test failure cases */
  47826. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  47827. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47828. in = client_key_der_2048;
  47829. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47830. &in, (long)sizeof_client_key_der_2048));
  47831. in = ecc_clikey_der_256;
  47832. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47833. &in, (long)sizeof_ecc_clikey_der_256));
  47834. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47835. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  47836. #else
  47837. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47838. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47839. EVP_PKEY_free(b);
  47840. b = NULL;
  47841. EVP_PKEY_free(a);
  47842. a = NULL;
  47843. #endif
  47844. /* invalid or empty failure cases */
  47845. a = EVP_PKEY_new();
  47846. b = EVP_PKEY_new();
  47847. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47848. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  47849. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  47850. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  47851. #ifdef NO_RSA
  47852. /* Type check will fail since RSA is the default EVP key type */
  47853. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  47854. #else
  47855. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47856. #endif
  47857. #else
  47858. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  47859. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  47860. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  47861. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47862. #endif
  47863. EVP_PKEY_free(b);
  47864. EVP_PKEY_free(a);
  47865. (void)in;
  47866. #endif
  47867. return EXPECT_RESULT();
  47868. }
  47869. static int test_ERR_load_crypto_strings(void)
  47870. {
  47871. #if defined(OPENSSL_ALL)
  47872. ERR_load_crypto_strings();
  47873. return TEST_SUCCESS;
  47874. #else
  47875. return TEST_SKIPPED;
  47876. #endif
  47877. }
  47878. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47879. static void free_x509(X509* x)
  47880. {
  47881. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  47882. }
  47883. #endif
  47884. static int test_sk_X509(void)
  47885. {
  47886. EXPECT_DECLS;
  47887. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47888. {
  47889. STACK_OF(X509)* s = NULL;
  47890. ExpectNotNull(s = sk_X509_new_null());
  47891. ExpectIntEQ(sk_X509_num(s), 0);
  47892. sk_X509_pop_free(s, NULL);
  47893. ExpectNotNull(s = sk_X509_new_null());
  47894. ExpectIntEQ(sk_X509_num(s), 0);
  47895. sk_X509_pop_free(s, NULL);
  47896. ExpectNotNull(s = sk_X509_new_null());
  47897. sk_X509_push(s, (X509*)1);
  47898. ExpectIntEQ(sk_X509_num(s), 1);
  47899. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  47900. sk_X509_push(s, (X509*)2);
  47901. ExpectIntEQ(sk_X509_num(s), 2);
  47902. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  47903. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  47904. sk_X509_push(s, (X509*)2);
  47905. sk_X509_pop_free(s, free_x509);
  47906. }
  47907. {
  47908. /* Push a list of 10 X509s onto stack, then verify that
  47909. * value(), push(), shift(), and pop() behave as expected. */
  47910. STACK_OF(X509)* s = NULL;
  47911. X509* xList[10];
  47912. int i = 0;
  47913. const int len = (sizeof(xList) / sizeof(xList[0]));
  47914. for (i = 0; i < len; ++i) {
  47915. xList[i] = NULL;
  47916. ExpectNotNull(xList[i] = X509_new());
  47917. }
  47918. /* test push, pop, and free */
  47919. ExpectNotNull(s = sk_X509_new_null());
  47920. for (i = 0; i < len; ++i) {
  47921. sk_X509_push(s, xList[i]);
  47922. ExpectIntEQ(sk_X509_num(s), i + 1);
  47923. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47924. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47925. }
  47926. /* pop returns and removes last pushed on stack, which is index 0
  47927. * in sk_x509_value */
  47928. for (i = 0; i < len; ++i) {
  47929. X509 * x = sk_X509_value(s, 0);
  47930. X509 * y = sk_X509_pop(s);
  47931. X509 * z = xList[len - 1 - i];
  47932. ExpectIntEQ((x == y), 1);
  47933. ExpectIntEQ((x == z), 1);
  47934. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47935. }
  47936. sk_free(s);
  47937. s = NULL;
  47938. /* test push, shift, and free */
  47939. ExpectNotNull(s = sk_X509_new_null());
  47940. for (i = 0; i < len; ++i) {
  47941. sk_X509_push(s, xList[i]);
  47942. ExpectIntEQ(sk_X509_num(s), i + 1);
  47943. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47944. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47945. }
  47946. /* shift returns and removes first pushed on stack, which is index i
  47947. * in sk_x509_value() */
  47948. for (i = 0; i < len; ++i) {
  47949. X509 * x = sk_X509_value(s, len - 1 - i);
  47950. X509 * y = sk_X509_shift(s);
  47951. X509 * z = xList[i];
  47952. ExpectIntEQ((x == y), 1);
  47953. ExpectIntEQ((x == z), 1);
  47954. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47955. }
  47956. sk_free(s);
  47957. for (i = 0; i < len; ++i)
  47958. X509_free(xList[i]);
  47959. }
  47960. #endif
  47961. return EXPECT_RESULT();
  47962. }
  47963. static int test_sk_X509_CRL(void)
  47964. {
  47965. EXPECT_DECLS;
  47966. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  47967. X509_CRL* crl = NULL;
  47968. XFILE fp = XBADFILE;
  47969. STACK_OF(X509_CRL)* s = NULL;
  47970. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  47971. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47972. NULL, NULL));
  47973. if (fp != XBADFILE)
  47974. XFCLOSE(fp);
  47975. ExpectNotNull(s = sk_X509_CRL_new());
  47976. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  47977. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  47978. if (EXPECT_FAIL()) {
  47979. X509_CRL_free(crl);
  47980. }
  47981. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  47982. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  47983. sk_X509_CRL_free(s);
  47984. #endif
  47985. return EXPECT_RESULT();
  47986. }
  47987. static int test_X509_get_signature_nid(void)
  47988. {
  47989. EXPECT_DECLS;
  47990. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47991. X509* x509 = NULL;
  47992. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  47993. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  47994. SSL_FILETYPE_PEM));
  47995. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  47996. X509_free(x509);
  47997. #endif
  47998. return EXPECT_RESULT();
  47999. }
  48000. static int test_X509_REQ(void)
  48001. {
  48002. EXPECT_DECLS;
  48003. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  48004. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  48005. X509_NAME* name = NULL;
  48006. #ifndef NO_RSA
  48007. X509_NAME* subject = NULL;
  48008. #endif
  48009. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48010. X509_REQ* req = NULL;
  48011. EVP_PKEY* priv = NULL;
  48012. EVP_PKEY* pub = NULL;
  48013. unsigned char* der = NULL;
  48014. int len;
  48015. #endif
  48016. #ifndef NO_RSA
  48017. EVP_MD_CTX *mctx = NULL;
  48018. EVP_PKEY_CTX *pkctx = NULL;
  48019. #ifdef USE_CERT_BUFFERS_1024
  48020. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  48021. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  48022. #elif defined(USE_CERT_BUFFERS_2048)
  48023. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  48024. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  48025. #endif
  48026. #endif
  48027. #ifdef HAVE_ECC
  48028. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  48029. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  48030. #endif
  48031. ExpectNotNull(name = X509_NAME_new());
  48032. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  48033. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  48034. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  48035. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  48036. #ifndef NO_RSA
  48037. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  48038. (long)sizeof_client_key_der_2048));
  48039. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  48040. (long)sizeof_client_keypub_der_2048));
  48041. ExpectNotNull(req = X509_REQ_new());
  48042. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  48043. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  48044. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  48045. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  48046. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  48047. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  48048. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  48049. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  48050. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  48051. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  48052. len = i2d_X509_REQ(req, &der);
  48053. DEBUG_WRITE_DER(der, len, "req.der");
  48054. #ifdef USE_CERT_BUFFERS_1024
  48055. ExpectIntEQ(len, 381);
  48056. #else
  48057. ExpectIntEQ(len, 643);
  48058. #endif
  48059. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  48060. der = NULL;
  48061. mctx = EVP_MD_CTX_new();
  48062. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  48063. WOLFSSL_SUCCESS);
  48064. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  48065. EVP_MD_CTX_free(mctx);
  48066. mctx = NULL;
  48067. X509_REQ_free(NULL);
  48068. X509_REQ_free(req);
  48069. req = NULL;
  48070. /* Test getting the subject from a newly created X509_REQ */
  48071. ExpectNotNull(req = X509_REQ_new());
  48072. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  48073. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  48074. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  48075. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  48076. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  48077. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  48078. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  48079. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  48080. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  48081. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  48082. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  48083. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  48084. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  48085. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  48086. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  48087. len = i2d_X509_REQ(req, &der);
  48088. DEBUG_WRITE_DER(der, len, "req2.der");
  48089. #ifdef USE_CERT_BUFFERS_1024
  48090. ExpectIntEQ(len, 435);
  48091. #else
  48092. ExpectIntEQ(len, 696);
  48093. #endif
  48094. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  48095. der = NULL;
  48096. EVP_PKEY_free(pub);
  48097. pub = NULL;
  48098. EVP_PKEY_free(priv);
  48099. priv = NULL;
  48100. X509_REQ_free(req);
  48101. req = NULL;
  48102. #endif
  48103. #ifdef HAVE_ECC
  48104. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  48105. sizeof_ecc_clikey_der_256));
  48106. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  48107. sizeof_ecc_clikeypub_der_256));
  48108. ExpectNotNull(req = X509_REQ_new());
  48109. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  48110. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  48111. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  48112. /* Signature is random and may be shorter or longer. */
  48113. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  48114. ExpectIntLE(len, 253);
  48115. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  48116. X509_REQ_free(req);
  48117. EVP_PKEY_free(pub);
  48118. EVP_PKEY_free(priv);
  48119. #ifdef FP_ECC
  48120. wc_ecc_fp_free();
  48121. #endif
  48122. #endif /* HAVE_ECC */
  48123. X509_NAME_free(name);
  48124. #endif
  48125. return EXPECT_RESULT();
  48126. }
  48127. static int test_wolfssl_PKCS7(void)
  48128. {
  48129. EXPECT_DECLS;
  48130. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  48131. !defined(NO_RSA)
  48132. PKCS7* pkcs7 = NULL;
  48133. byte data[FOURK_BUF];
  48134. word32 len = sizeof(data);
  48135. const byte* p = data;
  48136. byte content[] = "Test data to encode.";
  48137. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  48138. BIO* bio = NULL;
  48139. byte key[sizeof(client_key_der_2048)];
  48140. word32 keySz = (word32)sizeof(key);
  48141. byte* out = NULL;
  48142. #endif
  48143. ExpectIntGT((len = (word32)CreatePKCS7SignedData(data, (int)len, content,
  48144. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  48145. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, (int)len));
  48146. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  48147. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  48148. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  48149. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  48150. PKCS7_free(pkcs7);
  48151. pkcs7 = NULL;
  48152. /* fail case, without PKCS7_NOVERIFY */
  48153. p = data;
  48154. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  48155. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  48156. 0), WOLFSSL_FAILURE);
  48157. PKCS7_free(pkcs7);
  48158. pkcs7 = NULL;
  48159. /* success case, with PKCS7_NOVERIFY */
  48160. p = data;
  48161. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  48162. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  48163. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  48164. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  48165. /* test i2d */
  48166. XMEMCPY(key, client_key_der_2048, keySz);
  48167. if (pkcs7 != NULL) {
  48168. pkcs7->privateKey = key;
  48169. pkcs7->privateKeySz = (word32)sizeof(key);
  48170. pkcs7->encryptOID = RSAk;
  48171. #ifdef NO_SHA
  48172. pkcs7->hashOID = SHA256h;
  48173. #else
  48174. pkcs7->hashOID = SHAh;
  48175. #endif
  48176. }
  48177. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48178. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  48179. #ifndef NO_ASN_TIME
  48180. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  48181. #else
  48182. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  48183. #endif
  48184. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48185. BIO_free(bio);
  48186. #endif
  48187. PKCS7_free(NULL);
  48188. PKCS7_free(pkcs7);
  48189. #endif
  48190. return EXPECT_RESULT();
  48191. }
  48192. static int test_wolfSSL_PKCS7_sign(void)
  48193. {
  48194. EXPECT_DECLS;
  48195. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  48196. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48197. PKCS7* p7 = NULL;
  48198. PKCS7* p7Ver = NULL;
  48199. byte* out = NULL;
  48200. byte* tmpPtr = NULL;
  48201. int outLen = 0;
  48202. int flags = 0;
  48203. byte data[] = "Test data to encode.";
  48204. const char* cert = "./certs/server-cert.pem";
  48205. const char* key = "./certs/server-key.pem";
  48206. const char* ca = "./certs/ca-cert.pem";
  48207. WOLFSSL_BIO* certBio = NULL;
  48208. WOLFSSL_BIO* keyBio = NULL;
  48209. WOLFSSL_BIO* caBio = NULL;
  48210. WOLFSSL_BIO* inBio = NULL;
  48211. X509* signCert = NULL;
  48212. EVP_PKEY* signKey = NULL;
  48213. X509* caCert = NULL;
  48214. X509_STORE* store = NULL;
  48215. #ifndef NO_PKCS7_STREAM
  48216. int z;
  48217. int ret;
  48218. #endif /* !NO_PKCS7_STREAM */
  48219. /* read signer cert/key into BIO */
  48220. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  48221. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  48222. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  48223. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  48224. /* read CA cert into store (for verify) */
  48225. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  48226. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  48227. ExpectNotNull(store = X509_STORE_new());
  48228. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  48229. /* data to be signed into BIO */
  48230. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48231. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48232. /* PKCS7_sign, bad args: signer NULL */
  48233. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  48234. /* PKCS7_sign, bad args: signer key NULL */
  48235. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  48236. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  48237. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  48238. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  48239. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  48240. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  48241. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  48242. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  48243. {
  48244. flags = PKCS7_BINARY;
  48245. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48246. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  48247. /* verify with d2i_PKCS7 */
  48248. tmpPtr = out;
  48249. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  48250. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48251. PKCS7_free(p7Ver);
  48252. p7Ver = NULL;
  48253. /* verify with wc_PKCS7_VerifySignedData */
  48254. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48255. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  48256. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  48257. #ifndef NO_PKCS7_STREAM
  48258. /* verify with wc_PKCS7_VerifySignedData streaming */
  48259. wc_PKCS7_Free(p7Ver);
  48260. p7Ver = NULL;
  48261. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48262. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  48263. /* test for streaming */
  48264. ret = -1;
  48265. for (z = 0; z < outLen && ret != 0; z++) {
  48266. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  48267. if (ret < 0){
  48268. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  48269. }
  48270. }
  48271. ExpectIntEQ(ret, 0);
  48272. #endif /* !NO_PKCS7_STREAM */
  48273. /* compare the signer found to expected signer */
  48274. ExpectIntNE(p7Ver->verifyCertSz, 0);
  48275. tmpPtr = NULL;
  48276. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  48277. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  48278. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  48279. tmpPtr = NULL;
  48280. wc_PKCS7_Free(p7Ver);
  48281. p7Ver = NULL;
  48282. ExpectNotNull(out);
  48283. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48284. out = NULL;
  48285. PKCS7_free(p7);
  48286. p7 = NULL;
  48287. }
  48288. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  48289. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  48290. {
  48291. /* re-populate input BIO, may have been consumed */
  48292. BIO_free(inBio);
  48293. inBio = NULL;
  48294. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48295. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48296. flags = PKCS7_BINARY | PKCS7_STREAM;
  48297. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48298. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  48299. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  48300. /* PKCS7_final, bad args: PKCS7 null */
  48301. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  48302. /* PKCS7_final, bad args: PKCS7 null */
  48303. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  48304. tmpPtr = out;
  48305. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  48306. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48307. PKCS7_free(p7Ver);
  48308. p7Ver = NULL;
  48309. ExpectNotNull(out);
  48310. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48311. out = NULL;
  48312. PKCS7_free(p7);
  48313. p7 = NULL;
  48314. }
  48315. /* TEST SUCCESS: Detached, not streaming, not MIME */
  48316. {
  48317. /* re-populate input BIO, may have been consumed */
  48318. BIO_free(inBio);
  48319. inBio = NULL;
  48320. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48321. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48322. flags = PKCS7_BINARY | PKCS7_DETACHED;
  48323. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48324. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  48325. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  48326. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48327. if (p7Ver != NULL) {
  48328. p7Ver->content = data;
  48329. p7Ver->contentSz = sizeof(data);
  48330. }
  48331. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  48332. wc_PKCS7_Free(p7Ver);
  48333. p7Ver = NULL;
  48334. #ifndef NO_PKCS7_STREAM
  48335. /* verify with wc_PKCS7_VerifySignedData streaming */
  48336. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48337. if (p7Ver != NULL) {
  48338. p7Ver->content = data;
  48339. p7Ver->contentSz = sizeof(data);
  48340. }
  48341. /* test for streaming */
  48342. ret = -1;
  48343. for (z = 0; z < outLen && ret != 0; z++) {
  48344. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  48345. if (ret < 0){
  48346. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  48347. }
  48348. }
  48349. ExpectIntEQ(ret, 0);
  48350. wc_PKCS7_Free(p7Ver);
  48351. p7Ver = NULL;
  48352. #endif /* !NO_PKCS7_STREAM */
  48353. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  48354. * yet support detached content */
  48355. tmpPtr = out;
  48356. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  48357. PKCS7_free(p7Ver);
  48358. p7Ver = NULL;
  48359. ExpectNotNull(out);
  48360. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48361. out = NULL;
  48362. PKCS7_free(p7);
  48363. p7 = NULL;
  48364. }
  48365. /* TEST SUCCESS: Detached, streaming, not MIME */
  48366. {
  48367. /* re-populate input BIO, may have been consumed */
  48368. BIO_free(inBio);
  48369. inBio = NULL;
  48370. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48371. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48372. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  48373. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48374. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  48375. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  48376. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  48377. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48378. if (p7Ver != NULL) {
  48379. p7Ver->content = data;
  48380. p7Ver->contentSz = sizeof(data);
  48381. }
  48382. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  48383. wc_PKCS7_Free(p7Ver);
  48384. p7Ver = NULL;
  48385. ExpectNotNull(out);
  48386. #ifndef NO_PKCS7_STREAM
  48387. /* verify with wc_PKCS7_VerifySignedData streaming */
  48388. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48389. if (p7Ver != NULL) {
  48390. p7Ver->content = data;
  48391. p7Ver->contentSz = sizeof(data);
  48392. }
  48393. /* test for streaming */
  48394. ret = -1;
  48395. for (z = 0; z < outLen && ret != 0; z++) {
  48396. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  48397. if (ret < 0){
  48398. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  48399. }
  48400. }
  48401. ExpectIntEQ(ret, 0);
  48402. ExpectNotNull(out);
  48403. wc_PKCS7_Free(p7Ver);
  48404. p7Ver = NULL;
  48405. #endif /* !NO_PKCS7_STREAM */
  48406. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48407. PKCS7_free(p7);
  48408. p7 = NULL;
  48409. }
  48410. X509_STORE_free(store);
  48411. X509_free(caCert);
  48412. X509_free(signCert);
  48413. EVP_PKEY_free(signKey);
  48414. BIO_free(inBio);
  48415. BIO_free(keyBio);
  48416. BIO_free(certBio);
  48417. BIO_free(caBio);
  48418. #endif
  48419. return EXPECT_RESULT();
  48420. }
  48421. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  48422. {
  48423. EXPECT_DECLS;
  48424. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  48425. PKCS7_SIGNED* pkcs7 = NULL;
  48426. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  48427. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  48428. PKCS7_SIGNED_free(pkcs7);
  48429. #endif
  48430. return EXPECT_RESULT();
  48431. }
  48432. #ifndef NO_BIO
  48433. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  48434. {
  48435. EXPECT_DECLS;
  48436. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  48437. PKCS7* pkcs7 = NULL;
  48438. BIO* bio = NULL;
  48439. const byte* cert_buf = NULL;
  48440. int ret = 0;
  48441. WC_RNG rng;
  48442. const byte data[] = { /* Hello World */
  48443. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  48444. 0x72,0x6c,0x64
  48445. };
  48446. #ifndef NO_RSA
  48447. #if defined(USE_CERT_BUFFERS_2048)
  48448. byte key[sizeof(client_key_der_2048)];
  48449. byte cert[sizeof(client_cert_der_2048)];
  48450. word32 keySz = (word32)sizeof(key);
  48451. word32 certSz = (word32)sizeof(cert);
  48452. XMEMSET(key, 0, keySz);
  48453. XMEMSET(cert, 0, certSz);
  48454. XMEMCPY(key, client_key_der_2048, keySz);
  48455. XMEMCPY(cert, client_cert_der_2048, certSz);
  48456. #elif defined(USE_CERT_BUFFERS_1024)
  48457. byte key[sizeof_client_key_der_1024];
  48458. byte cert[sizeof(sizeof_client_cert_der_1024)];
  48459. word32 keySz = (word32)sizeof(key);
  48460. word32 certSz = (word32)sizeof(cert);
  48461. XMEMSET(key, 0, keySz);
  48462. XMEMSET(cert, 0, certSz);
  48463. XMEMCPY(key, client_key_der_1024, keySz);
  48464. XMEMCPY(cert, client_cert_der_1024, certSz);
  48465. #else
  48466. unsigned char cert[ONEK_BUF];
  48467. unsigned char key[ONEK_BUF];
  48468. XFILE fp = XBADFILE;
  48469. int certSz;
  48470. int keySz;
  48471. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  48472. XBADFILE);
  48473. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  48474. fp), 0);
  48475. if (fp != XBADFILE) {
  48476. XFCLOSE(fp);
  48477. fp = XBADFILE;
  48478. }
  48479. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  48480. XBADFILE);
  48481. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  48482. 0);
  48483. if (fp != XBADFILE) {
  48484. XFCLOSE(fp);
  48485. fp = XBADFILE;
  48486. }
  48487. #endif
  48488. #elif defined(HAVE_ECC)
  48489. #if defined(USE_CERT_BUFFERS_256)
  48490. unsigned char cert[sizeof(cliecc_cert_der_256)];
  48491. unsigned char key[sizeof(ecc_clikey_der_256)];
  48492. int certSz = (int)sizeof(cert);
  48493. int keySz = (int)sizeof(key);
  48494. XMEMSET(cert, 0, certSz);
  48495. XMEMSET(key, 0, keySz);
  48496. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  48497. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  48498. #else
  48499. unsigned char cert[ONEK_BUF];
  48500. unsigned char key[ONEK_BUF];
  48501. XFILE fp = XBADFILE;
  48502. int certSz, keySz;
  48503. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  48504. XBADFILE);
  48505. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  48506. fp), 0);
  48507. if (fp != XBADFILE) {
  48508. XFCLOSE(fp);
  48509. fp = XBADFILE;
  48510. }
  48511. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  48512. XBADFILE);
  48513. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  48514. 0);
  48515. if (fp != XBADFILE) {
  48516. XFCLOSE(fp);
  48517. fp = XBADFILE;
  48518. }
  48519. #endif
  48520. #else
  48521. #error PKCS7 requires ECC or RSA
  48522. #endif
  48523. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  48524. /* initialize with DER encoded cert */
  48525. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  48526. /* init rng */
  48527. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48528. ExpectIntEQ(wc_InitRng(&rng), 0);
  48529. if (pkcs7 != NULL) {
  48530. pkcs7->rng = &rng;
  48531. pkcs7->content = (byte*)data; /* not used for ex */
  48532. pkcs7->contentSz = (word32)sizeof(data);
  48533. pkcs7->contentOID = SIGNED_DATA;
  48534. pkcs7->privateKey = key;
  48535. pkcs7->privateKeySz = (word32)sizeof(key);
  48536. pkcs7->encryptOID = RSAk;
  48537. #ifdef NO_SHA
  48538. pkcs7->hashOID = SHA256h;
  48539. #else
  48540. pkcs7->hashOID = SHAh;
  48541. #endif
  48542. pkcs7->signedAttribs = NULL;
  48543. pkcs7->signedAttribsSz = 0;
  48544. }
  48545. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48546. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  48547. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  48548. /* Read PKCS#7 PEM from BIO */
  48549. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  48550. ExpectIntGE(ret, 0);
  48551. BIO_free(bio);
  48552. wc_PKCS7_Free(pkcs7);
  48553. wc_FreeRng(&rng);
  48554. #endif
  48555. return EXPECT_RESULT();
  48556. }
  48557. #ifdef HAVE_SMIME
  48558. /* // NOLINTBEGIN(clang-analyzer-unix.Stream) */
  48559. static int test_wolfSSL_SMIME_read_PKCS7(void)
  48560. {
  48561. EXPECT_DECLS;
  48562. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  48563. !defined(NO_RSA)
  48564. PKCS7* pkcs7 = NULL;
  48565. BIO* bio = NULL;
  48566. BIO* bcont = NULL;
  48567. BIO* out = NULL;
  48568. const byte* outBuf = NULL;
  48569. int outBufLen = 0;
  48570. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  48571. XFILE smimeTestFile = XBADFILE;
  48572. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  48573. XBADFILE);
  48574. /* smime-test.p7s */
  48575. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  48576. ExpectNotNull(bio);
  48577. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48578. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48579. ExpectNotNull(pkcs7);
  48580. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48581. PKCS7_NOVERIFY), SSL_SUCCESS);
  48582. if (smimeTestFile != XBADFILE) {
  48583. XFCLOSE(smimeTestFile);
  48584. smimeTestFile = XBADFILE;
  48585. }
  48586. if (bcont) BIO_free(bcont);
  48587. bcont = NULL;
  48588. wolfSSL_PKCS7_free(pkcs7);
  48589. pkcs7 = NULL;
  48590. /* smime-test-multipart.p7s */
  48591. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  48592. ExpectFalse(smimeTestFile == XBADFILE);
  48593. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48594. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48595. ExpectNotNull(pkcs7);
  48596. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48597. PKCS7_NOVERIFY), SSL_SUCCESS);
  48598. if (smimeTestFile != XBADFILE) {
  48599. XFCLOSE(smimeTestFile);
  48600. smimeTestFile = XBADFILE;
  48601. }
  48602. if (bcont) BIO_free(bcont);
  48603. bcont = NULL;
  48604. wolfSSL_PKCS7_free(pkcs7);
  48605. pkcs7 = NULL;
  48606. /* smime-test-multipart-badsig.p7s */
  48607. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  48608. ExpectFalse(smimeTestFile == XBADFILE);
  48609. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48610. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48611. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  48612. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48613. PKCS7_NOVERIFY), SSL_FAILURE);
  48614. if (smimeTestFile != XBADFILE) {
  48615. XFCLOSE(smimeTestFile);
  48616. smimeTestFile = XBADFILE;
  48617. }
  48618. if (bcont) BIO_free(bcont);
  48619. bcont = NULL;
  48620. wolfSSL_PKCS7_free(pkcs7);
  48621. pkcs7 = NULL;
  48622. /* smime-test-canon.p7s */
  48623. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  48624. ExpectFalse(smimeTestFile == XBADFILE);
  48625. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48626. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48627. ExpectNotNull(pkcs7);
  48628. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48629. PKCS7_NOVERIFY), SSL_SUCCESS);
  48630. if (smimeTestFile != XBADFILE) {
  48631. XFCLOSE(smimeTestFile);
  48632. smimeTestFile = XBADFILE;
  48633. }
  48634. if (bcont) BIO_free(bcont);
  48635. bcont = NULL;
  48636. wolfSSL_PKCS7_free(pkcs7);
  48637. pkcs7 = NULL;
  48638. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  48639. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  48640. ExpectFalse(smimeTestFile == XBADFILE);
  48641. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48642. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48643. ExpectNotNull(pkcs7);
  48644. out = wolfSSL_BIO_new(BIO_s_mem());
  48645. ExpectNotNull(out);
  48646. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  48647. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  48648. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  48649. /* Content-Type should not show up at beginning of output buffer */
  48650. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  48651. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  48652. BIO_free(out);
  48653. BIO_free(bio);
  48654. if (bcont) BIO_free(bcont);
  48655. wolfSSL_PKCS7_free(pkcs7);
  48656. #endif
  48657. return EXPECT_RESULT();
  48658. }
  48659. /* // NOLINTEND(clang-analyzer-unix.Stream) */
  48660. static int test_wolfSSL_SMIME_write_PKCS7(void)
  48661. {
  48662. EXPECT_DECLS;
  48663. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  48664. PKCS7* p7 = NULL;
  48665. PKCS7* p7Ver = NULL;
  48666. int flags = 0;
  48667. byte data[] = "Test data to encode.";
  48668. const char* cert = "./certs/server-cert.pem";
  48669. const char* key = "./certs/server-key.pem";
  48670. const char* ca = "./certs/ca-cert.pem";
  48671. WOLFSSL_BIO* certBio = NULL;
  48672. WOLFSSL_BIO* keyBio = NULL;
  48673. WOLFSSL_BIO* caBio = NULL;
  48674. WOLFSSL_BIO* inBio = NULL;
  48675. WOLFSSL_BIO* outBio = NULL;
  48676. WOLFSSL_BIO* content = NULL;
  48677. X509* signCert = NULL;
  48678. EVP_PKEY* signKey = NULL;
  48679. X509* caCert = NULL;
  48680. X509_STORE* store = NULL;
  48681. /* read signer cert/key into BIO */
  48682. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  48683. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  48684. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  48685. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  48686. /* read CA cert into store (for verify) */
  48687. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  48688. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  48689. ExpectNotNull(store = X509_STORE_new());
  48690. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  48691. /* generate and verify SMIME: not detached */
  48692. {
  48693. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48694. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48695. flags = PKCS7_STREAM;
  48696. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48697. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48698. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48699. /* bad arg: out NULL */
  48700. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  48701. /* bad arg: pkcs7 NULL */
  48702. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  48703. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48704. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48705. BIO_free(content);
  48706. content = NULL;
  48707. BIO_free(inBio);
  48708. inBio = NULL;
  48709. BIO_free(outBio);
  48710. outBio = NULL;
  48711. PKCS7_free(p7Ver);
  48712. p7Ver = NULL;
  48713. PKCS7_free(p7);
  48714. p7 = NULL;
  48715. }
  48716. /* generate and verify SMIME: not detached, add Content-Type */
  48717. {
  48718. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48719. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48720. flags = PKCS7_STREAM | PKCS7_TEXT;
  48721. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48722. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48723. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48724. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48725. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48726. BIO_free(content);
  48727. content = NULL;
  48728. BIO_free(inBio);
  48729. inBio = NULL;
  48730. BIO_free(outBio);
  48731. outBio = NULL;
  48732. PKCS7_free(p7Ver);
  48733. p7Ver = NULL;
  48734. PKCS7_free(p7);
  48735. p7 = NULL;
  48736. }
  48737. /* generate and verify SMIME: detached */
  48738. {
  48739. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48740. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48741. flags = PKCS7_DETACHED | PKCS7_STREAM;
  48742. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48743. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48744. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48745. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48746. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  48747. BIO_free(content);
  48748. content = NULL;
  48749. BIO_free(inBio);
  48750. inBio = NULL;
  48751. BIO_free(outBio);
  48752. outBio = NULL;
  48753. PKCS7_free(p7Ver);
  48754. p7Ver = NULL;
  48755. PKCS7_free(p7);
  48756. p7 = NULL;
  48757. }
  48758. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  48759. {
  48760. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48761. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48762. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  48763. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48764. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48765. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48766. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48767. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  48768. BIO_free(content);
  48769. content = NULL;
  48770. BIO_free(inBio);
  48771. inBio = NULL;
  48772. BIO_free(outBio);
  48773. outBio = NULL;
  48774. PKCS7_free(p7Ver);
  48775. p7Ver = NULL;
  48776. PKCS7_free(p7);
  48777. p7 = NULL;
  48778. }
  48779. X509_STORE_free(store);
  48780. X509_free(caCert);
  48781. X509_free(signCert);
  48782. EVP_PKEY_free(signKey);
  48783. BIO_free(keyBio);
  48784. BIO_free(certBio);
  48785. BIO_free(caBio);
  48786. #endif
  48787. return EXPECT_RESULT();
  48788. }
  48789. #endif /* HAVE_SMIME */
  48790. #endif /* !NO_BIO */
  48791. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  48792. * returns 0) */
  48793. static int test_X509_STORE_No_SSL_CTX(void)
  48794. {
  48795. EXPECT_DECLS;
  48796. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48797. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48798. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48799. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48800. defined(HAVE_CRL) && !defined(NO_RSA)
  48801. X509_STORE * store = NULL;
  48802. X509_STORE_CTX * storeCtx = NULL;
  48803. X509_CRL * crl = NULL;
  48804. X509 * ca = NULL;
  48805. X509 * cert = NULL;
  48806. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48807. const char srvCert[] = "./certs/server-cert.pem";
  48808. const char caCert[] = "./certs/ca-cert.pem";
  48809. const char caDir[] = "./certs/crl/hash_pem";
  48810. XFILE fp = XBADFILE;
  48811. X509_LOOKUP * lookup = NULL;
  48812. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48813. /* Set up store with CA */
  48814. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48815. SSL_FILETYPE_PEM)));
  48816. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48817. /* Add CRL lookup directory to store
  48818. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  48819. * of crl.pem */
  48820. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48821. X509_LOOKUP_hash_dir())));
  48822. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  48823. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  48824. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48825. SSL_SUCCESS);
  48826. /* Add CRL to store NOT containing the verified certificate, which
  48827. * forces use of the CRL lookup directory */
  48828. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48829. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48830. NULL, NULL));
  48831. if (fp != XBADFILE)
  48832. XFCLOSE(fp);
  48833. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48834. /* Create verification context outside of an SSL session */
  48835. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48836. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48837. SSL_FILETYPE_PEM)));
  48838. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48839. /* Perform verification, which should NOT indicate CRL missing due to the
  48840. * store CM's X509 store pointer being NULL */
  48841. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48842. X509_CRL_free(crl);
  48843. X509_STORE_free(store);
  48844. X509_STORE_CTX_free(storeCtx);
  48845. X509_free(cert);
  48846. X509_free(ca);
  48847. #endif
  48848. return EXPECT_RESULT();
  48849. }
  48850. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  48851. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  48852. static int test_X509_LOOKUP_add_dir(void)
  48853. {
  48854. EXPECT_DECLS;
  48855. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48856. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48857. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48858. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48859. defined(HAVE_CRL) && !defined(NO_RSA)
  48860. X509_STORE * store = NULL;
  48861. X509_STORE_CTX * storeCtx = NULL;
  48862. X509_CRL * crl = NULL;
  48863. X509 * ca = NULL;
  48864. X509 * cert = NULL;
  48865. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48866. const char srvCert[] = "./certs/server-cert.pem";
  48867. const char caCert[] = "./certs/ca-cert.pem";
  48868. const char caDir[] = "./certs/crl/hash_der";
  48869. XFILE fp = XBADFILE;
  48870. X509_LOOKUP * lookup = NULL;
  48871. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48872. /* Set up store with CA */
  48873. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48874. SSL_FILETYPE_PEM)));
  48875. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48876. /* Add CRL lookup directory to store.
  48877. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  48878. * of crl.der */
  48879. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48880. X509_LOOKUP_hash_dir())));
  48881. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  48882. SSL_SUCCESS);
  48883. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48884. SSL_SUCCESS);
  48885. /* Add CRL to store NOT containing the verified certificate, which
  48886. * forces use of the CRL lookup directory */
  48887. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48888. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48889. NULL, NULL));
  48890. if (fp != XBADFILE) {
  48891. XFCLOSE(fp);
  48892. fp = XBADFILE;
  48893. }
  48894. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48895. /* Create verification context outside of an SSL session */
  48896. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48897. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48898. SSL_FILETYPE_PEM)));
  48899. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48900. /* Perform verification, which should NOT return CRL missing */
  48901. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48902. X509_CRL_free(crl);
  48903. crl = NULL;
  48904. X509_STORE_free(store);
  48905. store = NULL;
  48906. X509_STORE_CTX_free(storeCtx);
  48907. storeCtx = NULL;
  48908. X509_free(cert);
  48909. cert = NULL;
  48910. X509_free(ca);
  48911. ca = NULL;
  48912. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  48913. * We should get CRL_MISSING at the end, because the lookup
  48914. * dir has only ASN1 CRLs. */
  48915. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48916. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48917. SSL_FILETYPE_PEM)));
  48918. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48919. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48920. X509_LOOKUP_hash_dir())));
  48921. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  48922. SSL_SUCCESS);
  48923. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48924. SSL_SUCCESS);
  48925. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48926. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48927. NULL, NULL));
  48928. if (fp != XBADFILE) {
  48929. XFCLOSE(fp);
  48930. fp = XBADFILE;
  48931. }
  48932. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48933. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48934. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48935. SSL_FILETYPE_PEM)));
  48936. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48937. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  48938. * in dir containing only ASN1/DER. */
  48939. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  48940. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  48941. X509_V_ERR_UNABLE_TO_GET_CRL);
  48942. X509_CRL_free(crl);
  48943. X509_STORE_free(store);
  48944. X509_STORE_CTX_free(storeCtx);
  48945. X509_free(cert);
  48946. X509_free(ca);
  48947. #endif
  48948. return EXPECT_RESULT();
  48949. }
  48950. /*----------------------------------------------------------------------------*
  48951. | Certificate Failure Checks
  48952. *----------------------------------------------------------------------------*/
  48953. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48954. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  48955. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48956. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  48957. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  48958. int type)
  48959. {
  48960. int ret;
  48961. WOLFSSL_CERT_MANAGER* cm = NULL;
  48962. switch (type) {
  48963. case TESTING_RSA:
  48964. #ifdef NO_RSA
  48965. fprintf(stderr, "RSA disabled, skipping test\n");
  48966. return ASN_SIG_CONFIRM_E;
  48967. #else
  48968. break;
  48969. #endif
  48970. case TESTING_ECC:
  48971. #ifndef HAVE_ECC
  48972. fprintf(stderr, "ECC disabled, skipping test\n");
  48973. return ASN_SIG_CONFIRM_E;
  48974. #else
  48975. break;
  48976. #endif
  48977. default:
  48978. fprintf(stderr, "Bad function argument\n");
  48979. return BAD_FUNC_ARG;
  48980. }
  48981. cm = wolfSSL_CertManagerNew();
  48982. if (cm == NULL) {
  48983. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  48984. return -1;
  48985. }
  48986. #ifndef NO_FILESYSTEM
  48987. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  48988. if (ret != WOLFSSL_SUCCESS) {
  48989. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  48990. wolfSSL_CertManagerFree(cm);
  48991. return ret;
  48992. }
  48993. #else
  48994. (void)ca;
  48995. #endif
  48996. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, (long int)cert_sz,
  48997. WOLFSSL_FILETYPE_ASN1);
  48998. /* Let ExpectIntEQ handle return code */
  48999. wolfSSL_CertManagerFree(cm);
  49000. return ret;
  49001. }
  49002. #endif
  49003. #if !defined(NO_FILESYSTEM)
  49004. static int test_RsaSigFailure_cm(void)
  49005. {
  49006. EXPECT_DECLS;
  49007. #ifndef NO_RSA
  49008. const char* ca_cert = "./certs/ca-cert.pem";
  49009. const char* server_cert = "./certs/server-cert.der";
  49010. byte* cert_buf = NULL;
  49011. size_t cert_sz = 0;
  49012. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  49013. if (cert_buf != NULL) {
  49014. /* corrupt DER - invert last byte, which is signature */
  49015. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  49016. /* test bad cert */
  49017. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49018. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  49019. WOLFSSL_FATAL_ERROR);
  49020. #else
  49021. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  49022. ASN_SIG_CONFIRM_E);
  49023. #endif
  49024. }
  49025. /* load_file() uses malloc. */
  49026. if (cert_buf != NULL) {
  49027. free(cert_buf);
  49028. }
  49029. #endif /* !NO_RSA */
  49030. return EXPECT_RESULT();
  49031. }
  49032. static int test_EccSigFailure_cm(void)
  49033. {
  49034. EXPECT_DECLS;
  49035. #ifdef HAVE_ECC
  49036. /* self-signed ECC cert, so use server cert as CA */
  49037. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  49038. const char* server_cert = "./certs/server-ecc.der";
  49039. byte* cert_buf = NULL;
  49040. size_t cert_sz = 0;
  49041. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  49042. if (cert_buf != NULL) {
  49043. /* corrupt DER - invert last byte, which is signature */
  49044. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  49045. /* test bad cert */
  49046. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49047. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  49048. WOLFSSL_FATAL_ERROR);
  49049. #else
  49050. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  49051. ASN_SIG_CONFIRM_E);
  49052. #endif
  49053. }
  49054. /* load_file() uses malloc. */
  49055. if (cert_buf != NULL) {
  49056. free(cert_buf);
  49057. }
  49058. #ifdef FP_ECC
  49059. wc_ecc_fp_free();
  49060. #endif
  49061. #endif /* HAVE_ECC */
  49062. return EXPECT_RESULT();
  49063. }
  49064. #endif /* !NO_FILESYSTEM */
  49065. #endif /* NO_CERTS */
  49066. #ifdef WOLFSSL_TLS13
  49067. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  49068. #ifdef WC_SHA384_DIGEST_SIZE
  49069. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  49070. #else
  49071. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  49072. #endif
  49073. #endif
  49074. #ifdef WOLFSSL_EARLY_DATA
  49075. static const char earlyData[] = "Early Data";
  49076. static char earlyDataBuffer[1];
  49077. #endif
  49078. static int test_tls13_apis(void)
  49079. {
  49080. EXPECT_DECLS;
  49081. int ret;
  49082. #ifndef WOLFSSL_NO_TLS12
  49083. #ifndef NO_WOLFSSL_CLIENT
  49084. WOLFSSL_CTX* clientTls12Ctx = NULL;
  49085. WOLFSSL* clientTls12Ssl = NULL;
  49086. #endif
  49087. #ifndef NO_WOLFSSL_SERVER
  49088. WOLFSSL_CTX* serverTls12Ctx = NULL;
  49089. WOLFSSL* serverTls12Ssl = NULL;
  49090. #endif
  49091. #endif
  49092. #ifndef NO_WOLFSSL_CLIENT
  49093. WOLFSSL_CTX* clientCtx = NULL;
  49094. WOLFSSL* clientSsl = NULL;
  49095. #endif
  49096. #ifndef NO_WOLFSSL_SERVER
  49097. WOLFSSL_CTX* serverCtx = NULL;
  49098. WOLFSSL* serverSsl = NULL;
  49099. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  49100. const char* ourCert = svrCertFile;
  49101. const char* ourKey = svrKeyFile;
  49102. #endif
  49103. #endif
  49104. int required;
  49105. #ifdef WOLFSSL_EARLY_DATA
  49106. int outSz;
  49107. #endif
  49108. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  49109. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  49110. #ifdef HAVE_PQC
  49111. WOLFSSL_KYBER_LEVEL1
  49112. #else
  49113. WOLFSSL_ECC_SECP256R1
  49114. #endif
  49115. };
  49116. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  49117. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  49118. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  49119. int numGroups = 2;
  49120. #endif
  49121. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49122. char groupList[] =
  49123. #ifdef HAVE_CURVE25519
  49124. "X25519:"
  49125. #endif
  49126. #ifdef HAVE_CURVE448
  49127. "X448:"
  49128. #endif
  49129. #ifndef NO_ECC_SECP
  49130. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  49131. "P-521:secp521r1:"
  49132. #endif
  49133. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  49134. "P-384:secp384r1:"
  49135. #endif
  49136. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  49137. "P-256:secp256r1"
  49138. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  49139. ":P256_KYBER_LEVEL1"
  49140. #endif
  49141. #endif
  49142. #endif /* !defined(NO_ECC_SECP) */
  49143. #ifdef HAVE_PQC
  49144. ":KYBER_LEVEL1"
  49145. #endif
  49146. "";
  49147. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  49148. (void)ret;
  49149. #ifndef WOLFSSL_NO_TLS12
  49150. #ifndef NO_WOLFSSL_CLIENT
  49151. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  49152. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  49153. #endif
  49154. #ifndef NO_WOLFSSL_SERVER
  49155. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  49156. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  49157. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  49158. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  49159. WOLFSSL_FILETYPE_PEM);
  49160. #endif
  49161. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  49162. #endif
  49163. #endif
  49164. #ifndef NO_WOLFSSL_CLIENT
  49165. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  49166. clientSsl = wolfSSL_new(clientCtx);
  49167. #endif
  49168. #ifndef NO_WOLFSSL_SERVER
  49169. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  49170. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  49171. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  49172. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  49173. #endif
  49174. serverSsl = wolfSSL_new(serverCtx);
  49175. ExpectNotNull(serverSsl);
  49176. #endif
  49177. #ifdef WOLFSSL_SEND_HRR_COOKIE
  49178. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  49179. #ifndef NO_WOLFSSL_CLIENT
  49180. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  49181. #endif
  49182. #ifndef NO_WOLFSSL_SERVER
  49183. #ifndef WOLFSSL_NO_TLS12
  49184. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  49185. BAD_FUNC_ARG);
  49186. #endif
  49187. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  49188. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  49189. WOLFSSL_SUCCESS);
  49190. #endif
  49191. #endif
  49192. #ifdef HAVE_SUPPORTED_CURVES
  49193. #ifdef HAVE_ECC
  49194. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  49195. BAD_FUNC_ARG);
  49196. #ifndef NO_WOLFSSL_SERVER
  49197. do {
  49198. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  49199. #ifdef WOLFSSL_ASYNC_CRYPT
  49200. if (ret == WC_PENDING_E)
  49201. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  49202. #endif
  49203. }
  49204. while (ret == WC_PENDING_E);
  49205. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49206. #endif
  49207. #ifndef NO_WOLFSSL_CLIENT
  49208. #ifndef WOLFSSL_NO_TLS12
  49209. do {
  49210. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  49211. #ifdef WOLFSSL_ASYNC_CRYPT
  49212. if (ret == WC_PENDING_E)
  49213. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  49214. #endif
  49215. }
  49216. while (ret == WC_PENDING_E);
  49217. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49218. #endif
  49219. do {
  49220. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  49221. #ifdef WOLFSSL_ASYNC_CRYPT
  49222. if (ret == WC_PENDING_E)
  49223. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  49224. #endif
  49225. }
  49226. while (ret == WC_PENDING_E);
  49227. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49228. #endif
  49229. #elif defined(HAVE_CURVE25519)
  49230. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  49231. #ifndef NO_WOLFSSL_SERVER
  49232. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  49233. WOLFSSL_SUCCESS);
  49234. #endif
  49235. #ifndef NO_WOLFSSL_CLIENT
  49236. #ifndef WOLFSSL_NO_TLS12
  49237. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  49238. WOLFSSL_SUCCESS);
  49239. #endif
  49240. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  49241. WOLFSSL_SUCCESS);
  49242. #endif
  49243. #elif defined(HAVE_CURVE448)
  49244. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  49245. #ifndef NO_WOLFSSL_SERVER
  49246. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  49247. WOLFSSL_SUCCESS);
  49248. #endif
  49249. #ifndef NO_WOLFSSL_CLIENT
  49250. #ifndef WOLFSSL_NO_TLS12
  49251. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  49252. WOLFSSL_SUCCESS);
  49253. #endif
  49254. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  49255. WOLFSSL_SUCCESS);
  49256. #endif
  49257. #else
  49258. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  49259. BAD_FUNC_ARG);
  49260. #ifndef NO_WOLFSSL_CLIENT
  49261. #ifndef WOLFSSL_NO_TLS12
  49262. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  49263. NOT_COMPILED_IN);
  49264. #endif
  49265. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  49266. NOT_COMPILED_IN);
  49267. #endif
  49268. #endif
  49269. #if defined(HAVE_PQC)
  49270. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  49271. #ifndef NO_WOLFSSL_SERVER
  49272. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  49273. WOLFSSL_SUCCESS);
  49274. #endif
  49275. #ifndef NO_WOLFSSL_CLIENT
  49276. #ifndef WOLFSSL_NO_TLS12
  49277. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  49278. BAD_FUNC_ARG);
  49279. #endif
  49280. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  49281. WOLFSSL_SUCCESS);
  49282. #endif
  49283. #endif
  49284. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  49285. #ifndef NO_WOLFSSL_SERVER
  49286. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  49287. #endif
  49288. #ifndef NO_WOLFSSL_CLIENT
  49289. #ifndef WOLFSSL_NO_TLS12
  49290. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  49291. #endif
  49292. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  49293. #endif
  49294. #endif /* HAVE_SUPPORTED_CURVES */
  49295. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  49296. #ifndef NO_WOLFSSL_CLIENT
  49297. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  49298. #endif
  49299. #ifndef NO_WOLFSSL_SERVER
  49300. #ifndef WOLFSSL_NO_TLS12
  49301. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  49302. #endif
  49303. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  49304. #endif
  49305. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  49306. #ifndef NO_WOLFSSL_CLIENT
  49307. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  49308. #endif
  49309. #ifndef NO_WOLFSSL_SERVER
  49310. #ifndef WOLFSSL_NO_TLS12
  49311. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  49312. #endif
  49313. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  49314. #endif
  49315. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  49316. #ifndef NO_WOLFSSL_CLIENT
  49317. #ifndef WOLFSSL_NO_TLS12
  49318. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  49319. #endif
  49320. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  49321. #endif
  49322. #ifndef NO_WOLFSSL_SERVER
  49323. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  49324. #endif
  49325. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  49326. #ifndef NO_WOLFSSL_CLIENT
  49327. #ifndef WOLFSSL_NO_TLS12
  49328. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  49329. #endif
  49330. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  49331. #endif
  49332. #ifndef NO_WOLFSSL_SERVER
  49333. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  49334. #endif
  49335. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  49336. #ifndef NO_WOLFSSL_CLIENT
  49337. #ifndef WOLFSSL_NO_TLS12
  49338. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  49339. #endif
  49340. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  49341. #endif
  49342. #ifndef NO_WOLFSSL_SERVER
  49343. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  49344. #endif
  49345. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  49346. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  49347. #ifndef NO_WOLFSSL_CLIENT
  49348. #ifndef WOLFSSL_NO_TLS12
  49349. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  49350. BAD_FUNC_ARG);
  49351. #endif
  49352. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  49353. #endif
  49354. #ifndef NO_WOLFSSL_SERVER
  49355. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  49356. #endif
  49357. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  49358. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  49359. #ifndef NO_WOLFSSL_SERVER
  49360. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  49361. #endif
  49362. #ifndef NO_WOLFSSL_CLIENT
  49363. #ifndef WOLFSSL_NO_TLS12
  49364. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  49365. BAD_FUNC_ARG);
  49366. #endif
  49367. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  49368. #endif
  49369. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  49370. #ifndef NO_WOLFSSL_SERVER
  49371. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  49372. #endif
  49373. #ifndef NO_WOLFSSL_CLIENT
  49374. #ifndef WOLFSSL_NO_TLS12
  49375. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  49376. BAD_FUNC_ARG);
  49377. #endif
  49378. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  49379. #endif
  49380. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  49381. #ifndef NO_WOLFSSL_CLIENT
  49382. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  49383. #endif
  49384. #ifndef NO_WOLFSSL_SERVER
  49385. #ifndef WOLFSSL_NO_TLS12
  49386. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  49387. BAD_FUNC_ARG);
  49388. #endif
  49389. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  49390. #endif
  49391. #endif
  49392. #ifdef HAVE_ECC
  49393. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  49394. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  49395. #ifndef NO_WOLFSSL_SERVER
  49396. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  49397. #endif
  49398. #ifndef NO_WOLFSSL_CLIENT
  49399. #ifndef WOLFSSL_NO_TLS12
  49400. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  49401. #endif
  49402. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  49403. #endif
  49404. #endif
  49405. #ifdef HAVE_SUPPORTED_CURVES
  49406. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  49407. #ifndef NO_WOLFSSL_CLIENT
  49408. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  49409. #endif
  49410. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  49411. #ifndef NO_WOLFSSL_CLIENT
  49412. #ifndef WOLFSSL_NO_TLS12
  49413. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  49414. WOLFSSL_SUCCESS);
  49415. #endif
  49416. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  49417. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  49418. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  49419. WOLFSSL_SUCCESS);
  49420. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  49421. BAD_FUNC_ARG);
  49422. #endif
  49423. #ifndef NO_WOLFSSL_SERVER
  49424. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  49425. WOLFSSL_SUCCESS);
  49426. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  49427. BAD_FUNC_ARG);
  49428. #endif
  49429. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  49430. #ifndef NO_WOLFSSL_CLIENT
  49431. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  49432. #endif
  49433. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  49434. #ifndef NO_WOLFSSL_CLIENT
  49435. #ifndef WOLFSSL_NO_TLS12
  49436. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  49437. WOLFSSL_SUCCESS);
  49438. #endif
  49439. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  49440. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  49441. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  49442. WOLFSSL_SUCCESS);
  49443. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  49444. BAD_FUNC_ARG);
  49445. #endif
  49446. #ifndef NO_WOLFSSL_SERVER
  49447. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  49448. WOLFSSL_SUCCESS);
  49449. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  49450. BAD_FUNC_ARG);
  49451. #endif
  49452. #ifdef OPENSSL_EXTRA
  49453. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  49454. #ifndef NO_WOLFSSL_CLIENT
  49455. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  49456. WOLFSSL_FAILURE);
  49457. #endif
  49458. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  49459. WOLFSSL_FAILURE);
  49460. #ifndef NO_WOLFSSL_CLIENT
  49461. #ifndef WOLFSSL_NO_TLS12
  49462. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  49463. WOLFSSL_SUCCESS);
  49464. #endif
  49465. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  49466. WOLFSSL_SUCCESS);
  49467. #endif
  49468. #ifndef NO_WOLFSSL_SERVER
  49469. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  49470. WOLFSSL_SUCCESS);
  49471. #endif
  49472. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  49473. #ifndef NO_WOLFSSL_CLIENT
  49474. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  49475. #endif
  49476. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  49477. #ifndef NO_WOLFSSL_CLIENT
  49478. #ifndef WOLFSSL_NO_TLS12
  49479. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  49480. WOLFSSL_SUCCESS);
  49481. #endif
  49482. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  49483. WOLFSSL_SUCCESS);
  49484. #endif
  49485. #ifndef NO_WOLFSSL_SERVER
  49486. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  49487. WOLFSSL_SUCCESS);
  49488. #endif
  49489. #endif /* OPENSSL_EXTRA */
  49490. #endif /* HAVE_SUPPORTED_CURVES */
  49491. #endif /* HAVE_ECC */
  49492. #ifdef WOLFSSL_EARLY_DATA
  49493. #ifndef OPENSSL_EXTRA
  49494. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  49495. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  49496. #else
  49497. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  49498. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  49499. #endif
  49500. #ifndef NO_WOLFSSL_CLIENT
  49501. #ifndef OPENSSL_EXTRA
  49502. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  49503. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  49504. #else
  49505. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  49506. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  49507. #endif
  49508. #endif
  49509. #ifndef NO_WOLFSSL_SERVER
  49510. #ifndef WOLFSSL_NO_TLS12
  49511. #ifndef OPENSSL_EXTRA
  49512. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  49513. BAD_FUNC_ARG);
  49514. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  49515. #else
  49516. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  49517. BAD_FUNC_ARG);
  49518. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  49519. #endif
  49520. #endif
  49521. #ifndef OPENSSL_EXTRA
  49522. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  49523. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  49524. WOLFSSL_SUCCESS);
  49525. #else
  49526. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  49527. #endif
  49528. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  49529. #else
  49530. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  49531. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  49532. #endif
  49533. #endif
  49534. #ifndef OPENSSL_EXTRA
  49535. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  49536. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  49537. #else
  49538. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  49539. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  49540. #endif
  49541. #ifndef NO_WOLFSSL_CLIENT
  49542. #ifndef OPENSSL_EXTRA
  49543. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  49544. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  49545. #else
  49546. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  49547. #endif
  49548. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  49549. #else
  49550. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  49551. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  49552. #endif
  49553. #endif
  49554. #ifndef NO_WOLFSSL_SERVER
  49555. #ifndef WOLFSSL_NO_TLS12
  49556. #ifndef OPENSSL_EXTRA
  49557. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  49558. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  49559. #else
  49560. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  49561. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  49562. #endif
  49563. #endif
  49564. #ifndef OPENSSL_EXTRA
  49565. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  49566. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  49567. #else
  49568. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  49569. #endif
  49570. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  49571. #else
  49572. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  49573. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  49574. #endif
  49575. #endif
  49576. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  49577. &outSz), BAD_FUNC_ARG);
  49578. #ifndef NO_WOLFSSL_CLIENT
  49579. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  49580. &outSz), BAD_FUNC_ARG);
  49581. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  49582. BAD_FUNC_ARG);
  49583. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  49584. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  49585. #endif
  49586. #ifndef NO_WOLFSSL_SERVER
  49587. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  49588. sizeof(earlyData), &outSz), SIDE_ERROR);
  49589. #endif
  49590. #ifndef NO_WOLFSSL_CLIENT
  49591. #ifndef WOLFSSL_NO_TLS12
  49592. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  49593. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  49594. #endif
  49595. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  49596. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  49597. #endif
  49598. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  49599. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49600. #ifndef NO_WOLFSSL_SERVER
  49601. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  49602. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49603. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  49604. &outSz), BAD_FUNC_ARG);
  49605. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  49606. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  49607. #endif
  49608. #ifndef NO_WOLFSSL_CLIENT
  49609. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  49610. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  49611. #endif
  49612. #ifndef NO_WOLFSSL_SERVER
  49613. #ifndef WOLFSSL_NO_TLS12
  49614. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  49615. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49616. #endif
  49617. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  49618. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  49619. #endif
  49620. #endif
  49621. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  49622. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  49623. #endif
  49624. #ifndef NO_WOLFSSL_SERVER
  49625. wolfSSL_free(serverSsl);
  49626. wolfSSL_CTX_free(serverCtx);
  49627. #endif
  49628. #ifndef NO_WOLFSSL_CLIENT
  49629. wolfSSL_free(clientSsl);
  49630. wolfSSL_CTX_free(clientCtx);
  49631. #endif
  49632. #ifndef WOLFSSL_NO_TLS12
  49633. #ifndef NO_WOLFSSL_SERVER
  49634. wolfSSL_free(serverTls12Ssl);
  49635. wolfSSL_CTX_free(serverTls12Ctx);
  49636. #endif
  49637. #ifndef NO_WOLFSSL_CLIENT
  49638. wolfSSL_free(clientTls12Ssl);
  49639. wolfSSL_CTX_free(clientTls12Ctx);
  49640. #endif
  49641. #endif
  49642. return EXPECT_RESULT();
  49643. }
  49644. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  49645. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  49646. defined(BUILD_TLS_AES_256_GCM_SHA384)
  49647. /* Called when writing. */
  49648. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  49649. {
  49650. (void)ssl;
  49651. (void)buf;
  49652. (void)sz;
  49653. (void)ctx;
  49654. /* Force error return from wolfSSL_accept_TLSv13(). */
  49655. return WANT_WRITE;
  49656. }
  49657. /* Called when reading. */
  49658. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  49659. {
  49660. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  49661. int len = (int)msg->length;
  49662. (void)ssl;
  49663. (void)sz;
  49664. /* Pass back as much of message as will fit in buffer. */
  49665. if (len > sz)
  49666. len = sz;
  49667. XMEMCPY(buf, msg->buffer, len);
  49668. /* Move over returned data. */
  49669. msg->buffer += len;
  49670. msg->length -= len;
  49671. /* Amount actually copied. */
  49672. return len;
  49673. }
  49674. #endif
  49675. static int test_tls13_cipher_suites(void)
  49676. {
  49677. EXPECT_DECLS;
  49678. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  49679. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  49680. defined(BUILD_TLS_AES_256_GCM_SHA384)
  49681. WOLFSSL_CTX* ctx = NULL;
  49682. WOLFSSL *ssl = NULL;
  49683. int i;
  49684. byte clientHello[] = {
  49685. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  49686. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  49687. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  49688. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  49689. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  49690. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  49691. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  49692. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  49693. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  49695. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  49696. 0x13, 0x01,
  49697. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  49698. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  49699. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  49700. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  49701. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  49702. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  49703. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  49704. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  49705. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  49706. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  49707. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  49708. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  49709. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  49710. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  49711. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  49712. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  49713. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  49714. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  49715. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  49716. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  49717. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  49718. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  49719. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  49720. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  49721. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  49722. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  49723. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  49724. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  49725. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  49726. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  49727. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  49728. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  49729. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  49730. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  49731. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  49732. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49733. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49734. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  49735. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  49736. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  49737. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  49738. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  49739. };
  49740. WOLFSSL_BUFFER_INFO msg;
  49741. /* Offset into ClientHello message data of first cipher suite. */
  49742. const int csOff = 78;
  49743. /* Server cipher list. */
  49744. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  49745. /* Suite list with duplicates. */
  49746. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  49747. "TLS13-AES128-GCM-SHA256:"
  49748. "TLS13-AES256-GCM-SHA384:"
  49749. "TLS13-AES256-GCM-SHA384:"
  49750. "TLS13-AES128-GCM-SHA256";
  49751. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  49752. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  49753. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  49754. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  49755. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  49756. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  49757. #endif
  49758. /* Set up wolfSSL context. */
  49759. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49760. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  49761. WOLFSSL_FILETYPE_PEM));
  49762. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  49763. WOLFSSL_FILETYPE_PEM));
  49764. /* Read from 'msg'. */
  49765. wolfSSL_SetIORecv(ctx, CsRecv);
  49766. /* No where to send to - dummy sender. */
  49767. wolfSSL_SetIOSend(ctx, CsSend);
  49768. /* Test cipher suite list with many copies of a cipher suite. */
  49769. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49770. msg.buffer = clientHello;
  49771. msg.length = (unsigned int)sizeof(clientHello);
  49772. wolfSSL_SetIOReadCtx(ssl, &msg);
  49773. /* Force server to have as many occurrences of same cipher suite as
  49774. * possible. */
  49775. if (ssl != NULL) {
  49776. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  49777. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  49778. for (i = 0; i < suites->suiteSz; i += 2) {
  49779. suites->suites[i + 0] = TLS13_BYTE;
  49780. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  49781. }
  49782. }
  49783. /* Test multiple occurrences of same cipher suite. */
  49784. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49785. wolfSSL_free(ssl);
  49786. ssl = NULL;
  49787. /* Set client order opposite to server order:
  49788. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  49789. clientHello[csOff + 0] = TLS13_BYTE;
  49790. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  49791. clientHello[csOff + 2] = TLS13_BYTE;
  49792. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  49793. /* Test server order negotiation. */
  49794. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49795. msg.buffer = clientHello;
  49796. msg.length = (unsigned int)sizeof(clientHello);
  49797. wolfSSL_SetIOReadCtx(ssl, &msg);
  49798. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49799. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49800. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  49801. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49802. /* Check refined order - server order. */
  49803. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49804. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49805. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49806. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49807. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49808. wolfSSL_free(ssl);
  49809. ssl = NULL;
  49810. /* Test client order negotiation. */
  49811. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49812. msg.buffer = clientHello;
  49813. msg.length = (unsigned int)sizeof(clientHello);
  49814. wolfSSL_SetIOReadCtx(ssl, &msg);
  49815. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49816. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49817. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  49818. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  49819. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49820. /* Check refined order - client order. */
  49821. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49822. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49823. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49824. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49825. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49826. wolfSSL_free(ssl);
  49827. ssl = NULL;
  49828. /* Check duplicate detection is working. */
  49829. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  49830. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49831. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49832. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49833. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49834. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49835. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  49836. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  49837. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  49838. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49839. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49840. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49841. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49842. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49843. #endif
  49844. wolfSSL_CTX_free(ctx);
  49845. #endif
  49846. return EXPECT_RESULT();
  49847. }
  49848. #endif
  49849. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  49850. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49851. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49852. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49853. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  49854. const unsigned char* priv, unsigned int privSz,
  49855. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  49856. unsigned char* out, unsigned int* outlen,
  49857. void* ctx)
  49858. {
  49859. int result;
  49860. /* Test fail when context associated with WOLFSSL is NULL */
  49861. if (ctx == NULL) {
  49862. return -1;
  49863. }
  49864. (void)ssl;
  49865. /* return 0 on success */
  49866. PRIVATE_KEY_UNLOCK();
  49867. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  49868. PRIVATE_KEY_LOCK();
  49869. return result;
  49870. }
  49871. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  49872. EXPECT_DECLS;
  49873. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  49874. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  49875. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  49876. WOLFSSL_SUCCESS);
  49877. #endif
  49878. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  49879. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  49880. WOLFSSL_SUCCESS);
  49881. #endif
  49882. return EXPECT_RESULT();
  49883. }
  49884. static int test_dh_ssl_setup(WOLFSSL* ssl)
  49885. {
  49886. EXPECT_DECLS;
  49887. static int dh_test_ctx = 1;
  49888. int ret;
  49889. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  49890. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  49891. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49892. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49893. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49894. }
  49895. return EXPECT_RESULT();
  49896. }
  49897. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  49898. {
  49899. EXPECT_DECLS;
  49900. int ret;
  49901. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  49902. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  49903. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49904. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49905. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49906. }
  49907. return EXPECT_RESULT();
  49908. }
  49909. #endif
  49910. static int test_DhCallbacks(void)
  49911. {
  49912. EXPECT_DECLS;
  49913. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49914. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49915. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49916. WOLFSSL_CTX *ctx = NULL;
  49917. WOLFSSL *ssl = NULL;
  49918. int test;
  49919. test_ssl_cbf func_cb_client;
  49920. test_ssl_cbf func_cb_server;
  49921. /* Test that DH callback APIs work. */
  49922. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  49923. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  49924. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  49925. /* load client ca cert */
  49926. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  49927. WOLFSSL_SUCCESS);
  49928. /* test with NULL arguments */
  49929. wolfSSL_SetDhAgreeCtx(NULL, &test);
  49930. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  49931. /* test success case */
  49932. test = 1;
  49933. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49934. wolfSSL_SetDhAgreeCtx(ssl, &test);
  49935. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  49936. wolfSSL_free(ssl);
  49937. wolfSSL_CTX_free(ctx);
  49938. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49939. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49940. /* set callbacks to use DH functions */
  49941. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49942. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  49943. func_cb_client.method = wolfTLSv1_2_client_method;
  49944. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49945. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  49946. func_cb_server.method = wolfTLSv1_2_server_method;
  49947. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49948. &func_cb_server, NULL), TEST_SUCCESS);
  49949. /* Test fail */
  49950. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49951. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49952. /* set callbacks to use DH functions */
  49953. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49954. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  49955. func_cb_client.method = wolfTLSv1_2_client_method;
  49956. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49957. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  49958. func_cb_server.method = wolfTLSv1_2_server_method;
  49959. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49960. &func_cb_server, NULL), TEST_FAIL);
  49961. #endif
  49962. return EXPECT_RESULT();
  49963. }
  49964. #endif /* HAVE_PK_CALLBACKS */
  49965. #ifdef HAVE_HASHDRBG
  49966. #ifdef TEST_RESEED_INTERVAL
  49967. static int test_wc_RNG_GenerateBlock_Reseed(void)
  49968. {
  49969. EXPECT_DECLS;
  49970. int i;
  49971. WC_RNG rng;
  49972. byte key[32];
  49973. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49974. ExpectIntEQ(wc_InitRng(&rng), 0);
  49975. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  49976. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49977. }
  49978. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49979. return EXPECT_RESULT();
  49980. }
  49981. #endif /* TEST_RESEED_INTERVAL */
  49982. static int test_wc_RNG_GenerateBlock(void)
  49983. {
  49984. EXPECT_DECLS;
  49985. int i;
  49986. WC_RNG rng;
  49987. byte key[32];
  49988. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49989. ExpectIntEQ(wc_InitRng(&rng), 0);
  49990. for (i = 0; i < 10; i++) {
  49991. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49992. }
  49993. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49994. return EXPECT_RESULT();
  49995. }
  49996. #endif /* HAVE_HASHDRBG */
  49997. /*
  49998. * Testing get_rand_digit
  49999. */
  50000. static int test_get_rand_digit(void)
  50001. {
  50002. EXPECT_DECLS;
  50003. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  50004. WC_RNG rng;
  50005. mp_digit d;
  50006. XMEMSET(&rng, 0, sizeof(WC_RNG));
  50007. ExpectIntEQ(wc_InitRng(&rng), 0);
  50008. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  50009. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  50010. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  50011. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  50012. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  50013. #endif
  50014. return EXPECT_RESULT();
  50015. } /* End test_get_rand_digit*/
  50016. /*
  50017. * Testing get_digit_count
  50018. */
  50019. static int test_get_digit_count(void)
  50020. {
  50021. EXPECT_DECLS;
  50022. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  50023. mp_int a;
  50024. XMEMSET(&a, 0, sizeof(mp_int));
  50025. ExpectIntEQ(mp_init(&a), 0);
  50026. ExpectIntEQ(get_digit_count(NULL), 0);
  50027. ExpectIntEQ(get_digit_count(&a), 0);
  50028. mp_clear(&a);
  50029. #endif
  50030. return EXPECT_RESULT();
  50031. } /* End test_get_digit_count*/
  50032. /*
  50033. * Testing mp_cond_copy
  50034. */
  50035. static int test_mp_cond_copy(void)
  50036. {
  50037. EXPECT_DECLS;
  50038. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  50039. defined(WOLFSSL_PUBLIC_MP)
  50040. mp_int a;
  50041. mp_int b;
  50042. int copy = 0;
  50043. XMEMSET(&a, 0, sizeof(mp_int));
  50044. XMEMSET(&b, 0, sizeof(mp_int));
  50045. ExpectIntEQ(mp_init(&a), MP_OKAY);
  50046. ExpectIntEQ(mp_init(&b), MP_OKAY);
  50047. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  50048. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  50049. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  50050. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  50051. mp_clear(&a);
  50052. mp_clear(&b);
  50053. #endif
  50054. return EXPECT_RESULT();
  50055. } /* End test_mp_cond_copy*/
  50056. /*
  50057. * Testing mp_rand
  50058. */
  50059. static int test_mp_rand(void)
  50060. {
  50061. EXPECT_DECLS;
  50062. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  50063. mp_int a;
  50064. WC_RNG rng;
  50065. int digits = 1;
  50066. XMEMSET(&a, 0, sizeof(mp_int));
  50067. XMEMSET(&rng, 0, sizeof(WC_RNG));
  50068. ExpectIntEQ(mp_init(&a), MP_OKAY);
  50069. ExpectIntEQ(wc_InitRng(&rng), 0);
  50070. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  50071. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  50072. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  50073. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  50074. mp_clear(&a);
  50075. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  50076. #endif
  50077. return EXPECT_RESULT();
  50078. } /* End test_mp_rand*/
  50079. /*
  50080. * Testing get_digit
  50081. */
  50082. static int test_get_digit(void)
  50083. {
  50084. EXPECT_DECLS;
  50085. #if defined(WOLFSSL_PUBLIC_MP)
  50086. mp_int a;
  50087. int n = 0;
  50088. XMEMSET(&a, 0, sizeof(mp_int));
  50089. ExpectIntEQ(mp_init(&a), MP_OKAY);
  50090. ExpectIntEQ(get_digit(NULL, n), 0);
  50091. ExpectIntEQ(get_digit(&a, n), 0);
  50092. mp_clear(&a);
  50093. #endif
  50094. return EXPECT_RESULT();
  50095. } /* End test_get_digit*/
  50096. /*
  50097. * Testing wc_export_int
  50098. */
  50099. static int test_wc_export_int(void)
  50100. {
  50101. EXPECT_DECLS;
  50102. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  50103. defined(WOLFSSL_PUBLIC_MP)
  50104. mp_int mp;
  50105. byte buf[32];
  50106. word32 keySz = (word32)sizeof(buf);
  50107. word32 len = (word32)sizeof(buf);
  50108. XMEMSET(&mp, 0, sizeof(mp_int));
  50109. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  50110. ExpectIntEQ(mp_set(&mp, 1234), 0);
  50111. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  50112. BAD_FUNC_ARG);
  50113. len = sizeof(buf)-1;
  50114. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  50115. BUFFER_E);
  50116. len = sizeof(buf);
  50117. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  50118. len = 4; /* test input too small */
  50119. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  50120. len = sizeof(buf);
  50121. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  50122. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  50123. ExpectIntEQ(len, 5);
  50124. mp_clear(&mp);
  50125. #endif
  50126. return EXPECT_RESULT();
  50127. } /* End test_wc_export_int*/
  50128. static int test_wc_InitRngNonce(void)
  50129. {
  50130. EXPECT_DECLS;
  50131. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  50132. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  50133. HAVE_FIPS_VERSION >= 2))
  50134. WC_RNG rng;
  50135. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  50136. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  50137. word32 nonceSz = sizeof(nonce);
  50138. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  50139. ExpectIntEQ(wc_FreeRng(&rng), 0);
  50140. #endif
  50141. return EXPECT_RESULT();
  50142. } /* End test_wc_InitRngNonce*/
  50143. /*
  50144. * Testing wc_InitRngNonce_ex
  50145. */
  50146. static int test_wc_InitRngNonce_ex(void)
  50147. {
  50148. EXPECT_DECLS;
  50149. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  50150. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  50151. HAVE_FIPS_VERSION >= 2))
  50152. WC_RNG rng;
  50153. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  50154. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  50155. word32 nonceSz = sizeof(nonce);
  50156. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  50157. 0);
  50158. ExpectIntEQ(wc_FreeRng(&rng), 0);
  50159. #endif
  50160. return EXPECT_RESULT();
  50161. } /* End test_wc_InitRngNonce_ex */
  50162. static int test_wolfSSL_X509_CRL(void)
  50163. {
  50164. EXPECT_DECLS;
  50165. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  50166. X509_CRL *crl = NULL;
  50167. char pem[][100] = {
  50168. "./certs/crl/crl.pem",
  50169. "./certs/crl/crl2.pem",
  50170. "./certs/crl/caEccCrl.pem",
  50171. "./certs/crl/eccCliCRL.pem",
  50172. "./certs/crl/eccSrvCRL.pem",
  50173. ""
  50174. };
  50175. #ifndef NO_BIO
  50176. BIO *bio = NULL;
  50177. #endif
  50178. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  50179. char der[][100] = {
  50180. "./certs/crl/crl.der",
  50181. "./certs/crl/crl2.der",
  50182. ""};
  50183. #endif
  50184. XFILE fp = XBADFILE;
  50185. int i;
  50186. for (i = 0; pem[i][0] != '\0'; i++)
  50187. {
  50188. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  50189. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50190. NULL, NULL));
  50191. ExpectNotNull(crl);
  50192. X509_CRL_free(crl);
  50193. if (fp != XBADFILE) {
  50194. XFCLOSE(fp);
  50195. fp = XBADFILE;
  50196. }
  50197. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  50198. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  50199. NULL));
  50200. if (EXPECT_FAIL()) {
  50201. crl = NULL;
  50202. }
  50203. ExpectNotNull(crl);
  50204. X509_CRL_free(crl);
  50205. crl = NULL;
  50206. if (fp != XBADFILE) {
  50207. XFCLOSE(fp);
  50208. fp = XBADFILE;
  50209. }
  50210. }
  50211. #ifndef NO_BIO
  50212. for (i = 0; pem[i][0] != '\0'; i++)
  50213. {
  50214. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  50215. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  50216. X509_CRL_free(crl);
  50217. crl = NULL;
  50218. BIO_free(bio);
  50219. bio = NULL;
  50220. }
  50221. #endif
  50222. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  50223. for (i = 0; der[i][0] != '\0'; i++) {
  50224. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  50225. ExpectTrue((fp != XBADFILE));
  50226. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  50227. ExpectNotNull(crl);
  50228. X509_CRL_free(crl);
  50229. if (fp != XBADFILE) {
  50230. XFCLOSE(fp);
  50231. fp = XBADFILE;
  50232. }
  50233. fp = XFOPEN(der[i], "rb");
  50234. ExpectTrue((fp != XBADFILE));
  50235. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  50236. if (EXPECT_FAIL()) {
  50237. crl = NULL;
  50238. }
  50239. ExpectNotNull(crl);
  50240. X509_CRL_free(crl);
  50241. crl = NULL;
  50242. if (fp != XBADFILE) {
  50243. XFCLOSE(fp);
  50244. fp = XBADFILE;
  50245. }
  50246. }
  50247. #endif
  50248. #endif
  50249. return EXPECT_RESULT();
  50250. }
  50251. static int test_wolfSSL_X509_load_crl_file(void)
  50252. {
  50253. EXPECT_DECLS;
  50254. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  50255. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  50256. int i;
  50257. char pem[][100] = {
  50258. "./certs/crl/crl.pem",
  50259. "./certs/crl/crl2.pem",
  50260. "./certs/crl/caEccCrl.pem",
  50261. "./certs/crl/eccCliCRL.pem",
  50262. "./certs/crl/eccSrvCRL.pem",
  50263. #ifdef WC_RSA_PSS
  50264. "./certs/crl/crl_rsapss.pem",
  50265. #endif
  50266. ""
  50267. };
  50268. char der[][100] = {
  50269. "./certs/crl/crl.der",
  50270. "./certs/crl/crl2.der",
  50271. ""
  50272. };
  50273. WOLFSSL_X509_STORE* store = NULL;
  50274. WOLFSSL_X509_LOOKUP* lookup = NULL;
  50275. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  50276. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  50277. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  50278. X509_FILETYPE_PEM), 1);
  50279. #ifdef WC_RSA_PSS
  50280. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  50281. X509_FILETYPE_PEM), 1);
  50282. #endif
  50283. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  50284. X509_FILETYPE_PEM), 1);
  50285. if (store) {
  50286. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  50287. WOLFSSL_FILETYPE_PEM), 1);
  50288. /* since store hasn't yet known the revoked cert*/
  50289. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50290. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  50291. }
  50292. for (i = 0; pem[i][0] != '\0'; i++) {
  50293. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  50294. 1);
  50295. }
  50296. if (store) {
  50297. /* since store knows crl list */
  50298. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50299. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  50300. CRL_CERT_REVOKED);
  50301. #ifdef WC_RSA_PSS
  50302. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50303. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  50304. CRL_CERT_REVOKED);
  50305. #endif
  50306. }
  50307. /* once feeing store */
  50308. X509_STORE_free(store);
  50309. store = NULL;
  50310. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  50311. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  50312. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  50313. X509_FILETYPE_PEM), 1);
  50314. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  50315. X509_FILETYPE_PEM), 1);
  50316. if (store) {
  50317. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  50318. WOLFSSL_FILETYPE_PEM), 1);
  50319. /* since store hasn't yet known the revoked cert*/
  50320. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50321. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  50322. }
  50323. for (i = 0; der[i][0] != '\0'; i++) {
  50324. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  50325. 1);
  50326. }
  50327. if (store) {
  50328. /* since store knows crl list */
  50329. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50330. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  50331. CRL_CERT_REVOKED);
  50332. }
  50333. /* test for incorrect parameter */
  50334. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  50335. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  50336. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  50337. X509_STORE_free(store);
  50338. store = NULL;
  50339. #endif
  50340. return EXPECT_RESULT();
  50341. }
  50342. static int test_wolfSSL_i2d_X509(void)
  50343. {
  50344. EXPECT_DECLS;
  50345. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  50346. const unsigned char* cert_buf = server_cert_der_2048;
  50347. unsigned char* out = NULL;
  50348. unsigned char* tmp = NULL;
  50349. X509* cert = NULL;
  50350. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  50351. /* Pointer should be advanced */
  50352. ExpectPtrGT(cert_buf, server_cert_der_2048);
  50353. ExpectIntGT(i2d_X509(cert, &out), 0);
  50354. ExpectNotNull(out);
  50355. tmp = out;
  50356. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  50357. ExpectPtrGT(tmp, out);
  50358. if (out != NULL)
  50359. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  50360. X509_free(cert);
  50361. #endif
  50362. return EXPECT_RESULT();
  50363. }
  50364. static int test_wolfSSL_d2i_X509_REQ(void)
  50365. {
  50366. EXPECT_DECLS;
  50367. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  50368. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  50369. !defined(WOLFSSL_SP_MATH)
  50370. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  50371. * were generated by libest
  50372. * ./certs/csr.attr.der contains sample attributes
  50373. * ./certs/csr.ext.der contains sample extensions */
  50374. const char* csrFile = "./certs/csr.signed.der";
  50375. const char* csrPopFile = "./certs/csr.attr.der";
  50376. const char* csrExtFile = "./certs/csr.ext.der";
  50377. /* ./certs/csr.dsa.pem is generated using
  50378. * openssl req -newkey dsa:certs/dsaparams.pem \
  50379. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  50380. * -outform PEM
  50381. * with the passphrase "wolfSSL"
  50382. */
  50383. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  50384. const char* csrDsaFile = "./certs/csr.dsa.pem";
  50385. XFILE f = XBADFILE;
  50386. #endif
  50387. BIO* bio = NULL;
  50388. X509* req = NULL;
  50389. EVP_PKEY *pub_key = NULL;
  50390. {
  50391. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  50392. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  50393. /*
  50394. * Extract the public key from the CSR
  50395. */
  50396. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  50397. /*
  50398. * Verify the signature in the CSR
  50399. */
  50400. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50401. X509_free(req);
  50402. req = NULL;
  50403. BIO_free(bio);
  50404. bio = NULL;
  50405. EVP_PKEY_free(pub_key);
  50406. pub_key = NULL;
  50407. }
  50408. {
  50409. #ifdef OPENSSL_ALL
  50410. X509_ATTRIBUTE* attr = NULL;
  50411. ASN1_TYPE *at = NULL;
  50412. #endif
  50413. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  50414. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  50415. /*
  50416. * Extract the public key from the CSR
  50417. */
  50418. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  50419. /*
  50420. * Verify the signature in the CSR
  50421. */
  50422. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50423. #ifdef OPENSSL_ALL
  50424. /*
  50425. * Obtain the challenge password from the CSR
  50426. */
  50427. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  50428. -1), 1);
  50429. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  50430. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  50431. ExpectNotNull(at->value.asn1_string);
  50432. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  50433. "2xIE+qqp/rhyTXP+");
  50434. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  50435. #endif
  50436. X509_free(req);
  50437. req = NULL;
  50438. BIO_free(bio);
  50439. bio = NULL;
  50440. EVP_PKEY_free(pub_key);
  50441. pub_key = NULL;
  50442. }
  50443. {
  50444. #ifdef OPENSSL_ALL
  50445. X509_ATTRIBUTE* attr = NULL;
  50446. ASN1_TYPE *at = NULL;
  50447. STACK_OF(X509_EXTENSION) *exts = NULL;
  50448. #endif
  50449. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  50450. /* This CSR contains an Extension Request attribute so
  50451. * we test extension parsing in a CSR attribute here. */
  50452. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  50453. /*
  50454. * Extract the public key from the CSR
  50455. */
  50456. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  50457. /*
  50458. * Verify the signature in the CSR
  50459. */
  50460. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50461. #ifdef OPENSSL_ALL
  50462. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  50463. req));
  50464. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  50465. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  50466. /*
  50467. * Obtain the challenge password from the CSR
  50468. */
  50469. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  50470. -1), 0);
  50471. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  50472. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  50473. ExpectNotNull(at->value.asn1_string);
  50474. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  50475. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  50476. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  50477. #endif
  50478. X509_free(req);
  50479. req = NULL;
  50480. BIO_free(bio);
  50481. bio = NULL;
  50482. EVP_PKEY_free(pub_key);
  50483. pub_key = NULL;
  50484. }
  50485. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  50486. {
  50487. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  50488. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  50489. /*
  50490. * Extract the public key from the CSR
  50491. */
  50492. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  50493. /*
  50494. * Verify the signature in the CSR
  50495. */
  50496. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50497. X509_free(req);
  50498. req = NULL;
  50499. BIO_free(bio);
  50500. /* Run the same test, but with a file pointer instead of a BIO.
  50501. * (PEM_read_X509_REQ)*/
  50502. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  50503. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  50504. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50505. X509_free(req);
  50506. EVP_PKEY_free(pub_key);
  50507. }
  50508. #endif /* !NO_DSA && !HAVE_SELFTEST */
  50509. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  50510. return EXPECT_RESULT();
  50511. }
  50512. static int test_wolfSSL_PEM_read_X509(void)
  50513. {
  50514. EXPECT_DECLS;
  50515. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  50516. !defined(NO_RSA)
  50517. X509 *x509 = NULL;
  50518. XFILE fp = XBADFILE;
  50519. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  50520. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  50521. X509_free(x509);
  50522. if (fp != XBADFILE)
  50523. XFCLOSE(fp);
  50524. #endif
  50525. return EXPECT_RESULT();
  50526. }
  50527. static int test_wolfSSL_PEM_read(void)
  50528. {
  50529. EXPECT_DECLS;
  50530. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  50531. const char* filename = "./certs/server-keyEnc.pem";
  50532. XFILE fp = XBADFILE;
  50533. char* name = NULL;
  50534. char* header = NULL;
  50535. byte* data = NULL;
  50536. long len;
  50537. EVP_CIPHER_INFO cipher;
  50538. WOLFSSL_BIO* bio = NULL;
  50539. byte* fileData = NULL;
  50540. size_t fileDataSz = 0;
  50541. byte* out;
  50542. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  50543. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  50544. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  50545. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  50546. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  50547. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  50548. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50549. ExpectIntGT(XSTRLEN(header), 0);
  50550. ExpectIntGT(len, 0);
  50551. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50552. name = NULL;
  50553. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50554. header = NULL;
  50555. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50556. data = NULL;
  50557. BIO_free(bio);
  50558. bio = NULL;
  50559. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  50560. /* Fail cases. */
  50561. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  50562. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  50563. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  50564. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  50565. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  50566. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50567. ExpectIntGT(XSTRLEN(header), 0);
  50568. ExpectIntGT(len, 0);
  50569. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  50570. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  50571. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  50572. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  50573. DYNAMIC_TYPE_TMP_BUFFER));
  50574. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  50575. if (fp != XBADFILE) {
  50576. XFCLOSE(fp);
  50577. fp = XBADFILE;
  50578. }
  50579. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50580. /* Fail cases. */
  50581. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  50582. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  50583. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  50584. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  50585. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  50586. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  50587. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  50588. /* Fail cases. */
  50589. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  50590. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  50591. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  50592. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  50593. /* Pass case */
  50594. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  50595. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50596. name = NULL;
  50597. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50598. header = NULL;
  50599. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50600. data = NULL;
  50601. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  50602. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  50603. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50604. ExpectIntGT(XSTRLEN(header), 0);
  50605. ExpectIntGT(len, 0);
  50606. /* Fail cases. */
  50607. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  50608. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  50609. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  50610. #ifndef NO_DES3
  50611. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  50612. #endif
  50613. /* Fail cases. */
  50614. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  50615. (void*)"yassl123"), WOLFSSL_FAILURE);
  50616. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  50617. (void*)"yassl123"), WOLFSSL_FAILURE);
  50618. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  50619. (void*)"yassl123"), WOLFSSL_FAILURE);
  50620. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  50621. (void*)"yassl123"), WOLFSSL_FAILURE);
  50622. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  50623. (void*)"yassl123"), WOLFSSL_FAILURE);
  50624. #if !defined(NO_DES3) && !defined(NO_MD5)
  50625. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  50626. (void*)"yassl123"), WOLFSSL_SUCCESS);
  50627. #else
  50628. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  50629. (void*)"yassl123"), WOLFSSL_FAILURE);
  50630. #endif
  50631. BIO_free(bio);
  50632. bio = NULL;
  50633. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50634. fileData = NULL;
  50635. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50636. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50637. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50638. name = NULL;
  50639. header = NULL;
  50640. data = NULL;
  50641. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  50642. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  50643. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50644. ExpectIntEQ(XSTRLEN(header), 0);
  50645. ExpectIntGT(len, 0);
  50646. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  50647. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  50648. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  50649. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  50650. DYNAMIC_TYPE_TMP_BUFFER));
  50651. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  50652. if (fp != XBADFILE)
  50653. XFCLOSE(fp);
  50654. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50655. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  50656. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  50657. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  50658. BIO_free(bio);
  50659. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50660. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50661. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50662. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50663. #endif
  50664. return EXPECT_RESULT();
  50665. }
  50666. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  50667. {
  50668. EXPECT_DECLS;
  50669. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50670. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50671. const byte iv[12] = { 0 };
  50672. const byte key[16] = { 0 };
  50673. const byte cleartext[16] = { 0 };
  50674. const byte aad[] = {
  50675. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  50676. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  50677. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  50678. 0x4f
  50679. };
  50680. byte out1Part[16];
  50681. byte outTag1Part[16];
  50682. byte out2Part[16];
  50683. byte outTag2Part[16];
  50684. byte decryptBuf[16];
  50685. int len = 0;
  50686. int tlen;
  50687. EVP_CIPHER_CTX* ctx = NULL;
  50688. /* ENCRYPT */
  50689. /* Send AAD and data in 1 part */
  50690. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50691. tlen = 0;
  50692. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50693. 1);
  50694. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50695. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  50696. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  50697. sizeof(cleartext)), 1);
  50698. tlen += len;
  50699. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  50700. tlen += len;
  50701. ExpectIntEQ(tlen, sizeof(cleartext));
  50702. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  50703. outTag1Part), 1);
  50704. EVP_CIPHER_CTX_free(ctx);
  50705. ctx = NULL;
  50706. /* DECRYPT */
  50707. /* Send AAD and data in 1 part */
  50708. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50709. tlen = 0;
  50710. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50711. 1);
  50712. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50713. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  50714. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  50715. sizeof(cleartext)), 1);
  50716. tlen += len;
  50717. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  50718. outTag1Part), 1);
  50719. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  50720. tlen += len;
  50721. ExpectIntEQ(tlen, sizeof(cleartext));
  50722. EVP_CIPHER_CTX_free(ctx);
  50723. ctx = NULL;
  50724. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  50725. /* ENCRYPT */
  50726. /* Send AAD and data in 2 parts */
  50727. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50728. tlen = 0;
  50729. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50730. 1);
  50731. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50732. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  50733. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  50734. 1);
  50735. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  50736. tlen += len;
  50737. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  50738. sizeof(cleartext) - 1), 1);
  50739. tlen += len;
  50740. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  50741. tlen += len;
  50742. ExpectIntEQ(tlen, sizeof(cleartext));
  50743. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  50744. outTag2Part), 1);
  50745. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  50746. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  50747. EVP_CIPHER_CTX_free(ctx);
  50748. ctx = NULL;
  50749. /* DECRYPT */
  50750. /* Send AAD and data in 2 parts */
  50751. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50752. tlen = 0;
  50753. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50754. 1);
  50755. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50756. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  50757. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  50758. 1);
  50759. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  50760. tlen += len;
  50761. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  50762. sizeof(cleartext) - 1), 1);
  50763. tlen += len;
  50764. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  50765. outTag1Part), 1);
  50766. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  50767. tlen += len;
  50768. ExpectIntEQ(tlen, sizeof(cleartext));
  50769. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  50770. /* Test AAD reuse */
  50771. EVP_CIPHER_CTX_free(ctx);
  50772. #endif
  50773. return EXPECT_RESULT();
  50774. }
  50775. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  50776. {
  50777. EXPECT_DECLS;
  50778. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50779. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50780. /* Zero length plain text */
  50781. byte key[] = {
  50782. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50783. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50784. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50785. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50786. }; /* align */
  50787. byte iv[] = {
  50788. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50789. }; /* align */
  50790. byte plaintxt[1];
  50791. int ivSz = 12;
  50792. int plaintxtSz = 0;
  50793. unsigned char tag[16];
  50794. unsigned char tag_kat[] = {
  50795. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  50796. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  50797. };
  50798. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50799. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50800. int ciphertxtSz = 0;
  50801. int decryptedtxtSz = 0;
  50802. int len = 0;
  50803. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50804. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50805. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  50806. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50807. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50808. plaintxtSz));
  50809. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50810. ciphertxtSz += len;
  50811. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50812. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50813. ExpectIntEQ(0, ciphertxtSz);
  50814. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50815. EVP_CIPHER_CTX_init(de);
  50816. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  50817. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50818. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50819. decryptedtxtSz = len;
  50820. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50821. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50822. decryptedtxtSz += len;
  50823. ExpectIntEQ(0, decryptedtxtSz);
  50824. EVP_CIPHER_CTX_free(en);
  50825. EVP_CIPHER_CTX_free(de);
  50826. #endif
  50827. return EXPECT_RESULT();
  50828. }
  50829. static int test_wolfssl_EVP_aes_gcm(void)
  50830. {
  50831. EXPECT_DECLS;
  50832. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50833. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50834. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50835. byte *key = (byte*)"01234567890123456789012345678901";
  50836. /* A 128 bit IV */
  50837. byte *iv = (byte*)"0123456789012345";
  50838. int ivSz = AES_BLOCK_SIZE;
  50839. /* Message to be encrypted */
  50840. byte *plaintxt = (byte*)"for things to change you have to change";
  50841. /* Additional non-confidential data */
  50842. byte *aad = (byte*)"Don't spend major time on minor things.";
  50843. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50844. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50845. int aadSz = (int)XSTRLEN((char*)aad);
  50846. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50847. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50848. int ciphertxtSz = 0;
  50849. int decryptedtxtSz = 0;
  50850. int len = 0;
  50851. int i = 0;
  50852. EVP_CIPHER_CTX en[2];
  50853. EVP_CIPHER_CTX de[2];
  50854. for (i = 0; i < 2; i++) {
  50855. EVP_CIPHER_CTX_init(&en[i]);
  50856. if (i == 0) {
  50857. /* Default uses 96-bits IV length */
  50858. #ifdef WOLFSSL_AES_128
  50859. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50860. key, iv));
  50861. #elif defined(WOLFSSL_AES_192)
  50862. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50863. key, iv));
  50864. #elif defined(WOLFSSL_AES_256)
  50865. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50866. key, iv));
  50867. #endif
  50868. }
  50869. else {
  50870. #ifdef WOLFSSL_AES_128
  50871. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50872. NULL, NULL));
  50873. #elif defined(WOLFSSL_AES_192)
  50874. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50875. NULL, NULL));
  50876. #elif defined(WOLFSSL_AES_256)
  50877. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50878. NULL, NULL));
  50879. #endif
  50880. /* non-default must to set the IV length first */
  50881. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50882. ivSz, NULL));
  50883. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50884. }
  50885. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50886. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50887. plaintxtSz));
  50888. ciphertxtSz = len;
  50889. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50890. ciphertxtSz += len;
  50891. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50892. AES_BLOCK_SIZE, tag));
  50893. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50894. EVP_CIPHER_CTX_init(&de[i]);
  50895. if (i == 0) {
  50896. /* Default uses 96-bits IV length */
  50897. #ifdef WOLFSSL_AES_128
  50898. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50899. key, iv));
  50900. #elif defined(WOLFSSL_AES_192)
  50901. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50902. key, iv));
  50903. #elif defined(WOLFSSL_AES_256)
  50904. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50905. key, iv));
  50906. #endif
  50907. }
  50908. else {
  50909. #ifdef WOLFSSL_AES_128
  50910. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50911. NULL, NULL));
  50912. #elif defined(WOLFSSL_AES_192)
  50913. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50914. NULL, NULL));
  50915. #elif defined(WOLFSSL_AES_256)
  50916. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50917. NULL, NULL));
  50918. #endif
  50919. /* non-default must to set the IV length first */
  50920. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50921. ivSz, NULL));
  50922. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50923. }
  50924. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50925. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50926. ciphertxtSz));
  50927. decryptedtxtSz = len;
  50928. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50929. AES_BLOCK_SIZE, tag));
  50930. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50931. decryptedtxtSz += len;
  50932. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50933. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50934. /* modify tag*/
  50935. if (i == 0) {
  50936. /* Default uses 96-bits IV length */
  50937. #ifdef WOLFSSL_AES_128
  50938. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50939. key, iv));
  50940. #elif defined(WOLFSSL_AES_192)
  50941. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50942. key, iv));
  50943. #elif defined(WOLFSSL_AES_256)
  50944. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50945. key, iv));
  50946. #endif
  50947. }
  50948. else {
  50949. #ifdef WOLFSSL_AES_128
  50950. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50951. NULL, NULL));
  50952. #elif defined(WOLFSSL_AES_192)
  50953. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50954. NULL, NULL));
  50955. #elif defined(WOLFSSL_AES_256)
  50956. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50957. NULL, NULL));
  50958. #endif
  50959. /* non-default must to set the IV length first */
  50960. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50961. ivSz, NULL));
  50962. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50963. }
  50964. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50965. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50966. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50967. AES_BLOCK_SIZE, tag));
  50968. /* fail due to wrong tag */
  50969. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50970. ciphertxtSz));
  50971. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50972. ExpectIntEQ(0, len);
  50973. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50974. }
  50975. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50976. return EXPECT_RESULT();
  50977. }
  50978. static int test_wolfssl_EVP_aria_gcm(void)
  50979. {
  50980. int res = TEST_SKIPPED;
  50981. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  50982. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50983. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50984. byte *key = (byte*)"01234567890123456789012345678901";
  50985. /* A 128 bit IV */
  50986. byte *iv = (byte*)"0123456789012345";
  50987. int ivSz = ARIA_BLOCK_SIZE;
  50988. /* Message to be encrypted */
  50989. const int plaintxtSz = 40;
  50990. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50991. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  50992. /* Additional non-confidential data */
  50993. byte *aad = (byte*)"Don't spend major time on minor things.";
  50994. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  50995. int aadSz = (int)XSTRLEN((char*)aad);
  50996. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50997. byte decryptedtxt[plaintxtSz];
  50998. int ciphertxtSz = 0;
  50999. int decryptedtxtSz = 0;
  51000. int len = 0;
  51001. int i = 0;
  51002. #define TEST_ARIA_GCM_COUNT 6
  51003. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  51004. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  51005. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  51006. EVP_CIPHER_CTX_init(&en[i]);
  51007. switch (i) {
  51008. case 0:
  51009. /* Default uses 96-bits IV length */
  51010. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  51011. break;
  51012. case 1:
  51013. /* Default uses 96-bits IV length */
  51014. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  51015. break;
  51016. case 2:
  51017. /* Default uses 96-bits IV length */
  51018. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  51019. break;
  51020. case 3:
  51021. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  51022. /* non-default must to set the IV length first */
  51023. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51024. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51025. break;
  51026. case 4:
  51027. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  51028. /* non-default must to set the IV length first */
  51029. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51030. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51031. break;
  51032. case 5:
  51033. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  51034. /* non-default must to set the IV length first */
  51035. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51036. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51037. break;
  51038. }
  51039. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  51040. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51041. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  51042. ciphertxtSz = len;
  51043. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51044. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  51045. ciphertxtSz += len;
  51046. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  51047. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51048. EVP_CIPHER_CTX_init(&de[i]);
  51049. switch (i) {
  51050. case 0:
  51051. /* Default uses 96-bits IV length */
  51052. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  51053. break;
  51054. case 1:
  51055. /* Default uses 96-bits IV length */
  51056. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  51057. break;
  51058. case 2:
  51059. /* Default uses 96-bits IV length */
  51060. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  51061. break;
  51062. case 3:
  51063. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  51064. /* non-default must to set the IV length first */
  51065. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51066. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51067. break;
  51068. case 4:
  51069. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  51070. /* non-default must to set the IV length first */
  51071. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51072. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51073. break;
  51074. case 5:
  51075. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  51076. /* non-default must to set the IV length first */
  51077. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51078. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51079. break;
  51080. }
  51081. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  51082. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51083. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  51084. decryptedtxtSz = len;
  51085. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  51086. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51087. decryptedtxtSz += len;
  51088. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  51089. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51090. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  51091. /* modify tag*/
  51092. tag[AES_BLOCK_SIZE-1]+=0xBB;
  51093. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51094. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  51095. /* fail due to wrong tag */
  51096. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  51097. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51098. AssertIntEQ(0, len);
  51099. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51100. }
  51101. res = TEST_RES_CHECK(1);
  51102. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  51103. return res;
  51104. }
  51105. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  51106. {
  51107. EXPECT_DECLS;
  51108. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  51109. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  51110. /* Zero length plain text */
  51111. byte key[] = {
  51112. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51113. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51114. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51115. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51116. }; /* align */
  51117. byte iv[] = {
  51118. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51119. }; /* align */
  51120. byte plaintxt[1];
  51121. int ivSz = 12;
  51122. int plaintxtSz = 0;
  51123. unsigned char tag[16];
  51124. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  51125. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  51126. int ciphertxtSz = 0;
  51127. int decryptedtxtSz = 0;
  51128. int len = 0;
  51129. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  51130. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  51131. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  51132. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51133. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  51134. plaintxtSz));
  51135. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  51136. ciphertxtSz += len;
  51137. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  51138. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  51139. ExpectIntEQ(0, ciphertxtSz);
  51140. EVP_CIPHER_CTX_init(de);
  51141. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  51142. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51143. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  51144. decryptedtxtSz = len;
  51145. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  51146. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  51147. decryptedtxtSz += len;
  51148. ExpectIntEQ(0, decryptedtxtSz);
  51149. EVP_CIPHER_CTX_free(en);
  51150. EVP_CIPHER_CTX_free(de);
  51151. #endif
  51152. return EXPECT_RESULT();
  51153. }
  51154. static int test_wolfssl_EVP_aes_ccm(void)
  51155. {
  51156. EXPECT_DECLS;
  51157. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  51158. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  51159. /* A 256 bit key, AES_128 will use the first 128 bit*/
  51160. byte *key = (byte*)"01234567890123456789012345678901";
  51161. /* A 128 bit IV */
  51162. byte *iv = (byte*)"0123456789012";
  51163. int ivSz = (int)XSTRLEN((char*)iv);
  51164. /* Message to be encrypted */
  51165. byte *plaintxt = (byte*)"for things to change you have to change";
  51166. /* Additional non-confidential data */
  51167. byte *aad = (byte*)"Don't spend major time on minor things.";
  51168. unsigned char tag[AES_BLOCK_SIZE] = {0};
  51169. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51170. int aadSz = (int)XSTRLEN((char*)aad);
  51171. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  51172. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  51173. int ciphertxtSz = 0;
  51174. int decryptedtxtSz = 0;
  51175. int len = 0;
  51176. int i = 0;
  51177. int ret;
  51178. EVP_CIPHER_CTX en[2];
  51179. EVP_CIPHER_CTX de[2];
  51180. for (i = 0; i < 2; i++) {
  51181. EVP_CIPHER_CTX_init(&en[i]);
  51182. if (i == 0) {
  51183. /* Default uses 96-bits IV length */
  51184. #ifdef WOLFSSL_AES_128
  51185. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  51186. key, iv));
  51187. #elif defined(WOLFSSL_AES_192)
  51188. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  51189. key, iv));
  51190. #elif defined(WOLFSSL_AES_256)
  51191. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  51192. key, iv));
  51193. #endif
  51194. }
  51195. else {
  51196. #ifdef WOLFSSL_AES_128
  51197. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  51198. NULL, NULL));
  51199. #elif defined(WOLFSSL_AES_192)
  51200. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  51201. NULL, NULL));
  51202. #elif defined(WOLFSSL_AES_256)
  51203. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  51204. NULL, NULL));
  51205. #endif
  51206. /* non-default must to set the IV length first */
  51207. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  51208. ivSz, NULL));
  51209. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51210. }
  51211. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51212. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51213. plaintxtSz));
  51214. ciphertxtSz = len;
  51215. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51216. ciphertxtSz += len;
  51217. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  51218. AES_BLOCK_SIZE, tag));
  51219. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  51220. ExpectIntEQ(ret, 1);
  51221. EVP_CIPHER_CTX_init(&de[i]);
  51222. if (i == 0) {
  51223. /* Default uses 96-bits IV length */
  51224. #ifdef WOLFSSL_AES_128
  51225. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  51226. key, iv));
  51227. #elif defined(WOLFSSL_AES_192)
  51228. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  51229. key, iv));
  51230. #elif defined(WOLFSSL_AES_256)
  51231. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  51232. key, iv));
  51233. #endif
  51234. }
  51235. else {
  51236. #ifdef WOLFSSL_AES_128
  51237. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  51238. NULL, NULL));
  51239. #elif defined(WOLFSSL_AES_192)
  51240. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  51241. NULL, NULL));
  51242. #elif defined(WOLFSSL_AES_256)
  51243. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  51244. NULL, NULL));
  51245. #endif
  51246. /* non-default must to set the IV length first */
  51247. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  51248. ivSz, NULL));
  51249. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51250. }
  51251. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51252. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51253. ciphertxtSz));
  51254. decryptedtxtSz = len;
  51255. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51256. AES_BLOCK_SIZE, tag));
  51257. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51258. decryptedtxtSz += len;
  51259. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51260. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51261. /* modify tag*/
  51262. tag[AES_BLOCK_SIZE-1]+=0xBB;
  51263. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51264. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51265. AES_BLOCK_SIZE, tag));
  51266. /* fail due to wrong tag */
  51267. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51268. ciphertxtSz));
  51269. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51270. ExpectIntEQ(0, len);
  51271. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  51272. ExpectIntEQ(ret, 1);
  51273. }
  51274. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  51275. return EXPECT_RESULT();
  51276. }
  51277. static int test_wolfssl_EVP_chacha20_poly1305(void)
  51278. {
  51279. EXPECT_DECLS;
  51280. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  51281. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  51282. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  51283. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  51284. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  51285. byte cipherText[sizeof(plainText)];
  51286. byte decryptedText[sizeof(plainText)];
  51287. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  51288. EVP_CIPHER_CTX* ctx = NULL;
  51289. int outSz;
  51290. /* Encrypt. */
  51291. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51292. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  51293. NULL), WOLFSSL_SUCCESS);
  51294. /* Invalid IV length. */
  51295. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  51296. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  51297. /* Valid IV length. */
  51298. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  51299. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  51300. /* Invalid tag length. */
  51301. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  51302. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  51303. /* Valid tag length. */
  51304. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  51305. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  51306. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51307. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  51308. WOLFSSL_SUCCESS);
  51309. ExpectIntEQ(outSz, sizeof(aad));
  51310. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51311. sizeof(plainText)), WOLFSSL_SUCCESS);
  51312. ExpectIntEQ(outSz, sizeof(plainText));
  51313. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  51314. ExpectIntEQ(outSz, 0);
  51315. /* Invalid tag length. */
  51316. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  51317. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  51318. /* Valid tag length. */
  51319. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  51320. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  51321. EVP_CIPHER_CTX_free(ctx);
  51322. ctx = NULL;
  51323. /* Decrypt. */
  51324. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51325. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  51326. NULL), WOLFSSL_SUCCESS);
  51327. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  51328. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  51329. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  51330. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  51331. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51332. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  51333. WOLFSSL_SUCCESS);
  51334. ExpectIntEQ(outSz, sizeof(aad));
  51335. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51336. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51337. ExpectIntEQ(outSz, sizeof(cipherText));
  51338. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51339. WOLFSSL_SUCCESS);
  51340. ExpectIntEQ(outSz, 0);
  51341. EVP_CIPHER_CTX_free(ctx);
  51342. ctx = NULL;
  51343. /* Test partial Inits. CipherInit() allow setting of key and iv
  51344. * in separate calls. */
  51345. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51346. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  51347. key, NULL, 1), WOLFSSL_SUCCESS);
  51348. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  51349. WOLFSSL_SUCCESS);
  51350. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  51351. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  51352. ExpectIntEQ(outSz, sizeof(aad));
  51353. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51354. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51355. ExpectIntEQ(outSz, sizeof(cipherText));
  51356. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51357. WOLFSSL_SUCCESS);
  51358. ExpectIntEQ(outSz, 0);
  51359. EVP_CIPHER_CTX_free(ctx);
  51360. #endif
  51361. return EXPECT_RESULT();
  51362. }
  51363. static int test_wolfssl_EVP_chacha20(void)
  51364. {
  51365. EXPECT_DECLS;
  51366. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  51367. byte key[CHACHA_MAX_KEY_SZ];
  51368. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  51369. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  51370. byte cipherText[sizeof(plainText)];
  51371. byte decryptedText[sizeof(plainText)];
  51372. EVP_CIPHER_CTX* ctx = NULL;
  51373. int outSz;
  51374. XMEMSET(key, 0, sizeof(key));
  51375. XMEMSET(iv, 0, sizeof(iv));
  51376. /* Encrypt. */
  51377. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51378. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  51379. NULL), WOLFSSL_SUCCESS);
  51380. /* Any tag length must fail - not an AEAD cipher. */
  51381. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  51382. 16, NULL), WOLFSSL_FAILURE);
  51383. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51384. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51385. sizeof(plainText)), WOLFSSL_SUCCESS);
  51386. ExpectIntEQ(outSz, sizeof(plainText));
  51387. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  51388. ExpectIntEQ(outSz, 0);
  51389. EVP_CIPHER_CTX_free(ctx);
  51390. ctx = NULL;
  51391. /* Decrypt. */
  51392. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51393. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  51394. NULL), WOLFSSL_SUCCESS);
  51395. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51396. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51397. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51398. ExpectIntEQ(outSz, sizeof(cipherText));
  51399. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51400. WOLFSSL_SUCCESS);
  51401. ExpectIntEQ(outSz, 0);
  51402. EVP_CIPHER_CTX_free(ctx);
  51403. ctx = NULL;
  51404. /* Test partial Inits. CipherInit() allow setting of key and iv
  51405. * in separate calls. */
  51406. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51407. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  51408. key, NULL, 1), WOLFSSL_SUCCESS);
  51409. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  51410. WOLFSSL_SUCCESS);
  51411. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51412. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51413. ExpectIntEQ(outSz, sizeof(cipherText));
  51414. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51415. WOLFSSL_SUCCESS);
  51416. ExpectIntEQ(outSz, 0);
  51417. EVP_CIPHER_CTX_free(ctx);
  51418. #endif
  51419. return EXPECT_RESULT();
  51420. }
  51421. static int test_wolfssl_EVP_sm4_ecb(void)
  51422. {
  51423. int res = TEST_SKIPPED;
  51424. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  51425. EXPECT_DECLS;
  51426. byte key[SM4_KEY_SIZE];
  51427. byte plainText[SM4_BLOCK_SIZE] = {
  51428. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  51429. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  51430. };
  51431. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  51432. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  51433. EVP_CIPHER_CTX* ctx;
  51434. int outSz;
  51435. XMEMSET(key, 0, sizeof(key));
  51436. /* Encrypt. */
  51437. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51438. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  51439. WOLFSSL_SUCCESS);
  51440. /* Any tag length must fail - not an AEAD cipher. */
  51441. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  51442. WOLFSSL_FAILURE);
  51443. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  51444. WOLFSSL_SUCCESS);
  51445. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51446. sizeof(plainText)), WOLFSSL_SUCCESS);
  51447. ExpectIntEQ(outSz, sizeof(plainText));
  51448. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  51449. WOLFSSL_SUCCESS);
  51450. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  51451. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  51452. EVP_CIPHER_CTX_free(ctx);
  51453. /* Decrypt. */
  51454. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51455. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  51456. WOLFSSL_SUCCESS);
  51457. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  51458. WOLFSSL_SUCCESS);
  51459. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51460. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51461. ExpectIntEQ(outSz, sizeof(plainText));
  51462. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  51463. WOLFSSL_SUCCESS);
  51464. ExpectIntEQ(outSz, 0);
  51465. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51466. EVP_CIPHER_CTX_free(ctx);
  51467. res = EXPECT_RESULT();
  51468. #endif
  51469. return res;
  51470. }
  51471. static int test_wolfssl_EVP_sm4_cbc(void)
  51472. {
  51473. int res = TEST_SKIPPED;
  51474. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  51475. EXPECT_DECLS;
  51476. byte key[SM4_KEY_SIZE];
  51477. byte iv[SM4_BLOCK_SIZE];
  51478. byte plainText[SM4_BLOCK_SIZE] = {
  51479. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  51480. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  51481. };
  51482. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  51483. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  51484. EVP_CIPHER_CTX* ctx;
  51485. int outSz;
  51486. XMEMSET(key, 0, sizeof(key));
  51487. XMEMSET(iv, 0, sizeof(iv));
  51488. /* Encrypt. */
  51489. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51490. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  51491. WOLFSSL_SUCCESS);
  51492. /* Any tag length must fail - not an AEAD cipher. */
  51493. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  51494. WOLFSSL_FAILURE);
  51495. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51496. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51497. sizeof(plainText)), WOLFSSL_SUCCESS);
  51498. ExpectIntEQ(outSz, sizeof(plainText));
  51499. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  51500. WOLFSSL_SUCCESS);
  51501. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  51502. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  51503. EVP_CIPHER_CTX_free(ctx);
  51504. /* Decrypt. */
  51505. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51506. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  51507. WOLFSSL_SUCCESS);
  51508. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51509. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51510. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51511. ExpectIntEQ(outSz, sizeof(plainText));
  51512. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  51513. WOLFSSL_SUCCESS);
  51514. ExpectIntEQ(outSz, 0);
  51515. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51516. EVP_CIPHER_CTX_free(ctx);
  51517. /* Test partial Inits. CipherInit() allow setting of key and iv
  51518. * in separate calls. */
  51519. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51520. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  51521. WOLFSSL_SUCCESS);
  51522. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  51523. WOLFSSL_SUCCESS);
  51524. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51525. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51526. ExpectIntEQ(outSz, sizeof(plainText));
  51527. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  51528. WOLFSSL_SUCCESS);
  51529. ExpectIntEQ(outSz, 0);
  51530. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51531. EVP_CIPHER_CTX_free(ctx);
  51532. res = EXPECT_RESULT();
  51533. #endif
  51534. return res;
  51535. }
  51536. static int test_wolfssl_EVP_sm4_ctr(void)
  51537. {
  51538. int res = TEST_SKIPPED;
  51539. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  51540. EXPECT_DECLS;
  51541. byte key[SM4_KEY_SIZE];
  51542. byte iv[SM4_BLOCK_SIZE];
  51543. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  51544. byte cipherText[sizeof(plainText)];
  51545. byte decryptedText[sizeof(plainText)];
  51546. EVP_CIPHER_CTX* ctx;
  51547. int outSz;
  51548. XMEMSET(key, 0, sizeof(key));
  51549. XMEMSET(iv, 0, sizeof(iv));
  51550. /* Encrypt. */
  51551. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51552. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  51553. WOLFSSL_SUCCESS);
  51554. /* Any tag length must fail - not an AEAD cipher. */
  51555. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  51556. WOLFSSL_FAILURE);
  51557. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51558. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51559. sizeof(plainText)), WOLFSSL_SUCCESS);
  51560. ExpectIntEQ(outSz, sizeof(plainText));
  51561. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  51562. ExpectIntEQ(outSz, 0);
  51563. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  51564. EVP_CIPHER_CTX_free(ctx);
  51565. /* Decrypt. */
  51566. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51567. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  51568. WOLFSSL_SUCCESS);
  51569. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51570. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51571. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51572. ExpectIntEQ(outSz, sizeof(cipherText));
  51573. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51574. WOLFSSL_SUCCESS);
  51575. ExpectIntEQ(outSz, 0);
  51576. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51577. EVP_CIPHER_CTX_free(ctx);
  51578. /* Test partial Inits. CipherInit() allow setting of key and iv
  51579. * in separate calls. */
  51580. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51581. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  51582. WOLFSSL_SUCCESS);
  51583. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  51584. WOLFSSL_SUCCESS);
  51585. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51586. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51587. ExpectIntEQ(outSz, sizeof(cipherText));
  51588. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51589. WOLFSSL_SUCCESS);
  51590. ExpectIntEQ(outSz, 0);
  51591. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51592. EVP_CIPHER_CTX_free(ctx);
  51593. res = EXPECT_RESULT();
  51594. #endif
  51595. return res;
  51596. }
  51597. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  51598. {
  51599. int res = TEST_SKIPPED;
  51600. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  51601. /* Zero length plain text */
  51602. EXPECT_DECLS;
  51603. byte key[] = {
  51604. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51605. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51606. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51608. }; /* align */
  51609. byte iv[] = {
  51610. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51611. }; /* align */
  51612. byte plaintxt[1];
  51613. int ivSz = 12;
  51614. int plaintxtSz = 0;
  51615. unsigned char tag[16];
  51616. unsigned char tag_kat[16] = {
  51617. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  51618. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  51619. };
  51620. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51621. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51622. int ciphertxtSz = 0;
  51623. int decryptedtxtSz = 0;
  51624. int len = 0;
  51625. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  51626. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  51627. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  51628. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51629. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  51630. plaintxtSz));
  51631. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  51632. ciphertxtSz += len;
  51633. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  51634. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  51635. ExpectIntEQ(0, ciphertxtSz);
  51636. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  51637. EVP_CIPHER_CTX_init(de);
  51638. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  51639. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51640. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  51641. decryptedtxtSz = len;
  51642. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  51643. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  51644. decryptedtxtSz += len;
  51645. ExpectIntEQ(0, decryptedtxtSz);
  51646. EVP_CIPHER_CTX_free(en);
  51647. EVP_CIPHER_CTX_free(de);
  51648. res = EXPECT_RESULT();
  51649. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  51650. return res;
  51651. }
  51652. static int test_wolfssl_EVP_sm4_gcm(void)
  51653. {
  51654. int res = TEST_SKIPPED;
  51655. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  51656. EXPECT_DECLS;
  51657. byte *key = (byte*)"0123456789012345";
  51658. /* A 128 bit IV */
  51659. byte *iv = (byte*)"0123456789012345";
  51660. int ivSz = SM4_BLOCK_SIZE;
  51661. /* Message to be encrypted */
  51662. byte *plaintxt = (byte*)"for things to change you have to change";
  51663. /* Additional non-confidential data */
  51664. byte *aad = (byte*)"Don't spend major time on minor things.";
  51665. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  51666. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51667. int aadSz = (int)XSTRLEN((char*)aad);
  51668. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51669. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51670. int ciphertxtSz = 0;
  51671. int decryptedtxtSz = 0;
  51672. int len = 0;
  51673. int i = 0;
  51674. EVP_CIPHER_CTX en[2];
  51675. EVP_CIPHER_CTX de[2];
  51676. for (i = 0; i < 2; i++) {
  51677. EVP_CIPHER_CTX_init(&en[i]);
  51678. if (i == 0) {
  51679. /* Default uses 96-bits IV length */
  51680. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  51681. iv));
  51682. }
  51683. else {
  51684. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  51685. NULL));
  51686. /* non-default must to set the IV length first */
  51687. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  51688. ivSz, NULL));
  51689. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51690. }
  51691. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51692. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51693. plaintxtSz));
  51694. ciphertxtSz = len;
  51695. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51696. ciphertxtSz += len;
  51697. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  51698. SM4_BLOCK_SIZE, tag));
  51699. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51700. EVP_CIPHER_CTX_init(&de[i]);
  51701. if (i == 0) {
  51702. /* Default uses 96-bits IV length */
  51703. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  51704. iv));
  51705. }
  51706. else {
  51707. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  51708. NULL));
  51709. /* non-default must to set the IV length first */
  51710. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  51711. ivSz, NULL));
  51712. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51713. }
  51714. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51715. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51716. ciphertxtSz));
  51717. decryptedtxtSz = len;
  51718. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  51719. SM4_BLOCK_SIZE, tag));
  51720. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51721. decryptedtxtSz += len;
  51722. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51723. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51724. /* modify tag*/
  51725. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  51726. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51727. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  51728. SM4_BLOCK_SIZE, tag));
  51729. /* fail due to wrong tag */
  51730. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51731. ciphertxtSz));
  51732. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51733. ExpectIntEQ(0, len);
  51734. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51735. }
  51736. res = EXPECT_RESULT();
  51737. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  51738. return res;
  51739. }
  51740. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  51741. {
  51742. int res = TEST_SKIPPED;
  51743. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  51744. /* Zero length plain text */
  51745. EXPECT_DECLS;
  51746. byte key[] = {
  51747. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51748. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51749. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51750. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51751. }; /* align */
  51752. byte iv[] = {
  51753. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51754. }; /* align */
  51755. byte plaintxt[1];
  51756. int ivSz = 12;
  51757. int plaintxtSz = 0;
  51758. unsigned char tag[16];
  51759. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51760. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51761. int ciphertxtSz = 0;
  51762. int decryptedtxtSz = 0;
  51763. int len = 0;
  51764. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  51765. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  51766. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  51767. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51768. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  51769. plaintxtSz));
  51770. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  51771. ciphertxtSz += len;
  51772. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  51773. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  51774. ExpectIntEQ(0, ciphertxtSz);
  51775. EVP_CIPHER_CTX_init(de);
  51776. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  51777. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51778. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  51779. decryptedtxtSz = len;
  51780. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  51781. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  51782. decryptedtxtSz += len;
  51783. ExpectIntEQ(0, decryptedtxtSz);
  51784. EVP_CIPHER_CTX_free(en);
  51785. EVP_CIPHER_CTX_free(de);
  51786. res = EXPECT_RESULT();
  51787. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  51788. return res;
  51789. }
  51790. static int test_wolfssl_EVP_sm4_ccm(void)
  51791. {
  51792. int res = TEST_SKIPPED;
  51793. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  51794. EXPECT_DECLS;
  51795. byte *key = (byte*)"0123456789012345";
  51796. byte *iv = (byte*)"0123456789012";
  51797. int ivSz = (int)XSTRLEN((char*)iv);
  51798. /* Message to be encrypted */
  51799. byte *plaintxt = (byte*)"for things to change you have to change";
  51800. /* Additional non-confidential data */
  51801. byte *aad = (byte*)"Don't spend major time on minor things.";
  51802. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  51803. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51804. int aadSz = (int)XSTRLEN((char*)aad);
  51805. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51806. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51807. int ciphertxtSz = 0;
  51808. int decryptedtxtSz = 0;
  51809. int len = 0;
  51810. int i = 0;
  51811. EVP_CIPHER_CTX en[2];
  51812. EVP_CIPHER_CTX de[2];
  51813. for (i = 0; i < 2; i++) {
  51814. EVP_CIPHER_CTX_init(&en[i]);
  51815. if (i == 0) {
  51816. /* Default uses 96-bits IV length */
  51817. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  51818. iv));
  51819. }
  51820. else {
  51821. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  51822. NULL));
  51823. /* non-default must to set the IV length first */
  51824. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  51825. ivSz, NULL));
  51826. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51827. }
  51828. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51829. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51830. plaintxtSz));
  51831. ciphertxtSz = len;
  51832. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51833. ciphertxtSz += len;
  51834. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  51835. SM4_BLOCK_SIZE, tag));
  51836. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51837. EVP_CIPHER_CTX_init(&de[i]);
  51838. if (i == 0) {
  51839. /* Default uses 96-bits IV length */
  51840. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  51841. iv));
  51842. }
  51843. else {
  51844. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  51845. NULL));
  51846. /* non-default must to set the IV length first */
  51847. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  51848. ivSz, NULL));
  51849. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51850. }
  51851. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51852. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51853. ciphertxtSz));
  51854. decryptedtxtSz = len;
  51855. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51856. SM4_BLOCK_SIZE, tag));
  51857. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51858. decryptedtxtSz += len;
  51859. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51860. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51861. /* modify tag*/
  51862. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  51863. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51864. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51865. SM4_BLOCK_SIZE, tag));
  51866. /* fail due to wrong tag */
  51867. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51868. ciphertxtSz));
  51869. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51870. ExpectIntEQ(0, len);
  51871. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51872. }
  51873. res = EXPECT_RESULT();
  51874. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  51875. return res;
  51876. }
  51877. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  51878. {
  51879. EXPECT_DECLS;
  51880. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  51881. EVP_PKEY_CTX* ctx = NULL;
  51882. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51883. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  51884. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  51885. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  51886. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  51887. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  51888. byte outKey[34];
  51889. size_t outKeySz = sizeof(outKey);
  51890. /* These expected outputs were gathered by running the same test below using
  51891. * OpenSSL. */
  51892. const byte extractAndExpand[] = {
  51893. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  51894. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  51895. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  51896. };
  51897. const byte extractOnly[] = {
  51898. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  51899. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  51900. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  51901. };
  51902. const byte expandOnly[] = {
  51903. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  51904. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  51905. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  51906. };
  51907. const byte extractAndExpandAddInfo[] = {
  51908. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  51909. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  51910. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  51911. };
  51912. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  51913. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  51914. /* NULL ctx. */
  51915. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  51916. /* NULL md. */
  51917. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  51918. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  51919. /* NULL ctx. */
  51920. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  51921. WOLFSSL_FAILURE);
  51922. /* NULL salt is ok. */
  51923. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  51924. WOLFSSL_SUCCESS);
  51925. /* Salt length <= 0. */
  51926. /* Length 0 salt is ok. */
  51927. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  51928. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  51929. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  51930. WOLFSSL_SUCCESS);
  51931. /* NULL ctx. */
  51932. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  51933. WOLFSSL_FAILURE);
  51934. /* NULL key. */
  51935. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  51936. WOLFSSL_FAILURE);
  51937. /* Key length <= 0 */
  51938. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  51939. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  51940. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  51941. WOLFSSL_SUCCESS);
  51942. /* NULL ctx. */
  51943. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  51944. WOLFSSL_FAILURE);
  51945. /* NULL info is ok. */
  51946. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  51947. WOLFSSL_SUCCESS);
  51948. /* Info length <= 0 */
  51949. /* Length 0 info is ok. */
  51950. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  51951. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  51952. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  51953. WOLFSSL_SUCCESS);
  51954. /* NULL ctx. */
  51955. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51956. WOLFSSL_FAILURE);
  51957. /* Extract and expand (default). */
  51958. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51959. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  51960. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  51961. /* Extract only. */
  51962. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51963. WOLFSSL_SUCCESS);
  51964. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51965. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  51966. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  51967. outKeySz = sizeof(outKey);
  51968. /* Expand only. */
  51969. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  51970. WOLFSSL_SUCCESS);
  51971. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51972. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  51973. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  51974. outKeySz = sizeof(outKey);
  51975. /* Extract and expand with appended additional info. */
  51976. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  51977. WOLFSSL_SUCCESS);
  51978. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  51979. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  51980. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51981. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  51982. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  51983. EVP_PKEY_CTX_free(ctx);
  51984. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  51985. return EXPECT_RESULT();
  51986. }
  51987. #ifndef NO_BIO
  51988. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  51989. {
  51990. EXPECT_DECLS;
  51991. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51992. BIO* bio = NULL;
  51993. X509_INFO* info = NULL;
  51994. STACK_OF(X509_INFO)* sk = NULL;
  51995. char* subject = NULL;
  51996. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  51997. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51998. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  51999. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  52000. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  52001. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  52002. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  52003. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  52004. /* using dereference to maintain testing for Apache port*/
  52005. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  52006. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  52007. 0, 0));
  52008. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  52009. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  52010. X509_INFO_free(info);
  52011. info = NULL;
  52012. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  52013. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  52014. 0, 0));
  52015. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  52016. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  52017. X509_INFO_free(info);
  52018. ExpectNull(info = sk_X509_INFO_pop(sk));
  52019. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  52020. BIO_free(bio);
  52021. #endif
  52022. return EXPECT_RESULT();
  52023. }
  52024. #endif /* !NO_BIO */
  52025. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  52026. {
  52027. EXPECT_DECLS;
  52028. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  52029. X509 *x509 = NULL;
  52030. X509_NAME* name = NULL;
  52031. int idx = 0;
  52032. X509_NAME_ENTRY *ne = NULL;
  52033. ASN1_OBJECT *object = NULL;
  52034. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  52035. WOLFSSL_FILETYPE_PEM));
  52036. ExpectNotNull(name = X509_get_subject_name(x509));
  52037. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  52038. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  52039. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  52040. X509_free(x509);
  52041. #endif
  52042. return EXPECT_RESULT();
  52043. }
  52044. static int test_wolfSSL_X509_STORE_get1_certs(void)
  52045. {
  52046. EXPECT_DECLS;
  52047. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  52048. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  52049. X509_STORE_CTX *storeCtx = NULL;
  52050. X509_STORE *store = NULL;
  52051. X509 *caX509 = NULL;
  52052. X509 *svrX509 = NULL;
  52053. X509_NAME *subject = NULL;
  52054. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  52055. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  52056. SSL_FILETYPE_PEM));
  52057. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  52058. SSL_FILETYPE_PEM)));
  52059. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  52060. ExpectNotNull(store = X509_STORE_new());
  52061. ExpectNotNull(subject = X509_get_subject_name(caX509));
  52062. /* Errors */
  52063. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  52064. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  52065. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  52066. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  52067. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  52068. SSL_SUCCESS);
  52069. /* Should find the cert */
  52070. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  52071. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  52072. sk_X509_pop_free(certs, NULL);
  52073. certs = NULL;
  52074. /* Should not find the cert */
  52075. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  52076. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  52077. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  52078. sk_X509_pop_free(certs, NULL);
  52079. certs = NULL;
  52080. X509_STORE_free(store);
  52081. X509_STORE_CTX_free(storeCtx);
  52082. X509_free(svrX509);
  52083. X509_free(caX509);
  52084. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  52085. return EXPECT_RESULT();
  52086. }
  52087. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  52088. defined(WOLFSSL_LOCAL_X509_STORE) && \
  52089. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  52090. static int test_wolfSSL_X509_STORE_set_get_crl_provider(X509_STORE_CTX* ctx,
  52091. X509_CRL** crl_out, X509* cert) {
  52092. X509_CRL *crl = NULL;
  52093. XFILE fp = XBADFILE;
  52094. char* cert_issuer = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
  52095. int ret = 0;
  52096. (void)ctx;
  52097. if (cert_issuer == NULL)
  52098. return 0;
  52099. if ((fp = XFOPEN("certs/crl/crl.pem", "rb")) != XBADFILE) {
  52100. PEM_read_X509_CRL(fp, &crl, NULL, NULL);
  52101. XFCLOSE(fp);
  52102. if (crl != NULL) {
  52103. char* crl_issuer = X509_NAME_oneline(
  52104. X509_CRL_get_issuer(crl), NULL, 0);
  52105. if (XSTRCMP(cert_issuer, crl_issuer) == 0) {
  52106. *crl_out = X509_CRL_dup(crl);
  52107. if (*crl_out != NULL)
  52108. ret = 1;
  52109. }
  52110. OPENSSL_free(crl_issuer);
  52111. }
  52112. }
  52113. X509_CRL_free(crl);
  52114. OPENSSL_free(cert_issuer);
  52115. return ret;
  52116. }
  52117. static int test_wolfSSL_X509_STORE_set_get_crl_provider2(X509_STORE_CTX* ctx,
  52118. X509_CRL** crl_out, X509* cert) {
  52119. (void)ctx;
  52120. (void)cert;
  52121. *crl_out = NULL;
  52122. return 1;
  52123. }
  52124. #ifndef NO_WOLFSSL_STUB
  52125. static int test_wolfSSL_X509_STORE_set_get_crl_check(X509_STORE_CTX* ctx,
  52126. X509_CRL* crl) {
  52127. (void)ctx;
  52128. (void)crl;
  52129. return 1;
  52130. }
  52131. #endif
  52132. static int test_wolfSSL_X509_STORE_set_get_crl_verify(int ok,
  52133. X509_STORE_CTX* ctx) {
  52134. int cert_error = X509_STORE_CTX_get_error(ctx);
  52135. X509_VERIFY_PARAM* param = X509_STORE_CTX_get0_param(ctx);
  52136. int flags = X509_VERIFY_PARAM_get_flags(param);
  52137. if ((flags & (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) !=
  52138. (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) {
  52139. /* Make sure the flags are set */
  52140. return 0;
  52141. }
  52142. /* Ignore CRL missing error */
  52143. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  52144. if (cert_error == CRL_MISSING)
  52145. #else
  52146. if (cert_error == X509_V_ERR_UNABLE_TO_GET_CRL)
  52147. #endif
  52148. return 1;
  52149. return ok;
  52150. }
  52151. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready(WOLFSSL_CTX* ctx)
  52152. {
  52153. EXPECT_DECLS;
  52154. X509_STORE* cert_store = NULL;
  52155. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  52156. WOLFSSL_SUCCESS);
  52157. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  52158. X509_STORE_set_get_crl(cert_store,
  52159. test_wolfSSL_X509_STORE_set_get_crl_provider);
  52160. #ifndef NO_WOLFSSL_STUB
  52161. X509_STORE_set_check_crl(cert_store,
  52162. test_wolfSSL_X509_STORE_set_get_crl_check);
  52163. #endif
  52164. return EXPECT_RESULT();
  52165. }
  52166. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2(WOLFSSL_CTX* ctx)
  52167. {
  52168. EXPECT_DECLS;
  52169. X509_STORE* cert_store = NULL;
  52170. X509_VERIFY_PARAM* param = NULL;
  52171. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  52172. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  52173. WOLFSSL_SUCCESS);
  52174. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  52175. X509_STORE_set_get_crl(cert_store,
  52176. test_wolfSSL_X509_STORE_set_get_crl_provider2);
  52177. #ifndef NO_WOLFSSL_STUB
  52178. X509_STORE_set_check_crl(cert_store,
  52179. test_wolfSSL_X509_STORE_set_get_crl_check);
  52180. #endif
  52181. X509_STORE_set_verify_cb(cert_store,
  52182. test_wolfSSL_X509_STORE_set_get_crl_verify);
  52183. ExpectNotNull(param = X509_STORE_get0_param(cert_store));
  52184. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(
  52185. param, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  52186. ExpectIntEQ(X509_STORE_set_flags(cert_store,
  52187. X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  52188. return EXPECT_RESULT();
  52189. }
  52190. #endif
  52191. /* This test mimics the usage of the CRL provider in gRPC */
  52192. static int test_wolfSSL_X509_STORE_set_get_crl(void)
  52193. {
  52194. EXPECT_DECLS;
  52195. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  52196. defined(WOLFSSL_LOCAL_X509_STORE) && \
  52197. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  52198. test_ssl_cbf func_cb_client;
  52199. test_ssl_cbf func_cb_server;
  52200. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  52201. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  52202. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready;
  52203. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  52204. &func_cb_server, NULL), TEST_SUCCESS);
  52205. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  52206. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  52207. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2;
  52208. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  52209. &func_cb_server, NULL), TEST_SUCCESS);
  52210. #endif
  52211. return EXPECT_RESULT();
  52212. }
  52213. static int test_wolfSSL_dup_CA_list(void)
  52214. {
  52215. int res = TEST_SKIPPED;
  52216. #if defined(OPENSSL_ALL)
  52217. EXPECT_DECLS;
  52218. STACK_OF(X509_NAME) *originalStack = NULL;
  52219. STACK_OF(X509_NAME) *copyStack = NULL;
  52220. int originalCount = 0;
  52221. int copyCount = 0;
  52222. X509_NAME *name = NULL;
  52223. int i;
  52224. originalStack = sk_X509_NAME_new_null();
  52225. ExpectNotNull(originalStack);
  52226. for (i = 0; i < 3; i++) {
  52227. name = X509_NAME_new();
  52228. ExpectNotNull(name);
  52229. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  52230. }
  52231. copyStack = SSL_dup_CA_list(originalStack);
  52232. ExpectNotNull(copyStack);
  52233. originalCount = sk_X509_NAME_num(originalStack);
  52234. copyCount = sk_X509_NAME_num(copyStack);
  52235. AssertIntEQ(originalCount, copyCount);
  52236. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  52237. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  52238. originalStack = NULL;
  52239. copyStack = NULL;
  52240. res = EXPECT_RESULT();
  52241. #endif /* OPENSSL_ALL */
  52242. return res;
  52243. }
  52244. static int test_ForceZero(void)
  52245. {
  52246. EXPECT_DECLS;
  52247. unsigned char data[32];
  52248. unsigned int i, j, len;
  52249. /* Test case with 0 length */
  52250. ForceZero(data, 0);
  52251. /* Test ForceZero */
  52252. for (i = 0; i < sizeof(data); i++) {
  52253. for (len = 1; len < sizeof(data) - i; len++) {
  52254. for (j = 0; j < sizeof(data); j++)
  52255. data[j] = j + 1;
  52256. ForceZero(data + i, len);
  52257. for (j = 0; j < sizeof(data); j++) {
  52258. if (j < i || j >= i + len) {
  52259. ExpectIntNE(data[j], 0x00);
  52260. }
  52261. else {
  52262. ExpectIntEQ(data[j], 0x00);
  52263. }
  52264. }
  52265. }
  52266. }
  52267. return EXPECT_RESULT();
  52268. }
  52269. #ifndef NO_BIO
  52270. static int test_wolfSSL_X509_print(void)
  52271. {
  52272. EXPECT_DECLS;
  52273. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52274. !defined(NO_RSA) && defined(XSNPRINTF)
  52275. X509 *x509 = NULL;
  52276. BIO *bio = NULL;
  52277. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  52278. const X509_ALGOR *cert_sig_alg = NULL;
  52279. #endif
  52280. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  52281. WOLFSSL_FILETYPE_PEM));
  52282. /* print to memory */
  52283. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52284. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  52285. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  52286. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  52287. /* Will print IP address subject alt name. */
  52288. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  52289. #elif defined(NO_ASN_TIME)
  52290. /* Will print IP address subject alt name but not Validity. */
  52291. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  52292. #else
  52293. /* Will print IP address subject alt name. */
  52294. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  52295. #endif
  52296. #elif defined(NO_ASN_TIME)
  52297. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  52298. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  52299. #else
  52300. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  52301. #endif
  52302. BIO_free(bio);
  52303. bio = NULL;
  52304. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  52305. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  52306. /* Print signature */
  52307. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  52308. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  52309. #endif
  52310. /* print to stderr */
  52311. #if !defined(NO_WOLFSSL_DIR)
  52312. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  52313. #endif
  52314. /* print again */
  52315. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  52316. X509_free(x509);
  52317. BIO_free(bio);
  52318. #endif
  52319. return EXPECT_RESULT();
  52320. }
  52321. static int test_wolfSSL_X509_CRL_print(void)
  52322. {
  52323. EXPECT_DECLS;
  52324. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  52325. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  52326. X509_CRL* crl = NULL;
  52327. BIO *bio = NULL;
  52328. XFILE fp = XBADFILE;
  52329. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  52330. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  52331. NULL, NULL));
  52332. if (fp != XBADFILE)
  52333. XFCLOSE(fp);
  52334. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52335. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  52336. X509_CRL_free(crl);
  52337. BIO_free(bio);
  52338. #endif
  52339. return EXPECT_RESULT();
  52340. }
  52341. static int test_wolfSSL_BIO_get_len(void)
  52342. {
  52343. EXPECT_DECLS;
  52344. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52345. BIO *bio = NULL;
  52346. const char txt[] = "Some example text to push to the BIO.";
  52347. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  52348. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  52349. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  52350. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  52351. BIO_free(bio);
  52352. bio = NULL;
  52353. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  52354. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  52355. BIO_free(bio);
  52356. #endif
  52357. return EXPECT_RESULT();
  52358. }
  52359. #endif /* !NO_BIO */
  52360. static int test_wolfSSL_RSA(void)
  52361. {
  52362. EXPECT_DECLS;
  52363. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  52364. RSA* rsa = NULL;
  52365. const BIGNUM *n = NULL;
  52366. const BIGNUM *e = NULL;
  52367. const BIGNUM *d = NULL;
  52368. const BIGNUM *p = NULL;
  52369. const BIGNUM *q = NULL;
  52370. const BIGNUM *dmp1 = NULL;
  52371. const BIGNUM *dmq1 = NULL;
  52372. const BIGNUM *iqmp = NULL;
  52373. ExpectNotNull(rsa = RSA_new());
  52374. ExpectIntEQ(RSA_size(NULL), 0);
  52375. ExpectIntEQ(RSA_size(rsa), 0);
  52376. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  52377. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  52378. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  52379. #ifdef WOLFSSL_RSA_KEY_CHECK
  52380. ExpectIntEQ(RSA_check_key(rsa), 0);
  52381. #endif
  52382. RSA_free(rsa);
  52383. rsa = NULL;
  52384. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52385. ExpectIntEQ(RSA_size(rsa), 256);
  52386. #ifdef WOLFSSL_RSA_KEY_CHECK
  52387. ExpectIntEQ(RSA_check_key(NULL), 0);
  52388. ExpectIntEQ(RSA_check_key(rsa), 1);
  52389. #endif
  52390. /* sanity check */
  52391. ExpectIntEQ(RSA_bits(NULL), 0);
  52392. /* key */
  52393. ExpectIntEQ(RSA_bits(rsa), 2048);
  52394. RSA_get0_key(rsa, &n, &e, &d);
  52395. ExpectPtrEq(rsa->n, n);
  52396. ExpectPtrEq(rsa->e, e);
  52397. ExpectPtrEq(rsa->d, d);
  52398. n = NULL;
  52399. e = NULL;
  52400. d = NULL;
  52401. ExpectNotNull(n = BN_new());
  52402. ExpectNotNull(e = BN_new());
  52403. ExpectNotNull(d = BN_new());
  52404. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  52405. if (EXPECT_FAIL()) {
  52406. BN_free((BIGNUM*)n);
  52407. BN_free((BIGNUM*)e);
  52408. BN_free((BIGNUM*)d);
  52409. }
  52410. ExpectPtrEq(rsa->n, n);
  52411. ExpectPtrEq(rsa->e, e);
  52412. ExpectPtrEq(rsa->d, d);
  52413. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  52414. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  52415. /* crt_params */
  52416. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  52417. ExpectPtrEq(rsa->dmp1, dmp1);
  52418. ExpectPtrEq(rsa->dmq1, dmq1);
  52419. ExpectPtrEq(rsa->iqmp, iqmp);
  52420. dmp1 = NULL;
  52421. dmq1 = NULL;
  52422. iqmp = NULL;
  52423. ExpectNotNull(dmp1 = BN_new());
  52424. ExpectNotNull(dmq1 = BN_new());
  52425. ExpectNotNull(iqmp = BN_new());
  52426. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  52427. (BIGNUM*)iqmp), 1);
  52428. if (EXPECT_FAIL()) {
  52429. BN_free((BIGNUM*)dmp1);
  52430. BN_free((BIGNUM*)dmq1);
  52431. BN_free((BIGNUM*)iqmp);
  52432. }
  52433. ExpectPtrEq(rsa->dmp1, dmp1);
  52434. ExpectPtrEq(rsa->dmq1, dmq1);
  52435. ExpectPtrEq(rsa->iqmp, iqmp);
  52436. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  52437. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  52438. (BIGNUM*)iqmp), 0);
  52439. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  52440. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  52441. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  52442. ExpectNull(dmp1);
  52443. ExpectNull(dmq1);
  52444. ExpectNull(iqmp);
  52445. /* factors */
  52446. RSA_get0_factors(rsa, NULL, NULL);
  52447. RSA_get0_factors(rsa, &p, &q);
  52448. ExpectPtrEq(rsa->p, p);
  52449. ExpectPtrEq(rsa->q, q);
  52450. p = NULL;
  52451. q = NULL;
  52452. ExpectNotNull(p = BN_new());
  52453. ExpectNotNull(q = BN_new());
  52454. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  52455. if (EXPECT_FAIL()) {
  52456. BN_free((BIGNUM*)p);
  52457. BN_free((BIGNUM*)q);
  52458. }
  52459. ExpectPtrEq(rsa->p, p);
  52460. ExpectPtrEq(rsa->q, q);
  52461. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  52462. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  52463. RSA_get0_factors(NULL, NULL, NULL);
  52464. RSA_get0_factors(NULL, &p, &q);
  52465. ExpectNull(p);
  52466. ExpectNull(q);
  52467. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  52468. ExpectIntEQ(RSA_bits(rsa), 21);
  52469. RSA_free(rsa);
  52470. rsa = NULL;
  52471. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  52472. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  52473. ExpectIntEQ(RSA_size(rsa), 384);
  52474. ExpectIntEQ(RSA_bits(rsa), 3072);
  52475. RSA_free(rsa);
  52476. rsa = NULL;
  52477. #endif
  52478. /* remove for now with odd key size until adjusting rsa key size check with
  52479. wc_MakeRsaKey()
  52480. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  52481. RSA_free(rsa);
  52482. rsa = NULL;
  52483. */
  52484. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  52485. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  52486. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  52487. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  52488. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  52489. {
  52490. byte buff[FOURK_BUF];
  52491. byte der[FOURK_BUF];
  52492. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  52493. XFILE f = XBADFILE;
  52494. int bytes = 0;
  52495. /* test loading encrypted RSA private pem w/o password */
  52496. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  52497. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  52498. if (f != XBADFILE)
  52499. XFCLOSE(f);
  52500. XMEMSET(der, 0, sizeof(der));
  52501. /* test that error value is returned with no password */
  52502. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  52503. 0);
  52504. }
  52505. #endif
  52506. #endif
  52507. return EXPECT_RESULT();
  52508. }
  52509. static int test_wolfSSL_RSA_DER(void)
  52510. {
  52511. EXPECT_DECLS;
  52512. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  52513. RSA *rsa = NULL;
  52514. int i;
  52515. const unsigned char *buff = NULL;
  52516. unsigned char *newBuff = NULL;
  52517. struct tbl_s
  52518. {
  52519. const unsigned char *der;
  52520. int sz;
  52521. } tbl[] = {
  52522. #ifdef USE_CERT_BUFFERS_1024
  52523. {client_key_der_1024, sizeof_client_key_der_1024},
  52524. {server_key_der_1024, sizeof_server_key_der_1024},
  52525. #endif
  52526. #ifdef USE_CERT_BUFFERS_2048
  52527. {client_key_der_2048, sizeof_client_key_der_2048},
  52528. {server_key_der_2048, sizeof_server_key_der_2048},
  52529. #endif
  52530. {NULL, 0}
  52531. };
  52532. /* Public Key DER */
  52533. struct tbl_s pub[] = {
  52534. #ifdef USE_CERT_BUFFERS_1024
  52535. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  52536. #endif
  52537. #ifdef USE_CERT_BUFFERS_2048
  52538. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  52539. #endif
  52540. {NULL, 0}
  52541. };
  52542. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  52543. buff = pub[0].der;
  52544. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  52545. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  52546. buff = tbl[0].der;
  52547. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  52548. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  52549. rsa = RSA_new();
  52550. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  52551. RSA_free(rsa);
  52552. rsa = NULL;
  52553. for (i = 0; tbl[i].der != NULL; i++)
  52554. {
  52555. /* Passing in pointer results in pointer moving. */
  52556. buff = tbl[i].der;
  52557. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  52558. ExpectNotNull(rsa);
  52559. RSA_free(rsa);
  52560. rsa = NULL;
  52561. }
  52562. for (i = 0; tbl[i].der != NULL; i++)
  52563. {
  52564. /* Passing in pointer results in pointer moving. */
  52565. buff = tbl[i].der;
  52566. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  52567. ExpectNotNull(rsa);
  52568. RSA_free(rsa);
  52569. rsa = NULL;
  52570. }
  52571. for (i = 0; pub[i].der != NULL; i++)
  52572. {
  52573. buff = pub[i].der;
  52574. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  52575. ExpectNotNull(rsa);
  52576. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  52577. newBuff = NULL;
  52578. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  52579. ExpectNotNull(newBuff);
  52580. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  52581. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52582. RSA_free(rsa);
  52583. rsa = NULL;
  52584. }
  52585. #endif
  52586. return EXPECT_RESULT();
  52587. }
  52588. static int test_wolfSSL_RSA_print(void)
  52589. {
  52590. EXPECT_DECLS;
  52591. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52592. !defined(NO_STDIO_FILESYSTEM) && \
  52593. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  52594. !defined(NO_BIO) && defined(XFPRINTF)
  52595. BIO *bio = NULL;
  52596. WOLFSSL_RSA* rsa = NULL;
  52597. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  52598. ExpectNotNull(rsa = RSA_new());
  52599. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  52600. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  52601. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  52602. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  52603. /* Some very large number of indent spaces. */
  52604. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  52605. /* RSA is empty. */
  52606. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  52607. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  52608. RSA_free(rsa);
  52609. rsa = NULL;
  52610. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52611. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  52612. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  52613. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  52614. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  52615. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  52616. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  52617. BIO_free(bio);
  52618. RSA_free(rsa);
  52619. #endif
  52620. return EXPECT_RESULT();
  52621. }
  52622. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  52623. {
  52624. EXPECT_DECLS;
  52625. #ifndef NO_RSA
  52626. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  52627. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52628. RSA *rsa = NULL;
  52629. const unsigned char *derBuf = client_key_der_2048;
  52630. unsigned char em[256] = {0}; /* len = 2048/8 */
  52631. /* Random data simulating a hash */
  52632. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  52633. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  52634. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  52635. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  52636. };
  52637. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  52638. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  52639. RSA_PSS_SALTLEN_DIGEST), 0);
  52640. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  52641. RSA_PSS_SALTLEN_DIGEST), 0);
  52642. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  52643. RSA_PSS_SALTLEN_DIGEST), 0);
  52644. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  52645. RSA_PSS_SALTLEN_DIGEST), 0);
  52646. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  52647. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  52648. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52649. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  52650. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52651. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  52652. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52653. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  52654. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52655. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52656. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52657. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  52658. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52659. RSA_PSS_SALTLEN_DIGEST), 1);
  52660. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52661. RSA_PSS_SALTLEN_DIGEST), 1);
  52662. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52663. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  52664. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52665. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  52666. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52667. RSA_PSS_SALTLEN_MAX), 1);
  52668. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52669. RSA_PSS_SALTLEN_MAX), 1);
  52670. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  52671. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  52672. RSA_free(rsa);
  52673. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52674. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  52675. #endif
  52676. return EXPECT_RESULT();
  52677. }
  52678. static int test_wolfSSL_RSA_sign_sha3(void)
  52679. {
  52680. EXPECT_DECLS;
  52681. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  52682. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  52683. RSA* rsa = NULL;
  52684. const unsigned char *derBuf = client_key_der_2048;
  52685. unsigned char sigRet[256] = {0};
  52686. unsigned int sigLen = sizeof(sigRet);
  52687. /* Random data simulating a hash */
  52688. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  52689. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  52690. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  52691. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  52692. };
  52693. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  52694. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  52695. rsa), 1);
  52696. RSA_free(rsa);
  52697. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  52698. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  52699. return EXPECT_RESULT();
  52700. }
  52701. static int test_wolfSSL_RSA_get0_key(void)
  52702. {
  52703. EXPECT_DECLS;
  52704. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52705. RSA *rsa = NULL;
  52706. const BIGNUM* n = NULL;
  52707. const BIGNUM* e = NULL;
  52708. const BIGNUM* d = NULL;
  52709. const unsigned char* der;
  52710. int derSz;
  52711. #ifdef USE_CERT_BUFFERS_1024
  52712. der = client_key_der_1024;
  52713. derSz = sizeof_client_key_der_1024;
  52714. #elif defined(USE_CERT_BUFFERS_2048)
  52715. der = client_key_der_2048;
  52716. derSz = sizeof_client_key_der_2048;
  52717. #else
  52718. der = NULL;
  52719. derSz = 0;
  52720. #endif
  52721. if (der != NULL) {
  52722. RSA_get0_key(NULL, NULL, NULL, NULL);
  52723. RSA_get0_key(rsa, NULL, NULL, NULL);
  52724. RSA_get0_key(NULL, &n, &e, &d);
  52725. ExpectNull(n);
  52726. ExpectNull(e);
  52727. ExpectNull(d);
  52728. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  52729. ExpectNotNull(rsa);
  52730. RSA_get0_key(rsa, NULL, NULL, NULL);
  52731. RSA_get0_key(rsa, &n, NULL, NULL);
  52732. ExpectNotNull(n);
  52733. RSA_get0_key(rsa, NULL, &e, NULL);
  52734. ExpectNotNull(e);
  52735. RSA_get0_key(rsa, NULL, NULL, &d);
  52736. ExpectNotNull(d);
  52737. RSA_get0_key(rsa, &n, &e, &d);
  52738. ExpectNotNull(n);
  52739. ExpectNotNull(e);
  52740. ExpectNotNull(d);
  52741. RSA_free(rsa);
  52742. }
  52743. #endif
  52744. return EXPECT_RESULT();
  52745. }
  52746. static int test_wolfSSL_RSA_meth(void)
  52747. {
  52748. EXPECT_DECLS;
  52749. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52750. RSA *rsa = NULL;
  52751. RSA_METHOD *rsa_meth = NULL;
  52752. #ifdef WOLFSSL_KEY_GEN
  52753. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52754. RSA_free(rsa);
  52755. rsa = NULL;
  52756. #else
  52757. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52758. #endif
  52759. ExpectNotNull(RSA_get_default_method());
  52760. wolfSSL_RSA_meth_free(NULL);
  52761. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  52762. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  52763. RSA_METHOD_FLAG_NO_CHECK));
  52764. #ifndef NO_WOLFSSL_STUB
  52765. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  52766. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  52767. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  52768. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  52769. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  52770. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  52771. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  52772. #endif
  52773. ExpectIntEQ(RSA_flags(NULL), 0);
  52774. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  52775. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  52776. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  52777. ExpectNotNull(rsa = RSA_new());
  52778. /* No method set. */
  52779. ExpectIntEQ(RSA_flags(rsa), 0);
  52780. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52781. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52782. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52783. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  52784. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  52785. if (EXPECT_FAIL()) {
  52786. wolfSSL_RSA_meth_free(rsa_meth);
  52787. }
  52788. ExpectNull(RSA_get_method(NULL));
  52789. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  52790. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  52791. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52792. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52793. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  52794. RSA_METHOD_FLAG_NO_CHECK);
  52795. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52796. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52797. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  52798. /* rsa_meth is freed here */
  52799. RSA_free(rsa);
  52800. #endif
  52801. return EXPECT_RESULT();
  52802. }
  52803. static int test_wolfSSL_RSA_verify(void)
  52804. {
  52805. EXPECT_DECLS;
  52806. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  52807. #ifndef NO_BIO
  52808. XFILE fp = XBADFILE;
  52809. RSA *pKey = NULL;
  52810. RSA *pubKey = NULL;
  52811. X509 *cert = NULL;
  52812. const char *text = "Hello wolfSSL !";
  52813. unsigned char hash[SHA256_DIGEST_LENGTH];
  52814. unsigned char signature[2048/8];
  52815. unsigned int signatureLength;
  52816. byte *buf = NULL;
  52817. BIO *bio = NULL;
  52818. SHA256_CTX c;
  52819. EVP_PKEY *evpPkey = NULL;
  52820. EVP_PKEY *evpPubkey = NULL;
  52821. size_t sz;
  52822. /* generate hash */
  52823. SHA256_Init(&c);
  52824. SHA256_Update(&c, text, strlen(text));
  52825. SHA256_Final(hash, &c);
  52826. #ifdef WOLFSSL_SMALL_STACK_CACHE
  52827. /* workaround for small stack cache case */
  52828. wc_Sha256Free((wc_Sha256*)&c);
  52829. #endif
  52830. /* read privete key file */
  52831. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  52832. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  52833. ExpectTrue((sz = XFTELL(fp)) > 0);
  52834. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  52835. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  52836. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  52837. if (fp != XBADFILE) {
  52838. XFCLOSE(fp);
  52839. fp = XBADFILE;
  52840. }
  52841. /* read private key and sign hash data */
  52842. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  52843. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  52844. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  52845. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  52846. signature, &signatureLength, pKey), SSL_SUCCESS);
  52847. /* read public key and verify signed data */
  52848. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  52849. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  52850. if (fp != XBADFILE)
  52851. XFCLOSE(fp);
  52852. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  52853. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  52854. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  52855. signatureLength, pubKey), SSL_SUCCESS);
  52856. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  52857. signatureLength, NULL), SSL_FAILURE);
  52858. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  52859. signatureLength, pubKey), SSL_FAILURE);
  52860. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  52861. signatureLength, pubKey), SSL_FAILURE);
  52862. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  52863. signatureLength, NULL), SSL_FAILURE);
  52864. RSA_free(pKey);
  52865. EVP_PKEY_free(evpPkey);
  52866. RSA_free(pubKey);
  52867. EVP_PKEY_free(evpPubkey);
  52868. X509_free(cert);
  52869. BIO_free(bio);
  52870. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  52871. #endif
  52872. #endif
  52873. return EXPECT_RESULT();
  52874. }
  52875. static int test_wolfSSL_RSA_sign(void)
  52876. {
  52877. EXPECT_DECLS;
  52878. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52879. RSA *rsa;
  52880. unsigned char hash[SHA256_DIGEST_LENGTH];
  52881. #ifdef USE_CERT_BUFFERS_1024
  52882. const unsigned char* privDer = client_key_der_1024;
  52883. size_t privDerSz = sizeof_client_key_der_1024;
  52884. const unsigned char* pubDer = client_keypub_der_1024;
  52885. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52886. unsigned char signature[1024/8];
  52887. #else
  52888. const unsigned char* privDer = client_key_der_2048;
  52889. size_t privDerSz = sizeof_client_key_der_2048;
  52890. const unsigned char* pubDer = client_keypub_der_2048;
  52891. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52892. unsigned char signature[2048/8];
  52893. #endif
  52894. unsigned int signatureLen;
  52895. const unsigned char* der;
  52896. XMEMSET(hash, 0, sizeof(hash));
  52897. der = privDer;
  52898. rsa = NULL;
  52899. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52900. /* Invalid parameters. */
  52901. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  52902. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  52903. &signatureLen, rsa), 0);
  52904. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  52905. &signatureLen, rsa), 0);
  52906. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  52907. &signatureLen, rsa), 0);
  52908. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52909. NULL, rsa), 0);
  52910. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52911. &signatureLen, NULL), 0);
  52912. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52913. &signatureLen, rsa), 1);
  52914. RSA_free(rsa);
  52915. der = pubDer;
  52916. rsa = NULL;
  52917. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52918. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  52919. signatureLen, rsa), 1);
  52920. RSA_free(rsa);
  52921. #endif
  52922. return EXPECT_RESULT();
  52923. }
  52924. static int test_wolfSSL_RSA_sign_ex(void)
  52925. {
  52926. EXPECT_DECLS;
  52927. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52928. RSA *rsa = NULL;
  52929. unsigned char hash[SHA256_DIGEST_LENGTH];
  52930. #ifdef USE_CERT_BUFFERS_1024
  52931. const unsigned char* privDer = client_key_der_1024;
  52932. size_t privDerSz = sizeof_client_key_der_1024;
  52933. const unsigned char* pubDer = client_keypub_der_1024;
  52934. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52935. unsigned char signature[1024/8];
  52936. #else
  52937. const unsigned char* privDer = client_key_der_2048;
  52938. size_t privDerSz = sizeof_client_key_der_2048;
  52939. const unsigned char* pubDer = client_keypub_der_2048;
  52940. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52941. unsigned char signature[2048/8];
  52942. #endif
  52943. unsigned int signatureLen;
  52944. const unsigned char* der;
  52945. unsigned char encodedHash[51];
  52946. unsigned int encodedHashLen;
  52947. const unsigned char expEncHash[] = {
  52948. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  52949. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  52950. 0x00, 0x04, 0x20,
  52951. /* Hash data */
  52952. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52953. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52954. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52955. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52956. };
  52957. XMEMSET(hash, 0, sizeof(hash));
  52958. ExpectNotNull(rsa = wolfSSL_RSA_new());
  52959. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52960. &signatureLen, rsa, 1), 0);
  52961. wolfSSL_RSA_free(rsa);
  52962. der = privDer;
  52963. rsa = NULL;
  52964. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52965. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  52966. -1), 0);
  52967. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  52968. signature, &signatureLen, rsa, 1), 0);
  52969. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52970. &signatureLen, rsa, 1), 0);
  52971. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52972. &signatureLen, rsa, 1), 0);
  52973. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52974. NULL, rsa, 1), 0);
  52975. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52976. &signatureLen, NULL, 1), 0);
  52977. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52978. &signatureLen, rsa, -1), 0);
  52979. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52980. &signatureLen, rsa, 0), 0);
  52981. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52982. &signatureLen, rsa, 0), 0);
  52983. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52984. NULL, rsa, 0), 0);
  52985. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52986. &signatureLen, rsa, 1), 1);
  52987. /* Test returning encoded hash. */
  52988. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  52989. &encodedHashLen, rsa, 0), 1);
  52990. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  52991. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  52992. RSA_free(rsa);
  52993. der = pubDer;
  52994. rsa = NULL;
  52995. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52996. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  52997. signatureLen, rsa), 1);
  52998. RSA_free(rsa);
  52999. #endif
  53000. return EXPECT_RESULT();
  53001. }
  53002. static int test_wolfSSL_RSA_public_decrypt(void)
  53003. {
  53004. EXPECT_DECLS;
  53005. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53006. RSA *rsa;
  53007. unsigned char msg[SHA256_DIGEST_LENGTH];
  53008. #ifdef USE_CERT_BUFFERS_1024
  53009. const unsigned char* pubDer = client_keypub_der_1024;
  53010. size_t pubDerSz = sizeof_client_keypub_der_1024;
  53011. unsigned char decMsg[1024/8];
  53012. const unsigned char encMsg[] = {
  53013. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  53014. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  53015. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  53016. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  53017. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  53018. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  53019. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  53020. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  53021. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  53022. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  53023. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  53024. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  53025. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  53026. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  53027. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  53028. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  53029. };
  53030. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  53031. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  53032. defined(WC_RSA_NO_PADDING)
  53033. const unsigned char encMsgNoPad[] = {
  53034. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  53035. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  53036. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  53037. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  53038. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  53039. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  53040. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  53041. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  53042. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  53043. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  53044. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  53045. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  53046. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  53047. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  53048. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  53049. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  53050. };
  53051. #endif
  53052. #else
  53053. const unsigned char* pubDer = client_keypub_der_2048;
  53054. size_t pubDerSz = sizeof_client_keypub_der_2048;
  53055. unsigned char decMsg[2048/8];
  53056. const unsigned char encMsg[] = {
  53057. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  53058. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  53059. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  53060. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  53061. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  53062. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  53063. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  53064. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  53065. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  53066. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  53067. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  53068. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  53069. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  53070. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  53071. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  53072. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  53073. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  53074. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  53075. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  53076. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  53077. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  53078. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  53079. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  53080. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  53081. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  53082. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  53083. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  53084. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  53085. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  53086. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  53087. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  53088. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  53089. };
  53090. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  53091. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  53092. defined(WC_RSA_NO_PADDING)
  53093. const unsigned char encMsgNoPad[] = {
  53094. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  53095. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  53096. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  53097. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  53098. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  53099. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  53100. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  53101. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  53102. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  53103. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  53104. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  53105. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  53106. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  53107. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  53108. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  53109. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  53110. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  53111. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  53112. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  53113. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  53114. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  53115. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  53116. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  53117. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  53118. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  53119. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  53120. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  53121. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  53122. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  53123. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  53124. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  53125. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  53126. };
  53127. #endif
  53128. #endif
  53129. const unsigned char* der;
  53130. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  53131. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  53132. defined(WC_RSA_NO_PADDING)
  53133. int i;
  53134. #endif
  53135. XMEMSET(msg, 0, sizeof(msg));
  53136. der = pubDer;
  53137. rsa = NULL;
  53138. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  53139. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  53140. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  53141. RSA_PKCS1_PADDING), -1);
  53142. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  53143. RSA_PKCS1_PADDING), -1);
  53144. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  53145. RSA_PKCS1_PADDING), -1);
  53146. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  53147. RSA_PKCS1_PADDING), -1);
  53148. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  53149. RSA_PKCS1_PSS_PADDING), -1);
  53150. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  53151. RSA_PKCS1_PADDING), 32);
  53152. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  53153. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  53154. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  53155. defined(WC_RSA_NO_PADDING)
  53156. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  53157. rsa, RSA_NO_PADDING), sizeof(decMsg));
  53158. /* Zeros before actual data. */
  53159. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  53160. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  53161. }
  53162. /* Check actual data. */
  53163. XMEMSET(msg, 0x01, sizeof(msg));
  53164. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  53165. #endif
  53166. RSA_free(rsa);
  53167. #endif
  53168. return EXPECT_RESULT();
  53169. }
  53170. static int test_wolfSSL_RSA_private_encrypt(void)
  53171. {
  53172. EXPECT_DECLS;
  53173. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53174. RSA *rsa;
  53175. unsigned char msg[SHA256_DIGEST_LENGTH];
  53176. #ifdef USE_CERT_BUFFERS_1024
  53177. const unsigned char* privDer = client_key_der_1024;
  53178. size_t privDerSz = sizeof_client_key_der_1024;
  53179. unsigned char encMsg[1024/8];
  53180. const unsigned char expEncMsg[] = {
  53181. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  53182. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  53183. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  53184. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  53185. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  53186. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  53187. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  53188. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  53189. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  53190. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  53191. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  53192. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  53193. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  53194. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  53195. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  53196. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  53197. };
  53198. #ifdef WC_RSA_NO_PADDING
  53199. const unsigned char expEncMsgNoPad[] = {
  53200. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  53201. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  53202. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  53203. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  53204. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  53205. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  53206. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  53207. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  53208. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  53209. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  53210. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  53211. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  53212. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  53213. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  53214. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  53215. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  53216. };
  53217. #endif
  53218. #else
  53219. const unsigned char* privDer = client_key_der_2048;
  53220. size_t privDerSz = sizeof_client_key_der_2048;
  53221. unsigned char encMsg[2048/8];
  53222. const unsigned char expEncMsg[] = {
  53223. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  53224. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  53225. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  53226. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  53227. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  53228. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  53229. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  53230. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  53231. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  53232. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  53233. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  53234. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  53235. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  53236. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  53237. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  53238. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  53239. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  53240. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  53241. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  53242. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  53243. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  53244. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  53245. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  53246. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  53247. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  53248. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  53249. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  53250. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  53251. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  53252. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  53253. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  53254. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  53255. };
  53256. #ifdef WC_RSA_NO_PADDING
  53257. const unsigned char expEncMsgNoPad[] = {
  53258. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  53259. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  53260. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  53261. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  53262. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  53263. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  53264. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  53265. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  53266. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  53267. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  53268. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  53269. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  53270. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  53271. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  53272. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  53273. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  53274. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  53275. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  53276. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  53277. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  53278. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  53279. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  53280. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  53281. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  53282. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  53283. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  53284. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  53285. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  53286. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  53287. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  53288. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  53289. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  53290. };
  53291. #endif
  53292. #endif
  53293. const unsigned char* der;
  53294. XMEMSET(msg, 0x00, sizeof(msg));
  53295. der = privDer;
  53296. rsa = NULL;
  53297. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53298. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  53299. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  53300. -1);
  53301. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  53302. RSA_PKCS1_PADDING), -1);
  53303. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  53304. RSA_PKCS1_PADDING), -1);
  53305. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  53306. RSA_PKCS1_PADDING), -1);
  53307. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  53308. RSA_PKCS1_PSS_PADDING), -1);
  53309. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  53310. RSA_PKCS1_PADDING), sizeof(encMsg));
  53311. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  53312. #ifdef WC_RSA_NO_PADDING
  53313. /* Non-zero message. */
  53314. XMEMSET(msg, 0x01, sizeof(msg));
  53315. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  53316. RSA_NO_PADDING), sizeof(encMsg));
  53317. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  53318. #endif
  53319. RSA_free(rsa);
  53320. #endif
  53321. return EXPECT_RESULT();
  53322. }
  53323. static int test_wolfSSL_RSA_public_encrypt(void)
  53324. {
  53325. EXPECT_DECLS;
  53326. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53327. RSA* rsa = NULL;
  53328. const unsigned char msg[2048/8] = { 0 };
  53329. unsigned char encMsg[2048/8];
  53330. ExpectNotNull(rsa = RSA_new());
  53331. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  53332. RSA_PKCS1_PADDING), -1);
  53333. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  53334. RSA_PKCS1_PADDING), -1);
  53335. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  53336. RSA_PKCS1_PADDING), -1);
  53337. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  53338. RSA_PKCS1_PADDING), -1);
  53339. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  53340. RSA_PKCS1_PSS_PADDING), -1);
  53341. /* Empty RSA key. */
  53342. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  53343. RSA_PKCS1_PADDING), -1);
  53344. RSA_free(rsa);
  53345. #endif
  53346. return EXPECT_RESULT();
  53347. }
  53348. static int test_wolfSSL_RSA_private_decrypt(void)
  53349. {
  53350. EXPECT_DECLS;
  53351. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53352. RSA* rsa = NULL;
  53353. unsigned char msg[2048/8];
  53354. const unsigned char encMsg[2048/8] = { 0 };
  53355. ExpectNotNull(rsa = RSA_new());
  53356. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  53357. RSA_PKCS1_PADDING), -1);
  53358. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  53359. RSA_PKCS1_PADDING), -1);
  53360. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  53361. RSA_PKCS1_PADDING), -1);
  53362. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  53363. RSA_PKCS1_PADDING), -1);
  53364. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  53365. RSA_PKCS1_PSS_PADDING), -1);
  53366. /* Empty RSA key. */
  53367. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  53368. RSA_PKCS1_PADDING), -1);
  53369. RSA_free(rsa);
  53370. #endif
  53371. return EXPECT_RESULT();
  53372. }
  53373. static int test_wolfSSL_RSA_GenAdd(void)
  53374. {
  53375. EXPECT_DECLS;
  53376. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53377. RSA *rsa;
  53378. #ifdef USE_CERT_BUFFERS_1024
  53379. const unsigned char* privDer = client_key_der_1024;
  53380. size_t privDerSz = sizeof_client_key_der_1024;
  53381. const unsigned char* pubDer = client_keypub_der_1024;
  53382. size_t pubDerSz = sizeof_client_keypub_der_1024;
  53383. #else
  53384. const unsigned char* privDer = client_key_der_2048;
  53385. size_t privDerSz = sizeof_client_key_der_2048;
  53386. const unsigned char* pubDer = client_keypub_der_2048;
  53387. size_t pubDerSz = sizeof_client_keypub_der_2048;
  53388. #endif
  53389. const unsigned char* der;
  53390. der = privDer;
  53391. rsa = NULL;
  53392. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53393. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  53394. #ifndef RSA_LOW_MEM
  53395. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  53396. #else
  53397. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  53398. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  53399. #endif
  53400. RSA_free(rsa);
  53401. der = pubDer;
  53402. rsa = NULL;
  53403. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  53404. /* Need private values. */
  53405. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  53406. RSA_free(rsa);
  53407. #endif
  53408. return EXPECT_RESULT();
  53409. }
  53410. static int test_wolfSSL_RSA_blinding_on(void)
  53411. {
  53412. EXPECT_DECLS;
  53413. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  53414. RSA *rsa;
  53415. WOLFSSL_BN_CTX *bnCtx = NULL;
  53416. #ifdef USE_CERT_BUFFERS_1024
  53417. const unsigned char* privDer = client_key_der_1024;
  53418. size_t privDerSz = sizeof_client_key_der_1024;
  53419. #else
  53420. const unsigned char* privDer = client_key_der_2048;
  53421. size_t privDerSz = sizeof_client_key_der_2048;
  53422. #endif
  53423. const unsigned char* der;
  53424. der = privDer;
  53425. rsa = NULL;
  53426. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53427. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  53428. /* Does nothing so all parameters are valid. */
  53429. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  53430. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  53431. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  53432. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  53433. wolfSSL_BN_CTX_free(bnCtx);
  53434. RSA_free(rsa);
  53435. #endif
  53436. return EXPECT_RESULT();
  53437. }
  53438. static int test_wolfSSL_RSA_ex_data(void)
  53439. {
  53440. EXPECT_DECLS;
  53441. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  53442. RSA* rsa = NULL;
  53443. unsigned char data[1];
  53444. ExpectNotNull(rsa = RSA_new());
  53445. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  53446. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  53447. #ifdef MAX_EX_DATA
  53448. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  53449. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  53450. #endif
  53451. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  53452. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  53453. #ifdef HAVE_EX_DATA
  53454. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  53455. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  53456. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  53457. #else
  53458. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  53459. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  53460. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  53461. #endif
  53462. RSA_free(rsa);
  53463. #endif /* !NO_RSA && OPENSSL_EXTRA */
  53464. return EXPECT_RESULT();
  53465. }
  53466. static int test_wolfSSL_RSA_LoadDer(void)
  53467. {
  53468. EXPECT_DECLS;
  53469. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  53470. defined(OPENSSL_EXTRA_X509_SMALL))
  53471. RSA *rsa = NULL;
  53472. #ifdef USE_CERT_BUFFERS_1024
  53473. const unsigned char* privDer = client_key_der_1024;
  53474. size_t privDerSz = sizeof_client_key_der_1024;
  53475. #else
  53476. const unsigned char* privDer = client_key_der_2048;
  53477. size_t privDerSz = sizeof_client_key_der_2048;
  53478. #endif
  53479. ExpectNotNull(rsa = RSA_new());
  53480. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  53481. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  53482. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  53483. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  53484. RSA_free(rsa);
  53485. #endif /* !NO_RSA && OPENSSL_EXTRA */
  53486. return EXPECT_RESULT();
  53487. }
  53488. /* Local API. */
  53489. static int test_wolfSSL_RSA_To_Der(void)
  53490. {
  53491. EXPECT_DECLS;
  53492. #ifdef WOLFSSL_TEST_STATIC_BUILD
  53493. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53494. RSA* rsa;
  53495. #ifdef USE_CERT_BUFFERS_1024
  53496. const unsigned char* privDer = client_key_der_1024;
  53497. size_t privDerSz = sizeof_client_key_der_1024;
  53498. const unsigned char* pubDer = client_keypub_der_1024;
  53499. size_t pubDerSz = sizeof_client_keypub_der_1024;
  53500. unsigned char out[sizeof(client_key_der_1024)];
  53501. #else
  53502. const unsigned char* privDer = client_key_der_2048;
  53503. size_t privDerSz = sizeof_client_key_der_2048;
  53504. const unsigned char* pubDer = client_keypub_der_2048;
  53505. size_t pubDerSz = sizeof_client_keypub_der_2048;
  53506. unsigned char out[sizeof(client_key_der_2048)];
  53507. #endif
  53508. const unsigned char* der;
  53509. unsigned char* outDer = NULL;
  53510. der = privDer;
  53511. rsa = NULL;
  53512. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53513. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  53514. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  53515. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  53516. outDer = out;
  53517. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  53518. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  53519. outDer = NULL;
  53520. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  53521. ExpectNotNull(outDer);
  53522. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  53523. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  53524. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  53525. outDer = out;
  53526. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  53527. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  53528. RSA_free(rsa);
  53529. ExpectNotNull(rsa = RSA_new());
  53530. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  53531. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  53532. RSA_free(rsa);
  53533. der = pubDer;
  53534. rsa = NULL;
  53535. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  53536. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  53537. RSA_free(rsa);
  53538. #endif
  53539. #endif
  53540. return EXPECT_RESULT();
  53541. }
  53542. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  53543. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  53544. {
  53545. EXPECT_DECLS;
  53546. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  53547. XFILE file = XBADFILE;
  53548. const char* fname = "./certs/server-keyPub.pem";
  53549. RSA *rsa = NULL;
  53550. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  53551. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  53552. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  53553. ExpectIntEQ(RSA_size(rsa), 256);
  53554. RSA_free(rsa);
  53555. if (file != XBADFILE)
  53556. XFCLOSE(file);
  53557. #endif
  53558. return EXPECT_RESULT();
  53559. }
  53560. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  53561. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  53562. {
  53563. EXPECT_DECLS;
  53564. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  53565. defined(WOLFSSL_KEY_GEN)
  53566. RSA* rsa = NULL;
  53567. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  53568. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  53569. /* Valid but stub so returns 0. */
  53570. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  53571. #endif
  53572. return EXPECT_RESULT();
  53573. }
  53574. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  53575. {
  53576. EXPECT_DECLS;
  53577. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  53578. (defined(WOLFSSL_PEM_TO_DER) || \
  53579. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  53580. RSA* rsa = NULL;
  53581. #ifdef USE_CERT_BUFFERS_1024
  53582. const unsigned char* privDer = client_key_der_1024;
  53583. size_t privDerSz = sizeof_client_key_der_1024;
  53584. #else
  53585. const unsigned char* privDer = client_key_der_2048;
  53586. size_t privDerSz = sizeof_client_key_der_2048;
  53587. #endif
  53588. const unsigned char* der;
  53589. #ifndef NO_AES
  53590. unsigned char passwd[] = "password";
  53591. #endif
  53592. ExpectNotNull(rsa = RSA_new());
  53593. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  53594. NULL, NULL), 0);
  53595. RSA_free(rsa);
  53596. der = privDer;
  53597. rsa = NULL;
  53598. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53599. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  53600. NULL, NULL), 0);
  53601. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  53602. NULL, NULL), 0);
  53603. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  53604. NULL, NULL), 1);
  53605. #ifndef NO_AES
  53606. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  53607. NULL, 0, NULL, NULL), 1);
  53608. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  53609. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  53610. #endif
  53611. RSA_free(rsa);
  53612. #endif
  53613. return EXPECT_RESULT();
  53614. }
  53615. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  53616. {
  53617. EXPECT_DECLS;
  53618. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  53619. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  53620. RSA* rsa = NULL;
  53621. #ifdef USE_CERT_BUFFERS_1024
  53622. const unsigned char* privDer = client_key_der_1024;
  53623. size_t privDerSz = sizeof_client_key_der_1024;
  53624. #else
  53625. const unsigned char* privDer = client_key_der_2048;
  53626. size_t privDerSz = sizeof_client_key_der_2048;
  53627. #endif
  53628. const unsigned char* der;
  53629. #ifndef NO_AES
  53630. unsigned char passwd[] = "password";
  53631. #endif
  53632. unsigned char* pem = NULL;
  53633. int plen;
  53634. ExpectNotNull(rsa = RSA_new());
  53635. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53636. &plen), 0);
  53637. RSA_free(rsa);
  53638. der = privDer;
  53639. rsa = NULL;
  53640. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53641. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  53642. &plen), 0);
  53643. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  53644. &plen), 0);
  53645. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53646. NULL), 0);
  53647. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53648. &plen), 1);
  53649. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53650. pem = NULL;
  53651. #ifndef NO_AES
  53652. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  53653. NULL, 0, &pem, &plen), 1);
  53654. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53655. pem = NULL;
  53656. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  53657. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  53658. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53659. #endif
  53660. RSA_free(rsa);
  53661. #endif
  53662. return EXPECT_RESULT();
  53663. }
  53664. static int test_wolfSSL_DH(void)
  53665. {
  53666. EXPECT_DECLS;
  53667. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53668. DH *dh = NULL;
  53669. BIGNUM* p;
  53670. BIGNUM* q;
  53671. BIGNUM* g;
  53672. BIGNUM* pub = NULL;
  53673. BIGNUM* priv = NULL;
  53674. #if defined(OPENSSL_ALL)
  53675. #if !defined(HAVE_FIPS) || \
  53676. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  53677. FILE* f = NULL;
  53678. unsigned char buf[268];
  53679. const unsigned char* pt = buf;
  53680. long len = 0;
  53681. dh = NULL;
  53682. XMEMSET(buf, 0, sizeof(buf));
  53683. /* Test 2048 bit parameters */
  53684. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  53685. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53686. if (f != XBADFILE)
  53687. XFCLOSE(f);
  53688. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  53689. ExpectNotNull(dh->p);
  53690. ExpectNotNull(dh->g);
  53691. ExpectTrue(pt == buf);
  53692. ExpectIntEQ(DH_generate_key(dh), 1);
  53693. ExpectIntEQ(DH_generate_key(dh), 1);
  53694. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  53695. ExpectNotNull(pub = BN_new());
  53696. ExpectIntEQ(BN_set_word(pub, 1), 1);
  53697. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  53698. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  53699. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  53700. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  53701. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  53702. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  53703. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  53704. BN_free(pub);
  53705. pub = NULL;
  53706. DH_get0_pqg(dh, (const BIGNUM**)&p,
  53707. (const BIGNUM**)&q,
  53708. (const BIGNUM**)&g);
  53709. ExpectPtrEq(p, dh->p);
  53710. ExpectPtrEq(q, dh->q);
  53711. ExpectPtrEq(g, dh->g);
  53712. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  53713. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  53714. ExpectPtrEq(pub, dh->pub_key);
  53715. ExpectPtrEq(priv, dh->priv_key);
  53716. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  53717. ExpectPtrEq(pub, dh->pub_key);
  53718. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  53719. ExpectPtrEq(priv, dh->priv_key);
  53720. pub = NULL;
  53721. priv = NULL;
  53722. ExpectNotNull(pub = BN_new());
  53723. ExpectNotNull(priv = BN_new());
  53724. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  53725. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  53726. if (EXPECT_FAIL()) {
  53727. BN_free(pub);
  53728. BN_free(priv);
  53729. }
  53730. pub = NULL;
  53731. priv = NULL;
  53732. ExpectNotNull(pub = BN_new());
  53733. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  53734. if (EXPECT_FAIL()) {
  53735. BN_free(pub);
  53736. }
  53737. ExpectNotNull(priv = BN_new());
  53738. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  53739. if (EXPECT_FAIL()) {
  53740. BN_free(priv);
  53741. }
  53742. ExpectPtrEq(pub, dh->pub_key);
  53743. ExpectPtrEq(priv, dh->priv_key);
  53744. pub = NULL;
  53745. priv = NULL;
  53746. DH_free(dh);
  53747. dh = NULL;
  53748. ExpectNotNull(dh = DH_new());
  53749. p = NULL;
  53750. ExpectNotNull(p = BN_new());
  53751. ExpectIntEQ(BN_set_word(p, 1), 1);
  53752. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  53753. ExpectNotNull(pub = BN_new());
  53754. ExpectNotNull(priv = BN_new());
  53755. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  53756. if (EXPECT_FAIL()) {
  53757. BN_free(pub);
  53758. BN_free(priv);
  53759. }
  53760. pub = NULL;
  53761. priv = NULL;
  53762. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  53763. BN_free(p);
  53764. p = NULL;
  53765. DH_free(dh);
  53766. dh = NULL;
  53767. #ifdef WOLFSSL_KEY_GEN
  53768. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  53769. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  53770. DH_free(dh);
  53771. dh = NULL;
  53772. #endif
  53773. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  53774. #endif /* OPENSSL_ALL */
  53775. (void)dh;
  53776. (void)p;
  53777. (void)q;
  53778. (void)g;
  53779. (void)pub;
  53780. (void)priv;
  53781. ExpectNotNull(dh = wolfSSL_DH_new());
  53782. /* invalid parameters test */
  53783. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  53784. (const BIGNUM**)&q,
  53785. (const BIGNUM**)&g);
  53786. DH_get0_pqg(dh, NULL,
  53787. (const BIGNUM**)&q,
  53788. (const BIGNUM**)&g);
  53789. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  53790. DH_get0_pqg(dh, NULL, NULL, NULL);
  53791. DH_get0_pqg(dh, (const BIGNUM**)&p,
  53792. (const BIGNUM**)&q,
  53793. (const BIGNUM**)&g);
  53794. ExpectPtrEq(p, NULL);
  53795. ExpectPtrEq(q, NULL);
  53796. ExpectPtrEq(g, NULL);
  53797. DH_free(dh);
  53798. dh = NULL;
  53799. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  53800. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  53801. #if defined(OPENSSL_ALL) || \
  53802. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  53803. dh = wolfSSL_DH_new();
  53804. ExpectNotNull(dh);
  53805. p = wolfSSL_BN_new();
  53806. ExpectNotNull(p);
  53807. ExpectIntEQ(BN_set_word(p, 11), 1);
  53808. g = wolfSSL_BN_new();
  53809. ExpectNotNull(g);
  53810. ExpectIntEQ(BN_set_word(g, 2), 1);
  53811. q = wolfSSL_BN_new();
  53812. ExpectNotNull(q);
  53813. ExpectIntEQ(BN_set_word(q, 5), 1);
  53814. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  53815. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  53816. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  53817. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  53818. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  53819. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  53820. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  53821. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  53822. /* Don't need q. */
  53823. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53824. if (EXPECT_FAIL()) {
  53825. BN_free(p);
  53826. BN_free(g);
  53827. }
  53828. p = NULL;
  53829. g = NULL;
  53830. /* Setting again will free the p and g. */
  53831. wolfSSL_BN_free(q);
  53832. q = NULL;
  53833. DH_free(dh);
  53834. dh = NULL;
  53835. dh = wolfSSL_DH_new();
  53836. ExpectNotNull(dh);
  53837. p = wolfSSL_BN_new();
  53838. ExpectNotNull(p);
  53839. ExpectIntEQ(BN_set_word(p, 11), 1);
  53840. g = wolfSSL_BN_new();
  53841. ExpectNotNull(g);
  53842. ExpectIntEQ(BN_set_word(g, 2), 1);
  53843. q = wolfSSL_BN_new();
  53844. ExpectNotNull(q);
  53845. ExpectIntEQ(BN_set_word(q, 5), 1);
  53846. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  53847. /* p, q and g are now owned by dh - don't free. */
  53848. if (EXPECT_FAIL()) {
  53849. BN_free(p);
  53850. BN_free(q);
  53851. BN_free(g);
  53852. }
  53853. p = NULL;
  53854. q = NULL;
  53855. g = NULL;
  53856. p = wolfSSL_BN_new();
  53857. ExpectNotNull(p);
  53858. ExpectIntEQ(BN_set_word(p, 11), 1);
  53859. g = wolfSSL_BN_new();
  53860. ExpectNotNull(g);
  53861. ExpectIntEQ(BN_set_word(g, 2), 1);
  53862. q = wolfSSL_BN_new();
  53863. ExpectNotNull(q);
  53864. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  53865. if (EXPECT_FAIL()) {
  53866. BN_free(p);
  53867. }
  53868. p = NULL;
  53869. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  53870. if (EXPECT_FAIL()) {
  53871. BN_free(q);
  53872. }
  53873. q = NULL;
  53874. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  53875. if (EXPECT_FAIL()) {
  53876. BN_free(g);
  53877. }
  53878. g = NULL;
  53879. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  53880. /* p, q and g are now owned by dh - don't free. */
  53881. DH_free(dh);
  53882. dh = NULL;
  53883. ExpectIntEQ(DH_generate_key(NULL), 0);
  53884. ExpectNotNull(dh = DH_new());
  53885. ExpectIntEQ(DH_generate_key(dh), 0);
  53886. p = wolfSSL_BN_new();
  53887. ExpectNotNull(p);
  53888. ExpectIntEQ(BN_set_word(p, 0), 1);
  53889. g = wolfSSL_BN_new();
  53890. ExpectNotNull(g);
  53891. ExpectIntEQ(BN_set_word(g, 2), 1);
  53892. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53893. if (EXPECT_FAIL()) {
  53894. BN_free(p);
  53895. BN_free(g);
  53896. }
  53897. p = NULL;
  53898. g = NULL;
  53899. ExpectIntEQ(DH_generate_key(dh), 0);
  53900. DH_free(dh);
  53901. dh = NULL;
  53902. #endif
  53903. #endif
  53904. /* Test DH_up_ref() */
  53905. dh = wolfSSL_DH_new();
  53906. ExpectNotNull(dh);
  53907. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  53908. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  53909. DH_free(dh); /* decrease ref count */
  53910. DH_free(dh); /* free WOLFSSL_DH */
  53911. dh = NULL;
  53912. q = NULL;
  53913. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  53914. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  53915. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  53916. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  53917. #ifdef HAVE_FFDHE_2048
  53918. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  53919. DH_free(dh);
  53920. dh = NULL;
  53921. q = NULL;
  53922. #endif
  53923. #ifdef HAVE_FFDHE_3072
  53924. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  53925. DH_free(dh);
  53926. dh = NULL;
  53927. q = NULL;
  53928. #endif
  53929. #ifdef HAVE_FFDHE_4096
  53930. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  53931. DH_free(dh);
  53932. dh = NULL;
  53933. q = NULL;
  53934. #endif
  53935. #else
  53936. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  53937. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  53938. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  53939. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  53940. #endif /* OPENSSL_EXTRA && !NO_DH */
  53941. return EXPECT_RESULT();
  53942. }
  53943. static int test_wolfSSL_DH_dup(void)
  53944. {
  53945. EXPECT_DECLS;
  53946. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  53947. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  53948. defined(OPENSSL_EXTRA)
  53949. DH *dh = NULL;
  53950. DH *dhDup = NULL;
  53951. ExpectNotNull(dh = wolfSSL_DH_new());
  53952. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  53953. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  53954. #if defined(OPENSSL_ALL) || \
  53955. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  53956. {
  53957. WOLFSSL_BIGNUM* p = NULL;
  53958. WOLFSSL_BIGNUM* g = NULL;
  53959. ExpectNotNull(p = wolfSSL_BN_new());
  53960. ExpectNotNull(g = wolfSSL_BN_new());
  53961. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  53962. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  53963. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53964. if (EXPECT_FAIL()) {
  53965. wolfSSL_BN_free(p);
  53966. wolfSSL_BN_free(g);
  53967. }
  53968. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  53969. wolfSSL_DH_free(dhDup);
  53970. }
  53971. #endif
  53972. wolfSSL_DH_free(dh);
  53973. #endif
  53974. #endif
  53975. return EXPECT_RESULT();
  53976. }
  53977. static int test_wolfSSL_DH_check(void)
  53978. {
  53979. EXPECT_DECLS;
  53980. #ifdef OPENSSL_ALL
  53981. #ifndef NO_DH
  53982. #ifndef NO_BIO
  53983. #ifndef NO_DSA
  53984. byte buf[6000];
  53985. char file[] = "./certs/dsaparams.pem";
  53986. XFILE f = XBADFILE;
  53987. int bytes;
  53988. BIO* bio = NULL;
  53989. DSA* dsa = NULL;
  53990. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53991. static const byte dh2048[] = {
  53992. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53993. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53994. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53995. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53996. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53997. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53998. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53999. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  54000. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  54001. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  54002. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  54003. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  54004. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  54005. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  54006. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  54007. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  54008. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  54009. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  54010. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  54011. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  54012. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  54013. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  54014. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  54015. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  54016. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  54017. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  54018. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  54019. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  54020. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  54021. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  54022. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  54023. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  54024. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  54025. 0x93, 0x02, 0x01, 0x02
  54026. };
  54027. const byte* params;
  54028. #endif
  54029. DH* dh = NULL;
  54030. WOLFSSL_BIGNUM* p = NULL;
  54031. WOLFSSL_BIGNUM* g = NULL;
  54032. WOLFSSL_BIGNUM* pTmp = NULL;
  54033. WOLFSSL_BIGNUM* gTmp = NULL;
  54034. int codes = -1;
  54035. #ifndef NO_DSA
  54036. /* Initialize DH */
  54037. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  54038. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  54039. if (f != XBADFILE)
  54040. XFCLOSE(f);
  54041. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  54042. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  54043. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  54044. ExpectNotNull(dh);
  54045. BIO_free(bio);
  54046. DSA_free(dsa);
  54047. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  54048. params = dh2048;
  54049. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  54050. (long)sizeof(dh2048)));
  54051. #else
  54052. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  54053. #endif
  54054. /* Test assumed to be valid dh.
  54055. * Should return WOLFSSL_SUCCESS
  54056. * codes should be 0
  54057. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  54058. */
  54059. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54060. ExpectIntEQ(codes, 0);
  54061. /* Test NULL dh: expected BAD_FUNC_ARG */
  54062. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  54063. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  54064. if (dh != NULL) {
  54065. pTmp = dh->p;
  54066. dh->p = NULL;
  54067. }
  54068. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54069. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  54070. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  54071. /* set dh->p back to normal so it won't fail on next tests */
  54072. if (dh != NULL) {
  54073. dh->p = pTmp;
  54074. pTmp = NULL;
  54075. }
  54076. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  54077. if (dh != NULL) {
  54078. gTmp = dh->g;
  54079. dh->g = NULL;
  54080. }
  54081. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54082. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  54083. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  54084. if (dh != NULL) {
  54085. dh->g = gTmp;
  54086. gTmp = NULL;
  54087. }
  54088. /* Cleanup */
  54089. DH_free(dh);
  54090. dh = NULL;
  54091. dh = DH_new();
  54092. ExpectNotNull(dh);
  54093. /* Check empty DH. */
  54094. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54095. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  54096. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  54097. /* Check non-prime valued p. */
  54098. ExpectNotNull(p = BN_new());
  54099. ExpectIntEQ(BN_set_word(p, 4), 1);
  54100. ExpectNotNull(g = BN_new());
  54101. ExpectIntEQ(BN_set_word(g, 2), 1);
  54102. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  54103. if (EXPECT_FAIL()) {
  54104. wolfSSL_BN_free(p);
  54105. wolfSSL_BN_free(g);
  54106. }
  54107. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54108. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  54109. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  54110. DH_free(dh);
  54111. dh = NULL;
  54112. #endif
  54113. #endif /* !NO_DH && !NO_DSA */
  54114. #endif
  54115. return EXPECT_RESULT();
  54116. }
  54117. static int test_wolfSSL_DH_prime(void)
  54118. {
  54119. EXPECT_DECLS;
  54120. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  54121. WOLFSSL_BIGNUM* bn = NULL;
  54122. #if WOLFSSL_MAX_BN_BITS >= 768
  54123. WOLFSSL_BIGNUM* bn2 = NULL;
  54124. #endif
  54125. bn = wolfSSL_DH_768_prime(NULL);
  54126. #if WOLFSSL_MAX_BN_BITS >= 768
  54127. ExpectNotNull(bn);
  54128. bn2 = wolfSSL_DH_768_prime(bn);
  54129. ExpectNotNull(bn2);
  54130. ExpectTrue(bn == bn2);
  54131. wolfSSL_BN_free(bn);
  54132. bn = NULL;
  54133. #else
  54134. ExpectNull(bn);
  54135. #endif
  54136. bn = wolfSSL_DH_1024_prime(NULL);
  54137. #if WOLFSSL_MAX_BN_BITS >= 1024
  54138. ExpectNotNull(bn);
  54139. wolfSSL_BN_free(bn);
  54140. bn = NULL;
  54141. #else
  54142. ExpectNull(bn);
  54143. #endif
  54144. bn = wolfSSL_DH_2048_prime(NULL);
  54145. #if WOLFSSL_MAX_BN_BITS >= 2048
  54146. ExpectNotNull(bn);
  54147. wolfSSL_BN_free(bn);
  54148. bn = NULL;
  54149. #else
  54150. ExpectNull(bn);
  54151. #endif
  54152. bn = wolfSSL_DH_3072_prime(NULL);
  54153. #if WOLFSSL_MAX_BN_BITS >= 3072
  54154. ExpectNotNull(bn);
  54155. wolfSSL_BN_free(bn);
  54156. bn = NULL;
  54157. #else
  54158. ExpectNull(bn);
  54159. #endif
  54160. bn = wolfSSL_DH_4096_prime(NULL);
  54161. #if WOLFSSL_MAX_BN_BITS >= 4096
  54162. ExpectNotNull(bn);
  54163. wolfSSL_BN_free(bn);
  54164. bn = NULL;
  54165. #else
  54166. ExpectNull(bn);
  54167. #endif
  54168. bn = wolfSSL_DH_6144_prime(NULL);
  54169. #if WOLFSSL_MAX_BN_BITS >= 6144
  54170. ExpectNotNull(bn);
  54171. wolfSSL_BN_free(bn);
  54172. bn = NULL;
  54173. #else
  54174. ExpectNull(bn);
  54175. #endif
  54176. bn = wolfSSL_DH_8192_prime(NULL);
  54177. #if WOLFSSL_MAX_BN_BITS >= 8192
  54178. ExpectNotNull(bn);
  54179. wolfSSL_BN_free(bn);
  54180. bn = NULL;
  54181. #else
  54182. ExpectNull(bn);
  54183. #endif
  54184. #endif
  54185. return EXPECT_RESULT();
  54186. }
  54187. static int test_wolfSSL_DH_1536_prime(void)
  54188. {
  54189. EXPECT_DECLS;
  54190. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  54191. BIGNUM* bn = NULL;
  54192. unsigned char bits[200];
  54193. int sz = 192; /* known binary size */
  54194. const byte expected[] = {
  54195. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  54196. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  54197. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  54198. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  54199. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  54200. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  54201. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  54202. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  54203. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  54204. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  54205. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  54206. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  54207. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  54208. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  54209. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  54210. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  54211. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  54212. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  54213. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  54214. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  54215. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  54216. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  54217. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  54218. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  54219. };
  54220. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  54221. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  54222. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  54223. BN_free(bn);
  54224. #endif
  54225. return EXPECT_RESULT();
  54226. }
  54227. static int test_wolfSSL_DH_get_2048_256(void)
  54228. {
  54229. EXPECT_DECLS;
  54230. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  54231. WOLFSSL_DH* dh = NULL;
  54232. const WOLFSSL_BIGNUM* pBn;
  54233. const WOLFSSL_BIGNUM* gBn;
  54234. const WOLFSSL_BIGNUM* qBn;
  54235. const byte pExpected[] = {
  54236. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  54237. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  54238. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  54239. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  54240. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  54241. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  54242. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  54243. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  54244. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  54245. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  54246. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  54247. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  54248. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  54249. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  54250. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  54251. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  54252. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  54253. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  54254. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  54255. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  54256. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  54257. 0x1E, 0x1A, 0x15, 0x97
  54258. };
  54259. const byte gExpected[] = {
  54260. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  54261. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  54262. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  54263. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  54264. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  54265. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  54266. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  54267. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  54268. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  54269. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  54270. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  54271. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  54272. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  54273. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  54274. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  54275. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  54276. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  54277. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  54278. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  54279. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  54280. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  54281. 0x6C, 0xC4, 0x16, 0x59
  54282. };
  54283. const byte qExpected[] = {
  54284. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  54285. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  54286. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  54287. };
  54288. int pSz = 0;
  54289. int qSz = 0;
  54290. int gSz = 0;
  54291. byte* pReturned = NULL;
  54292. byte* qReturned = NULL;
  54293. byte* gReturned = NULL;
  54294. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  54295. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  54296. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  54297. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  54298. DYNAMIC_TYPE_TMP_BUFFER));
  54299. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  54300. ExpectIntEQ(pSz, sizeof(pExpected));
  54301. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  54302. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  54303. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  54304. DYNAMIC_TYPE_TMP_BUFFER));
  54305. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  54306. ExpectIntEQ(qSz, sizeof(qExpected));
  54307. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  54308. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  54309. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  54310. DYNAMIC_TYPE_TMP_BUFFER));
  54311. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  54312. ExpectIntEQ(gSz, sizeof(gExpected));
  54313. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  54314. wolfSSL_DH_free(dh);
  54315. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54316. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54317. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54318. #endif
  54319. return EXPECT_RESULT();
  54320. }
  54321. static int test_wolfSSL_PEM_write_DHparams(void)
  54322. {
  54323. EXPECT_DECLS;
  54324. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  54325. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  54326. DH* dh = NULL;
  54327. BIO* bio = NULL;
  54328. XFILE fp = XBADFILE;
  54329. byte pem[2048];
  54330. int pemSz = 0;
  54331. const char expected[] =
  54332. "-----BEGIN DH PARAMETERS-----\n"
  54333. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  54334. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  54335. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  54336. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  54337. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  54338. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  54339. "-----END DH PARAMETERS-----\n";
  54340. const char badPem[] =
  54341. "-----BEGIN DH PARAMETERS-----\n"
  54342. "-----END DH PARAMETERS-----\n";
  54343. const char emptySeqPem[] =
  54344. "-----BEGIN DH PARAMETERS-----\n"
  54345. "MAA=\n"
  54346. "-----END DH PARAMETERS-----\n";
  54347. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  54348. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  54349. if (fp != XBADFILE) {
  54350. XFCLOSE(fp);
  54351. fp = XBADFILE;
  54352. }
  54353. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  54354. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  54355. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54356. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  54357. (int)sizeof(badPem));
  54358. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54359. BIO_free(bio);
  54360. bio = NULL;
  54361. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  54362. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54363. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  54364. (int)sizeof(emptySeqPem));
  54365. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54366. BIO_free(bio);
  54367. bio = NULL;
  54368. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  54369. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  54370. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54371. BIO_free(bio);
  54372. bio = NULL;
  54373. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  54374. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  54375. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  54376. DH_free(dh);
  54377. dh = NULL;
  54378. dh = wolfSSL_DH_new();
  54379. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  54380. if (fp != XBADFILE) {
  54381. XFCLOSE(fp);
  54382. fp = XBADFILE;
  54383. }
  54384. wolfSSL_DH_free(dh);
  54385. dh = NULL;
  54386. /* check results */
  54387. XMEMSET(pem, 0, sizeof(pem));
  54388. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  54389. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  54390. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  54391. if (fp != XBADFILE)
  54392. XFCLOSE(fp);
  54393. #endif
  54394. return EXPECT_RESULT();
  54395. }
  54396. static int test_wolfSSL_d2i_DHparams(void)
  54397. {
  54398. EXPECT_DECLS;
  54399. #ifdef OPENSSL_ALL
  54400. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  54401. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54402. XFILE f = XBADFILE;
  54403. unsigned char buf[4096];
  54404. const unsigned char* pt = buf;
  54405. #ifdef HAVE_FFDHE_2048
  54406. const char* params1 = "./certs/dh2048.der";
  54407. #endif
  54408. #ifdef HAVE_FFDHE_3072
  54409. const char* params2 = "./certs/dh3072.der";
  54410. #endif
  54411. long len = 0;
  54412. WOLFSSL_DH* dh = NULL;
  54413. XMEMSET(buf, 0, sizeof(buf));
  54414. /* Test 2048 bit parameters */
  54415. #ifdef HAVE_FFDHE_2048
  54416. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  54417. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54418. if (f != XBADFILE) {
  54419. XFCLOSE(f);
  54420. f = XBADFILE;
  54421. }
  54422. /* Valid case */
  54423. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  54424. ExpectNotNull(dh->p);
  54425. ExpectNotNull(dh->g);
  54426. ExpectTrue(pt == buf);
  54427. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  54428. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  54429. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  54430. /* Invalid cases */
  54431. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  54432. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  54433. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  54434. DH_free(dh);
  54435. dh = NULL;
  54436. *buf = 0;
  54437. pt = buf;
  54438. #endif /* HAVE_FFDHE_2048 */
  54439. /* Test 3072 bit parameters */
  54440. #ifdef HAVE_FFDHE_3072
  54441. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  54442. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54443. if (f != XBADFILE) {
  54444. XFCLOSE(f);
  54445. f = XBADFILE;
  54446. }
  54447. /* Valid case */
  54448. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  54449. ExpectNotNull(dh->p);
  54450. ExpectNotNull(dh->g);
  54451. ExpectTrue(pt != buf);
  54452. ExpectIntEQ(DH_generate_key(dh), 1);
  54453. /* Invalid cases */
  54454. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  54455. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  54456. DH_free(dh);
  54457. dh = NULL;
  54458. #endif /* HAVE_FFDHE_3072 */
  54459. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54460. #endif /* !NO_DH */
  54461. #endif
  54462. return EXPECT_RESULT();
  54463. }
  54464. static int test_wolfSSL_DH_LoadDer(void)
  54465. {
  54466. EXPECT_DECLS;
  54467. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  54468. defined(OPENSSL_EXTRA)
  54469. static const byte dh2048[] = {
  54470. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  54471. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  54472. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  54473. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  54474. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  54475. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  54476. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  54477. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  54478. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  54479. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  54480. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  54481. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  54482. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  54483. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  54484. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  54485. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  54486. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  54487. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  54488. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  54489. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  54490. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  54491. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  54492. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  54493. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  54494. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  54495. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  54496. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  54497. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  54498. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  54499. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  54500. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  54501. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  54502. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  54503. 0x93, 0x02, 0x01, 0x02
  54504. };
  54505. WOLFSSL_DH* dh = NULL;
  54506. ExpectNotNull(dh = wolfSSL_DH_new());
  54507. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  54508. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  54509. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  54510. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  54511. wolfSSL_DH_free(dh);
  54512. #endif
  54513. return EXPECT_RESULT();
  54514. }
  54515. static int test_wolfSSL_i2d_DHparams(void)
  54516. {
  54517. EXPECT_DECLS;
  54518. #ifdef OPENSSL_ALL
  54519. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  54520. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54521. XFILE f = XBADFILE;
  54522. unsigned char buf[4096];
  54523. const unsigned char* pt;
  54524. unsigned char* pt2;
  54525. #ifdef HAVE_FFDHE_2048
  54526. const char* params1 = "./certs/dh2048.der";
  54527. #endif
  54528. #ifdef HAVE_FFDHE_3072
  54529. const char* params2 = "./certs/dh3072.der";
  54530. #endif
  54531. long len = 0;
  54532. WOLFSSL_DH* dh = NULL;
  54533. /* Test 2048 bit parameters */
  54534. #ifdef HAVE_FFDHE_2048
  54535. pt = buf;
  54536. pt2 = buf;
  54537. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  54538. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54539. if (f != XBADFILE) {
  54540. XFCLOSE(f);
  54541. f = XBADFILE;
  54542. }
  54543. /* Valid case */
  54544. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  54545. ExpectTrue(pt == buf);
  54546. ExpectIntEQ(DH_generate_key(dh), 1);
  54547. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  54548. /* Invalid case */
  54549. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  54550. /* Return length only */
  54551. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  54552. DH_free(dh);
  54553. dh = NULL;
  54554. *buf = 0;
  54555. #endif
  54556. /* Test 3072 bit parameters */
  54557. #ifdef HAVE_FFDHE_3072
  54558. pt = buf;
  54559. pt2 = buf;
  54560. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  54561. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54562. if (f != XBADFILE) {
  54563. XFCLOSE(f);
  54564. f = XBADFILE;
  54565. }
  54566. /* Valid case */
  54567. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  54568. ExpectTrue(pt == buf);
  54569. ExpectIntEQ(DH_generate_key(dh), 1);
  54570. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  54571. /* Invalid case */
  54572. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  54573. /* Return length only */
  54574. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  54575. DH_free(dh);
  54576. dh = NULL;
  54577. #endif
  54578. dh = DH_new();
  54579. ExpectNotNull(dh);
  54580. pt2 = buf;
  54581. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  54582. DH_free(dh);
  54583. dh = NULL;
  54584. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54585. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  54586. #endif
  54587. return EXPECT_RESULT();
  54588. }
  54589. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  54590. /*----------------------------------------------------------------------------*
  54591. | EC
  54592. *----------------------------------------------------------------------------*/
  54593. static int test_wolfSSL_EC_GROUP(void)
  54594. {
  54595. EXPECT_DECLS;
  54596. #ifdef OPENSSL_EXTRA
  54597. EC_GROUP *group = NULL;
  54598. EC_GROUP *group2 = NULL;
  54599. EC_GROUP *group3 = NULL;
  54600. #ifndef HAVE_ECC_BRAINPOOL
  54601. EC_GROUP *group4 = NULL;
  54602. #endif
  54603. WOLFSSL_BIGNUM* order = NULL;
  54604. int group_bits;
  54605. int i;
  54606. static const int knownEccNids[] = {
  54607. NID_X9_62_prime192v1,
  54608. NID_X9_62_prime192v2,
  54609. NID_X9_62_prime192v3,
  54610. NID_X9_62_prime239v1,
  54611. NID_X9_62_prime239v2,
  54612. NID_X9_62_prime239v3,
  54613. NID_X9_62_prime256v1,
  54614. NID_secp112r1,
  54615. NID_secp112r2,
  54616. NID_secp128r1,
  54617. NID_secp128r2,
  54618. NID_secp160r1,
  54619. NID_secp160r2,
  54620. NID_secp224r1,
  54621. NID_secp384r1,
  54622. NID_secp521r1,
  54623. NID_secp160k1,
  54624. NID_secp192k1,
  54625. NID_secp224k1,
  54626. NID_secp256k1,
  54627. NID_brainpoolP160r1,
  54628. NID_brainpoolP192r1,
  54629. NID_brainpoolP224r1,
  54630. NID_brainpoolP256r1,
  54631. NID_brainpoolP320r1,
  54632. NID_brainpoolP384r1,
  54633. NID_brainpoolP512r1,
  54634. };
  54635. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  54636. static const int knownEccEnums[] = {
  54637. ECC_SECP192R1,
  54638. ECC_PRIME192V2,
  54639. ECC_PRIME192V3,
  54640. ECC_PRIME239V1,
  54641. ECC_PRIME239V2,
  54642. ECC_PRIME239V3,
  54643. ECC_SECP256R1,
  54644. ECC_SECP112R1,
  54645. ECC_SECP112R2,
  54646. ECC_SECP128R1,
  54647. ECC_SECP128R2,
  54648. ECC_SECP160R1,
  54649. ECC_SECP160R2,
  54650. ECC_SECP224R1,
  54651. ECC_SECP384R1,
  54652. ECC_SECP521R1,
  54653. ECC_SECP160K1,
  54654. ECC_SECP192K1,
  54655. ECC_SECP224K1,
  54656. ECC_SECP256K1,
  54657. ECC_BRAINPOOLP160R1,
  54658. ECC_BRAINPOOLP192R1,
  54659. ECC_BRAINPOOLP224R1,
  54660. ECC_BRAINPOOLP256R1,
  54661. ECC_BRAINPOOLP320R1,
  54662. ECC_BRAINPOOLP384R1,
  54663. ECC_BRAINPOOLP512R1,
  54664. };
  54665. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  54666. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54667. ExpectNotNull(group2 = EC_GROUP_dup(group));
  54668. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  54669. #ifndef HAVE_ECC_BRAINPOOL
  54670. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  54671. NID_brainpoolP256r1));
  54672. #endif
  54673. ExpectNull(EC_GROUP_dup(NULL));
  54674. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  54675. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  54676. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  54677. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  54678. #ifndef HAVE_ECC_BRAINPOOL
  54679. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  54680. #endif
  54681. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  54682. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  54683. ExpectNotNull(order = BN_new());
  54684. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  54685. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  54686. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  54687. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  54688. wolfSSL_BN_free(order);
  54689. ExpectNotNull(EC_GROUP_method_of(group));
  54690. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  54691. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  54692. NID_X9_62_prime_field);
  54693. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  54694. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  54695. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  54696. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  54697. #ifndef NO_WOLFSSL_STUB
  54698. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  54699. #endif
  54700. #ifndef HAVE_ECC_BRAINPOOL
  54701. EC_GROUP_free(group4);
  54702. #endif
  54703. EC_GROUP_free(group3);
  54704. EC_GROUP_free(group2);
  54705. EC_GROUP_free(group);
  54706. for (i = 0; i < knowEccNidsLen; i++) {
  54707. group = NULL;
  54708. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  54709. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  54710. EC_GROUP_free(group);
  54711. }
  54712. for (i = 0; i < knowEccEnumsLen; i++) {
  54713. group = NULL;
  54714. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  54715. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  54716. EC_GROUP_free(group);
  54717. }
  54718. #endif
  54719. return EXPECT_RESULT();
  54720. }
  54721. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  54722. {
  54723. EXPECT_DECLS;
  54724. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54725. EC_GROUP *group = NULL;
  54726. BIO* bio = NULL;
  54727. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  54728. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  54729. EC_GROUP *ret = NULL;
  54730. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  54731. "BgUrgQQAIg==\n"
  54732. "-----END EC PARAMETERS-----";
  54733. #endif
  54734. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  54735. "MAA=\n"
  54736. "-----END EC PARAMETERS-----";
  54737. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  54738. "BgA=\n"
  54739. "-----END EC PARAMETERS-----";
  54740. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  54741. "BgE=\n"
  54742. "-----END EC PARAMETERS-----";
  54743. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  54744. "BgE*\n"
  54745. "-----END EC PARAMETERS-----";
  54746. /* Test that first parameter, bio, being NULL fails. */
  54747. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  54748. /* Test that reading named parameters works. */
  54749. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  54750. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  54751. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54752. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  54753. BIO_free(bio);
  54754. bio = NULL;
  54755. EC_GROUP_free(group);
  54756. group = NULL;
  54757. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  54758. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  54759. /* Test that reusing group works. */
  54760. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  54761. sizeof(ec_nc_p384)));
  54762. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  54763. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  54764. BIO_free(bio);
  54765. bio = NULL;
  54766. EC_GROUP_free(group);
  54767. group = NULL;
  54768. /* Test that returning through group works. */
  54769. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  54770. sizeof(ec_nc_p384)));
  54771. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  54772. ExpectIntEQ(group == ret, 1);
  54773. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  54774. BIO_free(bio);
  54775. bio = NULL;
  54776. EC_GROUP_free(group);
  54777. group = NULL;
  54778. #endif
  54779. /* Test 0x30, 0x00 (not and object id) fails. */
  54780. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  54781. sizeof(ec_nc_bad_1)));
  54782. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54783. BIO_free(bio);
  54784. bio = NULL;
  54785. /* Test 0x06, 0x00 (empty object id) fails. */
  54786. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  54787. sizeof(ec_nc_bad_2)));
  54788. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54789. BIO_free(bio);
  54790. bio = NULL;
  54791. /* Test 0x06, 0x01 (badly formed object id) fails. */
  54792. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  54793. sizeof(ec_nc_bad_3)));
  54794. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54795. BIO_free(bio);
  54796. bio = NULL;
  54797. /* Test invalid PEM encoding - invalid character. */
  54798. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  54799. sizeof(ec_nc_bad_4)));
  54800. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54801. BIO_free(bio);
  54802. #endif
  54803. return EXPECT_RESULT();
  54804. }
  54805. static int test_wolfSSL_EC_POINT(void)
  54806. {
  54807. EXPECT_DECLS;
  54808. #if !defined(WOLFSSL_SP_MATH) && \
  54809. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  54810. #ifdef OPENSSL_EXTRA
  54811. BN_CTX* ctx = NULL;
  54812. EC_GROUP* group = NULL;
  54813. #ifndef HAVE_ECC_BRAINPOOL
  54814. EC_GROUP* group2 = NULL;
  54815. #endif
  54816. EC_POINT* Gxy = NULL;
  54817. EC_POINT* new_point = NULL;
  54818. EC_POINT* set_point = NULL;
  54819. EC_POINT* get_point = NULL;
  54820. EC_POINT* infinity = NULL;
  54821. BIGNUM* k = NULL;
  54822. BIGNUM* Gx = NULL;
  54823. BIGNUM* Gy = NULL;
  54824. BIGNUM* Gz = NULL;
  54825. BIGNUM* X = NULL;
  54826. BIGNUM* Y = NULL;
  54827. BIGNUM* set_point_bn = NULL;
  54828. char* hexStr = NULL;
  54829. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  54830. "17AF381913FF7A96314EA47055EA0FD0";
  54831. /* NISTP256R1 Gx/Gy */
  54832. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  54833. "77037D812DEB33A0F4A13945D898C296";
  54834. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  54835. "2BCE33576B315ECECBB6406837BF51F5";
  54836. const char* uncompG
  54837. = "046B17D1F2E12C4247F8BCE6E563A440F2"
  54838. "77037D812DEB33A0F4A13945D898C296"
  54839. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  54840. "2BCE33576B315ECECBB6406837BF51F5";
  54841. const char* compG
  54842. = "036B17D1F2E12C4247F8BCE6E563A440F2"
  54843. "77037D812DEB33A0F4A13945D898C296";
  54844. #ifndef HAVE_SELFTEST
  54845. EC_POINT *tmp = NULL;
  54846. size_t bin_len;
  54847. unsigned int blen = 0;
  54848. unsigned char* buf = NULL;
  54849. unsigned char bufInf[1] = { 0x00 };
  54850. const unsigned char binUncompG[] = {
  54851. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54852. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54853. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54854. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54855. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54856. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54857. };
  54858. const unsigned char binUncompGBad[] = {
  54859. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54860. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54861. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54862. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54863. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54864. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54865. };
  54866. #ifdef HAVE_COMP_KEY
  54867. const unsigned char binCompG[] = {
  54868. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54869. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54870. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54871. };
  54872. #endif
  54873. #endif
  54874. ExpectNotNull(ctx = BN_CTX_new());
  54875. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54876. #ifndef HAVE_ECC_BRAINPOOL
  54877. /* Used to make groups curve_idx == -1. */
  54878. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  54879. #endif
  54880. ExpectNull(EC_POINT_new(NULL));
  54881. ExpectNotNull(Gxy = EC_POINT_new(group));
  54882. ExpectNotNull(new_point = EC_POINT_new(group));
  54883. ExpectNotNull(set_point = EC_POINT_new(group));
  54884. ExpectNotNull(X = BN_new());
  54885. ExpectNotNull(Y = BN_new());
  54886. ExpectNotNull(set_point_bn = BN_new());
  54887. ExpectNotNull(infinity = EC_POINT_new(group));
  54888. /* load test values */
  54889. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  54890. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  54891. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  54892. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  54893. /* populate coordinates for input point */
  54894. if (Gxy != NULL) {
  54895. Gxy->X = Gx;
  54896. Gxy->Y = Gy;
  54897. Gxy->Z = Gz;
  54898. }
  54899. /* Test handling of NULL point. */
  54900. EC_POINT_clear_free(NULL);
  54901. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54902. NULL, NULL, ctx), 0);
  54903. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  54904. NULL, NULL, ctx), 0);
  54905. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  54906. NULL, NULL, ctx), 0);
  54907. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54908. X, NULL, ctx), 0);
  54909. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54910. NULL, Y, ctx), 0);
  54911. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  54912. X, Y, ctx), 0);
  54913. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  54914. X, Y, ctx), 0);
  54915. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  54916. NULL, Y, ctx), 0);
  54917. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  54918. X, NULL, ctx), 0);
  54919. /* Getting point at infinity returns an error. */
  54920. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  54921. X, Y, ctx), 0);
  54922. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54923. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54924. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54925. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  54926. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  54927. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  54928. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  54929. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  54930. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  54931. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  54932. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  54933. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  54934. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  54935. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  54936. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  54937. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  54938. /* perform point multiplication */
  54939. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  54940. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54941. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54942. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54943. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  54944. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54945. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54946. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54947. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  54948. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54949. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54950. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54951. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  54952. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  54953. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  54954. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54955. /* Set point to something. */
  54956. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  54957. #else
  54958. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  54959. ctx), 1);
  54960. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54961. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54962. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54963. #endif
  54964. /* check if point X coordinate is zero */
  54965. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54966. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  54967. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  54968. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  54969. #endif
  54970. /* extract the coordinates from point */
  54971. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54972. ctx), WOLFSSL_SUCCESS);
  54973. /* check if point X coordinate is zero */
  54974. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  54975. /* set the same X and Y points in another object */
  54976. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  54977. ctx), WOLFSSL_SUCCESS);
  54978. /* compare points as they should be the same */
  54979. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  54980. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  54981. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  54982. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  54983. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  54984. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  54985. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  54986. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  54987. /* Test copying */
  54988. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  54989. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  54990. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  54991. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  54992. /* Test inverting */
  54993. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  54994. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  54995. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  54996. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  54997. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54998. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54999. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  55000. {
  55001. EC_POINT* orig_point = NULL;
  55002. ExpectNotNull(orig_point = EC_POINT_new(group));
  55003. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  55004. 1);
  55005. /* new_point should be set_point inverted so adding it will revert
  55006. * the point back to set_point */
  55007. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  55008. NULL), 1);
  55009. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  55010. EC_POINT_free(orig_point);
  55011. }
  55012. #endif
  55013. /* Test getting affine converts from projective. */
  55014. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  55015. /* Force non-affine coordinates */
  55016. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  55017. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  55018. if (new_point != NULL) {
  55019. new_point->inSet = 0;
  55020. }
  55021. /* extract the coordinates from point */
  55022. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  55023. ctx), WOLFSSL_SUCCESS);
  55024. /* check if point ordinates have changed. */
  55025. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  55026. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  55027. /* Test check for infinity */
  55028. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  55029. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  55030. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  55031. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  55032. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  55033. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  55034. #else
  55035. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  55036. #endif
  55037. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  55038. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  55039. /* check bn2hex */
  55040. hexStr = BN_bn2hex(k);
  55041. ExpectStrEQ(hexStr, kTest);
  55042. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  55043. defined(XFPRINTF)
  55044. BN_print_fp(stderr, k);
  55045. fprintf(stderr, "\n");
  55046. #endif
  55047. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55048. hexStr = BN_bn2hex(Gx);
  55049. ExpectStrEQ(hexStr, kGx);
  55050. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  55051. defined(XFPRINTF)
  55052. BN_print_fp(stderr, Gx);
  55053. fprintf(stderr, "\n");
  55054. #endif
  55055. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55056. hexStr = BN_bn2hex(Gy);
  55057. ExpectStrEQ(hexStr, kGy);
  55058. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  55059. defined(XFPRINTF)
  55060. BN_print_fp(stderr, Gy);
  55061. fprintf(stderr, "\n");
  55062. #endif
  55063. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55064. /* Test point to hex */
  55065. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  55066. ctx));
  55067. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55068. ctx));
  55069. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  55070. ctx));
  55071. #ifndef HAVE_ECC_BRAINPOOL
  55072. /* Group not supported in wolfCrypt. */
  55073. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55074. ctx));
  55075. #endif
  55076. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  55077. ExpectNotNull(hexStr);
  55078. ExpectStrEQ(hexStr, uncompG);
  55079. AssertNotNull(get_point = EC_POINT_hex2point(group, hexStr, NULL, ctx));
  55080. AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  55081. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55082. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  55083. ExpectNotNull(hexStr);
  55084. ExpectStrEQ(hexStr, compG);
  55085. #ifdef HAVE_COMP_KEY
  55086. AssertNotNull(get_point = EC_POINT_hex2point
  55087. (group, hexStr, get_point, ctx));
  55088. AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  55089. #endif
  55090. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55091. EC_POINT_free(get_point);
  55092. #ifndef HAVE_SELFTEST
  55093. /* Test point to oct */
  55094. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  55095. NULL, 0, ctx), 0);
  55096. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55097. NULL, 0, ctx), 0);
  55098. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  55099. NULL, 0, ctx), 0);
  55100. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55101. NULL, 0, ctx);
  55102. ExpectIntEQ(bin_len, sizeof(binUncompG));
  55103. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  55104. DYNAMIC_TYPE_ECC));
  55105. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55106. buf, bin_len, ctx), bin_len);
  55107. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  55108. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  55109. /* Infinity (x=0, y=0) encodes as '0x00'. */
  55110. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  55111. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  55112. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  55113. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  55114. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  55115. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  55116. ExpectIntEQ(bufInf[0], 0);
  55117. wolfSSL_EC_POINT_dump(NULL, NULL);
  55118. /* Test point i2d */
  55119. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  55120. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  55121. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  55122. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  55123. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  55124. ExpectIntEQ(blen, sizeof(binUncompG));
  55125. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  55126. blen -= 1;
  55127. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  55128. blen += 1;
  55129. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  55130. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  55131. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  55132. #ifdef HAVE_COMP_KEY
  55133. /* Test point to oct compressed */
  55134. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  55135. 0, ctx);
  55136. ExpectIntEQ(bin_len, sizeof(binCompG));
  55137. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  55138. DYNAMIC_TYPE_ECC));
  55139. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  55140. bin_len, ctx), bin_len);
  55141. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  55142. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  55143. #endif
  55144. /* Test point BN */
  55145. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  55146. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  55147. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  55148. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  55149. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  55150. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  55151. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  55152. /* Test oct to point */
  55153. ExpectNotNull(tmp = EC_POINT_new(group));
  55154. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  55155. ctx), 0);
  55156. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  55157. ctx), 0);
  55158. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  55159. ctx), 0);
  55160. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  55161. sizeof(binUncompGBad), ctx), 0);
  55162. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  55163. ctx), 1);
  55164. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  55165. EC_POINT_free(tmp);
  55166. tmp = NULL;
  55167. /* Test setting BN ordinates. */
  55168. ExpectNotNull(tmp = EC_POINT_new(group));
  55169. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  55170. NULL, ctx), 0);
  55171. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  55172. NULL, ctx), 0);
  55173. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  55174. NULL, ctx), 0);
  55175. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  55176. NULL, ctx), 0);
  55177. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  55178. Gy, ctx), 0);
  55179. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  55180. ctx), 0);
  55181. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  55182. ctx), 0);
  55183. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  55184. Gy, ctx), 0);
  55185. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  55186. NULL, ctx), 0);
  55187. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  55188. ctx), 1);
  55189. EC_POINT_free(tmp);
  55190. tmp = NULL;
  55191. /* Test point d2i */
  55192. ExpectNotNull(tmp = EC_POINT_new(group));
  55193. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  55194. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  55195. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  55196. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  55197. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  55198. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  55199. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  55200. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  55201. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  55202. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  55203. EC_POINT_free(tmp);
  55204. tmp = NULL;
  55205. #ifdef HAVE_COMP_KEY
  55206. /* Test oct compressed to point */
  55207. ExpectNotNull(tmp = EC_POINT_new(group));
  55208. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  55209. 1);
  55210. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  55211. EC_POINT_free(tmp);
  55212. tmp = NULL;
  55213. /* Test point d2i - compressed */
  55214. ExpectNotNull(tmp = EC_POINT_new(group));
  55215. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  55216. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  55217. EC_POINT_free(tmp);
  55218. tmp = NULL;
  55219. #endif
  55220. #endif
  55221. /* test BN_mod_add */
  55222. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  55223. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  55224. 1);
  55225. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  55226. /* cleanup */
  55227. BN_free(X);
  55228. BN_free(Y);
  55229. BN_free(k);
  55230. BN_free(set_point_bn);
  55231. EC_POINT_free(infinity);
  55232. EC_POINT_free(new_point);
  55233. EC_POINT_free(set_point);
  55234. EC_POINT_clear_free(Gxy);
  55235. #ifndef HAVE_ECC_BRAINPOOL
  55236. EC_GROUP_free(group2);
  55237. #endif
  55238. EC_GROUP_free(group);
  55239. BN_CTX_free(ctx);
  55240. #endif
  55241. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  55242. return EXPECT_RESULT();
  55243. }
  55244. static int test_wolfSSL_SPAKE(void)
  55245. {
  55246. EXPECT_DECLS;
  55247. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  55248. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  55249. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  55250. BIGNUM* x = NULL; /* kdc priv */
  55251. BIGNUM* y = NULL; /* client priv */
  55252. BIGNUM* w = NULL; /* shared value */
  55253. byte M_bytes[] = {
  55254. /* uncompressed */
  55255. 0x04,
  55256. /* x */
  55257. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  55258. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  55259. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  55260. /* y */
  55261. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  55262. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  55263. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  55264. };
  55265. EC_POINT* M = NULL; /* shared value */
  55266. byte N_bytes[] = {
  55267. /* uncompressed */
  55268. 0x04,
  55269. /* x */
  55270. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  55271. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  55272. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  55273. /* y */
  55274. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  55275. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  55276. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  55277. };
  55278. EC_POINT* N = NULL; /* shared value */
  55279. EC_POINT* T = NULL; /* kdc pub */
  55280. EC_POINT* tmp1 = NULL; /* kdc pub */
  55281. EC_POINT* tmp2 = NULL; /* kdc pub */
  55282. EC_POINT* S = NULL; /* client pub */
  55283. EC_POINT* client_secret = NULL;
  55284. EC_POINT* kdc_secret = NULL;
  55285. EC_GROUP* group = NULL;
  55286. BN_CTX* bn_ctx = NULL;
  55287. /* Values taken from a test run of Kerberos 5 */
  55288. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  55289. ExpectNotNull(bn_ctx = BN_CTX_new());
  55290. ExpectNotNull(M = EC_POINT_new(group));
  55291. ExpectNotNull(N = EC_POINT_new(group));
  55292. ExpectNotNull(T = EC_POINT_new(group));
  55293. ExpectNotNull(tmp1 = EC_POINT_new(group));
  55294. ExpectNotNull(tmp2 = EC_POINT_new(group));
  55295. ExpectNotNull(S = EC_POINT_new(group));
  55296. ExpectNotNull(client_secret = EC_POINT_new(group));
  55297. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  55298. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  55299. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  55300. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  55301. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  55302. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  55303. "506AB395478F0AAB647752CF117B36250"), 1);
  55304. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  55305. 1);
  55306. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  55307. 1);
  55308. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  55309. /* kdc */
  55310. /* T=x*P+w*M */
  55311. /* All in one function call */
  55312. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  55313. /* Spread into separate calls */
  55314. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  55315. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  55316. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  55317. 1);
  55318. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  55319. /* client */
  55320. /* S=y*P+w*N */
  55321. /* All in one function call */
  55322. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  55323. /* Spread into separate calls */
  55324. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  55325. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  55326. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  55327. 1);
  55328. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  55329. /* K=y*(T-w*M) */
  55330. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  55331. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  55332. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  55333. 1);
  55334. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  55335. bn_ctx), 1);
  55336. /* kdc */
  55337. /* K=x*(S-w*N) */
  55338. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  55339. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  55340. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  55341. 1);
  55342. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  55343. 1);
  55344. /* kdc_secret == client_secret */
  55345. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  55346. BN_free(x);
  55347. BN_free(y);
  55348. BN_free(w);
  55349. EC_POINT_free(M);
  55350. EC_POINT_free(N);
  55351. EC_POINT_free(T);
  55352. EC_POINT_free(tmp1);
  55353. EC_POINT_free(tmp2);
  55354. EC_POINT_free(S);
  55355. EC_POINT_free(client_secret);
  55356. EC_POINT_free(kdc_secret);
  55357. EC_GROUP_free(group);
  55358. BN_CTX_free(bn_ctx);
  55359. #endif
  55360. return EXPECT_RESULT();
  55361. }
  55362. static int test_wolfSSL_EC_KEY_generate(void)
  55363. {
  55364. EXPECT_DECLS;
  55365. #ifdef OPENSSL_EXTRA
  55366. WOLFSSL_EC_KEY* key = NULL;
  55367. #ifndef HAVE_ECC_BRAINPOOL
  55368. WOLFSSL_EC_GROUP* group = NULL;
  55369. #endif
  55370. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55371. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  55372. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  55373. wolfSSL_EC_KEY_free(key);
  55374. key = NULL;
  55375. #ifndef HAVE_ECC_BRAINPOOL
  55376. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  55377. NID_brainpoolP256r1));
  55378. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  55379. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  55380. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  55381. wolfSSL_EC_KEY_free(key);
  55382. wolfSSL_EC_GROUP_free(group);
  55383. #endif
  55384. #endif
  55385. return EXPECT_RESULT();
  55386. }
  55387. static int test_EC_i2d(void)
  55388. {
  55389. EXPECT_DECLS;
  55390. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  55391. EC_KEY *key = NULL;
  55392. EC_KEY *copy = NULL;
  55393. int len = 0;
  55394. unsigned char *buf = NULL;
  55395. unsigned char *p = NULL;
  55396. const unsigned char *tmp = NULL;
  55397. const unsigned char octBad[] = {
  55398. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  55399. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  55400. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  55401. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  55402. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  55403. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  55404. };
  55405. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55406. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  55407. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  55408. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  55409. DYNAMIC_TYPE_TMP_BUFFER));
  55410. p = buf;
  55411. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  55412. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  55413. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  55414. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  55415. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  55416. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  55417. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  55418. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  55419. tmp = buf;
  55420. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  55421. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  55422. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  55423. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  55424. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  55425. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  55426. tmp = buf;
  55427. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  55428. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  55429. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  55430. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  55431. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  55432. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  55433. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  55434. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  55435. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  55436. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  55437. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  55438. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  55439. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  55440. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55441. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  55442. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55443. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  55444. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55445. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  55446. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55447. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  55448. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  55449. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  55450. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  55451. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  55452. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  55453. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55454. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55455. buf = NULL;
  55456. buf = NULL;
  55457. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  55458. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  55459. DYNAMIC_TYPE_TMP_BUFFER));
  55460. p = buf;
  55461. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  55462. p = NULL;
  55463. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  55464. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55465. p = NULL;
  55466. /* Bad point is also an invalid private key. */
  55467. tmp = octBad;
  55468. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  55469. tmp = buf;
  55470. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  55471. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55472. buf = NULL;
  55473. buf = NULL;
  55474. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  55475. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  55476. DYNAMIC_TYPE_TMP_BUFFER));
  55477. p = buf;
  55478. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  55479. p = NULL;
  55480. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  55481. tmp = buf;
  55482. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  55483. tmp = octBad;
  55484. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  55485. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  55486. ExpectIntEQ(EC_KEY_check_key(key), 1);
  55487. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  55488. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  55489. EC_KEY_free(key);
  55490. EC_KEY_free(copy);
  55491. #endif
  55492. return EXPECT_RESULT();
  55493. }
  55494. static int test_wolfSSL_EC_curve(void)
  55495. {
  55496. EXPECT_DECLS;
  55497. #if defined(OPENSSL_EXTRA)
  55498. int nid = NID_secp160k1;
  55499. const char* nid_name = NULL;
  55500. ExpectNull(EC_curve_nid2nist(NID_sha256));
  55501. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  55502. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  55503. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  55504. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  55505. #endif
  55506. return EXPECT_RESULT();
  55507. }
  55508. static int test_wolfSSL_EC_KEY_dup(void)
  55509. {
  55510. EXPECT_DECLS;
  55511. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  55512. WOLFSSL_EC_KEY* ecKey = NULL;
  55513. WOLFSSL_EC_KEY* dupKey = NULL;
  55514. ecc_key* srcKey = NULL;
  55515. ecc_key* destKey = NULL;
  55516. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55517. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55518. /* Valid cases */
  55519. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55520. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  55521. /* Compare pubkey */
  55522. if (ecKey != NULL) {
  55523. srcKey = (ecc_key*)ecKey->internal;
  55524. }
  55525. if (dupKey != NULL) {
  55526. destKey = (ecc_key*)dupKey->internal;
  55527. }
  55528. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  55529. /* compare EC_GROUP */
  55530. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  55531. /* compare EC_POINT */
  55532. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  55533. dupKey->pub_key, NULL), MP_EQ);
  55534. /* compare BIGNUM */
  55535. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  55536. wolfSSL_EC_KEY_free(dupKey);
  55537. dupKey = NULL;
  55538. /* Invalid cases */
  55539. /* NULL key */
  55540. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  55541. /* NULL ecc_key */
  55542. if (ecKey != NULL) {
  55543. wc_ecc_free((ecc_key*)ecKey->internal);
  55544. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  55545. ecKey->internal = NULL; /* Set ecc_key to NULL */
  55546. }
  55547. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55548. wolfSSL_EC_KEY_free(ecKey);
  55549. ecKey = NULL;
  55550. wolfSSL_EC_KEY_free(dupKey);
  55551. dupKey = NULL;
  55552. /* NULL Group */
  55553. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55554. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55555. if (ecKey != NULL) {
  55556. wolfSSL_EC_GROUP_free(ecKey->group);
  55557. ecKey->group = NULL; /* Set group to NULL */
  55558. }
  55559. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55560. wolfSSL_EC_KEY_free(ecKey);
  55561. ecKey = NULL;
  55562. wolfSSL_EC_KEY_free(dupKey);
  55563. dupKey = NULL;
  55564. /* NULL public key */
  55565. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55566. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55567. if (ecKey != NULL) {
  55568. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  55569. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  55570. }
  55571. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55572. if (ecKey != NULL) {
  55573. wolfSSL_EC_POINT_free(ecKey->pub_key);
  55574. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  55575. }
  55576. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55577. wolfSSL_EC_KEY_free(ecKey);
  55578. ecKey = NULL;
  55579. wolfSSL_EC_KEY_free(dupKey);
  55580. dupKey = NULL;
  55581. /* NULL private key */
  55582. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55583. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55584. if (ecKey != NULL) {
  55585. wolfSSL_BN_free(ecKey->priv_key);
  55586. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  55587. }
  55588. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55589. wolfSSL_EC_KEY_free(ecKey);
  55590. ecKey = NULL;
  55591. wolfSSL_EC_KEY_free(dupKey);
  55592. dupKey = NULL;
  55593. /* Test EC_KEY_up_ref */
  55594. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55595. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  55596. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  55597. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  55598. /* reference count doesn't follow duplicate */
  55599. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55600. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  55601. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  55602. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  55603. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  55604. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  55605. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  55606. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  55607. #endif
  55608. return EXPECT_RESULT();
  55609. }
  55610. static int test_wolfSSL_EC_KEY_set_group(void)
  55611. {
  55612. EXPECT_DECLS;
  55613. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  55614. defined(OPENSSL_EXTRA)
  55615. EC_KEY *key = NULL;
  55616. EC_GROUP *group = NULL;
  55617. const EC_GROUP *group2 = NULL;
  55618. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  55619. ExpectNotNull(key = EC_KEY_new());
  55620. ExpectNull(EC_KEY_get0_group(NULL));
  55621. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  55622. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  55623. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  55624. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  55625. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  55626. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  55627. EC_GROUP_free(group);
  55628. EC_KEY_free(key);
  55629. #endif
  55630. return EXPECT_RESULT();
  55631. }
  55632. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  55633. {
  55634. EXPECT_DECLS;
  55635. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55636. BIO* bio = NULL;
  55637. EC_KEY* key = NULL;
  55638. /* Error condition: NULL key. */
  55639. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  55640. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  55641. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  55642. /* Conversion form defaults to uncompressed. */
  55643. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55644. #ifdef HAVE_COMP_KEY
  55645. /* Explicitly set to compressed. */
  55646. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  55647. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  55648. #else
  55649. /* Will still work just won't change anything. */
  55650. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  55651. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55652. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  55653. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55654. #endif
  55655. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  55656. BIO_free(bio);
  55657. EC_KEY_free(key);
  55658. #endif
  55659. return EXPECT_RESULT();
  55660. }
  55661. static int test_wolfSSL_EC_KEY_private_key(void)
  55662. {
  55663. EXPECT_DECLS;
  55664. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55665. WOLFSSL_EC_KEY* key = NULL;
  55666. WOLFSSL_BIGNUM* priv = NULL;
  55667. WOLFSSL_BIGNUM* priv2 = NULL;
  55668. WOLFSSL_BIGNUM* bn;
  55669. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55670. ExpectNotNull(priv = wolfSSL_BN_new());
  55671. ExpectNotNull(priv2 = wolfSSL_BN_new());
  55672. ExpectIntNE(BN_set_word(priv, 2), 0);
  55673. ExpectIntNE(BN_set_word(priv2, 2), 0);
  55674. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  55675. /* No private key set. */
  55676. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  55677. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  55678. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  55679. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  55680. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  55681. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  55682. ExpectPtrNE(bn, priv);
  55683. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  55684. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  55685. ExpectPtrNE(bn, priv2);
  55686. wolfSSL_BN_free(priv2);
  55687. wolfSSL_BN_free(priv);
  55688. wolfSSL_EC_KEY_free(key);
  55689. #endif
  55690. return EXPECT_RESULT();
  55691. }
  55692. static int test_wolfSSL_EC_KEY_public_key(void)
  55693. {
  55694. EXPECT_DECLS;
  55695. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55696. WOLFSSL_EC_KEY* key = NULL;
  55697. WOLFSSL_EC_POINT* pub = NULL;
  55698. WOLFSSL_EC_POINT* point = NULL;
  55699. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55700. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  55701. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  55702. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  55703. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  55704. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  55705. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  55706. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  55707. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  55708. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  55709. ExpectPtrEq(point, pub);
  55710. wolfSSL_EC_KEY_free(key);
  55711. #endif
  55712. return EXPECT_RESULT();
  55713. }
  55714. static int test_wolfSSL_EC_KEY_print_fp(void)
  55715. {
  55716. EXPECT_DECLS;
  55717. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  55718. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  55719. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  55720. !defined(NO_STDIO_FILESYSTEM)
  55721. EC_KEY* key = NULL;
  55722. /* Bad file pointer. */
  55723. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  55724. /* NULL key. */
  55725. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  55726. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  55727. /* Negative indent. */
  55728. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  55729. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55730. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  55731. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55732. wolfSSL_EC_KEY_free(key);
  55733. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  55734. NID_X9_62_prime256v1)));
  55735. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  55736. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55737. wolfSSL_EC_KEY_free(key);
  55738. #endif
  55739. return EXPECT_RESULT();
  55740. }
  55741. static int test_wolfSSL_EC_get_builtin_curves(void)
  55742. {
  55743. EXPECT_DECLS;
  55744. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  55745. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  55746. EC_builtin_curve* curves = NULL;
  55747. size_t crv_len = 0;
  55748. size_t i = 0;
  55749. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  55750. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  55751. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  55752. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  55753. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  55754. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  55755. if (curves[i].comment != NULL) {
  55756. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  55757. }
  55758. }
  55759. if (crv_len > 1) {
  55760. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  55761. }
  55762. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55763. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  55764. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  55765. return EXPECT_RESULT();
  55766. }
  55767. static int test_wolfSSL_ECDSA_SIG(void)
  55768. {
  55769. EXPECT_DECLS;
  55770. #ifdef OPENSSL_EXTRA
  55771. WOLFSSL_ECDSA_SIG* sig = NULL;
  55772. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  55773. WOLFSSL_BIGNUM* r = NULL;
  55774. WOLFSSL_BIGNUM* s = NULL;
  55775. const WOLFSSL_BIGNUM* r2 = NULL;
  55776. const WOLFSSL_BIGNUM* s2 = NULL;
  55777. const unsigned char* cp = NULL;
  55778. unsigned char* p = NULL;
  55779. unsigned char outSig[8];
  55780. unsigned char sigData[8] =
  55781. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  55782. unsigned char sigDataBad[8] =
  55783. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  55784. wolfSSL_ECDSA_SIG_free(NULL);
  55785. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  55786. ExpectNotNull(r = wolfSSL_BN_new());
  55787. ExpectNotNull(s = wolfSSL_BN_new());
  55788. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  55789. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  55790. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  55791. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  55792. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  55793. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  55794. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  55795. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  55796. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  55797. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  55798. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  55799. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  55800. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  55801. r2 = NULL;
  55802. s2 = NULL;
  55803. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  55804. ExpectNull(r2);
  55805. ExpectNull(s2);
  55806. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  55807. if (EXPECT_FAIL()) {
  55808. wolfSSL_BN_free(r);
  55809. wolfSSL_BN_free(s);
  55810. }
  55811. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  55812. ExpectPtrEq(r2, r);
  55813. ExpectPtrEq(s2, s);
  55814. r2 = NULL;
  55815. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  55816. ExpectPtrEq(r2, r);
  55817. s2 = NULL;
  55818. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  55819. ExpectPtrEq(s2, s);
  55820. /* r and s are freed when sig is freed. */
  55821. wolfSSL_ECDSA_SIG_free(sig);
  55822. sig = NULL;
  55823. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  55824. cp = sigDataBad;
  55825. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  55826. cp = sigData;
  55827. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  55828. ExpectIntEQ((cp == sigData + 8), 1);
  55829. cp = sigData;
  55830. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  55831. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  55832. ExpectIntEQ((sig == sig2), 1);
  55833. cp = outSig;
  55834. p = outSig;
  55835. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  55836. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  55837. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  55838. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  55839. ExpectIntEQ((p == outSig + 8), 1);
  55840. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  55841. wolfSSL_ECDSA_SIG_free(sig);
  55842. #endif
  55843. return EXPECT_RESULT();
  55844. }
  55845. static int test_ECDSA_size_sign(void)
  55846. {
  55847. EXPECT_DECLS;
  55848. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  55849. EC_KEY* key = NULL;
  55850. ECDSA_SIG* ecdsaSig = NULL;
  55851. int id;
  55852. byte hash[WC_MAX_DIGEST_SIZE];
  55853. byte hash2[WC_MAX_DIGEST_SIZE];
  55854. byte sig[ECC_MAX_SIG_SIZE];
  55855. unsigned int sigSz = sizeof(sig);
  55856. XMEMSET(hash, 123, sizeof(hash));
  55857. XMEMSET(hash2, 234, sizeof(hash2));
  55858. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  55859. ExpectIntEQ(id, ECC_SECP256R1);
  55860. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55861. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  55862. ExpectIntGE(ECDSA_size(NULL), 0);
  55863. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  55864. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  55865. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  55866. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, NULL), 0);
  55867. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, (int)sigSz, key), 0);
  55868. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, (int)sigSz, key), 0);
  55869. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  55870. ExpectIntGE(ECDSA_size(key), sigSz);
  55871. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, key), 1);
  55872. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, (int)sigSz, key), 0);
  55873. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  55874. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  55875. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  55876. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  55877. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  55878. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  55879. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  55880. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  55881. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  55882. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  55883. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  55884. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  55885. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  55886. ECDSA_SIG_free(ecdsaSig);
  55887. EC_KEY_free(key);
  55888. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  55889. return EXPECT_RESULT();
  55890. }
  55891. static int test_ECDH_compute_key(void)
  55892. {
  55893. EXPECT_DECLS;
  55894. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  55895. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  55896. EC_KEY* key1 = NULL;
  55897. EC_KEY* key2 = NULL;
  55898. EC_POINT* pub1 = NULL;
  55899. EC_POINT* pub2 = NULL;
  55900. byte secret1[32];
  55901. byte secret2[32];
  55902. int i;
  55903. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55904. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  55905. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  55906. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55907. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  55908. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  55909. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  55910. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  55911. 0);
  55912. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  55913. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  55914. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  55915. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  55916. 0);
  55917. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  55918. 0);
  55919. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  55920. NULL), 0);
  55921. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  55922. sizeof(secret1));
  55923. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  55924. sizeof(secret2));
  55925. for (i = 0; i < (int)sizeof(secret1); i++) {
  55926. ExpectIntEQ(secret1[i], secret2[i]);
  55927. }
  55928. EC_KEY_free(key2);
  55929. EC_KEY_free(key1);
  55930. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  55931. * !WOLF_CRYPTO_CB_ONLY_ECC */
  55932. return EXPECT_RESULT();
  55933. }
  55934. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  55935. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  55936. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  55937. !defined(NO_ASN_TIME)
  55938. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  55939. int expectedDerSz)
  55940. {
  55941. EXPECT_DECLS;
  55942. X509* x509 = NULL;
  55943. BIGNUM* serial_number = NULL;
  55944. X509_NAME* name = NULL;
  55945. time_t epoch_off = 0;
  55946. ASN1_INTEGER* asn1_serial_number;
  55947. long not_before, not_after;
  55948. int derSz;
  55949. ExpectNotNull(x509 = X509_new());
  55950. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  55951. ExpectNotNull(serial_number = BN_new());
  55952. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  55953. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  55954. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  55955. /* version 3 */
  55956. ExpectIntNE(X509_set_version(x509, 2L), 0);
  55957. ExpectNotNull(name = X509_NAME_new());
  55958. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  55959. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  55960. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  55961. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  55962. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  55963. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  55964. not_before = (long)wc_Time(NULL);
  55965. not_after = not_before + (365 * 24 * 60 * 60);
  55966. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  55967. &epoch_off));
  55968. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  55969. &epoch_off));
  55970. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  55971. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  55972. ExpectIntGE(derSz, expectedDerSz);
  55973. BN_free(serial_number);
  55974. X509_NAME_free(name);
  55975. X509_free(x509);
  55976. return EXPECT_RESULT();
  55977. }
  55978. #endif
  55979. static int test_openssl_generate_key_and_cert(void)
  55980. {
  55981. EXPECT_DECLS;
  55982. #if defined(OPENSSL_EXTRA)
  55983. int expectedDerSz;
  55984. EVP_PKEY* pkey = NULL;
  55985. #ifdef HAVE_ECC
  55986. EC_KEY* ec_key = NULL;
  55987. #endif
  55988. #if !defined(NO_RSA)
  55989. int key_length = 2048;
  55990. BIGNUM* exponent = NULL;
  55991. RSA* rsa = NULL;
  55992. ExpectNotNull(pkey = EVP_PKEY_new());
  55993. ExpectNotNull(exponent = BN_new());
  55994. ExpectNotNull(rsa = RSA_new());
  55995. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  55996. #ifndef WOLFSSL_KEY_GEN
  55997. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  55998. #if defined(USE_CERT_BUFFERS_1024)
  55999. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  56000. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  56001. key_length = 1024;
  56002. #elif defined(USE_CERT_BUFFERS_2048)
  56003. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  56004. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  56005. #else
  56006. RSA_free(rsa);
  56007. rsa = NULL;
  56008. #endif
  56009. #else
  56010. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  56011. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  56012. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  56013. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  56014. #endif
  56015. if (rsa) {
  56016. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  56017. if (EXPECT_FAIL()) {
  56018. RSA_free(rsa);
  56019. }
  56020. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  56021. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  56022. expectedDerSz = 743;
  56023. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  56024. expectedDerSz), TEST_SUCCESS);
  56025. #endif
  56026. }
  56027. EVP_PKEY_free(pkey);
  56028. pkey = NULL;
  56029. BN_free(exponent);
  56030. #endif /* !NO_RSA */
  56031. #ifdef HAVE_ECC
  56032. ExpectNotNull(pkey = EVP_PKEY_new());
  56033. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  56034. #ifndef NO_WOLFSSL_STUB
  56035. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  56036. #endif
  56037. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  56038. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  56039. if (EXPECT_FAIL()) {
  56040. EC_KEY_free(ec_key);
  56041. }
  56042. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  56043. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  56044. expectedDerSz = 344;
  56045. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  56046. TEST_SUCCESS);
  56047. #endif
  56048. EVP_PKEY_free(pkey);
  56049. #endif /* HAVE_ECC */
  56050. (void)pkey;
  56051. (void)expectedDerSz;
  56052. #endif /* OPENSSL_EXTRA */
  56053. return EXPECT_RESULT();
  56054. }
  56055. static int test_stubs_are_stubs(void)
  56056. {
  56057. EXPECT_DECLS;
  56058. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  56059. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  56060. WOLFSSL_CTX* ctx = NULL;
  56061. WOLFSSL_CTX* ctxN = NULL;
  56062. #ifndef NO_WOLFSSL_CLIENT
  56063. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56064. #elif !defined(NO_WOLFSSL_SERVER)
  56065. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56066. #endif
  56067. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  56068. ExpectIntEQ((int) x(z), 0)
  56069. /* test logic, all stubs return same result regardless of ctx being NULL
  56070. * as there are no sanity checks, it's just a stub! If at some
  56071. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  56072. * if invalid inputs are supplied. Test calling both
  56073. * with and without valid inputs, if a stub functionality remains unchanged.
  56074. */
  56075. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  56076. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  56077. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  56078. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  56079. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  56080. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  56081. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  56082. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  56083. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  56084. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  56085. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  56086. wolfSSL_CTX_free(ctx);
  56087. ctx = NULL;
  56088. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  56089. * !NO_WOLFSSL_SERVER) */
  56090. return EXPECT_RESULT();
  56091. }
  56092. static int test_CONF_modules_xxx(void)
  56093. {
  56094. int res = TEST_SKIPPED;
  56095. #if defined(OPENSSL_EXTRA)
  56096. CONF_modules_free();
  56097. CONF_modules_unload(0);
  56098. CONF_modules_unload(1);
  56099. CONF_modules_unload(-1);
  56100. res = TEST_SUCCESS;
  56101. #endif /* OPENSSL_EXTRA */
  56102. return res;
  56103. }
  56104. static int test_CRYPTO_set_dynlock_xxx(void)
  56105. {
  56106. int res = TEST_SKIPPED;
  56107. #if defined(OPENSSL_EXTRA)
  56108. CRYPTO_set_dynlock_create_callback(
  56109. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  56110. CRYPTO_set_dynlock_create_callback(
  56111. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  56112. CRYPTO_set_dynlock_destroy_callback(
  56113. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  56114. CRYPTO_set_dynlock_destroy_callback(
  56115. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  56116. CRYPTO_set_dynlock_lock_callback(
  56117. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  56118. CRYPTO_set_dynlock_lock_callback(
  56119. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  56120. res = TEST_SUCCESS;
  56121. #endif /* OPENSSL_EXTRA */
  56122. return res;
  56123. }
  56124. static int test_CRYPTO_THREADID_xxx(void)
  56125. {
  56126. EXPECT_DECLS;
  56127. #if defined(OPENSSL_EXTRA)
  56128. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  56129. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  56130. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  56131. #endif /* OPENSSL_EXTRA */
  56132. return EXPECT_RESULT();
  56133. }
  56134. static int test_ENGINE_cleanup(void)
  56135. {
  56136. int res = TEST_SKIPPED;
  56137. #if defined(OPENSSL_EXTRA)
  56138. ENGINE_cleanup();
  56139. res = TEST_SUCCESS;
  56140. #endif /* OPENSSL_EXTRA */
  56141. return res;
  56142. }
  56143. static int test_wolfSSL_CTX_LoadCRL(void)
  56144. {
  56145. EXPECT_DECLS;
  56146. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  56147. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  56148. WOLFSSL_CTX* ctx = NULL;
  56149. WOLFSSL* ssl = NULL;
  56150. const char* badPath = "dummypath";
  56151. const char* validPath = "./certs/crl";
  56152. const char* validFilePath = "./certs/crl/cliCrl.pem";
  56153. const char* issuerCert = "./certs/client-cert.pem";
  56154. int derType = WOLFSSL_FILETYPE_ASN1;
  56155. int pemType = WOLFSSL_FILETYPE_PEM;
  56156. #ifdef HAVE_CRL_MONITOR
  56157. int monitor = WOLFSSL_CRL_MONITOR;
  56158. #else
  56159. int monitor = 0;
  56160. #endif
  56161. WOLFSSL_CERT_MANAGER* cm = NULL;
  56162. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  56163. BAD_FUNC_ARG)
  56164. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  56165. NOT_COMPILED_IN)
  56166. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  56167. WOLFSSL_SUCCESS)
  56168. #ifndef NO_WOLFSSL_CLIENT
  56169. #define NEW_CTX(ctx) ExpectNotNull( \
  56170. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  56171. #elif !defined(NO_WOLFSSL_SERVER)
  56172. #define NEW_CTX(ctx) ExpectNotNull( \
  56173. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  56174. #else
  56175. #define NEW_CTX(ctx) return
  56176. #endif
  56177. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  56178. NEW_CTX(ctx);
  56179. #ifndef HAVE_CRL_MONITOR
  56180. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  56181. wolfSSL_CTX_free(ctx);
  56182. NEW_CTX(ctx);
  56183. #endif
  56184. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  56185. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  56186. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  56187. wolfSSL_CTX_free(ctx);
  56188. ctx = NULL;
  56189. NEW_CTX(ctx);
  56190. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  56191. WOLFSSL_SUCCESS);
  56192. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  56193. wolfSSL_CTX_free(ctx);
  56194. ctx = NULL;
  56195. NEW_CTX(ctx);
  56196. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  56197. WOLFSSL_SUCCESS);
  56198. ExpectNotNull(ssl = wolfSSL_new(ctx));
  56199. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  56200. wolfSSL_free(ssl);
  56201. ssl = NULL;
  56202. wolfSSL_CTX_free(ctx);
  56203. ctx = NULL;
  56204. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56205. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  56206. WOLFSSL_SUCCESS);
  56207. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  56208. WOLFSSL_SUCCESS);
  56209. wolfSSL_CertManagerFree(cm);
  56210. #endif
  56211. return EXPECT_RESULT();
  56212. }
  56213. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  56214. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  56215. {
  56216. EXPECT_DECLS;
  56217. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56218. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  56219. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56220. return EXPECT_RESULT();
  56221. }
  56222. #endif
  56223. static int test_multiple_crls_same_issuer(void)
  56224. {
  56225. EXPECT_DECLS;
  56226. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  56227. test_ssl_cbf client_cbs, server_cbs;
  56228. struct {
  56229. const char* server_cert;
  56230. const char* server_key;
  56231. } test_params[] = {
  56232. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  56233. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  56234. };
  56235. size_t i;
  56236. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  56237. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56238. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56239. server_cbs.certPemFile = test_params[i].server_cert;
  56240. server_cbs.keyPemFile = test_params[i].server_key;
  56241. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  56242. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  56243. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56244. &server_cbs, NULL), TEST_FAIL);
  56245. }
  56246. #endif
  56247. return EXPECT_RESULT();
  56248. }
  56249. static int test_SetTmpEC_DHE_Sz(void)
  56250. {
  56251. EXPECT_DECLS;
  56252. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  56253. WOLFSSL_CTX *ctx = NULL;
  56254. WOLFSSL *ssl = NULL;
  56255. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56256. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  56257. ExpectNotNull(ssl = wolfSSL_new(ctx));
  56258. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  56259. wolfSSL_free(ssl);
  56260. wolfSSL_CTX_free(ctx);
  56261. #endif
  56262. return EXPECT_RESULT();
  56263. }
  56264. static int test_wolfSSL_CTX_get0_privatekey(void)
  56265. {
  56266. EXPECT_DECLS;
  56267. #ifdef OPENSSL_ALL
  56268. WOLFSSL_CTX* ctx = NULL;
  56269. (void)ctx;
  56270. #ifndef NO_RSA
  56271. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56272. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  56273. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  56274. WOLFSSL_FILETYPE_PEM));
  56275. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  56276. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  56277. WOLFSSL_FILETYPE_PEM));
  56278. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  56279. wolfSSL_CTX_free(ctx);
  56280. ctx = NULL;
  56281. #endif
  56282. #ifdef HAVE_ECC
  56283. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56284. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  56285. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  56286. WOLFSSL_FILETYPE_PEM));
  56287. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  56288. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  56289. WOLFSSL_FILETYPE_PEM));
  56290. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  56291. wolfSSL_CTX_free(ctx);
  56292. #endif
  56293. #endif
  56294. return EXPECT_RESULT();
  56295. }
  56296. static int test_wolfSSL_dtls_set_mtu(void)
  56297. {
  56298. EXPECT_DECLS;
  56299. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  56300. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  56301. !defined(WOLFSSL_NO_TLS12)
  56302. WOLFSSL_CTX* ctx = NULL;
  56303. WOLFSSL* ssl = NULL;
  56304. const char* testCertFile;
  56305. const char* testKeyFile;
  56306. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  56307. #ifndef NO_RSA
  56308. testCertFile = svrCertFile;
  56309. testKeyFile = svrKeyFile;
  56310. #elif defined(HAVE_ECC)
  56311. testCertFile = eccCertFile;
  56312. testKeyFile = eccKeyFile;
  56313. #endif
  56314. if (testCertFile != NULL && testKeyFile != NULL) {
  56315. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  56316. WOLFSSL_FILETYPE_PEM));
  56317. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56318. WOLFSSL_FILETYPE_PEM));
  56319. }
  56320. ExpectNotNull(ssl = wolfSSL_new(ctx));
  56321. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  56322. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  56323. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  56324. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  56325. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  56326. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  56327. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  56328. wolfSSL_free(ssl);
  56329. wolfSSL_CTX_free(ctx);
  56330. #endif
  56331. return EXPECT_RESULT();
  56332. }
  56333. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  56334. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56335. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  56336. enum HandShakeType hsType, word16 length)
  56337. {
  56338. size_t idx = 0;
  56339. byte* l;
  56340. /* record layer */
  56341. /* handshake type */
  56342. out[idx++] = handshake;
  56343. /* protocol version */
  56344. out[idx++] = 0xfe;
  56345. out[idx++] = 0xfd; /* DTLS 1.2 */
  56346. /* epoch 0 */
  56347. XMEMSET(out + idx, 0, 2);
  56348. idx += 2;
  56349. /* sequence number */
  56350. XMEMSET(out + idx, 0, 6);
  56351. c32toa(seq, out + idx + 2);
  56352. idx += 6;
  56353. /* length in BE */
  56354. if (length)
  56355. c16toa(length, out + idx);
  56356. else
  56357. c16toa(outSz - idx - 2, out + idx);
  56358. idx += 2;
  56359. /* handshake layer */
  56360. /* handshake type */
  56361. out[idx++] = (byte)hsType;
  56362. /* length */
  56363. l = out + idx;
  56364. idx += 3;
  56365. /* message seq */
  56366. c16toa(0, out + idx);
  56367. idx += 2;
  56368. /* frag offset */
  56369. c32to24(0, out + idx);
  56370. idx += 3;
  56371. /* frag length */
  56372. c32to24((word32)outSz - (word32)idx - 3, l);
  56373. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  56374. idx += 3;
  56375. XMEMSET(out + idx, 0, outSz - idx);
  56376. }
  56377. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  56378. {
  56379. byte msg[] = "This is a msg for the client";
  56380. byte reply[40];
  56381. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  56382. reply[sizeof(reply) - 1] = '\0';
  56383. fprintf(stderr, "Client message: %s\n", reply);
  56384. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  56385. }
  56386. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  56387. {
  56388. byte ch[50];
  56389. int fd = wolfSSL_get_fd(ssl);
  56390. byte msg[] = "This is a msg for the server";
  56391. byte reply[40];
  56392. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  56393. /* Server should ignore this datagram */
  56394. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  56395. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  56396. /* Server should ignore this datagram */
  56397. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  56398. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  56399. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  56400. reply[sizeof(reply) - 1] = '\0';
  56401. fprintf(stderr, "Server response: %s\n", reply);
  56402. }
  56403. static int test_wolfSSL_dtls_plaintext(void)
  56404. {
  56405. callback_functions func_cb_client;
  56406. callback_functions func_cb_server;
  56407. size_t i;
  56408. struct test_params {
  56409. method_provider client_meth;
  56410. method_provider server_meth;
  56411. ssl_callback on_result_server;
  56412. ssl_callback on_result_client;
  56413. } params[] = {
  56414. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56415. test_wolfSSL_dtls_plaintext_server,
  56416. test_wolfSSL_dtls_plaintext_client},
  56417. };
  56418. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56419. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56420. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56421. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56422. func_cb_server.method = params[i].server_meth;
  56423. func_cb_client.method = params[i].client_meth;
  56424. func_cb_client.on_result = params[i].on_result_client;
  56425. func_cb_server.on_result = params[i].on_result_server;
  56426. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56427. if (!func_cb_client.return_code)
  56428. return TEST_FAIL;
  56429. if (!func_cb_server.return_code)
  56430. return TEST_FAIL;
  56431. }
  56432. return TEST_RES_CHECK(1);
  56433. }
  56434. #else
  56435. static int test_wolfSSL_dtls_plaintext(void) {
  56436. return TEST_SKIPPED;
  56437. }
  56438. #endif
  56439. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  56440. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56441. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  56442. {
  56443. byte b[1100]; /* buffer for the messages to send */
  56444. size_t idx = 0;
  56445. size_t seq_offset = 0;
  56446. size_t msg_offset = 0;
  56447. int i;
  56448. int fd = wolfSSL_get_fd(ssl);
  56449. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  56450. word32 seq_number = 100; /* start high so server definitely reads this */
  56451. word16 msg_number = 50; /* start high so server has to buffer this */
  56452. AssertIntEQ(ret, 1);
  56453. /* Now let's start spamming the peer with fragments it needs to store */
  56454. XMEMSET(b, -1, sizeof(b));
  56455. /* record layer */
  56456. /* handshake type */
  56457. b[idx++] = 22;
  56458. /* protocol version */
  56459. b[idx++] = 0xfe;
  56460. b[idx++] = 0xfd; /* DTLS 1.2 */
  56461. /* epoch 0 */
  56462. XMEMSET(b + idx, 0, 2);
  56463. idx += 2;
  56464. /* sequence number */
  56465. XMEMSET(b + idx, 0, 6);
  56466. seq_offset = idx + 2; /* increment only the low 32 bits */
  56467. idx += 6;
  56468. /* static length in BE */
  56469. c16toa(42, b + idx);
  56470. idx += 2;
  56471. /* handshake layer */
  56472. /* cert type */
  56473. b[idx++] = 11;
  56474. /* length */
  56475. c32to24(1000, b + idx);
  56476. idx += 3;
  56477. /* message seq */
  56478. c16toa(0, b + idx);
  56479. msg_offset = idx;
  56480. idx += 2;
  56481. /* frag offset */
  56482. c32to24(500, b + idx);
  56483. idx += 3;
  56484. /* frag length */
  56485. c32to24(30, b + idx);
  56486. idx += 3;
  56487. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  56488. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  56489. seq_number++, msg_number++, i++) {
  56490. struct timespec delay;
  56491. XMEMSET(&delay, 0, sizeof(delay));
  56492. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  56493. c32toa(seq_number, b + seq_offset);
  56494. c16toa(msg_number, b + msg_offset);
  56495. ret = (int)send(fd, b, 55, 0);
  56496. nanosleep(&delay, NULL);
  56497. }
  56498. }
  56499. #ifdef WOLFSSL_DTLS13
  56500. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  56501. {
  56502. const word16 sendCountMax = 100;
  56503. byte b[150]; /* buffer for the messages to send */
  56504. size_t idx = 0;
  56505. size_t msg_offset = 0;
  56506. int fd = wolfSSL_get_fd(ssl);
  56507. word16 msg_number = 10; /* start high so server has to buffer this */
  56508. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  56509. AssertIntEQ(ret, 1);
  56510. /* Now let's start spamming the peer with fragments it needs to store */
  56511. XMEMSET(b, -1, sizeof(b));
  56512. /* handshake type */
  56513. b[idx++] = 11;
  56514. /* length */
  56515. c32to24(10000, b + idx);
  56516. idx += 3;
  56517. /* message_seq */
  56518. msg_offset = idx;
  56519. idx += 2;
  56520. /* fragment_offset */
  56521. c32to24(5000, b + idx);
  56522. idx += 3;
  56523. /* fragment_length */
  56524. c32to24(100, b + idx);
  56525. idx += 3;
  56526. /* fragment contents */
  56527. idx += 100;
  56528. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  56529. byte sendBuf[150];
  56530. int sendSz = sizeof(sendBuf);
  56531. struct timespec delay;
  56532. XMEMSET(&delay, 0, sizeof(delay));
  56533. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  56534. c16toa(msg_number, b + msg_offset);
  56535. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  56536. (int)idx, handshake, 0, 0, 0);
  56537. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  56538. nanosleep(&delay, NULL);
  56539. }
  56540. }
  56541. #endif
  56542. static int test_wolfSSL_dtls_fragments(void)
  56543. {
  56544. EXPECT_DECLS;
  56545. callback_functions func_cb_client;
  56546. callback_functions func_cb_server;
  56547. size_t i;
  56548. struct test_params {
  56549. method_provider client_meth;
  56550. method_provider server_meth;
  56551. ssl_callback spammer;
  56552. } params[] = {
  56553. #if !defined(WOLFSSL_NO_TLS12)
  56554. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56555. test_wolfSSL_dtls12_fragments_spammer},
  56556. #endif
  56557. #ifdef WOLFSSL_DTLS13
  56558. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56559. test_wolfSSL_dtls13_fragments_spammer},
  56560. #endif
  56561. };
  56562. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56563. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56564. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56565. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56566. func_cb_server.method = params[i].server_meth;
  56567. func_cb_client.method = params[i].client_meth;
  56568. func_cb_client.ssl_ready = params[i].spammer;
  56569. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56570. ExpectFalse(func_cb_client.return_code);
  56571. ExpectFalse(func_cb_server.return_code);
  56572. /* The socket should be closed by the server resulting in a
  56573. * socket error, fatal error or reading a close notify alert */
  56574. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  56575. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  56576. func_cb_client.last_err != FATAL_ERROR) {
  56577. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  56578. }
  56579. /* Check the server returned an error indicating the msg buffer
  56580. * was full */
  56581. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  56582. if (EXPECT_FAIL())
  56583. break;
  56584. }
  56585. return EXPECT_RESULT();
  56586. }
  56587. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  56588. {
  56589. int fd, ret;
  56590. byte alert_msg[] = {
  56591. 0x15, /* alert type */
  56592. 0xfe, 0xfd, /* version */
  56593. 0x00, 0x00, /* epoch */
  56594. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  56595. 0x00, 0x02, /* length */
  56596. 0x02, /* level: fatal */
  56597. 0x46 /* protocol version */
  56598. };
  56599. fd = wolfSSL_get_fd(ssl);
  56600. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  56601. AssertIntGT(ret, 0);
  56602. }
  56603. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  56604. {
  56605. callback_functions client_cbs, server_cbs;
  56606. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56607. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56608. client_cbs.doUdp = server_cbs.doUdp = 1;
  56609. if (version12) {
  56610. #if !defined(WOLFSSL_NO_TLS12)
  56611. client_cbs.method = wolfDTLSv1_2_client_method;
  56612. server_cbs.method = wolfDTLSv1_2_server_method;
  56613. #else
  56614. return TEST_SKIPPED;
  56615. #endif
  56616. }
  56617. else
  56618. {
  56619. #ifdef WOLFSSL_DTLS13
  56620. client_cbs.method = wolfDTLSv1_3_client_method;
  56621. server_cbs.method = wolfDTLSv1_3_server_method;
  56622. #else
  56623. return TEST_SKIPPED;
  56624. #endif /* WOLFSSL_DTLS13 */
  56625. }
  56626. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  56627. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56628. if (!client_cbs.return_code)
  56629. return TEST_FAIL;
  56630. if (!server_cbs.return_code)
  56631. return TEST_FAIL;
  56632. return TEST_SUCCESS;
  56633. }
  56634. static int test_wolfSSL_ignore_alert_before_cookie(void)
  56635. {
  56636. int ret;
  56637. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  56638. if (ret != 0)
  56639. return ret;
  56640. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  56641. if (ret != 0)
  56642. return ret;
  56643. return 0;
  56644. }
  56645. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  56646. {
  56647. int ret;
  56648. int fd;
  56649. byte bad_msg[] = {
  56650. 0x17, /* app data */
  56651. 0xaa, 0xfd, /* bad version */
  56652. 0x00, 0x01, /* epoch 1 */
  56653. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  56654. 0x00, 0x26, /* length: 38 bytes */
  56655. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  56656. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  56657. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  56658. 0x03, 0x18, 0x72
  56659. };
  56660. fd = wolfSSL_get_fd(ssl);
  56661. AssertIntGE(fd, 0);
  56662. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  56663. AssertIntEQ(ret, sizeof(bad_msg));
  56664. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  56665. AssertIntEQ(ret, sizeof("badrecordtest"));
  56666. }
  56667. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  56668. {
  56669. byte buf[100];
  56670. int ret;
  56671. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  56672. AssertIntGT(ret, 0);
  56673. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  56674. }
  56675. static int _test_wolfSSL_dtls_bad_record(
  56676. method_provider client_method, method_provider server_method)
  56677. {
  56678. callback_functions client_cbs, server_cbs;
  56679. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56680. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56681. client_cbs.doUdp = server_cbs.doUdp = 1;
  56682. client_cbs.method = client_method;
  56683. server_cbs.method = server_method;
  56684. client_cbs.on_result = test_wolfSSL_send_bad_record;
  56685. server_cbs.on_result = test_wolfSSL_read_string;
  56686. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56687. if (!client_cbs.return_code)
  56688. return TEST_FAIL;
  56689. if (!server_cbs.return_code)
  56690. return TEST_FAIL;
  56691. return TEST_SUCCESS;
  56692. }
  56693. static int test_wolfSSL_dtls_bad_record(void)
  56694. {
  56695. int ret = TEST_SUCCESS;
  56696. #if !defined(WOLFSSL_NO_TLS12)
  56697. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  56698. wolfDTLSv1_2_server_method);
  56699. #endif
  56700. #ifdef WOLFSSL_DTLS13
  56701. if (ret == TEST_SUCCESS) {
  56702. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  56703. wolfDTLSv1_3_server_method);
  56704. }
  56705. #endif /* WOLFSSL_DTLS13 */
  56706. return ret;
  56707. }
  56708. #else
  56709. static int test_wolfSSL_dtls_fragments(void) {
  56710. return TEST_SKIPPED;
  56711. }
  56712. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  56713. return TEST_SKIPPED;
  56714. }
  56715. static int test_wolfSSL_dtls_bad_record(void) {
  56716. return TEST_SKIPPED;
  56717. }
  56718. #endif
  56719. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  56720. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56721. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56722. static byte test_AEAD_fail_decryption = 0;
  56723. static byte test_AEAD_seq_num = 0;
  56724. static byte test_AEAD_done = 0;
  56725. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  56726. {
  56727. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  56728. if (ret > 0) {
  56729. if (test_AEAD_fail_decryption) {
  56730. /* Modify the packet to trigger a decryption failure */
  56731. buf[ret/2] ^= 0xFF;
  56732. if (test_AEAD_fail_decryption == 1)
  56733. test_AEAD_fail_decryption = 0;
  56734. }
  56735. }
  56736. (void)ctx;
  56737. return ret;
  56738. }
  56739. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  56740. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  56741. {
  56742. if (sendLimit)
  56743. w64Zero(sendLimit);
  56744. switch (ssl->specs.bulk_cipher_algorithm) {
  56745. case wolfssl_aes_gcm:
  56746. if (sendLimit)
  56747. *sendLimit = AEAD_AES_LIMIT;
  56748. FALL_THROUGH;
  56749. case wolfssl_chacha:
  56750. if (hardLimit)
  56751. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  56752. if (keyUpdateLimit)
  56753. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  56754. break;
  56755. case wolfssl_aes_ccm:
  56756. if (sendLimit)
  56757. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  56758. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  56759. if (hardLimit)
  56760. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  56761. if (keyUpdateLimit)
  56762. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  56763. }
  56764. else {
  56765. if (hardLimit)
  56766. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  56767. if (keyUpdateLimit)
  56768. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  56769. }
  56770. break;
  56771. default:
  56772. fprintf(stderr, "Unrecognized bulk cipher");
  56773. AssertFalse(1);
  56774. break;
  56775. }
  56776. }
  56777. static void test_AEAD_limit_client(WOLFSSL* ssl)
  56778. {
  56779. int ret;
  56780. int i;
  56781. int didReKey = 0;
  56782. char msgBuf[20];
  56783. w64wrapper hardLimit;
  56784. w64wrapper keyUpdateLimit;
  56785. w64wrapper counter;
  56786. w64wrapper sendLimit;
  56787. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  56788. w64Zero(&counter);
  56789. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  56790. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  56791. for (i = 0; i < 10; i++) {
  56792. /* Test some failed decryptions */
  56793. test_AEAD_fail_decryption = 1;
  56794. w64Increment(&counter);
  56795. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56796. /* Should succeed since decryption failures are dropped */
  56797. AssertIntGT(ret, 0);
  56798. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  56799. }
  56800. test_AEAD_fail_decryption = 1;
  56801. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  56802. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  56803. /* 100 read calls should be enough to complete the key update */
  56804. w64Zero(&counter);
  56805. for (i = 0; i < 100; i++) {
  56806. /* Key update should be sent and negotiated */
  56807. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56808. AssertIntGT(ret, 0);
  56809. /* Epoch after one key update is 4 */
  56810. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  56811. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  56812. didReKey = 1;
  56813. break;
  56814. }
  56815. }
  56816. AssertTrue(didReKey);
  56817. if (!w64IsZero(sendLimit)) {
  56818. /* Test the sending limit for AEAD ciphers */
  56819. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  56820. test_AEAD_seq_num = 1;
  56821. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  56822. AssertIntGT(ret, 0);
  56823. didReKey = 0;
  56824. w64Zero(&counter);
  56825. /* 100 read calls should be enough to complete the key update */
  56826. for (i = 0; i < 100; i++) {
  56827. /* Key update should be sent and negotiated */
  56828. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56829. AssertIntGT(ret, 0);
  56830. /* Epoch after another key update is 5 */
  56831. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  56832. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  56833. didReKey = 1;
  56834. break;
  56835. }
  56836. }
  56837. AssertTrue(didReKey);
  56838. }
  56839. test_AEAD_fail_decryption = 2;
  56840. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  56841. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  56842. /* Connection should fail with a DECRYPT_ERROR */
  56843. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56844. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  56845. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  56846. test_AEAD_done = 1;
  56847. }
  56848. int counter = 0;
  56849. static void test_AEAD_limit_server(WOLFSSL* ssl)
  56850. {
  56851. char msgBuf[] = "Sending data";
  56852. int ret = WOLFSSL_SUCCESS;
  56853. w64wrapper sendLimit;
  56854. SOCKET_T fd = wolfSSL_get_fd(ssl);
  56855. struct timespec delay;
  56856. XMEMSET(&delay, 0, sizeof(delay));
  56857. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  56858. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  56859. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  56860. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  56861. while (!test_AEAD_done && ret > 0) {
  56862. counter++;
  56863. if (test_AEAD_seq_num) {
  56864. /* We need to update the seq number so that we can understand the
  56865. * peer. Otherwise we will incorrectly interpret the seq number. */
  56866. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  56867. AssertNotNull(e);
  56868. e->nextPeerSeqNumber = sendLimit;
  56869. test_AEAD_seq_num = 0;
  56870. }
  56871. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56872. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  56873. nanosleep(&delay, NULL);
  56874. }
  56875. }
  56876. static int test_wolfSSL_dtls_AEAD_limit(void)
  56877. {
  56878. callback_functions func_cb_client;
  56879. callback_functions func_cb_server;
  56880. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56881. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56882. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56883. func_cb_server.method = wolfDTLSv1_3_server_method;
  56884. func_cb_client.method = wolfDTLSv1_3_client_method;
  56885. func_cb_server.on_result = test_AEAD_limit_server;
  56886. func_cb_client.on_result = test_AEAD_limit_client;
  56887. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56888. if (!func_cb_client.return_code)
  56889. return TEST_FAIL;
  56890. if (!func_cb_server.return_code)
  56891. return TEST_FAIL;
  56892. return TEST_SUCCESS;
  56893. }
  56894. #else
  56895. static int test_wolfSSL_dtls_AEAD_limit(void)
  56896. {
  56897. return TEST_SKIPPED;
  56898. }
  56899. #endif
  56900. #if defined(WOLFSSL_DTLS) && \
  56901. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  56902. !defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
  56903. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  56904. {
  56905. int fd, ret;
  56906. byte ch_msg[] = {
  56907. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  56908. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  56909. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  56910. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  56911. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  56912. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  56913. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  56914. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  56915. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  56916. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  56917. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  56918. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  56919. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  56920. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  56921. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  56922. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  56923. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  56924. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  56925. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  56926. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  56927. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  56928. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  56929. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  56930. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  56931. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  56932. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  56933. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  56934. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  56935. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  56936. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  56937. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  56938. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  56939. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  56940. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  56941. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  56942. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  56943. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  56944. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  56945. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  56946. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  56947. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  56948. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  56949. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  56950. 0x31, 0x68, 0x4c
  56951. };
  56952. fd = wolfSSL_get_fd(ssl);
  56953. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  56954. AssertIntGT(ret, 0);
  56955. /* consume the HRR otherwise handshake will fail */
  56956. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  56957. AssertIntGT(ret, 0);
  56958. }
  56959. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56960. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  56961. {
  56962. int fd, ret;
  56963. byte ch_msh_invalid_cookie[] = {
  56964. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  56965. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  56966. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  56967. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  56968. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  56969. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  56970. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  56971. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  56972. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  56973. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  56974. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  56975. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  56976. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  56977. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  56978. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56979. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56980. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56981. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56982. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56983. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  56984. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  56985. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  56986. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  56987. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  56988. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  56989. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  56990. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  56991. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  56992. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  56993. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  56994. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  56995. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  56996. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  56997. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  56998. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  56999. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  57000. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  57001. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  57002. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  57003. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  57004. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  57005. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  57006. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  57007. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  57008. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  57009. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  57010. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  57011. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  57012. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  57013. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  57014. 0x23, 0x00, 0x00
  57015. };
  57016. byte alert_reply[50];
  57017. byte expected_alert_reply[] = {
  57018. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  57019. 0x02, 0x02, 0x2f
  57020. };
  57021. fd = wolfSSL_get_fd(ssl);
  57022. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  57023. AssertIntGT(ret, 0);
  57024. /* should reply with an illegal_parameter reply */
  57025. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  57026. AssertIntEQ(ret, sizeof(expected_alert_reply));
  57027. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  57028. }
  57029. #endif
  57030. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  57031. {
  57032. #ifndef NO_MD5
  57033. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  57034. #elif !defined(NO_SHA)
  57035. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  57036. #elif !defined(NO_SHA256)
  57037. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  57038. #else
  57039. #error "We need a digest to hash the WOLFSSL object"
  57040. #endif
  57041. byte hashBuf[WC_MAX_DIGEST_SIZE];
  57042. wc_HashAlg hash;
  57043. const TLSX* exts = ssl->extensions;
  57044. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  57045. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  57046. * InitHandshakeHashes */
  57047. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  57048. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  57049. /* Following fields are not important to compare */
  57050. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  57051. sslCopy.buffers.inputBuffer.buffer = NULL;
  57052. sslCopy.buffers.inputBuffer.bufferSize = 0;
  57053. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  57054. sslCopy.buffers.inputBuffer.offset = 0;
  57055. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  57056. sslCopy.buffers.outputBuffer.buffer = NULL;
  57057. sslCopy.buffers.outputBuffer.bufferSize = 0;
  57058. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  57059. sslCopy.buffers.outputBuffer.offset = 0;
  57060. sslCopy.error = 0;
  57061. sslCopy.curSize = 0;
  57062. sslCopy.curStartIdx = 0;
  57063. sslCopy.keys.curSeq_lo = 0;
  57064. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  57065. #ifdef WOLFSSL_DTLS13
  57066. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  57067. sslCopy.dtls13FastTimeout = 0;
  57068. #endif
  57069. sslCopy.keys.dtls_peer_handshake_number = 0;
  57070. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  57071. sslCopy.hsHashes = NULL;
  57072. #ifdef WOLFSSL_ASYNC_IO
  57073. #ifdef WOLFSSL_ASYNC_CRYPT
  57074. sslCopy.asyncDev = NULL;
  57075. #endif
  57076. sslCopy.async = NULL;
  57077. #endif
  57078. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  57079. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  57080. /* hash extension list */
  57081. while (exts != NULL) {
  57082. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  57083. exts = exts->next;
  57084. }
  57085. /* Hash suites */
  57086. if (sslCopy.suites != NULL) {
  57087. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  57088. sizeof(struct Suites)), 0);
  57089. }
  57090. /* Hash hsHashes */
  57091. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  57092. sizeof(*hsHashes)), 0);
  57093. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  57094. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  57095. return MakeWordFromHash(hashBuf);
  57096. }
  57097. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  57098. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  57099. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  57100. char *buf, int sz, void *ctx)
  57101. {
  57102. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  57103. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  57104. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  57105. }
  57106. else {
  57107. return WOLFSSL_CBIO_ERR_WANT_READ;
  57108. }
  57109. }
  57110. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  57111. {
  57112. /* Compare the ssl object before and after one ClientHello msg */
  57113. SOCKET_T fd = wolfSSL_get_fd(ssl);
  57114. int res;
  57115. int err;
  57116. word32 initHash;
  57117. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  57118. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  57119. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  57120. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  57121. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  57122. (void)initHash;
  57123. res = tcp_select(fd, 5);
  57124. /* We are expecting a msg. A timeout indicates failure. */
  57125. AssertIntEQ(res, TEST_RECV_READY);
  57126. res = wolfSSL_accept(ssl);
  57127. err = wolfSSL_get_error(ssl, res);
  57128. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  57129. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  57130. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  57131. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  57132. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  57133. }
  57134. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  57135. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  57136. {
  57137. int ret;
  57138. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  57139. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  57140. test_wolfSSL_dtls_compare_stateless(ssl);
  57141. }
  57142. #endif
  57143. static int test_wolfSSL_dtls_stateless(void)
  57144. {
  57145. callback_functions client_cbs, server_cbs;
  57146. size_t i;
  57147. struct {
  57148. method_provider client_meth;
  57149. method_provider server_meth;
  57150. ssl_callback client_ssl_ready;
  57151. ssl_callback server_ssl_ready;
  57152. } test_params[] = {
  57153. #if !defined(WOLFSSL_NO_TLS12)
  57154. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  57155. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  57156. #endif
  57157. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  57158. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  57159. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  57160. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  57161. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  57162. #endif
  57163. };
  57164. if (0 == sizeof(test_params)){
  57165. return TEST_SKIPPED;
  57166. }
  57167. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  57168. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57169. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57170. client_cbs.doUdp = server_cbs.doUdp = 1;
  57171. client_cbs.method = test_params[i].client_meth;
  57172. server_cbs.method = test_params[i].server_meth;
  57173. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  57174. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  57175. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57176. if (!client_cbs.return_code)
  57177. return TEST_FAIL;
  57178. if (!server_cbs.return_code)
  57179. return TEST_FAIL;
  57180. }
  57181. return TEST_SUCCESS;
  57182. }
  57183. #else
  57184. static int test_wolfSSL_dtls_stateless(void)
  57185. {
  57186. return TEST_SKIPPED;
  57187. }
  57188. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  57189. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  57190. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  57191. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  57192. !defined(WOLFSSL_NO_CLIENT_AUTH))
  57193. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  57194. {
  57195. int ret;
  57196. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  57197. fprintf(stderr, "loading cert %s failed\n", certA);
  57198. fprintf(stderr, "Error: (%d): %s\n", ret,
  57199. wolfSSL_ERR_reason_error_string(ret));
  57200. return -1;
  57201. }
  57202. return 0;
  57203. }
  57204. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  57205. {
  57206. int ret;
  57207. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  57208. != WOLFSSL_SUCCESS) {
  57209. fprintf(stderr, "could not verify the cert: %s\n", certA);
  57210. fprintf(stderr, "Error: (%d): %s\n", ret,
  57211. wolfSSL_ERR_reason_error_string(ret));
  57212. return -1;
  57213. }
  57214. else {
  57215. fprintf(stderr, "successfully verified: %s\n", certA);
  57216. }
  57217. return 0;
  57218. }
  57219. #define LOAD_ONE_CA(a, b, c, d) \
  57220. do { \
  57221. (a) = load_ca_into_cm(c, d); \
  57222. if ((a) != 0) \
  57223. return (b); \
  57224. else \
  57225. (b)--; \
  57226. } while(0)
  57227. #define VERIFY_ONE_CERT(a, b, c, d) \
  57228. do { \
  57229. (a) = verify_cert_with_cm(c, d);\
  57230. if ((a) != 0) \
  57231. return (b); \
  57232. else \
  57233. (b)--; \
  57234. } while(0)
  57235. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  57236. {
  57237. int ret;
  57238. int i = -1;
  57239. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  57240. char chainGArr[9][50] = {"certs/ca-cert.pem",
  57241. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  57242. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  57243. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  57244. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  57245. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  57246. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  57247. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  57248. "certs/test-pathlen/chainG-entity.pem"};
  57249. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  57250. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  57251. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  57252. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  57253. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  57254. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  57255. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  57256. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  57257. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  57258. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  57259. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  57260. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  57261. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  57262. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  57263. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  57264. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  57265. /* test validating the entity twice, should have no effect on pathLen since
  57266. * entity/leaf cert */
  57267. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  57268. return ret;
  57269. }
  57270. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  57271. {
  57272. int ret;
  57273. int i = -1;
  57274. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  57275. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  57276. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  57277. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  57278. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  57279. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  57280. */
  57281. char chainHArr[6][50] = {"certs/ca-cert.pem",
  57282. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  57283. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  57284. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  57285. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  57286. "certs/test-pathlen/chainH-entity.pem"};
  57287. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  57288. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  57289. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  57290. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  57291. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  57292. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  57293. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  57294. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  57295. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  57296. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  57297. return ret;
  57298. }
  57299. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  57300. {
  57301. int ret;
  57302. int i = -1;
  57303. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  57304. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  57305. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  57306. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  57307. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  57308. */
  57309. char chainIArr[5][50] = {"certs/ca-cert.pem",
  57310. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  57311. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  57312. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  57313. "certs/test-pathlen/chainI-entity.pem"};
  57314. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  57315. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  57316. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  57317. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  57318. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  57319. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  57320. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  57321. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  57322. return ret;
  57323. }
  57324. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  57325. {
  57326. int ret;
  57327. int i = -1;
  57328. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  57329. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  57330. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  57331. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  57332. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  57333. */
  57334. char chainJArr[6][50] = {"certs/ca-cert.pem",
  57335. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  57336. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  57337. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  57338. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  57339. "certs/test-pathlen/chainJ-entity.pem"};
  57340. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  57341. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  57342. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  57343. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  57344. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  57345. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  57346. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  57347. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  57348. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  57349. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  57350. return ret;
  57351. }
  57352. static int test_various_pathlen_chains(void)
  57353. {
  57354. EXPECT_DECLS;
  57355. WOLFSSL_CERT_MANAGER* cm = NULL;
  57356. /* Test chain G (large chain with varying pathLens) */
  57357. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57358. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  57359. ExpectIntEQ(test_chainG(cm), -1);
  57360. #else
  57361. ExpectIntEQ(test_chainG(cm), 0);
  57362. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  57363. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57364. wolfSSL_CertManagerFree(cm);
  57365. /* end test chain G */
  57366. /* Test chain H (5 chain with same pathLens) */
  57367. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57368. ExpectIntLT(test_chainH(cm), 0);
  57369. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57370. wolfSSL_CertManagerFree(cm);
  57371. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57372. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57373. wolfSSL_CertManagerFree(cm);
  57374. /* end test chain H */
  57375. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  57376. * followed by 2 ICA's, should pass) */
  57377. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57378. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  57379. ExpectIntEQ(test_chainI(cm), -1);
  57380. #else
  57381. ExpectIntEQ(test_chainI(cm), 0);
  57382. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  57383. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57384. wolfSSL_CertManagerFree(cm);
  57385. cm = NULL;
  57386. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57387. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57388. wolfSSL_CertManagerFree(cm);
  57389. cm = NULL;
  57390. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  57391. * this time followed by 3 ICA's, should fail */
  57392. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57393. ExpectIntLT(test_chainJ(cm), 0);
  57394. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57395. wolfSSL_CertManagerFree(cm);
  57396. cm = NULL;
  57397. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57398. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57399. wolfSSL_CertManagerFree(cm);
  57400. return EXPECT_RESULT();
  57401. }
  57402. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  57403. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57404. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  57405. {
  57406. EXPECT_DECLS;
  57407. byte ekm[100] = {0};
  57408. (void)ctx;
  57409. /* Success Cases */
  57410. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57411. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  57412. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57413. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  57414. /* Use some random context */
  57415. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57416. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  57417. /* Failure cases */
  57418. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57419. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  57420. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57421. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  57422. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57423. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  57424. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57425. "extended master secret", XSTR_SIZEOF("extended master secret"),
  57426. NULL, 0, 0), 0);
  57427. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57428. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  57429. return EXPECT_RESULT();
  57430. }
  57431. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  57432. {
  57433. wolfSSL_KeepArrays(ssl);
  57434. return TEST_SUCCESS;
  57435. }
  57436. static int test_export_keying_material(void)
  57437. {
  57438. EXPECT_DECLS;
  57439. test_ssl_cbf serverCb;
  57440. test_ssl_cbf clientCb;
  57441. XMEMSET(&serverCb, 0, sizeof(serverCb));
  57442. XMEMSET(&clientCb, 0, sizeof(clientCb));
  57443. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  57444. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  57445. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  57446. return EXPECT_RESULT();
  57447. }
  57448. #endif /* HAVE_KEYING_MATERIAL */
  57449. static int test_wolfSSL_THREADID_hash(void)
  57450. {
  57451. EXPECT_DECLS;
  57452. #if defined(OPENSSL_EXTRA)
  57453. CRYPTO_THREADID id;
  57454. CRYPTO_THREADID_current(NULL);
  57455. /* Hash result is unsigned long. */
  57456. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  57457. XMEMSET(&id, 0, sizeof(id));
  57458. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  57459. #endif /* OPENSSL_EXTRA */
  57460. return EXPECT_RESULT();
  57461. }
  57462. static int test_wolfSSL_set_ecdh_auto(void)
  57463. {
  57464. EXPECT_DECLS;
  57465. #if defined(OPENSSL_EXTRA)
  57466. WOLFSSL* ssl = NULL;
  57467. ExpectIntEQ(SSL_set_ecdh_auto(NULL,0), 1);
  57468. ExpectIntEQ(SSL_set_ecdh_auto(NULL,1), 1);
  57469. ExpectIntEQ(SSL_set_ecdh_auto(ssl,0), 1);
  57470. ExpectIntEQ(SSL_set_ecdh_auto(ssl,1), 1);
  57471. #endif /* OPENSSL_EXTRA */
  57472. return EXPECT_RESULT();
  57473. }
  57474. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  57475. {
  57476. EXPECT_DECLS;
  57477. #if defined(OPENSSL_EXTRA)
  57478. WOLFSSL_CTX* ctx = NULL;
  57479. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  57480. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  57481. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  57482. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  57483. #endif /* OPENSSL_EXTRA */
  57484. return EXPECT_RESULT();
  57485. }
  57486. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  57487. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  57488. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  57489. {
  57490. EXPECT_DECLS;
  57491. callback_functions* callbacks = NULL;
  57492. WOLFSSL_CTX* ctx = NULL;
  57493. WOLFSSL* ssl = NULL;
  57494. SOCKET_T sfd = 0;
  57495. SOCKET_T cfd = 0;
  57496. word16 port;
  57497. char msg[] = "I hear you fa shizzle!";
  57498. int len = (int) XSTRLEN(msg);
  57499. char input[1024];
  57500. int ret = 0;
  57501. int err = 0;
  57502. if (!args)
  57503. WOLFSSL_RETURN_FROM_THREAD(0);
  57504. ((func_args*)args)->return_code = TEST_FAIL;
  57505. callbacks = ((func_args*)args)->callbacks;
  57506. ctx = wolfSSL_CTX_new(callbacks->method());
  57507. #if defined(USE_WINDOWS_API)
  57508. port = ((func_args*)args)->signal->port;
  57509. #else
  57510. /* Let tcp_listen assign port */
  57511. port = 0;
  57512. #endif
  57513. #ifdef WOLFSSL_TIRTOS
  57514. fdOpenSession(Task_self());
  57515. #endif
  57516. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  57517. caCertFile, 0));
  57518. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  57519. svrCertFile, WOLFSSL_FILETYPE_PEM));
  57520. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  57521. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  57522. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  57523. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  57524. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57525. #elif !defined(NO_DH)
  57526. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  57527. #endif
  57528. if (callbacks->ctx_ready)
  57529. callbacks->ctx_ready(ctx);
  57530. ssl = wolfSSL_new(ctx);
  57531. ExpectNotNull(ssl);
  57532. /* listen and accept */
  57533. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  57534. CloseSocket(sfd);
  57535. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  57536. if (callbacks->ssl_ready)
  57537. callbacks->ssl_ready(ssl);
  57538. if (EXPECT_SUCCESS()) {
  57539. do {
  57540. err = 0; /* Reset error */
  57541. ret = wolfSSL_accept(ssl);
  57542. if (ret != WOLFSSL_SUCCESS) {
  57543. err = wolfSSL_get_error(ssl, 0);
  57544. }
  57545. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  57546. }
  57547. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57548. /* read and write data */
  57549. XMEMSET(input, 0, sizeof(input));
  57550. while (EXPECT_SUCCESS()) {
  57551. ret = wolfSSL_read(ssl, input, sizeof(input));
  57552. if (ret > 0) {
  57553. break;
  57554. }
  57555. else {
  57556. err = wolfSSL_get_error(ssl,ret);
  57557. if (err == WOLFSSL_ERROR_WANT_READ) {
  57558. continue;
  57559. }
  57560. break;
  57561. }
  57562. }
  57563. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  57564. do {
  57565. ret = wolfSSL_write(ssl, msg, len);
  57566. if (ret > 0) {
  57567. break;
  57568. }
  57569. } while (ret < 0);
  57570. }
  57571. /* bidirectional shutdown */
  57572. while (EXPECT_SUCCESS()) {
  57573. ret = wolfSSL_shutdown(ssl);
  57574. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  57575. if (ret == WOLFSSL_SUCCESS) {
  57576. break;
  57577. }
  57578. }
  57579. if (EXPECT_SUCCESS()) {
  57580. /* wait for the peer to disconnect the tcp connection */
  57581. do {
  57582. ret = wolfSSL_read(ssl, input, sizeof(input));
  57583. err = wolfSSL_get_error(ssl, ret);
  57584. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  57585. }
  57586. /* detect TCP disconnect */
  57587. ExpectIntLE(ret,WOLFSSL_FAILURE);
  57588. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  57589. ((func_args*)args)->return_code = EXPECT_RESULT();
  57590. wolfSSL_free(ssl);
  57591. wolfSSL_CTX_free(ctx);
  57592. CloseSocket(cfd);
  57593. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  57594. wc_ecc_fp_free(); /* free per thread cache */
  57595. #endif
  57596. WOLFSSL_RETURN_FROM_THREAD(0);
  57597. }
  57598. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  57599. {
  57600. EXPECT_DECLS;
  57601. callback_functions* callbacks = NULL;
  57602. WOLFSSL_CTX* ctx = NULL;
  57603. WOLFSSL* ssl = NULL;
  57604. SOCKET_T sfd = 0;
  57605. char msg[] = "hello wolfssl server!";
  57606. int len = (int) XSTRLEN(msg);
  57607. char input[1024];
  57608. int idx;
  57609. int ret, err;
  57610. if (!args)
  57611. WOLFSSL_RETURN_FROM_THREAD(0);
  57612. ((func_args*)args)->return_code = TEST_FAIL;
  57613. callbacks = ((func_args*)args)->callbacks;
  57614. ctx = wolfSSL_CTX_new(callbacks->method());
  57615. #ifdef WOLFSSL_TIRTOS
  57616. fdOpenSession(Task_self());
  57617. #endif
  57618. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  57619. caCertFile, 0));
  57620. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  57621. cliCertFile, WOLFSSL_FILETYPE_PEM));
  57622. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  57623. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  57624. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  57625. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  57626. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  57627. if (EXPECT_SUCCESS()) {
  57628. do {
  57629. err = 0; /* Reset error */
  57630. ret = wolfSSL_connect(ssl);
  57631. if (ret != WOLFSSL_SUCCESS) {
  57632. err = wolfSSL_get_error(ssl, 0);
  57633. }
  57634. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  57635. }
  57636. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  57637. if (EXPECT_SUCCESS()) {
  57638. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  57639. input[idx] = 0;
  57640. }
  57641. }
  57642. if (EXPECT_SUCCESS()) {
  57643. ret = wolfSSL_shutdown(ssl);
  57644. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  57645. ret = wolfSSL_shutdown(ssl);
  57646. }
  57647. }
  57648. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57649. ((func_args*)args)->return_code = EXPECT_RESULT();
  57650. wolfSSL_free(ssl);
  57651. wolfSSL_CTX_free(ctx);
  57652. CloseSocket(sfd);
  57653. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  57654. wc_ecc_fp_free(); /* free per thread cache */
  57655. #endif
  57656. WOLFSSL_RETURN_FROM_THREAD(0);
  57657. }
  57658. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  57659. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  57660. /* This test is to check wolfSSL_read behaves as same as
  57661. * openSSL when it is called after SSL_shutdown completes.
  57662. */
  57663. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  57664. {
  57665. EXPECT_DECLS;
  57666. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  57667. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  57668. tcp_ready ready;
  57669. func_args client_args;
  57670. func_args server_args;
  57671. THREAD_TYPE serverThread;
  57672. THREAD_TYPE clientThread;
  57673. callback_functions server_cbf;
  57674. callback_functions client_cbf;
  57675. #ifdef WOLFSSL_TIRTOS
  57676. fdOpenSession(Task_self());
  57677. #endif
  57678. StartTCP();
  57679. InitTcpReady(&ready);
  57680. #if defined(USE_WINDOWS_API)
  57681. /* use RNG to get random port if using windows */
  57682. ready.port = GetRandomPort();
  57683. #endif
  57684. XMEMSET(&client_args, 0, sizeof(func_args));
  57685. XMEMSET(&server_args, 0, sizeof(func_args));
  57686. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  57687. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  57688. server_cbf.method = wolfTLSv1_2_server_method;
  57689. client_cbf.method = wolfTLSv1_2_client_method;
  57690. server_args.callbacks = &server_cbf;
  57691. client_args.callbacks = &client_cbf;
  57692. server_args.signal = &ready;
  57693. client_args.signal = &ready;
  57694. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  57695. wait_tcp_ready(&server_args);
  57696. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  57697. join_thread(clientThread);
  57698. join_thread(serverThread);
  57699. ExpectTrue(client_args.return_code);
  57700. ExpectTrue(server_args.return_code);
  57701. FreeTcpReady(&ready);
  57702. #endif
  57703. return EXPECT_RESULT();
  57704. }
  57705. static int test_wolfSSL_CTX_get_min_proto_version(void)
  57706. {
  57707. EXPECT_DECLS;
  57708. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  57709. WOLFSSL_CTX *ctx = NULL;
  57710. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57711. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  57712. WOLFSSL_SUCCESS);
  57713. #ifdef WOLFSSL_ALLOW_SSLV3
  57714. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  57715. #else
  57716. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  57717. #endif
  57718. wolfSSL_CTX_free(ctx);
  57719. ctx = NULL;
  57720. #ifndef NO_OLD_TLS
  57721. #ifdef WOLFSSL_ALLOW_TLSV10
  57722. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  57723. #else
  57724. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57725. #endif
  57726. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  57727. WOLFSSL_SUCCESS);
  57728. #ifdef WOLFSSL_ALLOW_TLSV10
  57729. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  57730. #else
  57731. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  57732. #endif
  57733. wolfSSL_CTX_free(ctx);
  57734. ctx = NULL;
  57735. #endif
  57736. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57737. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  57738. WOLFSSL_SUCCESS);
  57739. #ifndef NO_OLD_TLS
  57740. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  57741. #else
  57742. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  57743. #endif
  57744. wolfSSL_CTX_free(ctx);
  57745. ctx = NULL;
  57746. #ifndef WOLFSSL_NO_TLS12
  57747. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  57748. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  57749. WOLFSSL_SUCCESS);
  57750. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  57751. wolfSSL_CTX_free(ctx);
  57752. ctx = NULL;
  57753. #endif
  57754. #ifdef WOLFSSL_TLS13
  57755. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  57756. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  57757. WOLFSSL_SUCCESS);
  57758. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  57759. wolfSSL_CTX_free(ctx);
  57760. ctx = NULL;
  57761. #endif
  57762. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  57763. return EXPECT_RESULT();
  57764. }
  57765. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57766. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57767. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57768. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  57769. static int test_wolfSSL_set_SSL_CTX(void)
  57770. {
  57771. EXPECT_DECLS;
  57772. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  57773. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  57774. !defined(NO_RSA)
  57775. WOLFSSL_CTX *ctx1 = NULL;
  57776. WOLFSSL_CTX *ctx2 = NULL;
  57777. WOLFSSL *ssl = NULL;
  57778. const byte *session_id1 = (const byte *)"CTX1";
  57779. const byte *session_id2 = (const byte *)"CTX2";
  57780. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  57781. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  57782. WOLFSSL_FILETYPE_PEM));
  57783. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  57784. WOLFSSL_FILETYPE_PEM));
  57785. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  57786. WOLFSSL_SUCCESS);
  57787. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  57788. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  57789. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  57790. WOLFSSL_SUCCESS);
  57791. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  57792. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  57793. WOLFSSL_FILETYPE_PEM));
  57794. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  57795. WOLFSSL_FILETYPE_PEM));
  57796. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  57797. WOLFSSL_SUCCESS);
  57798. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  57799. WOLFSSL_SUCCESS);
  57800. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  57801. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  57802. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  57803. WOLFSSL_SUCCESS);
  57804. #ifdef HAVE_SESSION_TICKET
  57805. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  57806. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  57807. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  57808. #endif
  57809. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  57810. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  57811. #ifdef WOLFSSL_INT_H
  57812. #ifdef WOLFSSL_SESSION_ID_CTX
  57813. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  57814. #endif
  57815. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  57816. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  57817. #endif
  57818. #ifdef HAVE_SESSION_TICKET
  57819. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  57820. #endif
  57821. /* Set the ctx1 that has TLSv1.3 as max proto version */
  57822. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  57823. /* MUST not change proto versions of ssl */
  57824. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  57825. #ifdef HAVE_SESSION_TICKET
  57826. /* MUST not change */
  57827. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  57828. #endif
  57829. /* MUST change */
  57830. #ifdef WOLFSSL_INT_H
  57831. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  57832. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  57833. #ifdef WOLFSSL_SESSION_ID_CTX
  57834. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  57835. #endif
  57836. #endif
  57837. wolfSSL_free(ssl);
  57838. wolfSSL_CTX_free(ctx1);
  57839. wolfSSL_CTX_free(ctx2);
  57840. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  57841. return EXPECT_RESULT();
  57842. }
  57843. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57844. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57845. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57846. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  57847. static int test_wolfSSL_security_level(void)
  57848. {
  57849. EXPECT_DECLS;
  57850. #if defined(OPENSSL_EXTRA)
  57851. SSL_CTX *ctx = NULL;
  57852. #ifdef WOLFSSL_TLS13
  57853. #ifdef NO_WOLFSSL_SERVER
  57854. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57855. #else
  57856. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57857. #endif
  57858. SSL_CTX_set_security_level(NULL, 1);
  57859. SSL_CTX_set_security_level(ctx, 1);
  57860. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  57861. /* Stub so nothing happens. */
  57862. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  57863. SSL_CTX_free(ctx);
  57864. #else
  57865. (void)ctx;
  57866. #endif
  57867. #endif
  57868. return EXPECT_RESULT();
  57869. }
  57870. static int test_wolfSSL_SSL_in_init(void)
  57871. {
  57872. EXPECT_DECLS;
  57873. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  57874. SSL_CTX* ctx = NULL;
  57875. SSL* ssl = NULL;
  57876. const char* testCertFile;
  57877. const char* testKeyFile;
  57878. #ifdef WOLFSSL_TLS13
  57879. #ifdef NO_WOLFSSL_SERVER
  57880. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57881. #else
  57882. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57883. #endif
  57884. #else
  57885. #ifdef NO_WOLFSSL_SERVER
  57886. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57887. #else
  57888. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57889. #endif
  57890. #endif
  57891. #ifndef NO_RSA
  57892. testCertFile = svrCertFile;
  57893. testKeyFile = svrKeyFile;
  57894. #elif defined(HAVE_ECC)
  57895. testCertFile = eccCertFile;
  57896. testKeyFile = eccKeyFile;
  57897. #else
  57898. testCertFile = NULL;
  57899. testKeyFile = NULL;
  57900. #endif
  57901. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  57902. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57903. SSL_FILETYPE_PEM));
  57904. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57905. SSL_FILETYPE_PEM));
  57906. }
  57907. ExpectNotNull(ssl = SSL_new(ctx));
  57908. ExpectIntEQ(SSL_in_init(ssl), 1);
  57909. SSL_CTX_free(ctx);
  57910. SSL_free(ssl);
  57911. #endif
  57912. return EXPECT_RESULT();
  57913. }
  57914. static int test_wolfSSL_CTX_set_timeout(void)
  57915. {
  57916. EXPECT_DECLS;
  57917. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  57918. int timeout;
  57919. WOLFSSL_CTX* ctx = NULL;
  57920. (void)timeout;
  57921. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57922. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  57923. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  57924. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  57925. */
  57926. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  57927. /* giving 0 as timeout value sets default timeout */
  57928. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  57929. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  57930. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  57931. #else
  57932. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  57933. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  57934. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  57935. #endif
  57936. wolfSSL_CTX_free(ctx);
  57937. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  57938. return EXPECT_RESULT();
  57939. }
  57940. static int test_wolfSSL_OpenSSL_version(void)
  57941. {
  57942. EXPECT_DECLS;
  57943. #if defined(OPENSSL_EXTRA)
  57944. const char* ver;
  57945. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  57946. ExpectNotNull(ver = OpenSSL_version(0));
  57947. #else
  57948. ExpectNotNull(ver = OpenSSL_version());
  57949. #endif
  57950. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  57951. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  57952. #endif
  57953. return EXPECT_RESULT();
  57954. }
  57955. static int test_CONF_CTX_CMDLINE(void)
  57956. {
  57957. EXPECT_DECLS;
  57958. #if defined(OPENSSL_ALL)
  57959. SSL_CTX* ctx = NULL;
  57960. SSL_CONF_CTX* cctx = NULL;
  57961. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  57962. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57963. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  57964. /* set flags */
  57965. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  57966. WOLFSSL_CONF_FLAG_CMDLINE);
  57967. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  57968. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  57969. /* cmd invalid command */
  57970. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  57971. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  57972. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  57973. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  57974. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  57975. /* cmd Certificate and Private Key*/
  57976. {
  57977. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57978. const char* ourCert = svrCertFile;
  57979. const char* ourKey = svrKeyFile;
  57980. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  57981. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  57982. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  57983. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  57984. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57985. #endif
  57986. }
  57987. /* cmd curves */
  57988. {
  57989. #if defined(HAVE_ECC)
  57990. const char* curve = "secp256r1";
  57991. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  57992. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  57993. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57994. #endif
  57995. }
  57996. /* cmd CipherString */
  57997. {
  57998. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57999. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  58000. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  58001. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58002. }
  58003. /* cmd DH parameter */
  58004. {
  58005. #if !defined(NO_DH) && !defined(NO_BIO)
  58006. const char* ourdhcert = "./certs/dh2048.pem";
  58007. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  58008. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  58009. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58010. #endif
  58011. }
  58012. SSL_CTX_free(ctx);
  58013. SSL_CONF_CTX_free(cctx);
  58014. #endif /* OPENSSL_EXTRA */
  58015. return EXPECT_RESULT();
  58016. }
  58017. static int test_CONF_CTX_FILE(void)
  58018. {
  58019. EXPECT_DECLS;
  58020. #if defined(OPENSSL_ALL)
  58021. SSL_CTX* ctx = NULL;
  58022. SSL_CONF_CTX* cctx = NULL;
  58023. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  58024. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  58025. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  58026. /* set flags */
  58027. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  58028. WOLFSSL_CONF_FLAG_FILE);
  58029. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  58030. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  58031. /* sanity check */
  58032. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  58033. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  58034. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  58035. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  58036. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  58037. /* cmd Certificate and Private Key*/
  58038. {
  58039. #if !defined(NO_CERTS) && !defined(NO_RSA)
  58040. const char* ourCert = svrCertFile;
  58041. const char* ourKey = svrKeyFile;
  58042. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  58043. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  58044. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  58045. WOLFSSL_SUCCESS);
  58046. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  58047. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58048. #endif
  58049. }
  58050. /* cmd curves */
  58051. {
  58052. #if defined(HAVE_ECC)
  58053. const char* curve = "secp256r1";
  58054. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  58055. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  58056. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58057. #endif
  58058. }
  58059. /* cmd CipherString */
  58060. {
  58061. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  58062. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  58063. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  58064. WOLFSSL_SUCCESS);
  58065. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58066. }
  58067. /* cmd DH parameter */
  58068. {
  58069. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  58070. const char* ourdhcert = "./certs/dh3072.pem";
  58071. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  58072. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  58073. WOLFSSL_SUCCESS);
  58074. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58075. #endif
  58076. }
  58077. SSL_CTX_free(ctx);
  58078. SSL_CONF_CTX_free(cctx);
  58079. #endif /* OPENSSL_EXTRA */
  58080. return EXPECT_RESULT();
  58081. }
  58082. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  58083. {
  58084. EXPECT_DECLS;
  58085. #ifdef HAVE_EX_DATA
  58086. int idx1, idx2;
  58087. /* test for unsupported class index */
  58088. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  58089. 0,NULL, NULL, NULL, NULL ), -1);
  58090. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  58091. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  58092. 0,NULL, NULL, NULL, NULL ), -1);
  58093. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  58094. 0,NULL, NULL, NULL, NULL ), -1);
  58095. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  58096. 0,NULL, NULL, NULL, NULL ), -1);
  58097. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  58098. 0,NULL, NULL, NULL, NULL ), -1);
  58099. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  58100. 0,NULL, NULL, NULL, NULL ), -1);
  58101. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  58102. 0,NULL, NULL, NULL, NULL ), -1);
  58103. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  58104. 0,NULL, NULL, NULL, NULL ), -1);
  58105. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  58106. 0,NULL, NULL, NULL, NULL ), -1);
  58107. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  58108. 0,NULL, NULL, NULL, NULL ), -1);
  58109. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  58110. 0,NULL, NULL, NULL, NULL ), -1);
  58111. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  58112. 0,NULL, NULL, NULL, NULL ), -1);
  58113. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  58114. 0,NULL, NULL, NULL, NULL ), -1);
  58115. /* test for supported class index */
  58116. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  58117. 0,NULL, NULL, NULL, NULL );
  58118. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  58119. 0,NULL, NULL, NULL, NULL );
  58120. ExpectIntNE(idx1, -1);
  58121. ExpectIntNE(idx2, -1);
  58122. ExpectIntNE(idx1, idx2);
  58123. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  58124. 0,NULL, NULL, NULL, NULL );
  58125. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  58126. 0,NULL, NULL, NULL, NULL );
  58127. ExpectIntNE(idx1, -1);
  58128. ExpectIntNE(idx2, -1);
  58129. ExpectIntNE(idx1, idx2);
  58130. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  58131. 0,NULL, NULL, NULL, NULL );
  58132. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  58133. 0,NULL, NULL, NULL, NULL );
  58134. ExpectIntNE(idx1, -1);
  58135. ExpectIntNE(idx2, -1);
  58136. ExpectIntNE(idx1, idx2);
  58137. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  58138. 0,NULL, NULL, NULL, NULL );
  58139. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  58140. 0,NULL, NULL, NULL, NULL );
  58141. ExpectIntNE(idx1, -1);
  58142. ExpectIntNE(idx2, -1);
  58143. ExpectIntNE(idx1, idx2);
  58144. #endif /* HAVE_EX_DATA */
  58145. return EXPECT_RESULT();
  58146. }
  58147. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  58148. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  58149. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  58150. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  58151. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  58152. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  58153. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  58154. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  58155. #define SESSION_NEW_IDX_PTR "Testing"
  58156. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  58157. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  58158. {
  58159. AssertNotNull(p);
  58160. AssertNull(ptr);
  58161. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  58162. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  58163. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  58164. }
  58165. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  58166. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  58167. void* arg)
  58168. {
  58169. EXPECT_DECLS;
  58170. ExpectNotNull(out);
  58171. ExpectNotNull(in);
  58172. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  58173. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  58174. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  58175. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  58176. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  58177. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  58178. if (EXPECT_SUCCESS()) {
  58179. return SSL_SUCCESS;
  58180. }
  58181. else {
  58182. return SSL_FAILURE;
  58183. }
  58184. }
  58185. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  58186. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  58187. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  58188. {
  58189. EXPECT_DECLS;
  58190. ExpectNotNull(p);
  58191. ExpectNull(ptr);
  58192. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  58193. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  58194. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  58195. if (EXPECT_SUCCESS()) {
  58196. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  58197. }
  58198. }
  58199. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  58200. {
  58201. EXPECT_DECLS;
  58202. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  58203. (void*)SESSION_NEW_IDX_PTR,
  58204. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  58205. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  58206. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  58207. SSL_SESSION* s = SSL_SESSION_new();
  58208. SSL_SESSION* d = NULL;
  58209. ExpectNotNull(s);
  58210. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  58211. ExpectNotNull(d = SSL_SESSION_dup(s));
  58212. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  58213. SSL_SESSION_free(s);
  58214. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  58215. SSL_SESSION_free(d);
  58216. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  58217. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  58218. crypto_ex_cb_ctx_session = NULL;
  58219. return EXPECT_RESULT();
  58220. }
  58221. #else
  58222. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  58223. {
  58224. return TEST_SKIPPED;
  58225. }
  58226. #endif
  58227. static int test_wolfSSL_set_psk_use_session_callback(void)
  58228. {
  58229. EXPECT_DECLS;
  58230. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  58231. SSL_CTX* ctx = NULL;
  58232. SSL* ssl = NULL;
  58233. const char* testCertFile;
  58234. const char* testKeyFile;
  58235. #ifdef WOLFSSL_TLS13
  58236. #ifdef NO_WOLFSSL_SERVER
  58237. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  58238. #else
  58239. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  58240. #endif
  58241. #else
  58242. #ifdef NO_WOLFSSL_SERVER
  58243. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  58244. #else
  58245. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  58246. #endif
  58247. #endif
  58248. #ifndef NO_RSA
  58249. testCertFile = svrCertFile;
  58250. testKeyFile = svrKeyFile;
  58251. #elif defined(HAVE_ECC)
  58252. testCertFile = eccCertFile;
  58253. testKeyFile = eccKeyFile;
  58254. #else
  58255. testCertFile = NULL;
  58256. testKeyFile = NULL;
  58257. #endif
  58258. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  58259. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  58260. SSL_FILETYPE_PEM));
  58261. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  58262. SSL_FILETYPE_PEM));
  58263. }
  58264. ExpectNotNull(ssl = SSL_new(ctx));
  58265. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  58266. SSL_CTX_free(ctx);
  58267. SSL_free(ssl);
  58268. #endif
  58269. return EXPECT_RESULT();
  58270. }
  58271. static int test_wolfSSL_ERR_strings(void)
  58272. {
  58273. EXPECT_DECLS;
  58274. #if !defined(NO_ERROR_STRINGS)
  58275. const char* err1 = "unsupported cipher suite";
  58276. const char* err2 = "wolfSSL PEM routines";
  58277. const char* err = NULL;
  58278. (void)err;
  58279. (void)err1;
  58280. (void)err2;
  58281. #if defined(OPENSSL_EXTRA)
  58282. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  58283. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  58284. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  58285. ExpectIntEQ((*err == '\0'), 1);
  58286. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  58287. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  58288. #else
  58289. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  58290. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  58291. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  58292. ExpectIntEQ((*err == '\0'), 1);
  58293. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  58294. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  58295. ExpectIntEQ((*err == '\0'), 1);
  58296. #endif
  58297. #endif
  58298. return EXPECT_RESULT();
  58299. }
  58300. static int test_wolfSSL_EVP_shake128(void)
  58301. {
  58302. EXPECT_DECLS;
  58303. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  58304. defined(WOLFSSL_SHAKE128)
  58305. const EVP_MD* md = NULL;
  58306. ExpectNotNull(md = EVP_shake128());
  58307. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  58308. #endif
  58309. return EXPECT_RESULT();
  58310. }
  58311. static int test_wolfSSL_EVP_shake256(void)
  58312. {
  58313. EXPECT_DECLS;
  58314. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  58315. defined(WOLFSSL_SHAKE256)
  58316. const EVP_MD* md = NULL;
  58317. ExpectNotNull(md = EVP_shake256());
  58318. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  58319. #endif
  58320. return EXPECT_RESULT();
  58321. }
  58322. /*
  58323. * Testing EVP digest API with SM3
  58324. */
  58325. static int test_wolfSSL_EVP_sm3(void)
  58326. {
  58327. int res = TEST_SKIPPED;
  58328. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  58329. EXPECT_DECLS;
  58330. const EVP_MD* md = NULL;
  58331. EVP_MD_CTX* mdCtx = NULL;
  58332. byte data[WC_SM3_BLOCK_SIZE * 4];
  58333. byte hash[WC_SM3_DIGEST_SIZE];
  58334. byte calcHash[WC_SM3_DIGEST_SIZE];
  58335. byte expHash[WC_SM3_DIGEST_SIZE] = {
  58336. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  58337. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  58338. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  58339. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  58340. };
  58341. word32 chunk;
  58342. word32 i;
  58343. unsigned int sz;
  58344. int ret;
  58345. XMEMSET(data, 0, sizeof(data));
  58346. md = EVP_sm3();
  58347. ExpectTrue(md != NULL);
  58348. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  58349. mdCtx = EVP_MD_CTX_new();
  58350. ExpectTrue(mdCtx != NULL);
  58351. /* Invalid Parameters */
  58352. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  58353. /* Valid Parameters */
  58354. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  58355. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  58356. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  58357. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  58358. /* Valid Parameters */
  58359. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  58360. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  58361. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  58362. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  58363. WOLFSSL_SUCCESS);
  58364. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  58365. WOLFSSL_SUCCESS);
  58366. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  58367. WOLFSSL_SUCCESS);
  58368. /* Ensure too many bytes for lengths. */
  58369. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  58370. WOLFSSL_SUCCESS);
  58371. /* Invalid Parameters */
  58372. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  58373. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  58374. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  58375. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  58376. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  58377. /* Valid Parameters */
  58378. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  58379. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  58380. /* Chunk tests. */
  58381. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  58382. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  58383. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  58384. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  58385. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  58386. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  58387. WOLFSSL_SUCCESS);
  58388. }
  58389. if (i < (word32)sizeof(data)) {
  58390. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  58391. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  58392. }
  58393. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  58394. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  58395. }
  58396. /* Not testing when the low 32-bit length overflows. */
  58397. ret = EVP_MD_CTX_cleanup(mdCtx);
  58398. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  58399. wolfSSL_EVP_MD_CTX_free(mdCtx);
  58400. res = EXPECT_RESULT();
  58401. #endif
  58402. return res;
  58403. } /* END test_EVP_sm3 */
  58404. static int test_EVP_blake2(void)
  58405. {
  58406. EXPECT_DECLS;
  58407. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  58408. const EVP_MD* md = NULL;
  58409. (void)md;
  58410. #if defined(HAVE_BLAKE2)
  58411. ExpectNotNull(md = EVP_blake2b512());
  58412. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  58413. #endif
  58414. #if defined(HAVE_BLAKE2S)
  58415. ExpectNotNull(md = EVP_blake2s256());
  58416. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  58417. #endif
  58418. #endif
  58419. return EXPECT_RESULT();
  58420. }
  58421. #if defined(OPENSSL_EXTRA)
  58422. static void list_md_fn(const EVP_MD* m, const char* from,
  58423. const char* to, void* arg)
  58424. {
  58425. const char* mn;
  58426. BIO *bio;
  58427. (void) from;
  58428. (void) to;
  58429. (void) arg;
  58430. (void) mn;
  58431. (void) bio;
  58432. if (!m) {
  58433. /* alias */
  58434. AssertNull(m);
  58435. AssertNotNull(to);
  58436. }
  58437. else {
  58438. AssertNotNull(m);
  58439. AssertNull(to);
  58440. }
  58441. AssertNotNull(from);
  58442. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  58443. mn = EVP_get_digestbyname(from);
  58444. /* print to stderr */
  58445. AssertNotNull(arg);
  58446. bio = BIO_new(BIO_s_file());
  58447. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  58448. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  58449. BIO_free(bio);
  58450. #endif
  58451. }
  58452. #endif
  58453. static int test_EVP_MD_do_all(void)
  58454. {
  58455. int res = TEST_SKIPPED;
  58456. #if defined(OPENSSL_EXTRA)
  58457. EVP_MD_do_all(NULL, stderr);
  58458. EVP_MD_do_all(list_md_fn, stderr);
  58459. res = TEST_SUCCESS;
  58460. #endif
  58461. return res;
  58462. }
  58463. #if defined(OPENSSL_EXTRA)
  58464. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  58465. {
  58466. (void)arg;
  58467. (void)nm;
  58468. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  58469. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  58470. /* print to stderr */
  58471. AssertNotNull(arg);
  58472. BIO *bio = BIO_new(BIO_s_file());
  58473. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  58474. BIO_printf(bio, "%s\n", nm);
  58475. BIO_free(bio);
  58476. #endif
  58477. }
  58478. #endif
  58479. static int test_OBJ_NAME_do_all(void)
  58480. {
  58481. int res = TEST_SKIPPED;
  58482. #if defined(OPENSSL_EXTRA)
  58483. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  58484. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  58485. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  58486. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  58487. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  58488. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  58489. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  58490. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  58491. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  58492. res = TEST_SUCCESS;
  58493. #endif
  58494. return res;
  58495. }
  58496. static int test_SSL_CIPHER_get_xxx(void)
  58497. {
  58498. EXPECT_DECLS;
  58499. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  58500. !defined(NO_FILESYSTEM)
  58501. const SSL_CIPHER* cipher = NULL;
  58502. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  58503. int i, numCiphers = 0;
  58504. SSL_CTX* ctx = NULL;
  58505. SSL* ssl = NULL;
  58506. const char* testCertFile;
  58507. const char* testKeyFile;
  58508. char buf[256] = {0};
  58509. const char* cipher_id = NULL;
  58510. int expect_nid1 = NID_undef;
  58511. int expect_nid2 = NID_undef;
  58512. int expect_nid3 = NID_undef;
  58513. int expect_nid4 = NID_undef;
  58514. int expect_nid5 = 0;
  58515. const char* cipher_id2 = NULL;
  58516. int expect_nid21 = NID_undef;
  58517. int expect_nid22 = NID_undef;
  58518. int expect_nid23 = NID_undef;
  58519. int expect_nid24 = NID_undef;
  58520. int expect_nid25 = 0;
  58521. (void)cipher;
  58522. (void)supportedCiphers;
  58523. (void)i;
  58524. (void)numCiphers;
  58525. (void)ctx;
  58526. (void)ssl;
  58527. (void)testCertFile;
  58528. (void)testKeyFile;
  58529. #if defined(WOLFSSL_TLS13)
  58530. cipher_id = "TLS13-AES128-GCM-SHA256";
  58531. expect_nid1 = NID_auth_rsa;
  58532. expect_nid2 = NID_aes_128_gcm;
  58533. expect_nid3 = NID_sha256;
  58534. expect_nid4 = NID_kx_any;
  58535. expect_nid5 = 1;
  58536. #if !defined(WOLFSSL_NO_TLS12)
  58537. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  58538. expect_nid21 = NID_auth_rsa;
  58539. expect_nid22 = NID_aes_256_gcm;
  58540. expect_nid23 = NID_sha384;
  58541. expect_nid24 = NID_kx_ecdhe;
  58542. expect_nid25 = 1;
  58543. #endif
  58544. #endif
  58545. #ifdef NO_WOLFSSL_SERVER
  58546. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  58547. #else
  58548. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  58549. #endif
  58550. if (cipher_id) {
  58551. #ifndef NO_RSA
  58552. testCertFile = svrCertFile;
  58553. testKeyFile = svrKeyFile;
  58554. #elif defined(HAVE_ECC)
  58555. testCertFile = eccCertFile;
  58556. testKeyFile = eccKeyFile;
  58557. #else
  58558. testCertFile = NULL;
  58559. testKeyFile = NULL;
  58560. #endif
  58561. if (testCertFile != NULL && testKeyFile != NULL) {
  58562. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  58563. SSL_FILETYPE_PEM));
  58564. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  58565. SSL_FILETYPE_PEM));
  58566. }
  58567. ExpectNotNull(ssl = SSL_new(ctx));
  58568. ExpectIntEQ(SSL_in_init(ssl), 1);
  58569. supportedCiphers = SSL_get_ciphers(ssl);
  58570. numCiphers = sk_num(supportedCiphers);
  58571. for (i = 0; i < numCiphers; ++i) {
  58572. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  58573. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  58574. }
  58575. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  58576. break;
  58577. }
  58578. }
  58579. /* test case for */
  58580. if (i != numCiphers) {
  58581. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  58582. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  58583. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  58584. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  58585. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  58586. }
  58587. if (cipher_id2) {
  58588. for (i = 0; i < numCiphers; ++i) {
  58589. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  58590. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  58591. }
  58592. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  58593. break;
  58594. }
  58595. }
  58596. /* test case for */
  58597. if (i != numCiphers) {
  58598. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  58599. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  58600. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  58601. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  58602. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  58603. }
  58604. }
  58605. }
  58606. SSL_CTX_free(ctx);
  58607. SSL_free(ssl);
  58608. #endif
  58609. return EXPECT_RESULT();
  58610. }
  58611. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  58612. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  58613. int* keyFormat)
  58614. {
  58615. int ret;
  58616. byte* key_buf = NULL;
  58617. size_t key_sz = 0;
  58618. EncryptedInfo encInfo;
  58619. XMEMSET(&encInfo, 0, sizeof(encInfo));
  58620. ret = load_file(privKeyFile, &key_buf, &key_sz);
  58621. if (ret == 0) {
  58622. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  58623. NULL, &encInfo, keyFormat);
  58624. }
  58625. if (key_buf != NULL) {
  58626. free(key_buf); key_buf = NULL;
  58627. }
  58628. (void)encInfo; /* not used in this test */
  58629. #ifdef DEBUG_WOLFSSL
  58630. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  58631. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  58632. (*pDer)->length);
  58633. #endif
  58634. return ret;
  58635. }
  58636. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  58637. {
  58638. int ret = CRYPTOCB_UNAVAILABLE;
  58639. const char* privKeyFile = (const char*)ctx;
  58640. DerBuffer* pDer = NULL;
  58641. int keyFormat = 0;
  58642. if (info->algo_type == WC_ALGO_TYPE_PK) {
  58643. #ifdef DEBUG_WOLFSSL
  58644. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  58645. #endif
  58646. #ifndef NO_RSA
  58647. if (info->pk.type == WC_PK_TYPE_RSA) {
  58648. switch (info->pk.rsa.type) {
  58649. case RSA_PUBLIC_ENCRYPT:
  58650. case RSA_PUBLIC_DECRYPT:
  58651. /* perform software based RSA public op */
  58652. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  58653. break;
  58654. case RSA_PRIVATE_ENCRYPT:
  58655. case RSA_PRIVATE_DECRYPT:
  58656. {
  58657. RsaKey key;
  58658. /* perform software based RSA private op */
  58659. #ifdef DEBUG_WOLFSSL
  58660. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  58661. #endif
  58662. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  58663. &keyFormat);
  58664. if (ret != 0) {
  58665. return ret;
  58666. }
  58667. ret = wc_InitRsaKey(&key, HEAP_HINT);
  58668. if (ret == 0) {
  58669. word32 keyIdx = 0;
  58670. /* load RSA private key and perform private transform */
  58671. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  58672. &key, pDer->length);
  58673. if (ret == 0) {
  58674. ret = wc_RsaFunction(
  58675. info->pk.rsa.in, info->pk.rsa.inLen,
  58676. info->pk.rsa.out, info->pk.rsa.outLen,
  58677. info->pk.rsa.type, &key, info->pk.rsa.rng);
  58678. }
  58679. else {
  58680. /* if decode fails, then fall-back to software based crypto */
  58681. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  58682. "key decode failed %d, falling back to "
  58683. "software\n", ret);
  58684. ret = CRYPTOCB_UNAVAILABLE;
  58685. }
  58686. wc_FreeRsaKey(&key);
  58687. }
  58688. wc_FreeDer(&pDer); pDer = NULL;
  58689. break;
  58690. }
  58691. }
  58692. #ifdef DEBUG_WOLFSSL
  58693. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  58694. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  58695. #endif
  58696. }
  58697. #endif /* !NO_RSA */
  58698. #ifdef HAVE_ECC
  58699. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  58700. /* mark this key as ephemeral */
  58701. if (info->pk.eckg.key != NULL) {
  58702. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  58703. sizeof(info->pk.eckg.key->label));
  58704. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  58705. }
  58706. }
  58707. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  58708. ecc_key key;
  58709. /* perform software based ECC sign */
  58710. #ifdef DEBUG_WOLFSSL
  58711. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  58712. #endif
  58713. if (info->pk.eccsign.key != NULL &&
  58714. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  58715. /* this is an empheral key */
  58716. #ifdef DEBUG_WOLFSSL
  58717. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  58718. "ephemeral key\n");
  58719. #endif
  58720. return CRYPTOCB_UNAVAILABLE;
  58721. }
  58722. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  58723. if (ret != 0) {
  58724. return ret;
  58725. }
  58726. ret = wc_ecc_init(&key);
  58727. if (ret == 0) {
  58728. word32 keyIdx = 0;
  58729. /* load ECC private key and perform private transform */
  58730. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  58731. &key, pDer->length);
  58732. if (ret == 0) {
  58733. ret = wc_ecc_sign_hash(
  58734. info->pk.eccsign.in, info->pk.eccsign.inlen,
  58735. info->pk.eccsign.out, info->pk.eccsign.outlen,
  58736. info->pk.eccsign.rng, &key);
  58737. }
  58738. else {
  58739. /* if decode fails, then fall-back to software based crypto */
  58740. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  58741. "decode failed %d, falling back to software\n", ret);
  58742. ret = CRYPTOCB_UNAVAILABLE;
  58743. }
  58744. wc_ecc_free(&key);
  58745. }
  58746. wc_FreeDer(&pDer); pDer = NULL;
  58747. #ifdef DEBUG_WOLFSSL
  58748. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  58749. ret, *info->pk.eccsign.outlen);
  58750. #endif
  58751. }
  58752. #endif /* HAVE_ECC */
  58753. #ifdef HAVE_ED25519
  58754. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  58755. ed25519_key key;
  58756. /* perform software based ED25519 sign */
  58757. #ifdef DEBUG_WOLFSSL
  58758. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  58759. #endif
  58760. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  58761. if (ret != 0) {
  58762. return ret;
  58763. }
  58764. ret = wc_ed25519_init(&key);
  58765. if (ret == 0) {
  58766. word32 keyIdx = 0;
  58767. /* load ED25519 private key and perform private transform */
  58768. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  58769. &key, pDer->length);
  58770. if (ret == 0) {
  58771. /* calculate public key */
  58772. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  58773. if (ret == 0) {
  58774. key.pubKeySet = 1;
  58775. ret = wc_ed25519_sign_msg_ex(
  58776. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  58777. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  58778. &key, info->pk.ed25519sign.type,
  58779. info->pk.ed25519sign.context,
  58780. info->pk.ed25519sign.contextLen);
  58781. }
  58782. }
  58783. else {
  58784. /* if decode fails, then fall-back to software based crypto */
  58785. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  58786. "decode failed %d, falling back to software\n", ret);
  58787. ret = CRYPTOCB_UNAVAILABLE;
  58788. }
  58789. wc_ed25519_free(&key);
  58790. }
  58791. wc_FreeDer(&pDer); pDer = NULL;
  58792. #ifdef DEBUG_WOLFSSL
  58793. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  58794. ret, *info->pk.ed25519sign.outLen);
  58795. #endif
  58796. }
  58797. #endif /* HAVE_ED25519 */
  58798. }
  58799. (void)thisDevId;
  58800. (void)keyFormat;
  58801. return ret;
  58802. }
  58803. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  58804. static int test_wc_CryptoCb_TLS(int tlsVer,
  58805. const char* cliCaPemFile, const char* cliCertPemFile,
  58806. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  58807. const char* svrCaPemFile, const char* svrCertPemFile,
  58808. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  58809. {
  58810. EXPECT_DECLS;
  58811. callback_functions client_cbf;
  58812. callback_functions server_cbf;
  58813. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58814. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58815. if (tlsVer == WOLFSSL_TLSV1_3) {
  58816. #ifdef WOLFSSL_TLS13
  58817. server_cbf.method = wolfTLSv1_3_server_method;
  58818. client_cbf.method = wolfTLSv1_3_client_method;
  58819. #endif
  58820. }
  58821. else if (tlsVer == WOLFSSL_TLSV1_2) {
  58822. #ifndef WOLFSSL_NO_TLS12
  58823. server_cbf.method = wolfTLSv1_2_server_method;
  58824. client_cbf.method = wolfTLSv1_2_client_method;
  58825. #endif
  58826. }
  58827. else if (tlsVer == WOLFSSL_TLSV1_1) {
  58828. #ifndef NO_OLD_TLS
  58829. server_cbf.method = wolfTLSv1_1_server_method;
  58830. client_cbf.method = wolfTLSv1_1_client_method;
  58831. #endif
  58832. }
  58833. else if (tlsVer == WOLFSSL_TLSV1) {
  58834. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  58835. server_cbf.method = wolfTLSv1_server_method;
  58836. client_cbf.method = wolfTLSv1_client_method;
  58837. #endif
  58838. }
  58839. else if (tlsVer == WOLFSSL_SSLV3) {
  58840. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  58841. defined(WOLFSSL_STATIC_RSA)
  58842. server_cbf.method = wolfSSLv3_server_method;
  58843. client_cbf.method = wolfSSLv3_client_method;
  58844. #endif
  58845. }
  58846. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  58847. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  58848. server_cbf.method = wolfDTLSv1_2_server_method;
  58849. client_cbf.method = wolfDTLSv1_2_client_method;
  58850. #endif
  58851. }
  58852. else if (tlsVer == WOLFSSL_DTLSV1) {
  58853. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  58854. server_cbf.method = wolfDTLSv1_server_method;
  58855. client_cbf.method = wolfDTLSv1_client_method;
  58856. #endif
  58857. }
  58858. if (server_cbf.method == NULL) {
  58859. /* not enabled */
  58860. return TEST_SUCCESS;
  58861. }
  58862. /* Setup the keys for the TLS test */
  58863. client_cbf.certPemFile = cliCertPemFile;
  58864. client_cbf.keyPemFile = cliPubKeyPemFile;
  58865. client_cbf.caPemFile = cliCaPemFile;
  58866. server_cbf.certPemFile = svrCertPemFile;
  58867. server_cbf.keyPemFile = svrPubKeyPemFile;
  58868. server_cbf.caPemFile = svrCaPemFile;
  58869. /* Setup a crypto callback with pointer to private key file for testing */
  58870. client_cbf.devId = 1;
  58871. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  58872. (void*)cliPrivKeyPemFile);
  58873. server_cbf.devId = 2;
  58874. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  58875. (void*)svrPrivKeyPemFile);
  58876. /* Perform TLS server and client test */
  58877. /* First test is at WOLFSSL_CTX level */
  58878. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58879. /* Check for success */
  58880. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58881. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58882. if (EXPECT_SUCCESS()) {
  58883. /* Second test is a WOLFSSL object level */
  58884. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  58885. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58886. }
  58887. /* Check for success */
  58888. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58889. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58890. /* Un register the devId's */
  58891. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  58892. client_cbf.devId = INVALID_DEVID;
  58893. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  58894. server_cbf.devId = INVALID_DEVID;
  58895. return EXPECT_RESULT();
  58896. }
  58897. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  58898. static int test_wc_CryptoCb(void)
  58899. {
  58900. EXPECT_DECLS;
  58901. #ifdef WOLF_CRYPTO_CB
  58902. /* TODO: Add crypto callback API tests */
  58903. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58904. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  58905. int tlsVer;
  58906. #endif
  58907. #ifndef NO_RSA
  58908. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58909. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58910. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  58911. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  58912. TEST_SUCCESS);
  58913. }
  58914. #endif
  58915. #ifdef HAVE_ECC
  58916. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58917. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58918. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  58919. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  58920. TEST_SUCCESS);
  58921. }
  58922. #endif
  58923. #ifdef HAVE_ED25519
  58924. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  58925. if (tlsVer == WOLFSSL_DTLSV1) continue;
  58926. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58927. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  58928. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  58929. TEST_SUCCESS);
  58930. }
  58931. #endif
  58932. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  58933. #endif /* WOLF_CRYPTO_CB */
  58934. return EXPECT_RESULT();
  58935. }
  58936. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  58937. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  58938. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  58939. const char* cliCaPemFile, const char* cliCertPemFile,
  58940. const char* cliPrivKeyPemFile,
  58941. const char* svrCaPemFile, const char* svrCertPemFile,
  58942. const char* svrPrivKeyPemFile,
  58943. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  58944. {
  58945. EXPECT_DECLS;
  58946. callback_functions client_cbf;
  58947. callback_functions server_cbf;
  58948. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58949. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58950. if (tlsVer == WOLFSSL_TLSV1_3) {
  58951. #ifdef WOLFSSL_TLS13
  58952. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  58953. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  58954. #endif
  58955. }
  58956. else if (tlsVer == WOLFSSL_TLSV1_2) {
  58957. #ifndef WOLFSSL_NO_TLS12
  58958. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  58959. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  58960. #endif
  58961. }
  58962. else if (tlsVer == WOLFSSL_TLSV1_1) {
  58963. #ifndef NO_OLD_TLS
  58964. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  58965. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  58966. #endif
  58967. }
  58968. else if (tlsVer == WOLFSSL_TLSV1) {
  58969. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  58970. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  58971. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  58972. #endif
  58973. }
  58974. else if (tlsVer == WOLFSSL_SSLV3) {
  58975. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  58976. defined(WOLFSSL_STATIC_RSA)
  58977. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  58978. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  58979. #endif
  58980. }
  58981. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  58982. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  58983. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  58984. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  58985. #endif
  58986. }
  58987. else if (tlsVer == WOLFSSL_DTLSV1) {
  58988. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  58989. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  58990. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  58991. #endif
  58992. }
  58993. if (server_cbf.method_ex == NULL) {
  58994. /* not enabled */
  58995. return TEST_SUCCESS;
  58996. }
  58997. /* Setup the keys for the TLS test */
  58998. client_cbf.certPemFile = cliCertPemFile;
  58999. client_cbf.keyPemFile = cliPrivKeyPemFile;
  59000. client_cbf.caPemFile = cliCaPemFile;
  59001. server_cbf.certPemFile = svrCertPemFile;
  59002. server_cbf.keyPemFile = svrPrivKeyPemFile;
  59003. server_cbf.caPemFile = svrCaPemFile;
  59004. client_cbf.mem = cliMem;
  59005. client_cbf.memSz = cliMemSz;
  59006. server_cbf.mem = svrMem;
  59007. server_cbf.memSz = svrMemSz;
  59008. client_cbf.devId = INVALID_DEVID;
  59009. server_cbf.devId = INVALID_DEVID;
  59010. /* Perform TLS server and client test */
  59011. /* First test is at WOLFSSL_CTX level */
  59012. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  59013. /* Check for success */
  59014. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  59015. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  59016. if (EXPECT_SUCCESS()) {
  59017. /* Second test is a WOLFSSL object level */
  59018. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  59019. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  59020. }
  59021. /* Check for success */
  59022. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  59023. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  59024. return EXPECT_RESULT();
  59025. }
  59026. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  59027. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  59028. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  59029. {
  59030. EXPECT_DECLS;
  59031. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  59032. WOLFSSL_MEM_STATS mem_stats;
  59033. WOLFSSL_MEM_CONN_STATS ssl_stats;
  59034. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  59035. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  59036. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59037. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  59038. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59039. #endif
  59040. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  59041. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  59042. /* this should fail because kMaxCtxClients == 2 */
  59043. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  59044. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  59045. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  59046. wolfSSL_PrintStatsConn(&ssl_stats);
  59047. #endif
  59048. (void)ssl_stats;
  59049. }
  59050. /* display collected statistics */
  59051. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  59052. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  59053. wolfSSL_PrintStats(&mem_stats);
  59054. #endif
  59055. (void)mem_stats;
  59056. }
  59057. wolfSSL_free(ssl1);
  59058. wolfSSL_free(ssl2);
  59059. return EXPECT_RESULT();
  59060. }
  59061. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  59062. static int test_wolfSSL_CTX_StaticMemory(void)
  59063. {
  59064. EXPECT_DECLS;
  59065. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  59066. wolfSSL_method_func method_func;
  59067. WOLFSSL_CTX* ctx;
  59068. const int kMaxCtxClients = 2;
  59069. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  59070. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  59071. int tlsVer;
  59072. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  59073. #endif
  59074. #endif
  59075. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  59076. #ifndef NO_WOLFSSL_SERVER
  59077. #ifndef WOLFSSL_NO_TLS12
  59078. method_func = wolfTLSv1_2_server_method_ex;
  59079. #else
  59080. method_func = wolfTLSv1_3_server_method_ex;
  59081. #endif
  59082. #else
  59083. #ifndef WOLFSSL_NO_TLS12
  59084. method_func = wolfTLSv1_2_client_method_ex;
  59085. #else
  59086. method_func = wolfTLSv1_3_client_method_ex;
  59087. #endif
  59088. #endif
  59089. /* Test creating CTX directly from static memory pool */
  59090. ctx = NULL;
  59091. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  59092. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  59093. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  59094. wolfSSL_CTX_free(ctx);
  59095. ctx = NULL;
  59096. /* Test for heap allocated CTX, then assigning static pool to it */
  59097. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  59098. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  59099. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  59100. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  59101. wolfSSL_CTX_free(ctx);
  59102. /* TLS Level Tests using static memory */
  59103. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  59104. #ifndef NO_RSA
  59105. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  59106. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  59107. svrCertFile, cliCertFile, cliKeyFile,
  59108. cliCertFile, svrCertFile, svrKeyFile,
  59109. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  59110. TEST_SUCCESS);
  59111. }
  59112. #endif
  59113. #ifdef HAVE_ECC
  59114. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  59115. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  59116. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  59117. cliEccCertFile, eccCertFile, eccKeyFile,
  59118. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  59119. TEST_SUCCESS);
  59120. }
  59121. #endif
  59122. #ifdef HAVE_ED25519
  59123. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  59124. if (tlsVer == WOLFSSL_DTLSV1) continue;
  59125. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  59126. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  59127. cliEdCertFile, edCertFile, edKeyFile,
  59128. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  59129. TEST_SUCCESS);
  59130. }
  59131. #endif
  59132. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  59133. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  59134. return EXPECT_RESULT();
  59135. }
  59136. static int test_openssl_FIPS_drbg(void)
  59137. {
  59138. EXPECT_DECLS;
  59139. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  59140. DRBG_CTX* dctx = NULL;
  59141. byte data1[32], data2[32], zeroData[32];
  59142. byte testSeed[16];
  59143. size_t dlen = sizeof(data1);
  59144. int i;
  59145. XMEMSET(data1, 0, dlen);
  59146. XMEMSET(data2, 0, dlen);
  59147. XMEMSET(zeroData, 0, sizeof(zeroData));
  59148. for (i = 0; i < (int)sizeof(testSeed); i++) {
  59149. testSeed[i] = (byte)i;
  59150. }
  59151. ExpectNotNull(dctx = FIPS_get_default_drbg());
  59152. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  59153. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  59154. WOLFSSL_SUCCESS);
  59155. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  59156. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  59157. WOLFSSL_SUCCESS);
  59158. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  59159. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  59160. WOLFSSL_SUCCESS);
  59161. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  59162. WOLFSSL_SUCCESS);
  59163. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  59164. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  59165. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  59166. #ifndef HAVE_GLOBAL_RNG
  59167. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  59168. wolfSSL_FIPS_drbg_free(dctx);
  59169. #endif
  59170. #endif
  59171. return EXPECT_RESULT();
  59172. }
  59173. static int test_wolfSSL_FIPS_mode(void)
  59174. {
  59175. EXPECT_DECLS;
  59176. #if defined(OPENSSL_ALL)
  59177. #ifdef HAVE_FIPS
  59178. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  59179. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  59180. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  59181. #else
  59182. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  59183. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  59184. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  59185. #endif
  59186. #endif
  59187. return EXPECT_RESULT();
  59188. }
  59189. #ifdef WOLFSSL_DTLS
  59190. /* Prints out the current window */
  59191. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  59192. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  59193. int i;
  59194. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  59195. word32 b = w[i];
  59196. int j;
  59197. /* Prints out a 32 bit binary number in big endian order */
  59198. for (j = 0; j < 32; j++, b <<= 1) {
  59199. if (b & (((word32)1) << 31))
  59200. fprintf(stderr, "1");
  59201. else
  59202. fprintf(stderr, "0");
  59203. }
  59204. fprintf(stderr, " ");
  59205. }
  59206. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  59207. #else
  59208. (void)h;
  59209. (void)l;
  59210. (void)w;
  59211. #endif
  59212. }
  59213. /* a - cur_hi
  59214. * b - cur_lo
  59215. * c - next_hi
  59216. * d - next_lo
  59217. * e - window
  59218. * f - expected next_hi
  59219. * g - expected next_lo
  59220. * h - expected window[1]
  59221. * i - expected window[0]
  59222. */
  59223. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  59224. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  59225. DUW_TEST_print_window_binary((a), (b), (e)); \
  59226. ExpectIntEQ((c), (f)); \
  59227. ExpectIntEQ((d), (g)); \
  59228. ExpectIntEQ((e)[1], (h)); \
  59229. ExpectIntEQ((e)[0], (i)); \
  59230. } while (0)
  59231. static int test_wolfSSL_DtlsUpdateWindow(void)
  59232. {
  59233. EXPECT_DECLS;
  59234. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  59235. word32 next_lo = 0;
  59236. word16 next_hi = 0;
  59237. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  59238. fprintf(stderr, "\n");
  59239. #endif
  59240. XMEMSET(window, 0, sizeof window);
  59241. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  59242. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  59243. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  59244. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  59245. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  59246. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  59247. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  59248. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  59249. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  59250. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  59251. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  59252. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  59253. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  59254. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  59255. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  59256. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  59257. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  59258. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  59259. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  59260. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  59261. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  59262. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  59263. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  59264. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  59265. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  59266. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  59267. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  59268. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  59269. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  59270. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  59271. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  59272. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  59273. return EXPECT_RESULT();
  59274. }
  59275. #endif /* WOLFSSL_DTLS */
  59276. #ifdef WOLFSSL_DTLS
  59277. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  59278. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  59279. {
  59280. DtlsMsg* cur;
  59281. static byte msg[100];
  59282. static byte msgInit = 0;
  59283. if (!msgInit) {
  59284. int i;
  59285. for (i = 0; i < 100; i++)
  59286. msg[i] = i + 1;
  59287. msgInit = 1;
  59288. }
  59289. /* Sanitize test parameters */
  59290. if (len > sizeof(msg))
  59291. return -1;
  59292. if (f_offset + f_len > sizeof(msg))
  59293. return -1;
  59294. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  59295. if (ssl->dtls_rx_msg_list == NULL)
  59296. return -100;
  59297. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  59298. return -200;
  59299. if (cur->fragBucketListCount != f_count)
  59300. return -300;
  59301. if (cur->ready != ready)
  59302. return -400;
  59303. if (cur->bytesReceived != bytesReceived)
  59304. return -500;
  59305. if (ready) {
  59306. if (cur->fragBucketList != NULL)
  59307. return -600;
  59308. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  59309. return -700;
  59310. }
  59311. else {
  59312. DtlsFragBucket* fb;
  59313. if (cur->fragBucketList == NULL)
  59314. return -800;
  59315. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  59316. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  59317. return -900;
  59318. }
  59319. }
  59320. return 0;
  59321. }
  59322. static int test_wolfSSL_DTLS_fragment_buckets(void)
  59323. {
  59324. EXPECT_DECLS;
  59325. WOLFSSL ssl[1];
  59326. XMEMSET(ssl, 0, sizeof(*ssl));
  59327. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  59328. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  59329. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  59330. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  59331. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  59332. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  59333. /* Test all permutations of 3 regions */
  59334. /* 1 2 3 */
  59335. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  59336. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  59337. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  59338. /* 1 3 2 */
  59339. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  59340. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  59341. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  59342. /* 2 1 3 */
  59343. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  59344. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  59345. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  59346. /* 2 3 1 */
  59347. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  59348. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  59349. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  59350. /* 3 1 2 */
  59351. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  59352. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  59353. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  59354. /* 3 2 1 */
  59355. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  59356. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  59357. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  59358. /* Test overlapping regions */
  59359. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  59360. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  59361. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  59362. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  59363. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  59364. /* Test overlapping multiple regions */
  59365. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  59366. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  59367. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  59368. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  59369. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  59370. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  59371. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  59372. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  59373. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  59374. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  59375. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  59376. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  59377. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  59378. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  59379. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  59380. ssl->dtls_rx_msg_list = NULL;
  59381. ssl->dtls_rx_msg_list_sz = 0;
  59382. return EXPECT_RESULT();
  59383. }
  59384. #endif
  59385. #if !defined(NO_FILESYSTEM) && \
  59386. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59387. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59388. static int test_wolfSSL_dtls_stateless2(void)
  59389. {
  59390. EXPECT_DECLS;
  59391. WOLFSSL *ssl_c = NULL;
  59392. WOLFSSL *ssl_c2 = NULL;
  59393. WOLFSSL *ssl_s = NULL;
  59394. struct test_memio_ctx test_ctx;
  59395. WOLFSSL_CTX *ctx_c = NULL;
  59396. WOLFSSL_CTX *ctx_s = NULL;
  59397. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59398. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59399. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59400. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  59401. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  59402. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  59403. /* send CH */
  59404. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  59405. (ssl_c2->error == WANT_READ));
  59406. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59407. (ssl_s->error == WANT_READ));
  59408. ExpectIntNE(test_ctx.c_len, 0);
  59409. /* consume HRR */
  59410. test_ctx.c_len = 0;
  59411. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59412. wolfSSL_free(ssl_c2);
  59413. wolfSSL_free(ssl_c);
  59414. wolfSSL_free(ssl_s);
  59415. wolfSSL_CTX_free(ctx_c);
  59416. wolfSSL_CTX_free(ctx_s);
  59417. return EXPECT_RESULT();
  59418. }
  59419. #ifdef HAVE_MAX_FRAGMENT
  59420. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  59421. {
  59422. EXPECT_DECLS;
  59423. WOLFSSL *ssl_c = NULL;
  59424. WOLFSSL *ssl_c2 = NULL;
  59425. WOLFSSL *ssl_s = NULL;
  59426. struct test_memio_ctx test_ctx;
  59427. WOLFSSL_CTX *ctx_c = NULL;
  59428. WOLFSSL_CTX *ctx_s = NULL;
  59429. word16 max_fragment = 0;
  59430. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59431. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59432. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59433. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  59434. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  59435. WOLFSSL_SUCCESS);
  59436. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  59437. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  59438. if (ssl_s != NULL) {
  59439. max_fragment = ssl_s->max_fragment;
  59440. }
  59441. /* send CH */
  59442. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  59443. (ssl_c2->error == WANT_READ));
  59444. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59445. (ssl_s->error == WANT_READ));
  59446. /* CH without cookie shouldn't change state */
  59447. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  59448. ExpectIntNE(test_ctx.c_len, 0);
  59449. /* consume HRR from buffer */
  59450. test_ctx.c_len = 0;
  59451. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59452. wolfSSL_free(ssl_c2);
  59453. wolfSSL_free(ssl_c);
  59454. wolfSSL_free(ssl_s);
  59455. wolfSSL_CTX_free(ctx_c);
  59456. wolfSSL_CTX_free(ctx_s);
  59457. return EXPECT_RESULT();
  59458. }
  59459. #endif /* HAVE_MAX_FRAGMENT */
  59460. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  59461. #define ROUNDS_WITH_HVR 4
  59462. #define ROUNDS_WITHOUT_HVR 2
  59463. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  59464. static int buf_is_hvr(const byte *data, int len)
  59465. {
  59466. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  59467. return 0;
  59468. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  59469. }
  59470. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  59471. {
  59472. EXPECT_DECLS;
  59473. struct test_memio_ctx test_ctx;
  59474. WOLFSSL_CTX *ctx_c = NULL;
  59475. WOLFSSL_CTX *ctx_s = NULL;
  59476. WOLFSSL *ssl_c = NULL;
  59477. WOLFSSL *ssl_s = NULL;
  59478. WOLFSSL_SESSION *sess = NULL;
  59479. int round_trips;
  59480. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59481. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  59482. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59483. #ifdef HAVE_SESSION_TICKET
  59484. if (useticket) {
  59485. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  59486. }
  59487. #endif
  59488. round_trips = ROUNDS_WITH_HVR;
  59489. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  59490. &round_trips), 0);
  59491. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  59492. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59493. wolfSSL_shutdown(ssl_c);
  59494. wolfSSL_shutdown(ssl_s);
  59495. wolfSSL_free(ssl_c);
  59496. ssl_c = NULL;
  59497. wolfSSL_free(ssl_s);
  59498. ssl_s = NULL;
  59499. test_ctx.c_len = test_ctx.s_len = 0;
  59500. /* make resumption invalid */
  59501. if (bad && (sess != NULL)) {
  59502. if (useticket) {
  59503. #ifdef HAVE_SESSION_TICKET
  59504. if (sess->ticket != NULL) {
  59505. sess->ticket[0] = !sess->ticket[0];
  59506. }
  59507. #endif /* HAVE_SESSION_TICKET */
  59508. }
  59509. else {
  59510. sess->sessionID[0] = !sess->sessionID[0];
  59511. }
  59512. }
  59513. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  59514. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  59515. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  59516. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  59517. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  59518. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  59519. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  59520. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59521. (ssl_c->error == WANT_READ));
  59522. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59523. (ssl_s->error == WANT_READ));
  59524. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  59525. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  59526. if (!useticket) {
  59527. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  59528. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  59529. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  59530. }
  59531. wolfSSL_SESSION_free(sess);
  59532. wolfSSL_free(ssl_c);
  59533. wolfSSL_free(ssl_s);
  59534. wolfSSL_CTX_free(ctx_c);
  59535. wolfSSL_CTX_free(ctx_s);
  59536. return EXPECT_RESULT();
  59537. }
  59538. static int test_wolfSSL_dtls_stateless_resume(void)
  59539. {
  59540. EXPECT_DECLS;
  59541. #ifdef HAVE_SESSION_TICKET
  59542. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  59543. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  59544. #endif /* HAVE_SESION_TICKET */
  59545. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  59546. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  59547. return EXPECT_RESULT();
  59548. }
  59549. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  59550. #if !defined(NO_OLD_TLS)
  59551. static int test_wolfSSL_dtls_stateless_downgrade(void)
  59552. {
  59553. EXPECT_DECLS;
  59554. WOLFSSL_CTX *ctx_c = NULL;
  59555. WOLFSSL_CTX *ctx_c2 = NULL;
  59556. WOLFSSL_CTX *ctx_s = NULL;
  59557. WOLFSSL *ssl_c = NULL;
  59558. WOLFSSL *ssl_c2 = NULL;
  59559. WOLFSSL *ssl_s = NULL;
  59560. struct test_memio_ctx test_ctx;
  59561. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59562. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59563. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59564. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  59565. WOLFSSL_SUCCESS);
  59566. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  59567. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  59568. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  59569. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  59570. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  59571. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  59572. /* send CH */
  59573. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  59574. (ssl_c2->error == WANT_READ));
  59575. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59576. (ssl_s->error == WANT_READ));
  59577. ExpectIntNE(test_ctx.c_len, 0);
  59578. /* consume HRR */
  59579. test_ctx.c_len = 0;
  59580. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59581. wolfSSL_free(ssl_c2);
  59582. wolfSSL_free(ssl_c);
  59583. wolfSSL_free(ssl_s);
  59584. wolfSSL_CTX_free(ctx_c);
  59585. wolfSSL_CTX_free(ctx_c2);
  59586. wolfSSL_CTX_free(ctx_s);
  59587. return EXPECT_RESULT();
  59588. }
  59589. #endif /* !defined(NO_OLD_TLS) */
  59590. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59591. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  59592. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59593. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59594. static int test_WOLFSSL_dtls_version_alert(void)
  59595. {
  59596. EXPECT_DECLS;
  59597. struct test_memio_ctx test_ctx;
  59598. WOLFSSL_CTX *ctx_c = NULL;
  59599. WOLFSSL_CTX *ctx_s = NULL;
  59600. WOLFSSL *ssl_c = NULL;
  59601. WOLFSSL *ssl_s = NULL;
  59602. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59603. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59604. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  59605. /* client hello */
  59606. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59607. (ssl_c->error == WANT_READ));
  59608. /* hrr */
  59609. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59610. (ssl_s->error == WANT_READ));
  59611. /* client hello 1 */
  59612. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59613. (ssl_c->error == WANT_READ));
  59614. /* server hello */
  59615. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59616. (ssl_s->error == WANT_READ));
  59617. /* should fail */
  59618. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59619. (ssl_c->error == VERSION_ERROR));
  59620. /* shuould fail */
  59621. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59622. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  59623. wolfSSL_free(ssl_c);
  59624. wolfSSL_free(ssl_s);
  59625. wolfSSL_CTX_free(ctx_c);
  59626. wolfSSL_CTX_free(ctx_s);
  59627. return EXPECT_RESULT();
  59628. }
  59629. #else
  59630. static int test_WOLFSSL_dtls_version_alert(void)
  59631. {
  59632. return TEST_SKIPPED;
  59633. }
  59634. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  59635. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  59636. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  59637. */
  59638. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  59639. && defined(WOLFSSL_TLS13) && \
  59640. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  59641. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59642. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  59643. {
  59644. struct test_memio_ctx *test_ctx;
  59645. byte buf[2048];
  59646. int idx, sz;
  59647. word32 tmp;
  59648. int ret;
  59649. idx = 5; /* space for record header */
  59650. buf[idx] = session_ticket; /* type */
  59651. idx++;
  59652. tmp = OPAQUE32_LEN +
  59653. OPAQUE32_LEN +
  59654. OPAQUE8_LEN + nonceLength +
  59655. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  59656. c32to24(tmp, buf + idx);
  59657. idx += OPAQUE24_LEN;
  59658. c32toa((word32)12345, buf+idx); /* lifetime */
  59659. idx += OPAQUE32_LEN;
  59660. c32toa((word32)12345, buf+idx); /* add */
  59661. idx += OPAQUE32_LEN;
  59662. buf[idx] = nonceLength; /* nonce length */
  59663. idx++;
  59664. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  59665. idx += nonceLength;
  59666. tmp = 1; /* ticket len */
  59667. c16toa((word16)tmp, buf+idx);
  59668. idx += 2;
  59669. buf[idx] = 0xFF; /* ticket */
  59670. idx++;
  59671. tmp = 0; /* ext len */
  59672. c16toa((word16)tmp, buf+idx);
  59673. idx += 2;
  59674. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  59675. handshake, 0, 0, 0);
  59676. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  59677. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  59678. return !(ret == sz);
  59679. }
  59680. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  59681. {
  59682. int ret = 0;
  59683. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  59684. ret = -1;
  59685. }
  59686. else {
  59687. int i;
  59688. for (i = 0; i < len; i++) {
  59689. if (sess->ticketNonce.data[i] != len) {
  59690. ret = -1;
  59691. break;
  59692. }
  59693. }
  59694. }
  59695. return ret;
  59696. }
  59697. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  59698. {
  59699. EXPECT_DECLS;
  59700. char *buf[1024];
  59701. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  59702. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  59703. (ssl_c->error == WANT_READ));
  59704. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  59705. return EXPECT_RESULT();
  59706. }
  59707. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  59708. {
  59709. EXPECT_DECLS;
  59710. WOLFSSL_SESSION *sess = NULL;
  59711. WOLFSSL_SESSION *cached = NULL;
  59712. WOLFSSL_CTX *ctx = ssl_c->ctx;
  59713. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  59714. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59715. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  59716. NULL, ssl_c->options.side, 1,NULL), 0);
  59717. ExpectNotNull(cached = wolfSSL_SESSION_new());
  59718. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  59719. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  59720. wolfSSL_SESSION_free(cached);
  59721. wolfSSL_SESSION_free(sess);
  59722. return EXPECT_RESULT();
  59723. }
  59724. static int test_ticket_nonce_malloc(void)
  59725. {
  59726. EXPECT_DECLS;
  59727. struct test_memio_ctx test_ctx;
  59728. WOLFSSL_CTX *ctx_c = NULL;
  59729. WOLFSSL_CTX *ctx_s = NULL;
  59730. WOLFSSL *ssl_c = NULL;
  59731. WOLFSSL *ssl_s = NULL;
  59732. byte small;
  59733. byte medium;
  59734. byte big;
  59735. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59736. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59737. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59738. /* will send ticket manually */
  59739. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  59740. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  59741. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  59742. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  59743. (ssl_s->options.handShakeDone == 0)) {
  59744. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  59745. (ssl_c->error == WANT_READ));
  59746. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  59747. (ssl_s->error == WANT_READ));
  59748. }
  59749. small = TLS13_TICKET_NONCE_STATIC_SZ;
  59750. medium = small + 20 <= 255 ? small + 20 : 255;
  59751. big = medium + 20 <= 255 ? small + 20 : 255;
  59752. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  59753. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  59754. ssl_c->session->ticketNonce.dataStatic);
  59755. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  59756. TEST_SUCCESS);
  59757. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  59758. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  59759. TEST_SUCCESS);
  59760. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  59761. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  59762. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  59763. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  59764. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  59765. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  59766. wolfSSL_free(ssl_c);
  59767. wolfSSL_free(ssl_s);
  59768. wolfSSL_CTX_free(ctx_c);
  59769. wolfSSL_CTX_free(ctx_s);
  59770. return EXPECT_RESULT();
  59771. }
  59772. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  59773. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  59774. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  59775. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59776. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  59777. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59778. static int test_ticket_ret_create(void)
  59779. {
  59780. EXPECT_DECLS;
  59781. WOLFSSL_CTX *ctx_c = NULL;
  59782. WOLFSSL_CTX *ctx_s = NULL;
  59783. WOLFSSL *ssl_c = NULL;
  59784. WOLFSSL *ssl_s = NULL;
  59785. byte ticket[SESSION_TICKET_LEN];
  59786. struct test_memio_ctx test_ctx;
  59787. WOLFSSL_SESSION *sess = NULL;
  59788. word16 ticketLen = 0;
  59789. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59790. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59791. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59792. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  59793. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  59794. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  59795. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59796. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59797. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  59798. if (sess != NULL) {
  59799. ticketLen = sess->ticketLen;
  59800. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  59801. }
  59802. wolfSSL_free(ssl_c);
  59803. ssl_c = NULL;
  59804. wolfSSL_free(ssl_s);
  59805. ssl_s = NULL;
  59806. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  59807. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  59808. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  59809. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  59810. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  59811. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  59812. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  59813. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59814. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  59815. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  59816. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  59817. wolfSSL_SESSION_free(sess);
  59818. wolfSSL_free(ssl_c);
  59819. wolfSSL_free(ssl_s);
  59820. wolfSSL_CTX_free(ctx_c);
  59821. wolfSSL_CTX_free(ctx_s);
  59822. return EXPECT_RESULT();
  59823. }
  59824. #else
  59825. static int test_ticket_ret_create(void)
  59826. {
  59827. return TEST_SKIPPED;
  59828. }
  59829. #endif
  59830. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  59831. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  59832. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59833. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59834. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  59835. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  59836. {
  59837. int ret;
  59838. WOLFSSL_SESSION* session = NULL;
  59839. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  59840. if (!wolfSSL_is_server(ssl)) {
  59841. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  59842. AssertNotNull(session);
  59843. }
  59844. do {
  59845. ret = wolfSSL_shutdown(ssl);
  59846. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  59847. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  59848. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  59849. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  59850. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  59851. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  59852. #endif
  59853. if (!wolfSSL_is_server(ssl)) {
  59854. /* client */
  59855. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59856. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59857. wolfSSL_set_session(ssl, session);
  59858. wolfSSL_SESSION_free(session);
  59859. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  59860. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  59861. }
  59862. else {
  59863. /* server */
  59864. /* Different ciphersuite so that the ticket will be invalidated based on
  59865. * the ciphersuite */
  59866. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  59867. WOLFSSL_SUCCESS);
  59868. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  59869. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  59870. }
  59871. }
  59872. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  59873. {
  59874. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  59875. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59876. WOLFSSL_SUCCESS);
  59877. }
  59878. static int test_ticket_and_psk_mixing(void)
  59879. {
  59880. EXPECT_DECLS;
  59881. /* Test mixing tickets and regular PSK */
  59882. callback_functions client_cbs, server_cbs;
  59883. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59884. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59885. client_cbs.method = wolfTLSv1_3_client_method;
  59886. server_cbs.method = wolfTLSv1_3_server_method;
  59887. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  59888. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  59889. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  59890. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59891. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  59892. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  59893. return EXPECT_RESULT();
  59894. }
  59895. #else
  59896. static int test_ticket_and_psk_mixing(void)
  59897. {
  59898. return TEST_SKIPPED;
  59899. }
  59900. #endif
  59901. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  59902. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  59903. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59904. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  59905. static int test_prioritize_psk_cb_called = FALSE;
  59906. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  59907. const char* identity, unsigned char* key, unsigned int key_max_len,
  59908. const char** ciphersuite)
  59909. {
  59910. test_prioritize_psk_cb_called = TRUE;
  59911. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  59912. }
  59913. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  59914. {
  59915. int ret;
  59916. WOLFSSL_SESSION* session = NULL;
  59917. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  59918. if (!wolfSSL_is_server(ssl)) {
  59919. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  59920. AssertNotNull(session);
  59921. }
  59922. do {
  59923. ret = wolfSSL_shutdown(ssl);
  59924. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  59925. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  59926. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  59927. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  59928. * important. */
  59929. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59930. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59931. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  59932. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  59933. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  59934. #endif
  59935. if (!wolfSSL_is_server(ssl)) {
  59936. /* client */
  59937. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  59938. wolfSSL_set_session(ssl, session);
  59939. wolfSSL_SESSION_free(session);
  59940. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  59941. }
  59942. else {
  59943. /* server */
  59944. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  59945. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  59946. #ifdef WOLFSSL_PRIORITIZE_PSK
  59947. /* The ticket should be first tried with all ciphersuites and chosen */
  59948. AssertFalse(test_prioritize_psk_cb_called);
  59949. #else
  59950. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  59951. * callback that sets this var. */
  59952. AssertTrue(test_prioritize_psk_cb_called);
  59953. #endif
  59954. }
  59955. }
  59956. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  59957. {
  59958. if (!wolfSSL_is_server(ssl))
  59959. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59960. WOLFSSL_SUCCESS);
  59961. else
  59962. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59963. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59964. }
  59965. static int test_prioritize_psk(void)
  59966. {
  59967. EXPECT_DECLS;
  59968. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  59969. * of the PSK's will be followed instead of the ciphersuite. */
  59970. callback_functions client_cbs, server_cbs;
  59971. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59972. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59973. client_cbs.method = wolfTLSv1_3_client_method;
  59974. server_cbs.method = wolfTLSv1_3_server_method;
  59975. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59976. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59977. client_cbs.on_result = test_prioritize_psk_on_result;
  59978. server_cbs.on_result = test_prioritize_psk_on_result;
  59979. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59980. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  59981. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  59982. return EXPECT_RESULT();
  59983. }
  59984. #else
  59985. static int test_prioritize_psk(void)
  59986. {
  59987. return TEST_SKIPPED;
  59988. }
  59989. #endif
  59990. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  59991. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59992. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59993. !defined(WOLFSSL_NO_TLS12)
  59994. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  59995. {
  59996. EXPECT_DECLS;
  59997. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  59998. /* Set TLS 1.3 specific suite */
  59999. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  60000. return EXPECT_RESULT();
  60001. }
  60002. static int test_wolfSSL_CTX_set_ciphersuites(void)
  60003. {
  60004. EXPECT_DECLS;
  60005. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  60006. * do a 1.2 connection. */
  60007. test_ssl_cbf client_cbs, server_cbs;
  60008. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60009. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60010. client_cbs.method = wolfTLSv1_2_client_method;
  60011. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  60012. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  60013. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60014. &server_cbs, NULL), TEST_SUCCESS);
  60015. return EXPECT_RESULT();
  60016. }
  60017. #else
  60018. static int test_wolfSSL_CTX_set_ciphersuites(void)
  60019. {
  60020. return TEST_SKIPPED;
  60021. }
  60022. #endif
  60023. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  60024. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60025. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  60026. {
  60027. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  60028. return TEST_SUCCESS;
  60029. }
  60030. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  60031. {
  60032. EXPECT_DECLS;
  60033. WOLFSSL_ALERT_HISTORY h;
  60034. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  60035. ExpectIntEQ(h.last_rx.level, alert_fatal);
  60036. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  60037. return EXPECT_RESULT();
  60038. }
  60039. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  60040. {
  60041. EXPECT_DECLS;
  60042. test_ssl_cbf client_cbs, server_cbs;
  60043. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60044. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60045. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  60046. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  60047. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  60048. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  60049. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  60050. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60051. &server_cbs, NULL), TEST_FAIL);
  60052. return EXPECT_RESULT();
  60053. }
  60054. #else
  60055. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  60056. {
  60057. return TEST_SKIPPED;
  60058. }
  60059. #endif
  60060. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  60061. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  60062. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  60063. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  60064. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  60065. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  60066. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  60067. static int test_TLS_13_ticket_different_ciphers_run = 0;
  60068. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  60069. {
  60070. EXPECT_DECLS;
  60071. switch (test_TLS_13_ticket_different_ciphers_run) {
  60072. case 0:
  60073. /* First run */
  60074. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  60075. WOLFSSL_SUCCESS);
  60076. if (wolfSSL_is_server(ssl)) {
  60077. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  60078. wolfSSL_get_SSL_CTX(ssl));
  60079. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  60080. test_TLS_13_ticket_different_ciphers_ctx));
  60081. }
  60082. break;
  60083. case 1:
  60084. /* Second run */
  60085. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  60086. "TLS13-AES128-GCM-SHA256"),
  60087. WOLFSSL_SUCCESS);
  60088. if (!wolfSSL_is_server(ssl)) {
  60089. ExpectIntEQ(wolfSSL_set_session(ssl,
  60090. test_TLS_13_ticket_different_ciphers_session),
  60091. WOLFSSL_SUCCESS);
  60092. }
  60093. break;
  60094. default:
  60095. /* Bad state? */
  60096. Fail(("Should not enter here"), ("Should not enter here"));
  60097. }
  60098. return EXPECT_RESULT();
  60099. }
  60100. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  60101. {
  60102. EXPECT_DECLS;
  60103. switch (test_TLS_13_ticket_different_ciphers_run) {
  60104. case 0:
  60105. /* First run */
  60106. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  60107. wolfSSL_get1_session(ssl));
  60108. break;
  60109. case 1:
  60110. /* Second run */
  60111. ExpectTrue(wolfSSL_session_reused(ssl));
  60112. break;
  60113. default:
  60114. /* Bad state? */
  60115. Fail(("Should not enter here"), ("Should not enter here"));
  60116. }
  60117. return EXPECT_RESULT();
  60118. }
  60119. static int test_TLS_13_ticket_different_ciphers(void)
  60120. {
  60121. EXPECT_DECLS;
  60122. /* Check that we handle the connection when the ticket doesn't match
  60123. * the first ciphersuite. */
  60124. test_ssl_cbf client_cbs, server_cbs;
  60125. struct test_params {
  60126. method_provider client_meth;
  60127. method_provider server_meth;
  60128. int doUdp;
  60129. } params[] = {
  60130. #ifdef WOLFSSL_DTLS13
  60131. /* Test that the stateless code handles sessions correctly */
  60132. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  60133. #endif
  60134. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  60135. };
  60136. size_t i;
  60137. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  60138. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60139. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60140. test_TLS_13_ticket_different_ciphers_run = 0;
  60141. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  60142. client_cbs.method = params[i].client_meth;
  60143. server_cbs.method = params[i].server_meth;
  60144. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  60145. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  60146. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  60147. server_cbs.ticNoInit = 1;
  60148. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60149. &server_cbs, NULL), TEST_SUCCESS);
  60150. test_TLS_13_ticket_different_ciphers_run++;
  60151. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  60152. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60153. &server_cbs, NULL), TEST_SUCCESS);
  60154. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  60155. test_TLS_13_ticket_different_ciphers_session = NULL;
  60156. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  60157. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  60158. }
  60159. return EXPECT_RESULT();
  60160. }
  60161. #else
  60162. static int test_TLS_13_ticket_different_ciphers(void)
  60163. {
  60164. return TEST_SKIPPED;
  60165. }
  60166. #endif
  60167. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  60168. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  60169. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  60170. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  60171. byte test_extra_alerts_wrong_cs_sh[] = {
  60172. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  60173. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  60174. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  60175. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  60176. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  60177. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  60178. 0xbb, 0x07, 0x54, 0x1c,
  60179. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  60180. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  60181. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  60182. };
  60183. static int test_extra_alerts_wrong_cs(void)
  60184. {
  60185. EXPECT_DECLS;
  60186. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  60187. struct test_memio_ctx test_ctx;
  60188. WOLFSSL_CTX *ctx_c = NULL;
  60189. WOLFSSL_ALERT_HISTORY h;
  60190. WOLFSSL *ssl_c = NULL;
  60191. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60192. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60193. wolfTLSv1_2_client_method, NULL), 0);
  60194. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  60195. WOLFSSL_SUCCESS);
  60196. /* CH */
  60197. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60198. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60199. WOLFSSL_ERROR_WANT_READ);
  60200. /* consume CH */
  60201. test_ctx.s_len = 0;
  60202. /* inject SH */
  60203. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  60204. sizeof(test_extra_alerts_wrong_cs_sh));
  60205. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  60206. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60207. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60208. WOLFSSL_ERROR_WANT_READ);
  60209. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  60210. ExpectIntEQ(h.last_tx.code, handshake_failure);
  60211. ExpectIntEQ(h.last_tx.level, alert_fatal);
  60212. wolfSSL_free(ssl_c);
  60213. wolfSSL_CTX_free(ctx_c);
  60214. #endif
  60215. return EXPECT_RESULT();
  60216. }
  60217. #else
  60218. static int test_extra_alerts_wrong_cs(void)
  60219. {
  60220. return TEST_SKIPPED;
  60221. }
  60222. #endif
  60223. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  60224. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  60225. #define TEST_CS_DOWNGRADE_CLIENT "ECDHE-RSA-AES256-GCM-SHA384"
  60226. byte test_wrong_cs_downgrade_sh[] = {
  60227. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0x10,
  60228. 0x2c, 0x88, 0xd9, 0x7a, 0x23, 0xc9, 0xbd, 0x11, 0x3b, 0x64, 0x24, 0xab,
  60229. 0x5b, 0x45, 0x33, 0xf6, 0x2c, 0x34, 0xe4, 0xcf, 0xf4, 0x78, 0xc8, 0x62,
  60230. 0x06, 0xc7, 0xe5, 0x30, 0x39, 0xbf, 0xa1, 0x20, 0xa3, 0x06, 0x74, 0xc3,
  60231. 0xa9, 0x74, 0x52, 0x8a, 0xfb, 0xae, 0xf0, 0xd8, 0x6f, 0xb2, 0x9d, 0xfe,
  60232. 0x78, 0xf0, 0x3f, 0x51, 0x8f, 0x9c, 0xcf, 0xbe, 0x61, 0x43, 0x9d, 0xf8,
  60233. 0x85, 0xe5, 0x2f, 0x54,
  60234. 0xc0, 0x2f, /* ECDHE-RSA-AES128-GCM-SHA256 */
  60235. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  60236. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  60237. };
  60238. static int test_wrong_cs_downgrade(void)
  60239. {
  60240. EXPECT_DECLS;
  60241. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  60242. struct test_memio_ctx test_ctx;
  60243. WOLFSSL_CTX *ctx_c = NULL;
  60244. WOLFSSL *ssl_c = NULL;
  60245. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60246. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60247. wolfSSLv23_client_method, NULL), 0);
  60248. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_CS_DOWNGRADE_CLIENT),
  60249. WOLFSSL_SUCCESS);
  60250. /* CH */
  60251. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60252. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60253. WOLFSSL_ERROR_WANT_READ);
  60254. /* consume CH */
  60255. test_ctx.s_len = 0;
  60256. /* inject SH */
  60257. XMEMCPY(test_ctx.c_buff, test_wrong_cs_downgrade_sh,
  60258. sizeof(test_wrong_cs_downgrade_sh));
  60259. test_ctx.c_len = sizeof(test_wrong_cs_downgrade_sh);
  60260. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60261. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60262. MATCH_SUITE_ERROR);
  60263. wolfSSL_free(ssl_c);
  60264. wolfSSL_CTX_free(ctx_c);
  60265. #endif
  60266. return EXPECT_RESULT();
  60267. }
  60268. #else
  60269. static int test_wrong_cs_downgrade(void)
  60270. {
  60271. return TEST_SKIPPED;
  60272. }
  60273. #endif
  60274. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  60275. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  60276. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  60277. {
  60278. tail_len -= msg_length;
  60279. XMEMMOVE(msg, msg + msg_length, tail_len);
  60280. *len = *len - msg_length;
  60281. }
  60282. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  60283. byte *found)
  60284. {
  60285. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  60286. const unsigned int _RECORD_HEADER_SZ = 5;
  60287. const int _change_cipher_hs = 55;
  60288. const int _change_cipher = 20;
  60289. const int _handshake = 22;
  60290. unsigned int tail_len;
  60291. byte *idx, *curr;
  60292. word8 currType;
  60293. word16 rLength;
  60294. word32 hLength;
  60295. idx = buf;
  60296. tail_len = (unsigned int)*len;
  60297. *found = 0;
  60298. while (tail_len > _RECORD_HEADER_SZ) {
  60299. curr = idx;
  60300. currType = *idx;
  60301. ato16(idx + 3, &rLength);
  60302. idx += _RECORD_HEADER_SZ;
  60303. tail_len -= _RECORD_HEADER_SZ;
  60304. if (tail_len < rLength)
  60305. return -1;
  60306. if (type == _change_cipher_hs && currType == _change_cipher) {
  60307. if (rLength != 1)
  60308. return -1;
  60309. /* match */
  60310. test_remove_msg(curr, *len - (int)(curr - buf),
  60311. len, _RECORD_HEADER_SZ + 1);
  60312. *found = 1;
  60313. return 0;
  60314. }
  60315. if (currType != _handshake) {
  60316. idx += rLength;
  60317. tail_len -= rLength;
  60318. continue;
  60319. }
  60320. if (rLength < _HANDSHAKE_HEADER_SZ)
  60321. return -1;
  60322. currType = *idx;
  60323. ato24(idx+1, &hLength);
  60324. hLength += _HANDSHAKE_HEADER_SZ;
  60325. if (tail_len < hLength)
  60326. return -1;
  60327. if (currType != type) {
  60328. idx += hLength;
  60329. tail_len -= hLength;
  60330. continue;
  60331. }
  60332. /* match */
  60333. test_remove_msg(curr, *len - (int)(curr - buf), len,
  60334. hLength + _RECORD_HEADER_SZ);
  60335. *found = 1;
  60336. return 0;
  60337. }
  60338. /* not found */
  60339. return 0;
  60340. }
  60341. static int test_remove_hs_message(byte hs_message_type,
  60342. int extra_round, byte alert_type)
  60343. {
  60344. EXPECT_DECLS;
  60345. WOLFSSL_CTX *ctx_c = NULL;
  60346. WOLFSSL_CTX *ctx_s = NULL;
  60347. WOLFSSL *ssl_c = NULL;
  60348. WOLFSSL *ssl_s = NULL;
  60349. struct test_memio_ctx test_ctx;
  60350. WOLFSSL_ALERT_HISTORY h;
  60351. byte found = 0;
  60352. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60353. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60354. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  60355. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60356. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60357. WOLFSSL_ERROR_WANT_READ);
  60358. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60359. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60360. WOLFSSL_ERROR_WANT_READ);
  60361. if (extra_round) {
  60362. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60363. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60364. WOLFSSL_ERROR_WANT_READ);
  60365. /* this will complete handshake from server side */
  60366. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60367. }
  60368. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  60369. &test_ctx.c_len, hs_message_type, &found), 0);
  60370. if (!found) {
  60371. wolfSSL_free(ssl_c);
  60372. wolfSSL_CTX_free(ctx_c);
  60373. wolfSSL_free(ssl_s);
  60374. wolfSSL_CTX_free(ctx_s);
  60375. return TEST_SKIPPED;
  60376. }
  60377. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60378. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60379. WOLFSSL_ERROR_WANT_READ);
  60380. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  60381. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  60382. ExpectIntEQ(h.last_tx.level, alert_fatal);
  60383. wolfSSL_free(ssl_c);
  60384. wolfSSL_CTX_free(ctx_c);
  60385. wolfSSL_free(ssl_s);
  60386. wolfSSL_CTX_free(ctx_s);
  60387. return EXPECT_RESULT();
  60388. }
  60389. static int test_extra_alerts_skip_hs(void)
  60390. {
  60391. EXPECT_DECLS;
  60392. const byte _server_key_exchange = 12;
  60393. const byte _server_hello = 2;
  60394. const byte _certificate = 11;
  60395. /* server_hello */
  60396. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  60397. unexpected_message), TEST_FAIL);
  60398. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  60399. 0xff), TEST_FAIL);
  60400. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  60401. unexpected_message), TEST_FAIL);
  60402. return EXPECT_RESULT();
  60403. }
  60404. #else
  60405. static int test_extra_alerts_skip_hs(void)
  60406. {
  60407. return TEST_SKIPPED;
  60408. }
  60409. #endif
  60410. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  60411. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  60412. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  60413. unsigned char* key, unsigned int key_max_len)
  60414. {
  60415. (void)ssl;
  60416. (void)id;
  60417. (void)key_max_len;
  60418. /* zero means error */
  60419. key[0] = 0x10;
  60420. return 1;
  60421. }
  60422. static int test_extra_alerts_bad_psk(void)
  60423. {
  60424. EXPECT_DECLS;
  60425. WOLFSSL_CTX *ctx_c = NULL;
  60426. WOLFSSL_CTX *ctx_s = NULL;
  60427. WOLFSSL *ssl_c = NULL;
  60428. WOLFSSL *ssl_s = NULL;
  60429. struct test_memio_ctx test_ctx;
  60430. WOLFSSL_ALERT_HISTORY h;
  60431. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60432. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60433. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  60434. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  60435. WOLFSSL_SUCCESS);
  60436. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  60437. WOLFSSL_SUCCESS);
  60438. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  60439. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60440. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60441. WOLFSSL_ERROR_WANT_READ);
  60442. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60443. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60444. WOLFSSL_ERROR_WANT_READ);
  60445. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60446. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60447. WOLFSSL_ERROR_WANT_READ);
  60448. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  60449. ExpectIntEQ(h.last_tx.code, handshake_failure);
  60450. ExpectIntEQ(h.last_tx.level, alert_fatal);
  60451. wolfSSL_free(ssl_c);
  60452. wolfSSL_CTX_free(ctx_c);
  60453. wolfSSL_free(ssl_s);
  60454. wolfSSL_CTX_free(ctx_s);
  60455. return EXPECT_RESULT();
  60456. }
  60457. #else
  60458. static int test_extra_alerts_bad_psk(void)
  60459. {
  60460. return TEST_SKIPPED;
  60461. }
  60462. #endif
  60463. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  60464. && !defined(NO_PSK)
  60465. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  60466. const char* hint, char* identity, unsigned int id_max_len,
  60467. unsigned char* key, unsigned int key_max_len)
  60468. {
  60469. (void)ssl;
  60470. (void)hint;
  60471. (void)key_max_len;
  60472. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  60473. XSTRNCPY(identity, "Client_identity", id_max_len);
  60474. key[0] = 0x20;
  60475. return 1;
  60476. }
  60477. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  60478. const char* id, unsigned char* key, unsigned int key_max_len)
  60479. {
  60480. (void)ssl;
  60481. (void)id;
  60482. (void)key_max_len;
  60483. /* zero means error */
  60484. key[0] = 0x10;
  60485. return 1;
  60486. }
  60487. #endif
  60488. static int test_tls13_bad_psk_binder(void)
  60489. {
  60490. EXPECT_DECLS;
  60491. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  60492. && !defined(NO_PSK)
  60493. WOLFSSL_CTX *ctx_c = NULL;
  60494. WOLFSSL_CTX *ctx_s = NULL;
  60495. WOLFSSL *ssl_c = NULL;
  60496. WOLFSSL *ssl_s = NULL;
  60497. struct test_memio_ctx test_ctx;
  60498. WOLFSSL_ALERT_HISTORY h;
  60499. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60500. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60501. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  60502. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  60503. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  60504. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60505. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60506. WOLFSSL_ERROR_WANT_READ);
  60507. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60508. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60509. BAD_BINDER);
  60510. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60511. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60512. FATAL_ERROR);
  60513. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  60514. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  60515. ExpectIntEQ(h.last_rx.level, alert_fatal);
  60516. wolfSSL_free(ssl_c);
  60517. wolfSSL_CTX_free(ctx_c);
  60518. wolfSSL_free(ssl_s);
  60519. wolfSSL_CTX_free(ctx_s);
  60520. #endif
  60521. return EXPECT_RESULT();
  60522. }
  60523. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  60524. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60525. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  60526. int sz, void *ctx)
  60527. {
  60528. static int sentServerHello = FALSE;
  60529. if (!sentServerHello) {
  60530. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  60531. size_t i;
  60532. if (sz < (int)sizeof(renegExt))
  60533. return WOLFSSL_CBIO_ERR_GENERAL;
  60534. /* Remove SCR from ServerHello */
  60535. for (i = 0; i < sz - sizeof(renegExt); i++) {
  60536. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  60537. /* Found the extension. Change it to something unrecognized. */
  60538. buf[i+1] = 0x11;
  60539. break;
  60540. }
  60541. }
  60542. sentServerHello = TRUE;
  60543. }
  60544. return EmbedSend(ssl, buf, sz, ctx);
  60545. }
  60546. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  60547. {
  60548. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  60549. }
  60550. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  60551. {
  60552. WOLFSSL_ALERT_HISTORY h;
  60553. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  60554. AssertIntEQ(h.last_rx.code, handshake_failure);
  60555. AssertIntEQ(h.last_rx.level, alert_fatal);
  60556. }
  60557. static int test_harden_no_secure_renegotiation(void)
  60558. {
  60559. EXPECT_DECLS;
  60560. callback_functions client_cbs, server_cbs;
  60561. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60562. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60563. client_cbs.method = wolfTLSv1_2_client_method;
  60564. server_cbs.method = wolfTLSv1_2_server_method;
  60565. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  60566. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  60567. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  60568. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  60569. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  60570. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  60571. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  60572. server_cbs.last_err == FATAL_ERROR);
  60573. return EXPECT_RESULT();
  60574. }
  60575. #else
  60576. static int test_harden_no_secure_renegotiation(void)
  60577. {
  60578. return TEST_SKIPPED;
  60579. }
  60580. #endif
  60581. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60582. static int test_override_alt_cert_chain_cert_cb(int preverify,
  60583. WOLFSSL_X509_STORE_CTX* store)
  60584. {
  60585. fprintf(stderr, "preverify: %d\n", preverify);
  60586. fprintf(stderr, "store->error: %d\n", store->error);
  60587. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  60588. if (store->error == OCSP_INVALID_STATUS) {
  60589. fprintf(stderr, "Overriding OCSP error\n");
  60590. return 1;
  60591. }
  60592. #ifndef WOLFSSL_ALT_CERT_CHAINS
  60593. else if ((store->error == ASN_NO_SIGNER_E ||
  60594. store->error == ASN_SELF_SIGNED_E
  60595. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  60596. defined(HAVE_WEBSERVER)
  60597. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  60598. #endif
  60599. ) && store->error_depth == store->totalCerts - 1) {
  60600. fprintf(stderr, "Overriding no signer error only for root cert\n");
  60601. return 1;
  60602. }
  60603. #endif
  60604. else
  60605. return preverify;
  60606. }
  60607. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  60608. int urlSz, unsigned char* request, int requestSz,
  60609. unsigned char** response)
  60610. {
  60611. (void)ioCtx;
  60612. (void)url;
  60613. (void)urlSz;
  60614. (void)request;
  60615. (void)requestSz;
  60616. (void)response;
  60617. return -1;
  60618. }
  60619. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  60620. {
  60621. EXPECT_DECLS;
  60622. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  60623. test_override_alt_cert_chain_cert_cb);
  60624. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  60625. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  60626. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  60627. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  60628. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  60629. WOLFSSL_SUCCESS);
  60630. return EXPECT_RESULT();
  60631. }
  60632. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  60633. {
  60634. EXPECT_DECLS;
  60635. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  60636. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  60637. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  60638. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  60639. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  60640. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  60641. WOLFSSL_SUCCESS);
  60642. return EXPECT_RESULT();
  60643. }
  60644. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  60645. {
  60646. EXPECT_DECLS;
  60647. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  60648. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  60649. return EXPECT_RESULT();
  60650. }
  60651. static int test_override_alt_cert_chain(void)
  60652. {
  60653. EXPECT_DECLS;
  60654. size_t i;
  60655. struct test_params {
  60656. ctx_cb client_ctx_cb;
  60657. ctx_cb server_ctx_cb;
  60658. int result;
  60659. } params[] = {
  60660. {test_override_alt_cert_chain_client_ctx_ready,
  60661. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  60662. {test_override_alt_cert_chain_client_ctx_ready2,
  60663. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  60664. };
  60665. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  60666. test_ssl_cbf client_cbs, server_cbs;
  60667. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60668. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60669. fprintf(stderr, "test config: %d\n", (int)i);
  60670. client_cbs.ctx_ready = params[i].client_ctx_cb;
  60671. server_cbs.ctx_ready = params[i].server_ctx_cb;
  60672. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60673. &server_cbs, NULL), params[i].result);
  60674. ExpectIntEQ(client_cbs.return_code, params[i].result);
  60675. ExpectIntEQ(server_cbs.return_code, params[i].result);
  60676. }
  60677. return EXPECT_RESULT();
  60678. }
  60679. #else
  60680. static int test_override_alt_cert_chain(void)
  60681. {
  60682. return TEST_SKIPPED;
  60683. }
  60684. #endif
  60685. #if defined(HAVE_RPK)
  60686. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  60687. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  60688. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60689. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  60690. {
  60691. int ret = WOLFSSL_SUCCESS;
  60692. (void)mode;
  60693. (void)strctx;
  60694. WOLFSSL_ENTER("MyRpkVerifyCb");
  60695. return ret;
  60696. }
  60697. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  60698. static WC_INLINE int test_rpk_memio_setup(
  60699. struct test_memio_ctx *ctx,
  60700. WOLFSSL_CTX **ctx_c,
  60701. WOLFSSL_CTX **ctx_s,
  60702. WOLFSSL **ssl_c,
  60703. WOLFSSL **ssl_s,
  60704. method_provider method_c,
  60705. method_provider method_s,
  60706. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  60707. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  60708. const char* pkey_c, int fmt_kc, /* client private key and format */
  60709. const char* pkey_s, int fmt_ks /* server private key and format */
  60710. )
  60711. {
  60712. int ret;
  60713. if (ctx_c != NULL && *ctx_c == NULL) {
  60714. *ctx_c = wolfSSL_CTX_new(method_c());
  60715. if (*ctx_c == NULL) {
  60716. return -1;
  60717. }
  60718. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  60719. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  60720. if (ret != WOLFSSL_SUCCESS) {
  60721. return -1;
  60722. }
  60723. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  60724. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  60725. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  60726. if (ret != WOLFSSL_SUCCESS) {
  60727. return -1;
  60728. }
  60729. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  60730. if (ret != WOLFSSL_SUCCESS) {
  60731. return -1;
  60732. }
  60733. }
  60734. if (ctx_s != NULL && *ctx_s == NULL) {
  60735. *ctx_s = wolfSSL_CTX_new(method_s());
  60736. if (*ctx_s == NULL) {
  60737. return -1;
  60738. }
  60739. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  60740. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  60741. if (ret != WOLFSSL_SUCCESS) {
  60742. return -1;
  60743. }
  60744. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  60745. if (ret != WOLFSSL_SUCCESS) {
  60746. return -1;
  60747. }
  60748. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  60749. if (ret != WOLFSSL_SUCCESS) {
  60750. return -1;
  60751. }
  60752. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  60753. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  60754. if (ctx->s_ciphers != NULL) {
  60755. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  60756. if (ret != WOLFSSL_SUCCESS) {
  60757. return -1;
  60758. }
  60759. }
  60760. }
  60761. if (ctx_c != NULL && ssl_c != NULL) {
  60762. *ssl_c = wolfSSL_new(*ctx_c);
  60763. if (*ssl_c == NULL) {
  60764. return -1;
  60765. }
  60766. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  60767. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  60768. }
  60769. if (ctx_s != NULL && ssl_s != NULL) {
  60770. *ssl_s = wolfSSL_new(*ctx_s);
  60771. if (*ssl_s == NULL) {
  60772. return -1;
  60773. }
  60774. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  60775. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  60776. #if !defined(NO_DH)
  60777. SetDH(*ssl_s);
  60778. #endif
  60779. }
  60780. return 0;
  60781. }
  60782. #endif /* HAVE_RPK */
  60783. static int test_rpk_set_xxx_cert_type(void)
  60784. {
  60785. EXPECT_DECLS;
  60786. #if defined(HAVE_RPK)
  60787. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  60788. WOLFSSL_CTX* ctx = NULL;
  60789. WOLFSSL* ssl = NULL;
  60790. int tp;
  60791. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  60792. ExpectNotNull(ctx);
  60793. ssl = wolfSSL_new(ctx);
  60794. ExpectNotNull(ssl);
  60795. /*--------------------------------------------*/
  60796. /* tests for wolfSSL_CTX_set_client_cert_type */
  60797. /*--------------------------------------------*/
  60798. /* illegal parameter test caces */
  60799. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  60800. MAX_CLIENT_CERT_TYPE_CNT),
  60801. BAD_FUNC_ARG);
  60802. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60803. sizeof(ctype)),
  60804. BAD_FUNC_ARG);
  60805. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60806. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60807. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60808. MAX_CLIENT_CERT_TYPE_CNT),
  60809. BAD_FUNC_ARG);
  60810. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60811. ctype[1] = 10; /* set unknown cert type */
  60812. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60813. MAX_CLIENT_CERT_TYPE_CNT),
  60814. BAD_FUNC_ARG);
  60815. /* pass larger type count */
  60816. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60817. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60818. ctype[2] = 1; /* pass unacceptable type count */
  60819. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60820. MAX_CLIENT_CERT_TYPE_CNT + 1),
  60821. BAD_FUNC_ARG);
  60822. /* should accept NULL for type buffer */
  60823. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  60824. MAX_CLIENT_CERT_TYPE_CNT),
  60825. WOLFSSL_SUCCESS);
  60826. /* should accept zero for type count */
  60827. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60828. 0),
  60829. WOLFSSL_SUCCESS);
  60830. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60831. MAX_CLIENT_CERT_TYPE_CNT),
  60832. WOLFSSL_SUCCESS);
  60833. /*--------------------------------------------*/
  60834. /* tests for wolfSSL_CTX_set_server_cert_type */
  60835. /*--------------------------------------------*/
  60836. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  60837. MAX_SERVER_CERT_TYPE_CNT),
  60838. BAD_FUNC_ARG);
  60839. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60840. sizeof(ctype)),
  60841. BAD_FUNC_ARG);
  60842. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60843. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60844. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60845. MAX_SERVER_CERT_TYPE_CNT),
  60846. BAD_FUNC_ARG);
  60847. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60848. ctype[1] = 10; /* set unknown cert type */
  60849. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60850. MAX_SERVER_CERT_TYPE_CNT),
  60851. BAD_FUNC_ARG);
  60852. /* pass larger type count */
  60853. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60854. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60855. ctype[2] = 1; /* pass unacceptable type count */
  60856. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60857. MAX_SERVER_CERT_TYPE_CNT + 1),
  60858. BAD_FUNC_ARG);
  60859. /* should accept NULL for type buffer */
  60860. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  60861. MAX_SERVER_CERT_TYPE_CNT),
  60862. WOLFSSL_SUCCESS);
  60863. /* should accept zero for type count */
  60864. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60865. 0),
  60866. WOLFSSL_SUCCESS);
  60867. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60868. MAX_CLIENT_CERT_TYPE_CNT),
  60869. WOLFSSL_SUCCESS);
  60870. /*--------------------------------------------*/
  60871. /* tests for wolfSSL_set_client_cert_type */
  60872. /*--------------------------------------------*/
  60873. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  60874. MAX_CLIENT_CERT_TYPE_CNT),
  60875. BAD_FUNC_ARG);
  60876. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60877. sizeof(ctype)),
  60878. BAD_FUNC_ARG);
  60879. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60880. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60881. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60882. MAX_CLIENT_CERT_TYPE_CNT),
  60883. BAD_FUNC_ARG);
  60884. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60885. ctype[1] = 10; /* set unknown cert type */
  60886. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60887. MAX_CLIENT_CERT_TYPE_CNT),
  60888. BAD_FUNC_ARG);
  60889. /* pass larger type count */
  60890. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60891. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60892. ctype[2] = 1; /* pass unacceptable type count */
  60893. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60894. MAX_CLIENT_CERT_TYPE_CNT + 1),
  60895. BAD_FUNC_ARG);
  60896. /* should accept NULL for type buffer */
  60897. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  60898. MAX_CLIENT_CERT_TYPE_CNT),
  60899. WOLFSSL_SUCCESS);
  60900. /* should accept zero for type count */
  60901. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60902. 0),
  60903. WOLFSSL_SUCCESS);
  60904. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60905. MAX_CLIENT_CERT_TYPE_CNT),
  60906. WOLFSSL_SUCCESS);
  60907. /*--------------------------------------------*/
  60908. /* tests for wolfSSL_CTX_set_server_cert_type */
  60909. /*--------------------------------------------*/
  60910. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  60911. MAX_SERVER_CERT_TYPE_CNT),
  60912. BAD_FUNC_ARG);
  60913. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60914. sizeof(ctype)),
  60915. BAD_FUNC_ARG);
  60916. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60917. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60918. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60919. MAX_SERVER_CERT_TYPE_CNT),
  60920. BAD_FUNC_ARG);
  60921. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60922. ctype[1] = 10; /* set unknown cert type */
  60923. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60924. MAX_SERVER_CERT_TYPE_CNT),
  60925. BAD_FUNC_ARG);
  60926. /* pass larger type count */
  60927. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60928. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60929. ctype[2] = 1; /* pass unacceptable type count */
  60930. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60931. MAX_SERVER_CERT_TYPE_CNT + 1),
  60932. BAD_FUNC_ARG);
  60933. /* should accept NULL for type buffer */
  60934. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  60935. MAX_SERVER_CERT_TYPE_CNT),
  60936. WOLFSSL_SUCCESS);
  60937. /* should accept zero for type count */
  60938. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60939. 0),
  60940. WOLFSSL_SUCCESS);
  60941. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60942. MAX_SERVER_CERT_TYPE_CNT),
  60943. WOLFSSL_SUCCESS);
  60944. /*------------------------------------------------*/
  60945. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  60946. /*------------------------------------------------*/
  60947. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  60948. BAD_FUNC_ARG);
  60949. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  60950. BAD_FUNC_ARG);
  60951. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  60952. BAD_FUNC_ARG);
  60953. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  60954. BAD_FUNC_ARG);
  60955. /* clean up */
  60956. wolfSSL_free(ssl);
  60957. wolfSSL_CTX_free(ctx);
  60958. #endif
  60959. return EXPECT_RESULT();
  60960. }
  60961. static int test_tls13_rpk_handshake(void)
  60962. {
  60963. EXPECT_DECLS;
  60964. #if defined(HAVE_RPK)
  60965. int ret = 0;
  60966. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60967. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60968. struct test_memio_ctx test_ctx;
  60969. int err;
  60970. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  60971. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  60972. int typeCnt_c;
  60973. int typeCnt_s;
  60974. int tp;
  60975. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60976. int isServer;
  60977. #endif
  60978. (void)err;
  60979. (void)typeCnt_c;
  60980. (void)typeCnt_s;
  60981. (void)certType_c;
  60982. (void)certType_s;
  60983. /* TLS1.2
  60984. * Both client and server load x509 cert and start handshaking.
  60985. * Check no negotiation occurred.
  60986. */
  60987. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60988. ExpectIntEQ(
  60989. test_rpk_memio_setup(
  60990. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60991. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60992. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60993. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60994. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60995. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  60996. , 0);
  60997. /* set client certificate type in client end */
  60998. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60999. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61000. typeCnt_c = 2;
  61001. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61002. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61003. typeCnt_s = 2;
  61004. /* both client and server do not call client/server_cert_type APIs,
  61005. * expecting default settings works and no negotiation performed.
  61006. */
  61007. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61008. return TEST_FAIL;
  61009. /* confirm no negotiation occurred */
  61010. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61011. WOLFSSL_SUCCESS);
  61012. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61013. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61014. WOLFSSL_SUCCESS);
  61015. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61016. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61017. WOLFSSL_SUCCESS);
  61018. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61019. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61020. WOLFSSL_SUCCESS);
  61021. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61022. (void)typeCnt_c;
  61023. (void)typeCnt_s;
  61024. wolfSSL_free(ssl_c);
  61025. wolfSSL_CTX_free(ctx_c);
  61026. wolfSSL_free(ssl_s);
  61027. wolfSSL_CTX_free(ctx_s);
  61028. ssl_c = ssl_s = NULL;
  61029. ctx_c = ctx_s = NULL;
  61030. /* Both client and server load x509 cert and start handshaking.
  61031. * Check no negotiation occurred.
  61032. */
  61033. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61034. ExpectIntEQ(
  61035. test_rpk_memio_setup(
  61036. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61037. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61038. cliCertFile, WOLFSSL_FILETYPE_PEM,
  61039. svrCertFile, WOLFSSL_FILETYPE_PEM,
  61040. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61041. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61042. , 0);
  61043. /* set client certificate type in client end */
  61044. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61045. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61046. typeCnt_c = 2;
  61047. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61048. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61049. typeCnt_s = 2;
  61050. /* both client and server do not call client/server_cert_type APIs,
  61051. * expecting default settings works and no negotiation performed.
  61052. */
  61053. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61054. return TEST_FAIL;
  61055. /* confirm no negotiation occurred */
  61056. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61057. WOLFSSL_SUCCESS);
  61058. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61059. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61060. WOLFSSL_SUCCESS);
  61061. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61062. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61063. WOLFSSL_SUCCESS);
  61064. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61065. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61066. WOLFSSL_SUCCESS);
  61067. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61068. (void)typeCnt_c;
  61069. (void)typeCnt_s;
  61070. wolfSSL_free(ssl_c);
  61071. wolfSSL_CTX_free(ctx_c);
  61072. wolfSSL_free(ssl_s);
  61073. wolfSSL_CTX_free(ctx_s);
  61074. ssl_c = ssl_s = NULL;
  61075. ctx_c = ctx_s = NULL;
  61076. /* Both client and server load RPK cert and start handshaking.
  61077. * Confirm negotiated cert types match as expected.
  61078. */
  61079. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61080. ExpectIntEQ(
  61081. test_rpk_memio_setup(
  61082. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61083. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61084. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61085. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61086. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61087. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61088. , 0);
  61089. /* set client certificate type in client end */
  61090. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61091. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61092. typeCnt_c = 2;
  61093. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61094. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61095. typeCnt_s = 2;
  61096. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61097. WOLFSSL_SUCCESS);
  61098. /* set server certificate type in client end */
  61099. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61100. WOLFSSL_SUCCESS);
  61101. /* set client certificate type in server end */
  61102. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61103. WOLFSSL_SUCCESS);
  61104. /* set server certificate type in server end */
  61105. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61106. WOLFSSL_SUCCESS);
  61107. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61108. return TEST_FAIL;
  61109. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61110. WOLFSSL_SUCCESS);
  61111. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61112. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61113. WOLFSSL_SUCCESS);
  61114. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61115. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61116. WOLFSSL_SUCCESS);
  61117. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61118. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61119. WOLFSSL_SUCCESS);
  61120. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61121. wolfSSL_free(ssl_c);
  61122. wolfSSL_CTX_free(ctx_c);
  61123. wolfSSL_free(ssl_s);
  61124. wolfSSL_CTX_free(ctx_s);
  61125. ssl_c = ssl_s = NULL;
  61126. ctx_c = ctx_s = NULL;
  61127. /* TLS1.2
  61128. * Both client and server load RPK cert and start handshaking.
  61129. * Confirm negotiated cert types match as expected.
  61130. */
  61131. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61132. ExpectIntEQ(
  61133. test_rpk_memio_setup(
  61134. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61135. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61136. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61137. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61138. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61139. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61140. , 0);
  61141. /* set client certificate type in client end */
  61142. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61143. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61144. typeCnt_c = 2;
  61145. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61146. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61147. typeCnt_s = 2;
  61148. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61149. WOLFSSL_SUCCESS);
  61150. /* set server certificate type in client end */
  61151. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61152. WOLFSSL_SUCCESS);
  61153. /* set client certificate type in server end */
  61154. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61155. WOLFSSL_SUCCESS);
  61156. /* set server certificate type in server end */
  61157. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61158. WOLFSSL_SUCCESS);
  61159. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61160. return TEST_FAIL;
  61161. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61162. WOLFSSL_SUCCESS);
  61163. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61164. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61165. WOLFSSL_SUCCESS);
  61166. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61167. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61168. WOLFSSL_SUCCESS);
  61169. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61170. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61171. WOLFSSL_SUCCESS);
  61172. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61173. wolfSSL_free(ssl_c);
  61174. wolfSSL_CTX_free(ctx_c);
  61175. wolfSSL_free(ssl_s);
  61176. wolfSSL_CTX_free(ctx_s);
  61177. ssl_c = ssl_s = NULL;
  61178. ctx_c = ctx_s = NULL;
  61179. /* Both client and server load x509 cert.
  61180. * Have client call set_client_cert_type with both RPK and x509.
  61181. * This doesn't makes client add client cert type extension to ClientHello,
  61182. * since it does not load RPK cert actually.
  61183. */
  61184. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61185. ExpectIntEQ(
  61186. test_rpk_memio_setup(
  61187. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61188. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61189. cliCertFile, WOLFSSL_FILETYPE_PEM,
  61190. svrCertFile, WOLFSSL_FILETYPE_PEM,
  61191. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61192. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61193. , 0);
  61194. /* set client certificate type in client end
  61195. *
  61196. * client indicates both RPK and x509 certs are available but loaded RPK
  61197. * cert only. It does not have client add client-cert-type extension in CH.
  61198. */
  61199. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61200. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61201. typeCnt_c = 2;
  61202. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61203. WOLFSSL_SUCCESS);
  61204. /* client indicates both RPK and x509 certs are acceptable */
  61205. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61206. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61207. typeCnt_s = 2;
  61208. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61209. WOLFSSL_SUCCESS);
  61210. /* server indicates both RPK and x509 certs are acceptable */
  61211. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61212. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61213. typeCnt_c = 2;
  61214. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61215. WOLFSSL_SUCCESS);
  61216. /* server should indicate only RPK cert is available */
  61217. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61218. certType_s[1] = -1;
  61219. typeCnt_s = 1;
  61220. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61221. WOLFSSL_SUCCESS);
  61222. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61223. return TEST_FAIL;
  61224. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  61225. * be returned as cert type.
  61226. */
  61227. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61228. WOLFSSL_SUCCESS);
  61229. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61230. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61231. WOLFSSL_SUCCESS);
  61232. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61233. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61234. WOLFSSL_SUCCESS);
  61235. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61236. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61237. WOLFSSL_SUCCESS);
  61238. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61239. wolfSSL_free(ssl_c);
  61240. wolfSSL_CTX_free(ctx_c);
  61241. wolfSSL_free(ssl_s);
  61242. wolfSSL_CTX_free(ctx_s);
  61243. ssl_c = ssl_s = NULL;
  61244. ctx_c = ctx_s = NULL;
  61245. /* Have client load RPK cert and have server load x509 cert.
  61246. * Check the negotiation result from both ends.
  61247. */
  61248. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61249. ExpectIntEQ(
  61250. test_rpk_memio_setup(
  61251. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61252. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61253. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61254. svrCertFile, WOLFSSL_FILETYPE_PEM,
  61255. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61256. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61257. , 0);
  61258. /* have client tell to use RPK cert */
  61259. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61260. certType_c[1] = -1;
  61261. typeCnt_c = 1;
  61262. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61263. WOLFSSL_SUCCESS);
  61264. /* have client tell to accept both RPK and x509 cert */
  61265. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61266. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  61267. typeCnt_s = 2;
  61268. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61269. WOLFSSL_SUCCESS);
  61270. /* have server accept to both RPK and x509 cert */
  61271. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  61272. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  61273. typeCnt_c = 2;
  61274. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61275. WOLFSSL_SUCCESS);
  61276. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  61277. * end, expecting the default setting works.
  61278. */
  61279. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61280. return TEST_FAIL;
  61281. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61282. WOLFSSL_SUCCESS);
  61283. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61284. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61285. WOLFSSL_SUCCESS);
  61286. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61287. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61288. WOLFSSL_SUCCESS);
  61289. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61290. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61291. WOLFSSL_SUCCESS);
  61292. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61293. wolfSSL_free(ssl_c);
  61294. wolfSSL_CTX_free(ctx_c);
  61295. wolfSSL_free(ssl_s);
  61296. wolfSSL_CTX_free(ctx_s);
  61297. ssl_c = ssl_s = NULL;
  61298. ctx_c = ctx_s = NULL;
  61299. /* Have both client and server load RPK cert, however, have server
  61300. * indicate its cert type x509.
  61301. * Client is expected to detect the cert type mismatch then to send alert
  61302. * with "unsupported_certificate".
  61303. */
  61304. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61305. ExpectIntEQ(
  61306. test_rpk_memio_setup(
  61307. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61308. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61309. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61310. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  61311. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61312. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61313. , 0);
  61314. /* have client tell to use RPK cert */
  61315. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61316. certType_c[1] = -1;
  61317. typeCnt_c = 1;
  61318. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61319. WOLFSSL_SUCCESS);
  61320. /* have client tell to accept both RPK and x509 cert */
  61321. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61322. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  61323. typeCnt_s = 2;
  61324. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61325. WOLFSSL_SUCCESS);
  61326. /* have server accept to both RPK and x509 cert */
  61327. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  61328. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  61329. typeCnt_c = 2;
  61330. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61331. WOLFSSL_SUCCESS);
  61332. /* have server tell to use x509 cert intentionally. This will bring
  61333. * certificate type mismatch in client side.
  61334. */
  61335. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61336. certType_s[1] = -1;
  61337. typeCnt_s = 1;
  61338. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61339. WOLFSSL_SUCCESS);
  61340. /* expect client detect cert type mismatch then send Alert */
  61341. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  61342. if (ret != -1)
  61343. return TEST_FAIL;
  61344. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  61345. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61346. WOLFSSL_SUCCESS);
  61347. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61348. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61349. WOLFSSL_SUCCESS);
  61350. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61351. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61352. WOLFSSL_SUCCESS);
  61353. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61354. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61355. WOLFSSL_SUCCESS);
  61356. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61357. wolfSSL_free(ssl_c);
  61358. wolfSSL_CTX_free(ctx_c);
  61359. wolfSSL_free(ssl_s);
  61360. wolfSSL_CTX_free(ctx_s);
  61361. ssl_c = ssl_s = NULL;
  61362. ctx_c = ctx_s = NULL;
  61363. /* Have client load x509 cert and server load RPK cert,
  61364. * however, have client indicate its cert type RPK.
  61365. * Server is expected to detect the cert type mismatch then to send alert
  61366. * with "unsupported_certificate".
  61367. */
  61368. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61369. ExpectIntEQ(
  61370. test_rpk_memio_setup(
  61371. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61372. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61373. cliCertFile, WOLFSSL_FILETYPE_PEM,
  61374. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61375. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61376. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61377. , 0);
  61378. /* have client tell to use RPK cert intentionally */
  61379. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61380. certType_c[1] = -1;
  61381. typeCnt_c = 1;
  61382. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61383. WOLFSSL_SUCCESS);
  61384. /* have client tell to accept both RPK and x509 cert */
  61385. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61386. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  61387. typeCnt_s = 2;
  61388. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61389. WOLFSSL_SUCCESS);
  61390. /* have server accept to both RPK and x509 cert */
  61391. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  61392. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  61393. typeCnt_c = 2;
  61394. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61395. WOLFSSL_SUCCESS);
  61396. /* have server tell to use x509 cert intentionally. This will bring
  61397. * certificate type mismatch in client side.
  61398. */
  61399. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61400. certType_s[1] = -1;
  61401. typeCnt_s = 1;
  61402. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61403. WOLFSSL_SUCCESS);
  61404. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  61405. /* expect server detect cert type mismatch then send Alert */
  61406. ExpectIntNE(ret, 0);
  61407. err = wolfSSL_get_error(ssl_c, ret);
  61408. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  61409. /* client did not load RPK cert actually, so negotiation did not happen */
  61410. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61411. WOLFSSL_SUCCESS);
  61412. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61413. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61414. WOLFSSL_SUCCESS);
  61415. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61416. /* client did not load RPK cert actually, so negotiation did not happen */
  61417. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61418. WOLFSSL_SUCCESS);
  61419. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61420. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61421. WOLFSSL_SUCCESS);
  61422. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61423. wolfSSL_free(ssl_c);
  61424. wolfSSL_CTX_free(ctx_c);
  61425. wolfSSL_free(ssl_s);
  61426. wolfSSL_CTX_free(ctx_s);
  61427. ssl_c = ssl_s = NULL;
  61428. ctx_c = ctx_s = NULL;
  61429. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  61430. /* Both client and server load RPK cert and set certificate verify
  61431. * callbacks then start handshaking.
  61432. * Confirm both side can refer the peer's cert.
  61433. */
  61434. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61435. ExpectIntEQ(
  61436. test_rpk_memio_setup(
  61437. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61438. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61439. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61440. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61441. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61442. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61443. , 0);
  61444. /* set client certificate type in client end */
  61445. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61446. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61447. typeCnt_c = 2;
  61448. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61449. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61450. typeCnt_s = 2;
  61451. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61452. WOLFSSL_SUCCESS);
  61453. /* set server certificate type in client end */
  61454. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61455. WOLFSSL_SUCCESS);
  61456. /* set client certificate type in server end */
  61457. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61458. WOLFSSL_SUCCESS);
  61459. /* set server certificate type in server end */
  61460. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61461. WOLFSSL_SUCCESS);
  61462. /* set certificate verify callback to both client and server */
  61463. isServer = 0;
  61464. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  61465. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  61466. isServer = 1;
  61467. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  61468. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  61469. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  61470. if (ret != 0)
  61471. return TEST_FAIL;
  61472. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61473. WOLFSSL_SUCCESS);
  61474. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61475. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61476. WOLFSSL_SUCCESS);
  61477. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61478. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61479. WOLFSSL_SUCCESS);
  61480. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61481. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61482. WOLFSSL_SUCCESS);
  61483. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61484. wolfSSL_free(ssl_c);
  61485. wolfSSL_CTX_free(ctx_c);
  61486. wolfSSL_free(ssl_s);
  61487. wolfSSL_CTX_free(ctx_s);
  61488. ssl_c = ssl_s = NULL;
  61489. ctx_c = ctx_s = NULL;
  61490. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  61491. #endif /* HAVE_RPK */
  61492. return EXPECT_RESULT();
  61493. }
  61494. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61495. static int test_dtls13_bad_epoch_ch(void)
  61496. {
  61497. EXPECT_DECLS;
  61498. WOLFSSL_CTX *ctx_c = NULL;
  61499. WOLFSSL_CTX *ctx_s = NULL;
  61500. WOLFSSL *ssl_c = NULL;
  61501. WOLFSSL *ssl_s = NULL;
  61502. struct test_memio_ctx test_ctx;
  61503. const int EPOCH_OFF = 3;
  61504. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61505. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61506. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61507. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  61508. * with just one message */
  61509. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  61510. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61511. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61512. WOLFSSL_ERROR_WANT_READ);
  61513. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  61514. /* first CH should use epoch 0x0 */
  61515. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  61516. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  61517. /* change epoch to 2 */
  61518. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  61519. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61520. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61521. WOLFSSL_ERROR_WANT_READ);
  61522. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  61523. /* resend the CH */
  61524. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61525. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61526. wolfSSL_free(ssl_c);
  61527. wolfSSL_CTX_free(ctx_c);
  61528. wolfSSL_free(ssl_s);
  61529. wolfSSL_CTX_free(ctx_s);
  61530. return EXPECT_RESULT();
  61531. }
  61532. #else
  61533. static int test_dtls13_bad_epoch_ch(void)
  61534. {
  61535. return TEST_SKIPPED;
  61536. }
  61537. #endif
  61538. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  61539. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  61540. {
  61541. EXPECT_DECLS;
  61542. WOLFSSL_SESSION *sess = NULL;
  61543. /* Setup the session to avoid errors */
  61544. ssl->session->timeout = -1;
  61545. ssl->session->side = WOLFSSL_CLIENT_END;
  61546. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  61547. defined(HAVE_SESSION_TICKET))
  61548. ssl->session->version = ssl->version;
  61549. #endif
  61550. /* Force a short session ID to be sent */
  61551. ssl->session->sessionIDSz = 4;
  61552. #ifndef NO_SESSION_CACHE_REF
  61553. /* Allow the client cache to be used */
  61554. ssl->session->idLen = 4;
  61555. #endif
  61556. ssl->session->isSetup = 1;
  61557. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  61558. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  61559. return EXPECT_RESULT();
  61560. }
  61561. static int test_short_session_id(void)
  61562. {
  61563. EXPECT_DECLS;
  61564. test_ssl_cbf client_cbf;
  61565. test_ssl_cbf server_cbf;
  61566. size_t i;
  61567. struct {
  61568. method_provider client_meth;
  61569. method_provider server_meth;
  61570. const char* tls_version;
  61571. } params[] = {
  61572. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  61573. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  61574. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  61575. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  61576. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  61577. #ifdef WOLFSSL_DTLS13
  61578. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  61579. #endif
  61580. #endif
  61581. #ifndef WOLFSSL_NO_TLS12
  61582. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  61583. #ifdef WOLFSSL_DTLS
  61584. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  61585. #endif
  61586. #endif
  61587. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  61588. !defined(NO_DES3))
  61589. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  61590. #ifdef WOLFSSL_DTLS
  61591. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  61592. #endif
  61593. #endif
  61594. };
  61595. fprintf(stderr, "\n");
  61596. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  61597. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61598. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61599. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  61600. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  61601. client_cbf.method = params[i].client_meth;
  61602. server_cbf.method = params[i].server_meth;
  61603. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61604. &server_cbf, NULL), TEST_SUCCESS);
  61605. }
  61606. return EXPECT_RESULT();
  61607. }
  61608. #else
  61609. static int test_short_session_id(void)
  61610. {
  61611. return TEST_SKIPPED;
  61612. }
  61613. #endif
  61614. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  61615. && defined(WOLFSSL_DTLS13)
  61616. static byte* test_find_string(const char *string,
  61617. byte *buf, int buf_size)
  61618. {
  61619. int string_size, i;
  61620. string_size = (int)XSTRLEN(string);
  61621. for (i = 0; i < buf_size - string_size - 1; i++) {
  61622. if (XSTRCMP((char*)&buf[i], string) == 0)
  61623. return &buf[i];
  61624. }
  61625. return NULL;
  61626. }
  61627. static int test_wolfSSL_dtls13_null_cipher(void)
  61628. {
  61629. EXPECT_DECLS;
  61630. WOLFSSL_CTX *ctx_c = NULL;
  61631. WOLFSSL_CTX *ctx_s = NULL;
  61632. WOLFSSL *ssl_c = NULL;
  61633. WOLFSSL *ssl_s = NULL;
  61634. struct test_memio_ctx test_ctx;
  61635. const char *test_str = "test";
  61636. int test_str_size;
  61637. byte buf[255], *ptr = NULL;
  61638. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61639. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  61640. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61641. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61642. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61643. test_str_size = XSTRLEN("test") + 1;
  61644. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61645. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61646. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61647. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61648. /* check that the packet was sent cleartext */
  61649. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  61650. test_ctx.s_len));
  61651. if (ptr != NULL) {
  61652. /* modify the message */
  61653. *ptr = 'H';
  61654. /* bad messages should be ignored in DTLS */
  61655. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  61656. ExpectIntEQ(ssl_s->error, WANT_READ);
  61657. }
  61658. wolfSSL_free(ssl_c);
  61659. wolfSSL_free(ssl_s);
  61660. wolfSSL_CTX_free(ctx_c);
  61661. wolfSSL_CTX_free(ctx_s);
  61662. return TEST_SUCCESS;
  61663. }
  61664. #else
  61665. static int test_wolfSSL_dtls13_null_cipher(void)
  61666. {
  61667. return TEST_SKIPPED;
  61668. }
  61669. #endif
  61670. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61671. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61672. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  61673. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  61674. {
  61675. SOCKADDR_S peer;
  61676. XSOCKLENT len;
  61677. int ret;
  61678. XMEMSET((byte*)&peer, 0, sizeof(peer));
  61679. len = sizeof(peer);
  61680. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  61681. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  61682. return -1;
  61683. switch (peer.ss_family) {
  61684. #ifdef WOLFSSL_IPV6
  61685. case WOLFSSL_IP6: {
  61686. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  61687. break;
  61688. }
  61689. #endif /* WOLFSSL_IPV6 */
  61690. case WOLFSSL_IP4:
  61691. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  61692. break;
  61693. default:
  61694. return -1;
  61695. }
  61696. return 0;
  61697. }
  61698. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  61699. {
  61700. char buf[1] = {'t'};
  61701. SOCKADDR_IN_T addr;
  61702. int sock_fd;
  61703. word16 port;
  61704. int err;
  61705. (void)ssl;
  61706. (void)ctx;
  61707. if (ssl == NULL)
  61708. return -1;
  61709. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  61710. if (err != 0)
  61711. return -1;
  61712. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  61713. if (sock_fd == -1)
  61714. return -1;
  61715. build_addr(&addr, wolfSSLIP, port, 1, 0);
  61716. /* send a packet to the server. Being another socket, the kernel will ensure
  61717. * the source port will be different. */
  61718. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  61719. sizeof(addr));
  61720. close(sock_fd);
  61721. if (err == -1)
  61722. return -1;
  61723. return 0;
  61724. }
  61725. /* setup a SSL session but just after the handshake send a packet to the server
  61726. * with a source address different than the one of the connected client. The I/O
  61727. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  61728. * is done in test_dtls_msg_from_other_peer_cb */
  61729. static int test_dtls_msg_from_other_peer(void)
  61730. {
  61731. EXPECT_DECLS;
  61732. callback_functions client_cbs;
  61733. callback_functions server_cbs;
  61734. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  61735. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  61736. client_cbs.method = wolfDTLSv1_2_client_method;
  61737. server_cbs.method = wolfDTLSv1_2_server_method;
  61738. client_cbs.doUdp = 1;
  61739. server_cbs.doUdp = 1;
  61740. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  61741. test_dtls_msg_from_other_peer_cb);
  61742. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  61743. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  61744. return EXPECT_RESULT();
  61745. }
  61746. #else
  61747. static int test_dtls_msg_from_other_peer(void)
  61748. {
  61749. return TEST_SKIPPED;
  61750. }
  61751. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61752. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61753. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  61754. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  61755. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61756. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  61757. static int test_dtls_ipv6_check(void)
  61758. {
  61759. EXPECT_DECLS;
  61760. WOLFSSL_CTX *ctx_c = NULL;
  61761. WOLFSSL_CTX *ctx_s = NULL;
  61762. WOLFSSL *ssl_c = NULL;
  61763. WOLFSSL *ssl_s = NULL;
  61764. SOCKADDR_IN fake_addr6;
  61765. int sockfd = -1;
  61766. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  61767. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  61768. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  61769. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  61770. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61771. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  61772. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61773. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  61774. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  61775. /* mimic a sockaddr_in6 struct, this way we can't test without
  61776. * WOLFSSL_IPV6 */
  61777. fake_addr6.sin_family = WOLFSSL_IP6;
  61778. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  61779. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  61780. /* can't return error here, as the peer is opaque for wolfssl library at
  61781. * this point */
  61782. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  61783. WOLFSSL_SUCCESS);
  61784. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  61785. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  61786. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61787. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  61788. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  61789. WOLFSSL_SUCCESS);
  61790. /* reuse the socket */
  61791. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  61792. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  61793. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61794. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  61795. if (sockfd != -1)
  61796. close(sockfd);
  61797. wolfSSL_free(ssl_c);
  61798. wolfSSL_CTX_free(ctx_c);
  61799. wolfSSL_free(ssl_s);
  61800. wolfSSL_CTX_free(ctx_s);
  61801. return EXPECT_RESULT();
  61802. }
  61803. #else
  61804. static int test_dtls_ipv6_check(void)
  61805. {
  61806. return TEST_SKIPPED;
  61807. }
  61808. #endif
  61809. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61810. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61811. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  61812. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  61813. {
  61814. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61815. }
  61816. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  61817. {
  61818. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  61819. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  61820. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  61821. }
  61822. else {
  61823. char testMsg[] = "Message after SCR";
  61824. char msgBuf[sizeof(testMsg)];
  61825. int ret;
  61826. if (!wolfSSL_is_server(ssl)) {
  61827. AssertIntEQ(WOLFSSL_SUCCESS,
  61828. wolfSSL_set_session(ssl,
  61829. test_wolfSSL_SCR_after_resumption_session));
  61830. }
  61831. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61832. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61833. sizeof(testMsg));
  61834. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  61835. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  61836. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  61837. AssertIntEQ(ret, sizeof(msgBuf));
  61838. }
  61839. }
  61840. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  61841. {
  61842. AssertIntEQ(WOLFSSL_SUCCESS,
  61843. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  61844. }
  61845. static int test_wolfSSL_SCR_after_resumption(void)
  61846. {
  61847. EXPECT_DECLS;
  61848. callback_functions func_cb_client;
  61849. callback_functions func_cb_server;
  61850. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61851. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61852. func_cb_client.method = wolfTLSv1_2_client_method;
  61853. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  61854. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  61855. func_cb_server.method = wolfTLSv1_2_server_method;
  61856. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  61857. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61858. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61859. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61860. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  61861. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  61862. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61863. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61864. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61865. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  61866. return EXPECT_RESULT();
  61867. }
  61868. #else
  61869. static int test_wolfSSL_SCR_after_resumption(void)
  61870. {
  61871. return TEST_SKIPPED;
  61872. }
  61873. #endif
  61874. static int test_wolfSSL_configure_args(void)
  61875. {
  61876. EXPECT_DECLS;
  61877. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  61878. ExpectNotNull(wolfSSL_configure_args());
  61879. #endif
  61880. return EXPECT_RESULT();
  61881. }
  61882. static int test_dtls_no_extensions(void)
  61883. {
  61884. EXPECT_DECLS;
  61885. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61886. !defined(WOLFSSL_NO_TLS12)
  61887. WOLFSSL *ssl_s = NULL;
  61888. WOLFSSL_CTX *ctx_s = NULL;
  61889. struct test_memio_ctx test_ctx;
  61890. const byte chNoExtensions[] = {
  61891. /* Handshake type */
  61892. 0x16,
  61893. /* Version */
  61894. 0xfe, 0xff,
  61895. /* Epoch */
  61896. 0x00, 0x00,
  61897. /* Seq number */
  61898. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61899. /* Length */
  61900. 0x00, 0x40,
  61901. /* CH type */
  61902. 0x01,
  61903. /* Length */
  61904. 0x00, 0x00, 0x34,
  61905. /* Msg Seq */
  61906. 0x00, 0x00,
  61907. /* Frag offset */
  61908. 0x00, 0x00, 0x00,
  61909. /* Frag length */
  61910. 0x00, 0x00, 0x34,
  61911. /* Version */
  61912. 0xfe, 0xff,
  61913. /* Random */
  61914. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  61915. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  61916. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  61917. /* Session Length */
  61918. 0x00,
  61919. /* Cookie Length */
  61920. 0x00,
  61921. /* CS Length */
  61922. 0x00, 0x0c,
  61923. /* CS */
  61924. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  61925. /* Comp Meths Length */
  61926. 0x01,
  61927. /* Comp Meths */
  61928. 0x00
  61929. /* And finally... no extensions */
  61930. };
  61931. int i;
  61932. #ifdef OPENSSL_EXTRA
  61933. int repeats = 2;
  61934. #else
  61935. int repeats = 1;
  61936. #endif
  61937. for (i = 0; i < repeats; i++) {
  61938. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61939. ssl_s = NULL;
  61940. ctx_s = NULL;
  61941. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  61942. NULL, wolfDTLS_server_method), 0);
  61943. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  61944. test_ctx.s_len = sizeof(chNoExtensions);
  61945. #ifdef OPENSSL_EXTRA
  61946. if (i > 0) {
  61947. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  61948. WOLFSSL_SUCCESS);
  61949. }
  61950. #endif
  61951. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  61952. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61953. /* Expecting a handshake msg. Either HVR or SH. */
  61954. ExpectIntGT(test_ctx.c_len, 0);
  61955. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  61956. wolfSSL_free(ssl_s);
  61957. wolfSSL_CTX_free(ctx_s);
  61958. }
  61959. #endif
  61960. return EXPECT_RESULT();
  61961. }
  61962. static int test_TLSX_CA_NAMES_bad_extension(void)
  61963. {
  61964. EXPECT_DECLS;
  61965. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61966. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  61967. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  61968. defined(HAVE_NULL_CIPHER)
  61969. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  61970. * parse the CA Names extension. Otherwise it will return other non-related
  61971. * errors. If CA Names will be parsed in more configurations, that should
  61972. * be reflected in the macro guard above. */
  61973. WOLFSSL *ssl_c = NULL;
  61974. WOLFSSL_CTX *ctx_c = NULL;
  61975. struct test_memio_ctx test_ctx;
  61976. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  61977. const byte shBadCaNamesExt[] = {
  61978. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  61979. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  61980. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  61981. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  61982. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  61983. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  61984. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  61985. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  61986. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  61987. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  61988. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  61989. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  61990. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  61991. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  61992. };
  61993. const byte shBadCaNamesExt2[] = {
  61994. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  61995. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  61996. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  61997. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  61998. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  61999. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  62000. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  62001. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  62002. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  62003. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  62004. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  62005. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  62006. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  62007. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  62008. };
  62009. int i = 0;
  62010. for (i = 0; i < 2; i++) {
  62011. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62012. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  62013. wolfTLSv1_3_client_method, NULL), 0);
  62014. switch (i) {
  62015. case 0:
  62016. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  62017. sizeof(shBadCaNamesExt));
  62018. test_ctx.c_len = sizeof(shBadCaNamesExt);
  62019. break;
  62020. case 1:
  62021. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  62022. sizeof(shBadCaNamesExt2));
  62023. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  62024. break;
  62025. }
  62026. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62027. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  62028. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  62029. #else
  62030. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  62031. #endif
  62032. wolfSSL_free(ssl_c);
  62033. ssl_c = NULL;
  62034. wolfSSL_CTX_free(ctx_c);
  62035. ctx_c = NULL;
  62036. }
  62037. #endif
  62038. return EXPECT_RESULT();
  62039. }
  62040. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  62041. defined(HAVE_IO_TESTS_DEPENDENCIES)
  62042. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  62043. {
  62044. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  62045. WOLFSSL_SUCCESS);
  62046. }
  62047. static int test_dtls_1_0_hvr_downgrade(void)
  62048. {
  62049. EXPECT_DECLS;
  62050. callback_functions func_cb_client;
  62051. callback_functions func_cb_server;
  62052. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62053. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62054. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  62055. func_cb_client.method = wolfDTLS_client_method;
  62056. func_cb_server.method = wolfDTLSv1_2_server_method;
  62057. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  62058. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62059. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62060. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62061. return EXPECT_RESULT();
  62062. }
  62063. #else
  62064. static int test_dtls_1_0_hvr_downgrade(void)
  62065. {
  62066. EXPECT_DECLS;
  62067. return EXPECT_RESULT();
  62068. }
  62069. #endif
  62070. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  62071. defined(HAVE_SESSION_TICKET)
  62072. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  62073. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  62074. {
  62075. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  62076. AssertNotNull(test_session_ticket_no_id_session);
  62077. }
  62078. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  62079. {
  62080. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  62081. }
  62082. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  62083. {
  62084. test_session_ticket_no_id_session->sessionIDSz = 0;
  62085. AssertIntEQ(WOLFSSL_SUCCESS,
  62086. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  62087. }
  62088. static int test_session_ticket_no_id(void)
  62089. {
  62090. /* We are testing an expired (invalid crypto context in out case since the
  62091. * ctx changes) session ticket being sent with the session ID being 0
  62092. * length. */
  62093. EXPECT_DECLS;
  62094. callback_functions func_cb_client;
  62095. callback_functions func_cb_server;
  62096. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  62097. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  62098. func_cb_client.method = wolfTLSv1_2_client_method;
  62099. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  62100. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  62101. func_cb_server.method = wolfTLSv1_2_server_method;
  62102. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  62103. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62104. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62105. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62106. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  62107. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  62108. func_cb_client.method = wolfTLSv1_2_client_method;
  62109. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  62110. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  62111. func_cb_server.method = wolfTLSv1_2_server_method;
  62112. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  62113. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62114. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62115. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62116. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  62117. return EXPECT_RESULT();
  62118. }
  62119. #else
  62120. static int test_session_ticket_no_id(void)
  62121. {
  62122. EXPECT_DECLS;
  62123. return EXPECT_RESULT();
  62124. }
  62125. #endif
  62126. static int test_session_ticket_hs_update(void)
  62127. {
  62128. EXPECT_DECLS;
  62129. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  62130. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  62131. struct test_memio_ctx test_ctx;
  62132. struct test_memio_ctx test_ctx2;
  62133. struct test_memio_ctx test_ctx3;
  62134. WOLFSSL_CTX *ctx_c = NULL;
  62135. WOLFSSL_CTX *ctx_s = NULL;
  62136. WOLFSSL *ssl_c = NULL;
  62137. WOLFSSL *ssl_c2 = NULL;
  62138. WOLFSSL *ssl_c3 = NULL;
  62139. WOLFSSL *ssl_s = NULL;
  62140. WOLFSSL *ssl_s2 = NULL;
  62141. WOLFSSL *ssl_s3 = NULL;
  62142. WOLFSSL_SESSION *sess = NULL;
  62143. byte read_data[1];
  62144. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62145. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  62146. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  62147. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62148. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  62149. /* Generate tickets */
  62150. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62151. wolfSSL_SetLoggingPrefix("client");
  62152. /* Read the ticket msg */
  62153. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  62154. WOLFSSL_FATAL_ERROR);
  62155. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  62156. WOLFSSL_ERROR_WANT_READ);
  62157. wolfSSL_SetLoggingPrefix(NULL);
  62158. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  62159. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  62160. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  62161. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  62162. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  62163. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  62164. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  62165. wolfSSL_SetLoggingPrefix("client");
  62166. /* Exchange initial flights for the second connection */
  62167. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  62168. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  62169. WOLFSSL_ERROR_WANT_READ);
  62170. wolfSSL_SetLoggingPrefix(NULL);
  62171. wolfSSL_SetLoggingPrefix("server");
  62172. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  62173. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  62174. WOLFSSL_ERROR_WANT_READ);
  62175. wolfSSL_SetLoggingPrefix(NULL);
  62176. /* Complete third connection so that new tickets are exchanged */
  62177. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  62178. /* Read the ticket msg */
  62179. wolfSSL_SetLoggingPrefix("client");
  62180. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  62181. WOLFSSL_FATAL_ERROR);
  62182. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  62183. WOLFSSL_ERROR_WANT_READ);
  62184. wolfSSL_SetLoggingPrefix(NULL);
  62185. /* Complete second connection */
  62186. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  62187. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  62188. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  62189. wolfSSL_free(ssl_c);
  62190. wolfSSL_free(ssl_c2);
  62191. wolfSSL_free(ssl_c3);
  62192. wolfSSL_free(ssl_s);
  62193. wolfSSL_free(ssl_s2);
  62194. wolfSSL_free(ssl_s3);
  62195. wolfSSL_CTX_free(ctx_c);
  62196. wolfSSL_CTX_free(ctx_s);
  62197. wolfSSL_SESSION_free(sess);
  62198. #endif
  62199. return EXPECT_RESULT();
  62200. }
  62201. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  62202. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  62203. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  62204. {
  62205. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  62206. WOLFSSL_SUCCESS);
  62207. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  62208. }
  62209. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  62210. {
  62211. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  62212. }
  62213. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  62214. {
  62215. char testMsg[] = "Message after SCR";
  62216. char msgBuf[sizeof(testMsg)];
  62217. if (wolfSSL_is_server(ssl)) {
  62218. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  62219. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  62220. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  62221. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  62222. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  62223. sizeof(testMsg));
  62224. }
  62225. else {
  62226. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  62227. sizeof(testMsg));
  62228. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  62229. }
  62230. }
  62231. static int test_dtls_downgrade_scr_server(void)
  62232. {
  62233. EXPECT_DECLS;
  62234. callback_functions func_cb_client;
  62235. callback_functions func_cb_server;
  62236. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62237. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62238. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  62239. func_cb_client.method = wolfDTLSv1_2_client_method;
  62240. func_cb_server.method = wolfDTLS_server_method;
  62241. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  62242. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  62243. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  62244. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  62245. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62246. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62247. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62248. return EXPECT_RESULT();
  62249. }
  62250. #else
  62251. static int test_dtls_downgrade_scr_server(void)
  62252. {
  62253. EXPECT_DECLS;
  62254. return EXPECT_RESULT();
  62255. }
  62256. #endif
  62257. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  62258. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  62259. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  62260. {
  62261. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  62262. WOLFSSL_SUCCESS);
  62263. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  62264. }
  62265. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  62266. {
  62267. char testMsg[] = "Message after SCR";
  62268. char msgBuf[sizeof(testMsg)];
  62269. if (wolfSSL_is_server(ssl)) {
  62270. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  62271. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  62272. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  62273. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  62274. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  62275. sizeof(testMsg));
  62276. }
  62277. else {
  62278. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  62279. sizeof(testMsg));
  62280. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  62281. }
  62282. }
  62283. static int test_dtls_downgrade_scr(void)
  62284. {
  62285. EXPECT_DECLS;
  62286. callback_functions func_cb_client;
  62287. callback_functions func_cb_server;
  62288. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62289. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62290. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  62291. func_cb_client.method = wolfDTLS_client_method;
  62292. func_cb_server.method = wolfDTLSv1_2_server_method;
  62293. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  62294. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  62295. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  62296. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62297. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62298. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62299. return EXPECT_RESULT();
  62300. }
  62301. #else
  62302. static int test_dtls_downgrade_scr(void)
  62303. {
  62304. EXPECT_DECLS;
  62305. return EXPECT_RESULT();
  62306. }
  62307. #endif
  62308. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62309. && !defined(WOLFSSL_NO_TLS12)
  62310. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  62311. char *data, int sz, void *ctx)
  62312. {
  62313. static int call_counter = 0;
  62314. call_counter++;
  62315. (void)ssl;
  62316. (void)data;
  62317. (void)sz;
  62318. (void)ctx;
  62319. switch (call_counter) {
  62320. case 1:
  62321. case 2:
  62322. return WOLFSSL_CBIO_ERR_TIMEOUT;
  62323. case 3:
  62324. return WOLFSSL_CBIO_ERR_WANT_READ;
  62325. default:
  62326. AssertIntLE(call_counter, 3);
  62327. return -1;
  62328. }
  62329. }
  62330. #endif
  62331. /* Make sure we don't send acks before getting a server hello */
  62332. static int test_dtls_client_hello_timeout_downgrade(void)
  62333. {
  62334. EXPECT_DECLS;
  62335. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62336. && !defined(WOLFSSL_NO_TLS12)
  62337. WOLFSSL_CTX *ctx_c = NULL;
  62338. WOLFSSL_CTX *ctx_s = NULL;
  62339. WOLFSSL *ssl_c = NULL;
  62340. WOLFSSL *ssl_s = NULL;
  62341. struct test_memio_ctx test_ctx;
  62342. DtlsRecordLayerHeader* dtlsRH;
  62343. size_t len;
  62344. byte sequence_number[8];
  62345. int i;
  62346. for (i = 0; i < 2; i++) {
  62347. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62348. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62349. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  62350. if (i == 0) {
  62351. /* First time simulate timeout in IO layer */
  62352. /* CH1 */
  62353. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62354. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62355. /* HVR */
  62356. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62357. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62358. /* CH2 */
  62359. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62360. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62361. /* SH flight */
  62362. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62363. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62364. /* Drop the SH */
  62365. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  62366. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62367. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  62368. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  62369. (sizeof(DtlsRecordLayerHeader) + len));
  62370. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  62371. /* Read the remainder of the flight */
  62372. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62373. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62374. wolfSSL_SSLSetIORecv(ssl_c,
  62375. test_dtls_client_hello_timeout_downgrade_read_cb);
  62376. /* CH3 */
  62377. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62378. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62379. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  62380. }
  62381. else {
  62382. /* Second time call wolfSSL_dtls_got_timeout */
  62383. /* CH1 */
  62384. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62385. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62386. /* HVR */
  62387. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62388. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62389. /* CH2 */
  62390. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62391. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62392. /* SH flight */
  62393. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62394. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62395. /* Drop the SH */
  62396. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  62397. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62398. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  62399. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  62400. (sizeof(DtlsRecordLayerHeader) + len));
  62401. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  62402. /* Read the remainder of the flight */
  62403. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62404. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62405. /* Quick timeout should be set as we received at least one msg */
  62406. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  62407. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62408. /* Quick timeout should be cleared after a quick timeout */
  62409. /* CH3 */
  62410. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  62411. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62412. }
  62413. /* Parse out to make sure we got exactly one ClientHello message */
  62414. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62415. /* Second ClientHello after HVR */
  62416. sequence_number[7] = 2;
  62417. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  62418. ExpectIntEQ(dtlsRH->type, handshake);
  62419. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62420. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62421. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62422. sizeof(sequence_number)), 0);
  62423. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62424. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  62425. /* Connection should be able to continue */
  62426. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62427. wolfSSL_free(ssl_c);
  62428. wolfSSL_free(ssl_s);
  62429. wolfSSL_CTX_free(ctx_c);
  62430. wolfSSL_CTX_free(ctx_s);
  62431. ssl_c = NULL;
  62432. ssl_s = NULL;
  62433. ctx_c = NULL;
  62434. ctx_s = NULL;
  62435. if (!EXPECT_SUCCESS())
  62436. break;
  62437. }
  62438. #endif
  62439. return EXPECT_RESULT();
  62440. }
  62441. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62442. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  62443. int sz, void *ctx)
  62444. {
  62445. static int call_counter = 0;
  62446. call_counter++;
  62447. (void)ssl;
  62448. (void)data;
  62449. (void)sz;
  62450. (void)ctx;
  62451. switch (call_counter) {
  62452. case 1:
  62453. return WOLFSSL_CBIO_ERR_TIMEOUT;
  62454. case 2:
  62455. return WOLFSSL_CBIO_ERR_WANT_READ;
  62456. default:
  62457. AssertIntLE(call_counter, 2);
  62458. return -1;
  62459. }
  62460. }
  62461. #endif
  62462. /* Make sure we don't send acks before getting a server hello */
  62463. static int test_dtls_client_hello_timeout(void)
  62464. {
  62465. EXPECT_DECLS;
  62466. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62467. WOLFSSL *ssl_c = NULL;
  62468. WOLFSSL_CTX *ctx_c = NULL;
  62469. struct test_memio_ctx test_ctx;
  62470. DtlsRecordLayerHeader* dtlsRH;
  62471. size_t idx;
  62472. size_t len;
  62473. byte sequence_number[8];
  62474. int i;
  62475. for (i = 0; i < 2; i++) {
  62476. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62477. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  62478. wolfDTLSv1_3_client_method, NULL), 0);
  62479. if (i == 0) {
  62480. /* First time simulate timeout in IO layer */
  62481. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  62482. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62483. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62484. }
  62485. else {
  62486. /* Second time call wolfSSL_dtls_got_timeout */
  62487. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62488. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62489. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62490. }
  62491. /* Parse out to make sure we got exactly two ClientHello messages */
  62492. idx = 0;
  62493. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62494. /* First ClientHello */
  62495. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  62496. ExpectIntEQ(dtlsRH->type, handshake);
  62497. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62498. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62499. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62500. sizeof(sequence_number)), 0);
  62501. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62502. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  62503. idx += sizeof(DtlsRecordLayerHeader) + len;
  62504. /* Second ClientHello */
  62505. sequence_number[7] = 1;
  62506. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  62507. ExpectIntEQ(dtlsRH->type, handshake);
  62508. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62509. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62510. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62511. sizeof(sequence_number)), 0);
  62512. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62513. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  62514. wolfSSL_free(ssl_c);
  62515. wolfSSL_CTX_free(ctx_c);
  62516. ssl_c = NULL;
  62517. ctx_c = NULL;
  62518. if (!EXPECT_SUCCESS())
  62519. break;
  62520. }
  62521. #endif
  62522. return EXPECT_RESULT();
  62523. }
  62524. /* DTLS test when dropping the changed cipher spec message */
  62525. static int test_dtls_dropped_ccs(void)
  62526. {
  62527. EXPECT_DECLS;
  62528. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62529. && !defined(WOLFSSL_NO_TLS12)
  62530. WOLFSSL_CTX *ctx_c = NULL;
  62531. WOLFSSL_CTX *ctx_s = NULL;
  62532. WOLFSSL *ssl_c = NULL;
  62533. WOLFSSL *ssl_s = NULL;
  62534. struct test_memio_ctx test_ctx;
  62535. DtlsRecordLayerHeader* dtlsRH;
  62536. size_t len;
  62537. byte data[1];
  62538. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62539. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62540. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  62541. /* CH1 */
  62542. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62543. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62544. /* HVR */
  62545. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62546. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62547. /* CH2 */
  62548. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62549. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62550. /* Server first flight */
  62551. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62552. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62553. /* Client flight */
  62554. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62555. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62556. /* Server ccs + finished */
  62557. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  62558. /* Drop the ccs */
  62559. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  62560. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62561. ExpectIntEQ(len, 1);
  62562. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  62563. if (EXPECT_SUCCESS()) {
  62564. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  62565. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  62566. (sizeof(DtlsRecordLayerHeader) + len));
  62567. }
  62568. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  62569. /* Client rtx flight */
  62570. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62571. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62572. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62573. /* Server ccs + finished rtx */
  62574. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  62575. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62576. /* Client processes finished */
  62577. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  62578. wolfSSL_free(ssl_c);
  62579. wolfSSL_free(ssl_s);
  62580. wolfSSL_CTX_free(ctx_c);
  62581. wolfSSL_CTX_free(ctx_s);
  62582. #endif
  62583. return EXPECT_RESULT();
  62584. }
  62585. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62586. && !defined(WOLFSSL_NO_TLS12)
  62587. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  62588. byte seq_num)
  62589. {
  62590. EXPECT_DECLS;
  62591. DtlsRecordLayerHeader* dtlsRH;
  62592. byte sequence_number[8];
  62593. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62594. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  62595. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  62596. ExpectIntEQ(dtlsRH->type, handshake);
  62597. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62598. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62599. sequence_number[7] = seq_num;
  62600. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62601. sizeof(sequence_number)), 0);
  62602. return EXPECT_RESULT();
  62603. }
  62604. #endif
  62605. /*
  62606. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  62607. * and after a HelloRetryRequest. This is testing the server side as it is
  62608. * operating statelessly and should copy the sequence number of the ClientHello.
  62609. */
  62610. static int test_dtls_seq_num_downgrade(void)
  62611. {
  62612. EXPECT_DECLS;
  62613. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62614. && !defined(WOLFSSL_NO_TLS12)
  62615. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62616. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62617. struct test_memio_ctx test_ctx;
  62618. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62619. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62620. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  62621. /* CH1 */
  62622. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62623. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62624. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  62625. test_ctx.s_len, 0), TEST_SUCCESS);
  62626. /* HVR */
  62627. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62628. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62629. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  62630. test_ctx.c_len, 0), TEST_SUCCESS);
  62631. /* CH2 */
  62632. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62633. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62634. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  62635. test_ctx.s_len, 1), TEST_SUCCESS);
  62636. /* Server first flight */
  62637. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62638. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62639. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  62640. test_ctx.c_len, 1), TEST_SUCCESS);
  62641. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62642. wolfSSL_free(ssl_c);
  62643. wolfSSL_CTX_free(ctx_c);
  62644. wolfSSL_free(ssl_s);
  62645. wolfSSL_CTX_free(ctx_s);
  62646. #endif
  62647. return EXPECT_RESULT();
  62648. }
  62649. /**
  62650. * Make sure we don't send RSA Signature Hash Algorithms in the
  62651. * CertificateRequest when we don't have any such ciphers set.
  62652. * @return EXPECT_RESULT()
  62653. */
  62654. static int test_certreq_sighash_algos(void)
  62655. {
  62656. EXPECT_DECLS;
  62657. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  62658. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  62659. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  62660. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  62661. WOLFSSL_CTX *ctx_c = NULL;
  62662. WOLFSSL_CTX *ctx_s = NULL;
  62663. WOLFSSL *ssl_c = NULL;
  62664. WOLFSSL *ssl_s = NULL;
  62665. struct test_memio_ctx test_ctx;
  62666. int idx = 0;
  62667. int maxIdx = 0;
  62668. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62669. test_ctx.c_ciphers = test_ctx.s_ciphers =
  62670. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  62671. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62672. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  62673. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  62674. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  62675. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  62676. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  62677. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62678. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  62679. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62680. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  62681. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  62682. WOLFSSL_ERROR_WANT_READ);
  62683. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  62684. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  62685. WOLFSSL_ERROR_WANT_READ);
  62686. /* Find the CertificateRequest message */
  62687. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  62688. word16 len;
  62689. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  62690. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  62691. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  62692. ato16(test_ctx.c_buff + idx, &len);
  62693. idx += OPAQUE16_LEN;
  62694. if (test_ctx.c_buff[idx] == certificate_request) {
  62695. idx++;
  62696. /* length */
  62697. idx += OPAQUE24_LEN;
  62698. /* cert types */
  62699. idx += 1 + test_ctx.c_buff[idx];
  62700. /* Sig algos */
  62701. ato16(test_ctx.c_buff + idx, &len);
  62702. idx += OPAQUE16_LEN;
  62703. maxIdx = idx + (int)len;
  62704. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  62705. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  62706. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  62707. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  62708. else
  62709. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  62710. }
  62711. break;
  62712. }
  62713. else {
  62714. idx += (int)len;
  62715. }
  62716. }
  62717. ExpectIntLT(idx, test_ctx.c_len);
  62718. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62719. wolfSSL_free(ssl_c);
  62720. wolfSSL_free(ssl_s);
  62721. wolfSSL_CTX_free(ctx_c);
  62722. wolfSSL_CTX_free(ctx_s);
  62723. #endif
  62724. return EXPECT_RESULT();
  62725. }
  62726. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62727. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  62728. {
  62729. EXPECT_DECLS;
  62730. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  62731. myVerifyAction = VERIFY_USE_PREVERFIY;
  62732. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62733. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62734. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62735. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62736. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  62737. WOLFSSL_SUCCESS);
  62738. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62739. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  62740. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62741. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62742. "./certs/crl/ca-int.pem",
  62743. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62744. return EXPECT_RESULT();
  62745. }
  62746. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  62747. {
  62748. EXPECT_DECLS;
  62749. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  62750. myVerifyAction = VERIFY_USE_PREVERFIY;
  62751. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62752. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62753. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62754. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62755. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62756. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62757. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  62758. WOLFSSL_SUCCESS);
  62759. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62760. "./certs/crl/ca-int2.pem",
  62761. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62762. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62763. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  62764. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62765. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62766. "./certs/crl/ca-int.pem",
  62767. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62768. return EXPECT_RESULT();
  62769. }
  62770. #endif
  62771. static int test_revoked_loaded_int_cert(void)
  62772. {
  62773. EXPECT_DECLS;
  62774. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62775. test_ssl_cbf client_cbf;
  62776. test_ssl_cbf server_cbf;
  62777. struct {
  62778. const char* certPemFile;
  62779. const char* keyPemFile;
  62780. ctx_cb client_ctx_ready;
  62781. } test_params[] = {
  62782. {"./certs/intermediate/ca-int2-cert.pem",
  62783. "./certs/intermediate/ca-int2-key.pem",
  62784. test_revoked_loaded_int_cert_ctx_ready1},
  62785. {"./certs/intermediate/server-chain.pem",
  62786. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  62787. {"./certs/intermediate/server-chain-short.pem",
  62788. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  62789. };
  62790. size_t i;
  62791. printf("\n");
  62792. for (i = 0; i < XELEM_CNT(test_params); i++) {
  62793. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62794. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62795. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  62796. server_cbf.certPemFile = test_params[i].certPemFile;
  62797. server_cbf.keyPemFile = test_params[i].keyPemFile;
  62798. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  62799. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62800. &server_cbf, NULL), TEST_FAIL);
  62801. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  62802. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  62803. if (!EXPECT_SUCCESS())
  62804. break;
  62805. printf("\t%s passed\n", test_params[i].certPemFile);
  62806. }
  62807. #endif
  62808. return EXPECT_RESULT();
  62809. }
  62810. static int test_dtls13_frag_ch_pq(void)
  62811. {
  62812. EXPECT_DECLS;
  62813. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62814. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  62815. WOLFSSL_CTX *ctx_c = NULL;
  62816. WOLFSSL_CTX *ctx_s = NULL;
  62817. WOLFSSL *ssl_c = NULL;
  62818. WOLFSSL *ssl_s = NULL;
  62819. struct test_memio_ctx test_ctx;
  62820. const char *test_str = "test";
  62821. int test_str_size;
  62822. byte buf[255];
  62823. int group = WOLFSSL_KYBER_LEVEL5;
  62824. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62825. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62826. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  62827. /* Add in a large post-quantum key share to make the CH long. */
  62828. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  62829. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  62830. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  62831. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62832. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  62833. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  62834. test_str_size = XSTRLEN("test") + 1;
  62835. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  62836. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  62837. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  62838. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  62839. wolfSSL_free(ssl_c);
  62840. wolfSSL_free(ssl_s);
  62841. wolfSSL_CTX_free(ctx_c);
  62842. wolfSSL_CTX_free(ctx_s);
  62843. #endif
  62844. return EXPECT_RESULT();
  62845. }
  62846. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62847. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  62848. static int test_dtls_frag_ch_count_records(byte* b, int len)
  62849. {
  62850. DtlsRecordLayerHeader* dtlsRH;
  62851. int records = 0;
  62852. size_t recordLen;
  62853. while (len > 0) {
  62854. records++;
  62855. dtlsRH = (DtlsRecordLayerHeader*)b;
  62856. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  62857. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  62858. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  62859. }
  62860. return records;
  62861. }
  62862. #endif
  62863. static int test_dtls_frag_ch(void)
  62864. {
  62865. EXPECT_DECLS;
  62866. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62867. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  62868. WOLFSSL_CTX *ctx_c = NULL;
  62869. WOLFSSL_CTX *ctx_s = NULL;
  62870. WOLFSSL *ssl_c = NULL;
  62871. WOLFSSL *ssl_s = NULL;
  62872. struct test_memio_ctx test_ctx;
  62873. static unsigned int DUMMY_MTU = 256;
  62874. unsigned char four_frag_CH[] = {
  62875. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62876. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62877. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  62878. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  62879. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  62880. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  62881. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  62882. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  62883. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  62884. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  62885. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  62886. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  62887. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  62888. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  62889. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  62890. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  62891. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  62892. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  62893. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  62894. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62895. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  62896. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  62897. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  62898. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  62899. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  62900. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  62901. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  62902. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  62903. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  62904. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  62905. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  62906. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  62907. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  62908. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  62909. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  62910. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  62911. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  62912. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  62913. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  62914. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  62915. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  62916. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  62917. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  62918. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  62919. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  62920. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  62921. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  62922. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  62923. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  62924. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  62925. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  62926. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  62927. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  62928. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  62929. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  62930. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  62931. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  62932. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  62933. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  62934. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  62935. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  62936. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  62937. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  62938. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  62939. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  62940. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  62941. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  62942. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  62943. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  62944. 0xa5, 0xcb, 0x68, 0xf3
  62945. };
  62946. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62947. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62948. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  62949. /* Fragment msgs */
  62950. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  62951. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  62952. /* Add in some key shares to make the CH long */
  62953. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  62954. WOLFSSL_SUCCESS);
  62955. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  62956. WOLFSSL_SUCCESS);
  62957. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  62958. WOLFSSL_SUCCESS);
  62959. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  62960. WOLFSSL_SUCCESS);
  62961. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  62962. /* Reject fragmented first CH */
  62963. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  62964. sizeof(four_frag_CH)), 4);
  62965. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  62966. test_ctx.s_len = sizeof(four_frag_CH);
  62967. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  62968. int s_len = test_ctx.s_len;
  62969. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62970. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62971. /* Fail if we didn't advance the buffer to avoid infinite loops */
  62972. ExpectIntLT(test_ctx.s_len, s_len);
  62973. }
  62974. /* Expect all fragments read */
  62975. ExpectIntEQ(test_ctx.s_len, 0);
  62976. /* Expect quietly dropping fragmented first CH */
  62977. ExpectIntEQ(test_ctx.c_len, 0);
  62978. /* CH1 */
  62979. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62980. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62981. /* Count records. Expect 1 unfragmented CH */
  62982. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  62983. test_ctx.s_len), 1);
  62984. /* HRR */
  62985. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62986. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62987. /* CH2 */
  62988. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62989. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62990. /* Count records. Expect fragmented CH */
  62991. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  62992. test_ctx.s_len), 1);
  62993. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62994. wolfSSL_free(ssl_c);
  62995. wolfSSL_free(ssl_s);
  62996. wolfSSL_CTX_free(ctx_c);
  62997. wolfSSL_CTX_free(ctx_s);
  62998. ssl_c = ssl_s = NULL;
  62999. ctx_c = ctx_s = NULL;
  63000. #endif
  63001. return EXPECT_RESULT();
  63002. }
  63003. static int test_dtls_empty_keyshare_with_cookie(void)
  63004. {
  63005. EXPECT_DECLS;
  63006. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  63007. WOLFSSL_CTX *ctx_s = NULL;
  63008. WOLFSSL *ssl_s = NULL;
  63009. struct test_memio_ctx test_ctx;
  63010. unsigned char ch_empty_keyshare_with_cookie[] = {
  63011. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  63012. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  63013. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  63014. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  63015. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  63016. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  63017. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  63018. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  63019. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  63020. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  63021. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  63022. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  63023. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  63024. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  63025. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  63026. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  63027. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  63028. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  63029. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  63030. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  63031. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  63032. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  63033. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  63034. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  63035. };
  63036. DtlsRecordLayerHeader* dtlsRH;
  63037. byte sequence_number[8];
  63038. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  63039. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63040. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  63041. sizeof(ch_empty_keyshare_with_cookie));
  63042. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  63043. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  63044. NULL, wolfDTLSv1_3_server_method), 0);
  63045. /* CH1 */
  63046. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63047. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63048. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  63049. ExpectIntEQ(test_ctx.c_len, 15);
  63050. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  63051. ExpectIntEQ(dtlsRH->type, alert);
  63052. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  63053. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  63054. sequence_number[7] = 1;
  63055. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  63056. sizeof(sequence_number)), 0);
  63057. ExpectIntEQ(dtlsRH->length[0], 0);
  63058. ExpectIntEQ(dtlsRH->length[1], 2);
  63059. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  63060. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  63061. wolfSSL_free(ssl_s);
  63062. wolfSSL_CTX_free(ctx_s);
  63063. #endif
  63064. return EXPECT_RESULT();
  63065. }
  63066. static int test_dtls_old_seq_number(void)
  63067. {
  63068. EXPECT_DECLS;
  63069. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  63070. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  63071. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  63072. struct test_memio_ctx test_ctx;
  63073. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63074. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63075. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  63076. /* CH1 */
  63077. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63078. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63079. /* HVR */
  63080. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63081. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63082. /* CH2 */
  63083. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63084. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63085. /* Server first flight */
  63086. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63087. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63088. /* Client second flight */
  63089. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63090. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63091. /* Modify the sequence number */
  63092. {
  63093. DtlsRecordLayerHeader* dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  63094. XMEMSET(dtlsRH->sequence_number, 0, sizeof(dtlsRH->sequence_number));
  63095. }
  63096. /* Server second flight */
  63097. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63098. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63099. /* Server should not do anything as a pkt was dropped */
  63100. ExpectIntEQ(test_ctx.c_len, 0);
  63101. ExpectIntEQ(test_ctx.s_len, 0);
  63102. /* Trigger rtx */
  63103. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63104. /* Complete connection */
  63105. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63106. wolfSSL_free(ssl_c);
  63107. wolfSSL_CTX_free(ctx_c);
  63108. wolfSSL_free(ssl_s);
  63109. wolfSSL_CTX_free(ctx_s);
  63110. #endif
  63111. return EXPECT_RESULT();
  63112. }
  63113. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  63114. defined(HAVE_LIBOQS)
  63115. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  63116. {
  63117. int group = WOLFSSL_KYBER_LEVEL5;
  63118. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  63119. }
  63120. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  63121. {
  63122. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  63123. }
  63124. #endif
  63125. static int test_tls13_pq_groups(void)
  63126. {
  63127. EXPECT_DECLS;
  63128. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  63129. defined(HAVE_LIBOQS)
  63130. callback_functions func_cb_client;
  63131. callback_functions func_cb_server;
  63132. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  63133. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  63134. func_cb_client.method = wolfTLSv1_3_client_method;
  63135. func_cb_server.method = wolfTLSv1_3_server_method;
  63136. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  63137. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  63138. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  63139. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63140. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63141. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63142. #endif
  63143. return EXPECT_RESULT();
  63144. }
  63145. static int test_tls13_early_data(void)
  63146. {
  63147. EXPECT_DECLS;
  63148. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  63149. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  63150. int written = 0;
  63151. int read = 0;
  63152. size_t i;
  63153. int splitEarlyData;
  63154. char msg[] = "This is early data";
  63155. char msg2[] = "This is client data";
  63156. char msg3[] = "This is server data";
  63157. char msg4[] = "This is server immediate data";
  63158. char msgBuf[50];
  63159. struct {
  63160. method_provider client_meth;
  63161. method_provider server_meth;
  63162. const char* tls_version;
  63163. int isUdp;
  63164. } params[] = {
  63165. #ifdef WOLFSSL_TLS13
  63166. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  63167. "TLS 1.3", 0 },
  63168. #endif
  63169. #ifdef WOLFSSL_DTLS13
  63170. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  63171. "DTLS 1.3", 1 },
  63172. #endif
  63173. };
  63174. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  63175. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  63176. struct test_memio_ctx test_ctx;
  63177. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  63178. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  63179. WOLFSSL_SESSION *sess = NULL;
  63180. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63181. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  63182. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  63183. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  63184. /* Get a ticket so that we can do 0-RTT on the next connection */
  63185. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63186. /* Make sure we read the ticket */
  63187. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  63188. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63189. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  63190. wolfSSL_free(ssl_c);
  63191. ssl_c = NULL;
  63192. wolfSSL_free(ssl_s);
  63193. ssl_s = NULL;
  63194. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63195. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63196. params[i].client_meth, params[i].server_meth), 0);
  63197. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  63198. #ifdef WOLFSSL_DTLS13
  63199. if (params[i].isUdp) {
  63200. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  63201. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  63202. #else
  63203. /* Let's test this but we generally don't recommend turning off the
  63204. * cookie exchange */
  63205. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  63206. #endif
  63207. }
  63208. #endif
  63209. /* Test 0-RTT data */
  63210. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  63211. &written), sizeof(msg));
  63212. ExpectIntEQ(written, sizeof(msg));
  63213. if (splitEarlyData) {
  63214. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  63215. &written), sizeof(msg));
  63216. ExpectIntEQ(written, sizeof(msg));
  63217. }
  63218. /* Read first 0-RTT data (if split otherwise entire data) */
  63219. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  63220. &read), sizeof(msg));
  63221. ExpectIntEQ(read, sizeof(msg));
  63222. ExpectStrEQ(msg, msgBuf);
  63223. /* Test 0.5-RTT data */
  63224. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  63225. if (splitEarlyData) {
  63226. /* Read second 0-RTT data */
  63227. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  63228. &read), sizeof(msg));
  63229. ExpectIntEQ(read, sizeof(msg));
  63230. ExpectStrEQ(msg, msgBuf);
  63231. }
  63232. if (params[i].isUdp) {
  63233. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63234. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  63235. /* Read server 0.5-RTT data */
  63236. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  63237. ExpectStrEQ(msg4, msgBuf);
  63238. /* Complete handshake */
  63239. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63240. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63241. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  63242. * a user would loop until it is true but here we control both sides so we
  63243. * just assert the expected value. wolfSSL_read_early_data does not provide
  63244. * handshake status to us with non-blocking IO and we can't use
  63245. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  63246. * parsing logic. */
  63247. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  63248. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  63249. &read), 0);
  63250. ExpectIntEQ(read, 0);
  63251. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  63252. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  63253. }
  63254. else {
  63255. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  63256. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  63257. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  63258. &read), 0);
  63259. ExpectIntEQ(read, 0);
  63260. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  63261. /* Read server 0.5-RTT data */
  63262. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  63263. ExpectStrEQ(msg4, msgBuf);
  63264. }
  63265. /* Test bi-directional write */
  63266. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  63267. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  63268. ExpectStrEQ(msg2, msgBuf);
  63269. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  63270. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  63271. ExpectStrEQ(msg3, msgBuf);
  63272. ExpectTrue(wolfSSL_session_reused(ssl_c));
  63273. ExpectTrue(wolfSSL_session_reused(ssl_s));
  63274. wolfSSL_SESSION_free(sess);
  63275. wolfSSL_free(ssl_c);
  63276. wolfSSL_free(ssl_s);
  63277. wolfSSL_CTX_free(ctx_c);
  63278. wolfSSL_CTX_free(ctx_s);
  63279. }
  63280. }
  63281. #endif
  63282. return EXPECT_RESULT();
  63283. }
  63284. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  63285. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  63286. {
  63287. EXPECT_DECLS;
  63288. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  63289. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  63290. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  63291. return EXPECT_RESULT();
  63292. }
  63293. #endif
  63294. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  63295. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  63296. {
  63297. EXPECT_DECLS;
  63298. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  63299. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  63300. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  63301. return EXPECT_RESULT();
  63302. }
  63303. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  63304. {
  63305. EXPECT_DECLS;
  63306. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  63307. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  63308. 0), 1);
  63309. return EXPECT_RESULT();
  63310. }
  63311. #endif
  63312. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  63313. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  63314. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  63315. {
  63316. EXPECT_DECLS;
  63317. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  63318. return EXPECT_RESULT();
  63319. }
  63320. #endif
  63321. static int test_self_signed_stapling(void)
  63322. {
  63323. EXPECT_DECLS;
  63324. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  63325. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  63326. test_ssl_cbf client_cbf;
  63327. test_ssl_cbf server_cbf;
  63328. size_t i;
  63329. struct {
  63330. method_provider client_meth;
  63331. method_provider server_meth;
  63332. ctx_cb client_ctx;
  63333. const char* tls_version;
  63334. } params[] = {
  63335. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  63336. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  63337. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  63338. #endif
  63339. #ifndef WOLFSSL_NO_TLS12
  63340. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  63341. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  63342. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  63343. #endif
  63344. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  63345. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  63346. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  63347. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  63348. test_self_signed_stapling_client_v2_multi_ctx_ready,
  63349. "TLSv1_2 v2 multi" },
  63350. #endif
  63351. #endif
  63352. };
  63353. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  63354. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63355. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63356. printf("\nTesting self-signed cert with status request: %s\n",
  63357. params[i].tls_version);
  63358. client_cbf.method = params[i].client_meth;
  63359. client_cbf.ctx_ready = params[i].client_ctx;
  63360. server_cbf.method = params[i].server_meth;
  63361. server_cbf.certPemFile = "certs/ca-cert.pem";
  63362. server_cbf.keyPemFile = "certs/ca-key.pem";
  63363. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  63364. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63365. &server_cbf, NULL), TEST_SUCCESS);
  63366. }
  63367. #endif
  63368. return EXPECT_RESULT();
  63369. }
  63370. static int test_tls_multi_handshakes_one_record(void)
  63371. {
  63372. EXPECT_DECLS;
  63373. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  63374. struct test_memio_ctx test_ctx;
  63375. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  63376. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  63377. RecordLayerHeader* rh = NULL;
  63378. byte *len ;
  63379. int newRecIdx = RECORD_HEADER_SZ;
  63380. int idx = 0;
  63381. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63382. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63383. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  63384. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63385. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63386. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  63387. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63388. /* Combine server handshake msgs into one record */
  63389. while (idx < test_ctx.c_len) {
  63390. word16 recLen;
  63391. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  63392. len = &rh->length[0];
  63393. ato16((const byte*)len, &recLen);
  63394. idx += RECORD_HEADER_SZ;
  63395. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  63396. (size_t)recLen);
  63397. newRecIdx += recLen;
  63398. idx += recLen;
  63399. }
  63400. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  63401. len = &rh->length[0];
  63402. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  63403. test_ctx.c_len = newRecIdx;
  63404. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63405. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63406. wolfSSL_free(ssl_c);
  63407. wolfSSL_free(ssl_s);
  63408. wolfSSL_CTX_free(ctx_c);
  63409. wolfSSL_CTX_free(ctx_s);
  63410. #endif
  63411. return EXPECT_RESULT();
  63412. }
  63413. static int test_write_dup(void)
  63414. {
  63415. EXPECT_DECLS;
  63416. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  63417. size_t i, j;
  63418. char hiWorld[] = "dup message";
  63419. char readData[sizeof(hiWorld) + 5];
  63420. struct {
  63421. method_provider client_meth;
  63422. method_provider server_meth;
  63423. const char* version_name;
  63424. int version;
  63425. } methods[] = {
  63426. #ifndef WOLFSSL_NO_TLS12
  63427. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  63428. #endif
  63429. #ifdef WOLFSSL_TLS13
  63430. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  63431. #endif
  63432. };
  63433. struct {
  63434. const char* cipher;
  63435. int version;
  63436. } ciphers[] = {
  63437. /* For simplicity the macros are copied from internal.h */
  63438. /* TLS 1.2 */
  63439. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  63440. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  63441. #ifndef NO_RSA
  63442. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  63443. #endif
  63444. #endif
  63445. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  63446. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  63447. #endif
  63448. #endif
  63449. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  63450. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  63451. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  63452. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  63453. #endif
  63454. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  63455. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  63456. #endif
  63457. #endif
  63458. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  63459. && !defined(NO_TLS) && !defined(NO_AES)
  63460. #ifdef HAVE_AESGCM
  63461. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  63462. #ifndef NO_RSA
  63463. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  63464. #endif
  63465. #endif
  63466. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  63467. #ifndef NO_RSA
  63468. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  63469. #endif
  63470. #endif
  63471. #endif
  63472. #endif
  63473. /* TLS 1.3 */
  63474. #ifdef WOLFSSL_TLS13
  63475. #ifdef HAVE_AESGCM
  63476. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  63477. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  63478. #endif
  63479. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  63480. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  63481. #endif
  63482. #endif
  63483. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  63484. #ifndef NO_SHA256
  63485. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  63486. #endif
  63487. #endif
  63488. #ifdef HAVE_AESCCM
  63489. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  63490. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  63491. #endif
  63492. #endif
  63493. #endif
  63494. };
  63495. for (i = 0; i < XELEM_CNT(methods); i++) {
  63496. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  63497. struct test_memio_ctx test_ctx;
  63498. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  63499. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  63500. WOLFSSL *ssl_c2 = NULL;
  63501. if (methods[i].version != ciphers[j].version)
  63502. continue;
  63503. if (i == 0 && j == 0)
  63504. printf("\n");
  63505. printf("Testing %s with %s... ", methods[i].version_name,
  63506. ciphers[j].cipher);
  63507. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63508. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  63509. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63510. methods[i].client_meth, methods[i].server_meth), 0);
  63511. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63512. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  63513. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  63514. WRITE_DUP_WRITE_E);
  63515. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  63516. sizeof(hiWorld));
  63517. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  63518. sizeof(hiWorld));
  63519. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  63520. sizeof(hiWorld));
  63521. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  63522. WRITE_DUP_READ_E);
  63523. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  63524. sizeof(hiWorld));
  63525. if (EXPECT_SUCCESS())
  63526. printf("ok\n");
  63527. else
  63528. printf("failed\n");
  63529. wolfSSL_free(ssl_c);
  63530. wolfSSL_free(ssl_c2);
  63531. wolfSSL_free(ssl_s);
  63532. wolfSSL_CTX_free(ctx_c);
  63533. wolfSSL_CTX_free(ctx_s);
  63534. }
  63535. }
  63536. #endif
  63537. return EXPECT_RESULT();
  63538. }
  63539. static int test_read_write_hs(void)
  63540. {
  63541. EXPECT_DECLS;
  63542. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  63543. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  63544. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  63545. struct test_memio_ctx test_ctx;
  63546. byte test_buffer[16];
  63547. unsigned int test;
  63548. /* test == 0 : client writes, server reads */
  63549. /* test == 1 : server writes, client reads */
  63550. for (test = 0; test < 2; test++) {
  63551. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63552. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63553. wolfTLSv1_2_client_method,
  63554. wolfTLSv1_2_server_method), 0);
  63555. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  63556. /* CH -> */
  63557. if (test == 0) {
  63558. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  63559. } else {
  63560. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63561. sizeof(test_buffer)), -1);
  63562. }
  63563. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63564. /* <- SH + SKE + SHD */
  63565. if (test == 0) {
  63566. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63567. sizeof(test_buffer)), -1);
  63568. } else {
  63569. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  63570. }
  63571. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63572. /* -> CKE + CLIENT FINISHED */
  63573. if (test == 0) {
  63574. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  63575. } else {
  63576. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63577. sizeof(test_buffer)), -1);
  63578. }
  63579. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63580. /* abide clang static analyzer */
  63581. if (ssl_s != NULL) {
  63582. /* disable group message to separate sending of ChangeCipherspec
  63583. * from Finished */
  63584. ssl_s->options.groupMessages = 0;
  63585. }
  63586. /* allow writing of CS, but not FINISHED */
  63587. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  63588. /* <- CS */
  63589. if (test == 0) {
  63590. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63591. sizeof(test_buffer)), -1);
  63592. } else {
  63593. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  63594. }
  63595. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  63596. /* move CS message where the client can read it */
  63597. memmove(test_ctx.c_buff,
  63598. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  63599. test_ctx.c_len = 6;
  63600. /* read CS */
  63601. if (test == 0) {
  63602. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  63603. } else {
  63604. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63605. sizeof(test_buffer)), -1);
  63606. }
  63607. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63608. ExpectIntEQ(test_ctx.c_len, 0);
  63609. if (test == 0) {
  63610. /* send SERVER FINISHED */
  63611. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63612. sizeof(test_buffer)), -1);
  63613. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  63614. WOLFSSL_ERROR_WANT_READ);
  63615. } else {
  63616. /* send SERVER FINISHED + App Data */
  63617. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  63618. }
  63619. ExpectIntGT(test_ctx.c_len, 0);
  63620. /* Send and receive the data */
  63621. if (test == 0) {
  63622. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  63623. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63624. sizeof(test_buffer)), 5);
  63625. } else {
  63626. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63627. sizeof(test_buffer)), 5);
  63628. }
  63629. ExpectBufEQ(test_buffer, "hello", 5);
  63630. wolfSSL_free(ssl_c);
  63631. wolfSSL_free(ssl_s);
  63632. wolfSSL_CTX_free(ctx_c);
  63633. wolfSSL_CTX_free(ctx_s);
  63634. ssl_c = ssl_s = NULL;
  63635. ctx_c = ctx_s = NULL;
  63636. }
  63637. #endif
  63638. return EXPECT_RESULT();
  63639. }
  63640. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  63641. static const char* test_get_signature_nid_siglag;
  63642. static int test_get_signature_nid_sig;
  63643. static int test_get_signature_nid_hash;
  63644. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  63645. {
  63646. EXPECT_DECLS;
  63647. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  63648. if (!wolfSSL_is_server(ssl)) {
  63649. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  63650. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  63651. }
  63652. return EXPECT_RESULT();
  63653. }
  63654. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63655. {
  63656. EXPECT_DECLS;
  63657. int nid = 0;
  63658. (void)ctx;
  63659. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  63660. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63661. ExpectIntEQ(nid, test_get_signature_nid_sig);
  63662. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63663. ExpectIntEQ(nid, test_get_signature_nid_hash);
  63664. }
  63665. else /* No sigalg info on static ciphersuite */
  63666. return TEST_SUCCESS;
  63667. return EXPECT_RESULT();
  63668. }
  63669. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63670. {
  63671. EXPECT_DECLS;
  63672. int nid = 0;
  63673. (void)ctx;
  63674. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63675. ExpectIntEQ(nid, test_get_signature_nid_sig);
  63676. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63677. ExpectIntEQ(nid, test_get_signature_nid_hash);
  63678. return EXPECT_RESULT();
  63679. }
  63680. #endif
  63681. static int test_get_signature_nid(void)
  63682. {
  63683. EXPECT_DECLS;
  63684. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  63685. test_ssl_cbf client_cbf;
  63686. test_ssl_cbf server_cbf;
  63687. size_t i;
  63688. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  63689. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  63690. caCertFile }
  63691. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  63692. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  63693. caEccCertFile }
  63694. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  63695. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  63696. caCertFile }
  63697. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  63698. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  63699. caEccCertFile }
  63700. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  63701. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  63702. caEdCertFile }
  63703. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  63704. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  63705. caEd448CertFile }
  63706. struct {
  63707. const char* siglag;
  63708. int sig_nid;
  63709. int hash_nid;
  63710. int tls_ver;
  63711. const char* server_cert;
  63712. const char* server_key;
  63713. const char* client_ca;
  63714. } params[] = {
  63715. #ifndef NO_RSA
  63716. #ifndef NO_SHA256
  63717. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  63718. #ifdef WC_RSA_PSS
  63719. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  63720. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  63721. #endif
  63722. #endif
  63723. #ifdef WOLFSSL_SHA512
  63724. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  63725. #ifdef WC_RSA_PSS
  63726. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  63727. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  63728. #endif
  63729. #endif
  63730. #endif
  63731. #ifdef HAVE_ECC
  63732. #ifndef NO_SHA256
  63733. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  63734. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  63735. #endif
  63736. #endif
  63737. #ifdef HAVE_ED25519
  63738. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  63739. #endif
  63740. #ifdef HAVE_ED448
  63741. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  63742. #endif
  63743. };
  63744. printf("\n");
  63745. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  63746. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63747. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63748. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  63749. params[i].siglag);
  63750. switch (params[i].tls_ver) {
  63751. #ifndef WOLFSSL_NO_TLS12
  63752. case WOLFSSL_TLSV1_2:
  63753. client_cbf.method = wolfTLSv1_2_client_method;
  63754. server_cbf.method = wolfTLSv1_2_server_method;
  63755. break;
  63756. #endif
  63757. #ifdef WOLFSSL_TLS13
  63758. case WOLFSSL_TLSV1_3:
  63759. client_cbf.method = wolfTLSv1_3_client_method;
  63760. server_cbf.method = wolfTLSv1_3_server_method;
  63761. break;
  63762. #endif
  63763. default:
  63764. printf("skipping\n");
  63765. continue;
  63766. }
  63767. test_get_signature_nid_siglag = params[i].siglag;
  63768. test_get_signature_nid_sig = params[i].sig_nid;
  63769. test_get_signature_nid_hash = params[i].hash_nid;
  63770. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  63771. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  63772. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  63773. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  63774. server_cbf.certPemFile = params[i].server_cert;
  63775. server_cbf.keyPemFile = params[i].server_key;
  63776. client_cbf.caPemFile = params[i].client_ca;
  63777. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63778. &server_cbf, NULL), TEST_SUCCESS);
  63779. if (EXPECT_SUCCESS())
  63780. printf("passed\n");
  63781. }
  63782. #endif
  63783. return EXPECT_RESULT();
  63784. }
  63785. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63786. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  63787. {
  63788. #ifndef NO_MD5
  63789. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  63790. #elif !defined(NO_SHA)
  63791. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  63792. #elif !defined(NO_SHA256)
  63793. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  63794. #else
  63795. #error "We need a digest to hash the Signer object"
  63796. #endif
  63797. byte hashBuf[WC_MAX_DIGEST_SIZE];
  63798. wc_HashAlg hash;
  63799. size_t i;
  63800. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  63801. for (i = 0; i < CA_TABLE_SIZE; i++) {
  63802. Signer* cur;
  63803. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  63804. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  63805. sizeof(*cur)), 0);
  63806. }
  63807. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  63808. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  63809. return MakeWordFromHash(hashBuf);
  63810. }
  63811. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  63812. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  63813. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  63814. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  63815. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  63816. {
  63817. EXPECT_DECLS;
  63818. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  63819. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  63820. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  63821. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  63822. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  63823. return EXPECT_RESULT();
  63824. }
  63825. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  63826. {
  63827. EXPECT_DECLS;
  63828. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  63829. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  63830. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  63831. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  63832. return EXPECT_RESULT();
  63833. }
  63834. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63835. {
  63836. EXPECT_DECLS;
  63837. WOLFSSL_CERT_MANAGER* cm;
  63838. (void)ssl;
  63839. /* WARNING: this approach bypasses the reference counter check in
  63840. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  63841. * cause unexpected behaviour when other active connections try accessing
  63842. * the caTable. */
  63843. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  63844. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  63845. WOLFSSL_SUCCESS);
  63846. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  63847. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  63848. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  63849. return EXPECT_RESULT();
  63850. }
  63851. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  63852. {
  63853. EXPECT_DECLS;
  63854. WOLFSSL_CTX* ctx;
  63855. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  63856. return EXPECT_RESULT();
  63857. }
  63858. #endif
  63859. static int test_tls_cert_store_unchanged(void)
  63860. {
  63861. EXPECT_DECLS;
  63862. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63863. test_ssl_cbf client_cbf;
  63864. test_ssl_cbf server_cbf;
  63865. int i;
  63866. for (i = 0; i < 2; i++) {
  63867. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63868. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63869. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  63870. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  63871. sizeof(test_tls_cert_store_unchanged_before_hashes));
  63872. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  63873. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  63874. sizeof(test_tls_cert_store_unchanged_after_hashes));
  63875. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  63876. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  63877. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  63878. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  63879. switch (i) {
  63880. case 0:
  63881. client_cbf.on_ctx_cleanup =
  63882. test_tls_cert_store_unchanged_ctx_cleanup;
  63883. server_cbf.on_ctx_cleanup =
  63884. test_tls_cert_store_unchanged_ctx_cleanup;
  63885. break;
  63886. case 1:
  63887. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  63888. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  63889. break;
  63890. default:
  63891. Fail(("Should not enter here"), ("Entered here"));
  63892. }
  63893. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  63894. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  63895. server_cbf.caPemFile = caCertFile;
  63896. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63897. &server_cbf, NULL), TEST_SUCCESS);
  63898. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  63899. test_tls_cert_store_unchanged_after_hashes,
  63900. sizeof(test_tls_cert_store_unchanged_after_hashes));
  63901. }
  63902. #endif
  63903. return EXPECT_RESULT();
  63904. }
  63905. static int test_wolfSSL_SendUserCanceled(void)
  63906. {
  63907. EXPECT_DECLS;
  63908. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  63909. size_t i;
  63910. struct {
  63911. method_provider client_meth;
  63912. method_provider server_meth;
  63913. const char* tls_version;
  63914. } params[] = {
  63915. #if defined(WOLFSSL_TLS13)
  63916. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  63917. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  63918. #ifdef WOLFSSL_DTLS13
  63919. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  63920. #endif
  63921. #endif
  63922. #ifndef WOLFSSL_NO_TLS12
  63923. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  63924. #ifdef WOLFSSL_DTLS
  63925. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  63926. #endif
  63927. #endif
  63928. #if !defined(NO_OLD_TLS)
  63929. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  63930. #ifdef WOLFSSL_DTLS
  63931. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  63932. #endif
  63933. #endif
  63934. };
  63935. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  63936. WOLFSSL_CTX *ctx_c = NULL;
  63937. WOLFSSL_CTX *ctx_s = NULL;
  63938. WOLFSSL *ssl_c = NULL;
  63939. WOLFSSL *ssl_s = NULL;
  63940. struct test_memio_ctx test_ctx;
  63941. WOLFSSL_ALERT_HISTORY h;
  63942. printf("Testing %s\n", params[i].tls_version);
  63943. XMEMSET(&h, 0, sizeof(h));
  63944. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63945. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63946. params[i].client_meth, params[i].server_meth), 0);
  63947. /* CH1 */
  63948. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63949. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63950. ExpectIntEQ(wolfSSL_SendUserCanceled(ssl_s), WOLFSSL_SHUTDOWN_NOT_DONE);
  63951. /* Alert closed connection */
  63952. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63953. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_ZERO_RETURN);
  63954. /* Last alert will be close notify because user_canceled should be
  63955. * followed by a close_notify */
  63956. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  63957. ExpectIntEQ(h.last_rx.code, close_notify);
  63958. ExpectIntEQ(h.last_rx.level, alert_warning);
  63959. wolfSSL_free(ssl_c);
  63960. wolfSSL_free(ssl_s);
  63961. wolfSSL_CTX_free(ctx_c);
  63962. wolfSSL_CTX_free(ctx_s);
  63963. }
  63964. #endif
  63965. return EXPECT_RESULT();
  63966. }
  63967. /*----------------------------------------------------------------------------*
  63968. | Main
  63969. *----------------------------------------------------------------------------*/
  63970. typedef int (*TEST_FUNC)(void);
  63971. typedef struct {
  63972. const char *name;
  63973. TEST_FUNC func;
  63974. byte run:1;
  63975. byte fail:1;
  63976. } TEST_CASE;
  63977. #define TEST_DECL(func) { #func, func, 0, 0 }
  63978. int testAll = 1;
  63979. TEST_CASE testCases[] = {
  63980. TEST_DECL(test_fileAccess),
  63981. /*********************************
  63982. * wolfcrypt
  63983. *********************************/
  63984. TEST_DECL(test_ForceZero),
  63985. TEST_DECL(test_wolfCrypt_Init),
  63986. TEST_DECL(test_wc_LoadStaticMemory_ex),
  63987. /* Locking with Compat Mutex */
  63988. TEST_DECL(test_wc_SetMutexCb),
  63989. TEST_DECL(test_wc_LockMutex_ex),
  63990. /* Digests */
  63991. TEST_DECL(test_wc_InitMd5),
  63992. TEST_DECL(test_wc_Md5Update),
  63993. TEST_DECL(test_wc_Md5Final),
  63994. TEST_DECL(test_wc_InitSha),
  63995. TEST_DECL(test_wc_ShaUpdate),
  63996. TEST_DECL(test_wc_ShaFinal),
  63997. TEST_DECL(test_wc_InitSha256),
  63998. TEST_DECL(test_wc_Sha256Update),
  63999. TEST_DECL(test_wc_Sha256Final),
  64000. TEST_DECL(test_wc_Sha256FinalRaw),
  64001. TEST_DECL(test_wc_Sha256GetFlags),
  64002. TEST_DECL(test_wc_Sha256Free),
  64003. TEST_DECL(test_wc_Sha256GetHash),
  64004. TEST_DECL(test_wc_Sha256Copy),
  64005. TEST_DECL(test_wc_InitSha224),
  64006. TEST_DECL(test_wc_Sha224Update),
  64007. TEST_DECL(test_wc_Sha224Final),
  64008. TEST_DECL(test_wc_Sha224SetFlags),
  64009. TEST_DECL(test_wc_Sha224GetFlags),
  64010. TEST_DECL(test_wc_Sha224Free),
  64011. TEST_DECL(test_wc_Sha224GetHash),
  64012. TEST_DECL(test_wc_Sha224Copy),
  64013. TEST_DECL(test_wc_InitSha512),
  64014. TEST_DECL(test_wc_Sha512Update),
  64015. TEST_DECL(test_wc_Sha512Final),
  64016. TEST_DECL(test_wc_Sha512GetFlags),
  64017. TEST_DECL(test_wc_Sha512FinalRaw),
  64018. TEST_DECL(test_wc_Sha512Free),
  64019. TEST_DECL(test_wc_Sha512GetHash),
  64020. TEST_DECL(test_wc_Sha512Copy),
  64021. TEST_DECL(test_wc_InitSha512_224),
  64022. TEST_DECL(test_wc_Sha512_224Update),
  64023. TEST_DECL(test_wc_Sha512_224Final),
  64024. TEST_DECL(test_wc_Sha512_224GetFlags),
  64025. TEST_DECL(test_wc_Sha512_224FinalRaw),
  64026. TEST_DECL(test_wc_Sha512_224Free),
  64027. TEST_DECL(test_wc_Sha512_224GetHash),
  64028. TEST_DECL(test_wc_Sha512_224Copy),
  64029. TEST_DECL(test_wc_InitSha512_256),
  64030. TEST_DECL(test_wc_Sha512_256Update),
  64031. TEST_DECL(test_wc_Sha512_256Final),
  64032. TEST_DECL(test_wc_Sha512_256GetFlags),
  64033. TEST_DECL(test_wc_Sha512_256FinalRaw),
  64034. TEST_DECL(test_wc_Sha512_256Free),
  64035. TEST_DECL(test_wc_Sha512_256GetHash),
  64036. TEST_DECL(test_wc_Sha512_256Copy),
  64037. TEST_DECL(test_wc_InitSha384),
  64038. TEST_DECL(test_wc_Sha384Update),
  64039. TEST_DECL(test_wc_Sha384Final),
  64040. TEST_DECL(test_wc_Sha384GetFlags),
  64041. TEST_DECL(test_wc_Sha384FinalRaw),
  64042. TEST_DECL(test_wc_Sha384Free),
  64043. TEST_DECL(test_wc_Sha384GetHash),
  64044. TEST_DECL(test_wc_Sha384Copy),
  64045. TEST_DECL(test_wc_InitBlake2b),
  64046. TEST_DECL(test_wc_InitBlake2b_WithKey),
  64047. TEST_DECL(test_wc_InitBlake2s_WithKey),
  64048. TEST_DECL(test_wc_InitRipeMd),
  64049. TEST_DECL(test_wc_RipeMdUpdate),
  64050. TEST_DECL(test_wc_RipeMdFinal),
  64051. TEST_DECL(test_wc_InitSha3),
  64052. TEST_DECL(testing_wc_Sha3_Update),
  64053. TEST_DECL(test_wc_Sha3_224_Final),
  64054. TEST_DECL(test_wc_Sha3_256_Final),
  64055. TEST_DECL(test_wc_Sha3_384_Final),
  64056. TEST_DECL(test_wc_Sha3_512_Final),
  64057. TEST_DECL(test_wc_Sha3_224_Copy),
  64058. TEST_DECL(test_wc_Sha3_256_Copy),
  64059. TEST_DECL(test_wc_Sha3_384_Copy),
  64060. TEST_DECL(test_wc_Sha3_512_Copy),
  64061. TEST_DECL(test_wc_Sha3_GetFlags),
  64062. TEST_DECL(test_wc_InitShake256),
  64063. TEST_DECL(testing_wc_Shake256_Update),
  64064. TEST_DECL(test_wc_Shake256_Final),
  64065. TEST_DECL(test_wc_Shake256_Copy),
  64066. TEST_DECL(test_wc_Shake256Hash),
  64067. /* SM3 Digest */
  64068. TEST_DECL(test_wc_InitSm3Free),
  64069. TEST_DECL(test_wc_Sm3UpdateFinal),
  64070. TEST_DECL(test_wc_Sm3GetHash),
  64071. TEST_DECL(test_wc_Sm3Copy),
  64072. TEST_DECL(test_wc_Sm3FinalRaw),
  64073. TEST_DECL(test_wc_Sm3GetSetFlags),
  64074. TEST_DECL(test_wc_Sm3Hash),
  64075. TEST_DECL(test_wc_HashInit),
  64076. TEST_DECL(test_wc_HashSetFlags),
  64077. TEST_DECL(test_wc_HashGetFlags),
  64078. /* HMAC */
  64079. TEST_DECL(test_wc_Md5HmacSetKey),
  64080. TEST_DECL(test_wc_Md5HmacUpdate),
  64081. TEST_DECL(test_wc_Md5HmacFinal),
  64082. TEST_DECL(test_wc_ShaHmacSetKey),
  64083. TEST_DECL(test_wc_ShaHmacUpdate),
  64084. TEST_DECL(test_wc_ShaHmacFinal),
  64085. TEST_DECL(test_wc_Sha224HmacSetKey),
  64086. TEST_DECL(test_wc_Sha224HmacUpdate),
  64087. TEST_DECL(test_wc_Sha224HmacFinal),
  64088. TEST_DECL(test_wc_Sha256HmacSetKey),
  64089. TEST_DECL(test_wc_Sha256HmacUpdate),
  64090. TEST_DECL(test_wc_Sha256HmacFinal),
  64091. TEST_DECL(test_wc_Sha384HmacSetKey),
  64092. TEST_DECL(test_wc_Sha384HmacUpdate),
  64093. TEST_DECL(test_wc_Sha384HmacFinal),
  64094. /* CMAC */
  64095. TEST_DECL(test_wc_InitCmac),
  64096. TEST_DECL(test_wc_CmacUpdate),
  64097. TEST_DECL(test_wc_CmacFinal),
  64098. TEST_DECL(test_wc_AesCmacGenerate),
  64099. /* Cipher */
  64100. TEST_DECL(test_wc_AesGcmStream),
  64101. TEST_DECL(test_wc_Des3_SetIV),
  64102. TEST_DECL(test_wc_Des3_SetKey),
  64103. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  64104. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  64105. TEST_DECL(test_wc_Des3_EcbEncrypt),
  64106. TEST_DECL(test_wc_Chacha_SetKey),
  64107. TEST_DECL(test_wc_Chacha_Process),
  64108. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  64109. TEST_DECL(test_wc_Poly1305SetKey),
  64110. TEST_DECL(test_wc_CamelliaSetKey),
  64111. TEST_DECL(test_wc_CamelliaSetIV),
  64112. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  64113. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  64114. TEST_DECL(test_wc_Arc4SetKey),
  64115. TEST_DECL(test_wc_Arc4Process),
  64116. TEST_DECL(test_wc_Rc2SetKey),
  64117. TEST_DECL(test_wc_Rc2SetIV),
  64118. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  64119. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  64120. /* AES cipher and GMAC. */
  64121. TEST_DECL(test_wc_AesSetKey),
  64122. TEST_DECL(test_wc_AesSetIV),
  64123. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  64124. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  64125. TEST_DECL(test_wc_AesGcmSetKey),
  64126. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  64127. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  64128. TEST_DECL(test_wc_GmacSetKey),
  64129. TEST_DECL(test_wc_GmacUpdate),
  64130. TEST_DECL(test_wc_AesCcmSetKey),
  64131. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  64132. #if defined(WOLFSSL_AES_EAX) && \
  64133. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  64134. TEST_DECL(test_wc_AesEaxVectors),
  64135. TEST_DECL(test_wc_AesEaxEncryptAuth),
  64136. TEST_DECL(test_wc_AesEaxDecryptAuth),
  64137. #endif /* WOLFSSL_AES_EAX */
  64138. /* SM4 cipher */
  64139. TEST_DECL(test_wc_Sm4),
  64140. TEST_DECL(test_wc_Sm4Ecb),
  64141. TEST_DECL(test_wc_Sm4Cbc),
  64142. TEST_DECL(test_wc_Sm4Ctr),
  64143. TEST_DECL(test_wc_Sm4Gcm),
  64144. TEST_DECL(test_wc_Sm4Ccm),
  64145. /* RNG tests */
  64146. #ifdef HAVE_HASHDRBG
  64147. #ifdef TEST_RESEED_INTERVAL
  64148. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  64149. #endif
  64150. TEST_DECL(test_wc_RNG_GenerateBlock),
  64151. #endif
  64152. TEST_DECL(test_get_rand_digit),
  64153. TEST_DECL(test_wc_InitRngNonce),
  64154. TEST_DECL(test_wc_InitRngNonce_ex),
  64155. /* MP API tests */
  64156. TEST_DECL(test_get_digit_count),
  64157. TEST_DECL(test_mp_cond_copy),
  64158. TEST_DECL(test_mp_rand),
  64159. TEST_DECL(test_get_digit),
  64160. TEST_DECL(test_wc_export_int),
  64161. /* RSA */
  64162. TEST_DECL(test_wc_InitRsaKey),
  64163. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  64164. TEST_DECL(test_wc_RsaPublicKeyDecode),
  64165. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  64166. TEST_DECL(test_wc_RsaPrivateKeyDecodeRaw),
  64167. TEST_DECL(test_wc_MakeRsaKey),
  64168. TEST_DECL(test_wc_CheckProbablePrime),
  64169. TEST_DECL(test_wc_RsaPSS_Verify),
  64170. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  64171. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  64172. TEST_DECL(test_wc_RsaKeyToDer),
  64173. TEST_DECL(test_wc_RsaKeyToPublicDer),
  64174. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  64175. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  64176. TEST_DECL(test_wc_RsaEncryptSize),
  64177. TEST_DECL(test_wc_RsaSSL_SignVerify),
  64178. TEST_DECL(test_wc_RsaFlattenPublicKey),
  64179. TEST_DECL(test_RsaDecryptBoundsCheck),
  64180. /* DSA */
  64181. TEST_DECL(test_wc_InitDsaKey),
  64182. TEST_DECL(test_wc_DsaSignVerify),
  64183. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  64184. TEST_DECL(test_wc_MakeDsaKey),
  64185. TEST_DECL(test_wc_DsaKeyToDer),
  64186. TEST_DECL(test_wc_DsaKeyToPublicDer),
  64187. TEST_DECL(test_wc_DsaImportParamsRaw),
  64188. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  64189. TEST_DECL(test_wc_DsaExportParamsRaw),
  64190. TEST_DECL(test_wc_DsaExportKeyRaw),
  64191. /* DH */
  64192. TEST_DECL(test_wc_DhPublicKeyDecode),
  64193. /* wolfCrypt ECC tests */
  64194. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  64195. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  64196. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  64197. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  64198. !defined(HAVE_SELFTEST) && \
  64199. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  64200. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  64201. #endif
  64202. TEST_DECL(test_wc_ecc_make_key),
  64203. TEST_DECL(test_wc_ecc_init),
  64204. TEST_DECL(test_wc_ecc_check_key),
  64205. TEST_DECL(test_wc_ecc_get_generator),
  64206. TEST_DECL(test_wc_ecc_size),
  64207. TEST_DECL(test_wc_ecc_params),
  64208. TEST_DECL(test_wc_ecc_signVerify_hash),
  64209. TEST_DECL(test_wc_ecc_shared_secret),
  64210. TEST_DECL(test_wc_ecc_export_x963),
  64211. TEST_DECL(test_wc_ecc_export_x963_ex),
  64212. TEST_DECL(test_wc_ecc_import_x963),
  64213. TEST_DECL(test_wc_ecc_import_private_key),
  64214. TEST_DECL(test_wc_ecc_export_private_only),
  64215. TEST_DECL(test_wc_ecc_rs_to_sig),
  64216. TEST_DECL(test_wc_ecc_import_raw),
  64217. TEST_DECL(test_wc_ecc_import_unsigned),
  64218. TEST_DECL(test_wc_ecc_sig_size),
  64219. TEST_DECL(test_wc_ecc_ctx_new),
  64220. TEST_DECL(test_wc_ecc_ctx_reset),
  64221. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  64222. TEST_DECL(test_wc_ecc_ctx_set_info),
  64223. TEST_DECL(test_wc_ecc_encryptDecrypt),
  64224. TEST_DECL(test_wc_ecc_del_point),
  64225. TEST_DECL(test_wc_ecc_pointFns),
  64226. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  64227. TEST_DECL(test_wc_ecc_verify_hash_ex),
  64228. TEST_DECL(test_wc_ecc_mulmod),
  64229. TEST_DECL(test_wc_ecc_is_valid_idx),
  64230. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  64231. TEST_DECL(test_wc_ecc_sig_size_calc),
  64232. TEST_DECL(test_wc_EccPrivateKeyToDer),
  64233. /* SM2 elliptic curve */
  64234. TEST_DECL(test_wc_ecc_sm2_make_key),
  64235. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  64236. TEST_DECL(test_wc_ecc_sm2_create_digest),
  64237. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  64238. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  64239. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  64240. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  64241. /* Curve25519 */
  64242. TEST_DECL(test_wc_curve25519_init),
  64243. TEST_DECL(test_wc_curve25519_size),
  64244. TEST_DECL(test_wc_curve25519_export_key_raw),
  64245. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  64246. TEST_DECL(test_wc_curve25519_make_key),
  64247. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  64248. TEST_DECL(test_wc_curve25519_make_pub),
  64249. TEST_DECL(test_wc_curve25519_export_public_ex),
  64250. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  64251. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  64252. TEST_DECL(test_wc_curve25519_import_private),
  64253. /* ED25519 */
  64254. TEST_DECL(test_wc_ed25519_make_key),
  64255. TEST_DECL(test_wc_ed25519_init),
  64256. TEST_DECL(test_wc_ed25519_sign_msg),
  64257. TEST_DECL(test_wc_ed25519_import_public),
  64258. TEST_DECL(test_wc_ed25519_import_private_key),
  64259. TEST_DECL(test_wc_ed25519_export),
  64260. TEST_DECL(test_wc_ed25519_size),
  64261. TEST_DECL(test_wc_ed25519_exportKey),
  64262. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  64263. TEST_DECL(test_wc_Ed25519KeyToDer),
  64264. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  64265. /* Curve448 */
  64266. TEST_DECL(test_wc_curve448_make_key),
  64267. TEST_DECL(test_wc_curve448_shared_secret_ex),
  64268. TEST_DECL(test_wc_curve448_export_public_ex),
  64269. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  64270. TEST_DECL(test_wc_curve448_export_key_raw),
  64271. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  64272. TEST_DECL(test_wc_curve448_import_private),
  64273. TEST_DECL(test_wc_curve448_init),
  64274. TEST_DECL(test_wc_curve448_size),
  64275. /* Ed448 */
  64276. TEST_DECL(test_wc_ed448_make_key),
  64277. TEST_DECL(test_wc_ed448_init),
  64278. TEST_DECL(test_wc_ed448_sign_msg),
  64279. TEST_DECL(test_wc_ed448_import_public),
  64280. TEST_DECL(test_wc_ed448_import_private_key),
  64281. TEST_DECL(test_wc_ed448_export),
  64282. TEST_DECL(test_wc_ed448_size),
  64283. TEST_DECL(test_wc_ed448_exportKey),
  64284. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  64285. TEST_DECL(test_wc_Ed448KeyToDer),
  64286. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  64287. TEST_DECL(test_wc_Curve448PrivateKeyToDer),
  64288. /* Signature API */
  64289. TEST_DECL(test_wc_SignatureGetSize_ecc),
  64290. TEST_DECL(test_wc_SignatureGetSize_rsa),
  64291. /* PEM and DER APIs. */
  64292. TEST_DECL(test_wc_PemToDer),
  64293. TEST_DECL(test_wc_AllocDer),
  64294. TEST_DECL(test_wc_CertPemToDer),
  64295. TEST_DECL(test_wc_KeyPemToDer),
  64296. TEST_DECL(test_wc_PubKeyPemToDer),
  64297. TEST_DECL(test_wc_PemPubKeyToDer),
  64298. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  64299. TEST_DECL(test_wc_CheckCertSigPubKey),
  64300. /* wolfCrypt ASN tests */
  64301. TEST_DECL(test_ToTraditional),
  64302. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  64303. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  64304. /* Certificate */
  64305. TEST_DECL(test_wc_SetSubjectRaw),
  64306. TEST_DECL(test_wc_GetSubjectRaw),
  64307. TEST_DECL(test_wc_SetIssuerRaw),
  64308. TEST_DECL(test_wc_SetIssueBuffer),
  64309. TEST_DECL(test_wc_SetSubjectKeyId),
  64310. TEST_DECL(test_wc_SetSubject),
  64311. TEST_DECL(test_CheckCertSignature),
  64312. TEST_DECL(test_wc_ParseCert),
  64313. TEST_DECL(test_wc_ParseCert_Error),
  64314. TEST_DECL(test_MakeCertWithPathLen),
  64315. TEST_DECL(test_MakeCertWith0Ser),
  64316. TEST_DECL(test_MakeCertWithCaFalse),
  64317. TEST_DECL(test_wc_SetKeyUsage),
  64318. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  64319. TEST_DECL(test_wc_SetSubjectBuffer),
  64320. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  64321. /* wolfcrypt PKCS#7 */
  64322. TEST_DECL(test_wc_PKCS7_New),
  64323. TEST_DECL(test_wc_PKCS7_Init),
  64324. TEST_DECL(test_wc_PKCS7_InitWithCert),
  64325. TEST_DECL(test_wc_PKCS7_EncodeData),
  64326. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  64327. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  64328. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  64329. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  64330. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  64331. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  64332. TEST_DECL(test_wc_PKCS7_Degenerate),
  64333. TEST_DECL(test_wc_PKCS7_BER),
  64334. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  64335. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  64336. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  64337. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  64338. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  64339. /* wolfCrypt PKCS#12 */
  64340. TEST_DECL(test_wc_i2d_PKCS12),
  64341. /*
  64342. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  64343. * avoid memory leaks.
  64344. */
  64345. TEST_DECL(test_wolfCrypt_Cleanup),
  64346. TEST_DECL(test_wolfSSL_Init),
  64347. TEST_DECL(test_dual_alg_support),
  64348. /*********************************
  64349. * OpenSSL compatibility API tests
  64350. *********************************/
  64351. /* If at some point a stub get implemented this test should fail indicating
  64352. * a need to implement a new test case
  64353. */
  64354. TEST_DECL(test_stubs_are_stubs),
  64355. /* ASN.1 compatibility API tests */
  64356. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  64357. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  64358. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  64359. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  64360. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  64361. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  64362. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  64363. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  64364. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  64365. TEST_DECL(test_wolfSSL_ASN1_get_object),
  64366. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  64367. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  64368. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  64369. TEST_DECL(test_wolfSSL_ASN1_STRING),
  64370. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  64371. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  64372. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  64373. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  64374. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  64375. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  64376. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  64377. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  64378. TEST_DECL(test_wolfSSL_ASN1_TIME),
  64379. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  64380. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  64381. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  64382. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  64383. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  64384. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  64385. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  64386. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  64387. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  64388. TEST_DECL(test_wolfSSL_lhash),
  64389. TEST_DECL(test_wolfSSL_certs),
  64390. TEST_DECL(test_wolfSSL_private_keys),
  64391. TEST_DECL(test_wolfSSL_PEM_def_callback),
  64392. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  64393. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  64394. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  64395. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  64396. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  64397. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  64398. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  64399. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  64400. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  64401. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  64402. #ifndef NO_BIO
  64403. TEST_DECL(test_wolfSSL_BIO),
  64404. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  64405. TEST_DECL(test_wolfSSL_PEM_read_bio),
  64406. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  64407. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  64408. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  64409. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  64410. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  64411. #endif
  64412. /* EVP API testing */
  64413. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  64414. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  64415. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  64416. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  64417. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  64418. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  64419. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  64420. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  64421. TEST_DECL(test_wolfSSL_EVP_shake128),
  64422. TEST_DECL(test_wolfSSL_EVP_shake256),
  64423. TEST_DECL(test_wolfSSL_EVP_sm3),
  64424. TEST_DECL(test_EVP_blake2),
  64425. #ifdef OPENSSL_ALL
  64426. TEST_DECL(test_wolfSSL_EVP_md4),
  64427. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  64428. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  64429. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  64430. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  64431. #endif
  64432. TEST_DECL(test_EVP_MD_do_all),
  64433. TEST_DECL(test_wolfSSL_EVP_MD_size),
  64434. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  64435. TEST_DECL(test_wolfSSL_EVP_Digest),
  64436. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  64437. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  64438. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  64439. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  64440. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  64441. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  64442. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  64443. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  64444. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  64445. TEST_DECL(test_wolfssl_EVP_chacha20),
  64446. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  64447. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  64448. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  64449. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  64450. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  64451. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  64452. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  64453. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  64454. #ifdef OPENSSL_ALL
  64455. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  64456. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  64457. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  64458. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  64459. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  64460. TEST_DECL(test_wolfSSL_EVP_rc4),
  64461. TEST_DECL(test_wolfSSL_EVP_enc_null),
  64462. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  64463. TEST_DECL(test_wolfSSL_EVP_mdc2),
  64464. TEST_DECL(test_evp_cipher_aes_gcm),
  64465. #endif
  64466. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  64467. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  64468. #ifdef OPENSSL_EXTRA
  64469. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  64470. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  64471. #endif
  64472. #ifdef OPENSSL_ALL
  64473. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  64474. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  64475. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  64476. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  64477. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  64478. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  64479. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  64480. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  64481. #endif
  64482. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  64483. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  64484. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  64485. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  64486. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  64487. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  64488. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  64489. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  64490. #ifndef NO_BIO
  64491. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  64492. #endif
  64493. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  64494. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  64495. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  64496. #ifndef NO_BIO
  64497. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  64498. #endif /* !NO_BIO */
  64499. #endif
  64500. #ifdef OPENSSL_ALL
  64501. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  64502. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  64503. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  64504. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  64505. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  64506. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  64507. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  64508. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  64509. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  64510. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  64511. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  64512. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  64513. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  64514. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  64515. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  64516. #endif
  64517. TEST_DECL(test_EVP_PKEY_rsa),
  64518. TEST_DECL(test_EVP_PKEY_ec),
  64519. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  64520. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  64521. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  64522. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  64523. TEST_DECL(test_EVP_PKEY_cmp),
  64524. #ifdef OPENSSL_ALL
  64525. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  64526. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  64527. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  64528. #endif
  64529. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  64530. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  64531. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  64532. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  64533. #endif
  64534. #ifndef NO_BIO
  64535. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  64536. TEST_DECL(test_wolfSSL_GetLoggingCb),
  64537. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  64538. TEST_DECL(test_wc_ERR_remove_state),
  64539. TEST_DECL(test_wc_ERR_print_errors_fp),
  64540. #endif
  64541. TEST_DECL(test_wolfSSL_configure_args),
  64542. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  64543. TEST_DECL(test_wolfSSL_set1_curves_list),
  64544. TEST_DECL(test_wolfSSL_curves_mismatch),
  64545. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  64546. TEST_DECL(test_wolfSSL_OtherName),
  64547. TEST_DECL(test_wolfSSL_FPKI),
  64548. TEST_DECL(test_wolfSSL_URI),
  64549. TEST_DECL(test_wolfSSL_TBS),
  64550. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  64551. TEST_DECL(test_X509_STORE_untrusted),
  64552. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  64553. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  64554. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  64555. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  64556. TEST_DECL(test_wolfSSL_X509_Name_canon),
  64557. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  64558. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  64559. TEST_DECL(test_wolfSSL_X509_NID),
  64560. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  64561. TEST_DECL(test_wolfSSL_get0_param),
  64562. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  64563. TEST_DECL(test_wolfSSL_set1_host),
  64564. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  64565. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  64566. TEST_DECL(test_wolfSSL_X509_STORE),
  64567. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  64568. TEST_DECL(test_X509_STORE_get0_objects),
  64569. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  64570. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  64571. TEST_DECL(test_wolfSSL_X509_STORE_set_get_crl),
  64572. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  64573. TEST_DECL(test_wolfSSL_X509_cmp_time),
  64574. TEST_DECL(test_wolfSSL_X509_time_adj),
  64575. /* X509 tests */
  64576. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  64577. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  64578. TEST_DECL(test_wolfSSL_X509_check_host),
  64579. TEST_DECL(test_wolfSSL_X509_check_email),
  64580. TEST_DECL(test_wolfSSL_X509_check_private_key),
  64581. TEST_DECL(test_wolfSSL_X509),
  64582. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  64583. TEST_DECL(test_wolfSSL_X509_sign),
  64584. TEST_DECL(test_wolfSSL_X509_sign2),
  64585. TEST_DECL(test_wolfSSL_X509_verify),
  64586. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  64587. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  64588. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  64589. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  64590. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  64591. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  64592. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  64593. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  64594. TEST_DECL(test_wolfSSL_X509_NAME),
  64595. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  64596. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  64597. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  64598. TEST_DECL(test_wolfSSL_X509_set_name),
  64599. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  64600. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  64601. TEST_DECL(test_wolfSSL_X509_set_version),
  64602. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  64603. TEST_DECL(test_wolfSSL_X509_CRL),
  64604. TEST_DECL(test_wolfSSL_i2d_X509),
  64605. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  64606. TEST_DECL(test_wolfSSL_PEM_read_X509),
  64607. TEST_DECL(test_wolfSSL_X509_check_ca),
  64608. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  64609. TEST_DECL(test_wolfSSL_X509_bad_altname),
  64610. TEST_DECL(test_wolfSSL_X509_name_match),
  64611. TEST_DECL(test_wolfSSL_X509_name_match2),
  64612. TEST_DECL(test_wolfSSL_X509_name_match3),
  64613. TEST_DECL(test_wolfSSL_X509_max_altnames),
  64614. TEST_DECL(test_wolfSSL_X509_max_name_constraints),
  64615. TEST_DECL(test_wolfSSL_make_cert),
  64616. #ifndef NO_BIO
  64617. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  64618. TEST_DECL(test_wolfSSL_X509_INFO),
  64619. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  64620. #endif
  64621. #ifdef OPENSSL_ALL
  64622. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  64623. #endif
  64624. TEST_DECL(test_wolfSSL_X509_CA_num),
  64625. TEST_DECL(test_wolfSSL_X509_get_version),
  64626. #ifndef NO_BIO
  64627. TEST_DECL(test_wolfSSL_X509_print),
  64628. TEST_DECL(test_wolfSSL_X509_CRL_print),
  64629. #endif
  64630. TEST_DECL(test_X509_get_signature_nid),
  64631. /* X509 extension testing. */
  64632. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  64633. TEST_DECL(test_wolfSSL_X509_get_ext),
  64634. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  64635. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  64636. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  64637. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  64638. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  64639. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  64640. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  64641. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  64642. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  64643. TEST_DECL(test_wolfSSL_X509V3_EXT),
  64644. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  64645. TEST_DECL(test_wolfSSL_X509_cmp),
  64646. TEST_DECL(test_GENERAL_NAME_set0_othername),
  64647. TEST_DECL(test_othername_and_SID_ext),
  64648. TEST_DECL(test_wolfSSL_dup_CA_list),
  64649. /* OpenSSL sk_X509 API test */
  64650. TEST_DECL(test_sk_X509),
  64651. /* OpenSSL sk_X509_CRL API test */
  64652. TEST_DECL(test_sk_X509_CRL),
  64653. /* OpenSSL X509 REQ API test */
  64654. TEST_DECL(test_X509_REQ),
  64655. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  64656. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  64657. TEST_DECL(test_X509_LOOKUP_add_dir),
  64658. /* RAND compatibility API */
  64659. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  64660. TEST_DECL(test_wolfSSL_RAND_bytes),
  64661. TEST_DECL(test_wolfSSL_RAND),
  64662. /* BN compatibility API */
  64663. TEST_DECL(test_wolfSSL_BN_CTX),
  64664. TEST_DECL(test_wolfSSL_BN),
  64665. TEST_DECL(test_wolfSSL_BN_init),
  64666. TEST_DECL(test_wolfSSL_BN_enc_dec),
  64667. TEST_DECL(test_wolfSSL_BN_word),
  64668. TEST_DECL(test_wolfSSL_BN_bits),
  64669. TEST_DECL(test_wolfSSL_BN_shift),
  64670. TEST_DECL(test_wolfSSL_BN_math),
  64671. TEST_DECL(test_wolfSSL_BN_math_mod),
  64672. TEST_DECL(test_wolfSSL_BN_math_other),
  64673. TEST_DECL(test_wolfSSL_BN_rand),
  64674. TEST_DECL(test_wolfSSL_BN_prime),
  64675. /* OpenSSL PKCS5 API test */
  64676. TEST_DECL(test_wolfSSL_PKCS5),
  64677. /* OpenSSL PKCS8 API test */
  64678. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  64679. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  64680. /* OpenSSL PKCS7 API test */
  64681. TEST_DECL(test_wolfssl_PKCS7),
  64682. TEST_DECL(test_wolfSSL_PKCS7_certs),
  64683. TEST_DECL(test_wolfSSL_PKCS7_sign),
  64684. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  64685. #ifndef NO_BIO
  64686. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  64687. #ifdef HAVE_SMIME
  64688. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  64689. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  64690. #endif /* HAVE_SMIME */
  64691. #endif /* !NO_BIO */
  64692. /* OpenSSL PKCS12 API test */
  64693. TEST_DECL(test_wolfSSL_PKCS12),
  64694. /* Can't memory test as callbacks use Assert. */
  64695. TEST_DECL(test_error_queue_per_thread),
  64696. TEST_DECL(test_wolfSSL_ERR_put_error),
  64697. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  64698. #ifndef NO_BIO
  64699. TEST_DECL(test_wolfSSL_ERR_print_errors),
  64700. #endif
  64701. TEST_DECL(test_OBJ_NAME_do_all),
  64702. TEST_DECL(test_wolfSSL_OBJ),
  64703. TEST_DECL(test_wolfSSL_OBJ_cmp),
  64704. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  64705. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  64706. #ifdef OPENSSL_ALL
  64707. TEST_DECL(test_wolfSSL_OBJ_ln),
  64708. TEST_DECL(test_wolfSSL_OBJ_sn),
  64709. #endif
  64710. #ifndef NO_BIO
  64711. TEST_DECL(test_wolfSSL_BIO_gets),
  64712. TEST_DECL(test_wolfSSL_BIO_puts),
  64713. TEST_DECL(test_wolfSSL_BIO_dump),
  64714. /* Can't memory test as server hangs. */
  64715. TEST_DECL(test_wolfSSL_BIO_should_retry),
  64716. TEST_DECL(test_wolfSSL_BIO_write),
  64717. TEST_DECL(test_wolfSSL_BIO_printf),
  64718. TEST_DECL(test_wolfSSL_BIO_f_md),
  64719. TEST_DECL(test_wolfSSL_BIO_up_ref),
  64720. TEST_DECL(test_wolfSSL_BIO_reset),
  64721. TEST_DECL(test_wolfSSL_BIO_get_len),
  64722. #endif
  64723. TEST_DECL(test_wolfSSL_check_domain),
  64724. TEST_DECL(test_wolfSSL_cert_cb),
  64725. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  64726. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  64727. TEST_DECL(test_wolfSSL_sigalg_info),
  64728. /* Can't memory test as tcp_connect aborts. */
  64729. TEST_DECL(test_wolfSSL_SESSION),
  64730. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  64731. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  64732. TEST_DECL(test_wolfSSL_ticket_keys),
  64733. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  64734. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  64735. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  64736. TEST_DECL(test_wolfSSL_verify_mode),
  64737. TEST_DECL(test_wolfSSL_verify_depth),
  64738. TEST_DECL(test_wolfSSL_verify_result),
  64739. TEST_DECL(test_wolfSSL_msg_callback),
  64740. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  64741. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  64742. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  64743. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  64744. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  64745. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  64746. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  64747. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  64748. TEST_DECL(test_wolfSSL_PEM_read),
  64749. TEST_DECL(test_wolfSSL_OpenSSL_version),
  64750. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  64751. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  64752. TEST_DECL(test_CONF_modules_xxx),
  64753. #ifdef OPENSSL_ALL
  64754. TEST_DECL(test_wolfSSL_TXT_DB),
  64755. TEST_DECL(test_wolfSSL_NCONF),
  64756. #endif
  64757. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  64758. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  64759. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  64760. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  64761. TEST_DECL(test_CRYPTO_THREADID_xxx),
  64762. TEST_DECL(test_ENGINE_cleanup),
  64763. /* test the no op functions for compatibility */
  64764. TEST_DECL(test_no_op_functions),
  64765. /* OpenSSL error API tests */
  64766. TEST_DECL(test_ERR_load_crypto_strings),
  64767. #ifdef OPENSSL_ALL
  64768. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  64769. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  64770. TEST_DECL(test_wolfSSL_CTX_ctrl),
  64771. #endif /* OPENSSL_ALL */
  64772. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  64773. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  64774. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  64775. /*********************************
  64776. * Crypto API tests
  64777. *********************************/
  64778. TEST_DECL(test_wolfSSL_MD4),
  64779. TEST_DECL(test_wolfSSL_MD5),
  64780. TEST_DECL(test_wolfSSL_MD5_Transform),
  64781. TEST_DECL(test_wolfSSL_SHA),
  64782. TEST_DECL(test_wolfSSL_SHA_Transform),
  64783. TEST_DECL(test_wolfSSL_SHA224),
  64784. TEST_DECL(test_wolfSSL_SHA256),
  64785. TEST_DECL(test_wolfSSL_SHA256_Transform),
  64786. TEST_DECL(test_wolfSSL_SHA512_Transform),
  64787. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  64788. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  64789. TEST_DECL(test_wolfSSL_HMAC_CTX),
  64790. TEST_DECL(test_wolfSSL_HMAC),
  64791. TEST_DECL(test_wolfSSL_CMAC),
  64792. TEST_DECL(test_wolfSSL_DES),
  64793. TEST_DECL(test_wolfSSL_DES_ncbc),
  64794. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  64795. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  64796. TEST_DECL(test_wolfSSL_AES_encrypt),
  64797. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  64798. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  64799. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  64800. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  64801. TEST_DECL(test_wolfSSL_RC4),
  64802. TEST_DECL(test_wolfSSL_RSA),
  64803. TEST_DECL(test_wolfSSL_RSA_DER),
  64804. TEST_DECL(test_wolfSSL_RSA_print),
  64805. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  64806. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  64807. TEST_DECL(test_wolfSSL_RSA_get0_key),
  64808. TEST_DECL(test_wolfSSL_RSA_meth),
  64809. TEST_DECL(test_wolfSSL_RSA_verify),
  64810. TEST_DECL(test_wolfSSL_RSA_sign),
  64811. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  64812. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  64813. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  64814. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  64815. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  64816. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  64817. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  64818. TEST_DECL(test_wolfSSL_RSA_ex_data),
  64819. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  64820. TEST_DECL(test_wolfSSL_RSA_To_Der),
  64821. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  64822. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  64823. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  64824. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  64825. TEST_DECL(test_wolfSSL_DH),
  64826. TEST_DECL(test_wolfSSL_DH_dup),
  64827. TEST_DECL(test_wolfSSL_DH_check),
  64828. TEST_DECL(test_wolfSSL_DH_prime),
  64829. TEST_DECL(test_wolfSSL_DH_1536_prime),
  64830. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  64831. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  64832. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  64833. TEST_DECL(test_wolfSSL_d2i_DHparams),
  64834. TEST_DECL(test_wolfSSL_DH_LoadDer),
  64835. TEST_DECL(test_wolfSSL_i2d_DHparams),
  64836. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  64837. TEST_DECL(test_wolfSSL_EC_GROUP),
  64838. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  64839. TEST_DECL(test_wolfSSL_EC_POINT),
  64840. TEST_DECL(test_wolfSSL_SPAKE),
  64841. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  64842. TEST_DECL(test_EC_i2d),
  64843. TEST_DECL(test_wolfSSL_EC_curve),
  64844. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  64845. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  64846. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  64847. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  64848. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  64849. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  64850. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  64851. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  64852. TEST_DECL(test_ECDSA_size_sign),
  64853. TEST_DECL(test_ECDH_compute_key),
  64854. #endif
  64855. #ifdef OPENSSL_EXTRA
  64856. TEST_DECL(test_EC25519),
  64857. TEST_DECL(test_ED25519),
  64858. TEST_DECL(test_EC448),
  64859. TEST_DECL(test_ED448),
  64860. #endif
  64861. TEST_DECL(test_DSA_do_sign_verify),
  64862. #ifdef OPENSSL_ALL
  64863. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  64864. TEST_DECL(test_wolfSSL_DSA_SIG),
  64865. #endif
  64866. TEST_DECL(test_openssl_generate_key_and_cert),
  64867. TEST_DECL(test_wolfSSL_FIPS_mode),
  64868. TEST_DECL(test_openssl_FIPS_drbg),
  64869. /*********************************
  64870. * CertManager API tests
  64871. *********************************/
  64872. TEST_DECL(test_wolfSSL_CertManagerAPI),
  64873. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  64874. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  64875. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  64876. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  64877. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  64878. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  64879. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  64880. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  64881. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  64882. TEST_DECL(test_wolfSSL_CertManagerCRL),
  64883. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  64884. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  64885. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  64886. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  64887. !defined(WOLFSSL_NO_CLIENT_AUTH))
  64888. TEST_DECL(test_various_pathlen_chains),
  64889. #endif
  64890. /*********************************
  64891. * SSL/TLS API tests
  64892. *********************************/
  64893. TEST_DECL(test_wolfSSL_Method_Allocators),
  64894. #ifndef NO_WOLFSSL_SERVER
  64895. TEST_DECL(test_wolfSSL_CTX_new),
  64896. #endif
  64897. TEST_DECL(test_server_wolfSSL_new),
  64898. TEST_DECL(test_client_wolfSSL_new),
  64899. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  64900. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  64901. TEST_DECL(test_for_double_Free),
  64902. #endif
  64903. TEST_DECL(test_wolfSSL_set_options),
  64904. #ifdef WOLFSSL_TLS13
  64905. /* TLS v1.3 API tests */
  64906. TEST_DECL(test_tls13_apis),
  64907. TEST_DECL(test_tls13_cipher_suites),
  64908. #endif
  64909. TEST_DECL(test_wolfSSL_tmp_dh),
  64910. TEST_DECL(test_wolfSSL_ctrl),
  64911. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  64912. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  64913. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  64914. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  64915. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  64916. #endif
  64917. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  64918. TEST_DECL(test_wolfSSL_security_level),
  64919. TEST_DECL(test_wolfSSL_SSL_in_init),
  64920. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  64921. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  64922. TEST_DECL(test_CONF_CTX_FILE),
  64923. TEST_DECL(test_CONF_CTX_CMDLINE),
  64924. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  64925. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  64926. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  64927. /* Bad certificate signature tests */
  64928. TEST_DECL(test_EccSigFailure_cm),
  64929. TEST_DECL(test_RsaSigFailure_cm),
  64930. #endif /* NO_CERTS */
  64931. /* PKCS8 testing */
  64932. TEST_DECL(test_wolfSSL_no_password_cb),
  64933. TEST_DECL(test_wolfSSL_PKCS8),
  64934. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  64935. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  64936. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  64937. TEST_DECL(test_wolfSSL_get_finished),
  64938. /* Uses Assert in handshake callback. */
  64939. TEST_DECL(test_wolfSSL_CTX_add_session),
  64940. /* Large number of memory allocations. */
  64941. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  64942. /* Large number of memory allocations. */
  64943. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  64944. /* Large number of memory allocations. */
  64945. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  64946. /* Large number of memory allocations. */
  64947. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  64948. /* Large number of memory allocations. */
  64949. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  64950. /* Large number of memory allocations. */
  64951. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  64952. #endif
  64953. TEST_DECL(test_SSL_CIPHER_get_xxx),
  64954. TEST_DECL(test_wolfSSL_ERR_strings),
  64955. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  64956. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  64957. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  64958. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  64959. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  64960. /* Large number of memory allocations. */
  64961. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  64962. TEST_DECL(test_wolfSSL_CertRsaPss),
  64963. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  64964. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  64965. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  64966. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  64967. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  64968. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  64969. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  64970. TEST_DECL(test_multiple_crls_same_issuer),
  64971. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  64972. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  64973. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  64974. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  64975. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  64976. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  64977. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  64978. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  64979. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  64980. TEST_DECL(test_SetTmpEC_DHE_Sz),
  64981. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  64982. #ifdef WOLFSSL_DTLS
  64983. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  64984. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  64985. #endif
  64986. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  64987. /* Uses Assert in handshake callback. */
  64988. TEST_DECL(test_wolfSSL_dtls_plaintext),
  64989. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  64990. defined(HAVE_IO_TESTS_DEPENDENCIES)
  64991. TEST_DECL(test_wolfSSL_read_write),
  64992. /* Can't memory test as server hangs if client fails before second connect.
  64993. */
  64994. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  64995. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  64996. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  64997. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  64998. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  64999. /* Can't memory test as server hangs. */
  65000. TEST_DECL(test_wolfSSL_dtls_export),
  65001. /* Uses Assert in handshake callback. */
  65002. TEST_DECL(test_wolfSSL_tls_export),
  65003. #endif
  65004. TEST_DECL(test_wolfSSL_dtls_export_peers),
  65005. TEST_DECL(test_wolfSSL_SetMinVersion),
  65006. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  65007. /* wolfSSL handshake APIs. */
  65008. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  65009. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  65010. TEST_DECL(test_wolfSSL_BUF),
  65011. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  65012. /* Can't memory test as server hangs. */
  65013. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  65014. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  65015. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  65016. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  65017. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  65018. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  65019. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  65020. /* Can't memory test as server hangs. */
  65021. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  65022. TEST_DECL(test_wolfSSL_Tls13_postauth),
  65023. TEST_DECL(test_wolfSSL_set_ecdh_auto),
  65024. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  65025. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  65026. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  65027. TEST_DECL(test_wolfSSL_THREADID_hash),
  65028. /* TLS extensions tests */
  65029. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  65030. #ifdef HAVE_SNI
  65031. TEST_DECL(test_wolfSSL_UseSNI_params),
  65032. /* Uses Assert in handshake callback. */
  65033. TEST_DECL(test_wolfSSL_UseSNI_connection),
  65034. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  65035. #endif /* HAVE_SNI */
  65036. #endif
  65037. TEST_DECL(test_wolfSSL_UseTrustedCA),
  65038. TEST_DECL(test_wolfSSL_UseMaxFragment),
  65039. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  65040. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  65041. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  65042. /* Uses Assert in handshake callback. */
  65043. TEST_DECL(test_wolfSSL_UseALPN_connection),
  65044. TEST_DECL(test_wolfSSL_UseALPN_params),
  65045. #endif
  65046. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  65047. /* Uses Assert in handshake callback. */
  65048. TEST_DECL(test_wolfSSL_set_alpn_protos),
  65049. #endif
  65050. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  65051. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  65052. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  65053. TEST_DECL(test_tls_ext_duplicate),
  65054. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  65055. defined(HAVE_IO_TESTS_DEPENDENCIES)
  65056. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  65057. /* Uses Assert in handshake callback. */
  65058. TEST_DECL(test_wolfSSL_Tls13_ECH),
  65059. #endif
  65060. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  65061. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  65062. /* OCSP Stapling */
  65063. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  65064. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  65065. TEST_DECL(test_self_signed_stapling),
  65066. /* Multicast */
  65067. TEST_DECL(test_wolfSSL_mcast),
  65068. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  65069. TEST_DECL(test_wolfSSL_msgCb),
  65070. TEST_DECL(test_wolfSSL_either_side),
  65071. TEST_DECL(test_wolfSSL_DTLS_either_side),
  65072. /* Uses Assert in handshake callback. */
  65073. TEST_DECL(test_wolfSSL_dtls_fragments),
  65074. /* Uses Assert in handshake callback. */
  65075. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  65076. /* Uses Assert in handshake callback. */
  65077. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  65078. /* Uses Assert in handshake callback. */
  65079. TEST_DECL(test_wolfSSL_dtls_bad_record),
  65080. /* Uses Assert in handshake callback. */
  65081. TEST_DECL(test_wolfSSL_dtls_stateless),
  65082. TEST_DECL(test_generate_cookie),
  65083. #ifndef NO_BIO
  65084. /* Can't memory test as server hangs. */
  65085. TEST_DECL(test_wolfSSL_BIO_connect),
  65086. /* Can't memory test as server Asserts in thread. */
  65087. TEST_DECL(test_wolfSSL_BIO_accept),
  65088. TEST_DECL(test_wolfSSL_BIO_tls),
  65089. #endif
  65090. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  65091. TEST_DECL(test_DhCallbacks),
  65092. #endif
  65093. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  65094. TEST_DECL(test_export_keying_material),
  65095. #endif
  65096. /* Can't memory test as client/server Asserts in thread. */
  65097. TEST_DECL(test_ticket_and_psk_mixing),
  65098. /* Can't memory test as client/server Asserts in thread. */
  65099. TEST_DECL(test_prioritize_psk),
  65100. /* Can't memory test as client/server hangs. */
  65101. TEST_DECL(test_wc_CryptoCb),
  65102. /* Can't memory test as client/server hangs. */
  65103. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  65104. #if !defined(NO_FILESYSTEM) && \
  65105. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  65106. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  65107. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  65108. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  65109. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  65110. #ifdef HAVE_MAX_FRAGMENT
  65111. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  65112. #endif /* HAVE_MAX_FRAGMENT */
  65113. #ifndef NO_RSA
  65114. TEST_DECL(test_wolfSSL_dtls_stateless2),
  65115. #if !defined(NO_OLD_TLS)
  65116. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  65117. #endif /* !defined(NO_OLD_TLS) */
  65118. #endif /* ! NO_RSA */
  65119. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  65120. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  65121. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  65122. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  65123. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  65124. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  65125. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  65126. && defined(WOLFSSL_TLS13) && \
  65127. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  65128. TEST_DECL(test_ticket_nonce_malloc),
  65129. #endif
  65130. TEST_DECL(test_ticket_ret_create),
  65131. TEST_DECL(test_wrong_cs_downgrade),
  65132. TEST_DECL(test_extra_alerts_wrong_cs),
  65133. TEST_DECL(test_extra_alerts_skip_hs),
  65134. TEST_DECL(test_extra_alerts_bad_psk),
  65135. TEST_DECL(test_tls13_bad_psk_binder),
  65136. /* Can't memory test as client/server Asserts. */
  65137. TEST_DECL(test_harden_no_secure_renegotiation),
  65138. TEST_DECL(test_override_alt_cert_chain),
  65139. TEST_DECL(test_rpk_set_xxx_cert_type),
  65140. TEST_DECL(test_tls13_rpk_handshake),
  65141. TEST_DECL(test_dtls13_bad_epoch_ch),
  65142. TEST_DECL(test_short_session_id),
  65143. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  65144. /* Can't memory test as client/server hangs. */
  65145. TEST_DECL(test_dtls_msg_from_other_peer),
  65146. TEST_DECL(test_dtls_ipv6_check),
  65147. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  65148. TEST_DECL(test_dtls_no_extensions),
  65149. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  65150. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  65151. TEST_DECL(test_session_ticket_no_id),
  65152. TEST_DECL(test_session_ticket_hs_update),
  65153. TEST_DECL(test_dtls_downgrade_scr_server),
  65154. TEST_DECL(test_dtls_downgrade_scr),
  65155. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  65156. TEST_DECL(test_dtls_client_hello_timeout),
  65157. TEST_DECL(test_dtls_dropped_ccs),
  65158. TEST_DECL(test_dtls_seq_num_downgrade),
  65159. TEST_DECL(test_certreq_sighash_algos),
  65160. TEST_DECL(test_revoked_loaded_int_cert),
  65161. TEST_DECL(test_dtls_frag_ch),
  65162. TEST_DECL(test_dtls13_frag_ch_pq),
  65163. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  65164. TEST_DECL(test_dtls_old_seq_number),
  65165. TEST_DECL(test_tls13_pq_groups),
  65166. TEST_DECL(test_tls13_early_data),
  65167. TEST_DECL(test_tls_multi_handshakes_one_record),
  65168. TEST_DECL(test_write_dup),
  65169. TEST_DECL(test_read_write_hs),
  65170. TEST_DECL(test_get_signature_nid),
  65171. TEST_DECL(test_tls_cert_store_unchanged),
  65172. TEST_DECL(test_wolfSSL_SendUserCanceled),
  65173. /* This test needs to stay at the end to clean up any caches allocated. */
  65174. TEST_DECL(test_wolfSSL_Cleanup)
  65175. };
  65176. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  65177. static void TestSetup(void)
  65178. {
  65179. /* Stub, for now. Add common test setup code here. */
  65180. }
  65181. static void TestCleanup(void)
  65182. {
  65183. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  65184. /* Clear any errors added to the error queue during the test run. */
  65185. wolfSSL_ERR_clear_error();
  65186. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  65187. }
  65188. /* Print out all API test cases with numeric identifier.
  65189. */
  65190. void ApiTest_PrintTestCases(void)
  65191. {
  65192. int i;
  65193. printf("All Test Cases:\n");
  65194. for (i = 0; i < TEST_CASE_CNT; i++) {
  65195. printf("%3d: %s\n", i + 1, testCases[i].name);
  65196. }
  65197. }
  65198. /* Add test case with index to the list to run.
  65199. *
  65200. * @param [in] idx Index of test case to run starting at 1.
  65201. * @return 0 on success.
  65202. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  65203. */
  65204. int ApiTest_RunIdx(int idx)
  65205. {
  65206. if (idx < 1 || idx > TEST_CASE_CNT) {
  65207. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  65208. return BAD_FUNC_ARG;
  65209. }
  65210. testAll = 0;
  65211. testCases[idx-1].run = 1;
  65212. return 0;
  65213. }
  65214. /* Add test case with name to the list to run.
  65215. *
  65216. * @param [in] name Name of test case to run.
  65217. * @return 0 on success.
  65218. * @return BAD_FUNC_ARG when name is not a known test case name.
  65219. */
  65220. int ApiTest_RunName(char* name)
  65221. {
  65222. int i;
  65223. for (i = 0; i < TEST_CASE_CNT; i++) {
  65224. if (XSTRCMP(testCases[i].name, name) == 0) {
  65225. testAll = 0;
  65226. testCases[i].run = 1;
  65227. return 0;
  65228. }
  65229. }
  65230. printf("Test case name not found: %s\n", name);
  65231. printf("Use --list to see all test case names.\n");
  65232. return BAD_FUNC_ARG;
  65233. }
  65234. /* Converts the result code to a string.
  65235. *
  65236. * @param [in] res Test result code.
  65237. * @return String describing test result.
  65238. */
  65239. static const char* apitest_res_string(int res)
  65240. {
  65241. const char* str = "invalid result";
  65242. switch (res) {
  65243. case TEST_SUCCESS:
  65244. str = "passed";
  65245. break;
  65246. case TEST_FAIL:
  65247. str = "failed";
  65248. break;
  65249. case TEST_SKIPPED:
  65250. str = "skipped";
  65251. break;
  65252. }
  65253. return str;
  65254. }
  65255. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65256. static double gettime_secs(void)
  65257. #if defined(_MSC_VER) && defined(_WIN32)
  65258. {
  65259. /* there's no gettimeofday for Windows, so we'll use system time */
  65260. #define EPOCH_DIFF 11644473600LL
  65261. FILETIME currentFileTime;
  65262. GetSystemTimePreciseAsFileTime(&currentFileTime);
  65263. ULARGE_INTEGER uli = { 0, 0 };
  65264. uli.LowPart = currentFileTime.dwLowDateTime;
  65265. uli.HighPart = currentFileTime.dwHighDateTime;
  65266. /* Convert to seconds since Unix epoch */
  65267. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  65268. }
  65269. #else
  65270. {
  65271. struct timeval tv;
  65272. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  65273. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  65274. }
  65275. #endif
  65276. #endif
  65277. int ApiTest(void)
  65278. {
  65279. int i;
  65280. int ret;
  65281. int res = 0;
  65282. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65283. double timeDiff;
  65284. #endif
  65285. printf(" Begin API Tests\n");
  65286. fflush(stdout);
  65287. /* we must perform init and cleanup if not all tests are running */
  65288. if (!testAll) {
  65289. #ifdef WOLFCRYPT_ONLY
  65290. if (wolfCrypt_Init() != 0) {
  65291. printf("wolfCrypt Initialization failed\n");
  65292. res = 1;
  65293. }
  65294. #else
  65295. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  65296. printf("wolfSSL Initialization failed\n");
  65297. res = 1;
  65298. }
  65299. #endif
  65300. }
  65301. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  65302. if (res == 0) {
  65303. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  65304. printf("failed to create tmp dir\n");
  65305. res = 1;
  65306. }
  65307. else {
  65308. tmpDirNameSet = 1;
  65309. }
  65310. }
  65311. #endif
  65312. if (res == 0) {
  65313. for (i = 0; i < TEST_CASE_CNT; ++i) {
  65314. EXPECT_DECLS;
  65315. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  65316. currentTestName = testCases[i].name;
  65317. #endif
  65318. /* When not testing all cases then skip if not marked for running.
  65319. */
  65320. if (!testAll && !testCases[i].run) {
  65321. continue;
  65322. }
  65323. TestSetup();
  65324. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  65325. fflush(stdout);
  65326. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65327. timeDiff = gettime_secs();
  65328. #endif
  65329. ret = testCases[i].func();
  65330. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65331. timeDiff = gettime_secs() - timeDiff;
  65332. #endif
  65333. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65334. if (ret != TEST_SKIPPED) {
  65335. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  65336. }
  65337. else
  65338. #endif
  65339. {
  65340. printf(" %s\n", apitest_res_string(ret));
  65341. }
  65342. fflush(stdout);
  65343. /* if return code is < 0 and not skipped then assert error */
  65344. Expect((ret > 0 || ret == TEST_SKIPPED),
  65345. ("Test failed\n"),
  65346. ("ret %d", ret));
  65347. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  65348. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  65349. TestCleanup();
  65350. }
  65351. }
  65352. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  65353. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  65354. wc_ecc_fp_free(); /* free per thread cache */
  65355. #endif
  65356. if (!testAll) {
  65357. #ifdef WOLFCRYPT_ONLY
  65358. wolfCrypt_Cleanup();
  65359. #else
  65360. wolfSSL_Cleanup();
  65361. #endif
  65362. }
  65363. (void)testDevId;
  65364. if (res != 0) {
  65365. printf("\nFAILURES:\n");
  65366. for (i = 0; i < TEST_CASE_CNT; ++i) {
  65367. if (testCases[i].fail) {
  65368. printf(" %3d: %s\n", i + 1, testCases[i].name);
  65369. }
  65370. }
  65371. printf("\n");
  65372. fflush(stdout);
  65373. }
  65374. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  65375. if (tmpDirNameSet) {
  65376. printf("\nBinary dumps of the memio streams can be found in the\n"
  65377. "%s directory. This can be imported into\n"
  65378. "Wireshark by transforming the file with\n"
  65379. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  65380. "And then loading test_output.dump.hex into Wireshark using\n"
  65381. "the \"Import from Hex Dump...\" option and selecting the\n"
  65382. "TCP encapsulation option.\n", tmpDirName);
  65383. }
  65384. #endif
  65385. printf(" End API Tests\n");
  65386. fflush(stdout);
  65387. return res;
  65388. }